Bug Summary

File:projects/compiler-rt/lib/lsan/lsan_common.cc
Warning:line 620, column 3
Called C++ object pointer is null

Annotated Source Code

1//=-- lsan_common.cc ------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of LeakSanitizer.
11// Implementation of common leak checking functionality.
12//
13//===----------------------------------------------------------------------===//
14
15#include "lsan_common.h"
16
17#include "sanitizer_common/sanitizer_common.h"
18#include "sanitizer_common/sanitizer_flags.h"
19#include "sanitizer_common/sanitizer_flag_parser.h"
20#include "sanitizer_common/sanitizer_placement_new.h"
21#include "sanitizer_common/sanitizer_procmaps.h"
22#include "sanitizer_common/sanitizer_stackdepot.h"
23#include "sanitizer_common/sanitizer_stacktrace.h"
24#include "sanitizer_common/sanitizer_suppressions.h"
25#include "sanitizer_common/sanitizer_report_decorator.h"
26#include "sanitizer_common/sanitizer_tls_get_addr.h"
27
28#if CAN_SANITIZE_LEAKS1
29namespace __lsan {
30
31// This mutex is used to prevent races between DoLeakCheck and IgnoreObject, and
32// also to protect the global list of root regions.
33BlockingMutex global_mutex(LINKER_INITIALIZED);
34
35Flags lsan_flags;
36
37void DisableCounterUnderflow() {
38 if (common_flags()->detect_leaks) {
39 Report("Unmatched call to __lsan_enable().\n");
40 Die();
41 }
42}
43
44void Flags::SetDefaults() {
45#define LSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
46#include "lsan_flags.inc"
47#undef LSAN_FLAG
48}
49
50void RegisterLsanFlags(FlagParser *parser, Flags *f) {
51#define LSAN_FLAG(Type, Name, DefaultValue, Description) \
52 RegisterFlag(parser, #Name, Description, &f->Name);
53#include "lsan_flags.inc"
54#undef LSAN_FLAG
55}
56
57#define LOG_POINTERS(...)do { if (flags()->log_pointers) Report(...); } while (0); \
58 do { \
59 if (flags()->log_pointers) Report(__VA_ARGS__); \
60 } while (0);
61
62#define LOG_THREADS(...)do { if (flags()->log_threads) Report(...); } while (0); \
63 do { \
64 if (flags()->log_threads) Report(__VA_ARGS__); \
65 } while (0);
66
67ALIGNED(64)__attribute__((aligned(64))) static char suppression_placeholder[sizeof(SuppressionContext)];
68static SuppressionContext *suppression_ctx = nullptr;
69static const char kSuppressionLeak[] = "leak";
70static const char *kSuppressionTypes[] = { kSuppressionLeak };
71static const char kStdSuppressions[] =
72#if SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT0
73 // For more details refer to the SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
74 // definition.
75 "leak:*pthread_exit*\n"
76#endif // SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT
77#if SANITIZER_MAC0
78 // For Darwin and os_log/os_trace: https://reviews.llvm.org/D35173
79 "leak:*_os_trace*\n"
80#endif
81 // TLS leak in some glibc versions, described in
82 // https://sourceware.org/bugzilla/show_bug.cgi?id=12650.
83 "leak:*tls_get_addr*\n";
84
85void InitializeSuppressions() {
86 CHECK_EQ(nullptr, suppression_ctx)do { __sanitizer::u64 v1 = (__sanitizer::u64)((nullptr)); __sanitizer
::u64 v2 = (__sanitizer::u64)((suppression_ctx)); if (__builtin_expect
(!!(!(v1 == v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 86, "(" "(nullptr)" ") " "==" " (" "(suppression_ctx)" ")",
v1, v2); } while (false)
;
87 suppression_ctx = new (suppression_placeholder) // NOLINT
88 SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes)(sizeof(kSuppressionTypes)/sizeof((kSuppressionTypes)[0])));
89 suppression_ctx->ParseFromFile(flags()->suppressions);
90 if (&__lsan_default_suppressions)
91 suppression_ctx->Parse(__lsan_default_suppressions());
92 suppression_ctx->Parse(kStdSuppressions);
93}
94
95static SuppressionContext *GetSuppressionContext() {
96 CHECK(suppression_ctx)do { __sanitizer::u64 v1 = (__sanitizer::u64)((suppression_ctx
)); __sanitizer::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect
(!!(!(v1 != v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 96, "(" "(suppression_ctx)" ") " "!=" " (" "0" ")", v1, v2)
; } while (false)
;
97 return suppression_ctx;
98}
99
100static InternalMmapVector<RootRegion> *root_regions;
101
102InternalMmapVector<RootRegion> const *GetRootRegions() { return root_regions; }
103
104void InitializeRootRegions() {
105 CHECK(!root_regions)do { __sanitizer::u64 v1 = (__sanitizer::u64)((!root_regions)
); __sanitizer::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect
(!!(!(v1 != v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 105, "(" "(!root_regions)" ") " "!=" " (" "0" ")", v1, v2);
} while (false)
;
106 ALIGNED(64)__attribute__((aligned(64))) static char placeholder[sizeof(InternalMmapVector<RootRegion>)];
107 root_regions = new(placeholder) InternalMmapVector<RootRegion>(1);
108}
109
110const char *MaybeCallLsanDefaultOptions() {
111 return (&__lsan_default_options) ? __lsan_default_options() : "";
112}
113
114void InitCommonLsan() {
115 InitializeRootRegions();
116 if (common_flags()->detect_leaks) {
117 // Initialization which can fail or print warnings should only be done if
118 // LSan is actually enabled.
119 InitializeSuppressions();
120 InitializePlatformSpecificModules();
121 }
122}
123
124class Decorator: public __sanitizer::SanitizerCommonDecorator {
125 public:
126 Decorator() : SanitizerCommonDecorator() { }
127 const char *Error() { return Red(); }
128 const char *Leak() { return Blue(); }
129};
130
131static inline bool CanBeAHeapPointer(uptr p) {
132 // Since our heap is located in mmap-ed memory, we can assume a sensible lower
133 // bound on heap addresses.
134 const uptr kMinAddress = 4 * 4096;
135 if (p < kMinAddress) return false;
136#if defined(__x86_64__1)
137 // Accept only canonical form user-space addresses.
138 return ((p >> 47) == 0);
139#elif defined(__mips64)
140 return ((p >> 40) == 0);
141#elif defined(__aarch64__)
142 unsigned runtimeVMA =
143 (MostSignificantSetBitIndex(GET_CURRENT_FRAME()(__sanitizer::uptr) __builtin_frame_address(0)) + 1);
144 return ((p >> runtimeVMA) == 0);
145#else
146 return true;
147#endif
148}
149
150// Scans the memory range, looking for byte patterns that point into allocator
151// chunks. Marks those chunks with |tag| and adds them to |frontier|.
152// There are two usage modes for this function: finding reachable chunks
153// (|tag| = kReachable) and finding indirectly leaked chunks
154// (|tag| = kIndirectlyLeaked). In the second case, there's no flood fill,
155// so |frontier| = 0.
156void ScanRangeForPointers(uptr begin, uptr end,
157 Frontier *frontier,
158 const char *region_type, ChunkTag tag) {
159 CHECK(tag == kReachable || tag == kIndirectlyLeaked)do { __sanitizer::u64 v1 = (__sanitizer::u64)((tag == kReachable
|| tag == kIndirectlyLeaked)); __sanitizer::u64 v2 = (__sanitizer
::u64)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)) __sanitizer
::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 159, "(" "(tag == kReachable || tag == kIndirectlyLeaked)" ") "
"!=" " (" "0" ")", v1, v2); } while (false)
;
160 const uptr alignment = flags()->pointer_alignment();
161 LOG_POINTERS("Scanning %s range %p-%p.\n", region_type, begin, end)do { if (flags()->log_pointers) Report("Scanning %s range %p-%p.\n"
, region_type, begin, end); } while (0);
;
162 uptr pp = begin;
163 if (pp % alignment)
164 pp = pp + alignment - pp % alignment;
165 for (; pp + sizeof(void *) <= end; pp += alignment) { // NOLINT
166 void *p = *reinterpret_cast<void **>(pp);
167 if (!CanBeAHeapPointer(reinterpret_cast<uptr>(p))) continue;
168 uptr chunk = PointsIntoChunk(p);
169 if (!chunk) continue;
170 // Pointers to self don't count. This matters when tag == kIndirectlyLeaked.
171 if (chunk == begin) continue;
172 LsanMetadata m(chunk);
173 if (m.tag() == kReachable || m.tag() == kIgnored) continue;
174
175 // Do this check relatively late so we can log only the interesting cases.
176 if (!flags()->use_poisoned && WordIsPoisoned(pp)) {
177 LOG_POINTERS(do { if (flags()->log_pointers) Report("%p is poisoned: ignoring %p pointing into chunk %p-%p of size "
"%zu.\n", pp, p, chunk, chunk + m.requested_size(), m.requested_size
()); } while (0);
178 "%p is poisoned: ignoring %p pointing into chunk %p-%p of size "do { if (flags()->log_pointers) Report("%p is poisoned: ignoring %p pointing into chunk %p-%p of size "
"%zu.\n", pp, p, chunk, chunk + m.requested_size(), m.requested_size
()); } while (0);
179 "%zu.\n",do { if (flags()->log_pointers) Report("%p is poisoned: ignoring %p pointing into chunk %p-%p of size "
"%zu.\n", pp, p, chunk, chunk + m.requested_size(), m.requested_size
()); } while (0);
180 pp, p, chunk, chunk + m.requested_size(), m.requested_size())do { if (flags()->log_pointers) Report("%p is poisoned: ignoring %p pointing into chunk %p-%p of size "
"%zu.\n", pp, p, chunk, chunk + m.requested_size(), m.requested_size
()); } while (0);
;
181 continue;
182 }
183
184 m.set_tag(tag);
185 LOG_POINTERS("%p: found %p pointing into chunk %p-%p of size %zu.\n", pp, p,do { if (flags()->log_pointers) Report("%p: found %p pointing into chunk %p-%p of size %zu.\n"
, pp, p, chunk, chunk + m.requested_size(), m.requested_size(
)); } while (0);
186 chunk, chunk + m.requested_size(), m.requested_size())do { if (flags()->log_pointers) Report("%p: found %p pointing into chunk %p-%p of size %zu.\n"
, pp, p, chunk, chunk + m.requested_size(), m.requested_size(
)); } while (0);
;
187 if (frontier)
188 frontier->push_back(chunk);
189 }
190}
191
192// Scans a global range for pointers
193void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier) {
194 uptr allocator_begin = 0, allocator_end = 0;
195 GetAllocatorGlobalRange(&allocator_begin, &allocator_end);
196 if (begin <= allocator_begin && allocator_begin < end) {
197 CHECK_LE(allocator_begin, allocator_end)do { __sanitizer::u64 v1 = (__sanitizer::u64)((allocator_begin
)); __sanitizer::u64 v2 = (__sanitizer::u64)((allocator_end))
; if (__builtin_expect(!!(!(v1 <= v2)), 0)) __sanitizer::CheckFailed
("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 197, "(" "(allocator_begin)" ") " "<=" " (" "(allocator_end)"
")", v1, v2); } while (false)
;
198 CHECK_LE(allocator_end, end)do { __sanitizer::u64 v1 = (__sanitizer::u64)((allocator_end)
); __sanitizer::u64 v2 = (__sanitizer::u64)((end)); if (__builtin_expect
(!!(!(v1 <= v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 198, "(" "(allocator_end)" ") " "<=" " (" "(end)" ")", v1
, v2); } while (false)
;
199 if (begin < allocator_begin)
200 ScanRangeForPointers(begin, allocator_begin, frontier, "GLOBAL",
201 kReachable);
202 if (allocator_end < end)
203 ScanRangeForPointers(allocator_end, end, frontier, "GLOBAL", kReachable);
204 } else {
205 ScanRangeForPointers(begin, end, frontier, "GLOBAL", kReachable);
206 }
207}
208
209void ForEachExtraStackRangeCb(uptr begin, uptr end, void* arg) {
210 Frontier *frontier = reinterpret_cast<Frontier *>(arg);
211 ScanRangeForPointers(begin, end, frontier, "FAKE STACK", kReachable);
212}
213
214// Scans thread data (stacks and TLS) for heap pointers.
215static void ProcessThreads(SuspendedThreadsList const &suspended_threads,
216 Frontier *frontier) {
217 InternalScopedBuffer<uptr> registers(suspended_threads.RegisterCount());
218 uptr registers_begin = reinterpret_cast<uptr>(registers.data());
219 uptr registers_end = registers_begin + registers.size();
220 for (uptr i = 0; i < suspended_threads.ThreadCount(); i++) {
221 tid_t os_id = static_cast<tid_t>(suspended_threads.GetThreadID(i));
222 LOG_THREADS("Processing thread %d.\n", os_id)do { if (flags()->log_threads) Report("Processing thread %d.\n"
, os_id); } while (0);
;
223 uptr stack_begin, stack_end, tls_begin, tls_end, cache_begin, cache_end;
224 DTLS *dtls;
225 bool thread_found = GetThreadRangesLocked(os_id, &stack_begin, &stack_end,
226 &tls_begin, &tls_end,
227 &cache_begin, &cache_end, &dtls);
228 if (!thread_found) {
229 // If a thread can't be found in the thread registry, it's probably in the
230 // process of destruction. Log this event and move on.
231 LOG_THREADS("Thread %d not found in registry.\n", os_id)do { if (flags()->log_threads) Report("Thread %d not found in registry.\n"
, os_id); } while (0);
;
232 continue;
233 }
234 uptr sp;
235 PtraceRegistersStatus have_registers =
236 suspended_threads.GetRegistersAndSP(i, registers.data(), &sp);
237 if (have_registers != REGISTERS_AVAILABLE) {
238 Report("Unable to get registers from thread %d.\n", os_id);
239 // If unable to get SP, consider the entire stack to be reachable unless
240 // GetRegistersAndSP failed with ESRCH.
241 if (have_registers == REGISTERS_UNAVAILABLE_FATAL) continue;
242 sp = stack_begin;
243 }
244
245 if (flags()->use_registers && have_registers)
246 ScanRangeForPointers(registers_begin, registers_end, frontier,
247 "REGISTERS", kReachable);
248
249 if (flags()->use_stacks) {
250 LOG_THREADS("Stack at %p-%p (SP = %p).\n", stack_begin, stack_end, sp)do { if (flags()->log_threads) Report("Stack at %p-%p (SP = %p).\n"
, stack_begin, stack_end, sp); } while (0);
;
251 if (sp < stack_begin || sp >= stack_end) {
252 // SP is outside the recorded stack range (e.g. the thread is running a
253 // signal handler on alternate stack, or swapcontext was used).
254 // Again, consider the entire stack range to be reachable.
255 LOG_THREADS("WARNING: stack pointer not in stack range.\n")do { if (flags()->log_threads) Report("WARNING: stack pointer not in stack range.\n"
); } while (0);
;
256 uptr page_size = GetPageSizeCached();
257 int skipped = 0;
258 while (stack_begin < stack_end &&
259 !IsAccessibleMemoryRange(stack_begin, 1)) {
260 skipped++;
261 stack_begin += page_size;
262 }
263 LOG_THREADS("Skipped %d guard page(s) to obtain stack %p-%p.\n",do { if (flags()->log_threads) Report("Skipped %d guard page(s) to obtain stack %p-%p.\n"
, skipped, stack_begin, stack_end); } while (0);
264 skipped, stack_begin, stack_end)do { if (flags()->log_threads) Report("Skipped %d guard page(s) to obtain stack %p-%p.\n"
, skipped, stack_begin, stack_end); } while (0);
;
265 } else {
266 // Shrink the stack range to ignore out-of-scope values.
267 stack_begin = sp;
268 }
269 ScanRangeForPointers(stack_begin, stack_end, frontier, "STACK",
270 kReachable);
271 ForEachExtraStackRange(os_id, ForEachExtraStackRangeCb, frontier);
272 }
273
274 if (flags()->use_tls) {
275 if (tls_begin) {
276 LOG_THREADS("TLS at %p-%p.\n", tls_begin, tls_end)do { if (flags()->log_threads) Report("TLS at %p-%p.\n", tls_begin
, tls_end); } while (0);
;
277 // If the tls and cache ranges don't overlap, scan full tls range,
278 // otherwise, only scan the non-overlapping portions
279 if (cache_begin == cache_end || tls_end < cache_begin ||
280 tls_begin > cache_end) {
281 ScanRangeForPointers(tls_begin, tls_end, frontier, "TLS", kReachable);
282 } else {
283 if (tls_begin < cache_begin)
284 ScanRangeForPointers(tls_begin, cache_begin, frontier, "TLS",
285 kReachable);
286 if (tls_end > cache_end)
287 ScanRangeForPointers(cache_end, tls_end, frontier, "TLS",
288 kReachable);
289 }
290 }
291 if (dtls && !DTLSInDestruction(dtls)) {
292 for (uptr j = 0; j < dtls->dtv_size; ++j) {
293 uptr dtls_beg = dtls->dtv[j].beg;
294 uptr dtls_end = dtls_beg + dtls->dtv[j].size;
295 if (dtls_beg < dtls_end) {
296 LOG_THREADS("DTLS %zu at %p-%p.\n", j, dtls_beg, dtls_end)do { if (flags()->log_threads) Report("DTLS %zu at %p-%p.\n"
, j, dtls_beg, dtls_end); } while (0);
;
297 ScanRangeForPointers(dtls_beg, dtls_end, frontier, "DTLS",
298 kReachable);
299 }
300 }
301 } else {
302 // We are handling a thread with DTLS under destruction. Log about
303 // this and continue.
304 LOG_THREADS("Thread %d has DTLS under destruction.\n", os_id)do { if (flags()->log_threads) Report("Thread %d has DTLS under destruction.\n"
, os_id); } while (0);
;
305 }
306 }
307 }
308}
309
310void ScanRootRegion(Frontier *frontier, const RootRegion &root_region,
311 uptr region_begin, uptr region_end, bool is_readable) {
312 uptr intersection_begin = Max(root_region.begin, region_begin);
313 uptr intersection_end = Min(region_end, root_region.begin + root_region.size);
314 if (intersection_begin >= intersection_end) return;
315 LOG_POINTERS("Root region %p-%p intersects with mapped region %p-%p (%s)\n",do { if (flags()->log_pointers) Report("Root region %p-%p intersects with mapped region %p-%p (%s)\n"
, root_region.begin, root_region.begin + root_region.size, region_begin
, region_end, is_readable ? "readable" : "unreadable"); } while
(0);
316 root_region.begin, root_region.begin + root_region.size,do { if (flags()->log_pointers) Report("Root region %p-%p intersects with mapped region %p-%p (%s)\n"
, root_region.begin, root_region.begin + root_region.size, region_begin
, region_end, is_readable ? "readable" : "unreadable"); } while
(0);
317 region_begin, region_end,do { if (flags()->log_pointers) Report("Root region %p-%p intersects with mapped region %p-%p (%s)\n"
, root_region.begin, root_region.begin + root_region.size, region_begin
, region_end, is_readable ? "readable" : "unreadable"); } while
(0);
318 is_readable ? "readable" : "unreadable")do { if (flags()->log_pointers) Report("Root region %p-%p intersects with mapped region %p-%p (%s)\n"
, root_region.begin, root_region.begin + root_region.size, region_begin
, region_end, is_readable ? "readable" : "unreadable"); } while
(0);
;
319 if (is_readable)
320 ScanRangeForPointers(intersection_begin, intersection_end, frontier, "ROOT",
321 kReachable);
322}
323
324static void ProcessRootRegion(Frontier *frontier,
325 const RootRegion &root_region) {
326 MemoryMappingLayout proc_maps(/*cache_enabled*/ true);
327 MemoryMappedSegment segment;
328 while (proc_maps.Next(&segment)) {
329 ScanRootRegion(frontier, root_region, segment.start, segment.end,
330 segment.IsReadable());
331 }
332}
333
334// Scans root regions for heap pointers.
335static void ProcessRootRegions(Frontier *frontier) {
336 if (!flags()->use_root_regions) return;
337 CHECK(root_regions)do { __sanitizer::u64 v1 = (__sanitizer::u64)((root_regions))
; __sanitizer::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect
(!!(!(v1 != v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 337, "(" "(root_regions)" ") " "!=" " (" "0" ")", v1, v2); }
while (false)
;
338 for (uptr i = 0; i < root_regions->size(); i++) {
339 ProcessRootRegion(frontier, (*root_regions)[i]);
340 }
341}
342
343static void FloodFillTag(Frontier *frontier, ChunkTag tag) {
344 while (frontier->size()) {
345 uptr next_chunk = frontier->back();
346 frontier->pop_back();
347 LsanMetadata m(next_chunk);
348 ScanRangeForPointers(next_chunk, next_chunk + m.requested_size(), frontier,
349 "HEAP", tag);
350 }
351}
352
353// ForEachChunk callback. If the chunk is marked as leaked, marks all chunks
354// which are reachable from it as indirectly leaked.
355static void MarkIndirectlyLeakedCb(uptr chunk, void *arg) {
356 chunk = GetUserBegin(chunk);
357 LsanMetadata m(chunk);
358 if (m.allocated() && m.tag() != kReachable) {
359 ScanRangeForPointers(chunk, chunk + m.requested_size(),
360 /* frontier */ nullptr, "HEAP", kIndirectlyLeaked);
361 }
362}
363
364// ForEachChunk callback. If chunk is marked as ignored, adds its address to
365// frontier.
366static void CollectIgnoredCb(uptr chunk, void *arg) {
367 CHECK(arg)do { __sanitizer::u64 v1 = (__sanitizer::u64)((arg)); __sanitizer
::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect(!!(!(v1
!= v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 367, "(" "(arg)" ") " "!=" " (" "0" ")", v1, v2); } while (
false)
;
368 chunk = GetUserBegin(chunk);
369 LsanMetadata m(chunk);
370 if (m.allocated() && m.tag() == kIgnored) {
371 LOG_POINTERS("Ignored: chunk %p-%p of size %zu.\n",do { if (flags()->log_pointers) Report("Ignored: chunk %p-%p of size %zu.\n"
, chunk, chunk + m.requested_size(), m.requested_size()); } while
(0);
372 chunk, chunk + m.requested_size(), m.requested_size())do { if (flags()->log_pointers) Report("Ignored: chunk %p-%p of size %zu.\n"
, chunk, chunk + m.requested_size(), m.requested_size()); } while
(0);
;
373 reinterpret_cast<Frontier *>(arg)->push_back(chunk);
374 }
375}
376
377static uptr GetCallerPC(u32 stack_id, StackDepotReverseMap *map) {
378 CHECK(stack_id)do { __sanitizer::u64 v1 = (__sanitizer::u64)((stack_id)); __sanitizer
::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect(!!(!(v1
!= v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 378, "(" "(stack_id)" ") " "!=" " (" "0" ")", v1, v2); } while
(false)
;
379 StackTrace stack = map->Get(stack_id);
380 // The top frame is our malloc/calloc/etc. The next frame is the caller.
381 if (stack.size >= 2)
382 return stack.trace[1];
383 return 0;
384}
385
386struct InvalidPCParam {
387 Frontier *frontier;
388 StackDepotReverseMap *stack_depot_reverse_map;
389 bool skip_linker_allocations;
390};
391
392// ForEachChunk callback. If the caller pc is invalid or is within the linker,
393// mark as reachable. Called by ProcessPlatformSpecificAllocations.
394static void MarkInvalidPCCb(uptr chunk, void *arg) {
395 CHECK(arg)do { __sanitizer::u64 v1 = (__sanitizer::u64)((arg)); __sanitizer
::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect(!!(!(v1
!= v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 395, "(" "(arg)" ") " "!=" " (" "0" ")", v1, v2); } while (
false)
;
396 InvalidPCParam *param = reinterpret_cast<InvalidPCParam *>(arg);
397 chunk = GetUserBegin(chunk);
398 LsanMetadata m(chunk);
399 if (m.allocated() && m.tag() != kReachable && m.tag() != kIgnored) {
400 u32 stack_id = m.stack_trace_id();
401 uptr caller_pc = 0;
402 if (stack_id > 0)
403 caller_pc = GetCallerPC(stack_id, param->stack_depot_reverse_map);
404 // If caller_pc is unknown, this chunk may be allocated in a coroutine. Mark
405 // it as reachable, as we can't properly report its allocation stack anyway.
406 if (caller_pc == 0 || (param->skip_linker_allocations &&
407 GetLinker()->containsAddress(caller_pc))) {
408 m.set_tag(kReachable);
409 param->frontier->push_back(chunk);
410 }
411 }
412}
413
414// On Linux, handles dynamically allocated TLS blocks by treating all chunks
415// allocated from ld-linux.so as reachable.
416// Dynamic TLS blocks contain the TLS variables of dynamically loaded modules.
417// They are allocated with a __libc_memalign() call in allocate_and_init()
418// (elf/dl-tls.c). Glibc won't tell us the address ranges occupied by those
419// blocks, but we can make sure they come from our own allocator by intercepting
420// __libc_memalign(). On top of that, there is no easy way to reach them. Their
421// addresses are stored in a dynamically allocated array (the DTV) which is
422// referenced from the static TLS. Unfortunately, we can't just rely on the DTV
423// being reachable from the static TLS, and the dynamic TLS being reachable from
424// the DTV. This is because the initial DTV is allocated before our interception
425// mechanism kicks in, and thus we don't recognize it as allocated memory. We
426// can't special-case it either, since we don't know its size.
427// Our solution is to include in the root set all allocations made from
428// ld-linux.so (which is where allocate_and_init() is implemented). This is
429// guaranteed to include all dynamic TLS blocks (and possibly other allocations
430// which we don't care about).
431// On all other platforms, this simply checks to ensure that the caller pc is
432// valid before reporting chunks as leaked.
433void ProcessPC(Frontier *frontier) {
434 StackDepotReverseMap stack_depot_reverse_map;
435 InvalidPCParam arg;
436 arg.frontier = frontier;
437 arg.stack_depot_reverse_map = &stack_depot_reverse_map;
438 arg.skip_linker_allocations =
439 flags()->use_tls && flags()->use_ld_allocations && GetLinker() != nullptr;
440 ForEachChunk(MarkInvalidPCCb, &arg);
441}
442
443// Sets the appropriate tag on each chunk.
444static void ClassifyAllChunks(SuspendedThreadsList const &suspended_threads) {
445 // Holds the flood fill frontier.
446 Frontier frontier(1);
447
448 ForEachChunk(CollectIgnoredCb, &frontier);
449 ProcessGlobalRegions(&frontier);
450 ProcessThreads(suspended_threads, &frontier);
451 ProcessRootRegions(&frontier);
452 FloodFillTag(&frontier, kReachable);
453
454 CHECK_EQ(0, frontier.size())do { __sanitizer::u64 v1 = (__sanitizer::u64)((0)); __sanitizer
::u64 v2 = (__sanitizer::u64)((frontier.size())); if (__builtin_expect
(!!(!(v1 == v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 454, "(" "(0)" ") " "==" " (" "(frontier.size())" ")", v1, v2
); } while (false)
;
455 ProcessPC(&frontier);
456
457 // The check here is relatively expensive, so we do this in a separate flood
458 // fill. That way we can skip the check for chunks that are reachable
459 // otherwise.
460 LOG_POINTERS("Processing platform-specific allocations.\n")do { if (flags()->log_pointers) Report("Processing platform-specific allocations.\n"
); } while (0);
;
461 ProcessPlatformSpecificAllocations(&frontier);
462 FloodFillTag(&frontier, kReachable);
463
464 // Iterate over leaked chunks and mark those that are reachable from other
465 // leaked chunks.
466 LOG_POINTERS("Scanning leaked chunks.\n")do { if (flags()->log_pointers) Report("Scanning leaked chunks.\n"
); } while (0);
;
467 ForEachChunk(MarkIndirectlyLeakedCb, nullptr);
468}
469
470// ForEachChunk callback. Resets the tags to pre-leak-check state.
471static void ResetTagsCb(uptr chunk, void *arg) {
472 (void)arg;
473 chunk = GetUserBegin(chunk);
474 LsanMetadata m(chunk);
475 if (m.allocated() && m.tag() != kIgnored)
476 m.set_tag(kDirectlyLeaked);
477}
478
479static void PrintStackTraceById(u32 stack_trace_id) {
480 CHECK(stack_trace_id)do { __sanitizer::u64 v1 = (__sanitizer::u64)((stack_trace_id
)); __sanitizer::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect
(!!(!(v1 != v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 480, "(" "(stack_trace_id)" ") " "!=" " (" "0" ")", v1, v2)
; } while (false)
;
481 StackDepotGet(stack_trace_id).Print();
482}
483
484// ForEachChunk callback. Aggregates information about unreachable chunks into
485// a LeakReport.
486static void CollectLeaksCb(uptr chunk, void *arg) {
487 CHECK(arg)do { __sanitizer::u64 v1 = (__sanitizer::u64)((arg)); __sanitizer
::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect(!!(!(v1
!= v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 487, "(" "(arg)" ") " "!=" " (" "0" ")", v1, v2); } while (
false)
;
488 LeakReport *leak_report = reinterpret_cast<LeakReport *>(arg);
489 chunk = GetUserBegin(chunk);
490 LsanMetadata m(chunk);
491 if (!m.allocated()) return;
492 if (m.tag() == kDirectlyLeaked || m.tag() == kIndirectlyLeaked) {
493 u32 resolution = flags()->resolution;
494 u32 stack_trace_id = 0;
495 if (resolution > 0) {
496 StackTrace stack = StackDepotGet(m.stack_trace_id());
497 stack.size = Min(stack.size, resolution);
498 stack_trace_id = StackDepotPut(stack);
499 } else {
500 stack_trace_id = m.stack_trace_id();
501 }
502 leak_report->AddLeakedChunk(chunk, stack_trace_id, m.requested_size(),
503 m.tag());
504 }
505}
506
507static void PrintMatchedSuppressions() {
508 InternalMmapVector<Suppression *> matched(1);
509 GetSuppressionContext()->GetMatched(&matched);
510 if (!matched.size())
511 return;
512 const char *line = "-----------------------------------------------------";
513 Printf("%s\n", line);
514 Printf("Suppressions used:\n");
515 Printf(" count bytes template\n");
516 for (uptr i = 0; i < matched.size(); i++)
517 Printf("%7zu %10zu %s\n", static_cast<uptr>(atomic_load_relaxed(
518 &matched[i]->hit_count)), matched[i]->weight, matched[i]->templ);
519 Printf("%s\n\n", line);
520}
521
522struct CheckForLeaksParam {
523 bool success;
524 LeakReport leak_report;
525};
526
527static void CheckForLeaksCallback(const SuspendedThreadsList &suspended_threads,
528 void *arg) {
529 CheckForLeaksParam *param = reinterpret_cast<CheckForLeaksParam *>(arg);
530 CHECK(param)do { __sanitizer::u64 v1 = (__sanitizer::u64)((param)); __sanitizer
::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect(!!(!(v1
!= v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 530, "(" "(param)" ") " "!=" " (" "0" ")", v1, v2); } while
(false)
;
531 CHECK(!param->success)do { __sanitizer::u64 v1 = (__sanitizer::u64)((!param->success
)); __sanitizer::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect
(!!(!(v1 != v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 531, "(" "(!param->success)" ") " "!=" " (" "0" ")", v1,
v2); } while (false)
;
532 ClassifyAllChunks(suspended_threads);
533 ForEachChunk(CollectLeaksCb, &param->leak_report);
534 // Clean up for subsequent leak checks. This assumes we did not overwrite any
535 // kIgnored tags.
536 ForEachChunk(ResetTagsCb, nullptr);
537 param->success = true;
538}
539
540static bool CheckForLeaks() {
541 if (&__lsan_is_turned_off && __lsan_is_turned_off())
542 return false;
543 EnsureMainThreadIDIsCorrect();
544 CheckForLeaksParam param;
545 param.success = false;
546 LockThreadRegistry();
547 LockAllocator();
548 DoStopTheWorld(CheckForLeaksCallback, &param);
549 UnlockAllocator();
550 UnlockThreadRegistry();
551
552 if (!param.success) {
553 Report("LeakSanitizer has encountered a fatal error.\n");
554 Report(
555 "HINT: For debugging, try setting environment variable "
556 "LSAN_OPTIONS=verbosity=1:log_threads=1\n");
557 Report(
558 "HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)\n");
559 Die();
560 }
561 param.leak_report.ApplySuppressions();
562 uptr unsuppressed_count = param.leak_report.UnsuppressedLeakCount();
563 if (unsuppressed_count > 0) {
564 Decorator d;
565 Printf("\n"
566 "================================================================="
567 "\n");
568 Printf("%s", d.Error());
569 Report("ERROR: LeakSanitizer: detected memory leaks\n");
570 Printf("%s", d.Default());
571 param.leak_report.ReportTopLeaks(flags()->max_leaks);
572 }
573 if (common_flags()->print_suppressions)
574 PrintMatchedSuppressions();
575 if (unsuppressed_count > 0) {
576 param.leak_report.PrintSummary();
577 return true;
578 }
579 return false;
580}
581
582static bool has_reported_leaks = false;
583bool HasReportedLeaks() { return has_reported_leaks; }
584
585void DoLeakCheck() {
586 BlockingMutexLock l(&global_mutex);
587 static bool already_done;
588 if (already_done) return;
589 already_done = true;
590 has_reported_leaks = CheckForLeaks();
591 if (has_reported_leaks) HandleLeaks();
592}
593
594static int DoRecoverableLeakCheck() {
595 BlockingMutexLock l(&global_mutex);
596 bool have_leaks = CheckForLeaks();
597 return have_leaks ? 1 : 0;
598}
599
600void DoRecoverableLeakCheckVoid() { DoRecoverableLeakCheck(); }
601
602static Suppression *GetSuppressionForAddr(uptr addr) {
603 Suppression *s = nullptr;
604
605 // Suppress by module name.
606 SuppressionContext *suppressions = GetSuppressionContext();
607 if (const char *module_name =
1
Taking false branch
608 Symbolizer::GetOrInit()->GetModuleNameForPc(addr))
609 if (suppressions->Match(module_name, kSuppressionLeak, &s))
610 return s;
611
612 // Suppress by file or function name.
613 SymbolizedStack *frames = Symbolizer::GetOrInit()->SymbolizePC(addr);
2
'frames' initialized here
614 for (SymbolizedStack *cur = frames; cur; cur = cur->next) {
3
Assuming pointer value is null
4
Loop condition is false. Execution continues on line 620
615 if (suppressions->Match(cur->info.function, kSuppressionLeak, &s) ||
616 suppressions->Match(cur->info.file, kSuppressionLeak, &s)) {
617 break;
618 }
619 }
620 frames->ClearAll();
5
Called C++ object pointer is null
621 return s;
622}
623
624static Suppression *GetSuppressionForStack(u32 stack_trace_id) {
625 StackTrace stack = StackDepotGet(stack_trace_id);
626 for (uptr i = 0; i < stack.size; i++) {
627 Suppression *s = GetSuppressionForAddr(
628 StackTrace::GetPreviousInstructionPc(stack.trace[i]));
629 if (s) return s;
630 }
631 return nullptr;
632}
633
634///// LeakReport implementation. /////
635
636// A hard limit on the number of distinct leaks, to avoid quadratic complexity
637// in LeakReport::AddLeakedChunk(). We don't expect to ever see this many leaks
638// in real-world applications.
639// FIXME: Get rid of this limit by changing the implementation of LeakReport to
640// use a hash table.
641const uptr kMaxLeaksConsidered = 5000;
642
643void LeakReport::AddLeakedChunk(uptr chunk, u32 stack_trace_id,
644 uptr leaked_size, ChunkTag tag) {
645 CHECK(tag == kDirectlyLeaked || tag == kIndirectlyLeaked)do { __sanitizer::u64 v1 = (__sanitizer::u64)((tag == kDirectlyLeaked
|| tag == kIndirectlyLeaked)); __sanitizer::u64 v2 = (__sanitizer
::u64)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)) __sanitizer
::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 645, "(" "(tag == kDirectlyLeaked || tag == kIndirectlyLeaked)"
") " "!=" " (" "0" ")", v1, v2); } while (false)
;
646 bool is_directly_leaked = (tag == kDirectlyLeaked);
647 uptr i;
648 for (i = 0; i < leaks_.size(); i++) {
649 if (leaks_[i].stack_trace_id == stack_trace_id &&
650 leaks_[i].is_directly_leaked == is_directly_leaked) {
651 leaks_[i].hit_count++;
652 leaks_[i].total_size += leaked_size;
653 break;
654 }
655 }
656 if (i == leaks_.size()) {
657 if (leaks_.size() == kMaxLeaksConsidered) return;
658 Leak leak = { next_id_++, /* hit_count */ 1, leaked_size, stack_trace_id,
659 is_directly_leaked, /* is_suppressed */ false };
660 leaks_.push_back(leak);
661 }
662 if (flags()->report_objects) {
663 LeakedObject obj = {leaks_[i].id, chunk, leaked_size};
664 leaked_objects_.push_back(obj);
665 }
666}
667
668static bool LeakComparator(const Leak &leak1, const Leak &leak2) {
669 if (leak1.is_directly_leaked == leak2.is_directly_leaked)
670 return leak1.total_size > leak2.total_size;
671 else
672 return leak1.is_directly_leaked;
673}
674
675void LeakReport::ReportTopLeaks(uptr num_leaks_to_report) {
676 CHECK(leaks_.size() <= kMaxLeaksConsidered)do { __sanitizer::u64 v1 = (__sanitizer::u64)((leaks_.size() <=
kMaxLeaksConsidered)); __sanitizer::u64 v2 = (__sanitizer::u64
)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)) __sanitizer::
CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 676, "(" "(leaks_.size() <= kMaxLeaksConsidered)" ") " "!="
" (" "0" ")", v1, v2); } while (false)
;
677 Printf("\n");
678 if (leaks_.size() == kMaxLeaksConsidered)
679 Printf("Too many leaks! Only the first %zu leaks encountered will be "
680 "reported.\n",
681 kMaxLeaksConsidered);
682
683 uptr unsuppressed_count = UnsuppressedLeakCount();
684 if (num_leaks_to_report > 0 && num_leaks_to_report < unsuppressed_count)
685 Printf("The %zu top leak(s):\n", num_leaks_to_report);
686 InternalSort(&leaks_, leaks_.size(), LeakComparator);
687 uptr leaks_reported = 0;
688 for (uptr i = 0; i < leaks_.size(); i++) {
689 if (leaks_[i].is_suppressed) continue;
690 PrintReportForLeak(i);
691 leaks_reported++;
692 if (leaks_reported == num_leaks_to_report) break;
693 }
694 if (leaks_reported < unsuppressed_count) {
695 uptr remaining = unsuppressed_count - leaks_reported;
696 Printf("Omitting %zu more leak(s).\n", remaining);
697 }
698}
699
700void LeakReport::PrintReportForLeak(uptr index) {
701 Decorator d;
702 Printf("%s", d.Leak());
703 Printf("%s leak of %zu byte(s) in %zu object(s) allocated from:\n",
704 leaks_[index].is_directly_leaked ? "Direct" : "Indirect",
705 leaks_[index].total_size, leaks_[index].hit_count);
706 Printf("%s", d.Default());
707
708 PrintStackTraceById(leaks_[index].stack_trace_id);
709
710 if (flags()->report_objects) {
711 Printf("Objects leaked above:\n");
712 PrintLeakedObjectsForLeak(index);
713 Printf("\n");
714 }
715}
716
717void LeakReport::PrintLeakedObjectsForLeak(uptr index) {
718 u32 leak_id = leaks_[index].id;
719 for (uptr j = 0; j < leaked_objects_.size(); j++) {
720 if (leaked_objects_[j].leak_id == leak_id)
721 Printf("%p (%zu bytes)\n", leaked_objects_[j].addr,
722 leaked_objects_[j].size);
723 }
724}
725
726void LeakReport::PrintSummary() {
727 CHECK(leaks_.size() <= kMaxLeaksConsidered)do { __sanitizer::u64 v1 = (__sanitizer::u64)((leaks_.size() <=
kMaxLeaksConsidered)); __sanitizer::u64 v2 = (__sanitizer::u64
)(0); if (__builtin_expect(!!(!(v1 != v2)), 0)) __sanitizer::
CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 727, "(" "(leaks_.size() <= kMaxLeaksConsidered)" ") " "!="
" (" "0" ")", v1, v2); } while (false)
;
728 uptr bytes = 0, allocations = 0;
729 for (uptr i = 0; i < leaks_.size(); i++) {
730 if (leaks_[i].is_suppressed) continue;
731 bytes += leaks_[i].total_size;
732 allocations += leaks_[i].hit_count;
733 }
734 InternalScopedString summary(kMaxSummaryLength);
735 summary.append("%zu byte(s) leaked in %zu allocation(s).", bytes,
736 allocations);
737 ReportErrorSummary(summary.data());
738}
739
740void LeakReport::ApplySuppressions() {
741 for (uptr i = 0; i < leaks_.size(); i++) {
742 Suppression *s = GetSuppressionForStack(leaks_[i].stack_trace_id);
743 if (s) {
744 s->weight += leaks_[i].total_size;
745 atomic_store_relaxed(&s->hit_count, atomic_load_relaxed(&s->hit_count) +
746 leaks_[i].hit_count);
747 leaks_[i].is_suppressed = true;
748 }
749 }
750}
751
752uptr LeakReport::UnsuppressedLeakCount() {
753 uptr result = 0;
754 for (uptr i = 0; i < leaks_.size(); i++)
755 if (!leaks_[i].is_suppressed) result++;
756 return result;
757}
758
759} // namespace __lsan
760#else // CAN_SANITIZE_LEAKS
761namespace __lsan {
762void InitCommonLsan() { }
763void DoLeakCheck() { }
764void DoRecoverableLeakCheckVoid() { }
765void DisableInThisThread() { }
766void EnableInThisThread() { }
767}
768#endif // CAN_SANITIZE_LEAKS
769
770using namespace __lsan; // NOLINT
771
772extern "C" {
773SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default")))
774void __lsan_ignore_object(const void *p) {
775#if CAN_SANITIZE_LEAKS1
776 if (!common_flags()->detect_leaks)
777 return;
778 // Cannot use PointsIntoChunk or LsanMetadata here, since the allocator is not
779 // locked.
780 BlockingMutexLock l(&global_mutex);
781 IgnoreObjectResult res = IgnoreObjectLocked(p);
782 if (res == kIgnoreObjectInvalid)
783 VReport(1, "__lsan_ignore_object(): no heap object found at %p", p)do { if ((uptr)Verbosity() >= (1)) Report("__lsan_ignore_object(): no heap object found at %p"
, p); } while (0)
;
784 if (res == kIgnoreObjectAlreadyIgnored)
785 VReport(1, "__lsan_ignore_object(): "do { if ((uptr)Verbosity() >= (1)) Report("__lsan_ignore_object(): "
"heap object at %p is already being ignored\n", p); } while (
0)
786 "heap object at %p is already being ignored\n", p)do { if ((uptr)Verbosity() >= (1)) Report("__lsan_ignore_object(): "
"heap object at %p is already being ignored\n", p); } while (
0)
;
787 if (res == kIgnoreObjectSuccess)
788 VReport(1, "__lsan_ignore_object(): ignoring heap object at %p\n", p)do { if ((uptr)Verbosity() >= (1)) Report("__lsan_ignore_object(): ignoring heap object at %p\n"
, p); } while (0)
;
789#endif // CAN_SANITIZE_LEAKS
790}
791
792SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default")))
793void __lsan_register_root_region(const void *begin, uptr size) {
794#if CAN_SANITIZE_LEAKS1
795 BlockingMutexLock l(&global_mutex);
796 CHECK(root_regions)do { __sanitizer::u64 v1 = (__sanitizer::u64)((root_regions))
; __sanitizer::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect
(!!(!(v1 != v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 796, "(" "(root_regions)" ") " "!=" " (" "0" ")", v1, v2); }
while (false)
;
797 RootRegion region = {reinterpret_cast<uptr>(begin), size};
798 root_regions->push_back(region);
799 VReport(1, "Registered root region at %p of size %llu\n", begin, size)do { if ((uptr)Verbosity() >= (1)) Report("Registered root region at %p of size %llu\n"
, begin, size); } while (0)
;
800#endif // CAN_SANITIZE_LEAKS
801}
802
803SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default")))
804void __lsan_unregister_root_region(const void *begin, uptr size) {
805#if CAN_SANITIZE_LEAKS1
806 BlockingMutexLock l(&global_mutex);
807 CHECK(root_regions)do { __sanitizer::u64 v1 = (__sanitizer::u64)((root_regions))
; __sanitizer::u64 v2 = (__sanitizer::u64)(0); if (__builtin_expect
(!!(!(v1 != v2)), 0)) __sanitizer::CheckFailed("/build/llvm-toolchain-snapshot-6.0~svn316068/projects/compiler-rt/lib/lsan/lsan_common.cc"
, 807, "(" "(root_regions)" ") " "!=" " (" "0" ")", v1, v2); }
while (false)
;
808 bool removed = false;
809 for (uptr i = 0; i < root_regions->size(); i++) {
810 RootRegion region = (*root_regions)[i];
811 if (region.begin == reinterpret_cast<uptr>(begin) && region.size == size) {
812 removed = true;
813 uptr last_index = root_regions->size() - 1;
814 (*root_regions)[i] = (*root_regions)[last_index];
815 root_regions->pop_back();
816 VReport(1, "Unregistered root region at %p of size %llu\n", begin, size)do { if ((uptr)Verbosity() >= (1)) Report("Unregistered root region at %p of size %llu\n"
, begin, size); } while (0)
;
817 break;
818 }
819 }
820 if (!removed) {
821 Report(
822 "__lsan_unregister_root_region(): region at %p of size %llu has not "
823 "been registered.\n",
824 begin, size);
825 Die();
826 }
827#endif // CAN_SANITIZE_LEAKS
828}
829
830SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default")))
831void __lsan_disable() {
832#if CAN_SANITIZE_LEAKS1
833 __lsan::DisableInThisThread();
834#endif
835}
836
837SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default")))
838void __lsan_enable() {
839#if CAN_SANITIZE_LEAKS1
840 __lsan::EnableInThisThread();
841#endif
842}
843
844SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default")))
845void __lsan_do_leak_check() {
846#if CAN_SANITIZE_LEAKS1
847 if (common_flags()->detect_leaks)
848 __lsan::DoLeakCheck();
849#endif // CAN_SANITIZE_LEAKS
850}
851
852SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default")))
853int __lsan_do_recoverable_leak_check() {
854#if CAN_SANITIZE_LEAKS1
855 if (common_flags()->detect_leaks)
856 return __lsan::DoRecoverableLeakCheck();
857#endif // CAN_SANITIZE_LEAKS
858 return 0;
859}
860
861#if !SANITIZER_SUPPORTS_WEAK_HOOKS1
862SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default"))) SANITIZER_WEAK_ATTRIBUTE__attribute__((weak))
863const char * __lsan_default_options() {
864 return "";
865}
866
867SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default"))) SANITIZER_WEAK_ATTRIBUTE__attribute__((weak))
868int __lsan_is_turned_off() {
869 return 0;
870}
871
872SANITIZER_INTERFACE_ATTRIBUTE__attribute__((visibility("default"))) SANITIZER_WEAK_ATTRIBUTE__attribute__((weak))
873const char *__lsan_default_suppressions() {
874 return "";
875}
876#endif
877} // extern "C"