* config/abi/post/alpha-linux-gnu/baseline_symbols.txt: Update.
[official-gcc.git] / libsanitizer / lsan / lsan_common.h
blob0c84d414be2ed8110c71b039953d08f960364f27
1 //=-- lsan_common.h -------------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of LeakSanitizer.
9 // Private LSan header.
11 //===----------------------------------------------------------------------===//
13 #ifndef LSAN_COMMON_H
14 #define LSAN_COMMON_H
16 #include "sanitizer_common/sanitizer_allocator.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_internal_defs.h"
19 #include "sanitizer_common/sanitizer_platform.h"
20 #include "sanitizer_common/sanitizer_symbolizer.h"
22 #if SANITIZER_LINUX && defined(__x86_64__) && (SANITIZER_WORDSIZE == 64)
23 #define CAN_SANITIZE_LEAKS 1
24 #else
25 #define CAN_SANITIZE_LEAKS 0
26 #endif
28 namespace __lsan {
30 // Chunk tags.
31 enum ChunkTag {
32 kDirectlyLeaked = 0, // default
33 kIndirectlyLeaked = 1,
34 kReachable = 2,
35 kIgnored = 3
38 struct Flags {
39 uptr pointer_alignment() const {
40 return use_unaligned ? 1 : sizeof(uptr);
43 // Print addresses of leaked objects after main leak report.
44 bool report_objects;
45 // Aggregate two objects into one leak if this many stack frames match. If
46 // zero, the entire stack trace must match.
47 int resolution;
48 // The number of leaks reported.
49 int max_leaks;
50 // If nonzero kill the process with this exit code upon finding leaks.
51 int exitcode;
52 // Print matched suppressions after leak checking.
53 bool print_suppressions;
54 // Suppressions file name.
55 const char* suppressions;
57 // Flags controlling the root set of reachable memory.
58 // Global variables (.data and .bss).
59 bool use_globals;
60 // Thread stacks.
61 bool use_stacks;
62 // Thread registers.
63 bool use_registers;
64 // TLS and thread-specific storage.
65 bool use_tls;
66 // Regions added via __lsan_register_root_region().
67 bool use_root_regions;
69 // Consider unaligned pointers valid.
70 bool use_unaligned;
71 // Consider pointers found in poisoned memory to be valid.
72 bool use_poisoned;
74 // Debug logging.
75 bool log_pointers;
76 bool log_threads;
79 extern Flags lsan_flags;
80 inline Flags *flags() { return &lsan_flags; }
82 struct Leak {
83 u32 id;
84 uptr hit_count;
85 uptr total_size;
86 u32 stack_trace_id;
87 bool is_directly_leaked;
88 bool is_suppressed;
91 struct LeakedObject {
92 u32 leak_id;
93 uptr addr;
94 uptr size;
97 // Aggregates leaks by stack trace prefix.
98 class LeakReport {
99 public:
100 LeakReport() : next_id_(0), leaks_(1), leaked_objects_(1) {}
101 void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
102 ChunkTag tag);
103 void ReportTopLeaks(uptr max_leaks);
104 void PrintSummary();
105 void ApplySuppressions();
106 uptr UnsuppressedLeakCount();
109 private:
110 void PrintReportForLeak(uptr index);
111 void PrintLeakedObjectsForLeak(uptr index);
113 u32 next_id_;
114 InternalMmapVector<Leak> leaks_;
115 InternalMmapVector<LeakedObject> leaked_objects_;
118 typedef InternalMmapVector<uptr> Frontier;
120 // Platform-specific functions.
121 void InitializePlatformSpecificModules();
122 void ProcessGlobalRegions(Frontier *frontier);
123 void ProcessPlatformSpecificAllocations(Frontier *frontier);
125 void ScanRangeForPointers(uptr begin, uptr end,
126 Frontier *frontier,
127 const char *region_type, ChunkTag tag);
129 enum IgnoreObjectResult {
130 kIgnoreObjectSuccess,
131 kIgnoreObjectAlreadyIgnored,
132 kIgnoreObjectInvalid
135 // Functions called from the parent tool.
136 void InitCommonLsan();
137 void DoLeakCheck();
138 bool DisabledInThisThread();
140 // Special case for "new T[0]" where T is a type with DTOR.
141 // new T[0] will allocate one word for the array size (0) and store a pointer
142 // to the end of allocated chunk.
143 inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
144 uptr addr) {
145 return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
146 *reinterpret_cast<uptr *>(chunk_beg) == 0;
149 // The following must be implemented in the parent tool.
151 void ForEachChunk(ForEachChunkCallback callback, void *arg);
152 // Returns the address range occupied by the global allocator object.
153 void GetAllocatorGlobalRange(uptr *begin, uptr *end);
154 // Wrappers for allocator's ForceLock()/ForceUnlock().
155 void LockAllocator();
156 void UnlockAllocator();
157 // Returns true if [addr, addr + sizeof(void *)) is poisoned.
158 bool WordIsPoisoned(uptr addr);
159 // Wrappers for ThreadRegistry access.
160 void LockThreadRegistry();
161 void UnlockThreadRegistry();
162 bool GetThreadRangesLocked(uptr os_id, uptr *stack_begin, uptr *stack_end,
163 uptr *tls_begin, uptr *tls_end,
164 uptr *cache_begin, uptr *cache_end);
165 void ForEachExtraStackRange(uptr os_id, RangeIteratorCallback callback,
166 void *arg);
167 // If called from the main thread, updates the main thread's TID in the thread
168 // registry. We need this to handle processes that fork() without a subsequent
169 // exec(), which invalidates the recorded TID. To update it, we must call
170 // gettid() from the main thread. Our solution is to call this function before
171 // leak checking and also before every call to pthread_create() (to handle cases
172 // where leak checking is initiated from a non-main thread).
173 void EnsureMainThreadIDIsCorrect();
174 // If p points into a chunk that has been allocated to the user, returns its
175 // user-visible address. Otherwise, returns 0.
176 uptr PointsIntoChunk(void *p);
177 // Returns address of user-visible chunk contained in this allocator chunk.
178 uptr GetUserBegin(uptr chunk);
179 // Helper for __lsan_ignore_object().
180 IgnoreObjectResult IgnoreObjectLocked(const void *p);
181 // Wrapper for chunk metadata operations.
182 class LsanMetadata {
183 public:
184 // Constructor accepts address of user-visible chunk.
185 explicit LsanMetadata(uptr chunk);
186 bool allocated() const;
187 ChunkTag tag() const;
188 void set_tag(ChunkTag value);
189 uptr requested_size() const;
190 u32 stack_trace_id() const;
191 private:
192 void *metadata_;
195 } // namespace __lsan
197 extern "C" {
198 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
199 int __lsan_is_turned_off();
201 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
202 const char *__lsan_default_suppressions();
203 } // extern "C"
205 #endif // LSAN_COMMON_H