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