]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/hwasan/hwasan_thread.cc
Add compiler-rt's libFuzzer, not connected to buildworld yet.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / hwasan / hwasan_thread.cc
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_tls_get_addr.h"
9
10 namespace __hwasan {
11
12 static u32 RandomSeed() {
13   u32 seed;
14   do {
15     if (UNLIKELY(!GetRandom(reinterpret_cast<void *>(&seed), sizeof(seed),
16                             /*blocking=*/false))) {
17       seed = static_cast<u32>(
18           (NanoTime() >> 12) ^
19           (reinterpret_cast<uptr>(__builtin_frame_address(0)) >> 4));
20     }
21   } while (!seed);
22   return seed;
23 }
24
25 HwasanThread *HwasanThread::Create(thread_callback_t start_routine,
26                                void *arg) {
27   uptr PageSize = GetPageSizeCached();
28   uptr size = RoundUpTo(sizeof(HwasanThread), PageSize);
29   HwasanThread *thread = (HwasanThread*)MmapOrDie(size, __func__);
30   thread->start_routine_ = start_routine;
31   thread->arg_ = arg;
32   thread->destructor_iterations_ = GetPthreadDestructorIterations();
33   thread->random_state_ = flags()->random_tags ? RandomSeed() : 0;
34
35   return thread;
36 }
37
38 void HwasanThread::SetThreadStackAndTls() {
39   uptr tls_size = 0;
40   uptr stack_size = 0;
41   GetThreadStackAndTls(IsMainThread(), &stack_bottom_, &stack_size,
42                        &tls_begin_, &tls_size);
43   stack_top_ = stack_bottom_ + stack_size;
44   tls_end_ = tls_begin_ + tls_size;
45
46   int local;
47   CHECK(AddrIsInStack((uptr)&local));
48 }
49
50 void HwasanThread::Init() {
51   SetThreadStackAndTls();
52   CHECK(MEM_IS_APP(stack_bottom_));
53   CHECK(MEM_IS_APP(stack_top_ - 1));
54 }
55
56 void HwasanThread::TSDDtor(void *tsd) {
57   HwasanThread *t = (HwasanThread*)tsd;
58   t->Destroy();
59 }
60
61 void HwasanThread::ClearShadowForThreadStackAndTLS() {
62   TagMemory(stack_bottom_, stack_top_ - stack_bottom_, 0);
63   if (tls_begin_ != tls_end_)
64     TagMemory(tls_begin_, tls_end_ - tls_begin_, 0);
65 }
66
67 void HwasanThread::Destroy() {
68   malloc_storage().CommitBack();
69   ClearShadowForThreadStackAndTLS();
70   uptr size = RoundUpTo(sizeof(HwasanThread), GetPageSizeCached());
71   UnmapOrDie(this, size);
72   DTLS_Destroy();
73 }
74
75 thread_return_t HwasanThread::ThreadStart() {
76   Init();
77
78   if (!start_routine_) {
79     // start_routine_ == 0 if we're on the main thread or on one of the
80     // OS X libdispatch worker threads. But nobody is supposed to call
81     // ThreadStart() for the worker threads.
82     return 0;
83   }
84
85   thread_return_t res = start_routine_(arg_);
86
87   return res;
88 }
89
90 static u32 xorshift(u32 state) {
91   state ^= state << 13;
92   state ^= state >> 17;
93   state ^= state << 5;
94   return state;
95 }
96
97 // Generate a (pseudo-)random non-zero tag.
98 tag_t HwasanThread::GenerateRandomTag() {
99   tag_t tag;
100   do {
101     if (flags()->random_tags) {
102       if (!random_buffer_)
103         random_buffer_ = random_state_ = xorshift(random_state_);
104       CHECK(random_buffer_);
105       tag = random_buffer_ & 0xFF;
106       random_buffer_ >>= 8;
107     } else {
108       tag = random_state_ = (random_state_ + 1) & 0xFF;
109     }
110   } while (!tag);
111   return tag;
112 }
113
114 } // namespace __hwasan