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_tls_get_addr.h"
26 #include "lsan_allocator.h"
27 #include "lsan_common.h"
28 #include "lsan_thread.h"
30 using namespace __lsan;
33 int pthread_attr_init(void *attr);
34 int pthread_attr_destroy(void *attr);
35 int pthread_attr_getdetachstate(void *attr, int *v);
36 int pthread_key_create(unsigned *key, void (*destructor)(void* v));
37 int pthread_setspecific(unsigned key, const void *v);
40 #define ENSURE_LSAN_INITED do { \
41 CHECK(!lsan_init_is_running); \
46 ///// Malloc/free interceptors. /////
48 const bool kAlwaysClearMemory = true;
54 INTERCEPTOR(void*, malloc, uptr size) {
56 GET_STACK_TRACE_MALLOC;
57 return Allocate(stack, size, 1, kAlwaysClearMemory);
60 INTERCEPTOR(void, free, void *p) {
65 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
66 if (lsan_init_is_running) {
67 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
68 const uptr kCallocPoolSize = 1024;
69 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
70 static uptr allocated;
71 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
72 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
73 allocated += size_in_words;
74 CHECK(allocated < kCallocPoolSize);
77 if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return nullptr;
79 GET_STACK_TRACE_MALLOC;
81 return Allocate(stack, size, 1, true);
84 INTERCEPTOR(void*, realloc, void *q, uptr size) {
86 GET_STACK_TRACE_MALLOC;
87 return Reallocate(stack, q, size, 1);
90 #if SANITIZER_INTERCEPT_MEMALIGN
91 INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
93 GET_STACK_TRACE_MALLOC;
94 return Allocate(stack, size, alignment, kAlwaysClearMemory);
96 #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
98 INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
100 GET_STACK_TRACE_MALLOC;
101 void *res = Allocate(stack, size, alignment, kAlwaysClearMemory);
102 DTLS_on_libc_memalign(res, size);
105 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
107 #define LSAN_MAYBE_INTERCEPT_MEMALIGN
108 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
109 #endif // SANITIZER_INTERCEPT_MEMALIGN
111 INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
113 GET_STACK_TRACE_MALLOC;
114 return Allocate(stack, size, alignment, kAlwaysClearMemory);
117 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
119 GET_STACK_TRACE_MALLOC;
120 *memptr = Allocate(stack, size, alignment, kAlwaysClearMemory);
121 // FIXME: Return ENOMEM if user requested more than max alloc size.
125 INTERCEPTOR(void*, valloc, uptr size) {
127 GET_STACK_TRACE_MALLOC;
129 size = GetPageSizeCached();
130 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
133 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
135 return GetMallocUsableSize(ptr);
138 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
139 struct fake_mallinfo {
143 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
144 struct fake_mallinfo res;
145 internal_memset(&res, 0, sizeof(res));
148 #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
150 INTERCEPTOR(int, mallopt, int cmd, int value) {
153 #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
155 #define LSAN_MAYBE_INTERCEPT_MALLINFO
156 #define LSAN_MAYBE_INTERCEPT_MALLOPT
157 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
159 #if SANITIZER_INTERCEPT_PVALLOC
160 INTERCEPTOR(void*, pvalloc, uptr size) {
162 GET_STACK_TRACE_MALLOC;
163 uptr PageSize = GetPageSizeCached();
164 size = RoundUpTo(size, PageSize);
166 // pvalloc(0) should allocate one page.
169 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
171 #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
173 #define LSAN_MAYBE_INTERCEPT_PVALLOC
174 #endif // SANITIZER_INTERCEPT_PVALLOC
176 #if SANITIZER_INTERCEPT_CFREE
177 INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
178 #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
180 #define LSAN_MAYBE_INTERCEPT_CFREE
181 #endif // SANITIZER_INTERCEPT_CFREE
183 #define OPERATOR_NEW_BODY \
184 ENSURE_LSAN_INITED; \
185 GET_STACK_TRACE_MALLOC; \
186 return Allocate(stack, size, 1, kAlwaysClearMemory);
188 INTERCEPTOR_ATTRIBUTE
189 void *operator new(uptr size) { OPERATOR_NEW_BODY; }
190 INTERCEPTOR_ATTRIBUTE
191 void *operator new[](uptr size) { OPERATOR_NEW_BODY; }
192 INTERCEPTOR_ATTRIBUTE
193 void *operator new(uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
194 INTERCEPTOR_ATTRIBUTE
195 void *operator new[](uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
197 #define OPERATOR_DELETE_BODY \
198 ENSURE_LSAN_INITED; \
201 INTERCEPTOR_ATTRIBUTE
202 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
203 INTERCEPTOR_ATTRIBUTE
204 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
205 INTERCEPTOR_ATTRIBUTE
206 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
207 INTERCEPTOR_ATTRIBUTE
208 void operator delete[](void *ptr, std::nothrow_t const &) {
209 OPERATOR_DELETE_BODY;
212 ///// Thread initialization and finalization. /////
214 static unsigned g_thread_finalize_key;
216 static void thread_finalize(void *v) {
219 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
220 Report("LeakSanitizer: failed to set thread key.\n");
229 void *(*callback)(void *arg);
231 atomic_uintptr_t tid;
234 extern "C" void *__lsan_thread_start_func(void *arg) {
235 ThreadParam *p = (ThreadParam*)arg;
236 void* (*callback)(void *arg) = p->callback;
237 void *param = p->param;
238 // Wait until the last iteration to maximize the chance that we are the last
239 // destructor to run.
240 if (pthread_setspecific(g_thread_finalize_key,
241 (void*)GetPthreadDestructorIterations())) {
242 Report("LeakSanitizer: failed to set thread key.\n");
246 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
247 internal_sched_yield();
248 SetCurrentThread(tid);
249 ThreadStart(tid, GetTid());
250 atomic_store(&p->tid, 0, memory_order_release);
251 return callback(param);
254 INTERCEPTOR(int, pthread_create, void *th, void *attr,
255 void *(*callback)(void *), void *param) {
257 EnsureMainThreadIDIsCorrect();
258 __sanitizer_pthread_attr_t myattr;
260 pthread_attr_init(&myattr);
263 AdjustStackSize(attr);
265 pthread_attr_getdetachstate(attr, &detached);
267 p.callback = callback;
269 atomic_store(&p.tid, 0, memory_order_relaxed);
272 // Ignore all allocations made by pthread_create: thread stack/TLS may be
273 // stored by pthread for future reuse even after thread destruction, and
274 // the linked list it's stored in doesn't even hold valid pointers to the
275 // objects, the latter are calculated by obscure pointer arithmetic.
276 ScopedInterceptorDisabler disabler;
277 res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
280 int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th, detached);
282 atomic_store(&p.tid, tid, memory_order_release);
283 while (atomic_load(&p.tid, memory_order_acquire) != 0)
284 internal_sched_yield();
287 pthread_attr_destroy(&myattr);
291 INTERCEPTOR(int, pthread_join, void *th, void **ret) {
293 int tid = ThreadTid((uptr)th);
294 int res = REAL(pthread_join)(th, ret);
302 void InitializeInterceptors() {
303 INTERCEPT_FUNCTION(malloc);
304 INTERCEPT_FUNCTION(free);
305 LSAN_MAYBE_INTERCEPT_CFREE;
306 INTERCEPT_FUNCTION(calloc);
307 INTERCEPT_FUNCTION(realloc);
308 LSAN_MAYBE_INTERCEPT_MEMALIGN;
309 LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
310 INTERCEPT_FUNCTION(aligned_alloc);
311 INTERCEPT_FUNCTION(posix_memalign);
312 INTERCEPT_FUNCTION(valloc);
313 LSAN_MAYBE_INTERCEPT_PVALLOC;
314 INTERCEPT_FUNCTION(malloc_usable_size);
315 LSAN_MAYBE_INTERCEPT_MALLINFO;
316 LSAN_MAYBE_INTERCEPT_MALLOPT;
317 INTERCEPT_FUNCTION(pthread_create);
318 INTERCEPT_FUNCTION(pthread_join);
320 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
321 Report("LeakSanitizer: failed to create thread key.\n");
326 } // namespace __lsan