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