Home | History | Annotate | Line # | Download | only in asan
asan_thread.h revision 1.1.1.3.4.1
      1 //===-- asan_thread.h -------------------------------------------*- C++ -*-===//
      2 //
      3 // This file is distributed under the University of Illinois Open Source
      4 // License. See LICENSE.TXT for details.
      5 //
      6 //===----------------------------------------------------------------------===//
      7 //
      8 // This file is a part of AddressSanitizer, an address sanity checker.
      9 //
     10 // ASan-private header for asan_thread.cc.
     11 //===----------------------------------------------------------------------===//
     12 
     13 #ifndef ASAN_THREAD_H
     14 #define ASAN_THREAD_H
     15 
     16 #include "asan_allocator.h"
     17 #include "asan_internal.h"
     18 #include "asan_fake_stack.h"
     19 #include "asan_stats.h"
     20 #include "sanitizer_common/sanitizer_common.h"
     21 #include "sanitizer_common/sanitizer_libc.h"
     22 #include "sanitizer_common/sanitizer_thread_registry.h"
     23 
     24 namespace __sanitizer {
     25 struct DTLS;
     26 }  // namespace __sanitizer
     27 
     28 namespace __asan {
     29 
     30 const u32 kInvalidTid = 0xffffff;  // Must fit into 24 bits.
     31 const u32 kMaxNumberOfThreads = (1 << 22);  // 4M
     32 
     33 class AsanThread;
     34 
     35 // These objects are created for every thread and are never deleted,
     36 // so we can find them by tid even if the thread is long dead.
     37 class AsanThreadContext : public ThreadContextBase {
     38  public:
     39   explicit AsanThreadContext(int tid)
     40       : ThreadContextBase(tid), announced(false),
     41         destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
     42         thread(nullptr) {}
     43   bool announced;
     44   u8 destructor_iterations;
     45   u32 stack_id;
     46   AsanThread *thread;
     47 
     48   void OnCreated(void *arg) override;
     49   void OnFinished() override;
     50 };
     51 
     52 // AsanThreadContext objects are never freed, so we need many of them.
     53 COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
     54 
     55 // AsanThread are stored in TSD and destroyed when the thread dies.
     56 class AsanThread {
     57  public:
     58   static AsanThread *Create(thread_callback_t start_routine, void *arg,
     59                             u32 parent_tid, StackTrace *stack, bool detached);
     60   static void TSDDtor(void *tsd);
     61   void Destroy();
     62 
     63   void Init();  // Should be called from the thread itself.
     64   thread_return_t ThreadStart(uptr os_id,
     65                               atomic_uintptr_t *signal_thread_is_registered);
     66 
     67   uptr stack_top();
     68   uptr stack_bottom();
     69   uptr stack_size();
     70   uptr tls_begin() { return tls_begin_; }
     71   uptr tls_end() { return tls_end_; }
     72   DTLS *dtls() { return dtls_; }
     73   u32 tid() { return context_->tid; }
     74   AsanThreadContext *context() { return context_; }
     75   void set_context(AsanThreadContext *context) { context_ = context; }
     76 
     77   struct StackFrameAccess {
     78     uptr offset;
     79     uptr frame_pc;
     80     const char *frame_descr;
     81   };
     82   bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
     83 
     84   bool AddrIsInStack(uptr addr);
     85 
     86   void DeleteFakeStack(int tid) {
     87     if (!fake_stack_) return;
     88     FakeStack *t = fake_stack_;
     89     fake_stack_ = nullptr;
     90     SetTLSFakeStack(nullptr);
     91     t->Destroy(tid);
     92   }
     93 
     94   void StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size);
     95   void FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old,
     96                          uptr *size_old);
     97 
     98   bool has_fake_stack() {
     99     return !atomic_load(&stack_switching_, memory_order_relaxed) &&
    100            (reinterpret_cast<uptr>(fake_stack_) > 1);
    101   }
    102 
    103   FakeStack *fake_stack() {
    104     if (!__asan_option_detect_stack_use_after_return)
    105       return nullptr;
    106     if (atomic_load(&stack_switching_, memory_order_relaxed))
    107       return nullptr;
    108     if (!has_fake_stack())
    109       return AsyncSignalSafeLazyInitFakeStack();
    110     return fake_stack_;
    111   }
    112 
    113   // True is this thread is currently unwinding stack (i.e. collecting a stack
    114   // trace). Used to prevent deadlocks on platforms where libc unwinder calls
    115   // malloc internally. See PR17116 for more details.
    116   bool isUnwinding() const { return unwinding_; }
    117   void setUnwinding(bool b) { unwinding_ = b; }
    118 
    119   // True if we are in a deadly signal handler.
    120   bool isInDeadlySignal() const { return in_deadly_signal_; }
    121   void setInDeadlySignal(bool b) { in_deadly_signal_ = b; }
    122 
    123   AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
    124   AsanStats &stats() { return stats_; }
    125 
    126  private:
    127   // NOTE: There is no AsanThread constructor. It is allocated
    128   // via mmap() and *must* be valid in zero-initialized state.
    129   void SetThreadStackAndTls();
    130   void ClearShadowForThreadStackAndTLS();
    131   FakeStack *AsyncSignalSafeLazyInitFakeStack();
    132 
    133   struct StackBounds {
    134     uptr bottom;
    135     uptr top;
    136   };
    137   StackBounds GetStackBounds() const;
    138 
    139   AsanThreadContext *context_;
    140   thread_callback_t start_routine_;
    141   void *arg_;
    142 
    143   uptr stack_top_;
    144   uptr stack_bottom_;
    145   // these variables are used when the thread is about to switch stack
    146   uptr next_stack_top_;
    147   uptr next_stack_bottom_;
    148   // true if switching is in progress
    149   atomic_uint8_t stack_switching_;
    150 
    151   uptr tls_begin_;
    152   uptr tls_end_;
    153   DTLS *dtls_;
    154 
    155   FakeStack *fake_stack_;
    156   AsanThreadLocalMallocStorage malloc_storage_;
    157   AsanStats stats_;
    158   bool unwinding_;
    159   bool in_deadly_signal_;
    160 };
    161 
    162 // ScopedUnwinding is a scope for stacktracing member of a context
    163 class ScopedUnwinding {
    164  public:
    165   explicit ScopedUnwinding(AsanThread *t) : thread(t) {
    166     t->setUnwinding(true);
    167   }
    168   ~ScopedUnwinding() { thread->setUnwinding(false); }
    169 
    170  private:
    171   AsanThread *thread;
    172 };
    173 
    174 // ScopedDeadlySignal is a scope for handling deadly signals.
    175 class ScopedDeadlySignal {
    176  public:
    177   explicit ScopedDeadlySignal(AsanThread *t) : thread(t) {
    178     if (thread) thread->setInDeadlySignal(true);
    179   }
    180   ~ScopedDeadlySignal() {
    181     if (thread) thread->setInDeadlySignal(false);
    182   }
    183 
    184  private:
    185   AsanThread *thread;
    186 };
    187 
    188 // Returns a single instance of registry.
    189 ThreadRegistry &asanThreadRegistry();
    190 
    191 // Must be called under ThreadRegistryLock.
    192 AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
    193 
    194 // Get the current thread. May return 0.
    195 AsanThread *GetCurrentThread();
    196 void SetCurrentThread(AsanThread *t);
    197 u32 GetCurrentTidOrInvalid();
    198 AsanThread *FindThreadByStackAddress(uptr addr);
    199 
    200 // Used to handle fork().
    201 void EnsureMainThreadIDIsCorrect();
    202 } // namespace __asan
    203 
    204 #endif // ASAN_THREAD_H
    205