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