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