From c958e26fd02d43a916ff297c89eee22166fe7be7 Mon Sep 17 00:00:00 2001 From: Scott Slack-Smith Date: Fri, 30 Jun 2017 17:12:56 +0100 Subject: [PATCH 1/8] *Silence false positive memory leaks reported by Microsoft's debug CRT* MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new RAII MemoryIsNotDeallocated class that excludes memory allocations from Microsoft’s debug CRT leak detection report. We use this RAII class to silence 2 false positive leaks that are caused by memory allocations that are intentionally never deallocated. *Background* The MS debug CRT has a lightweight memory leak detection mechanism that can only detect if a memory allocation is missing a matching deallocation. Consequently, it will report a false positive leak for memory that’s intentionally never deallocated. For example, memory that’s reachable for the entire lifetime of a app. Note the MS debug CRT is always tracking memory allocations but the final memory leak report is disabled by default. As you can’t avoid paying for its cost, you may as well use it. The memory leak report can be enabled by calling the following function #ifdef _MSC_VER _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF); #endif // _MSC_VER anywhere before exiting main. For example, the following are the false positive leaks reported before this change; Detected memory leaks! Dumping objects -> {750} normal block at 0x015DF938, 8 bytes long. Data: < ] > 00 F9 5D 01 00 00 00 00 {749} normal block at 0x015DEE60, 32 bytes long. Data: <` ] ` ] ` ] > 60 EE 5D 01 60 EE 5D 01 60 EE 5D 01 01 01 CD CD {748} normal block at 0x015DF900, 12 bytes long. Data: <8 ] ` ] > 38 F9 5D 01 60 EE 5D 01 00 00 00 00 {747} normal block at 0x015DA0F8, 24 bytes long. Data: < > FF FF FF FF FF FF FF FF 00 00 00 00 00 00 00 00 Object dump complete. As you can see from above it’s not easy to identify the above are false positives. Consequently, if false positive leaks are not fixed or silenced, then it becomes impractical to identify real memory leaks. --- googletest/src/gtest-port.cc | 52 ++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc index d80bd80c..edd115df 100644 --- a/googletest/src/gtest-port.cc +++ b/googletest/src/gtest-port.cc @@ -279,6 +279,43 @@ void Mutex::AssertHeld() { << "The current thread is not holding the mutex @" << this; } +namespace { + +// Use the RAII idiom to flag mem allocs that are intentionally never +// deallocated. The motivation is to silence the false positive mem leaks +// that are reported by the debug version of MS's CRT which can only detect +// if an alloc is missing a matching deallocation. +// Example: +// MemoryIsNotDeallocated memory_is_not_deallocated; +// critical_section_ = new CRITICAL_SECTION; +// +class MemoryIsNotDeallocated +{ +public: + MemoryIsNotDeallocated() : old_crtdbg_flag_(0) { +#ifdef _MSC_VER + old_crtdbg_flag_ = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); + // Set heap allocation block type to _IGNORE_BLOCK so that MS debug CRT + // doesn't report mem leak if there's no matching deallocation. + _CrtSetDbgFlag(old_crtdbg_flag_ & ~_CRTDBG_ALLOC_MEM_DF); +#endif // _MSC_VER + } + + ~MemoryIsNotDeallocated() { +#ifdef _MSC_VER + // Restore the original _CRTDBG_ALLOC_MEM_DF flag + _CrtSetDbgFlag(old_crtdbg_flag_); +#endif // _MSC_VER + } + +private: + int old_crtdbg_flag_; + + GTEST_DISALLOW_COPY_AND_ASSIGN_(MemoryIsNotDeallocated); +}; + +} // namespace + // Initializes owner_thread_id_ and critical_section_ in static mutexes. void Mutex::ThreadSafeLazyInit() { // Dynamic mutexes are initialized in the constructor. @@ -289,7 +326,11 @@ void Mutex::ThreadSafeLazyInit() { // If critical_section_init_phase_ was 0 before the exchange, we // are the first to test it and need to perform the initialization. owner_thread_id_ = 0; - critical_section_ = new CRITICAL_SECTION; + { + // Use RAII to flag that following mem alloc is never deallocated. + MemoryIsNotDeallocated memory_is_not_deallocated; + critical_section_ = new CRITICAL_SECTION; + } ::InitializeCriticalSection(critical_section_); // Updates the critical_section_init_phase_ to 2 to signal // initialization complete. @@ -528,10 +569,17 @@ class ThreadLocalRegistryImpl { return 0; } + // Return a newly constructed ThreadIdToThreadLocals that's intentionally never deleted + static ThreadIdToThreadLocals* NewThreadIdToThreadLocals() { + // Use RAII to flag that following mem alloc is never deallocated. + MemoryIsNotDeallocated memory_is_not_deallocated; + return new ThreadIdToThreadLocals; + } + // Returns map of thread local instances. static ThreadIdToThreadLocals* GetThreadLocalsMapLocked() { mutex_.AssertHeld(); - static ThreadIdToThreadLocals* map = new ThreadIdToThreadLocals; + static ThreadIdToThreadLocals* map = NewThreadIdToThreadLocals(); return map; } From b22e8dec408935a7f76420026a738eeb61f8af38 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Thu, 5 Apr 2018 13:38:33 +0200 Subject: [PATCH 2/8] Clean up cache non-advanced variable for subproject --- googlemock/CMakeLists.txt | 18 ++++++++++++++---- googletest/CMakeLists.txt | 25 ++++++++++++++++++++----- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/googlemock/CMakeLists.txt b/googlemock/CMakeLists.txt index bac2e3bf..7d66eb28 100644 --- a/googlemock/CMakeLists.txt +++ b/googlemock/CMakeLists.txt @@ -5,10 +5,6 @@ # ctest. You can select which tests to run using 'ctest -R regex'. # For more options, run 'ctest --help'. -# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to -# make it prominent in the GUI. -option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF) - option(gmock_build_tests "Build all of Google Mock's own tests." OFF) # A directory to find Google Test sources. @@ -55,6 +51,20 @@ endif() # if they are the same (the default). add_subdirectory("${gtest_dir}" "${gmock_BINARY_DIR}/gtest") + +# These commands only run if this is the main project +if(CMAKE_PROJECT_NAME STREQUAL "gmock" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution") + + # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to + # make it prominent in the GUI. + option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF) + +else() + + mark_as_advanced(gmock_build_tests) + +endif() + # Although Google Test's CMakeLists.txt calls this function, the # changes there don't affect the current scope. Therefore we have to # call it again here. diff --git a/googletest/CMakeLists.txt b/googletest/CMakeLists.txt index b09c46ed..2a9b9898 100644 --- a/googletest/CMakeLists.txt +++ b/googletest/CMakeLists.txt @@ -5,10 +5,6 @@ # ctest. You can select which tests to run using 'ctest -R regex'. # For more options, run 'ctest --help'. -# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to -# make it prominent in the GUI. -option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF) - # When other libraries are using a shared version of runtime libraries, # Google Test also has to use one. option( @@ -60,6 +56,25 @@ if (COMMAND set_up_hermetic_build) set_up_hermetic_build() endif() +# These commands only run if this is the main project +if(CMAKE_PROJECT_NAME STREQUAL "gtest" OR CMAKE_PROJECT_NAME STREQUAL "googletest-distribution") + + # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to + # make it prominent in the GUI. + option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." OFF) + +else() + + mark_as_advanced( + gtest_force_shared_crt + gtest_build_tests + gtest_build_samples + gtest_disable_pthreads + gtest_hide_internal_symbols) + +endif() + + if (gtest_hide_internal_symbols) set(CMAKE_CXX_VISIBILITY_PRESET hidden) set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) @@ -86,7 +101,7 @@ include_directories( if (MSVC AND MSVC_VERSION EQUAL 1700) add_definitions(/D _VARIADIC_MAX=10) endif() - + ######################################################################## # # Defines the gtest & gtest_main libraries. User tests should link From dfddc987186100c98d93a830d6ca88fa66ab3dbc Mon Sep 17 00:00:00 2001 From: tisi1988 Date: Wed, 27 Jun 2018 22:47:18 +0200 Subject: [PATCH 3/8] FIX: Compilation warning with GCC regarding a non-initialised member from MutexBase class. --- googletest/include/gtest/internal/gtest-port.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 437a4ed7..08c50494 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -2093,7 +2093,7 @@ class MutexBase { // This allows initialization to work whether pthread_t is a scalar or struct. // The flag -Wmissing-field-initializers must not be specified for this to work. # define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ - ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false } + ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false, 0 } // The Mutex class can only be used for mutexes created at runtime. It // shares its API with MutexBase otherwise. From 51b65058ad72d2d92fff89f04aad99f8bbddb147 Mon Sep 17 00:00:00 2001 From: wxf Date: Fri, 3 Aug 2018 16:23:38 +0800 Subject: [PATCH 4/8] Ignore cmake generated files when used as submodule --- .gitignore | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.gitignore b/.gitignore index 16c56e68..73cdd2c2 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,14 @@ googletest/fused-src/ # macOS files .DS_Store + +# Ignore cmake generated directories and files. +CMakeFiles +CTestTestfile.cmake +Makefile +cmake_install.cmake +googlemock/CMakeFiles +googlemock/CTestTestfile.cmake +googlemock/Makefile +googlemock/cmake_install.cmake +googlemock/gtest From ca87cc72e222a2b2ffcfac686801946451e09ef1 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 15 Aug 2018 16:38:42 -0400 Subject: [PATCH 5/8] googletest export - 208874130 Copybara tweaks, testing various round-trip sutuations(fu... by misterg - 208880646 Fix header guards and remove special case copybara by misterg - 208874252 Copybara tweaks for googletest reversible transform by misterg - 208853103 Adding a flow to export PR from GitHub into Google3 and a... by misterg - 208708150 Removing leakr-sensitive words. by misterg - 208672083 Import of OSS PR 1665 by misterg - 208663904 Remove LEAKR "author" warnings for googletest copybara ex... by misterg - 208646244 Incrementally finalizing OSS<->google3 transforms by misterg - 208548323 Move custom google3 only code to custom/ by misterg - 208234974 Removed scrubs, replaced with reversible transforms by misterg - 208211213 Move custom tests under custom by misterg - 208082996 Replace of OSS insert with reversible replace by misterg - 208072299 Replace scrubs with reversible replaces, incrementally ge... by misterg - 208059357 Replace scrub with reversible replace by misterg - 208055415 Fixing GCC brace warning that shows up in OSS with GCC8 a... by misterg - 207933728 Incrementally getting close to the reversible transformat... by misterg - 207917581 Removing stripping with replace dictionary entries to eas... by misterg - 207911026 Incremental Tweaks, on the way to reversible google3<-> g... by misterg - 207905179 Removing unnecessary comments stripping by misterg - 207901741 Fix typo in Fuchsia death-test implementation. by Abseil Team - 207776408 Move custom tests into /custom by misterg - 207746583 Remove stripping for printer for absl:variant by misterg - 207733597 Suppress default exception handling for death-test proces... by Abseil Team - 207719598 Import of googletest from Github. by misterg - 207283991 PR1673, extra parentheses in declaration cause GCC 8.1.1 ... by misterg - 206986279 Fix Duplicate definition, (original in googletest-test_te... by misterg - 206980794 Allow googletest-json-output unitest to handle supporting... by misterg - 206957064 Refactor to avoid OSS round-trip transformation problems ... by misterg - 206760733 Fixed weird syntax in these tests that was causing OSS tr... by misterg - 206750694 Tweak copybara, by misterg - 206611945 Make files consistent to enable copybara round-trip trans... by misterg - 206589404 OSS changes to open source two more tests by misterg - 206355044 Fixing copybara (was missing comment) by misterg - 206323492 Make reversible transforms possible for Copybara OSS<->go... by misterg - 206011852 Consolidate various copybara files into one file. by misterg - 205999518 remove weird char, should be space. pump and generated .h... by misterg - 205897244 Small cleanups to avoid potentially hard-to-reverse OSS t... by misterg - 205894405 Simplifying include path for tests. by misterg - 205892873 Removing obsolete files by misterg - 205873647 Simplifying include path for samples. by misterg - 205712910 Continue restructuring, will have common copybara file to... by misterg - 205711819 Removing non-ASCII chars by misterg - 205702635 Refactor internal googletest name to match OSS Name by misterg - 205403311 Comments change by misterg - 205246538 OSS community (https://github.com/google/googletest/pull/... by misterg - 205242422 Moving RE2 into custom where it rightfully belongs by misterg - 205138666 Add a 3-ary Property() matcher overload for ref-qualified... by Abseil Team - 205128154 Automated g4 rollback of changelist 205108639. by misterg - 205108639 Moving RE2 into custom where it rightfully belongs by misterg - 205102342 Comment link fix by misterg - 205097052 OSS sync, still need to worry about not C++11 by misterg - 205080271 Keeping up with the changes, ensure that the code still t... by misterg - 204815384 Mark the various RE legacy versions of the matchers as de... by Abseil Team - 204744294 OSS, someone noticed that if GTEST_HAS_EXCEPTIONS is set ... by misterg - 204363541 Add stacktrace support to the non-Google3 version of Goog... by Abseil Team - 204330832 Google Test: absl::variant is now open source, so add the by Abseil Team - 204130690 Bringing in OSS PR 1647 by misterg - 203979061 Set 'reason' field for leakr.disable_check() transformati... by Abseil Team - 203954557 Fixing comments, otherwise copybara leaves extra "//" in ... by misterg - 203487065 Correctly handle legacy regular expressions in googletest... by Abseil Team - 201997367 Remove references to GTEST_HAS_PROTOBUF_. by Abseil Team - 201735597 Upgrade gUnit from RE to RE2 -- Step 3/4 by Abseil Team - 201229160 Upgrade gUnit from RE to RE2 -- Step 1/4 by Abseil Team - 201228020 Remove extra copy of gunit samples - there should really ... by misterg - 200602156 Eliminate GTEST_TEST_FILTER_ENV_VAR_. by Abseil Team - 200500026 Make RegisterTasks faster by Abseil Team - 200361990 Add IWYU pragmas to gmock headers. by Abseil Team - 200292286 Fix speling by Abseil Team - 200222319 Adding docs to copybara. by misterg - 199815917 Fuchsia: Change fdio include path. by Abseil Team - 199195290 Remove launchpad dependency from Fuchsia. by Abseil Team - 199134849 Add printer for std::nullptr_t. by Abseil Team - 198710999 Properly decay variadic matchers by Abseil Team - 197733704 WIP - copybara script capable of google3-to-github by misterg - 197166689 Keeping up, sync cl/197012432 to combined "googletest" di... by misterg - 196253300 Keep up with changes,cl/196162435 by misterg - 195816901 go/googletest-plan by misterg - 195816542 Moving http://cl/167016557 and http://cl/195690905 into c... by misterg - 195712930 Following up for http://cl/195677772 More fixing typos, p... by misterg - 195702162 Moving http://cl/195020996 into combined dir by misterg - 195677772 Fix typos, the original IWYU was by misterg - 195249681 go/googletest-plan , Combine gUnit and gMock into third_p... by misterg PiperOrigin-RevId: 208874130 --- googlemock/include/gmock/gmock-more-matchers.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/googlemock/include/gmock/gmock-more-matchers.h b/googlemock/include/gmock/gmock-more-matchers.h index e5460e79..1c9a399a 100644 --- a/googlemock/include/gmock/gmock-more-matchers.h +++ b/googlemock/include/gmock/gmock-more-matchers.h @@ -37,8 +37,8 @@ // GOOGLETEST_CM0002 DO NOT DELETE -#ifndef GMOCK_GMOCK_MORE_MATCHERS_H_ -#define GMOCK_GMOCK_MORE_MATCHERS_H_ +#ifndef GMOCK_INCLUDE_GMOCK_MORE_MATCHERS_H_ +#define GMOCK_INCLUDE_GMOCK_MORE_MATCHERS_H_ #include "gmock/gmock-generated-matchers.h" @@ -89,4 +89,4 @@ MATCHER(IsFalse, negation ? "is true" : "is false") { } // namespace testing -#endif // GMOCK_GMOCK_MORE_MATCHERS_H_ +#endif // GMOCK_INCLUDE_GMOCK_MORE_MATCHERS_H_ From c38f4b9f2c542d16611b59e37b5e4d2ec0c8f924 Mon Sep 17 00:00:00 2001 From: Gennadiy Civil Date: Thu, 16 Aug 2018 13:18:13 -0400 Subject: [PATCH 6/8] Small style changes. Just small style changes and we can accept this PR --- googletest/src/gtest-port.cc | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc index f6bc125c..fecb5d11 100644 --- a/googletest/src/gtest-port.cc +++ b/googletest/src/gtest-port.cc @@ -296,8 +296,8 @@ void Mutex::AssertHeld() { namespace { -// Use the RAII idiom to flag mem allocs that are intentionally never -// deallocated. The motivation is to silence the false positive mem leaks +// Use the RAII idiom to flag mem allocs that are intentionally never +// deallocated. The motivation is to silence the false positive mem leaks // that are reported by the debug version of MS's CRT which can only detect // if an alloc is missing a matching deallocation. // Example: @@ -306,24 +306,24 @@ namespace { // class MemoryIsNotDeallocated { -public: + public: MemoryIsNotDeallocated() : old_crtdbg_flag_(0) { #ifdef _MSC_VER old_crtdbg_flag_ = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Set heap allocation block type to _IGNORE_BLOCK so that MS debug CRT // doesn't report mem leak if there's no matching deallocation. _CrtSetDbgFlag(old_crtdbg_flag_ & ~_CRTDBG_ALLOC_MEM_DF); -#endif // _MSC_VER +#endif // _MSC_VER } ~MemoryIsNotDeallocated() { #ifdef _MSC_VER // Restore the original _CRTDBG_ALLOC_MEM_DF flag _CrtSetDbgFlag(old_crtdbg_flag_); -#endif // _MSC_VER +#endif // _MSC_VER } -private: + private: int old_crtdbg_flag_; GTEST_DISALLOW_COPY_AND_ASSIGN_(MemoryIsNotDeallocated); @@ -584,17 +584,11 @@ class ThreadLocalRegistryImpl { return 0; } - // Return a newly constructed ThreadIdToThreadLocals that's intentionally never deleted - static ThreadIdToThreadLocals* NewThreadIdToThreadLocals() { - // Use RAII to flag that following mem alloc is never deallocated. - MemoryIsNotDeallocated memory_is_not_deallocated; - return new ThreadIdToThreadLocals; - } - // Returns map of thread local instances. static ThreadIdToThreadLocals* GetThreadLocalsMapLocked() { mutex_.AssertHeld(); - static ThreadIdToThreadLocals* map = NewThreadIdToThreadLocals(); + MemoryIsNotDeallocated memory_is_not_deallocated; + static ThreadIdToThreadLocals* map = new ThreadIdToThreadLocals(); return map; } From b1bfdf0bf48bb2288ba73bc8f423de2831e0032f Mon Sep 17 00:00:00 2001 From: Gennadiy Civil Date: Thu, 16 Aug 2018 15:10:07 -0400 Subject: [PATCH 7/8] Small formatting change And then we can merge --- googletest/include/gtest/internal/gtest-port.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 1d5b24b8..786497d8 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -2107,8 +2107,8 @@ class MutexBase { // particular, the owner_ field (a pthread_t) is not explicitly initialized. // This allows initialization to work whether pthread_t is a scalar or struct. // The flag -Wmissing-field-initializers must not be specified for this to work. -# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ - ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false, 0 } +#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ + ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0} // The Mutex class can only be used for mutexes created at runtime. It // shares its API with MutexBase otherwise. From d20fa182a60c77b58eac272b85d7f884fb650bb0 Mon Sep 17 00:00:00 2001 From: Gennadiy Civil Date: Thu, 16 Aug 2018 15:11:40 -0400 Subject: [PATCH 8/8] was not quite right, Fomatting