lsan_mac.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 (GetCurrentThread() == kInvalidTid) {
  64. u32 tid = ThreadCreate(parent_tid, true);
  65. ThreadStart(tid, GetTid());
  66. SetCurrentThread(tid);
  67. }
  68. }
  69. // For use by only those functions that allocated the context via
  70. // alloc_lsan_context().
  71. extern "C" void lsan_dispatch_call_block_and_release(void *block) {
  72. lsan_block_context_t *context = (lsan_block_context_t *)block;
  73. VReport(2,
  74. "lsan_dispatch_call_block_and_release(): "
  75. "context: %p, pthread_self: %p\n",
  76. block, pthread_self());
  77. lsan_register_worker_thread(context->parent_tid);
  78. // Call the original dispatcher for the block.
  79. context->func(context->block);
  80. lsan_free(context);
  81. }
  82. } // namespace __lsan
  83. using namespace __lsan;
  84. // Wrap |ctxt| and |func| into an lsan_block_context_t.
  85. // The caller retains control of the allocated context.
  86. extern "C" lsan_block_context_t *alloc_lsan_context(void *ctxt,
  87. dispatch_function_t func) {
  88. GET_STACK_TRACE_THREAD;
  89. lsan_block_context_t *lsan_ctxt =
  90. (lsan_block_context_t *)lsan_malloc(sizeof(lsan_block_context_t), stack);
  91. lsan_ctxt->block = ctxt;
  92. lsan_ctxt->func = func;
  93. lsan_ctxt->parent_tid = GetCurrentThread();
  94. return lsan_ctxt;
  95. }
  96. // Define interceptor for dispatch_*_f function with the three most common
  97. // parameters: dispatch_queue_t, context, dispatch_function_t.
  98. #define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \
  99. INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \
  100. dispatch_function_t func) { \
  101. lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func); \
  102. return REAL(dispatch_x_f)(dq, (void *)lsan_ctxt, \
  103. lsan_dispatch_call_block_and_release); \
  104. }
  105. INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)
  106. INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)
  107. INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)
  108. INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when, dispatch_queue_t dq,
  109. void *ctxt, dispatch_function_t func) {
  110. lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func);
  111. return REAL(dispatch_after_f)(when, dq, (void *)lsan_ctxt,
  112. lsan_dispatch_call_block_and_release);
  113. }
  114. INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
  115. dispatch_queue_t dq, void *ctxt, dispatch_function_t func) {
  116. lsan_block_context_t *lsan_ctxt = alloc_lsan_context(ctxt, func);
  117. REAL(dispatch_group_async_f)
  118. (group, dq, (void *)lsan_ctxt, lsan_dispatch_call_block_and_release);
  119. }
  120. #if !defined(MISSING_BLOCKS_SUPPORT)
  121. extern "C" {
  122. void dispatch_async(dispatch_queue_t dq, void (^work)(void));
  123. void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
  124. void (^work)(void));
  125. void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
  126. void (^work)(void));
  127. void dispatch_source_set_cancel_handler(dispatch_source_t ds,
  128. void (^work)(void));
  129. void dispatch_source_set_event_handler(dispatch_source_t ds,
  130. void (^work)(void));
  131. }
  132. #define GET_LSAN_BLOCK(work) \
  133. void (^lsan_block)(void); \
  134. int parent_tid = GetCurrentThread(); \
  135. lsan_block = ^(void) { \
  136. lsan_register_worker_thread(parent_tid); \
  137. work(); \
  138. }
  139. INTERCEPTOR(void, dispatch_async, dispatch_queue_t dq, void (^work)(void)) {
  140. GET_LSAN_BLOCK(work);
  141. REAL(dispatch_async)(dq, lsan_block);
  142. }
  143. INTERCEPTOR(void, dispatch_group_async, dispatch_group_t dg,
  144. dispatch_queue_t dq, void (^work)(void)) {
  145. GET_LSAN_BLOCK(work);
  146. REAL(dispatch_group_async)(dg, dq, lsan_block);
  147. }
  148. INTERCEPTOR(void, dispatch_after, dispatch_time_t when, dispatch_queue_t queue,
  149. void (^work)(void)) {
  150. GET_LSAN_BLOCK(work);
  151. REAL(dispatch_after)(when, queue, lsan_block);
  152. }
  153. INTERCEPTOR(void, dispatch_source_set_cancel_handler, dispatch_source_t ds,
  154. void (^work)(void)) {
  155. if (!work) {
  156. REAL(dispatch_source_set_cancel_handler)(ds, work);
  157. return;
  158. }
  159. GET_LSAN_BLOCK(work);
  160. REAL(dispatch_source_set_cancel_handler)(ds, lsan_block);
  161. }
  162. INTERCEPTOR(void, dispatch_source_set_event_handler, dispatch_source_t ds,
  163. void (^work)(void)) {
  164. GET_LSAN_BLOCK(work);
  165. REAL(dispatch_source_set_event_handler)(ds, lsan_block);
  166. }
  167. #endif
  168. #endif // SANITIZER_APPLE