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
13namespace __hwasan {
14
15static 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
28void 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
37void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size) {
38  static u64 unique_id;
39  unique_id_ = unique_id++;
40  if (auto sz = flags()->heap_history_size)
41    heap_allocations_ = HeapAllocationsRingBuffer::New(sz);
42
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);
50
51  // ScopedTaggingDisable needs GetCurrentThread to be set up.
52  ScopedTaggingDisabler disabler;
53
54  uptr tls_size;
55  uptr stack_size;
56  GetThreadStackAndTls(IsMainThread(), &stack_bottom_, &stack_size, &tls_begin_,
57                       &tls_size);
58  stack_top_ = stack_bottom_ + stack_size;
59  tls_end_ = tls_begin_ + tls_size;
60
61  if (stack_bottom_) {
62    int local;
63    CHECK(AddrIsInStack((uptr)&local));
64    CHECK(MemIsApp(stack_bottom_));
65    CHECK(MemIsApp(stack_top_ - 1));
66  }
67
68  if (flags()->verbose_threads) {
69    if (IsMainThread()) {
70      Printf("sizeof(Thread): %zd sizeof(HeapRB): %zd sizeof(StackRB): %zd\n",
71             sizeof(Thread), heap_allocations_->SizeInBytes(),
72             stack_allocations_->size() * sizeof(uptr));
73    }
74    Print("Creating  : ");
75  }
76}
77
78void 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);
83}
84
85void Thread::Destroy() {
86  if (flags()->verbose_threads)
87    Print("Destroying: ");
88  AllocatorSwallowThreadLocalCache(allocator_cache());
89  ClearShadowForThreadStackAndTLS();
90  if (heap_allocations_)
91    heap_allocations_->Delete();
92  DTLS_Destroy();
93}
94
95void 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());
100}
101
102static u32 xorshift(u32 state) {
103  state ^= state << 13;
104  state ^= state >> 17;
105  state ^= state << 5;
106  return state;
107}
108
109// Generate a (pseudo-)random non-zero tag.
110tag_t Thread::GenerateRandomTag() {
111  if (tagging_disabled_) return 0;
112  tag_t tag;
113  do {
114    if (flags()->random_tags) {
115      if (!random_buffer_)
116        random_buffer_ = random_state_ = xorshift(random_state_);
117      CHECK(random_buffer_);
118      tag = random_buffer_ & 0xFF;
119      random_buffer_ >>= 8;
120    } else {
121      tag = random_state_ = (random_state_ + 1) & 0xFF;
122    }
123  } while (!tag);
124  return tag;
125}
126
127} // namespace __hwasan
128