Home | History | Annotate | Line # | Download | only in asan
asan_thread.h revision 1.1.1.3.4.1
      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.1.3       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.1.1.2       mrg #include "asan_fake_stack.h"
     19          1.1       mrg #include "asan_stats.h"
     20      1.1.1.2       mrg #include "sanitizer_common/sanitizer_common.h"
     21          1.1       mrg #include "sanitizer_common/sanitizer_libc.h"
     22      1.1.1.2       mrg #include "sanitizer_common/sanitizer_thread_registry.h"
     23          1.1       mrg 
     24  1.1.1.3.4.1  christos namespace __sanitizer {
     25  1.1.1.3.4.1  christos struct DTLS;
     26  1.1.1.3.4.1  christos }  // namespace __sanitizer
     27  1.1.1.3.4.1  christos 
     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.1.1.2       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.1.1.2       mrg class AsanThreadContext : public ThreadContextBase {
     38          1.1       mrg  public:
     39      1.1.1.2       mrg   explicit AsanThreadContext(int tid)
     40      1.1.1.3       mrg       : ThreadContextBase(tid), announced(false),
     41      1.1.1.3       mrg         destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
     42      1.1.1.3       mrg         thread(nullptr) {}
     43      1.1.1.2       mrg   bool announced;
     44      1.1.1.2       mrg   u8 destructor_iterations;
     45      1.1.1.2       mrg   u32 stack_id;
     46      1.1.1.2       mrg   AsanThread *thread;
     47          1.1       mrg 
     48      1.1.1.3       mrg   void OnCreated(void *arg) override;
     49      1.1.1.3       mrg   void OnFinished() override;
     50          1.1       mrg };
     51          1.1       mrg 
     52      1.1.1.2       mrg // AsanThreadContext objects are never freed, so we need many of them.
     53      1.1.1.2       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.1.1.3       mrg   static AsanThread *Create(thread_callback_t start_routine, void *arg,
     59      1.1.1.3       mrg                             u32 parent_tid, StackTrace *stack, bool detached);
     60      1.1.1.2       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.1.1.3       mrg   thread_return_t ThreadStart(uptr os_id,
     65      1.1.1.3       mrg                               atomic_uintptr_t *signal_thread_is_registered);
     66          1.1       mrg 
     67  1.1.1.3.4.1  christos   uptr stack_top();
     68  1.1.1.3.4.1  christos   uptr stack_bottom();
     69  1.1.1.3.4.1  christos   uptr stack_size();
     70      1.1.1.2       mrg   uptr tls_begin() { return tls_begin_; }
     71      1.1.1.2       mrg   uptr tls_end() { return tls_end_; }
     72  1.1.1.3.4.1  christos   DTLS *dtls() { return dtls_; }
     73      1.1.1.2       mrg   u32 tid() { return context_->tid; }
     74      1.1.1.2       mrg   AsanThreadContext *context() { return context_; }
     75      1.1.1.2       mrg   void set_context(AsanThreadContext *context) { context_ = context; }
     76      1.1.1.2       mrg 
     77      1.1.1.2       mrg   struct StackFrameAccess {
     78      1.1.1.2       mrg     uptr offset;
     79      1.1.1.2       mrg     uptr frame_pc;
     80      1.1.1.2       mrg     const char *frame_descr;
     81      1.1.1.2       mrg   };
     82      1.1.1.2       mrg   bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
     83          1.1       mrg 
     84  1.1.1.3.4.1  christos   bool AddrIsInStack(uptr addr);
     85          1.1       mrg 
     86      1.1.1.2       mrg   void DeleteFakeStack(int tid) {
     87      1.1.1.2       mrg     if (!fake_stack_) return;
     88      1.1.1.2       mrg     FakeStack *t = fake_stack_;
     89      1.1.1.3       mrg     fake_stack_ = nullptr;
     90      1.1.1.3       mrg     SetTLSFakeStack(nullptr);
     91      1.1.1.2       mrg     t->Destroy(tid);
     92      1.1.1.2       mrg   }
     93      1.1.1.2       mrg 
     94  1.1.1.3.4.1  christos   void StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size);
     95  1.1.1.3.4.1  christos   void FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old,
     96  1.1.1.3.4.1  christos                          uptr *size_old);
     97  1.1.1.3.4.1  christos 
     98      1.1.1.2       mrg   bool has_fake_stack() {
     99  1.1.1.3.4.1  christos     return !atomic_load(&stack_switching_, memory_order_relaxed) &&
    100  1.1.1.3.4.1  christos            (reinterpret_cast<uptr>(fake_stack_) > 1);
    101      1.1.1.2       mrg   }
    102      1.1.1.2       mrg 
    103      1.1.1.2       mrg   FakeStack *fake_stack() {
    104      1.1.1.2       mrg     if (!__asan_option_detect_stack_use_after_return)
    105      1.1.1.3       mrg       return nullptr;
    106  1.1.1.3.4.1  christos     if (atomic_load(&stack_switching_, memory_order_relaxed))
    107  1.1.1.3.4.1  christos       return nullptr;
    108      1.1.1.2       mrg     if (!has_fake_stack())
    109      1.1.1.2       mrg       return AsyncSignalSafeLazyInitFakeStack();
    110      1.1.1.2       mrg     return fake_stack_;
    111      1.1.1.2       mrg   }
    112      1.1.1.2       mrg 
    113      1.1.1.2       mrg   // True is this thread is currently unwinding stack (i.e. collecting a stack
    114      1.1.1.2       mrg   // trace). Used to prevent deadlocks on platforms where libc unwinder calls
    115      1.1.1.2       mrg   // malloc internally. See PR17116 for more details.
    116      1.1.1.2       mrg   bool isUnwinding() const { return unwinding_; }
    117      1.1.1.2       mrg   void setUnwinding(bool b) { unwinding_ = b; }
    118      1.1.1.2       mrg 
    119      1.1.1.2       mrg   // True if we are in a deadly signal handler.
    120      1.1.1.2       mrg   bool isInDeadlySignal() const { return in_deadly_signal_; }
    121      1.1.1.2       mrg   void setInDeadlySignal(bool b) { in_deadly_signal_ = b; }
    122      1.1.1.2       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.1.1.2       mrg   // NOTE: There is no AsanThread constructor. It is allocated
    128      1.1.1.2       mrg   // via mmap() and *must* be valid in zero-initialized state.
    129      1.1.1.2       mrg   void SetThreadStackAndTls();
    130      1.1.1.2       mrg   void ClearShadowForThreadStackAndTLS();
    131      1.1.1.2       mrg   FakeStack *AsyncSignalSafeLazyInitFakeStack();
    132      1.1.1.2       mrg 
    133  1.1.1.3.4.1  christos   struct StackBounds {
    134  1.1.1.3.4.1  christos     uptr bottom;
    135  1.1.1.3.4.1  christos     uptr top;
    136  1.1.1.3.4.1  christos   };
    137  1.1.1.3.4.1  christos   StackBounds GetStackBounds() const;
    138  1.1.1.3.4.1  christos 
    139      1.1.1.2       mrg   AsanThreadContext *context_;
    140          1.1       mrg   thread_callback_t start_routine_;
    141          1.1       mrg   void *arg_;
    142  1.1.1.3.4.1  christos 
    143      1.1.1.2       mrg   uptr stack_top_;
    144      1.1.1.2       mrg   uptr stack_bottom_;
    145  1.1.1.3.4.1  christos   // these variables are used when the thread is about to switch stack
    146  1.1.1.3.4.1  christos   uptr next_stack_top_;
    147  1.1.1.3.4.1  christos   uptr next_stack_bottom_;
    148  1.1.1.3.4.1  christos   // true if switching is in progress
    149  1.1.1.3.4.1  christos   atomic_uint8_t stack_switching_;
    150  1.1.1.3.4.1  christos 
    151      1.1.1.2       mrg   uptr tls_begin_;
    152      1.1.1.2       mrg   uptr tls_end_;
    153  1.1.1.3.4.1  christos   DTLS *dtls_;
    154          1.1       mrg 
    155      1.1.1.2       mrg   FakeStack *fake_stack_;
    156          1.1       mrg   AsanThreadLocalMallocStorage malloc_storage_;
    157          1.1       mrg   AsanStats stats_;
    158      1.1.1.2       mrg   bool unwinding_;
    159      1.1.1.2       mrg   bool in_deadly_signal_;
    160      1.1.1.2       mrg };
    161      1.1.1.2       mrg 
    162      1.1.1.2       mrg // ScopedUnwinding is a scope for stacktracing member of a context
    163      1.1.1.2       mrg class ScopedUnwinding {
    164      1.1.1.2       mrg  public:
    165      1.1.1.2       mrg   explicit ScopedUnwinding(AsanThread *t) : thread(t) {
    166      1.1.1.2       mrg     t->setUnwinding(true);
    167      1.1.1.2       mrg   }
    168      1.1.1.2       mrg   ~ScopedUnwinding() { thread->setUnwinding(false); }
    169      1.1.1.2       mrg 
    170      1.1.1.2       mrg  private:
    171      1.1.1.2       mrg   AsanThread *thread;
    172          1.1       mrg };
    173          1.1       mrg 
    174      1.1.1.2       mrg // ScopedDeadlySignal is a scope for handling deadly signals.
    175      1.1.1.2       mrg class ScopedDeadlySignal {
    176      1.1.1.2       mrg  public:
    177      1.1.1.2       mrg   explicit ScopedDeadlySignal(AsanThread *t) : thread(t) {
    178      1.1.1.2       mrg     if (thread) thread->setInDeadlySignal(true);
    179      1.1.1.2       mrg   }
    180      1.1.1.2       mrg   ~ScopedDeadlySignal() {
    181      1.1.1.2       mrg     if (thread) thread->setInDeadlySignal(false);
    182      1.1.1.2       mrg   }
    183      1.1.1.2       mrg 
    184      1.1.1.2       mrg  private:
    185      1.1.1.2       mrg   AsanThread *thread;
    186      1.1.1.2       mrg };
    187      1.1.1.2       mrg 
    188      1.1.1.2       mrg // Returns a single instance of registry.
    189      1.1.1.2       mrg ThreadRegistry &asanThreadRegistry();
    190      1.1.1.2       mrg 
    191      1.1.1.2       mrg // Must be called under ThreadRegistryLock.
    192      1.1.1.2       mrg AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
    193      1.1.1.2       mrg 
    194      1.1.1.2       mrg // Get the current thread. May return 0.
    195      1.1.1.2       mrg AsanThread *GetCurrentThread();
    196      1.1.1.2       mrg void SetCurrentThread(AsanThread *t);
    197      1.1.1.2       mrg u32 GetCurrentTidOrInvalid();
    198      1.1.1.2       mrg AsanThread *FindThreadByStackAddress(uptr addr);
    199      1.1.1.2       mrg 
    200      1.1.1.2       mrg // Used to handle fork().
    201      1.1.1.2       mrg void EnsureMainThreadIDIsCorrect();
    202      1.1.1.3       mrg } // namespace __asan
    203          1.1       mrg 
    204      1.1.1.3       mrg #endif // ASAN_THREAD_H
    205