* Add missing ChangeLog entry.
[official-gcc.git] / libsanitizer / lsan / lsan_common.cc
blobe340b8953e1f821a97347fe3797708e249ce3279
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(bool standalone) {
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->use_registers = true;
45 f->use_globals = true;
46 f->use_stacks = true;
47 f->use_tls = true;
48 f->use_root_regions = true;
49 f->use_unaligned = false;
50 f->use_poisoned = false;
51 f->log_pointers = false;
52 f->log_threads = false;
54 const char *options = GetEnv("LSAN_OPTIONS");
55 if (options) {
56 ParseFlag(options, &f->use_registers, "use_registers", "");
57 ParseFlag(options, &f->use_globals, "use_globals", "");
58 ParseFlag(options, &f->use_stacks, "use_stacks", "");
59 ParseFlag(options, &f->use_tls, "use_tls", "");
60 ParseFlag(options, &f->use_root_regions, "use_root_regions", "");
61 ParseFlag(options, &f->use_unaligned, "use_unaligned", "");
62 ParseFlag(options, &f->use_poisoned, "use_poisoned", "");
63 ParseFlag(options, &f->report_objects, "report_objects", "");
64 ParseFlag(options, &f->resolution, "resolution", "");
65 CHECK_GE(&f->resolution, 0);
66 ParseFlag(options, &f->max_leaks, "max_leaks", "");
67 CHECK_GE(&f->max_leaks, 0);
68 ParseFlag(options, &f->log_pointers, "log_pointers", "");
69 ParseFlag(options, &f->log_threads, "log_threads", "");
70 ParseFlag(options, &f->exitcode, "exitcode", "");
73 // Set defaults for common flags (only in standalone mode) and parse
74 // them from LSAN_OPTIONS.
75 CommonFlags *cf = common_flags();
76 if (standalone) {
77 SetCommonFlagsDefaults(cf);
78 cf->external_symbolizer_path = GetEnv("LSAN_SYMBOLIZER_PATH");
79 cf->malloc_context_size = 30;
80 cf->detect_leaks = true;
82 ParseCommonFlagsFromString(cf, options);
85 #define LOG_POINTERS(...) \
86 do { \
87 if (flags()->log_pointers) Report(__VA_ARGS__); \
88 } while (0);
90 #define LOG_THREADS(...) \
91 do { \
92 if (flags()->log_threads) Report(__VA_ARGS__); \
93 } while (0);
95 static bool suppressions_inited = false;
97 void InitializeSuppressions() {
98 CHECK(!suppressions_inited);
99 SuppressionContext::InitIfNecessary();
100 if (&__lsan_default_suppressions)
101 SuppressionContext::Get()->Parse(__lsan_default_suppressions());
102 suppressions_inited = true;
105 struct RootRegion {
106 const void *begin;
107 uptr size;
110 InternalMmapVector<RootRegion> *root_regions;
112 void InitializeRootRegions() {
113 CHECK(!root_regions);
114 ALIGNED(64) static char placeholder[sizeof(InternalMmapVector<RootRegion>)];
115 root_regions = new(placeholder) InternalMmapVector<RootRegion>(1);
118 void InitCommonLsan(bool standalone) {
119 InitializeFlags(standalone);
120 InitializeRootRegions();
121 if (common_flags()->detect_leaks) {
122 // Initialization which can fail or print warnings should only be done if
123 // LSan is actually enabled.
124 InitializeSuppressions();
125 InitializePlatformSpecificModules();
129 class Decorator: public __sanitizer::SanitizerCommonDecorator {
130 public:
131 Decorator() : SanitizerCommonDecorator() { }
132 const char *Error() { return Red(); }
133 const char *Leak() { return Blue(); }
134 const char *End() { return Default(); }
137 static inline bool CanBeAHeapPointer(uptr p) {
138 // Since our heap is located in mmap-ed memory, we can assume a sensible lower
139 // bound on heap addresses.
140 const uptr kMinAddress = 4 * 4096;
141 if (p < kMinAddress) return false;
142 #ifdef __x86_64__
143 // Accept only canonical form user-space addresses.
144 return ((p >> 47) == 0);
145 #else
146 return true;
147 #endif
150 // Scans the memory range, looking for byte patterns that point into allocator
151 // chunks. Marks those chunks with |tag| and adds them to |frontier|.
152 // There are two usage modes for this function: finding reachable or ignored
153 // chunks (|tag| = kReachable or kIgnored) and finding indirectly leaked chunks
154 // (|tag| = kIndirectlyLeaked). In the second case, there's no flood fill,
155 // so |frontier| = 0.
156 void ScanRangeForPointers(uptr begin, uptr end,
157 Frontier *frontier,
158 const char *region_type, ChunkTag tag) {
159 const uptr alignment = flags()->pointer_alignment();
160 LOG_POINTERS("Scanning %s range %p-%p.\n", region_type, begin, end);
161 uptr pp = begin;
162 if (pp % alignment)
163 pp = pp + alignment - pp % alignment;
164 for (; pp + sizeof(void *) <= end; pp += alignment) { // NOLINT
165 void *p = *reinterpret_cast<void **>(pp);
166 if (!CanBeAHeapPointer(reinterpret_cast<uptr>(p))) continue;
167 uptr chunk = PointsIntoChunk(p);
168 if (!chunk) continue;
169 // Pointers to self don't count. This matters when tag == kIndirectlyLeaked.
170 if (chunk == begin) continue;
171 LsanMetadata m(chunk);
172 // Reachable beats ignored beats leaked.
173 if (m.tag() == kReachable) continue;
174 if (m.tag() == kIgnored && tag != kReachable) continue;
176 // Do this check relatively late so we can log only the interesting cases.
177 if (!flags()->use_poisoned && WordIsPoisoned(pp)) {
178 LOG_POINTERS(
179 "%p is poisoned: ignoring %p pointing into chunk %p-%p of size "
180 "%zu.\n",
181 pp, p, chunk, chunk + m.requested_size(), m.requested_size());
182 continue;
185 m.set_tag(tag);
186 LOG_POINTERS("%p: found %p pointing into chunk %p-%p of size %zu.\n", pp, p,
187 chunk, chunk + m.requested_size(), m.requested_size());
188 if (frontier)
189 frontier->push_back(chunk);
193 void ForEachExtraStackRangeCb(uptr begin, uptr end, void* arg) {
194 Frontier *frontier = reinterpret_cast<Frontier *>(arg);
195 ScanRangeForPointers(begin, end, frontier, "FAKE STACK", kReachable);
198 // Scans thread data (stacks and TLS) for heap pointers.
199 static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
200 Frontier *frontier) {
201 InternalScopedBuffer<uptr> registers(SuspendedThreadsList::RegisterCount());
202 uptr registers_begin = reinterpret_cast<uptr>(registers.data());
203 uptr registers_end = registers_begin + registers.size();
204 for (uptr i = 0; i < suspended_threads.thread_count(); i++) {
205 uptr os_id = static_cast<uptr>(suspended_threads.GetThreadID(i));
206 LOG_THREADS("Processing thread %d.\n", os_id);
207 uptr stack_begin, stack_end, tls_begin, tls_end, cache_begin, cache_end;
208 bool thread_found = GetThreadRangesLocked(os_id, &stack_begin, &stack_end,
209 &tls_begin, &tls_end,
210 &cache_begin, &cache_end);
211 if (!thread_found) {
212 // If a thread can't be found in the thread registry, it's probably in the
213 // process of destruction. Log this event and move on.
214 LOG_THREADS("Thread %d not found in registry.\n", os_id);
215 continue;
217 uptr sp;
218 bool have_registers =
219 (suspended_threads.GetRegistersAndSP(i, registers.data(), &sp) == 0);
220 if (!have_registers) {
221 Report("Unable to get registers from thread %d.\n");
222 // If unable to get SP, consider the entire stack to be reachable.
223 sp = stack_begin;
226 if (flags()->use_registers && have_registers)
227 ScanRangeForPointers(registers_begin, registers_end, frontier,
228 "REGISTERS", kReachable);
230 if (flags()->use_stacks) {
231 LOG_THREADS("Stack at %p-%p (SP = %p).\n", stack_begin, stack_end, sp);
232 if (sp < stack_begin || sp >= stack_end) {
233 // SP is outside the recorded stack range (e.g. the thread is running a
234 // signal handler on alternate stack). Again, consider the entire stack
235 // range to be reachable.
236 LOG_THREADS("WARNING: stack pointer not in stack range.\n");
237 } else {
238 // Shrink the stack range to ignore out-of-scope values.
239 stack_begin = sp;
241 ScanRangeForPointers(stack_begin, stack_end, frontier, "STACK",
242 kReachable);
243 ForEachExtraStackRange(os_id, ForEachExtraStackRangeCb, frontier);
246 if (flags()->use_tls) {
247 LOG_THREADS("TLS at %p-%p.\n", tls_begin, tls_end);
248 if (cache_begin == cache_end) {
249 ScanRangeForPointers(tls_begin, tls_end, frontier, "TLS", kReachable);
250 } else {
251 // Because LSan should not be loaded with dlopen(), we can assume
252 // that allocator cache will be part of static TLS image.
253 CHECK_LE(tls_begin, cache_begin);
254 CHECK_GE(tls_end, cache_end);
255 if (tls_begin < cache_begin)
256 ScanRangeForPointers(tls_begin, cache_begin, frontier, "TLS",
257 kReachable);
258 if (tls_end > cache_end)
259 ScanRangeForPointers(cache_end, tls_end, frontier, "TLS", kReachable);
265 static void ProcessRootRegion(Frontier *frontier, uptr root_begin,
266 uptr root_end) {
267 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
268 uptr begin, end, prot;
269 while (proc_maps.Next(&begin, &end,
270 /*offset*/ 0, /*filename*/ 0, /*filename_size*/ 0,
271 &prot)) {
272 uptr intersection_begin = Max(root_begin, begin);
273 uptr intersection_end = Min(end, root_end);
274 if (intersection_begin >= intersection_end) continue;
275 bool is_readable = prot & MemoryMappingLayout::kProtectionRead;
276 LOG_POINTERS("Root region %p-%p intersects with mapped region %p-%p (%s)\n",
277 root_begin, root_end, begin, end,
278 is_readable ? "readable" : "unreadable");
279 if (is_readable)
280 ScanRangeForPointers(intersection_begin, intersection_end, frontier,
281 "ROOT", kReachable);
285 // Scans root regions for heap pointers.
286 static void ProcessRootRegions(Frontier *frontier) {
287 if (!flags()->use_root_regions) return;
288 CHECK(root_regions);
289 for (uptr i = 0; i < root_regions->size(); i++) {
290 RootRegion region = (*root_regions)[i];
291 uptr begin_addr = reinterpret_cast<uptr>(region.begin);
292 ProcessRootRegion(frontier, begin_addr, begin_addr + region.size);
296 static void FloodFillTag(Frontier *frontier, ChunkTag tag) {
297 while (frontier->size()) {
298 uptr next_chunk = frontier->back();
299 frontier->pop_back();
300 LsanMetadata m(next_chunk);
301 ScanRangeForPointers(next_chunk, next_chunk + m.requested_size(), frontier,
302 "HEAP", tag);
306 // ForEachChunk callback. If the chunk is marked as leaked, marks all chunks
307 // which are reachable from it as indirectly leaked.
308 static void MarkIndirectlyLeakedCb(uptr chunk, void *arg) {
309 chunk = GetUserBegin(chunk);
310 LsanMetadata m(chunk);
311 if (m.allocated() && m.tag() != kReachable) {
312 ScanRangeForPointers(chunk, chunk + m.requested_size(),
313 /* frontier */ 0, "HEAP", kIndirectlyLeaked);
317 // ForEachChunk callback. If chunk is marked as ignored, adds its address to
318 // frontier.
319 static void CollectIgnoredCb(uptr chunk, void *arg) {
320 CHECK(arg);
321 chunk = GetUserBegin(chunk);
322 LsanMetadata m(chunk);
323 if (m.allocated() && m.tag() == kIgnored)
324 reinterpret_cast<Frontier *>(arg)->push_back(chunk);
327 // Sets the appropriate tag on each chunk.
328 static void ClassifyAllChunks(SuspendedThreadsList const &suspended_threads) {
329 // Holds the flood fill frontier.
330 Frontier frontier(1);
332 ProcessGlobalRegions(&frontier);
333 ProcessThreads(suspended_threads, &frontier);
334 ProcessRootRegions(&frontier);
335 FloodFillTag(&frontier, kReachable);
336 // The check here is relatively expensive, so we do this in a separate flood
337 // fill. That way we can skip the check for chunks that are reachable
338 // otherwise.
339 LOG_POINTERS("Processing platform-specific allocations.\n");
340 ProcessPlatformSpecificAllocations(&frontier);
341 FloodFillTag(&frontier, kReachable);
343 LOG_POINTERS("Scanning ignored chunks.\n");
344 CHECK_EQ(0, frontier.size());
345 ForEachChunk(CollectIgnoredCb, &frontier);
346 FloodFillTag(&frontier, kIgnored);
348 // Iterate over leaked chunks and mark those that are reachable from other
349 // leaked chunks.
350 LOG_POINTERS("Scanning leaked chunks.\n");
351 ForEachChunk(MarkIndirectlyLeakedCb, 0 /* arg */);
354 static void PrintStackTraceById(u32 stack_trace_id) {
355 CHECK(stack_trace_id);
356 uptr size = 0;
357 const uptr *trace = StackDepotGet(stack_trace_id, &size);
358 StackTrace::PrintStack(trace, size);
361 // ForEachChunk callback. Aggregates information about unreachable chunks into
362 // a LeakReport.
363 static void CollectLeaksCb(uptr chunk, void *arg) {
364 CHECK(arg);
365 LeakReport *leak_report = reinterpret_cast<LeakReport *>(arg);
366 chunk = GetUserBegin(chunk);
367 LsanMetadata m(chunk);
368 if (!m.allocated()) return;
369 if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) {
370 uptr resolution = flags()->resolution;
371 u32 stack_trace_id = 0;
372 if (resolution > 0) {
373 uptr size = 0;
374 const uptr *trace = StackDepotGet(m.stack_trace_id(), &size);
375 size = Min(size, resolution);
376 stack_trace_id = StackDepotPut(trace, size);
377 } else {
378 stack_trace_id = m.stack_trace_id();
380 leak_report->AddLeakedChunk(chunk, stack_trace_id, m.requested_size(),
381 m.tag());
385 static void PrintMatchedSuppressions() {
386 InternalMmapVector<Suppression *> matched(1);
387 SuppressionContext::Get()->GetMatched(&matched);
388 if (!matched.size())
389 return;
390 const char *line = "-----------------------------------------------------";
391 Printf("%s\n", line);
392 Printf("Suppressions used:\n");
393 Printf(" count bytes template\n");
394 for (uptr i = 0; i < matched.size(); i++)
395 Printf("%7zu %10zu %s\n", static_cast<uptr>(matched[i]->hit_count),
396 matched[i]->weight, matched[i]->templ);
397 Printf("%s\n\n", line);
400 struct DoLeakCheckParam {
401 bool success;
402 LeakReport leak_report;
405 static void DoLeakCheckCallback(const SuspendedThreadsList &suspended_threads,
406 void *arg) {
407 DoLeakCheckParam *param = reinterpret_cast<DoLeakCheckParam *>(arg);
408 CHECK(param);
409 CHECK(!param->success);
410 ClassifyAllChunks(suspended_threads);
411 ForEachChunk(CollectLeaksCb, &param->leak_report);
412 param->success = true;
415 void DoLeakCheck() {
416 EnsureMainThreadIDIsCorrect();
417 BlockingMutexLock l(&global_mutex);
418 static bool already_done;
419 if (already_done) return;
420 already_done = true;
421 if (&__lsan_is_turned_off && __lsan_is_turned_off())
422 return;
424 DoLeakCheckParam param;
425 param.success = false;
426 LockThreadRegistry();
427 LockAllocator();
428 StopTheWorld(DoLeakCheckCallback, &param);
429 UnlockAllocator();
430 UnlockThreadRegistry();
432 if (!param.success) {
433 Report("LeakSanitizer has encountered a fatal error.\n");
434 Die();
436 param.leak_report.ApplySuppressions();
437 uptr unsuppressed_count = param.leak_report.UnsuppressedLeakCount();
438 if (unsuppressed_count > 0) {
439 Decorator d;
440 Printf("\n"
441 "================================================================="
442 "\n");
443 Printf("%s", d.Error());
444 Report("ERROR: LeakSanitizer: detected memory leaks\n");
445 Printf("%s", d.End());
446 param.leak_report.ReportTopLeaks(flags()->max_leaks);
448 if (common_flags()->print_suppressions)
449 PrintMatchedSuppressions();
450 if (unsuppressed_count > 0) {
451 param.leak_report.PrintSummary();
452 if (flags()->exitcode)
453 internal__exit(flags()->exitcode);
457 static Suppression *GetSuppressionForAddr(uptr addr) {
458 Suppression *s;
460 // Suppress by module name.
461 const char *module_name;
462 uptr module_offset;
463 if (Symbolizer::GetOrInit()
464 ->GetModuleNameAndOffsetForPC(addr, &module_name, &module_offset) &&
465 SuppressionContext::Get()->Match(module_name, SuppressionLeak, &s))
466 return s;
468 // Suppress by file or function name.
469 static const uptr kMaxAddrFrames = 16;
470 InternalScopedBuffer<AddressInfo> addr_frames(kMaxAddrFrames);
471 for (uptr i = 0; i < kMaxAddrFrames; i++) new (&addr_frames[i]) AddressInfo();
472 uptr addr_frames_num = Symbolizer::GetOrInit()->SymbolizePC(
473 addr, addr_frames.data(), kMaxAddrFrames);
474 for (uptr i = 0; i < addr_frames_num; i++) {
475 if (SuppressionContext::Get()->Match(addr_frames[i].function,
476 SuppressionLeak, &s) ||
477 SuppressionContext::Get()->Match(addr_frames[i].file, SuppressionLeak,
478 &s))
479 return s;
481 return 0;
484 static Suppression *GetSuppressionForStack(u32 stack_trace_id) {
485 uptr size = 0;
486 const uptr *trace = StackDepotGet(stack_trace_id, &size);
487 for (uptr i = 0; i < size; i++) {
488 Suppression *s =
489 GetSuppressionForAddr(StackTrace::GetPreviousInstructionPc(trace[i]));
490 if (s) return s;
492 return 0;
495 ///// LeakReport implementation. /////
497 // A hard limit on the number of distinct leaks, to avoid quadratic complexity
498 // in LeakReport::AddLeakedChunk(). We don't expect to ever see this many leaks
499 // in real-world applications.
500 // FIXME: Get rid of this limit by changing the implementation of LeakReport to
501 // use a hash table.
502 const uptr kMaxLeaksConsidered = 5000;
504 void LeakReport::AddLeakedChunk(uptr chunk, u32 stack_trace_id,
505 uptr leaked_size, ChunkTag tag) {
506 CHECK(tag == kDirectlyLeaked || tag == kIndirectlyLeaked);
507 bool is_directly_leaked = (tag == kDirectlyLeaked);
508 uptr i;
509 for (i = 0; i < leaks_.size(); i++) {
510 if (leaks_[i].stack_trace_id == stack_trace_id &&
511 leaks_[i].is_directly_leaked == is_directly_leaked) {
512 leaks_[i].hit_count++;
513 leaks_[i].total_size += leaked_size;
514 break;
517 if (i == leaks_.size()) {
518 if (leaks_.size() == kMaxLeaksConsidered) return;
519 Leak leak = { next_id_++, /* hit_count */ 1, leaked_size, stack_trace_id,
520 is_directly_leaked, /* is_suppressed */ false };
521 leaks_.push_back(leak);
523 if (flags()->report_objects) {
524 LeakedObject obj = {leaks_[i].id, chunk, leaked_size};
525 leaked_objects_.push_back(obj);
529 static bool LeakComparator(const Leak &leak1, const Leak &leak2) {
530 if (leak1.is_directly_leaked == leak2.is_directly_leaked)
531 return leak1.total_size > leak2.total_size;
532 else
533 return leak1.is_directly_leaked;
536 void LeakReport::ReportTopLeaks(uptr num_leaks_to_report) {
537 CHECK(leaks_.size() <= kMaxLeaksConsidered);
538 Printf("\n");
539 if (leaks_.size() == kMaxLeaksConsidered)
540 Printf("Too many leaks! Only the first %zu leaks encountered will be "
541 "reported.\n",
542 kMaxLeaksConsidered);
544 uptr unsuppressed_count = UnsuppressedLeakCount();
545 if (num_leaks_to_report > 0 && num_leaks_to_report < unsuppressed_count)
546 Printf("The %zu top leak(s):\n", num_leaks_to_report);
547 InternalSort(&leaks_, leaks_.size(), LeakComparator);
548 uptr leaks_reported = 0;
549 for (uptr i = 0; i < leaks_.size(); i++) {
550 if (leaks_[i].is_suppressed) continue;
551 PrintReportForLeak(i);
552 leaks_reported++;
553 if (leaks_reported == num_leaks_to_report) break;
555 if (leaks_reported < unsuppressed_count) {
556 uptr remaining = unsuppressed_count - leaks_reported;
557 Printf("Omitting %zu more leak(s).\n", remaining);
561 void LeakReport::PrintReportForLeak(uptr index) {
562 Decorator d;
563 Printf("%s", d.Leak());
564 Printf("%s leak of %zu byte(s) in %zu object(s) allocated from:\n",
565 leaks_[index].is_directly_leaked ? "Direct" : "Indirect",
566 leaks_[index].total_size, leaks_[index].hit_count);
567 Printf("%s", d.End());
569 PrintStackTraceById(leaks_[index].stack_trace_id);
571 if (flags()->report_objects) {
572 Printf("Objects leaked above:\n");
573 PrintLeakedObjectsForLeak(index);
574 Printf("\n");
578 void LeakReport::PrintLeakedObjectsForLeak(uptr index) {
579 u32 leak_id = leaks_[index].id;
580 for (uptr j = 0; j < leaked_objects_.size(); j++) {
581 if (leaked_objects_[j].leak_id == leak_id)
582 Printf("%p (%zu bytes)\n", leaked_objects_[j].addr,
583 leaked_objects_[j].size);
587 void LeakReport::PrintSummary() {
588 CHECK(leaks_.size() <= kMaxLeaksConsidered);
589 uptr bytes = 0, allocations = 0;
590 for (uptr i = 0; i < leaks_.size(); i++) {
591 if (leaks_[i].is_suppressed) continue;
592 bytes += leaks_[i].total_size;
593 allocations += leaks_[i].hit_count;
595 InternalScopedBuffer<char> summary(kMaxSummaryLength);
596 internal_snprintf(summary.data(), summary.size(),
597 "%zu byte(s) leaked in %zu allocation(s).", bytes,
598 allocations);
599 ReportErrorSummary(summary.data());
602 void LeakReport::ApplySuppressions() {
603 for (uptr i = 0; i < leaks_.size(); i++) {
604 Suppression *s = GetSuppressionForStack(leaks_[i].stack_trace_id);
605 if (s) {
606 s->weight += leaks_[i].total_size;
607 s->hit_count += leaks_[i].hit_count;
608 leaks_[i].is_suppressed = true;
613 uptr LeakReport::UnsuppressedLeakCount() {
614 uptr result = 0;
615 for (uptr i = 0; i < leaks_.size(); i++)
616 if (!leaks_[i].is_suppressed) result++;
617 return result;
620 } // namespace __lsan
621 #endif // CAN_SANITIZE_LEAKS
623 using namespace __lsan; // NOLINT
625 extern "C" {
626 SANITIZER_INTERFACE_ATTRIBUTE
627 void __lsan_ignore_object(const void *p) {
628 #if CAN_SANITIZE_LEAKS
629 if (!common_flags()->detect_leaks)
630 return;
631 // Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not
632 // locked.
633 BlockingMutexLock l(&global_mutex);
634 IgnoreObjectResult res = IgnoreObjectLocked(p);
635 if (res == kIgnoreObjectInvalid)
636 VReport(1, "__lsan_ignore_object(): no heap object found at %p", p);
637 if (res == kIgnoreObjectAlreadyIgnored)
638 VReport(1, "__lsan_ignore_object(): "
639 "heap object at %p is already being ignored\n", p);
640 if (res == kIgnoreObjectSuccess)
641 VReport(1, "__lsan_ignore_object(): ignoring heap object at %p\n", p);
642 #endif // CAN_SANITIZE_LEAKS
645 SANITIZER_INTERFACE_ATTRIBUTE
646 void __lsan_register_root_region(const void *begin, uptr size) {
647 #if CAN_SANITIZE_LEAKS
648 BlockingMutexLock l(&global_mutex);
649 CHECK(root_regions);
650 RootRegion region = {begin, size};
651 root_regions->push_back(region);
652 VReport(1, "Registered root region at %p of size %llu\n", begin, size);
653 #endif // CAN_SANITIZE_LEAKS
656 SANITIZER_INTERFACE_ATTRIBUTE
657 void __lsan_unregister_root_region(const void *begin, uptr size) {
658 #if CAN_SANITIZE_LEAKS
659 BlockingMutexLock l(&global_mutex);
660 CHECK(root_regions);
661 bool removed = false;
662 for (uptr i = 0; i < root_regions->size(); i++) {
663 RootRegion region = (*root_regions)[i];
664 if (region.begin == begin && region.size == size) {
665 removed = true;
666 uptr last_index = root_regions->size() - 1;
667 (*root_regions)[i] = (*root_regions)[last_index];
668 root_regions->pop_back();
669 VReport(1, "Unregistered root region at %p of size %llu\n", begin, size);
670 break;
673 if (!removed) {
674 Report(
675 "__lsan_unregister_root_region(): region at %p of size %llu has not "
676 "been registered.\n",
677 begin, size);
678 Die();
680 #endif // CAN_SANITIZE_LEAKS
683 SANITIZER_INTERFACE_ATTRIBUTE
684 void __lsan_disable() {
685 #if CAN_SANITIZE_LEAKS
686 __lsan::disable_counter++;
687 #endif
690 SANITIZER_INTERFACE_ATTRIBUTE
691 void __lsan_enable() {
692 #if CAN_SANITIZE_LEAKS
693 if (!__lsan::disable_counter && common_flags()->detect_leaks) {
694 Report("Unmatched call to __lsan_enable().\n");
695 Die();
697 __lsan::disable_counter--;
698 #endif
701 SANITIZER_INTERFACE_ATTRIBUTE
702 void __lsan_do_leak_check() {
703 #if CAN_SANITIZE_LEAKS
704 if (common_flags()->detect_leaks)
705 __lsan::DoLeakCheck();
706 #endif // CAN_SANITIZE_LEAKS
709 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
710 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
711 int __lsan_is_turned_off() {
712 return 0;
714 #endif
715 } // extern "C"