1 //===-- asan_mac.cpp ------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // Mac-specific details.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
17 #include "asan_interceptors.h"
18 #include "asan_internal.h"
19 #include "asan_mapping.h"
20 #include "asan_stack.h"
21 #include "asan_thread.h"
22 #include "sanitizer_common/sanitizer_atomic.h"
23 #include "sanitizer_common/sanitizer_libc.h"
24 #include "sanitizer_common/sanitizer_mac.h"
28 #include <libkern/OSAtomic.h>
29 #include <mach-o/dyld.h>
30 #include <mach-o/getsect.h>
31 #include <mach-o/loader.h>
33 #include <stdlib.h> // for free()
35 #include <sys/resource.h>
36 #include <sys/sysctl.h>
37 #include <sys/ucontext.h>
40 // from <crt_externs.h>, but we don't have that file on iOS
42 extern char ***_NSGetArgv(void);
43 extern char ***_NSGetEnviron(void);
48 void InitializePlatformInterceptors() {}
49 void InitializePlatformExceptionHandlers() {}
50 bool IsSystemHeapAddress (uptr addr
) { return false; }
52 // No-op. Mac does not support static linkage anyway.
53 void *AsanDoesNotSupportStaticLinkage() {
57 uptr
FindDynamicShadowStart() {
58 return MapDynamicShadow(MemToShadowSize(kHighMemEnd
), ASAN_SHADOW_SCALE
,
59 /*min_shadow_base_alignment*/ 0, kHighMemEnd
);
62 // No-op. Mac does not support static linkage anyway.
63 void AsanCheckDynamicRTPrereqs() {}
65 // No-op. Mac does not support static linkage anyway.
66 void AsanCheckIncompatibleRT() {}
68 void AsanApplyToGlobals(globals_op_fptr op
, const void *needle
) {
69 // Find the Mach-O header for the image containing the needle
71 int err
= dladdr(needle
, &info
);
75 const struct mach_header_64
*mh
= (struct mach_header_64
*)info
.dli_fbase
;
77 const struct mach_header
*mh
= (struct mach_header
*)info
.dli_fbase
;
80 // Look up the __asan_globals section in that image and register its globals
81 unsigned long size
= 0;
82 __asan_global
*globals
= (__asan_global
*)getsectiondata(
84 "__DATA", "__asan_globals",
88 if (size
% sizeof(__asan_global
) != 0) return;
89 op(globals
, size
/ sizeof(__asan_global
));
92 void FlushUnneededASanShadowMemory(uptr p
, uptr size
) {
93 // Since asan's mapping is compacting, the shadow chunk may be
94 // not page-aligned, so we only flush the page-aligned portion.
95 ReleaseMemoryPagesToOS(MemToShadow(p
), MemToShadow(p
+ size
));
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(), GetTid(), ThreadType::Worker
,
154 // For use by only those functions that allocated the context via
155 // alloc_asan_context().
157 void asan_dispatch_call_block_and_release(void *block
) {
158 GET_STACK_TRACE_THREAD
;
159 asan_block_context_t
*context
= (asan_block_context_t
*)block
;
161 "asan_dispatch_call_block_and_release(): "
162 "context: %p, pthread_self: %p\n",
163 block
, pthread_self());
164 asan_register_worker_thread(context
->parent_tid
, &stack
);
165 // Call the original dispatcher for the block.
166 context
->func(context
->block
);
167 asan_free(context
, &stack
, FROM_MALLOC
);
170 } // namespace __asan
172 using namespace __asan
;
174 // Wrap |ctxt| and |func| into an asan_block_context_t.
175 // The caller retains control of the allocated context.
177 asan_block_context_t
*alloc_asan_context(void *ctxt
, dispatch_function_t func
,
178 BufferedStackTrace
*stack
) {
179 asan_block_context_t
*asan_ctxt
=
180 (asan_block_context_t
*) asan_malloc(sizeof(asan_block_context_t
), stack
);
181 asan_ctxt
->block
= ctxt
;
182 asan_ctxt
->func
= func
;
183 asan_ctxt
->parent_tid
= GetCurrentTidOrInvalid();
187 // Define interceptor for dispatch_*_f function with the three most common
188 // parameters: dispatch_queue_t, context, dispatch_function_t.
189 #define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \
190 INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \
191 dispatch_function_t func) { \
192 GET_STACK_TRACE_THREAD; \
193 asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); \
194 if (Verbosity() >= 2) { \
195 Report(#dispatch_x_f "(): context: %p, pthread_self: %p\n", \
196 asan_ctxt, pthread_self()); \
197 PRINT_CURRENT_STACK(); \
199 return REAL(dispatch_x_f)(dq, (void*)asan_ctxt, \
200 asan_dispatch_call_block_and_release); \
203 INTERCEPT_DISPATCH_X_F_3(dispatch_async_f
)
204 INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f
)
205 INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f
)
207 INTERCEPTOR(void, dispatch_after_f
, dispatch_time_t when
,
208 dispatch_queue_t dq
, void *ctxt
,
209 dispatch_function_t func
) {
210 GET_STACK_TRACE_THREAD
;
211 asan_block_context_t
*asan_ctxt
= alloc_asan_context(ctxt
, func
, &stack
);
212 if (Verbosity() >= 2) {
213 Report("dispatch_after_f: %p\n", asan_ctxt
);
214 PRINT_CURRENT_STACK();
216 return REAL(dispatch_after_f
)(when
, dq
, (void*)asan_ctxt
,
217 asan_dispatch_call_block_and_release
);
220 INTERCEPTOR(void, dispatch_group_async_f
, dispatch_group_t group
,
221 dispatch_queue_t dq
, void *ctxt
,
222 dispatch_function_t func
) {
223 GET_STACK_TRACE_THREAD
;
224 asan_block_context_t
*asan_ctxt
= alloc_asan_context(ctxt
, func
, &stack
);
225 if (Verbosity() >= 2) {
226 Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n",
227 asan_ctxt
, pthread_self());
228 PRINT_CURRENT_STACK();
230 REAL(dispatch_group_async_f
)(group
, dq
, (void*)asan_ctxt
,
231 asan_dispatch_call_block_and_release
);
234 #if !defined(MISSING_BLOCKS_SUPPORT)
236 void dispatch_async(dispatch_queue_t dq
, void(^work
)(void));
237 void dispatch_group_async(dispatch_group_t dg
, dispatch_queue_t dq
,
239 void dispatch_after(dispatch_time_t when
, dispatch_queue_t queue
,
241 void dispatch_source_set_cancel_handler(dispatch_source_t ds
,
243 void dispatch_source_set_event_handler(dispatch_source_t ds
, void(^work
)(void));
246 #define GET_ASAN_BLOCK(work) \
247 void (^asan_block)(void); \
248 int parent_tid = GetCurrentTidOrInvalid(); \
249 asan_block = ^(void) { \
250 GET_STACK_TRACE_THREAD; \
251 asan_register_worker_thread(parent_tid, &stack); \
255 INTERCEPTOR(void, dispatch_async
,
256 dispatch_queue_t dq
, void(^work
)(void)) {
257 ENABLE_FRAME_POINTER
;
258 GET_ASAN_BLOCK(work
);
259 REAL(dispatch_async
)(dq
, asan_block
);
262 INTERCEPTOR(void, dispatch_group_async
,
263 dispatch_group_t dg
, dispatch_queue_t dq
, void(^work
)(void)) {
264 ENABLE_FRAME_POINTER
;
265 GET_ASAN_BLOCK(work
);
266 REAL(dispatch_group_async
)(dg
, dq
, asan_block
);
269 INTERCEPTOR(void, dispatch_after
,
270 dispatch_time_t when
, dispatch_queue_t queue
, void(^work
)(void)) {
271 ENABLE_FRAME_POINTER
;
272 GET_ASAN_BLOCK(work
);
273 REAL(dispatch_after
)(when
, queue
, asan_block
);
276 INTERCEPTOR(void, dispatch_source_set_cancel_handler
,
277 dispatch_source_t ds
, void(^work
)(void)) {
279 REAL(dispatch_source_set_cancel_handler
)(ds
, work
);
282 ENABLE_FRAME_POINTER
;
283 GET_ASAN_BLOCK(work
);
284 REAL(dispatch_source_set_cancel_handler
)(ds
, asan_block
);
287 INTERCEPTOR(void, dispatch_source_set_event_handler
,
288 dispatch_source_t ds
, void(^work
)(void)) {
289 ENABLE_FRAME_POINTER
;
290 GET_ASAN_BLOCK(work
);
291 REAL(dispatch_source_set_event_handler
)(ds
, asan_block
);
295 #endif // SANITIZER_APPLE