Home | History | Annotate | Line # | Download | only in asan
      1 //===-- asan_rtems.cc -----------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file is a part of AddressSanitizer, an address sanity checker.
     11 //
     12 // RTEMS-specific details.
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "sanitizer_common/sanitizer_rtems.h"
     16 #if SANITIZER_RTEMS
     17 
     18 #include "asan_internal.h"
     19 #include "asan_interceptors.h"
     20 #include "asan_mapping.h"
     21 #include "asan_poisoning.h"
     22 #include "asan_report.h"
     23 #include "asan_stack.h"
     24 #include "sanitizer_common/sanitizer_common.h"
     25 #include "sanitizer_common/sanitizer_libc.h"
     26 
     27 #include <pthread.h>
     28 #include <stdlib.h>
     29 
     30 namespace __asan {
     31 
     32 static void ResetShadowMemory() {
     33   uptr shadow_start = SHADOW_OFFSET;
     34   uptr shadow_end = MEM_TO_SHADOW(kMyriadMemoryEnd32);
     35   uptr gap_start = MEM_TO_SHADOW(shadow_start);
     36   uptr gap_end = MEM_TO_SHADOW(shadow_end);
     37 
     38   REAL(memset)((void *)shadow_start, 0, shadow_end - shadow_start);
     39   REAL(memset)((void *)gap_start, kAsanShadowGap, gap_end - gap_start);
     40 }
     41 
     42 void InitializeShadowMemory() {
     43   kHighMemEnd = 0;
     44   kMidMemBeg =  0;
     45   kMidMemEnd =  0;
     46 
     47   ResetShadowMemory();
     48 }
     49 
     50 void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
     51   UNIMPLEMENTED();
     52 }
     53 
     54 void AsanCheckDynamicRTPrereqs() {}
     55 void AsanCheckIncompatibleRT() {}
     56 void InitializeAsanInterceptors() {}
     57 void InitializePlatformInterceptors() {}
     58 void InitializePlatformExceptionHandlers() {}
     59 
     60 // RTEMS only support static linking; it sufficies to return with no
     61 // error.
     62 void *AsanDoesNotSupportStaticLinkage() { return nullptr; }
     63 
     64 void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
     65   UNIMPLEMENTED();
     66 }
     67 
     68 void EarlyInit() {
     69   // Provide early initialization of shadow memory so that
     70   // instrumented code running before full initialzation will not
     71   // report spurious errors.
     72   ResetShadowMemory();
     73 }
     74 
     75 // We can use a plain thread_local variable for TSD.
     76 static thread_local void *per_thread;
     77 
     78 void *AsanTSDGet() { return per_thread; }
     79 
     80 void AsanTSDSet(void *tsd) { per_thread = tsd; }
     81 
     82 // There's no initialization needed, and the passed-in destructor
     83 // will never be called.  Instead, our own thread destruction hook
     84 // (below) will call AsanThread::TSDDtor directly.
     85 void AsanTSDInit(void (*destructor)(void *tsd)) {
     86   DCHECK(destructor == &PlatformTSDDtor);
     87 }
     88 
     89 void PlatformTSDDtor(void *tsd) { UNREACHABLE(__func__); }
     90 
     91 //
     92 // Thread registration.  We provide an API similar to the Fushia port.
     93 //
     94 
     95 struct AsanThread::InitOptions {
     96   uptr stack_bottom, stack_size, tls_bottom, tls_size;
     97 };
     98 
     99 // Shared setup between thread creation and startup for the initial thread.
    100 static AsanThread *CreateAsanThread(StackTrace *stack, u32 parent_tid,
    101                                     uptr user_id, bool detached,
    102                                     uptr stack_bottom, uptr stack_size,
    103                                     uptr tls_bottom, uptr tls_size) {
    104   // In lieu of AsanThread::Create.
    105   AsanThread *thread = (AsanThread *)MmapOrDie(sizeof(AsanThread), __func__);
    106   AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
    107   asanThreadRegistry().CreateThread(user_id, detached, parent_tid, &args);
    108 
    109   // On other systems, AsanThread::Init() is called from the new
    110   // thread itself.  But on RTEMS we already know the stack address
    111   // range beforehand, so we can do most of the setup right now.
    112   const AsanThread::InitOptions options = {stack_bottom, stack_size,
    113                                            tls_bottom, tls_size};
    114   thread->Init(&options);
    115   return thread;
    116 }
    117 
    118 // This gets the same arguments passed to Init by CreateAsanThread, above.
    119 // We're in the creator thread before the new thread is actually started, but
    120 // its stack and tls address range are already known.
    121 void AsanThread::SetThreadStackAndTls(const AsanThread::InitOptions *options) {
    122   DCHECK_NE(GetCurrentThread(), this);
    123   DCHECK_NE(GetCurrentThread(), nullptr);
    124   CHECK_NE(options->stack_bottom, 0);
    125   CHECK_NE(options->stack_size, 0);
    126   stack_bottom_ = options->stack_bottom;
    127   stack_top_ = options->stack_bottom + options->stack_size;
    128   tls_begin_ = options->tls_bottom;
    129   tls_end_ = options->tls_bottom + options->tls_size;
    130 }
    131 
    132 // Called by __asan::AsanInitInternal (asan_rtl.c).  Unlike other ports, the
    133 // main thread on RTEMS does not require special treatment; its AsanThread is
    134 // already created by the provided hooks.  This function simply looks up and
    135 // returns the created thread.
    136 AsanThread *CreateMainThread() {
    137   return GetThreadContextByTidLocked(0)->thread;
    138 }
    139 
    140 // This is called before each thread creation is attempted.  So, in
    141 // its first call, the calling thread is the initial and sole thread.
    142 static void *BeforeThreadCreateHook(uptr user_id, bool detached,
    143                                     uptr stack_bottom, uptr stack_size,
    144                                     uptr tls_bottom, uptr tls_size) {
    145   EnsureMainThreadIDIsCorrect();
    146   // Strict init-order checking is thread-hostile.
    147   if (flags()->strict_init_order) StopInitOrderChecking();
    148 
    149   GET_STACK_TRACE_THREAD;
    150   u32 parent_tid = GetCurrentTidOrInvalid();
    151 
    152   return CreateAsanThread(&stack, parent_tid, user_id, detached,
    153                           stack_bottom, stack_size, tls_bottom, tls_size);
    154 }
    155 
    156 // This is called after creating a new thread (in the creating thread),
    157 // with the pointer returned by BeforeThreadCreateHook (above).
    158 static void ThreadCreateHook(void *hook, bool aborted) {
    159   AsanThread *thread = static_cast<AsanThread *>(hook);
    160   if (!aborted) {
    161     // The thread was created successfully.
    162     // ThreadStartHook is already running in the new thread.
    163   } else {
    164     // The thread wasn't created after all.
    165     // Clean up everything we set up in BeforeThreadCreateHook.
    166     asanThreadRegistry().FinishThread(thread->tid());
    167     UnmapOrDie(thread, sizeof(AsanThread));
    168   }
    169 }
    170 
    171 // This is called (1) in the newly-created thread before it runs anything else,
    172 // with the pointer returned by BeforeThreadCreateHook (above).  (2) before a
    173 // thread restart.
    174 static void ThreadStartHook(void *hook, uptr os_id) {
    175   if (!hook)
    176     return;
    177 
    178   AsanThread *thread = static_cast<AsanThread *>(hook);
    179   SetCurrentThread(thread);
    180 
    181   ThreadStatus status =
    182       asanThreadRegistry().GetThreadLocked(thread->tid())->status;
    183   DCHECK(status == ThreadStatusCreated || status == ThreadStatusRunning);
    184   // Determine whether we are starting or restarting the thread.
    185   if (status == ThreadStatusCreated)
    186     // In lieu of AsanThread::ThreadStart.
    187     asanThreadRegistry().StartThread(thread->tid(), os_id,
    188                                      /*workerthread*/ false, nullptr);
    189   else {
    190     // In a thread restart, a thread may resume execution at an
    191     // arbitrary function entry point, with its stack and TLS state
    192     // reset.  We unpoison the stack in that case.
    193     PoisonShadow(thread->stack_bottom(), thread->stack_size(), 0);
    194   }
    195 }
    196 
    197 // Each thread runs this just before it exits,
    198 // with the pointer returned by BeforeThreadCreateHook (above).
    199 // All per-thread destructors have already been called.
    200 static void ThreadExitHook(void *hook, uptr os_id) {
    201   AsanThread *thread = static_cast<AsanThread *>(hook);
    202   if (thread)
    203     AsanThread::TSDDtor(thread->context());
    204 }
    205 
    206 static void HandleExit() {
    207   // Disable ASan by setting it to uninitialized.  Also reset the
    208   // shadow memory to avoid reporting errors after the run-time has
    209   // been desroyed.
    210   if (asan_inited) {
    211     asan_inited = false;
    212     ResetShadowMemory();
    213   }
    214 }
    215 
    216 bool HandleDlopenInit() {
    217   // Not supported on this platform.
    218   static_assert(!SANITIZER_SUPPORTS_INIT_FOR_DLOPEN,
    219                 "Expected SANITIZER_SUPPORTS_INIT_FOR_DLOPEN to be false");
    220   return false;
    221 }
    222 }  // namespace __asan
    223 
    224 // These are declared (in extern "C") by <some_path/sanitizer.h>.
    225 // The system runtime will call our definitions directly.
    226 
    227 extern "C" {
    228 void __sanitizer_early_init() {
    229   __asan::EarlyInit();
    230 }
    231 
    232 void *__sanitizer_before_thread_create_hook(uptr thread, bool detached,
    233                                             const char *name,
    234                                             void *stack_base, size_t stack_size,
    235                                             void *tls_base, size_t tls_size) {
    236   return __asan::BeforeThreadCreateHook(
    237       thread, detached,
    238       reinterpret_cast<uptr>(stack_base), stack_size,
    239       reinterpret_cast<uptr>(tls_base), tls_size);
    240 }
    241 
    242 void __sanitizer_thread_create_hook(void *handle, uptr thread, int status) {
    243   __asan::ThreadCreateHook(handle, status != 0);
    244 }
    245 
    246 void __sanitizer_thread_start_hook(void *handle, uptr self) {
    247   __asan::ThreadStartHook(handle, self);
    248 }
    249 
    250 void __sanitizer_thread_exit_hook(void *handle, uptr self) {
    251   __asan::ThreadExitHook(handle, self);
    252 }
    253 
    254 void __sanitizer_exit() {
    255   __asan::HandleExit();
    256 }
    257 }  // "C"
    258 
    259 #endif  // SANITIZER_RTEMS
    260