asan_fuchsia.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //===-- asan_fuchsia.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. // Fuchsia-specific details.
  12. //===---------------------------------------------------------------------===//
  13. #include "sanitizer_common/sanitizer_fuchsia.h"
  14. #if SANITIZER_FUCHSIA
  15. #include "asan_interceptors.h"
  16. #include "asan_internal.h"
  17. #include "asan_stack.h"
  18. #include "asan_thread.h"
  19. #include <limits.h>
  20. #error #include <zircon/sanitizer.h>
  21. #error #include <zircon/syscalls.h>
  22. #error #include <zircon/threads.h>
  23. namespace __asan {
  24. // The system already set up the shadow memory for us.
  25. // __sanitizer::GetMaxUserVirtualAddress has already been called by
  26. // AsanInitInternal->InitializeHighMemEnd (asan_rtl.cpp).
  27. // Just do some additional sanity checks here.
  28. void InitializeShadowMemory() {
  29. if (Verbosity())
  30. PrintAddressSpaceLayout();
  31. // Make sure SHADOW_OFFSET doesn't use __asan_shadow_memory_dynamic_address.
  32. __asan_shadow_memory_dynamic_address = kDefaultShadowSentinel;
  33. DCHECK(kLowShadowBeg != kDefaultShadowSentinel);
  34. __asan_shadow_memory_dynamic_address = kLowShadowBeg;
  35. CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);
  36. CHECK_EQ(kHighMemEnd, __sanitizer::ShadowBounds.memory_limit - 1);
  37. CHECK_EQ(kHighMemBeg, __sanitizer::ShadowBounds.shadow_limit);
  38. CHECK_EQ(kHighShadowBeg, __sanitizer::ShadowBounds.shadow_base);
  39. CHECK_EQ(kShadowGapEnd, __sanitizer::ShadowBounds.shadow_base - 1);
  40. CHECK_EQ(kLowShadowEnd, 0);
  41. CHECK_EQ(kLowShadowBeg, 0);
  42. }
  43. void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
  44. UNIMPLEMENTED();
  45. }
  46. void AsanCheckDynamicRTPrereqs() {}
  47. void AsanCheckIncompatibleRT() {}
  48. void InitializeAsanInterceptors() {}
  49. void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
  50. void InitializePlatformExceptionHandlers() {}
  51. void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
  52. UNIMPLEMENTED();
  53. }
  54. bool PlatformUnpoisonStacks() {
  55. // The current sp might not point to the default stack. This
  56. // could be because we are in a crash stack from fuzzing for example.
  57. // Unpoison the default stack and the current stack page.
  58. AsanThread *curr_thread = GetCurrentThread();
  59. CHECK(curr_thread != nullptr);
  60. uptr top = curr_thread->stack_top();
  61. uptr bottom = curr_thread->stack_bottom();
  62. // The default stack grows from top to bottom. (bottom < top).
  63. uptr local_stack = reinterpret_cast<uptr>(__builtin_frame_address(0));
  64. if (local_stack >= bottom && local_stack <= top) {
  65. // The current stack is the default stack.
  66. // We only need to unpoison from where we are using until the end.
  67. bottom = RoundDownTo(local_stack, GetPageSize());
  68. UnpoisonStack(bottom, top, "default");
  69. } else {
  70. // The current stack is not the default stack.
  71. // Unpoison the entire default stack and the current stack page.
  72. UnpoisonStack(bottom, top, "default");
  73. bottom = RoundDownTo(local_stack, GetPageSize());
  74. top = bottom + GetPageSize();
  75. UnpoisonStack(bottom, top, "unknown");
  76. return true;
  77. }
  78. return false;
  79. }
  80. // We can use a plain thread_local variable for TSD.
  81. static thread_local void *per_thread;
  82. void *AsanTSDGet() { return per_thread; }
  83. void AsanTSDSet(void *tsd) { per_thread = tsd; }
  84. // There's no initialization needed, and the passed-in destructor
  85. // will never be called. Instead, our own thread destruction hook
  86. // (below) will call AsanThread::TSDDtor directly.
  87. void AsanTSDInit(void (*destructor)(void *tsd)) {
  88. DCHECK(destructor == &PlatformTSDDtor);
  89. }
  90. void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }
  91. static inline size_t AsanThreadMmapSize() {
  92. return RoundUpTo(sizeof(AsanThread), _zx_system_get_page_size());
  93. }
  94. struct AsanThread::InitOptions {
  95. uptr stack_bottom, stack_size;
  96. };
  97. // Shared setup between thread creation and startup for the initial thread.
  98. static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,
  99. bool detached, const char *name) {
  100. // In lieu of AsanThread::Create.
  101. AsanThread *thread = (AsanThread *)MmapOrDie(AsanThreadMmapSize(), __func__);
  102. AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
  103. u32 tid = asanThreadRegistry().CreateThread(0, detached, parent_tid, &args);
  104. asanThreadRegistry().SetThreadName(tid, name);
  105. return thread;
  106. }
  107. // This gets the same arguments passed to Init by CreateAsanThread, above.
  108. // We're in the creator thread before the new thread is actually started,
  109. // but its stack address range is already known. We don't bother tracking
  110. // the static TLS address range because the system itself already uses an
  111. // ASan-aware allocator for that.
  112. void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {
  113. DCHECK_NE(GetCurrentThread(), this);
  114. DCHECK_NE(GetCurrentThread(), nullptr);
  115. CHECK_NE(options->stack_bottom, 0);
  116. CHECK_NE(options->stack_size, 0);
  117. stack_bottom_ = options->stack_bottom;
  118. stack_top_ = options->stack_bottom + options->stack_size;
  119. }
  120. // Called by __asan::AsanInitInternal (asan_rtl.c).
  121. AsanThread *CreateMainThread() {
  122. thrd_t self = thrd_current();
  123. char name[ZX_MAX_NAME_LEN];
  124. CHECK_NE(__sanitizer::MainThreadStackBase, 0);
  125. CHECK_GT(__sanitizer::MainThreadStackSize, 0);
  126. AsanThread *t = CreateAsanThread(
  127. nullptr, 0, true,
  128. _zx_object_get_property(thrd_get_zx_handle(self), ZX_PROP_NAME, name,
  129. sizeof(name)) == ZX_OK
  130. ? name
  131. : nullptr);
  132. // We need to set the current thread before calling AsanThread::Init() below,
  133. // since it reads the thread ID.
  134. SetCurrentThread(t);
  135. DCHECK_EQ(t->tid(), 0);
  136. const AsanThread::InitOptions options = {__sanitizer::MainThreadStackBase,
  137. __sanitizer::MainThreadStackSize};
  138. t->Init(&options);
  139. return t;
  140. }
  141. // This is called before each thread creation is attempted. So, in
  142. // its first call, the calling thread is the initial and sole thread.
  143. static void *BeforeThreadCreateHook(uptr user_id, bool detached,
  144. const char *name, uptr stack_bottom,
  145. uptr stack_size) {
  146. EnsureMainThreadIDIsCorrect();
  147. // Strict init-order checking is thread-hostile.
  148. if (flags()->strict_init_order)
  149. StopInitOrderChecking();
  150. GET_STACK_TRACE_THREAD;
  151. u32 parent_tid = GetCurrentTidOrInvalid();
  152. AsanThread *thread = CreateAsanThread(&stack, parent_tid, detached, name);
  153. // On other systems, AsanThread::Init() is called from the new
  154. // thread itself. But on Fuchsia we already know the stack address
  155. // range beforehand, so we can do most of the setup right now.
  156. const AsanThread::InitOptions options = {stack_bottom, stack_size};
  157. thread->Init(&options);
  158. return thread;
  159. }
  160. // This is called after creating a new thread (in the creating thread),
  161. // with the pointer returned by BeforeThreadCreateHook (above).
  162. static void ThreadCreateHook(void *hook, bool aborted) {
  163. AsanThread *thread = static_cast<AsanThread *>(hook);
  164. if (!aborted) {
  165. // The thread was created successfully.
  166. // ThreadStartHook is already running in the new thread.
  167. } else {
  168. // The thread wasn't created after all.
  169. // Clean up everything we set up in BeforeThreadCreateHook.
  170. asanThreadRegistry().FinishThread(thread->tid());
  171. UnmapOrDie(thread, AsanThreadMmapSize());
  172. }
  173. }
  174. // This is called in the newly-created thread before it runs anything else,
  175. // with the pointer returned by BeforeThreadCreateHook (above).
  176. // cf. asan_interceptors.cpp:asan_thread_start
  177. static void ThreadStartHook(void *hook, uptr os_id) {
  178. AsanThread *thread = static_cast<AsanThread *>(hook);
  179. SetCurrentThread(thread);
  180. // In lieu of AsanThread::ThreadStart.
  181. asanThreadRegistry().StartThread(thread->tid(), os_id, ThreadType::Regular,
  182. nullptr);
  183. }
  184. // Each thread runs this just before it exits,
  185. // with the pointer returned by BeforeThreadCreateHook (above).
  186. // All per-thread destructors have already been called.
  187. static void ThreadExitHook(void *hook, uptr os_id) {
  188. AsanThread::TSDDtor(per_thread);
  189. }
  190. bool HandleDlopenInit() {
  191. // Not supported on this platform.
  192. static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
  193. "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
  194. return false;
  195. }
  196. void FlushUnneededASanShadowMemory(uptr p, uptr size) {
  197. __sanitizer_fill_shadow(p, size, 0, 0);
  198. }
  199. } // namespace __asan
  200. // These are declared (in extern "C") by <zircon/sanitizer.h>.
  201. // The system runtime will call our definitions directly.
  202. void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
  203. const char *name, void *stack_base,
  204. size_t stack_size) {
  205. return __asan::BeforeThreadCreateHook(
  206. reinterpret_cast<uptr>(thread), detached, name,
  207. reinterpret_cast<uptr>(stack_base), stack_size);
  208. }
  209. void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {
  210. __asan::ThreadCreateHook(hook, error != thrd_success);
  211. }
  212. void __sanitizer_thread_start_hook(void *hook, thrd_t self) {
  213. __asan::ThreadStartHook(hook, reinterpret_cast<uptr>(self));
  214. }
  215. void __sanitizer_thread_exit_hook(void *hook, thrd_t self) {
  216. __asan::ThreadExitHook(hook, reinterpret_cast<uptr>(self));
  217. }
  218. #endif // SANITIZER_FUCHSIA