diff options
author | Maxim Ostapenko <m.ostapenko@samsung.com> | 2016-10-05 17:04:08 +0300 |
---|---|---|
committer | Maxim Ostapenko <m.ostapenko@samsung.com> | 2016-10-05 17:04:08 +0300 |
commit | 5d1988812d63ea43dca9eec4014946fdbad8fb78 (patch) | |
tree | 6000815d65803ee357adc40fcd3ad4ee0c3a49d2 | |
parent | 25c4be4b2e0dac020660048951266ef2cf67aac0 (diff) | |
download | linaro-gcc-5d1988812d63ea43dca9eec4014946fdbad8fb78.tar.gz linaro-gcc-5d1988812d63ea43dca9eec4014946fdbad8fb78.tar.bz2 linaro-gcc-5d1988812d63ea43dca9eec4014946fdbad8fb78.zip |
Add mmap/munmap interceptors.
Some allocators may use mmap for their local pools that may cause false positive
reports because LSan currently doesn't intercept mmap call mainly due to its
complexity (we don't know whether a particular mmap would contain live pointers).
However for some cases (e.g. anonymous rw mmaps) we can guess that they would
contain live pointers thus we can add them into root regions. Thus, now we
intercept mmap/munmap calls, add anonymous rw mappings into root regions and
call real mmap.
Change-Id: Ie0bde91497a31ab670f15d67b34d328969dc0981
Signed-off-by: Maxim Ostapenko <m.ostapenko@samsung.com>
-rw-r--r-- | libsanitizer/lsan/lsan.cc | 1 | ||||
-rw-r--r-- | libsanitizer/lsan/lsan.h | 1 | ||||
-rw-r--r-- | libsanitizer/lsan/lsan_common.cc | 11 | ||||
-rw-r--r-- | libsanitizer/lsan/lsan_interceptors.cc | 32 |
4 files changed, 41 insertions, 4 deletions
diff --git a/libsanitizer/lsan/lsan.cc b/libsanitizer/lsan/lsan.cc index 8d20bb6eb07..c1428d6fd74 100644 --- a/libsanitizer/lsan/lsan.cc +++ b/libsanitizer/lsan/lsan.cc @@ -22,6 +22,7 @@ bool lsan_inited; bool lsan_init_is_running; +bool lsan_check_in_progress; namespace __lsan { diff --git a/libsanitizer/lsan/lsan.h b/libsanitizer/lsan/lsan.h index 30f06ca40bf..ff7c291fc6d 100644 --- a/libsanitizer/lsan/lsan.h +++ b/libsanitizer/lsan/lsan.h @@ -49,5 +49,6 @@ void InitializeInterceptors(); extern bool lsan_inited; extern bool lsan_init_is_running; +extern bool lsan_check_in_progress; extern "C" void __lsan_init(); diff --git a/libsanitizer/lsan/lsan_common.cc b/libsanitizer/lsan/lsan_common.cc index 1bc0a78d1da..57b48a41b7a 100644 --- a/libsanitizer/lsan/lsan_common.cc +++ b/libsanitizer/lsan/lsan_common.cc @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +#include "lsan.h" #include "lsan_common.h" #include "sanitizer_common/sanitizer_common.h" @@ -410,6 +411,7 @@ static void CheckForLeaksCallback(const SuspendedThreadsList &suspended_threads, static bool CheckForLeaks() { if (&__lsan_is_turned_off && __lsan_is_turned_off()) return false; + lsan_check_in_progress = true; EnsureMainThreadIDIsCorrect(); CheckForLeaksParam param; param.success = false; @@ -439,8 +441,10 @@ static bool CheckForLeaks() { PrintMatchedSuppressions(); if (unsuppressed_count > 0) { param.leak_report.PrintSummary(); + lsan_check_in_progress = false; return true; } + lsan_check_in_progress = false; return false; } @@ -649,7 +653,6 @@ void __lsan_ignore_object(const void *p) { SANITIZER_INTERFACE_ATTRIBUTE void __lsan_register_root_region(const void *begin, uptr size) { #if CAN_SANITIZE_LEAKS - BlockingMutexLock l(&global_mutex); CHECK(root_regions); RootRegion region = {begin, size}; root_regions->push_back(region); @@ -660,7 +663,6 @@ void __lsan_register_root_region(const void *begin, uptr size) { SANITIZER_INTERFACE_ATTRIBUTE void __lsan_unregister_root_region(const void *begin, uptr size) { #if CAN_SANITIZE_LEAKS - BlockingMutexLock l(&global_mutex); CHECK(root_regions); bool removed = false; for (uptr i = 0; i < root_regions->size(); i++) { @@ -675,11 +677,12 @@ void __lsan_unregister_root_region(const void *begin, uptr size) { } } if (!removed) { - Report( + VReport(1, "__lsan_unregister_root_region(): region at %p of size %llu has not " "been registered.\n", begin, size); - Die(); + // Do nothing. +// Die(); } #endif // CAN_SANITIZE_LEAKS } diff --git a/libsanitizer/lsan/lsan_interceptors.cc b/libsanitizer/lsan/lsan_interceptors.cc index 60b2c8e63dc..ef791991a3e 100644 --- a/libsanitizer/lsan/lsan_interceptors.cc +++ b/libsanitizer/lsan/lsan_interceptors.cc @@ -22,6 +22,7 @@ #include "lsan_allocator.h" #include "lsan_thread.h" #include <stddef.h> +#include <sys/mman.h> using namespace __lsan; @@ -32,8 +33,12 @@ int pthread_attr_getdetachstate(void *attr, int *v); int pthread_key_create(unsigned *key, void (*destructor)(void* v)); int pthread_setspecific(unsigned key, const void *v); int __lsan_do_recoverable_leak_check(); +void __lsan_register_root_region(const void *p, size_t size); +void __lsan_unregister_root_region(const void *p, size_t size); } +BlockingMutex mmap_mutex(LINKER_INITIALIZED); + #define ENSURE_LSAN_INITED do { \ CHECK(!lsan_init_is_running); \ if (!lsan_inited) \ @@ -266,6 +271,31 @@ INTERCEPTOR(void, elm_win_lower, Elm_Win *win) { REAL(elm_win_lower)(win); } +static inline bool isInterestingMmap(int prot, int flags, int fd) { + return ((prot & PROT_WRITE) && (prot & PROT_READ)) && + ((flags & MAP_ANON) || + (flags & MAP_ANONYMOUS)) && !(flags & MAP_STACK) && + (fd == -1); +} + +INTERCEPTOR(void *, mmap, void *addr, size_t length, int prot, int flags, + int fd, off_t offset) { + void *res = REAL(mmap)(addr, length, prot, flags, fd, offset); + if (!lsan_check_in_progress && isInterestingMmap(prot, flags, fd)) { + BlockingMutexLock l(&mmap_mutex); + __lsan_register_root_region(res, length); + } + return res; +} + +INTERCEPTOR(int, munmap, void *addr, size_t length) { + if (!lsan_check_in_progress) { + BlockingMutexLock l(&mmap_mutex); + __lsan_unregister_root_region(addr, length); + } + return REAL(munmap)(addr, length); +} + namespace __lsan { void InitializeInterceptors() { @@ -284,6 +314,8 @@ void InitializeInterceptors() { INTERCEPT_FUNCTION(mallopt); INTERCEPT_FUNCTION(pthread_create); INTERCEPT_FUNCTION(pthread_join); + INTERCEPT_FUNCTION(mmap); + INTERCEPT_FUNCTION(munmap); INTERCEPT_FUNCTION(elm_win_lower); if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) { |