[clang] Handle __declspec() attributes in using
[llvm-project.git] / compiler-rt / lib / hwasan / hwasan_thread.cpp
blob3375782ef29b46bd289fde92792b8a975cb83dcb
2 #include "hwasan_thread.h"
4 #include "hwasan.h"
5 #include "hwasan_interface_internal.h"
6 #include "hwasan_mapping.h"
7 #include "hwasan_poisoning.h"
8 #include "hwasan_thread_list.h"
9 #include "sanitizer_common/sanitizer_atomic.h"
10 #include "sanitizer_common/sanitizer_file.h"
11 #include "sanitizer_common/sanitizer_placement_new.h"
12 #include "sanitizer_common/sanitizer_tls_get_addr.h"
14 namespace __hwasan {
16 static u32 RandomSeed() {
17 u32 seed;
18 do {
19 if (UNLIKELY(!GetRandom(reinterpret_cast<void *>(&seed), sizeof(seed),
20 /*blocking=*/false))) {
21 seed = static_cast<u32>(
22 (NanoTime() >> 12) ^
23 (reinterpret_cast<uptr>(__builtin_frame_address(0)) >> 4));
25 } while (!seed);
26 return seed;
29 void Thread::InitRandomState() {
30 random_state_ = flags()->random_tags ? RandomSeed() : unique_id_;
31 random_state_inited_ = true;
33 // Push a random number of zeros onto the ring buffer so that the first stack
34 // tag base will be random.
35 for (tag_t i = 0, e = GenerateRandomTag(); i != e; ++i)
36 stack_allocations_->push(0);
39 void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size,
40 const InitState *state) {
41 CHECK_EQ(0, unique_id_); // try to catch bad stack reuse
42 CHECK_EQ(0, stack_top_);
43 CHECK_EQ(0, stack_bottom_);
45 static atomic_uint64_t unique_id;
46 unique_id_ = atomic_fetch_add(&unique_id, 1, memory_order_relaxed);
47 if (!IsMainThread())
48 os_id_ = GetTid();
50 if (auto sz = flags()->heap_history_size)
51 heap_allocations_ = HeapAllocationsRingBuffer::New(sz);
53 #if !SANITIZER_FUCHSIA
54 // Do not initialize the stack ring buffer just yet on Fuchsia. Threads will
55 // be initialized before we enter the thread itself, so we will instead call
56 // this later.
57 InitStackRingBuffer(stack_buffer_start, stack_buffer_size);
58 #endif
59 InitStackAndTls(state);
60 dtls_ = DTLS_Get();
63 void Thread::InitStackRingBuffer(uptr stack_buffer_start,
64 uptr stack_buffer_size) {
65 HwasanTSDThreadInit(); // Only needed with interceptors.
66 uptr *ThreadLong = GetCurrentThreadLongPtr();
67 // The following implicitly sets (this) as the current thread.
68 stack_allocations_ = new (ThreadLong)
69 StackAllocationsRingBuffer((void *)stack_buffer_start, stack_buffer_size);
70 // Check that it worked.
71 CHECK_EQ(GetCurrentThread(), this);
73 // ScopedTaggingDisable needs GetCurrentThread to be set up.
74 ScopedTaggingDisabler disabler;
76 if (stack_bottom_) {
77 int local;
78 CHECK(AddrIsInStack((uptr)&local));
79 CHECK(MemIsApp(stack_bottom_));
80 CHECK(MemIsApp(stack_top_ - 1));
83 if (flags()->verbose_threads) {
84 if (IsMainThread()) {
85 Printf("sizeof(Thread): %zd sizeof(HeapRB): %zd sizeof(StackRB): %zd\n",
86 sizeof(Thread), heap_allocations_->SizeInBytes(),
87 stack_allocations_->size() * sizeof(uptr));
89 Print("Creating : ");
93 void Thread::ClearShadowForThreadStackAndTLS() {
94 if (stack_top_ != stack_bottom_)
95 TagMemory(stack_bottom_, stack_top_ - stack_bottom_, 0);
96 if (tls_begin_ != tls_end_)
97 TagMemory(tls_begin_, tls_end_ - tls_begin_, 0);
100 void Thread::Destroy() {
101 if (flags()->verbose_threads)
102 Print("Destroying: ");
103 AllocatorSwallowThreadLocalCache(allocator_cache());
104 ClearShadowForThreadStackAndTLS();
105 if (heap_allocations_)
106 heap_allocations_->Delete();
107 DTLS_Destroy();
108 // Unregister this as the current thread.
109 // Instrumented code can not run on this thread from this point onwards, but
110 // malloc/free can still be served. Glibc may call free() very late, after all
111 // TSD destructors are done.
112 CHECK_EQ(GetCurrentThread(), this);
113 *GetCurrentThreadLongPtr() = 0;
116 void Thread::Print(const char *Prefix) {
117 Printf("%sT%zd %p stack: [%p,%p) sz: %zd tls: [%p,%p)\n", Prefix, unique_id_,
118 (void *)this, stack_bottom(), stack_top(),
119 stack_top() - stack_bottom(), tls_begin(), tls_end());
122 static u32 xorshift(u32 state) {
123 state ^= state << 13;
124 state ^= state >> 17;
125 state ^= state << 5;
126 return state;
129 // Generate a (pseudo-)random non-zero tag.
130 tag_t Thread::GenerateRandomTag(uptr num_bits) {
131 DCHECK_GT(num_bits, 0);
132 if (tagging_disabled_)
133 return 0;
134 tag_t tag;
135 const uptr tag_mask = (1ULL << num_bits) - 1;
136 do {
137 if (flags()->random_tags) {
138 if (!random_buffer_) {
139 EnsureRandomStateInited();
140 random_buffer_ = random_state_ = xorshift(random_state_);
142 CHECK(random_buffer_);
143 tag = random_buffer_ & tag_mask;
144 random_buffer_ >>= num_bits;
145 } else {
146 EnsureRandomStateInited();
147 random_state_ += 1;
148 tag = random_state_ & tag_mask;
150 } while (!tag);
151 return tag;
154 void EnsureMainThreadIDIsCorrect() {
155 auto *t = __hwasan::GetCurrentThread();
156 if (t && (t->IsMainThread()))
157 t->set_os_id(GetTid());
160 } // namespace __hwasan
162 // --- Implementation of LSan-specific functions --- {{{1
163 namespace __lsan {
165 static __hwasan::HwasanThreadList *GetHwasanThreadListLocked() {
166 auto &tl = __hwasan::hwasanThreadList();
167 tl.CheckLocked();
168 return &tl;
171 static __hwasan::Thread *GetThreadByOsIDLocked(tid_t os_id) {
172 return GetHwasanThreadListLocked()->FindThreadLocked(
173 [os_id](__hwasan::Thread *t) { return t->os_id() == os_id; });
176 void LockThreadRegistry() { __hwasan::hwasanThreadList().Lock(); }
178 void UnlockThreadRegistry() { __hwasan::hwasanThreadList().Unlock(); }
180 void EnsureMainThreadIDIsCorrect() { __hwasan::EnsureMainThreadIDIsCorrect(); }
182 bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
183 uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
184 uptr *cache_end, DTLS **dtls) {
185 auto *t = GetThreadByOsIDLocked(os_id);
186 if (!t)
187 return false;
188 *stack_begin = t->stack_bottom();
189 *stack_end = t->stack_top();
190 *tls_begin = t->tls_begin();
191 *tls_end = t->tls_end();
192 // Fixme: is this correct for HWASan.
193 *cache_begin = 0;
194 *cache_end = 0;
195 *dtls = t->dtls();
196 return true;
199 void GetAllThreadAllocatorCachesLocked(InternalMmapVector<uptr> *caches) {}
201 void GetThreadExtraStackRangesLocked(tid_t os_id,
202 InternalMmapVector<Range> *ranges) {}
203 void GetThreadExtraStackRangesLocked(InternalMmapVector<Range> *ranges) {}
205 void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs) {}
206 void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {}
208 } // namespace __lsan