1 //===-- asan_thread.h -------------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // ASan-private header for asan_thread.cc.
11 //===----------------------------------------------------------------------===//
16 #include "asan_allocator.h"
17 #include "asan_internal.h"
18 #include "asan_fake_stack.h"
19 #include "asan_stats.h"
20 #include "sanitizer_common/sanitizer_common.h"
21 #include "sanitizer_common/sanitizer_libc.h"
22 #include "sanitizer_common/sanitizer_thread_registry.h"
24 namespace __sanitizer
{
26 } // namespace __sanitizer
30 const u32 kInvalidTid
= 0xffffff; // Must fit into 24 bits.
31 const u32 kMaxNumberOfThreads
= (1 << 22); // 4M
35 // These objects are created for every thread and are never deleted,
36 // so we can find them by tid even if the thread is long dead.
37 class AsanThreadContext
: public ThreadContextBase
{
39 explicit AsanThreadContext(int tid
)
40 : ThreadContextBase(tid
), announced(false),
41 destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
44 u8 destructor_iterations
;
48 void OnCreated(void *arg
) override
;
49 void OnFinished() override
;
52 // AsanThreadContext objects are never freed, so we need many of them.
53 COMPILER_CHECK(sizeof(AsanThreadContext
) <= 256);
55 // AsanThread are stored in TSD and destroyed when the thread dies.
58 static AsanThread
*Create(thread_callback_t start_routine
, void *arg
,
59 u32 parent_tid
, StackTrace
*stack
, bool detached
);
60 static void TSDDtor(void *tsd
);
63 void Init(); // Should be called from the thread itself.
64 thread_return_t
ThreadStart(uptr os_id
,
65 atomic_uintptr_t
*signal_thread_is_registered
);
70 uptr
tls_begin() { return tls_begin_
; }
71 uptr
tls_end() { return tls_end_
; }
72 DTLS
*dtls() { return dtls_
; }
73 u32
tid() { return context_
->tid
; }
74 AsanThreadContext
*context() { return context_
; }
75 void set_context(AsanThreadContext
*context
) { context_
= context
; }
77 struct StackFrameAccess
{
80 const char *frame_descr
;
82 bool GetStackFrameAccessByAddr(uptr addr
, StackFrameAccess
*access
);
84 bool AddrIsInStack(uptr addr
);
86 void DeleteFakeStack(int tid
) {
87 if (!fake_stack_
) return;
88 FakeStack
*t
= fake_stack_
;
89 fake_stack_
= nullptr;
90 SetTLSFakeStack(nullptr);
94 void StartSwitchFiber(FakeStack
**fake_stack_save
, uptr bottom
, uptr size
);
95 void FinishSwitchFiber(FakeStack
*fake_stack_save
, uptr
*bottom_old
,
98 bool has_fake_stack() {
99 return !atomic_load(&stack_switching_
, memory_order_relaxed
) &&
100 (reinterpret_cast<uptr
>(fake_stack_
) > 1);
103 FakeStack
*fake_stack() {
104 if (!__asan_option_detect_stack_use_after_return
)
106 if (atomic_load(&stack_switching_
, memory_order_relaxed
))
108 if (!has_fake_stack())
109 return AsyncSignalSafeLazyInitFakeStack();
113 // True is this thread is currently unwinding stack (i.e. collecting a stack
114 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
115 // malloc internally. See PR17116 for more details.
116 bool isUnwinding() const { return unwinding_
; }
117 void setUnwinding(bool b
) { unwinding_
= b
; }
119 // True if we are in a deadly signal handler.
120 bool isInDeadlySignal() const { return in_deadly_signal_
; }
121 void setInDeadlySignal(bool b
) { in_deadly_signal_
= b
; }
123 AsanThreadLocalMallocStorage
&malloc_storage() { return malloc_storage_
; }
124 AsanStats
&stats() { return stats_
; }
127 // NOTE: There is no AsanThread constructor. It is allocated
128 // via mmap() and *must* be valid in zero-initialized state.
129 void SetThreadStackAndTls();
130 void ClearShadowForThreadStackAndTLS();
131 FakeStack
*AsyncSignalSafeLazyInitFakeStack();
137 StackBounds
GetStackBounds() const;
139 AsanThreadContext
*context_
;
140 thread_callback_t start_routine_
;
145 // these variables are used when the thread is about to switch stack
146 uptr next_stack_top_
;
147 uptr next_stack_bottom_
;
148 // true if switching is in progress
149 atomic_uint8_t stack_switching_
;
155 FakeStack
*fake_stack_
;
156 AsanThreadLocalMallocStorage malloc_storage_
;
159 bool in_deadly_signal_
;
162 // ScopedUnwinding is a scope for stacktracing member of a context
163 class ScopedUnwinding
{
165 explicit ScopedUnwinding(AsanThread
*t
) : thread(t
) {
166 t
->setUnwinding(true);
168 ~ScopedUnwinding() { thread
->setUnwinding(false); }
174 // ScopedDeadlySignal is a scope for handling deadly signals.
175 class ScopedDeadlySignal
{
177 explicit ScopedDeadlySignal(AsanThread
*t
) : thread(t
) {
178 if (thread
) thread
->setInDeadlySignal(true);
180 ~ScopedDeadlySignal() {
181 if (thread
) thread
->setInDeadlySignal(false);
188 // Returns a single instance of registry.
189 ThreadRegistry
&asanThreadRegistry();
191 // Must be called under ThreadRegistryLock.
192 AsanThreadContext
*GetThreadContextByTidLocked(u32 tid
);
194 // Get the current thread. May return 0.
195 AsanThread
*GetCurrentThread();
196 void SetCurrentThread(AsanThread
*t
);
197 u32
GetCurrentTidOrInvalid();
198 AsanThread
*FindThreadByStackAddress(uptr addr
);
200 // Used to handle fork().
201 void EnsureMainThreadIDIsCorrect();
202 } // namespace __asan
204 #endif // ASAN_THREAD_H