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_netbsd.h"
24 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
25 #include "sanitizer_common/sanitizer_posix.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. /////
48 enum class align_val_t: size_t;
52 INTERCEPTOR(void*, malloc, uptr size) {
54 GET_STACK_TRACE_MALLOC;
55 return lsan_malloc(size, stack);
58 INTERCEPTOR(void, free, void *p) {
63 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
64 if (lsan_init_is_running) {
65 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
66 const uptr kCallocPoolSize = 1024;
67 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
68 static uptr allocated;
69 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
70 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
71 allocated += size_in_words;
72 CHECK(allocated < kCallocPoolSize);
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 #if SANITIZER_INTERCEPT_MEMALIGN
102 INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
104 GET_STACK_TRACE_MALLOC;
105 return lsan_memalign(alignment, size, stack);
107 #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
109 INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
111 GET_STACK_TRACE_MALLOC;
112 void *res = lsan_memalign(alignment, size, stack);
113 DTLS_on_libc_memalign(res, size);
116 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
118 #define LSAN_MAYBE_INTERCEPT_MEMALIGN
119 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
120 #endif // SANITIZER_INTERCEPT_MEMALIGN
122 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
123 INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
125 GET_STACK_TRACE_MALLOC;
126 return lsan_memalign(alignment, size, stack);
128 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
130 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
133 #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE
134 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
136 return GetMallocUsableSize(ptr);
138 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
139 INTERCEPT_FUNCTION(malloc_usable_size)
141 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
144 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
145 struct fake_mallinfo {
149 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
150 struct fake_mallinfo res;
151 internal_memset(&res, 0, sizeof(res));
154 #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
156 INTERCEPTOR(int, mallopt, int cmd, int value) {
159 #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
161 #define LSAN_MAYBE_INTERCEPT_MALLINFO
162 #define LSAN_MAYBE_INTERCEPT_MALLOPT
163 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
165 #if SANITIZER_INTERCEPT_PVALLOC
166 INTERCEPTOR(void*, pvalloc, uptr size) {
168 GET_STACK_TRACE_MALLOC;
169 uptr PageSize = GetPageSizeCached();
170 size = RoundUpTo(size, PageSize);
172 // pvalloc(0) should allocate one page.
175 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
177 #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
179 #define LSAN_MAYBE_INTERCEPT_PVALLOC
180 #endif // SANITIZER_INTERCEPT_PVALLOC
182 #if SANITIZER_INTERCEPT_CFREE
183 INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
184 #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
186 #define LSAN_MAYBE_INTERCEPT_CFREE
187 #endif // SANITIZER_INTERCEPT_CFREE
189 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
190 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
194 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
198 INTERCEPTOR(int, mprobe, void *ptr) {
201 #endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
204 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
205 #define OPERATOR_NEW_BODY(nothrow) \
206 ENSURE_LSAN_INITED; \
207 GET_STACK_TRACE_MALLOC; \
208 void *res = lsan_malloc(size, stack); \
209 if (!nothrow && UNLIKELY(!res)) DieOnFailure::OnOOM(); \
211 #define OPERATOR_NEW_BODY_ALIGN(nothrow) \
212 ENSURE_LSAN_INITED; \
213 GET_STACK_TRACE_MALLOC; \
214 void *res = lsan_memalign((uptr)align, size, stack); \
215 if (!nothrow && UNLIKELY(!res)) DieOnFailure::OnOOM(); \
218 #define OPERATOR_DELETE_BODY \
219 ENSURE_LSAN_INITED; \
222 // On OS X it's not enough to just provide our own 'operator new' and
223 // 'operator delete' implementations, because they're going to be in the runtime
224 // dylib, and the main executable will depend on both the runtime dylib and
225 // libstdc++, each of has its implementation of new and delete.
226 // To make sure that C++ allocation/deallocation operators are overridden on
227 // OS X we need to intercept them using their mangled names.
230 INTERCEPTOR_ATTRIBUTE
231 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
232 INTERCEPTOR_ATTRIBUTE
233 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
234 INTERCEPTOR_ATTRIBUTE
235 void *operator new(size_t size, std::nothrow_t const&)
236 { OPERATOR_NEW_BODY(true /*nothrow*/); }
237 INTERCEPTOR_ATTRIBUTE
238 void *operator new[](size_t size, std::nothrow_t const&)
239 { OPERATOR_NEW_BODY(true /*nothrow*/); }
240 INTERCEPTOR_ATTRIBUTE
241 void *operator new(size_t size, std::align_val_t align)
242 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
243 INTERCEPTOR_ATTRIBUTE
244 void *operator new[](size_t size, std::align_val_t align)
245 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
246 INTERCEPTOR_ATTRIBUTE
247 void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
248 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
249 INTERCEPTOR_ATTRIBUTE
250 void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
251 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
253 INTERCEPTOR_ATTRIBUTE
254 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
255 INTERCEPTOR_ATTRIBUTE
256 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
257 INTERCEPTOR_ATTRIBUTE
258 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
259 INTERCEPTOR_ATTRIBUTE
260 void operator delete[](void *ptr, std::nothrow_t const &)
261 { OPERATOR_DELETE_BODY; }
262 INTERCEPTOR_ATTRIBUTE
263 void operator delete(void *ptr, size_t size) NOEXCEPT
264 { OPERATOR_DELETE_BODY; }
265 INTERCEPTOR_ATTRIBUTE
266 void operator delete[](void *ptr, size_t size) NOEXCEPT
267 { OPERATOR_DELETE_BODY; }
268 INTERCEPTOR_ATTRIBUTE
269 void operator delete(void *ptr, std::align_val_t) NOEXCEPT
270 { OPERATOR_DELETE_BODY; }
271 INTERCEPTOR_ATTRIBUTE
272 void operator delete[](void *ptr, std::align_val_t) NOEXCEPT
273 { OPERATOR_DELETE_BODY; }
274 INTERCEPTOR_ATTRIBUTE
275 void operator delete(void *ptr, std::align_val_t, std::nothrow_t const&)
276 { OPERATOR_DELETE_BODY; }
277 INTERCEPTOR_ATTRIBUTE
278 void operator delete[](void *ptr, std::align_val_t, std::nothrow_t const&)
279 { OPERATOR_DELETE_BODY; }
280 INTERCEPTOR_ATTRIBUTE
281 void operator delete(void *ptr, size_t size, std::align_val_t) NOEXCEPT
282 { OPERATOR_DELETE_BODY; }
283 INTERCEPTOR_ATTRIBUTE
284 void operator delete[](void *ptr, size_t size, std::align_val_t) NOEXCEPT
285 { OPERATOR_DELETE_BODY; }
287 #else // SANITIZER_MAC
289 INTERCEPTOR(void *, _Znwm, size_t size)
290 { OPERATOR_NEW_BODY(false /*nothrow*/); }
291 INTERCEPTOR(void *, _Znam, size_t size)
292 { OPERATOR_NEW_BODY(false /*nothrow*/); }
293 INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&)
294 { OPERATOR_NEW_BODY(true /*nothrow*/); }
295 INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&)
296 { OPERATOR_NEW_BODY(true /*nothrow*/); }
298 INTERCEPTOR(void, _ZdlPv, void *ptr)
299 { OPERATOR_DELETE_BODY; }
300 INTERCEPTOR(void, _ZdaPv, void *ptr)
301 { OPERATOR_DELETE_BODY; }
302 INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
303 { OPERATOR_DELETE_BODY; }
304 INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
305 { OPERATOR_DELETE_BODY; }
307 #endif // !SANITIZER_MAC
310 ///// Thread initialization and finalization. /////
312 #if !SANITIZER_NETBSD
313 static unsigned g_thread_finalize_key;
315 static void thread_finalize(void *v) {
318 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
319 Report("LeakSanitizer: failed to set thread key.\n");
329 INTERCEPTOR(void, _lwp_exit) {
334 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT INTERCEPT_FUNCTION(_lwp_exit)
336 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT
340 void *(*callback)(void *arg);
342 atomic_uintptr_t tid;
345 extern "C" void *__lsan_thread_start_func(void *arg) {
346 ThreadParam *p = (ThreadParam*)arg;
347 void* (*callback)(void *arg) = p->callback;
348 void *param = p->param;
349 // Wait until the last iteration to maximize the chance that we are the last
350 // destructor to run.
351 #if !SANITIZER_NETBSD
352 if (pthread_setspecific(g_thread_finalize_key,
353 (void*)GetPthreadDestructorIterations())) {
354 Report("LeakSanitizer: failed to set thread key.\n");
359 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
360 internal_sched_yield();
361 SetCurrentThread(tid);
362 ThreadStart(tid, GetTid());
363 atomic_store(&p->tid, 0, memory_order_release);
364 return callback(param);
367 INTERCEPTOR(int, pthread_create, void *th, void *attr,
368 void *(*callback)(void *), void *param) {
370 EnsureMainThreadIDIsCorrect();
371 __sanitizer_pthread_attr_t myattr;
373 pthread_attr_init(&myattr);
376 AdjustStackSize(attr);
378 pthread_attr_getdetachstate(attr, &detached);
380 p.callback = callback;
382 atomic_store(&p.tid, 0, memory_order_relaxed);
385 // Ignore all allocations made by pthread_create: thread stack/TLS may be
386 // stored by pthread for future reuse even after thread destruction, and
387 // the linked list it's stored in doesn't even hold valid pointers to the
388 // objects, the latter are calculated by obscure pointer arithmetic.
389 ScopedInterceptorDisabler disabler;
390 res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
393 int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th,
394 IsStateDetached(detached));
396 atomic_store(&p.tid, tid, memory_order_release);
397 while (atomic_load(&p.tid, memory_order_acquire) != 0)
398 internal_sched_yield();
401 pthread_attr_destroy(&myattr);
405 INTERCEPTOR(int, pthread_join, void *th, void **ret) {
407 int tid = ThreadTid((uptr)th);
408 int res = REAL(pthread_join)(th, ret);
414 INTERCEPTOR(void, _exit, int status) {
415 if (status == 0 && HasReportedLeaks()) status = common_flags()->exitcode;
419 #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
420 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
424 void InitializeInterceptors() {
425 InitializeSignalInterceptors();
427 INTERCEPT_FUNCTION(malloc);
428 INTERCEPT_FUNCTION(free);
429 LSAN_MAYBE_INTERCEPT_CFREE;
430 INTERCEPT_FUNCTION(calloc);
431 INTERCEPT_FUNCTION(realloc);
432 LSAN_MAYBE_INTERCEPT_MEMALIGN;
433 LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
434 LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
435 INTERCEPT_FUNCTION(posix_memalign);
436 INTERCEPT_FUNCTION(valloc);
437 LSAN_MAYBE_INTERCEPT_PVALLOC;
438 LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
439 LSAN_MAYBE_INTERCEPT_MALLINFO;
440 LSAN_MAYBE_INTERCEPT_MALLOPT;
441 INTERCEPT_FUNCTION(pthread_create);
442 INTERCEPT_FUNCTION(pthread_join);
443 INTERCEPT_FUNCTION(_exit);
445 LSAN_MAYBE_INTERCEPT__LWP_EXIT;
447 #if !SANITIZER_NETBSD
448 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
449 Report("LeakSanitizer: failed to create thread key.\n");
455 } // namespace __lsan