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