lsan_interceptors.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_common/sanitizer_allocator.h"
  13. #include "sanitizer_common/sanitizer_atomic.h"
  14. #include "sanitizer_common/sanitizer_common.h"
  15. #include "sanitizer_common/sanitizer_flags.h"
  16. #include "sanitizer_common/sanitizer_interception.h"
  17. #include "sanitizer_common/sanitizer_internal_defs.h"
  18. #include "sanitizer_common/sanitizer_linux.h"
  19. #include "sanitizer_common/sanitizer_platform_limits_posix.h"
  20. #include "lsan.h"
  21. #include "lsan_allocator.h"
  22. #include "lsan_thread.h"
  23. using namespace __lsan;
  24. extern "C" {
  25. int pthread_attr_init(void *attr);
  26. int pthread_attr_destroy(void *attr);
  27. int pthread_attr_getdetachstate(void *attr, int *v);
  28. int pthread_key_create(unsigned *key, void (*destructor)(void* v));
  29. int pthread_setspecific(unsigned key, const void *v);
  30. }
  31. #define ENSURE_LSAN_INITED do { \
  32. CHECK(!lsan_init_is_running); \
  33. if (!lsan_inited) \
  34. __lsan_init(); \
  35. } while (0)
  36. ///// Malloc/free interceptors. /////
  37. const bool kAlwaysClearMemory = true;
  38. namespace std {
  39. struct nothrow_t;
  40. }
  41. INTERCEPTOR(void*, malloc, uptr size) {
  42. ENSURE_LSAN_INITED;
  43. GET_STACK_TRACE_MALLOC;
  44. return Allocate(stack, size, 1, kAlwaysClearMemory);
  45. }
  46. INTERCEPTOR(void, free, void *p) {
  47. ENSURE_LSAN_INITED;
  48. Deallocate(p);
  49. }
  50. INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
  51. if (lsan_init_is_running) {
  52. // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
  53. const uptr kCallocPoolSize = 1024;
  54. static uptr calloc_memory_for_dlsym[kCallocPoolSize];
  55. static uptr allocated;
  56. uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
  57. void *mem = (void*)&calloc_memory_for_dlsym[allocated];
  58. allocated += size_in_words;
  59. CHECK(allocated < kCallocPoolSize);
  60. return mem;
  61. }
  62. if (CallocShouldReturnNullDueToOverflow(size, nmemb)) return 0;
  63. ENSURE_LSAN_INITED;
  64. GET_STACK_TRACE_MALLOC;
  65. size *= nmemb;
  66. return Allocate(stack, size, 1, true);
  67. }
  68. INTERCEPTOR(void*, realloc, void *q, uptr size) {
  69. ENSURE_LSAN_INITED;
  70. GET_STACK_TRACE_MALLOC;
  71. return Reallocate(stack, q, size, 1);
  72. }
  73. INTERCEPTOR(void*, memalign, uptr alignment, uptr size) {
  74. ENSURE_LSAN_INITED;
  75. GET_STACK_TRACE_MALLOC;
  76. return Allocate(stack, size, alignment, kAlwaysClearMemory);
  77. }
  78. INTERCEPTOR(void*, aligned_alloc, uptr alignment, uptr size) {
  79. ENSURE_LSAN_INITED;
  80. GET_STACK_TRACE_MALLOC;
  81. return Allocate(stack, size, alignment, kAlwaysClearMemory);
  82. }
  83. INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
  84. ENSURE_LSAN_INITED;
  85. GET_STACK_TRACE_MALLOC;
  86. *memptr = Allocate(stack, size, alignment, kAlwaysClearMemory);
  87. // FIXME: Return ENOMEM if user requested more than max alloc size.
  88. return 0;
  89. }
  90. INTERCEPTOR(void*, valloc, uptr size) {
  91. ENSURE_LSAN_INITED;
  92. GET_STACK_TRACE_MALLOC;
  93. if (size == 0)
  94. size = GetPageSizeCached();
  95. return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
  96. }
  97. INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
  98. ENSURE_LSAN_INITED;
  99. return GetMallocUsableSize(ptr);
  100. }
  101. struct fake_mallinfo {
  102. int x[10];
  103. };
  104. INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
  105. struct fake_mallinfo res;
  106. internal_memset(&res, 0, sizeof(res));
  107. return res;
  108. }
  109. INTERCEPTOR(int, mallopt, int cmd, int value) {
  110. return -1;
  111. }
  112. INTERCEPTOR(void*, pvalloc, uptr size) {
  113. ENSURE_LSAN_INITED;
  114. GET_STACK_TRACE_MALLOC;
  115. uptr PageSize = GetPageSizeCached();
  116. size = RoundUpTo(size, PageSize);
  117. if (size == 0) {
  118. // pvalloc(0) should allocate one page.
  119. size = PageSize;
  120. }
  121. return Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory);
  122. }
  123. INTERCEPTOR(void, cfree, void *p) ALIAS(WRAPPER_NAME(free));
  124. #define OPERATOR_NEW_BODY \
  125. ENSURE_LSAN_INITED; \
  126. GET_STACK_TRACE_MALLOC; \
  127. return Allocate(stack, size, 1, kAlwaysClearMemory);
  128. INTERCEPTOR_ATTRIBUTE
  129. void *operator new(uptr size) { OPERATOR_NEW_BODY; }
  130. INTERCEPTOR_ATTRIBUTE
  131. void *operator new[](uptr size) { OPERATOR_NEW_BODY; }
  132. INTERCEPTOR_ATTRIBUTE
  133. void *operator new(uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
  134. INTERCEPTOR_ATTRIBUTE
  135. void *operator new[](uptr size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
  136. #define OPERATOR_DELETE_BODY \
  137. ENSURE_LSAN_INITED; \
  138. Deallocate(ptr);
  139. INTERCEPTOR_ATTRIBUTE
  140. void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
  141. INTERCEPTOR_ATTRIBUTE
  142. void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
  143. INTERCEPTOR_ATTRIBUTE
  144. void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
  145. INTERCEPTOR_ATTRIBUTE
  146. void operator delete[](void *ptr, std::nothrow_t const &) {
  147. OPERATOR_DELETE_BODY;
  148. }
  149. // We need this to intercept the __libc_memalign calls that are used to
  150. // allocate dynamic TLS space in ld-linux.so.
  151. INTERCEPTOR(void *, __libc_memalign, uptr align, uptr s)
  152. ALIAS(WRAPPER_NAME(memalign));
  153. ///// Thread initialization and finalization. /////
  154. static unsigned g_thread_finalize_key;
  155. static void thread_finalize(void *v) {
  156. uptr iter = (uptr)v;
  157. if (iter > 1) {
  158. if (pthread_setspecific(g_thread_finalize_key, (void*)(iter - 1))) {
  159. Report("LeakSanitizer: failed to set thread key.\n");
  160. Die();
  161. }
  162. return;
  163. }
  164. ThreadFinish();
  165. }
  166. struct ThreadParam {
  167. void *(*callback)(void *arg);
  168. void *param;
  169. atomic_uintptr_t tid;
  170. };
  171. extern "C" void *__lsan_thread_start_func(void *arg) {
  172. ThreadParam *p = (ThreadParam*)arg;
  173. void* (*callback)(void *arg) = p->callback;
  174. void *param = p->param;
  175. // Wait until the last iteration to maximize the chance that we are the last
  176. // destructor to run.
  177. if (pthread_setspecific(g_thread_finalize_key,
  178. (void*)kPthreadDestructorIterations)) {
  179. Report("LeakSanitizer: failed to set thread key.\n");
  180. Die();
  181. }
  182. int tid = 0;
  183. while ((tid = atomic_load(&p->tid, memory_order_acquire)) == 0)
  184. internal_sched_yield();
  185. atomic_store(&p->tid, 0, memory_order_release);
  186. SetCurrentThread(tid);
  187. ThreadStart(tid, GetTid());
  188. return callback(param);
  189. }
  190. INTERCEPTOR(int, pthread_create, void *th, void *attr,
  191. void *(*callback)(void *), void *param) {
  192. ENSURE_LSAN_INITED;
  193. EnsureMainThreadIDIsCorrect();
  194. __sanitizer_pthread_attr_t myattr;
  195. if (attr == 0) {
  196. pthread_attr_init(&myattr);
  197. attr = &myattr;
  198. }
  199. AdjustStackSize(attr);
  200. int detached = 0;
  201. pthread_attr_getdetachstate(attr, &detached);
  202. ThreadParam p;
  203. p.callback = callback;
  204. p.param = param;
  205. atomic_store(&p.tid, 0, memory_order_relaxed);
  206. int res = REAL(pthread_create)(th, attr, __lsan_thread_start_func, &p);
  207. if (res == 0) {
  208. int tid = ThreadCreate(GetCurrentThread(), *(uptr *)th, detached);
  209. CHECK_NE(tid, 0);
  210. atomic_store(&p.tid, tid, memory_order_release);
  211. while (atomic_load(&p.tid, memory_order_acquire) != 0)
  212. internal_sched_yield();
  213. }
  214. if (attr == &myattr)
  215. pthread_attr_destroy(&myattr);
  216. return res;
  217. }
  218. INTERCEPTOR(int, pthread_join, void *th, void **ret) {
  219. ENSURE_LSAN_INITED;
  220. int tid = ThreadTid((uptr)th);
  221. int res = REAL(pthread_join)(th, ret);
  222. if (res == 0)
  223. ThreadJoin(tid);
  224. return res;
  225. }
  226. namespace __lsan {
  227. void InitializeInterceptors() {
  228. INTERCEPT_FUNCTION(malloc);
  229. INTERCEPT_FUNCTION(free);
  230. INTERCEPT_FUNCTION(cfree);
  231. INTERCEPT_FUNCTION(calloc);
  232. INTERCEPT_FUNCTION(realloc);
  233. INTERCEPT_FUNCTION(memalign);
  234. INTERCEPT_FUNCTION(posix_memalign);
  235. INTERCEPT_FUNCTION(__libc_memalign);
  236. INTERCEPT_FUNCTION(valloc);
  237. INTERCEPT_FUNCTION(pvalloc);
  238. INTERCEPT_FUNCTION(malloc_usable_size);
  239. INTERCEPT_FUNCTION(mallinfo);
  240. INTERCEPT_FUNCTION(mallopt);
  241. INTERCEPT_FUNCTION(pthread_create);
  242. INTERCEPT_FUNCTION(pthread_join);
  243. if (pthread_key_create(&g_thread_finalize_key, &thread_finalize)) {
  244. Report("LeakSanitizer: failed to create thread key.\n");
  245. Die();
  246. }
  247. }
  248. } // namespace __lsan