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