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