asan_mac.cpp 10 KB

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