]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/lsan/lsan_common.h
Merge compiler-rt trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / lsan / lsan_common.h
1 //=-- lsan_common.h -------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of LeakSanitizer.
11 // Private LSan header.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LSAN_COMMON_H
16 #define LSAN_COMMON_H
17
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"
24
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
43 #else
44 #define CAN_SANITIZE_LEAKS 0
45 #endif
46
47 namespace __sanitizer {
48 class FlagParser;
49 struct DTLS;
50 }
51
52 namespace __lsan {
53
54 // Chunk tags.
55 enum ChunkTag {
56   kDirectlyLeaked = 0,  // default
57   kIndirectlyLeaked = 1,
58   kReachable = 2,
59   kIgnored = 3
60 };
61
62 const u32 kInvalidTid = (u32) -1;
63
64 struct Flags {
65 #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
66 #include "lsan_flags.inc"
67 #undef LSAN_FLAG
68
69   void SetDefaults();
70   uptr pointer_alignment() const {
71     return use_unaligned ? 1 : sizeof(uptr);
72   }
73 };
74
75 extern Flags lsan_flags;
76 inline Flags *flags() { return &lsan_flags; }
77 void RegisterLsanFlags(FlagParser *parser, Flags *f);
78
79 struct Leak {
80   u32 id;
81   uptr hit_count;
82   uptr total_size;
83   u32 stack_trace_id;
84   bool is_directly_leaked;
85   bool is_suppressed;
86 };
87
88 struct LeakedObject {
89   u32 leak_id;
90   uptr addr;
91   uptr size;
92 };
93
94 // Aggregates leaks by stack trace prefix.
95 class LeakReport {
96  public:
97   LeakReport() : next_id_(0), leaks_(1), leaked_objects_(1) {}
98   void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
99                       ChunkTag tag);
100   void ReportTopLeaks(uptr max_leaks);
101   void PrintSummary();
102   void ApplySuppressions();
103   uptr UnsuppressedLeakCount();
104
105
106  private:
107   void PrintReportForLeak(uptr index);
108   void PrintLeakedObjectsForLeak(uptr index);
109
110   u32 next_id_;
111   InternalMmapVector<Leak> leaks_;
112   InternalMmapVector<LeakedObject> leaked_objects_;
113 };
114
115 typedef InternalMmapVector<uptr> Frontier;
116
117 // Platform-specific functions.
118 void InitializePlatformSpecificModules();
119 void ProcessGlobalRegions(Frontier *frontier);
120 void ProcessPlatformSpecificAllocations(Frontier *frontier);
121
122 struct RootRegion {
123   uptr begin;
124   uptr size;
125 };
126
127 InternalMmapVector<RootRegion> const *GetRootRegions();
128 void ScanRootRegion(Frontier *frontier, RootRegion const &region,
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);
132
133 void ScanRangeForPointers(uptr begin, uptr end,
134                           Frontier *frontier,
135                           const char *region_type, ChunkTag tag);
136 void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier);
137
138 enum IgnoreObjectResult {
139   kIgnoreObjectSuccess,
140   kIgnoreObjectAlreadyIgnored,
141   kIgnoreObjectInvalid
142 };
143
144 // Functions called from the parent tool.
145 void InitCommonLsan();
146 void DoLeakCheck();
147 void DisableCounterUnderflow();
148 bool DisabledInThisThread();
149
150 // Used to implement __lsan::ScopedDisabler.
151 void DisableInThisThread();
152 void EnableInThisThread();
153 // Can be used to ignore memory allocated by an intercepted
154 // function.
155 struct ScopedInterceptorDisabler {
156   ScopedInterceptorDisabler() { DisableInThisThread(); }
157   ~ScopedInterceptorDisabler() { EnableInThisThread(); }
158 };
159
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,
163                                            uptr addr) {
164   return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
165          *reinterpret_cast<uptr *>(chunk_beg) == 0;
166 }
167
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;
172 // };
173 static inline bool IsARMABIArrayCookie(uptr chunk_beg, uptr chunk_size,
174                                        uptr addr) {
175   return chunk_size == 2 * sizeof(uptr) && chunk_beg + chunk_size == addr &&
176          *reinterpret_cast<uptr *>(chunk_beg + sizeof(uptr)) == 0;
177 }
178
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,
184                                         uptr addr) {
185 #if defined(__arm__)
186   return IsARMABIArrayCookie(chunk_beg, chunk_size, addr);
187 #else
188   return IsItaniumABIArrayCookie(chunk_beg, chunk_size, addr);
189 #endif
190 }
191
192 // The following must be implemented in the parent tool.
193
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,
209                             void *arg);
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);
224
225 // Return the linker module, if valid for the platform.
226 LoadedModule *GetLinker();
227
228 // Wrapper for chunk metadata operations.
229 class LsanMetadata {
230  public:
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;
238  private:
239   void *metadata_;
240 };
241
242 }  // namespace __lsan
243
244 extern "C" {
245 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
246 int __lsan_is_turned_off();
247
248 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
249 const char *__lsan_default_suppressions();
250 }  // extern "C"
251
252 #endif  // LSAN_COMMON_H