xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/lsan/lsan_common.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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.
33*349cc55cSDimitry Andric Mutex global_mutex;
3468d75effSDimitry Andric 
3568d75effSDimitry Andric Flags lsan_flags;
3668d75effSDimitry Andric 
375ffd83dbSDimitry Andric 
3868d75effSDimitry Andric void DisableCounterUnderflow() {
3968d75effSDimitry Andric   if (common_flags()->detect_leaks) {
4068d75effSDimitry Andric     Report("Unmatched call to __lsan_enable().\n");
4168d75effSDimitry Andric     Die();
4268d75effSDimitry Andric   }
4368d75effSDimitry Andric }
4468d75effSDimitry Andric 
4568d75effSDimitry Andric void Flags::SetDefaults() {
4668d75effSDimitry Andric #define LSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
4768d75effSDimitry Andric #include "lsan_flags.inc"
4868d75effSDimitry Andric #undef LSAN_FLAG
4968d75effSDimitry Andric }
5068d75effSDimitry Andric 
5168d75effSDimitry Andric void RegisterLsanFlags(FlagParser *parser, Flags *f) {
5268d75effSDimitry Andric #define LSAN_FLAG(Type, Name, DefaultValue, Description) \
5368d75effSDimitry Andric   RegisterFlag(parser, #Name, Description, &f->Name);
5468d75effSDimitry Andric #include "lsan_flags.inc"
5568d75effSDimitry Andric #undef LSAN_FLAG
5668d75effSDimitry Andric }
5768d75effSDimitry Andric 
5868d75effSDimitry Andric #define LOG_POINTERS(...)                           \
5968d75effSDimitry Andric   do {                                              \
6068d75effSDimitry Andric     if (flags()->log_pointers) Report(__VA_ARGS__); \
6168d75effSDimitry Andric   } while (0)
6268d75effSDimitry Andric 
6368d75effSDimitry Andric #define LOG_THREADS(...)                           \
6468d75effSDimitry Andric   do {                                             \
6568d75effSDimitry Andric     if (flags()->log_threads) Report(__VA_ARGS__); \
6668d75effSDimitry Andric   } while (0)
6768d75effSDimitry Andric 
68e8d8bef9SDimitry Andric class LeakSuppressionContext {
69e8d8bef9SDimitry Andric   bool parsed = false;
70e8d8bef9SDimitry Andric   SuppressionContext context;
71e8d8bef9SDimitry Andric   bool suppressed_stacks_sorted = true;
72e8d8bef9SDimitry Andric   InternalMmapVector<u32> suppressed_stacks;
73e8d8bef9SDimitry Andric 
74e8d8bef9SDimitry Andric   Suppression *GetSuppressionForAddr(uptr addr);
75e8d8bef9SDimitry Andric   void LazyInit();
76e8d8bef9SDimitry Andric 
77e8d8bef9SDimitry Andric  public:
78e8d8bef9SDimitry Andric   LeakSuppressionContext(const char *supprression_types[],
79e8d8bef9SDimitry Andric                          int suppression_types_num)
80e8d8bef9SDimitry Andric       : context(supprression_types, suppression_types_num) {}
81e8d8bef9SDimitry Andric 
82*349cc55cSDimitry Andric   Suppression *GetSuppressionForStack(u32 stack_trace_id,
83*349cc55cSDimitry Andric                                       const StackTrace &stack);
84e8d8bef9SDimitry Andric 
85e8d8bef9SDimitry Andric   const InternalMmapVector<u32> &GetSortedSuppressedStacks() {
86e8d8bef9SDimitry Andric     if (!suppressed_stacks_sorted) {
87e8d8bef9SDimitry Andric       suppressed_stacks_sorted = true;
88e8d8bef9SDimitry Andric       SortAndDedup(suppressed_stacks);
89e8d8bef9SDimitry Andric     }
90e8d8bef9SDimitry Andric     return suppressed_stacks;
91e8d8bef9SDimitry Andric   }
92e8d8bef9SDimitry Andric   void PrintMatchedSuppressions();
93e8d8bef9SDimitry Andric };
94e8d8bef9SDimitry Andric 
95e8d8bef9SDimitry Andric ALIGNED(64) static char suppression_placeholder[sizeof(LeakSuppressionContext)];
96e8d8bef9SDimitry Andric static LeakSuppressionContext *suppression_ctx = nullptr;
9768d75effSDimitry Andric static const char kSuppressionLeak[] = "leak";
9868d75effSDimitry Andric static const char *kSuppressionTypes[] = { kSuppressionLeak };
9968d75effSDimitry Andric static const char kStdSuppressions[] =
10068d75effSDimitry Andric #if SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
10168d75effSDimitry Andric     // For more details refer to the SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
10268d75effSDimitry Andric     // definition.
10368d75effSDimitry Andric     "leak:*pthread_exit*\n"
10468d75effSDimitry Andric #endif  // SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
10568d75effSDimitry Andric #if SANITIZER_MAC
10668d75effSDimitry Andric     // For Darwin and os_log/os_trace: https://reviews.llvm.org/D35173
10768d75effSDimitry Andric     "leak:*_os_trace*\n"
10868d75effSDimitry Andric #endif
10968d75effSDimitry Andric     // TLS leak in some glibc versions, described in
11068d75effSDimitry Andric     // https://sourceware.org/bugzilla/show_bug.cgi?id=12650.
11168d75effSDimitry Andric     "leak:*tls_get_addr*\n";
11268d75effSDimitry Andric 
11368d75effSDimitry Andric void InitializeSuppressions() {
11468d75effSDimitry Andric   CHECK_EQ(nullptr, suppression_ctx);
11568d75effSDimitry Andric   suppression_ctx = new (suppression_placeholder)
116e8d8bef9SDimitry Andric       LeakSuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
11768d75effSDimitry Andric }
11868d75effSDimitry Andric 
119e8d8bef9SDimitry Andric void LeakSuppressionContext::LazyInit() {
120e8d8bef9SDimitry Andric   if (!parsed) {
121e8d8bef9SDimitry Andric     parsed = true;
122e8d8bef9SDimitry Andric     context.ParseFromFile(flags()->suppressions);
123e8d8bef9SDimitry Andric     if (&__lsan_default_suppressions)
124e8d8bef9SDimitry Andric       context.Parse(__lsan_default_suppressions());
125e8d8bef9SDimitry Andric     context.Parse(kStdSuppressions);
126e8d8bef9SDimitry Andric   }
127e8d8bef9SDimitry Andric }
128e8d8bef9SDimitry Andric 
129e8d8bef9SDimitry Andric static LeakSuppressionContext *GetSuppressionContext() {
13068d75effSDimitry Andric   CHECK(suppression_ctx);
13168d75effSDimitry Andric   return suppression_ctx;
13268d75effSDimitry Andric }
13368d75effSDimitry Andric 
134*349cc55cSDimitry Andric static InternalMmapVectorNoCtor<RootRegion> root_regions;
13568d75effSDimitry Andric 
136*349cc55cSDimitry Andric InternalMmapVectorNoCtor<RootRegion> const *GetRootRegions() {
137*349cc55cSDimitry Andric   return &root_regions;
13868d75effSDimitry Andric }
13968d75effSDimitry Andric 
14068d75effSDimitry Andric void InitCommonLsan() {
14168d75effSDimitry Andric   if (common_flags()->detect_leaks) {
14268d75effSDimitry Andric     // Initialization which can fail or print warnings should only be done if
14368d75effSDimitry Andric     // LSan is actually enabled.
14468d75effSDimitry Andric     InitializeSuppressions();
14568d75effSDimitry Andric     InitializePlatformSpecificModules();
14668d75effSDimitry Andric   }
14768d75effSDimitry Andric }
14868d75effSDimitry Andric 
14968d75effSDimitry Andric class Decorator: public __sanitizer::SanitizerCommonDecorator {
15068d75effSDimitry Andric  public:
15168d75effSDimitry Andric   Decorator() : SanitizerCommonDecorator() { }
15268d75effSDimitry Andric   const char *Error() { return Red(); }
15368d75effSDimitry Andric   const char *Leak() { return Blue(); }
15468d75effSDimitry Andric };
15568d75effSDimitry Andric 
15668d75effSDimitry Andric static inline bool CanBeAHeapPointer(uptr p) {
15768d75effSDimitry Andric   // Since our heap is located in mmap-ed memory, we can assume a sensible lower
15868d75effSDimitry Andric   // bound on heap addresses.
15968d75effSDimitry Andric   const uptr kMinAddress = 4 * 4096;
16068d75effSDimitry Andric   if (p < kMinAddress) return false;
16168d75effSDimitry Andric #if defined(__x86_64__)
16268d75effSDimitry Andric   // Accept only canonical form user-space addresses.
16368d75effSDimitry Andric   return ((p >> 47) == 0);
16468d75effSDimitry Andric #elif defined(__mips64)
16568d75effSDimitry Andric   return ((p >> 40) == 0);
16668d75effSDimitry Andric #elif defined(__aarch64__)
16768d75effSDimitry Andric   unsigned runtimeVMA =
16868d75effSDimitry Andric     (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1);
16968d75effSDimitry Andric   return ((p >> runtimeVMA) == 0);
17068d75effSDimitry Andric #else
17168d75effSDimitry Andric   return true;
17268d75effSDimitry Andric #endif
17368d75effSDimitry Andric }
17468d75effSDimitry Andric 
17568d75effSDimitry Andric // Scans the memory range, looking for byte patterns that point into allocator
17668d75effSDimitry Andric // chunks. Marks those chunks with |tag| and adds them to |frontier|.
17768d75effSDimitry Andric // There are two usage modes for this function: finding reachable chunks
17868d75effSDimitry Andric // (|tag| = kReachable) and finding indirectly leaked chunks
17968d75effSDimitry Andric // (|tag| = kIndirectlyLeaked). In the second case, there's no flood fill,
18068d75effSDimitry Andric // so |frontier| = 0.
18168d75effSDimitry Andric void ScanRangeForPointers(uptr begin, uptr end,
18268d75effSDimitry Andric                           Frontier *frontier,
18368d75effSDimitry Andric                           const char *region_type, ChunkTag tag) {
18468d75effSDimitry Andric   CHECK(tag == kReachable || tag == kIndirectlyLeaked);
18568d75effSDimitry Andric   const uptr alignment = flags()->pointer_alignment();
186*349cc55cSDimitry Andric   LOG_POINTERS("Scanning %s range %p-%p.\n", region_type, (void *)begin,
187*349cc55cSDimitry Andric                (void *)end);
18868d75effSDimitry Andric   uptr pp = begin;
18968d75effSDimitry Andric   if (pp % alignment)
19068d75effSDimitry Andric     pp = pp + alignment - pp % alignment;
19168d75effSDimitry Andric   for (; pp + sizeof(void *) <= end; pp += alignment) {
19268d75effSDimitry Andric     void *p = *reinterpret_cast<void **>(pp);
19368d75effSDimitry Andric     if (!CanBeAHeapPointer(reinterpret_cast<uptr>(p))) continue;
19468d75effSDimitry Andric     uptr chunk = PointsIntoChunk(p);
19568d75effSDimitry Andric     if (!chunk) continue;
19668d75effSDimitry Andric     // Pointers to self don't count. This matters when tag == kIndirectlyLeaked.
19768d75effSDimitry Andric     if (chunk == begin) continue;
19868d75effSDimitry Andric     LsanMetadata m(chunk);
19968d75effSDimitry Andric     if (m.tag() == kReachable || m.tag() == kIgnored) continue;
20068d75effSDimitry Andric 
20168d75effSDimitry Andric     // Do this check relatively late so we can log only the interesting cases.
20268d75effSDimitry Andric     if (!flags()->use_poisoned && WordIsPoisoned(pp)) {
20368d75effSDimitry Andric       LOG_POINTERS(
20468d75effSDimitry Andric           "%p is poisoned: ignoring %p pointing into chunk %p-%p of size "
20568d75effSDimitry Andric           "%zu.\n",
206*349cc55cSDimitry Andric           (void *)pp, p, (void *)chunk, (void *)(chunk + m.requested_size()),
207*349cc55cSDimitry Andric           m.requested_size());
20868d75effSDimitry Andric       continue;
20968d75effSDimitry Andric     }
21068d75effSDimitry Andric 
21168d75effSDimitry Andric     m.set_tag(tag);
212*349cc55cSDimitry Andric     LOG_POINTERS("%p: found %p pointing into chunk %p-%p of size %zu.\n",
213*349cc55cSDimitry Andric                  (void *)pp, p, (void *)chunk,
214*349cc55cSDimitry Andric                  (void *)(chunk + m.requested_size()), m.requested_size());
21568d75effSDimitry Andric     if (frontier)
21668d75effSDimitry Andric       frontier->push_back(chunk);
21768d75effSDimitry Andric   }
21868d75effSDimitry Andric }
21968d75effSDimitry Andric 
22068d75effSDimitry Andric // Scans a global range for pointers
22168d75effSDimitry Andric void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier) {
22268d75effSDimitry Andric   uptr allocator_begin = 0, allocator_end = 0;
22368d75effSDimitry Andric   GetAllocatorGlobalRange(&allocator_begin, &allocator_end);
22468d75effSDimitry Andric   if (begin <= allocator_begin && allocator_begin < end) {
22568d75effSDimitry Andric     CHECK_LE(allocator_begin, allocator_end);
22668d75effSDimitry Andric     CHECK_LE(allocator_end, end);
22768d75effSDimitry Andric     if (begin < allocator_begin)
22868d75effSDimitry Andric       ScanRangeForPointers(begin, allocator_begin, frontier, "GLOBAL",
22968d75effSDimitry Andric                            kReachable);
23068d75effSDimitry Andric     if (allocator_end < end)
23168d75effSDimitry Andric       ScanRangeForPointers(allocator_end, end, frontier, "GLOBAL", kReachable);
23268d75effSDimitry Andric   } else {
23368d75effSDimitry Andric     ScanRangeForPointers(begin, end, frontier, "GLOBAL", kReachable);
23468d75effSDimitry Andric   }
23568d75effSDimitry Andric }
23668d75effSDimitry Andric 
23768d75effSDimitry Andric void ForEachExtraStackRangeCb(uptr begin, uptr end, void* arg) {
23868d75effSDimitry Andric   Frontier *frontier = reinterpret_cast<Frontier *>(arg);
23968d75effSDimitry Andric   ScanRangeForPointers(begin, end, frontier, "FAKE STACK", kReachable);
24068d75effSDimitry Andric }
24168d75effSDimitry Andric 
2425ffd83dbSDimitry Andric #if SANITIZER_FUCHSIA
2435ffd83dbSDimitry Andric 
2445ffd83dbSDimitry Andric // Fuchsia handles all threads together with its own callback.
2455ffd83dbSDimitry Andric static void ProcessThreads(SuspendedThreadsList const &, Frontier *) {}
2465ffd83dbSDimitry Andric 
2475ffd83dbSDimitry Andric #else
2485ffd83dbSDimitry Andric 
249e8d8bef9SDimitry Andric #if SANITIZER_ANDROID
250e8d8bef9SDimitry Andric // FIXME: Move this out into *libcdep.cpp
251e8d8bef9SDimitry Andric extern "C" SANITIZER_WEAK_ATTRIBUTE void __libc_iterate_dynamic_tls(
252e8d8bef9SDimitry Andric     pid_t, void (*cb)(void *, void *, uptr, void *), void *);
253e8d8bef9SDimitry Andric #endif
254e8d8bef9SDimitry Andric 
255e8d8bef9SDimitry Andric static void ProcessThreadRegistry(Frontier *frontier) {
256e8d8bef9SDimitry Andric   InternalMmapVector<uptr> ptrs;
257e8d8bef9SDimitry Andric   GetThreadRegistryLocked()->RunCallbackForEachThreadLocked(
258e8d8bef9SDimitry Andric       GetAdditionalThreadContextPtrs, &ptrs);
259e8d8bef9SDimitry Andric 
260e8d8bef9SDimitry Andric   for (uptr i = 0; i < ptrs.size(); ++i) {
261e8d8bef9SDimitry Andric     void *ptr = reinterpret_cast<void *>(ptrs[i]);
262e8d8bef9SDimitry Andric     uptr chunk = PointsIntoChunk(ptr);
263e8d8bef9SDimitry Andric     if (!chunk)
264e8d8bef9SDimitry Andric       continue;
265e8d8bef9SDimitry Andric     LsanMetadata m(chunk);
266e8d8bef9SDimitry Andric     if (!m.allocated())
267e8d8bef9SDimitry Andric       continue;
268e8d8bef9SDimitry Andric 
269e8d8bef9SDimitry Andric     // Mark as reachable and add to frontier.
270e8d8bef9SDimitry Andric     LOG_POINTERS("Treating pointer %p from ThreadContext as reachable\n", ptr);
271e8d8bef9SDimitry Andric     m.set_tag(kReachable);
272e8d8bef9SDimitry Andric     frontier->push_back(chunk);
273e8d8bef9SDimitry Andric   }
274e8d8bef9SDimitry Andric }
275e8d8bef9SDimitry Andric 
27668d75effSDimitry Andric // Scans thread data (stacks and TLS) for heap pointers.
27768d75effSDimitry Andric static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
27868d75effSDimitry Andric                            Frontier *frontier) {
279e8d8bef9SDimitry Andric   InternalMmapVector<uptr> registers;
28068d75effSDimitry Andric   for (uptr i = 0; i < suspended_threads.ThreadCount(); i++) {
28168d75effSDimitry Andric     tid_t os_id = static_cast<tid_t>(suspended_threads.GetThreadID(i));
282*349cc55cSDimitry Andric     LOG_THREADS("Processing thread %llu.\n", os_id);
28368d75effSDimitry Andric     uptr stack_begin, stack_end, tls_begin, tls_end, cache_begin, cache_end;
28468d75effSDimitry Andric     DTLS *dtls;
28568d75effSDimitry Andric     bool thread_found = GetThreadRangesLocked(os_id, &stack_begin, &stack_end,
28668d75effSDimitry Andric                                               &tls_begin, &tls_end,
28768d75effSDimitry Andric                                               &cache_begin, &cache_end, &dtls);
28868d75effSDimitry Andric     if (!thread_found) {
28968d75effSDimitry Andric       // If a thread can't be found in the thread registry, it's probably in the
29068d75effSDimitry Andric       // process of destruction. Log this event and move on.
291*349cc55cSDimitry Andric       LOG_THREADS("Thread %llu not found in registry.\n", os_id);
29268d75effSDimitry Andric       continue;
29368d75effSDimitry Andric     }
29468d75effSDimitry Andric     uptr sp;
29568d75effSDimitry Andric     PtraceRegistersStatus have_registers =
296e8d8bef9SDimitry Andric         suspended_threads.GetRegistersAndSP(i, &registers, &sp);
29768d75effSDimitry Andric     if (have_registers != REGISTERS_AVAILABLE) {
298*349cc55cSDimitry Andric       Report("Unable to get registers from thread %llu.\n", os_id);
29968d75effSDimitry Andric       // If unable to get SP, consider the entire stack to be reachable unless
30068d75effSDimitry Andric       // GetRegistersAndSP failed with ESRCH.
30168d75effSDimitry Andric       if (have_registers == REGISTERS_UNAVAILABLE_FATAL) continue;
30268d75effSDimitry Andric       sp = stack_begin;
30368d75effSDimitry Andric     }
30468d75effSDimitry Andric 
305e8d8bef9SDimitry Andric     if (flags()->use_registers && have_registers) {
306e8d8bef9SDimitry Andric       uptr registers_begin = reinterpret_cast<uptr>(registers.data());
307e8d8bef9SDimitry Andric       uptr registers_end =
308e8d8bef9SDimitry Andric           reinterpret_cast<uptr>(registers.data() + registers.size());
30968d75effSDimitry Andric       ScanRangeForPointers(registers_begin, registers_end, frontier,
31068d75effSDimitry Andric                            "REGISTERS", kReachable);
311e8d8bef9SDimitry Andric     }
31268d75effSDimitry Andric 
31368d75effSDimitry Andric     if (flags()->use_stacks) {
314*349cc55cSDimitry Andric       LOG_THREADS("Stack at %p-%p (SP = %p).\n", (void *)stack_begin,
315*349cc55cSDimitry Andric                   (void *)stack_end, (void *)sp);
31668d75effSDimitry Andric       if (sp < stack_begin || sp >= stack_end) {
31768d75effSDimitry Andric         // SP is outside the recorded stack range (e.g. the thread is running a
31868d75effSDimitry Andric         // signal handler on alternate stack, or swapcontext was used).
31968d75effSDimitry Andric         // Again, consider the entire stack range to be reachable.
32068d75effSDimitry Andric         LOG_THREADS("WARNING: stack pointer not in stack range.\n");
32168d75effSDimitry Andric         uptr page_size = GetPageSizeCached();
32268d75effSDimitry Andric         int skipped = 0;
32368d75effSDimitry Andric         while (stack_begin < stack_end &&
32468d75effSDimitry Andric                !IsAccessibleMemoryRange(stack_begin, 1)) {
32568d75effSDimitry Andric           skipped++;
32668d75effSDimitry Andric           stack_begin += page_size;
32768d75effSDimitry Andric         }
32868d75effSDimitry Andric         LOG_THREADS("Skipped %d guard page(s) to obtain stack %p-%p.\n",
329*349cc55cSDimitry Andric                     skipped, (void *)stack_begin, (void *)stack_end);
33068d75effSDimitry Andric       } else {
33168d75effSDimitry Andric         // Shrink the stack range to ignore out-of-scope values.
33268d75effSDimitry Andric         stack_begin = sp;
33368d75effSDimitry Andric       }
33468d75effSDimitry Andric       ScanRangeForPointers(stack_begin, stack_end, frontier, "STACK",
33568d75effSDimitry Andric                            kReachable);
33668d75effSDimitry Andric       ForEachExtraStackRange(os_id, ForEachExtraStackRangeCb, frontier);
33768d75effSDimitry Andric     }
33868d75effSDimitry Andric 
33968d75effSDimitry Andric     if (flags()->use_tls) {
34068d75effSDimitry Andric       if (tls_begin) {
341*349cc55cSDimitry Andric         LOG_THREADS("TLS at %p-%p.\n", (void *)tls_begin, (void *)tls_end);
34268d75effSDimitry Andric         // If the tls and cache ranges don't overlap, scan full tls range,
34368d75effSDimitry Andric         // otherwise, only scan the non-overlapping portions
34468d75effSDimitry Andric         if (cache_begin == cache_end || tls_end < cache_begin ||
34568d75effSDimitry Andric             tls_begin > cache_end) {
34668d75effSDimitry Andric           ScanRangeForPointers(tls_begin, tls_end, frontier, "TLS", kReachable);
34768d75effSDimitry Andric         } else {
34868d75effSDimitry Andric           if (tls_begin < cache_begin)
34968d75effSDimitry Andric             ScanRangeForPointers(tls_begin, cache_begin, frontier, "TLS",
35068d75effSDimitry Andric                                  kReachable);
35168d75effSDimitry Andric           if (tls_end > cache_end)
35268d75effSDimitry Andric             ScanRangeForPointers(cache_end, tls_end, frontier, "TLS",
35368d75effSDimitry Andric                                  kReachable);
35468d75effSDimitry Andric         }
35568d75effSDimitry Andric       }
356e8d8bef9SDimitry Andric #if SANITIZER_ANDROID
357e8d8bef9SDimitry Andric       auto *cb = +[](void *dtls_begin, void *dtls_end, uptr /*dso_idd*/,
358e8d8bef9SDimitry Andric                      void *arg) -> void {
359e8d8bef9SDimitry Andric         ScanRangeForPointers(reinterpret_cast<uptr>(dtls_begin),
360e8d8bef9SDimitry Andric                              reinterpret_cast<uptr>(dtls_end),
361e8d8bef9SDimitry Andric                              reinterpret_cast<Frontier *>(arg), "DTLS",
362e8d8bef9SDimitry Andric                              kReachable);
363e8d8bef9SDimitry Andric       };
364e8d8bef9SDimitry Andric 
365e8d8bef9SDimitry Andric       // FIXME: There might be a race-condition here (and in Bionic) if the
366e8d8bef9SDimitry Andric       // thread is suspended in the middle of updating its DTLS. IOWs, we
367e8d8bef9SDimitry Andric       // could scan already freed memory. (probably fine for now)
368e8d8bef9SDimitry Andric       __libc_iterate_dynamic_tls(os_id, cb, frontier);
369e8d8bef9SDimitry Andric #else
37068d75effSDimitry Andric       if (dtls && !DTLSInDestruction(dtls)) {
371e8d8bef9SDimitry Andric         ForEachDVT(dtls, [&](const DTLS::DTV &dtv, int id) {
372e8d8bef9SDimitry Andric           uptr dtls_beg = dtv.beg;
373e8d8bef9SDimitry Andric           uptr dtls_end = dtls_beg + dtv.size;
37468d75effSDimitry Andric           if (dtls_beg < dtls_end) {
375*349cc55cSDimitry Andric             LOG_THREADS("DTLS %d at %p-%p.\n", id, (void *)dtls_beg,
376*349cc55cSDimitry Andric                         (void *)dtls_end);
37768d75effSDimitry Andric             ScanRangeForPointers(dtls_beg, dtls_end, frontier, "DTLS",
37868d75effSDimitry Andric                                  kReachable);
37968d75effSDimitry Andric           }
380e8d8bef9SDimitry Andric         });
38168d75effSDimitry Andric       } else {
38268d75effSDimitry Andric         // We are handling a thread with DTLS under destruction. Log about
38368d75effSDimitry Andric         // this and continue.
384*349cc55cSDimitry Andric         LOG_THREADS("Thread %llu has DTLS under destruction.\n", os_id);
38568d75effSDimitry Andric       }
386e8d8bef9SDimitry Andric #endif
38768d75effSDimitry Andric     }
38868d75effSDimitry Andric   }
389e8d8bef9SDimitry Andric 
390e8d8bef9SDimitry Andric   // Add pointers reachable from ThreadContexts
391e8d8bef9SDimitry Andric   ProcessThreadRegistry(frontier);
39268d75effSDimitry Andric }
39368d75effSDimitry Andric 
3945ffd83dbSDimitry Andric #endif  // SANITIZER_FUCHSIA
3955ffd83dbSDimitry Andric 
39668d75effSDimitry Andric void ScanRootRegion(Frontier *frontier, const RootRegion &root_region,
39768d75effSDimitry Andric                     uptr region_begin, uptr region_end, bool is_readable) {
39868d75effSDimitry Andric   uptr intersection_begin = Max(root_region.begin, region_begin);
39968d75effSDimitry Andric   uptr intersection_end = Min(region_end, root_region.begin + root_region.size);
40068d75effSDimitry Andric   if (intersection_begin >= intersection_end) return;
40168d75effSDimitry Andric   LOG_POINTERS("Root region %p-%p intersects with mapped region %p-%p (%s)\n",
402*349cc55cSDimitry Andric                (void *)root_region.begin,
403*349cc55cSDimitry Andric                (void *)(root_region.begin + root_region.size),
404*349cc55cSDimitry Andric                (void *)region_begin, (void *)region_end,
40568d75effSDimitry Andric                is_readable ? "readable" : "unreadable");
40668d75effSDimitry Andric   if (is_readable)
40768d75effSDimitry Andric     ScanRangeForPointers(intersection_begin, intersection_end, frontier, "ROOT",
40868d75effSDimitry Andric                          kReachable);
40968d75effSDimitry Andric }
41068d75effSDimitry Andric 
41168d75effSDimitry Andric static void ProcessRootRegion(Frontier *frontier,
41268d75effSDimitry Andric                               const RootRegion &root_region) {
41368d75effSDimitry Andric   MemoryMappingLayout proc_maps(/*cache_enabled*/ true);
41468d75effSDimitry Andric   MemoryMappedSegment segment;
41568d75effSDimitry Andric   while (proc_maps.Next(&segment)) {
41668d75effSDimitry Andric     ScanRootRegion(frontier, root_region, segment.start, segment.end,
41768d75effSDimitry Andric                    segment.IsReadable());
41868d75effSDimitry Andric   }
41968d75effSDimitry Andric }
42068d75effSDimitry Andric 
42168d75effSDimitry Andric // Scans root regions for heap pointers.
42268d75effSDimitry Andric static void ProcessRootRegions(Frontier *frontier) {
42368d75effSDimitry Andric   if (!flags()->use_root_regions) return;
424*349cc55cSDimitry Andric   for (uptr i = 0; i < root_regions.size(); i++)
425*349cc55cSDimitry Andric     ProcessRootRegion(frontier, root_regions[i]);
42668d75effSDimitry Andric }
42768d75effSDimitry Andric 
42868d75effSDimitry Andric static void FloodFillTag(Frontier *frontier, ChunkTag tag) {
42968d75effSDimitry Andric   while (frontier->size()) {
43068d75effSDimitry Andric     uptr next_chunk = frontier->back();
43168d75effSDimitry Andric     frontier->pop_back();
43268d75effSDimitry Andric     LsanMetadata m(next_chunk);
43368d75effSDimitry Andric     ScanRangeForPointers(next_chunk, next_chunk + m.requested_size(), frontier,
43468d75effSDimitry Andric                          "HEAP", tag);
43568d75effSDimitry Andric   }
43668d75effSDimitry Andric }
43768d75effSDimitry Andric 
43868d75effSDimitry Andric // ForEachChunk callback. If the chunk is marked as leaked, marks all chunks
43968d75effSDimitry Andric // which are reachable from it as indirectly leaked.
44068d75effSDimitry Andric static void MarkIndirectlyLeakedCb(uptr chunk, void *arg) {
44168d75effSDimitry Andric   chunk = GetUserBegin(chunk);
44268d75effSDimitry Andric   LsanMetadata m(chunk);
44368d75effSDimitry Andric   if (m.allocated() && m.tag() != kReachable) {
44468d75effSDimitry Andric     ScanRangeForPointers(chunk, chunk + m.requested_size(),
44568d75effSDimitry Andric                          /* frontier */ nullptr, "HEAP", kIndirectlyLeaked);
44668d75effSDimitry Andric   }
44768d75effSDimitry Andric }
44868d75effSDimitry Andric 
449e8d8bef9SDimitry Andric static void IgnoredSuppressedCb(uptr chunk, void *arg) {
450e8d8bef9SDimitry Andric   CHECK(arg);
451e8d8bef9SDimitry Andric   chunk = GetUserBegin(chunk);
452e8d8bef9SDimitry Andric   LsanMetadata m(chunk);
453e8d8bef9SDimitry Andric   if (!m.allocated() || m.tag() == kIgnored)
454e8d8bef9SDimitry Andric     return;
455e8d8bef9SDimitry Andric 
456e8d8bef9SDimitry Andric   const InternalMmapVector<u32> &suppressed =
457e8d8bef9SDimitry Andric       *static_cast<const InternalMmapVector<u32> *>(arg);
458e8d8bef9SDimitry Andric   uptr idx = InternalLowerBound(suppressed, m.stack_trace_id());
459e8d8bef9SDimitry Andric   if (idx >= suppressed.size() || m.stack_trace_id() != suppressed[idx])
460e8d8bef9SDimitry Andric     return;
461e8d8bef9SDimitry Andric 
462*349cc55cSDimitry Andric   LOG_POINTERS("Suppressed: chunk %p-%p of size %zu.\n", (void *)chunk,
463*349cc55cSDimitry Andric                (void *)(chunk + m.requested_size()), m.requested_size());
464e8d8bef9SDimitry Andric   m.set_tag(kIgnored);
465e8d8bef9SDimitry Andric }
466e8d8bef9SDimitry Andric 
46768d75effSDimitry Andric // ForEachChunk callback. If chunk is marked as ignored, adds its address to
46868d75effSDimitry Andric // frontier.
46968d75effSDimitry Andric static void CollectIgnoredCb(uptr chunk, void *arg) {
47068d75effSDimitry Andric   CHECK(arg);
47168d75effSDimitry Andric   chunk = GetUserBegin(chunk);
47268d75effSDimitry Andric   LsanMetadata m(chunk);
47368d75effSDimitry Andric   if (m.allocated() && m.tag() == kIgnored) {
474*349cc55cSDimitry Andric     LOG_POINTERS("Ignored: chunk %p-%p of size %zu.\n", (void *)chunk,
475*349cc55cSDimitry Andric                  (void *)(chunk + m.requested_size()), m.requested_size());
47668d75effSDimitry Andric     reinterpret_cast<Frontier *>(arg)->push_back(chunk);
47768d75effSDimitry Andric   }
47868d75effSDimitry Andric }
47968d75effSDimitry Andric 
480*349cc55cSDimitry Andric static uptr GetCallerPC(const StackTrace &stack) {
48168d75effSDimitry Andric   // The top frame is our malloc/calloc/etc. The next frame is the caller.
48268d75effSDimitry Andric   if (stack.size >= 2)
48368d75effSDimitry Andric     return stack.trace[1];
48468d75effSDimitry Andric   return 0;
48568d75effSDimitry Andric }
48668d75effSDimitry Andric 
48768d75effSDimitry Andric struct InvalidPCParam {
48868d75effSDimitry Andric   Frontier *frontier;
48968d75effSDimitry Andric   bool skip_linker_allocations;
49068d75effSDimitry Andric };
49168d75effSDimitry Andric 
49268d75effSDimitry Andric // ForEachChunk callback. If the caller pc is invalid or is within the linker,
49368d75effSDimitry Andric // mark as reachable. Called by ProcessPlatformSpecificAllocations.
49468d75effSDimitry Andric static void MarkInvalidPCCb(uptr chunk, void *arg) {
49568d75effSDimitry Andric   CHECK(arg);
49668d75effSDimitry Andric   InvalidPCParam *param = reinterpret_cast<InvalidPCParam *>(arg);
49768d75effSDimitry Andric   chunk = GetUserBegin(chunk);
49868d75effSDimitry Andric   LsanMetadata m(chunk);
49968d75effSDimitry Andric   if (m.allocated() && m.tag() != kReachable && m.tag() != kIgnored) {
50068d75effSDimitry Andric     u32 stack_id = m.stack_trace_id();
50168d75effSDimitry Andric     uptr caller_pc = 0;
50268d75effSDimitry Andric     if (stack_id > 0)
503*349cc55cSDimitry Andric       caller_pc = GetCallerPC(StackDepotGet(stack_id));
50468d75effSDimitry Andric     // If caller_pc is unknown, this chunk may be allocated in a coroutine. Mark
50568d75effSDimitry Andric     // it as reachable, as we can't properly report its allocation stack anyway.
50668d75effSDimitry Andric     if (caller_pc == 0 || (param->skip_linker_allocations &&
50768d75effSDimitry Andric                            GetLinker()->containsAddress(caller_pc))) {
50868d75effSDimitry Andric       m.set_tag(kReachable);
50968d75effSDimitry Andric       param->frontier->push_back(chunk);
51068d75effSDimitry Andric     }
51168d75effSDimitry Andric   }
51268d75effSDimitry Andric }
51368d75effSDimitry Andric 
51468d75effSDimitry Andric // On Linux, treats all chunks allocated from ld-linux.so as reachable, which
51568d75effSDimitry Andric // covers dynamically allocated TLS blocks, internal dynamic loader's loaded
51668d75effSDimitry Andric // modules accounting etc.
51768d75effSDimitry Andric // Dynamic TLS blocks contain the TLS variables of dynamically loaded modules.
51868d75effSDimitry Andric // They are allocated with a __libc_memalign() call in allocate_and_init()
51968d75effSDimitry Andric // (elf/dl-tls.c). Glibc won't tell us the address ranges occupied by those
52068d75effSDimitry Andric // blocks, but we can make sure they come from our own allocator by intercepting
52168d75effSDimitry Andric // __libc_memalign(). On top of that, there is no easy way to reach them. Their
52268d75effSDimitry Andric // addresses are stored in a dynamically allocated array (the DTV) which is
52368d75effSDimitry Andric // referenced from the static TLS. Unfortunately, we can't just rely on the DTV
52468d75effSDimitry Andric // being reachable from the static TLS, and the dynamic TLS being reachable from
52568d75effSDimitry Andric // the DTV. This is because the initial DTV is allocated before our interception
52668d75effSDimitry Andric // mechanism kicks in, and thus we don't recognize it as allocated memory. We
52768d75effSDimitry Andric // can't special-case it either, since we don't know its size.
52868d75effSDimitry Andric // Our solution is to include in the root set all allocations made from
52968d75effSDimitry Andric // ld-linux.so (which is where allocate_and_init() is implemented). This is
53068d75effSDimitry Andric // guaranteed to include all dynamic TLS blocks (and possibly other allocations
53168d75effSDimitry Andric // which we don't care about).
53268d75effSDimitry Andric // On all other platforms, this simply checks to ensure that the caller pc is
53368d75effSDimitry Andric // valid before reporting chunks as leaked.
534*349cc55cSDimitry Andric static void ProcessPC(Frontier *frontier) {
53568d75effSDimitry Andric   InvalidPCParam arg;
53668d75effSDimitry Andric   arg.frontier = frontier;
53768d75effSDimitry Andric   arg.skip_linker_allocations =
53868d75effSDimitry Andric       flags()->use_tls && flags()->use_ld_allocations && GetLinker() != nullptr;
53968d75effSDimitry Andric   ForEachChunk(MarkInvalidPCCb, &arg);
54068d75effSDimitry Andric }
54168d75effSDimitry Andric 
54268d75effSDimitry Andric // Sets the appropriate tag on each chunk.
5435ffd83dbSDimitry Andric static void ClassifyAllChunks(SuspendedThreadsList const &suspended_threads,
5445ffd83dbSDimitry Andric                               Frontier *frontier) {
545e8d8bef9SDimitry Andric   const InternalMmapVector<u32> &suppressed_stacks =
546e8d8bef9SDimitry Andric       GetSuppressionContext()->GetSortedSuppressedStacks();
547e8d8bef9SDimitry Andric   if (!suppressed_stacks.empty()) {
548e8d8bef9SDimitry Andric     ForEachChunk(IgnoredSuppressedCb,
549e8d8bef9SDimitry Andric                  const_cast<InternalMmapVector<u32> *>(&suppressed_stacks));
550e8d8bef9SDimitry Andric   }
5515ffd83dbSDimitry Andric   ForEachChunk(CollectIgnoredCb, frontier);
5525ffd83dbSDimitry Andric   ProcessGlobalRegions(frontier);
5535ffd83dbSDimitry Andric   ProcessThreads(suspended_threads, frontier);
5545ffd83dbSDimitry Andric   ProcessRootRegions(frontier);
5555ffd83dbSDimitry Andric   FloodFillTag(frontier, kReachable);
55668d75effSDimitry Andric 
5575ffd83dbSDimitry Andric   CHECK_EQ(0, frontier->size());
5585ffd83dbSDimitry Andric   ProcessPC(frontier);
55968d75effSDimitry Andric 
56068d75effSDimitry Andric   // The check here is relatively expensive, so we do this in a separate flood
56168d75effSDimitry Andric   // fill. That way we can skip the check for chunks that are reachable
56268d75effSDimitry Andric   // otherwise.
56368d75effSDimitry Andric   LOG_POINTERS("Processing platform-specific allocations.\n");
5645ffd83dbSDimitry Andric   ProcessPlatformSpecificAllocations(frontier);
5655ffd83dbSDimitry Andric   FloodFillTag(frontier, kReachable);
56668d75effSDimitry Andric 
56768d75effSDimitry Andric   // Iterate over leaked chunks and mark those that are reachable from other
56868d75effSDimitry Andric   // leaked chunks.
56968d75effSDimitry Andric   LOG_POINTERS("Scanning leaked chunks.\n");
57068d75effSDimitry Andric   ForEachChunk(MarkIndirectlyLeakedCb, nullptr);
57168d75effSDimitry Andric }
57268d75effSDimitry Andric 
57368d75effSDimitry Andric // ForEachChunk callback. Resets the tags to pre-leak-check state.
57468d75effSDimitry Andric static void ResetTagsCb(uptr chunk, void *arg) {
57568d75effSDimitry Andric   (void)arg;
57668d75effSDimitry Andric   chunk = GetUserBegin(chunk);
57768d75effSDimitry Andric   LsanMetadata m(chunk);
57868d75effSDimitry Andric   if (m.allocated() && m.tag() != kIgnored)
57968d75effSDimitry Andric     m.set_tag(kDirectlyLeaked);
58068d75effSDimitry Andric }
58168d75effSDimitry Andric 
58268d75effSDimitry Andric // ForEachChunk callback. Aggregates information about unreachable chunks into
58368d75effSDimitry Andric // a LeakReport.
58468d75effSDimitry Andric static void CollectLeaksCb(uptr chunk, void *arg) {
58568d75effSDimitry Andric   CHECK(arg);
58668d75effSDimitry Andric   LeakReport *leak_report = reinterpret_cast<LeakReport *>(arg);
58768d75effSDimitry Andric   chunk = GetUserBegin(chunk);
58868d75effSDimitry Andric   LsanMetadata m(chunk);
58968d75effSDimitry Andric   if (!m.allocated()) return;
59068d75effSDimitry Andric   if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) {
591*349cc55cSDimitry Andric     leak_report->AddLeakedChunk(chunk, m.stack_trace_id(), m.requested_size(),
59268d75effSDimitry Andric                                 m.tag());
59368d75effSDimitry Andric   }
59468d75effSDimitry Andric }
59568d75effSDimitry Andric 
596e8d8bef9SDimitry Andric void LeakSuppressionContext::PrintMatchedSuppressions() {
59768d75effSDimitry Andric   InternalMmapVector<Suppression *> matched;
598e8d8bef9SDimitry Andric   context.GetMatched(&matched);
59968d75effSDimitry Andric   if (!matched.size())
60068d75effSDimitry Andric     return;
60168d75effSDimitry Andric   const char *line = "-----------------------------------------------------";
60268d75effSDimitry Andric   Printf("%s\n", line);
60368d75effSDimitry Andric   Printf("Suppressions used:\n");
60468d75effSDimitry Andric   Printf("  count      bytes template\n");
605e8d8bef9SDimitry Andric   for (uptr i = 0; i < matched.size(); i++) {
606e8d8bef9SDimitry Andric     Printf("%7zu %10zu %s\n",
607e8d8bef9SDimitry Andric            static_cast<uptr>(atomic_load_relaxed(&matched[i]->hit_count)),
608e8d8bef9SDimitry Andric            matched[i]->weight, matched[i]->templ);
609e8d8bef9SDimitry Andric   }
61068d75effSDimitry Andric   Printf("%s\n\n", line);
61168d75effSDimitry Andric }
61268d75effSDimitry Andric 
61368d75effSDimitry Andric static void ReportIfNotSuspended(ThreadContextBase *tctx, void *arg) {
61468d75effSDimitry Andric   const InternalMmapVector<tid_t> &suspended_threads =
61568d75effSDimitry Andric       *(const InternalMmapVector<tid_t> *)arg;
61668d75effSDimitry Andric   if (tctx->status == ThreadStatusRunning) {
617e8d8bef9SDimitry Andric     uptr i = InternalLowerBound(suspended_threads, tctx->os_id);
61868d75effSDimitry Andric     if (i >= suspended_threads.size() || suspended_threads[i] != tctx->os_id)
619*349cc55cSDimitry Andric       Report(
620*349cc55cSDimitry Andric           "Running thread %llu was not suspended. False leaks are possible.\n",
62168d75effSDimitry Andric           tctx->os_id);
62268d75effSDimitry Andric   }
62368d75effSDimitry Andric }
62468d75effSDimitry Andric 
6255ffd83dbSDimitry Andric #if SANITIZER_FUCHSIA
6265ffd83dbSDimitry Andric 
6275ffd83dbSDimitry Andric // Fuchsia provides a libc interface that guarantees all threads are
6285ffd83dbSDimitry Andric // covered, and SuspendedThreadList is never really used.
6295ffd83dbSDimitry Andric static void ReportUnsuspendedThreads(const SuspendedThreadsList &) {}
6305ffd83dbSDimitry Andric 
6315ffd83dbSDimitry Andric #else  // !SANITIZER_FUCHSIA
6325ffd83dbSDimitry Andric 
63368d75effSDimitry Andric static void ReportUnsuspendedThreads(
63468d75effSDimitry Andric     const SuspendedThreadsList &suspended_threads) {
63568d75effSDimitry Andric   InternalMmapVector<tid_t> threads(suspended_threads.ThreadCount());
63668d75effSDimitry Andric   for (uptr i = 0; i < suspended_threads.ThreadCount(); ++i)
63768d75effSDimitry Andric     threads[i] = suspended_threads.GetThreadID(i);
63868d75effSDimitry Andric 
63968d75effSDimitry Andric   Sort(threads.data(), threads.size());
64068d75effSDimitry Andric 
64168d75effSDimitry Andric   GetThreadRegistryLocked()->RunCallbackForEachThreadLocked(
64268d75effSDimitry Andric       &ReportIfNotSuspended, &threads);
64368d75effSDimitry Andric }
64468d75effSDimitry Andric 
6455ffd83dbSDimitry Andric #endif  // !SANITIZER_FUCHSIA
6465ffd83dbSDimitry Andric 
64768d75effSDimitry Andric static void CheckForLeaksCallback(const SuspendedThreadsList &suspended_threads,
64868d75effSDimitry Andric                                   void *arg) {
64968d75effSDimitry Andric   CheckForLeaksParam *param = reinterpret_cast<CheckForLeaksParam *>(arg);
65068d75effSDimitry Andric   CHECK(param);
65168d75effSDimitry Andric   CHECK(!param->success);
65268d75effSDimitry Andric   ReportUnsuspendedThreads(suspended_threads);
6535ffd83dbSDimitry Andric   ClassifyAllChunks(suspended_threads, &param->frontier);
65468d75effSDimitry Andric   ForEachChunk(CollectLeaksCb, &param->leak_report);
65568d75effSDimitry Andric   // Clean up for subsequent leak checks. This assumes we did not overwrite any
65668d75effSDimitry Andric   // kIgnored tags.
65768d75effSDimitry Andric   ForEachChunk(ResetTagsCb, nullptr);
65868d75effSDimitry Andric   param->success = true;
65968d75effSDimitry Andric }
66068d75effSDimitry Andric 
661e8d8bef9SDimitry Andric static bool PrintResults(LeakReport &report) {
662e8d8bef9SDimitry Andric   uptr unsuppressed_count = report.UnsuppressedLeakCount();
663e8d8bef9SDimitry Andric   if (unsuppressed_count) {
664e8d8bef9SDimitry Andric     Decorator d;
665e8d8bef9SDimitry Andric     Printf(
666e8d8bef9SDimitry Andric         "\n"
667e8d8bef9SDimitry Andric         "================================================================="
668e8d8bef9SDimitry Andric         "\n");
669e8d8bef9SDimitry Andric     Printf("%s", d.Error());
670e8d8bef9SDimitry Andric     Report("ERROR: LeakSanitizer: detected memory leaks\n");
671e8d8bef9SDimitry Andric     Printf("%s", d.Default());
672e8d8bef9SDimitry Andric     report.ReportTopLeaks(flags()->max_leaks);
673e8d8bef9SDimitry Andric   }
674e8d8bef9SDimitry Andric   if (common_flags()->print_suppressions)
675e8d8bef9SDimitry Andric     GetSuppressionContext()->PrintMatchedSuppressions();
676e8d8bef9SDimitry Andric   if (unsuppressed_count > 0) {
677e8d8bef9SDimitry Andric     report.PrintSummary();
678e8d8bef9SDimitry Andric     return true;
679e8d8bef9SDimitry Andric   }
680e8d8bef9SDimitry Andric   return false;
681e8d8bef9SDimitry Andric }
682e8d8bef9SDimitry Andric 
68368d75effSDimitry Andric static bool CheckForLeaks() {
68468d75effSDimitry Andric   if (&__lsan_is_turned_off && __lsan_is_turned_off())
68568d75effSDimitry Andric     return false;
686e8d8bef9SDimitry Andric   // Inside LockStuffAndStopTheWorld we can't run symbolizer, so we can't match
687e8d8bef9SDimitry Andric   // suppressions. However if a stack id was previously suppressed, it should be
688e8d8bef9SDimitry Andric   // suppressed in future checks as well.
689e8d8bef9SDimitry Andric   for (int i = 0;; ++i) {
69068d75effSDimitry Andric     EnsureMainThreadIDIsCorrect();
69168d75effSDimitry Andric     CheckForLeaksParam param;
69268d75effSDimitry Andric     LockStuffAndStopTheWorld(CheckForLeaksCallback, &param);
69368d75effSDimitry Andric     if (!param.success) {
69468d75effSDimitry Andric       Report("LeakSanitizer has encountered a fatal error.\n");
69568d75effSDimitry Andric       Report(
69668d75effSDimitry Andric           "HINT: For debugging, try setting environment variable "
69768d75effSDimitry Andric           "LSAN_OPTIONS=verbosity=1:log_threads=1\n");
69868d75effSDimitry Andric       Report(
699e8d8bef9SDimitry Andric           "HINT: LeakSanitizer does not work under ptrace (strace, gdb, "
700e8d8bef9SDimitry Andric           "etc)\n");
70168d75effSDimitry Andric       Die();
70268d75effSDimitry Andric     }
703e8d8bef9SDimitry Andric     // No new suppressions stacks, so rerun will not help and we can report.
704e8d8bef9SDimitry Andric     if (!param.leak_report.ApplySuppressions())
705e8d8bef9SDimitry Andric       return PrintResults(param.leak_report);
706e8d8bef9SDimitry Andric 
707e8d8bef9SDimitry Andric     // No indirect leaks to report, so we are done here.
708e8d8bef9SDimitry Andric     if (!param.leak_report.IndirectUnsuppressedLeakCount())
709e8d8bef9SDimitry Andric       return PrintResults(param.leak_report);
710e8d8bef9SDimitry Andric 
711e8d8bef9SDimitry Andric     if (i >= 8) {
712e8d8bef9SDimitry Andric       Report("WARNING: LeakSanitizer gave up on indirect leaks suppression.\n");
713e8d8bef9SDimitry Andric       return PrintResults(param.leak_report);
71468d75effSDimitry Andric     }
715e8d8bef9SDimitry Andric 
716e8d8bef9SDimitry Andric     // We found a new previously unseen suppressed call stack. Rerun to make
717e8d8bef9SDimitry Andric     // sure it does not hold indirect leaks.
718e8d8bef9SDimitry Andric     VReport(1, "Rerun with %zu suppressed stacks.",
719e8d8bef9SDimitry Andric             GetSuppressionContext()->GetSortedSuppressedStacks().size());
72068d75effSDimitry Andric   }
72168d75effSDimitry Andric }
72268d75effSDimitry Andric 
72368d75effSDimitry Andric static bool has_reported_leaks = false;
72468d75effSDimitry Andric bool HasReportedLeaks() { return has_reported_leaks; }
72568d75effSDimitry Andric 
72668d75effSDimitry Andric void DoLeakCheck() {
727*349cc55cSDimitry Andric   Lock l(&global_mutex);
72868d75effSDimitry Andric   static bool already_done;
72968d75effSDimitry Andric   if (already_done) return;
73068d75effSDimitry Andric   already_done = true;
73168d75effSDimitry Andric   has_reported_leaks = CheckForLeaks();
73268d75effSDimitry Andric   if (has_reported_leaks) HandleLeaks();
73368d75effSDimitry Andric }
73468d75effSDimitry Andric 
73568d75effSDimitry Andric static int DoRecoverableLeakCheck() {
736*349cc55cSDimitry Andric   Lock l(&global_mutex);
73768d75effSDimitry Andric   bool have_leaks = CheckForLeaks();
73868d75effSDimitry Andric   return have_leaks ? 1 : 0;
73968d75effSDimitry Andric }
74068d75effSDimitry Andric 
74168d75effSDimitry Andric void DoRecoverableLeakCheckVoid() { DoRecoverableLeakCheck(); }
74268d75effSDimitry Andric 
743e8d8bef9SDimitry Andric Suppression *LeakSuppressionContext::GetSuppressionForAddr(uptr addr) {
74468d75effSDimitry Andric   Suppression *s = nullptr;
74568d75effSDimitry Andric 
74668d75effSDimitry Andric   // Suppress by module name.
74768d75effSDimitry Andric   if (const char *module_name =
74868d75effSDimitry Andric           Symbolizer::GetOrInit()->GetModuleNameForPc(addr))
749e8d8bef9SDimitry Andric     if (context.Match(module_name, kSuppressionLeak, &s))
75068d75effSDimitry Andric       return s;
75168d75effSDimitry Andric 
75268d75effSDimitry Andric   // Suppress by file or function name.
75368d75effSDimitry Andric   SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(addr);
75468d75effSDimitry Andric   for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
755e8d8bef9SDimitry Andric     if (context.Match(cur->info.function, kSuppressionLeak, &s) ||
756e8d8bef9SDimitry Andric         context.Match(cur->info.file, kSuppressionLeak, &s)) {
75768d75effSDimitry Andric       break;
75868d75effSDimitry Andric     }
75968d75effSDimitry Andric   }
76068d75effSDimitry Andric   frames->ClearAll();
76168d75effSDimitry Andric   return s;
76268d75effSDimitry Andric }
76368d75effSDimitry Andric 
764e8d8bef9SDimitry Andric Suppression *LeakSuppressionContext::GetSuppressionForStack(
765*349cc55cSDimitry Andric     u32 stack_trace_id, const StackTrace &stack) {
766e8d8bef9SDimitry Andric   LazyInit();
76768d75effSDimitry Andric   for (uptr i = 0; i < stack.size; i++) {
76868d75effSDimitry Andric     Suppression *s = GetSuppressionForAddr(
76968d75effSDimitry Andric         StackTrace::GetPreviousInstructionPc(stack.trace[i]));
770e8d8bef9SDimitry Andric     if (s) {
771e8d8bef9SDimitry Andric       suppressed_stacks_sorted = false;
772e8d8bef9SDimitry Andric       suppressed_stacks.push_back(stack_trace_id);
773e8d8bef9SDimitry Andric       return s;
774e8d8bef9SDimitry Andric     }
77568d75effSDimitry Andric   }
77668d75effSDimitry Andric   return nullptr;
77768d75effSDimitry Andric }
77868d75effSDimitry Andric 
77968d75effSDimitry Andric ///// LeakReport implementation. /////
78068d75effSDimitry Andric 
78168d75effSDimitry Andric // A hard limit on the number of distinct leaks, to avoid quadratic complexity
78268d75effSDimitry Andric // in LeakReport::AddLeakedChunk(). We don't expect to ever see this many leaks
78368d75effSDimitry Andric // in real-world applications.
78468d75effSDimitry Andric // FIXME: Get rid of this limit by changing the implementation of LeakReport to
78568d75effSDimitry Andric // use a hash table.
78668d75effSDimitry Andric const uptr kMaxLeaksConsidered = 5000;
78768d75effSDimitry Andric 
78868d75effSDimitry Andric void LeakReport::AddLeakedChunk(uptr chunk, u32 stack_trace_id,
78968d75effSDimitry Andric                                 uptr leaked_size, ChunkTag tag) {
79068d75effSDimitry Andric   CHECK(tag == kDirectlyLeaked || tag == kIndirectlyLeaked);
791*349cc55cSDimitry Andric 
792*349cc55cSDimitry Andric   if (u32 resolution = flags()->resolution) {
793*349cc55cSDimitry Andric     StackTrace stack = StackDepotGet(stack_trace_id);
794*349cc55cSDimitry Andric     stack.size = Min(stack.size, resolution);
795*349cc55cSDimitry Andric     stack_trace_id = StackDepotPut(stack);
796*349cc55cSDimitry Andric   }
797*349cc55cSDimitry Andric 
79868d75effSDimitry Andric   bool is_directly_leaked = (tag == kDirectlyLeaked);
79968d75effSDimitry Andric   uptr i;
80068d75effSDimitry Andric   for (i = 0; i < leaks_.size(); i++) {
80168d75effSDimitry Andric     if (leaks_[i].stack_trace_id == stack_trace_id &&
80268d75effSDimitry Andric         leaks_[i].is_directly_leaked == is_directly_leaked) {
80368d75effSDimitry Andric       leaks_[i].hit_count++;
80468d75effSDimitry Andric       leaks_[i].total_size += leaked_size;
80568d75effSDimitry Andric       break;
80668d75effSDimitry Andric     }
80768d75effSDimitry Andric   }
80868d75effSDimitry Andric   if (i == leaks_.size()) {
80968d75effSDimitry Andric     if (leaks_.size() == kMaxLeaksConsidered) return;
81068d75effSDimitry Andric     Leak leak = { next_id_++, /* hit_count */ 1, leaked_size, stack_trace_id,
81168d75effSDimitry Andric                   is_directly_leaked, /* is_suppressed */ false };
81268d75effSDimitry Andric     leaks_.push_back(leak);
81368d75effSDimitry Andric   }
81468d75effSDimitry Andric   if (flags()->report_objects) {
81568d75effSDimitry Andric     LeakedObject obj = {leaks_[i].id, chunk, leaked_size};
81668d75effSDimitry Andric     leaked_objects_.push_back(obj);
81768d75effSDimitry Andric   }
81868d75effSDimitry Andric }
81968d75effSDimitry Andric 
82068d75effSDimitry Andric static bool LeakComparator(const Leak &leak1, const Leak &leak2) {
82168d75effSDimitry Andric   if (leak1.is_directly_leaked == leak2.is_directly_leaked)
82268d75effSDimitry Andric     return leak1.total_size > leak2.total_size;
82368d75effSDimitry Andric   else
82468d75effSDimitry Andric     return leak1.is_directly_leaked;
82568d75effSDimitry Andric }
82668d75effSDimitry Andric 
82768d75effSDimitry Andric void LeakReport::ReportTopLeaks(uptr num_leaks_to_report) {
82868d75effSDimitry Andric   CHECK(leaks_.size() <= kMaxLeaksConsidered);
82968d75effSDimitry Andric   Printf("\n");
83068d75effSDimitry Andric   if (leaks_.size() == kMaxLeaksConsidered)
83168d75effSDimitry Andric     Printf("Too many leaks! Only the first %zu leaks encountered will be "
83268d75effSDimitry Andric            "reported.\n",
83368d75effSDimitry Andric            kMaxLeaksConsidered);
83468d75effSDimitry Andric 
83568d75effSDimitry Andric   uptr unsuppressed_count = UnsuppressedLeakCount();
83668d75effSDimitry Andric   if (num_leaks_to_report > 0 && num_leaks_to_report < unsuppressed_count)
83768d75effSDimitry Andric     Printf("The %zu top leak(s):\n", num_leaks_to_report);
83868d75effSDimitry Andric   Sort(leaks_.data(), leaks_.size(), &LeakComparator);
83968d75effSDimitry Andric   uptr leaks_reported = 0;
84068d75effSDimitry Andric   for (uptr i = 0; i < leaks_.size(); i++) {
84168d75effSDimitry Andric     if (leaks_[i].is_suppressed) continue;
84268d75effSDimitry Andric     PrintReportForLeak(i);
84368d75effSDimitry Andric     leaks_reported++;
84468d75effSDimitry Andric     if (leaks_reported == num_leaks_to_report) break;
84568d75effSDimitry Andric   }
84668d75effSDimitry Andric   if (leaks_reported < unsuppressed_count) {
84768d75effSDimitry Andric     uptr remaining = unsuppressed_count - leaks_reported;
84868d75effSDimitry Andric     Printf("Omitting %zu more leak(s).\n", remaining);
84968d75effSDimitry Andric   }
85068d75effSDimitry Andric }
85168d75effSDimitry Andric 
85268d75effSDimitry Andric void LeakReport::PrintReportForLeak(uptr index) {
85368d75effSDimitry Andric   Decorator d;
85468d75effSDimitry Andric   Printf("%s", d.Leak());
85568d75effSDimitry Andric   Printf("%s leak of %zu byte(s) in %zu object(s) allocated from:\n",
85668d75effSDimitry Andric          leaks_[index].is_directly_leaked ? "Direct" : "Indirect",
85768d75effSDimitry Andric          leaks_[index].total_size, leaks_[index].hit_count);
85868d75effSDimitry Andric   Printf("%s", d.Default());
85968d75effSDimitry Andric 
860*349cc55cSDimitry Andric   CHECK(leaks_[index].stack_trace_id);
861*349cc55cSDimitry Andric   StackDepotGet(leaks_[index].stack_trace_id).Print();
86268d75effSDimitry Andric 
86368d75effSDimitry Andric   if (flags()->report_objects) {
86468d75effSDimitry Andric     Printf("Objects leaked above:\n");
86568d75effSDimitry Andric     PrintLeakedObjectsForLeak(index);
86668d75effSDimitry Andric     Printf("\n");
86768d75effSDimitry Andric   }
86868d75effSDimitry Andric }
86968d75effSDimitry Andric 
87068d75effSDimitry Andric void LeakReport::PrintLeakedObjectsForLeak(uptr index) {
87168d75effSDimitry Andric   u32 leak_id = leaks_[index].id;
87268d75effSDimitry Andric   for (uptr j = 0; j < leaked_objects_.size(); j++) {
87368d75effSDimitry Andric     if (leaked_objects_[j].leak_id == leak_id)
874*349cc55cSDimitry Andric       Printf("%p (%zu bytes)\n", (void *)leaked_objects_[j].addr,
87568d75effSDimitry Andric              leaked_objects_[j].size);
87668d75effSDimitry Andric   }
87768d75effSDimitry Andric }
87868d75effSDimitry Andric 
87968d75effSDimitry Andric void LeakReport::PrintSummary() {
88068d75effSDimitry Andric   CHECK(leaks_.size() <= kMaxLeaksConsidered);
88168d75effSDimitry Andric   uptr bytes = 0, allocations = 0;
88268d75effSDimitry Andric   for (uptr i = 0; i < leaks_.size(); i++) {
88368d75effSDimitry Andric       if (leaks_[i].is_suppressed) continue;
88468d75effSDimitry Andric       bytes += leaks_[i].total_size;
88568d75effSDimitry Andric       allocations += leaks_[i].hit_count;
88668d75effSDimitry Andric   }
887fe6060f1SDimitry Andric   InternalScopedString summary;
88868d75effSDimitry Andric   summary.append("%zu byte(s) leaked in %zu allocation(s).", bytes,
88968d75effSDimitry Andric                  allocations);
89068d75effSDimitry Andric   ReportErrorSummary(summary.data());
89168d75effSDimitry Andric }
89268d75effSDimitry Andric 
893e8d8bef9SDimitry Andric uptr LeakReport::ApplySuppressions() {
894e8d8bef9SDimitry Andric   LeakSuppressionContext *suppressions = GetSuppressionContext();
895e8d8bef9SDimitry Andric   uptr new_suppressions = false;
89668d75effSDimitry Andric   for (uptr i = 0; i < leaks_.size(); i++) {
897*349cc55cSDimitry Andric     Suppression *s = suppressions->GetSuppressionForStack(
898*349cc55cSDimitry Andric         leaks_[i].stack_trace_id, StackDepotGet(leaks_[i].stack_trace_id));
89968d75effSDimitry Andric     if (s) {
90068d75effSDimitry Andric       s->weight += leaks_[i].total_size;
90168d75effSDimitry Andric       atomic_store_relaxed(&s->hit_count, atomic_load_relaxed(&s->hit_count) +
90268d75effSDimitry Andric           leaks_[i].hit_count);
90368d75effSDimitry Andric       leaks_[i].is_suppressed = true;
904e8d8bef9SDimitry Andric       ++new_suppressions;
90568d75effSDimitry Andric     }
90668d75effSDimitry Andric   }
907e8d8bef9SDimitry Andric   return new_suppressions;
90868d75effSDimitry Andric }
90968d75effSDimitry Andric 
91068d75effSDimitry Andric uptr LeakReport::UnsuppressedLeakCount() {
91168d75effSDimitry Andric   uptr result = 0;
91268d75effSDimitry Andric   for (uptr i = 0; i < leaks_.size(); i++)
91368d75effSDimitry Andric     if (!leaks_[i].is_suppressed) result++;
91468d75effSDimitry Andric   return result;
91568d75effSDimitry Andric }
91668d75effSDimitry Andric 
917e8d8bef9SDimitry Andric uptr LeakReport::IndirectUnsuppressedLeakCount() {
918e8d8bef9SDimitry Andric   uptr result = 0;
919e8d8bef9SDimitry Andric   for (uptr i = 0; i < leaks_.size(); i++)
920e8d8bef9SDimitry Andric     if (!leaks_[i].is_suppressed && !leaks_[i].is_directly_leaked)
921e8d8bef9SDimitry Andric       result++;
922e8d8bef9SDimitry Andric   return result;
923e8d8bef9SDimitry Andric }
924e8d8bef9SDimitry Andric 
92568d75effSDimitry Andric } // namespace __lsan
92668d75effSDimitry Andric #else // CAN_SANITIZE_LEAKS
92768d75effSDimitry Andric namespace __lsan {
92868d75effSDimitry Andric void InitCommonLsan() { }
92968d75effSDimitry Andric void DoLeakCheck() { }
93068d75effSDimitry Andric void DoRecoverableLeakCheckVoid() { }
93168d75effSDimitry Andric void DisableInThisThread() { }
93268d75effSDimitry Andric void EnableInThisThread() { }
93368d75effSDimitry Andric }
93468d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS
93568d75effSDimitry Andric 
93668d75effSDimitry Andric using namespace __lsan;
93768d75effSDimitry Andric 
93868d75effSDimitry Andric extern "C" {
93968d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
94068d75effSDimitry Andric void __lsan_ignore_object(const void *p) {
94168d75effSDimitry Andric #if CAN_SANITIZE_LEAKS
94268d75effSDimitry Andric   if (!common_flags()->detect_leaks)
94368d75effSDimitry Andric     return;
94468d75effSDimitry Andric   // Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not
94568d75effSDimitry Andric   // locked.
946*349cc55cSDimitry Andric   Lock l(&global_mutex);
94768d75effSDimitry Andric   IgnoreObjectResult res = IgnoreObjectLocked(p);
94868d75effSDimitry Andric   if (res == kIgnoreObjectInvalid)
94968d75effSDimitry Andric     VReport(1, "__lsan_ignore_object(): no heap object found at %p", p);
95068d75effSDimitry Andric   if (res == kIgnoreObjectAlreadyIgnored)
95168d75effSDimitry Andric     VReport(1, "__lsan_ignore_object(): "
95268d75effSDimitry Andric            "heap object at %p is already being ignored\n", p);
95368d75effSDimitry Andric   if (res == kIgnoreObjectSuccess)
95468d75effSDimitry Andric     VReport(1, "__lsan_ignore_object(): ignoring heap object at %p\n", p);
95568d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS
95668d75effSDimitry Andric }
95768d75effSDimitry Andric 
95868d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
95968d75effSDimitry Andric void __lsan_register_root_region(const void *begin, uptr size) {
96068d75effSDimitry Andric #if CAN_SANITIZE_LEAKS
961*349cc55cSDimitry Andric   Lock l(&global_mutex);
96268d75effSDimitry Andric   RootRegion region = {reinterpret_cast<uptr>(begin), size};
963*349cc55cSDimitry Andric   root_regions.push_back(region);
964*349cc55cSDimitry Andric   VReport(1, "Registered root region at %p of size %zu\n", begin, size);
96568d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS
96668d75effSDimitry Andric }
96768d75effSDimitry Andric 
96868d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
96968d75effSDimitry Andric void __lsan_unregister_root_region(const void *begin, uptr size) {
97068d75effSDimitry Andric #if CAN_SANITIZE_LEAKS
971*349cc55cSDimitry Andric   Lock l(&global_mutex);
97268d75effSDimitry Andric   bool removed = false;
973*349cc55cSDimitry Andric   for (uptr i = 0; i < root_regions.size(); i++) {
974*349cc55cSDimitry Andric     RootRegion region = root_regions[i];
97568d75effSDimitry Andric     if (region.begin == reinterpret_cast<uptr>(begin) && region.size == size) {
97668d75effSDimitry Andric       removed = true;
977*349cc55cSDimitry Andric       uptr last_index = root_regions.size() - 1;
978*349cc55cSDimitry Andric       root_regions[i] = root_regions[last_index];
979*349cc55cSDimitry Andric       root_regions.pop_back();
980*349cc55cSDimitry Andric       VReport(1, "Unregistered root region at %p of size %zu\n", begin, size);
98168d75effSDimitry Andric       break;
98268d75effSDimitry Andric     }
98368d75effSDimitry Andric   }
98468d75effSDimitry Andric   if (!removed) {
98568d75effSDimitry Andric     Report(
986*349cc55cSDimitry Andric         "__lsan_unregister_root_region(): region at %p of size %zu has not "
98768d75effSDimitry Andric         "been registered.\n",
98868d75effSDimitry Andric         begin, size);
98968d75effSDimitry Andric     Die();
99068d75effSDimitry Andric   }
99168d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS
99268d75effSDimitry Andric }
99368d75effSDimitry Andric 
99468d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
99568d75effSDimitry Andric void __lsan_disable() {
99668d75effSDimitry Andric #if CAN_SANITIZE_LEAKS
99768d75effSDimitry Andric   __lsan::DisableInThisThread();
99868d75effSDimitry Andric #endif
99968d75effSDimitry Andric }
100068d75effSDimitry Andric 
100168d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
100268d75effSDimitry Andric void __lsan_enable() {
100368d75effSDimitry Andric #if CAN_SANITIZE_LEAKS
100468d75effSDimitry Andric   __lsan::EnableInThisThread();
100568d75effSDimitry Andric #endif
100668d75effSDimitry Andric }
100768d75effSDimitry Andric 
100868d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
100968d75effSDimitry Andric void __lsan_do_leak_check() {
101068d75effSDimitry Andric #if CAN_SANITIZE_LEAKS
101168d75effSDimitry Andric   if (common_flags()->detect_leaks)
101268d75effSDimitry Andric     __lsan::DoLeakCheck();
101368d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS
101468d75effSDimitry Andric }
101568d75effSDimitry Andric 
101668d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE
101768d75effSDimitry Andric int __lsan_do_recoverable_leak_check() {
101868d75effSDimitry Andric #if CAN_SANITIZE_LEAKS
101968d75effSDimitry Andric   if (common_flags()->detect_leaks)
102068d75effSDimitry Andric     return __lsan::DoRecoverableLeakCheck();
102168d75effSDimitry Andric #endif // CAN_SANITIZE_LEAKS
102268d75effSDimitry Andric   return 0;
102368d75effSDimitry Andric }
102468d75effSDimitry Andric 
1025e8d8bef9SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(const char *, __lsan_default_options, void) {
102668d75effSDimitry Andric   return "";
102768d75effSDimitry Andric }
102868d75effSDimitry Andric 
1029e8d8bef9SDimitry Andric #if !SANITIZER_SUPPORTS_WEAK_HOOKS
103068d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
103168d75effSDimitry Andric int __lsan_is_turned_off() {
103268d75effSDimitry Andric   return 0;
103368d75effSDimitry Andric }
103468d75effSDimitry Andric 
103568d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
103668d75effSDimitry Andric const char *__lsan_default_suppressions() {
103768d75effSDimitry Andric   return "";
103868d75effSDimitry Andric }
103968d75effSDimitry Andric #endif
104068d75effSDimitry Andric } // extern "C"
1041