xref: /freebsd/contrib/llvm-project/compiler-rt/lib/lsan/lsan_common.h (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //=-- lsan_common.h -------------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file is a part of LeakSanitizer.
10*0b57cec5SDimitry Andric // Private LSan header.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric #ifndef LSAN_COMMON_H
15*0b57cec5SDimitry Andric #define LSAN_COMMON_H
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_allocator.h"
18*0b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_common.h"
19*0b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_internal_defs.h"
20*0b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_platform.h"
21*0b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_stoptheworld.h"
22*0b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_symbolizer.h"
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric // LeakSanitizer relies on some Glibc's internals (e.g. TLS machinery) on Linux.
25*0b57cec5SDimitry Andric // Also, LSan doesn't like 32 bit architectures
26*0b57cec5SDimitry Andric // because of "small" (4 bytes) pointer size that leads to high false negative
27*0b57cec5SDimitry Andric // ratio on large leaks. But we still want to have it for some 32 bit arches
28*0b57cec5SDimitry Andric // (e.g. x86), see https://github.com/google/sanitizers/issues/403.
29*0b57cec5SDimitry Andric // To enable LeakSanitizer on a new architecture, one needs to implement the
30*0b57cec5SDimitry Andric // internal_clone function as well as (probably) adjust the TLS machinery for
31*0b57cec5SDimitry Andric // the new architecture inside the sanitizer library.
32*0b57cec5SDimitry Andric #if (SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC) && \
33*0b57cec5SDimitry Andric     (SANITIZER_WORDSIZE == 64) &&                               \
34*0b57cec5SDimitry Andric     (defined(__x86_64__) || defined(__mips64) || defined(__aarch64__) || \
35*0b57cec5SDimitry Andric      defined(__powerpc64__))
36*0b57cec5SDimitry Andric #define CAN_SANITIZE_LEAKS 1
37*0b57cec5SDimitry Andric #elif defined(__i386__) && \
38*0b57cec5SDimitry Andric     (SANITIZER_LINUX && !SANITIZER_ANDROID || SANITIZER_MAC)
39*0b57cec5SDimitry Andric #define CAN_SANITIZE_LEAKS 1
40*0b57cec5SDimitry Andric #elif defined(__arm__) && \
41*0b57cec5SDimitry Andric     SANITIZER_LINUX && !SANITIZER_ANDROID
42*0b57cec5SDimitry Andric #define CAN_SANITIZE_LEAKS 1
43*0b57cec5SDimitry Andric #elif SANITIZER_NETBSD
44*0b57cec5SDimitry Andric #define CAN_SANITIZE_LEAKS 1
45*0b57cec5SDimitry Andric #else
46*0b57cec5SDimitry Andric #define CAN_SANITIZE_LEAKS 0
47*0b57cec5SDimitry Andric #endif
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric namespace __sanitizer {
50*0b57cec5SDimitry Andric class FlagParser;
51*0b57cec5SDimitry Andric class ThreadRegistry;
52*0b57cec5SDimitry Andric struct DTLS;
53*0b57cec5SDimitry Andric }
54*0b57cec5SDimitry Andric 
55*0b57cec5SDimitry Andric namespace __lsan {
56*0b57cec5SDimitry Andric 
57*0b57cec5SDimitry Andric // Chunk tags.
58*0b57cec5SDimitry Andric enum ChunkTag {
59*0b57cec5SDimitry Andric   kDirectlyLeaked = 0,  // default
60*0b57cec5SDimitry Andric   kIndirectlyLeaked = 1,
61*0b57cec5SDimitry Andric   kReachable = 2,
62*0b57cec5SDimitry Andric   kIgnored = 3
63*0b57cec5SDimitry Andric };
64*0b57cec5SDimitry Andric 
65*0b57cec5SDimitry Andric const u32 kInvalidTid = (u32) -1;
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric struct Flags {
68*0b57cec5SDimitry Andric #define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
69*0b57cec5SDimitry Andric #include "lsan_flags.inc"
70*0b57cec5SDimitry Andric #undef LSAN_FLAG
71*0b57cec5SDimitry Andric 
72*0b57cec5SDimitry Andric   void SetDefaults();
73*0b57cec5SDimitry Andric   uptr pointer_alignment() const {
74*0b57cec5SDimitry Andric     return use_unaligned ? 1 : sizeof(uptr);
75*0b57cec5SDimitry Andric   }
76*0b57cec5SDimitry Andric };
77*0b57cec5SDimitry Andric 
78*0b57cec5SDimitry Andric extern Flags lsan_flags;
79*0b57cec5SDimitry Andric inline Flags *flags() { return &lsan_flags; }
80*0b57cec5SDimitry Andric void RegisterLsanFlags(FlagParser *parser, Flags *f);
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric struct Leak {
83*0b57cec5SDimitry Andric   u32 id;
84*0b57cec5SDimitry Andric   uptr hit_count;
85*0b57cec5SDimitry Andric   uptr total_size;
86*0b57cec5SDimitry Andric   u32 stack_trace_id;
87*0b57cec5SDimitry Andric   bool is_directly_leaked;
88*0b57cec5SDimitry Andric   bool is_suppressed;
89*0b57cec5SDimitry Andric };
90*0b57cec5SDimitry Andric 
91*0b57cec5SDimitry Andric struct LeakedObject {
92*0b57cec5SDimitry Andric   u32 leak_id;
93*0b57cec5SDimitry Andric   uptr addr;
94*0b57cec5SDimitry Andric   uptr size;
95*0b57cec5SDimitry Andric };
96*0b57cec5SDimitry Andric 
97*0b57cec5SDimitry Andric // Aggregates leaks by stack trace prefix.
98*0b57cec5SDimitry Andric class LeakReport {
99*0b57cec5SDimitry Andric  public:
100*0b57cec5SDimitry Andric   LeakReport() {}
101*0b57cec5SDimitry Andric   void AddLeakedChunk(uptr chunk, u32 stack_trace_id, uptr leaked_size,
102*0b57cec5SDimitry Andric                       ChunkTag tag);
103*0b57cec5SDimitry Andric   void ReportTopLeaks(uptr max_leaks);
104*0b57cec5SDimitry Andric   void PrintSummary();
105*0b57cec5SDimitry Andric   void ApplySuppressions();
106*0b57cec5SDimitry Andric   uptr UnsuppressedLeakCount();
107*0b57cec5SDimitry Andric 
108*0b57cec5SDimitry Andric  private:
109*0b57cec5SDimitry Andric   void PrintReportForLeak(uptr index);
110*0b57cec5SDimitry Andric   void PrintLeakedObjectsForLeak(uptr index);
111*0b57cec5SDimitry Andric 
112*0b57cec5SDimitry Andric   u32 next_id_ = 0;
113*0b57cec5SDimitry Andric   InternalMmapVector<Leak> leaks_;
114*0b57cec5SDimitry Andric   InternalMmapVector<LeakedObject> leaked_objects_;
115*0b57cec5SDimitry Andric };
116*0b57cec5SDimitry Andric 
117*0b57cec5SDimitry Andric typedef InternalMmapVector<uptr> Frontier;
118*0b57cec5SDimitry Andric 
119*0b57cec5SDimitry Andric // Platform-specific functions.
120*0b57cec5SDimitry Andric void InitializePlatformSpecificModules();
121*0b57cec5SDimitry Andric void ProcessGlobalRegions(Frontier *frontier);
122*0b57cec5SDimitry Andric void ProcessPlatformSpecificAllocations(Frontier *frontier);
123*0b57cec5SDimitry Andric 
124*0b57cec5SDimitry Andric struct RootRegion {
125*0b57cec5SDimitry Andric   uptr begin;
126*0b57cec5SDimitry Andric   uptr size;
127*0b57cec5SDimitry Andric };
128*0b57cec5SDimitry Andric 
129*0b57cec5SDimitry Andric InternalMmapVector<RootRegion> const *GetRootRegions();
130*0b57cec5SDimitry Andric void ScanRootRegion(Frontier *frontier, RootRegion const &region,
131*0b57cec5SDimitry Andric                     uptr region_begin, uptr region_end, bool is_readable);
132*0b57cec5SDimitry Andric // Run stoptheworld while holding any platform-specific locks.
133*0b57cec5SDimitry Andric void DoStopTheWorld(StopTheWorldCallback callback, void* argument);
134*0b57cec5SDimitry Andric 
135*0b57cec5SDimitry Andric void ScanRangeForPointers(uptr begin, uptr end,
136*0b57cec5SDimitry Andric                           Frontier *frontier,
137*0b57cec5SDimitry Andric                           const char *region_type, ChunkTag tag);
138*0b57cec5SDimitry Andric void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier);
139*0b57cec5SDimitry Andric 
140*0b57cec5SDimitry Andric enum IgnoreObjectResult {
141*0b57cec5SDimitry Andric   kIgnoreObjectSuccess,
142*0b57cec5SDimitry Andric   kIgnoreObjectAlreadyIgnored,
143*0b57cec5SDimitry Andric   kIgnoreObjectInvalid
144*0b57cec5SDimitry Andric };
145*0b57cec5SDimitry Andric 
146*0b57cec5SDimitry Andric // Functions called from the parent tool.
147*0b57cec5SDimitry Andric const char *MaybeCallLsanDefaultOptions();
148*0b57cec5SDimitry Andric void InitCommonLsan();
149*0b57cec5SDimitry Andric void DoLeakCheck();
150*0b57cec5SDimitry Andric void DoRecoverableLeakCheckVoid();
151*0b57cec5SDimitry Andric void DisableCounterUnderflow();
152*0b57cec5SDimitry Andric bool DisabledInThisThread();
153*0b57cec5SDimitry Andric 
154*0b57cec5SDimitry Andric // Used to implement __lsan::ScopedDisabler.
155*0b57cec5SDimitry Andric void DisableInThisThread();
156*0b57cec5SDimitry Andric void EnableInThisThread();
157*0b57cec5SDimitry Andric // Can be used to ignore memory allocated by an intercepted
158*0b57cec5SDimitry Andric // function.
159*0b57cec5SDimitry Andric struct ScopedInterceptorDisabler {
160*0b57cec5SDimitry Andric   ScopedInterceptorDisabler() { DisableInThisThread(); }
161*0b57cec5SDimitry Andric   ~ScopedInterceptorDisabler() { EnableInThisThread(); }
162*0b57cec5SDimitry Andric };
163*0b57cec5SDimitry Andric 
164*0b57cec5SDimitry Andric // According to Itanium C++ ABI array cookie is a one word containing
165*0b57cec5SDimitry Andric // size of allocated array.
166*0b57cec5SDimitry Andric static inline bool IsItaniumABIArrayCookie(uptr chunk_beg, uptr chunk_size,
167*0b57cec5SDimitry Andric                                            uptr addr) {
168*0b57cec5SDimitry Andric   return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
169*0b57cec5SDimitry Andric          *reinterpret_cast<uptr *>(chunk_beg) == 0;
170*0b57cec5SDimitry Andric }
171*0b57cec5SDimitry Andric 
172*0b57cec5SDimitry Andric // According to ARM C++ ABI array cookie consists of two words:
173*0b57cec5SDimitry Andric // struct array_cookie {
174*0b57cec5SDimitry Andric //   std::size_t element_size; // element_size != 0
175*0b57cec5SDimitry Andric //   std::size_t element_count;
176*0b57cec5SDimitry Andric // };
177*0b57cec5SDimitry Andric static inline bool IsARMABIArrayCookie(uptr chunk_beg, uptr chunk_size,
178*0b57cec5SDimitry Andric                                        uptr addr) {
179*0b57cec5SDimitry Andric   return chunk_size == 2 * sizeof(uptr) && chunk_beg + chunk_size == addr &&
180*0b57cec5SDimitry Andric          *reinterpret_cast<uptr *>(chunk_beg + sizeof(uptr)) == 0;
181*0b57cec5SDimitry Andric }
182*0b57cec5SDimitry Andric 
183*0b57cec5SDimitry Andric // Special case for "new T[0]" where T is a type with DTOR.
184*0b57cec5SDimitry Andric // new T[0] will allocate a cookie (one or two words) for the array size (0)
185*0b57cec5SDimitry Andric // and store a pointer to the end of allocated chunk. The actual cookie layout
186*0b57cec5SDimitry Andric // varies between platforms according to their C++ ABI implementation.
187*0b57cec5SDimitry Andric inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
188*0b57cec5SDimitry Andric                                         uptr addr) {
189*0b57cec5SDimitry Andric #if defined(__arm__)
190*0b57cec5SDimitry Andric   return IsARMABIArrayCookie(chunk_beg, chunk_size, addr);
191*0b57cec5SDimitry Andric #else
192*0b57cec5SDimitry Andric   return IsItaniumABIArrayCookie(chunk_beg, chunk_size, addr);
193*0b57cec5SDimitry Andric #endif
194*0b57cec5SDimitry Andric }
195*0b57cec5SDimitry Andric 
196*0b57cec5SDimitry Andric // The following must be implemented in the parent tool.
197*0b57cec5SDimitry Andric 
198*0b57cec5SDimitry Andric void ForEachChunk(ForEachChunkCallback callback, void *arg);
199*0b57cec5SDimitry Andric // Returns the address range occupied by the global allocator object.
200*0b57cec5SDimitry Andric void GetAllocatorGlobalRange(uptr *begin, uptr *end);
201*0b57cec5SDimitry Andric // Wrappers for allocator's ForceLock()/ForceUnlock().
202*0b57cec5SDimitry Andric void LockAllocator();
203*0b57cec5SDimitry Andric void UnlockAllocator();
204*0b57cec5SDimitry Andric // Returns true if [addr, addr + sizeof(void *)) is poisoned.
205*0b57cec5SDimitry Andric bool WordIsPoisoned(uptr addr);
206*0b57cec5SDimitry Andric // Wrappers for ThreadRegistry access.
207*0b57cec5SDimitry Andric void LockThreadRegistry();
208*0b57cec5SDimitry Andric void UnlockThreadRegistry();
209*0b57cec5SDimitry Andric ThreadRegistry *GetThreadRegistryLocked();
210*0b57cec5SDimitry Andric bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
211*0b57cec5SDimitry Andric                            uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
212*0b57cec5SDimitry Andric                            uptr *cache_end, DTLS **dtls);
213*0b57cec5SDimitry Andric void ForEachExtraStackRange(tid_t os_id, RangeIteratorCallback callback,
214*0b57cec5SDimitry Andric                             void *arg);
215*0b57cec5SDimitry Andric // If called from the main thread, updates the main thread's TID in the thread
216*0b57cec5SDimitry Andric // registry. We need this to handle processes that fork() without a subsequent
217*0b57cec5SDimitry Andric // exec(), which invalidates the recorded TID. To update it, we must call
218*0b57cec5SDimitry Andric // gettid() from the main thread. Our solution is to call this function before
219*0b57cec5SDimitry Andric // leak checking and also before every call to pthread_create() (to handle cases
220*0b57cec5SDimitry Andric // where leak checking is initiated from a non-main thread).
221*0b57cec5SDimitry Andric void EnsureMainThreadIDIsCorrect();
222*0b57cec5SDimitry Andric // If p points into a chunk that has been allocated to the user, returns its
223*0b57cec5SDimitry Andric // user-visible address. Otherwise, returns 0.
224*0b57cec5SDimitry Andric uptr PointsIntoChunk(void *p);
225*0b57cec5SDimitry Andric // Returns address of user-visible chunk contained in this allocator chunk.
226*0b57cec5SDimitry Andric uptr GetUserBegin(uptr chunk);
227*0b57cec5SDimitry Andric // Helper for __lsan_ignore_object().
228*0b57cec5SDimitry Andric IgnoreObjectResult IgnoreObjectLocked(const void *p);
229*0b57cec5SDimitry Andric 
230*0b57cec5SDimitry Andric // Return the linker module, if valid for the platform.
231*0b57cec5SDimitry Andric LoadedModule *GetLinker();
232*0b57cec5SDimitry Andric 
233*0b57cec5SDimitry Andric // Return true if LSan has finished leak checking and reported leaks.
234*0b57cec5SDimitry Andric bool HasReportedLeaks();
235*0b57cec5SDimitry Andric 
236*0b57cec5SDimitry Andric // Run platform-specific leak handlers.
237*0b57cec5SDimitry Andric void HandleLeaks();
238*0b57cec5SDimitry Andric 
239*0b57cec5SDimitry Andric // Wrapper for chunk metadata operations.
240*0b57cec5SDimitry Andric class LsanMetadata {
241*0b57cec5SDimitry Andric  public:
242*0b57cec5SDimitry Andric   // Constructor accepts address of user-visible chunk.
243*0b57cec5SDimitry Andric   explicit LsanMetadata(uptr chunk);
244*0b57cec5SDimitry Andric   bool allocated() const;
245*0b57cec5SDimitry Andric   ChunkTag tag() const;
246*0b57cec5SDimitry Andric   void set_tag(ChunkTag value);
247*0b57cec5SDimitry Andric   uptr requested_size() const;
248*0b57cec5SDimitry Andric   u32 stack_trace_id() const;
249*0b57cec5SDimitry Andric  private:
250*0b57cec5SDimitry Andric   void *metadata_;
251*0b57cec5SDimitry Andric };
252*0b57cec5SDimitry Andric 
253*0b57cec5SDimitry Andric }  // namespace __lsan
254*0b57cec5SDimitry Andric 
255*0b57cec5SDimitry Andric extern "C" {
256*0b57cec5SDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
257*0b57cec5SDimitry Andric const char *__lsan_default_options();
258*0b57cec5SDimitry Andric 
259*0b57cec5SDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
260*0b57cec5SDimitry Andric int __lsan_is_turned_off();
261*0b57cec5SDimitry Andric 
262*0b57cec5SDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
263*0b57cec5SDimitry Andric const char *__lsan_default_suppressions();
264*0b57cec5SDimitry Andric }  // extern "C"
265*0b57cec5SDimitry Andric 
266*0b57cec5SDimitry Andric #endif  // LSAN_COMMON_H
267