1 //===-- dfsan_thread.h -------------------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of DataFlowSanitizer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef DFSAN_THREAD_H
15 #define DFSAN_THREAD_H
16 
17 #include "dfsan_allocator.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 
20 namespace __dfsan {
21 
22 class DFsanThread {
23  public:
24   // NOTE: There is no DFsanThread constructor. It is allocated
25   // via mmap() and *must* be valid in zero-initialized state.
26 
27   static DFsanThread *Create(void *start_routine_trampoline,
28                              thread_callback_t start_routine, void *arg,
29                              bool track_origins = false);
30   static void TSDDtor(void *tsd);
31   void Destroy();
32 
33   void Init();  // Should be called from the thread itself.
34   thread_return_t ThreadStart();
35 
36   uptr stack_top();
37   uptr stack_bottom();
38   uptr tls_begin() { return tls_begin_; }
39   uptr tls_end() { return tls_end_; }
40   bool IsMainThread() { return start_routine_ == nullptr; }
41 
42   bool InSignalHandler() { return in_signal_handler_; }
43   void EnterSignalHandler() { in_signal_handler_++; }
44   void LeaveSignalHandler() { in_signal_handler_--; }
45 
46   DFsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
47 
48   int destructor_iterations_;
49 
50  private:
51   void SetThreadStackAndTls();
52   void ClearShadowForThreadStackAndTLS();
53   struct StackBounds {
54     uptr bottom;
55     uptr top;
56   };
57   StackBounds GetStackBounds() const;
58 
59   bool AddrIsInStack(uptr addr);
60 
61   void *start_routine_trampoline_;
62   thread_callback_t start_routine_;
63   void *arg_;
64   bool track_origins_;
65 
66   StackBounds stack_;
67 
68   uptr tls_begin_;
69   uptr tls_end_;
70 
71   unsigned in_signal_handler_;
72 
73   DFsanThreadLocalMallocStorage malloc_storage_;
74 };
75 
76 DFsanThread *GetCurrentThread();
77 void SetCurrentThread(DFsanThread *t);
78 void DFsanTSDInit(void (*destructor)(void *tsd));
79 void DFsanTSDDtor(void *tsd);
80 
81 }  // namespace __dfsan
82 
83 #endif  // DFSAN_THREAD_H
84