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