* config/abi/post/alpha-linux-gnu/baseline_symbols.txt: Update.
[official-gcc.git] / libsanitizer / lsan / lsan_common.cc
blob78afa7706d6b64d29d152c22538ab6f2c81c1f95
1 //=-- lsan_common.cc ------------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of LeakSanitizer.
9 // Implementation of common leak checking functionality.
11 //===----------------------------------------------------------------------===//
13 #include "lsan_common.h"
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "sanitizer_common/sanitizer_flags.h"
17 #include "sanitizer_common/sanitizer_placement_new.h"
18 #include "sanitizer_common/sanitizer_procmaps.h"
19 #include "sanitizer_common/sanitizer_stackdepot.h"
20 #include "sanitizer_common/sanitizer_stacktrace.h"
21 #include "sanitizer_common/sanitizer_stoptheworld.h"
22 #include "sanitizer_common/sanitizer_suppressions.h"
23 #include "sanitizer_common/sanitizer_report_decorator.h"
25 #if CAN_SANITIZE_LEAKS
26 namespace __lsan {
28 // This mutex is used to prevent races between DoLeakCheck and IgnoreObject, and
29 // also to protect the global list of root regions.
30 BlockingMutex global_mutex(LINKER_INITIALIZED);
32 THREADLOCAL int disable_counter;
33 bool DisabledInThisThread() { return disable_counter > 0; }
35 Flags lsan_flags;
37 static void InitializeFlags() {
38 Flags *f = flags();
39 // Default values.
40 f->report_objects = false;
41 f->resolution = 0;
42 f->max_leaks = 0;
43 f->exitcode = 23;
44 f->print_suppressions = true;
45 f->suppressions="";
46 f->use_registers = true;
47 f->use_globals = true;
48 f->use_stacks = true;
49 f->use_tls = true;
50 f->use_root_regions = true;
51 f->use_unaligned = false;
52 f->use_poisoned = false;
53 f->log_pointers = false;
54 f->log_threads = false;
56 const char *options = GetEnv("LSAN_OPTIONS");
57 if (options) {
58 ParseFlag(options, &f->use_registers, "use_registers", "");
59 ParseFlag(options, &f->use_globals, "use_globals", "");
60 ParseFlag(options, &f->use_stacks, "use_stacks", "");
61 ParseFlag(options, &f->use_tls, "use_tls", "");
62 ParseFlag(options, &f->use_root_regions, "use_root_regions", "");
63 ParseFlag(options, &f->use_unaligned, "use_unaligned", "");
64 ParseFlag(options, &f->use_poisoned, "use_poisoned", "");
65 ParseFlag(options, &f->report_objects, "report_objects", "");
66 ParseFlag(options, &f->resolution, "resolution", "");
67 CHECK_GE(&f->resolution, 0);
68 ParseFlag(options, &f->max_leaks, "max_leaks", "");
69 CHECK_GE(&f->max_leaks, 0);
70 ParseFlag(options, &f->log_pointers, "log_pointers", "");
71 ParseFlag(options, &f->log_threads, "log_threads", "");
72 ParseFlag(options, &f->exitcode, "exitcode", "");
73 ParseFlag(options, &f->print_suppressions, "print_suppressions", "");
74 ParseFlag(options, &f->suppressions, "suppressions", "");
78 #define LOG_POINTERS(...) \
79 do { \
80 if (flags()->log_pointers) Report(__VA_ARGS__); \
81 } while (0);
83 #define LOG_THREADS(...) \
84 do { \
85 if (flags()->log_threads) Report(__VA_ARGS__); \
86 } while (0);
88 SuppressionContext *suppression_ctx;
90 void InitializeSuppressions() {
91 CHECK(!suppression_ctx);
92 ALIGNED(64) static char placeholder[sizeof(SuppressionContext)];
93 suppression_ctx = new(placeholder) SuppressionContext;
94 char *suppressions_from_file;
95 uptr buffer_size;
96 if (ReadFileToBuffer(flags()->suppressions, &suppressions_from_file,
97 &buffer_size, 1 << 26 /* max_len */))
98 suppression_ctx->Parse(suppressions_from_file);
99 if (flags()->suppressions[0] && !buffer_size) {
100 Printf("LeakSanitizer: failed to read suppressions file '%s'\n",
101 flags()->suppressions);
102 Die();
104 if (&__lsan_default_suppressions)
105 suppression_ctx->Parse(__lsan_default_suppressions());
108 struct RootRegion {
109 const void *begin;
110 uptr size;
113 InternalMmapVector<RootRegion> *root_regions;
115 void InitializeRootRegions() {
116 CHECK(!root_regions);
117 ALIGNED(64) static char placeholder[sizeof(InternalMmapVector<RootRegion>)];
118 root_regions = new(placeholder) InternalMmapVector<RootRegion>(1);
121 void InitCommonLsan() {
122 InitializeFlags();
123 InitializeRootRegions();
124 if (common_flags()->detect_leaks) {
125 // Initialization which can fail or print warnings should only be done if
126 // LSan is actually enabled.
127 InitializeSuppressions();
128 InitializePlatformSpecificModules();
132 class Decorator: private __sanitizer::AnsiColorDecorator {
133 public:
134 Decorator() : __sanitizer::AnsiColorDecorator(PrintsToTtyCached()) { }
135 const char *Error() { return Red(); }
136 const char *Leak() { return Blue(); }
137 const char *End() { return Default(); }
140 static inline bool CanBeAHeapPointer(uptr p) {
141 // Since our heap is located in mmap-ed memory, we can assume a sensible lower
142 // bound on heap addresses.
143 const uptr kMinAddress = 4 * 4096;
144 if (p < kMinAddress) return false;
145 #ifdef __x86_64__
146 // Accept only canonical form user-space addresses.
147 return ((p >> 47) == 0);
148 #else
149 return true;
150 #endif
153 // Scans the memory range, looking for byte patterns that point into allocator
154 // chunks. Marks those chunks with |tag| and adds them to |frontier|.
155 // There are two usage modes for this function: finding reachable or ignored
156 // chunks (|tag| = kReachable or kIgnored) and finding indirectly leaked chunks
157 // (|tag| = kIndirectlyLeaked). In the second case, there's no flood fill,
158 // so |frontier| = 0.
159 void ScanRangeForPointers(uptr begin, uptr end,
160 Frontier *frontier,
161 const char *region_type, ChunkTag tag) {
162 const uptr alignment = flags()->pointer_alignment();
163 LOG_POINTERS("Scanning %s range %p-%p.\n", region_type, begin, end);
164 uptr pp = begin;
165 if (pp % alignment)
166 pp = pp + alignment - pp % alignment;
167 for (; pp + sizeof(void *) <= end; pp += alignment) { // NOLINT
168 void *p = *reinterpret_cast<void **>(pp);
169 if (!CanBeAHeapPointer(reinterpret_cast<uptr>(p))) continue;
170 uptr chunk = PointsIntoChunk(p);
171 if (!chunk) continue;
172 // Pointers to self don't count. This matters when tag == kIndirectlyLeaked.
173 if (chunk == begin) continue;
174 LsanMetadata m(chunk);
175 // Reachable beats ignored beats leaked.
176 if (m.tag() == kReachable) continue;
177 if (m.tag() == kIgnored && tag != kReachable) continue;
179 // Do this check relatively late so we can log only the interesting cases.
180 if (!flags()->use_poisoned && WordIsPoisoned(pp)) {
181 LOG_POINTERS(
182 "%p is poisoned: ignoring %p pointing into chunk %p-%p of size "
183 "%zu.\n",
184 pp, p, chunk, chunk + m.requested_size(), m.requested_size());
185 continue;
188 m.set_tag(tag);
189 LOG_POINTERS("%p: found %p pointing into chunk %p-%p of size %zu.\n", pp, p,
190 chunk, chunk + m.requested_size(), m.requested_size());
191 if (frontier)
192 frontier->push_back(chunk);
196 void ForEachExtraStackRangeCb(uptr begin, uptr end, void* arg) {
197 Frontier *frontier = reinterpret_cast<Frontier *>(arg);
198 ScanRangeForPointers(begin, end, frontier, "FAKE STACK", kReachable);
201 // Scans thread data (stacks and TLS) for heap pointers.
202 static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
203 Frontier *frontier) {
204 InternalScopedBuffer<uptr> registers(SuspendedThreadsList::RegisterCount());
205 uptr registers_begin = reinterpret_cast<uptr>(registers.data());
206 uptr registers_end = registers_begin + registers.size();
207 for (uptr i = 0; i < suspended_threads.thread_count(); i++) {
208 uptr os_id = static_cast<uptr>(suspended_threads.GetThreadID(i));
209 LOG_THREADS("Processing thread %d.\n", os_id);
210 uptr stack_begin, stack_end, tls_begin, tls_end, cache_begin, cache_end;
211 bool thread_found = GetThreadRangesLocked(os_id, &stack_begin, &stack_end,
212 &tls_begin, &tls_end,
213 &cache_begin, &cache_end);
214 if (!thread_found) {
215 // If a thread can't be found in the thread registry, it's probably in the
216 // process of destruction. Log this event and move on.
217 LOG_THREADS("Thread %d not found in registry.\n", os_id);
218 continue;
220 uptr sp;
221 bool have_registers =
222 (suspended_threads.GetRegistersAndSP(i, registers.data(), &sp) == 0);
223 if (!have_registers) {
224 Report("Unable to get registers from thread %d.\n");
225 // If unable to get SP, consider the entire stack to be reachable.
226 sp = stack_begin;
229 if (flags()->use_registers && have_registers)
230 ScanRangeForPointers(registers_begin, registers_end, frontier,
231 "REGISTERS", kReachable);
233 if (flags()->use_stacks) {
234 LOG_THREADS("Stack at %p-%p (SP = %p).\n", stack_begin, stack_end, sp);
235 if (sp < stack_begin || sp >= stack_end) {
236 // SP is outside the recorded stack range (e.g. the thread is running a
237 // signal handler on alternate stack). Again, consider the entire stack
238 // range to be reachable.
239 LOG_THREADS("WARNING: stack pointer not in stack range.\n");
240 } else {
241 // Shrink the stack range to ignore out-of-scope values.
242 stack_begin = sp;
244 ScanRangeForPointers(stack_begin, stack_end, frontier, "STACK",
245 kReachable);
246 ForEachExtraStackRange(os_id, ForEachExtraStackRangeCb, frontier);
249 if (flags()->use_tls) {
250 LOG_THREADS("TLS at %p-%p.\n", tls_begin, tls_end);
251 if (cache_begin == cache_end) {
252 ScanRangeForPointers(tls_begin, tls_end, frontier, "TLS", kReachable);
253 } else {
254 // Because LSan should not be loaded with dlopen(), we can assume
255 // that allocator cache will be part of static TLS image.
256 CHECK_LE(tls_begin, cache_begin);
257 CHECK_GE(tls_end, cache_end);
258 if (tls_begin < cache_begin)
259 ScanRangeForPointers(tls_begin, cache_begin, frontier, "TLS",
260 kReachable);
261 if (tls_end > cache_end)
262 ScanRangeForPointers(cache_end, tls_end, frontier, "TLS", kReachable);
268 static void ProcessRootRegion(Frontier *frontier, uptr root_begin,
269 uptr root_end) {
270 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
271 uptr begin, end, prot;
272 while (proc_maps.Next(&begin, &end,
273 /*offset*/ 0, /*filename*/ 0, /*filename_size*/ 0,
274 &prot)) {
275 uptr intersection_begin = Max(root_begin, begin);
276 uptr intersection_end = Min(end, root_end);
277 if (intersection_begin >= intersection_end) continue;
278 bool is_readable = prot & MemoryMappingLayout::kProtectionRead;
279 LOG_POINTERS("Root region %p-%p intersects with mapped region %p-%p (%s)\n",
280 root_begin, root_end, begin, end,
281 is_readable ? "readable" : "unreadable");
282 if (is_readable)
283 ScanRangeForPointers(intersection_begin, intersection_end, frontier,
284 "ROOT", kReachable);
288 // Scans root regions for heap pointers.
289 static void ProcessRootRegions(Frontier *frontier) {
290 if (!flags()->use_root_regions) return;
291 CHECK(root_regions);
292 for (uptr i = 0; i < root_regions->size(); i++) {
293 RootRegion region = (*root_regions)[i];
294 uptr begin_addr = reinterpret_cast<uptr>(region.begin);
295 ProcessRootRegion(frontier, begin_addr, begin_addr + region.size);
299 static void FloodFillTag(Frontier *frontier, ChunkTag tag) {
300 while (frontier->size()) {
301 uptr next_chunk = frontier->back();
302 frontier->pop_back();
303 LsanMetadata m(next_chunk);
304 ScanRangeForPointers(next_chunk, next_chunk + m.requested_size(), frontier,
305 "HEAP", tag);
309 // ForEachChunk callback. If the chunk is marked as leaked, marks all chunks
310 // which are reachable from it as indirectly leaked.
311 static void MarkIndirectlyLeakedCb(uptr chunk, void *arg) {
312 chunk = GetUserBegin(chunk);
313 LsanMetadata m(chunk);
314 if (m.allocated() && m.tag() != kReachable) {
315 ScanRangeForPointers(chunk, chunk + m.requested_size(),
316 /* frontier */ 0, "HEAP", kIndirectlyLeaked);
320 // ForEachChunk callback. If chunk is marked as ignored, adds its address to
321 // frontier.
322 static void CollectIgnoredCb(uptr chunk, void *arg) {
323 CHECK(arg);
324 chunk = GetUserBegin(chunk);
325 LsanMetadata m(chunk);
326 if (m.allocated() && m.tag() == kIgnored)
327 reinterpret_cast<Frontier *>(arg)->push_back(chunk);
330 // Sets the appropriate tag on each chunk.
331 static void ClassifyAllChunks(SuspendedThreadsList const &suspended_threads) {
332 // Holds the flood fill frontier.
333 Frontier frontier(1);
335 ProcessGlobalRegions(&frontier);
336 ProcessThreads(suspended_threads, &frontier);
337 ProcessRootRegions(&frontier);
338 FloodFillTag(&frontier, kReachable);
339 // The check here is relatively expensive, so we do this in a separate flood
340 // fill. That way we can skip the check for chunks that are reachable
341 // otherwise.
342 LOG_POINTERS("Processing platform-specific allocations.\n");
343 ProcessPlatformSpecificAllocations(&frontier);
344 FloodFillTag(&frontier, kReachable);
346 LOG_POINTERS("Scanning ignored chunks.\n");
347 CHECK_EQ(0, frontier.size());
348 ForEachChunk(CollectIgnoredCb, &frontier);
349 FloodFillTag(&frontier, kIgnored);
351 // Iterate over leaked chunks and mark those that are reachable from other
352 // leaked chunks.
353 LOG_POINTERS("Scanning leaked chunks.\n");
354 ForEachChunk(MarkIndirectlyLeakedCb, 0 /* arg */);
357 static void PrintStackTraceById(u32 stack_trace_id) {
358 CHECK(stack_trace_id);
359 uptr size = 0;
360 const uptr *trace = StackDepotGet(stack_trace_id, &size);
361 StackTrace::PrintStack(trace, size);
364 // ForEachChunk callback. Aggregates information about unreachable chunks into
365 // a LeakReport.
366 static void CollectLeaksCb(uptr chunk, void *arg) {
367 CHECK(arg);
368 LeakReport *leak_report = reinterpret_cast<LeakReport *>(arg);
369 chunk = GetUserBegin(chunk);
370 LsanMetadata m(chunk);
371 if (!m.allocated()) return;
372 if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) {
373 uptr resolution = flags()->resolution;
374 u32 stack_trace_id = 0;
375 if (resolution > 0) {
376 uptr size = 0;
377 const uptr *trace = StackDepotGet(m.stack_trace_id(), &size);
378 size = Min(size, resolution);
379 stack_trace_id = StackDepotPut(trace, size);
380 } else {
381 stack_trace_id = m.stack_trace_id();
383 leak_report->AddLeakedChunk(chunk, stack_trace_id, m.requested_size(),
384 m.tag());
388 static void PrintMatchedSuppressions() {
389 InternalMmapVector<Suppression *> matched(1);
390 suppression_ctx->GetMatched(&matched);
391 if (!matched.size())
392 return;
393 const char *line = "-----------------------------------------------------";
394 Printf("%s\n", line);
395 Printf("Suppressions used:\n");
396 Printf(" count bytes template\n");
397 for (uptr i = 0; i < matched.size(); i++)
398 Printf("%7zu %10zu %s\n", static_cast<uptr>(matched[i]->hit_count),
399 matched[i]->weight, matched[i]->templ);
400 Printf("%s\n\n", line);
403 struct DoLeakCheckParam {
404 bool success;
405 LeakReport leak_report;
408 static void DoLeakCheckCallback(const SuspendedThreadsList &suspended_threads,
409 void *arg) {
410 DoLeakCheckParam *param = reinterpret_cast<DoLeakCheckParam *>(arg);
411 CHECK(param);
412 CHECK(!param->success);
413 ClassifyAllChunks(suspended_threads);
414 ForEachChunk(CollectLeaksCb, &param->leak_report);
415 param->success = true;
418 void DoLeakCheck() {
419 EnsureMainThreadIDIsCorrect();
420 BlockingMutexLock l(&global_mutex);
421 static bool already_done;
422 if (already_done) return;
423 already_done = true;
424 if (&__lsan_is_turned_off && __lsan_is_turned_off())
425 return;
427 DoLeakCheckParam param;
428 param.success = false;
429 LockThreadRegistry();
430 LockAllocator();
431 StopTheWorld(DoLeakCheckCallback, &param);
432 UnlockAllocator();
433 UnlockThreadRegistry();
435 if (!param.success) {
436 Report("LeakSanitizer has encountered a fatal error.\n");
437 Die();
439 param.leak_report.ApplySuppressions();
440 uptr unsuppressed_count = param.leak_report.UnsuppressedLeakCount();
441 if (unsuppressed_count > 0) {
442 Decorator d;
443 Printf("\n"
444 "================================================================="
445 "\n");
446 Printf("%s", d.Error());
447 Report("ERROR: LeakSanitizer: detected memory leaks\n");
448 Printf("%s", d.End());
449 param.leak_report.ReportTopLeaks(flags()->max_leaks);
451 if (flags()->print_suppressions)
452 PrintMatchedSuppressions();
453 if (unsuppressed_count > 0) {
454 param.leak_report.PrintSummary();
455 if (flags()->exitcode)
456 internal__exit(flags()->exitcode);
460 static Suppression *GetSuppressionForAddr(uptr addr) {
461 Suppression *s;
463 // Suppress by module name.
464 const char *module_name;
465 uptr module_offset;
466 if (Symbolizer::Get()->GetModuleNameAndOffsetForPC(addr, &module_name,
467 &module_offset) &&
468 suppression_ctx->Match(module_name, SuppressionLeak, &s))
469 return s;
471 // Suppress by file or function name.
472 static const uptr kMaxAddrFrames = 16;
473 InternalScopedBuffer<AddressInfo> addr_frames(kMaxAddrFrames);
474 for (uptr i = 0; i < kMaxAddrFrames; i++) new (&addr_frames[i]) AddressInfo();
475 uptr addr_frames_num = Symbolizer::Get()->SymbolizePC(
476 addr, addr_frames.data(), kMaxAddrFrames);
477 for (uptr i = 0; i < addr_frames_num; i++) {
478 if (suppression_ctx->Match(addr_frames[i].function, SuppressionLeak, &s) ||
479 suppression_ctx->Match(addr_frames[i].file, SuppressionLeak, &s))
480 return s;
482 return 0;
485 static Suppression *GetSuppressionForStack(u32 stack_trace_id) {
486 uptr size = 0;
487 const uptr *trace = StackDepotGet(stack_trace_id, &size);
488 for (uptr i = 0; i < size; i++) {
489 Suppression *s =
490 GetSuppressionForAddr(StackTrace::GetPreviousInstructionPc(trace[i]));
491 if (s) return s;
493 return 0;
496 ///// LeakReport implementation. /////
498 // A hard limit on the number of distinct leaks, to avoid quadratic complexity
499 // in LeakReport::AddLeakedChunk(). We don't expect to ever see this many leaks
500 // in real-world applications.
501 // FIXME: Get rid of this limit by changing the implementation of LeakReport to
502 // use a hash table.
503 const uptr kMaxLeaksConsidered = 5000;
505 void LeakReport::AddLeakedChunk(uptr chunk, u32 stack_trace_id,
506 uptr leaked_size, ChunkTag tag) {
507 CHECK(tag == kDirectlyLeaked || tag == kIndirectlyLeaked);
508 bool is_directly_leaked = (tag == kDirectlyLeaked);
509 uptr i;
510 for (i = 0; i < leaks_.size(); i++) {
511 if (leaks_[i].stack_trace_id == stack_trace_id &&
512 leaks_[i].is_directly_leaked == is_directly_leaked) {
513 leaks_[i].hit_count++;
514 leaks_[i].total_size += leaked_size;
515 break;
518 if (i == leaks_.size()) {
519 if (leaks_.size() == kMaxLeaksConsidered) return;
520 Leak leak = { next_id_++, /* hit_count */ 1, leaked_size, stack_trace_id,
521 is_directly_leaked, /* is_suppressed */ false };
522 leaks_.push_back(leak);
524 if (flags()->report_objects) {
525 LeakedObject obj = {leaks_[i].id, chunk, leaked_size};
526 leaked_objects_.push_back(obj);
530 static bool LeakComparator(const Leak &leak1, const Leak &leak2) {
531 if (leak1.is_directly_leaked == leak2.is_directly_leaked)
532 return leak1.total_size > leak2.total_size;
533 else
534 return leak1.is_directly_leaked;
537 void LeakReport::ReportTopLeaks(uptr num_leaks_to_report) {
538 CHECK(leaks_.size() <= kMaxLeaksConsidered);
539 Printf("\n");
540 if (leaks_.size() == kMaxLeaksConsidered)
541 Printf("Too many leaks! Only the first %zu leaks encountered will be "
542 "reported.\n",
543 kMaxLeaksConsidered);
545 uptr unsuppressed_count = UnsuppressedLeakCount();
546 if (num_leaks_to_report > 0 && num_leaks_to_report < unsuppressed_count)
547 Printf("The %zu top leak(s):\n", num_leaks_to_report);
548 InternalSort(&leaks_, leaks_.size(), LeakComparator);
549 uptr leaks_reported = 0;
550 for (uptr i = 0; i < leaks_.size(); i++) {
551 if (leaks_[i].is_suppressed) continue;
552 PrintReportForLeak(i);
553 leaks_reported++;
554 if (leaks_reported == num_leaks_to_report) break;
556 if (leaks_reported < unsuppressed_count) {
557 uptr remaining = unsuppressed_count - leaks_reported;
558 Printf("Omitting %zu more leak(s).\n", remaining);
562 void LeakReport::PrintReportForLeak(uptr index) {
563 Decorator d;
564 Printf("%s", d.Leak());
565 Printf("%s leak of %zu byte(s) in %zu object(s) allocated from:\n",
566 leaks_[index].is_directly_leaked ? "Direct" : "Indirect",
567 leaks_[index].total_size, leaks_[index].hit_count);
568 Printf("%s", d.End());
570 PrintStackTraceById(leaks_[index].stack_trace_id);
572 if (flags()->report_objects) {
573 Printf("Objects leaked above:\n");
574 PrintLeakedObjectsForLeak(index);
575 Printf("\n");
579 void LeakReport::PrintLeakedObjectsForLeak(uptr index) {
580 u32 leak_id = leaks_[index].id;
581 for (uptr j = 0; j < leaked_objects_.size(); j++) {
582 if (leaked_objects_[j].leak_id == leak_id)
583 Printf("%p (%zu bytes)\n", leaked_objects_[j].addr,
584 leaked_objects_[j].size);
588 void LeakReport::PrintSummary() {
589 CHECK(leaks_.size() <= kMaxLeaksConsidered);
590 uptr bytes = 0, allocations = 0;
591 for (uptr i = 0; i < leaks_.size(); i++) {
592 if (leaks_[i].is_suppressed) continue;
593 bytes += leaks_[i].total_size;
594 allocations += leaks_[i].hit_count;
596 InternalScopedBuffer<char> summary(kMaxSummaryLength);
597 internal_snprintf(summary.data(), summary.size(),
598 "%zu byte(s) leaked in %zu allocation(s).", bytes,
599 allocations);
600 ReportErrorSummary(summary.data());
603 void LeakReport::ApplySuppressions() {
604 for (uptr i = 0; i < leaks_.size(); i++) {
605 Suppression *s = GetSuppressionForStack(leaks_[i].stack_trace_id);
606 if (s) {
607 s->weight += leaks_[i].total_size;
608 s->hit_count += leaks_[i].hit_count;
609 leaks_[i].is_suppressed = true;
614 uptr LeakReport::UnsuppressedLeakCount() {
615 uptr result = 0;
616 for (uptr i = 0; i < leaks_.size(); i++)
617 if (!leaks_[i].is_suppressed) result++;
618 return result;
621 } // namespace __lsan
622 #endif // CAN_SANITIZE_LEAKS
624 using namespace __lsan; // NOLINT
626 extern "C" {
627 SANITIZER_INTERFACE_ATTRIBUTE
628 void __lsan_ignore_object(const void *p) {
629 #if CAN_SANITIZE_LEAKS
630 if (!common_flags()->detect_leaks)
631 return;
632 // Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not
633 // locked.
634 BlockingMutexLock l(&global_mutex);
635 IgnoreObjectResult res = IgnoreObjectLocked(p);
636 if (res == kIgnoreObjectInvalid)
637 VReport(1, "__lsan_ignore_object(): no heap object found at %p", p);
638 if (res == kIgnoreObjectAlreadyIgnored)
639 VReport(1, "__lsan_ignore_object(): "
640 "heap object at %p is already being ignored\n", p);
641 if (res == kIgnoreObjectSuccess)
642 VReport(1, "__lsan_ignore_object(): ignoring heap object at %p\n", p);
643 #endif // CAN_SANITIZE_LEAKS
646 SANITIZER_INTERFACE_ATTRIBUTE
647 void __lsan_register_root_region(const void *begin, uptr size) {
648 #if CAN_SANITIZE_LEAKS
649 BlockingMutexLock l(&global_mutex);
650 CHECK(root_regions);
651 RootRegion region = {begin, size};
652 root_regions->push_back(region);
653 VReport(1, "Registered root region at %p of size %llu\n", begin, size);
654 #endif // CAN_SANITIZE_LEAKS
657 SANITIZER_INTERFACE_ATTRIBUTE
658 void __lsan_unregister_root_region(const void *begin, uptr size) {
659 #if CAN_SANITIZE_LEAKS
660 BlockingMutexLock l(&global_mutex);
661 CHECK(root_regions);
662 bool removed = false;
663 for (uptr i = 0; i < root_regions->size(); i++) {
664 RootRegion region = (*root_regions)[i];
665 if (region.begin == begin && region.size == size) {
666 removed = true;
667 uptr last_index = root_regions->size() - 1;
668 (*root_regions)[i] = (*root_regions)[last_index];
669 root_regions->pop_back();
670 VReport(1, "Unregistered root region at %p of size %llu\n", begin, size);
671 break;
674 if (!removed) {
675 Report(
676 "__lsan_unregister_root_region(): region at %p of size %llu has not "
677 "been registered.\n",
678 begin, size);
679 Die();
681 #endif // CAN_SANITIZE_LEAKS
684 SANITIZER_INTERFACE_ATTRIBUTE
685 void __lsan_disable() {
686 #if CAN_SANITIZE_LEAKS
687 __lsan::disable_counter++;
688 #endif
691 SANITIZER_INTERFACE_ATTRIBUTE
692 void __lsan_enable() {
693 #if CAN_SANITIZE_LEAKS
694 if (!__lsan::disable_counter && common_flags()->detect_leaks) {
695 Report("Unmatched call to __lsan_enable().\n");
696 Die();
698 __lsan::disable_counter--;
699 #endif
702 SANITIZER_INTERFACE_ATTRIBUTE
703 void __lsan_do_leak_check() {
704 #if CAN_SANITIZE_LEAKS
705 if (common_flags()->detect_leaks)
706 __lsan::DoLeakCheck();
707 #endif // CAN_SANITIZE_LEAKS
710 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
711 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
712 int __lsan_is_turned_off() {
713 return 0;
715 #endif
716 } // extern "C"