2013-05-20 19:06:50 +08:00
|
|
|
//=-- lsan_common.cc ------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of LeakSanitizer.
|
|
|
|
// Implementation of common leak checking functionality.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lsan_common.h"
|
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stoptheworld.h"
|
|
|
|
|
2013-06-21 23:14:57 +08:00
|
|
|
#if CAN_SANITIZE_LEAKS
|
2013-06-21 23:50:49 +08:00
|
|
|
namespace __lsan {
|
2013-05-21 23:35:34 +08:00
|
|
|
|
2013-06-06 22:17:56 +08:00
|
|
|
// This mutex is used to prevent races between DoLeakCheck and SuppressObject.
|
|
|
|
BlockingMutex global_mutex(LINKER_INITIALIZED);
|
|
|
|
|
2013-06-21 22:51:52 +08:00
|
|
|
THREADLOCAL int disable_counter;
|
2013-06-21 23:50:49 +08:00
|
|
|
bool DisabledInThisThread() { return disable_counter > 0; }
|
2013-06-21 22:51:52 +08:00
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
Flags lsan_flags;
|
|
|
|
|
|
|
|
static void InitializeFlags() {
|
|
|
|
Flags *f = flags();
|
|
|
|
// Default values.
|
2013-06-11 23:26:20 +08:00
|
|
|
f->report_objects = false;
|
2013-05-20 19:06:50 +08:00
|
|
|
f->resolution = 0;
|
|
|
|
f->max_leaks = 0;
|
2013-05-24 21:16:02 +08:00
|
|
|
f->exitcode = 23;
|
2013-05-27 19:41:46 +08:00
|
|
|
f->use_registers = true;
|
|
|
|
f->use_globals = true;
|
|
|
|
f->use_stacks = true;
|
|
|
|
f->use_tls = true;
|
|
|
|
f->use_unaligned = false;
|
2013-06-06 22:17:56 +08:00
|
|
|
f->verbosity = 0;
|
2013-05-20 19:06:50 +08:00
|
|
|
f->log_pointers = false;
|
|
|
|
f->log_threads = false;
|
|
|
|
|
|
|
|
const char *options = GetEnv("LSAN_OPTIONS");
|
|
|
|
if (options) {
|
2013-05-27 19:41:46 +08:00
|
|
|
ParseFlag(options, &f->use_registers, "use_registers");
|
|
|
|
ParseFlag(options, &f->use_globals, "use_globals");
|
|
|
|
ParseFlag(options, &f->use_stacks, "use_stacks");
|
|
|
|
ParseFlag(options, &f->use_tls, "use_tls");
|
|
|
|
ParseFlag(options, &f->use_unaligned, "use_unaligned");
|
2013-06-11 23:26:20 +08:00
|
|
|
ParseFlag(options, &f->report_objects, "report_objects");
|
2013-05-20 19:06:50 +08:00
|
|
|
ParseFlag(options, &f->resolution, "resolution");
|
|
|
|
CHECK_GE(&f->resolution, 0);
|
|
|
|
ParseFlag(options, &f->max_leaks, "max_leaks");
|
|
|
|
CHECK_GE(&f->max_leaks, 0);
|
2013-06-06 22:17:56 +08:00
|
|
|
ParseFlag(options, &f->verbosity, "verbosity");
|
2013-05-20 19:06:50 +08:00
|
|
|
ParseFlag(options, &f->log_pointers, "log_pointers");
|
|
|
|
ParseFlag(options, &f->log_threads, "log_threads");
|
2013-05-24 21:16:02 +08:00
|
|
|
ParseFlag(options, &f->exitcode, "exitcode");
|
2013-05-20 19:06:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitCommonLsan() {
|
|
|
|
InitializeFlags();
|
|
|
|
InitializePlatformSpecificModules();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool CanBeAHeapPointer(uptr p) {
|
|
|
|
// Since our heap is located in mmap-ed memory, we can assume a sensible lower
|
|
|
|
// boundary on heap addresses.
|
|
|
|
const uptr kMinAddress = 4 * 4096;
|
|
|
|
if (p < kMinAddress) return false;
|
|
|
|
#ifdef __x86_64__
|
|
|
|
// Accept only canonical form user-space addresses.
|
|
|
|
return ((p >> 47) == 0);
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the memory range, looking for byte patterns that point into allocator
|
|
|
|
// chunks. Mark those chunks with tag and add them to the frontier.
|
2013-06-21 22:51:52 +08:00
|
|
|
// There are two usage modes for this function: finding reachable or ignored
|
2013-06-11 23:26:20 +08:00
|
|
|
// chunks (tag = kReachable or kIgnored) and finding indirectly leaked chunks
|
2013-05-20 19:06:50 +08:00
|
|
|
// (tag = kIndirectlyLeaked). In the second case, there's no flood fill,
|
|
|
|
// so frontier = 0.
|
2013-06-14 17:59:40 +08:00
|
|
|
void ScanRangeForPointers(uptr begin, uptr end,
|
2013-06-14 18:07:56 +08:00
|
|
|
Frontier *frontier,
|
2013-05-20 19:06:50 +08:00
|
|
|
const char *region_type, ChunkTag tag) {
|
|
|
|
const uptr alignment = flags()->pointer_alignment();
|
|
|
|
if (flags()->log_pointers)
|
|
|
|
Report("Scanning %s range %p-%p.\n", region_type, begin, end);
|
|
|
|
uptr pp = begin;
|
|
|
|
if (pp % alignment)
|
|
|
|
pp = pp + alignment - pp % alignment;
|
2013-06-21 23:10:20 +08:00
|
|
|
for (; pp + sizeof(void *) <= end; pp += alignment) {
|
2013-05-20 19:06:50 +08:00
|
|
|
void *p = *reinterpret_cast<void**>(pp);
|
|
|
|
if (!CanBeAHeapPointer(reinterpret_cast<uptr>(p))) continue;
|
|
|
|
void *chunk = PointsIntoChunk(p);
|
|
|
|
if (!chunk) continue;
|
|
|
|
LsanMetadata m(chunk);
|
2013-06-21 22:51:52 +08:00
|
|
|
// Reachable beats ignored beats leaked.
|
2013-05-20 19:06:50 +08:00
|
|
|
if (m.tag() == kReachable) continue;
|
2013-06-11 23:26:20 +08:00
|
|
|
if (m.tag() == kIgnored && tag != kReachable) continue;
|
2013-05-20 19:06:50 +08:00
|
|
|
m.set_tag(tag);
|
|
|
|
if (flags()->log_pointers)
|
2013-06-21 23:10:20 +08:00
|
|
|
Report("%p: found %p pointing into chunk %p-%p of size %zu.\n", pp, p,
|
2013-05-20 19:06:50 +08:00
|
|
|
chunk, reinterpret_cast<uptr>(chunk) + m.requested_size(),
|
|
|
|
m.requested_size());
|
|
|
|
if (frontier)
|
|
|
|
frontier->push_back(reinterpret_cast<uptr>(chunk));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan thread data (stacks and TLS) for heap pointers.
|
|
|
|
static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
|
2013-06-14 18:07:56 +08:00
|
|
|
Frontier *frontier) {
|
2013-05-20 19:06:50 +08:00
|
|
|
InternalScopedBuffer<uptr> registers(SuspendedThreadsList::RegisterCount());
|
|
|
|
uptr registers_begin = reinterpret_cast<uptr>(registers.data());
|
|
|
|
uptr registers_end = registers_begin + registers.size();
|
|
|
|
for (uptr i = 0; i < suspended_threads.thread_count(); i++) {
|
|
|
|
uptr os_id = static_cast<uptr>(suspended_threads.GetThreadID(i));
|
|
|
|
if (flags()->log_threads) Report("Processing thread %d.\n", os_id);
|
|
|
|
uptr stack_begin, stack_end, tls_begin, tls_end, cache_begin, cache_end;
|
|
|
|
bool thread_found = GetThreadRangesLocked(os_id, &stack_begin, &stack_end,
|
|
|
|
&tls_begin, &tls_end,
|
|
|
|
&cache_begin, &cache_end);
|
|
|
|
if (!thread_found) {
|
|
|
|
// If a thread can't be found in the thread registry, it's probably in the
|
|
|
|
// process of destruction. Log this event and move on.
|
|
|
|
if (flags()->log_threads)
|
|
|
|
Report("Thread %d not found in registry.\n", os_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uptr sp;
|
|
|
|
bool have_registers =
|
|
|
|
(suspended_threads.GetRegistersAndSP(i, registers.data(), &sp) == 0);
|
|
|
|
if (!have_registers) {
|
|
|
|
Report("Unable to get registers from thread %d.\n");
|
|
|
|
// If unable to get SP, consider the entire stack to be reachable.
|
|
|
|
sp = stack_begin;
|
|
|
|
}
|
|
|
|
|
2013-05-27 19:41:46 +08:00
|
|
|
if (flags()->use_registers && have_registers)
|
2013-05-20 19:06:50 +08:00
|
|
|
ScanRangeForPointers(registers_begin, registers_end, frontier,
|
|
|
|
"REGISTERS", kReachable);
|
|
|
|
|
2013-05-27 19:41:46 +08:00
|
|
|
if (flags()->use_stacks) {
|
2013-05-20 19:06:50 +08:00
|
|
|
if (flags()->log_threads)
|
|
|
|
Report("Stack at %p-%p, SP = %p.\n", stack_begin, stack_end, sp);
|
|
|
|
if (sp < stack_begin || sp >= stack_end) {
|
|
|
|
// SP is outside the recorded stack range (e.g. the thread is running a
|
|
|
|
// signal handler on alternate stack). Again, consider the entire stack
|
|
|
|
// range to be reachable.
|
|
|
|
if (flags()->log_threads)
|
|
|
|
Report("WARNING: stack_pointer not in stack_range.\n");
|
|
|
|
} else {
|
|
|
|
// Shrink the stack range to ignore out-of-scope values.
|
|
|
|
stack_begin = sp;
|
|
|
|
}
|
|
|
|
ScanRangeForPointers(stack_begin, stack_end, frontier, "STACK",
|
|
|
|
kReachable);
|
|
|
|
}
|
|
|
|
|
2013-05-27 19:41:46 +08:00
|
|
|
if (flags()->use_tls) {
|
2013-05-20 19:06:50 +08:00
|
|
|
if (flags()->log_threads) Report("TLS at %p-%p.\n", tls_begin, tls_end);
|
2013-05-25 02:07:53 +08:00
|
|
|
if (cache_begin == cache_end) {
|
|
|
|
ScanRangeForPointers(tls_begin, tls_end, frontier, "TLS", kReachable);
|
|
|
|
} else {
|
|
|
|
// Because LSan should not be loaded with dlopen(), we can assume
|
|
|
|
// that allocator cache will be part of static TLS image.
|
|
|
|
CHECK_LE(tls_begin, cache_begin);
|
|
|
|
CHECK_GE(tls_end, cache_end);
|
|
|
|
if (tls_begin < cache_begin)
|
|
|
|
ScanRangeForPointers(tls_begin, cache_begin, frontier, "TLS",
|
|
|
|
kReachable);
|
|
|
|
if (tls_end > cache_end)
|
|
|
|
ScanRangeForPointers(cache_end, tls_end, frontier, "TLS", kReachable);
|
|
|
|
}
|
2013-05-20 19:06:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 18:07:56 +08:00
|
|
|
static void FloodFillTag(Frontier *frontier, ChunkTag tag) {
|
2013-05-20 19:06:50 +08:00
|
|
|
while (frontier->size()) {
|
|
|
|
uptr next_chunk = frontier->back();
|
|
|
|
frontier->pop_back();
|
|
|
|
LsanMetadata m(reinterpret_cast<void *>(next_chunk));
|
|
|
|
ScanRangeForPointers(next_chunk, next_chunk + m.requested_size(), frontier,
|
2013-06-03 19:21:34 +08:00
|
|
|
"HEAP", tag);
|
2013-05-20 19:06:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark leaked chunks which are reachable from other leaked chunks.
|
|
|
|
void MarkIndirectlyLeakedCb::operator()(void *p) const {
|
2013-05-20 21:08:23 +08:00
|
|
|
p = GetUserBegin(p);
|
2013-05-20 19:06:50 +08:00
|
|
|
LsanMetadata m(p);
|
|
|
|
if (m.allocated() && m.tag() != kReachable) {
|
|
|
|
ScanRangeForPointers(reinterpret_cast<uptr>(p),
|
|
|
|
reinterpret_cast<uptr>(p) + m.requested_size(),
|
|
|
|
/* frontier */ 0, "HEAP", kIndirectlyLeaked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:51:52 +08:00
|
|
|
void CollectIgnoredCb::operator()(void *p) const {
|
2013-06-03 19:21:34 +08:00
|
|
|
p = GetUserBegin(p);
|
|
|
|
LsanMetadata m(p);
|
2013-06-11 23:26:20 +08:00
|
|
|
if (m.allocated() && m.tag() == kIgnored)
|
2013-06-03 19:21:34 +08:00
|
|
|
frontier_->push_back(reinterpret_cast<uptr>(p));
|
|
|
|
}
|
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
// Set the appropriate tag on each chunk.
|
|
|
|
static void ClassifyAllChunks(SuspendedThreadsList const &suspended_threads) {
|
|
|
|
// Holds the flood fill frontier.
|
2013-06-14 18:07:56 +08:00
|
|
|
Frontier frontier(GetPageSizeCached());
|
2013-05-20 19:06:50 +08:00
|
|
|
|
2013-05-27 19:41:46 +08:00
|
|
|
if (flags()->use_globals)
|
2013-05-20 19:06:50 +08:00
|
|
|
ProcessGlobalRegions(&frontier);
|
|
|
|
ProcessThreads(suspended_threads, &frontier);
|
2013-06-03 19:21:34 +08:00
|
|
|
FloodFillTag(&frontier, kReachable);
|
|
|
|
// The check here is relatively expensive, so we do this in a separate flood
|
|
|
|
// fill. That way we can skip the check for chunks that are reachable
|
|
|
|
// otherwise.
|
2013-05-20 19:06:50 +08:00
|
|
|
ProcessPlatformSpecificAllocations(&frontier);
|
2013-06-03 19:21:34 +08:00
|
|
|
FloodFillTag(&frontier, kReachable);
|
2013-05-20 19:06:50 +08:00
|
|
|
|
|
|
|
if (flags()->log_pointers)
|
2013-06-11 23:26:20 +08:00
|
|
|
Report("Scanning ignored chunks.\n");
|
2013-06-03 19:21:34 +08:00
|
|
|
CHECK_EQ(0, frontier.size());
|
2013-06-21 22:51:52 +08:00
|
|
|
ForEachChunk(CollectIgnoredCb(&frontier));
|
2013-06-11 23:26:20 +08:00
|
|
|
FloodFillTag(&frontier, kIgnored);
|
2013-05-20 19:06:50 +08:00
|
|
|
|
2013-06-03 19:21:34 +08:00
|
|
|
// Iterate over leaked chunks and mark those that are reachable from other
|
|
|
|
// leaked chunks.
|
|
|
|
if (flags()->log_pointers)
|
2013-06-11 23:26:20 +08:00
|
|
|
Report("Scanning leaked chunks.\n");
|
2013-06-03 19:21:34 +08:00
|
|
|
ForEachChunk(MarkIndirectlyLeakedCb());
|
2013-05-20 19:06:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintStackTraceById(u32 stack_trace_id) {
|
|
|
|
CHECK(stack_trace_id);
|
|
|
|
uptr size = 0;
|
|
|
|
const uptr *trace = StackDepotGet(stack_trace_id, &size);
|
|
|
|
StackTrace::PrintStack(trace, size, common_flags()->symbolize,
|
|
|
|
common_flags()->strip_path_prefix, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollectLeaksCb::operator()(void *p) const {
|
2013-05-20 21:08:23 +08:00
|
|
|
p = GetUserBegin(p);
|
2013-05-20 19:06:50 +08:00
|
|
|
LsanMetadata m(p);
|
|
|
|
if (!m.allocated()) return;
|
2013-06-03 19:21:34 +08:00
|
|
|
if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) {
|
2013-05-20 19:06:50 +08:00
|
|
|
uptr resolution = flags()->resolution;
|
|
|
|
if (resolution > 0) {
|
|
|
|
uptr size = 0;
|
|
|
|
const uptr *trace = StackDepotGet(m.stack_trace_id(), &size);
|
|
|
|
size = Min(size, resolution);
|
|
|
|
leak_report_->Add(StackDepotPut(trace, size), m.requested_size(),
|
|
|
|
m.tag());
|
|
|
|
} else {
|
|
|
|
leak_report_->Add(m.stack_trace_id(), m.requested_size(), m.tag());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CollectLeaks(LeakReport *leak_report) {
|
|
|
|
ForEachChunk(CollectLeaksCb(leak_report));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintLeakedCb::operator()(void *p) const {
|
2013-05-20 21:08:23 +08:00
|
|
|
p = GetUserBegin(p);
|
2013-05-20 19:06:50 +08:00
|
|
|
LsanMetadata m(p);
|
|
|
|
if (!m.allocated()) return;
|
2013-06-03 19:21:34 +08:00
|
|
|
if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) {
|
2013-06-21 23:10:20 +08:00
|
|
|
Printf("%s leaked %zu byte object at %p.\n",
|
2013-05-20 19:06:50 +08:00
|
|
|
m.tag() == kDirectlyLeaked ? "Directly" : "Indirectly",
|
|
|
|
m.requested_size(), p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintLeaked() {
|
2013-06-19 22:04:11 +08:00
|
|
|
Printf("\n");
|
2013-06-11 23:26:20 +08:00
|
|
|
Printf("Reporting individual objects:\n");
|
2013-05-20 19:06:50 +08:00
|
|
|
ForEachChunk(PrintLeakedCb());
|
|
|
|
}
|
|
|
|
|
2013-06-19 22:04:11 +08:00
|
|
|
struct DoLeakCheckParam {
|
|
|
|
bool success;
|
|
|
|
LeakReport leak_report;
|
2013-05-24 21:16:02 +08:00
|
|
|
};
|
|
|
|
|
2013-05-20 19:06:50 +08:00
|
|
|
static void DoLeakCheckCallback(const SuspendedThreadsList &suspended_threads,
|
|
|
|
void *arg) {
|
2013-06-19 22:04:11 +08:00
|
|
|
DoLeakCheckParam *param = reinterpret_cast<DoLeakCheckParam *>(arg);
|
|
|
|
CHECK(param);
|
|
|
|
CHECK(!param->success);
|
|
|
|
CHECK(param->leak_report.IsEmpty());
|
2013-05-20 19:06:50 +08:00
|
|
|
ClassifyAllChunks(suspended_threads);
|
2013-06-19 22:04:11 +08:00
|
|
|
CollectLeaks(¶m->leak_report);
|
|
|
|
if (!param->leak_report.IsEmpty() && flags()->report_objects)
|
2013-05-24 21:16:02 +08:00
|
|
|
PrintLeaked();
|
2013-06-19 22:04:11 +08:00
|
|
|
param->success = true;
|
2013-05-20 19:06:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DoLeakCheck() {
|
2013-06-06 22:17:56 +08:00
|
|
|
BlockingMutexLock l(&global_mutex);
|
2013-06-03 19:21:34 +08:00
|
|
|
static bool already_done;
|
2013-06-06 22:17:56 +08:00
|
|
|
CHECK(!already_done);
|
|
|
|
already_done = true;
|
2013-06-19 22:04:11 +08:00
|
|
|
|
|
|
|
DoLeakCheckParam param;
|
|
|
|
param.success = false;
|
2013-06-03 19:21:34 +08:00
|
|
|
LockThreadRegistry();
|
|
|
|
LockAllocator();
|
2013-06-19 22:04:11 +08:00
|
|
|
StopTheWorld(DoLeakCheckCallback, ¶m);
|
2013-06-03 19:21:34 +08:00
|
|
|
UnlockAllocator();
|
|
|
|
UnlockThreadRegistry();
|
2013-06-19 22:04:11 +08:00
|
|
|
|
|
|
|
if (!param.success) {
|
2013-05-24 21:16:02 +08:00
|
|
|
Report("LeakSanitizer has encountered a fatal error.\n");
|
|
|
|
Die();
|
2013-06-19 22:04:11 +08:00
|
|
|
}
|
|
|
|
if (!param.leak_report.IsEmpty()) {
|
|
|
|
Printf("\n================================================================="
|
|
|
|
"\n");
|
|
|
|
Report("ERROR: LeakSanitizer: detected memory leaks\n");
|
|
|
|
param.leak_report.PrintLargest(flags()->max_leaks);
|
|
|
|
param.leak_report.PrintSummary();
|
2013-05-24 21:16:02 +08:00
|
|
|
if (flags()->exitcode)
|
|
|
|
internal__exit(flags()->exitcode);
|
|
|
|
}
|
2013-05-20 19:06:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
///// LeakReport implementation. /////
|
|
|
|
|
|
|
|
// A hard limit on the number of distinct leaks, to avoid quadratic complexity
|
|
|
|
// in LeakReport::Add(). We don't expect to ever see this many leaks in
|
|
|
|
// real-world applications.
|
|
|
|
// FIXME: Get rid of this limit by changing the implementation of LeakReport to
|
|
|
|
// use a hash table.
|
|
|
|
const uptr kMaxLeaksConsidered = 1000;
|
|
|
|
|
|
|
|
void LeakReport::Add(u32 stack_trace_id, uptr leaked_size, ChunkTag tag) {
|
|
|
|
CHECK(tag == kDirectlyLeaked || tag == kIndirectlyLeaked);
|
|
|
|
bool is_directly_leaked = (tag == kDirectlyLeaked);
|
|
|
|
for (uptr i = 0; i < leaks_.size(); i++)
|
|
|
|
if (leaks_[i].stack_trace_id == stack_trace_id &&
|
|
|
|
leaks_[i].is_directly_leaked == is_directly_leaked) {
|
|
|
|
leaks_[i].hit_count++;
|
|
|
|
leaks_[i].total_size += leaked_size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (leaks_.size() == kMaxLeaksConsidered) return;
|
|
|
|
Leak leak = { /* hit_count */ 1, leaked_size, stack_trace_id,
|
|
|
|
is_directly_leaked };
|
|
|
|
leaks_.push_back(leak);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsLarger(const Leak &leak1, const Leak &leak2) {
|
|
|
|
return leak1.total_size > leak2.total_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LeakReport::PrintLargest(uptr max_leaks) {
|
|
|
|
CHECK(leaks_.size() <= kMaxLeaksConsidered);
|
|
|
|
Printf("\n");
|
|
|
|
if (leaks_.size() == kMaxLeaksConsidered)
|
2013-06-21 23:10:20 +08:00
|
|
|
Printf("Too many leaks! Only the first %zu leaks encountered will be "
|
2013-05-20 19:06:50 +08:00
|
|
|
"reported.\n",
|
|
|
|
kMaxLeaksConsidered);
|
|
|
|
if (max_leaks > 0 && max_leaks < leaks_.size())
|
2013-06-21 23:10:20 +08:00
|
|
|
Printf("The %zu largest leak(s):\n", max_leaks);
|
2013-05-20 19:06:50 +08:00
|
|
|
InternalSort(&leaks_, leaks_.size(), IsLarger);
|
|
|
|
max_leaks = max_leaks > 0 ? Min(max_leaks, leaks_.size()) : leaks_.size();
|
|
|
|
for (uptr i = 0; i < max_leaks; i++) {
|
2013-06-21 23:10:20 +08:00
|
|
|
Printf("%s leak of %zu byte(s) in %zu object(s) allocated from:\n",
|
2013-05-20 19:06:50 +08:00
|
|
|
leaks_[i].is_directly_leaked ? "Direct" : "Indirect",
|
2013-05-24 23:36:30 +08:00
|
|
|
leaks_[i].total_size, leaks_[i].hit_count);
|
2013-05-20 19:06:50 +08:00
|
|
|
PrintStackTraceById(leaks_[i].stack_trace_id);
|
2013-05-24 22:49:13 +08:00
|
|
|
Printf("\n");
|
2013-05-20 19:06:50 +08:00
|
|
|
}
|
|
|
|
if (max_leaks < leaks_.size()) {
|
|
|
|
uptr remaining = leaks_.size() - max_leaks;
|
2013-06-21 23:10:20 +08:00
|
|
|
Printf("Omitting %zu more leak(s).\n", remaining);
|
2013-05-20 19:06:50 +08:00
|
|
|
}
|
|
|
|
}
|
2013-05-21 23:35:34 +08:00
|
|
|
|
2013-05-24 22:49:13 +08:00
|
|
|
void LeakReport::PrintSummary() {
|
|
|
|
CHECK(leaks_.size() <= kMaxLeaksConsidered);
|
2013-05-24 23:36:30 +08:00
|
|
|
uptr bytes = 0, allocations = 0;
|
2013-05-24 22:49:13 +08:00
|
|
|
for (uptr i = 0; i < leaks_.size(); i++) {
|
2013-05-24 23:36:30 +08:00
|
|
|
bytes += leaks_[i].total_size;
|
|
|
|
allocations += leaks_[i].hit_count;
|
2013-05-24 22:49:13 +08:00
|
|
|
}
|
2013-06-21 22:51:52 +08:00
|
|
|
Printf(
|
2013-06-21 23:10:20 +08:00
|
|
|
"SUMMARY: LeakSanitizer: %zu byte(s) leaked in %zu allocation(s).\n\n",
|
2013-06-21 22:51:52 +08:00
|
|
|
bytes, allocations);
|
2013-05-24 22:49:13 +08:00
|
|
|
}
|
2013-05-20 19:06:50 +08:00
|
|
|
} // namespace __lsan
|
2013-06-21 23:50:49 +08:00
|
|
|
#endif // CAN_SANITIZE_LEAKS
|
2013-06-06 22:17:56 +08:00
|
|
|
|
|
|
|
using namespace __lsan; // NOLINT
|
|
|
|
|
|
|
|
extern "C" {
|
2013-06-07 02:40:55 +08:00
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
2013-06-06 22:17:56 +08:00
|
|
|
void __lsan_ignore_object(const void *p) {
|
2013-06-20 21:39:42 +08:00
|
|
|
#if CAN_SANITIZE_LEAKS
|
2013-06-06 22:17:56 +08:00
|
|
|
// Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not
|
|
|
|
// locked.
|
|
|
|
BlockingMutexLock l(&global_mutex);
|
|
|
|
IgnoreObjectResult res = IgnoreObjectLocked(p);
|
|
|
|
if (res == kIgnoreObjectInvalid && flags()->verbosity >= 1)
|
|
|
|
Report("__lsan_ignore_object(): no heap object found at %p", p);
|
|
|
|
if (res == kIgnoreObjectAlreadyIgnored && flags()->verbosity >= 1)
|
|
|
|
Report("__lsan_ignore_object(): "
|
|
|
|
"heap object at %p is already being ignored\n", p);
|
|
|
|
if (res == kIgnoreObjectSuccess && flags()->verbosity >= 2)
|
|
|
|
Report("__lsan_ignore_object(): ignoring heap object at %p\n", p);
|
2013-06-20 21:39:42 +08:00
|
|
|
#endif // CAN_SANITIZE_LEAKS
|
2013-06-06 22:17:56 +08:00
|
|
|
}
|
2013-06-21 22:51:52 +08:00
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __lsan_disable() {
|
|
|
|
#if CAN_SANITIZE_LEAKS
|
|
|
|
__lsan::disable_counter++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __lsan_enable() {
|
|
|
|
#if CAN_SANITIZE_LEAKS
|
|
|
|
if (!__lsan::disable_counter) {
|
|
|
|
Report("Unmatched call to __lsan_enable().\n");
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
__lsan::disable_counter--;
|
|
|
|
#endif
|
|
|
|
}
|
2013-06-06 22:17:56 +08:00
|
|
|
} // extern "C"
|