xref: /freebsd/contrib/llvm-project/compiler-rt/lib/asan/asan_fuchsia.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 
14 #include "sanitizer_common/sanitizer_fuchsia.h"
15 #if SANITIZER_FUCHSIA
16 
17 #include "asan_interceptors.h"
18 #include "asan_internal.h"
19 #include "asan_stack.h"
20 #include "asan_thread.h"
21 
22 #include <limits.h>
23 #include <zircon/sanitizer.h>
24 #include <zircon/syscalls.h>
25 #include <zircon/threads.h>
26 
27 namespace __asan {
28 
29 // The system already set up the shadow memory for us.
30 // __sanitizer::GetMaxUserVirtualAddress has already been called by
31 // AsanInitInternal->InitializeHighMemEnd (asan_rtl.cpp).
32 // Just do some additional sanity checks here.
33 void InitializeShadowMemory() {
34   if (Verbosity()) PrintAddressSpaceLayout();
35 
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;
40 
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);
48 }
49 
50 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
51   UNIMPLEMENTED();
52 }
53 
54 void AsanCheckDynamicRTPrereqs() {}
55 void AsanCheckIncompatibleRT() {}
56 void InitializeAsanInterceptors() {}
57 
58 void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
59 
60 void InitializePlatformExceptionHandlers() {}
61 void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
62   UNIMPLEMENTED();
63 }
64 
65 bool PlatformUnpoisonStacks() { return false; }
66 
67 // We can use a plain thread_local variable for TSD.
68 static thread_local void *per_thread;
69 
70 void *AsanTSDGet() { return per_thread; }
71 
72 void AsanTSDSet(void *tsd) { per_thread = tsd; }
73 
74 // There's no initialization needed, and the passed-in destructor
75 // will never be called.  Instead, our own thread destruction hook
76 // (below) will call AsanThread::TSDDtor directly.
77 void AsanTSDInit(void (*destructor)(void *tsd)) {
78   DCHECK(destructor == &PlatformTSDDtor);
79 }
80 
81 void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }
82 
83 static inline size_t AsanThreadMmapSize() {
84   return RoundUpTo(sizeof(AsanThread), PAGE_SIZE);
85 }
86 
87 struct AsanThread::InitOptions {
88   uptr stack_bottom, stack_size;
89 };
90 
91 // Shared setup between thread creation and startup for the initial thread.
92 static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,
93                                     uptr user_id, bool detached,
94                                     const char *name, uptr stack_bottom,
95                                     uptr stack_size) {
96   // In lieu of AsanThread::Create.
97   AsanThread *thread = (AsanThread *)MmapOrDie(AsanThreadMmapSize(), __func__);
98 
99   AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
100   u32 tid =
101       asanThreadRegistry().CreateThread(user_id, detached, parent_tid, &args);
102   asanThreadRegistry().SetThreadName(tid, name);
103 
104   // On other systems, AsanThread::Init() is called from the new
105   // thread itself.  But on Fuchsia we already know the stack address
106   // range beforehand, so we can do most of the setup right now.
107   const AsanThread::InitOptions options = {stack_bottom, stack_size};
108   thread->Init(&options);
109 
110   return thread;
111 }
112 
113 // This gets the same arguments passed to Init by CreateAsanThread, above.
114 // We're in the creator thread before the new thread is actually started,
115 // but its stack address range is already known.  We don't bother tracking
116 // the static TLS address range because the system itself already uses an
117 // ASan-aware allocator for that.
118 void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {
119   DCHECK_NE(GetCurrentThread(), this);
120   DCHECK_NE(GetCurrentThread(), nullptr);
121   CHECK_NE(options->stack_bottom, 0);
122   CHECK_NE(options->stack_size, 0);
123   stack_bottom_ = options->stack_bottom;
124   stack_top_ = options->stack_bottom + options->stack_size;
125 }
126 
127 // Called by __asan::AsanInitInternal (asan_rtl.c).
128 AsanThread *CreateMainThread() {
129   thrd_t self = thrd_current();
130   char name[ZX_MAX_NAME_LEN];
131   CHECK_NE(__sanitizer::MainThreadStackBase, 0);
132   CHECK_GT(__sanitizer::MainThreadStackSize, 0);
133   AsanThread *t = CreateAsanThread(
134       nullptr, 0, reinterpret_cast<uptr>(self), true,
135       _zx_object_get_property(thrd_get_zx_handle(self), ZX_PROP_NAME, name,
136                               sizeof(name)) == ZX_OK
137           ? name
138           : nullptr,
139       __sanitizer::MainThreadStackBase, __sanitizer::MainThreadStackSize);
140   SetCurrentThread(t);
141   return t;
142 }
143 
144 // This is called before each thread creation is attempted.  So, in
145 // its first call, the calling thread is the initial and sole thread.
146 static void *BeforeThreadCreateHook(uptr user_id, bool detached,
147                                     const char *name, uptr stack_bottom,
148                                     uptr stack_size) {
149   EnsureMainThreadIDIsCorrect();
150   // Strict init-order checking is thread-hostile.
151   if (flags()->strict_init_order) StopInitOrderChecking();
152 
153   GET_STACK_TRACE_THREAD;
154   u32 parent_tid = GetCurrentTidOrInvalid();
155 
156   return CreateAsanThread(&stack, parent_tid, user_id, detached, name,
157                           stack_bottom, stack_size);
158 }
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 
175 // This is called in the newly-created thread before it runs anything else,
176 // with the pointer returned by BeforeThreadCreateHook (above).
177 // cf. asan_interceptors.cpp:asan_thread_start
178 static void ThreadStartHook(void *hook, uptr os_id) {
179   AsanThread *thread = static_cast<AsanThread *>(hook);
180   SetCurrentThread(thread);
181 
182   // In lieu of AsanThread::ThreadStart.
183   asanThreadRegistry().StartThread(thread->tid(), os_id, ThreadType::Regular,
184                                    nullptr);
185 }
186 
187 // Each thread runs this just before it exits,
188 // with the pointer returned by BeforeThreadCreateHook (above).
189 // All per-thread destructors have already been called.
190 static void ThreadExitHook(void *hook, uptr os_id) {
191   AsanThread::TSDDtor(per_thread);
192 }
193 
194 bool HandleDlopenInit() {
195   // Not supported on this platform.
196   static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
197                 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
198   return false;
199 }
200 
201 }  // namespace __asan
202 
203 // These are declared (in extern "C") by <zircon/sanitizer.h>.
204 // The system runtime will call our definitions directly.
205 
206 void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,
207                                             const char *name, void *stack_base,
208                                             size_t stack_size) {
209   return __asan::BeforeThreadCreateHook(
210       reinterpret_cast<uptr>(thread), detached, name,
211       reinterpret_cast<uptr>(stack_base), stack_size);
212 }
213 
214 void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {
215   __asan::ThreadCreateHook(hook, error != thrd_success);
216 }
217 
218 void __sanitizer_thread_start_hook(void *hook, thrd_t self) {
219   __asan::ThreadStartHook(hook, reinterpret_cast<uptr>(self));
220 }
221 
222 void __sanitizer_thread_exit_hook(void *hook, thrd_t self) {
223   __asan::ThreadExitHook(hook, reinterpret_cast<uptr>(self));
224 }
225 
226 #endif  // SANITIZER_FUCHSIA
227