1 //===-- asan_mac.cc -------------------------------------------------------===// 2 // 3 // This file is distributed under the University of Illinois Open Source 4 // License. See LICENSE.TXT for details. 5 // 6 //===----------------------------------------------------------------------===// 7 // 8 // This file is a part of AddressSanitizer, an address sanity checker. 9 // 10 // Mac-specific details. 11 //===----------------------------------------------------------------------===// 12 13 #include "sanitizer_common/sanitizer_platform.h" 14 #if SANITIZER_MAC 15 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" 24 25 #include <dlfcn.h> 26 #include <fcntl.h> 27 #include <libkern/OSAtomic.h> 28 #include <mach-o/dyld.h> 29 #include <mach-o/getsect.h> 30 #include <mach-o/loader.h> 31 #include <pthread.h> 32 #include <stdlib.h> // for free() 33 #include <sys/mman.h> 34 #include <sys/resource.h> 35 #include <sys/sysctl.h> 36 #include <sys/ucontext.h> 37 #include <unistd.h> 38 39 // from <crt_externs.h>, but we don't have that file on iOS 40 extern "C" { 41 extern char ***_NSGetArgv(void); 42 extern char ***_NSGetEnviron(void); 43 } 44 45 namespace __asan { 46 47 void InitializePlatformInterceptors() {} 48 void InitializePlatformExceptionHandlers() {} 49 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; 57 } 58 59 // No-op. Mac does not support static linkage anyway. 60 void *AsanDoesNotSupportStaticLinkage() { 61 return 0; 62 } 63 64 // No-op. Mac does not support static linkage anyway. 65 void AsanCheckDynamicRTPrereqs() {} 66 67 // No-op. Mac does not support static linkage anyway. 68 void AsanCheckIncompatibleRT() {} 69 70 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) { 71 // Find the Mach-O header for the image containing the needle 72 Dl_info info; 73 int err = dladdr(needle, &info); 74 if (err == 0) return; 75 76 #if __LP64__ 77 const struct mach_header_64 *mh = (struct mach_header_64 *)info.dli_fbase; 78 #else 79 const struct mach_header *mh = (struct mach_header *)info.dli_fbase; 80 #endif 81 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( 85 mh, 86 "__DATA", "__asan_globals", 87 &size); 88 89 if (!globals) return; 90 if (size % sizeof(__asan_global) != 0) return; 91 op(globals, size / sizeof(__asan_global)); 92 } 93 94 void ReadContextStack(void *context, uptr *stack, uptr *ssize) { 95 UNIMPLEMENTED(); 96 } 97 98 // Support for the following functions from libdispatch on Mac OS: 99 // dispatch_async_f() 100 // dispatch_async() 101 // dispatch_sync_f() 102 // dispatch_sync() 103 // dispatch_after_f() 104 // dispatch_after() 105 // dispatch_group_async_f() 106 // dispatch_group_async() 107 // TODO(glider): libdispatch API contains other functions that we don't support 108 // yet. 109 // 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 113 // them). 114 // 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() 121 // 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 126 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); 133 134 // A wrapper for the ObjC blocks used to support libdispatch. 135 typedef struct { 136 void *block; 137 dispatch_function_t func; 138 u32 parent_tid; 139 } asan_block_context_t; 140 141 ALWAYS_INLINE 142 void asan_register_worker_thread(int parent_tid, StackTrace *stack) { 143 AsanThread *t = GetCurrentThread(); 144 if (!t) { 145 t = AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr, 146 parent_tid, stack, /* detached */ true); 147 t->Init(); 148 asanThreadRegistry().StartThread(t->tid(), 0, 0); 149 SetCurrentThread(t); 150 } 151 } 152 153 // For use by only those functions that allocated the context via 154 // alloc_asan_context(). 155 extern "C" 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; 159 VReport(2, 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); 167 } 168 169 } // namespace __asan 170 171 using namespace __asan; // NOLINT 172 173 // Wrap |ctxt| and |func| into an asan_block_context_t. 174 // The caller retains control of the allocated context. 175 extern "C" 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(); 183 return asan_ctxt; 184 } 185 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(); \ 197 } \ 198 return REAL(dispatch_x_f)(dq, (void*)asan_ctxt, \ 199 asan_dispatch_call_block_and_release); \ 200 } 201 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) 205 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(); 214 } 215 return REAL(dispatch_after_f)(when, dq, (void*)asan_ctxt, 216 asan_dispatch_call_block_and_release); 217 } 218 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(); 228 } 229 REAL(dispatch_group_async_f)(group, dq, (void*)asan_ctxt, 230 asan_dispatch_call_block_and_release); 231 } 232 233 #if !defined(MISSING_BLOCKS_SUPPORT) 234 extern "C" { 235 void dispatch_async(dispatch_queue_t dq, void(^work)(void)); 236 void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq, 237 void(^work)(void)); 238 void dispatch_after(dispatch_time_t when, dispatch_queue_t queue, 239 void(^work)(void)); 240 void dispatch_source_set_cancel_handler(dispatch_source_t ds, 241 void(^work)(void)); 242 void dispatch_source_set_event_handler(dispatch_source_t ds, void(^work)(void)); 243 } 244 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); \ 251 work(); \ 252 } 253 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); 259 } 260 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); 266 } 267 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); 273 } 274 275 INTERCEPTOR(void, dispatch_source_set_cancel_handler, 276 dispatch_source_t ds, void(^work)(void)) { 277 if (!work) { 278 REAL(dispatch_source_set_cancel_handler)(ds, work); 279 return; 280 } 281 ENABLE_FRAME_POINTER; 282 GET_ASAN_BLOCK(work); 283 REAL(dispatch_source_set_cancel_handler)(ds, asan_block); 284 } 285 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); 291 } 292 #endif 293 294 #endif // SANITIZER_MAC 295