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 const char *GetFrameNameByAddr(uptr addr
, uptr
*offset
, uptr
*frame_pc
);
74 bool AddrIsInStack(uptr addr
) {
75 return addr
>= stack_bottom_
&& addr
< stack_top_
;
78 void DeleteFakeStack(int tid
) {
79 if (!fake_stack_
) return;
80 FakeStack
*t
= fake_stack_
;
86 bool has_fake_stack() {
87 return (reinterpret_cast<uptr
>(fake_stack_
) > 1);
90 FakeStack
*fake_stack() {
91 if (!__asan_option_detect_stack_use_after_return
)
93 if (!has_fake_stack())
94 return AsyncSignalSafeLazyInitFakeStack();
98 // True is this thread is currently unwinding stack (i.e. collecting a stack
99 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
100 // malloc internally. See PR17116 for more details.
101 bool isUnwinding() const { return unwinding_
; }
102 void setUnwinding(bool b
) { unwinding_
= b
; }
104 AsanThreadLocalMallocStorage
&malloc_storage() { return malloc_storage_
; }
105 AsanStats
&stats() { return stats_
; }
108 // NOTE: There is no AsanThread constructor. It is allocated
109 // via mmap() and *must* be valid in zero-initialized state.
110 void SetThreadStackAndTls();
111 void ClearShadowForThreadStackAndTLS();
112 FakeStack
*AsyncSignalSafeLazyInitFakeStack();
114 AsanThreadContext
*context_
;
115 thread_callback_t start_routine_
;
119 // stack_size_ == stack_top_ - stack_bottom_;
120 // It needs to be set in a async-signal-safe manner.
125 FakeStack
*fake_stack_
;
126 AsanThreadLocalMallocStorage malloc_storage_
;
131 // ScopedUnwinding is a scope for stacktracing member of a context
132 class ScopedUnwinding
{
134 explicit ScopedUnwinding(AsanThread
*t
) : thread(t
) {
135 t
->setUnwinding(true);
137 ~ScopedUnwinding() { thread
->setUnwinding(false); }
143 struct CreateThreadContextArgs
{
148 // Returns a single instance of registry.
149 ThreadRegistry
&asanThreadRegistry();
151 // Must be called under ThreadRegistryLock.
152 AsanThreadContext
*GetThreadContextByTidLocked(u32 tid
);
154 // Get the current thread. May return 0.
155 AsanThread
*GetCurrentThread();
156 void SetCurrentThread(AsanThread
*t
);
157 u32
GetCurrentTidOrInvalid();
158 AsanThread
*FindThreadByStackAddress(uptr addr
);
160 // Used to handle fork().
161 void EnsureMainThreadIDIsCorrect();
162 } // namespace __asan
164 #endif // ASAN_THREAD_H