Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_fuchsia.cpp
35233 views
//===-- asan_fuchsia.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===---------------------------------------------------------------------===//7//8// This file is a part of AddressSanitizer, an address sanity checker.9//10// Fuchsia-specific details.11//===---------------------------------------------------------------------===//1213#include "sanitizer_common/sanitizer_fuchsia.h"14#if SANITIZER_FUCHSIA1516#include <limits.h>17#include <zircon/sanitizer.h>18#include <zircon/syscalls.h>19#include <zircon/threads.h>2021# include "asan_interceptors.h"22# include "asan_internal.h"23# include "asan_stack.h"24# include "asan_thread.h"25# include "lsan/lsan_common.h"2627namespace __asan {2829// The system already set up the shadow memory for us.30// __sanitizer::GetMaxUserVirtualAddress has already been called by31// AsanInitInternal->InitializeHighMemEnd (asan_rtl.cpp).32// Just do some additional sanity checks here.33void InitializeShadowMemory() {34if (Verbosity())35PrintAddressSpaceLayout();3637// Make sure SHADOW_OFFSET doesn't use __asan_shadow_memory_dynamic_address.38__asan_shadow_memory_dynamic_address = kDefaultShadowSentinel;39DCHECK(kLowShadowBeg != kDefaultShadowSentinel);40__asan_shadow_memory_dynamic_address = kLowShadowBeg;4142CHECK_EQ(kShadowGapEnd, kHighShadowBeg - 1);43CHECK_EQ(kHighMemEnd, __sanitizer::ShadowBounds.memory_limit - 1);44CHECK_EQ(kHighMemBeg, __sanitizer::ShadowBounds.shadow_limit);45CHECK_EQ(kHighShadowBeg, __sanitizer::ShadowBounds.shadow_base);46CHECK_EQ(kShadowGapEnd, __sanitizer::ShadowBounds.shadow_base - 1);47CHECK_EQ(kLowShadowEnd, 0);48CHECK_EQ(kLowShadowBeg, 0);49}5051void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {52UNIMPLEMENTED();53}5455void AsanCheckDynamicRTPrereqs() {}56void AsanCheckIncompatibleRT() {}57void InitializeAsanInterceptors() {}5859void InitializePlatformExceptionHandlers() {}60void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {61UNIMPLEMENTED();62}6364bool PlatformUnpoisonStacks() {65// The current sp might not point to the default stack. This66// could be because we are in a crash stack from fuzzing for example.67// Unpoison the default stack and the current stack page.68AsanThread *curr_thread = GetCurrentThread();69CHECK(curr_thread != nullptr);70uptr top = curr_thread->stack_top();71uptr bottom = curr_thread->stack_bottom();72// The default stack grows from top to bottom. (bottom < top).7374uptr local_stack = reinterpret_cast<uptr>(__builtin_frame_address(0));75if (local_stack >= bottom && local_stack <= top) {76// The current stack is the default stack.77// We only need to unpoison from where we are using until the end.78bottom = RoundDownTo(local_stack, GetPageSize());79UnpoisonStack(bottom, top, "default");80} else {81// The current stack is not the default stack.82// Unpoison the entire default stack and the current stack page.83UnpoisonStack(bottom, top, "default");84bottom = RoundDownTo(local_stack, GetPageSize());85top = bottom + GetPageSize();86UnpoisonStack(bottom, top, "unknown");87return true;88}8990return false;91}9293// We can use a plain thread_local variable for TSD.94static thread_local void *per_thread;9596void *AsanTSDGet() { return per_thread; }9798void AsanTSDSet(void *tsd) { per_thread = tsd; }99100// There's no initialization needed, and the passed-in destructor101// will never be called. Instead, our own thread destruction hook102// (below) will call AsanThread::TSDDtor directly.103void AsanTSDInit(void (*destructor)(void *tsd)) {104DCHECK(destructor == &PlatformTSDDtor);105}106107void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }108109static inline size_t AsanThreadMmapSize() {110return RoundUpTo(sizeof(AsanThread), _zx_system_get_page_size());111}112113struct AsanThread::InitOptions {114uptr stack_bottom, stack_size;115};116117// Shared setup between thread creation and startup for the initial thread.118static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,119bool detached, const char *name) {120// In lieu of AsanThread::Create.121AsanThread *thread = (AsanThread *)MmapOrDie(AsanThreadMmapSize(), __func__);122123AsanThreadContext::CreateThreadContextArgs args = {thread, stack};124u32 tid = asanThreadRegistry().CreateThread(0, detached, parent_tid, &args);125asanThreadRegistry().SetThreadName(tid, name);126127return thread;128}129130// This gets the same arguments passed to Init by CreateAsanThread, above.131// We're in the creator thread before the new thread is actually started,132// but its stack address range is already known. We don't bother tracking133// the static TLS address range because the system itself already uses an134// ASan-aware allocator for that.135void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {136DCHECK_NE(GetCurrentThread(), this);137DCHECK_NE(GetCurrentThread(), nullptr);138CHECK_NE(options->stack_bottom, 0);139CHECK_NE(options->stack_size, 0);140stack_bottom_ = options->stack_bottom;141stack_top_ = options->stack_bottom + options->stack_size;142}143144// Called by __asan::AsanInitInternal (asan_rtl.c).145AsanThread *CreateMainThread() {146thrd_t self = thrd_current();147char name[ZX_MAX_NAME_LEN];148CHECK_NE(__sanitizer::MainThreadStackBase, 0);149CHECK_GT(__sanitizer::MainThreadStackSize, 0);150AsanThread *t = CreateAsanThread(151nullptr, 0, true,152_zx_object_get_property(thrd_get_zx_handle(self), ZX_PROP_NAME, name,153sizeof(name)) == ZX_OK154? name155: nullptr);156// We need to set the current thread before calling AsanThread::Init() below,157// since it reads the thread ID.158SetCurrentThread(t);159DCHECK_EQ(t->tid(), 0);160161const AsanThread::InitOptions options = {__sanitizer::MainThreadStackBase,162__sanitizer::MainThreadStackSize};163t->Init(&options);164165return t;166}167168// This is called before each thread creation is attempted. So, in169// its first call, the calling thread is the initial and sole thread.170static void *BeforeThreadCreateHook(uptr user_id, bool detached,171const char *name, uptr stack_bottom,172uptr stack_size) {173EnsureMainThreadIDIsCorrect();174// Strict init-order checking is thread-hostile.175if (flags()->strict_init_order)176StopInitOrderChecking();177178GET_STACK_TRACE_THREAD;179u32 parent_tid = GetCurrentTidOrInvalid();180181AsanThread *thread = CreateAsanThread(&stack, parent_tid, detached, name);182183// On other systems, AsanThread::Init() is called from the new184// thread itself. But on Fuchsia we already know the stack address185// range beforehand, so we can do most of the setup right now.186const AsanThread::InitOptions options = {stack_bottom, stack_size};187thread->Init(&options);188return thread;189}190191// This is called after creating a new thread (in the creating thread),192// with the pointer returned by BeforeThreadCreateHook (above).193static void ThreadCreateHook(void *hook, bool aborted) {194AsanThread *thread = static_cast<AsanThread *>(hook);195if (!aborted) {196// The thread was created successfully.197// ThreadStartHook is already running in the new thread.198} else {199// The thread wasn't created after all.200// Clean up everything we set up in BeforeThreadCreateHook.201asanThreadRegistry().FinishThread(thread->tid());202UnmapOrDie(thread, AsanThreadMmapSize());203}204}205206// This is called in the newly-created thread before it runs anything else,207// with the pointer returned by BeforeThreadCreateHook (above).208// cf. asan_interceptors.cpp:asan_thread_start209static void ThreadStartHook(void *hook, uptr os_id) {210AsanThread *thread = static_cast<AsanThread *>(hook);211SetCurrentThread(thread);212213// In lieu of AsanThread::ThreadStart.214asanThreadRegistry().StartThread(thread->tid(), os_id, ThreadType::Regular,215nullptr);216}217218// Each thread runs this just before it exits,219// with the pointer returned by BeforeThreadCreateHook (above).220// All per-thread destructors have already been called.221static void ThreadExitHook(void *hook, uptr os_id) {222AsanThread::TSDDtor(per_thread);223}224225bool HandleDlopenInit() {226// Not supported on this platform.227static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,228"Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");229return false;230}231232void FlushUnneededASanShadowMemory(uptr p, uptr size) {233__sanitizer_fill_shadow(p, size, 0, 0);234}235236// On Fuchsia, leak detection is done by a special hook after atexit hooks.237// So this doesn't install any atexit hook like on other platforms.238void InstallAtExitCheckLeaks() {}239240void InstallAtForkHandler() {}241242} // namespace __asan243244namespace __lsan {245246bool UseExitcodeOnLeak() { return __asan::flags()->halt_on_error; }247248} // namespace __lsan249250// These are declared (in extern "C") by <zircon/sanitizer.h>.251// The system runtime will call our definitions directly.252253void *__sanitizer_before_thread_create_hook(thrd_t thread, bool detached,254const char *name, void *stack_base,255size_t stack_size) {256return __asan::BeforeThreadCreateHook(257reinterpret_cast<uptr>(thread), detached, name,258reinterpret_cast<uptr>(stack_base), stack_size);259}260261void __sanitizer_thread_create_hook(void *hook, thrd_t thread, int error) {262__asan::ThreadCreateHook(hook, error != thrd_success);263}264265void __sanitizer_thread_start_hook(void *hook, thrd_t self) {266__asan::ThreadStartHook(hook, reinterpret_cast<uptr>(self));267}268269void __sanitizer_thread_exit_hook(void *hook, thrd_t self) {270__asan::ThreadExitHook(hook, reinterpret_cast<uptr>(self));271}272273#endif // SANITIZER_FUCHSIA274275276