Path: blob/main/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_thread.h
35235 views
//===-- hwasan_thread.h -----------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of HWAddressSanitizer.9//10//===----------------------------------------------------------------------===//1112#ifndef HWASAN_THREAD_H13#define HWASAN_THREAD_H1415#include "hwasan_allocator.h"16#include "sanitizer_common/sanitizer_common.h"17#include "sanitizer_common/sanitizer_ring_buffer.h"1819namespace __hwasan {2021typedef __sanitizer::CompactRingBuffer<uptr> StackAllocationsRingBuffer;2223class Thread {24public:25// These are optional parameters that can be passed to Init.26struct InitState;2728void Init(uptr stack_buffer_start, uptr stack_buffer_size,29const InitState *state = nullptr);3031void InitStackAndTls(const InitState *state = nullptr);3233// Must be called from the thread itself.34void InitStackRingBuffer(uptr stack_buffer_start, uptr stack_buffer_size);3536inline void EnsureRandomStateInited() {37if (UNLIKELY(!random_state_inited_))38InitRandomState();39}4041void Destroy();4243uptr stack_top() { return stack_top_; }44uptr stack_bottom() { return stack_bottom_; }45uptr stack_size() { return stack_top() - stack_bottom(); }46uptr tls_begin() { return tls_begin_; }47uptr tls_end() { return tls_end_; }48DTLS *dtls() { return dtls_; }49bool IsMainThread() { return unique_id_ == 0; }5051bool AddrIsInStack(uptr addr) {52return addr >= stack_bottom_ && addr < stack_top_;53}5455AllocatorCache *allocator_cache() { return &allocator_cache_; }56HeapAllocationsRingBuffer *heap_allocations() { return heap_allocations_; }57StackAllocationsRingBuffer *stack_allocations() { return stack_allocations_; }5859tag_t GenerateRandomTag(uptr num_bits = kTagBits);6061void DisableTagging() { tagging_disabled_++; }62void EnableTagging() { tagging_disabled_--; }6364u32 unique_id() const { return unique_id_; }65void Announce() {66if (announced_) return;67announced_ = true;68Print("Thread: ");69}7071tid_t os_id() const { return os_id_; }72void set_os_id(tid_t os_id) { os_id_ = os_id; }7374uptr &vfork_spill() { return vfork_spill_; }7576private:77// NOTE: There is no Thread constructor. It is allocated78// via mmap() and *must* be valid in zero-initialized state.79void ClearShadowForThreadStackAndTLS();80void Print(const char *prefix);81void InitRandomState();82uptr vfork_spill_;83uptr stack_top_;84uptr stack_bottom_;85uptr tls_begin_;86uptr tls_end_;87DTLS *dtls_;8889u32 random_state_;90u32 random_buffer_;9192AllocatorCache allocator_cache_;93HeapAllocationsRingBuffer *heap_allocations_;94StackAllocationsRingBuffer *stack_allocations_;9596u32 unique_id_; // counting from zero.9798tid_t os_id_;99100u32 tagging_disabled_; // if non-zero, malloc uses zero tag in this thread.101102bool announced_;103104bool random_state_inited_; // Whether InitRandomState() has been called.105106friend struct ThreadListHead;107};108109Thread *GetCurrentThread();110uptr *GetCurrentThreadLongPtr();111112// Used to handle fork().113void EnsureMainThreadIDIsCorrect();114115struct ScopedTaggingDisabler {116ScopedTaggingDisabler() { GetCurrentThread()->DisableTagging(); }117~ScopedTaggingDisabler() { GetCurrentThread()->EnableTagging(); }118};119120} // namespace __hwasan121122#endif // HWASAN_THREAD_H123124125