Path: blob/main/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread.cpp
35236 views
1#include "hwasan_thread.h"23#include "hwasan.h"4#include "hwasan_interface_internal.h"5#include "hwasan_mapping.h"6#include "hwasan_poisoning.h"7#include "hwasan_thread_list.h"8#include "sanitizer_common/sanitizer_atomic.h"9#include "sanitizer_common/sanitizer_file.h"10#include "sanitizer_common/sanitizer_placement_new.h"11#include "sanitizer_common/sanitizer_tls_get_addr.h"1213namespace __hwasan {1415static u32 RandomSeed() {16u32 seed;17do {18if (UNLIKELY(!GetRandom(reinterpret_cast<void *>(&seed), sizeof(seed),19/*blocking=*/false))) {20seed = static_cast<u32>(21(NanoTime() >> 12) ^22(reinterpret_cast<uptr>(__builtin_frame_address(0)) >> 4));23}24} while (!seed);25return seed;26}2728void Thread::InitRandomState() {29random_state_ = flags()->random_tags ? RandomSeed() : unique_id_;30random_state_inited_ = true;3132// Push a random number of zeros onto the ring buffer so that the first stack33// tag base will be random.34for (tag_t i = 0, e = GenerateRandomTag(); i != e; ++i)35stack_allocations_->push(0);36}3738void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size,39const InitState *state) {40CHECK_EQ(0, unique_id_); // try to catch bad stack reuse41CHECK_EQ(0, stack_top_);42CHECK_EQ(0, stack_bottom_);4344static atomic_uint64_t unique_id;45unique_id_ = atomic_fetch_add(&unique_id, 1, memory_order_relaxed);46if (!IsMainThread())47os_id_ = GetTid();4849if (auto sz = flags()->heap_history_size)50heap_allocations_ = HeapAllocationsRingBuffer::New(sz);5152#if !SANITIZER_FUCHSIA53// Do not initialize the stack ring buffer just yet on Fuchsia. Threads will54// be initialized before we enter the thread itself, so we will instead call55// this later.56InitStackRingBuffer(stack_buffer_start, stack_buffer_size);57#endif58InitStackAndTls(state);59dtls_ = DTLS_Get();60AllocatorThreadStart(allocator_cache());6162if (flags()->verbose_threads) {63if (IsMainThread()) {64Printf("sizeof(Thread): %zd sizeof(HeapRB): %zd sizeof(StackRB): %zd\n",65sizeof(Thread), heap_allocations_->SizeInBytes(),66stack_allocations_->size() * sizeof(uptr));67}68Print("Creating : ");69}70ClearShadowForThreadStackAndTLS();71}7273void Thread::InitStackRingBuffer(uptr stack_buffer_start,74uptr stack_buffer_size) {75HwasanTSDThreadInit(); // Only needed with interceptors.76uptr *ThreadLong = GetCurrentThreadLongPtr();77// The following implicitly sets (this) as the current thread.78stack_allocations_ = new (ThreadLong)79StackAllocationsRingBuffer((void *)stack_buffer_start, stack_buffer_size);80// Check that it worked.81CHECK_EQ(GetCurrentThread(), this);8283// ScopedTaggingDisable needs GetCurrentThread to be set up.84ScopedTaggingDisabler disabler;8586if (stack_bottom_) {87int local;88CHECK(AddrIsInStack((uptr)&local));89CHECK(MemIsApp(stack_bottom_));90CHECK(MemIsApp(stack_top_ - 1));91}92}9394void Thread::ClearShadowForThreadStackAndTLS() {95if (stack_top_ != stack_bottom_)96TagMemory(UntagAddr(stack_bottom_),97UntagAddr(stack_top_) - UntagAddr(stack_bottom_),98GetTagFromPointer(stack_top_));99if (tls_begin_ != tls_end_)100TagMemory(UntagAddr(tls_begin_),101UntagAddr(tls_end_) - UntagAddr(tls_begin_),102GetTagFromPointer(tls_begin_));103}104105void Thread::Destroy() {106if (flags()->verbose_threads)107Print("Destroying: ");108AllocatorThreadFinish(allocator_cache());109ClearShadowForThreadStackAndTLS();110if (heap_allocations_)111heap_allocations_->Delete();112DTLS_Destroy();113// Unregister this as the current thread.114// Instrumented code can not run on this thread from this point onwards, but115// malloc/free can still be served. Glibc may call free() very late, after all116// TSD destructors are done.117CHECK_EQ(GetCurrentThread(), this);118*GetCurrentThreadLongPtr() = 0;119}120121void Thread::Print(const char *Prefix) {122Printf("%sT%zd %p stack: [%p,%p) sz: %zd tls: [%p,%p)\n", Prefix, unique_id_,123(void *)this, stack_bottom(), stack_top(),124stack_top() - stack_bottom(), tls_begin(), tls_end());125}126127static u32 xorshift(u32 state) {128state ^= state << 13;129state ^= state >> 17;130state ^= state << 5;131return state;132}133134// Generate a (pseudo-)random non-zero tag.135tag_t Thread::GenerateRandomTag(uptr num_bits) {136DCHECK_GT(num_bits, 0);137if (tagging_disabled_)138return 0;139tag_t tag;140const uptr tag_mask = (1ULL << num_bits) - 1;141do {142if (flags()->random_tags) {143if (!random_buffer_) {144EnsureRandomStateInited();145random_buffer_ = random_state_ = xorshift(random_state_);146}147CHECK(random_buffer_);148tag = random_buffer_ & tag_mask;149random_buffer_ >>= num_bits;150} else {151EnsureRandomStateInited();152random_state_ += 1;153tag = random_state_ & tag_mask;154}155} while (!tag);156return tag;157}158159void EnsureMainThreadIDIsCorrect() {160auto *t = __hwasan::GetCurrentThread();161if (t && (t->IsMainThread()))162t->set_os_id(GetTid());163}164165} // namespace __hwasan166167// --- Implementation of LSan-specific functions --- {{{1168namespace __lsan {169170static __hwasan::HwasanThreadList *GetHwasanThreadListLocked() {171auto &tl = __hwasan::hwasanThreadList();172tl.CheckLocked();173return &tl;174}175176static __hwasan::Thread *GetThreadByOsIDLocked(tid_t os_id) {177return GetHwasanThreadListLocked()->FindThreadLocked(178[os_id](__hwasan::Thread *t) { return t->os_id() == os_id; });179}180181void LockThreads() {182__hwasan::hwasanThreadList().Lock();183__hwasan::hwasanThreadArgRetval().Lock();184}185186void UnlockThreads() {187__hwasan::hwasanThreadArgRetval().Unlock();188__hwasan::hwasanThreadList().Unlock();189}190191void EnsureMainThreadIDIsCorrect() { __hwasan::EnsureMainThreadIDIsCorrect(); }192193bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,194uptr *tls_begin, uptr *tls_end, uptr *cache_begin,195uptr *cache_end, DTLS **dtls) {196auto *t = GetThreadByOsIDLocked(os_id);197if (!t)198return false;199*stack_begin = t->stack_bottom();200*stack_end = t->stack_top();201*tls_begin = t->tls_begin();202*tls_end = t->tls_end();203// Fixme: is this correct for HWASan.204*cache_begin = 0;205*cache_end = 0;206*dtls = t->dtls();207return true;208}209210void GetAllThreadAllocatorCachesLocked(InternalMmapVector<uptr> *caches) {}211212void GetThreadExtraStackRangesLocked(tid_t os_id,213InternalMmapVector<Range> *ranges) {}214void GetThreadExtraStackRangesLocked(InternalMmapVector<Range> *ranges) {}215216void GetAdditionalThreadContextPtrsLocked(InternalMmapVector<uptr> *ptrs) {217__hwasan::hwasanThreadArgRetval().GetAllPtrsLocked(ptrs);218}219220void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads) {}221222} // namespace __lsan223224225