1 
2 #include "msan.h"
3 #include "msan_thread.h"
4 #include "msan_interface_internal.h"
5 
6 #include "sanitizer_common/sanitizer_tls_get_addr.h"
7 
8 namespace __msan {
9 
10 MsanThread *MsanThread::Create(thread_callback_t start_routine,
11                                void *arg) {
12   uptr PageSize = GetPageSizeCached();
13   uptr size = RoundUpTo(sizeof(MsanThread), PageSize);
14   MsanThread *thread = (MsanThread*)MmapOrDie(size, __func__);
15   thread->start_routine_ = start_routine;
16   thread->arg_ = arg;
17   thread->destructor_iterations_ = GetPthreadDestructorIterations();
18 
19   return thread;
20 }
21 
22 void MsanThread::SetThreadStackAndTls() {
23   uptr tls_size = 0;
24   uptr stack_size = 0;
25   GetThreadStackAndTls(IsMainThread(), &stack_.bottom, &stack_size, &tls_begin_,
26                        &tls_size);
27   stack_.top = stack_.bottom + stack_size;
28   tls_end_ = tls_begin_ + tls_size;
29 
30   int local;
31   CHECK(AddrIsInStack((uptr)&local));
32 }
33 
34 void MsanThread::ClearShadowForThreadStackAndTLS() {
35   __msan_unpoison((void *)stack_.bottom, stack_.top - stack_.bottom);
36   if (tls_begin_ != tls_end_)
37     __msan_unpoison((void *)tls_begin_, tls_end_ - tls_begin_);
38   DTLS *dtls = DTLS_Get();
39   CHECK_NE(dtls, 0);
40   ForEachDVT(dtls, [](const DTLS::DTV &dtv, int id) {
41     __msan_unpoison((void *)(dtv.beg), dtv.size);
42   });
43 }
44 
45 void MsanThread::Init() {
46   SetThreadStackAndTls();
47   CHECK(MEM_IS_APP(stack_.bottom));
48   CHECK(MEM_IS_APP(stack_.top - 1));
49   ClearShadowForThreadStackAndTLS();
50 }
51 
52 void MsanThread::TSDDtor(void *tsd) {
53   MsanThread *t = (MsanThread*)tsd;
54   t->Destroy();
55 }
56 
57 void MsanThread::Destroy() {
58   malloc_storage().CommitBack();
59   // We also clear the shadow on thread destruction because
60   // some code may still be executing in later TSD destructors
61   // and we don't want it to have any poisoned stack.
62   ClearShadowForThreadStackAndTLS();
63   uptr size = RoundUpTo(sizeof(MsanThread), GetPageSizeCached());
64   UnmapOrDie(this, size);
65   DTLS_Destroy();
66 }
67 
68 thread_return_t MsanThread::ThreadStart() {
69   if (!start_routine_) {
70     // start_routine_ == 0 if we're on the main thread or on one of the
71     // OS X libdispatch worker threads. But nobody is supposed to call
72     // ThreadStart() for the worker threads.
73     return 0;
74   }
75 
76   thread_return_t res = start_routine_(arg_);
77 
78   return res;
79 }
80 
81 MsanThread::StackBounds MsanThread::GetStackBounds() const {
82   if (!stack_switching_)
83     return {stack_.bottom, stack_.top};
84   const uptr cur_stack = GET_CURRENT_FRAME();
85   // Note: need to check next stack first, because FinishSwitchFiber
86   // may be in process of overwriting stack_.top/bottom_. But in such case
87   // we are already on the next stack.
88   if (cur_stack >= next_stack_.bottom && cur_stack < next_stack_.top)
89     return {next_stack_.bottom, next_stack_.top};
90   return {stack_.bottom, stack_.top};
91 }
92 
93 uptr MsanThread::stack_top() { return GetStackBounds().top; }
94 
95 uptr MsanThread::stack_bottom() { return GetStackBounds().bottom; }
96 
97 bool MsanThread::AddrIsInStack(uptr addr) {
98   const auto bounds = GetStackBounds();
99   return addr >= bounds.bottom && addr < bounds.top;
100 }
101 
102 void MsanThread::StartSwitchFiber(uptr bottom, uptr size) {
103   CHECK(!stack_switching_);
104   next_stack_.bottom = bottom;
105   next_stack_.top = bottom + size;
106   stack_switching_ = true;
107 }
108 
109 void MsanThread::FinishSwitchFiber(uptr *bottom_old, uptr *size_old) {
110   CHECK(stack_switching_);
111   if (bottom_old)
112     *bottom_old = stack_.bottom;
113   if (size_old)
114     *size_old = stack_.top - stack_.bottom;
115   stack_.bottom = next_stack_.bottom;
116   stack_.top = next_stack_.top;
117   stack_switching_ = false;
118   next_stack_.top = 0;
119   next_stack_.bottom = 0;
120 }
121 
122 } // namespace __msan
123