Retro68/gcc/libsanitizer/lsan/lsan_common.cc

722 lines
24 KiB
C++
Raw Normal View History

2014-09-21 17:33:12 +00:00
//=-- lsan_common.cc ------------------------------------------------------===//
//
// 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"
2017-04-10 11:32:00 +00:00
#include "sanitizer_common/sanitizer_flag_parser.h"
2014-09-21 17:33:12 +00:00
#include "sanitizer_common/sanitizer_placement_new.h"
2015-08-28 15:33:40 +00:00
#include "sanitizer_common/sanitizer_procmaps.h"
2014-09-21 17:33:12 +00:00
#include "sanitizer_common/sanitizer_stackdepot.h"
#include "sanitizer_common/sanitizer_stacktrace.h"
#include "sanitizer_common/sanitizer_suppressions.h"
#include "sanitizer_common/sanitizer_report_decorator.h"
#if CAN_SANITIZE_LEAKS
namespace __lsan {
2015-08-28 15:33:40 +00:00
// This mutex is used to prevent races between DoLeakCheck and IgnoreObject, and
// also to protect the global list of root regions.
2014-09-21 17:33:12 +00:00
BlockingMutex global_mutex(LINKER_INITIALIZED);
THREADLOCAL int disable_counter;
bool DisabledInThisThread() { return disable_counter > 0; }
Flags lsan_flags;
2017-04-10 11:32:00 +00:00
void Flags::SetDefaults() {
#define LSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
#include "lsan_flags.inc"
#undef LSAN_FLAG
}
2015-08-28 15:33:40 +00:00
2017-04-10 11:32:00 +00:00
void RegisterLsanFlags(FlagParser *parser, Flags *f) {
#define LSAN_FLAG(Type, Name, DefaultValue, Description) \
RegisterFlag(parser, #Name, Description, &f->Name);
#include "lsan_flags.inc"
#undef LSAN_FLAG
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
#define LOG_POINTERS(...) \
do { \
if (flags()->log_pointers) Report(__VA_ARGS__); \
} while (0);
#define LOG_THREADS(...) \
do { \
if (flags()->log_threads) Report(__VA_ARGS__); \
} while (0);
2017-04-10 11:32:00 +00:00
ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)];
static SuppressionContext *suppression_ctx = nullptr;
static const char kSuppressionLeak[] = "leak";
static const char *kSuppressionTypes[] = { kSuppressionLeak };
2014-09-21 17:33:12 +00:00
void InitializeSuppressions() {
2017-04-10 11:32:00 +00:00
CHECK_EQ(nullptr, suppression_ctx);
suppression_ctx = new (suppression_placeholder) // NOLINT
SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
suppression_ctx->ParseFromFile(flags()->suppressions);
2014-09-21 17:33:12 +00:00
if (&__lsan_default_suppressions)
2017-04-10 11:32:00 +00:00
suppression_ctx->Parse(__lsan_default_suppressions());
}
static SuppressionContext *GetSuppressionContext() {
CHECK(suppression_ctx);
return suppression_ctx;
2015-08-28 15:33:40 +00:00
}
struct RootRegion {
const void *begin;
uptr size;
};
InternalMmapVector<RootRegion> *root_regions;
void InitializeRootRegions() {
CHECK(!root_regions);
ALIGNED(64) static char placeholder[sizeof(InternalMmapVector<RootRegion>)];
root_regions = new(placeholder) InternalMmapVector<RootRegion>(1);
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
void InitCommonLsan() {
2015-08-28 15:33:40 +00:00
InitializeRootRegions();
2014-09-21 17:33:12 +00:00
if (common_flags()->detect_leaks) {
// Initialization which can fail or print warnings should only be done if
// LSan is actually enabled.
InitializeSuppressions();
InitializePlatformSpecificModules();
}
}
2015-08-28 15:33:40 +00:00
class Decorator: public __sanitizer::SanitizerCommonDecorator {
2014-09-21 17:33:12 +00:00
public:
2015-08-28 15:33:40 +00:00
Decorator() : SanitizerCommonDecorator() { }
2014-09-21 17:33:12 +00:00
const char *Error() { return Red(); }
const char *Leak() { return Blue(); }
const char *End() { return Default(); }
};
static inline bool CanBeAHeapPointer(uptr p) {
// Since our heap is located in mmap-ed memory, we can assume a sensible lower
// bound on heap addresses.
const uptr kMinAddress = 4 * 4096;
if (p < kMinAddress) return false;
2017-04-10 11:32:00 +00:00
#if defined(__x86_64__)
2014-09-21 17:33:12 +00:00
// Accept only canonical form user-space addresses.
return ((p >> 47) == 0);
2017-04-10 11:32:00 +00:00
#elif defined(__mips64)
return ((p >> 40) == 0);
#elif defined(__aarch64__)
unsigned runtimeVMA =
(MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1);
return ((p >> runtimeVMA) == 0);
2014-09-21 17:33:12 +00:00
#else
return true;
#endif
}
// Scans the memory range, looking for byte patterns that point into allocator
// chunks. Marks those chunks with |tag| and adds them to |frontier|.
2017-04-10 11:32:00 +00:00
// There are two usage modes for this function: finding reachable chunks
// (|tag| = kReachable) and finding indirectly leaked chunks
2014-09-21 17:33:12 +00:00
// (|tag| = kIndirectlyLeaked). In the second case, there's no flood fill,
// so |frontier| = 0.
void ScanRangeForPointers(uptr begin, uptr end,
Frontier *frontier,
const char *region_type, ChunkTag tag) {
2017-04-10 11:32:00 +00:00
CHECK(tag == kReachable || tag == kIndirectlyLeaked);
2014-09-21 17:33:12 +00:00
const uptr alignment = flags()->pointer_alignment();
2015-08-28 15:33:40 +00:00
LOG_POINTERS("Scanning %s range %p-%p.\n", region_type, begin, end);
2014-09-21 17:33:12 +00:00
uptr pp = begin;
if (pp % alignment)
pp = pp + alignment - pp % alignment;
for (; pp + sizeof(void *) <= end; pp += alignment) { // NOLINT
void *p = *reinterpret_cast<void **>(pp);
if (!CanBeAHeapPointer(reinterpret_cast<uptr>(p))) continue;
uptr chunk = PointsIntoChunk(p);
if (!chunk) continue;
// Pointers to self don't count. This matters when tag == kIndirectlyLeaked.
if (chunk == begin) continue;
LsanMetadata m(chunk);
2017-04-10 11:32:00 +00:00
if (m.tag() == kReachable || m.tag() == kIgnored) continue;
2015-08-28 15:33:40 +00:00
// Do this check relatively late so we can log only the interesting cases.
if (!flags()->use_poisoned && WordIsPoisoned(pp)) {
LOG_POINTERS(
"%p is poisoned: ignoring %p pointing into chunk %p-%p of size "
"%zu.\n",
pp, p, chunk, chunk + m.requested_size(), m.requested_size());
continue;
}
2014-09-21 17:33:12 +00:00
m.set_tag(tag);
2015-08-28 15:33:40 +00:00
LOG_POINTERS("%p: found %p pointing into chunk %p-%p of size %zu.\n", pp, p,
chunk, chunk + m.requested_size(), m.requested_size());
2014-09-21 17:33:12 +00:00
if (frontier)
frontier->push_back(chunk);
}
}
void ForEachExtraStackRangeCb(uptr begin, uptr end, void* arg) {
Frontier *frontier = reinterpret_cast<Frontier *>(arg);
ScanRangeForPointers(begin, end, frontier, "FAKE STACK", kReachable);
}
// Scans thread data (stacks and TLS) for heap pointers.
static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
Frontier *frontier) {
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));
2015-08-28 15:33:40 +00:00
LOG_THREADS("Processing thread %d.\n", os_id);
2014-09-21 17:33:12 +00:00
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.
2015-08-28 15:33:40 +00:00
LOG_THREADS("Thread %d not found in registry.\n", os_id);
2014-09-21 17:33:12 +00:00
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;
}
if (flags()->use_registers && have_registers)
ScanRangeForPointers(registers_begin, registers_end, frontier,
"REGISTERS", kReachable);
if (flags()->use_stacks) {
2015-08-28 15:33:40 +00:00
LOG_THREADS("Stack at %p-%p (SP = %p).\n", stack_begin, stack_end, sp);
2014-09-21 17:33:12 +00:00
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.
2015-08-28 15:33:40 +00:00
LOG_THREADS("WARNING: stack pointer not in stack range.\n");
2014-09-21 17:33:12 +00:00
} else {
// Shrink the stack range to ignore out-of-scope values.
stack_begin = sp;
}
ScanRangeForPointers(stack_begin, stack_end, frontier, "STACK",
kReachable);
ForEachExtraStackRange(os_id, ForEachExtraStackRangeCb, frontier);
}
if (flags()->use_tls) {
2015-08-28 15:33:40 +00:00
LOG_THREADS("TLS at %p-%p.\n", tls_begin, tls_end);
2014-09-21 17:33:12 +00: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);
}
}
}
}
2015-08-28 15:33:40 +00:00
static void ProcessRootRegion(Frontier *frontier, uptr root_begin,
uptr root_end) {
MemoryMappingLayout proc_maps(/*cache_enabled*/true);
uptr begin, end, prot;
while (proc_maps.Next(&begin, &end,
2017-04-10 11:32:00 +00:00
/*offset*/ nullptr, /*filename*/ nullptr,
/*filename_size*/ 0, &prot)) {
2015-08-28 15:33:40 +00:00
uptr intersection_begin = Max(root_begin, begin);
uptr intersection_end = Min(end, root_end);
if (intersection_begin >= intersection_end) continue;
bool is_readable = prot & MemoryMappingLayout::kProtectionRead;
LOG_POINTERS("Root region %p-%p intersects with mapped region %p-%p (%s)\n",
root_begin, root_end, begin, end,
is_readable ? "readable" : "unreadable");
if (is_readable)
ScanRangeForPointers(intersection_begin, intersection_end, frontier,
"ROOT", kReachable);
}
}
// Scans root regions for heap pointers.
static void ProcessRootRegions(Frontier *frontier) {
if (!flags()->use_root_regions) return;
CHECK(root_regions);
for (uptr i = 0; i < root_regions->size(); i++) {
RootRegion region = (*root_regions)[i];
uptr begin_addr = reinterpret_cast<uptr>(region.begin);
ProcessRootRegion(frontier, begin_addr, begin_addr + region.size);
}
}
2014-09-21 17:33:12 +00:00
static void FloodFillTag(Frontier *frontier, ChunkTag tag) {
while (frontier->size()) {
uptr next_chunk = frontier->back();
frontier->pop_back();
LsanMetadata m(next_chunk);
ScanRangeForPointers(next_chunk, next_chunk + m.requested_size(), frontier,
"HEAP", tag);
}
}
// ForEachChunk callback. If the chunk is marked as leaked, marks all chunks
// which are reachable from it as indirectly leaked.
static void MarkIndirectlyLeakedCb(uptr chunk, void *arg) {
chunk = GetUserBegin(chunk);
LsanMetadata m(chunk);
if (m.allocated() && m.tag() != kReachable) {
ScanRangeForPointers(chunk, chunk + m.requested_size(),
2017-04-10 11:32:00 +00:00
/* frontier */ nullptr, "HEAP", kIndirectlyLeaked);
2014-09-21 17:33:12 +00:00
}
}
// ForEachChunk callback. If chunk is marked as ignored, adds its address to
// frontier.
static void CollectIgnoredCb(uptr chunk, void *arg) {
CHECK(arg);
chunk = GetUserBegin(chunk);
LsanMetadata m(chunk);
2017-04-10 11:32:00 +00:00
if (m.allocated() && m.tag() == kIgnored) {
LOG_POINTERS("Ignored: chunk %p-%p of size %zu.\n",
chunk, chunk + m.requested_size(), m.requested_size());
2014-09-21 17:33:12 +00:00
reinterpret_cast<Frontier *>(arg)->push_back(chunk);
2017-04-10 11:32:00 +00:00
}
2014-09-21 17:33:12 +00:00
}
// Sets the appropriate tag on each chunk.
static void ClassifyAllChunks(SuspendedThreadsList const &suspended_threads) {
// Holds the flood fill frontier.
2015-08-28 15:33:40 +00:00
Frontier frontier(1);
2014-09-21 17:33:12 +00:00
2017-04-10 11:32:00 +00:00
ForEachChunk(CollectIgnoredCb, &frontier);
2015-08-28 15:33:40 +00:00
ProcessGlobalRegions(&frontier);
2014-09-21 17:33:12 +00:00
ProcessThreads(suspended_threads, &frontier);
2015-08-28 15:33:40 +00:00
ProcessRootRegions(&frontier);
2014-09-21 17:33:12 +00:00
FloodFillTag(&frontier, kReachable);
2017-04-10 11:32:00 +00:00
2014-09-21 17:33:12 +00:00
// 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.
2015-08-28 15:33:40 +00:00
LOG_POINTERS("Processing platform-specific allocations.\n");
2017-04-10 11:32:00 +00:00
CHECK_EQ(0, frontier.size());
2014-09-21 17:33:12 +00:00
ProcessPlatformSpecificAllocations(&frontier);
FloodFillTag(&frontier, kReachable);
// Iterate over leaked chunks and mark those that are reachable from other
// leaked chunks.
2015-08-28 15:33:40 +00:00
LOG_POINTERS("Scanning leaked chunks.\n");
2017-04-10 11:32:00 +00:00
ForEachChunk(MarkIndirectlyLeakedCb, nullptr);
}
// ForEachChunk callback. Resets the tags to pre-leak-check state.
static void ResetTagsCb(uptr chunk, void *arg) {
(void)arg;
chunk = GetUserBegin(chunk);
LsanMetadata m(chunk);
if (m.allocated() && m.tag() != kIgnored)
m.set_tag(kDirectlyLeaked);
2014-09-21 17:33:12 +00:00
}
static void PrintStackTraceById(u32 stack_trace_id) {
CHECK(stack_trace_id);
2015-08-28 15:33:40 +00:00
StackDepotGet(stack_trace_id).Print();
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
// ForEachChunk callback. Aggregates information about unreachable chunks into
// a LeakReport.
2014-09-21 17:33:12 +00:00
static void CollectLeaksCb(uptr chunk, void *arg) {
CHECK(arg);
LeakReport *leak_report = reinterpret_cast<LeakReport *>(arg);
chunk = GetUserBegin(chunk);
LsanMetadata m(chunk);
if (!m.allocated()) return;
if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) {
2017-04-10 11:32:00 +00:00
u32 resolution = flags()->resolution;
2015-08-28 15:33:40 +00:00
u32 stack_trace_id = 0;
2014-09-21 17:33:12 +00:00
if (resolution > 0) {
2015-08-28 15:33:40 +00:00
StackTrace stack = StackDepotGet(m.stack_trace_id());
stack.size = Min(stack.size, resolution);
stack_trace_id = StackDepotPut(stack);
2014-09-21 17:33:12 +00:00
} else {
2015-08-28 15:33:40 +00:00
stack_trace_id = m.stack_trace_id();
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
leak_report->AddLeakedChunk(chunk, stack_trace_id, m.requested_size(),
m.tag());
2014-09-21 17:33:12 +00:00
}
}
static void PrintMatchedSuppressions() {
InternalMmapVector<Suppression *> matched(1);
2017-04-10 11:32:00 +00:00
GetSuppressionContext()->GetMatched(&matched);
2014-09-21 17:33:12 +00:00
if (!matched.size())
return;
const char *line = "-----------------------------------------------------";
Printf("%s\n", line);
Printf("Suppressions used:\n");
Printf(" count bytes template\n");
for (uptr i = 0; i < matched.size(); i++)
2017-04-10 11:32:00 +00:00
Printf("%7zu %10zu %s\n", static_cast<uptr>(atomic_load_relaxed(
&matched[i]->hit_count)), matched[i]->weight, matched[i]->templ);
2014-09-21 17:33:12 +00:00
Printf("%s\n\n", line);
}
2017-04-10 11:32:00 +00:00
struct CheckForLeaksParam {
2014-09-21 17:33:12 +00:00
bool success;
LeakReport leak_report;
};
2017-04-10 11:32:00 +00:00
static void CheckForLeaksCallback(const SuspendedThreadsList &suspended_threads,
void *arg) {
CheckForLeaksParam *param = reinterpret_cast<CheckForLeaksParam *>(arg);
2014-09-21 17:33:12 +00:00
CHECK(param);
CHECK(!param->success);
ClassifyAllChunks(suspended_threads);
ForEachChunk(CollectLeaksCb, &param->leak_report);
2017-04-10 11:32:00 +00:00
// Clean up for subsequent leak checks. This assumes we did not overwrite any
// kIgnored tags.
ForEachChunk(ResetTagsCb, nullptr);
2014-09-21 17:33:12 +00:00
param->success = true;
}
2017-04-10 11:32:00 +00:00
static bool CheckForLeaks() {
2014-09-21 17:33:12 +00:00
if (&__lsan_is_turned_off && __lsan_is_turned_off())
2017-04-10 11:32:00 +00:00
return false;
EnsureMainThreadIDIsCorrect();
CheckForLeaksParam param;
2014-09-21 17:33:12 +00:00
param.success = false;
LockThreadRegistry();
LockAllocator();
2017-04-10 11:32:00 +00:00
DoStopTheWorld(CheckForLeaksCallback, &param);
2014-09-21 17:33:12 +00:00
UnlockAllocator();
UnlockThreadRegistry();
if (!param.success) {
Report("LeakSanitizer has encountered a fatal error.\n");
Die();
}
2015-08-28 15:33:40 +00:00
param.leak_report.ApplySuppressions();
uptr unsuppressed_count = param.leak_report.UnsuppressedLeakCount();
if (unsuppressed_count > 0) {
2014-09-21 17:33:12 +00:00
Decorator d;
Printf("\n"
"================================================================="
"\n");
Printf("%s", d.Error());
Report("ERROR: LeakSanitizer: detected memory leaks\n");
Printf("%s", d.End());
2015-08-28 15:33:40 +00:00
param.leak_report.ReportTopLeaks(flags()->max_leaks);
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
if (common_flags()->print_suppressions)
2014-09-21 17:33:12 +00:00
PrintMatchedSuppressions();
2015-08-28 15:33:40 +00:00
if (unsuppressed_count > 0) {
2014-09-21 17:33:12 +00:00
param.leak_report.PrintSummary();
2017-04-10 11:32:00 +00:00
return true;
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
return false;
}
void DoLeakCheck() {
BlockingMutexLock l(&global_mutex);
static bool already_done;
if (already_done) return;
already_done = true;
bool have_leaks = CheckForLeaks();
if (!have_leaks) {
return;
}
if (common_flags()->exitcode) {
Die();
}
}
static int DoRecoverableLeakCheck() {
BlockingMutexLock l(&global_mutex);
bool have_leaks = CheckForLeaks();
return have_leaks ? 1 : 0;
2014-09-21 17:33:12 +00:00
}
static Suppression *GetSuppressionForAddr(uptr addr) {
2017-04-10 11:32:00 +00:00
Suppression *s = nullptr;
2015-08-28 15:33:40 +00:00
// Suppress by module name.
2017-04-10 11:32:00 +00:00
SuppressionContext *suppressions = GetSuppressionContext();
if (const char *module_name =
Symbolizer::GetOrInit()->GetModuleNameForPc(addr))
if (suppressions->Match(module_name, kSuppressionLeak, &s))
return s;
2015-08-28 15:33:40 +00:00
// Suppress by file or function name.
2017-04-10 11:32:00 +00:00
SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(addr);
for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
if (suppressions->Match(cur->info.function, kSuppressionLeak, &s) ||
suppressions->Match(cur->info.file, kSuppressionLeak, &s)) {
break;
}
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
frames->ClearAll();
return s;
2014-09-21 17:33:12 +00:00
}
static Suppression *GetSuppressionForStack(u32 stack_trace_id) {
2015-08-28 15:33:40 +00:00
StackTrace stack = StackDepotGet(stack_trace_id);
for (uptr i = 0; i < stack.size; i++) {
Suppression *s = GetSuppressionForAddr(
StackTrace::GetPreviousInstructionPc(stack.trace[i]));
2014-09-21 17:33:12 +00:00
if (s) return s;
}
2017-04-10 11:32:00 +00:00
return nullptr;
2014-09-21 17:33:12 +00:00
}
///// LeakReport implementation. /////
// A hard limit on the number of distinct leaks, to avoid quadratic complexity
2015-08-28 15:33:40 +00:00
// in LeakReport::AddLeakedChunk(). We don't expect to ever see this many leaks
// in real-world applications.
2014-09-21 17:33:12 +00:00
// FIXME: Get rid of this limit by changing the implementation of LeakReport to
// use a hash table.
const uptr kMaxLeaksConsidered = 5000;
2015-08-28 15:33:40 +00:00
void LeakReport::AddLeakedChunk(uptr chunk, u32 stack_trace_id,
uptr leaked_size, ChunkTag tag) {
2014-09-21 17:33:12 +00:00
CHECK(tag == kDirectlyLeaked || tag == kIndirectlyLeaked);
bool is_directly_leaked = (tag == kDirectlyLeaked);
2015-08-28 15:33:40 +00:00
uptr i;
for (i = 0; i < leaks_.size(); i++) {
2014-09-21 17:33:12 +00:00
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;
2015-08-28 15:33:40 +00:00
break;
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
}
if (i == leaks_.size()) {
if (leaks_.size() == kMaxLeaksConsidered) return;
Leak leak = { next_id_++, /* hit_count */ 1, leaked_size, stack_trace_id,
is_directly_leaked, /* is_suppressed */ false };
leaks_.push_back(leak);
}
if (flags()->report_objects) {
LeakedObject obj = {leaks_[i].id, chunk, leaked_size};
leaked_objects_.push_back(obj);
}
2014-09-21 17:33:12 +00:00
}
static bool LeakComparator(const Leak &leak1, const Leak &leak2) {
if (leak1.is_directly_leaked == leak2.is_directly_leaked)
return leak1.total_size > leak2.total_size;
else
return leak1.is_directly_leaked;
}
2015-08-28 15:33:40 +00:00
void LeakReport::ReportTopLeaks(uptr num_leaks_to_report) {
2014-09-21 17:33:12 +00:00
CHECK(leaks_.size() <= kMaxLeaksConsidered);
Printf("\n");
if (leaks_.size() == kMaxLeaksConsidered)
Printf("Too many leaks! Only the first %zu leaks encountered will be "
"reported.\n",
kMaxLeaksConsidered);
2015-08-28 15:33:40 +00:00
uptr unsuppressed_count = UnsuppressedLeakCount();
if (num_leaks_to_report > 0 && num_leaks_to_report < unsuppressed_count)
Printf("The %zu top leak(s):\n", num_leaks_to_report);
2014-09-21 17:33:12 +00:00
InternalSort(&leaks_, leaks_.size(), LeakComparator);
2015-08-28 15:33:40 +00:00
uptr leaks_reported = 0;
2014-09-21 17:33:12 +00:00
for (uptr i = 0; i < leaks_.size(); i++) {
if (leaks_[i].is_suppressed) continue;
2015-08-28 15:33:40 +00:00
PrintReportForLeak(i);
leaks_reported++;
if (leaks_reported == num_leaks_to_report) break;
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
if (leaks_reported < unsuppressed_count) {
uptr remaining = unsuppressed_count - leaks_reported;
2014-09-21 17:33:12 +00:00
Printf("Omitting %zu more leak(s).\n", remaining);
}
}
2015-08-28 15:33:40 +00:00
void LeakReport::PrintReportForLeak(uptr index) {
Decorator d;
Printf("%s", d.Leak());
Printf("%s leak of %zu byte(s) in %zu object(s) allocated from:\n",
leaks_[index].is_directly_leaked ? "Direct" : "Indirect",
leaks_[index].total_size, leaks_[index].hit_count);
Printf("%s", d.End());
PrintStackTraceById(leaks_[index].stack_trace_id);
if (flags()->report_objects) {
Printf("Objects leaked above:\n");
PrintLeakedObjectsForLeak(index);
Printf("\n");
}
}
void LeakReport::PrintLeakedObjectsForLeak(uptr index) {
u32 leak_id = leaks_[index].id;
for (uptr j = 0; j < leaked_objects_.size(); j++) {
if (leaked_objects_[j].leak_id == leak_id)
Printf("%p (%zu bytes)\n", leaked_objects_[j].addr,
leaked_objects_[j].size);
}
}
2014-09-21 17:33:12 +00:00
void LeakReport::PrintSummary() {
CHECK(leaks_.size() <= kMaxLeaksConsidered);
uptr bytes = 0, allocations = 0;
for (uptr i = 0; i < leaks_.size(); i++) {
if (leaks_[i].is_suppressed) continue;
bytes += leaks_[i].total_size;
allocations += leaks_[i].hit_count;
}
2017-04-10 11:32:00 +00:00
InternalScopedString summary(kMaxSummaryLength);
summary.append("%zu byte(s) leaked in %zu allocation(s).", bytes,
allocations);
2014-09-21 17:33:12 +00:00
ReportErrorSummary(summary.data());
}
2015-08-28 15:33:40 +00:00
void LeakReport::ApplySuppressions() {
2014-09-21 17:33:12 +00:00
for (uptr i = 0; i < leaks_.size(); i++) {
Suppression *s = GetSuppressionForStack(leaks_[i].stack_trace_id);
if (s) {
s->weight += leaks_[i].total_size;
2017-04-10 11:32:00 +00:00
atomic_store_relaxed(&s->hit_count, atomic_load_relaxed(&s->hit_count) +
leaks_[i].hit_count);
2014-09-21 17:33:12 +00:00
leaks_[i].is_suppressed = true;
}
}
}
2015-08-28 15:33:40 +00:00
uptr LeakReport::UnsuppressedLeakCount() {
uptr result = 0;
for (uptr i = 0; i < leaks_.size(); i++)
if (!leaks_[i].is_suppressed) result++;
return result;
}
2017-04-10 11:32:00 +00:00
} // namespace __lsan
#endif // CAN_SANITIZE_LEAKS
2014-09-21 17:33:12 +00:00
using namespace __lsan; // NOLINT
extern "C" {
SANITIZER_INTERFACE_ATTRIBUTE
void __lsan_ignore_object(const void *p) {
#if CAN_SANITIZE_LEAKS
if (!common_flags()->detect_leaks)
return;
// Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not
// locked.
BlockingMutexLock l(&global_mutex);
IgnoreObjectResult res = IgnoreObjectLocked(p);
2015-08-28 15:33:40 +00:00
if (res == kIgnoreObjectInvalid)
VReport(1, "__lsan_ignore_object(): no heap object found at %p", p);
if (res == kIgnoreObjectAlreadyIgnored)
VReport(1, "__lsan_ignore_object(): "
2014-09-21 17:33:12 +00:00
"heap object at %p is already being ignored\n", p);
2015-08-28 15:33:40 +00:00
if (res == kIgnoreObjectSuccess)
VReport(1, "__lsan_ignore_object(): ignoring heap object at %p\n", p);
2017-04-10 11:32:00 +00:00
#endif // CAN_SANITIZE_LEAKS
2015-08-28 15:33:40 +00:00
}
SANITIZER_INTERFACE_ATTRIBUTE
void __lsan_register_root_region(const void *begin, uptr size) {
#if CAN_SANITIZE_LEAKS
BlockingMutexLock l(&global_mutex);
CHECK(root_regions);
RootRegion region = {begin, size};
root_regions->push_back(region);
VReport(1, "Registered root region at %p of size %llu\n", begin, size);
2017-04-10 11:32:00 +00:00
#endif // CAN_SANITIZE_LEAKS
2015-08-28 15:33:40 +00:00
}
SANITIZER_INTERFACE_ATTRIBUTE
void __lsan_unregister_root_region(const void *begin, uptr size) {
#if CAN_SANITIZE_LEAKS
BlockingMutexLock l(&global_mutex);
CHECK(root_regions);
bool removed = false;
for (uptr i = 0; i < root_regions->size(); i++) {
RootRegion region = (*root_regions)[i];
if (region.begin == begin && region.size == size) {
removed = true;
uptr last_index = root_regions->size() - 1;
(*root_regions)[i] = (*root_regions)[last_index];
root_regions->pop_back();
VReport(1, "Unregistered root region at %p of size %llu\n", begin, size);
break;
}
}
if (!removed) {
Report(
"__lsan_unregister_root_region(): region at %p of size %llu has not "
"been registered.\n",
begin, size);
Die();
}
2017-04-10 11:32:00 +00:00
#endif // CAN_SANITIZE_LEAKS
2014-09-21 17:33:12 +00: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 && common_flags()->detect_leaks) {
Report("Unmatched call to __lsan_enable().\n");
Die();
}
__lsan::disable_counter--;
#endif
}
SANITIZER_INTERFACE_ATTRIBUTE
void __lsan_do_leak_check() {
#if CAN_SANITIZE_LEAKS
if (common_flags()->detect_leaks)
__lsan::DoLeakCheck();
2017-04-10 11:32:00 +00:00
#endif // CAN_SANITIZE_LEAKS
}
SANITIZER_INTERFACE_ATTRIBUTE
int __lsan_do_recoverable_leak_check() {
#if CAN_SANITIZE_LEAKS
if (common_flags()->detect_leaks)
return __lsan::DoRecoverableLeakCheck();
#endif // CAN_SANITIZE_LEAKS
return 0;
2014-09-21 17:33:12 +00:00
}
#if !SANITIZER_SUPPORTS_WEAK_HOOKS
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
int __lsan_is_turned_off() {
return 0;
}
#endif
2017-04-10 11:32:00 +00:00
} // extern "C"