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