1 //===-- asan_fuchsia.cc --------------------------------------------------===//
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
7 //===---------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // Fuchsia-specific details.
12 //===---------------------------------------------------------------------===//
14 #include "sanitizer_common/sanitizer_fuchsia.h"
17 #include "asan_interceptors.h"
18 #include "asan_internal.h"
19 #include "asan_stack.h"
20 #include "asan_thread.h"
23 #include <zircon/sanitizer.h>
24 #include <zircon/syscalls.h>
25 #include <zircon/threads.h>
29 // The system already set up the shadow memory for us.
30 // __sanitizer::GetMaxUserVirtualAddress has already been called by
31 // AsanInitInternal->InitializeHighMemEnd (asan_rtl.cc).
32 // Just do some additional sanity checks here.
33 void InitializeShadowMemory() {
34 if (Verbosity()) PrintAddressSpaceLayout();
36 // Make sure SHADOW_OFFSET doesn't use __asan_shadow_memory_dynamic_address.
37 __asan_shadow_memory_dynamic_address = kDefaultShadowSentinel;
38 DCHECK(kLowShadowBeg != kDefaultShadowSentinel);
39 __asan_shadow_memory_dynamic_address = kLowShadowBeg;
41 CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);
42 CHECK_EQ(kHighMemEnd, __sanitizer::ShadowBounds.memory_limit - 1);
43 CHECK_EQ(kHighMemBeg, __sanitizer::ShadowBounds.shadow_limit);
44 CHECK_EQ(kHighShadowBeg, __sanitizer::ShadowBounds.shadow_base);
45 CHECK_EQ(kShadowGapEnd, __sanitizer::ShadowBounds.shadow_base - 1);
46 CHECK_EQ(kLowShadowEnd, 0);
47 CHECK_EQ(kLowShadowBeg, 0);
50 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
54 void AsanCheckDynamicRTPrereqs() {}
55 void AsanCheckIncompatibleRT() {}
56 void InitializeAsanInterceptors() {}
58 void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
60 void InitializePlatformExceptionHandlers() {}
61 void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
65 // We can use a plain thread_local variable for TSD.
66 static thread_local void *per_thread;
68 void *AsanTSDGet() { return per_thread; }
70 void AsanTSDSet(void *tsd) { per_thread = tsd; }
72 // There's no initialization needed, and the passed-in destructor
73 // will never be called. Instead, our own thread destruction hook
74 // (below) will call AsanThread::TSDDtor directly.
75 void AsanTSDInit(void (*destructor)(void *tsd)) {
76 DCHECK(destructor == &PlatformTSDDtor);
79 void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }
81 static inline size_t AsanThreadMmapSize() {
82 return RoundUpTo(sizeof(AsanThread), PAGE_SIZE);
85 struct AsanThread::InitOptions {
86 uptr stack_bottom, stack_size;
89 // Shared setup between thread creation and startup for the initial thread.
90 static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,
91 uptr user_id, bool detached,
92 const char *name, uptr stack_bottom,
94 // In lieu of AsanThread::Create.
95 AsanThread *thread = (AsanThread *)MmapOrDie(AsanThreadMmapSize(), __func__);
97 AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
99 asanThreadRegistry().CreateThread(user_id, detached, parent_tid, &args);
100 asanThreadRegistry().SetThreadName(tid, name);
102 // On other systems, AsanThread::Init() is called from the new
103 // thread itself. But on Fuchsia we already know the stack address
104 // range beforehand, so we can do most of the setup right now.
105 const AsanThread::InitOptions options = {stack_bottom, stack_size};
106 thread->Init(&options);
111 // This gets the same arguments passed to Init by CreateAsanThread, above.
112 // We're in the creator thread before the new thread is actually started,
113 // but its stack address range is already known. We don't bother tracking
114 // the static TLS address range because the system itself already uses an
115 // ASan-aware allocator for that.
116 void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {
117 DCHECK_NE(GetCurrentThread(), this);
118 DCHECK_NE(GetCurrentThread(), nullptr);
119 CHECK_NE(options->stack_bottom, 0);
120 CHECK_NE(options->stack_size, 0);
121 stack_bottom_ = options->stack_bottom;
122 stack_top_ = options->stack_bottom + options->stack_size;
125 // Called by __asan::AsanInitInternal (asan_rtl.c).
126 AsanThread *CreateMainThread() {
127 thrd_t self = thrd_current();
128 char name[ZX_MAX_NAME_LEN];
129 CHECK_NE(__sanitizer::MainThreadStackBase, 0);
130 CHECK_GT(__sanitizer::MainThreadStackSize, 0);
131 AsanThread *t = CreateAsanThread(
132 nullptr, 0, reinterpret_cast<uptr>(self), true,
133 _zx_object_get_property(thrd_get_zx_handle(self), ZX_PROP_NAME, name,
134 sizeof(name)) == ZX_OK
137 __sanitizer::MainThreadStackBase, __sanitizer::MainThreadStackSize);
142 // This is called before each thread creation is attempted. So, in
143 // its first call, the calling thread is the initial and sole thread.
144 static void *BeforeThreadCreateHook(uptr user_id, bool detached,
145 const char *name, uptr stack_bottom,
147 EnsureMainThreadIDIsCorrect();
148 // Strict init-order checking is thread-hostile.
149 if (flags()->strict_init_order) StopInitOrderChecking();
151 GET_STACK_TRACE_THREAD;
152 u32 parent_tid = GetCurrentTidOrInvalid();
154 return CreateAsanThread(&stack, parent_tid, user_id, detached, name,
155 stack_bottom, stack_size);
158 // This is called after creating a new thread (in the creating thread),
159 // with the pointer returned by BeforeThreadCreateHook (above).
160 static void ThreadCreateHook(void *hook, bool aborted) {
161 AsanThread *thread = static_cast<AsanThread *>(hook);
163 // The thread was created successfully.
164 // ThreadStartHook is already running in the new thread.
166 // The thread wasn't created after all.
167 // Clean up everything we set up in BeforeThreadCreateHook.
168 asanThreadRegistry().FinishThread(thread->tid());
169 UnmapOrDie(thread, AsanThreadMmapSize());
173 // This is called in the newly-created thread before it runs anything else,
174 // with the pointer returned by BeforeThreadCreateHook (above).
175 // cf. asan_interceptors.cc:asan_thread_start
176 static void ThreadStartHook(void *hook, uptr os_id) {
177 AsanThread *thread = static_cast<AsanThread *>(hook);
178 SetCurrentThread(thread);
180 // In lieu of AsanThread::ThreadStart.
181 asanThreadRegistry().StartThread(thread->tid(), os_id, ThreadType::Regular,
185 // Each thread runs this just before it exits,
186 // with the pointer returned by BeforeThreadCreateHook (above).
187 // All per-thread destructors have already been called.
188 static void ThreadExitHook(void *hook, uptr os_id) {
189 AsanThread::TSDDtor(per_thread);
192 bool HandleDlopenInit() {
193 // Not supported on this platform.
194 static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
195 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
199 } // namespace __asan
201 // These are declared (in extern "C") by <zircon/sanitizer.h>.
202 // The system runtime will call our definitions directly.
204 void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
205 const char *name, void *stack_base,
207 return __asan::BeforeThreadCreateHook(
208 reinterpret_cast<uptr>(thread), detached, name,
209 reinterpret_cast<uptr>(stack_base), stack_size);
212 void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {
213 __asan::ThreadCreateHook(hook, error != thrd_success);
216 void __sanitizer_thread_start_hook(void *hook, thrd_t self) {
217 __asan::ThreadStartHook(hook, reinterpret_cast<uptr>(self));
220 void __sanitizer_thread_exit_hook(void *hook, thrd_t self) {
221 __asan::ThreadExitHook(hook, reinterpret_cast<uptr>(self));
224 #endif // SANITIZER_FUCHSIA