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