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.3 mrg #include "asan_fake_stack.h" 18 1.1 mrg #include "asan_stats.h" 19 1.3 mrg #include "sanitizer_common/sanitizer_common.h" 20 1.1 mrg #include "sanitizer_common/sanitizer_libc.h" 21 1.3 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.3 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.3 mrg class AsanThreadContext : public ThreadContextBase { 33 1.1 mrg public: 34 1.3 mrg explicit AsanThreadContext(int tid) 35 1.3 mrg : ThreadContextBase(tid), 36 1.3 mrg announced(false), 37 1.3 mrg destructor_iterations(kPthreadDestructorIterations), 38 1.3 mrg stack_id(0), 39 1.3 mrg thread(0) { 40 1.1 mrg } 41 1.3 mrg bool announced; 42 1.3 mrg u8 destructor_iterations; 43 1.3 mrg u32 stack_id; 44 1.3 mrg AsanThread *thread; 45 1.1 mrg 46 1.3 mrg void OnCreated(void *arg); 47 1.3 mrg void OnFinished(); 48 1.1 mrg }; 49 1.1 mrg 50 1.3 mrg // AsanThreadContext objects are never freed, so we need many of them. 51 1.3 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.3 mrg static AsanThread *Create(thread_callback_t start_routine, void *arg); 57 1.3 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.3 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.3 mrg uptr stack_size() { return stack_size_; } 66 1.3 mrg uptr tls_begin() { return tls_begin_; } 67 1.3 mrg uptr tls_end() { return tls_end_; } 68 1.3 mrg u32 tid() { return context_->tid; } 69 1.3 mrg AsanThreadContext *context() { return context_; } 70 1.3 mrg void set_context(AsanThreadContext *context) { context_ = context; } 71 1.3 mrg 72 1.3 mrg struct StackFrameAccess { 73 1.3 mrg uptr offset; 74 1.3 mrg uptr frame_pc; 75 1.3 mrg const char *frame_descr; 76 1.3 mrg }; 77 1.3 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.3 mrg void DeleteFakeStack(int tid) { 84 1.3 mrg if (!fake_stack_) return; 85 1.3 mrg FakeStack *t = fake_stack_; 86 1.3 mrg fake_stack_ = 0; 87 1.3 mrg SetTLSFakeStack(0); 88 1.3 mrg t->Destroy(tid); 89 1.3 mrg } 90 1.3 mrg 91 1.3 mrg bool has_fake_stack() { 92 1.3 mrg return (reinterpret_cast<uptr>(fake_stack_) > 1); 93 1.3 mrg } 94 1.3 mrg 95 1.3 mrg FakeStack *fake_stack() { 96 1.3 mrg if (!__asan_option_detect_stack_use_after_return) 97 1.3 mrg return 0; 98 1.3 mrg if (!has_fake_stack()) 99 1.3 mrg return AsyncSignalSafeLazyInitFakeStack(); 100 1.3 mrg return fake_stack_; 101 1.3 mrg } 102 1.3 mrg 103 1.3 mrg // True is this thread is currently unwinding stack (i.e. collecting a stack 104 1.3 mrg // trace). Used to prevent deadlocks on platforms where libc unwinder calls 105 1.3 mrg // malloc internally. See PR17116 for more details. 106 1.3 mrg bool isUnwinding() const { return unwinding_; } 107 1.3 mrg void setUnwinding(bool b) { unwinding_ = b; } 108 1.3 mrg 109 1.3 mrg // True if we are in a deadly signal handler. 110 1.3 mrg bool isInDeadlySignal() const { return in_deadly_signal_; } 111 1.3 mrg void setInDeadlySignal(bool b) { in_deadly_signal_ = b; } 112 1.3 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.3 mrg // NOTE: There is no AsanThread constructor. It is allocated 118 1.3 mrg // via mmap() and *must* be valid in zero-initialized state. 119 1.3 mrg void SetThreadStackAndTls(); 120 1.3 mrg void ClearShadowForThreadStackAndTLS(); 121 1.3 mrg FakeStack *AsyncSignalSafeLazyInitFakeStack(); 122 1.3 mrg 123 1.3 mrg AsanThreadContext *context_; 124 1.1 mrg thread_callback_t start_routine_; 125 1.1 mrg void *arg_; 126 1.3 mrg uptr stack_top_; 127 1.3 mrg uptr stack_bottom_; 128 1.3 mrg // stack_size_ == stack_top_ - stack_bottom_; 129 1.3 mrg // It needs to be set in a async-signal-safe manner. 130 1.3 mrg uptr stack_size_; 131 1.3 mrg uptr tls_begin_; 132 1.3 mrg uptr tls_end_; 133 1.1 mrg 134 1.3 mrg FakeStack *fake_stack_; 135 1.1 mrg AsanThreadLocalMallocStorage malloc_storage_; 136 1.1 mrg AsanStats stats_; 137 1.3 mrg bool unwinding_; 138 1.3 mrg bool in_deadly_signal_; 139 1.3 mrg }; 140 1.3 mrg 141 1.3 mrg // ScopedUnwinding is a scope for stacktracing member of a context 142 1.3 mrg class ScopedUnwinding { 143 1.3 mrg public: 144 1.3 mrg explicit ScopedUnwinding(AsanThread *t) : thread(t) { 145 1.3 mrg t->setUnwinding(true); 146 1.3 mrg } 147 1.3 mrg ~ScopedUnwinding() { thread->setUnwinding(false); } 148 1.3 mrg 149 1.3 mrg private: 150 1.3 mrg AsanThread *thread; 151 1.1 mrg }; 152 1.1 mrg 153 1.3 mrg // ScopedDeadlySignal is a scope for handling deadly signals. 154 1.3 mrg class ScopedDeadlySignal { 155 1.3 mrg public: 156 1.3 mrg explicit ScopedDeadlySignal(AsanThread *t) : thread(t) { 157 1.3 mrg if (thread) thread->setInDeadlySignal(true); 158 1.3 mrg } 159 1.3 mrg ~ScopedDeadlySignal() { 160 1.3 mrg if (thread) thread->setInDeadlySignal(false); 161 1.3 mrg } 162 1.3 mrg 163 1.3 mrg private: 164 1.3 mrg AsanThread *thread; 165 1.3 mrg }; 166 1.3 mrg 167 1.3 mrg struct CreateThreadContextArgs { 168 1.3 mrg AsanThread *thread; 169 1.3 mrg StackTrace *stack; 170 1.3 mrg }; 171 1.3 mrg 172 1.3 mrg // Returns a single instance of registry. 173 1.3 mrg ThreadRegistry &asanThreadRegistry(); 174 1.3 mrg 175 1.3 mrg // Must be called under ThreadRegistryLock. 176 1.3 mrg AsanThreadContext *GetThreadContextByTidLocked(u32 tid); 177 1.3 mrg 178 1.3 mrg // Get the current thread. May return 0. 179 1.3 mrg AsanThread *GetCurrentThread(); 180 1.3 mrg void SetCurrentThread(AsanThread *t); 181 1.3 mrg u32 GetCurrentTidOrInvalid(); 182 1.3 mrg AsanThread *FindThreadByStackAddress(uptr addr); 183 1.3 mrg 184 1.3 mrg // Used to handle fork(). 185 1.3 mrg void EnsureMainThreadIDIsCorrect(); 186 1.1 mrg } // namespace __asan 187 1.1 mrg 188 1.1 mrg #endif // ASAN_THREAD_H 189