1 //===-- hwasan_thread.h -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of HWAddressSanitizer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef HWASAN_THREAD_H
15 #define HWASAN_THREAD_H
16 
17 #include "hwasan_allocator.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_ring_buffer.h"
20 
21 namespace __hwasan {
22 
23 typedef __sanitizer::CompactRingBuffer<uptr> StackAllocationsRingBuffer;
24 
25 class Thread {
26  public:
27   void Init(uptr stack_buffer_start, uptr stack_buffer_size);  // Must be called from the thread itself.
28   void Destroy();
29 
stack_top()30   uptr stack_top() { return stack_top_; }
stack_bottom()31   uptr stack_bottom() { return stack_bottom_; }
stack_size()32   uptr stack_size() { return stack_top() - stack_bottom(); }
tls_begin()33   uptr tls_begin() { return tls_begin_; }
tls_end()34   uptr tls_end() { return tls_end_; }
IsMainThread()35   bool IsMainThread() { return unique_id_ == 0; }
36 
AddrIsInStack(uptr addr)37   bool AddrIsInStack(uptr addr) {
38     return addr >= stack_bottom_ && addr < stack_top_;
39   }
40 
InSignalHandler()41   bool InSignalHandler() { return in_signal_handler_; }
EnterSignalHandler()42   void EnterSignalHandler() { in_signal_handler_++; }
LeaveSignalHandler()43   void LeaveSignalHandler() { in_signal_handler_--; }
44 
InSymbolizer()45   bool InSymbolizer() { return in_symbolizer_; }
EnterSymbolizer()46   void EnterSymbolizer() { in_symbolizer_++; }
LeaveSymbolizer()47   void LeaveSymbolizer() { in_symbolizer_--; }
48 
InInterceptorScope()49   bool InInterceptorScope() { return in_interceptor_scope_; }
EnterInterceptorScope()50   void EnterInterceptorScope() { in_interceptor_scope_++; }
LeaveInterceptorScope()51   void LeaveInterceptorScope() { in_interceptor_scope_--; }
52 
allocator_cache()53   AllocatorCache *allocator_cache() { return &allocator_cache_; }
heap_allocations()54   HeapAllocationsRingBuffer *heap_allocations() { return heap_allocations_; }
stack_allocations()55   StackAllocationsRingBuffer *stack_allocations() { return stack_allocations_; }
56 
57   tag_t GenerateRandomTag();
58 
DisableTagging()59   void DisableTagging() { tagging_disabled_++; }
EnableTagging()60   void EnableTagging() { tagging_disabled_--; }
TaggingIsDisabled()61   bool TaggingIsDisabled() const { return tagging_disabled_; }
62 
unique_id()63   u64 unique_id() const { return unique_id_; }
Announce()64   void Announce() {
65     if (announced_) return;
66     announced_ = true;
67     Print("Thread: ");
68   }
69 
70  private:
71   // NOTE: There is no Thread constructor. It is allocated
72   // via mmap() and *must* be valid in zero-initialized state.
73   void ClearShadowForThreadStackAndTLS();
74   void Print(const char *prefix);
75   uptr stack_top_;
76   uptr stack_bottom_;
77   uptr tls_begin_;
78   uptr tls_end_;
79 
80   unsigned in_signal_handler_;
81   unsigned in_symbolizer_;
82   unsigned in_interceptor_scope_;
83 
84   u32 random_state_;
85   u32 random_buffer_;
86 
87   AllocatorCache allocator_cache_;
88   HeapAllocationsRingBuffer *heap_allocations_;
89   StackAllocationsRingBuffer *stack_allocations_;
90 
91   static void InsertIntoThreadList(Thread *t);
92   static void RemoveFromThreadList(Thread *t);
93   Thread *next_;  // All live threads form a linked list.
94 
95   u64 unique_id_;  // counting from zero.
96 
97   u32 tagging_disabled_;  // if non-zero, malloc uses zero tag in this thread.
98 
99   bool announced_;
100 
101   friend struct ThreadListHead;
102 };
103 
104 Thread *GetCurrentThread();
105 uptr *GetCurrentThreadLongPtr();
106 
107 struct ScopedTaggingDisabler {
ScopedTaggingDisablerScopedTaggingDisabler108   ScopedTaggingDisabler() { GetCurrentThread()->DisableTagging(); }
~ScopedTaggingDisablerScopedTaggingDisabler109   ~ScopedTaggingDisabler() { GetCurrentThread()->EnableTagging(); }
110 };
111 
112 } // namespace __hwasan
113 
114 #endif // HWASAN_THREAD_H
115