Path: blob/main/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan_thread.cpp
35233 views
#include "dfsan_thread.h"12#include <pthread.h>34#include "dfsan.h"5#include "sanitizer_common/sanitizer_tls_get_addr.h"67namespace __dfsan {89DFsanThread *DFsanThread::Create(thread_callback_t start_routine, void *arg,10bool track_origins) {11uptr PageSize = GetPageSizeCached();12uptr size = RoundUpTo(sizeof(DFsanThread), PageSize);13DFsanThread *thread = (DFsanThread *)MmapOrDie(size, __func__);14thread->start_routine_ = start_routine;15thread->arg_ = arg;16thread->track_origins_ = track_origins;17thread->destructor_iterations_ = GetPthreadDestructorIterations();1819return thread;20}2122void DFsanThread::SetThreadStackAndTls() {23uptr tls_size = 0;24uptr stack_size = 0;25GetThreadStackAndTls(IsMainThread(), &stack_.bottom, &stack_size, &tls_begin_,26&tls_size);27stack_.top = stack_.bottom + stack_size;28tls_end_ = tls_begin_ + tls_size;2930int local;31CHECK(AddrIsInStack((uptr)&local));32}3334void DFsanThread::ClearShadowForThreadStackAndTLS() {35dfsan_set_label(0, (void *)stack_.bottom, stack_.top - stack_.bottom);36if (tls_begin_ != tls_end_)37dfsan_set_label(0, (void *)tls_begin_, tls_end_ - tls_begin_);38DTLS *dtls = DTLS_Get();39CHECK_NE(dtls, 0);40ForEachDVT(dtls, [](const DTLS::DTV &dtv, int id) {41dfsan_set_label(0, (void *)(dtv.beg), dtv.size);42});43}4445void DFsanThread::Init() {46SetThreadStackAndTls();47ClearShadowForThreadStackAndTLS();48}4950void DFsanThread::TSDDtor(void *tsd) {51DFsanThread *t = (DFsanThread *)tsd;52t->Destroy();53}5455void DFsanThread::Destroy() {56malloc_storage().CommitBack();57// We also clear the shadow on thread destruction because58// some code may still be executing in later TSD destructors59// and we don't want it to have any poisoned stack.60ClearShadowForThreadStackAndTLS();61uptr size = RoundUpTo(sizeof(DFsanThread), GetPageSizeCached());62UnmapOrDie(this, size);63DTLS_Destroy();64}6566thread_return_t DFsanThread::ThreadStart() {67if (!start_routine_) {68// start_routine_ == 0 if we're on the main thread or on one of the69// OS X libdispatch worker threads. But nobody is supposed to call70// ThreadStart() for the worker threads.71return 0;72}7374// The only argument is void* arg.75//76// We have never supported propagating the pointer arg as tainted,77// __dfsw_pthread_create/__dfso_pthread_create ignore the taint label.78// Note that the bytes pointed-to (probably the much more common case)79// can still have taint labels attached to them.80dfsan_clear_thread_local_state();8182return start_routine_(arg_);83}8485DFsanThread::StackBounds DFsanThread::GetStackBounds() const {86return {stack_.bottom, stack_.top};87}8889uptr DFsanThread::stack_top() { return GetStackBounds().top; }9091uptr DFsanThread::stack_bottom() { return GetStackBounds().bottom; }9293bool DFsanThread::AddrIsInStack(uptr addr) {94const auto bounds = GetStackBounds();95return addr >= bounds.bottom && addr < bounds.top;96}9798static pthread_key_t tsd_key;99static bool tsd_key_inited = false;100101void DFsanTSDInit(void (*destructor)(void *tsd)) {102CHECK(!tsd_key_inited);103tsd_key_inited = true;104CHECK_EQ(0, pthread_key_create(&tsd_key, destructor));105}106107static THREADLOCAL DFsanThread *dfsan_current_thread;108109DFsanThread *GetCurrentThread() { return dfsan_current_thread; }110111void SetCurrentThread(DFsanThread *t) {112// Make sure we do not reset the current DFsanThread.113CHECK_EQ(0, dfsan_current_thread);114dfsan_current_thread = t;115// Make sure that DFsanTSDDtor gets called at the end.116CHECK(tsd_key_inited);117pthread_setspecific(tsd_key, t);118}119120void DFsanTSDDtor(void *tsd) {121DFsanThread *t = (DFsanThread *)tsd;122if (t->destructor_iterations_ > 1) {123t->destructor_iterations_--;124CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));125return;126}127dfsan_current_thread = nullptr;128// Make sure that signal handler can not see a stale current thread pointer.129atomic_signal_fence(memory_order_seq_cst);130DFsanThread::TSDDtor(tsd);131}132133} // namespace __dfsan134135136