3 #include "hwasan_mapping.h"
4 #include "hwasan_thread.h"
5 #include "hwasan_poisoning.h"
6 #include "hwasan_interface_internal.h"
8 #include "sanitizer_common/sanitizer_file.h"
9 #include "sanitizer_common/sanitizer_placement_new.h"
10 #include "sanitizer_common/sanitizer_tls_get_addr.h"
15 static u32 RandomSeed() {
18 if (UNLIKELY(!GetRandom(reinterpret_cast<void *>(&seed), sizeof(seed),
19 /*blocking=*/false))) {
20 seed = static_cast<u32>(
22 (reinterpret_cast<uptr>(__builtin_frame_address(0)) >> 4));
28 void Thread::InitRandomState() {
29 random_state_ = flags()->random_tags ? RandomSeed() : unique_id_;
31 // Push a random number of zeros onto the ring buffer so that the first stack
32 // tag base will be random.
33 for (tag_t i = 0, e = GenerateRandomTag(); i != e; ++i)
34 stack_allocations_->push(0);
37 void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size) {
39 unique_id_ = unique_id++;
40 if (auto sz = flags()->heap_history_size)
41 heap_allocations_ = HeapAllocationsRingBuffer::New(sz);
43 HwasanTSDThreadInit(); // Only needed with interceptors.
44 uptr *ThreadLong = GetCurrentThreadLongPtr();
45 // The following implicitly sets (this) as the current thread.
46 stack_allocations_ = new (ThreadLong)
47 StackAllocationsRingBuffer((void *)stack_buffer_start, stack_buffer_size);
48 // Check that it worked.
49 CHECK_EQ(GetCurrentThread(), this);
51 // ScopedTaggingDisable needs GetCurrentThread to be set up.
52 ScopedTaggingDisabler disabler;
56 GetThreadStackAndTls(IsMainThread(), &stack_bottom_, &stack_size, &tls_begin_,
58 stack_top_ = stack_bottom_ + stack_size;
59 tls_end_ = tls_begin_ + tls_size;
63 CHECK(AddrIsInStack((uptr)&local));
64 CHECK(MemIsApp(stack_bottom_));
65 CHECK(MemIsApp(stack_top_ - 1));
68 if (flags()->verbose_threads) {
70 Printf("sizeof(Thread): %zd sizeof(HeapRB): %zd sizeof(StackRB): %zd\n",
71 sizeof(Thread), heap_allocations_->SizeInBytes(),
72 stack_allocations_->size() * sizeof(uptr));
78 void Thread::ClearShadowForThreadStackAndTLS() {
79 if (stack_top_ != stack_bottom_)
80 TagMemory(stack_bottom_, stack_top_ - stack_bottom_, 0);
81 if (tls_begin_ != tls_end_)
82 TagMemory(tls_begin_, tls_end_ - tls_begin_, 0);
85 void Thread::Destroy() {
86 if (flags()->verbose_threads)
87 Print("Destroying: ");
88 AllocatorSwallowThreadLocalCache(allocator_cache());
89 ClearShadowForThreadStackAndTLS();
90 if (heap_allocations_)
91 heap_allocations_->Delete();
95 void Thread::Print(const char *Prefix) {
96 Printf("%sT%zd %p stack: [%p,%p) sz: %zd tls: [%p,%p)\n", Prefix,
97 unique_id_, this, stack_bottom(), stack_top(),
98 stack_top() - stack_bottom(),
99 tls_begin(), tls_end());
102 static u32 xorshift(u32 state) {
103 state ^= state << 13;
104 state ^= state >> 17;
109 // Generate a (pseudo-)random non-zero tag.
110 tag_t Thread::GenerateRandomTag() {
111 if (tagging_disabled_) return 0;
114 if (flags()->random_tags) {
116 random_buffer_ = random_state_ = xorshift(random_state_);
117 CHECK(random_buffer_);
118 tag = random_buffer_ & 0xFF;
119 random_buffer_ >>= 8;
121 tag = random_state_ = (random_state_ + 1) & 0xFF;
127 } // namespace __hwasan