1 //=-- lsan_interceptors.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of LeakSanitizer.
10 // Interceptors for standalone LSan.
12 //===----------------------------------------------------------------------===//
14 #include "interception/interception.h"
15 #include "sanitizer_common/sanitizer_allocator.h"
16 #include "sanitizer_common/sanitizer_allocator_dlsym.h"
17 #include "sanitizer_common/sanitizer_allocator_report.h"
18 #include "sanitizer_common/sanitizer_atomic.h"
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_flags.h"
21 #include "sanitizer_common/sanitizer_internal_defs.h"
22 #include "sanitizer_common/sanitizer_linux.h"
23 #include "sanitizer_common/sanitizer_platform_interceptors.h"
24 #include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
25 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
27 #include "sanitizer_common/sanitizer_posix.h"
29 #include "sanitizer_common/sanitizer_tls_get_addr.h"
31 #include "lsan_allocator.h"
32 #include "lsan_common.h"
33 #include "lsan_thread.h"
37 using namespace __lsan
;
40 int pthread_attr_init(void *attr
);
41 int pthread_attr_destroy(void *attr
);
42 int pthread_attr_getdetachstate(void *attr
, int *v
);
43 int pthread_key_create(unsigned *key
, void (*destructor
)(void* v
));
44 int pthread_setspecific(unsigned key
, const void *v
);
47 struct DlsymAlloc
: DlSymAllocator
<DlsymAlloc
> {
48 static bool UseImpl() { return lsan_init_is_running
; }
49 static void OnAllocate(const void *ptr
, uptr size
) {
50 #if CAN_SANITIZE_LEAKS
51 // Suppress leaks from dlerror(). Previously dlsym hack on global array was
52 // used by leak sanitizer as a root region.
53 __lsan_register_root_region(ptr
, size
);
56 static void OnFree(const void *ptr
, uptr size
) {
57 #if CAN_SANITIZE_LEAKS
58 __lsan_unregister_root_region(ptr
, size
);
63 ///// Malloc/free interceptors. /////
67 enum class align_val_t
: size_t;
71 INTERCEPTOR(void*, malloc
, uptr size
) {
72 if (DlsymAlloc::Use())
73 return DlsymAlloc::Allocate(size
);
75 GET_STACK_TRACE_MALLOC
;
76 return lsan_malloc(size
, stack
);
79 INTERCEPTOR(void, free
, void *p
) {
80 if (DlsymAlloc::PointerIsMine(p
))
81 return DlsymAlloc::Free(p
);
86 INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
) {
87 if (DlsymAlloc::Use())
88 return DlsymAlloc::Callocate(nmemb
, size
);
90 GET_STACK_TRACE_MALLOC
;
91 return lsan_calloc(nmemb
, size
, stack
);
94 INTERCEPTOR(void *, realloc
, void *ptr
, uptr size
) {
95 if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr
))
96 return DlsymAlloc::Realloc(ptr
, size
);
98 GET_STACK_TRACE_MALLOC
;
99 return lsan_realloc(ptr
, size
, stack
);
102 INTERCEPTOR(void*, reallocarray
, void *q
, uptr nmemb
, uptr size
) {
104 GET_STACK_TRACE_MALLOC
;
105 return lsan_reallocarray(q
, nmemb
, size
, stack
);
108 INTERCEPTOR(int, posix_memalign
, void **memptr
, uptr alignment
, uptr size
) {
110 GET_STACK_TRACE_MALLOC
;
111 return lsan_posix_memalign(memptr
, alignment
, size
, stack
);
114 INTERCEPTOR(void*, valloc
, uptr size
) {
116 GET_STACK_TRACE_MALLOC
;
117 return lsan_valloc(size
, stack
);
119 #endif // !SANITIZER_APPLE
121 #if SANITIZER_INTERCEPT_MEMALIGN
122 INTERCEPTOR(void*, memalign
, uptr alignment
, uptr size
) {
124 GET_STACK_TRACE_MALLOC
;
125 return lsan_memalign(alignment
, size
, stack
);
127 #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
129 #define LSAN_MAYBE_INTERCEPT_MEMALIGN
130 #endif // SANITIZER_INTERCEPT_MEMALIGN
132 #if SANITIZER_INTERCEPT___LIBC_MEMALIGN
133 INTERCEPTOR(void *, __libc_memalign
, uptr alignment
, uptr size
) {
135 GET_STACK_TRACE_MALLOC
;
136 void *res
= lsan_memalign(alignment
, size
, stack
);
137 DTLS_on_libc_memalign(res
, size
);
140 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
142 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
143 #endif // SANITIZER_INTERCEPT___LIBC_MEMALIGN
145 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
146 INTERCEPTOR(void*, aligned_alloc
, uptr alignment
, uptr size
) {
148 GET_STACK_TRACE_MALLOC
;
149 return lsan_aligned_alloc(alignment
, size
, stack
);
151 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
153 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
156 #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE
157 INTERCEPTOR(uptr
, malloc_usable_size
, void *ptr
) {
159 return GetMallocUsableSize(ptr
);
161 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
162 INTERCEPT_FUNCTION(malloc_usable_size)
164 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
167 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
168 struct fake_mallinfo
{
172 INTERCEPTOR(struct fake_mallinfo
, mallinfo
, void) {
173 struct fake_mallinfo res
;
174 internal_memset(&res
, 0, sizeof(res
));
177 #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
179 INTERCEPTOR(int, mallopt
, int cmd
, int value
) {
182 #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
184 #define LSAN_MAYBE_INTERCEPT_MALLINFO
185 #define LSAN_MAYBE_INTERCEPT_MALLOPT
186 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
188 #if SANITIZER_INTERCEPT_PVALLOC
189 INTERCEPTOR(void*, pvalloc
, uptr size
) {
191 GET_STACK_TRACE_MALLOC
;
192 return lsan_pvalloc(size
, stack
);
194 #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
196 #define LSAN_MAYBE_INTERCEPT_PVALLOC
197 #endif // SANITIZER_INTERCEPT_PVALLOC
199 #if SANITIZER_INTERCEPT_CFREE
200 INTERCEPTOR(void, cfree
, void *p
) ALIAS(WRAPPER_NAME(free
));
201 #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
203 #define LSAN_MAYBE_INTERCEPT_CFREE
204 #endif // SANITIZER_INTERCEPT_CFREE
206 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
207 INTERCEPTOR(int, mcheck
, void (*abortfunc
)(int mstatus
)) {
211 INTERCEPTOR(int, mcheck_pedantic
, void (*abortfunc
)(int mstatus
)) {
215 INTERCEPTOR(int, mprobe
, void *ptr
) {
218 #endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
221 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
222 #define OPERATOR_NEW_BODY(nothrow)\
224 GET_STACK_TRACE_MALLOC;\
225 void *res = lsan_malloc(size, stack);\
226 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
228 #define OPERATOR_NEW_BODY_ALIGN(nothrow)\
230 GET_STACK_TRACE_MALLOC;\
231 void *res = lsan_memalign((uptr)align, size, stack);\
232 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
235 #define OPERATOR_DELETE_BODY\
239 // On OS X it's not enough to just provide our own 'operator new' and
240 // 'operator delete' implementations, because they're going to be in the runtime
241 // dylib, and the main executable will depend on both the runtime dylib and
242 // libstdc++, each of has its implementation of new and delete.
243 // To make sure that C++ allocation/deallocation operators are overridden on
244 // OS X we need to intercept them using their mangled names.
247 INTERCEPTOR_ATTRIBUTE
248 void *operator new(size_t size
) { OPERATOR_NEW_BODY(false /*nothrow*/); }
249 INTERCEPTOR_ATTRIBUTE
250 void *operator new[](size_t size
) { OPERATOR_NEW_BODY(false /*nothrow*/); }
251 INTERCEPTOR_ATTRIBUTE
252 void *operator new(size_t size
, std::nothrow_t
const&)
253 { OPERATOR_NEW_BODY(true /*nothrow*/); }
254 INTERCEPTOR_ATTRIBUTE
255 void *operator new[](size_t size
, std::nothrow_t
const&)
256 { OPERATOR_NEW_BODY(true /*nothrow*/); }
257 INTERCEPTOR_ATTRIBUTE
258 void *operator new(size_t size
, std::align_val_t align
)
259 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
260 INTERCEPTOR_ATTRIBUTE
261 void *operator new[](size_t size
, std::align_val_t align
)
262 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
263 INTERCEPTOR_ATTRIBUTE
264 void *operator new(size_t size
, std::align_val_t align
, std::nothrow_t
const&)
265 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
266 INTERCEPTOR_ATTRIBUTE
267 void *operator new[](size_t size
, std::align_val_t align
, std::nothrow_t
const&)
268 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
270 INTERCEPTOR_ATTRIBUTE
271 void operator delete(void *ptr
) NOEXCEPT
{ OPERATOR_DELETE_BODY
; }
272 INTERCEPTOR_ATTRIBUTE
273 void operator delete[](void *ptr
) NOEXCEPT
{ OPERATOR_DELETE_BODY
; }
274 INTERCEPTOR_ATTRIBUTE
275 void operator delete(void *ptr
, std::nothrow_t
const&) { OPERATOR_DELETE_BODY
; }
276 INTERCEPTOR_ATTRIBUTE
277 void operator delete[](void *ptr
, std::nothrow_t
const &)
278 { OPERATOR_DELETE_BODY
; }
279 INTERCEPTOR_ATTRIBUTE
280 void operator delete(void *ptr
, size_t size
) NOEXCEPT
281 { OPERATOR_DELETE_BODY
; }
282 INTERCEPTOR_ATTRIBUTE
283 void operator delete[](void *ptr
, size_t size
) NOEXCEPT
284 { OPERATOR_DELETE_BODY
; }
285 INTERCEPTOR_ATTRIBUTE
286 void operator delete(void *ptr
, std::align_val_t
) NOEXCEPT
287 { OPERATOR_DELETE_BODY
; }
288 INTERCEPTOR_ATTRIBUTE
289 void operator delete[](void *ptr
, std::align_val_t
) NOEXCEPT
290 { OPERATOR_DELETE_BODY
; }
291 INTERCEPTOR_ATTRIBUTE
292 void operator delete(void *ptr
, std::align_val_t
, std::nothrow_t
const&)
293 { OPERATOR_DELETE_BODY
; }
294 INTERCEPTOR_ATTRIBUTE
295 void operator delete[](void *ptr
, std::align_val_t
, std::nothrow_t
const&)
296 { OPERATOR_DELETE_BODY
; }
297 INTERCEPTOR_ATTRIBUTE
298 void operator delete(void *ptr
, size_t size
, std::align_val_t
) NOEXCEPT
299 { OPERATOR_DELETE_BODY
; }
300 INTERCEPTOR_ATTRIBUTE
301 void operator delete[](void *ptr
, size_t size
, std::align_val_t
) NOEXCEPT
302 { OPERATOR_DELETE_BODY
; }
304 #else // SANITIZER_APPLE
306 INTERCEPTOR(void *, _Znwm
, size_t size
)
307 { OPERATOR_NEW_BODY(false /*nothrow*/); }
308 INTERCEPTOR(void *, _Znam
, size_t size
)
309 { OPERATOR_NEW_BODY(false /*nothrow*/); }
310 INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t
, size_t size
, std::nothrow_t
const&)
311 { OPERATOR_NEW_BODY(true /*nothrow*/); }
312 INTERCEPTOR(void *, _ZnamRKSt9nothrow_t
, size_t size
, std::nothrow_t
const&)
313 { OPERATOR_NEW_BODY(true /*nothrow*/); }
315 INTERCEPTOR(void, _ZdlPv
, void *ptr
)
316 { OPERATOR_DELETE_BODY
; }
317 INTERCEPTOR(void, _ZdaPv
, void *ptr
)
318 { OPERATOR_DELETE_BODY
; }
319 INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t
, void *ptr
, std::nothrow_t
const&)
320 { OPERATOR_DELETE_BODY
; }
321 INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t
, void *ptr
, std::nothrow_t
const&)
322 { OPERATOR_DELETE_BODY
; }
324 #endif // !SANITIZER_APPLE
327 ///// Thread initialization and finalization. /////
329 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD && !SANITIZER_FUCHSIA
330 static unsigned g_thread_finalize_key
;
332 static void thread_finalize(void *v
) {
335 if (pthread_setspecific(g_thread_finalize_key
, (void*)(iter
- 1))) {
336 Report("LeakSanitizer: failed to set thread key.\n");
346 INTERCEPTOR(void, _lwp_exit
) {
351 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT INTERCEPT_FUNCTION(_lwp_exit)
353 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT
356 #if SANITIZER_INTERCEPT_THR_EXIT
357 INTERCEPTOR(void, thr_exit
, tid_t
*state
) {
360 REAL(thr_exit
)(state
);
362 #define LSAN_MAYBE_INTERCEPT_THR_EXIT INTERCEPT_FUNCTION(thr_exit)
364 #define LSAN_MAYBE_INTERCEPT_THR_EXIT
367 #if SANITIZER_INTERCEPT___CXA_ATEXIT
368 INTERCEPTOR(int, __cxa_atexit
, void (*func
)(void *), void *arg
,
370 __lsan::ScopedInterceptorDisabler disabler
;
371 return REAL(__cxa_atexit
)(func
, arg
, dso_handle
);
373 #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT INTERCEPT_FUNCTION(__cxa_atexit)
375 #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT
378 #if SANITIZER_INTERCEPT_ATEXIT
379 INTERCEPTOR(int, atexit
, void (*f
)()) {
380 __lsan::ScopedInterceptorDisabler disabler
;
381 return REAL(__cxa_atexit
)((void (*)(void *a
))f
, 0, 0);
383 #define LSAN_MAYBE_INTERCEPT_ATEXIT INTERCEPT_FUNCTION(atexit)
385 #define LSAN_MAYBE_INTERCEPT_ATEXIT
388 #if SANITIZER_INTERCEPT_PTHREAD_ATFORK
390 extern int _pthread_atfork(void (*prepare
)(), void (*parent
)(),
394 INTERCEPTOR(int, pthread_atfork
, void (*prepare
)(), void (*parent
)(),
396 __lsan::ScopedInterceptorDisabler disabler
;
397 // REAL(pthread_atfork) cannot be called due to symbol indirections at least
399 return _pthread_atfork(prepare
, parent
, child
);
401 #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK INTERCEPT_FUNCTION(pthread_atfork)
403 #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK
406 #if SANITIZER_INTERCEPT_STRERROR
407 INTERCEPTOR(char *, strerror
, int errnum
) {
408 __lsan::ScopedInterceptorDisabler disabler
;
409 return REAL(strerror
)(errnum
);
411 #define LSAN_MAYBE_INTERCEPT_STRERROR INTERCEPT_FUNCTION(strerror)
413 #define LSAN_MAYBE_INTERCEPT_STRERROR
419 void *(*callback
)(void *arg
);
421 atomic_uintptr_t tid
;
424 extern "C" void *__lsan_thread_start_func(void *arg
) {
425 ThreadParam
*p
= (ThreadParam
*)arg
;
426 void* (*callback
)(void *arg
) = p
->callback
;
427 void *param
= p
->param
;
428 // Wait until the last iteration to maximize the chance that we are the last
429 // destructor to run.
430 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
431 if (pthread_setspecific(g_thread_finalize_key
,
432 (void*)GetPthreadDestructorIterations())) {
433 Report("LeakSanitizer: failed to set thread key.\n");
438 while ((tid
= atomic_load(&p
->tid
, memory_order_acquire
)) == 0)
439 internal_sched_yield();
440 ThreadStart(tid
, GetTid());
441 atomic_store(&p
->tid
, 0, memory_order_release
);
442 return callback(param
);
445 INTERCEPTOR(int, pthread_create
, void *th
, void *attr
,
446 void *(*callback
)(void *), void *param
) {
448 EnsureMainThreadIDIsCorrect();
449 __sanitizer_pthread_attr_t myattr
;
451 pthread_attr_init(&myattr
);
454 AdjustStackSize(attr
);
456 pthread_attr_getdetachstate(attr
, &detached
);
458 p
.callback
= callback
;
460 atomic_store(&p
.tid
, 0, memory_order_relaxed
);
463 // Ignore all allocations made by pthread_create: thread stack/TLS may be
464 // stored by pthread for future reuse even after thread destruction, and
465 // the linked list it's stored in doesn't even hold valid pointers to the
466 // objects, the latter are calculated by obscure pointer arithmetic.
467 ScopedInterceptorDisabler disabler
;
468 res
= REAL(pthread_create
)(th
, attr
, __lsan_thread_start_func
, &p
);
471 int tid
= ThreadCreate(GetCurrentThreadId(), IsStateDetached(detached
));
472 CHECK_NE(tid
, kMainTid
);
473 atomic_store(&p
.tid
, tid
, memory_order_release
);
474 while (atomic_load(&p
.tid
, memory_order_acquire
) != 0)
475 internal_sched_yield();
478 pthread_attr_destroy(&myattr
);
482 INTERCEPTOR(int, pthread_join
, void *t
, void **arg
) {
483 return REAL(pthread_join
)(t
, arg
);
486 DEFINE_REAL_PTHREAD_FUNCTIONS
488 INTERCEPTOR(void, _exit
, int status
) {
489 if (status
== 0 && HasReportedLeaks()) status
= common_flags()->exitcode
;
493 #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
494 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
496 #endif // SANITIZER_POSIX
500 void InitializeInterceptors() {
501 // Fuchsia doesn't use interceptors that require any setup.
502 #if !SANITIZER_FUCHSIA
503 InitializeSignalInterceptors();
505 INTERCEPT_FUNCTION(malloc
);
506 INTERCEPT_FUNCTION(free
);
507 LSAN_MAYBE_INTERCEPT_CFREE
;
508 INTERCEPT_FUNCTION(calloc
);
509 INTERCEPT_FUNCTION(realloc
);
510 LSAN_MAYBE_INTERCEPT_MEMALIGN
;
511 LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
;
512 LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
;
513 INTERCEPT_FUNCTION(posix_memalign
);
514 INTERCEPT_FUNCTION(valloc
);
515 LSAN_MAYBE_INTERCEPT_PVALLOC
;
516 LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
;
517 LSAN_MAYBE_INTERCEPT_MALLINFO
;
518 LSAN_MAYBE_INTERCEPT_MALLOPT
;
519 INTERCEPT_FUNCTION(pthread_create
);
520 INTERCEPT_FUNCTION(pthread_join
);
521 INTERCEPT_FUNCTION(_exit
);
523 LSAN_MAYBE_INTERCEPT__LWP_EXIT
;
524 LSAN_MAYBE_INTERCEPT_THR_EXIT
;
526 LSAN_MAYBE_INTERCEPT___CXA_ATEXIT
;
527 LSAN_MAYBE_INTERCEPT_ATEXIT
;
528 LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK
;
530 LSAN_MAYBE_INTERCEPT_STRERROR
;
532 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
533 if (pthread_key_create(&g_thread_finalize_key
, &thread_finalize
)) {
534 Report("LeakSanitizer: failed to create thread key.\n");
539 #endif // !SANITIZER_FUCHSIA
542 } // namespace __lsan