d: Merge upstream dmd, druntime c8ae4adb2e, phobos 792c8b7c1.
[official-gcc.git] / libsanitizer / asan / asan_mac.cpp
bloba2d5c31a3f7740f3d3398c06968f95a49ffc0a6a
1 //===-- asan_mac.cpp ------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // Mac-specific details.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_platform.h"
15 #if SANITIZER_APPLE
17 #include "asan_interceptors.h"
18 #include "asan_internal.h"
19 #include "asan_mapping.h"
20 #include "asan_stack.h"
21 #include "asan_thread.h"
22 #include "sanitizer_common/sanitizer_atomic.h"
23 #include "sanitizer_common/sanitizer_libc.h"
24 #include "sanitizer_common/sanitizer_mac.h"
26 #include <dlfcn.h>
27 #include <fcntl.h>
28 #include <libkern/OSAtomic.h>
29 #include <mach-o/dyld.h>
30 #include <mach-o/getsect.h>
31 #include <mach-o/loader.h>
32 #include <pthread.h>
33 #include <stdlib.h> // for free()
34 #include <sys/mman.h>
35 #include <sys/resource.h>
36 #include <sys/sysctl.h>
37 #include <sys/ucontext.h>
38 #include <unistd.h>
40 // from <crt_externs.h>, but we don't have that file on iOS
41 extern "C" {
42 extern char ***_NSGetArgv(void);
43 extern char ***_NSGetEnviron(void);
46 namespace __asan {
48 void InitializePlatformInterceptors() {}
49 void InitializePlatformExceptionHandlers() {}
50 bool IsSystemHeapAddress (uptr addr) { return false; }
52 // No-op. Mac does not support static linkage anyway.
53 void *AsanDoesNotSupportStaticLinkage() {
54 return 0;
57 uptr FindDynamicShadowStart() {
58 return MapDynamicShadow(MemToShadowSize(kHighMemEnd), ASAN_SHADOW_SCALE,
59 /*min_shadow_base_alignment*/ 0, kHighMemEnd);
62 // No-op. Mac does not support static linkage anyway.
63 void AsanCheckDynamicRTPrereqs() {}
65 // No-op. Mac does not support static linkage anyway.
66 void AsanCheckIncompatibleRT() {}
68 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
69 // Find the Mach-O header for the image containing the needle
70 Dl_info info;
71 int err = dladdr(needle, &info);
72 if (err == 0) return;
74 #if __LP64__
75 const struct mach_header_64 *mh = (struct mach_header_64 *)info.dli_fbase;
76 #else
77 const struct mach_header *mh = (struct mach_header *)info.dli_fbase;
78 #endif
80 // Look up the __asan_globals section in that image and register its globals
81 unsigned long size = 0;
82 __asan_global *globals = (__asan_global *)getsectiondata(
83 mh,
84 "__DATA", "__asan_globals",
85 &size);
87 if (!globals) return;
88 if (size % sizeof(__asan_global) != 0) return;
89 op(globals, size / sizeof(__asan_global));
92 void FlushUnneededASanShadowMemory(uptr p, uptr size) {
93 // Since asan's mapping is compacting, the shadow chunk may be
94 // not page-aligned, so we only flush the page-aligned portion.
95 ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size));
98 void ReadContextStack(void *context, uptr *stack, uptr *ssize) {
99 UNIMPLEMENTED();
102 void ResetContextStack(void *context) { UNIMPLEMENTED(); }
104 // Support for the following functions from libdispatch on Mac OS:
105 // dispatch_async_f()
106 // dispatch_async()
107 // dispatch_sync_f()
108 // dispatch_sync()
109 // dispatch_after_f()
110 // dispatch_after()
111 // dispatch_group_async_f()
112 // dispatch_group_async()
113 // TODO(glider): libdispatch API contains other functions that we don't support
114 // yet.
116 // dispatch_sync() and dispatch_sync_f() are synchronous, although chances are
117 // they can cause jobs to run on a thread different from the current one.
118 // TODO(glider): if so, we need a test for this (otherwise we should remove
119 // them).
121 // The following functions use dispatch_barrier_async_f() (which isn't a library
122 // function but is exported) and are thus supported:
123 // dispatch_source_set_cancel_handler_f()
124 // dispatch_source_set_cancel_handler()
125 // dispatch_source_set_event_handler_f()
126 // dispatch_source_set_event_handler()
128 // The reference manual for Grand Central Dispatch is available at
129 // http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
130 // The implementation details are at
131 // http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c
133 typedef void* dispatch_group_t;
134 typedef void* dispatch_queue_t;
135 typedef void* dispatch_source_t;
136 typedef u64 dispatch_time_t;
137 typedef void (*dispatch_function_t)(void *block);
138 typedef void* (*worker_t)(void *block);
140 // A wrapper for the ObjC blocks used to support libdispatch.
141 typedef struct {
142 void *block;
143 dispatch_function_t func;
144 u32 parent_tid;
145 } asan_block_context_t;
147 ALWAYS_INLINE
148 void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
149 AsanThread *t = GetCurrentThread();
150 if (!t) {
151 t = AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr,
152 parent_tid, stack, /* detached */ true);
153 t->Init();
154 asanThreadRegistry().StartThread(t->tid(), GetTid(), ThreadType::Worker,
155 nullptr);
156 SetCurrentThread(t);
160 // For use by only those functions that allocated the context via
161 // alloc_asan_context().
162 extern "C"
163 void asan_dispatch_call_block_and_release(void *block) {
164 GET_STACK_TRACE_THREAD;
165 asan_block_context_t *context = (asan_block_context_t*)block;
166 VReport(2,
167 "asan_dispatch_call_block_and_release(): "
168 "context: %p, pthread_self: %p\n",
169 block, pthread_self());
170 asan_register_worker_thread(context->parent_tid, &stack);
171 // Call the original dispatcher for the block.
172 context->func(context->block);
173 asan_free(context, &stack, FROM_MALLOC);
176 } // namespace __asan
178 using namespace __asan;
180 // Wrap |ctxt| and |func| into an asan_block_context_t.
181 // The caller retains control of the allocated context.
182 extern "C"
183 asan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func,
184 BufferedStackTrace *stack) {
185 asan_block_context_t *asan_ctxt =
186 (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack);
187 asan_ctxt->block = ctxt;
188 asan_ctxt->func = func;
189 asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
190 return asan_ctxt;
193 // Define interceptor for dispatch_*_f function with the three most common
194 // parameters: dispatch_queue_t, context, dispatch_function_t.
195 #define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \
196 INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \
197 dispatch_function_t func) { \
198 GET_STACK_TRACE_THREAD; \
199 asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); \
200 if (Verbosity() >= 2) { \
201 Report(#dispatch_x_f "(): context: %p, pthread_self: %p\n", \
202 asan_ctxt, pthread_self()); \
203 PRINT_CURRENT_STACK(); \
205 return REAL(dispatch_x_f)(dq, (void*)asan_ctxt, \
206 asan_dispatch_call_block_and_release); \
209 INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)
210 INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)
211 INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)
213 INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when,
214 dispatch_queue_t dq, void *ctxt,
215 dispatch_function_t func) {
216 GET_STACK_TRACE_THREAD;
217 asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
218 if (Verbosity() >= 2) {
219 Report("dispatch_after_f: %p\n", asan_ctxt);
220 PRINT_CURRENT_STACK();
222 return REAL(dispatch_after_f)(when, dq, (void*)asan_ctxt,
223 asan_dispatch_call_block_and_release);
226 INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
227 dispatch_queue_t dq, void *ctxt,
228 dispatch_function_t func) {
229 GET_STACK_TRACE_THREAD;
230 asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
231 if (Verbosity() >= 2) {
232 Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n",
233 asan_ctxt, pthread_self());
234 PRINT_CURRENT_STACK();
236 REAL(dispatch_group_async_f)(group, dq, (void*)asan_ctxt,
237 asan_dispatch_call_block_and_release);
240 #if !defined(MISSING_BLOCKS_SUPPORT)
241 extern "C" {
242 void dispatch_async(dispatch_queue_t dq, void(^work)(void));
243 void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
244 void(^work)(void));
245 void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
246 void(^work)(void));
247 void dispatch_source_set_cancel_handler(dispatch_source_t ds,
248 void(^work)(void));
249 void dispatch_source_set_event_handler(dispatch_source_t ds, void(^work)(void));
252 #define GET_ASAN_BLOCK(work) \
253 void (^asan_block)(void); \
254 int parent_tid = GetCurrentTidOrInvalid(); \
255 asan_block = ^(void) { \
256 GET_STACK_TRACE_THREAD; \
257 asan_register_worker_thread(parent_tid, &stack); \
258 work(); \
261 INTERCEPTOR(void, dispatch_async,
262 dispatch_queue_t dq, void(^work)(void)) {
263 ENABLE_FRAME_POINTER;
264 GET_ASAN_BLOCK(work);
265 REAL(dispatch_async)(dq, asan_block);
268 INTERCEPTOR(void, dispatch_group_async,
269 dispatch_group_t dg, dispatch_queue_t dq, void(^work)(void)) {
270 ENABLE_FRAME_POINTER;
271 GET_ASAN_BLOCK(work);
272 REAL(dispatch_group_async)(dg, dq, asan_block);
275 INTERCEPTOR(void, dispatch_after,
276 dispatch_time_t when, dispatch_queue_t queue, void(^work)(void)) {
277 ENABLE_FRAME_POINTER;
278 GET_ASAN_BLOCK(work);
279 REAL(dispatch_after)(when, queue, asan_block);
282 INTERCEPTOR(void, dispatch_source_set_cancel_handler,
283 dispatch_source_t ds, void(^work)(void)) {
284 if (!work) {
285 REAL(dispatch_source_set_cancel_handler)(ds, work);
286 return;
288 ENABLE_FRAME_POINTER;
289 GET_ASAN_BLOCK(work);
290 REAL(dispatch_source_set_cancel_handler)(ds, asan_block);
293 INTERCEPTOR(void, dispatch_source_set_event_handler,
294 dispatch_source_t ds, void(^work)(void)) {
295 ENABLE_FRAME_POINTER;
296 GET_ASAN_BLOCK(work);
297 REAL(dispatch_source_set_event_handler)(ds, asan_block);
299 #endif
301 #endif // SANITIZER_APPLE