* config/abi/post/alpha-linux-gnu/baseline_symbols.txt: Update.
[official-gcc.git] / libsanitizer / lsan / lsan_interceptors.cc
blobdfaad325672e93418d3ed26d83d2d0d6f80aa6ea
1 //=-- lsan_interceptors.cc ------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of LeakSanitizer.
9 // Interceptors for standalone LSan.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_allocator.h"
14 #include "sanitizer_common/sanitizer_atomic.h"
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "sanitizer_common/sanitizer_flags.h"
17 #include "sanitizer_common/sanitizer_interception.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"
21 #include "lsan.h"
22 #include "lsan_allocator.h"
23 #include "lsan_thread.h"
25 using namespace __lsan;
27 extern "C" {
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 \
36 StackTrace stack; \
37 { \
38 uptr stack_top = 0, stack_bottom = 0; \
39 ThreadContext *t; \
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(); \
44 } \
45 stack.Unwind(__sanitizer::common_flags()->malloc_context_size, \
46 StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), 0, \
47 stack_top, stack_bottom, fast); \
50 #define ENSURE_LSAN_INITED do { \
51 CHECK(!lsan_init_is_running); \
52 if (!lsan_inited) \
53 __lsan_init(); \
54 } while (0)
56 ///// Malloc/free interceptors. /////
58 const bool kAlwaysClearMemory = true;
60 namespace std {
61 struct nothrow_t;
64 INTERCEPTOR(void*, malloc, uptr size) {
65 ENSURE_LSAN_INITED;
66 GET_STACK_TRACE;
67 return Allocate(stack, size, 1, kAlwaysClearMemory);
70 INTERCEPTOR(void, free, void *p) {
71 ENSURE_LSAN_INITED;
72 Deallocate(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);
85 return mem;
87 if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return 0;
88 ENSURE_LSAN_INITED;
89 GET_STACK_TRACE;
90 size *= nmemb;
91 return Allocate(stack, size, 1, true);
94 INTERCEPTOR(void*, realloc, void *q, uptr size) {
95 ENSURE_LSAN_INITED;
96 GET_STACK_TRACE;
97 return Reallocate(stack, q, size, 1);
100 INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
101 ENSURE_LSAN_INITED;
102 GET_STACK_TRACE;
103 return Allocate(stack, size, alignment, kAlwaysClearMemory);
106 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
107 ENSURE_LSAN_INITED;
108 GET_STACK_TRACE;
109 *memptr = Allocate(stack, size, alignment, kAlwaysClearMemory);
110 // FIXME: Return ENOMEM if user requested more than max alloc size.
111 return 0;
114 INTERCEPTOR(void*, valloc, uptr size) {
115 ENSURE_LSAN_INITED;
116 GET_STACK_TRACE;
117 if (size == 0)
118 size = GetPageSizeCached();
119 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
122 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
123 ENSURE_LSAN_INITED;
124 return GetMallocUsableSize(ptr);
127 struct fake_mallinfo {
128 int x[10];
131 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
132 struct fake_mallinfo res;
133 internal_memset(&res, 0, sizeof(res));
134 return res;
137 INTERCEPTOR(int, mallopt, int cmd, int value) {
138 return -1;
141 INTERCEPTOR(void*, pvalloc, uptr size) {
142 ENSURE_LSAN_INITED;
143 GET_STACK_TRACE;
144 uptr PageSize = GetPageSizeCached();
145 size = RoundUpTo(size, PageSize);
146 if (size == 0) {
147 // pvalloc(0) should allocate one page.
148 size = PageSize;
150 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
153 INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
155 #define OPERATOR_NEW_BODY \
156 ENSURE_LSAN_INITED; \
157 GET_STACK_TRACE; \
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; \
171 Deallocate(ptr);
173 INTERCEPTOR_ATTRIBUTE
174 void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
175 INTERCEPTOR_ATTRIBUTE
176 void operator delete[](void *ptr) throw() { 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)
187 ALIAS(WRAPPER_NAME(memalign));
189 ///// Thread initialization and finalization. /////
191 static unsigned g_thread_finalize_key;
193 static void thread_finalize(void *v) {
194 uptr iter = (uptr)v;
195 if (iter > 1) {
196 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
197 Report("LeakSanitizer: failed to set thread key.\n");
198 Die();
200 return;
202 ThreadFinish();
205 struct ThreadParam {
206 void *(*callback)(void *arg);
207 void *param;
208 atomic_uintptr_t tid;
211 extern "C" void *__lsan_thread_start_func(void *arg) {
212 ThreadParam *p = (ThreadParam*)arg;
213 void* (*callback)(void *arg) = p->callback;
214 void *param = p->param;
215 // Wait until the last iteration to maximize the chance that we are the last
216 // destructor to run.
217 if (pthread_setspecific(g_thread_finalize_key,
218 (void*)kPthreadDestructorIterations)) {
219 Report("LeakSanitizer: failed to set thread key.\n");
220 Die();
222 int tid = 0;
223 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
224 internal_sched_yield();
225 atomic_store(&p->tid, 0, memory_order_release);
226 SetCurrentThread(tid);
227 ThreadStart(tid, GetTid());
228 return callback(param);
231 INTERCEPTOR(int, pthread_create, void *th, void *attr,
232 void *(*callback)(void *), void *param) {
233 ENSURE_LSAN_INITED;
234 EnsureMainThreadIDIsCorrect();
235 __sanitizer_pthread_attr_t myattr;
236 if (attr == 0) {
237 pthread_attr_init(&myattr);
238 attr = &myattr;
240 AdjustStackSize(attr);
241 int detached = 0;
242 pthread_attr_getdetachstate(attr, &detached);
243 ThreadParam p;
244 p.callback = callback;
245 p.param = param;
246 atomic_store(&p.tid, 0, memory_order_relaxed);
247 int res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
248 if (res == 0) {
249 int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th, detached);
250 CHECK_NE(tid, 0);
251 atomic_store(&p.tid, tid, memory_order_release);
252 while (atomic_load(&p.tid, memory_order_acquire) != 0)
253 internal_sched_yield();
255 if (attr == &myattr)
256 pthread_attr_destroy(&myattr);
257 return res;
260 INTERCEPTOR(int, pthread_join, void *th, void **ret) {
261 ENSURE_LSAN_INITED;
262 int tid = ThreadTid((uptr)th);
263 int res = REAL(pthread_join)(th, ret);
264 if (res == 0)
265 ThreadJoin(tid);
266 return res;
269 namespace __lsan {
271 void InitializeInterceptors() {
272 INTERCEPT_FUNCTION(malloc);
273 INTERCEPT_FUNCTION(free);
274 INTERCEPT_FUNCTION(cfree);
275 INTERCEPT_FUNCTION(calloc);
276 INTERCEPT_FUNCTION(realloc);
277 INTERCEPT_FUNCTION(memalign);
278 INTERCEPT_FUNCTION(posix_memalign);
279 INTERCEPT_FUNCTION(__libc_memalign);
280 INTERCEPT_FUNCTION(valloc);
281 INTERCEPT_FUNCTION(pvalloc);
282 INTERCEPT_FUNCTION(malloc_usable_size);
283 INTERCEPT_FUNCTION(mallinfo);
284 INTERCEPT_FUNCTION(mallopt);
285 INTERCEPT_FUNCTION(pthread_create);
286 INTERCEPT_FUNCTION(pthread_join);
288 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
289 Report("LeakSanitizer: failed to create thread key.\n");
290 Die();
294 } // namespace __lsan