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 #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
42 #include "lsan_flags.inc"
46 uptr pointer_alignment() const {
47 return use_unaligned ? 1 : sizeof(uptr);
51 extern Flags lsan_flags;
52 inline Flags *flags() { return &lsan_flags; }
59 bool is_directly_leaked;
69 // Aggregates leaks by stack trace prefix.
72 LeakReport() : next_id_(0), leaks_(1), leaked_objects_(1) {}
73 void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
75 void ReportTopLeaks(uptr max_leaks);
77 void ApplySuppressions();
78 uptr UnsuppressedLeakCount();
82 void PrintReportForLeak(uptr index);
83 void PrintLeakedObjectsForLeak(uptr index);
86 InternalMmapVector<Leak> leaks_;
87 InternalMmapVector<LeakedObject> leaked_objects_;
90 typedef InternalMmapVector<uptr> Frontier;
92 // Platform-specific functions.
93 void InitializePlatformSpecificModules();
94 void ProcessGlobalRegions(Frontier *frontier);
95 void ProcessPlatformSpecificAllocations(Frontier *frontier);
97 void ScanRangeForPointers(uptr begin, uptr end,
99 const char *region_type, ChunkTag tag);
101 enum IgnoreObjectResult {
102 kIgnoreObjectSuccess,
103 kIgnoreObjectAlreadyIgnored,
107 // Functions called from the parent tool.
108 void InitCommonLsan(bool standalone);
110 bool DisabledInThisThread();
112 // Special case for "new T[0]" where T is a type with DTOR.
113 // new T[0] will allocate one word for the array size (0) and store a pointer
114 // to the end of allocated chunk.
115 inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
117 return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
118 *reinterpret_cast<uptr *>(chunk_beg) == 0;
121 // The following must be implemented in the parent tool.
123 void ForEachChunk(ForEachChunkCallback callback, void *arg);
124 // Returns the address range occupied by the global allocator object.
125 void GetAllocatorGlobalRange(uptr *begin, uptr *end);
126 // Wrappers for allocator's ForceLock()/ForceUnlock().
127 void LockAllocator();
128 void UnlockAllocator();
129 // Returns true if [addr, addr + sizeof(void *)) is poisoned.
130 bool WordIsPoisoned(uptr addr);
131 // Wrappers for ThreadRegistry access.
132 void LockThreadRegistry();
133 void UnlockThreadRegistry();
134 bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
135 uptr *tls_begin, uptr *tls_end,
136 uptr *cache_begin, uptr *cache_end);
137 void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
139 // If called from the main thread, updates the main thread's TID in the thread
140 // registry. We need this to handle processes that fork() without a subsequent
141 // exec(), which invalidates the recorded TID. To update it, we must call
142 // gettid() from the main thread. Our solution is to call this function before
143 // leak checking and also before every call to pthread_create() (to handle cases
144 // where leak checking is initiated from a non-main thread).
145 void EnsureMainThreadIDIsCorrect();
146 // If p points into a chunk that has been allocated to the user, returns its
147 // user-visible address. Otherwise, returns 0.
148 uptr PointsIntoChunk(void *p);
149 // Returns address of user-visible chunk contained in this allocator chunk.
150 uptr GetUserBegin(uptr chunk);
151 // Helper for __lsan_ignore_object().
152 IgnoreObjectResult IgnoreObjectLocked(const void *p);
153 // Wrapper for chunk metadata operations.
156 // Constructor accepts address of user-visible chunk.
157 explicit LsanMetadata(uptr chunk);
158 bool allocated() const;
159 ChunkTag tag() const;
160 void set_tag(ChunkTag value);
161 uptr requested_size() const;
162 u32 stack_trace_id() const;
167 } // namespace __lsan
170 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
171 int __lsan_is_turned_off();
173 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
174 const char *__lsan_default_suppressions();
177 #endif // LSAN_COMMON_H