xref: /netbsd-src/external/gpl3/gcc.old/dist/libsanitizer/sanitizer_common/sanitizer_stoptheworld_mac.cc (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 //===-- sanitizer_stoptheworld_mac.cc -------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // See sanitizer_stoptheworld.h for details.
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #include "sanitizer_platform.h"
13 
14 #if SANITIZER_MAC && (defined(__x86_64__) || defined(__aarch64__) || \
15                       defined(__i386))
16 
17 #include <mach/mach.h>
18 #include <mach/thread_info.h>
19 #include <pthread.h>
20 
21 #include "sanitizer_stoptheworld.h"
22 
23 namespace __sanitizer {
24 typedef struct {
25   tid_t tid;
26   thread_t thread;
27 } SuspendedThreadInfo;
28 
29 class SuspendedThreadsListMac : public SuspendedThreadsList {
30  public:
SuspendedThreadsListMac()31   SuspendedThreadsListMac() : threads_(1024) {}
32 
33   tid_t GetThreadID(uptr index) const;
34   thread_t GetThread(uptr index) const;
35   uptr ThreadCount() const;
36   bool ContainsThread(thread_t thread) const;
37   void Append(thread_t thread);
38 
39   PtraceRegistersStatus GetRegistersAndSP(uptr index, uptr *buffer,
40                                           uptr *sp) const;
41   uptr RegisterCount() const;
42 
43  private:
44   InternalMmapVector<SuspendedThreadInfo> threads_;
45 };
46 
47 struct RunThreadArgs {
48   StopTheWorldCallback callback;
49   void *argument;
50 };
51 
RunThread(void * arg)52 void RunThread(void *arg) {
53   struct RunThreadArgs *run_args = (struct RunThreadArgs *)arg;
54   SuspendedThreadsListMac suspended_threads_list;
55 
56   thread_array_t threads;
57   mach_msg_type_number_t num_threads;
58   kern_return_t err = task_threads(mach_task_self(), &threads, &num_threads);
59   if (err != KERN_SUCCESS) {
60     VReport(1, "Failed to get threads for task (errno %d).\n", err);
61     return;
62   }
63 
64   thread_t thread_self = mach_thread_self();
65   for (unsigned int i = 0; i < num_threads; ++i) {
66     if (threads[i] == thread_self) continue;
67 
68     thread_suspend(threads[i]);
69     suspended_threads_list.Append(threads[i]);
70   }
71 
72   run_args->callback(suspended_threads_list, run_args->argument);
73 
74   uptr num_suspended = suspended_threads_list.ThreadCount();
75   for (unsigned int i = 0; i < num_suspended; ++i) {
76     thread_resume(suspended_threads_list.GetThread(i));
77   }
78 }
79 
StopTheWorld(StopTheWorldCallback callback,void * argument)80 void StopTheWorld(StopTheWorldCallback callback, void *argument) {
81   struct RunThreadArgs arg = {callback, argument};
82   pthread_t run_thread = (pthread_t)internal_start_thread(RunThread, &arg);
83   internal_join_thread(run_thread);
84 }
85 
86 #if defined(__x86_64__)
87 typedef x86_thread_state64_t regs_struct;
88 
89 #define SP_REG __rsp
90 
91 #elif defined(__aarch64__)
92 typedef arm_thread_state64_t regs_struct;
93 
94 # if __DARWIN_UNIX03
95 #  define SP_REG __sp
96 # else
97 #  define SP_REG sp
98 # endif
99 
100 #elif defined(__i386)
101 typedef x86_thread_state32_t regs_struct;
102 
103 #define SP_REG __esp
104 
105 #else
106 #error "Unsupported architecture"
107 #endif
108 
GetThreadID(uptr index) const109 tid_t SuspendedThreadsListMac::GetThreadID(uptr index) const {
110   CHECK_LT(index, threads_.size());
111   return threads_[index].tid;
112 }
113 
GetThread(uptr index) const114 thread_t SuspendedThreadsListMac::GetThread(uptr index) const {
115   CHECK_LT(index, threads_.size());
116   return threads_[index].thread;
117 }
118 
ThreadCount() const119 uptr SuspendedThreadsListMac::ThreadCount() const {
120   return threads_.size();
121 }
122 
ContainsThread(thread_t thread) const123 bool SuspendedThreadsListMac::ContainsThread(thread_t thread) const {
124   for (uptr i = 0; i < threads_.size(); i++) {
125     if (threads_[i].thread == thread) return true;
126   }
127   return false;
128 }
129 
Append(thread_t thread)130 void SuspendedThreadsListMac::Append(thread_t thread) {
131   thread_identifier_info_data_t info;
132   mach_msg_type_number_t info_count = THREAD_IDENTIFIER_INFO_COUNT;
133   kern_return_t err = thread_info(thread, THREAD_IDENTIFIER_INFO,
134                                   (thread_info_t)&info, &info_count);
135   if (err != KERN_SUCCESS) {
136     VReport(1, "Error - unable to get thread ident for a thread\n");
137     return;
138   }
139   threads_.push_back({info.thread_id, thread});
140 }
141 
GetRegistersAndSP(uptr index,uptr * buffer,uptr * sp) const142 PtraceRegistersStatus SuspendedThreadsListMac::GetRegistersAndSP(
143     uptr index, uptr *buffer, uptr *sp) const {
144   thread_t thread = GetThread(index);
145   regs_struct regs;
146   int err;
147   mach_msg_type_number_t reg_count = MACHINE_THREAD_STATE_COUNT;
148   err = thread_get_state(thread, MACHINE_THREAD_STATE, (thread_state_t)&regs,
149                          &reg_count);
150   if (err != KERN_SUCCESS) {
151     VReport(1, "Error - unable to get registers for a thread\n");
152     // KERN_INVALID_ARGUMENT indicates that either the flavor is invalid,
153     // or the thread does not exist. The other possible error case,
154     // MIG_ARRAY_TOO_LARGE, means that the state is too large, but it's
155     // still safe to proceed.
156     return err == KERN_INVALID_ARGUMENT ? REGISTERS_UNAVAILABLE_FATAL
157                                         : REGISTERS_UNAVAILABLE;
158   }
159 
160   internal_memcpy(buffer, &regs, sizeof(regs));
161   *sp = regs.SP_REG;
162 
163   // On x86_64 and aarch64, we must account for the stack redzone, which is 128
164   // bytes.
165   if (SANITIZER_WORDSIZE == 64) *sp -= 128;
166 
167   return REGISTERS_AVAILABLE;
168 }
169 
RegisterCount() const170 uptr SuspendedThreadsListMac::RegisterCount() const {
171   return MACHINE_THREAD_STATE_COUNT;
172 }
173 } // namespace __sanitizer
174 
175 #endif  // SANITIZER_MAC && (defined(__x86_64__) || defined(__aarch64__)) ||
176         //                   defined(__i386))
177