168d75effSDimitry Andric //=-- lsan_common.cpp -----------------------------------------------------===// 268d75effSDimitry Andric // 368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 668d75effSDimitry Andric // 768d75effSDimitry Andric //===----------------------------------------------------------------------===// 868d75effSDimitry Andric // 968d75effSDimitry Andric // This file is a part of LeakSanitizer. 1068d75effSDimitry Andric // Implementation of common leak checking functionality. 1168d75effSDimitry Andric // 1268d75effSDimitry Andric //===----------------------------------------------------------------------===// 1368d75effSDimitry Andric 1468d75effSDimitry Andric #include "lsan_common.h" 1568d75effSDimitry Andric 1668d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h" 1768d75effSDimitry Andric #include "sanitizer_common/sanitizer_flag_parser.h" 1868d75effSDimitry Andric #include "sanitizer_common/sanitizer_flags.h" 1968d75effSDimitry Andric #include "sanitizer_common/sanitizer_placement_new.h" 2068d75effSDimitry Andric #include "sanitizer_common/sanitizer_procmaps.h" 2168d75effSDimitry Andric #include "sanitizer_common/sanitizer_report_decorator.h" 2268d75effSDimitry Andric #include "sanitizer_common/sanitizer_stackdepot.h" 2368d75effSDimitry Andric #include "sanitizer_common/sanitizer_stacktrace.h" 2468d75effSDimitry Andric #include "sanitizer_common/sanitizer_suppressions.h" 2568d75effSDimitry Andric #include "sanitizer_common/sanitizer_thread_registry.h" 2668d75effSDimitry Andric #include "sanitizer_common/sanitizer_tls_get_addr.h" 2768d75effSDimitry Andric 2868d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 2968d75effSDimitry Andric namespace __lsan { 3068d75effSDimitry Andric 3168d75effSDimitry Andric // This mutex is used to prevent races between DoLeakCheck and IgnoreObject, and 3268d75effSDimitry Andric // also to protect the global list of root regions. 33349cc55cSDimitry Andric Mutex global_mutex; 3468d75effSDimitry Andric 3568d75effSDimitry Andric Flags lsan_flags; 3668d75effSDimitry Andric 3768d75effSDimitry Andric void DisableCounterUnderflow() { 3868d75effSDimitry Andric if (common_flags()->detect_leaks) { 3968d75effSDimitry Andric Report("Unmatched call to __lsan_enable().\n"); 4068d75effSDimitry Andric Die(); 4168d75effSDimitry Andric } 4268d75effSDimitry Andric } 4368d75effSDimitry Andric 4468d75effSDimitry Andric void Flags::SetDefaults() { 4568d75effSDimitry Andric # define LSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue; 4668d75effSDimitry Andric # include "lsan_flags.inc" 4768d75effSDimitry Andric # undef LSAN_FLAG 4868d75effSDimitry Andric } 4968d75effSDimitry Andric 5068d75effSDimitry Andric void RegisterLsanFlags(FlagParser *parser, Flags *f) { 5168d75effSDimitry Andric # define LSAN_FLAG(Type, Name, DefaultValue, Description) \ 5268d75effSDimitry Andric RegisterFlag(parser, #Name, Description, &f->Name); 5368d75effSDimitry Andric # include "lsan_flags.inc" 5468d75effSDimitry Andric # undef LSAN_FLAG 5568d75effSDimitry Andric } 5668d75effSDimitry Andric 5768d75effSDimitry Andric # define LOG_POINTERS(...) \ 5868d75effSDimitry Andric do { \ 590eae32dcSDimitry Andric if (flags()->log_pointers) \ 600eae32dcSDimitry Andric Report(__VA_ARGS__); \ 6168d75effSDimitry Andric } while (0) 6268d75effSDimitry Andric 6368d75effSDimitry Andric # define LOG_THREADS(...) \ 6468d75effSDimitry Andric do { \ 650eae32dcSDimitry Andric if (flags()->log_threads) \ 660eae32dcSDimitry Andric Report(__VA_ARGS__); \ 6768d75effSDimitry Andric } while (0) 6868d75effSDimitry Andric 69e8d8bef9SDimitry Andric class LeakSuppressionContext { 70e8d8bef9SDimitry Andric bool parsed = false; 71e8d8bef9SDimitry Andric SuppressionContext context; 72e8d8bef9SDimitry Andric bool suppressed_stacks_sorted = true; 73e8d8bef9SDimitry Andric InternalMmapVector<u32> suppressed_stacks; 740eae32dcSDimitry Andric const LoadedModule *suppress_module = nullptr; 75e8d8bef9SDimitry Andric 76e8d8bef9SDimitry Andric void LazyInit(); 770eae32dcSDimitry Andric Suppression *GetSuppressionForAddr(uptr addr); 780eae32dcSDimitry Andric bool SuppressInvalid(const StackTrace &stack); 790eae32dcSDimitry Andric bool SuppressByRule(const StackTrace &stack, uptr hit_count, uptr total_size); 80e8d8bef9SDimitry Andric 81e8d8bef9SDimitry Andric public: 82e8d8bef9SDimitry Andric LeakSuppressionContext(const char *supprression_types[], 83e8d8bef9SDimitry Andric int suppression_types_num) 84e8d8bef9SDimitry Andric : context(supprression_types, suppression_types_num) {} 85e8d8bef9SDimitry Andric 860eae32dcSDimitry Andric bool Suppress(u32 stack_trace_id, uptr hit_count, uptr total_size); 87e8d8bef9SDimitry Andric 88e8d8bef9SDimitry Andric const InternalMmapVector<u32> &GetSortedSuppressedStacks() { 89e8d8bef9SDimitry Andric if (!suppressed_stacks_sorted) { 90e8d8bef9SDimitry Andric suppressed_stacks_sorted = true; 91e8d8bef9SDimitry Andric SortAndDedup(suppressed_stacks); 92e8d8bef9SDimitry Andric } 93e8d8bef9SDimitry Andric return suppressed_stacks; 94e8d8bef9SDimitry Andric } 95e8d8bef9SDimitry Andric void PrintMatchedSuppressions(); 96e8d8bef9SDimitry Andric }; 97e8d8bef9SDimitry Andric 98e8d8bef9SDimitry Andric ALIGNED(64) static char suppression_placeholder[sizeof(LeakSuppressionContext)]; 99e8d8bef9SDimitry Andric static LeakSuppressionContext *suppression_ctx = nullptr; 10068d75effSDimitry Andric static const char kSuppressionLeak[] = "leak"; 10168d75effSDimitry Andric static const char *kSuppressionTypes[] = {kSuppressionLeak}; 10268d75effSDimitry Andric static const char kStdSuppressions[] = 10368d75effSDimitry Andric # if SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 10468d75effSDimitry Andric // For more details refer to the SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 10568d75effSDimitry Andric // definition. 10668d75effSDimitry Andric "leak:*pthread_exit*\n" 10768d75effSDimitry Andric # endif // SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 108*81ad6265SDimitry Andric # if SANITIZER_APPLE 10968d75effSDimitry Andric // For Darwin and os_log/os_trace: https://reviews.llvm.org/D35173 11068d75effSDimitry Andric "leak:*_os_trace*\n" 11168d75effSDimitry Andric # endif 11268d75effSDimitry Andric // TLS leak in some glibc versions, described in 11368d75effSDimitry Andric // https://sourceware.org/bugzilla/show_bug.cgi?id=12650. 11468d75effSDimitry Andric "leak:*tls_get_addr*\n"; 11568d75effSDimitry Andric 11668d75effSDimitry Andric void InitializeSuppressions() { 11768d75effSDimitry Andric CHECK_EQ(nullptr, suppression_ctx); 11868d75effSDimitry Andric suppression_ctx = new (suppression_placeholder) 119e8d8bef9SDimitry Andric LeakSuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes)); 12068d75effSDimitry Andric } 12168d75effSDimitry Andric 122e8d8bef9SDimitry Andric void LeakSuppressionContext::LazyInit() { 123e8d8bef9SDimitry Andric if (!parsed) { 124e8d8bef9SDimitry Andric parsed = true; 125e8d8bef9SDimitry Andric context.ParseFromFile(flags()->suppressions); 126e8d8bef9SDimitry Andric if (&__lsan_default_suppressions) 127e8d8bef9SDimitry Andric context.Parse(__lsan_default_suppressions()); 128e8d8bef9SDimitry Andric context.Parse(kStdSuppressions); 1290eae32dcSDimitry Andric if (flags()->use_tls && flags()->use_ld_allocations) 1300eae32dcSDimitry Andric suppress_module = GetLinker(); 131e8d8bef9SDimitry Andric } 132e8d8bef9SDimitry Andric } 133e8d8bef9SDimitry Andric 1340eae32dcSDimitry Andric Suppression *LeakSuppressionContext::GetSuppressionForAddr(uptr addr) { 1350eae32dcSDimitry Andric Suppression *s = nullptr; 1360eae32dcSDimitry Andric 1370eae32dcSDimitry Andric // Suppress by module name. 138*81ad6265SDimitry Andric const char *module_name = Symbolizer::GetOrInit()->GetModuleNameForPc(addr); 139*81ad6265SDimitry Andric if (!module_name) 140*81ad6265SDimitry Andric module_name = "<unknown module>"; 1410eae32dcSDimitry Andric if (context.Match(module_name, kSuppressionLeak, &s)) 1420eae32dcSDimitry Andric return s; 1430eae32dcSDimitry Andric 1440eae32dcSDimitry Andric // Suppress by file or function name. 1450eae32dcSDimitry Andric SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(addr); 1460eae32dcSDimitry Andric for (SymbolizedStack *cur = frames; cur; cur = cur->next) { 1470eae32dcSDimitry Andric if (context.Match(cur->info.function, kSuppressionLeak, &s) || 1480eae32dcSDimitry Andric context.Match(cur->info.file, kSuppressionLeak, &s)) { 1490eae32dcSDimitry Andric break; 1500eae32dcSDimitry Andric } 1510eae32dcSDimitry Andric } 1520eae32dcSDimitry Andric frames->ClearAll(); 1530eae32dcSDimitry Andric return s; 1540eae32dcSDimitry Andric } 1550eae32dcSDimitry Andric 1560eae32dcSDimitry Andric static uptr GetCallerPC(const StackTrace &stack) { 1570eae32dcSDimitry Andric // The top frame is our malloc/calloc/etc. The next frame is the caller. 1580eae32dcSDimitry Andric if (stack.size >= 2) 1590eae32dcSDimitry Andric return stack.trace[1]; 1600eae32dcSDimitry Andric return 0; 1610eae32dcSDimitry Andric } 1620eae32dcSDimitry Andric 1630eae32dcSDimitry Andric // On Linux, treats all chunks allocated from ld-linux.so as reachable, which 1640eae32dcSDimitry Andric // covers dynamically allocated TLS blocks, internal dynamic loader's loaded 1650eae32dcSDimitry Andric // modules accounting etc. 1660eae32dcSDimitry Andric // Dynamic TLS blocks contain the TLS variables of dynamically loaded modules. 1670eae32dcSDimitry Andric // They are allocated with a __libc_memalign() call in allocate_and_init() 1680eae32dcSDimitry Andric // (elf/dl-tls.c). Glibc won't tell us the address ranges occupied by those 1690eae32dcSDimitry Andric // blocks, but we can make sure they come from our own allocator by intercepting 1700eae32dcSDimitry Andric // __libc_memalign(). On top of that, there is no easy way to reach them. Their 1710eae32dcSDimitry Andric // addresses are stored in a dynamically allocated array (the DTV) which is 1720eae32dcSDimitry Andric // referenced from the static TLS. Unfortunately, we can't just rely on the DTV 1730eae32dcSDimitry Andric // being reachable from the static TLS, and the dynamic TLS being reachable from 1740eae32dcSDimitry Andric // the DTV. This is because the initial DTV is allocated before our interception 1750eae32dcSDimitry Andric // mechanism kicks in, and thus we don't recognize it as allocated memory. We 1760eae32dcSDimitry Andric // can't special-case it either, since we don't know its size. 1770eae32dcSDimitry Andric // Our solution is to include in the root set all allocations made from 1780eae32dcSDimitry Andric // ld-linux.so (which is where allocate_and_init() is implemented). This is 1790eae32dcSDimitry Andric // guaranteed to include all dynamic TLS blocks (and possibly other allocations 1800eae32dcSDimitry Andric // which we don't care about). 1810eae32dcSDimitry Andric // On all other platforms, this simply checks to ensure that the caller pc is 1820eae32dcSDimitry Andric // valid before reporting chunks as leaked. 1830eae32dcSDimitry Andric bool LeakSuppressionContext::SuppressInvalid(const StackTrace &stack) { 1840eae32dcSDimitry Andric uptr caller_pc = GetCallerPC(stack); 1850eae32dcSDimitry Andric // If caller_pc is unknown, this chunk may be allocated in a coroutine. Mark 1860eae32dcSDimitry Andric // it as reachable, as we can't properly report its allocation stack anyway. 1870eae32dcSDimitry Andric return !caller_pc || 1880eae32dcSDimitry Andric (suppress_module && suppress_module->containsAddress(caller_pc)); 1890eae32dcSDimitry Andric } 1900eae32dcSDimitry Andric 1910eae32dcSDimitry Andric bool LeakSuppressionContext::SuppressByRule(const StackTrace &stack, 1920eae32dcSDimitry Andric uptr hit_count, uptr total_size) { 1930eae32dcSDimitry Andric for (uptr i = 0; i < stack.size; i++) { 1940eae32dcSDimitry Andric Suppression *s = GetSuppressionForAddr( 1950eae32dcSDimitry Andric StackTrace::GetPreviousInstructionPc(stack.trace[i])); 1960eae32dcSDimitry Andric if (s) { 1970eae32dcSDimitry Andric s->weight += total_size; 1980eae32dcSDimitry Andric atomic_fetch_add(&s->hit_count, hit_count, memory_order_relaxed); 1990eae32dcSDimitry Andric return true; 2000eae32dcSDimitry Andric } 2010eae32dcSDimitry Andric } 2020eae32dcSDimitry Andric return false; 2030eae32dcSDimitry Andric } 2040eae32dcSDimitry Andric 2050eae32dcSDimitry Andric bool LeakSuppressionContext::Suppress(u32 stack_trace_id, uptr hit_count, 2060eae32dcSDimitry Andric uptr total_size) { 2070eae32dcSDimitry Andric LazyInit(); 2080eae32dcSDimitry Andric StackTrace stack = StackDepotGet(stack_trace_id); 2090eae32dcSDimitry Andric if (!SuppressInvalid(stack) && !SuppressByRule(stack, hit_count, total_size)) 2100eae32dcSDimitry Andric return false; 2110eae32dcSDimitry Andric suppressed_stacks_sorted = false; 2120eae32dcSDimitry Andric suppressed_stacks.push_back(stack_trace_id); 2130eae32dcSDimitry Andric return true; 2140eae32dcSDimitry Andric } 2150eae32dcSDimitry Andric 216e8d8bef9SDimitry Andric static LeakSuppressionContext *GetSuppressionContext() { 21768d75effSDimitry Andric CHECK(suppression_ctx); 21868d75effSDimitry Andric return suppression_ctx; 21968d75effSDimitry Andric } 22068d75effSDimitry Andric 221349cc55cSDimitry Andric static InternalMmapVectorNoCtor<RootRegion> root_regions; 22268d75effSDimitry Andric 223349cc55cSDimitry Andric InternalMmapVectorNoCtor<RootRegion> const *GetRootRegions() { 224349cc55cSDimitry Andric return &root_regions; 22568d75effSDimitry Andric } 22668d75effSDimitry Andric 22768d75effSDimitry Andric void InitCommonLsan() { 22868d75effSDimitry Andric if (common_flags()->detect_leaks) { 22968d75effSDimitry Andric // Initialization which can fail or print warnings should only be done if 23068d75effSDimitry Andric // LSan is actually enabled. 23168d75effSDimitry Andric InitializeSuppressions(); 23268d75effSDimitry Andric InitializePlatformSpecificModules(); 23368d75effSDimitry Andric } 23468d75effSDimitry Andric } 23568d75effSDimitry Andric 23668d75effSDimitry Andric class Decorator : public __sanitizer::SanitizerCommonDecorator { 23768d75effSDimitry Andric public: 23868d75effSDimitry Andric Decorator() : SanitizerCommonDecorator() {} 23968d75effSDimitry Andric const char *Error() { return Red(); } 24068d75effSDimitry Andric const char *Leak() { return Blue(); } 24168d75effSDimitry Andric }; 24268d75effSDimitry Andric 243*81ad6265SDimitry Andric static inline bool MaybeUserPointer(uptr p) { 24468d75effSDimitry Andric // Since our heap is located in mmap-ed memory, we can assume a sensible lower 24568d75effSDimitry Andric // bound on heap addresses. 24668d75effSDimitry Andric const uptr kMinAddress = 4 * 4096; 2470eae32dcSDimitry Andric if (p < kMinAddress) 2480eae32dcSDimitry Andric return false; 24968d75effSDimitry Andric # if defined(__x86_64__) 25068d75effSDimitry Andric // Accept only canonical form user-space addresses. 25168d75effSDimitry Andric return ((p >> 47) == 0); 25268d75effSDimitry Andric # elif defined(__mips64) 25368d75effSDimitry Andric return ((p >> 40) == 0); 25468d75effSDimitry Andric # elif defined(__aarch64__) 255*81ad6265SDimitry Andric // Accept up to 48 bit VMA. 256*81ad6265SDimitry Andric return ((p >> 48) == 0); 25768d75effSDimitry Andric # else 25868d75effSDimitry Andric return true; 25968d75effSDimitry Andric # endif 26068d75effSDimitry Andric } 26168d75effSDimitry Andric 26268d75effSDimitry Andric // Scans the memory range, looking for byte patterns that point into allocator 26368d75effSDimitry Andric // chunks. Marks those chunks with |tag| and adds them to |frontier|. 26468d75effSDimitry Andric // There are two usage modes for this function: finding reachable chunks 26568d75effSDimitry Andric // (|tag| = kReachable) and finding indirectly leaked chunks 26668d75effSDimitry Andric // (|tag| = kIndirectlyLeaked). In the second case, there's no flood fill, 26768d75effSDimitry Andric // so |frontier| = 0. 2680eae32dcSDimitry Andric void ScanRangeForPointers(uptr begin, uptr end, Frontier *frontier, 26968d75effSDimitry Andric const char *region_type, ChunkTag tag) { 27068d75effSDimitry Andric CHECK(tag == kReachable || tag == kIndirectlyLeaked); 27168d75effSDimitry Andric const uptr alignment = flags()->pointer_alignment(); 272349cc55cSDimitry Andric LOG_POINTERS("Scanning %s range %p-%p.\n", region_type, (void *)begin, 273349cc55cSDimitry Andric (void *)end); 27468d75effSDimitry Andric uptr pp = begin; 27568d75effSDimitry Andric if (pp % alignment) 27668d75effSDimitry Andric pp = pp + alignment - pp % alignment; 27768d75effSDimitry Andric for (; pp + sizeof(void *) <= end; pp += alignment) { 27868d75effSDimitry Andric void *p = *reinterpret_cast<void **>(pp); 279*81ad6265SDimitry Andric if (!MaybeUserPointer(reinterpret_cast<uptr>(p))) 2800eae32dcSDimitry Andric continue; 28168d75effSDimitry Andric uptr chunk = PointsIntoChunk(p); 2820eae32dcSDimitry Andric if (!chunk) 2830eae32dcSDimitry Andric continue; 28468d75effSDimitry Andric // Pointers to self don't count. This matters when tag == kIndirectlyLeaked. 2850eae32dcSDimitry Andric if (chunk == begin) 2860eae32dcSDimitry Andric continue; 28768d75effSDimitry Andric LsanMetadata m(chunk); 2880eae32dcSDimitry Andric if (m.tag() == kReachable || m.tag() == kIgnored) 2890eae32dcSDimitry Andric continue; 29068d75effSDimitry Andric 29168d75effSDimitry Andric // Do this check relatively late so we can log only the interesting cases. 29268d75effSDimitry Andric if (!flags()->use_poisoned && WordIsPoisoned(pp)) { 29368d75effSDimitry Andric LOG_POINTERS( 29468d75effSDimitry Andric "%p is poisoned: ignoring %p pointing into chunk %p-%p of size " 29568d75effSDimitry Andric "%zu.\n", 296349cc55cSDimitry Andric (void *)pp, p, (void *)chunk, (void *)(chunk + m.requested_size()), 297349cc55cSDimitry Andric m.requested_size()); 29868d75effSDimitry Andric continue; 29968d75effSDimitry Andric } 30068d75effSDimitry Andric 30168d75effSDimitry Andric m.set_tag(tag); 302349cc55cSDimitry Andric LOG_POINTERS("%p: found %p pointing into chunk %p-%p of size %zu.\n", 303349cc55cSDimitry Andric (void *)pp, p, (void *)chunk, 304349cc55cSDimitry Andric (void *)(chunk + m.requested_size()), m.requested_size()); 30568d75effSDimitry Andric if (frontier) 30668d75effSDimitry Andric frontier->push_back(chunk); 30768d75effSDimitry Andric } 30868d75effSDimitry Andric } 30968d75effSDimitry Andric 31068d75effSDimitry Andric // Scans a global range for pointers 31168d75effSDimitry Andric void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier) { 31268d75effSDimitry Andric uptr allocator_begin = 0, allocator_end = 0; 31368d75effSDimitry Andric GetAllocatorGlobalRange(&allocator_begin, &allocator_end); 31468d75effSDimitry Andric if (begin <= allocator_begin && allocator_begin < end) { 31568d75effSDimitry Andric CHECK_LE(allocator_begin, allocator_end); 31668d75effSDimitry Andric CHECK_LE(allocator_end, end); 31768d75effSDimitry Andric if (begin < allocator_begin) 31868d75effSDimitry Andric ScanRangeForPointers(begin, allocator_begin, frontier, "GLOBAL", 31968d75effSDimitry Andric kReachable); 32068d75effSDimitry Andric if (allocator_end < end) 32168d75effSDimitry Andric ScanRangeForPointers(allocator_end, end, frontier, "GLOBAL", kReachable); 32268d75effSDimitry Andric } else { 32368d75effSDimitry Andric ScanRangeForPointers(begin, end, frontier, "GLOBAL", kReachable); 32468d75effSDimitry Andric } 32568d75effSDimitry Andric } 32668d75effSDimitry Andric 32768d75effSDimitry Andric void ForEachExtraStackRangeCb(uptr begin, uptr end, void *arg) { 32868d75effSDimitry Andric Frontier *frontier = reinterpret_cast<Frontier *>(arg); 32968d75effSDimitry Andric ScanRangeForPointers(begin, end, frontier, "FAKE STACK", kReachable); 33068d75effSDimitry Andric } 33168d75effSDimitry Andric 3325ffd83dbSDimitry Andric # if SANITIZER_FUCHSIA 3335ffd83dbSDimitry Andric 3345ffd83dbSDimitry Andric // Fuchsia handles all threads together with its own callback. 3355ffd83dbSDimitry Andric static void ProcessThreads(SuspendedThreadsList const &, Frontier *) {} 3365ffd83dbSDimitry Andric 3375ffd83dbSDimitry Andric # else 3385ffd83dbSDimitry Andric 339e8d8bef9SDimitry Andric # if SANITIZER_ANDROID 340e8d8bef9SDimitry Andric // FIXME: Move this out into *libcdep.cpp 341e8d8bef9SDimitry Andric extern "C" SANITIZER_WEAK_ATTRIBUTE void __libc_iterate_dynamic_tls( 342e8d8bef9SDimitry Andric pid_t, void (*cb)(void *, void *, uptr, void *), void *); 343e8d8bef9SDimitry Andric # endif 344e8d8bef9SDimitry Andric 345e8d8bef9SDimitry Andric static void ProcessThreadRegistry(Frontier *frontier) { 346e8d8bef9SDimitry Andric InternalMmapVector<uptr> ptrs; 347e8d8bef9SDimitry Andric GetThreadRegistryLocked()->RunCallbackForEachThreadLocked( 348e8d8bef9SDimitry Andric GetAdditionalThreadContextPtrs, &ptrs); 349e8d8bef9SDimitry Andric 350e8d8bef9SDimitry Andric for (uptr i = 0; i < ptrs.size(); ++i) { 351e8d8bef9SDimitry Andric void *ptr = reinterpret_cast<void *>(ptrs[i]); 352e8d8bef9SDimitry Andric uptr chunk = PointsIntoChunk(ptr); 353e8d8bef9SDimitry Andric if (!chunk) 354e8d8bef9SDimitry Andric continue; 355e8d8bef9SDimitry Andric LsanMetadata m(chunk); 356e8d8bef9SDimitry Andric if (!m.allocated()) 357e8d8bef9SDimitry Andric continue; 358e8d8bef9SDimitry Andric 359e8d8bef9SDimitry Andric // Mark as reachable and add to frontier. 360e8d8bef9SDimitry Andric LOG_POINTERS("Treating pointer %p from ThreadContext as reachable\n", ptr); 361e8d8bef9SDimitry Andric m.set_tag(kReachable); 362e8d8bef9SDimitry Andric frontier->push_back(chunk); 363e8d8bef9SDimitry Andric } 364e8d8bef9SDimitry Andric } 365e8d8bef9SDimitry Andric 36668d75effSDimitry Andric // Scans thread data (stacks and TLS) for heap pointers. 36768d75effSDimitry Andric static void ProcessThreads(SuspendedThreadsList const &suspended_threads, 36868d75effSDimitry Andric Frontier *frontier) { 369e8d8bef9SDimitry Andric InternalMmapVector<uptr> registers; 37068d75effSDimitry Andric for (uptr i = 0; i < suspended_threads.ThreadCount(); i++) { 37168d75effSDimitry Andric tid_t os_id = static_cast<tid_t>(suspended_threads.GetThreadID(i)); 372349cc55cSDimitry Andric LOG_THREADS("Processing thread %llu.\n", os_id); 37368d75effSDimitry Andric uptr stack_begin, stack_end, tls_begin, tls_end, cache_begin, cache_end; 37468d75effSDimitry Andric DTLS *dtls; 3750eae32dcSDimitry Andric bool thread_found = 3760eae32dcSDimitry Andric GetThreadRangesLocked(os_id, &stack_begin, &stack_end, &tls_begin, 3770eae32dcSDimitry Andric &tls_end, &cache_begin, &cache_end, &dtls); 37868d75effSDimitry Andric if (!thread_found) { 37968d75effSDimitry Andric // If a thread can't be found in the thread registry, it's probably in the 38068d75effSDimitry Andric // process of destruction. Log this event and move on. 381349cc55cSDimitry Andric LOG_THREADS("Thread %llu not found in registry.\n", os_id); 38268d75effSDimitry Andric continue; 38368d75effSDimitry Andric } 38468d75effSDimitry Andric uptr sp; 38568d75effSDimitry Andric PtraceRegistersStatus have_registers = 386e8d8bef9SDimitry Andric suspended_threads.GetRegistersAndSP(i, ®isters, &sp); 38768d75effSDimitry Andric if (have_registers != REGISTERS_AVAILABLE) { 388349cc55cSDimitry Andric Report("Unable to get registers from thread %llu.\n", os_id); 38968d75effSDimitry Andric // If unable to get SP, consider the entire stack to be reachable unless 39068d75effSDimitry Andric // GetRegistersAndSP failed with ESRCH. 3910eae32dcSDimitry Andric if (have_registers == REGISTERS_UNAVAILABLE_FATAL) 3920eae32dcSDimitry Andric continue; 39368d75effSDimitry Andric sp = stack_begin; 39468d75effSDimitry Andric } 39568d75effSDimitry Andric 396e8d8bef9SDimitry Andric if (flags()->use_registers && have_registers) { 397e8d8bef9SDimitry Andric uptr registers_begin = reinterpret_cast<uptr>(registers.data()); 398e8d8bef9SDimitry Andric uptr registers_end = 399e8d8bef9SDimitry Andric reinterpret_cast<uptr>(registers.data() + registers.size()); 40068d75effSDimitry Andric ScanRangeForPointers(registers_begin, registers_end, frontier, 40168d75effSDimitry Andric "REGISTERS", kReachable); 402e8d8bef9SDimitry Andric } 40368d75effSDimitry Andric 40468d75effSDimitry Andric if (flags()->use_stacks) { 405349cc55cSDimitry Andric LOG_THREADS("Stack at %p-%p (SP = %p).\n", (void *)stack_begin, 406349cc55cSDimitry Andric (void *)stack_end, (void *)sp); 40768d75effSDimitry Andric if (sp < stack_begin || sp >= stack_end) { 40868d75effSDimitry Andric // SP is outside the recorded stack range (e.g. the thread is running a 40968d75effSDimitry Andric // signal handler on alternate stack, or swapcontext was used). 41068d75effSDimitry Andric // Again, consider the entire stack range to be reachable. 41168d75effSDimitry Andric LOG_THREADS("WARNING: stack pointer not in stack range.\n"); 41268d75effSDimitry Andric uptr page_size = GetPageSizeCached(); 41368d75effSDimitry Andric int skipped = 0; 41468d75effSDimitry Andric while (stack_begin < stack_end && 41568d75effSDimitry Andric !IsAccessibleMemoryRange(stack_begin, 1)) { 41668d75effSDimitry Andric skipped++; 41768d75effSDimitry Andric stack_begin += page_size; 41868d75effSDimitry Andric } 41968d75effSDimitry Andric LOG_THREADS("Skipped %d guard page(s) to obtain stack %p-%p.\n", 420349cc55cSDimitry Andric skipped, (void *)stack_begin, (void *)stack_end); 42168d75effSDimitry Andric } else { 42268d75effSDimitry Andric // Shrink the stack range to ignore out-of-scope values. 42368d75effSDimitry Andric stack_begin = sp; 42468d75effSDimitry Andric } 42568d75effSDimitry Andric ScanRangeForPointers(stack_begin, stack_end, frontier, "STACK", 42668d75effSDimitry Andric kReachable); 42768d75effSDimitry Andric ForEachExtraStackRange(os_id, ForEachExtraStackRangeCb, frontier); 42868d75effSDimitry Andric } 42968d75effSDimitry Andric 43068d75effSDimitry Andric if (flags()->use_tls) { 43168d75effSDimitry Andric if (tls_begin) { 432349cc55cSDimitry Andric LOG_THREADS("TLS at %p-%p.\n", (void *)tls_begin, (void *)tls_end); 43368d75effSDimitry Andric // If the tls and cache ranges don't overlap, scan full tls range, 43468d75effSDimitry Andric // otherwise, only scan the non-overlapping portions 43568d75effSDimitry Andric if (cache_begin == cache_end || tls_end < cache_begin || 43668d75effSDimitry Andric tls_begin > cache_end) { 43768d75effSDimitry Andric ScanRangeForPointers(tls_begin, tls_end, frontier, "TLS", kReachable); 43868d75effSDimitry Andric } else { 43968d75effSDimitry Andric if (tls_begin < cache_begin) 44068d75effSDimitry Andric ScanRangeForPointers(tls_begin, cache_begin, frontier, "TLS", 44168d75effSDimitry Andric kReachable); 44268d75effSDimitry Andric if (tls_end > cache_end) 44368d75effSDimitry Andric ScanRangeForPointers(cache_end, tls_end, frontier, "TLS", 44468d75effSDimitry Andric kReachable); 44568d75effSDimitry Andric } 44668d75effSDimitry Andric } 447e8d8bef9SDimitry Andric # if SANITIZER_ANDROID 448e8d8bef9SDimitry Andric auto *cb = +[](void *dtls_begin, void *dtls_end, uptr /*dso_idd*/, 449e8d8bef9SDimitry Andric void *arg) -> void { 450e8d8bef9SDimitry Andric ScanRangeForPointers(reinterpret_cast<uptr>(dtls_begin), 451e8d8bef9SDimitry Andric reinterpret_cast<uptr>(dtls_end), 452e8d8bef9SDimitry Andric reinterpret_cast<Frontier *>(arg), "DTLS", 453e8d8bef9SDimitry Andric kReachable); 454e8d8bef9SDimitry Andric }; 455e8d8bef9SDimitry Andric 456e8d8bef9SDimitry Andric // FIXME: There might be a race-condition here (and in Bionic) if the 457e8d8bef9SDimitry Andric // thread is suspended in the middle of updating its DTLS. IOWs, we 458e8d8bef9SDimitry Andric // could scan already freed memory. (probably fine for now) 459e8d8bef9SDimitry Andric __libc_iterate_dynamic_tls(os_id, cb, frontier); 460e8d8bef9SDimitry Andric # else 46168d75effSDimitry Andric if (dtls && !DTLSInDestruction(dtls)) { 462e8d8bef9SDimitry Andric ForEachDVT(dtls, [&](const DTLS::DTV &dtv, int id) { 463e8d8bef9SDimitry Andric uptr dtls_beg = dtv.beg; 464e8d8bef9SDimitry Andric uptr dtls_end = dtls_beg + dtv.size; 46568d75effSDimitry Andric if (dtls_beg < dtls_end) { 466349cc55cSDimitry Andric LOG_THREADS("DTLS %d at %p-%p.\n", id, (void *)dtls_beg, 467349cc55cSDimitry Andric (void *)dtls_end); 46868d75effSDimitry Andric ScanRangeForPointers(dtls_beg, dtls_end, frontier, "DTLS", 46968d75effSDimitry Andric kReachable); 47068d75effSDimitry Andric } 471e8d8bef9SDimitry Andric }); 47268d75effSDimitry Andric } else { 47368d75effSDimitry Andric // We are handling a thread with DTLS under destruction. Log about 47468d75effSDimitry Andric // this and continue. 475349cc55cSDimitry Andric LOG_THREADS("Thread %llu has DTLS under destruction.\n", os_id); 47668d75effSDimitry Andric } 477e8d8bef9SDimitry Andric # endif 47868d75effSDimitry Andric } 47968d75effSDimitry Andric } 480e8d8bef9SDimitry Andric 481e8d8bef9SDimitry Andric // Add pointers reachable from ThreadContexts 482e8d8bef9SDimitry Andric ProcessThreadRegistry(frontier); 48368d75effSDimitry Andric } 48468d75effSDimitry Andric 4855ffd83dbSDimitry Andric # endif // SANITIZER_FUCHSIA 4865ffd83dbSDimitry Andric 48768d75effSDimitry Andric void ScanRootRegion(Frontier *frontier, const RootRegion &root_region, 48868d75effSDimitry Andric uptr region_begin, uptr region_end, bool is_readable) { 48968d75effSDimitry Andric uptr intersection_begin = Max(root_region.begin, region_begin); 49068d75effSDimitry Andric uptr intersection_end = Min(region_end, root_region.begin + root_region.size); 4910eae32dcSDimitry Andric if (intersection_begin >= intersection_end) 4920eae32dcSDimitry Andric return; 49368d75effSDimitry Andric LOG_POINTERS("Root region %p-%p intersects with mapped region %p-%p (%s)\n", 494349cc55cSDimitry Andric (void *)root_region.begin, 495349cc55cSDimitry Andric (void *)(root_region.begin + root_region.size), 496349cc55cSDimitry Andric (void *)region_begin, (void *)region_end, 49768d75effSDimitry Andric is_readable ? "readable" : "unreadable"); 49868d75effSDimitry Andric if (is_readable) 49968d75effSDimitry Andric ScanRangeForPointers(intersection_begin, intersection_end, frontier, "ROOT", 50068d75effSDimitry Andric kReachable); 50168d75effSDimitry Andric } 50268d75effSDimitry Andric 50368d75effSDimitry Andric static void ProcessRootRegion(Frontier *frontier, 50468d75effSDimitry Andric const RootRegion &root_region) { 50568d75effSDimitry Andric MemoryMappingLayout proc_maps(/*cache_enabled*/ true); 50668d75effSDimitry Andric MemoryMappedSegment segment; 50768d75effSDimitry Andric while (proc_maps.Next(&segment)) { 50868d75effSDimitry Andric ScanRootRegion(frontier, root_region, segment.start, segment.end, 50968d75effSDimitry Andric segment.IsReadable()); 51068d75effSDimitry Andric } 51168d75effSDimitry Andric } 51268d75effSDimitry Andric 51368d75effSDimitry Andric // Scans root regions for heap pointers. 51468d75effSDimitry Andric static void ProcessRootRegions(Frontier *frontier) { 5150eae32dcSDimitry Andric if (!flags()->use_root_regions) 5160eae32dcSDimitry Andric return; 517349cc55cSDimitry Andric for (uptr i = 0; i < root_regions.size(); i++) 518349cc55cSDimitry Andric ProcessRootRegion(frontier, root_regions[i]); 51968d75effSDimitry Andric } 52068d75effSDimitry Andric 52168d75effSDimitry Andric static void FloodFillTag(Frontier *frontier, ChunkTag tag) { 52268d75effSDimitry Andric while (frontier->size()) { 52368d75effSDimitry Andric uptr next_chunk = frontier->back(); 52468d75effSDimitry Andric frontier->pop_back(); 52568d75effSDimitry Andric LsanMetadata m(next_chunk); 52668d75effSDimitry Andric ScanRangeForPointers(next_chunk, next_chunk + m.requested_size(), frontier, 52768d75effSDimitry Andric "HEAP", tag); 52868d75effSDimitry Andric } 52968d75effSDimitry Andric } 53068d75effSDimitry Andric 53168d75effSDimitry Andric // ForEachChunk callback. If the chunk is marked as leaked, marks all chunks 53268d75effSDimitry Andric // which are reachable from it as indirectly leaked. 53368d75effSDimitry Andric static void MarkIndirectlyLeakedCb(uptr chunk, void *arg) { 53468d75effSDimitry Andric chunk = GetUserBegin(chunk); 53568d75effSDimitry Andric LsanMetadata m(chunk); 53668d75effSDimitry Andric if (m.allocated() && m.tag() != kReachable) { 53768d75effSDimitry Andric ScanRangeForPointers(chunk, chunk + m.requested_size(), 53868d75effSDimitry Andric /* frontier */ nullptr, "HEAP", kIndirectlyLeaked); 53968d75effSDimitry Andric } 54068d75effSDimitry Andric } 54168d75effSDimitry Andric 542e8d8bef9SDimitry Andric static void IgnoredSuppressedCb(uptr chunk, void *arg) { 543e8d8bef9SDimitry Andric CHECK(arg); 544e8d8bef9SDimitry Andric chunk = GetUserBegin(chunk); 545e8d8bef9SDimitry Andric LsanMetadata m(chunk); 546e8d8bef9SDimitry Andric if (!m.allocated() || m.tag() == kIgnored) 547e8d8bef9SDimitry Andric return; 548e8d8bef9SDimitry Andric 549e8d8bef9SDimitry Andric const InternalMmapVector<u32> &suppressed = 550e8d8bef9SDimitry Andric *static_cast<const InternalMmapVector<u32> *>(arg); 551e8d8bef9SDimitry Andric uptr idx = InternalLowerBound(suppressed, m.stack_trace_id()); 552e8d8bef9SDimitry Andric if (idx >= suppressed.size() || m.stack_trace_id() != suppressed[idx]) 553e8d8bef9SDimitry Andric return; 554e8d8bef9SDimitry Andric 555349cc55cSDimitry Andric LOG_POINTERS("Suppressed: chunk %p-%p of size %zu.\n", (void *)chunk, 556349cc55cSDimitry Andric (void *)(chunk + m.requested_size()), m.requested_size()); 557e8d8bef9SDimitry Andric m.set_tag(kIgnored); 558e8d8bef9SDimitry Andric } 559e8d8bef9SDimitry Andric 56068d75effSDimitry Andric // ForEachChunk callback. If chunk is marked as ignored, adds its address to 56168d75effSDimitry Andric // frontier. 56268d75effSDimitry Andric static void CollectIgnoredCb(uptr chunk, void *arg) { 56368d75effSDimitry Andric CHECK(arg); 56468d75effSDimitry Andric chunk = GetUserBegin(chunk); 56568d75effSDimitry Andric LsanMetadata m(chunk); 56668d75effSDimitry Andric if (m.allocated() && m.tag() == kIgnored) { 567349cc55cSDimitry Andric LOG_POINTERS("Ignored: chunk %p-%p of size %zu.\n", (void *)chunk, 568349cc55cSDimitry Andric (void *)(chunk + m.requested_size()), m.requested_size()); 56968d75effSDimitry Andric reinterpret_cast<Frontier *>(arg)->push_back(chunk); 57068d75effSDimitry Andric } 57168d75effSDimitry Andric } 57268d75effSDimitry Andric 57368d75effSDimitry Andric // Sets the appropriate tag on each chunk. 5745ffd83dbSDimitry Andric static void ClassifyAllChunks(SuspendedThreadsList const &suspended_threads, 5755ffd83dbSDimitry Andric Frontier *frontier) { 576e8d8bef9SDimitry Andric const InternalMmapVector<u32> &suppressed_stacks = 577e8d8bef9SDimitry Andric GetSuppressionContext()->GetSortedSuppressedStacks(); 578e8d8bef9SDimitry Andric if (!suppressed_stacks.empty()) { 579e8d8bef9SDimitry Andric ForEachChunk(IgnoredSuppressedCb, 580e8d8bef9SDimitry Andric const_cast<InternalMmapVector<u32> *>(&suppressed_stacks)); 581e8d8bef9SDimitry Andric } 5825ffd83dbSDimitry Andric ForEachChunk(CollectIgnoredCb, frontier); 5835ffd83dbSDimitry Andric ProcessGlobalRegions(frontier); 5845ffd83dbSDimitry Andric ProcessThreads(suspended_threads, frontier); 5855ffd83dbSDimitry Andric ProcessRootRegions(frontier); 5865ffd83dbSDimitry Andric FloodFillTag(frontier, kReachable); 58768d75effSDimitry Andric 58868d75effSDimitry Andric // The check here is relatively expensive, so we do this in a separate flood 58968d75effSDimitry Andric // fill. That way we can skip the check for chunks that are reachable 59068d75effSDimitry Andric // otherwise. 59168d75effSDimitry Andric LOG_POINTERS("Processing platform-specific allocations.\n"); 5925ffd83dbSDimitry Andric ProcessPlatformSpecificAllocations(frontier); 5935ffd83dbSDimitry Andric FloodFillTag(frontier, kReachable); 59468d75effSDimitry Andric 59568d75effSDimitry Andric // Iterate over leaked chunks and mark those that are reachable from other 59668d75effSDimitry Andric // leaked chunks. 59768d75effSDimitry Andric LOG_POINTERS("Scanning leaked chunks.\n"); 59868d75effSDimitry Andric ForEachChunk(MarkIndirectlyLeakedCb, nullptr); 59968d75effSDimitry Andric } 60068d75effSDimitry Andric 60168d75effSDimitry Andric // ForEachChunk callback. Resets the tags to pre-leak-check state. 60268d75effSDimitry Andric static void ResetTagsCb(uptr chunk, void *arg) { 60368d75effSDimitry Andric (void)arg; 60468d75effSDimitry Andric chunk = GetUserBegin(chunk); 60568d75effSDimitry Andric LsanMetadata m(chunk); 60668d75effSDimitry Andric if (m.allocated() && m.tag() != kIgnored) 60768d75effSDimitry Andric m.set_tag(kDirectlyLeaked); 60868d75effSDimitry Andric } 60968d75effSDimitry Andric 61068d75effSDimitry Andric // ForEachChunk callback. Aggregates information about unreachable chunks into 61168d75effSDimitry Andric // a LeakReport. 61268d75effSDimitry Andric static void CollectLeaksCb(uptr chunk, void *arg) { 61368d75effSDimitry Andric CHECK(arg); 6140eae32dcSDimitry Andric LeakedChunks *leaks = reinterpret_cast<LeakedChunks *>(arg); 61568d75effSDimitry Andric chunk = GetUserBegin(chunk); 61668d75effSDimitry Andric LsanMetadata m(chunk); 6170eae32dcSDimitry Andric if (!m.allocated()) 6180eae32dcSDimitry Andric return; 6190eae32dcSDimitry Andric if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) 6200eae32dcSDimitry Andric leaks->push_back({chunk, m.stack_trace_id(), m.requested_size(), m.tag()}); 62168d75effSDimitry Andric } 62268d75effSDimitry Andric 623e8d8bef9SDimitry Andric void LeakSuppressionContext::PrintMatchedSuppressions() { 62468d75effSDimitry Andric InternalMmapVector<Suppression *> matched; 625e8d8bef9SDimitry Andric context.GetMatched(&matched); 62668d75effSDimitry Andric if (!matched.size()) 62768d75effSDimitry Andric return; 62868d75effSDimitry Andric const char *line = "-----------------------------------------------------"; 62968d75effSDimitry Andric Printf("%s\n", line); 63068d75effSDimitry Andric Printf("Suppressions used:\n"); 63168d75effSDimitry Andric Printf(" count bytes template\n"); 632e8d8bef9SDimitry Andric for (uptr i = 0; i < matched.size(); i++) { 633e8d8bef9SDimitry Andric Printf("%7zu %10zu %s\n", 634e8d8bef9SDimitry Andric static_cast<uptr>(atomic_load_relaxed(&matched[i]->hit_count)), 635e8d8bef9SDimitry Andric matched[i]->weight, matched[i]->templ); 636e8d8bef9SDimitry Andric } 63768d75effSDimitry Andric Printf("%s\n\n", line); 63868d75effSDimitry Andric } 63968d75effSDimitry Andric 64068d75effSDimitry Andric static void ReportIfNotSuspended(ThreadContextBase *tctx, void *arg) { 64168d75effSDimitry Andric const InternalMmapVector<tid_t> &suspended_threads = 64268d75effSDimitry Andric *(const InternalMmapVector<tid_t> *)arg; 64368d75effSDimitry Andric if (tctx->status == ThreadStatusRunning) { 644e8d8bef9SDimitry Andric uptr i = InternalLowerBound(suspended_threads, tctx->os_id); 64568d75effSDimitry Andric if (i >= suspended_threads.size() || suspended_threads[i] != tctx->os_id) 646349cc55cSDimitry Andric Report( 647349cc55cSDimitry Andric "Running thread %llu was not suspended. False leaks are possible.\n", 64868d75effSDimitry Andric tctx->os_id); 64968d75effSDimitry Andric } 65068d75effSDimitry Andric } 65168d75effSDimitry Andric 6525ffd83dbSDimitry Andric # if SANITIZER_FUCHSIA 6535ffd83dbSDimitry Andric 6545ffd83dbSDimitry Andric // Fuchsia provides a libc interface that guarantees all threads are 6555ffd83dbSDimitry Andric // covered, and SuspendedThreadList is never really used. 6565ffd83dbSDimitry Andric static void ReportUnsuspendedThreads(const SuspendedThreadsList &) {} 6575ffd83dbSDimitry Andric 6585ffd83dbSDimitry Andric # else // !SANITIZER_FUCHSIA 6595ffd83dbSDimitry Andric 66068d75effSDimitry Andric static void ReportUnsuspendedThreads( 66168d75effSDimitry Andric const SuspendedThreadsList &suspended_threads) { 66268d75effSDimitry Andric InternalMmapVector<tid_t> threads(suspended_threads.ThreadCount()); 66368d75effSDimitry Andric for (uptr i = 0; i < suspended_threads.ThreadCount(); ++i) 66468d75effSDimitry Andric threads[i] = suspended_threads.GetThreadID(i); 66568d75effSDimitry Andric 66668d75effSDimitry Andric Sort(threads.data(), threads.size()); 66768d75effSDimitry Andric 66868d75effSDimitry Andric GetThreadRegistryLocked()->RunCallbackForEachThreadLocked( 66968d75effSDimitry Andric &ReportIfNotSuspended, &threads); 67068d75effSDimitry Andric } 67168d75effSDimitry Andric 6725ffd83dbSDimitry Andric # endif // !SANITIZER_FUCHSIA 6735ffd83dbSDimitry Andric 67468d75effSDimitry Andric static void CheckForLeaksCallback(const SuspendedThreadsList &suspended_threads, 67568d75effSDimitry Andric void *arg) { 67668d75effSDimitry Andric CheckForLeaksParam *param = reinterpret_cast<CheckForLeaksParam *>(arg); 67768d75effSDimitry Andric CHECK(param); 67868d75effSDimitry Andric CHECK(!param->success); 67968d75effSDimitry Andric ReportUnsuspendedThreads(suspended_threads); 6805ffd83dbSDimitry Andric ClassifyAllChunks(suspended_threads, ¶m->frontier); 6810eae32dcSDimitry Andric ForEachChunk(CollectLeaksCb, ¶m->leaks); 68268d75effSDimitry Andric // Clean up for subsequent leak checks. This assumes we did not overwrite any 68368d75effSDimitry Andric // kIgnored tags. 68468d75effSDimitry Andric ForEachChunk(ResetTagsCb, nullptr); 68568d75effSDimitry Andric param->success = true; 68668d75effSDimitry Andric } 68768d75effSDimitry Andric 688e8d8bef9SDimitry Andric static bool PrintResults(LeakReport &report) { 689e8d8bef9SDimitry Andric uptr unsuppressed_count = report.UnsuppressedLeakCount(); 690e8d8bef9SDimitry Andric if (unsuppressed_count) { 691e8d8bef9SDimitry Andric Decorator d; 692e8d8bef9SDimitry Andric Printf( 693e8d8bef9SDimitry Andric "\n" 694e8d8bef9SDimitry Andric "=================================================================" 695e8d8bef9SDimitry Andric "\n"); 696e8d8bef9SDimitry Andric Printf("%s", d.Error()); 697e8d8bef9SDimitry Andric Report("ERROR: LeakSanitizer: detected memory leaks\n"); 698e8d8bef9SDimitry Andric Printf("%s", d.Default()); 699e8d8bef9SDimitry Andric report.ReportTopLeaks(flags()->max_leaks); 700e8d8bef9SDimitry Andric } 701e8d8bef9SDimitry Andric if (common_flags()->print_suppressions) 702e8d8bef9SDimitry Andric GetSuppressionContext()->PrintMatchedSuppressions(); 703e8d8bef9SDimitry Andric if (unsuppressed_count > 0) { 704e8d8bef9SDimitry Andric report.PrintSummary(); 705e8d8bef9SDimitry Andric return true; 706e8d8bef9SDimitry Andric } 707e8d8bef9SDimitry Andric return false; 708e8d8bef9SDimitry Andric } 709e8d8bef9SDimitry Andric 71068d75effSDimitry Andric static bool CheckForLeaks() { 71168d75effSDimitry Andric if (&__lsan_is_turned_off && __lsan_is_turned_off()) 71268d75effSDimitry Andric return false; 713e8d8bef9SDimitry Andric // Inside LockStuffAndStopTheWorld we can't run symbolizer, so we can't match 714e8d8bef9SDimitry Andric // suppressions. However if a stack id was previously suppressed, it should be 715e8d8bef9SDimitry Andric // suppressed in future checks as well. 716e8d8bef9SDimitry Andric for (int i = 0;; ++i) { 71768d75effSDimitry Andric EnsureMainThreadIDIsCorrect(); 71868d75effSDimitry Andric CheckForLeaksParam param; 71968d75effSDimitry Andric LockStuffAndStopTheWorld(CheckForLeaksCallback, ¶m); 72068d75effSDimitry Andric if (!param.success) { 72168d75effSDimitry Andric Report("LeakSanitizer has encountered a fatal error.\n"); 72268d75effSDimitry Andric Report( 72368d75effSDimitry Andric "HINT: For debugging, try setting environment variable " 72468d75effSDimitry Andric "LSAN_OPTIONS=verbosity=1:log_threads=1\n"); 72568d75effSDimitry Andric Report( 726e8d8bef9SDimitry Andric "HINT: LeakSanitizer does not work under ptrace (strace, gdb, " 727e8d8bef9SDimitry Andric "etc)\n"); 72868d75effSDimitry Andric Die(); 72968d75effSDimitry Andric } 7300eae32dcSDimitry Andric LeakReport leak_report; 7310eae32dcSDimitry Andric leak_report.AddLeakedChunks(param.leaks); 7320eae32dcSDimitry Andric 733e8d8bef9SDimitry Andric // No new suppressions stacks, so rerun will not help and we can report. 7340eae32dcSDimitry Andric if (!leak_report.ApplySuppressions()) 7350eae32dcSDimitry Andric return PrintResults(leak_report); 736e8d8bef9SDimitry Andric 737e8d8bef9SDimitry Andric // No indirect leaks to report, so we are done here. 7380eae32dcSDimitry Andric if (!leak_report.IndirectUnsuppressedLeakCount()) 7390eae32dcSDimitry Andric return PrintResults(leak_report); 740e8d8bef9SDimitry Andric 741e8d8bef9SDimitry Andric if (i >= 8) { 742e8d8bef9SDimitry Andric Report("WARNING: LeakSanitizer gave up on indirect leaks suppression.\n"); 7430eae32dcSDimitry Andric return PrintResults(leak_report); 74468d75effSDimitry Andric } 745e8d8bef9SDimitry Andric 746e8d8bef9SDimitry Andric // We found a new previously unseen suppressed call stack. Rerun to make 747e8d8bef9SDimitry Andric // sure it does not hold indirect leaks. 748e8d8bef9SDimitry Andric VReport(1, "Rerun with %zu suppressed stacks.", 749e8d8bef9SDimitry Andric GetSuppressionContext()->GetSortedSuppressedStacks().size()); 75068d75effSDimitry Andric } 75168d75effSDimitry Andric } 75268d75effSDimitry Andric 75368d75effSDimitry Andric static bool has_reported_leaks = false; 75468d75effSDimitry Andric bool HasReportedLeaks() { return has_reported_leaks; } 75568d75effSDimitry Andric 75668d75effSDimitry Andric void DoLeakCheck() { 757349cc55cSDimitry Andric Lock l(&global_mutex); 75868d75effSDimitry Andric static bool already_done; 7590eae32dcSDimitry Andric if (already_done) 7600eae32dcSDimitry Andric return; 76168d75effSDimitry Andric already_done = true; 76268d75effSDimitry Andric has_reported_leaks = CheckForLeaks(); 7630eae32dcSDimitry Andric if (has_reported_leaks) 7640eae32dcSDimitry Andric HandleLeaks(); 76568d75effSDimitry Andric } 76668d75effSDimitry Andric 76768d75effSDimitry Andric static int DoRecoverableLeakCheck() { 768349cc55cSDimitry Andric Lock l(&global_mutex); 76968d75effSDimitry Andric bool have_leaks = CheckForLeaks(); 77068d75effSDimitry Andric return have_leaks ? 1 : 0; 77168d75effSDimitry Andric } 77268d75effSDimitry Andric 77368d75effSDimitry Andric void DoRecoverableLeakCheckVoid() { DoRecoverableLeakCheck(); } 77468d75effSDimitry Andric 77568d75effSDimitry Andric ///// LeakReport implementation. ///// 77668d75effSDimitry Andric 77768d75effSDimitry Andric // A hard limit on the number of distinct leaks, to avoid quadratic complexity 77868d75effSDimitry Andric // in LeakReport::AddLeakedChunk(). We don't expect to ever see this many leaks 77968d75effSDimitry Andric // in real-world applications. 7800eae32dcSDimitry Andric // FIXME: Get rid of this limit by moving logic into DedupLeaks. 78168d75effSDimitry Andric const uptr kMaxLeaksConsidered = 5000; 78268d75effSDimitry Andric 7830eae32dcSDimitry Andric void LeakReport::AddLeakedChunks(const LeakedChunks &chunks) { 7840eae32dcSDimitry Andric for (const LeakedChunk &leak : chunks) { 7850eae32dcSDimitry Andric uptr chunk = leak.chunk; 7860eae32dcSDimitry Andric u32 stack_trace_id = leak.stack_trace_id; 7870eae32dcSDimitry Andric uptr leaked_size = leak.leaked_size; 7880eae32dcSDimitry Andric ChunkTag tag = leak.tag; 78968d75effSDimitry Andric CHECK(tag == kDirectlyLeaked || tag == kIndirectlyLeaked); 790349cc55cSDimitry Andric 791349cc55cSDimitry Andric if (u32 resolution = flags()->resolution) { 792349cc55cSDimitry Andric StackTrace stack = StackDepotGet(stack_trace_id); 793349cc55cSDimitry Andric stack.size = Min(stack.size, resolution); 794349cc55cSDimitry Andric stack_trace_id = StackDepotPut(stack); 795349cc55cSDimitry Andric } 796349cc55cSDimitry Andric 79768d75effSDimitry Andric bool is_directly_leaked = (tag == kDirectlyLeaked); 79868d75effSDimitry Andric uptr i; 79968d75effSDimitry Andric for (i = 0; i < leaks_.size(); i++) { 80068d75effSDimitry Andric if (leaks_[i].stack_trace_id == stack_trace_id && 80168d75effSDimitry Andric leaks_[i].is_directly_leaked == is_directly_leaked) { 80268d75effSDimitry Andric leaks_[i].hit_count++; 80368d75effSDimitry Andric leaks_[i].total_size += leaked_size; 80468d75effSDimitry Andric break; 80568d75effSDimitry Andric } 80668d75effSDimitry Andric } 80768d75effSDimitry Andric if (i == leaks_.size()) { 8080eae32dcSDimitry Andric if (leaks_.size() == kMaxLeaksConsidered) 8090eae32dcSDimitry Andric return; 8100eae32dcSDimitry Andric Leak leak = {next_id_++, /* hit_count */ 1, 8110eae32dcSDimitry Andric leaked_size, stack_trace_id, 81268d75effSDimitry Andric is_directly_leaked, /* is_suppressed */ false}; 81368d75effSDimitry Andric leaks_.push_back(leak); 81468d75effSDimitry Andric } 81568d75effSDimitry Andric if (flags()->report_objects) { 81668d75effSDimitry Andric LeakedObject obj = {leaks_[i].id, chunk, leaked_size}; 81768d75effSDimitry Andric leaked_objects_.push_back(obj); 81868d75effSDimitry Andric } 81968d75effSDimitry Andric } 8200eae32dcSDimitry Andric } 82168d75effSDimitry Andric 82268d75effSDimitry Andric static bool LeakComparator(const Leak &leak1, const Leak &leak2) { 82368d75effSDimitry Andric if (leak1.is_directly_leaked == leak2.is_directly_leaked) 82468d75effSDimitry Andric return leak1.total_size > leak2.total_size; 82568d75effSDimitry Andric else 82668d75effSDimitry Andric return leak1.is_directly_leaked; 82768d75effSDimitry Andric } 82868d75effSDimitry Andric 82968d75effSDimitry Andric void LeakReport::ReportTopLeaks(uptr num_leaks_to_report) { 83068d75effSDimitry Andric CHECK(leaks_.size() <= kMaxLeaksConsidered); 83168d75effSDimitry Andric Printf("\n"); 83268d75effSDimitry Andric if (leaks_.size() == kMaxLeaksConsidered) 8330eae32dcSDimitry Andric Printf( 8340eae32dcSDimitry Andric "Too many leaks! Only the first %zu leaks encountered will be " 83568d75effSDimitry Andric "reported.\n", 83668d75effSDimitry Andric kMaxLeaksConsidered); 83768d75effSDimitry Andric 83868d75effSDimitry Andric uptr unsuppressed_count = UnsuppressedLeakCount(); 83968d75effSDimitry Andric if (num_leaks_to_report > 0 && num_leaks_to_report < unsuppressed_count) 84068d75effSDimitry Andric Printf("The %zu top leak(s):\n", num_leaks_to_report); 84168d75effSDimitry Andric Sort(leaks_.data(), leaks_.size(), &LeakComparator); 84268d75effSDimitry Andric uptr leaks_reported = 0; 84368d75effSDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) { 8440eae32dcSDimitry Andric if (leaks_[i].is_suppressed) 8450eae32dcSDimitry Andric continue; 84668d75effSDimitry Andric PrintReportForLeak(i); 84768d75effSDimitry Andric leaks_reported++; 8480eae32dcSDimitry Andric if (leaks_reported == num_leaks_to_report) 8490eae32dcSDimitry Andric break; 85068d75effSDimitry Andric } 85168d75effSDimitry Andric if (leaks_reported < unsuppressed_count) { 85268d75effSDimitry Andric uptr remaining = unsuppressed_count - leaks_reported; 85368d75effSDimitry Andric Printf("Omitting %zu more leak(s).\n", remaining); 85468d75effSDimitry Andric } 85568d75effSDimitry Andric } 85668d75effSDimitry Andric 85768d75effSDimitry Andric void LeakReport::PrintReportForLeak(uptr index) { 85868d75effSDimitry Andric Decorator d; 85968d75effSDimitry Andric Printf("%s", d.Leak()); 86068d75effSDimitry Andric Printf("%s leak of %zu byte(s) in %zu object(s) allocated from:\n", 86168d75effSDimitry Andric leaks_[index].is_directly_leaked ? "Direct" : "Indirect", 86268d75effSDimitry Andric leaks_[index].total_size, leaks_[index].hit_count); 86368d75effSDimitry Andric Printf("%s", d.Default()); 86468d75effSDimitry Andric 865349cc55cSDimitry Andric CHECK(leaks_[index].stack_trace_id); 866349cc55cSDimitry Andric StackDepotGet(leaks_[index].stack_trace_id).Print(); 86768d75effSDimitry Andric 86868d75effSDimitry Andric if (flags()->report_objects) { 86968d75effSDimitry Andric Printf("Objects leaked above:\n"); 87068d75effSDimitry Andric PrintLeakedObjectsForLeak(index); 87168d75effSDimitry Andric Printf("\n"); 87268d75effSDimitry Andric } 87368d75effSDimitry Andric } 87468d75effSDimitry Andric 87568d75effSDimitry Andric void LeakReport::PrintLeakedObjectsForLeak(uptr index) { 87668d75effSDimitry Andric u32 leak_id = leaks_[index].id; 87768d75effSDimitry Andric for (uptr j = 0; j < leaked_objects_.size(); j++) { 87868d75effSDimitry Andric if (leaked_objects_[j].leak_id == leak_id) 879349cc55cSDimitry Andric Printf("%p (%zu bytes)\n", (void *)leaked_objects_[j].addr, 88068d75effSDimitry Andric leaked_objects_[j].size); 88168d75effSDimitry Andric } 88268d75effSDimitry Andric } 88368d75effSDimitry Andric 88468d75effSDimitry Andric void LeakReport::PrintSummary() { 88568d75effSDimitry Andric CHECK(leaks_.size() <= kMaxLeaksConsidered); 88668d75effSDimitry Andric uptr bytes = 0, allocations = 0; 88768d75effSDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) { 8880eae32dcSDimitry Andric if (leaks_[i].is_suppressed) 8890eae32dcSDimitry Andric continue; 89068d75effSDimitry Andric bytes += leaks_[i].total_size; 89168d75effSDimitry Andric allocations += leaks_[i].hit_count; 89268d75effSDimitry Andric } 893fe6060f1SDimitry Andric InternalScopedString summary; 89468d75effSDimitry Andric summary.append("%zu byte(s) leaked in %zu allocation(s).", bytes, 89568d75effSDimitry Andric allocations); 89668d75effSDimitry Andric ReportErrorSummary(summary.data()); 89768d75effSDimitry Andric } 89868d75effSDimitry Andric 899e8d8bef9SDimitry Andric uptr LeakReport::ApplySuppressions() { 900e8d8bef9SDimitry Andric LeakSuppressionContext *suppressions = GetSuppressionContext(); 901e8d8bef9SDimitry Andric uptr new_suppressions = false; 90268d75effSDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) { 9030eae32dcSDimitry Andric if (suppressions->Suppress(leaks_[i].stack_trace_id, leaks_[i].hit_count, 9040eae32dcSDimitry Andric leaks_[i].total_size)) { 90568d75effSDimitry Andric leaks_[i].is_suppressed = true; 906e8d8bef9SDimitry Andric ++new_suppressions; 90768d75effSDimitry Andric } 90868d75effSDimitry Andric } 909e8d8bef9SDimitry Andric return new_suppressions; 91068d75effSDimitry Andric } 91168d75effSDimitry Andric 91268d75effSDimitry Andric uptr LeakReport::UnsuppressedLeakCount() { 91368d75effSDimitry Andric uptr result = 0; 91468d75effSDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) 9150eae32dcSDimitry Andric if (!leaks_[i].is_suppressed) 9160eae32dcSDimitry Andric result++; 91768d75effSDimitry Andric return result; 91868d75effSDimitry Andric } 91968d75effSDimitry Andric 920e8d8bef9SDimitry Andric uptr LeakReport::IndirectUnsuppressedLeakCount() { 921e8d8bef9SDimitry Andric uptr result = 0; 922e8d8bef9SDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) 923e8d8bef9SDimitry Andric if (!leaks_[i].is_suppressed && !leaks_[i].is_directly_leaked) 924e8d8bef9SDimitry Andric result++; 925e8d8bef9SDimitry Andric return result; 926e8d8bef9SDimitry Andric } 927e8d8bef9SDimitry Andric 92868d75effSDimitry Andric } // namespace __lsan 92968d75effSDimitry Andric #else // CAN_SANITIZE_LEAKS 93068d75effSDimitry Andric namespace __lsan { 93168d75effSDimitry Andric void InitCommonLsan() {} 93268d75effSDimitry Andric void DoLeakCheck() {} 93368d75effSDimitry Andric void DoRecoverableLeakCheckVoid() {} 93468d75effSDimitry Andric void DisableInThisThread() {} 93568d75effSDimitry Andric void EnableInThisThread() {} 9360eae32dcSDimitry Andric } // namespace __lsan 93768d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 93868d75effSDimitry Andric 93968d75effSDimitry Andric using namespace __lsan; 94068d75effSDimitry Andric 94168d75effSDimitry Andric extern "C" { 94268d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 94368d75effSDimitry Andric void __lsan_ignore_object(const void *p) { 94468d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 94568d75effSDimitry Andric if (!common_flags()->detect_leaks) 94668d75effSDimitry Andric return; 94768d75effSDimitry Andric // Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not 94868d75effSDimitry Andric // locked. 949349cc55cSDimitry Andric Lock l(&global_mutex); 95068d75effSDimitry Andric IgnoreObjectResult res = IgnoreObjectLocked(p); 95168d75effSDimitry Andric if (res == kIgnoreObjectInvalid) 95268d75effSDimitry Andric VReport(1, "__lsan_ignore_object(): no heap object found at %p", p); 95368d75effSDimitry Andric if (res == kIgnoreObjectAlreadyIgnored) 9540eae32dcSDimitry Andric VReport(1, 9550eae32dcSDimitry Andric "__lsan_ignore_object(): " 9560eae32dcSDimitry Andric "heap object at %p is already being ignored\n", 9570eae32dcSDimitry Andric p); 95868d75effSDimitry Andric if (res == kIgnoreObjectSuccess) 95968d75effSDimitry Andric VReport(1, "__lsan_ignore_object(): ignoring heap object at %p\n", p); 96068d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 96168d75effSDimitry Andric } 96268d75effSDimitry Andric 96368d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 96468d75effSDimitry Andric void __lsan_register_root_region(const void *begin, uptr size) { 96568d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 966349cc55cSDimitry Andric Lock l(&global_mutex); 96768d75effSDimitry Andric RootRegion region = {reinterpret_cast<uptr>(begin), size}; 968349cc55cSDimitry Andric root_regions.push_back(region); 969349cc55cSDimitry Andric VReport(1, "Registered root region at %p of size %zu\n", begin, size); 97068d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 97168d75effSDimitry Andric } 97268d75effSDimitry Andric 97368d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 97468d75effSDimitry Andric void __lsan_unregister_root_region(const void *begin, uptr size) { 97568d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 976349cc55cSDimitry Andric Lock l(&global_mutex); 97768d75effSDimitry Andric bool removed = false; 978349cc55cSDimitry Andric for (uptr i = 0; i < root_regions.size(); i++) { 979349cc55cSDimitry Andric RootRegion region = root_regions[i]; 98068d75effSDimitry Andric if (region.begin == reinterpret_cast<uptr>(begin) && region.size == size) { 98168d75effSDimitry Andric removed = true; 982349cc55cSDimitry Andric uptr last_index = root_regions.size() - 1; 983349cc55cSDimitry Andric root_regions[i] = root_regions[last_index]; 984349cc55cSDimitry Andric root_regions.pop_back(); 985349cc55cSDimitry Andric VReport(1, "Unregistered root region at %p of size %zu\n", begin, size); 98668d75effSDimitry Andric break; 98768d75effSDimitry Andric } 98868d75effSDimitry Andric } 98968d75effSDimitry Andric if (!removed) { 99068d75effSDimitry Andric Report( 991349cc55cSDimitry Andric "__lsan_unregister_root_region(): region at %p of size %zu has not " 99268d75effSDimitry Andric "been registered.\n", 99368d75effSDimitry Andric begin, size); 99468d75effSDimitry Andric Die(); 99568d75effSDimitry Andric } 99668d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 99768d75effSDimitry Andric } 99868d75effSDimitry Andric 99968d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 100068d75effSDimitry Andric void __lsan_disable() { 100168d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 100268d75effSDimitry Andric __lsan::DisableInThisThread(); 100368d75effSDimitry Andric #endif 100468d75effSDimitry Andric } 100568d75effSDimitry Andric 100668d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 100768d75effSDimitry Andric void __lsan_enable() { 100868d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 100968d75effSDimitry Andric __lsan::EnableInThisThread(); 101068d75effSDimitry Andric #endif 101168d75effSDimitry Andric } 101268d75effSDimitry Andric 101368d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 101468d75effSDimitry Andric void __lsan_do_leak_check() { 101568d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 101668d75effSDimitry Andric if (common_flags()->detect_leaks) 101768d75effSDimitry Andric __lsan::DoLeakCheck(); 101868d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 101968d75effSDimitry Andric } 102068d75effSDimitry Andric 102168d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 102268d75effSDimitry Andric int __lsan_do_recoverable_leak_check() { 102368d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 102468d75effSDimitry Andric if (common_flags()->detect_leaks) 102568d75effSDimitry Andric return __lsan::DoRecoverableLeakCheck(); 102668d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 102768d75effSDimitry Andric return 0; 102868d75effSDimitry Andric } 102968d75effSDimitry Andric 1030e8d8bef9SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(const char *, __lsan_default_options, void) { 103168d75effSDimitry Andric return ""; 103268d75effSDimitry Andric } 103368d75effSDimitry Andric 1034e8d8bef9SDimitry Andric #if !SANITIZER_SUPPORTS_WEAK_HOOKS 1035*81ad6265SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(int, __lsan_is_turned_off, void) { 103668d75effSDimitry Andric return 0; 103768d75effSDimitry Andric } 103868d75effSDimitry Andric 1039*81ad6265SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(const char *, __lsan_default_suppressions, void) { 104068d75effSDimitry Andric return ""; 104168d75effSDimitry Andric } 104268d75effSDimitry Andric #endif 104368d75effSDimitry Andric } // extern "C" 1044