annotate libsanitizer/sanitizer_common/sanitizer_stoptheworld_linux_libcdep.cc @ 118:fd00160c1b76

ifdef TARGET_64BIT
author mir3636
date Tue, 27 Feb 2018 15:01:35 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 //===-- sanitizer_stoptheworld_linux_libcdep.cc ---------------------------===//
kono
parents:
diff changeset
2 //
kono
parents:
diff changeset
3 // This file is distributed under the University of Illinois Open Source
kono
parents:
diff changeset
4 // License. See LICENSE.TXT for details.
kono
parents:
diff changeset
5 //
kono
parents:
diff changeset
6 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
7 //
kono
parents:
diff changeset
8 // See sanitizer_stoptheworld.h for details.
kono
parents:
diff changeset
9 // This implementation was inspired by Markus Gutschke's linuxthreads.cc.
kono
parents:
diff changeset
10 //
kono
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 #include "sanitizer_platform.h"
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__) || \
kono
parents:
diff changeset
16 defined(__aarch64__) || defined(__powerpc64__) || \
kono
parents:
diff changeset
17 defined(__s390__) || defined(__i386__) || \
kono
parents:
diff changeset
18 defined(__arm__))
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "sanitizer_stoptheworld.h"
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 #include "sanitizer_platform_limits_posix.h"
kono
parents:
diff changeset
23 #include "sanitizer_atomic.h"
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 #include <errno.h>
kono
parents:
diff changeset
26 #include <sched.h> // for CLONE_* definitions
kono
parents:
diff changeset
27 #include <stddef.h>
kono
parents:
diff changeset
28 #include <sys/prctl.h> // for PR_* definitions
kono
parents:
diff changeset
29 #include <sys/ptrace.h> // for PTRACE_* definitions
kono
parents:
diff changeset
30 #include <sys/types.h> // for pid_t
kono
parents:
diff changeset
31 #include <sys/uio.h> // for iovec
kono
parents:
diff changeset
32 #include <elf.h> // for NT_PRSTATUS
kono
parents:
diff changeset
33 #if defined(__aarch64__) && !SANITIZER_ANDROID
kono
parents:
diff changeset
34 // GLIBC 2.20+ sys/user does not include asm/ptrace.h
kono
parents:
diff changeset
35 # include <asm/ptrace.h>
kono
parents:
diff changeset
36 #endif
kono
parents:
diff changeset
37 #include <sys/user.h> // for user_regs_struct
kono
parents:
diff changeset
38 #if SANITIZER_ANDROID && SANITIZER_MIPS
kono
parents:
diff changeset
39 # include <asm/reg.h> // for mips SP register in sys/user.h
kono
parents:
diff changeset
40 #endif
kono
parents:
diff changeset
41 #include <sys/wait.h> // for signal-related stuff
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 #ifdef sa_handler
kono
parents:
diff changeset
44 # undef sa_handler
kono
parents:
diff changeset
45 #endif
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 #ifdef sa_sigaction
kono
parents:
diff changeset
48 # undef sa_sigaction
kono
parents:
diff changeset
49 #endif
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 #include "sanitizer_common.h"
kono
parents:
diff changeset
52 #include "sanitizer_flags.h"
kono
parents:
diff changeset
53 #include "sanitizer_libc.h"
kono
parents:
diff changeset
54 #include "sanitizer_linux.h"
kono
parents:
diff changeset
55 #include "sanitizer_mutex.h"
kono
parents:
diff changeset
56 #include "sanitizer_placement_new.h"
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 // This module works by spawning a Linux task which then attaches to every
kono
parents:
diff changeset
59 // thread in the caller process with ptrace. This suspends the threads, and
kono
parents:
diff changeset
60 // PTRACE_GETREGS can then be used to obtain their register state. The callback
kono
parents:
diff changeset
61 // supplied to StopTheWorld() is run in the tracer task while the threads are
kono
parents:
diff changeset
62 // suspended.
kono
parents:
diff changeset
63 // The tracer task must be placed in a different thread group for ptrace to
kono
parents:
diff changeset
64 // work, so it cannot be spawned as a pthread. Instead, we use the low-level
kono
parents:
diff changeset
65 // clone() interface (we want to share the address space with the caller
kono
parents:
diff changeset
66 // process, so we prefer clone() over fork()).
kono
parents:
diff changeset
67 //
kono
parents:
diff changeset
68 // We don't use any libc functions, relying instead on direct syscalls. There
kono
parents:
diff changeset
69 // are two reasons for this:
kono
parents:
diff changeset
70 // 1. calling a library function while threads are suspended could cause a
kono
parents:
diff changeset
71 // deadlock, if one of the treads happens to be holding a libc lock;
kono
parents:
diff changeset
72 // 2. it's generally not safe to call libc functions from the tracer task,
kono
parents:
diff changeset
73 // because clone() does not set up a thread-local storage for it. Any
kono
parents:
diff changeset
74 // thread-local variables used by libc will be shared between the tracer task
kono
parents:
diff changeset
75 // and the thread which spawned it.
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 namespace __sanitizer {
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 class SuspendedThreadsListLinux : public SuspendedThreadsList {
kono
parents:
diff changeset
80 public:
kono
parents:
diff changeset
81 SuspendedThreadsListLinux() : thread_ids_(1024) {}
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 tid_t GetThreadID(uptr index) const;
kono
parents:
diff changeset
84 uptr ThreadCount() const;
kono
parents:
diff changeset
85 bool ContainsTid(tid_t thread_id) const;
kono
parents:
diff changeset
86 void Append(tid_t tid);
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 PtraceRegistersStatus GetRegistersAndSP(uptr index, uptr *buffer,
kono
parents:
diff changeset
89 uptr *sp) const;
kono
parents:
diff changeset
90 uptr RegisterCount() const;
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 private:
kono
parents:
diff changeset
93 InternalMmapVector<tid_t> thread_ids_;
kono
parents:
diff changeset
94 };
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 // Structure for passing arguments into the tracer thread.
kono
parents:
diff changeset
97 struct TracerThreadArgument {
kono
parents:
diff changeset
98 StopTheWorldCallback callback;
kono
parents:
diff changeset
99 void *callback_argument;
kono
parents:
diff changeset
100 // The tracer thread waits on this mutex while the parent finishes its
kono
parents:
diff changeset
101 // preparations.
kono
parents:
diff changeset
102 BlockingMutex mutex;
kono
parents:
diff changeset
103 // Tracer thread signals its completion by setting done.
kono
parents:
diff changeset
104 atomic_uintptr_t done;
kono
parents:
diff changeset
105 uptr parent_pid;
kono
parents:
diff changeset
106 };
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 // This class handles thread suspending/unsuspending in the tracer thread.
kono
parents:
diff changeset
109 class ThreadSuspender {
kono
parents:
diff changeset
110 public:
kono
parents:
diff changeset
111 explicit ThreadSuspender(pid_t pid, TracerThreadArgument *arg)
kono
parents:
diff changeset
112 : arg(arg)
kono
parents:
diff changeset
113 , pid_(pid) {
kono
parents:
diff changeset
114 CHECK_GE(pid, 0);
kono
parents:
diff changeset
115 }
kono
parents:
diff changeset
116 bool SuspendAllThreads();
kono
parents:
diff changeset
117 void ResumeAllThreads();
kono
parents:
diff changeset
118 void KillAllThreads();
kono
parents:
diff changeset
119 SuspendedThreadsListLinux &suspended_threads_list() {
kono
parents:
diff changeset
120 return suspended_threads_list_;
kono
parents:
diff changeset
121 }
kono
parents:
diff changeset
122 TracerThreadArgument *arg;
kono
parents:
diff changeset
123 private:
kono
parents:
diff changeset
124 SuspendedThreadsListLinux suspended_threads_list_;
kono
parents:
diff changeset
125 pid_t pid_;
kono
parents:
diff changeset
126 bool SuspendThread(tid_t thread_id);
kono
parents:
diff changeset
127 };
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 bool ThreadSuspender::SuspendThread(tid_t tid) {
kono
parents:
diff changeset
130 // Are we already attached to this thread?
kono
parents:
diff changeset
131 // Currently this check takes linear time, however the number of threads is
kono
parents:
diff changeset
132 // usually small.
kono
parents:
diff changeset
133 if (suspended_threads_list_.ContainsTid(tid)) return false;
kono
parents:
diff changeset
134 int pterrno;
kono
parents:
diff changeset
135 if (internal_iserror(internal_ptrace(PTRACE_ATTACH, tid, nullptr, nullptr),
kono
parents:
diff changeset
136 &pterrno)) {
kono
parents:
diff changeset
137 // Either the thread is dead, or something prevented us from attaching.
kono
parents:
diff changeset
138 // Log this event and move on.
kono
parents:
diff changeset
139 VReport(1, "Could not attach to thread %zu (errno %d).\n", (uptr)tid,
kono
parents:
diff changeset
140 pterrno);
kono
parents:
diff changeset
141 return false;
kono
parents:
diff changeset
142 } else {
kono
parents:
diff changeset
143 VReport(2, "Attached to thread %zu.\n", (uptr)tid);
kono
parents:
diff changeset
144 // The thread is not guaranteed to stop before ptrace returns, so we must
kono
parents:
diff changeset
145 // wait on it. Note: if the thread receives a signal concurrently,
kono
parents:
diff changeset
146 // we can get notification about the signal before notification about stop.
kono
parents:
diff changeset
147 // In such case we need to forward the signal to the thread, otherwise
kono
parents:
diff changeset
148 // the signal will be missed (as we do PTRACE_DETACH with arg=0) and
kono
parents:
diff changeset
149 // any logic relying on signals will break. After forwarding we need to
kono
parents:
diff changeset
150 // continue to wait for stopping, because the thread is not stopped yet.
kono
parents:
diff changeset
151 // We do ignore delivery of SIGSTOP, because we want to make stop-the-world
kono
parents:
diff changeset
152 // as invisible as possible.
kono
parents:
diff changeset
153 for (;;) {
kono
parents:
diff changeset
154 int status;
kono
parents:
diff changeset
155 uptr waitpid_status;
kono
parents:
diff changeset
156 HANDLE_EINTR(waitpid_status, internal_waitpid(tid, &status, __WALL));
kono
parents:
diff changeset
157 int wperrno;
kono
parents:
diff changeset
158 if (internal_iserror(waitpid_status, &wperrno)) {
kono
parents:
diff changeset
159 // Got a ECHILD error. I don't think this situation is possible, but it
kono
parents:
diff changeset
160 // doesn't hurt to report it.
kono
parents:
diff changeset
161 VReport(1, "Waiting on thread %zu failed, detaching (errno %d).\n",
kono
parents:
diff changeset
162 (uptr)tid, wperrno);
kono
parents:
diff changeset
163 internal_ptrace(PTRACE_DETACH, tid, nullptr, nullptr);
kono
parents:
diff changeset
164 return false;
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166 if (WIFSTOPPED(status) && WSTOPSIG(status) != SIGSTOP) {
kono
parents:
diff changeset
167 internal_ptrace(PTRACE_CONT, tid, nullptr,
kono
parents:
diff changeset
168 (void*)(uptr)WSTOPSIG(status));
kono
parents:
diff changeset
169 continue;
kono
parents:
diff changeset
170 }
kono
parents:
diff changeset
171 break;
kono
parents:
diff changeset
172 }
kono
parents:
diff changeset
173 suspended_threads_list_.Append(tid);
kono
parents:
diff changeset
174 return true;
kono
parents:
diff changeset
175 }
kono
parents:
diff changeset
176 }
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 void ThreadSuspender::ResumeAllThreads() {
kono
parents:
diff changeset
179 for (uptr i = 0; i < suspended_threads_list_.ThreadCount(); i++) {
kono
parents:
diff changeset
180 pid_t tid = suspended_threads_list_.GetThreadID(i);
kono
parents:
diff changeset
181 int pterrno;
kono
parents:
diff changeset
182 if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, nullptr, nullptr),
kono
parents:
diff changeset
183 &pterrno)) {
kono
parents:
diff changeset
184 VReport(2, "Detached from thread %d.\n", tid);
kono
parents:
diff changeset
185 } else {
kono
parents:
diff changeset
186 // Either the thread is dead, or we are already detached.
kono
parents:
diff changeset
187 // The latter case is possible, for instance, if this function was called
kono
parents:
diff changeset
188 // from a signal handler.
kono
parents:
diff changeset
189 VReport(1, "Could not detach from thread %d (errno %d).\n", tid, pterrno);
kono
parents:
diff changeset
190 }
kono
parents:
diff changeset
191 }
kono
parents:
diff changeset
192 }
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 void ThreadSuspender::KillAllThreads() {
kono
parents:
diff changeset
195 for (uptr i = 0; i < suspended_threads_list_.ThreadCount(); i++)
kono
parents:
diff changeset
196 internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i),
kono
parents:
diff changeset
197 nullptr, nullptr);
kono
parents:
diff changeset
198 }
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 bool ThreadSuspender::SuspendAllThreads() {
kono
parents:
diff changeset
201 ThreadLister thread_lister(pid_);
kono
parents:
diff changeset
202 bool added_threads;
kono
parents:
diff changeset
203 bool first_iteration = true;
kono
parents:
diff changeset
204 do {
kono
parents:
diff changeset
205 // Run through the directory entries once.
kono
parents:
diff changeset
206 added_threads = false;
kono
parents:
diff changeset
207 pid_t tid = thread_lister.GetNextTID();
kono
parents:
diff changeset
208 while (tid >= 0) {
kono
parents:
diff changeset
209 if (SuspendThread(tid))
kono
parents:
diff changeset
210 added_threads = true;
kono
parents:
diff changeset
211 tid = thread_lister.GetNextTID();
kono
parents:
diff changeset
212 }
kono
parents:
diff changeset
213 if (thread_lister.error() || (first_iteration && !added_threads)) {
kono
parents:
diff changeset
214 // Detach threads and fail.
kono
parents:
diff changeset
215 ResumeAllThreads();
kono
parents:
diff changeset
216 return false;
kono
parents:
diff changeset
217 }
kono
parents:
diff changeset
218 thread_lister.Reset();
kono
parents:
diff changeset
219 first_iteration = false;
kono
parents:
diff changeset
220 } while (added_threads);
kono
parents:
diff changeset
221 return true;
kono
parents:
diff changeset
222 }
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 // Pointer to the ThreadSuspender instance for use in signal handler.
kono
parents:
diff changeset
225 static ThreadSuspender *thread_suspender_instance = nullptr;
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 // Synchronous signals that should not be blocked.
kono
parents:
diff changeset
228 static const int kSyncSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV, SIGBUS,
kono
parents:
diff changeset
229 SIGXCPU, SIGXFSZ };
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 static void TracerThreadDieCallback() {
kono
parents:
diff changeset
232 // Generally a call to Die() in the tracer thread should be fatal to the
kono
parents:
diff changeset
233 // parent process as well, because they share the address space.
kono
parents:
diff changeset
234 // This really only works correctly if all the threads are suspended at this
kono
parents:
diff changeset
235 // point. So we correctly handle calls to Die() from within the callback, but
kono
parents:
diff changeset
236 // not those that happen before or after the callback. Hopefully there aren't
kono
parents:
diff changeset
237 // a lot of opportunities for that to happen...
kono
parents:
diff changeset
238 ThreadSuspender *inst = thread_suspender_instance;
kono
parents:
diff changeset
239 if (inst && stoptheworld_tracer_pid == internal_getpid()) {
kono
parents:
diff changeset
240 inst->KillAllThreads();
kono
parents:
diff changeset
241 thread_suspender_instance = nullptr;
kono
parents:
diff changeset
242 }
kono
parents:
diff changeset
243 }
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 // Signal handler to wake up suspended threads when the tracer thread dies.
kono
parents:
diff changeset
246 static void TracerThreadSignalHandler(int signum, void *siginfo, void *uctx) {
kono
parents:
diff changeset
247 SignalContext ctx(siginfo, uctx);
kono
parents:
diff changeset
248 Printf("Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n", signum,
kono
parents:
diff changeset
249 ctx.addr, ctx.pc, ctx.sp);
kono
parents:
diff changeset
250 ThreadSuspender *inst = thread_suspender_instance;
kono
parents:
diff changeset
251 if (inst) {
kono
parents:
diff changeset
252 if (signum == SIGABRT)
kono
parents:
diff changeset
253 inst->KillAllThreads();
kono
parents:
diff changeset
254 else
kono
parents:
diff changeset
255 inst->ResumeAllThreads();
kono
parents:
diff changeset
256 RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback));
kono
parents:
diff changeset
257 thread_suspender_instance = nullptr;
kono
parents:
diff changeset
258 atomic_store(&inst->arg->done, 1, memory_order_relaxed);
kono
parents:
diff changeset
259 }
kono
parents:
diff changeset
260 internal__exit((signum == SIGABRT) ? 1 : 2);
kono
parents:
diff changeset
261 }
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 // Size of alternative stack for signal handlers in the tracer thread.
kono
parents:
diff changeset
264 static const int kHandlerStackSize = 8192;
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 // This function will be run as a cloned task.
kono
parents:
diff changeset
267 static int TracerThread(void* argument) {
kono
parents:
diff changeset
268 TracerThreadArgument *tracer_thread_argument =
kono
parents:
diff changeset
269 (TracerThreadArgument *)argument;
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
kono
parents:
diff changeset
272 // Check if parent is already dead.
kono
parents:
diff changeset
273 if (internal_getppid() != tracer_thread_argument->parent_pid)
kono
parents:
diff changeset
274 internal__exit(4);
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 // Wait for the parent thread to finish preparations.
kono
parents:
diff changeset
277 tracer_thread_argument->mutex.Lock();
kono
parents:
diff changeset
278 tracer_thread_argument->mutex.Unlock();
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 RAW_CHECK(AddDieCallback(TracerThreadDieCallback));
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 ThreadSuspender thread_suspender(internal_getppid(), tracer_thread_argument);
kono
parents:
diff changeset
283 // Global pointer for the signal handler.
kono
parents:
diff changeset
284 thread_suspender_instance = &thread_suspender;
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 // Alternate stack for signal handling.
kono
parents:
diff changeset
287 InternalScopedBuffer<char> handler_stack_memory(kHandlerStackSize);
kono
parents:
diff changeset
288 stack_t handler_stack;
kono
parents:
diff changeset
289 internal_memset(&handler_stack, 0, sizeof(handler_stack));
kono
parents:
diff changeset
290 handler_stack.ss_sp = handler_stack_memory.data();
kono
parents:
diff changeset
291 handler_stack.ss_size = kHandlerStackSize;
kono
parents:
diff changeset
292 internal_sigaltstack(&handler_stack, nullptr);
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 // Install our handler for synchronous signals. Other signals should be
kono
parents:
diff changeset
295 // blocked by the mask we inherited from the parent thread.
kono
parents:
diff changeset
296 for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++) {
kono
parents:
diff changeset
297 __sanitizer_sigaction act;
kono
parents:
diff changeset
298 internal_memset(&act, 0, sizeof(act));
kono
parents:
diff changeset
299 act.sigaction = TracerThreadSignalHandler;
kono
parents:
diff changeset
300 act.sa_flags = SA_ONSTACK | SA_SIGINFO;
kono
parents:
diff changeset
301 internal_sigaction_norestorer(kSyncSignals[i], &act, 0);
kono
parents:
diff changeset
302 }
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 int exit_code = 0;
kono
parents:
diff changeset
305 if (!thread_suspender.SuspendAllThreads()) {
kono
parents:
diff changeset
306 VReport(1, "Failed suspending threads.\n");
kono
parents:
diff changeset
307 exit_code = 3;
kono
parents:
diff changeset
308 } else {
kono
parents:
diff changeset
309 tracer_thread_argument->callback(thread_suspender.suspended_threads_list(),
kono
parents:
diff changeset
310 tracer_thread_argument->callback_argument);
kono
parents:
diff changeset
311 thread_suspender.ResumeAllThreads();
kono
parents:
diff changeset
312 exit_code = 0;
kono
parents:
diff changeset
313 }
kono
parents:
diff changeset
314 RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback));
kono
parents:
diff changeset
315 thread_suspender_instance = nullptr;
kono
parents:
diff changeset
316 atomic_store(&tracer_thread_argument->done, 1, memory_order_relaxed);
kono
parents:
diff changeset
317 return exit_code;
kono
parents:
diff changeset
318 }
kono
parents:
diff changeset
319
kono
parents:
diff changeset
320 class ScopedStackSpaceWithGuard {
kono
parents:
diff changeset
321 public:
kono
parents:
diff changeset
322 explicit ScopedStackSpaceWithGuard(uptr stack_size) {
kono
parents:
diff changeset
323 stack_size_ = stack_size;
kono
parents:
diff changeset
324 guard_size_ = GetPageSizeCached();
kono
parents:
diff changeset
325 // FIXME: Omitting MAP_STACK here works in current kernels but might break
kono
parents:
diff changeset
326 // in the future.
kono
parents:
diff changeset
327 guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_,
kono
parents:
diff changeset
328 "ScopedStackWithGuard");
kono
parents:
diff changeset
329 CHECK(MprotectNoAccess((uptr)guard_start_, guard_size_));
kono
parents:
diff changeset
330 }
kono
parents:
diff changeset
331 ~ScopedStackSpaceWithGuard() {
kono
parents:
diff changeset
332 UnmapOrDie((void *)guard_start_, stack_size_ + guard_size_);
kono
parents:
diff changeset
333 }
kono
parents:
diff changeset
334 void *Bottom() const {
kono
parents:
diff changeset
335 return (void *)(guard_start_ + stack_size_ + guard_size_);
kono
parents:
diff changeset
336 }
kono
parents:
diff changeset
337
kono
parents:
diff changeset
338 private:
kono
parents:
diff changeset
339 uptr stack_size_;
kono
parents:
diff changeset
340 uptr guard_size_;
kono
parents:
diff changeset
341 uptr guard_start_;
kono
parents:
diff changeset
342 };
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 // We have a limitation on the stack frame size, so some stuff had to be moved
kono
parents:
diff changeset
345 // into globals.
kono
parents:
diff changeset
346 static __sanitizer_sigset_t blocked_sigset;
kono
parents:
diff changeset
347 static __sanitizer_sigset_t old_sigset;
kono
parents:
diff changeset
348
kono
parents:
diff changeset
349 class StopTheWorldScope {
kono
parents:
diff changeset
350 public:
kono
parents:
diff changeset
351 StopTheWorldScope() {
kono
parents:
diff changeset
352 // Make this process dumpable. Processes that are not dumpable cannot be
kono
parents:
diff changeset
353 // attached to.
kono
parents:
diff changeset
354 process_was_dumpable_ = internal_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
kono
parents:
diff changeset
355 if (!process_was_dumpable_)
kono
parents:
diff changeset
356 internal_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
kono
parents:
diff changeset
357 }
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 ~StopTheWorldScope() {
kono
parents:
diff changeset
360 // Restore the dumpable flag.
kono
parents:
diff changeset
361 if (!process_was_dumpable_)
kono
parents:
diff changeset
362 internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
kono
parents:
diff changeset
363 }
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 private:
kono
parents:
diff changeset
366 int process_was_dumpable_;
kono
parents:
diff changeset
367 };
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 // When sanitizer output is being redirected to file (i.e. by using log_path),
kono
parents:
diff changeset
370 // the tracer should write to the parent's log instead of trying to open a new
kono
parents:
diff changeset
371 // file. Alert the logging code to the fact that we have a tracer.
kono
parents:
diff changeset
372 struct ScopedSetTracerPID {
kono
parents:
diff changeset
373 explicit ScopedSetTracerPID(uptr tracer_pid) {
kono
parents:
diff changeset
374 stoptheworld_tracer_pid = tracer_pid;
kono
parents:
diff changeset
375 stoptheworld_tracer_ppid = internal_getpid();
kono
parents:
diff changeset
376 }
kono
parents:
diff changeset
377 ~ScopedSetTracerPID() {
kono
parents:
diff changeset
378 stoptheworld_tracer_pid = 0;
kono
parents:
diff changeset
379 stoptheworld_tracer_ppid = 0;
kono
parents:
diff changeset
380 }
kono
parents:
diff changeset
381 };
kono
parents:
diff changeset
382
kono
parents:
diff changeset
383 void StopTheWorld(StopTheWorldCallback callback, void *argument) {
kono
parents:
diff changeset
384 StopTheWorldScope in_stoptheworld;
kono
parents:
diff changeset
385 // Prepare the arguments for TracerThread.
kono
parents:
diff changeset
386 struct TracerThreadArgument tracer_thread_argument;
kono
parents:
diff changeset
387 tracer_thread_argument.callback = callback;
kono
parents:
diff changeset
388 tracer_thread_argument.callback_argument = argument;
kono
parents:
diff changeset
389 tracer_thread_argument.parent_pid = internal_getpid();
kono
parents:
diff changeset
390 atomic_store(&tracer_thread_argument.done, 0, memory_order_relaxed);
kono
parents:
diff changeset
391 const uptr kTracerStackSize = 2 * 1024 * 1024;
kono
parents:
diff changeset
392 ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize);
kono
parents:
diff changeset
393 // Block the execution of TracerThread until after we have set ptrace
kono
parents:
diff changeset
394 // permissions.
kono
parents:
diff changeset
395 tracer_thread_argument.mutex.Lock();
kono
parents:
diff changeset
396 // Signal handling story.
kono
parents:
diff changeset
397 // We don't want async signals to be delivered to the tracer thread,
kono
parents:
diff changeset
398 // so we block all async signals before creating the thread. An async signal
kono
parents:
diff changeset
399 // handler can temporary modify errno, which is shared with this thread.
kono
parents:
diff changeset
400 // We ought to use pthread_sigmask here, because sigprocmask has undefined
kono
parents:
diff changeset
401 // behavior in multithreaded programs. However, on linux sigprocmask is
kono
parents:
diff changeset
402 // equivalent to pthread_sigmask with the exception that pthread_sigmask
kono
parents:
diff changeset
403 // does not allow to block some signals used internally in pthread
kono
parents:
diff changeset
404 // implementation. We are fine with blocking them here, we are really not
kono
parents:
diff changeset
405 // going to pthread_cancel the thread.
kono
parents:
diff changeset
406 // The tracer thread should not raise any synchronous signals. But in case it
kono
parents:
diff changeset
407 // does, we setup a special handler for sync signals that properly kills the
kono
parents:
diff changeset
408 // parent as well. Note: we don't pass CLONE_SIGHAND to clone, so handlers
kono
parents:
diff changeset
409 // in the tracer thread won't interfere with user program. Double note: if a
kono
parents:
diff changeset
410 // user does something along the lines of 'kill -11 pid', that can kill the
kono
parents:
diff changeset
411 // process even if user setup own handler for SEGV.
kono
parents:
diff changeset
412 // Thing to watch out for: this code should not change behavior of user code
kono
parents:
diff changeset
413 // in any observable way. In particular it should not override user signal
kono
parents:
diff changeset
414 // handlers.
kono
parents:
diff changeset
415 internal_sigfillset(&blocked_sigset);
kono
parents:
diff changeset
416 for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++)
kono
parents:
diff changeset
417 internal_sigdelset(&blocked_sigset, kSyncSignals[i]);
kono
parents:
diff changeset
418 int rv = internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset);
kono
parents:
diff changeset
419 CHECK_EQ(rv, 0);
kono
parents:
diff changeset
420 uptr tracer_pid = internal_clone(
kono
parents:
diff changeset
421 TracerThread, tracer_stack.Bottom(),
kono
parents:
diff changeset
422 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,
kono
parents:
diff changeset
423 &tracer_thread_argument, nullptr /* parent_tidptr */,
kono
parents:
diff changeset
424 nullptr /* newtls */, nullptr /* child_tidptr */);
kono
parents:
diff changeset
425 internal_sigprocmask(SIG_SETMASK, &old_sigset, 0);
kono
parents:
diff changeset
426 int local_errno = 0;
kono
parents:
diff changeset
427 if (internal_iserror(tracer_pid, &local_errno)) {
kono
parents:
diff changeset
428 VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno);
kono
parents:
diff changeset
429 tracer_thread_argument.mutex.Unlock();
kono
parents:
diff changeset
430 } else {
kono
parents:
diff changeset
431 ScopedSetTracerPID scoped_set_tracer_pid(tracer_pid);
kono
parents:
diff changeset
432 // On some systems we have to explicitly declare that we want to be traced
kono
parents:
diff changeset
433 // by the tracer thread.
kono
parents:
diff changeset
434 #ifdef PR_SET_PTRACER
kono
parents:
diff changeset
435 internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0);
kono
parents:
diff changeset
436 #endif
kono
parents:
diff changeset
437 // Allow the tracer thread to start.
kono
parents:
diff changeset
438 tracer_thread_argument.mutex.Unlock();
kono
parents:
diff changeset
439 // NOTE: errno is shared between this thread and the tracer thread.
kono
parents:
diff changeset
440 // internal_waitpid() may call syscall() which can access/spoil errno,
kono
parents:
diff changeset
441 // so we can't call it now. Instead we for the tracer thread to finish using
kono
parents:
diff changeset
442 // the spin loop below. Man page for sched_yield() says "In the Linux
kono
parents:
diff changeset
443 // implementation, sched_yield() always succeeds", so let's hope it does not
kono
parents:
diff changeset
444 // spoil errno. Note that this spin loop runs only for brief periods before
kono
parents:
diff changeset
445 // the tracer thread has suspended us and when it starts unblocking threads.
kono
parents:
diff changeset
446 while (atomic_load(&tracer_thread_argument.done, memory_order_relaxed) == 0)
kono
parents:
diff changeset
447 sched_yield();
kono
parents:
diff changeset
448 // Now the tracer thread is about to exit and does not touch errno,
kono
parents:
diff changeset
449 // wait for it.
kono
parents:
diff changeset
450 for (;;) {
kono
parents:
diff changeset
451 uptr waitpid_status = internal_waitpid(tracer_pid, nullptr, __WALL);
kono
parents:
diff changeset
452 if (!internal_iserror(waitpid_status, &local_errno))
kono
parents:
diff changeset
453 break;
kono
parents:
diff changeset
454 if (local_errno == EINTR)
kono
parents:
diff changeset
455 continue;
kono
parents:
diff changeset
456 VReport(1, "Waiting on the tracer thread failed (errno %d).\n",
kono
parents:
diff changeset
457 local_errno);
kono
parents:
diff changeset
458 break;
kono
parents:
diff changeset
459 }
kono
parents:
diff changeset
460 }
kono
parents:
diff changeset
461 }
kono
parents:
diff changeset
462
kono
parents:
diff changeset
463 // Platform-specific methods from SuspendedThreadsList.
kono
parents:
diff changeset
464 #if SANITIZER_ANDROID && defined(__arm__)
kono
parents:
diff changeset
465 typedef pt_regs regs_struct;
kono
parents:
diff changeset
466 #define REG_SP ARM_sp
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 #elif SANITIZER_LINUX && defined(__arm__)
kono
parents:
diff changeset
469 typedef user_regs regs_struct;
kono
parents:
diff changeset
470 #define REG_SP uregs[13]
kono
parents:
diff changeset
471
kono
parents:
diff changeset
472 #elif defined(__i386__) || defined(__x86_64__)
kono
parents:
diff changeset
473 typedef user_regs_struct regs_struct;
kono
parents:
diff changeset
474 #if defined(__i386__)
kono
parents:
diff changeset
475 #define REG_SP esp
kono
parents:
diff changeset
476 #else
kono
parents:
diff changeset
477 #define REG_SP rsp
kono
parents:
diff changeset
478 #endif
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 #elif defined(__powerpc__) || defined(__powerpc64__)
kono
parents:
diff changeset
481 typedef pt_regs regs_struct;
kono
parents:
diff changeset
482 #define REG_SP gpr[PT_R1]
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 #elif defined(__mips__)
kono
parents:
diff changeset
485 typedef struct user regs_struct;
kono
parents:
diff changeset
486 # if SANITIZER_ANDROID
kono
parents:
diff changeset
487 # define REG_SP regs[EF_R29]
kono
parents:
diff changeset
488 # else
kono
parents:
diff changeset
489 # define REG_SP regs[EF_REG29]
kono
parents:
diff changeset
490 # endif
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 #elif defined(__aarch64__)
kono
parents:
diff changeset
493 typedef struct user_pt_regs regs_struct;
kono
parents:
diff changeset
494 #define REG_SP sp
kono
parents:
diff changeset
495 #define ARCH_IOVEC_FOR_GETREGSET
kono
parents:
diff changeset
496
kono
parents:
diff changeset
497 #elif defined(__s390__)
kono
parents:
diff changeset
498 typedef _user_regs_struct regs_struct;
kono
parents:
diff changeset
499 #define REG_SP gprs[15]
kono
parents:
diff changeset
500 #define ARCH_IOVEC_FOR_GETREGSET
kono
parents:
diff changeset
501
kono
parents:
diff changeset
502 #else
kono
parents:
diff changeset
503 #error "Unsupported architecture"
kono
parents:
diff changeset
504 #endif // SANITIZER_ANDROID && defined(__arm__)
kono
parents:
diff changeset
505
kono
parents:
diff changeset
506 tid_t SuspendedThreadsListLinux::GetThreadID(uptr index) const {
kono
parents:
diff changeset
507 CHECK_LT(index, thread_ids_.size());
kono
parents:
diff changeset
508 return thread_ids_[index];
kono
parents:
diff changeset
509 }
kono
parents:
diff changeset
510
kono
parents:
diff changeset
511 uptr SuspendedThreadsListLinux::ThreadCount() const {
kono
parents:
diff changeset
512 return thread_ids_.size();
kono
parents:
diff changeset
513 }
kono
parents:
diff changeset
514
kono
parents:
diff changeset
515 bool SuspendedThreadsListLinux::ContainsTid(tid_t thread_id) const {
kono
parents:
diff changeset
516 for (uptr i = 0; i < thread_ids_.size(); i++) {
kono
parents:
diff changeset
517 if (thread_ids_[i] == thread_id) return true;
kono
parents:
diff changeset
518 }
kono
parents:
diff changeset
519 return false;
kono
parents:
diff changeset
520 }
kono
parents:
diff changeset
521
kono
parents:
diff changeset
522 void SuspendedThreadsListLinux::Append(tid_t tid) {
kono
parents:
diff changeset
523 thread_ids_.push_back(tid);
kono
parents:
diff changeset
524 }
kono
parents:
diff changeset
525
kono
parents:
diff changeset
526 PtraceRegistersStatus SuspendedThreadsListLinux::GetRegistersAndSP(
kono
parents:
diff changeset
527 uptr index, uptr *buffer, uptr *sp) const {
kono
parents:
diff changeset
528 pid_t tid = GetThreadID(index);
kono
parents:
diff changeset
529 regs_struct regs;
kono
parents:
diff changeset
530 int pterrno;
kono
parents:
diff changeset
531 #ifdef ARCH_IOVEC_FOR_GETREGSET
kono
parents:
diff changeset
532 struct iovec regset_io;
kono
parents:
diff changeset
533 regset_io.iov_base = &regs;
kono
parents:
diff changeset
534 regset_io.iov_len = sizeof(regs_struct);
kono
parents:
diff changeset
535 bool isErr = internal_iserror(internal_ptrace(PTRACE_GETREGSET, tid,
kono
parents:
diff changeset
536 (void*)NT_PRSTATUS, (void*)&regset_io),
kono
parents:
diff changeset
537 &pterrno);
kono
parents:
diff changeset
538 #else
kono
parents:
diff changeset
539 bool isErr = internal_iserror(internal_ptrace(PTRACE_GETREGS, tid, nullptr,
kono
parents:
diff changeset
540 &regs), &pterrno);
kono
parents:
diff changeset
541 #endif
kono
parents:
diff changeset
542 if (isErr) {
kono
parents:
diff changeset
543 VReport(1, "Could not get registers from thread %d (errno %d).\n", tid,
kono
parents:
diff changeset
544 pterrno);
kono
parents:
diff changeset
545 // ESRCH means that the given thread is not suspended or already dead.
kono
parents:
diff changeset
546 // Therefore it's unsafe to inspect its data (e.g. walk through stack) and
kono
parents:
diff changeset
547 // we should notify caller about this.
kono
parents:
diff changeset
548 return pterrno == ESRCH ? REGISTERS_UNAVAILABLE_FATAL
kono
parents:
diff changeset
549 : REGISTERS_UNAVAILABLE;
kono
parents:
diff changeset
550 }
kono
parents:
diff changeset
551
kono
parents:
diff changeset
552 *sp = regs.REG_SP;
kono
parents:
diff changeset
553 internal_memcpy(buffer, &regs, sizeof(regs));
kono
parents:
diff changeset
554 return REGISTERS_AVAILABLE;
kono
parents:
diff changeset
555 }
kono
parents:
diff changeset
556
kono
parents:
diff changeset
557 uptr SuspendedThreadsListLinux::RegisterCount() const {
kono
parents:
diff changeset
558 return sizeof(regs_struct) / sizeof(uptr);
kono
parents:
diff changeset
559 }
kono
parents:
diff changeset
560 } // namespace __sanitizer
kono
parents:
diff changeset
561
kono
parents:
diff changeset
562 #endif // SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__)
kono
parents:
diff changeset
563 // || defined(__aarch64__) || defined(__powerpc64__)
kono
parents:
diff changeset
564 // || defined(__s390__) || defined(__i386__) || defined(__arm__)