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