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