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