rs6000: New attributes for load/store: "sign_extend", "update" and "indexed"
[official-gcc.git] / libsanitizer / asan / asan_thread.h
blob5a917fa9a3da03d9e5ab1277c03e3282645d0586
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_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 __asan {
26 const u32 kInvalidTid = 0xffffff; // Must fit into 24 bits.
27 const u32 kMaxNumberOfThreads = (1 << 22); // 4M
29 class AsanThread;
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 {
34 public:
35 explicit AsanThreadContext(int tid)
36 : ThreadContextBase(tid),
37 announced(false),
38 destructor_iterations(kPthreadDestructorIterations),
39 stack_id(0),
40 thread(0) {
42 bool announced;
43 u8 destructor_iterations;
44 u32 stack_id;
45 AsanThread *thread;
47 void OnCreated(void *arg);
48 void OnFinished();
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.
55 class AsanThread {
56 public:
57 static AsanThread *Create(thread_callback_t start_routine, void *arg);
58 static void TSDDtor(void *tsd);
59 void Destroy();
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_;
82 fake_stack_ = 0;
83 SetTLSFakeStack(0);
84 t->Destroy();
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)
93 return 0;
94 if (!has_fake_stack())
95 return AsyncSignalSafeLazyInitFakeStack();
96 return fake_stack_;
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_; }
108 private:
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_;
117 void *arg_;
118 uptr stack_top_;
119 uptr stack_bottom_;
120 // stack_size_ == stack_top_ - stack_bottom_;
121 // It needs to be set in a async-signal-safe manner.
122 uptr stack_size_;
123 uptr tls_begin_;
124 uptr tls_end_;
126 FakeStack *fake_stack_;
127 AsanThreadLocalMallocStorage malloc_storage_;
128 AsanStats stats_;
129 bool unwinding_;
132 // ScopedUnwinding is a scope for stacktracing member of a context
133 class ScopedUnwinding {
134 public:
135 explicit ScopedUnwinding(AsanThread *t) : thread(t) {
136 t->setUnwinding(true);
138 ~ScopedUnwinding() { thread->setUnwinding(false); }
140 private:
141 AsanThread *thread;
144 struct CreateThreadContextArgs {
145 AsanThread *thread;
146 StackTrace *stack;
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