lsan_interceptors.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. #include "interception/interception.h"
  14. #include "sanitizer_common/sanitizer_allocator.h"
  15. #include "sanitizer_common/sanitizer_allocator_dlsym.h"
  16. #include "sanitizer_common/sanitizer_allocator_report.h"
  17. #include "sanitizer_common/sanitizer_atomic.h"
  18. #include "sanitizer_common/sanitizer_common.h"
  19. #include "sanitizer_common/sanitizer_flags.h"
  20. #include "sanitizer_common/sanitizer_internal_defs.h"
  21. #include "sanitizer_common/sanitizer_linux.h"
  22. #include "sanitizer_common/sanitizer_platform_interceptors.h"
  23. #include "sanitizer_common/sanitizer_platform_limits_netbsd.h"
  24. #include "sanitizer_common/sanitizer_platform_limits_posix.h"
  25. #if SANITIZER_POSIX
  26. #include "sanitizer_common/sanitizer_posix.h"
  27. #endif
  28. #include "sanitizer_common/sanitizer_tls_get_addr.h"
  29. #include "lsan.h"
  30. #include "lsan_allocator.h"
  31. #include "lsan_common.h"
  32. #include "lsan_thread.h"
  33. #include <stddef.h>
  34. using namespace __lsan;
  35. extern "C" {
  36. int pthread_attr_init(void *attr);
  37. int pthread_attr_destroy(void *attr);
  38. int pthread_attr_getdetachstate(void *attr, int *v);
  39. int pthread_key_create(unsigned *key, void (*destructor)(void* v));
  40. int pthread_setspecific(unsigned key, const void *v);
  41. }
  42. struct DlsymAlloc : DlSymAllocator<DlsymAlloc> {
  43. static bool UseImpl() { return lsan_init_is_running; }
  44. static void OnAllocate(const void *ptr, uptr size) {
  45. #if CAN_SANITIZE_LEAKS
  46. // Suppress leaks from dlerror(). Previously dlsym hack on global array was
  47. // used by leak sanitizer as a root region.
  48. __lsan_register_root_region(ptr, size);
  49. #endif
  50. }
  51. static void OnFree(const void *ptr, uptr size) {
  52. #if CAN_SANITIZE_LEAKS
  53. __lsan_unregister_root_region(ptr, size);
  54. #endif
  55. }
  56. };
  57. ///// Malloc/free interceptors. /////
  58. namespace std {
  59. struct nothrow_t;
  60. enum class align_val_t: size_t;
  61. }
  62. #if !SANITIZER_APPLE
  63. INTERCEPTOR(void*, malloc, uptr size) {
  64. if (DlsymAlloc::Use())
  65. return DlsymAlloc::Allocate(size);
  66. ENSURE_LSAN_INITED;
  67. GET_STACK_TRACE_MALLOC;
  68. return lsan_malloc(size, stack);
  69. }
  70. INTERCEPTOR(void, free, void *p) {
  71. if (DlsymAlloc::PointerIsMine(p))
  72. return DlsymAlloc::Free(p);
  73. ENSURE_LSAN_INITED;
  74. lsan_free(p);
  75. }
  76. INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
  77. if (DlsymAlloc::Use())
  78. return DlsymAlloc::Callocate(nmemb, size);
  79. ENSURE_LSAN_INITED;
  80. GET_STACK_TRACE_MALLOC;
  81. return lsan_calloc(nmemb, size, stack);
  82. }
  83. INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
  84. if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
  85. return DlsymAlloc::Realloc(ptr, size);
  86. ENSURE_LSAN_INITED;
  87. GET_STACK_TRACE_MALLOC;
  88. return lsan_realloc(ptr, size, stack);
  89. }
  90. INTERCEPTOR(void*, reallocarray, void *q, uptr nmemb, uptr size) {
  91. ENSURE_LSAN_INITED;
  92. GET_STACK_TRACE_MALLOC;
  93. return lsan_reallocarray(q, nmemb, size, stack);
  94. }
  95. INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
  96. ENSURE_LSAN_INITED;
  97. GET_STACK_TRACE_MALLOC;
  98. return lsan_posix_memalign(memptr, alignment, size, stack);
  99. }
  100. INTERCEPTOR(void*, valloc, uptr size) {
  101. ENSURE_LSAN_INITED;
  102. GET_STACK_TRACE_MALLOC;
  103. return lsan_valloc(size, stack);
  104. }
  105. #endif // !SANITIZER_APPLE
  106. #if SANITIZER_INTERCEPT_MEMALIGN
  107. INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
  108. ENSURE_LSAN_INITED;
  109. GET_STACK_TRACE_MALLOC;
  110. return lsan_memalign(alignment, size, stack);
  111. }
  112. #define LSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
  113. #else
  114. #define LSAN_MAYBE_INTERCEPT_MEMALIGN
  115. #endif // SANITIZER_INTERCEPT_MEMALIGN
  116. #if SANITIZER_INTERCEPT___LIBC_MEMALIGN
  117. INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
  118. ENSURE_LSAN_INITED;
  119. GET_STACK_TRACE_MALLOC;
  120. void *res = lsan_memalign(alignment, size, stack);
  121. DTLS_on_libc_memalign(res, size);
  122. return res;
  123. }
  124. #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign)
  125. #else
  126. #define LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
  127. #endif // SANITIZER_INTERCEPT___LIBC_MEMALIGN
  128. #if SANITIZER_INTERCEPT_ALIGNED_ALLOC
  129. INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
  130. ENSURE_LSAN_INITED;
  131. GET_STACK_TRACE_MALLOC;
  132. return lsan_aligned_alloc(alignment, size, stack);
  133. }
  134. #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
  135. #else
  136. #define LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
  137. #endif
  138. #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE
  139. INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
  140. ENSURE_LSAN_INITED;
  141. return GetMallocUsableSize(ptr);
  142. }
  143. #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \
  144. INTERCEPT_FUNCTION(malloc_usable_size)
  145. #else
  146. #define LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
  147. #endif
  148. #if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
  149. struct fake_mallinfo {
  150. int x[10];
  151. };
  152. INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
  153. struct fake_mallinfo res;
  154. internal_memset(&res, 0, sizeof(res));
  155. return res;
  156. }
  157. #define LSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
  158. INTERCEPTOR(int, mallopt, int cmd, int value) {
  159. return 0;
  160. }
  161. #define LSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt)
  162. #else
  163. #define LSAN_MAYBE_INTERCEPT_MALLINFO
  164. #define LSAN_MAYBE_INTERCEPT_MALLOPT
  165. #endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
  166. #if SANITIZER_INTERCEPT_PVALLOC
  167. INTERCEPTOR(void*, pvalloc, uptr size) {
  168. ENSURE_LSAN_INITED;
  169. GET_STACK_TRACE_MALLOC;
  170. return lsan_pvalloc(size, stack);
  171. }
  172. #define LSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
  173. #else
  174. #define LSAN_MAYBE_INTERCEPT_PVALLOC
  175. #endif // SANITIZER_INTERCEPT_PVALLOC
  176. #if SANITIZER_INTERCEPT_CFREE
  177. INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
  178. #define LSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree)
  179. #else
  180. #define LSAN_MAYBE_INTERCEPT_CFREE
  181. #endif // SANITIZER_INTERCEPT_CFREE
  182. #if SANITIZER_INTERCEPT_MCHECK_MPROBE
  183. INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
  184. return 0;
  185. }
  186. INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
  187. return 0;
  188. }
  189. INTERCEPTOR(int, mprobe, void *ptr) {
  190. return 0;
  191. }
  192. #endif // SANITIZER_INTERCEPT_MCHECK_MPROBE
  193. // TODO(alekseys): throw std::bad_alloc instead of dying on OOM.
  194. #define OPERATOR_NEW_BODY(nothrow)\
  195. ENSURE_LSAN_INITED;\
  196. GET_STACK_TRACE_MALLOC;\
  197. void *res = lsan_malloc(size, stack);\
  198. if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
  199. return res;
  200. #define OPERATOR_NEW_BODY_ALIGN(nothrow)\
  201. ENSURE_LSAN_INITED;\
  202. GET_STACK_TRACE_MALLOC;\
  203. void *res = lsan_memalign((uptr)align, size, stack);\
  204. if (!nothrow && UNLIKELY(!res)) ReportOutOfMemory(size, &stack);\
  205. return res;
  206. #define OPERATOR_DELETE_BODY\
  207. ENSURE_LSAN_INITED;\
  208. lsan_free(ptr);
  209. // On OS X it's not enough to just provide our own 'operator new' and
  210. // 'operator delete' implementations, because they're going to be in the runtime
  211. // dylib, and the main executable will depend on both the runtime dylib and
  212. // libstdc++, each of has its implementation of new and delete.
  213. // To make sure that C++ allocation/deallocation operators are overridden on
  214. // OS X we need to intercept them using their mangled names.
  215. #if !SANITIZER_APPLE
  216. INTERCEPTOR_ATTRIBUTE
  217. void *operator new(size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
  218. INTERCEPTOR_ATTRIBUTE
  219. void *operator new[](size_t size) { OPERATOR_NEW_BODY(false /*nothrow*/); }
  220. INTERCEPTOR_ATTRIBUTE
  221. void *operator new(size_t size, std::nothrow_t const&)
  222. { OPERATOR_NEW_BODY(true /*nothrow*/); }
  223. INTERCEPTOR_ATTRIBUTE
  224. void *operator new[](size_t size, std::nothrow_t const&)
  225. { OPERATOR_NEW_BODY(true /*nothrow*/); }
  226. INTERCEPTOR_ATTRIBUTE
  227. void *operator new(size_t size, std::align_val_t align)
  228. { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
  229. INTERCEPTOR_ATTRIBUTE
  230. void *operator new[](size_t size, std::align_val_t align)
  231. { OPERATOR_NEW_BODY_ALIGN(false /*nothrow*/); }
  232. INTERCEPTOR_ATTRIBUTE
  233. void *operator new(size_t size, std::align_val_t align, std::nothrow_t const&)
  234. { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
  235. INTERCEPTOR_ATTRIBUTE
  236. void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const&)
  237. { OPERATOR_NEW_BODY_ALIGN(true /*nothrow*/); }
  238. INTERCEPTOR_ATTRIBUTE
  239. void operator delete(void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
  240. INTERCEPTOR_ATTRIBUTE
  241. void operator delete[](void *ptr) NOEXCEPT { OPERATOR_DELETE_BODY; }
  242. INTERCEPTOR_ATTRIBUTE
  243. void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
  244. INTERCEPTOR_ATTRIBUTE
  245. void operator delete[](void *ptr, std::nothrow_t const &)
  246. { OPERATOR_DELETE_BODY; }
  247. INTERCEPTOR_ATTRIBUTE
  248. void operator delete(void *ptr, size_t size) NOEXCEPT
  249. { OPERATOR_DELETE_BODY; }
  250. INTERCEPTOR_ATTRIBUTE
  251. void operator delete[](void *ptr, size_t size) NOEXCEPT
  252. { OPERATOR_DELETE_BODY; }
  253. INTERCEPTOR_ATTRIBUTE
  254. void operator delete(void *ptr, std::align_val_t) NOEXCEPT
  255. { OPERATOR_DELETE_BODY; }
  256. INTERCEPTOR_ATTRIBUTE
  257. void operator delete[](void *ptr, std::align_val_t) NOEXCEPT
  258. { OPERATOR_DELETE_BODY; }
  259. INTERCEPTOR_ATTRIBUTE
  260. void operator delete(void *ptr, std::align_val_t, std::nothrow_t const&)
  261. { OPERATOR_DELETE_BODY; }
  262. INTERCEPTOR_ATTRIBUTE
  263. void operator delete[](void *ptr, std::align_val_t, std::nothrow_t const&)
  264. { OPERATOR_DELETE_BODY; }
  265. INTERCEPTOR_ATTRIBUTE
  266. void operator delete(void *ptr, size_t size, std::align_val_t) NOEXCEPT
  267. { OPERATOR_DELETE_BODY; }
  268. INTERCEPTOR_ATTRIBUTE
  269. void operator delete[](void *ptr, size_t size, std::align_val_t) NOEXCEPT
  270. { OPERATOR_DELETE_BODY; }
  271. #else // SANITIZER_APPLE
  272. INTERCEPTOR(void *, _Znwm, size_t size)
  273. { OPERATOR_NEW_BODY(false /*nothrow*/); }
  274. INTERCEPTOR(void *, _Znam, size_t size)
  275. { OPERATOR_NEW_BODY(false /*nothrow*/); }
  276. INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&)
  277. { OPERATOR_NEW_BODY(true /*nothrow*/); }
  278. INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&)
  279. { OPERATOR_NEW_BODY(true /*nothrow*/); }
  280. INTERCEPTOR(void, _ZdlPv, void *ptr)
  281. { OPERATOR_DELETE_BODY; }
  282. INTERCEPTOR(void, _ZdaPv, void *ptr)
  283. { OPERATOR_DELETE_BODY; }
  284. INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
  285. { OPERATOR_DELETE_BODY; }
  286. INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&)
  287. { OPERATOR_DELETE_BODY; }
  288. #endif // !SANITIZER_APPLE
  289. ///// Thread initialization and finalization. /////
  290. #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD && !SANITIZER_FUCHSIA
  291. static unsigned g_thread_finalize_key;
  292. static void thread_finalize(void *v) {
  293. uptr iter = (uptr)v;
  294. if (iter > 1) {
  295. if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
  296. Report("LeakSanitizer: failed to set thread key.\n");
  297. Die();
  298. }
  299. return;
  300. }
  301. ThreadFinish();
  302. }
  303. #endif
  304. #if SANITIZER_NETBSD
  305. INTERCEPTOR(void, _lwp_exit) {
  306. ENSURE_LSAN_INITED;
  307. ThreadFinish();
  308. REAL(_lwp_exit)();
  309. }
  310. #define LSAN_MAYBE_INTERCEPT__LWP_EXIT INTERCEPT_FUNCTION(_lwp_exit)
  311. #else
  312. #define LSAN_MAYBE_INTERCEPT__LWP_EXIT
  313. #endif
  314. #if SANITIZER_INTERCEPT_THR_EXIT
  315. INTERCEPTOR(void, thr_exit, tid_t *state) {
  316. ENSURE_LSAN_INITED;
  317. ThreadFinish();
  318. REAL(thr_exit)(state);
  319. }
  320. #define LSAN_MAYBE_INTERCEPT_THR_EXIT INTERCEPT_FUNCTION(thr_exit)
  321. #else
  322. #define LSAN_MAYBE_INTERCEPT_THR_EXIT
  323. #endif
  324. #if SANITIZER_INTERCEPT___CXA_ATEXIT
  325. INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
  326. void *dso_handle) {
  327. __lsan::ScopedInterceptorDisabler disabler;
  328. return REAL(__cxa_atexit)(func, arg, dso_handle);
  329. }
  330. #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT INTERCEPT_FUNCTION(__cxa_atexit)
  331. #else
  332. #define LSAN_MAYBE_INTERCEPT___CXA_ATEXIT
  333. #endif
  334. #if SANITIZER_INTERCEPT_ATEXIT
  335. INTERCEPTOR(int, atexit, void (*f)()) {
  336. __lsan::ScopedInterceptorDisabler disabler;
  337. return REAL(__cxa_atexit)((void (*)(void *a))f, 0, 0);
  338. }
  339. #define LSAN_MAYBE_INTERCEPT_ATEXIT INTERCEPT_FUNCTION(atexit)
  340. #else
  341. #define LSAN_MAYBE_INTERCEPT_ATEXIT
  342. #endif
  343. #if SANITIZER_INTERCEPT_PTHREAD_ATFORK
  344. extern "C" {
  345. extern int _pthread_atfork(void (*prepare)(), void (*parent)(),
  346. void (*child)());
  347. };
  348. INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(),
  349. void (*child)()) {
  350. __lsan::ScopedInterceptorDisabler disabler;
  351. // REAL(pthread_atfork) cannot be called due to symbol indirections at least
  352. // on NetBSD
  353. return _pthread_atfork(prepare, parent, child);
  354. }
  355. #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK INTERCEPT_FUNCTION(pthread_atfork)
  356. #else
  357. #define LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK
  358. #endif
  359. #if SANITIZER_INTERCEPT_STRERROR
  360. INTERCEPTOR(char *, strerror, int errnum) {
  361. __lsan::ScopedInterceptorDisabler disabler;
  362. return REAL(strerror)(errnum);
  363. }
  364. #define LSAN_MAYBE_INTERCEPT_STRERROR INTERCEPT_FUNCTION(strerror)
  365. #else
  366. #define LSAN_MAYBE_INTERCEPT_STRERROR
  367. #endif
  368. #if SANITIZER_POSIX
  369. struct ThreadParam {
  370. void *(*callback)(void *arg);
  371. void *param;
  372. atomic_uintptr_t tid;
  373. };
  374. extern "C" void *__lsan_thread_start_func(void *arg) {
  375. ThreadParam *p = (ThreadParam*)arg;
  376. void* (*callback)(void *arg) = p->callback;
  377. void *param = p->param;
  378. // Wait until the last iteration to maximize the chance that we are the last
  379. // destructor to run.
  380. #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
  381. if (pthread_setspecific(g_thread_finalize_key,
  382. (void*)GetPthreadDestructorIterations())) {
  383. Report("LeakSanitizer: failed to set thread key.\n");
  384. Die();
  385. }
  386. #endif
  387. int tid = 0;
  388. while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
  389. internal_sched_yield();
  390. ThreadStart(tid, GetTid());
  391. atomic_store(&p->tid, 0, memory_order_release);
  392. return callback(param);
  393. }
  394. INTERCEPTOR(int, pthread_create, void *th, void *attr,
  395. void *(*callback)(void *), void *param) {
  396. ENSURE_LSAN_INITED;
  397. EnsureMainThreadIDIsCorrect();
  398. __sanitizer_pthread_attr_t myattr;
  399. if (!attr) {
  400. pthread_attr_init(&myattr);
  401. attr = &myattr;
  402. }
  403. AdjustStackSize(attr);
  404. int detached = 0;
  405. pthread_attr_getdetachstate(attr, &detached);
  406. ThreadParam p;
  407. p.callback = callback;
  408. p.param = param;
  409. atomic_store(&p.tid, 0, memory_order_relaxed);
  410. int res;
  411. {
  412. // Ignore all allocations made by pthread_create: thread stack/TLS may be
  413. // stored by pthread for future reuse even after thread destruction, and
  414. // the linked list it's stored in doesn't even hold valid pointers to the
  415. // objects, the latter are calculated by obscure pointer arithmetic.
  416. ScopedInterceptorDisabler disabler;
  417. res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
  418. }
  419. if (res == 0) {
  420. int tid = ThreadCreate(GetCurrentThread(), IsStateDetached(detached));
  421. CHECK_NE(tid, kMainTid);
  422. atomic_store(&p.tid, tid, memory_order_release);
  423. while (atomic_load(&p.tid, memory_order_acquire) != 0)
  424. internal_sched_yield();
  425. }
  426. if (attr == &myattr)
  427. pthread_attr_destroy(&myattr);
  428. return res;
  429. }
  430. INTERCEPTOR(int, pthread_join, void *t, void **arg) {
  431. return REAL(pthread_join)(t, arg);
  432. }
  433. DEFINE_REAL_PTHREAD_FUNCTIONS
  434. INTERCEPTOR(void, _exit, int status) {
  435. if (status == 0 && HasReportedLeaks()) status = common_flags()->exitcode;
  436. REAL(_exit)(status);
  437. }
  438. #define COMMON_INTERCEPT_FUNCTION(name) INTERCEPT_FUNCTION(name)
  439. #include "sanitizer_common/sanitizer_signal_interceptors.inc"
  440. #endif // SANITIZER_POSIX
  441. namespace __lsan {
  442. void InitializeInterceptors() {
  443. // Fuchsia doesn't use interceptors that require any setup.
  444. #if !SANITIZER_FUCHSIA
  445. InitializeSignalInterceptors();
  446. INTERCEPT_FUNCTION(malloc);
  447. INTERCEPT_FUNCTION(free);
  448. LSAN_MAYBE_INTERCEPT_CFREE;
  449. INTERCEPT_FUNCTION(calloc);
  450. INTERCEPT_FUNCTION(realloc);
  451. LSAN_MAYBE_INTERCEPT_MEMALIGN;
  452. LSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
  453. LSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
  454. INTERCEPT_FUNCTION(posix_memalign);
  455. INTERCEPT_FUNCTION(valloc);
  456. LSAN_MAYBE_INTERCEPT_PVALLOC;
  457. LSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
  458. LSAN_MAYBE_INTERCEPT_MALLINFO;
  459. LSAN_MAYBE_INTERCEPT_MALLOPT;
  460. INTERCEPT_FUNCTION(pthread_create);
  461. INTERCEPT_FUNCTION(pthread_join);
  462. INTERCEPT_FUNCTION(_exit);
  463. LSAN_MAYBE_INTERCEPT__LWP_EXIT;
  464. LSAN_MAYBE_INTERCEPT_THR_EXIT;
  465. LSAN_MAYBE_INTERCEPT___CXA_ATEXIT;
  466. LSAN_MAYBE_INTERCEPT_ATEXIT;
  467. LSAN_MAYBE_INTERCEPT_PTHREAD_ATFORK;
  468. LSAN_MAYBE_INTERCEPT_STRERROR;
  469. #if !SANITIZER_NETBSD && !SANITIZER_FREEBSD
  470. if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
  471. Report("LeakSanitizer: failed to create thread key.\n");
  472. Die();
  473. }
  474. #endif
  475. #endif // !SANITIZER_FUCHSIA
  476. }
  477. } // namespace __lsan