Rebase.
[official-gcc.git] / libsanitizer / asan / asan_thread.h
blob33242efabaa26fd3cd54ed4ae0b5e1e37f813f96
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 //===----------------------------------------------------------------------===//
12 #ifndef ASAN_THREAD_H
13 #define ASAN_THREAD_H
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"
23 namespace __asan {
25 const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
26 const u32 kMaxNumberOfThreads = (1 << 22); // 4M
28 class AsanThread;
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 {
33 public:
34 explicit AsanThreadContext(int tid)
35 : ThreadContextBase(tid),
36 announced(false),
37 destructor_iterations(kPthreadDestructorIterations),
38 stack_id(0),
39 thread(0) {
41 bool announced;
42 u8 destructor_iterations;
43 u32 stack_id;
44 AsanThread *thread;
46 void OnCreated(void *arg);
47 void OnFinished();
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.
54 class AsanThread {
55 public:
56 static AsanThread *Create(thread_callback_t start_routine, void *arg);
57 static void TSDDtor(void *tsd);
58 void Destroy();
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_;
81 fake_stack_ = 0;
82 SetTLSFakeStack(0);
83 t->Destroy(tid);
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)
92 return 0;
93 if (!has_fake_stack())
94 return AsyncSignalSafeLazyInitFakeStack();
95 return fake_stack_;
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_; }
107 private:
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_;
116 void *arg_;
117 uptr stack_top_;
118 uptr stack_bottom_;
119 // stack_size_ == stack_top_ - stack_bottom_;
120 // It needs to be set in a async-signal-safe manner.
121 uptr stack_size_;
122 uptr tls_begin_;
123 uptr tls_end_;
125 FakeStack *fake_stack_;
126 AsanThreadLocalMallocStorage malloc_storage_;
127 AsanStats stats_;
128 bool unwinding_;
131 // ScopedUnwinding is a scope for stacktracing member of a context
132 class ScopedUnwinding {
133 public:
134 explicit ScopedUnwinding(AsanThread *t) : thread(t) {
135 t->setUnwinding(true);
137 ~ScopedUnwinding() { thread->setUnwinding(false); }
139 private:
140 AsanThread *thread;
143 struct CreateThreadContextArgs {
144 AsanThread *thread;
145 StackTrace *stack;
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