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 GET_STACK_TRACE \
38 uptr stack_top = 0, stack_bottom = 0; \
40 bool fast = common_flags()->fast_unwind_on_malloc; \
41 if (fast && (t = CurrentThreadContext())) { \
42 stack_top = t->stack_end(); \
43 stack_bottom = t->stack_begin(); \
45 stack.Unwind(__sanitizer::common_flags()->malloc_context_size, \
46 StackTrace::GetCurrentPc(), \
47 GET_CURRENT_FRAME(), stack_top, stack_bottom, fast); \
50 #define ENSURE_LSAN_INITED do { \
51 CHECK(!lsan_init_is_running); \
56 ///// Malloc/free interceptors. /////
58 const bool kAlwaysClearMemory
= true;
64 INTERCEPTOR(void*, malloc
, uptr size
) {
67 return Allocate(stack
, size
, 1, kAlwaysClearMemory
);
70 INTERCEPTOR(void, free
, void *p
) {
75 INTERCEPTOR(void*, calloc
, uptr nmemb
, uptr size
) {
76 if (lsan_init_is_running
) {
77 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
78 const uptr kCallocPoolSize
= 1024;
79 static uptr calloc_memory_for_dlsym
[kCallocPoolSize
];
80 static uptr allocated
;
81 uptr size_in_words
= ((nmemb
* size
) + kWordSize
- 1) / kWordSize
;
82 void *mem
= (void*)&calloc_memory_for_dlsym
[allocated
];
83 allocated
+= size_in_words
;
84 CHECK(allocated
< kCallocPoolSize
);
87 if (CallocShouldReturnNullDueToOverflow(size
, nmemb
)) return 0;
91 return Allocate(stack
, size
, 1, true);
94 INTERCEPTOR(void*, realloc
, void *q
, uptr size
) {
97 return Reallocate(stack
, q
, size
, 1);
100 INTERCEPTOR(void*, memalign
, uptr alignment
, uptr size
) {
103 return Allocate(stack
, size
, alignment
, kAlwaysClearMemory
);
106 INTERCEPTOR(int, posix_memalign
, void **memptr
, uptr alignment
, uptr size
) {
109 *memptr
= Allocate(stack
, size
, alignment
, kAlwaysClearMemory
);
110 // FIXME: Return ENOMEM if user requested more than max alloc size.
114 INTERCEPTOR(void*, valloc
, uptr size
) {
118 size
= GetPageSizeCached();
119 return Allocate(stack
, size
, GetPageSizeCached(), kAlwaysClearMemory
);
122 INTERCEPTOR(uptr
, malloc_usable_size
, void *ptr
) {
124 return GetMallocUsableSize(ptr
);
127 struct fake_mallinfo
{
131 INTERCEPTOR(struct fake_mallinfo
, mallinfo
, void) {
132 struct fake_mallinfo res
;
133 internal_memset(&res
, 0, sizeof(res
));
137 INTERCEPTOR(int, mallopt
, int cmd
, int value
) {
141 INTERCEPTOR(void*, pvalloc
, uptr size
) {
144 uptr PageSize
= GetPageSizeCached();
145 size
= RoundUpTo(size
, PageSize
);
147 // pvalloc(0) should allocate one page.
150 return Allocate(stack
, size
, GetPageSizeCached(), kAlwaysClearMemory
);
153 INTERCEPTOR(void, cfree
, void *p
) ALIAS("free");
155 #define OPERATOR_NEW_BODY \
156 ENSURE_LSAN_INITED; \
158 return Allocate(stack, size, 1, kAlwaysClearMemory);
160 INTERCEPTOR_ATTRIBUTE
161 void *operator new(uptr size
) { OPERATOR_NEW_BODY
; }
162 INTERCEPTOR_ATTRIBUTE
163 void *operator new[](uptr size
) { OPERATOR_NEW_BODY
; }
164 INTERCEPTOR_ATTRIBUTE
165 void *operator new(uptr size
, std::nothrow_t
const&) { OPERATOR_NEW_BODY
; }
166 INTERCEPTOR_ATTRIBUTE
167 void *operator new[](uptr size
, std::nothrow_t
const&) { OPERATOR_NEW_BODY
; }
169 #define OPERATOR_DELETE_BODY \
170 ENSURE_LSAN_INITED; \
173 INTERCEPTOR_ATTRIBUTE
174 void operator delete(void *ptr
) { OPERATOR_DELETE_BODY
; }
175 INTERCEPTOR_ATTRIBUTE
176 void operator delete[](void *ptr
) { OPERATOR_DELETE_BODY
; }
177 INTERCEPTOR_ATTRIBUTE
178 void operator delete(void *ptr
, std::nothrow_t
const&) { OPERATOR_DELETE_BODY
; }
179 INTERCEPTOR_ATTRIBUTE
180 void operator delete[](void *ptr
, std::nothrow_t
const &) {
181 OPERATOR_DELETE_BODY
;
184 // We need this to intercept the __libc_memalign calls that are used to
185 // allocate dynamic TLS space in ld-linux.so.
186 INTERCEPTOR(void *, __libc_memalign
, uptr align
, uptr s
) ALIAS("memalign");
188 ///// Thread initialization and finalization. /////
190 static unsigned g_thread_finalize_key
;
192 static void thread_finalize(void *v
) {
195 if (pthread_setspecific(g_thread_finalize_key
, (void*)(iter
- 1))) {
196 Report("LeakSanitizer: failed to set thread key.\n");
205 void *(*callback
)(void *arg
);
207 atomic_uintptr_t tid
;
210 extern "C" void *__lsan_thread_start_func(void *arg
) {
211 ThreadParam
*p
= (ThreadParam
*)arg
;
212 void* (*callback
)(void *arg
) = p
->callback
;
213 void *param
= p
->param
;
214 // Wait until the last iteration to maximize the chance that we are the last
215 // destructor to run.
216 if (pthread_setspecific(g_thread_finalize_key
,
217 (void*)kPthreadDestructorIterations
)) {
218 Report("LeakSanitizer: failed to set thread key.\n");
222 while ((tid
= atomic_load(&p
->tid
, memory_order_acquire
)) == 0)
223 internal_sched_yield();
224 atomic_store(&p
->tid
, 0, memory_order_release
);
225 SetCurrentThread(tid
);
226 ThreadStart(tid
, GetTid());
227 return callback(param
);
230 INTERCEPTOR(int, pthread_create
, void *th
, void *attr
,
231 void *(*callback
)(void *), void *param
) {
233 EnsureMainThreadIDIsCorrect();
234 __sanitizer_pthread_attr_t myattr
;
236 pthread_attr_init(&myattr
);
239 AdjustStackSizeLinux(attr
);
241 pthread_attr_getdetachstate(attr
, &detached
);
243 p
.callback
= callback
;
245 atomic_store(&p
.tid
, 0, memory_order_relaxed
);
246 int res
= REAL(pthread_create
)(th
, attr
, __lsan_thread_start_func
, &p
);
248 int tid
= ThreadCreate(GetCurrentThread(), *(uptr
*)th
, detached
);
250 atomic_store(&p
.tid
, tid
, memory_order_release
);
251 while (atomic_load(&p
.tid
, memory_order_acquire
) != 0)
252 internal_sched_yield();
255 pthread_attr_destroy(&myattr
);
259 INTERCEPTOR(int, pthread_join
, void *th
, void **ret
) {
261 int tid
= ThreadTid((uptr
)th
);
262 int res
= REAL(pthread_join
)(th
, ret
);
270 void InitializeInterceptors() {
271 INTERCEPT_FUNCTION(malloc
);
272 INTERCEPT_FUNCTION(free
);
273 INTERCEPT_FUNCTION(cfree
);
274 INTERCEPT_FUNCTION(calloc
);
275 INTERCEPT_FUNCTION(realloc
);
276 INTERCEPT_FUNCTION(memalign
);
277 INTERCEPT_FUNCTION(posix_memalign
);
278 INTERCEPT_FUNCTION(__libc_memalign
);
279 INTERCEPT_FUNCTION(valloc
);
280 INTERCEPT_FUNCTION(pvalloc
);
281 INTERCEPT_FUNCTION(malloc_usable_size
);
282 INTERCEPT_FUNCTION(mallinfo
);
283 INTERCEPT_FUNCTION(mallopt
);
284 INTERCEPT_FUNCTION(pthread_create
);
285 INTERCEPT_FUNCTION(pthread_join
);
287 if (pthread_key_create(&g_thread_finalize_key
, &thread_finalize
)) {
288 Report("LeakSanitizer: failed to create thread key.\n");
293 } // namespace __lsan