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"
28 const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
29 const u32 kMaxNumberOfThreads = (1 << 22); // 4M
33 // These objects are created for every thread and are never deleted,
34 // so we can find them by tid even if the thread is long dead.
35 class AsanThreadContext : public ThreadContextBase {
37 explicit AsanThreadContext(int tid)
38 : ThreadContextBase(tid), announced(false),
39 destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
42 u8 destructor_iterations;
46 void OnCreated(void *arg) override;
47 void OnFinished() override;
50 // AsanThreadContext objects are never freed, so we need many of them.
51 COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
53 // AsanThread are stored in TSD and destroyed when the thread dies.
56 static AsanThread *Create(thread_callback_t start_routine, void *arg,
57 u32 parent_tid, StackTrace *stack, bool detached);
58 static void TSDDtor(void *tsd);
61 void Init(); // Should be called from the thread itself.
62 thread_return_t ThreadStart(uptr os_id,
63 atomic_uintptr_t *signal_thread_is_registered);
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; }
74 struct StackFrameAccess {
77 const char *frame_descr;
79 bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
81 bool AddrIsInStack(uptr addr) {
82 return addr >= stack_bottom_ && addr < stack_top_;
85 void DeleteFakeStack(int tid) {
86 if (!fake_stack_) return;
87 FakeStack *t = fake_stack_;
88 fake_stack_ = nullptr;
89 SetTLSFakeStack(nullptr);
93 bool has_fake_stack() {
94 return (reinterpret_cast<uptr>(fake_stack_) > 1);
97 FakeStack *fake_stack() {
98 if (!__asan_option_detect_stack_use_after_return)
100 if (!has_fake_stack())
101 return AsyncSignalSafeLazyInitFakeStack();
105 // True is this thread is currently unwinding stack (i.e. collecting a stack
106 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
107 // malloc internally. See PR17116 for more details.
108 bool isUnwinding() const { return unwinding_; }
109 void setUnwinding(bool b) { unwinding_ = b; }
111 // True if we are in a deadly signal handler.
112 bool isInDeadlySignal() const { return in_deadly_signal_; }
113 void setInDeadlySignal(bool b) { in_deadly_signal_ = b; }
115 AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
116 AsanStats &stats() { return stats_; }
119 // NOTE: There is no AsanThread constructor. It is allocated
120 // via mmap() and *must* be valid in zero-initialized state.
121 void SetThreadStackAndTls();
122 void ClearShadowForThreadStackAndTLS();
123 FakeStack *AsyncSignalSafeLazyInitFakeStack();
125 AsanThreadContext *context_;
126 thread_callback_t start_routine_;
130 // stack_size_ == stack_top_ - stack_bottom_;
131 // It needs to be set in a async-signal-safe manner.
136 FakeStack *fake_stack_;
137 AsanThreadLocalMallocStorage malloc_storage_;
140 bool in_deadly_signal_;
143 // ScopedUnwinding is a scope for stacktracing member of a context
144 class ScopedUnwinding {
146 explicit ScopedUnwinding(AsanThread *t) : thread(t) {
147 t->setUnwinding(true);
149 ~ScopedUnwinding() { thread->setUnwinding(false); }
155 // ScopedDeadlySignal is a scope for handling deadly signals.
156 class ScopedDeadlySignal {
158 explicit ScopedDeadlySignal(AsanThread *t) : thread(t) {
159 if (thread) thread->setInDeadlySignal(true);
161 ~ScopedDeadlySignal() {
162 if (thread) thread->setInDeadlySignal(false);
169 // Returns a single instance of registry.
170 ThreadRegistry &asanThreadRegistry();
172 // Must be called under ThreadRegistryLock.
173 AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
175 // Get the current thread. May return 0.
176 AsanThread *GetCurrentThread();
177 void SetCurrentThread(AsanThread *t);
178 u32 GetCurrentTidOrInvalid();
179 AsanThread *FindThreadByStackAddress(uptr addr);
181 // Used to handle fork().
182 void EnsureMainThreadIDIsCorrect();
183 } // namespace __asan
185 #endif // ASAN_THREAD_H