xref: /freebsd/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread.cpp (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
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   // 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);
35 }
36 
37 void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size,
38                   const InitState *state) {
39   CHECK_EQ(0, unique_id_);  // try to catch bad stack reuse
40   CHECK_EQ(0, stack_top_);
41   CHECK_EQ(0, stack_bottom_);
42 
43   static u64 unique_id;
44   unique_id_ = unique_id++;
45   if (auto sz = flags()->heap_history_size)
46     heap_allocations_ = HeapAllocationsRingBuffer::New(sz);
47 
48   InitStackAndTls(state);
49 #if !SANITIZER_FUCHSIA
50   // Do not initialize the stack ring buffer just yet on Fuchsia. Threads will
51   // be initialized before we enter the thread itself, so we will instead call
52   // this later.
53   InitStackRingBuffer(stack_buffer_start, stack_buffer_size);
54 #endif
55 }
56 
57 void Thread::InitStackRingBuffer(uptr stack_buffer_start,
58                                  uptr stack_buffer_size) {
59   HwasanTSDThreadInit();  // Only needed with interceptors.
60   uptr *ThreadLong = GetCurrentThreadLongPtr();
61   // The following implicitly sets (this) as the current thread.
62   stack_allocations_ = new (ThreadLong)
63       StackAllocationsRingBuffer((void *)stack_buffer_start, stack_buffer_size);
64   // Check that it worked.
65   CHECK_EQ(GetCurrentThread(), this);
66 
67   // ScopedTaggingDisable needs GetCurrentThread to be set up.
68   ScopedTaggingDisabler disabler;
69 
70   if (stack_bottom_) {
71     int local;
72     CHECK(AddrIsInStack((uptr)&local));
73     CHECK(MemIsApp(stack_bottom_));
74     CHECK(MemIsApp(stack_top_ - 1));
75   }
76 
77   if (flags()->verbose_threads) {
78     if (IsMainThread()) {
79       Printf("sizeof(Thread): %zd sizeof(HeapRB): %zd sizeof(StackRB): %zd\n",
80              sizeof(Thread), heap_allocations_->SizeInBytes(),
81              stack_allocations_->size() * sizeof(uptr));
82     }
83     Print("Creating  : ");
84   }
85 }
86 
87 void Thread::ClearShadowForThreadStackAndTLS() {
88   if (stack_top_ != stack_bottom_)
89     TagMemory(stack_bottom_, stack_top_ - stack_bottom_, 0);
90   if (tls_begin_ != tls_end_)
91     TagMemory(tls_begin_, tls_end_ - tls_begin_, 0);
92 }
93 
94 void Thread::Destroy() {
95   if (flags()->verbose_threads)
96     Print("Destroying: ");
97   AllocatorSwallowThreadLocalCache(allocator_cache());
98   ClearShadowForThreadStackAndTLS();
99   if (heap_allocations_)
100     heap_allocations_->Delete();
101   DTLS_Destroy();
102   // Unregister this as the current thread.
103   // Instrumented code can not run on this thread from this point onwards, but
104   // malloc/free can still be served. Glibc may call free() very late, after all
105   // TSD destructors are done.
106   CHECK_EQ(GetCurrentThread(), this);
107   *GetCurrentThreadLongPtr() = 0;
108 }
109 
110 void Thread::Print(const char *Prefix) {
111   Printf("%sT%zd %p stack: [%p,%p) sz: %zd tls: [%p,%p)\n", Prefix,
112          unique_id_, this, stack_bottom(), stack_top(),
113          stack_top() - stack_bottom(),
114          tls_begin(), tls_end());
115 }
116 
117 static u32 xorshift(u32 state) {
118   state ^= state << 13;
119   state ^= state >> 17;
120   state ^= state << 5;
121   return state;
122 }
123 
124 // Generate a (pseudo-)random non-zero tag.
125 tag_t Thread::GenerateRandomTag(uptr num_bits) {
126   DCHECK_GT(num_bits, 0);
127   if (tagging_disabled_) return 0;
128   tag_t tag;
129   const uptr tag_mask = (1ULL << num_bits) - 1;
130   do {
131     if (flags()->random_tags) {
132       if (!random_buffer_)
133         random_buffer_ = random_state_ = xorshift(random_state_);
134       CHECK(random_buffer_);
135       tag = random_buffer_ & tag_mask;
136       random_buffer_ >>= num_bits;
137     } else {
138       random_state_ += 1;
139       tag = random_state_ & tag_mask;
140     }
141   } while (!tag);
142   return tag;
143 }
144 
145 } // namespace __hwasan
146