Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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" |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_flag_parser.h" |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_placement_new.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_procmaps.h" |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_stackdepot.h" |
| 23 | #include "sanitizer_common/sanitizer_stacktrace.h" |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_suppressions.h" |
Sergey Matveev | 650c7d4 | 2013-09-03 13:31:03 +0000 | [diff] [blame] | 25 | #include "sanitizer_common/sanitizer_report_decorator.h" |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 26 | |
Sergey Matveev | ca74cff | 2013-06-21 15:14:57 +0000 | [diff] [blame] | 27 | #if CAN_SANITIZE_LEAKS |
Sergey Matveev | 21e024e | 2013-06-21 15:50:49 +0000 | [diff] [blame] | 28 | namespace __lsan { |
Sergey Matveev | 0bc8177 | 2013-05-21 15:35:34 +0000 | [diff] [blame] | 29 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 30 | // This mutex is used to prevent races between DoLeakCheck and IgnoreObject, and |
| 31 | // also to protect the global list of root regions. |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 32 | BlockingMutex global_mutex(LINKER_INITIALIZED); |
| 33 | |
Sergey Matveev | 200afbd | 2013-06-21 14:51:52 +0000 | [diff] [blame] | 34 | THREADLOCAL int disable_counter; |
Sergey Matveev | 21e024e | 2013-06-21 15:50:49 +0000 | [diff] [blame] | 35 | bool DisabledInThisThread() { return disable_counter > 0; } |
Sergey Matveev | 200afbd | 2013-06-21 14:51:52 +0000 | [diff] [blame] | 36 | |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 37 | Flags lsan_flags; |
| 38 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 39 | void Flags::SetDefaults() { |
| 40 | #define LSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue; |
| 41 | #include "lsan_flags.inc" |
| 42 | #undef LSAN_FLAG |
| 43 | } |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 44 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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 |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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 }; |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 66 | |
| 67 | void InitializeSuppressions() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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); |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 72 | if (&__lsan_default_suppressions) |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 73 | suppression_ctx->Parse(__lsan_default_suppressions()); |
| 74 | } |
| 75 | |
| 76 | static SuppressionContext *GetSuppressionContext() { |
| 77 | CHECK(suppression_ctx); |
| 78 | return suppression_ctx; |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 94 | void InitCommonLsan() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 95 | InitializeRootRegions(); |
Sergey Matveev | 137a9b8 | 2013-10-21 19:35:00 +0000 | [diff] [blame] | 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 | } |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 104 | class Decorator: public __sanitizer::SanitizerCommonDecorator { |
Sergey Matveev | 650c7d4 | 2013-09-03 13:31:03 +0000 | [diff] [blame] | 105 | public: |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 106 | Decorator() : SanitizerCommonDecorator() { } |
Sergey Matveev | 650c7d4 | 2013-09-03 13:31:03 +0000 | [diff] [blame] | 107 | const char *Error() { return Red(); } |
| 108 | const char *Leak() { return Blue(); } |
| 109 | const char *End() { return Default(); } |
| 110 | }; |
| 111 | |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 112 | static inline bool CanBeAHeapPointer(uptr p) { |
| 113 | // Since our heap is located in mmap-ed memory, we can assume a sensible lower |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 114 | // bound on heap addresses. |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 115 | const uptr kMinAddress = 4 * 4096; |
| 116 | if (p < kMinAddress) return false; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 117 | #if defined(__x86_64__) |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 118 | // Accept only canonical form user-space addresses. |
| 119 | return ((p >> 47) == 0); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 120 | #elif defined(__mips64) |
| 121 | return ((p >> 40) == 0); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 122 | #else |
| 123 | return true; |
| 124 | #endif |
| 125 | } |
| 126 | |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 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|. |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 129 | // There are two usage modes for this function: finding reachable chunks |
| 130 | // (|tag| = kReachable) and finding indirectly leaked chunks |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 131 | // (|tag| = kIndirectlyLeaked). In the second case, there's no flood fill, |
| 132 | // so |frontier| = 0. |
Alexey Samsonov | a64d435 | 2013-06-14 09:59:40 +0000 | [diff] [blame] | 133 | void ScanRangeForPointers(uptr begin, uptr end, |
Alexey Samsonov | dbeb48d | 2013-06-14 10:07:56 +0000 | [diff] [blame] | 134 | Frontier *frontier, |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 135 | const char *region_type, ChunkTag tag) { |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 136 | CHECK(tag == kReachable || tag == kIndirectlyLeaked); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 137 | const uptr alignment = flags()->pointer_alignment(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 138 | LOG_POINTERS("Scanning %s range %p-%p.\n", region_type, begin, end); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 139 | uptr pp = begin; |
| 140 | if (pp % alignment) |
| 141 | pp = pp + alignment - pp % alignment; |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 142 | for (; pp + sizeof(void *) <= end; pp += alignment) { // NOLINT |
Sergey Matveev | f93fa97 | 2013-07-18 14:06:07 +0000 | [diff] [blame] | 143 | void *p = *reinterpret_cast<void **>(pp); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 144 | if (!CanBeAHeapPointer(reinterpret_cast<uptr>(p))) continue; |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 145 | uptr chunk = PointsIntoChunk(p); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 146 | if (!chunk) continue; |
Sergey Matveev | e852e41 | 2013-10-15 16:00:11 +0000 | [diff] [blame] | 147 | // Pointers to self don't count. This matters when tag == kIndirectlyLeaked. |
| 148 | if (chunk == begin) continue; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 149 | LsanMetadata m(chunk); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 150 | if (m.tag() == kReachable || m.tag() == kIgnored) continue; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 161 | m.set_tag(tag); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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()); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 164 | if (frontier) |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 165 | frontier->push_back(chunk); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Sergey Matveev | c519335 | 2013-10-14 14:04:50 +0000 | [diff] [blame] | 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 | |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 174 | // Scans thread data (stacks and TLS) for heap pointers. |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 175 | static void ProcessThreads(SuspendedThreadsList const &suspended_threads, |
Alexey Samsonov | dbeb48d | 2013-06-14 10:07:56 +0000 | [diff] [blame] | 176 | Frontier *frontier) { |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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)); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 182 | LOG_THREADS("Processing thread %d.\n", os_id); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 190 | LOG_THREADS("Thread %d not found in registry.\n", os_id); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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 | |
Sergey Matveev | ebe3a36 | 2013-05-27 11:41:46 +0000 | [diff] [blame] | 202 | if (flags()->use_registers && have_registers) |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 203 | ScanRangeForPointers(registers_begin, registers_end, frontier, |
| 204 | "REGISTERS", kReachable); |
| 205 | |
Sergey Matveev | ebe3a36 | 2013-05-27 11:41:46 +0000 | [diff] [blame] | 206 | if (flags()->use_stacks) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 207 | LOG_THREADS("Stack at %p-%p (SP = %p).\n", stack_begin, stack_end, sp); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 212 | LOG_THREADS("WARNING: stack pointer not in stack range.\n"); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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); |
Sergey Matveev | c519335 | 2013-10-14 14:04:50 +0000 | [diff] [blame] | 219 | ForEachExtraStackRange(os_id, ForEachExtraStackRangeCb, frontier); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Sergey Matveev | ebe3a36 | 2013-05-27 11:41:46 +0000 | [diff] [blame] | 222 | if (flags()->use_tls) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 223 | LOG_THREADS("TLS at %p-%p.\n", tls_begin, tls_end); |
Sergey Matveev | f5a9ace | 2013-05-24 18:07:53 +0000 | [diff] [blame] | 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 | } |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | |
Alexey Samsonov | dbeb48d | 2013-06-14 10:07:56 +0000 | [diff] [blame] | 272 | static void FloodFillTag(Frontier *frontier, ChunkTag tag) { |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 273 | while (frontier->size()) { |
| 274 | uptr next_chunk = frontier->back(); |
| 275 | frontier->pop_back(); |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 276 | LsanMetadata m(next_chunk); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 277 | ScanRangeForPointers(next_chunk, next_chunk + m.requested_size(), frontier, |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 278 | "HEAP", tag); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 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); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 287 | if (m.allocated() && m.tag() != kReachable) { |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 288 | ScanRangeForPointers(chunk, chunk + m.requested_size(), |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 289 | /* frontier */ nullptr, "HEAP", kIndirectlyLeaked); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 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); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 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()); |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 302 | reinterpret_cast<Frontier *>(arg)->push_back(chunk); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 303 | } |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 306 | // Sets the appropriate tag on each chunk. |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 307 | static void ClassifyAllChunks(SuspendedThreadsList const &suspended_threads) { |
| 308 | // Holds the flood fill frontier. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 309 | Frontier frontier(1); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 310 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 311 | ForEachChunk(CollectIgnoredCb, &frontier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 312 | ProcessGlobalRegions(&frontier); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 313 | ProcessThreads(suspended_threads, &frontier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 314 | ProcessRootRegions(&frontier); |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 315 | FloodFillTag(&frontier, kReachable); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 316 | |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 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. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 320 | LOG_POINTERS("Processing platform-specific allocations.\n"); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 321 | CHECK_EQ(0, frontier.size()); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 322 | ProcessPlatformSpecificAllocations(&frontier); |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 323 | FloodFillTag(&frontier, kReachable); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 324 | |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 325 | // Iterate over leaked chunks and mark those that are reachable from other |
| 326 | // leaked chunks. |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 327 | LOG_POINTERS("Scanning leaked chunks.\n"); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 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); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static void PrintStackTraceById(u32 stack_trace_id) { |
| 341 | CHECK(stack_trace_id); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 342 | StackDepotGet(stack_trace_id).Print(); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 345 | // ForEachChunk callback. Aggregates information about unreachable chunks into |
| 346 | // a LeakReport. |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 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); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 352 | if (!m.allocated()) return; |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 353 | if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 354 | u32 resolution = flags()->resolution; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 355 | u32 stack_trace_id = 0; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 356 | if (resolution > 0) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 357 | StackTrace stack = StackDepotGet(m.stack_trace_id()); |
| 358 | stack.size = Min(stack.size, resolution); |
| 359 | stack_trace_id = StackDepotPut(stack); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 360 | } else { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 361 | stack_trace_id = m.stack_trace_id(); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 362 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 363 | leak_report->AddLeakedChunk(chunk, stack_trace_id, m.requested_size(), |
| 364 | m.tag()); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 368 | static void PrintMatchedSuppressions() { |
| 369 | InternalMmapVector<Suppression *> matched(1); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 370 | GetSuppressionContext()->GetMatched(&matched); |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 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 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 383 | struct CheckForLeaksParam { |
Sergey Matveev | c085fe8 | 2013-06-19 14:04:11 +0000 | [diff] [blame] | 384 | bool success; |
| 385 | LeakReport leak_report; |
Sergey Matveev | 969b529 | 2013-05-24 13:16:02 +0000 | [diff] [blame] | 386 | }; |
| 387 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 388 | static void CheckForLeaksCallback(const SuspendedThreadsList &suspended_threads, |
| 389 | void *arg) { |
| 390 | CheckForLeaksParam *param = reinterpret_cast<CheckForLeaksParam *>(arg); |
Sergey Matveev | c085fe8 | 2013-06-19 14:04:11 +0000 | [diff] [blame] | 391 | CHECK(param); |
| 392 | CHECK(!param->success); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 393 | ClassifyAllChunks(suspended_threads); |
Sergey Matveev | ac78d00 | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 394 | ForEachChunk(CollectLeaksCb, ¶m->leak_report); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 395 | // Clean up for subsequent leak checks. This assumes we did not overwrite any |
| 396 | // kIgnored tags. |
| 397 | ForEachChunk(ResetTagsCb, nullptr); |
Sergey Matveev | c085fe8 | 2013-06-19 14:04:11 +0000 | [diff] [blame] | 398 | param->success = true; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 401 | static bool CheckForLeaks() { |
Alexey Samsonov | 9fbfd96 | 2013-06-27 09:35:50 +0000 | [diff] [blame] | 402 | if (&__lsan_is_turned_off && __lsan_is_turned_off()) |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 403 | return false; |
| 404 | EnsureMainThreadIDIsCorrect(); |
| 405 | CheckForLeaksParam param; |
Sergey Matveev | c085fe8 | 2013-06-19 14:04:11 +0000 | [diff] [blame] | 406 | param.success = false; |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 407 | LockThreadRegistry(); |
| 408 | LockAllocator(); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 409 | DoStopTheWorld(CheckForLeaksCallback, ¶m); |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 410 | UnlockAllocator(); |
| 411 | UnlockThreadRegistry(); |
Sergey Matveev | c085fe8 | 2013-06-19 14:04:11 +0000 | [diff] [blame] | 412 | |
| 413 | if (!param.success) { |
Sergey Matveev | 969b529 | 2013-05-24 13:16:02 +0000 | [diff] [blame] | 414 | Report("LeakSanitizer has encountered a fatal error.\n"); |
| 415 | Die(); |
Sergey Matveev | c085fe8 | 2013-06-19 14:04:11 +0000 | [diff] [blame] | 416 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 417 | param.leak_report.ApplySuppressions(); |
| 418 | uptr unsuppressed_count = param.leak_report.UnsuppressedLeakCount(); |
| 419 | if (unsuppressed_count > 0) { |
Sergey Matveev | 650c7d4 | 2013-09-03 13:31:03 +0000 | [diff] [blame] | 420 | Decorator d; |
Sergey Matveev | 9b4ba94 | 2013-06-28 15:05:16 +0000 | [diff] [blame] | 421 | Printf("\n" |
| 422 | "=================================================================" |
Sergey Matveev | c085fe8 | 2013-06-19 14:04:11 +0000 | [diff] [blame] | 423 | "\n"); |
Sergey Matveev | 650c7d4 | 2013-09-03 13:31:03 +0000 | [diff] [blame] | 424 | Printf("%s", d.Error()); |
Sergey Matveev | c085fe8 | 2013-06-19 14:04:11 +0000 | [diff] [blame] | 425 | Report("ERROR: LeakSanitizer: detected memory leaks\n"); |
Sergey Matveev | 650c7d4 | 2013-09-03 13:31:03 +0000 | [diff] [blame] | 426 | Printf("%s", d.End()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 427 | param.leak_report.ReportTopLeaks(flags()->max_leaks); |
Sergey Matveev | 9b4ba94 | 2013-06-28 15:05:16 +0000 | [diff] [blame] | 428 | } |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 429 | if (common_flags()->print_suppressions) |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 430 | PrintMatchedSuppressions(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 431 | if (unsuppressed_count > 0) { |
Sergey Matveev | c085fe8 | 2013-06-19 14:04:11 +0000 | [diff] [blame] | 432 | param.leak_report.PrintSummary(); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 433 | return true; |
Sergey Matveev | 969b529 | 2013-05-24 13:16:02 +0000 | [diff] [blame] | 434 | } |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 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; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 460 | static Suppression *GetSuppressionForAddr(uptr addr) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 461 | Suppression *s = nullptr; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 462 | |
| 463 | // Suppress by module name. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 464 | SuppressionContext *suppressions = GetSuppressionContext(); |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 465 | if (const char *module_name = |
| 466 | Symbolizer::GetOrInit()->GetModuleNameForPc(addr)) |
| 467 | if (suppressions->Match(module_name, kSuppressionLeak, &s)) |
| 468 | return s; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 469 | |
| 470 | // Suppress by file or function name. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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 | } |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 477 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 478 | frames->ClearAll(); |
| 479 | return s; |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static Suppression *GetSuppressionForStack(u32 stack_trace_id) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 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])); |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 487 | if (s) return s; |
| 488 | } |
| 489 | return 0; |
| 490 | } |
| 491 | |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 492 | ///// LeakReport implementation. ///// |
| 493 | |
| 494 | // A hard limit on the number of distinct leaks, to avoid quadratic complexity |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 495 | // in LeakReport::AddLeakedChunk(). We don't expect to ever see this many leaks |
| 496 | // in real-world applications. |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 497 | // FIXME: Get rid of this limit by changing the implementation of LeakReport to |
| 498 | // use a hash table. |
Sergey Matveev | bac1f7b | 2013-07-12 12:31:22 +0000 | [diff] [blame] | 499 | const uptr kMaxLeaksConsidered = 5000; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 500 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 501 | void LeakReport::AddLeakedChunk(uptr chunk, u32 stack_trace_id, |
| 502 | uptr leaked_size, ChunkTag tag) { |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 503 | CHECK(tag == kDirectlyLeaked || tag == kIndirectlyLeaked); |
| 504 | bool is_directly_leaked = (tag == kDirectlyLeaked); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 505 | uptr i; |
| 506 | for (i = 0; i < leaks_.size(); i++) { |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 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; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 511 | break; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 512 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | } |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Sergey Matveev | 841b6f2 | 2013-07-22 11:18:32 +0000 | [diff] [blame] | 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; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 533 | void LeakReport::ReportTopLeaks(uptr num_leaks_to_report) { |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 534 | CHECK(leaks_.size() <= kMaxLeaksConsidered); |
| 535 | Printf("\n"); |
| 536 | if (leaks_.size() == kMaxLeaksConsidered) |
Sergey Matveev | ef89d6b | 2013-06-21 15:10:20 +0000 | [diff] [blame] | 537 | Printf("Too many leaks! Only the first %zu leaks encountered will be " |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 538 | "reported.\n", |
| 539 | kMaxLeaksConsidered); |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 540 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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); |
Sergey Matveev | 841b6f2 | 2013-07-22 11:18:32 +0000 | [diff] [blame] | 544 | InternalSort(&leaks_, leaks_.size(), LeakComparator); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 545 | uptr leaks_reported = 0; |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 546 | for (uptr i = 0; i < leaks_.size(); i++) { |
| 547 | if (leaks_[i].is_suppressed) continue; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 548 | PrintReportForLeak(i); |
| 549 | leaks_reported++; |
| 550 | if (leaks_reported == num_leaks_to_report) break; |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 551 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 552 | if (leaks_reported < unsuppressed_count) { |
| 553 | uptr remaining = unsuppressed_count - leaks_reported; |
Sergey Matveev | ef89d6b | 2013-06-21 15:10:20 +0000 | [diff] [blame] | 554 | Printf("Omitting %zu more leak(s).\n", remaining); |
Sergey Matveev | ab0f744 | 2013-05-20 11:06:50 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
Sergey Matveev | 0bc8177 | 2013-05-21 15:35:34 +0000 | [diff] [blame] | 557 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | |
Sergey Matveev | 6c3634b | 2013-05-24 14:49:13 +0000 | [diff] [blame] | 584 | void LeakReport::PrintSummary() { |
| 585 | CHECK(leaks_.size() <= kMaxLeaksConsidered); |
Sergey Matveev | 8e66cf5 | 2013-05-24 15:36:30 +0000 | [diff] [blame] | 586 | uptr bytes = 0, allocations = 0; |
Sergey Matveev | 6c3634b | 2013-05-24 14:49:13 +0000 | [diff] [blame] | 587 | for (uptr i = 0; i < leaks_.size(); i++) { |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 588 | if (leaks_[i].is_suppressed) continue; |
Sergey Matveev | 8e66cf5 | 2013-05-24 15:36:30 +0000 | [diff] [blame] | 589 | bytes += leaks_[i].total_size; |
| 590 | allocations += leaks_[i].hit_count; |
Sergey Matveev | 6c3634b | 2013-05-24 14:49:13 +0000 | [diff] [blame] | 591 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 592 | InternalScopedString summary(kMaxSummaryLength); |
| 593 | summary.append("%zu byte(s) leaked in %zu allocation(s).", bytes, |
| 594 | allocations); |
Alexey Samsonov | 2fb0872 | 2013-11-01 17:02:14 +0000 | [diff] [blame] | 595 | ReportErrorSummary(summary.data()); |
Sergey Matveev | 6c3634b | 2013-05-24 14:49:13 +0000 | [diff] [blame] | 596 | } |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 597 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 598 | void LeakReport::ApplySuppressions() { |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 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; |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
Sergey Matveev | b33cfeb | 2013-06-28 14:38:31 +0000 | [diff] [blame] | 607 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | |
Sergey Matveev | ca74cff | 2013-06-21 15:14:57 +0000 | [diff] [blame] | 616 | } // namespace __lsan |
Sergey Matveev | 21e024e | 2013-06-21 15:50:49 +0000 | [diff] [blame] | 617 | #endif // CAN_SANITIZE_LEAKS |
Sergey Matveev | ca74cff | 2013-06-21 15:14:57 +0000 | [diff] [blame] | 618 | |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 619 | using namespace __lsan; // NOLINT |
| 620 | |
| 621 | extern "C" { |
Sergey Matveev | 46ed75f | 2013-06-06 18:40:55 +0000 | [diff] [blame] | 622 | SANITIZER_INTERFACE_ATTRIBUTE |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 623 | void __lsan_ignore_object(const void *p) { |
Sergey Matveev | 9b618a7 | 2013-06-20 13:39:42 +0000 | [diff] [blame] | 624 | #if CAN_SANITIZE_LEAKS |
Sergey Matveev | 137a9b8 | 2013-10-21 19:35:00 +0000 | [diff] [blame] | 625 | if (!common_flags()->detect_leaks) |
| 626 | return; |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 627 | // Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not |
| 628 | // locked. |
| 629 | BlockingMutexLock l(&global_mutex); |
| 630 | IgnoreObjectResult res = IgnoreObjectLocked(p); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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(): " |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 635 | "heap object at %p is already being ignored\n", p); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | } |
Sergey Matveev | 9b618a7 | 2013-06-20 13:39:42 +0000 | [diff] [blame] | 676 | #endif // CAN_SANITIZE_LEAKS |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 677 | } |
Sergey Matveev | 200afbd | 2013-06-21 14:51:52 +0000 | [diff] [blame] | 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 |
Sergey Matveev | 137a9b8 | 2013-10-21 19:35:00 +0000 | [diff] [blame] | 689 | if (!__lsan::disable_counter && common_flags()->detect_leaks) { |
Sergey Matveev | 200afbd | 2013-06-21 14:51:52 +0000 | [diff] [blame] | 690 | Report("Unmatched call to __lsan_enable().\n"); |
| 691 | Die(); |
| 692 | } |
| 693 | __lsan::disable_counter--; |
| 694 | #endif |
| 695 | } |
Alexey Samsonov | 9fbfd96 | 2013-06-27 09:35:50 +0000 | [diff] [blame] | 696 | |
Sergey Matveev | f93fa97 | 2013-07-18 14:06:07 +0000 | [diff] [blame] | 697 | SANITIZER_INTERFACE_ATTRIBUTE |
| 698 | void __lsan_do_leak_check() { |
| 699 | #if CAN_SANITIZE_LEAKS |
Sergey Matveev | 62519eb | 2013-07-22 12:38:17 +0000 | [diff] [blame] | 700 | if (common_flags()->detect_leaks) |
| 701 | __lsan::DoLeakCheck(); |
| 702 | #endif // CAN_SANITIZE_LEAKS |
Sergey Matveev | f93fa97 | 2013-07-18 14:06:07 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 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 | |
Alexey Samsonov | 9fbfd96 | 2013-06-27 09:35:50 +0000 | [diff] [blame] | 714 | #if !SANITIZER_SUPPORTS_WEAK_HOOKS |
Timur Iskhodzhanov | 3c80c6c | 2013-08-13 11:42:45 +0000 | [diff] [blame] | 715 | SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE |
Alexey Samsonov | 9fbfd96 | 2013-06-27 09:35:50 +0000 | [diff] [blame] | 716 | int __lsan_is_turned_off() { |
| 717 | return 0; |
| 718 | } |
| 719 | #endif |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 720 | } // extern "C" |