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