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