lsan_mac.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //===-- lsan_mac.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, a memory leak checker.
  10. //
  11. // Mac-specific details.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_common/sanitizer_platform.h"
  14. #if SANITIZER_APPLE
  15. #include "interception/interception.h"
  16. #include "lsan.h"
  17. #include "lsan_allocator.h"
  18. #include "lsan_thread.h"
  19. #include <pthread.h>
  20. namespace __lsan {
  21. // Support for the following functions from libdispatch on Mac OS:
  22. // dispatch_async_f()
  23. // dispatch_async()
  24. // dispatch_sync_f()
  25. // dispatch_sync()
  26. // dispatch_after_f()
  27. // dispatch_after()
  28. // dispatch_group_async_f()
  29. // dispatch_group_async()
  30. // TODO(glider): libdispatch API contains other functions that we don't support
  31. // yet.
  32. //
  33. // dispatch_sync() and dispatch_sync_f() are synchronous, although chances are
  34. // they can cause jobs to run on a thread different from the current one.
  35. // TODO(glider): if so, we need a test for this (otherwise we should remove
  36. // them).
  37. //
  38. // The following functions use dispatch_barrier_async_f() (which isn't a library
  39. // function but is exported) and are thus supported:
  40. // dispatch_source_set_cancel_handler_f()
  41. // dispatch_source_set_cancel_handler()
  42. // dispatch_source_set_event_handler_f()
  43. // dispatch_source_set_event_handler()
  44. //
  45. // The reference manual for Grand Central Dispatch is available at
  46. // http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
  47. // The implementation details are at
  48. // http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c
  49. typedef void *dispatch_group_t;
  50. typedef void *dispatch_queue_t;
  51. typedef void *dispatch_source_t;
  52. typedef u64 dispatch_time_t;
  53. typedef void (*dispatch_function_t)(void *block);
  54. typedef void *(*worker_t)(void *block);
  55. // A wrapper for the ObjC blocks used to support libdispatch.
  56. typedef struct {
  57. void *block;
  58. dispatch_function_t func;
  59. u32 parent_tid;
  60. } lsan_block_context_t;
  61. ALWAYS_INLINE
  62. void lsan_register_worker_thread(int parent_tid) {
  63. if (GetCurrentThreadId() == kInvalidTid) {
  64. u32 tid = ThreadCreate(parent_tid, true);
  65. ThreadStart(tid, GetTid());
  66. }
  67. }
  68. // For use by only those functions that allocated the context via
  69. // alloc_lsan_context().
  70. extern "C" void lsan_dispatch_call_block_and_release(void *block) {
  71. lsan_block_context_t *context = (lsan_block_context_t *)block;
  72. VReport(2,
  73. "lsan_dispatch_call_block_and_release(): "
  74. "context: %p, pthread_self: %p\n",
  75. block, (void*)pthread_self());
  76. lsan_register_worker_thread(context->parent_tid);
  77. // Call the original dispatcher for the block.
  78. context->func(context->block);
  79. lsan_free(context);
  80. }
  81. } // namespace __lsan
  82. using namespace __lsan;
  83. // Wrap |ctxt| and |func| into an lsan_block_context_t.
  84. // The caller retains control of the allocated context.
  85. extern "C" lsan_block_context_t *alloc_lsan_context(void *ctxt,
  86. dispatch_function_t func) {
  87. GET_STACK_TRACE_THREAD;
  88. lsan_block_context_t *lsan_ctxt =
  89. (lsan_block_context_t *)lsan_malloc(sizeof(lsan_block_context_t), stack);
  90. lsan_ctxt->block = ctxt;
  91. lsan_ctxt->func = func;
  92. lsan_ctxt->parent_tid = GetCurrentThreadId();
  93. return lsan_ctxt;
  94. }
  95. // Define interceptor for dispatch_*_f function with the three most common
  96. // parameters: dispatch_queue_t, context, dispatch_function_t.
  97. #define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \
  98. INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \
  99. dispatch_function_t func) { \
  100. lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func); \
  101. return REAL(dispatch_x_f)(dq, (void *)lsan_ctxt, \
  102. lsan_dispatch_call_block_and_release); \
  103. }
  104. INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)
  105. INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)
  106. INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)
  107. INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when, dispatch_queue_t dq,
  108. void *ctxt, dispatch_function_t func) {
  109. lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func);
  110. return REAL(dispatch_after_f)(when, dq, (void *)lsan_ctxt,
  111. lsan_dispatch_call_block_and_release);
  112. }
  113. INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
  114. dispatch_queue_t dq, void *ctxt, dispatch_function_t func) {
  115. lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func);
  116. REAL(dispatch_group_async_f)
  117. (group, dq, (void *)lsan_ctxt, lsan_dispatch_call_block_and_release);
  118. }
  119. #if !defined(MISSING_BLOCKS_SUPPORT)
  120. extern "C" {
  121. void dispatch_async(dispatch_queue_t dq, void (^work)(void));
  122. void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
  123. void (^work)(void));
  124. void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
  125. void (^work)(void));
  126. void dispatch_source_set_cancel_handler(dispatch_source_t ds,
  127. void (^work)(void));
  128. void dispatch_source_set_event_handler(dispatch_source_t ds,
  129. void (^work)(void));
  130. }
  131. # define GET_LSAN_BLOCK(work) \
  132. void (^lsan_block)(void); \
  133. int parent_tid = GetCurrentThreadId(); \
  134. lsan_block = ^(void) { \
  135. lsan_register_worker_thread(parent_tid); \
  136. work(); \
  137. }
  138. INTERCEPTOR(void, dispatch_async, dispatch_queue_t dq, void (^work)(void)) {
  139. GET_LSAN_BLOCK(work);
  140. REAL(dispatch_async)(dq, lsan_block);
  141. }
  142. INTERCEPTOR(void, dispatch_group_async, dispatch_group_t dg,
  143. dispatch_queue_t dq, void (^work)(void)) {
  144. GET_LSAN_BLOCK(work);
  145. REAL(dispatch_group_async)(dg, dq, lsan_block);
  146. }
  147. INTERCEPTOR(void, dispatch_after, dispatch_time_t when, dispatch_queue_t queue,
  148. void (^work)(void)) {
  149. GET_LSAN_BLOCK(work);
  150. REAL(dispatch_after)(when, queue, lsan_block);
  151. }
  152. INTERCEPTOR(void, dispatch_source_set_cancel_handler, dispatch_source_t ds,
  153. void (^work)(void)) {
  154. if (!work) {
  155. REAL(dispatch_source_set_cancel_handler)(ds, work);
  156. return;
  157. }
  158. GET_LSAN_BLOCK(work);
  159. REAL(dispatch_source_set_cancel_handler)(ds, lsan_block);
  160. }
  161. INTERCEPTOR(void, dispatch_source_set_event_handler, dispatch_source_t ds,
  162. void (^work)(void)) {
  163. GET_LSAN_BLOCK(work);
  164. REAL(dispatch_source_set_event_handler)(ds, lsan_block);
  165. }
  166. #endif
  167. #endif // SANITIZER_APPLE