From 70a35422b5509a456584b132ad8ce4466af323ea Mon Sep 17 00:00:00 2001 From: Francis Ricci Date: Thu, 15 Jun 2017 11:04:33 -0400 Subject: [PATCH] Ignore current_instance heap allocation when leak sanitizer is enabled Without this patch, any user program that enables LeakSanitizer will see a leak from tcmalloc. Add a weak hook to __lsan_ignore_object, so that if LeakSanitizer is enabled, the allocation can be ignored. --- Makefile.am | 3 +++ src/malloc_extension.cc | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Makefile.am b/Makefile.am index 0aeb77d..ab9041c 100755 --- a/Makefile.am +++ b/Makefile.am @@ -79,6 +79,9 @@ TCMALLOC_FLAGS = if MINGW TCMALLOC_FLAGS += -Wl,-u__tcmalloc endif MINGW +if OSX +AM_LDFLAGS += -Wl,-U,___lsan_ignore_object +endif OSX # If we have objcopy, make malloc/free/etc weak symbols. That way folks # can override our malloc if they want to (they can still use tc_malloc). diff --git a/src/malloc_extension.cc b/src/malloc_extension.cc index 6e69552..5c98c0e 100644 --- a/src/malloc_extension.cc +++ b/src/malloc_extension.cc @@ -205,6 +205,16 @@ void MallocExtension::MarkThreadTemporarilyIdle() { static MallocExtension* current_instance; +#if (!defined(_WIN32) && !defined(__MINGW32__)) +// Provide a weak hook for __lsan_ignore_object, so that +// if leak sanitizer is enabled, we can ignore the current_instance +// heap allocation. +extern "C" void __attribute__((weak)) __lsan_ignore_object(const void *); +#else +// Weak hooks aren't supported on windows, but neither is leak sanitizer. +void (*__lsan_ignore_object)(const void *) = nullptr; +#endif + static void InitModule() { if (current_instance != NULL) { return; @@ -213,6 +223,9 @@ static void InitModule() { #ifndef NO_HEAP_CHECK HeapLeakChecker::IgnoreObject(current_instance); #endif + if (__lsan_ignore_object) { + __lsan_ignore_object(current_instance); + } } REGISTER_MODULE_INITIALIZER(malloc_extension_init, InitModule())