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_stoptheworld.h"
23 #include "sanitizer_common/sanitizer_symbolizer.h"
25 // LeakSanitizer relies on some Glibc's internals (e.g. TLS machinery) thus
26 // supported for Linux only. Also, LSan doesn't like 32 bit architectures
27 // because of "small" (4 bytes) pointer size that leads to high false negative
28 // ratio on large leaks. But we still want to have it for some 32 bit arches
29 // (e.g. x86), see https://github.com/google/sanitizers/issues/403.
30 // To enable LeakSanitizer on new architecture, one need to implement
31 // internal_clone function as well as (probably) adjust TLS machinery for
32 // new architecture inside sanitizer library.
33 #if (SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC) && \
34 (SANITIZER_WORDSIZE == 64) && \
35 (defined(__x86_64__) || defined(__mips64) || defined(__aarch64__))
36 #define CAN_SANITIZE_LEAKS 1
37 #elif defined(__i386__) && \
38 (SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC)
39 #define CAN_SANITIZE_LEAKS 1
40 #elif defined(__arm__) && \
41 SANITIZER_LINUX && !SANITIZER_ANDROID
42 #define CAN_SANITIZE_LEAKS 1
44 #define CAN_SANITIZE_LEAKS 0
47 namespace __sanitizer {
56 kDirectlyLeaked = 0, // default
57 kIndirectlyLeaked = 1,
62 const u32 kInvalidTid = (u32) -1;
65 #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
66 #include "lsan_flags.inc"
70 uptr pointer_alignment() const {
71 return use_unaligned ? 1 : sizeof(uptr);
75 extern Flags lsan_flags;
76 inline Flags *flags() { return &lsan_flags; }
77 void RegisterLsanFlags(FlagParser *parser, Flags *f);
84 bool is_directly_leaked;
94 // Aggregates leaks by stack trace prefix.
97 LeakReport() : next_id_(0), leaks_(1), leaked_objects_(1) {}
98 void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
100 void ReportTopLeaks(uptr max_leaks);
102 void ApplySuppressions();
103 uptr UnsuppressedLeakCount();
107 void PrintReportForLeak(uptr index);
108 void PrintLeakedObjectsForLeak(uptr index);
111 InternalMmapVector<Leak> leaks_;
112 InternalMmapVector<LeakedObject> leaked_objects_;
115 typedef InternalMmapVector<uptr> Frontier;
117 // Platform-specific functions.
118 void InitializePlatformSpecificModules();
119 void ProcessGlobalRegions(Frontier *frontier);
120 void ProcessPlatformSpecificAllocations(Frontier *frontier);
127 InternalMmapVector<RootRegion> const *GetRootRegions();
128 void ScanRootRegion(Frontier *frontier, RootRegion const ®ion,
129 uptr region_begin, uptr region_end, uptr prot);
130 // Run stoptheworld while holding any platform-specific locks.
131 void DoStopTheWorld(StopTheWorldCallback callback, void* argument);
133 void ScanRangeForPointers(uptr begin, uptr end,
135 const char *region_type, ChunkTag tag);
136 void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier);
138 enum IgnoreObjectResult {
139 kIgnoreObjectSuccess,
140 kIgnoreObjectAlreadyIgnored,
144 // Functions called from the parent tool.
145 void InitCommonLsan();
147 void DisableCounterUnderflow();
148 bool DisabledInThisThread();
150 // Used to implement __lsan::ScopedDisabler.
151 void DisableInThisThread();
152 void EnableInThisThread();
153 // Can be used to ignore memory allocated by an intercepted
155 struct ScopedInterceptorDisabler {
156 ScopedInterceptorDisabler() { DisableInThisThread(); }
157 ~ScopedInterceptorDisabler() { EnableInThisThread(); }
160 // According to Itanium C++ ABI array cookie is a one word containing
161 // size of allocated array.
162 static inline bool IsItaniumABIArrayCookie(uptr chunk_beg, uptr chunk_size,
164 return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
165 *reinterpret_cast<uptr *>(chunk_beg) == 0;
168 // According to ARM C++ ABI array cookie consists of two words:
169 // struct array_cookie {
170 // std::size_t element_size; // element_size != 0
171 // std::size_t element_count;
173 static inline bool IsARMABIArrayCookie(uptr chunk_beg, uptr chunk_size,
175 return chunk_size == 2 * sizeof(uptr) && chunk_beg + chunk_size == addr &&
176 *reinterpret_cast<uptr *>(chunk_beg + sizeof(uptr)) == 0;
179 // Special case for "new T[0]" where T is a type with DTOR.
180 // new T[0] will allocate a cookie (one or two words) for the array size (0)
181 // and store a pointer to the end of allocated chunk. The actual cookie layout
182 // varies between platforms according to their C++ ABI implementation.
183 inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
186 return IsARMABIArrayCookie(chunk_beg, chunk_size, addr);
188 return IsItaniumABIArrayCookie(chunk_beg, chunk_size, addr);
192 // The following must be implemented in the parent tool.
194 void ForEachChunk(ForEachChunkCallback callback, void *arg);
195 // Returns the address range occupied by the global allocator object.
196 void GetAllocatorGlobalRange(uptr *begin, uptr *end);
197 // Wrappers for allocator's ForceLock()/ForceUnlock().
198 void LockAllocator();
199 void UnlockAllocator();
200 // Returns true if [addr, addr + sizeof(void *)) is poisoned.
201 bool WordIsPoisoned(uptr addr);
202 // Wrappers for ThreadRegistry access.
203 void LockThreadRegistry();
204 void UnlockThreadRegistry();
205 bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
206 uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
207 uptr *cache_end, DTLS **dtls);
208 void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
210 // If called from the main thread, updates the main thread's TID in the thread
211 // registry. We need this to handle processes that fork() without a subsequent
212 // exec(), which invalidates the recorded TID. To update it, we must call
213 // gettid() from the main thread. Our solution is to call this function before
214 // leak checking and also before every call to pthread_create() (to handle cases
215 // where leak checking is initiated from a non-main thread).
216 void EnsureMainThreadIDIsCorrect();
217 // If p points into a chunk that has been allocated to the user, returns its
218 // user-visible address. Otherwise, returns 0.
219 uptr PointsIntoChunk(void *p);
220 // Returns address of user-visible chunk contained in this allocator chunk.
221 uptr GetUserBegin(uptr chunk);
222 // Helper for __lsan_ignore_object().
223 IgnoreObjectResult IgnoreObjectLocked(const void *p);
225 // Return the linker module, if valid for the platform.
226 LoadedModule *GetLinker();
228 // Wrapper for chunk metadata operations.
231 // Constructor accepts address of user-visible chunk.
232 explicit LsanMetadata(uptr chunk);
233 bool allocated() const;
234 ChunkTag tag() const;
235 void set_tag(ChunkTag value);
236 uptr requested_size() const;
237 u32 stack_trace_id() const;
242 } // namespace __lsan
245 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
246 int __lsan_is_turned_off();
248 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
249 const char *__lsan_default_suppressions();
252 #endif // LSAN_COMMON_H