]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/hwasan/hwasan_thread.h
Add LLVM openmp trunk r351319 (just before the release_80 branch point)
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / hwasan / hwasan_thread.h
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
30   uptr stack_top() { return stack_top_; }
31   uptr stack_bottom() { return stack_bottom_; }
32   uptr stack_size() { return stack_top() - stack_bottom(); }
33   uptr tls_begin() { return tls_begin_; }
34   uptr tls_end() { return tls_end_; }
35   bool IsMainThread() { return unique_id_ == 0; }
36
37   bool AddrIsInStack(uptr addr) {
38     return addr >= stack_bottom_ && addr < stack_top_;
39   }
40
41   bool InSignalHandler() { return in_signal_handler_; }
42   void EnterSignalHandler() { in_signal_handler_++; }
43   void LeaveSignalHandler() { in_signal_handler_--; }
44
45   bool InSymbolizer() { return in_symbolizer_; }
46   void EnterSymbolizer() { in_symbolizer_++; }
47   void LeaveSymbolizer() { in_symbolizer_--; }
48
49   bool InInterceptorScope() { return in_interceptor_scope_; }
50   void EnterInterceptorScope() { in_interceptor_scope_++; }
51   void LeaveInterceptorScope() { in_interceptor_scope_--; }
52
53   AllocatorCache *allocator_cache() { return &allocator_cache_; }
54   HeapAllocationsRingBuffer *heap_allocations() { return heap_allocations_; }
55   StackAllocationsRingBuffer *stack_allocations() { return stack_allocations_; }
56
57   tag_t GenerateRandomTag();
58
59   void DisableTagging() { tagging_disabled_++; }
60   void EnableTagging() { tagging_disabled_--; }
61   bool TaggingIsDisabled() const { return tagging_disabled_; }
62
63   u64 unique_id() const { return unique_id_; }
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 {
108   ScopedTaggingDisabler() { GetCurrentThread()->DisableTagging(); }
109   ~ScopedTaggingDisabler() { GetCurrentThread()->EnableTagging(); }
110 };
111
112 } // namespace __hwasan
113
114 #endif // HWASAN_THREAD_H