hwasan_thread.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. DTLS *dtls() { return dtls_; }
  39. bool IsMainThread() { return unique_id_ == 0; }
  40. bool AddrIsInStack(uptr addr) {
  41. return addr >= stack_bottom_ && addr < stack_top_;
  42. }
  43. AllocatorCache *allocator_cache() { return &allocator_cache_; }
  44. HeapAllocationsRingBuffer *heap_allocations() { return heap_allocations_; }
  45. StackAllocationsRingBuffer *stack_allocations() { return stack_allocations_; }
  46. tag_t GenerateRandomTag(uptr num_bits = kTagBits);
  47. void DisableTagging() { tagging_disabled_++; }
  48. void EnableTagging() { tagging_disabled_--; }
  49. u32 unique_id() const { return unique_id_; }
  50. void Announce() {
  51. if (announced_) return;
  52. announced_ = true;
  53. Print("Thread: ");
  54. }
  55. tid_t os_id() const { return os_id_; }
  56. void set_os_id(tid_t os_id) { os_id_ = os_id; }
  57. uptr &vfork_spill() { return vfork_spill_; }
  58. private:
  59. // NOTE: There is no Thread constructor. It is allocated
  60. // via mmap() and *must* be valid in zero-initialized state.
  61. void ClearShadowForThreadStackAndTLS();
  62. void Print(const char *prefix);
  63. void InitRandomState();
  64. uptr vfork_spill_;
  65. uptr stack_top_;
  66. uptr stack_bottom_;
  67. uptr tls_begin_;
  68. uptr tls_end_;
  69. DTLS *dtls_;
  70. u32 random_state_;
  71. u32 random_buffer_;
  72. AllocatorCache allocator_cache_;
  73. HeapAllocationsRingBuffer *heap_allocations_;
  74. StackAllocationsRingBuffer *stack_allocations_;
  75. u32 unique_id_; // counting from zero.
  76. tid_t os_id_;
  77. u32 tagging_disabled_; // if non-zero, malloc uses zero tag in this thread.
  78. bool announced_;
  79. bool random_state_inited_; // Whether InitRandomState() has been called.
  80. friend struct ThreadListHead;
  81. };
  82. Thread *GetCurrentThread();
  83. uptr *GetCurrentThreadLongPtr();
  84. struct ScopedTaggingDisabler {
  85. ScopedTaggingDisabler() { GetCurrentThread()->DisableTagging(); }
  86. ~ScopedTaggingDisabler() { GetCurrentThread()->EnableTagging(); }
  87. };
  88. } // namespace __hwasan
  89. #endif // HWASAN_THREAD_H