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_limits_posix.h"
23 #include "sanitizer_common/sanitizer_tls_get_addr.h"
25 #include "lsan_allocator.h"
26 #include "lsan_common.h"
27 #include "lsan_thread.h"
29 using namespace __lsan;
32 int pthread_attr_init(void *attr);
33 int pthread_attr_destroy(void *attr);
34 int pthread_attr_getdetachstate(void *attr, int *v);
35 int pthread_key_create(unsigned *key, void (*destructor)(void* v));
36 int pthread_setspecific(unsigned key, const void *v);
39 #define ENSURE_LSAN_INITED do { \
40 CHECK(!lsan_init_is_running); \
45 ///// Malloc/free interceptors. /////
47 const bool kAlwaysClearMemory = true;
53 INTERCEPTOR(void*, malloc, uptr size) {
55 GET_STACK_TRACE_MALLOC;
56 return Allocate(stack, size, 1, kAlwaysClearMemory);
59 INTERCEPTOR(void, free, void *p) {
64 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
65 if (lsan_init_is_running) {
66 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
67 const uptr kCallocPoolSize = 1024;
68 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
69 static uptr allocated;
70 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
71 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
72 allocated += size_in_words;
73 CHECK(allocated < kCallocPoolSize);
76 if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return nullptr;
78 GET_STACK_TRACE_MALLOC;
80 return Allocate(stack, size, 1, true);
83 INTERCEPTOR(void*, realloc, void *q, uptr size) {
85 GET_STACK_TRACE_MALLOC;
86 return Reallocate(stack, q, size, 1);
89 INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
91 GET_STACK_TRACE_MALLOC;
92 return Allocate(stack, size, alignment, kAlwaysClearMemory);
95 INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
97 GET_STACK_TRACE_MALLOC;
98 return Allocate(stack, size, alignment, kAlwaysClearMemory);
101 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
103 GET_STACK_TRACE_MALLOC;
104 *memptr = Allocate(stack, size, alignment, kAlwaysClearMemory);
105 // FIXME: Return ENOMEM if user requested more than max alloc size.
109 INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
111 GET_STACK_TRACE_MALLOC;
112 void *res = Allocate(stack, size, alignment, kAlwaysClearMemory);
113 DTLS_on_libc_memalign(res, size);
117 INTERCEPTOR(void*, valloc, uptr size) {
119 GET_STACK_TRACE_MALLOC;
121 size = GetPageSizeCached();
122 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
125 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
127 return GetMallocUsableSize(ptr);
130 struct fake_mallinfo {
134 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
135 struct fake_mallinfo res;
136 internal_memset(&res, 0, sizeof(res));
140 INTERCEPTOR(int, mallopt, int cmd, int value) {
144 INTERCEPTOR(void*, pvalloc, uptr size) {
146 GET_STACK_TRACE_MALLOC;
147 uptr PageSize = GetPageSizeCached();
148 size = RoundUpTo(size, PageSize);
150 // pvalloc(0) should allocate one page.
153 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
156 INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
158 #define OPERATOR_NEW_BODY \
159 ENSURE_LSAN_INITED; \
160 GET_STACK_TRACE_MALLOC; \
161 return Allocate(stack, size, 1, kAlwaysClearMemory);
163 INTERCEPTOR_ATTRIBUTE
164 void *operator new(uptr size) { OPERATOR_NEW_BODY; }
165 INTERCEPTOR_ATTRIBUTE
166 void *operator new[](uptr size) { OPERATOR_NEW_BODY; }
167 INTERCEPTOR_ATTRIBUTE
168 void *operator new(uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
169 INTERCEPTOR_ATTRIBUTE
170 void *operator new[](uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
172 #define OPERATOR_DELETE_BODY \
173 ENSURE_LSAN_INITED; \
176 INTERCEPTOR_ATTRIBUTE
177 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
178 INTERCEPTOR_ATTRIBUTE
179 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
180 INTERCEPTOR_ATTRIBUTE
181 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
182 INTERCEPTOR_ATTRIBUTE
183 void operator delete[](void *ptr, std::nothrow_t const &) {
184 OPERATOR_DELETE_BODY;
187 ///// Thread initialization and finalization. /////
189 static unsigned g_thread_finalize_key;
191 static void thread_finalize(void *v) {
194 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
195 Report("LeakSanitizer: failed to set thread key.\n");
204 void *(*callback)(void *arg);
206 atomic_uintptr_t tid;
209 extern "C" void *__lsan_thread_start_func(void *arg) {
210 ThreadParam *p = (ThreadParam*)arg;
211 void* (*callback)(void *arg) = p->callback;
212 void *param = p->param;
213 // Wait until the last iteration to maximize the chance that we are the last
214 // destructor to run.
215 if (pthread_setspecific(g_thread_finalize_key,
216 (void*)GetPthreadDestructorIterations())) {
217 Report("LeakSanitizer: failed to set thread key.\n");
221 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
222 internal_sched_yield();
223 SetCurrentThread(tid);
224 ThreadStart(tid, GetTid());
225 atomic_store(&p->tid, 0, memory_order_release);
226 return callback(param);
229 INTERCEPTOR(int, pthread_create, void *th, void *attr,
230 void *(*callback)(void *), void *param) {
232 EnsureMainThreadIDIsCorrect();
233 __sanitizer_pthread_attr_t myattr;
235 pthread_attr_init(&myattr);
238 AdjustStackSize(attr);
240 pthread_attr_getdetachstate(attr, &detached);
242 p.callback = callback;
244 atomic_store(&p.tid, 0, memory_order_relaxed);
247 // Ignore all allocations made by pthread_create: thread stack/TLS may be
248 // stored by pthread for future reuse even after thread destruction, and
249 // the linked list it's stored in doesn't even hold valid pointers to the
250 // objects, the latter are calculated by obscure pointer arithmetic.
251 ScopedInterceptorDisabler disabler;
252 res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
255 int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th, detached);
257 atomic_store(&p.tid, tid, memory_order_release);
258 while (atomic_load(&p.tid, memory_order_acquire) != 0)
259 internal_sched_yield();
262 pthread_attr_destroy(&myattr);
266 INTERCEPTOR(int, pthread_join, void *th, void **ret) {
268 int tid = ThreadTid((uptr)th);
269 int res = REAL(pthread_join)(th, ret);
277 void InitializeInterceptors() {
278 INTERCEPT_FUNCTION(malloc);
279 INTERCEPT_FUNCTION(free);
280 INTERCEPT_FUNCTION(cfree);
281 INTERCEPT_FUNCTION(calloc);
282 INTERCEPT_FUNCTION(realloc);
283 INTERCEPT_FUNCTION(memalign);
284 INTERCEPT_FUNCTION(posix_memalign);
285 INTERCEPT_FUNCTION(__libc_memalign);
286 INTERCEPT_FUNCTION(valloc);
287 INTERCEPT_FUNCTION(pvalloc);
288 INTERCEPT_FUNCTION(malloc_usable_size);
289 INTERCEPT_FUNCTION(mallinfo);
290 INTERCEPT_FUNCTION(mallopt);
291 INTERCEPT_FUNCTION(pthread_create);
292 INTERCEPT_FUNCTION(pthread_join);
294 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
295 Report("LeakSanitizer: failed to create thread key.\n");
300 } // namespace __lsan