1 //=-- lsan_interceptors.cc ------------------------------------------------===//
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 // Interceptors for standalone LSan.
13 //===----------------------------------------------------------------------===//
15 #include "interception/interception.h"
16 #include "sanitizer_common/sanitizer_allocator.h"
17 #include "sanitizer_common/sanitizer_atomic.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_flags.h"
20 #include "sanitizer_common/sanitizer_internal_defs.h"
21 #include "sanitizer_common/sanitizer_linux.h"
22 #include "sanitizer_common/sanitizer_platform_interceptors.h"
23 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
24 #include "sanitizer_common/sanitizer_posix.h"
25 #include "sanitizer_common/sanitizer_stackdepot.h"
26 #include "sanitizer_common/sanitizer_tls_get_addr.h"
28 #include "lsan_allocator.h"
29 #include "lsan_common.h"
30 #include "lsan_thread.h"
34 using namespace __lsan;
37 int pthread_attr_init(void *attr);
38 int pthread_attr_destroy(void *attr);
39 int pthread_attr_getdetachstate(void *attr, int *v);
40 int pthread_key_create(unsigned *key, void (*destructor)(void* v));
41 int pthread_setspecific(unsigned key, const void *v);
44 ///// Malloc/free interceptors. /////
51 INTERCEPTOR(void*, malloc, uptr size) {
53 GET_STACK_TRACE_MALLOC;
54 return lsan_malloc(size, stack);
57 INTERCEPTOR(void, free, void *p) {
62 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
63 if (lsan_init_is_running) {
64 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
65 const uptr kCallocPoolSize = 1024;
66 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
67 static uptr allocated;
68 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
69 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
70 allocated += size_in_words;
71 CHECK(allocated < kCallocPoolSize);
74 if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return nullptr;
76 GET_STACK_TRACE_MALLOC;
77 return lsan_calloc(nmemb, size, stack);
80 INTERCEPTOR(void*, realloc, void *q, uptr size) {
82 GET_STACK_TRACE_MALLOC;
83 return lsan_realloc(q, size, stack);
86 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
88 GET_STACK_TRACE_MALLOC;
89 *memptr = lsan_memalign(alignment, size, stack);
90 // FIXME: Return ENOMEM if user requested more than max alloc size.
94 INTERCEPTOR(void*, valloc, uptr size) {
96 GET_STACK_TRACE_MALLOC;
97 return lsan_valloc(size, stack);
101 static void BeforeFork() {
102 if (SANITIZER_LINUX) {
108 static void AfterFork() {
109 if (SANITIZER_LINUX) {
110 StackDepotUnlockAll();
115 INTERCEPTOR(int, fork, void) {
118 int pid = REAL(fork)();
123 #if SANITIZER_INTERCEPT_MEMALIGN
124 INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
126 GET_STACK_TRACE_MALLOC;
127 return lsan_memalign(alignment, size, stack);
129 #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
131 INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
133 GET_STACK_TRACE_MALLOC;
134 void *res = lsan_memalign(alignment, size, stack);
135 DTLS_on_libc_memalign(res, size);
138 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
140 #define LSAN_MAYBE_INTERCEPT_MEMALIGN
141 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
142 #endif // SANITIZER_INTERCEPT_MEMALIGN
144 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
145 INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
147 GET_STACK_TRACE_MALLOC;
148 return lsan_memalign(alignment, size, stack);
150 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
152 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
155 #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE
156 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
158 return GetMallocUsableSize(ptr);
160 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
161 INTERCEPT_FUNCTION(malloc_usable_size)
163 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
166 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
167 struct fake_mallinfo {
171 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
172 struct fake_mallinfo res;
173 internal_memset(&res, 0, sizeof(res));
176 #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
178 INTERCEPTOR(int, mallopt, int cmd, int value) {
181 #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
183 #define LSAN_MAYBE_INTERCEPT_MALLINFO
184 #define LSAN_MAYBE_INTERCEPT_MALLOPT
185 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
187 #if SANITIZER_INTERCEPT_PVALLOC
188 INTERCEPTOR(void*, pvalloc, uptr size) {
190 GET_STACK_TRACE_MALLOC;
191 uptr PageSize = GetPageSizeCached();
192 size = RoundUpTo(size, PageSize);
194 // pvalloc(0) should allocate one page.
197 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
199 #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
201 #define LSAN_MAYBE_INTERCEPT_PVALLOC
202 #endif // SANITIZER_INTERCEPT_PVALLOC
204 #if SANITIZER_INTERCEPT_CFREE
205 INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
206 #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
208 #define LSAN_MAYBE_INTERCEPT_CFREE
209 #endif // SANITIZER_INTERCEPT_CFREE
211 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
212 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
216 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
220 INTERCEPTOR(int, mprobe, void *ptr) {
223 #endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
225 #define OPERATOR_NEW_BODY \
226 ENSURE_LSAN_INITED; \
227 GET_STACK_TRACE_MALLOC; \
228 return Allocate(stack, size, 1, kAlwaysClearMemory);
230 INTERCEPTOR_ATTRIBUTE
231 void *operator new(size_t size) { OPERATOR_NEW_BODY; }
232 INTERCEPTOR_ATTRIBUTE
233 void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
234 INTERCEPTOR_ATTRIBUTE
235 void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
236 INTERCEPTOR_ATTRIBUTE
237 void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
239 #define OPERATOR_DELETE_BODY \
240 ENSURE_LSAN_INITED; \
243 INTERCEPTOR_ATTRIBUTE
244 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
245 INTERCEPTOR_ATTRIBUTE
246 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
247 INTERCEPTOR_ATTRIBUTE
248 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
249 INTERCEPTOR_ATTRIBUTE
250 void operator delete[](void *ptr, std::nothrow_t const &) {
251 OPERATOR_DELETE_BODY;
254 ///// Thread initialization and finalization. /////
256 static unsigned g_thread_finalize_key;
258 static void thread_finalize(void *v) {
261 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
262 Report("LeakSanitizer: failed to set thread key.\n");
271 void *(*callback)(void *arg);
273 atomic_uintptr_t tid;
276 extern "C" void *__lsan_thread_start_func(void *arg) {
277 ThreadParam *p = (ThreadParam*)arg;
278 void* (*callback)(void *arg) = p->callback;
279 void *param = p->param;
280 // Wait until the last iteration to maximize the chance that we are the last
281 // destructor to run.
282 if (pthread_setspecific(g_thread_finalize_key,
283 (void*)GetPthreadDestructorIterations())) {
284 Report("LeakSanitizer: failed to set thread key.\n");
288 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
289 internal_sched_yield();
290 SetCurrentThread(tid);
291 ThreadStart(tid, GetTid());
292 atomic_store(&p->tid, 0, memory_order_release);
293 return callback(param);
296 INTERCEPTOR(int, pthread_create, void *th, void *attr,
297 void *(*callback)(void *), void *param) {
299 EnsureMainThreadIDIsCorrect();
300 __sanitizer_pthread_attr_t myattr;
302 pthread_attr_init(&myattr);
305 AdjustStackSize(attr);
307 pthread_attr_getdetachstate(attr, &detached);
309 p.callback = callback;
311 atomic_store(&p.tid, 0, memory_order_relaxed);
314 // Ignore all allocations made by pthread_create: thread stack/TLS may be
315 // stored by pthread for future reuse even after thread destruction, and
316 // the linked list it's stored in doesn't even hold valid pointers to the
317 // objects, the latter are calculated by obscure pointer arithmetic.
318 ScopedInterceptorDisabler disabler;
319 res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
322 int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th,
323 IsStateDetached(detached));
325 atomic_store(&p.tid, tid, memory_order_release);
326 while (atomic_load(&p.tid, memory_order_acquire) != 0)
327 internal_sched_yield();
330 pthread_attr_destroy(&myattr);
334 INTERCEPTOR(int, pthread_join, void *th, void **ret) {
336 int tid = ThreadTid((uptr)th);
337 int res = REAL(pthread_join)(th, ret);
345 void InitializeInterceptors() {
346 INTERCEPT_FUNCTION(malloc);
347 INTERCEPT_FUNCTION(free);
348 LSAN_MAYBE_INTERCEPT_CFREE;
349 INTERCEPT_FUNCTION(calloc);
350 INTERCEPT_FUNCTION(realloc);
351 LSAN_MAYBE_INTERCEPT_MEMALIGN;
352 LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
353 LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
354 INTERCEPT_FUNCTION(posix_memalign);
355 INTERCEPT_FUNCTION(valloc);
356 LSAN_MAYBE_INTERCEPT_PVALLOC;
357 LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
358 LSAN_MAYBE_INTERCEPT_MALLINFO;
359 LSAN_MAYBE_INTERCEPT_MALLOPT;
360 INTERCEPT_FUNCTION(pthread_create);
361 INTERCEPT_FUNCTION(pthread_join);
362 INTERCEPT_FUNCTION(fork);
364 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
365 Report("LeakSanitizer: failed to create thread key.\n");
370 } // namespace __lsan