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 29*bdd1243dSDimitry Andric 30*bdd1243dSDimitry Andric # if SANITIZER_APPLE 31*bdd1243dSDimitry Andric // https://github.com/apple-oss-distributions/objc4/blob/8701d5672d3fd3cd817aeb84db1077aafe1a1604/runtime/objc-runtime-new.h#L127 32*bdd1243dSDimitry Andric # if SANITIZER_IOS && !SANITIZER_IOSSIM 33*bdd1243dSDimitry Andric # define OBJC_DATA_MASK 0x0000007ffffffff8UL 34*bdd1243dSDimitry Andric # else 35*bdd1243dSDimitry Andric # define OBJC_DATA_MASK 0x00007ffffffffff8UL 36*bdd1243dSDimitry Andric # endif 37*bdd1243dSDimitry Andric // https://github.com/apple-oss-distributions/objc4/blob/8701d5672d3fd3cd817aeb84db1077aafe1a1604/runtime/objc-runtime-new.h#L139 38*bdd1243dSDimitry Andric # define OBJC_FAST_IS_RW 0x8000000000000000UL 39*bdd1243dSDimitry Andric # endif 40*bdd1243dSDimitry Andric 4168d75effSDimitry Andric namespace __lsan { 4268d75effSDimitry Andric 4368d75effSDimitry Andric // This mutex is used to prevent races between DoLeakCheck and IgnoreObject, and 4468d75effSDimitry Andric // also to protect the global list of root regions. 45349cc55cSDimitry Andric Mutex global_mutex; 4668d75effSDimitry Andric 4768d75effSDimitry Andric Flags lsan_flags; 4868d75effSDimitry Andric 4968d75effSDimitry Andric void DisableCounterUnderflow() { 5068d75effSDimitry Andric if (common_flags()->detect_leaks) { 5168d75effSDimitry Andric Report("Unmatched call to __lsan_enable().\n"); 5268d75effSDimitry Andric Die(); 5368d75effSDimitry Andric } 5468d75effSDimitry Andric } 5568d75effSDimitry Andric 5668d75effSDimitry Andric void Flags::SetDefaults() { 5768d75effSDimitry Andric # define LSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue; 5868d75effSDimitry Andric # include "lsan_flags.inc" 5968d75effSDimitry Andric # undef LSAN_FLAG 6068d75effSDimitry Andric } 6168d75effSDimitry Andric 6268d75effSDimitry Andric void RegisterLsanFlags(FlagParser *parser, Flags *f) { 6368d75effSDimitry Andric # define LSAN_FLAG(Type, Name, DefaultValue, Description) \ 6468d75effSDimitry Andric RegisterFlag(parser, #Name, Description, &f->Name); 6568d75effSDimitry Andric # include "lsan_flags.inc" 6668d75effSDimitry Andric # undef LSAN_FLAG 6768d75effSDimitry Andric } 6868d75effSDimitry Andric 6968d75effSDimitry Andric # define LOG_POINTERS(...) \ 7068d75effSDimitry Andric do { \ 710eae32dcSDimitry Andric if (flags()->log_pointers) \ 720eae32dcSDimitry Andric Report(__VA_ARGS__); \ 7368d75effSDimitry Andric } while (0) 7468d75effSDimitry Andric 7568d75effSDimitry Andric # define LOG_THREADS(...) \ 7668d75effSDimitry Andric do { \ 770eae32dcSDimitry Andric if (flags()->log_threads) \ 780eae32dcSDimitry Andric Report(__VA_ARGS__); \ 7968d75effSDimitry Andric } while (0) 8068d75effSDimitry Andric 81e8d8bef9SDimitry Andric class LeakSuppressionContext { 82e8d8bef9SDimitry Andric bool parsed = false; 83e8d8bef9SDimitry Andric SuppressionContext context; 84e8d8bef9SDimitry Andric bool suppressed_stacks_sorted = true; 85e8d8bef9SDimitry Andric InternalMmapVector<u32> suppressed_stacks; 860eae32dcSDimitry Andric const LoadedModule *suppress_module = nullptr; 87e8d8bef9SDimitry Andric 88e8d8bef9SDimitry Andric void LazyInit(); 890eae32dcSDimitry Andric Suppression *GetSuppressionForAddr(uptr addr); 900eae32dcSDimitry Andric bool SuppressInvalid(const StackTrace &stack); 910eae32dcSDimitry Andric bool SuppressByRule(const StackTrace &stack, uptr hit_count, uptr total_size); 92e8d8bef9SDimitry Andric 93e8d8bef9SDimitry Andric public: 94e8d8bef9SDimitry Andric LeakSuppressionContext(const char *supprression_types[], 95e8d8bef9SDimitry Andric int suppression_types_num) 96e8d8bef9SDimitry Andric : context(supprression_types, suppression_types_num) {} 97e8d8bef9SDimitry Andric 980eae32dcSDimitry Andric bool Suppress(u32 stack_trace_id, uptr hit_count, uptr total_size); 99e8d8bef9SDimitry Andric 100e8d8bef9SDimitry Andric const InternalMmapVector<u32> &GetSortedSuppressedStacks() { 101e8d8bef9SDimitry Andric if (!suppressed_stacks_sorted) { 102e8d8bef9SDimitry Andric suppressed_stacks_sorted = true; 103e8d8bef9SDimitry Andric SortAndDedup(suppressed_stacks); 104e8d8bef9SDimitry Andric } 105e8d8bef9SDimitry Andric return suppressed_stacks; 106e8d8bef9SDimitry Andric } 107e8d8bef9SDimitry Andric void PrintMatchedSuppressions(); 108e8d8bef9SDimitry Andric }; 109e8d8bef9SDimitry Andric 110e8d8bef9SDimitry Andric ALIGNED(64) static char suppression_placeholder[sizeof(LeakSuppressionContext)]; 111e8d8bef9SDimitry Andric static LeakSuppressionContext *suppression_ctx = nullptr; 11268d75effSDimitry Andric static const char kSuppressionLeak[] = "leak"; 11368d75effSDimitry Andric static const char *kSuppressionTypes[] = {kSuppressionLeak}; 11468d75effSDimitry Andric static const char kStdSuppressions[] = 11568d75effSDimitry Andric # if SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 11668d75effSDimitry Andric // For more details refer to the SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 11768d75effSDimitry Andric // definition. 11868d75effSDimitry Andric "leak:*pthread_exit*\n" 11968d75effSDimitry Andric # endif // SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 12081ad6265SDimitry Andric # if SANITIZER_APPLE 12168d75effSDimitry Andric // For Darwin and os_log/os_trace: https://reviews.llvm.org/D35173 12268d75effSDimitry Andric "leak:*_os_trace*\n" 12368d75effSDimitry Andric # endif 12468d75effSDimitry Andric // TLS leak in some glibc versions, described in 12568d75effSDimitry Andric // https://sourceware.org/bugzilla/show_bug.cgi?id=12650. 12668d75effSDimitry Andric "leak:*tls_get_addr*\n"; 12768d75effSDimitry Andric 12868d75effSDimitry Andric void InitializeSuppressions() { 12968d75effSDimitry Andric CHECK_EQ(nullptr, suppression_ctx); 13068d75effSDimitry Andric suppression_ctx = new (suppression_placeholder) 131e8d8bef9SDimitry Andric LeakSuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes)); 13268d75effSDimitry Andric } 13368d75effSDimitry Andric 134e8d8bef9SDimitry Andric void LeakSuppressionContext::LazyInit() { 135e8d8bef9SDimitry Andric if (!parsed) { 136e8d8bef9SDimitry Andric parsed = true; 137e8d8bef9SDimitry Andric context.ParseFromFile(flags()->suppressions); 138e8d8bef9SDimitry Andric if (&__lsan_default_suppressions) 139e8d8bef9SDimitry Andric context.Parse(__lsan_default_suppressions()); 140e8d8bef9SDimitry Andric context.Parse(kStdSuppressions); 1410eae32dcSDimitry Andric if (flags()->use_tls && flags()->use_ld_allocations) 1420eae32dcSDimitry Andric suppress_module = GetLinker(); 143e8d8bef9SDimitry Andric } 144e8d8bef9SDimitry Andric } 145e8d8bef9SDimitry Andric 1460eae32dcSDimitry Andric Suppression *LeakSuppressionContext::GetSuppressionForAddr(uptr addr) { 1470eae32dcSDimitry Andric Suppression *s = nullptr; 1480eae32dcSDimitry Andric 1490eae32dcSDimitry Andric // Suppress by module name. 15081ad6265SDimitry Andric const char *module_name = Symbolizer::GetOrInit()->GetModuleNameForPc(addr); 15181ad6265SDimitry Andric if (!module_name) 15281ad6265SDimitry Andric module_name = "<unknown module>"; 1530eae32dcSDimitry Andric if (context.Match(module_name, kSuppressionLeak, &s)) 1540eae32dcSDimitry Andric return s; 1550eae32dcSDimitry Andric 1560eae32dcSDimitry Andric // Suppress by file or function name. 1570eae32dcSDimitry Andric SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(addr); 1580eae32dcSDimitry Andric for (SymbolizedStack *cur = frames; cur; cur = cur->next) { 1590eae32dcSDimitry Andric if (context.Match(cur->info.function, kSuppressionLeak, &s) || 1600eae32dcSDimitry Andric context.Match(cur->info.file, kSuppressionLeak, &s)) { 1610eae32dcSDimitry Andric break; 1620eae32dcSDimitry Andric } 1630eae32dcSDimitry Andric } 1640eae32dcSDimitry Andric frames->ClearAll(); 1650eae32dcSDimitry Andric return s; 1660eae32dcSDimitry Andric } 1670eae32dcSDimitry Andric 1680eae32dcSDimitry Andric static uptr GetCallerPC(const StackTrace &stack) { 1690eae32dcSDimitry Andric // The top frame is our malloc/calloc/etc. The next frame is the caller. 1700eae32dcSDimitry Andric if (stack.size >= 2) 1710eae32dcSDimitry Andric return stack.trace[1]; 1720eae32dcSDimitry Andric return 0; 1730eae32dcSDimitry Andric } 1740eae32dcSDimitry Andric 175*bdd1243dSDimitry Andric # if SANITIZER_APPLE 176*bdd1243dSDimitry Andric // Objective-C class data pointers are stored with flags in the low bits, so 177*bdd1243dSDimitry Andric // they need to be transformed back into something that looks like a pointer. 178*bdd1243dSDimitry Andric static inline void *MaybeTransformPointer(void *p) { 179*bdd1243dSDimitry Andric uptr ptr = reinterpret_cast<uptr>(p); 180*bdd1243dSDimitry Andric if ((ptr & OBJC_FAST_IS_RW) == OBJC_FAST_IS_RW) 181*bdd1243dSDimitry Andric ptr &= OBJC_DATA_MASK; 182*bdd1243dSDimitry Andric return reinterpret_cast<void *>(ptr); 183*bdd1243dSDimitry Andric } 184*bdd1243dSDimitry Andric # endif 185*bdd1243dSDimitry Andric 1860eae32dcSDimitry Andric // On Linux, treats all chunks allocated from ld-linux.so as reachable, which 1870eae32dcSDimitry Andric // covers dynamically allocated TLS blocks, internal dynamic loader's loaded 1880eae32dcSDimitry Andric // modules accounting etc. 1890eae32dcSDimitry Andric // Dynamic TLS blocks contain the TLS variables of dynamically loaded modules. 1900eae32dcSDimitry Andric // They are allocated with a __libc_memalign() call in allocate_and_init() 1910eae32dcSDimitry Andric // (elf/dl-tls.c). Glibc won't tell us the address ranges occupied by those 1920eae32dcSDimitry Andric // blocks, but we can make sure they come from our own allocator by intercepting 1930eae32dcSDimitry Andric // __libc_memalign(). On top of that, there is no easy way to reach them. Their 1940eae32dcSDimitry Andric // addresses are stored in a dynamically allocated array (the DTV) which is 1950eae32dcSDimitry Andric // referenced from the static TLS. Unfortunately, we can't just rely on the DTV 1960eae32dcSDimitry Andric // being reachable from the static TLS, and the dynamic TLS being reachable from 1970eae32dcSDimitry Andric // the DTV. This is because the initial DTV is allocated before our interception 1980eae32dcSDimitry Andric // mechanism kicks in, and thus we don't recognize it as allocated memory. We 1990eae32dcSDimitry Andric // can't special-case it either, since we don't know its size. 2000eae32dcSDimitry Andric // Our solution is to include in the root set all allocations made from 2010eae32dcSDimitry Andric // ld-linux.so (which is where allocate_and_init() is implemented). This is 2020eae32dcSDimitry Andric // guaranteed to include all dynamic TLS blocks (and possibly other allocations 2030eae32dcSDimitry Andric // which we don't care about). 2040eae32dcSDimitry Andric // On all other platforms, this simply checks to ensure that the caller pc is 2050eae32dcSDimitry Andric // valid before reporting chunks as leaked. 2060eae32dcSDimitry Andric bool LeakSuppressionContext::SuppressInvalid(const StackTrace &stack) { 2070eae32dcSDimitry Andric uptr caller_pc = GetCallerPC(stack); 2080eae32dcSDimitry Andric // If caller_pc is unknown, this chunk may be allocated in a coroutine. Mark 2090eae32dcSDimitry Andric // it as reachable, as we can't properly report its allocation stack anyway. 2100eae32dcSDimitry Andric return !caller_pc || 2110eae32dcSDimitry Andric (suppress_module && suppress_module->containsAddress(caller_pc)); 2120eae32dcSDimitry Andric } 2130eae32dcSDimitry Andric 2140eae32dcSDimitry Andric bool LeakSuppressionContext::SuppressByRule(const StackTrace &stack, 2150eae32dcSDimitry Andric uptr hit_count, uptr total_size) { 2160eae32dcSDimitry Andric for (uptr i = 0; i < stack.size; i++) { 2170eae32dcSDimitry Andric Suppression *s = GetSuppressionForAddr( 2180eae32dcSDimitry Andric StackTrace::GetPreviousInstructionPc(stack.trace[i])); 2190eae32dcSDimitry Andric if (s) { 2200eae32dcSDimitry Andric s->weight += total_size; 2210eae32dcSDimitry Andric atomic_fetch_add(&s->hit_count, hit_count, memory_order_relaxed); 2220eae32dcSDimitry Andric return true; 2230eae32dcSDimitry Andric } 2240eae32dcSDimitry Andric } 2250eae32dcSDimitry Andric return false; 2260eae32dcSDimitry Andric } 2270eae32dcSDimitry Andric 2280eae32dcSDimitry Andric bool LeakSuppressionContext::Suppress(u32 stack_trace_id, uptr hit_count, 2290eae32dcSDimitry Andric uptr total_size) { 2300eae32dcSDimitry Andric LazyInit(); 2310eae32dcSDimitry Andric StackTrace stack = StackDepotGet(stack_trace_id); 2320eae32dcSDimitry Andric if (!SuppressInvalid(stack) && !SuppressByRule(stack, hit_count, total_size)) 2330eae32dcSDimitry Andric return false; 2340eae32dcSDimitry Andric suppressed_stacks_sorted = false; 2350eae32dcSDimitry Andric suppressed_stacks.push_back(stack_trace_id); 2360eae32dcSDimitry Andric return true; 2370eae32dcSDimitry Andric } 2380eae32dcSDimitry Andric 239e8d8bef9SDimitry Andric static LeakSuppressionContext *GetSuppressionContext() { 24068d75effSDimitry Andric CHECK(suppression_ctx); 24168d75effSDimitry Andric return suppression_ctx; 24268d75effSDimitry Andric } 24368d75effSDimitry Andric 244349cc55cSDimitry Andric static InternalMmapVectorNoCtor<RootRegion> root_regions; 24568d75effSDimitry Andric 246349cc55cSDimitry Andric InternalMmapVectorNoCtor<RootRegion> const *GetRootRegions() { 247349cc55cSDimitry Andric return &root_regions; 24868d75effSDimitry Andric } 24968d75effSDimitry Andric 25068d75effSDimitry Andric void InitCommonLsan() { 25168d75effSDimitry Andric if (common_flags()->detect_leaks) { 25268d75effSDimitry Andric // Initialization which can fail or print warnings should only be done if 25368d75effSDimitry Andric // LSan is actually enabled. 25468d75effSDimitry Andric InitializeSuppressions(); 25568d75effSDimitry Andric InitializePlatformSpecificModules(); 25668d75effSDimitry Andric } 25768d75effSDimitry Andric } 25868d75effSDimitry Andric 25968d75effSDimitry Andric class Decorator : public __sanitizer::SanitizerCommonDecorator { 26068d75effSDimitry Andric public: 26168d75effSDimitry Andric Decorator() : SanitizerCommonDecorator() {} 26268d75effSDimitry Andric const char *Error() { return Red(); } 26368d75effSDimitry Andric const char *Leak() { return Blue(); } 26468d75effSDimitry Andric }; 26568d75effSDimitry Andric 26681ad6265SDimitry Andric static inline bool MaybeUserPointer(uptr p) { 26768d75effSDimitry Andric // Since our heap is located in mmap-ed memory, we can assume a sensible lower 26868d75effSDimitry Andric // bound on heap addresses. 26968d75effSDimitry Andric const uptr kMinAddress = 4 * 4096; 2700eae32dcSDimitry Andric if (p < kMinAddress) 2710eae32dcSDimitry Andric return false; 27268d75effSDimitry Andric # if defined(__x86_64__) 27368d75effSDimitry Andric // Accept only canonical form user-space addresses. 27468d75effSDimitry Andric return ((p >> 47) == 0); 27568d75effSDimitry Andric # elif defined(__mips64) 27668d75effSDimitry Andric return ((p >> 40) == 0); 27768d75effSDimitry Andric # elif defined(__aarch64__) 27881ad6265SDimitry Andric // Accept up to 48 bit VMA. 27981ad6265SDimitry Andric return ((p >> 48) == 0); 280*bdd1243dSDimitry Andric # elif defined(__loongarch_lp64) 281*bdd1243dSDimitry Andric // Allow 47-bit user-space VMA at current. 282*bdd1243dSDimitry Andric return ((p >> 47) == 0); 28368d75effSDimitry Andric # else 28468d75effSDimitry Andric return true; 28568d75effSDimitry Andric # endif 28668d75effSDimitry Andric } 28768d75effSDimitry Andric 28868d75effSDimitry Andric // Scans the memory range, looking for byte patterns that point into allocator 28968d75effSDimitry Andric // chunks. Marks those chunks with |tag| and adds them to |frontier|. 29068d75effSDimitry Andric // There are two usage modes for this function: finding reachable chunks 29168d75effSDimitry Andric // (|tag| = kReachable) and finding indirectly leaked chunks 29268d75effSDimitry Andric // (|tag| = kIndirectlyLeaked). In the second case, there's no flood fill, 29368d75effSDimitry Andric // so |frontier| = 0. 2940eae32dcSDimitry Andric void ScanRangeForPointers(uptr begin, uptr end, Frontier *frontier, 29568d75effSDimitry Andric const char *region_type, ChunkTag tag) { 29668d75effSDimitry Andric CHECK(tag == kReachable || tag == kIndirectlyLeaked); 29768d75effSDimitry Andric const uptr alignment = flags()->pointer_alignment(); 298349cc55cSDimitry Andric LOG_POINTERS("Scanning %s range %p-%p.\n", region_type, (void *)begin, 299349cc55cSDimitry Andric (void *)end); 30068d75effSDimitry Andric uptr pp = begin; 30168d75effSDimitry Andric if (pp % alignment) 30268d75effSDimitry Andric pp = pp + alignment - pp % alignment; 30368d75effSDimitry Andric for (; pp + sizeof(void *) <= end; pp += alignment) { 30468d75effSDimitry Andric void *p = *reinterpret_cast<void **>(pp); 305*bdd1243dSDimitry Andric # if SANITIZER_APPLE 306*bdd1243dSDimitry Andric p = MaybeTransformPointer(p); 307*bdd1243dSDimitry Andric # endif 30881ad6265SDimitry Andric if (!MaybeUserPointer(reinterpret_cast<uptr>(p))) 3090eae32dcSDimitry Andric continue; 31068d75effSDimitry Andric uptr chunk = PointsIntoChunk(p); 3110eae32dcSDimitry Andric if (!chunk) 3120eae32dcSDimitry Andric continue; 31368d75effSDimitry Andric // Pointers to self don't count. This matters when tag == kIndirectlyLeaked. 3140eae32dcSDimitry Andric if (chunk == begin) 3150eae32dcSDimitry Andric continue; 31668d75effSDimitry Andric LsanMetadata m(chunk); 3170eae32dcSDimitry Andric if (m.tag() == kReachable || m.tag() == kIgnored) 3180eae32dcSDimitry Andric continue; 31968d75effSDimitry Andric 32068d75effSDimitry Andric // Do this check relatively late so we can log only the interesting cases. 32168d75effSDimitry Andric if (!flags()->use_poisoned && WordIsPoisoned(pp)) { 32268d75effSDimitry Andric LOG_POINTERS( 32368d75effSDimitry Andric "%p is poisoned: ignoring %p pointing into chunk %p-%p of size " 32468d75effSDimitry Andric "%zu.\n", 325349cc55cSDimitry Andric (void *)pp, p, (void *)chunk, (void *)(chunk + m.requested_size()), 326349cc55cSDimitry Andric m.requested_size()); 32768d75effSDimitry Andric continue; 32868d75effSDimitry Andric } 32968d75effSDimitry Andric 33068d75effSDimitry Andric m.set_tag(tag); 331349cc55cSDimitry Andric LOG_POINTERS("%p: found %p pointing into chunk %p-%p of size %zu.\n", 332349cc55cSDimitry Andric (void *)pp, p, (void *)chunk, 333349cc55cSDimitry Andric (void *)(chunk + m.requested_size()), m.requested_size()); 33468d75effSDimitry Andric if (frontier) 33568d75effSDimitry Andric frontier->push_back(chunk); 33668d75effSDimitry Andric } 33768d75effSDimitry Andric } 33868d75effSDimitry Andric 33968d75effSDimitry Andric // Scans a global range for pointers 34068d75effSDimitry Andric void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier) { 34168d75effSDimitry Andric uptr allocator_begin = 0, allocator_end = 0; 34268d75effSDimitry Andric GetAllocatorGlobalRange(&allocator_begin, &allocator_end); 34368d75effSDimitry Andric if (begin <= allocator_begin && allocator_begin < end) { 34468d75effSDimitry Andric CHECK_LE(allocator_begin, allocator_end); 34568d75effSDimitry Andric CHECK_LE(allocator_end, end); 34668d75effSDimitry Andric if (begin < allocator_begin) 34768d75effSDimitry Andric ScanRangeForPointers(begin, allocator_begin, frontier, "GLOBAL", 34868d75effSDimitry Andric kReachable); 34968d75effSDimitry Andric if (allocator_end < end) 35068d75effSDimitry Andric ScanRangeForPointers(allocator_end, end, frontier, "GLOBAL", kReachable); 35168d75effSDimitry Andric } else { 35268d75effSDimitry Andric ScanRangeForPointers(begin, end, frontier, "GLOBAL", kReachable); 35368d75effSDimitry Andric } 35468d75effSDimitry Andric } 35568d75effSDimitry Andric 356*bdd1243dSDimitry Andric void ScanExtraStackRanges(const InternalMmapVector<Range> &ranges, 357*bdd1243dSDimitry Andric Frontier *frontier) { 358*bdd1243dSDimitry Andric for (uptr i = 0; i < ranges.size(); i++) { 359*bdd1243dSDimitry Andric ScanRangeForPointers(ranges[i].begin, ranges[i].end, frontier, "FAKE STACK", 360*bdd1243dSDimitry Andric kReachable); 361*bdd1243dSDimitry Andric } 36268d75effSDimitry Andric } 36368d75effSDimitry Andric 3645ffd83dbSDimitry Andric # if SANITIZER_FUCHSIA 3655ffd83dbSDimitry Andric 3665ffd83dbSDimitry Andric // Fuchsia handles all threads together with its own callback. 367*bdd1243dSDimitry Andric static void ProcessThreads(SuspendedThreadsList const &, Frontier *, tid_t, 368*bdd1243dSDimitry Andric uptr) {} 3695ffd83dbSDimitry Andric 3705ffd83dbSDimitry Andric # else 3715ffd83dbSDimitry Andric 372e8d8bef9SDimitry Andric # if SANITIZER_ANDROID 373e8d8bef9SDimitry Andric // FIXME: Move this out into *libcdep.cpp 374e8d8bef9SDimitry Andric extern "C" SANITIZER_WEAK_ATTRIBUTE void __libc_iterate_dynamic_tls( 375e8d8bef9SDimitry Andric pid_t, void (*cb)(void *, void *, uptr, void *), void *); 376e8d8bef9SDimitry Andric # endif 377e8d8bef9SDimitry Andric 378e8d8bef9SDimitry Andric static void ProcessThreadRegistry(Frontier *frontier) { 379e8d8bef9SDimitry Andric InternalMmapVector<uptr> ptrs; 380*bdd1243dSDimitry Andric GetAdditionalThreadContextPtrsLocked(&ptrs); 381e8d8bef9SDimitry Andric 382e8d8bef9SDimitry Andric for (uptr i = 0; i < ptrs.size(); ++i) { 383e8d8bef9SDimitry Andric void *ptr = reinterpret_cast<void *>(ptrs[i]); 384e8d8bef9SDimitry Andric uptr chunk = PointsIntoChunk(ptr); 385e8d8bef9SDimitry Andric if (!chunk) 386e8d8bef9SDimitry Andric continue; 387e8d8bef9SDimitry Andric LsanMetadata m(chunk); 388e8d8bef9SDimitry Andric if (!m.allocated()) 389e8d8bef9SDimitry Andric continue; 390e8d8bef9SDimitry Andric 391e8d8bef9SDimitry Andric // Mark as reachable and add to frontier. 392e8d8bef9SDimitry Andric LOG_POINTERS("Treating pointer %p from ThreadContext as reachable\n", ptr); 393e8d8bef9SDimitry Andric m.set_tag(kReachable); 394e8d8bef9SDimitry Andric frontier->push_back(chunk); 395e8d8bef9SDimitry Andric } 396e8d8bef9SDimitry Andric } 397e8d8bef9SDimitry Andric 39868d75effSDimitry Andric // Scans thread data (stacks and TLS) for heap pointers. 39968d75effSDimitry Andric static void ProcessThreads(SuspendedThreadsList const &suspended_threads, 400*bdd1243dSDimitry Andric Frontier *frontier, tid_t caller_tid, 401*bdd1243dSDimitry Andric uptr caller_sp) { 402e8d8bef9SDimitry Andric InternalMmapVector<uptr> registers; 403*bdd1243dSDimitry Andric InternalMmapVector<Range> extra_ranges; 40468d75effSDimitry Andric for (uptr i = 0; i < suspended_threads.ThreadCount(); i++) { 40568d75effSDimitry Andric tid_t os_id = static_cast<tid_t>(suspended_threads.GetThreadID(i)); 406349cc55cSDimitry Andric LOG_THREADS("Processing thread %llu.\n", os_id); 40768d75effSDimitry Andric uptr stack_begin, stack_end, tls_begin, tls_end, cache_begin, cache_end; 40868d75effSDimitry Andric DTLS *dtls; 4090eae32dcSDimitry Andric bool thread_found = 4100eae32dcSDimitry Andric GetThreadRangesLocked(os_id, &stack_begin, &stack_end, &tls_begin, 4110eae32dcSDimitry Andric &tls_end, &cache_begin, &cache_end, &dtls); 41268d75effSDimitry Andric if (!thread_found) { 41368d75effSDimitry Andric // If a thread can't be found in the thread registry, it's probably in the 41468d75effSDimitry Andric // process of destruction. Log this event and move on. 415349cc55cSDimitry Andric LOG_THREADS("Thread %llu not found in registry.\n", os_id); 41668d75effSDimitry Andric continue; 41768d75effSDimitry Andric } 41868d75effSDimitry Andric uptr sp; 41968d75effSDimitry Andric PtraceRegistersStatus have_registers = 420e8d8bef9SDimitry Andric suspended_threads.GetRegistersAndSP(i, ®isters, &sp); 42168d75effSDimitry Andric if (have_registers != REGISTERS_AVAILABLE) { 422349cc55cSDimitry Andric Report("Unable to get registers from thread %llu.\n", os_id); 42368d75effSDimitry Andric // If unable to get SP, consider the entire stack to be reachable unless 42468d75effSDimitry Andric // GetRegistersAndSP failed with ESRCH. 4250eae32dcSDimitry Andric if (have_registers == REGISTERS_UNAVAILABLE_FATAL) 4260eae32dcSDimitry Andric continue; 42768d75effSDimitry Andric sp = stack_begin; 42868d75effSDimitry Andric } 429*bdd1243dSDimitry Andric if (suspended_threads.GetThreadID(i) == caller_tid) { 430*bdd1243dSDimitry Andric sp = caller_sp; 431*bdd1243dSDimitry Andric } 43268d75effSDimitry Andric 433e8d8bef9SDimitry Andric if (flags()->use_registers && have_registers) { 434e8d8bef9SDimitry Andric uptr registers_begin = reinterpret_cast<uptr>(registers.data()); 435e8d8bef9SDimitry Andric uptr registers_end = 436e8d8bef9SDimitry Andric reinterpret_cast<uptr>(registers.data() + registers.size()); 43768d75effSDimitry Andric ScanRangeForPointers(registers_begin, registers_end, frontier, 43868d75effSDimitry Andric "REGISTERS", kReachable); 439e8d8bef9SDimitry Andric } 44068d75effSDimitry Andric 44168d75effSDimitry Andric if (flags()->use_stacks) { 442349cc55cSDimitry Andric LOG_THREADS("Stack at %p-%p (SP = %p).\n", (void *)stack_begin, 443349cc55cSDimitry Andric (void *)stack_end, (void *)sp); 44468d75effSDimitry Andric if (sp < stack_begin || sp >= stack_end) { 44568d75effSDimitry Andric // SP is outside the recorded stack range (e.g. the thread is running a 44668d75effSDimitry Andric // signal handler on alternate stack, or swapcontext was used). 44768d75effSDimitry Andric // Again, consider the entire stack range to be reachable. 44868d75effSDimitry Andric LOG_THREADS("WARNING: stack pointer not in stack range.\n"); 44968d75effSDimitry Andric uptr page_size = GetPageSizeCached(); 45068d75effSDimitry Andric int skipped = 0; 45168d75effSDimitry Andric while (stack_begin < stack_end && 45268d75effSDimitry Andric !IsAccessibleMemoryRange(stack_begin, 1)) { 45368d75effSDimitry Andric skipped++; 45468d75effSDimitry Andric stack_begin += page_size; 45568d75effSDimitry Andric } 45668d75effSDimitry Andric LOG_THREADS("Skipped %d guard page(s) to obtain stack %p-%p.\n", 457349cc55cSDimitry Andric skipped, (void *)stack_begin, (void *)stack_end); 45868d75effSDimitry Andric } else { 45968d75effSDimitry Andric // Shrink the stack range to ignore out-of-scope values. 46068d75effSDimitry Andric stack_begin = sp; 46168d75effSDimitry Andric } 46268d75effSDimitry Andric ScanRangeForPointers(stack_begin, stack_end, frontier, "STACK", 46368d75effSDimitry Andric kReachable); 464*bdd1243dSDimitry Andric extra_ranges.clear(); 465*bdd1243dSDimitry Andric GetThreadExtraStackRangesLocked(os_id, &extra_ranges); 466*bdd1243dSDimitry Andric ScanExtraStackRanges(extra_ranges, frontier); 46768d75effSDimitry Andric } 46868d75effSDimitry Andric 46968d75effSDimitry Andric if (flags()->use_tls) { 47068d75effSDimitry Andric if (tls_begin) { 471349cc55cSDimitry Andric LOG_THREADS("TLS at %p-%p.\n", (void *)tls_begin, (void *)tls_end); 47268d75effSDimitry Andric // If the tls and cache ranges don't overlap, scan full tls range, 47368d75effSDimitry Andric // otherwise, only scan the non-overlapping portions 47468d75effSDimitry Andric if (cache_begin == cache_end || tls_end < cache_begin || 47568d75effSDimitry Andric tls_begin > cache_end) { 47668d75effSDimitry Andric ScanRangeForPointers(tls_begin, tls_end, frontier, "TLS", kReachable); 47768d75effSDimitry Andric } else { 47868d75effSDimitry Andric if (tls_begin < cache_begin) 47968d75effSDimitry Andric ScanRangeForPointers(tls_begin, cache_begin, frontier, "TLS", 48068d75effSDimitry Andric kReachable); 48168d75effSDimitry Andric if (tls_end > cache_end) 48268d75effSDimitry Andric ScanRangeForPointers(cache_end, tls_end, frontier, "TLS", 48368d75effSDimitry Andric kReachable); 48468d75effSDimitry Andric } 48568d75effSDimitry Andric } 486e8d8bef9SDimitry Andric # if SANITIZER_ANDROID 487e8d8bef9SDimitry Andric auto *cb = +[](void *dtls_begin, void *dtls_end, uptr /*dso_idd*/, 488e8d8bef9SDimitry Andric void *arg) -> void { 489e8d8bef9SDimitry Andric ScanRangeForPointers(reinterpret_cast<uptr>(dtls_begin), 490e8d8bef9SDimitry Andric reinterpret_cast<uptr>(dtls_end), 491e8d8bef9SDimitry Andric reinterpret_cast<Frontier *>(arg), "DTLS", 492e8d8bef9SDimitry Andric kReachable); 493e8d8bef9SDimitry Andric }; 494e8d8bef9SDimitry Andric 495e8d8bef9SDimitry Andric // FIXME: There might be a race-condition here (and in Bionic) if the 496e8d8bef9SDimitry Andric // thread is suspended in the middle of updating its DTLS. IOWs, we 497e8d8bef9SDimitry Andric // could scan already freed memory. (probably fine for now) 498e8d8bef9SDimitry Andric __libc_iterate_dynamic_tls(os_id, cb, frontier); 499e8d8bef9SDimitry Andric # else 50068d75effSDimitry Andric if (dtls && !DTLSInDestruction(dtls)) { 501e8d8bef9SDimitry Andric ForEachDVT(dtls, [&](const DTLS::DTV &dtv, int id) { 502e8d8bef9SDimitry Andric uptr dtls_beg = dtv.beg; 503e8d8bef9SDimitry Andric uptr dtls_end = dtls_beg + dtv.size; 50468d75effSDimitry Andric if (dtls_beg < dtls_end) { 505349cc55cSDimitry Andric LOG_THREADS("DTLS %d at %p-%p.\n", id, (void *)dtls_beg, 506349cc55cSDimitry Andric (void *)dtls_end); 50768d75effSDimitry Andric ScanRangeForPointers(dtls_beg, dtls_end, frontier, "DTLS", 50868d75effSDimitry Andric kReachable); 50968d75effSDimitry Andric } 510e8d8bef9SDimitry Andric }); 51168d75effSDimitry Andric } else { 51268d75effSDimitry Andric // We are handling a thread with DTLS under destruction. Log about 51368d75effSDimitry Andric // this and continue. 514349cc55cSDimitry Andric LOG_THREADS("Thread %llu has DTLS under destruction.\n", os_id); 51568d75effSDimitry Andric } 516e8d8bef9SDimitry Andric # endif 51768d75effSDimitry Andric } 51868d75effSDimitry Andric } 519e8d8bef9SDimitry Andric 520e8d8bef9SDimitry Andric // Add pointers reachable from ThreadContexts 521e8d8bef9SDimitry Andric ProcessThreadRegistry(frontier); 52268d75effSDimitry Andric } 52368d75effSDimitry Andric 5245ffd83dbSDimitry Andric # endif // SANITIZER_FUCHSIA 5255ffd83dbSDimitry Andric 52668d75effSDimitry Andric void ScanRootRegion(Frontier *frontier, const RootRegion &root_region, 52768d75effSDimitry Andric uptr region_begin, uptr region_end, bool is_readable) { 52868d75effSDimitry Andric uptr intersection_begin = Max(root_region.begin, region_begin); 52968d75effSDimitry Andric uptr intersection_end = Min(region_end, root_region.begin + root_region.size); 5300eae32dcSDimitry Andric if (intersection_begin >= intersection_end) 5310eae32dcSDimitry Andric return; 53268d75effSDimitry Andric LOG_POINTERS("Root region %p-%p intersects with mapped region %p-%p (%s)\n", 533349cc55cSDimitry Andric (void *)root_region.begin, 534349cc55cSDimitry Andric (void *)(root_region.begin + root_region.size), 535349cc55cSDimitry Andric (void *)region_begin, (void *)region_end, 53668d75effSDimitry Andric is_readable ? "readable" : "unreadable"); 53768d75effSDimitry Andric if (is_readable) 53868d75effSDimitry Andric ScanRangeForPointers(intersection_begin, intersection_end, frontier, "ROOT", 53968d75effSDimitry Andric kReachable); 54068d75effSDimitry Andric } 54168d75effSDimitry Andric 54268d75effSDimitry Andric static void ProcessRootRegion(Frontier *frontier, 54368d75effSDimitry Andric const RootRegion &root_region) { 54468d75effSDimitry Andric MemoryMappingLayout proc_maps(/*cache_enabled*/ true); 54568d75effSDimitry Andric MemoryMappedSegment segment; 54668d75effSDimitry Andric while (proc_maps.Next(&segment)) { 54768d75effSDimitry Andric ScanRootRegion(frontier, root_region, segment.start, segment.end, 54868d75effSDimitry Andric segment.IsReadable()); 54968d75effSDimitry Andric } 55068d75effSDimitry Andric } 55168d75effSDimitry Andric 55268d75effSDimitry Andric // Scans root regions for heap pointers. 55368d75effSDimitry Andric static void ProcessRootRegions(Frontier *frontier) { 5540eae32dcSDimitry Andric if (!flags()->use_root_regions) 5550eae32dcSDimitry Andric return; 556349cc55cSDimitry Andric for (uptr i = 0; i < root_regions.size(); i++) 557349cc55cSDimitry Andric ProcessRootRegion(frontier, root_regions[i]); 55868d75effSDimitry Andric } 55968d75effSDimitry Andric 56068d75effSDimitry Andric static void FloodFillTag(Frontier *frontier, ChunkTag tag) { 56168d75effSDimitry Andric while (frontier->size()) { 56268d75effSDimitry Andric uptr next_chunk = frontier->back(); 56368d75effSDimitry Andric frontier->pop_back(); 56468d75effSDimitry Andric LsanMetadata m(next_chunk); 56568d75effSDimitry Andric ScanRangeForPointers(next_chunk, next_chunk + m.requested_size(), frontier, 56668d75effSDimitry Andric "HEAP", tag); 56768d75effSDimitry Andric } 56868d75effSDimitry Andric } 56968d75effSDimitry Andric 57068d75effSDimitry Andric // ForEachChunk callback. If the chunk is marked as leaked, marks all chunks 57168d75effSDimitry Andric // which are reachable from it as indirectly leaked. 57268d75effSDimitry Andric static void MarkIndirectlyLeakedCb(uptr chunk, void *arg) { 57368d75effSDimitry Andric chunk = GetUserBegin(chunk); 57468d75effSDimitry Andric LsanMetadata m(chunk); 57568d75effSDimitry Andric if (m.allocated() && m.tag() != kReachable) { 57668d75effSDimitry Andric ScanRangeForPointers(chunk, chunk + m.requested_size(), 57768d75effSDimitry Andric /* frontier */ nullptr, "HEAP", kIndirectlyLeaked); 57868d75effSDimitry Andric } 57968d75effSDimitry Andric } 58068d75effSDimitry Andric 581e8d8bef9SDimitry Andric static void IgnoredSuppressedCb(uptr chunk, void *arg) { 582e8d8bef9SDimitry Andric CHECK(arg); 583e8d8bef9SDimitry Andric chunk = GetUserBegin(chunk); 584e8d8bef9SDimitry Andric LsanMetadata m(chunk); 585e8d8bef9SDimitry Andric if (!m.allocated() || m.tag() == kIgnored) 586e8d8bef9SDimitry Andric return; 587e8d8bef9SDimitry Andric 588e8d8bef9SDimitry Andric const InternalMmapVector<u32> &suppressed = 589e8d8bef9SDimitry Andric *static_cast<const InternalMmapVector<u32> *>(arg); 590e8d8bef9SDimitry Andric uptr idx = InternalLowerBound(suppressed, m.stack_trace_id()); 591e8d8bef9SDimitry Andric if (idx >= suppressed.size() || m.stack_trace_id() != suppressed[idx]) 592e8d8bef9SDimitry Andric return; 593e8d8bef9SDimitry Andric 594349cc55cSDimitry Andric LOG_POINTERS("Suppressed: chunk %p-%p of size %zu.\n", (void *)chunk, 595349cc55cSDimitry Andric (void *)(chunk + m.requested_size()), m.requested_size()); 596e8d8bef9SDimitry Andric m.set_tag(kIgnored); 597e8d8bef9SDimitry Andric } 598e8d8bef9SDimitry Andric 59968d75effSDimitry Andric // ForEachChunk callback. If chunk is marked as ignored, adds its address to 60068d75effSDimitry Andric // frontier. 60168d75effSDimitry Andric static void CollectIgnoredCb(uptr chunk, void *arg) { 60268d75effSDimitry Andric CHECK(arg); 60368d75effSDimitry Andric chunk = GetUserBegin(chunk); 60468d75effSDimitry Andric LsanMetadata m(chunk); 60568d75effSDimitry Andric if (m.allocated() && m.tag() == kIgnored) { 606349cc55cSDimitry Andric LOG_POINTERS("Ignored: chunk %p-%p of size %zu.\n", (void *)chunk, 607349cc55cSDimitry Andric (void *)(chunk + m.requested_size()), m.requested_size()); 60868d75effSDimitry Andric reinterpret_cast<Frontier *>(arg)->push_back(chunk); 60968d75effSDimitry Andric } 61068d75effSDimitry Andric } 61168d75effSDimitry Andric 61268d75effSDimitry Andric // Sets the appropriate tag on each chunk. 6135ffd83dbSDimitry Andric static void ClassifyAllChunks(SuspendedThreadsList const &suspended_threads, 614*bdd1243dSDimitry Andric Frontier *frontier, tid_t caller_tid, 615*bdd1243dSDimitry Andric uptr caller_sp) { 616e8d8bef9SDimitry Andric const InternalMmapVector<u32> &suppressed_stacks = 617e8d8bef9SDimitry Andric GetSuppressionContext()->GetSortedSuppressedStacks(); 618e8d8bef9SDimitry Andric if (!suppressed_stacks.empty()) { 619e8d8bef9SDimitry Andric ForEachChunk(IgnoredSuppressedCb, 620e8d8bef9SDimitry Andric const_cast<InternalMmapVector<u32> *>(&suppressed_stacks)); 621e8d8bef9SDimitry Andric } 6225ffd83dbSDimitry Andric ForEachChunk(CollectIgnoredCb, frontier); 6235ffd83dbSDimitry Andric ProcessGlobalRegions(frontier); 624*bdd1243dSDimitry Andric ProcessThreads(suspended_threads, frontier, caller_tid, caller_sp); 6255ffd83dbSDimitry Andric ProcessRootRegions(frontier); 6265ffd83dbSDimitry Andric FloodFillTag(frontier, kReachable); 62768d75effSDimitry Andric 62868d75effSDimitry Andric // The check here is relatively expensive, so we do this in a separate flood 62968d75effSDimitry Andric // fill. That way we can skip the check for chunks that are reachable 63068d75effSDimitry Andric // otherwise. 63168d75effSDimitry Andric LOG_POINTERS("Processing platform-specific allocations.\n"); 6325ffd83dbSDimitry Andric ProcessPlatformSpecificAllocations(frontier); 6335ffd83dbSDimitry Andric FloodFillTag(frontier, kReachable); 63468d75effSDimitry Andric 63568d75effSDimitry Andric // Iterate over leaked chunks and mark those that are reachable from other 63668d75effSDimitry Andric // leaked chunks. 63768d75effSDimitry Andric LOG_POINTERS("Scanning leaked chunks.\n"); 63868d75effSDimitry Andric ForEachChunk(MarkIndirectlyLeakedCb, nullptr); 63968d75effSDimitry Andric } 64068d75effSDimitry Andric 64168d75effSDimitry Andric // ForEachChunk callback. Resets the tags to pre-leak-check state. 64268d75effSDimitry Andric static void ResetTagsCb(uptr chunk, void *arg) { 64368d75effSDimitry Andric (void)arg; 64468d75effSDimitry Andric chunk = GetUserBegin(chunk); 64568d75effSDimitry Andric LsanMetadata m(chunk); 64668d75effSDimitry Andric if (m.allocated() && m.tag() != kIgnored) 64768d75effSDimitry Andric m.set_tag(kDirectlyLeaked); 64868d75effSDimitry Andric } 64968d75effSDimitry Andric 65068d75effSDimitry Andric // ForEachChunk callback. Aggregates information about unreachable chunks into 65168d75effSDimitry Andric // a LeakReport. 65268d75effSDimitry Andric static void CollectLeaksCb(uptr chunk, void *arg) { 65368d75effSDimitry Andric CHECK(arg); 6540eae32dcSDimitry Andric LeakedChunks *leaks = reinterpret_cast<LeakedChunks *>(arg); 65568d75effSDimitry Andric chunk = GetUserBegin(chunk); 65668d75effSDimitry Andric LsanMetadata m(chunk); 6570eae32dcSDimitry Andric if (!m.allocated()) 6580eae32dcSDimitry Andric return; 6590eae32dcSDimitry Andric if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) 6600eae32dcSDimitry Andric leaks->push_back({chunk, m.stack_trace_id(), m.requested_size(), m.tag()}); 66168d75effSDimitry Andric } 66268d75effSDimitry Andric 663e8d8bef9SDimitry Andric void LeakSuppressionContext::PrintMatchedSuppressions() { 66468d75effSDimitry Andric InternalMmapVector<Suppression *> matched; 665e8d8bef9SDimitry Andric context.GetMatched(&matched); 66668d75effSDimitry Andric if (!matched.size()) 66768d75effSDimitry Andric return; 66868d75effSDimitry Andric const char *line = "-----------------------------------------------------"; 66968d75effSDimitry Andric Printf("%s\n", line); 67068d75effSDimitry Andric Printf("Suppressions used:\n"); 67168d75effSDimitry Andric Printf(" count bytes template\n"); 672e8d8bef9SDimitry Andric for (uptr i = 0; i < matched.size(); i++) { 673e8d8bef9SDimitry Andric Printf("%7zu %10zu %s\n", 674e8d8bef9SDimitry Andric static_cast<uptr>(atomic_load_relaxed(&matched[i]->hit_count)), 675e8d8bef9SDimitry Andric matched[i]->weight, matched[i]->templ); 676e8d8bef9SDimitry Andric } 67768d75effSDimitry Andric Printf("%s\n\n", line); 67868d75effSDimitry Andric } 67968d75effSDimitry Andric 6805ffd83dbSDimitry Andric # if SANITIZER_FUCHSIA 6815ffd83dbSDimitry Andric 6825ffd83dbSDimitry Andric // Fuchsia provides a libc interface that guarantees all threads are 6835ffd83dbSDimitry Andric // covered, and SuspendedThreadList is never really used. 6845ffd83dbSDimitry Andric static void ReportUnsuspendedThreads(const SuspendedThreadsList &) {} 6855ffd83dbSDimitry Andric 6865ffd83dbSDimitry Andric # else // !SANITIZER_FUCHSIA 6875ffd83dbSDimitry Andric 68868d75effSDimitry Andric static void ReportUnsuspendedThreads( 68968d75effSDimitry Andric const SuspendedThreadsList &suspended_threads) { 69068d75effSDimitry Andric InternalMmapVector<tid_t> threads(suspended_threads.ThreadCount()); 69168d75effSDimitry Andric for (uptr i = 0; i < suspended_threads.ThreadCount(); ++i) 69268d75effSDimitry Andric threads[i] = suspended_threads.GetThreadID(i); 69368d75effSDimitry Andric 69468d75effSDimitry Andric Sort(threads.data(), threads.size()); 69568d75effSDimitry Andric 696*bdd1243dSDimitry Andric InternalMmapVector<tid_t> unsuspended; 697*bdd1243dSDimitry Andric GetRunningThreadsLocked(&unsuspended); 698*bdd1243dSDimitry Andric 699*bdd1243dSDimitry Andric for (auto os_id : unsuspended) { 700*bdd1243dSDimitry Andric uptr i = InternalLowerBound(threads, os_id); 701*bdd1243dSDimitry Andric if (i >= threads.size() || threads[i] != os_id) 702*bdd1243dSDimitry Andric Report( 703*bdd1243dSDimitry Andric "Running thread %zu was not suspended. False leaks are possible.\n", 704*bdd1243dSDimitry Andric os_id); 705*bdd1243dSDimitry Andric } 70668d75effSDimitry Andric } 70768d75effSDimitry Andric 7085ffd83dbSDimitry Andric # endif // !SANITIZER_FUCHSIA 7095ffd83dbSDimitry Andric 71068d75effSDimitry Andric static void CheckForLeaksCallback(const SuspendedThreadsList &suspended_threads, 71168d75effSDimitry Andric void *arg) { 71268d75effSDimitry Andric CheckForLeaksParam *param = reinterpret_cast<CheckForLeaksParam *>(arg); 71368d75effSDimitry Andric CHECK(param); 71468d75effSDimitry Andric CHECK(!param->success); 71568d75effSDimitry Andric ReportUnsuspendedThreads(suspended_threads); 716*bdd1243dSDimitry Andric ClassifyAllChunks(suspended_threads, ¶m->frontier, param->caller_tid, 717*bdd1243dSDimitry Andric param->caller_sp); 7180eae32dcSDimitry Andric ForEachChunk(CollectLeaksCb, ¶m->leaks); 71968d75effSDimitry Andric // Clean up for subsequent leak checks. This assumes we did not overwrite any 72068d75effSDimitry Andric // kIgnored tags. 72168d75effSDimitry Andric ForEachChunk(ResetTagsCb, nullptr); 72268d75effSDimitry Andric param->success = true; 72368d75effSDimitry Andric } 72468d75effSDimitry Andric 725e8d8bef9SDimitry Andric static bool PrintResults(LeakReport &report) { 726e8d8bef9SDimitry Andric uptr unsuppressed_count = report.UnsuppressedLeakCount(); 727e8d8bef9SDimitry Andric if (unsuppressed_count) { 728e8d8bef9SDimitry Andric Decorator d; 729e8d8bef9SDimitry Andric Printf( 730e8d8bef9SDimitry Andric "\n" 731e8d8bef9SDimitry Andric "=================================================================" 732e8d8bef9SDimitry Andric "\n"); 733e8d8bef9SDimitry Andric Printf("%s", d.Error()); 734e8d8bef9SDimitry Andric Report("ERROR: LeakSanitizer: detected memory leaks\n"); 735e8d8bef9SDimitry Andric Printf("%s", d.Default()); 736e8d8bef9SDimitry Andric report.ReportTopLeaks(flags()->max_leaks); 737e8d8bef9SDimitry Andric } 738e8d8bef9SDimitry Andric if (common_flags()->print_suppressions) 739e8d8bef9SDimitry Andric GetSuppressionContext()->PrintMatchedSuppressions(); 740e8d8bef9SDimitry Andric if (unsuppressed_count > 0) { 741e8d8bef9SDimitry Andric report.PrintSummary(); 742e8d8bef9SDimitry Andric return true; 743e8d8bef9SDimitry Andric } 744e8d8bef9SDimitry Andric return false; 745e8d8bef9SDimitry Andric } 746e8d8bef9SDimitry Andric 74768d75effSDimitry Andric static bool CheckForLeaks() { 748*bdd1243dSDimitry Andric if (&__lsan_is_turned_off && __lsan_is_turned_off()) { 749*bdd1243dSDimitry Andric VReport(1, "LeakSanitizer is disabled"); 75068d75effSDimitry Andric return false; 751*bdd1243dSDimitry Andric } 752*bdd1243dSDimitry Andric VReport(1, "LeakSanitizer: checking for leaks"); 753e8d8bef9SDimitry Andric // Inside LockStuffAndStopTheWorld we can't run symbolizer, so we can't match 754e8d8bef9SDimitry Andric // suppressions. However if a stack id was previously suppressed, it should be 755e8d8bef9SDimitry Andric // suppressed in future checks as well. 756e8d8bef9SDimitry Andric for (int i = 0;; ++i) { 75768d75effSDimitry Andric EnsureMainThreadIDIsCorrect(); 75868d75effSDimitry Andric CheckForLeaksParam param; 759*bdd1243dSDimitry Andric // Capture calling thread's stack pointer early, to avoid false negatives. 760*bdd1243dSDimitry Andric // Old frame with dead pointers might be overlapped by new frame inside 761*bdd1243dSDimitry Andric // CheckForLeaks which does not use bytes with pointers before the 762*bdd1243dSDimitry Andric // threads are suspended and stack pointers captured. 763*bdd1243dSDimitry Andric param.caller_tid = GetTid(); 764*bdd1243dSDimitry Andric param.caller_sp = reinterpret_cast<uptr>(__builtin_frame_address(0)); 76568d75effSDimitry Andric LockStuffAndStopTheWorld(CheckForLeaksCallback, ¶m); 76668d75effSDimitry Andric if (!param.success) { 76768d75effSDimitry Andric Report("LeakSanitizer has encountered a fatal error.\n"); 76868d75effSDimitry Andric Report( 76968d75effSDimitry Andric "HINT: For debugging, try setting environment variable " 77068d75effSDimitry Andric "LSAN_OPTIONS=verbosity=1:log_threads=1\n"); 77168d75effSDimitry Andric Report( 772e8d8bef9SDimitry Andric "HINT: LeakSanitizer does not work under ptrace (strace, gdb, " 773e8d8bef9SDimitry Andric "etc)\n"); 77468d75effSDimitry Andric Die(); 77568d75effSDimitry Andric } 7760eae32dcSDimitry Andric LeakReport leak_report; 7770eae32dcSDimitry Andric leak_report.AddLeakedChunks(param.leaks); 7780eae32dcSDimitry Andric 779e8d8bef9SDimitry Andric // No new suppressions stacks, so rerun will not help and we can report. 7800eae32dcSDimitry Andric if (!leak_report.ApplySuppressions()) 7810eae32dcSDimitry Andric return PrintResults(leak_report); 782e8d8bef9SDimitry Andric 783e8d8bef9SDimitry Andric // No indirect leaks to report, so we are done here. 7840eae32dcSDimitry Andric if (!leak_report.IndirectUnsuppressedLeakCount()) 7850eae32dcSDimitry Andric return PrintResults(leak_report); 786e8d8bef9SDimitry Andric 787e8d8bef9SDimitry Andric if (i >= 8) { 788e8d8bef9SDimitry Andric Report("WARNING: LeakSanitizer gave up on indirect leaks suppression.\n"); 7890eae32dcSDimitry Andric return PrintResults(leak_report); 79068d75effSDimitry Andric } 791e8d8bef9SDimitry Andric 792e8d8bef9SDimitry Andric // We found a new previously unseen suppressed call stack. Rerun to make 793e8d8bef9SDimitry Andric // sure it does not hold indirect leaks. 794e8d8bef9SDimitry Andric VReport(1, "Rerun with %zu suppressed stacks.", 795e8d8bef9SDimitry Andric GetSuppressionContext()->GetSortedSuppressedStacks().size()); 79668d75effSDimitry Andric } 79768d75effSDimitry Andric } 79868d75effSDimitry Andric 79968d75effSDimitry Andric static bool has_reported_leaks = false; 80068d75effSDimitry Andric bool HasReportedLeaks() { return has_reported_leaks; } 80168d75effSDimitry Andric 80268d75effSDimitry Andric void DoLeakCheck() { 803349cc55cSDimitry Andric Lock l(&global_mutex); 80468d75effSDimitry Andric static bool already_done; 8050eae32dcSDimitry Andric if (already_done) 8060eae32dcSDimitry Andric return; 80768d75effSDimitry Andric already_done = true; 80868d75effSDimitry Andric has_reported_leaks = CheckForLeaks(); 8090eae32dcSDimitry Andric if (has_reported_leaks) 8100eae32dcSDimitry Andric HandleLeaks(); 81168d75effSDimitry Andric } 81268d75effSDimitry Andric 81368d75effSDimitry Andric static int DoRecoverableLeakCheck() { 814349cc55cSDimitry Andric Lock l(&global_mutex); 81568d75effSDimitry Andric bool have_leaks = CheckForLeaks(); 81668d75effSDimitry Andric return have_leaks ? 1 : 0; 81768d75effSDimitry Andric } 81868d75effSDimitry Andric 81968d75effSDimitry Andric void DoRecoverableLeakCheckVoid() { DoRecoverableLeakCheck(); } 82068d75effSDimitry Andric 82168d75effSDimitry Andric ///// LeakReport implementation. ///// 82268d75effSDimitry Andric 82368d75effSDimitry Andric // A hard limit on the number of distinct leaks, to avoid quadratic complexity 82468d75effSDimitry Andric // in LeakReport::AddLeakedChunk(). We don't expect to ever see this many leaks 82568d75effSDimitry Andric // in real-world applications. 8260eae32dcSDimitry Andric // FIXME: Get rid of this limit by moving logic into DedupLeaks. 82768d75effSDimitry Andric const uptr kMaxLeaksConsidered = 5000; 82868d75effSDimitry Andric 8290eae32dcSDimitry Andric void LeakReport::AddLeakedChunks(const LeakedChunks &chunks) { 8300eae32dcSDimitry Andric for (const LeakedChunk &leak : chunks) { 8310eae32dcSDimitry Andric uptr chunk = leak.chunk; 8320eae32dcSDimitry Andric u32 stack_trace_id = leak.stack_trace_id; 8330eae32dcSDimitry Andric uptr leaked_size = leak.leaked_size; 8340eae32dcSDimitry Andric ChunkTag tag = leak.tag; 83568d75effSDimitry Andric CHECK(tag == kDirectlyLeaked || tag == kIndirectlyLeaked); 836349cc55cSDimitry Andric 837349cc55cSDimitry Andric if (u32 resolution = flags()->resolution) { 838349cc55cSDimitry Andric StackTrace stack = StackDepotGet(stack_trace_id); 839349cc55cSDimitry Andric stack.size = Min(stack.size, resolution); 840349cc55cSDimitry Andric stack_trace_id = StackDepotPut(stack); 841349cc55cSDimitry Andric } 842349cc55cSDimitry Andric 84368d75effSDimitry Andric bool is_directly_leaked = (tag == kDirectlyLeaked); 84468d75effSDimitry Andric uptr i; 84568d75effSDimitry Andric for (i = 0; i < leaks_.size(); i++) { 84668d75effSDimitry Andric if (leaks_[i].stack_trace_id == stack_trace_id && 84768d75effSDimitry Andric leaks_[i].is_directly_leaked == is_directly_leaked) { 84868d75effSDimitry Andric leaks_[i].hit_count++; 84968d75effSDimitry Andric leaks_[i].total_size += leaked_size; 85068d75effSDimitry Andric break; 85168d75effSDimitry Andric } 85268d75effSDimitry Andric } 85368d75effSDimitry Andric if (i == leaks_.size()) { 8540eae32dcSDimitry Andric if (leaks_.size() == kMaxLeaksConsidered) 8550eae32dcSDimitry Andric return; 8560eae32dcSDimitry Andric Leak leak = {next_id_++, /* hit_count */ 1, 8570eae32dcSDimitry Andric leaked_size, stack_trace_id, 85868d75effSDimitry Andric is_directly_leaked, /* is_suppressed */ false}; 85968d75effSDimitry Andric leaks_.push_back(leak); 86068d75effSDimitry Andric } 86168d75effSDimitry Andric if (flags()->report_objects) { 86268d75effSDimitry Andric LeakedObject obj = {leaks_[i].id, chunk, leaked_size}; 86368d75effSDimitry Andric leaked_objects_.push_back(obj); 86468d75effSDimitry Andric } 86568d75effSDimitry Andric } 8660eae32dcSDimitry Andric } 86768d75effSDimitry Andric 86868d75effSDimitry Andric static bool LeakComparator(const Leak &leak1, const Leak &leak2) { 86968d75effSDimitry Andric if (leak1.is_directly_leaked == leak2.is_directly_leaked) 87068d75effSDimitry Andric return leak1.total_size > leak2.total_size; 87168d75effSDimitry Andric else 87268d75effSDimitry Andric return leak1.is_directly_leaked; 87368d75effSDimitry Andric } 87468d75effSDimitry Andric 87568d75effSDimitry Andric void LeakReport::ReportTopLeaks(uptr num_leaks_to_report) { 87668d75effSDimitry Andric CHECK(leaks_.size() <= kMaxLeaksConsidered); 87768d75effSDimitry Andric Printf("\n"); 87868d75effSDimitry Andric if (leaks_.size() == kMaxLeaksConsidered) 8790eae32dcSDimitry Andric Printf( 8800eae32dcSDimitry Andric "Too many leaks! Only the first %zu leaks encountered will be " 88168d75effSDimitry Andric "reported.\n", 88268d75effSDimitry Andric kMaxLeaksConsidered); 88368d75effSDimitry Andric 88468d75effSDimitry Andric uptr unsuppressed_count = UnsuppressedLeakCount(); 88568d75effSDimitry Andric if (num_leaks_to_report > 0 && num_leaks_to_report < unsuppressed_count) 88668d75effSDimitry Andric Printf("The %zu top leak(s):\n", num_leaks_to_report); 88768d75effSDimitry Andric Sort(leaks_.data(), leaks_.size(), &LeakComparator); 88868d75effSDimitry Andric uptr leaks_reported = 0; 88968d75effSDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) { 8900eae32dcSDimitry Andric if (leaks_[i].is_suppressed) 8910eae32dcSDimitry Andric continue; 89268d75effSDimitry Andric PrintReportForLeak(i); 89368d75effSDimitry Andric leaks_reported++; 8940eae32dcSDimitry Andric if (leaks_reported == num_leaks_to_report) 8950eae32dcSDimitry Andric break; 89668d75effSDimitry Andric } 89768d75effSDimitry Andric if (leaks_reported < unsuppressed_count) { 89868d75effSDimitry Andric uptr remaining = unsuppressed_count - leaks_reported; 89968d75effSDimitry Andric Printf("Omitting %zu more leak(s).\n", remaining); 90068d75effSDimitry Andric } 90168d75effSDimitry Andric } 90268d75effSDimitry Andric 90368d75effSDimitry Andric void LeakReport::PrintReportForLeak(uptr index) { 90468d75effSDimitry Andric Decorator d; 90568d75effSDimitry Andric Printf("%s", d.Leak()); 90668d75effSDimitry Andric Printf("%s leak of %zu byte(s) in %zu object(s) allocated from:\n", 90768d75effSDimitry Andric leaks_[index].is_directly_leaked ? "Direct" : "Indirect", 90868d75effSDimitry Andric leaks_[index].total_size, leaks_[index].hit_count); 90968d75effSDimitry Andric Printf("%s", d.Default()); 91068d75effSDimitry Andric 911349cc55cSDimitry Andric CHECK(leaks_[index].stack_trace_id); 912349cc55cSDimitry Andric StackDepotGet(leaks_[index].stack_trace_id).Print(); 91368d75effSDimitry Andric 91468d75effSDimitry Andric if (flags()->report_objects) { 91568d75effSDimitry Andric Printf("Objects leaked above:\n"); 91668d75effSDimitry Andric PrintLeakedObjectsForLeak(index); 91768d75effSDimitry Andric Printf("\n"); 91868d75effSDimitry Andric } 91968d75effSDimitry Andric } 92068d75effSDimitry Andric 92168d75effSDimitry Andric void LeakReport::PrintLeakedObjectsForLeak(uptr index) { 92268d75effSDimitry Andric u32 leak_id = leaks_[index].id; 92368d75effSDimitry Andric for (uptr j = 0; j < leaked_objects_.size(); j++) { 92468d75effSDimitry Andric if (leaked_objects_[j].leak_id == leak_id) 925349cc55cSDimitry Andric Printf("%p (%zu bytes)\n", (void *)leaked_objects_[j].addr, 92668d75effSDimitry Andric leaked_objects_[j].size); 92768d75effSDimitry Andric } 92868d75effSDimitry Andric } 92968d75effSDimitry Andric 93068d75effSDimitry Andric void LeakReport::PrintSummary() { 93168d75effSDimitry Andric CHECK(leaks_.size() <= kMaxLeaksConsidered); 93268d75effSDimitry Andric uptr bytes = 0, allocations = 0; 93368d75effSDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) { 9340eae32dcSDimitry Andric if (leaks_[i].is_suppressed) 9350eae32dcSDimitry Andric continue; 93668d75effSDimitry Andric bytes += leaks_[i].total_size; 93768d75effSDimitry Andric allocations += leaks_[i].hit_count; 93868d75effSDimitry Andric } 939fe6060f1SDimitry Andric InternalScopedString summary; 94068d75effSDimitry Andric summary.append("%zu byte(s) leaked in %zu allocation(s).", bytes, 94168d75effSDimitry Andric allocations); 94268d75effSDimitry Andric ReportErrorSummary(summary.data()); 94368d75effSDimitry Andric } 94468d75effSDimitry Andric 945e8d8bef9SDimitry Andric uptr LeakReport::ApplySuppressions() { 946e8d8bef9SDimitry Andric LeakSuppressionContext *suppressions = GetSuppressionContext(); 947e8d8bef9SDimitry Andric uptr new_suppressions = false; 94868d75effSDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) { 9490eae32dcSDimitry Andric if (suppressions->Suppress(leaks_[i].stack_trace_id, leaks_[i].hit_count, 9500eae32dcSDimitry Andric leaks_[i].total_size)) { 95168d75effSDimitry Andric leaks_[i].is_suppressed = true; 952e8d8bef9SDimitry Andric ++new_suppressions; 95368d75effSDimitry Andric } 95468d75effSDimitry Andric } 955e8d8bef9SDimitry Andric return new_suppressions; 95668d75effSDimitry Andric } 95768d75effSDimitry Andric 95868d75effSDimitry Andric uptr LeakReport::UnsuppressedLeakCount() { 95968d75effSDimitry Andric uptr result = 0; 96068d75effSDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) 9610eae32dcSDimitry Andric if (!leaks_[i].is_suppressed) 9620eae32dcSDimitry Andric result++; 96368d75effSDimitry Andric return result; 96468d75effSDimitry Andric } 96568d75effSDimitry Andric 966e8d8bef9SDimitry Andric uptr LeakReport::IndirectUnsuppressedLeakCount() { 967e8d8bef9SDimitry Andric uptr result = 0; 968e8d8bef9SDimitry Andric for (uptr i = 0; i < leaks_.size(); i++) 969e8d8bef9SDimitry Andric if (!leaks_[i].is_suppressed && !leaks_[i].is_directly_leaked) 970e8d8bef9SDimitry Andric result++; 971e8d8bef9SDimitry Andric return result; 972e8d8bef9SDimitry Andric } 973e8d8bef9SDimitry Andric 97468d75effSDimitry Andric } // namespace __lsan 97568d75effSDimitry Andric #else // CAN_SANITIZE_LEAKS 97668d75effSDimitry Andric namespace __lsan { 97768d75effSDimitry Andric void InitCommonLsan() {} 97868d75effSDimitry Andric void DoLeakCheck() {} 97968d75effSDimitry Andric void DoRecoverableLeakCheckVoid() {} 98068d75effSDimitry Andric void DisableInThisThread() {} 98168d75effSDimitry Andric void EnableInThisThread() {} 9820eae32dcSDimitry Andric } // namespace __lsan 98368d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 98468d75effSDimitry Andric 98568d75effSDimitry Andric using namespace __lsan; 98668d75effSDimitry Andric 98768d75effSDimitry Andric extern "C" { 98868d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 98968d75effSDimitry Andric void __lsan_ignore_object(const void *p) { 99068d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 99168d75effSDimitry Andric if (!common_flags()->detect_leaks) 99268d75effSDimitry Andric return; 99368d75effSDimitry Andric // Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not 99468d75effSDimitry Andric // locked. 995349cc55cSDimitry Andric Lock l(&global_mutex); 99668d75effSDimitry Andric IgnoreObjectResult res = IgnoreObjectLocked(p); 99768d75effSDimitry Andric if (res == kIgnoreObjectInvalid) 998fcaf7f86SDimitry Andric VReport(1, "__lsan_ignore_object(): no heap object found at %p\n", p); 99968d75effSDimitry Andric if (res == kIgnoreObjectAlreadyIgnored) 10000eae32dcSDimitry Andric VReport(1, 10010eae32dcSDimitry Andric "__lsan_ignore_object(): " 10020eae32dcSDimitry Andric "heap object at %p is already being ignored\n", 10030eae32dcSDimitry Andric p); 100468d75effSDimitry Andric if (res == kIgnoreObjectSuccess) 100568d75effSDimitry Andric VReport(1, "__lsan_ignore_object(): ignoring heap object at %p\n", p); 100668d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 100768d75effSDimitry Andric } 100868d75effSDimitry Andric 100968d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 101068d75effSDimitry Andric void __lsan_register_root_region(const void *begin, uptr size) { 101168d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 1012349cc55cSDimitry Andric Lock l(&global_mutex); 101368d75effSDimitry Andric RootRegion region = {reinterpret_cast<uptr>(begin), size}; 1014349cc55cSDimitry Andric root_regions.push_back(region); 1015349cc55cSDimitry Andric VReport(1, "Registered root region at %p of size %zu\n", begin, size); 101668d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 101768d75effSDimitry Andric } 101868d75effSDimitry Andric 101968d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 102068d75effSDimitry Andric void __lsan_unregister_root_region(const void *begin, uptr size) { 102168d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 1022349cc55cSDimitry Andric Lock l(&global_mutex); 102368d75effSDimitry Andric bool removed = false; 1024349cc55cSDimitry Andric for (uptr i = 0; i < root_regions.size(); i++) { 1025349cc55cSDimitry Andric RootRegion region = root_regions[i]; 102668d75effSDimitry Andric if (region.begin == reinterpret_cast<uptr>(begin) && region.size == size) { 102768d75effSDimitry Andric removed = true; 1028349cc55cSDimitry Andric uptr last_index = root_regions.size() - 1; 1029349cc55cSDimitry Andric root_regions[i] = root_regions[last_index]; 1030349cc55cSDimitry Andric root_regions.pop_back(); 1031349cc55cSDimitry Andric VReport(1, "Unregistered root region at %p of size %zu\n", begin, size); 103268d75effSDimitry Andric break; 103368d75effSDimitry Andric } 103468d75effSDimitry Andric } 103568d75effSDimitry Andric if (!removed) { 103668d75effSDimitry Andric Report( 1037349cc55cSDimitry Andric "__lsan_unregister_root_region(): region at %p of size %zu has not " 103868d75effSDimitry Andric "been registered.\n", 103968d75effSDimitry Andric begin, size); 104068d75effSDimitry Andric Die(); 104168d75effSDimitry Andric } 104268d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 104368d75effSDimitry Andric } 104468d75effSDimitry Andric 104568d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 104668d75effSDimitry Andric void __lsan_disable() { 104768d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 104868d75effSDimitry Andric __lsan::DisableInThisThread(); 104968d75effSDimitry Andric #endif 105068d75effSDimitry Andric } 105168d75effSDimitry Andric 105268d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 105368d75effSDimitry Andric void __lsan_enable() { 105468d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 105568d75effSDimitry Andric __lsan::EnableInThisThread(); 105668d75effSDimitry Andric #endif 105768d75effSDimitry Andric } 105868d75effSDimitry Andric 105968d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 106068d75effSDimitry Andric void __lsan_do_leak_check() { 106168d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 106268d75effSDimitry Andric if (common_flags()->detect_leaks) 106368d75effSDimitry Andric __lsan::DoLeakCheck(); 106468d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 106568d75effSDimitry Andric } 106668d75effSDimitry Andric 106768d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE 106868d75effSDimitry Andric int __lsan_do_recoverable_leak_check() { 106968d75effSDimitry Andric #if CAN_SANITIZE_LEAKS 107068d75effSDimitry Andric if (common_flags()->detect_leaks) 107168d75effSDimitry Andric return __lsan::DoRecoverableLeakCheck(); 107268d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS 107368d75effSDimitry Andric return 0; 107468d75effSDimitry Andric } 107568d75effSDimitry Andric 1076e8d8bef9SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(const char *, __lsan_default_options, void) { 107768d75effSDimitry Andric return ""; 107868d75effSDimitry Andric } 107968d75effSDimitry Andric 1080e8d8bef9SDimitry Andric #if !SANITIZER_SUPPORTS_WEAK_HOOKS 108181ad6265SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(int, __lsan_is_turned_off, void) { 108268d75effSDimitry Andric return 0; 108368d75effSDimitry Andric } 108468d75effSDimitry Andric 108581ad6265SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(const char *, __lsan_default_suppressions, void) { 108668d75effSDimitry Andric return ""; 108768d75effSDimitry Andric } 108868d75effSDimitry Andric #endif 108968d75effSDimitry Andric } // extern "C" 1090