1 //===-- asan_thread.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // ASan-private header for asan_thread.cc.
13 //===----------------------------------------------------------------------===//
18 #include "asan_allocator.h"
19 #include "asan_internal.h"
20 #include "asan_fake_stack.h"
21 #include "asan_stats.h"
22 #include "sanitizer_common/sanitizer_common.h"
23 #include "sanitizer_common/sanitizer_libc.h"
24 #include "sanitizer_common/sanitizer_thread_registry.h"
26 namespace __sanitizer {
28 } // namespace __sanitizer
32 const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
33 const u32 kMaxNumberOfThreads = (1 << 22); // 4M
37 // These objects are created for every thread and are never deleted,
38 // so we can find them by tid even if the thread is long dead.
39 class AsanThreadContext : public ThreadContextBase {
41 explicit AsanThreadContext(int tid)
42 : ThreadContextBase(tid), announced(false),
43 destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
46 u8 destructor_iterations;
50 void OnCreated(void *arg) override;
51 void OnFinished() override;
54 // AsanThreadContext objects are never freed, so we need many of them.
55 COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
57 // AsanThread are stored in TSD and destroyed when the thread dies.
60 static AsanThread *Create(thread_callback_t start_routine, void *arg,
61 u32 parent_tid, StackTrace *stack, bool detached);
62 static void TSDDtor(void *tsd);
65 void Init(); // Should be called from the thread itself.
66 thread_return_t ThreadStart(uptr os_id,
67 atomic_uintptr_t *signal_thread_is_registered);
72 uptr tls_begin() { return tls_begin_; }
73 uptr tls_end() { return tls_end_; }
74 DTLS *dtls() { return dtls_; }
75 u32 tid() { return context_->tid; }
76 AsanThreadContext *context() { return context_; }
77 void set_context(AsanThreadContext *context) { context_ = context; }
79 struct StackFrameAccess {
82 const char *frame_descr;
84 bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
86 bool AddrIsInStack(uptr addr);
88 void DeleteFakeStack(int tid) {
89 if (!fake_stack_) return;
90 FakeStack *t = fake_stack_;
91 fake_stack_ = nullptr;
92 SetTLSFakeStack(nullptr);
96 void StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size);
97 void FinishSwitchFiber(FakeStack *fake_stack_save);
99 bool has_fake_stack() {
100 return !atomic_load(&stack_switching_, memory_order_relaxed) &&
101 (reinterpret_cast<uptr>(fake_stack_) > 1);
104 FakeStack *fake_stack() {
105 if (!__asan_option_detect_stack_use_after_return)
107 if (atomic_load(&stack_switching_, memory_order_relaxed))
109 if (!has_fake_stack())
110 return AsyncSignalSafeLazyInitFakeStack();
114 // True is this thread is currently unwinding stack (i.e. collecting a stack
115 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
116 // malloc internally. See PR17116 for more details.
117 bool isUnwinding() const { return unwinding_; }
118 void setUnwinding(bool b) { unwinding_ = b; }
120 // True if we are in a deadly signal handler.
121 bool isInDeadlySignal() const { return in_deadly_signal_; }
122 void setInDeadlySignal(bool b) { in_deadly_signal_ = b; }
124 AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
125 AsanStats &stats() { return stats_; }
128 // NOTE: There is no AsanThread constructor. It is allocated
129 // via mmap() and *must* be valid in zero-initialized state.
130 void SetThreadStackAndTls();
131 void ClearShadowForThreadStackAndTLS();
132 FakeStack *AsyncSignalSafeLazyInitFakeStack();
138 StackBounds GetStackBounds() const;
140 AsanThreadContext *context_;
141 thread_callback_t start_routine_;
146 // these variables are used when the thread is about to switch stack
147 uptr next_stack_top_;
148 uptr next_stack_bottom_;
149 // true if switching is in progress
150 atomic_uint8_t stack_switching_;
156 FakeStack *fake_stack_;
157 AsanThreadLocalMallocStorage malloc_storage_;
160 bool in_deadly_signal_;
163 // ScopedUnwinding is a scope for stacktracing member of a context
164 class ScopedUnwinding {
166 explicit ScopedUnwinding(AsanThread *t) : thread(t) {
167 t->setUnwinding(true);
169 ~ScopedUnwinding() { thread->setUnwinding(false); }
175 // ScopedDeadlySignal is a scope for handling deadly signals.
176 class ScopedDeadlySignal {
178 explicit ScopedDeadlySignal(AsanThread *t) : thread(t) {
179 if (thread) thread->setInDeadlySignal(true);
181 ~ScopedDeadlySignal() {
182 if (thread) thread->setInDeadlySignal(false);
189 // Returns a single instance of registry.
190 ThreadRegistry &asanThreadRegistry();
192 // Must be called under ThreadRegistryLock.
193 AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
195 // Get the current thread. May return 0.
196 AsanThread *GetCurrentThread();
197 void SetCurrentThread(AsanThread *t);
198 u32 GetCurrentTidOrInvalid();
199 AsanThread *FindThreadByStackAddress(uptr addr);
201 // Used to handle fork().
202 void EnsureMainThreadIDIsCorrect();
203 } // namespace __asan
205 #endif // ASAN_THREAD_H