xref: /freebsd/contrib/llvm-project/compiler-rt/lib/lsan/lsan_interceptors.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //=-- lsan_interceptors.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of LeakSanitizer.
10 // Interceptors for standalone LSan.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "interception/interception.h"
15 #include "sanitizer_common/sanitizer_allocator.h"
16 #include "sanitizer_common/sanitizer_allocator_dlsym.h"
17 #include "sanitizer_common/sanitizer_allocator_report.h"
18 #include "sanitizer_common/sanitizer_atomic.h"
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_flags.h"
21 #include "sanitizer_common/sanitizer_internal_defs.h"
22 #include "sanitizer_common/sanitizer_linux.h"
23 #include "sanitizer_common/sanitizer_platform_interceptors.h"
24 #include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
25 #include "sanitizer_common/sanitizer_platform_limits_posix.h"
26 #if SANITIZER_POSIX
27 #include "sanitizer_common/sanitizer_posix.h"
28 #endif
29 #include "sanitizer_common/sanitizer_tls_get_addr.h"
30 #include "lsan.h"
31 #include "lsan_allocator.h"
32 #include "lsan_common.h"
33 #include "lsan_thread.h"
34 
35 #include <stddef.h>
36 
37 using namespace __lsan;
38 
39 extern "C" {
40 int pthread_attr_init(void *attr);
41 int pthread_attr_destroy(void *attr);
42 int pthread_attr_getdetachstate(void *attr, int *v);
43 int pthread_key_create(unsigned *key, void (*destructor)(void* v));
44 int pthread_setspecific(unsigned key, const void *v);
45 }
46 
47 struct DlsymAlloc : DlSymAllocator<DlsymAlloc> {
UseImplDlsymAlloc48   static bool UseImpl() { return lsan_init_is_running; }
OnAllocateDlsymAlloc49   static void OnAllocate(const void *ptr, uptr size) {
50 #if CAN_SANITIZE_LEAKS
51     // Suppress leaks from dlerror(). Previously dlsym hack on global array was
52     // used by leak sanitizer as a root region.
53     __lsan_register_root_region(ptr, size);
54 #endif
55   }
OnFreeDlsymAlloc56   static void OnFree(const void *ptr, uptr size) {
57 #if CAN_SANITIZE_LEAKS
58     __lsan_unregister_root_region(ptr, size);
59 #endif
60   }
61 };
62 
63 ///// Malloc/free interceptors. /////
64 
65 namespace std {
66   struct nothrow_t;
67   enum class align_val_t: size_t;
68 }
69 
70 #if !SANITIZER_APPLE
INTERCEPTOR(void *,malloc,uptr size)71 INTERCEPTOR(void*, malloc, uptr size) {
72   if (DlsymAlloc::Use())
73     return DlsymAlloc::Allocate(size);
74   ENSURE_LSAN_INITED;
75   GET_STACK_TRACE_MALLOC;
76   return lsan_malloc(size, stack);
77 }
78 
INTERCEPTOR(void,free,void * p)79 INTERCEPTOR(void, free, void *p) {
80   if (DlsymAlloc::PointerIsMine(p))
81     return DlsymAlloc::Free(p);
82   ENSURE_LSAN_INITED;
83   lsan_free(p);
84 }
85 
INTERCEPTOR(void *,calloc,uptr nmemb,uptr size)86 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
87   if (DlsymAlloc::Use())
88     return DlsymAlloc::Callocate(nmemb, size);
89   ENSURE_LSAN_INITED;
90   GET_STACK_TRACE_MALLOC;
91   return lsan_calloc(nmemb, size, stack);
92 }
93 
INTERCEPTOR(void *,realloc,void * ptr,uptr size)94 INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
95   if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
96     return DlsymAlloc::Realloc(ptr, size);
97   ENSURE_LSAN_INITED;
98   GET_STACK_TRACE_MALLOC;
99   return lsan_realloc(ptr, size, stack);
100 }
101 
INTERCEPTOR(void *,reallocarray,void * q,uptr nmemb,uptr size)102 INTERCEPTOR(void*, reallocarray, void *q, uptr nmemb, uptr size) {
103   ENSURE_LSAN_INITED;
104   GET_STACK_TRACE_MALLOC;
105   return lsan_reallocarray(q, nmemb, size, stack);
106 }
107 
INTERCEPTOR(int,posix_memalign,void ** memptr,uptr alignment,uptr size)108 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
109   ENSURE_LSAN_INITED;
110   GET_STACK_TRACE_MALLOC;
111   return lsan_posix_memalign(memptr, alignment, size, stack);
112 }
113 
INTERCEPTOR(void *,valloc,uptr size)114 INTERCEPTOR(void*, valloc, uptr size) {
115   ENSURE_LSAN_INITED;
116   GET_STACK_TRACE_MALLOC;
117   return lsan_valloc(size, stack);
118 }
119 #endif  // !SANITIZER_APPLE
120 
121 #if SANITIZER_INTERCEPT_MEMALIGN
INTERCEPTOR(void *,memalign,uptr alignment,uptr size)122 INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
123   ENSURE_LSAN_INITED;
124   GET_STACK_TRACE_MALLOC;
125   return lsan_memalign(alignment, size, stack);
126 }
127 #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
128 #else
129 #define LSAN_MAYBE_INTERCEPT_MEMALIGN
130 #endif  // SANITIZER_INTERCEPT_MEMALIGN
131 
132 #if SANITIZER_INTERCEPT___LIBC_MEMALIGN
INTERCEPTOR(void *,__libc_memalign,uptr alignment,uptr size)133 INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
134   ENSURE_LSAN_INITED;
135   GET_STACK_TRACE_MALLOC;
136   void *res = lsan_memalign(alignment, size, stack);
137   DTLS_on_libc_memalign(res, size);
138   return res;
139 }
140 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
141 #else
142 #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
143 #endif  // SANITIZER_INTERCEPT___LIBC_MEMALIGN
144 
145 #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
INTERCEPTOR(void *,aligned_alloc,uptr alignment,uptr size)146 INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
147   ENSURE_LSAN_INITED;
148   GET_STACK_TRACE_MALLOC;
149   return lsan_aligned_alloc(alignment, size, stack);
150 }
151 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
152 #else
153 #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
154 #endif
155 
156 #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE
INTERCEPTOR(uptr,malloc_usable_size,void * ptr)157 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
158   ENSURE_LSAN_INITED;
159   return GetMallocUsableSize(ptr);
160 }
161 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
162         INTERCEPT_FUNCTION(malloc_usable_size)
163 #else
164 #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
165 #endif
166 
167 #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
168 struct fake_mallinfo {
169   int x[10];
170 };
171 
INTERCEPTOR(struct fake_mallinfo,mallinfo,void)172 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
173   struct fake_mallinfo res;
174   internal_memset(&res, 0, sizeof(res));
175   return res;
176 }
177 #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
178 
INTERCEPTOR(int,mallopt,int cmd,int value)179 INTERCEPTOR(int, mallopt, int cmd, int value) {
180   return 0;
181 }
182 #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
183 #else
184 #define LSAN_MAYBE_INTERCEPT_MALLINFO
185 #define LSAN_MAYBE_INTERCEPT_MALLOPT
186 #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
187 
188 #if SANITIZER_INTERCEPT_PVALLOC
INTERCEPTOR(void *,pvalloc,uptr size)189 INTERCEPTOR(void*, pvalloc, uptr size) {
190   ENSURE_LSAN_INITED;
191   GET_STACK_TRACE_MALLOC;
192   return lsan_pvalloc(size, stack);
193 }
194 #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
195 #else
196 #define LSAN_MAYBE_INTERCEPT_PVALLOC
197 #endif // SANITIZER_INTERCEPT_PVALLOC
198 
199 #if SANITIZER_INTERCEPT_CFREE
200 INTERCEPTOR(void, cfree, void *p) ALIAS(WRAP(free));
201 #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
202 #else
203 #define LSAN_MAYBE_INTERCEPT_CFREE
204 #endif // SANITIZER_INTERCEPT_CFREE
205 
206 #if SANITIZER_INTERCEPT_MCHECK_MPROBE
INTERCEPTOR(int,mcheck,void (* abortfunc)(int mstatus))207 INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
208   return 0;
209 }
210 
INTERCEPTOR(int,mcheck_pedantic,void (* abortfunc)(int mstatus))211 INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
212   return 0;
213 }
214 
INTERCEPTOR(int,mprobe,void * ptr)215 INTERCEPTOR(int, mprobe, void *ptr) {
216   return 0;
217 }
218 #endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
219 
220 
221 // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
222 #define OPERATOR_NEW_BODY(nothrow)\
223   ENSURE_LSAN_INITED;\
224   GET_STACK_TRACE_MALLOC;\
225   void *res = lsan_malloc(size, stack);\
226   if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
227   return res;
228 #define OPERATOR_NEW_BODY_ALIGN(nothrow)\
229   ENSURE_LSAN_INITED;\
230   GET_STACK_TRACE_MALLOC;\
231   void *res = lsan_memalign((uptr)align, size, stack);\
232   if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
233   return res;
234 
235 #define OPERATOR_DELETE_BODY\
236   ENSURE_LSAN_INITED;\
237   lsan_free(ptr);
238 
239 // On OS X it's not enough to just provide our own 'operator new' and
240 // 'operator delete' implementations, because they're going to be in the runtime
241 // dylib, and the main executable will depend on both the runtime dylib and
242 // libstdc++, each of has its implementation of new and delete.
243 // To make sure that C++ allocation/deallocation operators are overridden on
244 // OS X we need to intercept them using their mangled names.
245 #if !SANITIZER_APPLE
246 
247 INTERCEPTOR_ATTRIBUTE
operator new(size_t size)248 void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
249 INTERCEPTOR_ATTRIBUTE
operator new[](size_t size)250 void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
251 INTERCEPTOR_ATTRIBUTE
operator new(size_t size,std::nothrow_t const &)252 void *operator new(size_t size, std::nothrow_t const&)
253 { OPERATOR_NEW_BODY(true /*nothrow*/); }
254 INTERCEPTOR_ATTRIBUTE
operator new[](size_t size,std::nothrow_t const &)255 void *operator new[](size_t size, std::nothrow_t const&)
256 { OPERATOR_NEW_BODY(true /*nothrow*/); }
257 INTERCEPTOR_ATTRIBUTE
operator new(size_t size,std::align_val_t align)258 void *operator new(size_t size, std::align_val_t align)
259 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
260 INTERCEPTOR_ATTRIBUTE
operator new[](size_t size,std::align_val_t align)261 void *operator new[](size_t size, std::align_val_t align)
262 { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
263 INTERCEPTOR_ATTRIBUTE
operator new(size_t size,std::align_val_t align,std::nothrow_t const &)264 void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
265 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
266 INTERCEPTOR_ATTRIBUTE
operator new[](size_t size,std::align_val_t align,std::nothrow_t const &)267 void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
268 { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
269 
270 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr)271 void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
272 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr)273 void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
274 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr,std::nothrow_t const &)275 void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
276 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr,std::nothrow_t const &)277 void operator delete[](void *ptr, std::nothrow_t const &)
278 { OPERATOR_DELETE_BODY; }
279 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr,size_t size)280 void operator delete(void *ptr, size_t size) NOEXCEPT
281 { OPERATOR_DELETE_BODY; }
282 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr,size_t size)283 void operator delete[](void *ptr, size_t size) NOEXCEPT
284 { OPERATOR_DELETE_BODY; }
285 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr,std::align_val_t)286 void operator delete(void *ptr, std::align_val_t) NOEXCEPT
287 { OPERATOR_DELETE_BODY; }
288 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr,std::align_val_t)289 void operator delete[](void *ptr, std::align_val_t) NOEXCEPT
290 { OPERATOR_DELETE_BODY; }
291 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr,std::align_val_t,std::nothrow_t const &)292 void operator delete(void *ptr, std::align_val_t, std::nothrow_t const&)
293 { OPERATOR_DELETE_BODY; }
294 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr,std::align_val_t,std::nothrow_t const &)295 void operator delete[](void *ptr, std::align_val_t, std::nothrow_t const&)
296 { OPERATOR_DELETE_BODY; }
297 INTERCEPTOR_ATTRIBUTE
operator delete(void * ptr,size_t size,std::align_val_t)298 void operator delete(void *ptr, size_t size, std::align_val_t) NOEXCEPT
299 { OPERATOR_DELETE_BODY; }
300 INTERCEPTOR_ATTRIBUTE
operator delete[](void * ptr,size_t size,std::align_val_t)301 void operator delete[](void *ptr, size_t size, std::align_val_t) NOEXCEPT
302 { OPERATOR_DELETE_BODY; }
303 
304 #else  // SANITIZER_APPLE
305 
INTERCEPTOR(void *,_Znwm,size_t size)306 INTERCEPTOR(void *, _Znwm, size_t size)
307 { OPERATOR_NEW_BODY(false /*nothrow*/); }
INTERCEPTOR(void *,_Znam,size_t size)308 INTERCEPTOR(void *, _Znam, size_t size)
309 { OPERATOR_NEW_BODY(false /*nothrow*/); }
INTERCEPTOR(void *,_ZnwmRKSt9nothrow_t,size_t size,std::nothrow_t const &)310 INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&)
311 { OPERATOR_NEW_BODY(true /*nothrow*/); }
INTERCEPTOR(void *,_ZnamRKSt9nothrow_t,size_t size,std::nothrow_t const &)312 INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&)
313 { OPERATOR_NEW_BODY(true /*nothrow*/); }
314 
INTERCEPTOR(void,_ZdlPv,void * ptr)315 INTERCEPTOR(void, _ZdlPv, void *ptr)
316 { OPERATOR_DELETE_BODY; }
INTERCEPTOR(void,_ZdaPv,void * ptr)317 INTERCEPTOR(void, _ZdaPv, void *ptr)
318 { OPERATOR_DELETE_BODY; }
INTERCEPTOR(void,_ZdlPvRKSt9nothrow_t,void * ptr,std::nothrow_t const &)319 INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
320 { OPERATOR_DELETE_BODY; }
INTERCEPTOR(void,_ZdaPvRKSt9nothrow_t,void * ptr,std::nothrow_t const &)321 INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
322 { OPERATOR_DELETE_BODY; }
323 
324 #endif  // !SANITIZER_APPLE
325 
326 
327 ///// Thread initialization and finalization. /////
328 
329 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD && !SANITIZER_FUCHSIA
330 static unsigned g_thread_finalize_key;
331 
thread_finalize(void * v)332 static void thread_finalize(void *v) {
333   uptr iter = (uptr)v;
334   if (iter > 1) {
335     if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
336       Report("LeakSanitizer: failed to set thread key.\n");
337       Die();
338     }
339     return;
340   }
341   ThreadFinish();
342 }
343 #endif
344 
345 #if SANITIZER_NETBSD
INTERCEPTOR(void,_lwp_exit)346 INTERCEPTOR(void, _lwp_exit) {
347   ENSURE_LSAN_INITED;
348   ThreadFinish();
349   REAL(_lwp_exit)();
350 }
351 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT INTERCEPT_FUNCTION(_lwp_exit)
352 #else
353 #define LSAN_MAYBE_INTERCEPT__LWP_EXIT
354 #endif
355 
356 #if SANITIZER_INTERCEPT_THR_EXIT
INTERCEPTOR(void,thr_exit,tid_t * state)357 INTERCEPTOR(void, thr_exit, tid_t *state) {
358   ENSURE_LSAN_INITED;
359   ThreadFinish();
360   REAL(thr_exit)(state);
361 }
362 #define LSAN_MAYBE_INTERCEPT_THR_EXIT INTERCEPT_FUNCTION(thr_exit)
363 #else
364 #define LSAN_MAYBE_INTERCEPT_THR_EXIT
365 #endif
366 
367 #if SANITIZER_INTERCEPT___CXA_ATEXIT
INTERCEPTOR(int,__cxa_atexit,void (* func)(void *),void * arg,void * dso_handle)368 INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
369             void *dso_handle) {
370   __lsan::ScopedInterceptorDisabler disabler;
371   return REAL(__cxa_atexit)(func, arg, dso_handle);
372 }
373 #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT INTERCEPT_FUNCTION(__cxa_atexit)
374 #else
375 #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT
376 #endif
377 
378 #if SANITIZER_INTERCEPT_ATEXIT
INTERCEPTOR(int,atexit,void (* f)())379 INTERCEPTOR(int, atexit, void (*f)()) {
380   __lsan::ScopedInterceptorDisabler disabler;
381   return REAL(__cxa_atexit)((void (*)(void *a))f, 0, 0);
382 }
383 #define LSAN_MAYBE_INTERCEPT_ATEXIT INTERCEPT_FUNCTION(atexit)
384 #else
385 #define LSAN_MAYBE_INTERCEPT_ATEXIT
386 #endif
387 
388 #if SANITIZER_INTERCEPT_PTHREAD_ATFORK
389 extern "C" {
390 extern int _pthread_atfork(void (*prepare)(), void (*parent)(),
391                            void (*child)());
392 }
393 
INTERCEPTOR(int,pthread_atfork,void (* prepare)(),void (* parent)(),void (* child)())394 INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
395             void (*child)()) {
396   __lsan::ScopedInterceptorDisabler disabler;
397   // REAL(pthread_atfork) cannot be called due to symbol indirections at least
398   // on NetBSD
399   return _pthread_atfork(prepare, parent, child);
400 }
401 #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK INTERCEPT_FUNCTION(pthread_atfork)
402 #else
403 #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK
404 #endif
405 
406 #if SANITIZER_INTERCEPT_STRERROR
INTERCEPTOR(char *,strerror,int errnum)407 INTERCEPTOR(char *, strerror, int errnum) {
408   __lsan::ScopedInterceptorDisabler disabler;
409   return REAL(strerror)(errnum);
410 }
411 #define LSAN_MAYBE_INTERCEPT_STRERROR INTERCEPT_FUNCTION(strerror)
412 #else
413 #define LSAN_MAYBE_INTERCEPT_STRERROR
414 #endif
415 
416 #if SANITIZER_POSIX
417 
418 template <bool Detached>
ThreadStartFunc(void * arg)419 static void *ThreadStartFunc(void *arg) {
420   u32 parent_tid = (uptr)arg;
421   uptr tid = ThreadCreate(parent_tid, Detached);
422   // Wait until the last iteration to maximize the chance that we are the last
423   // destructor to run.
424 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
425   if (pthread_setspecific(g_thread_finalize_key,
426                           (void*)GetPthreadDestructorIterations())) {
427     Report("LeakSanitizer: failed to set thread key.\n");
428     Die();
429   }
430 #  endif
431   ThreadStart(tid, GetTid());
432   auto self = GetThreadSelf();
433   auto args = GetThreadArgRetval().GetArgs(self);
434   void *retval = (*args.routine)(args.arg_retval);
435   GetThreadArgRetval().Finish(self, retval);
436   return retval;
437 }
438 
INTERCEPTOR(int,pthread_create,void * th,void * attr,void * (* callback)(void *),void * param)439 INTERCEPTOR(int, pthread_create, void *th, void *attr,
440             void *(*callback)(void *), void *param) {
441   ENSURE_LSAN_INITED;
442   EnsureMainThreadIDIsCorrect();
443 
444   bool detached = [attr]() {
445     int d = 0;
446     return attr && !pthread_attr_getdetachstate(attr, &d) && IsStateDetached(d);
447   }();
448 
449   __sanitizer_pthread_attr_t myattr;
450   if (!attr) {
451     pthread_attr_init(&myattr);
452     attr = &myattr;
453   }
454   AdjustStackSize(attr);
455   uptr this_tid = GetCurrentThreadId();
456   int result;
457   {
458     // Ignore all allocations made by pthread_create: thread stack/TLS may be
459     // stored by pthread for future reuse even after thread destruction, and
460     // the linked list it's stored in doesn't even hold valid pointers to the
461     // objects, the latter are calculated by obscure pointer arithmetic.
462     ScopedInterceptorDisabler disabler;
463     GetThreadArgRetval().Create(detached, {callback, param}, [&]() -> uptr {
464       result = REAL(pthread_create)(
465           th, attr, detached ? ThreadStartFunc<true> : ThreadStartFunc<false>,
466           (void *)this_tid);
467       return result ? 0 : *(uptr *)(th);
468     });
469   }
470   if (attr == &myattr)
471     pthread_attr_destroy(&myattr);
472   return result;
473 }
474 
INTERCEPTOR(int,pthread_join,void * thread,void ** retval)475 INTERCEPTOR(int, pthread_join, void *thread, void **retval) {
476   int result;
477   GetThreadArgRetval().Join((uptr)thread, [&]() {
478     result = REAL(pthread_join)(thread, retval);
479     return !result;
480   });
481   return result;
482 }
483 
INTERCEPTOR(int,pthread_detach,void * thread)484 INTERCEPTOR(int, pthread_detach, void *thread) {
485   int result;
486   GetThreadArgRetval().Detach((uptr)thread, [&]() {
487     result = REAL(pthread_detach)(thread);
488     return !result;
489   });
490   return result;
491 }
492 
INTERCEPTOR(void,pthread_exit,void * retval)493 INTERCEPTOR(void, pthread_exit, void *retval) {
494   GetThreadArgRetval().Finish(GetThreadSelf(), retval);
495   REAL(pthread_exit)(retval);
496 }
497 
498 #  if SANITIZER_INTERCEPT_TRYJOIN
INTERCEPTOR(int,pthread_tryjoin_np,void * thread,void ** ret)499 INTERCEPTOR(int, pthread_tryjoin_np, void *thread, void **ret) {
500   int result;
501   GetThreadArgRetval().Join((uptr)thread, [&]() {
502     result = REAL(pthread_tryjoin_np)(thread, ret);
503     return !result;
504   });
505   return result;
506 }
507 #    define LSAN_MAYBE_INTERCEPT_TRYJOIN INTERCEPT_FUNCTION(pthread_tryjoin_np)
508 #  else
509 #    define LSAN_MAYBE_INTERCEPT_TRYJOIN
510 #  endif  // SANITIZER_INTERCEPT_TRYJOIN
511 
512 #  if SANITIZER_INTERCEPT_TIMEDJOIN
INTERCEPTOR(int,pthread_timedjoin_np,void * thread,void ** ret,const struct timespec * abstime)513 INTERCEPTOR(int, pthread_timedjoin_np, void *thread, void **ret,
514             const struct timespec *abstime) {
515   int result;
516   GetThreadArgRetval().Join((uptr)thread, [&]() {
517     result = REAL(pthread_timedjoin_np)(thread, ret, abstime);
518     return !result;
519   });
520   return result;
521 }
522 #    define LSAN_MAYBE_INTERCEPT_TIMEDJOIN \
523       INTERCEPT_FUNCTION(pthread_timedjoin_np)
524 #  else
525 #    define LSAN_MAYBE_INTERCEPT_TIMEDJOIN
526 #  endif  // SANITIZER_INTERCEPT_TIMEDJOIN
527 
528 DEFINE_INTERNAL_PTHREAD_FUNCTIONS
529 
INTERCEPTOR(void,_exit,int status)530 INTERCEPTOR(void, _exit, int status) {
531   if (status == 0 && HasReportedLeaks()) status = common_flags()->exitcode;
532   REAL(_exit)(status);
533 }
534 
535 #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
536 #define SIGNAL_INTERCEPTOR_ENTER() ENSURE_LSAN_INITED
537 #include "sanitizer_common/sanitizer_signal_interceptors.inc"
538 
539 #endif  // SANITIZER_POSIX
540 
541 namespace __lsan {
542 
InitializeInterceptors()543 void InitializeInterceptors() {
544   // Fuchsia doesn't use interceptors that require any setup.
545 #if !SANITIZER_FUCHSIA
546   __interception::DoesNotSupportStaticLinking();
547   InitializeSignalInterceptors();
548 
549   INTERCEPT_FUNCTION(malloc);
550   INTERCEPT_FUNCTION(free);
551   LSAN_MAYBE_INTERCEPT_CFREE;
552   INTERCEPT_FUNCTION(calloc);
553   INTERCEPT_FUNCTION(realloc);
554   LSAN_MAYBE_INTERCEPT_MEMALIGN;
555   LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
556   LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
557   INTERCEPT_FUNCTION(posix_memalign);
558   INTERCEPT_FUNCTION(valloc);
559   LSAN_MAYBE_INTERCEPT_PVALLOC;
560   LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
561   LSAN_MAYBE_INTERCEPT_MALLINFO;
562   LSAN_MAYBE_INTERCEPT_MALLOPT;
563   INTERCEPT_FUNCTION(pthread_create);
564   INTERCEPT_FUNCTION(pthread_join);
565   INTERCEPT_FUNCTION(pthread_detach);
566   INTERCEPT_FUNCTION(pthread_exit);
567   LSAN_MAYBE_INTERCEPT_TIMEDJOIN;
568   LSAN_MAYBE_INTERCEPT_TRYJOIN;
569   INTERCEPT_FUNCTION(_exit);
570 
571   LSAN_MAYBE_INTERCEPT__LWP_EXIT;
572   LSAN_MAYBE_INTERCEPT_THR_EXIT;
573 
574   LSAN_MAYBE_INTERCEPT___CXA_ATEXIT;
575   LSAN_MAYBE_INTERCEPT_ATEXIT;
576   LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK;
577 
578   LSAN_MAYBE_INTERCEPT_STRERROR;
579 
580 #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
581   if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
582     Report("LeakSanitizer: failed to create thread key.\n");
583     Die();
584   }
585 #endif
586 
587 #endif  // !SANITIZER_FUCHSIA
588 }
589 
590 } // namespace __lsan
591