asan_thread.h revision 1.3 1 1.1 mrg //===-- asan_thread.h -------------------------------------------*- C++ -*-===//
2 1.1 mrg //
3 1.3 mrg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 1.3 mrg // See https://llvm.org/LICENSE.txt for license information.
5 1.3 mrg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 1.1 mrg //
7 1.1 mrg //===----------------------------------------------------------------------===//
8 1.1 mrg //
9 1.1 mrg // This file is a part of AddressSanitizer, an address sanity checker.
10 1.1 mrg //
11 1.3 mrg // ASan-private header for asan_thread.cpp.
12 1.1 mrg //===----------------------------------------------------------------------===//
13 1.2 mrg
14 1.1 mrg #ifndef ASAN_THREAD_H
15 1.1 mrg #define ASAN_THREAD_H
16 1.1 mrg
17 1.1 mrg #include "asan_allocator.h"
18 1.1 mrg #include "asan_internal.h"
19 1.2 mrg #include "asan_fake_stack.h"
20 1.1 mrg #include "asan_stats.h"
21 1.2 mrg #include "sanitizer_common/sanitizer_common.h"
22 1.1 mrg #include "sanitizer_common/sanitizer_libc.h"
23 1.2 mrg #include "sanitizer_common/sanitizer_thread_registry.h"
24 1.2 mrg
25 1.2 mrg namespace __sanitizer {
26 1.2 mrg struct DTLS;
27 1.2 mrg } // namespace __sanitizer
28 1.1 mrg
29 1.1 mrg namespace __asan {
30 1.1 mrg
31 1.1 mrg class AsanThread;
32 1.1 mrg
33 1.1 mrg // These objects are created for every thread and are never deleted,
34 1.1 mrg // so we can find them by tid even if the thread is long dead.
35 1.3 mrg class AsanThreadContext final : public ThreadContextBase {
36 1.1 mrg public:
37 1.2 mrg explicit AsanThreadContext(int tid)
38 1.2 mrg : ThreadContextBase(tid), announced(false),
39 1.2 mrg destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
40 1.2 mrg thread(nullptr) {}
41 1.2 mrg bool announced;
42 1.2 mrg u8 destructor_iterations;
43 1.2 mrg u32 stack_id;
44 1.2 mrg AsanThread *thread;
45 1.2 mrg
46 1.2 mrg void OnCreated(void *arg) override;
47 1.2 mrg void OnFinished() override;
48 1.2 mrg
49 1.2 mrg struct CreateThreadContextArgs {
50 1.2 mrg AsanThread *thread;
51 1.2 mrg StackTrace *stack;
52 1.2 mrg };
53 1.1 mrg };
54 1.1 mrg
55 1.2 mrg // AsanThreadContext objects are never freed, so we need many of them.
56 1.2 mrg COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
57 1.1 mrg
58 1.1 mrg // AsanThread are stored in TSD and destroyed when the thread dies.
59 1.1 mrg class AsanThread {
60 1.1 mrg public:
61 1.2 mrg static AsanThread *Create(thread_callback_t start_routine, void *arg,
62 1.2 mrg u32 parent_tid, StackTrace *stack, bool detached);
63 1.2 mrg static void TSDDtor(void *tsd);
64 1.1 mrg void Destroy();
65 1.1 mrg
66 1.2 mrg struct InitOptions;
67 1.2 mrg void Init(const InitOptions *options = nullptr);
68 1.2 mrg
69 1.3 mrg thread_return_t ThreadStart(tid_t os_id);
70 1.1 mrg
71 1.2 mrg uptr stack_top();
72 1.2 mrg uptr stack_bottom();
73 1.2 mrg uptr stack_size();
74 1.2 mrg uptr tls_begin() { return tls_begin_; }
75 1.2 mrg uptr tls_end() { return tls_end_; }
76 1.2 mrg DTLS *dtls() { return dtls_; }
77 1.2 mrg u32 tid() { return context_->tid; }
78 1.2 mrg AsanThreadContext *context() { return context_; }
79 1.2 mrg void set_context(AsanThreadContext *context) { context_ = context; }
80 1.2 mrg
81 1.2 mrg struct StackFrameAccess {
82 1.2 mrg uptr offset;
83 1.2 mrg uptr frame_pc;
84 1.2 mrg const char *frame_descr;
85 1.2 mrg };
86 1.2 mrg bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
87 1.2 mrg
88 1.2 mrg // Returns a pointer to the start of the stack variable's shadow memory.
89 1.2 mrg uptr GetStackVariableShadowStart(uptr addr);
90 1.2 mrg
91 1.2 mrg bool AddrIsInStack(uptr addr);
92 1.2 mrg
93 1.2 mrg void DeleteFakeStack(int tid) {
94 1.2 mrg if (!fake_stack_) return;
95 1.2 mrg FakeStack *t = fake_stack_;
96 1.2 mrg fake_stack_ = nullptr;
97 1.2 mrg SetTLSFakeStack(nullptr);
98 1.2 mrg t->Destroy(tid);
99 1.2 mrg }
100 1.1 mrg
101 1.2 mrg void StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size);
102 1.2 mrg void FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old,
103 1.2 mrg uptr *size_old);
104 1.2 mrg
105 1.3 mrg FakeStack *get_fake_stack() {
106 1.3 mrg if (atomic_load(&stack_switching_, memory_order_relaxed))
107 1.3 mrg return nullptr;
108 1.3 mrg if (reinterpret_cast<uptr>(fake_stack_) <= 1)
109 1.3 mrg return nullptr;
110 1.3 mrg return fake_stack_;
111 1.2 mrg }
112 1.1 mrg
113 1.3 mrg FakeStack *get_or_create_fake_stack() {
114 1.2 mrg if (atomic_load(&stack_switching_, memory_order_relaxed))
115 1.2 mrg return nullptr;
116 1.3 mrg if (reinterpret_cast<uptr>(fake_stack_) <= 1)
117 1.2 mrg return AsyncSignalSafeLazyInitFakeStack();
118 1.2 mrg return fake_stack_;
119 1.1 mrg }
120 1.1 mrg
121 1.2 mrg // True is this thread is currently unwinding stack (i.e. collecting a stack
122 1.2 mrg // trace). Used to prevent deadlocks on platforms where libc unwinder calls
123 1.2 mrg // malloc internally. See PR17116 for more details.
124 1.2 mrg bool isUnwinding() const { return unwinding_; }
125 1.2 mrg void setUnwinding(bool b) { unwinding_ = b; }
126 1.2 mrg
127 1.1 mrg AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
128 1.1 mrg AsanStats &stats() { return stats_; }
129 1.1 mrg
130 1.3 mrg void *extra_spill_area() { return &extra_spill_area_; }
131 1.3 mrg
132 1.3 mrg void *get_arg() { return arg_; }
133 1.3 mrg
134 1.1 mrg private:
135 1.2 mrg // NOTE: There is no AsanThread constructor. It is allocated
136 1.2 mrg // via mmap() and *must* be valid in zero-initialized state.
137 1.2 mrg
138 1.2 mrg void SetThreadStackAndTls(const InitOptions *options);
139 1.2 mrg
140 1.2 mrg void ClearShadowForThreadStackAndTLS();
141 1.2 mrg FakeStack *AsyncSignalSafeLazyInitFakeStack();
142 1.2 mrg
143 1.2 mrg struct StackBounds {
144 1.2 mrg uptr bottom;
145 1.2 mrg uptr top;
146 1.2 mrg };
147 1.2 mrg StackBounds GetStackBounds() const;
148 1.2 mrg
149 1.2 mrg AsanThreadContext *context_;
150 1.1 mrg thread_callback_t start_routine_;
151 1.1 mrg void *arg_;
152 1.1 mrg
153 1.2 mrg uptr stack_top_;
154 1.2 mrg uptr stack_bottom_;
155 1.2 mrg // these variables are used when the thread is about to switch stack
156 1.2 mrg uptr next_stack_top_;
157 1.2 mrg uptr next_stack_bottom_;
158 1.2 mrg // true if switching is in progress
159 1.2 mrg atomic_uint8_t stack_switching_;
160 1.2 mrg
161 1.2 mrg uptr tls_begin_;
162 1.2 mrg uptr tls_end_;
163 1.2 mrg DTLS *dtls_;
164 1.2 mrg
165 1.2 mrg FakeStack *fake_stack_;
166 1.1 mrg AsanThreadLocalMallocStorage malloc_storage_;
167 1.1 mrg AsanStats stats_;
168 1.2 mrg bool unwinding_;
169 1.3 mrg uptr extra_spill_area_;
170 1.1 mrg };
171 1.1 mrg
172 1.2 mrg // Returns a single instance of registry.
173 1.2 mrg ThreadRegistry &asanThreadRegistry();
174 1.2 mrg
175 1.2 mrg // Must be called under ThreadRegistryLock.
176 1.2 mrg AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
177 1.2 mrg
178 1.2 mrg // Get the current thread. May return 0.
179 1.2 mrg AsanThread *GetCurrentThread();
180 1.2 mrg void SetCurrentThread(AsanThread *t);
181 1.2 mrg u32 GetCurrentTidOrInvalid();
182 1.2 mrg AsanThread *FindThreadByStackAddress(uptr addr);
183 1.2 mrg
184 1.2 mrg // Used to handle fork().
185 1.2 mrg void EnsureMainThreadIDIsCorrect();
186 1.2 mrg } // namespace __asan
187 1.1 mrg
188 1.2 mrg #endif // ASAN_THREAD_H
189