1 //=-- lsan_interceptors.cc ------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of LeakSanitizer.
9 // Interceptors for standalone LSan.
11 //===----------------------------------------------------------------------===//
13 #include "interception/interception.h"
14 #include "sanitizer_common/sanitizer_allocator.h"
15 #include "sanitizer_common/sanitizer_atomic.h"
16 #include "sanitizer_common/sanitizer_common.h"
17 #include "sanitizer_common/sanitizer_flags.h"
18 #include "sanitizer_common/sanitizer_internal_defs.h"
19 #include "sanitizer_common/sanitizer_linux.h"
20 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
22 #include "lsan_allocator.h"
23 #include "lsan_thread.h"
25 using namespace __lsan
;
28 int pthread_attr_init(void *attr
);
29 int pthread_attr_destroy(void *attr
);
30 int pthread_attr_getdetachstate(void *attr
, int *v
);
31 int pthread_key_create(unsigned *key
, void (*destructor
)(void* v
));
32 int pthread_setspecific(unsigned key
, const void *v
);
35 #define ENSURE_LSAN_INITED do { \
36 CHECK(!lsan_init_is_running); \
41 ///// Malloc/free interceptors. /////
43 const bool kAlwaysClearMemory
= true;
49 INTERCEPTOR(void*, malloc
, uptr size
) {
51 GET_STACK_TRACE_MALLOC
;
52 return Allocate(stack
, size
, 1, kAlwaysClearMemory
);
55 INTERCEPTOR(void, free
, void *p
) {
60 INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
) {
61 if (lsan_init_is_running
) {
62 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
63 const uptr kCallocPoolSize
= 1024;
64 static uptr calloc_memory_for_dlsym
[kCallocPoolSize
];
65 static uptr allocated
;
66 uptr size_in_words
= ((nmemb
* size
) + kWordSize
- 1) / kWordSize
;
67 void *mem
= (void*)&calloc_memory_for_dlsym
[allocated
];
68 allocated
+= size_in_words
;
69 CHECK(allocated
< kCallocPoolSize
);
72 if (CallocShouldReturnNullDueToOverflow(size
, nmemb
)) return nullptr;
74 GET_STACK_TRACE_MALLOC
;
76 return Allocate(stack
, size
, 1, true);
79 INTERCEPTOR(void*, realloc
, void *q
, uptr size
) {
81 GET_STACK_TRACE_MALLOC
;
82 return Reallocate(stack
, q
, size
, 1);
85 INTERCEPTOR(void*, memalign
, uptr alignment
, uptr size
) {
87 GET_STACK_TRACE_MALLOC
;
88 return Allocate(stack
, size
, alignment
, kAlwaysClearMemory
);
91 INTERCEPTOR(void*, aligned_alloc
, uptr alignment
, uptr size
) {
93 GET_STACK_TRACE_MALLOC
;
94 return Allocate(stack
, size
, alignment
, kAlwaysClearMemory
);
97 INTERCEPTOR(int, posix_memalign
, void **memptr
, uptr alignment
, uptr size
) {
99 GET_STACK_TRACE_MALLOC
;
100 *memptr
= Allocate(stack
, size
, alignment
, kAlwaysClearMemory
);
101 // FIXME: Return ENOMEM if user requested more than max alloc size.
105 INTERCEPTOR(void*, valloc
, uptr size
) {
107 GET_STACK_TRACE_MALLOC
;
109 size
= GetPageSizeCached();
110 return Allocate(stack
, size
, GetPageSizeCached(), kAlwaysClearMemory
);
113 INTERCEPTOR(uptr
, malloc_usable_size
, void *ptr
) {
115 return GetMallocUsableSize(ptr
);
118 struct fake_mallinfo
{
122 INTERCEPTOR(struct fake_mallinfo
, mallinfo
, void) {
123 struct fake_mallinfo res
;
124 internal_memset(&res
, 0, sizeof(res
));
128 INTERCEPTOR(int, mallopt
, int cmd
, int value
) {
132 INTERCEPTOR(void*, pvalloc
, uptr size
) {
134 GET_STACK_TRACE_MALLOC
;
135 uptr PageSize
= GetPageSizeCached();
136 size
= RoundUpTo(size
, PageSize
);
138 // pvalloc(0) should allocate one page.
141 return Allocate(stack
, size
, GetPageSizeCached(), kAlwaysClearMemory
);
144 INTERCEPTOR(void, cfree
, void *p
) ALIAS(WRAPPER_NAME(free
));
146 #define OPERATOR_NEW_BODY \
147 ENSURE_LSAN_INITED; \
148 GET_STACK_TRACE_MALLOC; \
149 return Allocate(stack, size, 1, kAlwaysClearMemory);
151 INTERCEPTOR_ATTRIBUTE
152 void *operator new(uptr size
) { OPERATOR_NEW_BODY
; }
153 INTERCEPTOR_ATTRIBUTE
154 void *operator new[](uptr size
) { OPERATOR_NEW_BODY
; }
155 INTERCEPTOR_ATTRIBUTE
156 void *operator new(uptr size
, std::nothrow_t
const&) { OPERATOR_NEW_BODY
; }
157 INTERCEPTOR_ATTRIBUTE
158 void *operator new[](uptr size
, std::nothrow_t
const&) { OPERATOR_NEW_BODY
; }
160 #define OPERATOR_DELETE_BODY \
161 ENSURE_LSAN_INITED; \
164 INTERCEPTOR_ATTRIBUTE
165 void operator delete(void *ptr
) NOEXCEPT
{ OPERATOR_DELETE_BODY
; }
166 INTERCEPTOR_ATTRIBUTE
167 void operator delete[](void *ptr
) NOEXCEPT
{ OPERATOR_DELETE_BODY
; }
168 INTERCEPTOR_ATTRIBUTE
169 void operator delete(void *ptr
, std::nothrow_t
const&) { OPERATOR_DELETE_BODY
; }
170 INTERCEPTOR_ATTRIBUTE
171 void operator delete[](void *ptr
, std::nothrow_t
const &) {
172 OPERATOR_DELETE_BODY
;
175 // We need this to intercept the __libc_memalign calls that are used to
176 // allocate dynamic TLS space in ld-linux.so.
177 INTERCEPTOR(void *, __libc_memalign
, uptr align
, uptr s
)
178 ALIAS(WRAPPER_NAME(memalign
));
180 ///// Thread initialization and finalization. /////
182 static unsigned g_thread_finalize_key
;
184 static void thread_finalize(void *v
) {
187 if (pthread_setspecific(g_thread_finalize_key
, (void*)(iter
- 1))) {
188 Report("LeakSanitizer: failed to set thread key.\n");
197 void *(*callback
)(void *arg
);
199 atomic_uintptr_t tid
;
202 extern "C" void *__lsan_thread_start_func(void *arg
) {
203 ThreadParam
*p
= (ThreadParam
*)arg
;
204 void* (*callback
)(void *arg
) = p
->callback
;
205 void *param
= p
->param
;
206 // Wait until the last iteration to maximize the chance that we are the last
207 // destructor to run.
208 if (pthread_setspecific(g_thread_finalize_key
,
209 (void*)GetPthreadDestructorIterations())) {
210 Report("LeakSanitizer: failed to set thread key.\n");
214 while ((tid
= atomic_load(&p
->tid
, memory_order_acquire
)) == 0)
215 internal_sched_yield();
216 SetCurrentThread(tid
);
217 ThreadStart(tid
, GetTid());
218 atomic_store(&p
->tid
, 0, memory_order_release
);
219 return callback(param
);
222 INTERCEPTOR(int, pthread_create
, void *th
, void *attr
,
223 void *(*callback
)(void *), void *param
) {
225 EnsureMainThreadIDIsCorrect();
226 __sanitizer_pthread_attr_t myattr
;
228 pthread_attr_init(&myattr
);
231 AdjustStackSize(attr
);
233 pthread_attr_getdetachstate(attr
, &detached
);
235 p
.callback
= callback
;
237 atomic_store(&p
.tid
, 0, memory_order_relaxed
);
238 int res
= REAL(pthread_create
)(th
, attr
, __lsan_thread_start_func
, &p
);
240 int tid
= ThreadCreate(GetCurrentThread(), *(uptr
*)th
, detached
);
242 atomic_store(&p
.tid
, tid
, memory_order_release
);
243 while (atomic_load(&p
.tid
, memory_order_acquire
) != 0)
244 internal_sched_yield();
247 pthread_attr_destroy(&myattr
);
251 INTERCEPTOR(int, pthread_join
, void *th
, void **ret
) {
253 int tid
= ThreadTid((uptr
)th
);
254 int res
= REAL(pthread_join
)(th
, ret
);
262 void InitializeInterceptors() {
263 INTERCEPT_FUNCTION(malloc
);
264 INTERCEPT_FUNCTION(free
);
265 INTERCEPT_FUNCTION(cfree
);
266 INTERCEPT_FUNCTION(calloc
);
267 INTERCEPT_FUNCTION(realloc
);
268 INTERCEPT_FUNCTION(memalign
);
269 INTERCEPT_FUNCTION(posix_memalign
);
270 INTERCEPT_FUNCTION(__libc_memalign
);
271 INTERCEPT_FUNCTION(valloc
);
272 INTERCEPT_FUNCTION(pvalloc
);
273 INTERCEPT_FUNCTION(malloc_usable_size
);
274 INTERCEPT_FUNCTION(mallinfo
);
275 INTERCEPT_FUNCTION(mallopt
);
276 INTERCEPT_FUNCTION(pthread_create
);
277 INTERCEPT_FUNCTION(pthread_join
);
279 if (pthread_key_create(&g_thread_finalize_key
, &thread_finalize
)) {
280 Report("LeakSanitizer: failed to create thread key.\n");
285 } // namespace __lsan