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
;
51 struct CreateThreadContextArgs
{
57 // AsanThreadContext objects are never freed, so we need many of them.
58 COMPILER_CHECK(sizeof(AsanThreadContext
) <= 256);
60 // AsanThread are stored in TSD and destroyed when the thread dies.
63 static AsanThread
*Create(thread_callback_t start_routine
, void *arg
,
64 u32 parent_tid
, StackTrace
*stack
, bool detached
);
65 static void TSDDtor(void *tsd
);
69 void Init(const InitOptions
*options
= nullptr);
71 thread_return_t
ThreadStart(tid_t os_id
,
72 atomic_uintptr_t
*signal_thread_is_registered
);
77 uptr
tls_begin() { return tls_begin_
; }
78 uptr
tls_end() { return tls_end_
; }
79 DTLS
*dtls() { return dtls_
; }
80 u32
tid() { return context_
->tid
; }
81 AsanThreadContext
*context() { return context_
; }
82 void set_context(AsanThreadContext
*context
) { context_
= context
; }
84 struct StackFrameAccess
{
87 const char *frame_descr
;
89 bool GetStackFrameAccessByAddr(uptr addr
, StackFrameAccess
*access
);
91 // Returns a pointer to the start of the stack variable's shadow memory.
92 uptr
GetStackVariableShadowStart(uptr addr
);
94 bool AddrIsInStack(uptr addr
);
96 void DeleteFakeStack(int tid
) {
97 if (!fake_stack_
) return;
98 FakeStack
*t
= fake_stack_
;
99 fake_stack_
= nullptr;
100 SetTLSFakeStack(nullptr);
104 void StartSwitchFiber(FakeStack
**fake_stack_save
, uptr bottom
, uptr size
);
105 void FinishSwitchFiber(FakeStack
*fake_stack_save
, uptr
*bottom_old
,
108 bool has_fake_stack() {
109 return !atomic_load(&stack_switching_
, memory_order_relaxed
) &&
110 (reinterpret_cast<uptr
>(fake_stack_
) > 1);
113 FakeStack
*fake_stack() {
114 if (!__asan_option_detect_stack_use_after_return
)
116 if (atomic_load(&stack_switching_
, memory_order_relaxed
))
118 if (!has_fake_stack())
119 return AsyncSignalSafeLazyInitFakeStack();
123 // True is this thread is currently unwinding stack (i.e. collecting a stack
124 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
125 // malloc internally. See PR17116 for more details.
126 bool isUnwinding() const { return unwinding_
; }
127 void setUnwinding(bool b
) { unwinding_
= b
; }
129 AsanThreadLocalMallocStorage
&malloc_storage() { return malloc_storage_
; }
130 AsanStats
&stats() { return stats_
; }
133 // NOTE: There is no AsanThread constructor. It is allocated
134 // via mmap() and *must* be valid in zero-initialized state.
136 void SetThreadStackAndTls(const InitOptions
*options
);
138 void ClearShadowForThreadStackAndTLS();
139 FakeStack
*AsyncSignalSafeLazyInitFakeStack();
145 StackBounds
GetStackBounds() const;
147 AsanThreadContext
*context_
;
148 thread_callback_t start_routine_
;
153 // these variables are used when the thread is about to switch stack
154 uptr next_stack_top_
;
155 uptr next_stack_bottom_
;
156 // true if switching is in progress
157 atomic_uint8_t stack_switching_
;
163 FakeStack
*fake_stack_
;
164 AsanThreadLocalMallocStorage malloc_storage_
;
169 // ScopedUnwinding is a scope for stacktracing member of a context
170 class ScopedUnwinding
{
172 explicit ScopedUnwinding(AsanThread
*t
) : thread(t
) {
173 t
->setUnwinding(true);
175 ~ScopedUnwinding() { thread
->setUnwinding(false); }
181 // Returns a single instance of registry.
182 ThreadRegistry
&asanThreadRegistry();
184 // Must be called under ThreadRegistryLock.
185 AsanThreadContext
*GetThreadContextByTidLocked(u32 tid
);
187 // Get the current thread. May return 0.
188 AsanThread
*GetCurrentThread();
189 void SetCurrentThread(AsanThread
*t
);
190 u32
GetCurrentTidOrInvalid();
191 AsanThread
*FindThreadByStackAddress(uptr addr
);
193 // Used to handle fork().
194 void EnsureMainThreadIDIsCorrect();
195 } // namespace __asan
197 #endif // ASAN_THREAD_H