1 //===-- hwasan_thread.h -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of HWAddressSanitizer.
12 //===----------------------------------------------------------------------===//
14 #ifndef HWASAN_THREAD_H
15 #define HWASAN_THREAD_H
17 #include "hwasan_allocator.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_ring_buffer.h"
23 typedef __sanitizer::CompactRingBuffer<uptr> StackAllocationsRingBuffer;
27 void Init(uptr stack_buffer_start, uptr stack_buffer_size); // Must be called from the thread itself.
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; }
37 bool AddrIsInStack(uptr addr) {
38 return addr >= stack_bottom_ && addr < stack_top_;
41 bool InSignalHandler() { return in_signal_handler_; }
42 void EnterSignalHandler() { in_signal_handler_++; }
43 void LeaveSignalHandler() { in_signal_handler_--; }
45 bool InSymbolizer() { return in_symbolizer_; }
46 void EnterSymbolizer() { in_symbolizer_++; }
47 void LeaveSymbolizer() { in_symbolizer_--; }
49 bool InInterceptorScope() { return in_interceptor_scope_; }
50 void EnterInterceptorScope() { in_interceptor_scope_++; }
51 void LeaveInterceptorScope() { in_interceptor_scope_--; }
53 AllocatorCache *allocator_cache() { return &allocator_cache_; }
54 HeapAllocationsRingBuffer *heap_allocations() { return heap_allocations_; }
55 StackAllocationsRingBuffer *stack_allocations() { return stack_allocations_; }
57 tag_t GenerateRandomTag();
59 void DisableTagging() { tagging_disabled_++; }
60 void EnableTagging() { tagging_disabled_--; }
61 bool TaggingIsDisabled() const { return tagging_disabled_; }
63 u64 unique_id() const { return unique_id_; }
65 if (announced_) return;
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);
80 unsigned in_signal_handler_;
81 unsigned in_symbolizer_;
82 unsigned in_interceptor_scope_;
87 AllocatorCache allocator_cache_;
88 HeapAllocationsRingBuffer *heap_allocations_;
89 StackAllocationsRingBuffer *stack_allocations_;
91 static void InsertIntoThreadList(Thread *t);
92 static void RemoveFromThreadList(Thread *t);
93 Thread *next_; // All live threads form a linked list.
95 u64 unique_id_; // counting from zero.
97 u32 tagging_disabled_; // if non-zero, malloc uses zero tag in this thread.
101 friend struct ThreadListHead;
104 Thread *GetCurrentThread();
105 uptr *GetCurrentThreadLongPtr();
107 struct ScopedTaggingDisabler {
108 ScopedTaggingDisabler() { GetCurrentThread()->DisableTagging(); }
109 ~ScopedTaggingDisabler() { GetCurrentThread()->EnableTagging(); }
112 } // namespace __hwasan
114 #endif // HWASAN_THREAD_H