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 //===----------------------------------------------------------------------===//
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"
27 const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
28 const u32 kMaxNumberOfThreads = (1 << 22); // 4M
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 {
36 explicit AsanThreadContext(int tid)
37 : ThreadContextBase(tid), announced(false),
38 destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
41 u8 destructor_iterations;
45 void OnCreated(void *arg) override;
46 void OnFinished() override;
49 // AsanThreadContext objects are never freed, so we need many of them.
50 COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
52 // AsanThread are stored in TSD and destroyed when the thread dies.
55 static AsanThread *Create(thread_callback_t start_routine, void *arg,
56 u32 parent_tid, StackTrace *stack, bool detached);
57 static void TSDDtor(void *tsd);
60 void Init(); // Should be called from the thread itself.
61 thread_return_t ThreadStart(uptr os_id,
62 atomic_uintptr_t *signal_thread_is_registered);
64 uptr stack_top() { return stack_top_; }
65 uptr stack_bottom() { return stack_bottom_; }
66 uptr stack_size() { return stack_size_; }
67 uptr tls_begin() { return tls_begin_; }
68 uptr tls_end() { return tls_end_; }
69 u32 tid() { return context_->tid; }
70 AsanThreadContext *context() { return context_; }
71 void set_context(AsanThreadContext *context) { context_ = context; }
73 struct StackFrameAccess {
76 const char *frame_descr;
78 bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
80 bool AddrIsInStack(uptr addr) {
81 return addr >= stack_bottom_ && addr < stack_top_;
84 void DeleteFakeStack(int tid) {
85 if (!fake_stack_) return;
86 FakeStack *t = fake_stack_;
92 bool has_fake_stack() {
93 return (reinterpret_cast<uptr>(fake_stack_) > 1);
96 FakeStack *fake_stack() {
97 if (!__asan_option_detect_stack_use_after_return)
99 if (!has_fake_stack())
100 return AsyncSignalSafeLazyInitFakeStack();
104 // True is this thread is currently unwinding stack (i.e. collecting a stack
105 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
106 // malloc internally. See PR17116 for more details.
107 bool isUnwinding() const { return unwinding_; }
108 void setUnwinding(bool b) { unwinding_ = b; }
110 // True if we are in a deadly signal handler.
111 bool isInDeadlySignal() const { return in_deadly_signal_; }
112 void setInDeadlySignal(bool b) { in_deadly_signal_ = b; }
114 AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
115 AsanStats &stats() { return stats_; }
118 // NOTE: There is no AsanThread constructor. It is allocated
119 // via mmap() and *must* be valid in zero-initialized state.
120 void SetThreadStackAndTls();
121 void ClearShadowForThreadStackAndTLS();
122 FakeStack *AsyncSignalSafeLazyInitFakeStack();
124 AsanThreadContext *context_;
125 thread_callback_t start_routine_;
129 // stack_size_ == stack_top_ - stack_bottom_;
130 // It needs to be set in a async-signal-safe manner.
135 FakeStack *fake_stack_;
136 AsanThreadLocalMallocStorage malloc_storage_;
139 bool in_deadly_signal_;
142 // ScopedUnwinding is a scope for stacktracing member of a context
143 class ScopedUnwinding {
145 explicit ScopedUnwinding(AsanThread *t) : thread(t) {
146 t->setUnwinding(true);
148 ~ScopedUnwinding() { thread->setUnwinding(false); }
154 // ScopedDeadlySignal is a scope for handling deadly signals.
155 class ScopedDeadlySignal {
157 explicit ScopedDeadlySignal(AsanThread *t) : thread(t) {
158 if (thread) thread->setInDeadlySignal(true);
160 ~ScopedDeadlySignal() {
161 if (thread) thread->setInDeadlySignal(false);
168 // Returns a single instance of registry.
169 ThreadRegistry &asanThreadRegistry();
171 // Must be called under ThreadRegistryLock.
172 AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
174 // Get the current thread. May return 0.
175 AsanThread *GetCurrentThread();
176 void SetCurrentThread(AsanThread *t);
177 u32 GetCurrentTidOrInvalid();
178 AsanThread *FindThreadByStackAddress(uptr addr);
180 // Used to handle fork().
181 void EnsureMainThreadIDIsCorrect();
182 } // namespace __asan
184 #endif // ASAN_THREAD_H