lsan_common.h revision 200afbd8ba4904241c1ebcef4fa79d739ca01f73
1//=-- lsan_common.h -------------------------------------------------------===// 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// Private LSan header. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LSAN_COMMON_H 16#define LSAN_COMMON_H 17 18#include "sanitizer_common/sanitizer_common.h" 19#include "sanitizer_common/sanitizer_internal_defs.h" 20#include "sanitizer_common/sanitizer_platform.h" 21#include "sanitizer_common/sanitizer_symbolizer.h" 22 23#if SANITIZER_LINUX && defined(__x86_64__) 24#define CAN_SANITIZE_LEAKS 1 25#else 26#define CAN_SANITIZE_LEAKS 0 27#endif 28 29namespace __lsan { 30 31// Chunk tags. 32enum ChunkTag { 33 kDirectlyLeaked = 0, // default 34 kIndirectlyLeaked = 1, 35 kReachable = 2, 36 kIgnored = 3 37}; 38 39struct Flags { 40 uptr pointer_alignment() const { 41 return use_unaligned ? 1 : sizeof(uptr); 42 } 43 44 // Print addresses of leaked objects after main leak report. 45 bool report_objects; 46 // Aggregate two objects into one leak if this many stack frames match. If 47 // zero, the entire stack trace must match. 48 int resolution; 49 // The number of leaks reported. 50 int max_leaks; 51 // If nonzero kill the process with this exit code upon finding leaks. 52 int exitcode; 53 54 // Flags controlling the root set of reachable memory. 55 // Global variables (.data and .bss). 56 bool use_globals; 57 // Thread stacks. 58 bool use_stacks; 59 // Thread registers. 60 bool use_registers; 61 // TLS and thread-specific storage. 62 bool use_tls; 63 64 // Consider unaligned pointers valid. 65 bool use_unaligned; 66 67 // User-visible verbosity. 68 int verbosity; 69 70 // Debug logging. 71 bool log_pointers; 72 bool log_threads; 73}; 74 75extern Flags lsan_flags; 76inline Flags *flags() { return &lsan_flags; } 77 78struct Leak { 79 uptr hit_count; 80 uptr total_size; 81 u32 stack_trace_id; 82 bool is_directly_leaked; 83}; 84 85// Aggregates leaks by stack trace prefix. 86class LeakReport { 87 public: 88 LeakReport() : leaks_(1) {} 89 void Add(u32 stack_trace_id, uptr leaked_size, ChunkTag tag); 90 void PrintLargest(uptr max_leaks); 91 void PrintSummary(); 92 bool IsEmpty() { return leaks_.size() == 0; } 93 private: 94 InternalMmapVector<Leak> leaks_; 95}; 96 97typedef InternalMmapVector<uptr> Frontier; 98 99// Platform-specific functions. 100void InitializePlatformSpecificModules(); 101void ProcessGlobalRegions(Frontier *frontier); 102void ProcessPlatformSpecificAllocations(Frontier *frontier); 103 104void ScanRangeForPointers(uptr begin, uptr end, 105 Frontier *frontier, 106 const char *region_type, ChunkTag tag); 107 108// Callables for iterating over chunks. Those classes are used as template 109// parameters in ForEachChunk, so we must expose them here to allow for explicit 110// template instantiation. 111 112// Identifies unreachable chunks which must be treated as reachable. Marks them 113// as reachable and adds them to the frontier. 114class ProcessPlatformSpecificAllocationsCb { 115 public: 116 explicit ProcessPlatformSpecificAllocationsCb( 117 Frontier *frontier) 118 : frontier_(frontier) {} 119 void operator()(void *p) const; 120 private: 121 Frontier *frontier_; 122}; 123 124// Prints addresses of unreachable chunks. 125class PrintLeakedCb { 126 public: 127 void operator()(void *p) const; 128}; 129 130// Aggregates unreachable chunks into a LeakReport. 131class CollectLeaksCb { 132 public: 133 explicit CollectLeaksCb(LeakReport *leak_report) 134 : leak_report_(leak_report) {} 135 void operator()(void *p) const; 136 private: 137 LeakReport *leak_report_; 138}; 139 140// Scans each leaked chunk for pointers to other leaked chunks, and marks each 141// of them as indirectly leaked. 142class MarkIndirectlyLeakedCb { 143 public: 144 void operator()(void *p) const; 145}; 146 147// Finds all chunk marked as kIgnored and adds their addresses to frontier. 148class CollectIgnoredCb { 149 public: 150 explicit CollectIgnoredCb(Frontier *frontier) 151 : frontier_(frontier) {} 152 void operator()(void *p) const; 153 private: 154 Frontier *frontier_; 155}; 156 157enum IgnoreObjectResult { 158 kIgnoreObjectSuccess, 159 kIgnoreObjectAlreadyIgnored, 160 kIgnoreObjectInvalid 161}; 162 163// Functions called from the parent tool. 164void InitCommonLsan(); 165void DoLeakCheck(); 166bool DisabledInThisThread(); 167 168// The following must be implemented in the parent tool. 169 170template<typename Callable> void ForEachChunk(Callable const &callback); 171// The address range occupied by the global allocator object. 172void GetAllocatorGlobalRange(uptr *begin, uptr *end); 173// Wrappers for allocator's ForceLock()/ForceUnlock(). 174void LockAllocator(); 175void UnlockAllocator(); 176// Wrappers for ThreadRegistry access. 177void LockThreadRegistry(); 178void UnlockThreadRegistry(); 179bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end, 180 uptr *tls_begin, uptr *tls_end, 181 uptr *cache_begin, uptr *cache_end); 182// If p points into a chunk that has been allocated to the user, return its 183// user-visible address. Otherwise, return 0. 184void *PointsIntoChunk(void *p); 185// Return address of user-visible chunk contained in this allocator chunk. 186void *GetUserBegin(void *p); 187// Helper for __lsan_ignore_object(). 188IgnoreObjectResult IgnoreObjectLocked(const void *p); 189// Wrapper for chunk metadata operations. 190class LsanMetadata { 191 public: 192 // Constructor accepts pointer to user-visible chunk. 193 explicit LsanMetadata(void *chunk); 194 bool allocated() const; 195 ChunkTag tag() const; 196 void set_tag(ChunkTag value); 197 uptr requested_size() const; 198 u32 stack_trace_id() const; 199 private: 200 void *metadata_; 201}; 202 203} // namespace __lsan 204 205#endif // LSAN_COMMON_H 206