1 //===-- asan_mac.cc -------------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Mac-specific details.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_platform.h"
16 #include "asan_interceptors.h"
17 #include "asan_internal.h"
18 #include "asan_mapping.h"
19 #include "asan_stack.h"
20 #include "asan_thread.h"
21 #include "sanitizer_common/sanitizer_atomic.h"
22 #include "sanitizer_common/sanitizer_libc.h"
23 #include "sanitizer_common/sanitizer_mac.h"
27 #include <libkern/OSAtomic.h>
28 #include <mach-o/dyld.h>
29 #include <mach-o/getsect.h>
30 #include <mach-o/loader.h>
32 #include <stdlib.h> // for free()
34 #include <sys/resource.h>
35 #include <sys/sysctl.h>
36 #include <sys/ucontext.h>
39 // from <crt_externs.h>, but we don't have that file on iOS
41 extern char ***_NSGetArgv(void);
42 extern char ***_NSGetEnviron(void);
47 void InitializePlatformInterceptors() {}
48 void InitializePlatformExceptionHandlers() {}
50 bool PlatformHasDifferentMemcpyAndMemmove() {
51 // On OS X 10.7 memcpy() and memmove() are both resolved
52 // into memmove$VARIANT$sse42.
53 // See also https://github.com/google/sanitizers/issues/34.
54 // TODO(glider): need to check dynamically that memcpy() and memmove() are
55 // actually the same function.
56 return GetMacosVersion() == MACOS_VERSION_SNOW_LEOPARD
;
59 // No-op. Mac does not support static linkage anyway.
60 void *AsanDoesNotSupportStaticLinkage() {
64 // No-op. Mac does not support static linkage anyway.
65 void AsanCheckDynamicRTPrereqs() {}
67 // No-op. Mac does not support static linkage anyway.
68 void AsanCheckIncompatibleRT() {}
70 void AsanApplyToGlobals(globals_op_fptr op
, const void *needle
) {
71 // Find the Mach-O header for the image containing the needle
73 int err
= dladdr(needle
, &info
);
77 const struct mach_header_64
*mh
= (struct mach_header_64
*)info
.dli_fbase
;
79 const struct mach_header
*mh
= (struct mach_header
*)info
.dli_fbase
;
82 // Look up the __asan_globals section in that image and register its globals
83 unsigned long size
= 0;
84 __asan_global
*globals
= (__asan_global
*)getsectiondata(
86 "__DATA", "__asan_globals",
90 if (size
% sizeof(__asan_global
) != 0) return;
91 op(globals
, size
/ sizeof(__asan_global
));
94 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
) {
98 // Support for the following functions from libdispatch on Mac OS:
103 // dispatch_after_f()
105 // dispatch_group_async_f()
106 // dispatch_group_async()
107 // TODO(glider): libdispatch API contains other functions that we don't support
110 // dispatch_sync() and dispatch_sync_f() are synchronous, although chances are
111 // they can cause jobs to run on a thread different from the current one.
112 // TODO(glider): if so, we need a test for this (otherwise we should remove
115 // The following functions use dispatch_barrier_async_f() (which isn't a library
116 // function but is exported) and are thus supported:
117 // dispatch_source_set_cancel_handler_f()
118 // dispatch_source_set_cancel_handler()
119 // dispatch_source_set_event_handler_f()
120 // dispatch_source_set_event_handler()
122 // The reference manual for Grand Central Dispatch is available at
123 // http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
124 // The implementation details are at
125 // http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c
127 typedef void* dispatch_group_t
;
128 typedef void* dispatch_queue_t
;
129 typedef void* dispatch_source_t
;
130 typedef u64 dispatch_time_t
;
131 typedef void (*dispatch_function_t
)(void *block
);
132 typedef void* (*worker_t
)(void *block
);
134 // A wrapper for the ObjC blocks used to support libdispatch.
137 dispatch_function_t func
;
139 } asan_block_context_t
;
142 void asan_register_worker_thread(int parent_tid
, StackTrace
*stack
) {
143 AsanThread
*t
= GetCurrentThread();
145 t
= AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr,
146 parent_tid
, stack
, /* detached */ true);
148 asanThreadRegistry().StartThread(t
->tid(), 0, 0);
153 // For use by only those functions that allocated the context via
154 // alloc_asan_context().
156 void asan_dispatch_call_block_and_release(void *block
) {
157 GET_STACK_TRACE_THREAD
;
158 asan_block_context_t
*context
= (asan_block_context_t
*)block
;
160 "asan_dispatch_call_block_and_release(): "
161 "context: %p, pthread_self: %p\n",
162 block
, pthread_self());
163 asan_register_worker_thread(context
->parent_tid
, &stack
);
164 // Call the original dispatcher for the block.
165 context
->func(context
->block
);
166 asan_free(context
, &stack
, FROM_MALLOC
);
169 } // namespace __asan
171 using namespace __asan
; // NOLINT
173 // Wrap |ctxt| and |func| into an asan_block_context_t.
174 // The caller retains control of the allocated context.
176 asan_block_context_t
*alloc_asan_context(void *ctxt
, dispatch_function_t func
,
177 BufferedStackTrace
*stack
) {
178 asan_block_context_t
*asan_ctxt
=
179 (asan_block_context_t
*) asan_malloc(sizeof(asan_block_context_t
), stack
);
180 asan_ctxt
->block
= ctxt
;
181 asan_ctxt
->func
= func
;
182 asan_ctxt
->parent_tid
= GetCurrentTidOrInvalid();
186 // Define interceptor for dispatch_*_f function with the three most common
187 // parameters: dispatch_queue_t, context, dispatch_function_t.
188 #define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \
189 INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \
190 dispatch_function_t func) { \
191 GET_STACK_TRACE_THREAD; \
192 asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); \
193 if (Verbosity() >= 2) { \
194 Report(#dispatch_x_f "(): context: %p, pthread_self: %p\n", \
195 asan_ctxt, pthread_self()); \
196 PRINT_CURRENT_STACK(); \
198 return REAL(dispatch_x_f)(dq, (void*)asan_ctxt, \
199 asan_dispatch_call_block_and_release); \
202 INTERCEPT_DISPATCH_X_F_3(dispatch_async_f
)
203 INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f
)
204 INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f
)
206 INTERCEPTOR(void, dispatch_after_f
, dispatch_time_t when
,
207 dispatch_queue_t dq
, void *ctxt
,
208 dispatch_function_t func
) {
209 GET_STACK_TRACE_THREAD
;
210 asan_block_context_t
*asan_ctxt
= alloc_asan_context(ctxt
, func
, &stack
);
211 if (Verbosity() >= 2) {
212 Report("dispatch_after_f: %p\n", asan_ctxt
);
213 PRINT_CURRENT_STACK();
215 return REAL(dispatch_after_f
)(when
, dq
, (void*)asan_ctxt
,
216 asan_dispatch_call_block_and_release
);
219 INTERCEPTOR(void, dispatch_group_async_f
, dispatch_group_t group
,
220 dispatch_queue_t dq
, void *ctxt
,
221 dispatch_function_t func
) {
222 GET_STACK_TRACE_THREAD
;
223 asan_block_context_t
*asan_ctxt
= alloc_asan_context(ctxt
, func
, &stack
);
224 if (Verbosity() >= 2) {
225 Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n",
226 asan_ctxt
, pthread_self());
227 PRINT_CURRENT_STACK();
229 REAL(dispatch_group_async_f
)(group
, dq
, (void*)asan_ctxt
,
230 asan_dispatch_call_block_and_release
);
233 #if !defined(MISSING_BLOCKS_SUPPORT)
235 void dispatch_async(dispatch_queue_t dq
, void(^work
)(void));
236 void dispatch_group_async(dispatch_group_t dg
, dispatch_queue_t dq
,
238 void dispatch_after(dispatch_time_t when
, dispatch_queue_t queue
,
240 void dispatch_source_set_cancel_handler(dispatch_source_t ds
,
242 void dispatch_source_set_event_handler(dispatch_source_t ds
, void(^work
)(void));
245 #define GET_ASAN_BLOCK(work) \
246 void (^asan_block)(void); \
247 int parent_tid = GetCurrentTidOrInvalid(); \
248 asan_block = ^(void) { \
249 GET_STACK_TRACE_THREAD; \
250 asan_register_worker_thread(parent_tid, &stack); \
254 INTERCEPTOR(void, dispatch_async
,
255 dispatch_queue_t dq
, void(^work
)(void)) {
256 ENABLE_FRAME_POINTER
;
257 GET_ASAN_BLOCK(work
);
258 REAL(dispatch_async
)(dq
, asan_block
);
261 INTERCEPTOR(void, dispatch_group_async
,
262 dispatch_group_t dg
, dispatch_queue_t dq
, void(^work
)(void)) {
263 ENABLE_FRAME_POINTER
;
264 GET_ASAN_BLOCK(work
);
265 REAL(dispatch_group_async
)(dg
, dq
, asan_block
);
268 INTERCEPTOR(void, dispatch_after
,
269 dispatch_time_t when
, dispatch_queue_t queue
, void(^work
)(void)) {
270 ENABLE_FRAME_POINTER
;
271 GET_ASAN_BLOCK(work
);
272 REAL(dispatch_after
)(when
, queue
, asan_block
);
275 INTERCEPTOR(void, dispatch_source_set_cancel_handler
,
276 dispatch_source_t ds
, void(^work
)(void)) {
278 REAL(dispatch_source_set_cancel_handler
)(ds
, work
);
281 ENABLE_FRAME_POINTER
;
282 GET_ASAN_BLOCK(work
);
283 REAL(dispatch_source_set_cancel_handler
)(ds
, asan_block
);
286 INTERCEPTOR(void, dispatch_source_set_event_handler
,
287 dispatch_source_t ds
, void(^work
)(void)) {
288 ENABLE_FRAME_POINTER
;
289 GET_ASAN_BLOCK(work
);
290 REAL(dispatch_source_set_event_handler
)(ds
, asan_block
);
294 #endif // SANITIZER_MAC