xref: /aosp_15_r20/external/compiler-rt/lib/msan/msan_thread.h (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- msan_thread.h -------------------------------------------*- C++ -*-===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of MemorySanitizer.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot #ifndef MSAN_THREAD_H
15*7c3d14c8STreehugger Robot #define MSAN_THREAD_H
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot #include "msan_allocator.h"
18*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_common.h"
19*7c3d14c8STreehugger Robot 
20*7c3d14c8STreehugger Robot namespace __msan {
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot class MsanThread {
23*7c3d14c8STreehugger Robot  public:
24*7c3d14c8STreehugger Robot   static MsanThread *Create(thread_callback_t start_routine, void *arg);
25*7c3d14c8STreehugger Robot   static void TSDDtor(void *tsd);
26*7c3d14c8STreehugger Robot   void Destroy();
27*7c3d14c8STreehugger Robot 
28*7c3d14c8STreehugger Robot   void Init();  // Should be called from the thread itself.
29*7c3d14c8STreehugger Robot   thread_return_t ThreadStart();
30*7c3d14c8STreehugger Robot 
stack_top()31*7c3d14c8STreehugger Robot   uptr stack_top() { return stack_top_; }
stack_bottom()32*7c3d14c8STreehugger Robot   uptr stack_bottom() { return stack_bottom_; }
tls_begin()33*7c3d14c8STreehugger Robot   uptr tls_begin() { return tls_begin_; }
tls_end()34*7c3d14c8STreehugger Robot   uptr tls_end() { return tls_end_; }
IsMainThread()35*7c3d14c8STreehugger Robot   bool IsMainThread() { return start_routine_ == nullptr; }
36*7c3d14c8STreehugger Robot 
AddrIsInStack(uptr addr)37*7c3d14c8STreehugger Robot   bool AddrIsInStack(uptr addr) {
38*7c3d14c8STreehugger Robot     return addr >= stack_bottom_ && addr < stack_top_;
39*7c3d14c8STreehugger Robot   }
40*7c3d14c8STreehugger Robot 
InSignalHandler()41*7c3d14c8STreehugger Robot   bool InSignalHandler() { return in_signal_handler_; }
EnterSignalHandler()42*7c3d14c8STreehugger Robot   void EnterSignalHandler() { in_signal_handler_++; }
LeaveSignalHandler()43*7c3d14c8STreehugger Robot   void LeaveSignalHandler() { in_signal_handler_--; }
44*7c3d14c8STreehugger Robot 
malloc_storage()45*7c3d14c8STreehugger Robot   MsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
46*7c3d14c8STreehugger Robot 
47*7c3d14c8STreehugger Robot   int destructor_iterations_;
48*7c3d14c8STreehugger Robot 
49*7c3d14c8STreehugger Robot  private:
50*7c3d14c8STreehugger Robot   // NOTE: There is no MsanThread constructor. It is allocated
51*7c3d14c8STreehugger Robot   // via mmap() and *must* be valid in zero-initialized state.
52*7c3d14c8STreehugger Robot   void SetThreadStackAndTls();
53*7c3d14c8STreehugger Robot   void ClearShadowForThreadStackAndTLS();
54*7c3d14c8STreehugger Robot   thread_callback_t start_routine_;
55*7c3d14c8STreehugger Robot   void *arg_;
56*7c3d14c8STreehugger Robot   uptr stack_top_;
57*7c3d14c8STreehugger Robot   uptr stack_bottom_;
58*7c3d14c8STreehugger Robot   uptr tls_begin_;
59*7c3d14c8STreehugger Robot   uptr tls_end_;
60*7c3d14c8STreehugger Robot 
61*7c3d14c8STreehugger Robot   unsigned in_signal_handler_;
62*7c3d14c8STreehugger Robot 
63*7c3d14c8STreehugger Robot   MsanThreadLocalMallocStorage malloc_storage_;
64*7c3d14c8STreehugger Robot };
65*7c3d14c8STreehugger Robot 
66*7c3d14c8STreehugger Robot MsanThread *GetCurrentThread();
67*7c3d14c8STreehugger Robot void SetCurrentThread(MsanThread *t);
68*7c3d14c8STreehugger Robot 
69*7c3d14c8STreehugger Robot } // namespace __msan
70*7c3d14c8STreehugger Robot 
71*7c3d14c8STreehugger Robot #endif // MSAN_THREAD_H
72