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::Init(uptr stack_buffer_start, uptr stack_buffer_size) {
30 unique_id_ = unique_id++;
31 random_state_ = flags()->random_tags ? RandomSeed() : unique_id_;
32 if (auto sz = flags()->heap_history_size)
33 heap_allocations_ = HeapAllocationsRingBuffer::New(sz);
35 HwasanTSDThreadInit(); // Only needed with interceptors.
36 uptr *ThreadLong = GetCurrentThreadLongPtr();
37 // The following implicitly sets (this) as the current thread.
38 stack_allocations_ = new (ThreadLong)
39 StackAllocationsRingBuffer((void *)stack_buffer_start, stack_buffer_size);
40 // Check that it worked.
41 CHECK_EQ(GetCurrentThread(), this);
43 // ScopedTaggingDisable needs GetCurrentThread to be set up.
44 ScopedTaggingDisabler disabler;
48 GetThreadStackAndTls(IsMainThread(), &stack_bottom_, &stack_size, &tls_begin_,
50 stack_top_ = stack_bottom_ + stack_size;
51 tls_end_ = tls_begin_ + tls_size;
55 CHECK(AddrIsInStack((uptr)&local));
56 CHECK(MemIsApp(stack_bottom_));
57 CHECK(MemIsApp(stack_top_ - 1));
60 if (flags()->verbose_threads) {
62 Printf("sizeof(Thread): %zd sizeof(HeapRB): %zd sizeof(StackRB): %zd\n",
63 sizeof(Thread), heap_allocations_->SizeInBytes(),
64 stack_allocations_->size() * sizeof(uptr));
70 void Thread::ClearShadowForThreadStackAndTLS() {
71 if (stack_top_ != stack_bottom_)
72 TagMemory(stack_bottom_, stack_top_ - stack_bottom_, 0);
73 if (tls_begin_ != tls_end_)
74 TagMemory(tls_begin_, tls_end_ - tls_begin_, 0);
77 void Thread::Destroy() {
78 if (flags()->verbose_threads)
79 Print("Destroying: ");
80 AllocatorSwallowThreadLocalCache(allocator_cache());
81 ClearShadowForThreadStackAndTLS();
82 if (heap_allocations_)
83 heap_allocations_->Delete();
87 void Thread::Print(const char *Prefix) {
88 Printf("%sT%zd %p stack: [%p,%p) sz: %zd tls: [%p,%p)\n", Prefix,
89 unique_id_, this, stack_bottom(), stack_top(),
90 stack_top() - stack_bottom(),
91 tls_begin(), tls_end());
94 static u32 xorshift(u32 state) {
101 // Generate a (pseudo-)random non-zero tag.
102 tag_t Thread::GenerateRandomTag() {
103 if (tagging_disabled_) return 0;
106 if (flags()->random_tags) {
108 random_buffer_ = random_state_ = xorshift(random_state_);
109 CHECK(random_buffer_);
110 tag = random_buffer_ & 0xFF;
111 random_buffer_ >>= 8;
113 tag = random_state_ = (random_state_ + 1) & 0xFF;
119 } // namespace __hwasan