1 //=-- lsan_common.cpp -----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of LeakSanitizer.
10 // Implementation of common leak checking functionality.
12 //===----------------------------------------------------------------------===//
14 #include "lsan_common.h"
16 #include "sanitizer_common/sanitizer_common.h"
17 #include "sanitizer_common/sanitizer_flag_parser.h"
18 #include "sanitizer_common/sanitizer_flags.h"
19 #include "sanitizer_common/sanitizer_placement_new.h"
20 #include "sanitizer_common/sanitizer_procmaps.h"
21 #include "sanitizer_common/sanitizer_report_decorator.h"
22 #include "sanitizer_common/sanitizer_stackdepot.h"
23 #include "sanitizer_common/sanitizer_stacktrace.h"
24 #include "sanitizer_common/sanitizer_suppressions.h"
25 #include "sanitizer_common/sanitizer_thread_registry.h"
26 #include "sanitizer_common/sanitizer_tls_get_addr.h"
28 #if CAN_SANITIZE_LEAKS
31 // https://github.com/apple-oss-distributions/objc4/blob/8701d5672d3fd3cd817aeb84db1077aafe1a1604/runtime/objc-runtime-new.h#L127
32 # if SANITIZER_IOS && !SANITIZER_IOSSIM
33 # define OBJC_DATA_MASK 0x0000007ffffffff8UL
35 # define OBJC_DATA_MASK 0x00007ffffffffff8UL
41 // This mutex is used to prevent races between DoLeakCheck and IgnoreObject, and
42 // also to protect the global list of root regions.
43 static Mutex global_mutex
;
47 void DisableCounterUnderflow() {
48 if (common_flags()->detect_leaks
) {
49 Report("Unmatched call to __lsan_enable().\n");
54 void Flags::SetDefaults() {
55 # define LSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
56 # include "lsan_flags.inc"
60 void RegisterLsanFlags(FlagParser
*parser
, Flags
*f
) {
61 # define LSAN_FLAG(Type, Name, DefaultValue, Description) \
62 RegisterFlag(parser, #Name, Description, &f->Name);
63 # include "lsan_flags.inc"
67 # define LOG_POINTERS(...) \
69 if (flags()->log_pointers) \
70 Report(__VA_ARGS__); \
73 # define LOG_THREADS(...) \
75 if (flags()->log_threads) \
76 Report(__VA_ARGS__); \
79 class LeakSuppressionContext
{
81 SuppressionContext context
;
82 bool suppressed_stacks_sorted
= true;
83 InternalMmapVector
<u32
> suppressed_stacks
;
84 const LoadedModule
*suppress_module
= nullptr;
87 Suppression
*GetSuppressionForAddr(uptr addr
);
88 bool SuppressInvalid(const StackTrace
&stack
);
89 bool SuppressByRule(const StackTrace
&stack
, uptr hit_count
, uptr total_size
);
92 LeakSuppressionContext(const char *supprression_types
[],
93 int suppression_types_num
)
94 : context(supprression_types
, suppression_types_num
) {}
96 bool Suppress(u32 stack_trace_id
, uptr hit_count
, uptr total_size
);
98 const InternalMmapVector
<u32
> &GetSortedSuppressedStacks() {
99 if (!suppressed_stacks_sorted
) {
100 suppressed_stacks_sorted
= true;
101 SortAndDedup(suppressed_stacks
);
103 return suppressed_stacks
;
105 void PrintMatchedSuppressions();
108 ALIGNED(64) static char suppression_placeholder
[sizeof(LeakSuppressionContext
)];
109 static LeakSuppressionContext
*suppression_ctx
= nullptr;
110 static const char kSuppressionLeak
[] = "leak";
111 static const char *kSuppressionTypes
[] = {kSuppressionLeak
};
112 static const char kStdSuppressions
[] =
113 # if SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
114 // For more details refer to the SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
116 "leak:*pthread_exit*\n"
117 # endif // SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
119 // For Darwin and os_log/os_trace: https://reviews.llvm.org/D35173
122 // TLS leak in some glibc versions, described in
123 // https://sourceware.org/bugzilla/show_bug.cgi?id=12650.
124 "leak:*tls_get_addr*\n";
126 void InitializeSuppressions() {
127 CHECK_EQ(nullptr, suppression_ctx
);
128 suppression_ctx
= new (suppression_placeholder
)
129 LeakSuppressionContext(kSuppressionTypes
, ARRAY_SIZE(kSuppressionTypes
));
132 void LeakSuppressionContext::LazyInit() {
135 context
.ParseFromFile(flags()->suppressions
);
136 if (&__lsan_default_suppressions
)
137 context
.Parse(__lsan_default_suppressions());
138 context
.Parse(kStdSuppressions
);
139 if (flags()->use_tls
&& flags()->use_ld_allocations
)
140 suppress_module
= GetLinker();
144 Suppression
*LeakSuppressionContext::GetSuppressionForAddr(uptr addr
) {
145 Suppression
*s
= nullptr;
147 // Suppress by module name.
148 const char *module_name
= Symbolizer::GetOrInit()->GetModuleNameForPc(addr
);
150 module_name
= "<unknown module>";
151 if (context
.Match(module_name
, kSuppressionLeak
, &s
))
154 // Suppress by file or function name.
155 SymbolizedStack
*frames
= Symbolizer::GetOrInit()->SymbolizePC(addr
);
156 for (SymbolizedStack
*cur
= frames
; cur
; cur
= cur
->next
) {
157 if (context
.Match(cur
->info
.function
, kSuppressionLeak
, &s
) ||
158 context
.Match(cur
->info
.file
, kSuppressionLeak
, &s
)) {
166 static uptr
GetCallerPC(const StackTrace
&stack
) {
167 // The top frame is our malloc/calloc/etc. The next frame is the caller.
169 return stack
.trace
[1];
174 // Several pointers in the Objective-C runtime (method cache and class_rw_t,
175 // for example) are tagged with additional bits we need to strip.
176 static inline void *TransformPointer(void *p
) {
177 uptr ptr
= reinterpret_cast<uptr
>(p
);
178 return reinterpret_cast<void *>(ptr
& OBJC_DATA_MASK
);
182 // On Linux, treats all chunks allocated from ld-linux.so as reachable, which
183 // covers dynamically allocated TLS blocks, internal dynamic loader's loaded
184 // modules accounting etc.
185 // Dynamic TLS blocks contain the TLS variables of dynamically loaded modules.
186 // They are allocated with a __libc_memalign() call in allocate_and_init()
187 // (elf/dl-tls.c). Glibc won't tell us the address ranges occupied by those
188 // blocks, but we can make sure they come from our own allocator by intercepting
189 // __libc_memalign(). On top of that, there is no easy way to reach them. Their
190 // addresses are stored in a dynamically allocated array (the DTV) which is
191 // referenced from the static TLS. Unfortunately, we can't just rely on the DTV
192 // being reachable from the static TLS, and the dynamic TLS being reachable from
193 // the DTV. This is because the initial DTV is allocated before our interception
194 // mechanism kicks in, and thus we don't recognize it as allocated memory. We
195 // can't special-case it either, since we don't know its size.
196 // Our solution is to include in the root set all allocations made from
197 // ld-linux.so (which is where allocate_and_init() is implemented). This is
198 // guaranteed to include all dynamic TLS blocks (and possibly other allocations
199 // which we don't care about).
200 // On all other platforms, this simply checks to ensure that the caller pc is
201 // valid before reporting chunks as leaked.
202 bool LeakSuppressionContext::SuppressInvalid(const StackTrace
&stack
) {
203 uptr caller_pc
= GetCallerPC(stack
);
204 // If caller_pc is unknown, this chunk may be allocated in a coroutine. Mark
205 // it as reachable, as we can't properly report its allocation stack anyway.
207 (suppress_module
&& suppress_module
->containsAddress(caller_pc
));
210 bool LeakSuppressionContext::SuppressByRule(const StackTrace
&stack
,
211 uptr hit_count
, uptr total_size
) {
212 for (uptr i
= 0; i
< stack
.size
; i
++) {
213 Suppression
*s
= GetSuppressionForAddr(
214 StackTrace::GetPreviousInstructionPc(stack
.trace
[i
]));
216 s
->weight
+= total_size
;
217 atomic_fetch_add(&s
->hit_count
, hit_count
, memory_order_relaxed
);
224 bool LeakSuppressionContext::Suppress(u32 stack_trace_id
, uptr hit_count
,
227 StackTrace stack
= StackDepotGet(stack_trace_id
);
228 if (!SuppressInvalid(stack
) && !SuppressByRule(stack
, hit_count
, total_size
))
230 suppressed_stacks_sorted
= false;
231 suppressed_stacks
.push_back(stack_trace_id
);
235 static LeakSuppressionContext
*GetSuppressionContext() {
236 CHECK(suppression_ctx
);
237 return suppression_ctx
;
240 void InitCommonLsan() {
241 if (common_flags()->detect_leaks
) {
242 // Initialization which can fail or print warnings should only be done if
243 // LSan is actually enabled.
244 InitializeSuppressions();
245 InitializePlatformSpecificModules();
249 class Decorator
: public __sanitizer::SanitizerCommonDecorator
{
251 Decorator() : SanitizerCommonDecorator() {}
252 const char *Error() { return Red(); }
253 const char *Leak() { return Blue(); }
256 static inline bool MaybeUserPointer(uptr p
) {
257 // Since our heap is located in mmap-ed memory, we can assume a sensible lower
258 // bound on heap addresses.
259 const uptr kMinAddress
= 4 * 4096;
262 # if defined(__x86_64__)
263 // TODO: support LAM48 and 5 level page tables.
264 // LAM_U57 mask format
265 // * top byte: 0x81 because the format is: [0] [6-bit tag] [0]
266 // * top-1 byte: 0xff because it should be 0
267 // * top-2 byte: 0x80 because Linux uses 128 TB VMA ending at 0x7fffffffffff
268 constexpr uptr kLAM_U57Mask
= 0x81ff80;
269 constexpr uptr kPointerMask
= kLAM_U57Mask
<< 40;
270 return ((p
& kPointerMask
) == 0);
271 # elif defined(__mips64)
272 return ((p
>> 40) == 0);
273 # elif defined(__aarch64__)
274 // TBI (Top Byte Ignore) feature of AArch64: bits [63:56] are ignored in
275 // address translation and can be used to store a tag.
276 constexpr uptr kPointerMask
= 255ULL << 48;
277 // Accept up to 48 bit VMA.
278 return ((p
& kPointerMask
) == 0);
279 # elif defined(__loongarch_lp64)
280 // Allow 47-bit user-space VMA at current.
281 return ((p
>> 47) == 0);
287 // Scans the memory range, looking for byte patterns that point into allocator
288 // chunks. Marks those chunks with |tag| and adds them to |frontier|.
289 // There are two usage modes for this function: finding reachable chunks
290 // (|tag| = kReachable) and finding indirectly leaked chunks
291 // (|tag| = kIndirectlyLeaked). In the second case, there's no flood fill,
292 // so |frontier| = 0.
293 void ScanRangeForPointers(uptr begin
, uptr end
, Frontier
*frontier
,
294 const char *region_type
, ChunkTag tag
) {
295 CHECK(tag
== kReachable
|| tag
== kIndirectlyLeaked
);
296 const uptr alignment
= flags()->pointer_alignment();
297 LOG_POINTERS("Scanning %s range %p-%p.\n", region_type
, (void *)begin
,
301 pp
= pp
+ alignment
- pp
% alignment
;
302 for (; pp
+ sizeof(void *) <= end
; pp
+= alignment
) {
303 void *p
= *reinterpret_cast<void **>(pp
);
305 p
= TransformPointer(p
);
307 if (!MaybeUserPointer(reinterpret_cast<uptr
>(p
)))
309 uptr chunk
= PointsIntoChunk(p
);
312 // Pointers to self don't count. This matters when tag == kIndirectlyLeaked.
315 LsanMetadata
m(chunk
);
316 if (m
.tag() == kReachable
|| m
.tag() == kIgnored
)
319 // Do this check relatively late so we can log only the interesting cases.
320 if (!flags()->use_poisoned
&& WordIsPoisoned(pp
)) {
322 "%p is poisoned: ignoring %p pointing into chunk %p-%p of size "
324 (void *)pp
, p
, (void *)chunk
, (void *)(chunk
+ m
.requested_size()),
330 LOG_POINTERS("%p: found %p pointing into chunk %p-%p of size %zu.\n",
331 (void *)pp
, p
, (void *)chunk
,
332 (void *)(chunk
+ m
.requested_size()), m
.requested_size());
334 frontier
->push_back(chunk
);
338 // Scans a global range for pointers
339 void ScanGlobalRange(uptr begin
, uptr end
, Frontier
*frontier
) {
340 uptr allocator_begin
= 0, allocator_end
= 0;
341 GetAllocatorGlobalRange(&allocator_begin
, &allocator_end
);
342 if (begin
<= allocator_begin
&& allocator_begin
< end
) {
343 CHECK_LE(allocator_begin
, allocator_end
);
344 CHECK_LE(allocator_end
, end
);
345 if (begin
< allocator_begin
)
346 ScanRangeForPointers(begin
, allocator_begin
, frontier
, "GLOBAL",
348 if (allocator_end
< end
)
349 ScanRangeForPointers(allocator_end
, end
, frontier
, "GLOBAL", kReachable
);
351 ScanRangeForPointers(begin
, end
, frontier
, "GLOBAL", kReachable
);
355 void ScanExtraStackRanges(const InternalMmapVector
<Range
> &ranges
,
356 Frontier
*frontier
) {
357 for (uptr i
= 0; i
< ranges
.size(); i
++) {
358 ScanRangeForPointers(ranges
[i
].begin
, ranges
[i
].end
, frontier
, "FAKE STACK",
363 # if SANITIZER_FUCHSIA
365 // Fuchsia handles all threads together with its own callback.
366 static void ProcessThreads(SuspendedThreadsList
const &, Frontier
*, tid_t
,
371 # if SANITIZER_ANDROID
372 // FIXME: Move this out into *libcdep.cpp
373 extern "C" SANITIZER_WEAK_ATTRIBUTE
void __libc_iterate_dynamic_tls(
374 pid_t
, void (*cb
)(void *, void *, uptr
, void *), void *);
377 static void ProcessThreadRegistry(Frontier
*frontier
) {
378 InternalMmapVector
<uptr
> ptrs
;
379 GetAdditionalThreadContextPtrsLocked(&ptrs
);
381 for (uptr i
= 0; i
< ptrs
.size(); ++i
) {
382 void *ptr
= reinterpret_cast<void *>(ptrs
[i
]);
383 uptr chunk
= PointsIntoChunk(ptr
);
386 LsanMetadata
m(chunk
);
390 // Mark as reachable and add to frontier.
391 LOG_POINTERS("Treating pointer %p from ThreadContext as reachable\n", ptr
);
392 m
.set_tag(kReachable
);
393 frontier
->push_back(chunk
);
397 // Scans thread data (stacks and TLS) for heap pointers.
398 static void ProcessThreads(SuspendedThreadsList
const &suspended_threads
,
399 Frontier
*frontier
, tid_t caller_tid
,
401 InternalMmapVector
<uptr
> registers
;
402 InternalMmapVector
<Range
> extra_ranges
;
403 for (uptr i
= 0; i
< suspended_threads
.ThreadCount(); i
++) {
404 tid_t os_id
= static_cast<tid_t
>(suspended_threads
.GetThreadID(i
));
405 LOG_THREADS("Processing thread %llu.\n", os_id
);
406 uptr stack_begin
, stack_end
, tls_begin
, tls_end
, cache_begin
, cache_end
;
409 GetThreadRangesLocked(os_id
, &stack_begin
, &stack_end
, &tls_begin
,
410 &tls_end
, &cache_begin
, &cache_end
, &dtls
);
412 // If a thread can't be found in the thread registry, it's probably in the
413 // process of destruction. Log this event and move on.
414 LOG_THREADS("Thread %llu not found in registry.\n", os_id
);
418 PtraceRegistersStatus have_registers
=
419 suspended_threads
.GetRegistersAndSP(i
, ®isters
, &sp
);
420 if (have_registers
!= REGISTERS_AVAILABLE
) {
421 Report("Unable to get registers from thread %llu.\n", os_id
);
422 // If unable to get SP, consider the entire stack to be reachable unless
423 // GetRegistersAndSP failed with ESRCH.
424 if (have_registers
== REGISTERS_UNAVAILABLE_FATAL
)
428 if (suspended_threads
.GetThreadID(i
) == caller_tid
) {
432 if (flags()->use_registers
&& have_registers
) {
433 uptr registers_begin
= reinterpret_cast<uptr
>(registers
.data());
435 reinterpret_cast<uptr
>(registers
.data() + registers
.size());
436 ScanRangeForPointers(registers_begin
, registers_end
, frontier
,
437 "REGISTERS", kReachable
);
440 if (flags()->use_stacks
) {
441 LOG_THREADS("Stack at %p-%p (SP = %p).\n", (void *)stack_begin
,
442 (void *)stack_end
, (void *)sp
);
443 if (sp
< stack_begin
|| sp
>= stack_end
) {
444 // SP is outside the recorded stack range (e.g. the thread is running a
445 // signal handler on alternate stack, or swapcontext was used).
446 // Again, consider the entire stack range to be reachable.
447 LOG_THREADS("WARNING: stack pointer not in stack range.\n");
448 uptr page_size
= GetPageSizeCached();
450 while (stack_begin
< stack_end
&&
451 !IsAccessibleMemoryRange(stack_begin
, 1)) {
453 stack_begin
+= page_size
;
455 LOG_THREADS("Skipped %d guard page(s) to obtain stack %p-%p.\n",
456 skipped
, (void *)stack_begin
, (void *)stack_end
);
458 // Shrink the stack range to ignore out-of-scope values.
461 ScanRangeForPointers(stack_begin
, stack_end
, frontier
, "STACK",
463 extra_ranges
.clear();
464 GetThreadExtraStackRangesLocked(os_id
, &extra_ranges
);
465 ScanExtraStackRanges(extra_ranges
, frontier
);
468 if (flags()->use_tls
) {
470 LOG_THREADS("TLS at %p-%p.\n", (void *)tls_begin
, (void *)tls_end
);
471 // If the tls and cache ranges don't overlap, scan full tls range,
472 // otherwise, only scan the non-overlapping portions
473 if (cache_begin
== cache_end
|| tls_end
< cache_begin
||
474 tls_begin
> cache_end
) {
475 ScanRangeForPointers(tls_begin
, tls_end
, frontier
, "TLS", kReachable
);
477 if (tls_begin
< cache_begin
)
478 ScanRangeForPointers(tls_begin
, cache_begin
, frontier
, "TLS",
480 if (tls_end
> cache_end
)
481 ScanRangeForPointers(cache_end
, tls_end
, frontier
, "TLS",
485 # if SANITIZER_ANDROID
486 auto *cb
= +[](void *dtls_begin
, void *dtls_end
, uptr
/*dso_idd*/,
488 ScanRangeForPointers(reinterpret_cast<uptr
>(dtls_begin
),
489 reinterpret_cast<uptr
>(dtls_end
),
490 reinterpret_cast<Frontier
*>(arg
), "DTLS",
494 // FIXME: There might be a race-condition here (and in Bionic) if the
495 // thread is suspended in the middle of updating its DTLS. IOWs, we
496 // could scan already freed memory. (probably fine for now)
497 __libc_iterate_dynamic_tls(os_id
, cb
, frontier
);
499 if (dtls
&& !DTLSInDestruction(dtls
)) {
500 ForEachDVT(dtls
, [&](const DTLS::DTV
&dtv
, int id
) {
501 uptr dtls_beg
= dtv
.beg
;
502 uptr dtls_end
= dtls_beg
+ dtv
.size
;
503 if (dtls_beg
< dtls_end
) {
504 LOG_THREADS("DTLS %d at %p-%p.\n", id
, (void *)dtls_beg
,
506 ScanRangeForPointers(dtls_beg
, dtls_end
, frontier
, "DTLS",
511 // We are handling a thread with DTLS under destruction. Log about
512 // this and continue.
513 LOG_THREADS("Thread %llu has DTLS under destruction.\n", os_id
);
519 // Add pointers reachable from ThreadContexts
520 ProcessThreadRegistry(frontier
);
523 # endif // SANITIZER_FUCHSIA
525 // A map that contains [region_begin, region_end) pairs.
526 using RootRegions
= DenseMap
<detail::DenseMapPair
<uptr
, uptr
>, uptr
>;
528 static RootRegions
&GetRootRegionsLocked() {
529 global_mutex
.CheckLocked();
530 static RootRegions
*regions
= nullptr;
531 alignas(RootRegions
) static char placeholder
[sizeof(RootRegions
)];
533 regions
= new (placeholder
) RootRegions();
537 bool HasRootRegions() { return !GetRootRegionsLocked().empty(); }
539 void ScanRootRegions(Frontier
*frontier
,
540 const InternalMmapVectorNoCtor
<Region
> &mapped_regions
) {
541 if (!flags()->use_root_regions
)
544 InternalMmapVector
<Region
> regions
;
545 GetRootRegionsLocked().forEach([&](const auto &kv
) {
546 regions
.push_back({kv
.first
.first
, kv
.first
.second
});
550 InternalMmapVector
<Region
> intersection
;
551 Intersect(mapped_regions
, regions
, intersection
);
553 for (const Region
&r
: intersection
) {
554 LOG_POINTERS("Root region intersects with mapped region at %p-%p\n",
555 (void *)r
.begin
, (void *)r
.end
);
556 ScanRangeForPointers(r
.begin
, r
.end
, frontier
, "ROOT", kReachable
);
560 // Scans root regions for heap pointers.
561 static void ProcessRootRegions(Frontier
*frontier
) {
562 if (!flags()->use_root_regions
|| !HasRootRegions())
564 MemoryMappingLayout
proc_maps(/*cache_enabled*/ true);
565 MemoryMappedSegment segment
;
566 InternalMmapVector
<Region
> mapped_regions
;
567 while (proc_maps
.Next(&segment
))
568 if (segment
.IsReadable())
569 mapped_regions
.push_back({segment
.start
, segment
.end
});
570 ScanRootRegions(frontier
, mapped_regions
);
573 static void FloodFillTag(Frontier
*frontier
, ChunkTag tag
) {
574 while (frontier
->size()) {
575 uptr next_chunk
= frontier
->back();
576 frontier
->pop_back();
577 LsanMetadata
m(next_chunk
);
578 ScanRangeForPointers(next_chunk
, next_chunk
+ m
.requested_size(), frontier
,
583 // ForEachChunk callback. If the chunk is marked as leaked, marks all chunks
584 // which are reachable from it as indirectly leaked.
585 static void MarkIndirectlyLeakedCb(uptr chunk
, void *arg
) {
586 chunk
= GetUserBegin(chunk
);
587 LsanMetadata
m(chunk
);
588 if (m
.allocated() && m
.tag() != kReachable
) {
589 ScanRangeForPointers(chunk
, chunk
+ m
.requested_size(),
590 /* frontier */ nullptr, "HEAP", kIndirectlyLeaked
);
594 static void IgnoredSuppressedCb(uptr chunk
, void *arg
) {
596 chunk
= GetUserBegin(chunk
);
597 LsanMetadata
m(chunk
);
598 if (!m
.allocated() || m
.tag() == kIgnored
)
601 const InternalMmapVector
<u32
> &suppressed
=
602 *static_cast<const InternalMmapVector
<u32
> *>(arg
);
603 uptr idx
= InternalLowerBound(suppressed
, m
.stack_trace_id());
604 if (idx
>= suppressed
.size() || m
.stack_trace_id() != suppressed
[idx
])
607 LOG_POINTERS("Suppressed: chunk %p-%p of size %zu.\n", (void *)chunk
,
608 (void *)(chunk
+ m
.requested_size()), m
.requested_size());
612 // ForEachChunk callback. If chunk is marked as ignored, adds its address to
614 static void CollectIgnoredCb(uptr chunk
, void *arg
) {
616 chunk
= GetUserBegin(chunk
);
617 LsanMetadata
m(chunk
);
618 if (m
.allocated() && m
.tag() == kIgnored
) {
619 LOG_POINTERS("Ignored: chunk %p-%p of size %zu.\n", (void *)chunk
,
620 (void *)(chunk
+ m
.requested_size()), m
.requested_size());
621 reinterpret_cast<Frontier
*>(arg
)->push_back(chunk
);
625 // Sets the appropriate tag on each chunk.
626 static void ClassifyAllChunks(SuspendedThreadsList
const &suspended_threads
,
627 Frontier
*frontier
, tid_t caller_tid
,
629 const InternalMmapVector
<u32
> &suppressed_stacks
=
630 GetSuppressionContext()->GetSortedSuppressedStacks();
631 if (!suppressed_stacks
.empty()) {
632 ForEachChunk(IgnoredSuppressedCb
,
633 const_cast<InternalMmapVector
<u32
> *>(&suppressed_stacks
));
635 ForEachChunk(CollectIgnoredCb
, frontier
);
636 ProcessGlobalRegions(frontier
);
637 ProcessThreads(suspended_threads
, frontier
, caller_tid
, caller_sp
);
638 ProcessRootRegions(frontier
);
639 FloodFillTag(frontier
, kReachable
);
641 // The check here is relatively expensive, so we do this in a separate flood
642 // fill. That way we can skip the check for chunks that are reachable
644 LOG_POINTERS("Processing platform-specific allocations.\n");
645 ProcessPlatformSpecificAllocations(frontier
);
646 FloodFillTag(frontier
, kReachable
);
648 // Iterate over leaked chunks and mark those that are reachable from other
650 LOG_POINTERS("Scanning leaked chunks.\n");
651 ForEachChunk(MarkIndirectlyLeakedCb
, nullptr);
654 // ForEachChunk callback. Resets the tags to pre-leak-check state.
655 static void ResetTagsCb(uptr chunk
, void *arg
) {
657 chunk
= GetUserBegin(chunk
);
658 LsanMetadata
m(chunk
);
659 if (m
.allocated() && m
.tag() != kIgnored
)
660 m
.set_tag(kDirectlyLeaked
);
663 // ForEachChunk callback. Aggregates information about unreachable chunks into
665 static void CollectLeaksCb(uptr chunk
, void *arg
) {
667 LeakedChunks
*leaks
= reinterpret_cast<LeakedChunks
*>(arg
);
668 chunk
= GetUserBegin(chunk
);
669 LsanMetadata
m(chunk
);
672 if (m
.tag() == kDirectlyLeaked
|| m
.tag() == kIndirectlyLeaked
)
673 leaks
->push_back({chunk
, m
.stack_trace_id(), m
.requested_size(), m
.tag()});
676 void LeakSuppressionContext::PrintMatchedSuppressions() {
677 InternalMmapVector
<Suppression
*> matched
;
678 context
.GetMatched(&matched
);
681 const char *line
= "-----------------------------------------------------";
682 Printf("%s\n", line
);
683 Printf("Suppressions used:\n");
684 Printf(" count bytes template\n");
685 for (uptr i
= 0; i
< matched
.size(); i
++) {
686 Printf("%7zu %10zu %s\n",
687 static_cast<uptr
>(atomic_load_relaxed(&matched
[i
]->hit_count
)),
688 matched
[i
]->weight
, matched
[i
]->templ
);
690 Printf("%s\n\n", line
);
693 # if SANITIZER_FUCHSIA
695 // Fuchsia provides a libc interface that guarantees all threads are
696 // covered, and SuspendedThreadList is never really used.
697 static void ReportUnsuspendedThreads(const SuspendedThreadsList
&) {}
699 # else // !SANITIZER_FUCHSIA
701 static void ReportUnsuspendedThreads(
702 const SuspendedThreadsList
&suspended_threads
) {
703 InternalMmapVector
<tid_t
> threads(suspended_threads
.ThreadCount());
704 for (uptr i
= 0; i
< suspended_threads
.ThreadCount(); ++i
)
705 threads
[i
] = suspended_threads
.GetThreadID(i
);
707 Sort(threads
.data(), threads
.size());
709 InternalMmapVector
<tid_t
> unsuspended
;
710 GetRunningThreadsLocked(&unsuspended
);
712 for (auto os_id
: unsuspended
) {
713 uptr i
= InternalLowerBound(threads
, os_id
);
714 if (i
>= threads
.size() || threads
[i
] != os_id
)
716 "Running thread %zu was not suspended. False leaks are possible.\n",
721 # endif // !SANITIZER_FUCHSIA
723 static void CheckForLeaksCallback(const SuspendedThreadsList
&suspended_threads
,
725 CheckForLeaksParam
*param
= reinterpret_cast<CheckForLeaksParam
*>(arg
);
727 CHECK(!param
->success
);
728 ReportUnsuspendedThreads(suspended_threads
);
729 ClassifyAllChunks(suspended_threads
, ¶m
->frontier
, param
->caller_tid
,
731 ForEachChunk(CollectLeaksCb
, ¶m
->leaks
);
732 // Clean up for subsequent leak checks. This assumes we did not overwrite any
734 ForEachChunk(ResetTagsCb
, nullptr);
735 param
->success
= true;
738 static bool PrintResults(LeakReport
&report
) {
739 uptr unsuppressed_count
= report
.UnsuppressedLeakCount();
740 if (unsuppressed_count
) {
744 "================================================================="
746 Printf("%s", d
.Error());
747 Report("ERROR: LeakSanitizer: detected memory leaks\n");
748 Printf("%s", d
.Default());
749 report
.ReportTopLeaks(flags()->max_leaks
);
751 if (common_flags()->print_suppressions
)
752 GetSuppressionContext()->PrintMatchedSuppressions();
753 if (unsuppressed_count
> 0) {
754 report
.PrintSummary();
760 static bool CheckForLeaks() {
761 if (&__lsan_is_turned_off
&& __lsan_is_turned_off()) {
762 VReport(1, "LeakSanitizer is disabled");
765 VReport(1, "LeakSanitizer: checking for leaks");
766 // Inside LockStuffAndStopTheWorld we can't run symbolizer, so we can't match
767 // suppressions. However if a stack id was previously suppressed, it should be
768 // suppressed in future checks as well.
769 for (int i
= 0;; ++i
) {
770 EnsureMainThreadIDIsCorrect();
771 CheckForLeaksParam param
;
772 // Capture calling thread's stack pointer early, to avoid false negatives.
773 // Old frame with dead pointers might be overlapped by new frame inside
774 // CheckForLeaks which does not use bytes with pointers before the
775 // threads are suspended and stack pointers captured.
776 param
.caller_tid
= GetTid();
777 param
.caller_sp
= reinterpret_cast<uptr
>(__builtin_frame_address(0));
778 LockStuffAndStopTheWorld(CheckForLeaksCallback
, ¶m
);
779 if (!param
.success
) {
780 Report("LeakSanitizer has encountered a fatal error.\n");
782 "HINT: For debugging, try setting environment variable "
783 "LSAN_OPTIONS=verbosity=1:log_threads=1\n");
785 "HINT: LeakSanitizer does not work under ptrace (strace, gdb, "
789 LeakReport leak_report
;
790 leak_report
.AddLeakedChunks(param
.leaks
);
792 // No new suppressions stacks, so rerun will not help and we can report.
793 if (!leak_report
.ApplySuppressions())
794 return PrintResults(leak_report
);
796 // No indirect leaks to report, so we are done here.
797 if (!leak_report
.IndirectUnsuppressedLeakCount())
798 return PrintResults(leak_report
);
801 Report("WARNING: LeakSanitizer gave up on indirect leaks suppression.\n");
802 return PrintResults(leak_report
);
805 // We found a new previously unseen suppressed call stack. Rerun to make
806 // sure it does not hold indirect leaks.
807 VReport(1, "Rerun with %zu suppressed stacks.",
808 GetSuppressionContext()->GetSortedSuppressedStacks().size());
812 static bool has_reported_leaks
= false;
813 bool HasReportedLeaks() { return has_reported_leaks
; }
816 Lock
l(&global_mutex
);
817 static bool already_done
;
821 has_reported_leaks
= CheckForLeaks();
822 if (has_reported_leaks
)
826 static int DoRecoverableLeakCheck() {
827 Lock
l(&global_mutex
);
828 bool have_leaks
= CheckForLeaks();
829 return have_leaks
? 1 : 0;
832 void DoRecoverableLeakCheckVoid() { DoRecoverableLeakCheck(); }
834 ///// LeakReport implementation. /////
836 // A hard limit on the number of distinct leaks, to avoid quadratic complexity
837 // in LeakReport::AddLeakedChunk(). We don't expect to ever see this many leaks
838 // in real-world applications.
839 // FIXME: Get rid of this limit by moving logic into DedupLeaks.
840 const uptr kMaxLeaksConsidered
= 5000;
842 void LeakReport::AddLeakedChunks(const LeakedChunks
&chunks
) {
843 for (const LeakedChunk
&leak
: chunks
) {
844 uptr chunk
= leak
.chunk
;
845 u32 stack_trace_id
= leak
.stack_trace_id
;
846 uptr leaked_size
= leak
.leaked_size
;
847 ChunkTag tag
= leak
.tag
;
848 CHECK(tag
== kDirectlyLeaked
|| tag
== kIndirectlyLeaked
);
850 if (u32 resolution
= flags()->resolution
) {
851 StackTrace stack
= StackDepotGet(stack_trace_id
);
852 stack
.size
= Min(stack
.size
, resolution
);
853 stack_trace_id
= StackDepotPut(stack
);
856 bool is_directly_leaked
= (tag
== kDirectlyLeaked
);
858 for (i
= 0; i
< leaks_
.size(); i
++) {
859 if (leaks_
[i
].stack_trace_id
== stack_trace_id
&&
860 leaks_
[i
].is_directly_leaked
== is_directly_leaked
) {
861 leaks_
[i
].hit_count
++;
862 leaks_
[i
].total_size
+= leaked_size
;
866 if (i
== leaks_
.size()) {
867 if (leaks_
.size() == kMaxLeaksConsidered
)
869 Leak leak
= {next_id_
++, /* hit_count */ 1,
870 leaked_size
, stack_trace_id
,
871 is_directly_leaked
, /* is_suppressed */ false};
872 leaks_
.push_back(leak
);
874 if (flags()->report_objects
) {
875 LeakedObject obj
= {leaks_
[i
].id
, GetUserAddr(chunk
), leaked_size
};
876 leaked_objects_
.push_back(obj
);
881 static bool LeakComparator(const Leak
&leak1
, const Leak
&leak2
) {
882 if (leak1
.is_directly_leaked
== leak2
.is_directly_leaked
)
883 return leak1
.total_size
> leak2
.total_size
;
885 return leak1
.is_directly_leaked
;
888 void LeakReport::ReportTopLeaks(uptr num_leaks_to_report
) {
889 CHECK(leaks_
.size() <= kMaxLeaksConsidered
);
891 if (leaks_
.size() == kMaxLeaksConsidered
)
893 "Too many leaks! Only the first %zu leaks encountered will be "
895 kMaxLeaksConsidered
);
897 uptr unsuppressed_count
= UnsuppressedLeakCount();
898 if (num_leaks_to_report
> 0 && num_leaks_to_report
< unsuppressed_count
)
899 Printf("The %zu top leak(s):\n", num_leaks_to_report
);
900 Sort(leaks_
.data(), leaks_
.size(), &LeakComparator
);
901 uptr leaks_reported
= 0;
902 for (uptr i
= 0; i
< leaks_
.size(); i
++) {
903 if (leaks_
[i
].is_suppressed
)
905 PrintReportForLeak(i
);
907 if (leaks_reported
== num_leaks_to_report
)
910 if (leaks_reported
< unsuppressed_count
) {
911 uptr remaining
= unsuppressed_count
- leaks_reported
;
912 Printf("Omitting %zu more leak(s).\n", remaining
);
916 void LeakReport::PrintReportForLeak(uptr index
) {
918 Printf("%s", d
.Leak());
919 Printf("%s leak of %zu byte(s) in %zu object(s) allocated from:\n",
920 leaks_
[index
].is_directly_leaked
? "Direct" : "Indirect",
921 leaks_
[index
].total_size
, leaks_
[index
].hit_count
);
922 Printf("%s", d
.Default());
924 CHECK(leaks_
[index
].stack_trace_id
);
925 StackDepotGet(leaks_
[index
].stack_trace_id
).Print();
927 if (flags()->report_objects
) {
928 Printf("Objects leaked above:\n");
929 PrintLeakedObjectsForLeak(index
);
934 void LeakReport::PrintLeakedObjectsForLeak(uptr index
) {
935 u32 leak_id
= leaks_
[index
].id
;
936 for (uptr j
= 0; j
< leaked_objects_
.size(); j
++) {
937 if (leaked_objects_
[j
].leak_id
== leak_id
)
938 Printf("%p (%zu bytes)\n", (void *)leaked_objects_
[j
].addr
,
939 leaked_objects_
[j
].size
);
943 void LeakReport::PrintSummary() {
944 CHECK(leaks_
.size() <= kMaxLeaksConsidered
);
945 uptr bytes
= 0, allocations
= 0;
946 for (uptr i
= 0; i
< leaks_
.size(); i
++) {
947 if (leaks_
[i
].is_suppressed
)
949 bytes
+= leaks_
[i
].total_size
;
950 allocations
+= leaks_
[i
].hit_count
;
952 InternalScopedString summary
;
953 summary
.AppendF("%zu byte(s) leaked in %zu allocation(s).", bytes
,
955 ReportErrorSummary(summary
.data());
958 uptr
LeakReport::ApplySuppressions() {
959 LeakSuppressionContext
*suppressions
= GetSuppressionContext();
960 uptr new_suppressions
= 0;
961 for (uptr i
= 0; i
< leaks_
.size(); i
++) {
962 if (suppressions
->Suppress(leaks_
[i
].stack_trace_id
, leaks_
[i
].hit_count
,
963 leaks_
[i
].total_size
)) {
964 leaks_
[i
].is_suppressed
= true;
968 return new_suppressions
;
971 uptr
LeakReport::UnsuppressedLeakCount() {
973 for (uptr i
= 0; i
< leaks_
.size(); i
++)
974 if (!leaks_
[i
].is_suppressed
)
979 uptr
LeakReport::IndirectUnsuppressedLeakCount() {
981 for (uptr i
= 0; i
< leaks_
.size(); i
++)
982 if (!leaks_
[i
].is_suppressed
&& !leaks_
[i
].is_directly_leaked
)
987 } // namespace __lsan
988 #else // CAN_SANITIZE_LEAKS
990 void InitCommonLsan() {}
991 void DoLeakCheck() {}
992 void DoRecoverableLeakCheckVoid() {}
993 void DisableInThisThread() {}
994 void EnableInThisThread() {}
995 } // namespace __lsan
996 #endif // CAN_SANITIZE_LEAKS
998 using namespace __lsan
;
1001 SANITIZER_INTERFACE_ATTRIBUTE
1002 void __lsan_ignore_object(const void *p
) {
1003 #if CAN_SANITIZE_LEAKS
1004 if (!common_flags()->detect_leaks
)
1006 // Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not
1008 Lock
l(&global_mutex
);
1009 IgnoreObjectResult res
= IgnoreObject(p
);
1010 if (res
== kIgnoreObjectInvalid
)
1011 VReport(1, "__lsan_ignore_object(): no heap object found at %p\n", p
);
1012 if (res
== kIgnoreObjectAlreadyIgnored
)
1014 "__lsan_ignore_object(): "
1015 "heap object at %p is already being ignored\n",
1017 if (res
== kIgnoreObjectSuccess
)
1018 VReport(1, "__lsan_ignore_object(): ignoring heap object at %p\n", p
);
1019 #endif // CAN_SANITIZE_LEAKS
1022 SANITIZER_INTERFACE_ATTRIBUTE
1023 void __lsan_register_root_region(const void *begin
, uptr size
) {
1024 #if CAN_SANITIZE_LEAKS
1025 VReport(1, "Registered root region at %p of size %zu\n", begin
, size
);
1026 uptr b
= reinterpret_cast<uptr
>(begin
);
1030 Lock
l(&global_mutex
);
1031 ++GetRootRegionsLocked()[{b
, e
}];
1032 #endif // CAN_SANITIZE_LEAKS
1035 SANITIZER_INTERFACE_ATTRIBUTE
1036 void __lsan_unregister_root_region(const void *begin
, uptr size
) {
1037 #if CAN_SANITIZE_LEAKS
1038 uptr b
= reinterpret_cast<uptr
>(begin
);
1041 VReport(1, "Unregistered root region at %p of size %zu\n", begin
, size
);
1044 Lock
l(&global_mutex
);
1045 if (auto *f
= GetRootRegionsLocked().find({b
, e
})) {
1046 if (--(f
->second
) == 0)
1047 GetRootRegionsLocked().erase(f
);
1052 "__lsan_unregister_root_region(): region at %p of size %zu has not "
1053 "been registered.\n",
1056 #endif // CAN_SANITIZE_LEAKS
1059 SANITIZER_INTERFACE_ATTRIBUTE
1060 void __lsan_disable() {
1061 #if CAN_SANITIZE_LEAKS
1062 __lsan::DisableInThisThread();
1066 SANITIZER_INTERFACE_ATTRIBUTE
1067 void __lsan_enable() {
1068 #if CAN_SANITIZE_LEAKS
1069 __lsan::EnableInThisThread();
1073 SANITIZER_INTERFACE_ATTRIBUTE
1074 void __lsan_do_leak_check() {
1075 #if CAN_SANITIZE_LEAKS
1076 if (common_flags()->detect_leaks
)
1077 __lsan::DoLeakCheck();
1078 #endif // CAN_SANITIZE_LEAKS
1081 SANITIZER_INTERFACE_ATTRIBUTE
1082 int __lsan_do_recoverable_leak_check() {
1083 #if CAN_SANITIZE_LEAKS
1084 if (common_flags()->detect_leaks
)
1085 return __lsan::DoRecoverableLeakCheck();
1086 #endif // CAN_SANITIZE_LEAKS
1090 SANITIZER_INTERFACE_WEAK_DEF(const char *, __lsan_default_options
, void) {
1094 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
1095 SANITIZER_INTERFACE_WEAK_DEF(int, __lsan_is_turned_off
, void) {
1099 SANITIZER_INTERFACE_WEAK_DEF(const char *, __lsan_default_suppressions
, void) {