Path: blob/main/contrib/llvm-project/compiler-rt/lib/msan/msan_thread.cpp
35262 views
1#include "msan_thread.h"23#include "msan.h"4#include "msan_interface_internal.h"5#include "sanitizer_common/sanitizer_tls_get_addr.h"67namespace __msan {89MsanThread *MsanThread::Create(thread_callback_t start_routine,10void *arg) {11uptr PageSize = GetPageSizeCached();12uptr size = RoundUpTo(sizeof(MsanThread), PageSize);13MsanThread *thread = (MsanThread*)MmapOrDie(size, __func__);14thread->start_routine_ = start_routine;15thread->arg_ = arg;16thread->destructor_iterations_ = GetPthreadDestructorIterations();1718return thread;19}2021void MsanThread::SetThreadStackAndTls() {22uptr tls_size = 0;23uptr stack_size = 0;24GetThreadStackAndTls(IsMainThread(), &stack_.bottom, &stack_size, &tls_begin_,25&tls_size);26stack_.top = stack_.bottom + stack_size;27tls_end_ = tls_begin_ + tls_size;2829int local;30CHECK(AddrIsInStack((uptr)&local));31}3233void MsanThread::ClearShadowForThreadStackAndTLS() {34__msan_unpoison((void *)stack_.bottom, stack_.top - stack_.bottom);35if (tls_begin_ != tls_end_)36__msan_unpoison((void *)tls_begin_, tls_end_ - tls_begin_);37DTLS *dtls = DTLS_Get();38CHECK_NE(dtls, 0);39ForEachDVT(dtls, [](const DTLS::DTV &dtv, int id) {40__msan_unpoison((void *)(dtv.beg), dtv.size);41});42}4344void MsanThread::Init() {45SetThreadStackAndTls();46CHECK(MEM_IS_APP(stack_.bottom));47CHECK(MEM_IS_APP(stack_.top - 1));48ClearShadowForThreadStackAndTLS();49malloc_storage().Init();50}5152void MsanThread::TSDDtor(void *tsd) {53MsanThread *t = (MsanThread*)tsd;54t->Destroy();55}5657void MsanThread::Destroy() {58malloc_storage().CommitBack();59// We also clear the shadow on thread destruction because60// some code may still be executing in later TSD destructors61// and we don't want it to have any poisoned stack.62ClearShadowForThreadStackAndTLS();63uptr size = RoundUpTo(sizeof(MsanThread), GetPageSizeCached());64UnmapOrDie(this, size);65DTLS_Destroy();66}6768thread_return_t MsanThread::ThreadStart() {69if (!start_routine_) {70// start_routine_ == 0 if we're on the main thread or on one of the71// OS X libdispatch worker threads. But nobody is supposed to call72// ThreadStart() for the worker threads.73return 0;74}7576thread_return_t res = start_routine_(arg_);7778return res;79}8081MsanThread::StackBounds MsanThread::GetStackBounds() const {82if (!stack_switching_)83return {stack_.bottom, stack_.top};84const uptr cur_stack = GET_CURRENT_FRAME();85// Note: need to check next stack first, because FinishSwitchFiber86// may be in process of overwriting stack_.top/bottom_. But in such case87// we are already on the next stack.88if (cur_stack >= next_stack_.bottom && cur_stack < next_stack_.top)89return {next_stack_.bottom, next_stack_.top};90return {stack_.bottom, stack_.top};91}9293uptr MsanThread::stack_top() { return GetStackBounds().top; }9495uptr MsanThread::stack_bottom() { return GetStackBounds().bottom; }9697bool MsanThread::AddrIsInStack(uptr addr) {98const auto bounds = GetStackBounds();99return addr >= bounds.bottom && addr < bounds.top;100}101102void MsanThread::StartSwitchFiber(uptr bottom, uptr size) {103CHECK(!stack_switching_);104next_stack_.bottom = bottom;105next_stack_.top = bottom + size;106stack_switching_ = true;107}108109void MsanThread::FinishSwitchFiber(uptr *bottom_old, uptr *size_old) {110CHECK(stack_switching_);111if (bottom_old)112*bottom_old = stack_.bottom;113if (size_old)114*size_old = stack_.top - stack_.bottom;115stack_.bottom = next_stack_.bottom;116stack_.top = next_stack_.top;117stack_switching_ = false;118next_stack_.top = 0;119next_stack_.bottom = 0;120}121122} // namespace __msan123124125