asan_mac.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_APPLE
  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), ASAN_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. // Support for the following functions from libdispatch on Mac OS:
  83. // dispatch_async_f()
  84. // dispatch_async()
  85. // dispatch_sync_f()
  86. // dispatch_sync()
  87. // dispatch_after_f()
  88. // dispatch_after()
  89. // dispatch_group_async_f()
  90. // dispatch_group_async()
  91. // TODO(glider): libdispatch API contains other functions that we don't support
  92. // yet.
  93. //
  94. // dispatch_sync() and dispatch_sync_f() are synchronous, although chances are
  95. // they can cause jobs to run on a thread different from the current one.
  96. // TODO(glider): if so, we need a test for this (otherwise we should remove
  97. // them).
  98. //
  99. // The following functions use dispatch_barrier_async_f() (which isn't a library
  100. // function but is exported) and are thus supported:
  101. // dispatch_source_set_cancel_handler_f()
  102. // dispatch_source_set_cancel_handler()
  103. // dispatch_source_set_event_handler_f()
  104. // dispatch_source_set_event_handler()
  105. //
  106. // The reference manual for Grand Central Dispatch is available at
  107. // http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html
  108. // The implementation details are at
  109. // http://libdispatch.macosforge.org/trac/browser/trunk/src/queue.c
  110. typedef void* dispatch_group_t;
  111. typedef void* dispatch_queue_t;
  112. typedef void* dispatch_source_t;
  113. typedef u64 dispatch_time_t;
  114. typedef void (*dispatch_function_t)(void *block);
  115. typedef void* (*worker_t)(void *block);
  116. // A wrapper for the ObjC blocks used to support libdispatch.
  117. typedef struct {
  118. void *block;
  119. dispatch_function_t func;
  120. u32 parent_tid;
  121. } asan_block_context_t;
  122. ALWAYS_INLINE
  123. void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
  124. AsanThread *t = GetCurrentThread();
  125. if (!t) {
  126. t = AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr,
  127. parent_tid, stack, /* detached */ true);
  128. t->Init();
  129. asanThreadRegistry().StartThread(t->tid(), GetTid(), ThreadType::Worker,
  130. nullptr);
  131. SetCurrentThread(t);
  132. }
  133. }
  134. // For use by only those functions that allocated the context via
  135. // alloc_asan_context().
  136. extern "C"
  137. void asan_dispatch_call_block_and_release(void *block) {
  138. GET_STACK_TRACE_THREAD;
  139. asan_block_context_t *context = (asan_block_context_t*)block;
  140. VReport(2,
  141. "asan_dispatch_call_block_and_release(): "
  142. "context: %p, pthread_self: %p\n",
  143. block, pthread_self());
  144. asan_register_worker_thread(context->parent_tid, &stack);
  145. // Call the original dispatcher for the block.
  146. context->func(context->block);
  147. asan_free(context, &stack, FROM_MALLOC);
  148. }
  149. } // namespace __asan
  150. using namespace __asan;
  151. // Wrap |ctxt| and |func| into an asan_block_context_t.
  152. // The caller retains control of the allocated context.
  153. extern "C"
  154. asan_block_context_t *alloc_asan_context(void *ctxt, dispatch_function_t func,
  155. BufferedStackTrace *stack) {
  156. asan_block_context_t *asan_ctxt =
  157. (asan_block_context_t*) asan_malloc(sizeof(asan_block_context_t), stack);
  158. asan_ctxt->block = ctxt;
  159. asan_ctxt->func = func;
  160. asan_ctxt->parent_tid = GetCurrentTidOrInvalid();
  161. return asan_ctxt;
  162. }
  163. // Define interceptor for dispatch_*_f function with the three most common
  164. // parameters: dispatch_queue_t, context, dispatch_function_t.
  165. #define INTERCEPT_DISPATCH_X_F_3(dispatch_x_f) \
  166. INTERCEPTOR(void, dispatch_x_f, dispatch_queue_t dq, void *ctxt, \
  167. dispatch_function_t func) { \
  168. GET_STACK_TRACE_THREAD; \
  169. asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack); \
  170. if (Verbosity() >= 2) { \
  171. Report(#dispatch_x_f "(): context: %p, pthread_self: %p\n", \
  172. asan_ctxt, pthread_self()); \
  173. PRINT_CURRENT_STACK(); \
  174. } \
  175. return REAL(dispatch_x_f)(dq, (void*)asan_ctxt, \
  176. asan_dispatch_call_block_and_release); \
  177. }
  178. INTERCEPT_DISPATCH_X_F_3(dispatch_async_f)
  179. INTERCEPT_DISPATCH_X_F_3(dispatch_sync_f)
  180. INTERCEPT_DISPATCH_X_F_3(dispatch_barrier_async_f)
  181. INTERCEPTOR(void, dispatch_after_f, dispatch_time_t when,
  182. 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_after_f: %p\n", asan_ctxt);
  188. PRINT_CURRENT_STACK();
  189. }
  190. return REAL(dispatch_after_f)(when, dq, (void*)asan_ctxt,
  191. asan_dispatch_call_block_and_release);
  192. }
  193. INTERCEPTOR(void, dispatch_group_async_f, dispatch_group_t group,
  194. dispatch_queue_t dq, void *ctxt,
  195. dispatch_function_t func) {
  196. GET_STACK_TRACE_THREAD;
  197. asan_block_context_t *asan_ctxt = alloc_asan_context(ctxt, func, &stack);
  198. if (Verbosity() >= 2) {
  199. Report("dispatch_group_async_f(): context: %p, pthread_self: %p\n",
  200. asan_ctxt, pthread_self());
  201. PRINT_CURRENT_STACK();
  202. }
  203. REAL(dispatch_group_async_f)(group, dq, (void*)asan_ctxt,
  204. asan_dispatch_call_block_and_release);
  205. }
  206. #if !defined(MISSING_BLOCKS_SUPPORT)
  207. extern "C" {
  208. void dispatch_async(dispatch_queue_t dq, void(^work)(void));
  209. void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
  210. void(^work)(void));
  211. void dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
  212. void(^work)(void));
  213. void dispatch_source_set_cancel_handler(dispatch_source_t ds,
  214. void(^work)(void));
  215. void dispatch_source_set_event_handler(dispatch_source_t ds, void(^work)(void));
  216. }
  217. #define GET_ASAN_BLOCK(work) \
  218. void (^asan_block)(void); \
  219. int parent_tid = GetCurrentTidOrInvalid(); \
  220. asan_block = ^(void) { \
  221. GET_STACK_TRACE_THREAD; \
  222. asan_register_worker_thread(parent_tid, &stack); \
  223. work(); \
  224. }
  225. INTERCEPTOR(void, dispatch_async,
  226. dispatch_queue_t dq, void(^work)(void)) {
  227. ENABLE_FRAME_POINTER;
  228. GET_ASAN_BLOCK(work);
  229. REAL(dispatch_async)(dq, asan_block);
  230. }
  231. INTERCEPTOR(void, dispatch_group_async,
  232. dispatch_group_t dg, dispatch_queue_t dq, void(^work)(void)) {
  233. ENABLE_FRAME_POINTER;
  234. GET_ASAN_BLOCK(work);
  235. REAL(dispatch_group_async)(dg, dq, asan_block);
  236. }
  237. INTERCEPTOR(void, dispatch_after,
  238. dispatch_time_t when, dispatch_queue_t queue, void(^work)(void)) {
  239. ENABLE_FRAME_POINTER;
  240. GET_ASAN_BLOCK(work);
  241. REAL(dispatch_after)(when, queue, asan_block);
  242. }
  243. INTERCEPTOR(void, dispatch_source_set_cancel_handler,
  244. dispatch_source_t ds, void(^work)(void)) {
  245. if (!work) {
  246. REAL(dispatch_source_set_cancel_handler)(ds, work);
  247. return;
  248. }
  249. ENABLE_FRAME_POINTER;
  250. GET_ASAN_BLOCK(work);
  251. REAL(dispatch_source_set_cancel_handler)(ds, asan_block);
  252. }
  253. INTERCEPTOR(void, dispatch_source_set_event_handler,
  254. dispatch_source_t ds, void(^work)(void)) {
  255. ENABLE_FRAME_POINTER;
  256. GET_ASAN_BLOCK(work);
  257. REAL(dispatch_source_set_event_handler)(ds, asan_block);
  258. }
  259. #endif
  260. #endif // SANITIZER_APPLE