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"
28 #include "sanitizer_common/sanitizer_procmaps.h"
30 #include "sanitizer_common/sanitizer_tls_get_addr.h"
32 #include "lsan_allocator.h"
33 #include "lsan_common.h"
34 #include "lsan_thread.h"
39 using namespace __lsan;
42 int pthread_attr_init(void *attr);
43 int pthread_attr_destroy(void *attr);
44 int pthread_attr_getdetachstate(void *attr, int *v);
45 int pthread_key_create(unsigned *key, void (*destructor)(void* v));
46 int pthread_setspecific(unsigned key, const void *v);
47 void __lsan_register_root_region(const void *p, size_t size);
48 void __lsan_unregister_root_region(const void *p, size_t size);
53 struct DlsymAlloc : DlSymAllocator<DlsymAlloc> {
54 static bool UseImpl() { return lsan_init_is_running; }
55 static void OnAllocate(const void *ptr, uptr size) {
56 #if CAN_SANITIZE_LEAKS
57 // Suppress leaks from dlerror(). Previously dlsym hack on global array was
58 // used by leak sanitizer as a root region.
59 __lsan_register_root_region(ptr, size);
62 static void OnFree(const void *ptr, uptr size) {
63 #if CAN_SANITIZE_LEAKS
64 __lsan_unregister_root_region(ptr, size);
69 ///// Malloc/free interceptors. /////
73 enum class align_val_t: size_t;
77 INTERCEPTOR(void*, malloc, uptr size) {
78 if (DlsymAlloc::Use())
79 return DlsymAlloc::Allocate(size);
81 GET_STACK_TRACE_MALLOC;
82 return lsan_malloc(size, stack);
85 INTERCEPTOR(void, free, void *p) {
86 if (DlsymAlloc::PointerIsMine(p))
87 return DlsymAlloc::Free(p);
92 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
93 if (DlsymAlloc::Use())
94 return DlsymAlloc::Callocate(nmemb, size);
96 GET_STACK_TRACE_MALLOC;
97 return lsan_calloc(nmemb, size, stack);
100 INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
101 if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
102 return DlsymAlloc::Realloc(ptr, size);
104 GET_STACK_TRACE_MALLOC;
105 return lsan_realloc(ptr, size, stack);
108 INTERCEPTOR(void*, reallocarray, void *q, uptr nmemb, uptr size) {
110 GET_STACK_TRACE_MALLOC;
111 return lsan_reallocarray(q, nmemb, size, stack);
114 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
116 GET_STACK_TRACE_MALLOC;
117 return lsan_posix_memalign(memptr, alignment, size, stack);
120 INTERCEPTOR(void*, valloc, uptr size) {
122 GET_STACK_TRACE_MALLOC;
123 return lsan_valloc(size, stack);
125 #endif // !SANITIZER_APPLE
127 #if SANITIZER_INTERCEPT_MEMALIGN
128 INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
130 GET_STACK_TRACE_MALLOC;
131 return lsan_memalign(alignment, size, stack);
133 #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
135 #define LSAN_MAYBE_INTERCEPT_MEMALIGN
136 #endif // SANITIZER_INTERCEPT_MEMALIGN
138 #if SANITIZER_INTERCEPT___LIBC_MEMALIGN
139 INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
141 GET_STACK_TRACE_MALLOC;
142 void *res = lsan_memalign(alignment, size, stack);
143 DTLS_on_libc_memalign(res, size);
146 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
148 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
149 #endif // SANITIZER_INTERCEPT___LIBC_MEMALIGN
151 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
152 INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
154 GET_STACK_TRACE_MALLOC;
155 return lsan_aligned_alloc(alignment, size, stack);
157 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
159 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
162 #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE
163 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
165 return GetMallocUsableSize(ptr);
167 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
168 INTERCEPT_FUNCTION(malloc_usable_size)
170 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
173 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
174 struct fake_mallinfo {
178 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
179 struct fake_mallinfo res;
180 internal_memset(&res, 0, sizeof(res));
183 #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
185 INTERCEPTOR(int, mallopt, int cmd, int value) {
188 #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
190 #define LSAN_MAYBE_INTERCEPT_MALLINFO
191 #define LSAN_MAYBE_INTERCEPT_MALLOPT
192 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
194 #if SANITIZER_INTERCEPT_PVALLOC
195 INTERCEPTOR(void*, pvalloc, uptr size) {
197 GET_STACK_TRACE_MALLOC;
198 return lsan_pvalloc(size, stack);
200 #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
202 #define LSAN_MAYBE_INTERCEPT_PVALLOC
203 #endif // SANITIZER_INTERCEPT_PVALLOC
205 #if SANITIZER_INTERCEPT_CFREE
206 INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
207 #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
209 #define LSAN_MAYBE_INTERCEPT_CFREE
210 #endif // SANITIZER_INTERCEPT_CFREE
212 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
213 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
217 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
221 INTERCEPTOR(int, mprobe, void *ptr) {
224 #endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
227 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
228 #define OPERATOR_NEW_BODY(nothrow)\
230 GET_STACK_TRACE_MALLOC;\
231 void *res = lsan_malloc(size, stack);\
232 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
234 #define OPERATOR_NEW_BODY_ALIGN(nothrow)\
236 GET_STACK_TRACE_MALLOC;\
237 void *res = lsan_memalign((uptr)align, size, stack);\
238 if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
241 #define OPERATOR_DELETE_BODY\
245 // On OS X it's not enough to just provide our own 'operator new' and
246 // 'operator delete' implementations, because they're going to be in the runtime
247 // dylib, and the main executable will depend on both the runtime dylib and
248 // libstdc++, each of has its implementation of new and delete.
249 // To make sure that C++ allocation/deallocation operators are overridden on
250 // OS X we need to intercept them using their mangled names.
253 INTERCEPTOR_ATTRIBUTE
254 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
255 INTERCEPTOR_ATTRIBUTE
256 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
257 INTERCEPTOR_ATTRIBUTE
258 void *operator new(size_t size, std::nothrow_t const&)
259 { OPERATOR_NEW_BODY(true /*nothrow*/); }
260 INTERCEPTOR_ATTRIBUTE
261 void *operator new[](size_t size, std::nothrow_t const&)
262 { OPERATOR_NEW_BODY(true /*nothrow*/); }
263 INTERCEPTOR_ATTRIBUTE
264 void *operator new(size_t size, std::align_val_t align)
265 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
266 INTERCEPTOR_ATTRIBUTE
267 void *operator new[](size_t size, std::align_val_t align)
268 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
269 INTERCEPTOR_ATTRIBUTE
270 void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
271 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
272 INTERCEPTOR_ATTRIBUTE
273 void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
274 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
276 INTERCEPTOR_ATTRIBUTE
277 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
278 INTERCEPTOR_ATTRIBUTE
279 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
280 INTERCEPTOR_ATTRIBUTE
281 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
282 INTERCEPTOR_ATTRIBUTE
283 void operator delete[](void *ptr, std::nothrow_t const &)
284 { OPERATOR_DELETE_BODY; }
285 INTERCEPTOR_ATTRIBUTE
286 void operator delete(void *ptr, size_t size) NOEXCEPT
287 { OPERATOR_DELETE_BODY; }
288 INTERCEPTOR_ATTRIBUTE
289 void operator delete[](void *ptr, size_t size) NOEXCEPT
290 { OPERATOR_DELETE_BODY; }
291 INTERCEPTOR_ATTRIBUTE
292 void operator delete(void *ptr, std::align_val_t) NOEXCEPT
293 { OPERATOR_DELETE_BODY; }
294 INTERCEPTOR_ATTRIBUTE
295 void operator delete[](void *ptr, std::align_val_t) NOEXCEPT
296 { OPERATOR_DELETE_BODY; }
297 INTERCEPTOR_ATTRIBUTE
298 void operator delete(void *ptr, std::align_val_t, std::nothrow_t const&)
299 { OPERATOR_DELETE_BODY; }
300 INTERCEPTOR_ATTRIBUTE
301 void operator delete[](void *ptr, std::align_val_t, std::nothrow_t const&)
302 { OPERATOR_DELETE_BODY; }
303 INTERCEPTOR_ATTRIBUTE
304 void operator delete(void *ptr, size_t size, std::align_val_t) NOEXCEPT
305 { OPERATOR_DELETE_BODY; }
306 INTERCEPTOR_ATTRIBUTE
307 void operator delete[](void *ptr, size_t size, std::align_val_t) NOEXCEPT
308 { OPERATOR_DELETE_BODY; }
310 #else // SANITIZER_APPLE
312 INTERCEPTOR(void *, _Znwm, size_t size)
313 { OPERATOR_NEW_BODY(false /*nothrow*/); }
314 INTERCEPTOR(void *, _Znam, size_t size)
315 { OPERATOR_NEW_BODY(false /*nothrow*/); }
316 INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&)
317 { OPERATOR_NEW_BODY(true /*nothrow*/); }
318 INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&)
319 { OPERATOR_NEW_BODY(true /*nothrow*/); }
321 INTERCEPTOR(void, _ZdlPv, void *ptr)
322 { OPERATOR_DELETE_BODY; }
323 INTERCEPTOR(void, _ZdaPv, void *ptr)
324 { OPERATOR_DELETE_BODY; }
325 INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
326 { OPERATOR_DELETE_BODY; }
327 INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
328 { OPERATOR_DELETE_BODY; }
330 #endif // !SANITIZER_APPLE
333 ///// Thread initialization and finalization. /////
335 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD && !SANITIZER_FUCHSIA
336 static unsigned g_thread_finalize_key;
338 static void thread_finalize(void *v) {
341 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
342 Report("LeakSanitizer: failed to set thread key.\n");
352 INTERCEPTOR(void, _lwp_exit) {
357 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT INTERCEPT_FUNCTION(_lwp_exit)
359 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT
362 #if SANITIZER_INTERCEPT_THR_EXIT
363 INTERCEPTOR(void, thr_exit, tid_t *state) {
366 REAL(thr_exit)(state);
368 #define LSAN_MAYBE_INTERCEPT_THR_EXIT INTERCEPT_FUNCTION(thr_exit)
370 #define LSAN_MAYBE_INTERCEPT_THR_EXIT
373 #if SANITIZER_INTERCEPT___CXA_ATEXIT
374 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
376 __lsan::ScopedInterceptorDisabler disabler;
377 return REAL(__cxa_atexit)(func, arg, dso_handle);
379 #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT INTERCEPT_FUNCTION(__cxa_atexit)
381 #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT
384 #if SANITIZER_INTERCEPT_ATEXIT
385 INTERCEPTOR(int, atexit, void (*f)()) {
386 __lsan::ScopedInterceptorDisabler disabler;
387 return REAL(__cxa_atexit)((void (*)(void *a))f, 0, 0);
389 #define LSAN_MAYBE_INTERCEPT_ATEXIT INTERCEPT_FUNCTION(atexit)
391 #define LSAN_MAYBE_INTERCEPT_ATEXIT
394 #if SANITIZER_INTERCEPT_PTHREAD_ATFORK
396 extern int _pthread_atfork(void (*prepare)(), void (*parent)(),
400 INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
402 __lsan::ScopedInterceptorDisabler disabler;
403 // REAL(pthread_atfork) cannot be called due to symbol indirections at least
405 return _pthread_atfork(prepare, parent, child);
407 #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK INTERCEPT_FUNCTION(pthread_atfork)
409 #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK
412 #if SANITIZER_INTERCEPT_STRERROR
413 INTERCEPTOR(char *, strerror, int errnum) {
414 __lsan::ScopedInterceptorDisabler disabler;
415 return REAL(strerror)(errnum);
417 #define LSAN_MAYBE_INTERCEPT_STRERROR INTERCEPT_FUNCTION(strerror)
419 #define LSAN_MAYBE_INTERCEPT_STRERROR
425 void *(*callback)(void *arg);
427 atomic_uintptr_t tid;
430 extern "C" void *__lsan_thread_start_func(void *arg) {
431 ThreadParam *p = (ThreadParam*)arg;
432 void* (*callback)(void *arg) = p->callback;
433 void *param = p->param;
434 // Wait until the last iteration to maximize the chance that we are the last
435 // destructor to run.
436 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
437 if (pthread_setspecific(g_thread_finalize_key,
438 (void*)GetPthreadDestructorIterations())) {
439 Report("LeakSanitizer: failed to set thread key.\n");
444 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
445 internal_sched_yield();
446 ThreadStart(tid, GetTid());
447 atomic_store(&p->tid, 0, memory_order_release);
448 return callback(param);
451 INTERCEPTOR(int, pthread_create, void *th, void *attr,
452 void *(*callback)(void *), void *param) {
454 EnsureMainThreadIDIsCorrect();
455 __sanitizer_pthread_attr_t myattr;
457 pthread_attr_init(&myattr);
460 AdjustStackSize(attr);
462 pthread_attr_getdetachstate(attr, &detached);
464 p.callback = callback;
466 atomic_store(&p.tid, 0, memory_order_relaxed);
469 // Ignore all allocations made by pthread_create: thread stack/TLS may be
470 // stored by pthread for future reuse even after thread destruction, and
471 // the linked list it's stored in doesn't even hold valid pointers to the
472 // objects, the latter are calculated by obscure pointer arithmetic.
473 ScopedInterceptorDisabler disabler;
474 res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
477 int tid = ThreadCreate(GetCurrentThread(), IsStateDetached(detached));
478 CHECK_NE(tid, kMainTid);
479 atomic_store(&p.tid, tid, memory_order_release);
480 while (atomic_load(&p.tid, memory_order_acquire) != 0)
481 internal_sched_yield();
484 pthread_attr_destroy(&myattr);
488 INTERCEPTOR(int, pthread_join, void *t, void **arg) {
489 return REAL(pthread_join)(t, arg);
492 DEFINE_REAL_PTHREAD_FUNCTIONS
494 INTERCEPTOR(void, _exit, int status) {
495 if (status == 0 && HasReportedLeaks()) status = common_flags()->exitcode;
499 static inline bool isSharedMmap(void *addr, int flags) {
500 return (flags & MAP_SHARED) && (addr == NULL);
503 static inline bool isAnonymousMmap(int flags, int fd) {
504 return ((flags & MAP_ANON) || (flags & MAP_ANONYMOUS)) &&
505 !(flags & MAP_STACK) && (fd == -1);
508 static inline bool isInterestingMmap(void *addr, int prot, int flags, int fd) {
509 return ((prot & PROT_WRITE) && (prot & PROT_READ)) &&
510 (isAnonymousMmap(flags, fd) || isSharedMmap(addr, flags));
513 static inline void MaybeRegisterMmapRegion(void *res, size_t length, void *addr,
514 int prot, int flags, int fd) {
515 if (!lsan_check_in_progress && isInterestingMmap(addr, prot, flags, fd)) {
517 __lsan_register_root_region(res, length);
521 INTERCEPTOR(void *, mmap, void *addr, size_t length, int prot, int flags,
522 int fd, off_t offset) {
523 void *res = REAL(mmap)(addr, length, prot, flags, fd, offset);
524 MaybeRegisterMmapRegion(res, length, addr, prot, flags, fd);
528 INTERCEPTOR(void *, mmap64, void *addr, size_t length, int prot, int flags,
529 int fd, __off64_t offset) {
530 void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
531 MaybeRegisterMmapRegion(res, length, addr, prot, flags, fd);
535 INTERCEPTOR(int, munmap, void *addr, size_t length) {
536 if (!lsan_check_in_progress) {
538 __lsan_unregister_root_region(addr, length);
540 return REAL(munmap)(addr, length);
543 #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
544 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
546 #endif // SANITIZER_POSIX
550 void InitializeInterceptors() {
551 // Fuchsia doesn't use interceptors that require any setup.
552 #if !SANITIZER_FUCHSIA
553 InitializeSignalInterceptors();
555 INTERCEPT_FUNCTION(malloc);
556 INTERCEPT_FUNCTION(free);
557 LSAN_MAYBE_INTERCEPT_CFREE;
558 INTERCEPT_FUNCTION(calloc);
559 INTERCEPT_FUNCTION(realloc);
560 LSAN_MAYBE_INTERCEPT_MEMALIGN;
561 LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
562 LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
563 INTERCEPT_FUNCTION(posix_memalign);
564 INTERCEPT_FUNCTION(valloc);
565 LSAN_MAYBE_INTERCEPT_PVALLOC;
566 LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
567 LSAN_MAYBE_INTERCEPT_MALLINFO;
568 LSAN_MAYBE_INTERCEPT_MALLOPT;
569 INTERCEPT_FUNCTION(pthread_create);
570 INTERCEPT_FUNCTION(pthread_join);
571 INTERCEPT_FUNCTION(_exit);
572 INTERCEPT_FUNCTION(mmap);
573 INTERCEPT_FUNCTION(mmap64);
574 INTERCEPT_FUNCTION(munmap);
576 LSAN_MAYBE_INTERCEPT__LWP_EXIT;
577 LSAN_MAYBE_INTERCEPT_THR_EXIT;
579 LSAN_MAYBE_INTERCEPT___CXA_ATEXIT;
580 LSAN_MAYBE_INTERCEPT_ATEXIT;
581 LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK;
583 LSAN_MAYBE_INTERCEPT_STRERROR;
585 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
586 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
587 Report("LeakSanitizer: failed to create thread key.\n");
592 #endif // !SANITIZER_FUCHSIA
595 } // namespace __lsan