1
2#include "hwasan_thread.h"
3
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"
13
14namespace __hwasan {
15
16static 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));
24 }
25 } while (!seed);
26 return seed;
27}
28
29void Thread::InitRandomState() {
30 random_state_ = flags()->random_tags ? RandomSeed() : unique_id_;
31 random_state_inited_ = true;
32
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(t: 0);
37}
38
39void 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_);
44
45 static atomic_uint64_t unique_id;
46 unique_id_ = atomic_fetch_add(a: &unique_id, v: 1, mo: memory_order_relaxed);
47 if (!IsMainThread())
48 os_id_ = GetTid();
49
50 if (auto sz = flags()->heap_history_size)
51 heap_allocations_ = HeapAllocationsRingBuffer::New(Size: sz);
52
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();
61 AllocatorThreadStart(cache: allocator_cache());
62
63 if (flags()->verbose_threads) {
64 if (IsMainThread()) {
65 Printf(format: "sizeof(Thread): %zd sizeof(HeapRB): %zd sizeof(StackRB): %zd\n",
66 sizeof(Thread), heap_allocations_->SizeInBytes(),
67 stack_allocations_->size() * sizeof(uptr));
68 }
69 Print(prefix: "Creating : ");
70 }
71 ClearShadowForThreadStackAndTLS();
72}
73
74void Thread::InitStackRingBuffer(uptr stack_buffer_start,
75 uptr stack_buffer_size) {
76 HwasanTSDThreadInit(); // Only needed with interceptors.
77 uptr *ThreadLong = GetCurrentThreadLongPtr();
78 // The following implicitly sets (this) as the current thread.
79 stack_allocations_ = new (ThreadLong)
80 StackAllocationsRingBuffer((void *)stack_buffer_start, stack_buffer_size);
81 // Check that it worked.
82 CHECK_EQ(GetCurrentThread(), this);
83
84 // ScopedTaggingDisable needs GetCurrentThread to be set up.
85 ScopedTaggingDisabler disabler;
86
87 if (stack_bottom_) {
88 int local;
89 CHECK(AddrIsInStack((uptr)&local));
90 CHECK(MemIsApp(stack_bottom_));
91 CHECK(MemIsApp(stack_top_ - 1));
92 }
93}
94
95void Thread::ClearShadowForThreadStackAndTLS() {
96 if (stack_top_ != stack_bottom_)
97 TagMemory(p: UntagAddr(tagged_addr: stack_bottom_),
98 size: UntagAddr(tagged_addr: stack_top_) - UntagAddr(tagged_addr: stack_bottom_),
99 tag: GetTagFromPointer(p: stack_top_));
100 if (tls_begin_ != tls_end_)
101 TagMemory(p: UntagAddr(tagged_addr: tls_begin_),
102 size: UntagAddr(tagged_addr: tls_end_) - UntagAddr(tagged_addr: tls_begin_),
103 tag: GetTagFromPointer(p: tls_begin_));
104}
105
106void Thread::Destroy() {
107 if (flags()->verbose_threads)
108 Print(prefix: "Destroying: ");
109 AllocatorThreadFinish(cache: allocator_cache());
110 ClearShadowForThreadStackAndTLS();
111 if (heap_allocations_)
112 heap_allocations_->Delete();
113 DTLS_Destroy();
114 // Unregister this as the current thread.
115 // Instrumented code can not run on this thread from this point onwards, but
116 // malloc/free can still be served. Glibc may call free() very late, after all
117 // TSD destructors are done.
118 CHECK_EQ(GetCurrentThread(), this);
119 *GetCurrentThreadLongPtr() = 0;
120}
121
122void Thread::Print(const char *Prefix) {
123 Printf(format: "%sT%zd %p stack: [%p,%p) sz: %zd tls: [%p,%p)\n", Prefix, unique_id_,
124 (void *)this, stack_bottom(), stack_top(),
125 stack_top() - stack_bottom(), tls_begin(), tls_end());
126}
127
128static u32 xorshift(u32 state) {
129 state ^= state << 13;
130 state ^= state >> 17;
131 state ^= state << 5;
132 return state;
133}
134
135// Generate a (pseudo-)random non-zero tag.
136tag_t Thread::GenerateRandomTag(uptr num_bits) {
137 DCHECK_GT(num_bits, 0);
138 if (tagging_disabled_)
139 return 0;
140 tag_t tag;
141 const uptr tag_mask = (1ULL << num_bits) - 1;
142 do {
143 if (flags()->random_tags) {
144 if (!random_buffer_) {
145 EnsureRandomStateInited();
146 random_buffer_ = random_state_ = xorshift(state: random_state_);
147 }
148 CHECK(random_buffer_);
149 tag = random_buffer_ & tag_mask;
150 random_buffer_ >>= num_bits;
151 } else {
152 EnsureRandomStateInited();
153 random_state_ += 1;
154 tag = random_state_ & tag_mask;
155 }
156 } while (!tag);
157 return tag;
158}
159
160void EnsureMainThreadIDIsCorrect() {
161 auto *t = __hwasan::GetCurrentThread();
162 if (t && (t->IsMainThread()))
163 t->set_os_id(GetTid());
164}
165
166} // namespace __hwasan
167
168// --- Implementation of LSan-specific functions --- {{{1
169namespace __lsan {
170
171static __hwasan::HwasanThreadList *GetHwasanThreadListLocked() {
172 auto &tl = __hwasan::hwasanThreadList();
173 tl.CheckLocked();
174 return &tl;
175}
176
177static __hwasan::Thread *GetThreadByOsIDLocked(tid_t os_id) {
178 return GetHwasanThreadListLocked()->FindThreadLocked(
179 cb: [os_id](__hwasan::Thread *t) { return t->os_id() == os_id; });
180}
181
182void LockThreads() {
183 __hwasan::hwasanThreadList().Lock();
184 __hwasan::hwasanThreadArgRetval().Lock();
185}
186
187void UnlockThreads() {
188 __hwasan::hwasanThreadArgRetval().Unlock();
189 __hwasan::hwasanThreadList().Unlock();
190}
191
192void EnsureMainThreadIDIsCorrect() { __hwasan::EnsureMainThreadIDIsCorrect(); }
193
194bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
195 uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
196 uptr *cache_end, DTLS **dtls) {
197 auto *t = GetThreadByOsIDLocked(os_id);
198 if (!t)
199 return false;
200 *stack_begin = t->stack_bottom();
201 *stack_end = t->stack_top();
202 *tls_begin = t->tls_begin();
203 *tls_end = t->tls_end();
204 // Fixme: is this correct for HWASan.
205 *cache_begin = 0;
206 *cache_end = 0;
207 *dtls = t->dtls();
208 return true;
209}
210
211void GetAllThreadAllocatorCachesLocked(InternalMmapVector<uptr> *caches) {}
212
213void GetThreadExtraStackRangesLocked(tid_t os_id,
214 InternalMmapVector<Range> *ranges) {}
215void GetThreadExtraStackRangesLocked(InternalMmapVector<Range> *ranges) {}
216
217void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs) {
218 __hwasan::hwasanThreadArgRetval().GetAllPtrsLocked(ptrs);
219}
220
221void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {}
222
223} // namespace __lsan
224