1 1.1 mrg //===-- asan_thread.h -------------------------------------------*- C++ -*-===// 2 1.1 mrg // 3 1.8 mrg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 1.8 mrg // See https://llvm.org/LICENSE.txt for license information. 5 1.8 mrg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 1.1 mrg // 7 1.1 mrg //===----------------------------------------------------------------------===// 8 1.1 mrg // 9 1.1 mrg // This file is a part of AddressSanitizer, an address sanity checker. 10 1.1 mrg // 11 1.8 mrg // ASan-private header for asan_thread.cpp. 12 1.1 mrg //===----------------------------------------------------------------------===// 13 1.4 mrg 14 1.1 mrg #ifndef ASAN_THREAD_H 15 1.1 mrg #define ASAN_THREAD_H 16 1.1 mrg 17 1.1 mrg #include "asan_allocator.h" 18 1.1 mrg #include "asan_internal.h" 19 1.3 mrg #include "asan_fake_stack.h" 20 1.1 mrg #include "asan_stats.h" 21 1.3 mrg #include "sanitizer_common/sanitizer_common.h" 22 1.1 mrg #include "sanitizer_common/sanitizer_libc.h" 23 1.3 mrg #include "sanitizer_common/sanitizer_thread_registry.h" 24 1.1 mrg 25 1.6 mrg namespace __sanitizer { 26 1.6 mrg struct DTLS; 27 1.6 mrg } // namespace __sanitizer 28 1.6 mrg 29 1.1 mrg namespace __asan { 30 1.1 mrg 31 1.1 mrg class AsanThread; 32 1.1 mrg 33 1.1 mrg // These objects are created for every thread and are never deleted, 34 1.1 mrg // so we can find them by tid even if the thread is long dead. 35 1.8 mrg class AsanThreadContext final : public ThreadContextBase { 36 1.1 mrg public: 37 1.3 mrg explicit AsanThreadContext(int tid) 38 1.4 mrg : ThreadContextBase(tid), announced(false), 39 1.4 mrg destructor_iterations(GetPthreadDestructorIterations()), stack_id(0), 40 1.4 mrg thread(nullptr) {} 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.4 mrg void OnCreated(void *arg) override; 47 1.4 mrg void OnFinished() override; 48 1.7 mrg 49 1.7 mrg struct CreateThreadContextArgs { 50 1.7 mrg AsanThread *thread; 51 1.7 mrg StackTrace *stack; 52 1.7 mrg }; 53 1.1 mrg }; 54 1.1 mrg 55 1.3 mrg // AsanThreadContext objects are never freed, so we need many of them. 56 1.3 mrg COMPILER_CHECK(sizeof(AsanThreadContext) <= 256); 57 1.1 mrg 58 1.1 mrg // AsanThread are stored in TSD and destroyed when the thread dies. 59 1.1 mrg class AsanThread { 60 1.1 mrg public: 61 1.4 mrg static AsanThread *Create(thread_callback_t start_routine, void *arg, 62 1.4 mrg u32 parent_tid, StackTrace *stack, bool detached); 63 1.3 mrg static void TSDDtor(void *tsd); 64 1.1 mrg void Destroy(); 65 1.1 mrg 66 1.7 mrg struct InitOptions; 67 1.7 mrg void Init(const InitOptions *options = nullptr); 68 1.7 mrg 69 1.8 mrg thread_return_t ThreadStart(tid_t os_id); 70 1.1 mrg 71 1.6 mrg uptr stack_top(); 72 1.6 mrg uptr stack_bottom(); 73 1.6 mrg uptr stack_size(); 74 1.3 mrg uptr tls_begin() { return tls_begin_; } 75 1.3 mrg uptr tls_end() { return tls_end_; } 76 1.6 mrg DTLS *dtls() { return dtls_; } 77 1.3 mrg u32 tid() { return context_->tid; } 78 1.3 mrg AsanThreadContext *context() { return context_; } 79 1.3 mrg void set_context(AsanThreadContext *context) { context_ = context; } 80 1.3 mrg 81 1.3 mrg struct StackFrameAccess { 82 1.3 mrg uptr offset; 83 1.3 mrg uptr frame_pc; 84 1.3 mrg const char *frame_descr; 85 1.3 mrg }; 86 1.3 mrg bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access); 87 1.1 mrg 88 1.7 mrg // Returns a pointer to the start of the stack variable's shadow memory. 89 1.7 mrg uptr GetStackVariableShadowStart(uptr addr); 90 1.7 mrg 91 1.6 mrg bool AddrIsInStack(uptr addr); 92 1.1 mrg 93 1.3 mrg void DeleteFakeStack(int tid) { 94 1.3 mrg if (!fake_stack_) return; 95 1.3 mrg FakeStack *t = fake_stack_; 96 1.4 mrg fake_stack_ = nullptr; 97 1.4 mrg SetTLSFakeStack(nullptr); 98 1.3 mrg t->Destroy(tid); 99 1.3 mrg } 100 1.3 mrg 101 1.6 mrg void StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size); 102 1.6 mrg void FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old, 103 1.6 mrg uptr *size_old); 104 1.6 mrg 105 1.8 mrg FakeStack *get_fake_stack() { 106 1.8 mrg if (atomic_load(&stack_switching_, memory_order_relaxed)) 107 1.8 mrg return nullptr; 108 1.8 mrg if (reinterpret_cast<uptr>(fake_stack_) <= 1) 109 1.8 mrg return nullptr; 110 1.8 mrg return fake_stack_; 111 1.3 mrg } 112 1.3 mrg 113 1.8 mrg FakeStack *get_or_create_fake_stack() { 114 1.6 mrg if (atomic_load(&stack_switching_, memory_order_relaxed)) 115 1.6 mrg return nullptr; 116 1.8 mrg if (reinterpret_cast<uptr>(fake_stack_) <= 1) 117 1.3 mrg return AsyncSignalSafeLazyInitFakeStack(); 118 1.3 mrg return fake_stack_; 119 1.3 mrg } 120 1.3 mrg 121 1.3 mrg // True is this thread is currently unwinding stack (i.e. collecting a stack 122 1.3 mrg // trace). Used to prevent deadlocks on platforms where libc unwinder calls 123 1.3 mrg // malloc internally. See PR17116 for more details. 124 1.3 mrg bool isUnwinding() const { return unwinding_; } 125 1.3 mrg void setUnwinding(bool b) { unwinding_ = b; } 126 1.3 mrg 127 1.1 mrg AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; } 128 1.1 mrg AsanStats &stats() { return stats_; } 129 1.1 mrg 130 1.8 mrg void *extra_spill_area() { return &extra_spill_area_; } 131 1.8 mrg 132 1.8 mrg void *get_arg() { return arg_; } 133 1.8 mrg 134 1.1 mrg private: 135 1.3 mrg // NOTE: There is no AsanThread constructor. It is allocated 136 1.3 mrg // via mmap() and *must* be valid in zero-initialized state. 137 1.7 mrg 138 1.7 mrg void SetThreadStackAndTls(const InitOptions *options); 139 1.7 mrg 140 1.3 mrg void ClearShadowForThreadStackAndTLS(); 141 1.3 mrg FakeStack *AsyncSignalSafeLazyInitFakeStack(); 142 1.3 mrg 143 1.6 mrg struct StackBounds { 144 1.6 mrg uptr bottom; 145 1.6 mrg uptr top; 146 1.6 mrg }; 147 1.6 mrg StackBounds GetStackBounds() const; 148 1.6 mrg 149 1.3 mrg AsanThreadContext *context_; 150 1.1 mrg thread_callback_t start_routine_; 151 1.1 mrg void *arg_; 152 1.6 mrg 153 1.3 mrg uptr stack_top_; 154 1.3 mrg uptr stack_bottom_; 155 1.6 mrg // these variables are used when the thread is about to switch stack 156 1.6 mrg uptr next_stack_top_; 157 1.6 mrg uptr next_stack_bottom_; 158 1.6 mrg // true if switching is in progress 159 1.6 mrg atomic_uint8_t stack_switching_; 160 1.6 mrg 161 1.3 mrg uptr tls_begin_; 162 1.3 mrg uptr tls_end_; 163 1.6 mrg DTLS *dtls_; 164 1.1 mrg 165 1.3 mrg FakeStack *fake_stack_; 166 1.1 mrg AsanThreadLocalMallocStorage malloc_storage_; 167 1.1 mrg AsanStats stats_; 168 1.3 mrg bool unwinding_; 169 1.8 mrg uptr extra_spill_area_; 170 1.1 mrg }; 171 1.1 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.4 mrg } // namespace __asan 187 1.1 mrg 188 1.4 mrg #endif // ASAN_THREAD_H 189