Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 1 | //===-- asan_mac.cc -------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // Mac-specific details. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 | [diff] [blame] | 15 | #ifdef __APPLE__ |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 16 | |
| 17 | #include "asan_mac.h" |
| 18 | |
| 19 | #include "asan_internal.h" |
| 20 | #include "asan_stack.h" |
| 21 | #include "asan_thread.h" |
| 22 | #include "asan_thread_registry.h" |
| 23 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 24 | #include <sys/mman.h> |
Kostya Serebryany | 2b08718 | 2012-01-06 02:12:25 | [diff] [blame] | 25 | #include <sys/resource.h> |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 | [diff] [blame] | 26 | #include <sys/ucontext.h> |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 | [diff] [blame] | 27 | #include <pthread.h> |
Kostya Serebryany | a772096 | 2011-12-28 23:28:54 | [diff] [blame] | 28 | #include <fcntl.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 29 | #include <unistd.h> |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 | [diff] [blame] | 30 | #include <libkern/OSAtomic.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 31 | |
| 32 | namespace __asan { |
| 33 | |
| 34 | extern dispatch_async_f_f real_dispatch_async_f; |
| 35 | extern dispatch_sync_f_f real_dispatch_sync_f; |
| 36 | extern dispatch_after_f_f real_dispatch_after_f; |
| 37 | extern dispatch_barrier_async_f_f real_dispatch_barrier_async_f; |
| 38 | extern dispatch_group_async_f_f real_dispatch_group_async_f; |
| 39 | extern pthread_workqueue_additem_np_f real_pthread_workqueue_additem_np; |
| 40 | |
Kostya Serebryany | 25d6c1b | 2012-01-06 19:11:09 | [diff] [blame] | 41 | void GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp) { |
| 42 | ucontext_t *ucontext = (ucontext_t*)context; |
| 43 | # if __WORDSIZE == 64 |
| 44 | *pc = ucontext->uc_mcontext->__ss.__rip; |
| 45 | *bp = ucontext->uc_mcontext->__ss.__rbp; |
| 46 | *sp = ucontext->uc_mcontext->__ss.__rsp; |
| 47 | # else |
| 48 | *pc = ucontext->uc_mcontext->__ss.__eip; |
| 49 | *bp = ucontext->uc_mcontext->__ss.__ebp; |
| 50 | *sp = ucontext->uc_mcontext->__ss.__esp; |
| 51 | # endif // __WORDSIZE |
| 52 | } |
| 53 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 54 | // No-op. Mac does not support static linkage anyway. |
| 55 | void *AsanDoesNotSupportStaticLinkage() { |
| 56 | return NULL; |
| 57 | } |
| 58 | |
Kostya Serebryany | 9fd01e5 | 2012-01-09 18:53:15 | [diff] [blame] | 59 | bool AsanInterceptsSignal(int signum) { |
| 60 | return (signum == SIGSEGV || signum == SIGBUS) && FLAG_handle_segv; |
| 61 | } |
| 62 | |
Kostya Serebryany | a772096 | 2011-12-28 23:28:54 | [diff] [blame] | 63 | static void *asan_mmap(void *addr, size_t length, int prot, int flags, |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 64 | int fd, uint64_t offset) { |
| 65 | return mmap(addr, length, prot, flags, fd, offset); |
| 66 | } |
| 67 | |
Kostya Serebryany | edb4a8a | 2012-01-09 23:11:26 | [diff] [blame] | 68 | size_t AsanWrite(int fd, const void *buf, size_t count) { |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 69 | return write(fd, buf, count); |
| 70 | } |
| 71 | |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 | [diff] [blame] | 72 | void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) { |
| 73 | size = RoundUpTo(size, kPageSize); |
| 74 | void *res = asan_mmap(0, size, |
| 75 | PROT_READ | PROT_WRITE, |
| 76 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 77 | if (res == (void*)-1) { |
| 78 | OutOfMemoryMessageAndDie(mem_type, size); |
| 79 | } |
| 80 | return res; |
| 81 | } |
| 82 | |
Kostya Serebryany | a772096 | 2011-12-28 23:28:54 | [diff] [blame] | 83 | void *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size) { |
| 84 | return asan_mmap((void*)fixed_addr, size, |
| 85 | PROT_READ | PROT_WRITE, |
| 86 | MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, |
| 87 | 0, 0); |
| 88 | } |
| 89 | |
| 90 | void *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size) { |
| 91 | return asan_mmap((void*)fixed_addr, size, |
| 92 | PROT_READ | PROT_WRITE, |
| 93 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 94 | 0, 0); |
| 95 | } |
| 96 | |
| 97 | void *AsanMprotect(uintptr_t fixed_addr, size_t size) { |
| 98 | return asan_mmap((void*)fixed_addr, size, |
| 99 | PROT_NONE, |
| 100 | MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, |
| 101 | 0, 0); |
| 102 | } |
| 103 | |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 | [diff] [blame] | 104 | void AsanUnmapOrDie(void *addr, size_t size) { |
| 105 | if (!addr || !size) return; |
| 106 | int res = munmap(addr, size); |
| 107 | if (res != 0) { |
| 108 | Report("Failed to unmap\n"); |
Kostya Serebryany | edb4a8a | 2012-01-09 23:11:26 | [diff] [blame] | 109 | AsanDie(); |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | int AsanOpenReadonly(const char* filename) { |
| 114 | return open(filename, O_RDONLY); |
| 115 | } |
| 116 | |
Kostya Serebryany | edb4a8a | 2012-01-09 23:11:26 | [diff] [blame] | 117 | size_t AsanRead(int fd, void *buf, size_t count) { |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 | [diff] [blame] | 118 | return read(fd, buf, count); |
| 119 | } |
| 120 | |
| 121 | int AsanClose(int fd) { |
| 122 | return close(fd); |
| 123 | } |
| 124 | |
Kostya Serebryany | 78d87d3 | 2012-01-05 01:07:27 | [diff] [blame] | 125 | void AsanThread::SetThreadStackTopAndBottom() { |
| 126 | size_t stacksize = pthread_get_stacksize_np(pthread_self()); |
| 127 | void *stackaddr = pthread_get_stackaddr_np(pthread_self()); |
| 128 | stack_top_ = (uintptr_t)stackaddr; |
| 129 | stack_bottom_ = stack_top_ - stacksize; |
| 130 | int local; |
| 131 | CHECK(AddrIsInStack((uintptr_t)&local)); |
| 132 | } |
| 133 | |
Kostya Serebryany | a82f0d4 | 2012-01-10 21:24:40 | [diff] [blame] | 134 | |
| 135 | AsanLock::AsanLock(LinkerInitialized) { |
| 136 | // We assume that OS_SPINLOCK_INIT is zero |
| 137 | } |
| 138 | |
| 139 | void AsanLock::Lock() { |
| 140 | CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_)); |
| 141 | CHECK(OS_SPINLOCK_INIT == 0); |
| 142 | CHECK(owner_ != (uintptr_t)pthread_self()); |
| 143 | OSSpinLockLock((OSSpinLock*)&opaque_storage_); |
| 144 | CHECK(!owner_); |
| 145 | owner_ = (uintptr_t)pthread_self(); |
| 146 | } |
| 147 | |
| 148 | void AsanLock::Unlock() { |
| 149 | CHECK(owner_ == (uintptr_t)pthread_self()); |
| 150 | owner_ = 0; |
| 151 | OSSpinLockUnlock((OSSpinLock*)&opaque_storage_); |
| 152 | } |
| 153 | |
| 154 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 155 | // Support for the following functions from libdispatch on Mac OS: |
| 156 | // dispatch_async_f() |
| 157 | // dispatch_async() |
| 158 | // dispatch_sync_f() |
| 159 | // dispatch_sync() |
| 160 | // dispatch_after_f() |
| 161 | // dispatch_after() |
| 162 | // dispatch_group_async_f() |
| 163 | // dispatch_group_async() |
| 164 | // TODO(glider): libdispatch API contains other functions that we don't support |
| 165 | // yet. |
| 166 | // |
| 167 | // dispatch_sync() and dispatch_sync_f() are synchronous, although chances are |
| 168 | // they can cause jobs to run on a thread different from the current one. |
| 169 | // TODO(glider): if so, we need a test for this (otherwise we should remove |
| 170 | // them). |
| 171 | // |
| 172 | // The following functions use dispatch_barrier_async_f() (which isn't a library |
| 173 | // function but is exported) and are thus supported: |
| 174 | // dispatch_source_set_cancel_handler_f() |
| 175 | // dispatch_source_set_cancel_handler() |
| 176 | // dispatch_source_set_event_handler_f() |
| 177 | // dispatch_source_set_event_handler() |
| 178 | // |
| 179 | // The reference manual for Grand Central Dispatch is available at |
| 180 | // http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html |
| 181 | // The implementation details are at |
| 182 | // http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c |
| 183 | |
| 184 | extern "C" |
| 185 | void asan_dispatch_call_block_and_release(void *block) { |
| 186 | GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false); |
| 187 | asan_block_context_t *context = (asan_block_context_t*)block; |
| 188 | if (FLAG_v >= 2) { |
| 189 | Report("asan_dispatch_call_block_and_release(): " |
| 190 | "context: %p, pthread_self: %p\n", |
| 191 | block, pthread_self()); |
| 192 | } |
| 193 | AsanThread *t = asanThreadRegistry().GetCurrent(); |
| 194 | if (t) { |
| 195 | // We've already executed a job on this worker thread. Let's reuse the |
| 196 | // AsanThread object. |
| 197 | if (t != asanThreadRegistry().GetMain()) { |
| 198 | // Flush the statistics and update the current thread's tid. |
| 199 | asanThreadRegistry().UnregisterThread(t); |
| 200 | asanThreadRegistry().RegisterThread(t, context->parent_tid, &stack); |
| 201 | } |
| 202 | // Otherwise the worker is being executed on the main thread -- we are |
| 203 | // draining the dispatch queue. |
| 204 | // TODO(glider): any checks for that? |
| 205 | } else { |
| 206 | // It's incorrect to assert that the current thread is not dying: at least |
| 207 | // the callbacks from dispatch_sync() are sometimes called after the TSD is |
| 208 | // destroyed. |
Kostya Serebryany | 3f4b9bb | 2012-01-06 19:44:11 | [diff] [blame] | 209 | AsanThread *t = AsanThread::Create(context->parent_tid, NULL, NULL); |
| 210 | asanThreadRegistry().RegisterThread(t, context->parent_tid, &stack); |
Kostya Serebryany | 6bb2f1d | 2011-12-16 19:13:35 | [diff] [blame] | 211 | t->Init(); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 | [diff] [blame] | 212 | asanThreadRegistry().SetCurrent(t); |
| 213 | } |
| 214 | // Call the original dispatcher for the block. |
| 215 | context->func(context->block); |
| 216 | asan_free(context, &stack); |
| 217 | } |
| 218 | |
| 219 | } // namespace __asan |
| 220 | |
| 221 | using namespace __asan; // NOLINT |
| 222 | |
| 223 | // Wrap |ctxt| and |func| into an asan_block_context_t. |
| 224 | // The caller retains control of the allocated context. |
| 225 | extern "C" |
| 226 | asan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func, |
| 227 | AsanStackTrace *stack) { |
| 228 | asan_block_context_t *asan_ctxt = |
| 229 | (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack); |
| 230 | asan_ctxt->block = ctxt; |
| 231 | asan_ctxt->func = func; |
| 232 | AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); |
| 233 | if (FLAG_debug) { |
| 234 | // Sometimes at Chromium teardown this assertion is violated: |
| 235 | // -- a task is created via dispatch_async() on the "CFMachPort" |
| 236 | // thread while doing _dispatch_queue_drain(); |
| 237 | // -- a task is created via dispatch_async_f() on the |
| 238 | // "com.apple.root.default-overcommit-priority" thread while doing |
| 239 | // _dispatch_dispose(). |
| 240 | // TODO(glider): find out what's going on. |
| 241 | CHECK(curr_thread || asanThreadRegistry().IsCurrentThreadDying()); |
| 242 | } |
| 243 | asan_ctxt->parent_tid = asanThreadRegistry().GetCurrentTidOrMinusOne(); |
| 244 | return asan_ctxt; |
| 245 | } |
| 246 | |
| 247 | // TODO(glider): can we reduce code duplication by introducing a macro? |
| 248 | extern "C" |
| 249 | int WRAP(dispatch_async_f)(dispatch_queue_t dq, |
| 250 | void *ctxt, |
| 251 | dispatch_function_t func) { |
| 252 | GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false); |
| 253 | asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); |
| 254 | if (FLAG_v >= 2) { |
| 255 | Report("dispatch_async_f(): context: %p, pthread_self: %p\n", |
| 256 | asan_ctxt, pthread_self()); |
| 257 | PRINT_CURRENT_STACK(); |
| 258 | } |
| 259 | return real_dispatch_async_f(dq, (void*)asan_ctxt, |
| 260 | asan_dispatch_call_block_and_release); |
| 261 | } |
| 262 | |
| 263 | extern "C" |
| 264 | int WRAP(dispatch_sync_f)(dispatch_queue_t dq, |
| 265 | void *ctxt, |
| 266 | dispatch_function_t func) { |
| 267 | GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false); |
| 268 | asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); |
| 269 | if (FLAG_v >= 2) { |
| 270 | Report("dispatch_sync_f(): context: %p, pthread_self: %p\n", |
| 271 | asan_ctxt, pthread_self()); |
| 272 | PRINT_CURRENT_STACK(); |
| 273 | } |
| 274 | return real_dispatch_sync_f(dq, (void*)asan_ctxt, |
| 275 | asan_dispatch_call_block_and_release); |
| 276 | } |
| 277 | |
| 278 | extern "C" |
| 279 | int WRAP(dispatch_after_f)(dispatch_time_t when, |
| 280 | dispatch_queue_t dq, |
| 281 | void *ctxt, |
| 282 | dispatch_function_t func) { |
| 283 | GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false); |
| 284 | asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); |
| 285 | if (FLAG_v >= 2) { |
| 286 | Report("dispatch_after_f: %p\n", asan_ctxt); |
| 287 | PRINT_CURRENT_STACK(); |
| 288 | } |
| 289 | return real_dispatch_after_f(when, dq, (void*)asan_ctxt, |
| 290 | asan_dispatch_call_block_and_release); |
| 291 | } |
| 292 | |
| 293 | extern "C" |
| 294 | void WRAP(dispatch_barrier_async_f)(dispatch_queue_t dq, |
| 295 | void *ctxt, dispatch_function_t func) { |
| 296 | GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false); |
| 297 | asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); |
| 298 | if (FLAG_v >= 2) { |
| 299 | Report("dispatch_barrier_async_f(): context: %p, pthread_self: %p\n", |
| 300 | asan_ctxt, pthread_self()); |
| 301 | PRINT_CURRENT_STACK(); |
| 302 | } |
| 303 | real_dispatch_barrier_async_f(dq, (void*)asan_ctxt, |
| 304 | asan_dispatch_call_block_and_release); |
| 305 | } |
| 306 | |
| 307 | extern "C" |
| 308 | void WRAP(dispatch_group_async_f)(dispatch_group_t group, |
| 309 | dispatch_queue_t dq, |
| 310 | void *ctxt, dispatch_function_t func) { |
| 311 | GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false); |
| 312 | asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); |
| 313 | if (FLAG_v >= 2) { |
| 314 | Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n", |
| 315 | asan_ctxt, pthread_self()); |
| 316 | PRINT_CURRENT_STACK(); |
| 317 | } |
| 318 | real_dispatch_group_async_f(group, dq, (void*)asan_ctxt, |
| 319 | asan_dispatch_call_block_and_release); |
| 320 | } |
| 321 | |
| 322 | // The following stuff has been extremely helpful while looking for the |
| 323 | // unhandled functions that spawned jobs on Chromium shutdown. If the verbosity |
| 324 | // level is 2 or greater, we wrap pthread_workqueue_additem_np() in order to |
| 325 | // find the points of worker thread creation (each of such threads may be used |
| 326 | // to run several tasks, that's why this is not enough to support the whole |
| 327 | // libdispatch API. |
| 328 | extern "C" |
| 329 | void *wrap_workitem_func(void *arg) { |
| 330 | if (FLAG_v >= 2) { |
| 331 | Report("wrap_workitem_func: %p, pthread_self: %p\n", arg, pthread_self()); |
| 332 | } |
| 333 | asan_block_context_t *ctxt = (asan_block_context_t*)arg; |
| 334 | worker_t fn = (worker_t)(ctxt->func); |
| 335 | void *result = fn(ctxt->block); |
| 336 | GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false); |
| 337 | asan_free(arg, &stack); |
| 338 | return result; |
| 339 | } |
| 340 | |
| 341 | extern "C" |
| 342 | int WRAP(pthread_workqueue_additem_np)(pthread_workqueue_t workq, |
| 343 | void *(*workitem_func)(void *), void * workitem_arg, |
| 344 | pthread_workitem_handle_t * itemhandlep, unsigned int *gencountp) { |
| 345 | GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false); |
| 346 | asan_block_context_t *asan_ctxt = |
| 347 | (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), &stack); |
| 348 | asan_ctxt->block = workitem_arg; |
| 349 | asan_ctxt->func = (dispatch_function_t)workitem_func; |
| 350 | asan_ctxt->parent_tid = asanThreadRegistry().GetCurrentTidOrMinusOne(); |
| 351 | if (FLAG_v >= 2) { |
| 352 | Report("pthread_workqueue_additem_np: %p\n", asan_ctxt); |
| 353 | PRINT_CURRENT_STACK(); |
| 354 | } |
| 355 | return real_pthread_workqueue_additem_np(workq, wrap_workitem_func, asan_ctxt, |
| 356 | itemhandlep, gencountp); |
| 357 | } |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 | [diff] [blame] | 358 | |
| 359 | #endif // __APPLE__ |