hwasan_thread.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===-- hwasan_thread.h -----------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of HWAddressSanitizer.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef HWASAN_THREAD_H
  13. #define HWASAN_THREAD_H
  14. #include "hwasan_allocator.h"
  15. #include "sanitizer_common/sanitizer_common.h"
  16. #include "sanitizer_common/sanitizer_ring_buffer.h"
  17. namespace __hwasan {
  18. typedef __sanitizer::CompactRingBuffer<uptr> StackAllocationsRingBuffer;
  19. class Thread {
  20. public:
  21. // These are optional parameters that can be passed to Init.
  22. struct InitState;
  23. void Init(uptr stack_buffer_start, uptr stack_buffer_size,
  24. const InitState *state = nullptr);
  25. void InitStackAndTls(const InitState *state = nullptr);
  26. // Must be called from the thread itself.
  27. void InitStackRingBuffer(uptr stack_buffer_start, uptr stack_buffer_size);
  28. inline void EnsureRandomStateInited() {
  29. if (UNLIKELY(!random_state_inited_))
  30. InitRandomState();
  31. }
  32. void Destroy();
  33. uptr stack_top() { return stack_top_; }
  34. uptr stack_bottom() { return stack_bottom_; }
  35. uptr stack_size() { return stack_top() - stack_bottom(); }
  36. uptr tls_begin() { return tls_begin_; }
  37. uptr tls_end() { return tls_end_; }
  38. bool IsMainThread() { return unique_id_ == 0; }
  39. bool AddrIsInStack(uptr addr) {
  40. return addr >= stack_bottom_ && addr < stack_top_;
  41. }
  42. AllocatorCache *allocator_cache() { return &allocator_cache_; }
  43. HeapAllocationsRingBuffer *heap_allocations() { return heap_allocations_; }
  44. StackAllocationsRingBuffer *stack_allocations() { return stack_allocations_; }
  45. tag_t GenerateRandomTag(uptr num_bits = kTagBits);
  46. void DisableTagging() { tagging_disabled_++; }
  47. void EnableTagging() { tagging_disabled_--; }
  48. u64 unique_id() const { return unique_id_; }
  49. void Announce() {
  50. if (announced_) return;
  51. announced_ = true;
  52. Print("Thread: ");
  53. }
  54. uptr &vfork_spill() { return vfork_spill_; }
  55. private:
  56. // NOTE: There is no Thread constructor. It is allocated
  57. // via mmap() and *must* be valid in zero-initialized state.
  58. void ClearShadowForThreadStackAndTLS();
  59. void Print(const char *prefix);
  60. void InitRandomState();
  61. uptr vfork_spill_;
  62. uptr stack_top_;
  63. uptr stack_bottom_;
  64. uptr tls_begin_;
  65. uptr tls_end_;
  66. u32 random_state_;
  67. u32 random_buffer_;
  68. AllocatorCache allocator_cache_;
  69. HeapAllocationsRingBuffer *heap_allocations_;
  70. StackAllocationsRingBuffer *stack_allocations_;
  71. u64 unique_id_; // counting from zero.
  72. u32 tagging_disabled_; // if non-zero, malloc uses zero tag in this thread.
  73. bool announced_;
  74. bool random_state_inited_; // Whether InitRandomState() has been called.
  75. friend struct ThreadListHead;
  76. };
  77. Thread *GetCurrentThread();
  78. uptr *GetCurrentThreadLongPtr();
  79. struct ScopedTaggingDisabler {
  80. ScopedTaggingDisabler() { GetCurrentThread()->DisableTagging(); }
  81. ~ScopedTaggingDisabler() { GetCurrentThread()->EnableTagging(); }
  82. };
  83. } // namespace __hwasan
  84. #endif // HWASAN_THREAD_H