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