hwasan_fuchsia.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //===-- hwasan_fuchsia.cpp --------------------------------------*- C++ -*-===//
  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. /// \file
  10. /// This file is a part of HWAddressSanitizer and contains Fuchsia-specific
  11. /// code.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "sanitizer_common/sanitizer_fuchsia.h"
  15. #if SANITIZER_FUCHSIA
  16. #error #include <zircon/features.h>
  17. #error #include <zircon/syscalls.h>
  18. #include "hwasan.h"
  19. #include "hwasan_interface_internal.h"
  20. #include "hwasan_report.h"
  21. #include "hwasan_thread.h"
  22. #include "hwasan_thread_list.h"
  23. // This TLS variable contains the location of the stack ring buffer and can be
  24. // used to always find the hwasan thread object associated with the current
  25. // running thread.
  26. [[gnu::tls_model("initial-exec")]]
  27. SANITIZER_INTERFACE_ATTRIBUTE
  28. THREADLOCAL uptr __hwasan_tls;
  29. namespace __hwasan {
  30. bool InitShadow() {
  31. __sanitizer::InitShadowBounds();
  32. CHECK_NE(__sanitizer::ShadowBounds.shadow_limit, 0);
  33. // These variables are used by MemIsShadow for asserting we have a correct
  34. // shadow address. On Fuchsia, we only have one region of shadow, so the
  35. // bounds of Low shadow can be zero while High shadow represents the true
  36. // bounds. Note that these are inclusive ranges.
  37. kLowShadowStart = 0;
  38. kLowShadowEnd = 0;
  39. kHighShadowStart = __sanitizer::ShadowBounds.shadow_base;
  40. kHighShadowEnd = __sanitizer::ShadowBounds.shadow_limit - 1;
  41. return true;
  42. }
  43. bool MemIsApp(uptr p) {
  44. CHECK(GetTagFromPointer(p) == 0);
  45. return __sanitizer::ShadowBounds.shadow_limit <= p &&
  46. p <= (__sanitizer::ShadowBounds.memory_limit - 1);
  47. }
  48. // These are known parameters passed to the hwasan runtime on thread creation.
  49. struct Thread::InitState {
  50. uptr stack_bottom, stack_top;
  51. };
  52. static void FinishThreadInitialization(Thread *thread);
  53. void InitThreads() {
  54. // This is the minimal alignment needed for the storage where hwasan threads
  55. // and their stack ring buffers are placed. This alignment is necessary so the
  56. // stack ring buffer can perform a simple calculation to get the next element
  57. // in the RB. The instructions for this calculation are emitted by the
  58. // compiler. (Full explanation in hwasan_thread_list.h.)
  59. uptr alloc_size = UINT64_C(1) << kShadowBaseAlignment;
  60. uptr thread_start = reinterpret_cast<uptr>(
  61. MmapAlignedOrDieOnFatalError(alloc_size, alloc_size, __func__));
  62. InitThreadList(thread_start, alloc_size);
  63. // Create the hwasan thread object for the current (main) thread. Stack info
  64. // for this thread is known from information passed via
  65. // __sanitizer_startup_hook.
  66. const Thread::InitState state = {
  67. .stack_bottom = __sanitizer::MainThreadStackBase,
  68. .stack_top =
  69. __sanitizer::MainThreadStackBase + __sanitizer::MainThreadStackSize,
  70. };
  71. FinishThreadInitialization(hwasanThreadList().CreateCurrentThread(&state));
  72. }
  73. uptr *GetCurrentThreadLongPtr() { return &__hwasan_tls; }
  74. // This is called from the parent thread before the new thread is created. Here
  75. // we can propagate known info like the stack bounds to Thread::Init before
  76. // jumping into the thread. We cannot initialize the stack ring buffer yet since
  77. // we have not entered the new thread.
  78. static void *BeforeThreadCreateHook(uptr user_id, bool detached,
  79. const char *name, uptr stack_bottom,
  80. uptr stack_size) {
  81. const Thread::InitState state = {
  82. .stack_bottom = stack_bottom,
  83. .stack_top = stack_bottom + stack_size,
  84. };
  85. return hwasanThreadList().CreateCurrentThread(&state);
  86. }
  87. // This sets the stack top and bottom according to the InitState passed to
  88. // CreateCurrentThread above.
  89. void Thread::InitStackAndTls(const InitState *state) {
  90. CHECK_NE(state->stack_bottom, 0);
  91. CHECK_NE(state->stack_top, 0);
  92. stack_bottom_ = state->stack_bottom;
  93. stack_top_ = state->stack_top;
  94. tls_end_ = tls_begin_ = 0;
  95. }
  96. // This is called after creating a new thread with the pointer returned by
  97. // BeforeThreadCreateHook. We are still in the creating thread and should check
  98. // if it was actually created correctly.
  99. static void ThreadCreateHook(void *hook, bool aborted) {
  100. Thread *thread = static_cast<Thread *>(hook);
  101. if (!aborted) {
  102. // The thread was created successfully.
  103. // ThreadStartHook can already be running in the new thread.
  104. } else {
  105. // The thread wasn't created after all.
  106. // Clean up everything we set up in BeforeThreadCreateHook.
  107. atomic_signal_fence(memory_order_seq_cst);
  108. hwasanThreadList().ReleaseThread(thread);
  109. }
  110. }
  111. // This is called in the newly-created thread before it runs anything else,
  112. // with the pointer returned by BeforeThreadCreateHook (above). Here we can
  113. // setup the stack ring buffer.
  114. static void ThreadStartHook(void *hook, thrd_t self) {
  115. Thread *thread = static_cast<Thread *>(hook);
  116. FinishThreadInitialization(thread);
  117. thread->EnsureRandomStateInited();
  118. }
  119. // This is the function that sets up the stack ring buffer and enables us to use
  120. // GetCurrentThread. This function should only be called while IN the thread
  121. // that we want to create the hwasan thread object for so __hwasan_tls can be
  122. // properly referenced.
  123. static void FinishThreadInitialization(Thread *thread) {
  124. CHECK_NE(thread, nullptr);
  125. // The ring buffer is located immediately before the thread object.
  126. uptr stack_buffer_size = hwasanThreadList().GetRingBufferSize();
  127. uptr stack_buffer_start = reinterpret_cast<uptr>(thread) - stack_buffer_size;
  128. thread->InitStackRingBuffer(stack_buffer_start, stack_buffer_size);
  129. }
  130. static void ThreadExitHook(void *hook, thrd_t self) {
  131. Thread *thread = static_cast<Thread *>(hook);
  132. atomic_signal_fence(memory_order_seq_cst);
  133. hwasanThreadList().ReleaseThread(thread);
  134. }
  135. uptr TagMemoryAligned(uptr p, uptr size, tag_t tag) {
  136. CHECK(IsAligned(p, kShadowAlignment));
  137. CHECK(IsAligned(size, kShadowAlignment));
  138. __sanitizer_fill_shadow(p, size, tag,
  139. common_flags()->clear_shadow_mmap_threshold);
  140. return AddTagToPointer(p, tag);
  141. }
  142. // Not implemented because Fuchsia does not use signal handlers.
  143. void HwasanOnDeadlySignal(int signo, void *info, void *context) {}
  144. // Not implemented because Fuchsia does not use interceptors.
  145. void InitializeInterceptors() {}
  146. // Not implemented because this is only relevant for Android.
  147. void AndroidTestTlsSlot() {}
  148. // TSD was normally used on linux as a means of calling the hwasan thread exit
  149. // handler passed to pthread_key_create. This is not needed on Fuchsia because
  150. // we will be using __sanitizer_thread_exit_hook.
  151. void HwasanTSDInit() {}
  152. void HwasanTSDThreadInit() {}
  153. // On linux, this just would call `atexit(HwasanAtExit)`. The functions in
  154. // HwasanAtExit are unimplemented for Fuchsia and effectively no-ops, so this
  155. // function is unneeded.
  156. void InstallAtExitHandler() {}
  157. void HwasanInstallAtForkHandler() {}
  158. void InstallAtExitCheckLeaks() {}
  159. void InitializeOsSupport() {
  160. #ifdef __aarch64__
  161. uint32_t features = 0;
  162. CHECK_EQ(zx_system_get_features(ZX_FEATURE_KIND_ADDRESS_TAGGING, &features),
  163. ZX_OK);
  164. if (!(features & ZX_ARM64_FEATURE_ADDRESS_TAGGING_TBI) &&
  165. flags()->fail_without_syscall_abi) {
  166. Printf(
  167. "FATAL: HWAddressSanitizer requires "
  168. "ZX_ARM64_FEATURE_ADDRESS_TAGGING_TBI.\n");
  169. Die();
  170. }
  171. #endif
  172. }
  173. } // namespace __hwasan
  174. namespace __lsan {
  175. bool UseExitcodeOnLeak() { return __hwasan::flags()->halt_on_error; }
  176. } // namespace __lsan
  177. extern "C" {
  178. void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
  179. const char *name, void *stack_base,
  180. size_t stack_size) {
  181. return __hwasan::BeforeThreadCreateHook(
  182. reinterpret_cast<uptr>(thread), detached, name,
  183. reinterpret_cast<uptr>(stack_base), stack_size);
  184. }
  185. void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {
  186. __hwasan::ThreadCreateHook(hook, error != thrd_success);
  187. }
  188. void __sanitizer_thread_start_hook(void *hook, thrd_t self) {
  189. __hwasan::ThreadStartHook(hook, reinterpret_cast<uptr>(self));
  190. }
  191. void __sanitizer_thread_exit_hook(void *hook, thrd_t self) {
  192. __hwasan::ThreadExitHook(hook, self);
  193. }
  194. void __sanitizer_module_loaded(const struct dl_phdr_info *info, size_t) {
  195. __hwasan_library_loaded(info->dlpi_addr, info->dlpi_phdr, info->dlpi_phnum);
  196. }
  197. } // extern "C"
  198. #endif // SANITIZER_FUCHSIA