]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/hwasan/hwasan_thread.cc
Merge ^/head r327169 through r327340.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / hwasan / hwasan_thread.cc
1
2 #include "hwasan.h"
3 #include "hwasan_thread.h"
4 #include "hwasan_poisoning.h"
5 #include "hwasan_interface_internal.h"
6
7 #include "sanitizer_common/sanitizer_tls_get_addr.h"
8
9 namespace __hwasan {
10
11 HwasanThread *HwasanThread::Create(thread_callback_t start_routine,
12                                void *arg) {
13   uptr PageSize = GetPageSizeCached();
14   uptr size = RoundUpTo(sizeof(HwasanThread), PageSize);
15   HwasanThread *thread = (HwasanThread*)MmapOrDie(size, __func__);
16   thread->start_routine_ = start_routine;
17   thread->arg_ = arg;
18   thread->destructor_iterations_ = GetPthreadDestructorIterations();
19
20   return thread;
21 }
22
23 void HwasanThread::SetThreadStackAndTls() {
24   uptr tls_size = 0;
25   uptr stack_size = 0;
26   GetThreadStackAndTls(IsMainThread(), &stack_bottom_, &stack_size,
27                        &tls_begin_, &tls_size);
28   stack_top_ = stack_bottom_ + stack_size;
29   tls_end_ = tls_begin_ + tls_size;
30
31   int local;
32   CHECK(AddrIsInStack((uptr)&local));
33 }
34
35 void HwasanThread::Init() {
36   SetThreadStackAndTls();
37   CHECK(MEM_IS_APP(stack_bottom_));
38   CHECK(MEM_IS_APP(stack_top_ - 1));
39 }
40
41 void HwasanThread::TSDDtor(void *tsd) {
42   HwasanThread *t = (HwasanThread*)tsd;
43   t->Destroy();
44 }
45
46 void HwasanThread::ClearShadowForThreadStackAndTLS() {
47   TagMemory(stack_bottom_, stack_top_ - stack_bottom_, 0);
48   if (tls_begin_ != tls_end_)
49     TagMemory(tls_begin_, tls_end_ - tls_begin_, 0);
50 }
51
52 void HwasanThread::Destroy() {
53   malloc_storage().CommitBack();
54   ClearShadowForThreadStackAndTLS();
55   uptr size = RoundUpTo(sizeof(HwasanThread), GetPageSizeCached());
56   UnmapOrDie(this, size);
57   DTLS_Destroy();
58 }
59
60 thread_return_t HwasanThread::ThreadStart() {
61   Init();
62
63   if (!start_routine_) {
64     // start_routine_ == 0 if we're on the main thread or on one of the
65     // OS X libdispatch worker threads. But nobody is supposed to call
66     // ThreadStart() for the worker threads.
67     return 0;
68   }
69
70   thread_return_t res = start_routine_(arg_);
71
72   return res;
73 }
74
75 } // namespace __hwasan