1 
2 #include "hwasan.h"
3 #include "hwasan_mapping.h"
4 #include "hwasan_thread.h"
5 #include "hwasan_poisoning.h"
6 #include "hwasan_interface_internal.h"
7 
8 #include "sanitizer_common/sanitizer_file.h"
9 #include "sanitizer_common/sanitizer_placement_new.h"
10 #include "sanitizer_common/sanitizer_tls_get_addr.h"
11 
12 
13 namespace __hwasan {
14 
15 static u32 RandomSeed() {
16   u32 seed;
17   do {
18     if (UNLIKELY(!GetRandom(reinterpret_cast<void *>(&seed), sizeof(seed),
19                             /*blocking=*/false))) {
20       seed = static_cast<u32>(
21           (NanoTime() >> 12) ^
22           (reinterpret_cast<uptr>(__builtin_frame_address(0)) >> 4));
23     }
24   } while (!seed);
25   return seed;
26 }
27 
28 void Thread::InitRandomState() {
29   random_state_ = flags()->random_tags ? RandomSeed() : unique_id_;
30 }
31 
32 void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size) {
33   static u64 unique_id;
34   unique_id_ = unique_id++;
35   if (auto sz = flags()->heap_history_size)
36     heap_allocations_ = HeapAllocationsRingBuffer::New(sz);
37 
38   HwasanTSDThreadInit();  // Only needed with interceptors.
39   uptr *ThreadLong = GetCurrentThreadLongPtr();
40   // The following implicitly sets (this) as the current thread.
41   stack_allocations_ = new (ThreadLong)
42       StackAllocationsRingBuffer((void *)stack_buffer_start, stack_buffer_size);
43   // Check that it worked.
44   CHECK_EQ(GetCurrentThread(), this);
45 
46   // ScopedTaggingDisable needs GetCurrentThread to be set up.
47   ScopedTaggingDisabler disabler;
48 
49   uptr tls_size;
50   uptr stack_size;
51   GetThreadStackAndTls(IsMainThread(), &stack_bottom_, &stack_size, &tls_begin_,
52                        &tls_size);
53   stack_top_ = stack_bottom_ + stack_size;
54   tls_end_ = tls_begin_ + tls_size;
55 
56   if (stack_bottom_) {
57     int local;
58     CHECK(AddrIsInStack((uptr)&local));
59     CHECK(MemIsApp(stack_bottom_));
60     CHECK(MemIsApp(stack_top_ - 1));
61   }
62 
63   if (flags()->verbose_threads) {
64     if (IsMainThread()) {
65       Printf("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("Creating  : ");
70   }
71 }
72 
73 void Thread::ClearShadowForThreadStackAndTLS() {
74   if (stack_top_ != stack_bottom_)
75     TagMemory(stack_bottom_, stack_top_ - stack_bottom_, 0);
76   if (tls_begin_ != tls_end_)
77     TagMemory(tls_begin_, tls_end_ - tls_begin_, 0);
78 }
79 
80 void Thread::Destroy() {
81   if (flags()->verbose_threads)
82     Print("Destroying: ");
83   AllocatorSwallowThreadLocalCache(allocator_cache());
84   ClearShadowForThreadStackAndTLS();
85   if (heap_allocations_)
86     heap_allocations_->Delete();
87   DTLS_Destroy();
88 }
89 
90 void Thread::Print(const char *Prefix) {
91   Printf("%sT%zd %p stack: [%p,%p) sz: %zd tls: [%p,%p)\n", Prefix,
92          unique_id_, this, stack_bottom(), stack_top(),
93          stack_top() - stack_bottom(),
94          tls_begin(), tls_end());
95 }
96 
97 static u32 xorshift(u32 state) {
98   state ^= state << 13;
99   state ^= state >> 17;
100   state ^= state << 5;
101   return state;
102 }
103 
104 // Generate a (pseudo-)random non-zero tag.
105 tag_t Thread::GenerateRandomTag() {
106   if (tagging_disabled_) return 0;
107   tag_t tag;
108   do {
109     if (flags()->random_tags) {
110       if (!random_buffer_)
111         random_buffer_ = random_state_ = xorshift(random_state_);
112       CHECK(random_buffer_);
113       tag = random_buffer_ & 0xFF;
114       random_buffer_ >>= 8;
115     } else {
116       tag = random_state_ = (random_state_ + 1) & 0xFF;
117     }
118   } while (!tag);
119   return tag;
120 }
121 
122 } // namespace __hwasan
123