]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_mac.cc
Merge compiler-rt trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_mac.cc
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
15 #include "sanitizer_common/sanitizer_platform.h"
16 #if SANITIZER_MAC
17
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"
26
27 #include <dlfcn.h>
28 #include <fcntl.h>
29 #include <libkern/OSAtomic.h>
30 #include <mach-o/dyld.h>
31 #include <mach-o/getsect.h>
32 #include <mach-o/loader.h>
33 #include <pthread.h>
34 #include <stdlib.h>  // for free()
35 #include <sys/mman.h>
36 #include <sys/resource.h>
37 #include <sys/sysctl.h>
38 #include <sys/ucontext.h>
39 #include <unistd.h>
40
41 // from <crt_externs.h>, but we don't have that file on iOS
42 extern "C" {
43   extern char ***_NSGetArgv(void);
44   extern char ***_NSGetEnviron(void);
45 }
46
47 namespace __asan {
48
49 void InitializePlatformInterceptors() {}
50 void InitializePlatformExceptionHandlers() {}
51 bool IsSystemHeapAddress (uptr addr) { return false; }
52
53 // No-op. Mac does not support static linkage anyway.
54 void *AsanDoesNotSupportStaticLinkage() {
55   return 0;
56 }
57
58 // No-op. Mac does not support static linkage anyway.
59 void AsanCheckDynamicRTPrereqs() {}
60
61 // No-op. Mac does not support static linkage anyway.
62 void AsanCheckIncompatibleRT() {}
63
64 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
65   // Find the Mach-O header for the image containing the needle
66   Dl_info info;
67   int err = dladdr(needle, &info);
68   if (err == 0) return;
69
70 #if __LP64__
71   const struct mach_header_64 *mh = (struct mach_header_64 *)info.dli_fbase;
72 #else
73   const struct mach_header *mh = (struct mach_header *)info.dli_fbase;
74 #endif
75
76   // Look up the __asan_globals section in that image and register its globals
77   unsigned long size = 0;
78   __asan_global *globals = (__asan_global *)getsectiondata(
79       mh,
80       "__DATA", "__asan_globals",
81       &size);
82
83   if (!globals) return;
84   if (size % sizeof(__asan_global) != 0) return;
85   op(globals, size / sizeof(__asan_global));
86 }
87
88 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
89   UNIMPLEMENTED();
90 }
91
92 // Support for the following functions from libdispatch on Mac OS:
93 //   dispatch_async_f()
94 //   dispatch_async()
95 //   dispatch_sync_f()
96 //   dispatch_sync()
97 //   dispatch_after_f()
98 //   dispatch_after()
99 //   dispatch_group_async_f()
100 //   dispatch_group_async()
101 // TODO(glider): libdispatch API contains other functions that we don't support
102 // yet.
103 //
104 // dispatch_sync() and dispatch_sync_f() are synchronous, although chances are
105 // they can cause jobs to run on a thread different from the current one.
106 // TODO(glider): if so, we need a test for this (otherwise we should remove
107 // them).
108 //
109 // The following functions use dispatch_barrier_async_f() (which isn't a library
110 // function but is exported) and are thus supported:
111 //   dispatch_source_set_cancel_handler_f()
112 //   dispatch_source_set_cancel_handler()
113 //   dispatch_source_set_event_handler_f()
114 //   dispatch_source_set_event_handler()
115 //
116 // The reference manual for Grand Central Dispatch is available at
117 //   http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
118 // The implementation details are at
119 //   http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c
120
121 typedef void* dispatch_group_t;
122 typedef void* dispatch_queue_t;
123 typedef void* dispatch_source_t;
124 typedef u64 dispatch_time_t;
125 typedef void (*dispatch_function_t)(void *block);
126 typedef void* (*worker_t)(void *block);
127
128 // A wrapper for the ObjC blocks used to support libdispatch.
129 typedef struct {
130   void *block;
131   dispatch_function_t func;
132   u32 parent_tid;
133 } asan_block_context_t;
134
135 ALWAYS_INLINE
136 void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
137   AsanThread *t = GetCurrentThread();
138   if (!t) {
139     t = AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr,
140                            parent_tid, stack, /* detached */ true);
141     t->Init();
142     asanThreadRegistry().StartThread(t->tid(), GetTid(),
143                                      /* workerthread */ true, 0);
144     SetCurrentThread(t);
145   }
146 }
147
148 // For use by only those functions that allocated the context via
149 // alloc_asan_context().
150 extern "C"
151 void asan_dispatch_call_block_and_release(void *block) {
152   GET_STACK_TRACE_THREAD;
153   asan_block_context_t *context = (asan_block_context_t*)block;
154   VReport(2,
155           "asan_dispatch_call_block_and_release(): "
156           "context: %p, pthread_self: %p\n",
157           block, pthread_self());
158   asan_register_worker_thread(context->parent_tid, &stack);
159   // Call the original dispatcher for the block.
160   context->func(context->block);
161   asan_free(context, &stack, FROM_MALLOC);
162 }
163
164 }  // namespace __asan
165
166 using namespace __asan;  // NOLINT
167
168 // Wrap |ctxt| and |func| into an asan_block_context_t.
169 // The caller retains control of the allocated context.
170 extern "C"
171 asan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func,
172                                          BufferedStackTrace *stack) {
173   asan_block_context_t *asan_ctxt =
174       (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack);
175   asan_ctxt->block = ctxt;
176   asan_ctxt->func = func;
177   asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
178   return asan_ctxt;
179 }
180
181 // Define interceptor for dispatch_*_f function with the three most common
182 // parameters: dispatch_queue_t, context, dispatch_function_t.
183 #define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f)                                \
184   INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt,            \
185                                   dispatch_function_t func) {                 \
186     GET_STACK_TRACE_THREAD;                                                   \
187     asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); \
188     if (Verbosity() >= 2) {                                     \
189       Report(#dispatch_x_f "(): context: %p, pthread_self: %p\n",             \
190              asan_ctxt, pthread_self());                                      \
191       PRINT_CURRENT_STACK();                                                  \
192     }                                                                         \
193     return REAL(dispatch_x_f)(dq, (void*)asan_ctxt,                           \
194                               asan_dispatch_call_block_and_release);          \
195   }
196
197 INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)
198 INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)
199 INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)
200
201 INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when,
202                                     dispatch_queue_t dq, void *ctxt,
203                                     dispatch_function_t func) {
204   GET_STACK_TRACE_THREAD;
205   asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
206   if (Verbosity() >= 2) {
207     Report("dispatch_after_f: %p\n", asan_ctxt);
208     PRINT_CURRENT_STACK();
209   }
210   return REAL(dispatch_after_f)(when, dq, (void*)asan_ctxt,
211                                 asan_dispatch_call_block_and_release);
212 }
213
214 INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
215                                           dispatch_queue_t dq, void *ctxt,
216                                           dispatch_function_t func) {
217   GET_STACK_TRACE_THREAD;
218   asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
219   if (Verbosity() >= 2) {
220     Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n",
221            asan_ctxt, pthread_self());
222     PRINT_CURRENT_STACK();
223   }
224   REAL(dispatch_group_async_f)(group, dq, (void*)asan_ctxt,
225                                asan_dispatch_call_block_and_release);
226 }
227
228 #if !defined(MISSING_BLOCKS_SUPPORT)
229 extern "C" {
230 void dispatch_async(dispatch_queue_t dq, void(^work)(void));
231 void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
232                           void(^work)(void));
233 void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
234                     void(^work)(void));
235 void dispatch_source_set_cancel_handler(dispatch_source_t ds,
236                                         void(^work)(void));
237 void dispatch_source_set_event_handler(dispatch_source_t ds, void(^work)(void));
238 }
239
240 #define GET_ASAN_BLOCK(work) \
241   void (^asan_block)(void);  \
242   int parent_tid = GetCurrentTidOrInvalid(); \
243   asan_block = ^(void) { \
244     GET_STACK_TRACE_THREAD; \
245     asan_register_worker_thread(parent_tid, &stack); \
246     work(); \
247   }
248
249 INTERCEPTOR(void, dispatch_async,
250             dispatch_queue_t dq, void(^work)(void)) {
251   ENABLE_FRAME_POINTER;
252   GET_ASAN_BLOCK(work);
253   REAL(dispatch_async)(dq, asan_block);
254 }
255
256 INTERCEPTOR(void, dispatch_group_async,
257             dispatch_group_t dg, dispatch_queue_t dq, void(^work)(void)) {
258   ENABLE_FRAME_POINTER;
259   GET_ASAN_BLOCK(work);
260   REAL(dispatch_group_async)(dg, dq, asan_block);
261 }
262
263 INTERCEPTOR(void, dispatch_after,
264             dispatch_time_t when, dispatch_queue_t queue, void(^work)(void)) {
265   ENABLE_FRAME_POINTER;
266   GET_ASAN_BLOCK(work);
267   REAL(dispatch_after)(when, queue, asan_block);
268 }
269
270 INTERCEPTOR(void, dispatch_source_set_cancel_handler,
271             dispatch_source_t ds, void(^work)(void)) {
272   if (!work) {
273     REAL(dispatch_source_set_cancel_handler)(ds, work);
274     return;
275   }
276   ENABLE_FRAME_POINTER;
277   GET_ASAN_BLOCK(work);
278   REAL(dispatch_source_set_cancel_handler)(ds, asan_block);
279 }
280
281 INTERCEPTOR(void, dispatch_source_set_event_handler,
282             dispatch_source_t ds, void(^work)(void)) {
283   ENABLE_FRAME_POINTER;
284   GET_ASAN_BLOCK(work);
285   REAL(dispatch_source_set_event_handler)(ds, asan_block);
286 }
287 #endif
288
289 #endif  // SANITIZER_MAC