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__) || defined(__mips64)) \
25 && (SANITIZER_WORDSIZE == 64)
26 #define CAN_SANITIZE_LEAKS 1
28 #define CAN_SANITIZE_LEAKS 0
31 namespace __sanitizer {
39 kDirectlyLeaked = 0, // default
40 kIndirectlyLeaked = 1,
46 #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
47 #include "lsan_flags.inc"
51 uptr pointer_alignment() const {
52 return use_unaligned ? 1 : sizeof(uptr);
56 extern Flags lsan_flags;
57 inline Flags *flags() { return &lsan_flags; }
58 void RegisterLsanFlags(FlagParser *parser, Flags *f);
65 bool is_directly_leaked;
75 // Aggregates leaks by stack trace prefix.
78 LeakReport() : next_id_(0), leaks_(1), leaked_objects_(1) {}
79 void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
81 void ReportTopLeaks(uptr max_leaks);
83 void ApplySuppressions();
84 uptr UnsuppressedLeakCount();
88 void PrintReportForLeak(uptr index);
89 void PrintLeakedObjectsForLeak(uptr index);
92 InternalMmapVector<Leak> leaks_;
93 InternalMmapVector<LeakedObject> leaked_objects_;
96 typedef InternalMmapVector<uptr> Frontier;
98 // Platform-specific functions.
99 void InitializePlatformSpecificModules();
100 void ProcessGlobalRegions(Frontier *frontier);
101 void ProcessPlatformSpecificAllocations(Frontier *frontier);
103 void ScanRangeForPointers(uptr begin, uptr end,
105 const char *region_type, ChunkTag tag);
107 enum IgnoreObjectResult {
108 kIgnoreObjectSuccess,
109 kIgnoreObjectAlreadyIgnored,
113 // Functions called from the parent tool.
114 void InitCommonLsan();
116 bool DisabledInThisThread();
118 // Special case for "new T[0]" where T is a type with DTOR.
119 // new T[0] will allocate one word for the array size (0) and store a pointer
120 // to the end of allocated chunk.
121 inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
123 return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
124 *reinterpret_cast<uptr *>(chunk_beg) == 0;
127 // The following must be implemented in the parent tool.
129 void ForEachChunk(ForEachChunkCallback callback, void *arg);
130 // Returns the address range occupied by the global allocator object.
131 void GetAllocatorGlobalRange(uptr *begin, uptr *end);
132 // Wrappers for allocator's ForceLock()/ForceUnlock().
133 void LockAllocator();
134 void UnlockAllocator();
135 // Returns true if [addr, addr + sizeof(void *)) is poisoned.
136 bool WordIsPoisoned(uptr addr);
137 // Wrappers for ThreadRegistry access.
138 void LockThreadRegistry();
139 void UnlockThreadRegistry();
140 bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
141 uptr *tls_begin, uptr *tls_end,
142 uptr *cache_begin, uptr *cache_end);
143 void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
145 // If called from the main thread, updates the main thread's TID in the thread
146 // registry. We need this to handle processes that fork() without a subsequent
147 // exec(), which invalidates the recorded TID. To update it, we must call
148 // gettid() from the main thread. Our solution is to call this function before
149 // leak checking and also before every call to pthread_create() (to handle cases
150 // where leak checking is initiated from a non-main thread).
151 void EnsureMainThreadIDIsCorrect();
152 // If p points into a chunk that has been allocated to the user, returns its
153 // user-visible address. Otherwise, returns 0.
154 uptr PointsIntoChunk(void *p);
155 // Returns address of user-visible chunk contained in this allocator chunk.
156 uptr GetUserBegin(uptr chunk);
157 // Helper for __lsan_ignore_object().
158 IgnoreObjectResult IgnoreObjectLocked(const void *p);
159 // Wrapper for chunk metadata operations.
162 // Constructor accepts address of user-visible chunk.
163 explicit LsanMetadata(uptr chunk);
164 bool allocated() const;
165 ChunkTag tag() const;
166 void set_tag(ChunkTag value);
167 uptr requested_size() const;
168 u32 stack_trace_id() const;
173 } // namespace __lsan
176 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
177 int __lsan_is_turned_off();
179 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
180 const char *__lsan_default_suppressions();
183 #endif // LSAN_COMMON_H