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_report.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_netbsd.h"
24 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
26 #include "sanitizer_common/sanitizer_posix.h"
28 #include "sanitizer_common/sanitizer_tls_get_addr.h"
30 #include "lsan_allocator.h"
31 #include "lsan_common.h"
32 #include "lsan_thread.h"
36 using namespace __lsan
;
39 int pthread_attr_init(void *attr
);
40 int pthread_attr_destroy(void *attr
);
41 int pthread_attr_getdetachstate(void *attr
, int *v
);
42 int pthread_key_create(unsigned *key
, void (*destructor
)(void* v
));
43 int pthread_setspecific(unsigned key
, const void *v
);
46 ///// Malloc/free interceptors. /////
50 enum class align_val_t
: size_t;
54 INTERCEPTOR(void*, malloc
, uptr size
) {
56 GET_STACK_TRACE_MALLOC
;
57 return lsan_malloc(size
, stack
);
60 INTERCEPTOR(void, free
, void *p
) {
65 INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
) {
66 // This hack is not required for Fuchsia because there are no dlsym calls
67 // involved in setting up interceptors.
68 #if !SANITIZER_FUCHSIA
69 if (lsan_init_is_running
) {
70 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
71 const uptr kCallocPoolSize
= 1024;
72 static uptr calloc_memory_for_dlsym
[kCallocPoolSize
];
73 static uptr allocated
;
74 uptr size_in_words
= ((nmemb
* size
) + kWordSize
- 1) / kWordSize
;
75 void *mem
= (void*)&calloc_memory_for_dlsym
[allocated
];
76 allocated
+= size_in_words
;
77 CHECK(allocated
< kCallocPoolSize
);
80 #endif // !SANITIZER_FUCHSIA
82 GET_STACK_TRACE_MALLOC
;
83 return lsan_calloc(nmemb
, size
, stack
);
86 INTERCEPTOR(void*, realloc
, void *q
, uptr size
) {
88 GET_STACK_TRACE_MALLOC
;
89 return lsan_realloc(q
, size
, stack
);
92 INTERCEPTOR(void*, reallocarray
, void *q
, uptr nmemb
, uptr size
) {
94 GET_STACK_TRACE_MALLOC
;
95 return lsan_reallocarray(q
, nmemb
, size
, stack
);
98 INTERCEPTOR(int, posix_memalign
, void **memptr
, uptr alignment
, uptr size
) {
100 GET_STACK_TRACE_MALLOC
;
101 return lsan_posix_memalign(memptr
, alignment
, size
, stack
);
104 INTERCEPTOR(void*, valloc
, uptr size
) {
106 GET_STACK_TRACE_MALLOC
;
107 return lsan_valloc(size
, stack
);
109 #endif // !SANITIZER_MAC
111 #if SANITIZER_INTERCEPT_MEMALIGN
112 INTERCEPTOR(void*, memalign
, uptr alignment
, uptr size
) {
114 GET_STACK_TRACE_MALLOC
;
115 return lsan_memalign(alignment
, size
, stack
);
117 #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
119 #define LSAN_MAYBE_INTERCEPT_MEMALIGN
120 #endif // SANITIZER_INTERCEPT_MEMALIGN
122 #if SANITIZER_INTERCEPT___LIBC_MEMALIGN
123 INTERCEPTOR(void *, __libc_memalign
, uptr alignment
, uptr size
) {
125 GET_STACK_TRACE_MALLOC
;
126 void *res
= lsan_memalign(alignment
, size
, stack
);
127 DTLS_on_libc_memalign(res
, size
);
130 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
132 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
133 #endif // SANITIZER_INTERCEPT___LIBC_MEMALIGN
135 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
136 INTERCEPTOR(void*, aligned_alloc
, uptr alignment
, uptr size
) {
138 GET_STACK_TRACE_MALLOC
;
139 return lsan_aligned_alloc(alignment
, size
, stack
);
141 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
143 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
146 #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE
147 INTERCEPTOR(uptr
, malloc_usable_size
, void *ptr
) {
149 return GetMallocUsableSize(ptr
);
151 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
152 INTERCEPT_FUNCTION(malloc_usable_size)
154 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
157 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
158 struct fake_mallinfo
{
162 INTERCEPTOR(struct fake_mallinfo
, mallinfo
, void) {
163 struct fake_mallinfo res
;
164 internal_memset(&res
, 0, sizeof(res
));
167 #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
169 INTERCEPTOR(int, mallopt
, int cmd
, int value
) {
172 #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
174 #define LSAN_MAYBE_INTERCEPT_MALLINFO
175 #define LSAN_MAYBE_INTERCEPT_MALLOPT
176 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
178 #if SANITIZER_INTERCEPT_PVALLOC
179 INTERCEPTOR(void*, pvalloc
, uptr size
) {
181 GET_STACK_TRACE_MALLOC
;
182 return lsan_pvalloc(size
, stack
);
184 #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
186 #define LSAN_MAYBE_INTERCEPT_PVALLOC
187 #endif // SANITIZER_INTERCEPT_PVALLOC
189 #if SANITIZER_INTERCEPT_CFREE
190 INTERCEPTOR(void, cfree
, void *p
) ALIAS(WRAPPER_NAME(free
));
191 #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
193 #define LSAN_MAYBE_INTERCEPT_CFREE
194 #endif // SANITIZER_INTERCEPT_CFREE
196 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
197 INTERCEPTOR(int, mcheck
, void (*abortfunc
)(int mstatus
)) {
201 INTERCEPTOR(int, mcheck_pedantic
, void (*abortfunc
)(int mstatus
)) {
205 INTERCEPTOR(int, mprobe
, void *ptr
) {
208 #endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
211 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
212 #define OPERATOR_NEW_BODY(nothrow)\
214 GET_STACK_TRACE_MALLOC;\
215 void *res = lsan_malloc(size, stack);\
216 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
218 #define OPERATOR_NEW_BODY_ALIGN(nothrow)\
220 GET_STACK_TRACE_MALLOC;\
221 void *res = lsan_memalign((uptr)align, size, stack);\
222 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
225 #define OPERATOR_DELETE_BODY\
229 // On OS X it's not enough to just provide our own 'operator new' and
230 // 'operator delete' implementations, because they're going to be in the runtime
231 // dylib, and the main executable will depend on both the runtime dylib and
232 // libstdc++, each of has its implementation of new and delete.
233 // To make sure that C++ allocation/deallocation operators are overridden on
234 // OS X we need to intercept them using their mangled names.
237 INTERCEPTOR_ATTRIBUTE
238 void *operator new(size_t size
) { OPERATOR_NEW_BODY(false /*nothrow*/); }
239 INTERCEPTOR_ATTRIBUTE
240 void *operator new[](size_t size
) { OPERATOR_NEW_BODY(false /*nothrow*/); }
241 INTERCEPTOR_ATTRIBUTE
242 void *operator new(size_t size
, std::nothrow_t
const&)
243 { OPERATOR_NEW_BODY(true /*nothrow*/); }
244 INTERCEPTOR_ATTRIBUTE
245 void *operator new[](size_t size
, std::nothrow_t
const&)
246 { OPERATOR_NEW_BODY(true /*nothrow*/); }
247 INTERCEPTOR_ATTRIBUTE
248 void *operator new(size_t size
, std::align_val_t align
)
249 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
250 INTERCEPTOR_ATTRIBUTE
251 void *operator new[](size_t size
, std::align_val_t align
)
252 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
253 INTERCEPTOR_ATTRIBUTE
254 void *operator new(size_t size
, std::align_val_t align
, std::nothrow_t
const&)
255 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
256 INTERCEPTOR_ATTRIBUTE
257 void *operator new[](size_t size
, std::align_val_t align
, std::nothrow_t
const&)
258 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
260 INTERCEPTOR_ATTRIBUTE
261 void operator delete(void *ptr
) NOEXCEPT
{ OPERATOR_DELETE_BODY
; }
262 INTERCEPTOR_ATTRIBUTE
263 void operator delete[](void *ptr
) NOEXCEPT
{ OPERATOR_DELETE_BODY
; }
264 INTERCEPTOR_ATTRIBUTE
265 void operator delete(void *ptr
, std::nothrow_t
const&) { OPERATOR_DELETE_BODY
; }
266 INTERCEPTOR_ATTRIBUTE
267 void operator delete[](void *ptr
, std::nothrow_t
const &)
268 { OPERATOR_DELETE_BODY
; }
269 INTERCEPTOR_ATTRIBUTE
270 void operator delete(void *ptr
, size_t size
) NOEXCEPT
271 { OPERATOR_DELETE_BODY
; }
272 INTERCEPTOR_ATTRIBUTE
273 void operator delete[](void *ptr
, size_t size
) NOEXCEPT
274 { OPERATOR_DELETE_BODY
; }
275 INTERCEPTOR_ATTRIBUTE
276 void operator delete(void *ptr
, std::align_val_t
) NOEXCEPT
277 { OPERATOR_DELETE_BODY
; }
278 INTERCEPTOR_ATTRIBUTE
279 void operator delete[](void *ptr
, std::align_val_t
) NOEXCEPT
280 { OPERATOR_DELETE_BODY
; }
281 INTERCEPTOR_ATTRIBUTE
282 void operator delete(void *ptr
, std::align_val_t
, std::nothrow_t
const&)
283 { OPERATOR_DELETE_BODY
; }
284 INTERCEPTOR_ATTRIBUTE
285 void operator delete[](void *ptr
, std::align_val_t
, std::nothrow_t
const&)
286 { OPERATOR_DELETE_BODY
; }
287 INTERCEPTOR_ATTRIBUTE
288 void operator delete(void *ptr
, size_t size
, std::align_val_t
) NOEXCEPT
289 { OPERATOR_DELETE_BODY
; }
290 INTERCEPTOR_ATTRIBUTE
291 void operator delete[](void *ptr
, size_t size
, std::align_val_t
) NOEXCEPT
292 { OPERATOR_DELETE_BODY
; }
294 #else // SANITIZER_MAC
296 INTERCEPTOR(void *, _Znwm
, size_t size
)
297 { OPERATOR_NEW_BODY(false /*nothrow*/); }
298 INTERCEPTOR(void *, _Znam
, size_t size
)
299 { OPERATOR_NEW_BODY(false /*nothrow*/); }
300 INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t
, size_t size
, std::nothrow_t
const&)
301 { OPERATOR_NEW_BODY(true /*nothrow*/); }
302 INTERCEPTOR(void *, _ZnamRKSt9nothrow_t
, size_t size
, std::nothrow_t
const&)
303 { OPERATOR_NEW_BODY(true /*nothrow*/); }
305 INTERCEPTOR(void, _ZdlPv
, void *ptr
)
306 { OPERATOR_DELETE_BODY
; }
307 INTERCEPTOR(void, _ZdaPv
, void *ptr
)
308 { OPERATOR_DELETE_BODY
; }
309 INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t
, void *ptr
, std::nothrow_t
const&)
310 { OPERATOR_DELETE_BODY
; }
311 INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t
, void *ptr
, std::nothrow_t
const&)
312 { OPERATOR_DELETE_BODY
; }
314 #endif // !SANITIZER_MAC
317 ///// Thread initialization and finalization. /////
319 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD && !SANITIZER_FUCHSIA
320 static unsigned g_thread_finalize_key
;
322 static void thread_finalize(void *v
) {
325 if (pthread_setspecific(g_thread_finalize_key
, (void*)(iter
- 1))) {
326 Report("LeakSanitizer: failed to set thread key.\n");
336 INTERCEPTOR(void, _lwp_exit
) {
341 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT INTERCEPT_FUNCTION(_lwp_exit)
343 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT
346 #if SANITIZER_INTERCEPT_THR_EXIT
347 INTERCEPTOR(void, thr_exit
, tid_t
*state
) {
350 REAL(thr_exit
)(state
);
352 #define LSAN_MAYBE_INTERCEPT_THR_EXIT INTERCEPT_FUNCTION(thr_exit)
354 #define LSAN_MAYBE_INTERCEPT_THR_EXIT
357 #if SANITIZER_INTERCEPT___CXA_ATEXIT
358 INTERCEPTOR(int, __cxa_atexit
, void (*func
)(void *), void *arg
,
360 __lsan::ScopedInterceptorDisabler disabler
;
361 return REAL(__cxa_atexit
)(func
, arg
, dso_handle
);
363 #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT INTERCEPT_FUNCTION(__cxa_atexit)
365 #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT
368 #if SANITIZER_INTERCEPT_ATEXIT
369 INTERCEPTOR(int, atexit
, void (*f
)()) {
370 __lsan::ScopedInterceptorDisabler disabler
;
371 return REAL(__cxa_atexit
)((void (*)(void *a
))f
, 0, 0);
373 #define LSAN_MAYBE_INTERCEPT_ATEXIT INTERCEPT_FUNCTION(atexit)
375 #define LSAN_MAYBE_INTERCEPT_ATEXIT
378 #if SANITIZER_INTERCEPT_PTHREAD_ATFORK
380 extern int _pthread_atfork(void (*prepare
)(), void (*parent
)(),
384 INTERCEPTOR(int, pthread_atfork
, void (*prepare
)(), void (*parent
)(),
386 __lsan::ScopedInterceptorDisabler disabler
;
387 // REAL(pthread_atfork) cannot be called due to symbol indirections at least
389 return _pthread_atfork(prepare
, parent
, child
);
391 #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK INTERCEPT_FUNCTION(pthread_atfork)
393 #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK
396 #if SANITIZER_INTERCEPT_STRERROR
397 INTERCEPTOR(char *, strerror
, int errnum
) {
398 __lsan::ScopedInterceptorDisabler disabler
;
399 return REAL(strerror
)(errnum
);
401 #define LSAN_MAYBE_INTERCEPT_STRERROR INTERCEPT_FUNCTION(strerror)
403 #define LSAN_MAYBE_INTERCEPT_STRERROR
409 void *(*callback
)(void *arg
);
411 atomic_uintptr_t tid
;
414 extern "C" void *__lsan_thread_start_func(void *arg
) {
415 ThreadParam
*p
= (ThreadParam
*)arg
;
416 void* (*callback
)(void *arg
) = p
->callback
;
417 void *param
= p
->param
;
418 // Wait until the last iteration to maximize the chance that we are the last
419 // destructor to run.
420 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
421 if (pthread_setspecific(g_thread_finalize_key
,
422 (void*)GetPthreadDestructorIterations())) {
423 Report("LeakSanitizer: failed to set thread key.\n");
428 while ((tid
= atomic_load(&p
->tid
, memory_order_acquire
)) == 0)
429 internal_sched_yield();
430 ThreadStart(tid
, GetTid());
431 atomic_store(&p
->tid
, 0, memory_order_release
);
432 return callback(param
);
435 INTERCEPTOR(int, pthread_create
, void *th
, void *attr
,
436 void *(*callback
)(void *), void *param
) {
438 EnsureMainThreadIDIsCorrect();
439 __sanitizer_pthread_attr_t myattr
;
441 pthread_attr_init(&myattr
);
444 AdjustStackSize(attr
);
446 pthread_attr_getdetachstate(attr
, &detached
);
448 p
.callback
= callback
;
450 atomic_store(&p
.tid
, 0, memory_order_relaxed
);
453 // Ignore all allocations made by pthread_create: thread stack/TLS may be
454 // stored by pthread for future reuse even after thread destruction, and
455 // the linked list it's stored in doesn't even hold valid pointers to the
456 // objects, the latter are calculated by obscure pointer arithmetic.
457 ScopedInterceptorDisabler disabler
;
458 res
= REAL(pthread_create
)(th
, attr
, __lsan_thread_start_func
, &p
);
461 int tid
= ThreadCreate(GetCurrentThread(), *(uptr
*)th
,
462 IsStateDetached(detached
));
464 atomic_store(&p
.tid
, tid
, memory_order_release
);
465 while (atomic_load(&p
.tid
, memory_order_acquire
) != 0)
466 internal_sched_yield();
469 pthread_attr_destroy(&myattr
);
473 INTERCEPTOR(int, pthread_join
, void *th
, void **ret
) {
475 int tid
= ThreadTid((uptr
)th
);
476 int res
= REAL(pthread_join
)(th
, ret
);
482 INTERCEPTOR(int, pthread_detach
, void *th
) {
484 int tid
= ThreadTid((uptr
)th
);
485 int res
= REAL(pthread_detach
)(th
);
491 INTERCEPTOR(void, _exit
, int status
) {
492 if (status
== 0 && HasReportedLeaks()) status
= common_flags()->exitcode
;
496 #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
497 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
499 #endif // SANITIZER_POSIX
503 void InitializeInterceptors() {
504 // Fuchsia doesn't use interceptors that require any setup.
505 #if !SANITIZER_FUCHSIA
506 InitializeSignalInterceptors();
508 INTERCEPT_FUNCTION(malloc
);
509 INTERCEPT_FUNCTION(free
);
510 LSAN_MAYBE_INTERCEPT_CFREE
;
511 INTERCEPT_FUNCTION(calloc
);
512 INTERCEPT_FUNCTION(realloc
);
513 LSAN_MAYBE_INTERCEPT_MEMALIGN
;
514 LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
;
515 LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
;
516 INTERCEPT_FUNCTION(posix_memalign
);
517 INTERCEPT_FUNCTION(valloc
);
518 LSAN_MAYBE_INTERCEPT_PVALLOC
;
519 LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
;
520 LSAN_MAYBE_INTERCEPT_MALLINFO
;
521 LSAN_MAYBE_INTERCEPT_MALLOPT
;
522 INTERCEPT_FUNCTION(pthread_create
);
523 INTERCEPT_FUNCTION(pthread_detach
);
524 INTERCEPT_FUNCTION(pthread_join
);
525 INTERCEPT_FUNCTION(_exit
);
527 LSAN_MAYBE_INTERCEPT__LWP_EXIT
;
528 LSAN_MAYBE_INTERCEPT_THR_EXIT
;
530 LSAN_MAYBE_INTERCEPT___CXA_ATEXIT
;
531 LSAN_MAYBE_INTERCEPT_ATEXIT
;
532 LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK
;
534 LSAN_MAYBE_INTERCEPT_STRERROR
;
536 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
537 if (pthread_key_create(&g_thread_finalize_key
, &thread_finalize
)) {
538 Report("LeakSanitizer: failed to create thread key.\n");
543 #endif // !SANITIZER_FUCHSIA
546 } // namespace __lsan