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_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
),
38 destructor_iterations(kPthreadDestructorIterations
),
43 u8 destructor_iterations
;
47 void OnCreated(void *arg
);
51 // AsanThreadContext objects are never freed, so we need many of them.
52 COMPILER_CHECK(sizeof(AsanThreadContext
) <= 256);
54 // AsanThread are stored in TSD and destroyed when the thread dies.
57 static AsanThread
*Create(thread_callback_t start_routine
, void *arg
);
58 static void TSDDtor(void *tsd
);
61 void Init(); // Should be called from the thread itself.
62 thread_return_t
ThreadStart(uptr os_id
);
64 uptr
stack_top() { return stack_top_
; }
65 uptr
stack_bottom() { return stack_bottom_
; }
66 uptr
stack_size() { return stack_size_
; }
67 uptr
tls_begin() { return tls_begin_
; }
68 uptr
tls_end() { return tls_end_
; }
69 u32
tid() { return context_
->tid
; }
70 AsanThreadContext
*context() { return context_
; }
71 void set_context(AsanThreadContext
*context
) { context_
= context
; }
73 const char *GetFrameNameByAddr(uptr addr
, uptr
*offset
, uptr
*frame_pc
);
75 bool AddrIsInStack(uptr addr
) {
76 return addr
>= stack_bottom_
&& addr
< stack_top_
;
79 void DeleteFakeStack() {
80 if (!fake_stack_
) return;
81 FakeStack
*t
= fake_stack_
;
87 bool has_fake_stack() {
88 return (reinterpret_cast<uptr
>(fake_stack_
) > 1);
91 FakeStack
*fake_stack() {
92 if (!__asan_option_detect_stack_use_after_return
)
94 if (!has_fake_stack())
95 return AsyncSignalSafeLazyInitFakeStack();
99 // True is this thread is currently unwinding stack (i.e. collecting a stack
100 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
101 // malloc internally. See PR17116 for more details.
102 bool isUnwinding() const { return unwinding_
; }
103 void setUnwinding(bool b
) { unwinding_
= b
; }
105 AsanThreadLocalMallocStorage
&malloc_storage() { return malloc_storage_
; }
106 AsanStats
&stats() { return stats_
; }
109 // NOTE: There is no AsanThread constructor. It is allocated
110 // via mmap() and *must* be valid in zero-initialized state.
111 void SetThreadStackAndTls();
112 void ClearShadowForThreadStackAndTLS();
113 FakeStack
*AsyncSignalSafeLazyInitFakeStack();
115 AsanThreadContext
*context_
;
116 thread_callback_t start_routine_
;
120 // stack_size_ == stack_top_ - stack_bottom_;
121 // It needs to be set in a async-signal-safe manner.
126 FakeStack
*fake_stack_
;
127 AsanThreadLocalMallocStorage malloc_storage_
;
132 // ScopedUnwinding is a scope for stacktracing member of a context
133 class ScopedUnwinding
{
135 explicit ScopedUnwinding(AsanThread
*t
) : thread(t
) {
136 t
->setUnwinding(true);
138 ~ScopedUnwinding() { thread
->setUnwinding(false); }
144 struct CreateThreadContextArgs
{
149 // Returns a single instance of registry.
150 ThreadRegistry
&asanThreadRegistry();
152 // Must be called under ThreadRegistryLock.
153 AsanThreadContext
*GetThreadContextByTidLocked(u32 tid
);
155 // Get the current thread. May return 0.
156 AsanThread
*GetCurrentThread();
157 void SetCurrentThread(AsanThread
*t
);
158 u32
GetCurrentTidOrInvalid();
159 AsanThread
*FindThreadByStackAddress(uptr addr
);
161 // Used to handle fork().
162 void EnsureMainThreadIDIsCorrect();
163 } // namespace __asan
165 #endif // ASAN_THREAD_H