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"
26 const u32 kInvalidTid
= 0xffffff; // Must fit into 24 bits.
27 const u32 kMaxNumberOfThreads
= (1 << 22); // 4M
31 // These objects are created for every thread and are never deleted,
32 // so we can find them by tid even if the thread is long dead.
33 class AsanThreadContext
: public ThreadContextBase
{
35 explicit AsanThreadContext(int tid
)
36 : ThreadContextBase(tid
), announced(false),
37 destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
40 u8 destructor_iterations
;
44 void OnCreated(void *arg
) override
;
45 void OnFinished() override
;
48 // AsanThreadContext objects are never freed, so we need many of them.
49 COMPILER_CHECK(sizeof(AsanThreadContext
) <= 256);
51 // AsanThread are stored in TSD and destroyed when the thread dies.
54 static AsanThread
*Create(thread_callback_t start_routine
, void *arg
,
55 u32 parent_tid
, StackTrace
*stack
, bool detached
);
56 static void TSDDtor(void *tsd
);
59 void Init(); // Should be called from the thread itself.
60 thread_return_t
ThreadStart(uptr os_id
,
61 atomic_uintptr_t
*signal_thread_is_registered
);
63 uptr
stack_top() { return stack_top_
; }
64 uptr
stack_bottom() { return stack_bottom_
; }
65 uptr
stack_size() { return stack_size_
; }
66 uptr
tls_begin() { return tls_begin_
; }
67 uptr
tls_end() { return tls_end_
; }
68 u32
tid() { return context_
->tid
; }
69 AsanThreadContext
*context() { return context_
; }
70 void set_context(AsanThreadContext
*context
) { context_
= context
; }
72 struct StackFrameAccess
{
75 const char *frame_descr
;
77 bool GetStackFrameAccessByAddr(uptr addr
, StackFrameAccess
*access
);
79 bool AddrIsInStack(uptr addr
) {
80 return addr
>= stack_bottom_
&& addr
< stack_top_
;
83 void DeleteFakeStack(int tid
) {
84 if (!fake_stack_
) return;
85 FakeStack
*t
= fake_stack_
;
86 fake_stack_
= nullptr;
87 SetTLSFakeStack(nullptr);
91 bool has_fake_stack() {
92 return (reinterpret_cast<uptr
>(fake_stack_
) > 1);
95 FakeStack
*fake_stack() {
96 if (!__asan_option_detect_stack_use_after_return
)
98 if (!has_fake_stack())
99 return AsyncSignalSafeLazyInitFakeStack();
103 // True is this thread is currently unwinding stack (i.e. collecting a stack
104 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
105 // malloc internally. See PR17116 for more details.
106 bool isUnwinding() const { return unwinding_
; }
107 void setUnwinding(bool b
) { unwinding_
= b
; }
109 // True if we are in a deadly signal handler.
110 bool isInDeadlySignal() const { return in_deadly_signal_
; }
111 void setInDeadlySignal(bool b
) { in_deadly_signal_
= b
; }
113 AsanThreadLocalMallocStorage
&malloc_storage() { return malloc_storage_
; }
114 AsanStats
&stats() { return stats_
; }
117 // NOTE: There is no AsanThread constructor. It is allocated
118 // via mmap() and *must* be valid in zero-initialized state.
119 void SetThreadStackAndTls();
120 void ClearShadowForThreadStackAndTLS();
121 FakeStack
*AsyncSignalSafeLazyInitFakeStack();
123 AsanThreadContext
*context_
;
124 thread_callback_t start_routine_
;
128 // stack_size_ == stack_top_ - stack_bottom_;
129 // It needs to be set in a async-signal-safe manner.
134 FakeStack
*fake_stack_
;
135 AsanThreadLocalMallocStorage malloc_storage_
;
138 bool in_deadly_signal_
;
141 // ScopedUnwinding is a scope for stacktracing member of a context
142 class ScopedUnwinding
{
144 explicit ScopedUnwinding(AsanThread
*t
) : thread(t
) {
145 t
->setUnwinding(true);
147 ~ScopedUnwinding() { thread
->setUnwinding(false); }
153 // ScopedDeadlySignal is a scope for handling deadly signals.
154 class ScopedDeadlySignal
{
156 explicit ScopedDeadlySignal(AsanThread
*t
) : thread(t
) {
157 if (thread
) thread
->setInDeadlySignal(true);
159 ~ScopedDeadlySignal() {
160 if (thread
) thread
->setInDeadlySignal(false);
167 // Returns a single instance of registry.
168 ThreadRegistry
&asanThreadRegistry();
170 // Must be called under ThreadRegistryLock.
171 AsanThreadContext
*GetThreadContextByTidLocked(u32 tid
);
173 // Get the current thread. May return 0.
174 AsanThread
*GetCurrentThread();
175 void SetCurrentThread(AsanThread
*t
);
176 u32
GetCurrentTidOrInvalid();
177 AsanThread
*FindThreadByStackAddress(uptr addr
);
179 // Used to handle fork().
180 void EnsureMainThreadIDIsCorrect();
181 } // namespace __asan
183 #endif // ASAN_THREAD_H