1 //===-- asan_thread.h -------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // ASan-private header for asan_thread.cpp.
12 //===----------------------------------------------------------------------===//
17 #include "asan_allocator.h"
18 #include "asan_internal.h"
19 #include "asan_fake_stack.h"
20 #include "asan_stats.h"
21 #include "sanitizer_common/sanitizer_common.h"
22 #include "sanitizer_common/sanitizer_libc.h"
23 #include "sanitizer_common/sanitizer_thread_registry.h"
25 namespace __sanitizer
{
27 } // namespace __sanitizer
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 final
: 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
);
76 uptr
tls_begin() { return tls_begin_
; }
77 uptr
tls_end() { return tls_end_
; }
78 DTLS
*dtls() { return dtls_
; }
79 u32
tid() { return context_
->tid
; }
80 AsanThreadContext
*context() { return context_
; }
81 void set_context(AsanThreadContext
*context
) { context_
= context
; }
83 struct StackFrameAccess
{
86 const char *frame_descr
;
88 bool GetStackFrameAccessByAddr(uptr addr
, StackFrameAccess
*access
);
90 // Returns a pointer to the start of the stack variable's shadow memory.
91 uptr
GetStackVariableShadowStart(uptr addr
);
93 bool AddrIsInStack(uptr addr
);
95 void DeleteFakeStack(int tid
) {
96 if (!fake_stack_
) return;
97 FakeStack
*t
= fake_stack_
;
98 fake_stack_
= nullptr;
99 SetTLSFakeStack(nullptr);
103 void StartSwitchFiber(FakeStack
**fake_stack_save
, uptr bottom
, uptr size
);
104 void FinishSwitchFiber(FakeStack
*fake_stack_save
, uptr
*bottom_old
,
107 bool has_fake_stack() {
108 return !atomic_load(&stack_switching_
, memory_order_relaxed
) &&
109 (reinterpret_cast<uptr
>(fake_stack_
) > 1);
112 FakeStack
*fake_stack() {
113 if (!__asan_option_detect_stack_use_after_return
)
115 if (atomic_load(&stack_switching_
, memory_order_relaxed
))
117 if (!has_fake_stack())
118 return AsyncSignalSafeLazyInitFakeStack();
122 // True is this thread is currently unwinding stack (i.e. collecting a stack
123 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
124 // malloc internally. See PR17116 for more details.
125 bool isUnwinding() const { return unwinding_
; }
126 void setUnwinding(bool b
) { unwinding_
= b
; }
128 AsanThreadLocalMallocStorage
&malloc_storage() { return malloc_storage_
; }
129 AsanStats
&stats() { return stats_
; }
131 void *extra_spill_area() { return &extra_spill_area_
; }
133 void *get_arg() { return arg_
; }
136 // NOTE: There is no AsanThread constructor. It is allocated
137 // via mmap() and *must* be valid in zero-initialized state.
139 void SetThreadStackAndTls(const InitOptions
*options
);
141 void ClearShadowForThreadStackAndTLS();
142 FakeStack
*AsyncSignalSafeLazyInitFakeStack();
148 StackBounds
GetStackBounds() const;
150 AsanThreadContext
*context_
;
151 thread_callback_t start_routine_
;
156 // these variables are used when the thread is about to switch stack
157 uptr next_stack_top_
;
158 uptr next_stack_bottom_
;
159 // true if switching is in progress
160 atomic_uint8_t stack_switching_
;
166 FakeStack
*fake_stack_
;
167 AsanThreadLocalMallocStorage malloc_storage_
;
170 uptr extra_spill_area_
;
173 // Returns a single instance of registry.
174 ThreadRegistry
&asanThreadRegistry();
176 // Must be called under ThreadRegistryLock.
177 AsanThreadContext
*GetThreadContextByTidLocked(u32 tid
);
179 // Get the current thread. May return 0.
180 AsanThread
*GetCurrentThread();
181 void SetCurrentThread(AsanThread
*t
);
182 u32
GetCurrentTidOrInvalid();
183 AsanThread
*FindThreadByStackAddress(uptr addr
);
185 // Used to handle fork().
186 void EnsureMainThreadIDIsCorrect();
187 } // namespace __asan
189 #endif // ASAN_THREAD_H