* doc/generic.texi (ANNOTATE_EXPR): Document 3rd operand.
[official-gcc.git] / libsanitizer / asan / asan_thread.h
blobf7a91f3e73b0a2413531b45abf1dcb874429aed4
1 //===-- asan_thread.h -------------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // ASan-private header for asan_thread.cc.
11 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_THREAD_H
14 #define ASAN_THREAD_H
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 {
25 struct DTLS;
26 } // namespace __sanitizer
28 namespace __asan {
30 const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
31 const u32 kMaxNumberOfThreads = (1 << 22); // 4M
33 class AsanThread;
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 {
38 public:
39 explicit AsanThreadContext(int tid)
40 : ThreadContextBase(tid), announced(false),
41 destructor_iterations(GetPthreadDestructorIterations()), stack_id(0),
42 thread(nullptr) {}
43 bool announced;
44 u8 destructor_iterations;
45 u32 stack_id;
46 AsanThread *thread;
48 void OnCreated(void *arg) override;
49 void OnFinished() override;
51 struct CreateThreadContextArgs {
52 AsanThread *thread;
53 StackTrace *stack;
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.
61 class AsanThread {
62 public:
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);
66 void Destroy();
68 struct InitOptions;
69 void Init(const InitOptions *options = nullptr);
71 thread_return_t ThreadStart(tid_t os_id,
72 atomic_uintptr_t *signal_thread_is_registered);
74 uptr stack_top();
75 uptr stack_bottom();
76 uptr stack_size();
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 {
85 uptr offset;
86 uptr frame_pc;
87 const char *frame_descr;
89 bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);
91 bool AddrIsInStack(uptr addr);
93 void DeleteFakeStack(int tid) {
94 if (!fake_stack_) return;
95 FakeStack *t = fake_stack_;
96 fake_stack_ = nullptr;
97 SetTLSFakeStack(nullptr);
98 t->Destroy(tid);
101 void StartSwitchFiber(FakeStack **fake_stack_save, uptr bottom, uptr size);
102 void FinishSwitchFiber(FakeStack *fake_stack_save, uptr *bottom_old,
103 uptr *size_old);
105 bool has_fake_stack() {
106 return !atomic_load(&stack_switching_, memory_order_relaxed) &&
107 (reinterpret_cast<uptr>(fake_stack_) > 1);
110 FakeStack *fake_stack() {
111 if (!__asan_option_detect_stack_use_after_return)
112 return nullptr;
113 if (atomic_load(&stack_switching_, memory_order_relaxed))
114 return nullptr;
115 if (!has_fake_stack())
116 return AsyncSignalSafeLazyInitFakeStack();
117 return fake_stack_;
120 // True is this thread is currently unwinding stack (i.e. collecting a stack
121 // trace). Used to prevent deadlocks on platforms where libc unwinder calls
122 // malloc internally. See PR17116 for more details.
123 bool isUnwinding() const { return unwinding_; }
124 void setUnwinding(bool b) { unwinding_ = b; }
126 AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
127 AsanStats &stats() { return stats_; }
129 private:
130 // NOTE: There is no AsanThread constructor. It is allocated
131 // via mmap() and *must* be valid in zero-initialized state.
133 void SetThreadStackAndTls(const InitOptions *options);
135 void ClearShadowForThreadStackAndTLS();
136 FakeStack *AsyncSignalSafeLazyInitFakeStack();
138 struct StackBounds {
139 uptr bottom;
140 uptr top;
142 StackBounds GetStackBounds() const;
144 AsanThreadContext *context_;
145 thread_callback_t start_routine_;
146 void *arg_;
148 uptr stack_top_;
149 uptr stack_bottom_;
150 // these variables are used when the thread is about to switch stack
151 uptr next_stack_top_;
152 uptr next_stack_bottom_;
153 // true if switching is in progress
154 atomic_uint8_t stack_switching_;
156 uptr tls_begin_;
157 uptr tls_end_;
158 DTLS *dtls_;
160 FakeStack *fake_stack_;
161 AsanThreadLocalMallocStorage malloc_storage_;
162 AsanStats stats_;
163 bool unwinding_;
166 // ScopedUnwinding is a scope for stacktracing member of a context
167 class ScopedUnwinding {
168 public:
169 explicit ScopedUnwinding(AsanThread *t) : thread(t) {
170 t->setUnwinding(true);
172 ~ScopedUnwinding() { thread->setUnwinding(false); }
174 private:
175 AsanThread *thread;
178 // Returns a single instance of registry.
179 ThreadRegistry &asanThreadRegistry();
181 // Must be called under ThreadRegistryLock.
182 AsanThreadContext *GetThreadContextByTidLocked(u32 tid);
184 // Get the current thread. May return 0.
185 AsanThread *GetCurrentThread();
186 void SetCurrentThread(AsanThread *t);
187 u32 GetCurrentTidOrInvalid();
188 AsanThread *FindThreadByStackAddress(uptr addr);
190 // Used to handle fork().
191 void EnsureMainThreadIDIsCorrect();
192 } // namespace __asan
194 #endif // ASAN_THREAD_H