1 //=-- lsan_common.h -------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of LeakSanitizer.
11 // Private LSan header.
13 //===----------------------------------------------------------------------===//
18 #include "sanitizer_common/sanitizer_allocator.h"
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_internal_defs.h"
21 #include "sanitizer_common/sanitizer_platform.h"
22 #include "sanitizer_common/sanitizer_symbolizer.h"
24 #if SANITIZER_LINUX && defined(__x86_64__) && (SANITIZER_WORDSIZE == 64)
25 #define CAN_SANITIZE_LEAKS 1
27 #define CAN_SANITIZE_LEAKS 0
34 kDirectlyLeaked
= 0, // default
35 kIndirectlyLeaked
= 1,
41 uptr
pointer_alignment() const {
42 return use_unaligned
? 1 : sizeof(uptr
);
45 // Print addresses of leaked objects after main leak report.
47 // Aggregate two objects into one leak if this many stack frames match. If
48 // zero, the entire stack trace must match.
50 // The number of leaks reported.
52 // If nonzero kill the process with this exit code upon finding leaks.
54 // Print matched suppressions after leak checking.
55 bool print_suppressions
;
56 // Suppressions file name.
57 const char* suppressions
;
59 // Flags controlling the root set of reachable memory.
60 // Global variables (.data and .bss).
66 // TLS and thread-specific storage.
68 // Regions added via __lsan_register_root_region().
69 bool use_root_regions
;
71 // Consider unaligned pointers valid.
73 // Consider pointers found in poisoned memory to be valid.
81 extern Flags lsan_flags
;
82 inline Flags
*flags() { return &lsan_flags
; }
89 bool is_directly_leaked
;
99 // Aggregates leaks by stack trace prefix.
102 LeakReport() : next_id_(0), leaks_(1), leaked_objects_(1) {}
103 void AddLeakedChunk(uptr chunk
, u32 stack_trace_id
, uptr leaked_size
,
105 void ReportTopLeaks(uptr max_leaks
);
107 void ApplySuppressions();
108 uptr
UnsuppressedLeakCount();
112 void PrintReportForLeak(uptr index
);
113 void PrintLeakedObjectsForLeak(uptr index
);
116 InternalMmapVector
<Leak
> leaks_
;
117 InternalMmapVector
<LeakedObject
> leaked_objects_
;
120 typedef InternalMmapVector
<uptr
> Frontier
;
122 // Platform-specific functions.
123 void InitializePlatformSpecificModules();
124 void ProcessGlobalRegions(Frontier
*frontier
);
125 void ProcessPlatformSpecificAllocations(Frontier
*frontier
);
127 void ScanRangeForPointers(uptr begin
, uptr end
,
129 const char *region_type
, ChunkTag tag
);
131 enum IgnoreObjectResult
{
132 kIgnoreObjectSuccess
,
133 kIgnoreObjectAlreadyIgnored
,
137 // Functions called from the parent tool.
138 void InitCommonLsan();
140 bool DisabledInThisThread();
142 // Special case for "new T[0]" where T is a type with DTOR.
143 // new T[0] will allocate one word for the array size (0) and store a pointer
144 // to the end of allocated chunk.
145 inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg
, uptr chunk_size
,
147 return chunk_size
== sizeof(uptr
) && chunk_beg
+ chunk_size
== addr
&&
148 *reinterpret_cast<uptr
*>(chunk_beg
) == 0;
151 // The following must be implemented in the parent tool.
153 void ForEachChunk(ForEachChunkCallback callback
, void *arg
);
154 // Returns the address range occupied by the global allocator object.
155 void GetAllocatorGlobalRange(uptr
*begin
, uptr
*end
);
156 // Wrappers for allocator's ForceLock()/ForceUnlock().
157 void LockAllocator();
158 void UnlockAllocator();
159 // Returns true if [addr, addr + sizeof(void *)) is poisoned.
160 bool WordIsPoisoned(uptr addr
);
161 // Wrappers for ThreadRegistry access.
162 void LockThreadRegistry();
163 void UnlockThreadRegistry();
164 bool GetThreadRangesLocked(uptr os_id
, uptr
*stack_begin
, uptr
*stack_end
,
165 uptr
*tls_begin
, uptr
*tls_end
,
166 uptr
*cache_begin
, uptr
*cache_end
);
167 void ForEachExtraStackRange(uptr os_id
, RangeIteratorCallback callback
,
169 // If called from the main thread, updates the main thread's TID in the thread
170 // registry. We need this to handle processes that fork() without a subsequent
171 // exec(), which invalidates the recorded TID. To update it, we must call
172 // gettid() from the main thread. Our solution is to call this function before
173 // leak checking and also before every call to pthread_create() (to handle cases
174 // where leak checking is initiated from a non-main thread).
175 void EnsureMainThreadIDIsCorrect();
176 // If p points into a chunk that has been allocated to the user, returns its
177 // user-visible address. Otherwise, returns 0.
178 uptr
PointsIntoChunk(void *p
);
179 // Returns address of user-visible chunk contained in this allocator chunk.
180 uptr
GetUserBegin(uptr chunk
);
181 // Helper for __lsan_ignore_object().
182 IgnoreObjectResult
IgnoreObjectLocked(const void *p
);
183 // Wrapper for chunk metadata operations.
186 // Constructor accepts address of user-visible chunk.
187 explicit LsanMetadata(uptr chunk
);
188 bool allocated() const;
189 ChunkTag
tag() const;
190 void set_tag(ChunkTag value
);
191 uptr
requested_size() const;
192 u32
stack_trace_id() const;
197 } // namespace __lsan
200 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
201 int __lsan_is_turned_off();
203 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
204 const char *__lsan_default_suppressions();
207 #endif // LSAN_COMMON_H