3 #include "msan_thread.h"
4 #include "msan_interface_internal.h"
6 #include "sanitizer_common/sanitizer_tls_get_addr.h"
10 MsanThread *MsanThread::Create(thread_callback_t start_routine,
12 uptr PageSize = GetPageSizeCached();
13 uptr size = RoundUpTo(sizeof(MsanThread), PageSize);
14 MsanThread *thread = (MsanThread*)MmapOrDie(size, __func__);
15 thread->start_routine_ = start_routine;
17 thread->destructor_iterations_ = GetPthreadDestructorIterations();
22 void MsanThread::SetThreadStackAndTls() {
25 GetThreadStackAndTls(IsMainThread(), &stack_bottom_, &stack_size,
26 &tls_begin_, &tls_size);
27 stack_top_ = stack_bottom_ + stack_size;
28 tls_end_ = tls_begin_ + tls_size;
31 CHECK(AddrIsInStack((uptr)&local));
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();
40 for (uptr i = 0; i < dtls->dtv_size; ++i)
41 __msan_unpoison((void *)(dtls->dtv[i].beg), dtls->dtv[i].size);
44 void MsanThread::Init() {
45 SetThreadStackAndTls();
46 CHECK(MEM_IS_APP(stack_bottom_));
47 CHECK(MEM_IS_APP(stack_top_ - 1));
48 ClearShadowForThreadStackAndTLS();
51 void MsanThread::TSDDtor(void *tsd) {
52 MsanThread *t = (MsanThread*)tsd;
56 void MsanThread::Destroy() {
57 malloc_storage().CommitBack();
58 // We also clear the shadow on thread destruction because
59 // some code may still be executing in later TSD destructors
60 // and we don't want it to have any poisoned stack.
61 ClearShadowForThreadStackAndTLS();
62 uptr size = RoundUpTo(sizeof(MsanThread), GetPageSizeCached());
63 UnmapOrDie(this, size);
67 thread_return_t MsanThread::ThreadStart() {
70 if (!start_routine_) {
71 // start_routine_ == 0 if we're on the main thread or on one of the
72 // OS X libdispatch worker threads. But nobody is supposed to call
73 // ThreadStart() for the worker threads.
77 thread_return_t res = start_routine_(arg_);