1 //===-- lsan_mac.cc -------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of LeakSanitizer, a memory leak checker.
12 // Mac-specific details.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
18 #include "interception/interception.h"
20 #include "lsan_allocator.h"
21 #include "lsan_thread.h"
26 // Support for the following functions from libdispatch on Mac OS:
33 // dispatch_group_async_f()
34 // dispatch_group_async()
35 // TODO(glider): libdispatch API contains other functions that we don't support
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
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()
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
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);
62 // A wrapper for the ObjC blocks used to support libdispatch.
65 dispatch_function_t func;
67 } lsan_block_context_t;
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);
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;
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);
94 using namespace __lsan; // NOLINT
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();
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); \
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)
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);
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);
137 #if !defined(MISSING_BLOCKS_SUPPORT)
139 void dispatch_async(dispatch_queue_t dq, void (^work)(void));
140 void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
142 void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
144 void dispatch_source_set_cancel_handler(dispatch_source_t ds,
146 void dispatch_source_set_event_handler(dispatch_source_t ds,
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); \
158 INTERCEPTOR(void, dispatch_async, dispatch_queue_t dq, void (^work)(void)) {
159 GET_LSAN_BLOCK(work);
160 REAL(dispatch_async)(dq, lsan_block);
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);
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);
175 INTERCEPTOR(void, dispatch_source_set_cancel_handler, dispatch_source_t ds,
176 void (^work)(void)) {
178 REAL(dispatch_source_set_cancel_handler)(ds, work);
181 GET_LSAN_BLOCK(work);
182 REAL(dispatch_source_set_cancel_handler)(ds, lsan_block);
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);
192 #endif // SANITIZER_MAC