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 //===----------------------------------------------------------------------===//
15 #include "asan_allocator.h"
16 #include "asan_internal.h"
17 #include "asan_fake_stack.h"
18 #include "asan_stats.h"
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "sanitizer_common/sanitizer_libc.h"
21 #include "sanitizer_common/sanitizer_thread_registry.h"
25 const u32 kInvalidTid
= 0xffffff; // Must fit into 24 bits.
26 const u32 kMaxNumberOfThreads
= (1 << 22); // 4M
30 // These objects are created for every thread and are never deleted,
31 // so we can find them by tid even if the thread is long dead.
32 class AsanThreadContext
: public ThreadContextBase
{
34 explicit AsanThreadContext(int tid
)
35 : ThreadContextBase(tid
),
37 destructor_iterations(kPthreadDestructorIterations
),
42 u8 destructor_iterations
;
46 void OnCreated(void *arg
);
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 static void TSDDtor(void *tsd
);
60 void Init(); // Should be called from the thread itself.
61 thread_return_t
ThreadStart(uptr os_id
);
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_
;
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 struct CreateThreadContextArgs
{
172 // Returns a single instance of registry.
173 ThreadRegistry
&asanThreadRegistry();
175 // Must be called under ThreadRegistryLock.
176 AsanThreadContext
*GetThreadContextByTidLocked(u32 tid
);
178 // Get the current thread. May return 0.
179 AsanThread
*GetCurrentThread();
180 void SetCurrentThread(AsanThread
*t
);
181 u32
GetCurrentTidOrInvalid();
182 AsanThread
*FindThreadByStackAddress(uptr addr
);
184 // Used to handle fork().
185 void EnsureMainThreadIDIsCorrect();
186 } // namespace __asan
188 #endif // ASAN_THREAD_H