1 //===-- asan_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 AddressSanitizer, an address sanity checker.
12 // Mac-specific details.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
18 #include "asan_interceptors.h"
19 #include "asan_internal.h"
20 #include "asan_mapping.h"
21 #include "asan_stack.h"
22 #include "asan_thread.h"
23 #include "sanitizer_common/sanitizer_atomic.h"
24 #include "sanitizer_common/sanitizer_libc.h"
25 #include "sanitizer_common/sanitizer_mac.h"
29 #include <libkern/OSAtomic.h>
30 #include <mach-o/dyld.h>
31 #include <mach-o/getsect.h>
32 #include <mach-o/loader.h>
34 #include <stdlib.h> // for free()
36 #include <sys/resource.h>
37 #include <sys/sysctl.h>
38 #include <sys/ucontext.h>
41 // from <crt_externs.h>, but we don't have that file on iOS
43 extern char ***_NSGetArgv(void);
44 extern char ***_NSGetEnviron(void);
49 void InitializePlatformInterceptors() {}
50 void InitializePlatformExceptionHandlers() {}
51 bool IsSystemHeapAddress (uptr addr) { return false; }
53 // No-op. Mac does not support static linkage anyway.
54 void *AsanDoesNotSupportStaticLinkage() {
58 uptr FindDynamicShadowStart() {
59 uptr granularity = GetMmapGranularity();
60 uptr alignment = 8 * granularity;
61 uptr left_padding = granularity;
62 uptr space_size = kHighShadowEnd + left_padding;
64 uptr largest_gap_found = 0;
65 uptr shadow_start = FindAvailableMemoryRange(space_size, alignment,
66 granularity, &largest_gap_found);
67 // If the shadow doesn't fit, restrict the address space to make it fit.
68 if (shadow_start == 0) {
69 uptr new_max_vm = RoundDownTo(largest_gap_found << SHADOW_SCALE, alignment);
70 RestrictMemoryToMaxAddress(new_max_vm);
71 kHighMemEnd = new_max_vm - 1;
72 space_size = kHighShadowEnd + left_padding;
74 FindAvailableMemoryRange(space_size, alignment, granularity, nullptr);
76 CHECK_NE((uptr)0, shadow_start);
77 CHECK(IsAligned(shadow_start, alignment));
81 // No-op. Mac does not support static linkage anyway.
82 void AsanCheckDynamicRTPrereqs() {}
84 // No-op. Mac does not support static linkage anyway.
85 void AsanCheckIncompatibleRT() {}
87 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
88 // Find the Mach-O header for the image containing the needle
90 int err = dladdr(needle, &info);
94 const struct mach_header_64 *mh = (struct mach_header_64 *)info.dli_fbase;
96 const struct mach_header *mh = (struct mach_header *)info.dli_fbase;
99 // Look up the __asan_globals section in that image and register its globals
100 unsigned long size = 0;
101 __asan_global *globals = (__asan_global *)getsectiondata(
103 "__DATA", "__asan_globals",
106 if (!globals) return;
107 if (size % sizeof(__asan_global) != 0) return;
108 op(globals, size / sizeof(__asan_global));
111 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
115 // Support for the following functions from libdispatch on Mac OS:
116 // dispatch_async_f()
120 // dispatch_after_f()
122 // dispatch_group_async_f()
123 // dispatch_group_async()
124 // TODO(glider): libdispatch API contains other functions that we don't support
127 // dispatch_sync() and dispatch_sync_f() are synchronous, although chances are
128 // they can cause jobs to run on a thread different from the current one.
129 // TODO(glider): if so, we need a test for this (otherwise we should remove
132 // The following functions use dispatch_barrier_async_f() (which isn't a library
133 // function but is exported) and are thus supported:
134 // dispatch_source_set_cancel_handler_f()
135 // dispatch_source_set_cancel_handler()
136 // dispatch_source_set_event_handler_f()
137 // dispatch_source_set_event_handler()
139 // The reference manual for Grand Central Dispatch is available at
140 // http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
141 // The implementation details are at
142 // http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c
144 typedef void* dispatch_group_t;
145 typedef void* dispatch_queue_t;
146 typedef void* dispatch_source_t;
147 typedef u64 dispatch_time_t;
148 typedef void (*dispatch_function_t)(void *block);
149 typedef void* (*worker_t)(void *block);
151 // A wrapper for the ObjC blocks used to support libdispatch.
154 dispatch_function_t func;
156 } asan_block_context_t;
159 void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
160 AsanThread *t = GetCurrentThread();
162 t = AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr,
163 parent_tid, stack, /* detached */ true);
165 asanThreadRegistry().StartThread(t->tid(), GetTid(),
166 /* workerthread */ true, 0);
171 // For use by only those functions that allocated the context via
172 // alloc_asan_context().
174 void asan_dispatch_call_block_and_release(void *block) {
175 GET_STACK_TRACE_THREAD;
176 asan_block_context_t *context = (asan_block_context_t*)block;
178 "asan_dispatch_call_block_and_release(): "
179 "context: %p, pthread_self: %p\n",
180 block, pthread_self());
181 asan_register_worker_thread(context->parent_tid, &stack);
182 // Call the original dispatcher for the block.
183 context->func(context->block);
184 asan_free(context, &stack, FROM_MALLOC);
187 } // namespace __asan
189 using namespace __asan; // NOLINT
191 // Wrap |ctxt| and |func| into an asan_block_context_t.
192 // The caller retains control of the allocated context.
194 asan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func,
195 BufferedStackTrace *stack) {
196 asan_block_context_t *asan_ctxt =
197 (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack);
198 asan_ctxt->block = ctxt;
199 asan_ctxt->func = func;
200 asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
204 // Define interceptor for dispatch_*_f function with the three most common
205 // parameters: dispatch_queue_t, context, dispatch_function_t.
206 #define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \
207 INTERCEPTOR(void, dispatch_x_f, 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_x_f "(): context: %p, pthread_self: %p\n", \
213 asan_ctxt, pthread_self()); \
214 PRINT_CURRENT_STACK(); \
216 return REAL(dispatch_x_f)(dq, (void*)asan_ctxt, \
217 asan_dispatch_call_block_and_release); \
220 INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)
221 INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)
222 INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)
224 INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when,
225 dispatch_queue_t dq, void *ctxt,
226 dispatch_function_t func) {
227 GET_STACK_TRACE_THREAD;
228 asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
229 if (Verbosity() >= 2) {
230 Report("dispatch_after_f: %p\n", asan_ctxt);
231 PRINT_CURRENT_STACK();
233 return REAL(dispatch_after_f)(when, dq, (void*)asan_ctxt,
234 asan_dispatch_call_block_and_release);
237 INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
238 dispatch_queue_t dq, void *ctxt,
239 dispatch_function_t func) {
240 GET_STACK_TRACE_THREAD;
241 asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
242 if (Verbosity() >= 2) {
243 Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n",
244 asan_ctxt, pthread_self());
245 PRINT_CURRENT_STACK();
247 REAL(dispatch_group_async_f)(group, dq, (void*)asan_ctxt,
248 asan_dispatch_call_block_and_release);
251 #if !defined(MISSING_BLOCKS_SUPPORT)
253 void dispatch_async(dispatch_queue_t dq, void(^work)(void));
254 void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
256 void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
258 void dispatch_source_set_cancel_handler(dispatch_source_t ds,
260 void dispatch_source_set_event_handler(dispatch_source_t ds, void(^work)(void));
263 #define GET_ASAN_BLOCK(work) \
264 void (^asan_block)(void); \
265 int parent_tid = GetCurrentTidOrInvalid(); \
266 asan_block = ^(void) { \
267 GET_STACK_TRACE_THREAD; \
268 asan_register_worker_thread(parent_tid, &stack); \
272 INTERCEPTOR(void, dispatch_async,
273 dispatch_queue_t dq, void(^work)(void)) {
274 ENABLE_FRAME_POINTER;
275 GET_ASAN_BLOCK(work);
276 REAL(dispatch_async)(dq, asan_block);
279 INTERCEPTOR(void, dispatch_group_async,
280 dispatch_group_t dg, dispatch_queue_t dq, void(^work)(void)) {
281 ENABLE_FRAME_POINTER;
282 GET_ASAN_BLOCK(work);
283 REAL(dispatch_group_async)(dg, dq, asan_block);
286 INTERCEPTOR(void, dispatch_after,
287 dispatch_time_t when, dispatch_queue_t queue, void(^work)(void)) {
288 ENABLE_FRAME_POINTER;
289 GET_ASAN_BLOCK(work);
290 REAL(dispatch_after)(when, queue, asan_block);
293 INTERCEPTOR(void, dispatch_source_set_cancel_handler,
294 dispatch_source_t ds, void(^work)(void)) {
296 REAL(dispatch_source_set_cancel_handler)(ds, work);
299 ENABLE_FRAME_POINTER;
300 GET_ASAN_BLOCK(work);
301 REAL(dispatch_source_set_cancel_handler)(ds, asan_block);
304 INTERCEPTOR(void, dispatch_source_set_event_handler,
305 dispatch_source_t ds, void(^work)(void)) {
306 ENABLE_FRAME_POINTER;
307 GET_ASAN_BLOCK(work);
308 REAL(dispatch_source_set_event_handler)(ds, asan_block);
312 #endif // SANITIZER_MAC