1//=-- lsan_common.h -------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of LeakSanitizer.
10// Private LSan header.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LSAN_COMMON_H
15#define LSAN_COMMON_H
16
17#include "sanitizer_common/sanitizer_allocator.h"
18#include "sanitizer_common/sanitizer_common.h"
19#include "sanitizer_common/sanitizer_internal_defs.h"
20#include "sanitizer_common/sanitizer_platform.h"
21#include "sanitizer_common/sanitizer_range.h"
22#include "sanitizer_common/sanitizer_stackdepot.h"
23#include "sanitizer_common/sanitizer_stoptheworld.h"
24#include "sanitizer_common/sanitizer_symbolizer.h"
25#include "sanitizer_common/sanitizer_thread_registry.h"
26
27// LeakSanitizer relies on some Glibc's internals (e.g. TLS machinery) on Linux.
28// Also, LSan doesn't like 32 bit architectures
29// because of "small" (4 bytes) pointer size that leads to high false negative
30// ratio on large leaks. But we still want to have it for some 32 bit arches
31// (e.g. x86), see https://github.com/google/sanitizers/issues/403.
32// To enable LeakSanitizer on a new architecture, one needs to implement the
33// internal_clone function as well as (probably) adjust the TLS machinery for
34// the new architecture inside the sanitizer library.
35// Exclude leak-detection on arm32 for Android because `__aeabi_read_tp`
36// is missing. This caused a link error.
37#if SANITIZER_ANDROID && (__ANDROID_API__ < 28 || defined(__arm__))
38# define CAN_SANITIZE_LEAKS 0
39#elif (SANITIZER_LINUX || SANITIZER_APPLE) && (SANITIZER_WORDSIZE == 64) && \
40 (defined(__x86_64__) || defined(__mips64) || defined(__aarch64__) || \
41 defined(__powerpc64__) || defined(__s390x__))
42# define CAN_SANITIZE_LEAKS 1
43#elif defined(__i386__) && (SANITIZER_LINUX || SANITIZER_APPLE)
44# define CAN_SANITIZE_LEAKS 1
45#elif defined(__arm__) && SANITIZER_LINUX
46# define CAN_SANITIZE_LEAKS 1
47#elif defined(__hexagon__) && SANITIZER_LINUX
48# define CAN_SANITIZE_LEAKS 1
49#elif SANITIZER_LOONGARCH64 && SANITIZER_LINUX
50# define CAN_SANITIZE_LEAKS 1
51#elif SANITIZER_RISCV64 && SANITIZER_LINUX
52# define CAN_SANITIZE_LEAKS 1
53#elif SANITIZER_NETBSD || SANITIZER_FUCHSIA
54# define CAN_SANITIZE_LEAKS 1
55#else
56# define CAN_SANITIZE_LEAKS 0
57#endif
58
59namespace __sanitizer {
60class FlagParser;
61class ThreadRegistry;
62class ThreadContextBase;
63struct DTLS;
64}
65
66// This section defines function and class prototypes which must be implemented
67// by the parent tool linking in LSan. There are implementations provided by the
68// LSan library which will be linked in when LSan is used as a standalone tool.
69namespace __lsan {
70
71// Chunk tags.
72enum ChunkTag {
73 kDirectlyLeaked = 0, // default
74 kIndirectlyLeaked = 1,
75 kReachable = 2,
76 kIgnored = 3
77};
78
79enum IgnoreObjectResult {
80 kIgnoreObjectSuccess,
81 kIgnoreObjectAlreadyIgnored,
82 kIgnoreObjectInvalid
83};
84
85//// --------------------------------------------------------------------------
86//// Poisoning prototypes.
87//// --------------------------------------------------------------------------
88
89// Returns true if [addr, addr + sizeof(void *)) is poisoned.
90bool WordIsPoisoned(uptr addr);
91
92//// --------------------------------------------------------------------------
93//// Thread prototypes.
94//// --------------------------------------------------------------------------
95
96// Wrappers for ThreadRegistry access.
97void LockThreads() SANITIZER_NO_THREAD_SAFETY_ANALYSIS;
98void UnlockThreads() SANITIZER_NO_THREAD_SAFETY_ANALYSIS;
99// If called from the main thread, updates the main thread's TID in the thread
100// registry. We need this to handle processes that fork() without a subsequent
101// exec(), which invalidates the recorded TID. To update it, we must call
102// gettid() from the main thread. Our solution is to call this function before
103// leak checking and also before every call to pthread_create() (to handle cases
104// where leak checking is initiated from a non-main thread).
105void EnsureMainThreadIDIsCorrect();
106
107bool GetThreadRangesLocked(ThreadID os_id, uptr *stack_begin, uptr *stack_end,
108 uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
109 uptr *cache_end, DTLS **dtls);
110void GetAllThreadAllocatorCachesLocked(InternalMmapVector<uptr> *caches);
111void GetThreadExtraStackRangesLocked(InternalMmapVector<Range> *ranges);
112void GetThreadExtraStackRangesLocked(ThreadID os_id,
113 InternalMmapVector<Range> *ranges);
114void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs);
115void GetRunningThreadsLocked(InternalMmapVector<ThreadID> *threads);
116void PrintThreads();
117
118//// --------------------------------------------------------------------------
119//// Allocator prototypes.
120//// --------------------------------------------------------------------------
121
122// Wrappers for allocator's ForceLock()/ForceUnlock().
123void LockAllocator();
124void UnlockAllocator();
125
126// Lock/unlock global mutext.
127void LockGlobal();
128void UnlockGlobal();
129
130// Returns the address range occupied by the global allocator object.
131void GetAllocatorGlobalRange(uptr *begin, uptr *end);
132// If p points into a chunk that has been allocated to the user, returns its
133// user-visible address. Otherwise, returns 0.
134uptr PointsIntoChunk(void *p);
135// Returns address of user-visible chunk contained in this allocator chunk.
136uptr GetUserBegin(uptr chunk);
137// Returns user-visible address for chunk. If memory tagging is used this
138// function will return the tagged address.
139uptr GetUserAddr(uptr chunk);
140
141// Wrapper for chunk metadata operations.
142class LsanMetadata {
143 public:
144 // Constructor accepts address of user-visible chunk.
145 explicit LsanMetadata(uptr chunk);
146 bool allocated() const;
147 ChunkTag tag() const;
148 void set_tag(ChunkTag value);
149 uptr requested_size() const;
150 u32 stack_trace_id() const;
151
152 private:
153 void *metadata_;
154};
155
156// Iterate over all existing chunks. Allocator must be locked.
157void ForEachChunk(ForEachChunkCallback callback, void *arg);
158
159// Helper for __lsan_ignore_object().
160IgnoreObjectResult IgnoreObject(const void *p);
161
162// The rest of the LSan interface which is implemented by library.
163
164struct ScopedStopTheWorldLock {
165 ScopedStopTheWorldLock() {
166 LockThreads();
167 LockAllocator();
168 }
169
170 ~ScopedStopTheWorldLock() {
171 UnlockAllocator();
172 UnlockThreads();
173 }
174
175 ScopedStopTheWorldLock &operator=(const ScopedStopTheWorldLock &) = delete;
176 ScopedStopTheWorldLock(const ScopedStopTheWorldLock &) = delete;
177};
178
179struct Flags {
180#define LSAN_FLAG(Type, Name, DefaultValue, Description) Type Name;
181#include "lsan_flags.inc"
182#undef LSAN_FLAG
183
184 void SetDefaults();
185 uptr pointer_alignment() const {
186 return use_unaligned ? 1 : sizeof(uptr);
187 }
188};
189
190extern Flags lsan_flags;
191inline Flags *flags() { return &lsan_flags; }
192void RegisterLsanFlags(FlagParser *parser, Flags *f);
193
194struct LeakedChunk {
195 uptr chunk;
196 u32 stack_trace_id;
197 uptr leaked_size;
198 ChunkTag tag;
199};
200
201using LeakedChunks = InternalMmapVector<LeakedChunk>;
202
203struct Leak {
204 u32 id;
205 uptr hit_count;
206 uptr total_size;
207 u32 stack_trace_id;
208 bool is_directly_leaked;
209 bool is_suppressed;
210};
211
212struct LeakedObject {
213 u32 leak_id;
214 uptr addr;
215 uptr size;
216};
217
218// Aggregates leaks by stack trace prefix.
219class LeakReport {
220 public:
221 LeakReport() {}
222 void AddLeakedChunks(const LeakedChunks &chunks);
223 void ReportTopLeaks(uptr max_leaks);
224 void PrintSummary();
225 uptr ApplySuppressions();
226 uptr UnsuppressedLeakCount();
227 uptr IndirectUnsuppressedLeakCount();
228
229 private:
230 void PrintReportForLeak(uptr index);
231 void PrintLeakedObjectsForLeak(uptr index);
232
233 u32 next_id_ = 0;
234 InternalMmapVector<Leak> leaks_;
235 InternalMmapVector<LeakedObject> leaked_objects_;
236};
237
238typedef InternalMmapVector<uptr> Frontier;
239
240// Platform-specific functions.
241void InitializePlatformSpecificModules();
242void ProcessGlobalRegions(Frontier *frontier);
243void ProcessPlatformSpecificAllocations(Frontier *frontier);
244
245// LockStuffAndStopTheWorld can start to use Scan* calls to collect into
246// this Frontier vector before the StopTheWorldCallback actually runs.
247// This is used when the OS has a unified callback API for suspending
248// threads and enumerating roots.
249struct CheckForLeaksParam {
250 Frontier frontier;
251 LeakedChunks leaks;
252 ThreadID caller_tid;
253 uptr caller_sp;
254 bool success = false;
255};
256
257using Region = Range;
258
259bool HasRootRegions();
260void ScanRootRegions(Frontier *frontier,
261 const InternalMmapVectorNoCtor<Region> &region);
262// Run stoptheworld while holding any platform-specific locks, as well as the
263// allocator and thread registry locks.
264void LockStuffAndStopTheWorld(StopTheWorldCallback callback,
265 CheckForLeaksParam* argument);
266
267void ScanRangeForPointers(uptr begin, uptr end,
268 Frontier *frontier,
269 const char *region_type, ChunkTag tag);
270void ScanGlobalRange(uptr begin, uptr end, Frontier *frontier);
271void ScanExtraStackRanges(const InternalMmapVector<Range> &ranges,
272 Frontier *frontier);
273
274// Functions called from the parent tool.
275const char *MaybeCallLsanDefaultOptions();
276void InitCommonLsan();
277void DoLeakCheck();
278void DoRecoverableLeakCheckVoid();
279void DisableCounterUnderflow();
280bool DisabledInThisThread();
281
282// Used to implement __lsan::ScopedDisabler.
283void DisableInThisThread();
284void EnableInThisThread();
285// Can be used to ignore memory allocated by an intercepted
286// function.
287struct ScopedInterceptorDisabler {
288 ScopedInterceptorDisabler() { DisableInThisThread(); }
289 ~ScopedInterceptorDisabler() { EnableInThisThread(); }
290};
291
292// According to Itanium C++ ABI array cookie is a one word containing
293// size of allocated array.
294static inline bool IsItaniumABIArrayCookie(uptr chunk_beg, uptr chunk_size,
295 uptr addr) {
296 return chunk_size == sizeof(uptr) && chunk_beg + chunk_size == addr &&
297 *reinterpret_cast<uptr *>(chunk_beg) == 0;
298}
299
300// According to ARM C++ ABI array cookie consists of two words:
301// struct array_cookie {
302// std::size_t element_size; // element_size != 0
303// std::size_t element_count;
304// };
305static inline bool IsARMABIArrayCookie(uptr chunk_beg, uptr chunk_size,
306 uptr addr) {
307 return chunk_size == 2 * sizeof(uptr) && chunk_beg + chunk_size == addr &&
308 *reinterpret_cast<uptr *>(chunk_beg + sizeof(uptr)) == 0;
309}
310
311// Special case for "new T[0]" where T is a type with DTOR.
312// new T[0] will allocate a cookie (one or two words) for the array size (0)
313// and store a pointer to the end of allocated chunk. The actual cookie layout
314// varies between platforms according to their C++ ABI implementation.
315inline bool IsSpecialCaseOfOperatorNew0(uptr chunk_beg, uptr chunk_size,
316 uptr addr) {
317#if defined(__arm__)
318 return IsARMABIArrayCookie(chunk_beg, chunk_size, addr);
319#else
320 return IsItaniumABIArrayCookie(chunk_beg, chunk_size, addr);
321#endif
322}
323
324// Return the linker module, if valid for the platform.
325LoadedModule *GetLinker();
326
327// Return true if LSan has finished leak checking and reported leaks.
328bool HasReportedLeaks();
329
330// Run platform-specific leak handlers.
331void HandleLeaks();
332
333} // namespace __lsan
334
335extern "C" {
336SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
337const char *__lsan_default_options();
338
339SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
340int __lsan_is_turned_off();
341
342SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
343const char *__lsan_default_suppressions();
344
345SANITIZER_INTERFACE_ATTRIBUTE
346void __lsan_register_root_region(const void *p, __lsan::uptr size);
347
348SANITIZER_INTERFACE_ATTRIBUTE
349void __lsan_unregister_root_region(const void *p, __lsan::uptr size);
350
351} // extern "C"
352
353#endif // LSAN_COMMON_H
354