blob: 695d957d63e50082b428ba39d03f1c70e15d892e [file] [log] [blame]
Kostya Serebryany019b76f2011-11-30 01:07:021//===-- 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 Serebryany5dfa4da2011-12-01 21:40:5215#ifdef __APPLE__
Kostya Serebryany019b76f2011-11-30 01:07:0216
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 Serebryany019b76f2011-11-30 01:07:0224#include <sys/mman.h>
Kostya Serebryany2b087182012-01-06 02:12:2525#include <sys/resource.h>
Kostya Serebryany25d6c1b2012-01-06 19:11:0926#include <sys/ucontext.h>
Kostya Serebryany78d87d32012-01-05 01:07:2727#include <pthread.h>
Kostya Serebryanya7720962011-12-28 23:28:5428#include <fcntl.h>
Kostya Serebryany019b76f2011-11-30 01:07:0229#include <unistd.h>
Kostya Serebryanya82f0d42012-01-10 21:24:4030#include <libkern/OSAtomic.h>
Kostya Serebryany019b76f2011-11-30 01:07:0231
32namespace __asan {
33
34extern dispatch_async_f_f real_dispatch_async_f;
35extern dispatch_sync_f_f real_dispatch_sync_f;
36extern dispatch_after_f_f real_dispatch_after_f;
37extern dispatch_barrier_async_f_f real_dispatch_barrier_async_f;
38extern dispatch_group_async_f_f real_dispatch_group_async_f;
39extern pthread_workqueue_additem_np_f real_pthread_workqueue_additem_np;
40
Kostya Serebryany25d6c1b2012-01-06 19:11:0941void 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 Serebryany019b76f2011-11-30 01:07:0254// No-op. Mac does not support static linkage anyway.
55void *AsanDoesNotSupportStaticLinkage() {
56 return NULL;
57}
58
Kostya Serebryany9fd01e52012-01-09 18:53:1559bool AsanInterceptsSignal(int signum) {
60 return (signum == SIGSEGV || signum == SIGBUS) && FLAG_handle_segv;
61}
62
Kostya Serebryanya7720962011-12-28 23:28:5463static void *asan_mmap(void *addr, size_t length, int prot, int flags,
Kostya Serebryany019b76f2011-11-30 01:07:0264 int fd, uint64_t offset) {
65 return mmap(addr, length, prot, flags, fd, offset);
66}
67
Kostya Serebryanyedb4a8a2012-01-09 23:11:2668size_t AsanWrite(int fd, const void *buf, size_t count) {
Kostya Serebryany019b76f2011-11-30 01:07:0269 return write(fd, buf, count);
70}
71
Kostya Serebryany6c4bd802011-12-28 22:58:0172void *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 Serebryanya7720962011-12-28 23:28:5483void *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
90void *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
97void *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 Serebryany6c4bd802011-12-28 22:58:01104void 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 Serebryanyedb4a8a2012-01-09 23:11:26109 AsanDie();
Kostya Serebryany6c4bd802011-12-28 22:58:01110 }
111}
112
113int AsanOpenReadonly(const char* filename) {
114 return open(filename, O_RDONLY);
115}
116
Kostya Serebryanyedb4a8a2012-01-09 23:11:26117size_t AsanRead(int fd, void *buf, size_t count) {
Kostya Serebryany6c4bd802011-12-28 22:58:01118 return read(fd, buf, count);
119}
120
121int AsanClose(int fd) {
122 return close(fd);
123}
124
Kostya Serebryany78d87d32012-01-05 01:07:27125void 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 Serebryanya82f0d42012-01-10 21:24:40134
135AsanLock::AsanLock(LinkerInitialized) {
136 // We assume that OS_SPINLOCK_INIT is zero
137}
138
139void 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
148void AsanLock::Unlock() {
149 CHECK(owner_ == (uintptr_t)pthread_self());
150 owner_ = 0;
151 OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
152}
153
154
Kostya Serebryany019b76f2011-11-30 01:07:02155// 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
184extern "C"
185void 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 Serebryany3f4b9bb2012-01-06 19:44:11209 AsanThread *t = AsanThread::Create(context->parent_tid, NULL, NULL);
210 asanThreadRegistry().RegisterThread(t, context->parent_tid, &stack);
Kostya Serebryany6bb2f1d2011-12-16 19:13:35211 t->Init();
Kostya Serebryany019b76f2011-11-30 01:07:02212 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
221using namespace __asan; // NOLINT
222
223// Wrap |ctxt| and |func| into an asan_block_context_t.
224// The caller retains control of the allocated context.
225extern "C"
226asan_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?
248extern "C"
249int 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
263extern "C"
264int 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
278extern "C"
279int 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
293extern "C"
294void 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
307extern "C"
308void 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.
328extern "C"
329void *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
341extern "C"
342int 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 Serebryany5dfa4da2011-12-01 21:40:52358
359#endif // __APPLE__