[PR c++/839888] Baselink tsubst ICE
[official-gcc.git] / libsanitizer / lsan / lsan_interceptors.cc
blobc9279aad676697c0e9765566b4ee1e27dcd3caef
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 "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_interceptors.h"
21 #include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
22 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
23 #include "sanitizer_common/sanitizer_posix.h"
24 #include "sanitizer_common/sanitizer_tls_get_addr.h"
25 #include "lsan.h"
26 #include "lsan_allocator.h"
27 #include "lsan_common.h"
28 #include "lsan_thread.h"
30 #include <stddef.h>
32 using namespace __lsan;
34 extern "C" {
35 int pthread_attr_init(void *attr);
36 int pthread_attr_destroy(void *attr);
37 int pthread_attr_getdetachstate(void *attr, int *v);
38 int pthread_key_create(unsigned *key, void (*destructor)(void* v));
39 int pthread_setspecific(unsigned key, const void *v);
42 ///// Malloc/free interceptors. /////
44 namespace std {
45 struct nothrow_t;
46 enum class align_val_t: size_t;
49 #if !SANITIZER_MAC
50 INTERCEPTOR(void*, malloc, uptr size) {
51 ENSURE_LSAN_INITED;
52 GET_STACK_TRACE_MALLOC;
53 return lsan_malloc(size, stack);
56 INTERCEPTOR(void, free, void *p) {
57 ENSURE_LSAN_INITED;
58 lsan_free(p);
61 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
62 if (lsan_init_is_running) {
63 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
64 const uptr kCallocPoolSize = 1024;
65 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
66 static uptr allocated;
67 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
68 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
69 allocated += size_in_words;
70 CHECK(allocated < kCallocPoolSize);
71 return mem;
73 ENSURE_LSAN_INITED;
74 GET_STACK_TRACE_MALLOC;
75 return lsan_calloc(nmemb, size, stack);
78 INTERCEPTOR(void*, realloc, void *q, uptr size) {
79 ENSURE_LSAN_INITED;
80 GET_STACK_TRACE_MALLOC;
81 return lsan_realloc(q, size, stack);
84 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
85 ENSURE_LSAN_INITED;
86 GET_STACK_TRACE_MALLOC;
87 *memptr = lsan_memalign(alignment, size, stack);
88 // FIXME: Return ENOMEM if user requested more than max alloc size.
89 return 0;
92 INTERCEPTOR(void*, valloc, uptr size) {
93 ENSURE_LSAN_INITED;
94 GET_STACK_TRACE_MALLOC;
95 return lsan_valloc(size, stack);
97 #endif
99 #if SANITIZER_INTERCEPT_MEMALIGN
100 INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
101 ENSURE_LSAN_INITED;
102 GET_STACK_TRACE_MALLOC;
103 return lsan_memalign(alignment, size, stack);
105 #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
107 INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
108 ENSURE_LSAN_INITED;
109 GET_STACK_TRACE_MALLOC;
110 void *res = lsan_memalign(alignment, size, stack);
111 DTLS_on_libc_memalign(res, size);
112 return res;
114 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
115 #else
116 #define LSAN_MAYBE_INTERCEPT_MEMALIGN
117 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
118 #endif // SANITIZER_INTERCEPT_MEMALIGN
120 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
121 INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
122 ENSURE_LSAN_INITED;
123 GET_STACK_TRACE_MALLOC;
124 return lsan_memalign(alignment, size, stack);
126 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
127 #else
128 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
129 #endif
131 #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE
132 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
133 ENSURE_LSAN_INITED;
134 return GetMallocUsableSize(ptr);
136 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
137 INTERCEPT_FUNCTION(malloc_usable_size)
138 #else
139 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
140 #endif
142 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
143 struct fake_mallinfo {
144 int x[10];
147 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
148 struct fake_mallinfo res;
149 internal_memset(&res, 0, sizeof(res));
150 return res;
152 #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
154 INTERCEPTOR(int, mallopt, int cmd, int value) {
155 return -1;
157 #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
158 #else
159 #define LSAN_MAYBE_INTERCEPT_MALLINFO
160 #define LSAN_MAYBE_INTERCEPT_MALLOPT
161 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
163 #if SANITIZER_INTERCEPT_PVALLOC
164 INTERCEPTOR(void*, pvalloc, uptr size) {
165 ENSURE_LSAN_INITED;
166 GET_STACK_TRACE_MALLOC;
167 uptr PageSize = GetPageSizeCached();
168 size = RoundUpTo(size, PageSize);
169 if (size == 0) {
170 // pvalloc(0) should allocate one page.
171 size = PageSize;
173 return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
175 #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
176 #else
177 #define LSAN_MAYBE_INTERCEPT_PVALLOC
178 #endif // SANITIZER_INTERCEPT_PVALLOC
180 #if SANITIZER_INTERCEPT_CFREE
181 INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
182 #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
183 #else
184 #define LSAN_MAYBE_INTERCEPT_CFREE
185 #endif // SANITIZER_INTERCEPT_CFREE
187 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
188 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
189 return 0;
192 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
193 return 0;
196 INTERCEPTOR(int, mprobe, void *ptr) {
197 return 0;
199 #endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
202 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
203 #define OPERATOR_NEW_BODY(nothrow) \
204 ENSURE_LSAN_INITED; \
205 GET_STACK_TRACE_MALLOC; \
206 void *res = lsan_malloc(size, stack); \
207 if (!nothrow && UNLIKELY(!res)) DieOnFailure::OnOOM(); \
208 return res;
209 #define OPERATOR_NEW_BODY_ALIGN(nothrow) \
210 ENSURE_LSAN_INITED; \
211 GET_STACK_TRACE_MALLOC; \
212 void *res = lsan_memalign((uptr)align, size, stack); \
213 if (!nothrow && UNLIKELY(!res)) DieOnFailure::OnOOM(); \
214 return res;
216 #define OPERATOR_DELETE_BODY \
217 ENSURE_LSAN_INITED; \
218 lsan_free(ptr);
220 // On OS X it's not enough to just provide our own 'operator new' and
221 // 'operator delete' implementations, because they're going to be in the runtime
222 // dylib, and the main executable will depend on both the runtime dylib and
223 // libstdc++, each of has its implementation of new and delete.
224 // To make sure that C++ allocation/deallocation operators are overridden on
225 // OS X we need to intercept them using their mangled names.
226 #if !SANITIZER_MAC
228 INTERCEPTOR_ATTRIBUTE
229 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
230 INTERCEPTOR_ATTRIBUTE
231 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
232 INTERCEPTOR_ATTRIBUTE
233 void *operator new(size_t size, std::nothrow_t const&)
234 { OPERATOR_NEW_BODY(true /*nothrow*/); }
235 INTERCEPTOR_ATTRIBUTE
236 void *operator new[](size_t size, std::nothrow_t const&)
237 { OPERATOR_NEW_BODY(true /*nothrow*/); }
238 INTERCEPTOR_ATTRIBUTE
239 void *operator new(size_t size, std::align_val_t align)
240 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
241 INTERCEPTOR_ATTRIBUTE
242 void *operator new[](size_t size, std::align_val_t align)
243 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
244 INTERCEPTOR_ATTRIBUTE
245 void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
246 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
247 INTERCEPTOR_ATTRIBUTE
248 void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
249 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
251 INTERCEPTOR_ATTRIBUTE
252 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
253 INTERCEPTOR_ATTRIBUTE
254 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
255 INTERCEPTOR_ATTRIBUTE
256 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
257 INTERCEPTOR_ATTRIBUTE
258 void operator delete[](void *ptr, std::nothrow_t const &)
259 { OPERATOR_DELETE_BODY; }
260 INTERCEPTOR_ATTRIBUTE
261 void operator delete(void *ptr, size_t size) NOEXCEPT
262 { OPERATOR_DELETE_BODY; }
263 INTERCEPTOR_ATTRIBUTE
264 void operator delete[](void *ptr, size_t size) NOEXCEPT
265 { OPERATOR_DELETE_BODY; }
266 INTERCEPTOR_ATTRIBUTE
267 void operator delete(void *ptr, std::align_val_t) NOEXCEPT
268 { OPERATOR_DELETE_BODY; }
269 INTERCEPTOR_ATTRIBUTE
270 void operator delete[](void *ptr, std::align_val_t) NOEXCEPT
271 { OPERATOR_DELETE_BODY; }
272 INTERCEPTOR_ATTRIBUTE
273 void operator delete(void *ptr, std::align_val_t, std::nothrow_t const&)
274 { OPERATOR_DELETE_BODY; }
275 INTERCEPTOR_ATTRIBUTE
276 void operator delete[](void *ptr, std::align_val_t, std::nothrow_t const&)
277 { OPERATOR_DELETE_BODY; }
278 INTERCEPTOR_ATTRIBUTE
279 void operator delete(void *ptr, size_t size, std::align_val_t) NOEXCEPT
280 { OPERATOR_DELETE_BODY; }
281 INTERCEPTOR_ATTRIBUTE
282 void operator delete[](void *ptr, size_t size, std::align_val_t) NOEXCEPT
283 { OPERATOR_DELETE_BODY; }
285 #else // SANITIZER_MAC
287 INTERCEPTOR(void *, _Znwm, size_t size)
288 { OPERATOR_NEW_BODY(false /*nothrow*/); }
289 INTERCEPTOR(void *, _Znam, size_t size)
290 { OPERATOR_NEW_BODY(false /*nothrow*/); }
291 INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&)
292 { OPERATOR_NEW_BODY(true /*nothrow*/); }
293 INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&)
294 { OPERATOR_NEW_BODY(true /*nothrow*/); }
296 INTERCEPTOR(void, _ZdlPv, void *ptr)
297 { OPERATOR_DELETE_BODY; }
298 INTERCEPTOR(void, _ZdaPv, void *ptr)
299 { OPERATOR_DELETE_BODY; }
300 INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
301 { OPERATOR_DELETE_BODY; }
302 INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
303 { OPERATOR_DELETE_BODY; }
305 #endif // !SANITIZER_MAC
308 ///// Thread initialization and finalization. /////
310 static unsigned g_thread_finalize_key;
312 static void thread_finalize(void *v) {
313 uptr iter = (uptr)v;
314 if (iter > 1) {
315 if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
316 Report("LeakSanitizer: failed to set thread key.\n");
317 Die();
319 return;
321 ThreadFinish();
324 struct ThreadParam {
325 void *(*callback)(void *arg);
326 void *param;
327 atomic_uintptr_t tid;
330 extern "C" void *__lsan_thread_start_func(void *arg) {
331 ThreadParam *p = (ThreadParam*)arg;
332 void* (*callback)(void *arg) = p->callback;
333 void *param = p->param;
334 // Wait until the last iteration to maximize the chance that we are the last
335 // destructor to run.
336 if (pthread_setspecific(g_thread_finalize_key,
337 (void*)GetPthreadDestructorIterations())) {
338 Report("LeakSanitizer: failed to set thread key.\n");
339 Die();
341 int tid = 0;
342 while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
343 internal_sched_yield();
344 SetCurrentThread(tid);
345 ThreadStart(tid, GetTid());
346 atomic_store(&p->tid, 0, memory_order_release);
347 return callback(param);
350 INTERCEPTOR(int, pthread_create, void *th, void *attr,
351 void *(*callback)(void *), void *param) {
352 ENSURE_LSAN_INITED;
353 EnsureMainThreadIDIsCorrect();
354 __sanitizer_pthread_attr_t myattr;
355 if (!attr) {
356 pthread_attr_init(&myattr);
357 attr = &myattr;
359 AdjustStackSize(attr);
360 int detached = 0;
361 pthread_attr_getdetachstate(attr, &detached);
362 ThreadParam p;
363 p.callback = callback;
364 p.param = param;
365 atomic_store(&p.tid, 0, memory_order_relaxed);
366 int res;
368 // Ignore all allocations made by pthread_create: thread stack/TLS may be
369 // stored by pthread for future reuse even after thread destruction, and
370 // the linked list it's stored in doesn't even hold valid pointers to the
371 // objects, the latter are calculated by obscure pointer arithmetic.
372 ScopedInterceptorDisabler disabler;
373 res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
375 if (res == 0) {
376 int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th,
377 IsStateDetached(detached));
378 CHECK_NE(tid, 0);
379 atomic_store(&p.tid, tid, memory_order_release);
380 while (atomic_load(&p.tid, memory_order_acquire) != 0)
381 internal_sched_yield();
383 if (attr == &myattr)
384 pthread_attr_destroy(&myattr);
385 return res;
388 INTERCEPTOR(int, pthread_join, void *th, void **ret) {
389 ENSURE_LSAN_INITED;
390 int tid = ThreadTid((uptr)th);
391 int res = REAL(pthread_join)(th, ret);
392 if (res == 0)
393 ThreadJoin(tid);
394 return res;
397 INTERCEPTOR(void, _exit, int status) {
398 if (status == 0 && HasReportedLeaks()) status = common_flags()->exitcode;
399 REAL(_exit)(status);
402 #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
403 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
405 namespace __lsan {
407 void InitializeInterceptors() {
408 InitializeSignalInterceptors();
410 INTERCEPT_FUNCTION(malloc);
411 INTERCEPT_FUNCTION(free);
412 LSAN_MAYBE_INTERCEPT_CFREE;
413 INTERCEPT_FUNCTION(calloc);
414 INTERCEPT_FUNCTION(realloc);
415 LSAN_MAYBE_INTERCEPT_MEMALIGN;
416 LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
417 LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
418 INTERCEPT_FUNCTION(posix_memalign);
419 INTERCEPT_FUNCTION(valloc);
420 LSAN_MAYBE_INTERCEPT_PVALLOC;
421 LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
422 LSAN_MAYBE_INTERCEPT_MALLINFO;
423 LSAN_MAYBE_INTERCEPT_MALLOPT;
424 INTERCEPT_FUNCTION(pthread_create);
425 INTERCEPT_FUNCTION(pthread_join);
426 INTERCEPT_FUNCTION(_exit);
428 if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
429 Report("LeakSanitizer: failed to create thread key.\n");
430 Die();
434 } // namespace __lsan