2019-08-01 02:51:27 +08:00
|
|
|
//===-- sanitizer_stoptheworld_linux_libcdep.cpp --------------------------===//
|
2013-03-15 22:37:21 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-03-15 22:37:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// See sanitizer_stoptheworld.h for details.
|
|
|
|
// This implementation was inspired by Markus Gutschke's linuxthreads.cc.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-03-19 22:33:38 +08:00
|
|
|
#include "sanitizer_platform.h"
|
2015-10-01 07:52:54 +08:00
|
|
|
|
2020-10-04 17:38:06 +08:00
|
|
|
#if SANITIZER_LINUX && \
|
|
|
|
(defined(__x86_64__) || defined(__mips__) || defined(__aarch64__) || \
|
|
|
|
defined(__powerpc64__) || defined(__s390__) || defined(__i386__) || \
|
|
|
|
defined(__arm__) || SANITIZER_RISCV64)
|
2013-03-15 22:37:21 +08:00
|
|
|
|
|
|
|
#include "sanitizer_stoptheworld.h"
|
|
|
|
|
2013-10-15 20:57:59 +08:00
|
|
|
#include "sanitizer_platform_limits_posix.h"
|
2015-03-05 22:37:28 +08:00
|
|
|
#include "sanitizer_atomic.h"
|
2013-10-15 20:57:59 +08:00
|
|
|
|
2013-03-15 22:37:21 +08:00
|
|
|
#include <errno.h>
|
2013-09-02 19:36:19 +08:00
|
|
|
#include <sched.h> // for CLONE_* definitions
|
2013-03-15 22:37:21 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/prctl.h> // for PR_* definitions
|
|
|
|
#include <sys/ptrace.h> // for PTRACE_* definitions
|
|
|
|
#include <sys/types.h> // for pid_t
|
2015-08-05 23:17:59 +08:00
|
|
|
#include <sys/uio.h> // for iovec
|
|
|
|
#include <elf.h> // for NT_PRSTATUS
|
2020-10-04 17:38:06 +08:00
|
|
|
#if (defined(__aarch64__) || SANITIZER_RISCV64) && !SANITIZER_ANDROID
|
2015-08-06 02:34:20 +08:00
|
|
|
// GLIBC 2.20+ sys/user does not include asm/ptrace.h
|
2017-04-18 15:22:26 +08:00
|
|
|
# include <asm/ptrace.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/user.h> // for user_regs_struct
|
|
|
|
#if SANITIZER_ANDROID && SANITIZER_MIPS
|
|
|
|
# include <asm/reg.h> // for mips SP register in sys/user.h
|
2013-04-03 15:06:10 +08:00
|
|
|
#endif
|
2013-03-15 22:37:21 +08:00
|
|
|
#include <sys/wait.h> // for signal-related stuff
|
|
|
|
|
2013-10-15 20:57:59 +08:00
|
|
|
#ifdef sa_handler
|
|
|
|
# undef sa_handler
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef sa_sigaction
|
|
|
|
# undef sa_sigaction
|
|
|
|
#endif
|
|
|
|
|
2013-03-15 22:37:21 +08:00
|
|
|
#include "sanitizer_common.h"
|
2013-10-15 23:35:56 +08:00
|
|
|
#include "sanitizer_flags.h"
|
2013-03-15 22:37:21 +08:00
|
|
|
#include "sanitizer_libc.h"
|
|
|
|
#include "sanitizer_linux.h"
|
|
|
|
#include "sanitizer_mutex.h"
|
2013-03-18 16:09:42 +08:00
|
|
|
#include "sanitizer_placement_new.h"
|
2013-03-15 22:37:21 +08:00
|
|
|
|
2017-11-08 15:25:19 +08:00
|
|
|
// Sufficiently old kernel headers don't provide this value, but we can still
|
|
|
|
// call prctl with it. If the runtime kernel is new enough, the prctl call will
|
|
|
|
// have the desired effect; if the kernel is too old, the call will error and we
|
|
|
|
// can ignore said error.
|
|
|
|
#ifndef PR_SET_PTRACER
|
|
|
|
#define PR_SET_PTRACER 0x59616d61
|
|
|
|
#endif
|
|
|
|
|
2013-03-15 22:37:21 +08:00
|
|
|
// This module works by spawning a Linux task which then attaches to every
|
|
|
|
// thread in the caller process with ptrace. This suspends the threads, and
|
|
|
|
// PTRACE_GETREGS can then be used to obtain their register state. The callback
|
|
|
|
// supplied to StopTheWorld() is run in the tracer task while the threads are
|
|
|
|
// suspended.
|
|
|
|
// The tracer task must be placed in a different thread group for ptrace to
|
|
|
|
// work, so it cannot be spawned as a pthread. Instead, we use the low-level
|
|
|
|
// clone() interface (we want to share the address space with the caller
|
|
|
|
// process, so we prefer clone() over fork()).
|
|
|
|
//
|
2013-10-15 19:54:38 +08:00
|
|
|
// We don't use any libc functions, relying instead on direct syscalls. There
|
|
|
|
// are two reasons for this:
|
2013-03-15 22:37:21 +08:00
|
|
|
// 1. calling a library function while threads are suspended could cause a
|
|
|
|
// deadlock, if one of the treads happens to be holding a libc lock;
|
|
|
|
// 2. it's generally not safe to call libc functions from the tracer task,
|
|
|
|
// because clone() does not set up a thread-local storage for it. Any
|
|
|
|
// thread-local variables used by libc will be shared between the tracer task
|
|
|
|
// and the thread which spawned it.
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
2015-03-05 22:37:28 +08:00
|
|
|
|
2020-11-03 09:33:22 +08:00
|
|
|
class SuspendedThreadsListLinux final : public SuspendedThreadsList {
|
2017-04-18 04:29:38 +08:00
|
|
|
public:
|
2018-05-07 13:56:24 +08:00
|
|
|
SuspendedThreadsListLinux() { thread_ids_.reserve(1024); }
|
2017-04-18 04:29:38 +08:00
|
|
|
|
2020-07-21 06:13:22 +08:00
|
|
|
tid_t GetThreadID(uptr index) const override;
|
2020-07-21 05:38:10 +08:00
|
|
|
uptr ThreadCount() const override;
|
2017-04-18 04:29:38 +08:00
|
|
|
bool ContainsTid(tid_t thread_id) const;
|
|
|
|
void Append(tid_t tid);
|
|
|
|
|
2020-09-16 16:14:55 +08:00
|
|
|
PtraceRegistersStatus GetRegistersAndSP(uptr index,
|
|
|
|
InternalMmapVector<uptr> *buffer,
|
2020-07-21 05:38:10 +08:00
|
|
|
uptr *sp) const override;
|
2017-04-18 04:29:38 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
InternalMmapVector<tid_t> thread_ids_;
|
|
|
|
};
|
|
|
|
|
2015-03-05 22:37:28 +08:00
|
|
|
// Structure for passing arguments into the tracer thread.
|
|
|
|
struct TracerThreadArgument {
|
|
|
|
StopTheWorldCallback callback;
|
|
|
|
void *callback_argument;
|
|
|
|
// The tracer thread waits on this mutex while the parent finishes its
|
|
|
|
// preparations.
|
|
|
|
BlockingMutex mutex;
|
|
|
|
// Tracer thread signals its completion by setting done.
|
|
|
|
atomic_uintptr_t done;
|
|
|
|
uptr parent_pid;
|
|
|
|
};
|
|
|
|
|
2013-03-15 22:37:21 +08:00
|
|
|
// This class handles thread suspending/unsuspending in the tracer thread.
|
|
|
|
class ThreadSuspender {
|
|
|
|
public:
|
2015-03-05 22:37:28 +08:00
|
|
|
explicit ThreadSuspender(pid_t pid, TracerThreadArgument *arg)
|
|
|
|
: arg(arg)
|
|
|
|
, pid_(pid) {
|
2013-03-15 22:37:21 +08:00
|
|
|
CHECK_GE(pid, 0);
|
|
|
|
}
|
|
|
|
bool SuspendAllThreads();
|
|
|
|
void ResumeAllThreads();
|
|
|
|
void KillAllThreads();
|
2017-04-18 04:29:38 +08:00
|
|
|
SuspendedThreadsListLinux &suspended_threads_list() {
|
2013-03-15 22:37:21 +08:00
|
|
|
return suspended_threads_list_;
|
|
|
|
}
|
2015-03-05 22:37:28 +08:00
|
|
|
TracerThreadArgument *arg;
|
2013-03-15 22:37:21 +08:00
|
|
|
private:
|
2017-04-18 04:29:38 +08:00
|
|
|
SuspendedThreadsListLinux suspended_threads_list_;
|
2013-03-15 22:37:21 +08:00
|
|
|
pid_t pid_;
|
2017-04-18 02:17:38 +08:00
|
|
|
bool SuspendThread(tid_t thread_id);
|
2013-03-15 22:37:21 +08:00
|
|
|
};
|
|
|
|
|
2017-04-18 02:17:38 +08:00
|
|
|
bool ThreadSuspender::SuspendThread(tid_t tid) {
|
2013-03-15 22:37:21 +08:00
|
|
|
// Are we already attached to this thread?
|
|
|
|
// Currently this check takes linear time, however the number of threads is
|
|
|
|
// usually small.
|
2017-04-18 04:29:38 +08:00
|
|
|
if (suspended_threads_list_.ContainsTid(tid)) return false;
|
2013-05-08 22:43:49 +08:00
|
|
|
int pterrno;
|
2015-10-01 07:52:54 +08:00
|
|
|
if (internal_iserror(internal_ptrace(PTRACE_ATTACH, tid, nullptr, nullptr),
|
2013-05-08 22:43:49 +08:00
|
|
|
&pterrno)) {
|
2013-03-15 22:37:21 +08:00
|
|
|
// Either the thread is dead, or something prevented us from attaching.
|
|
|
|
// Log this event and move on.
|
2017-04-18 09:08:00 +08:00
|
|
|
VReport(1, "Could not attach to thread %zu (errno %d).\n", (uptr)tid,
|
|
|
|
pterrno);
|
2013-03-15 22:37:21 +08:00
|
|
|
return false;
|
|
|
|
} else {
|
2017-04-18 09:08:00 +08:00
|
|
|
VReport(2, "Attached to thread %zu.\n", (uptr)tid);
|
2013-03-15 22:37:21 +08:00
|
|
|
// The thread is not guaranteed to stop before ptrace returns, so we must
|
2015-02-19 17:02:29 +08:00
|
|
|
// wait on it. Note: if the thread receives a signal concurrently,
|
|
|
|
// we can get notification about the signal before notification about stop.
|
|
|
|
// In such case we need to forward the signal to the thread, otherwise
|
|
|
|
// the signal will be missed (as we do PTRACE_DETACH with arg=0) and
|
|
|
|
// any logic relying on signals will break. After forwarding we need to
|
|
|
|
// continue to wait for stopping, because the thread is not stopped yet.
|
|
|
|
// We do ignore delivery of SIGSTOP, because we want to make stop-the-world
|
|
|
|
// as invisible as possible.
|
|
|
|
for (;;) {
|
|
|
|
int status;
|
|
|
|
uptr waitpid_status;
|
|
|
|
HANDLE_EINTR(waitpid_status, internal_waitpid(tid, &status, __WALL));
|
|
|
|
int wperrno;
|
|
|
|
if (internal_iserror(waitpid_status, &wperrno)) {
|
|
|
|
// Got a ECHILD error. I don't think this situation is possible, but it
|
|
|
|
// doesn't hurt to report it.
|
2017-04-18 09:08:00 +08:00
|
|
|
VReport(1, "Waiting on thread %zu failed, detaching (errno %d).\n",
|
|
|
|
(uptr)tid, wperrno);
|
2015-10-01 07:52:54 +08:00
|
|
|
internal_ptrace(PTRACE_DETACH, tid, nullptr, nullptr);
|
2015-02-19 17:02:29 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (WIFSTOPPED(status) && WSTOPSIG(status) != SIGSTOP) {
|
2015-10-01 08:48:07 +08:00
|
|
|
internal_ptrace(PTRACE_CONT, tid, nullptr,
|
|
|
|
(void*)(uptr)WSTOPSIG(status));
|
2015-02-19 17:02:29 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2013-03-15 22:37:21 +08:00
|
|
|
}
|
2015-02-19 17:02:29 +08:00
|
|
|
suspended_threads_list_.Append(tid);
|
2013-03-15 22:37:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadSuspender::ResumeAllThreads() {
|
2017-04-18 04:29:38 +08:00
|
|
|
for (uptr i = 0; i < suspended_threads_list_.ThreadCount(); i++) {
|
2013-03-15 22:37:21 +08:00
|
|
|
pid_t tid = suspended_threads_list_.GetThreadID(i);
|
2013-05-08 22:43:49 +08:00
|
|
|
int pterrno;
|
2015-10-01 07:52:54 +08:00
|
|
|
if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, nullptr, nullptr),
|
2013-05-08 22:43:49 +08:00
|
|
|
&pterrno)) {
|
2015-03-03 01:36:02 +08:00
|
|
|
VReport(2, "Detached from thread %d.\n", tid);
|
2013-03-15 22:37:21 +08:00
|
|
|
} else {
|
|
|
|
// Either the thread is dead, or we are already detached.
|
|
|
|
// The latter case is possible, for instance, if this function was called
|
|
|
|
// from a signal handler.
|
2013-12-05 20:04:51 +08:00
|
|
|
VReport(1, "Could not detach from thread %d (errno %d).\n", tid, pterrno);
|
2013-03-15 22:37:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadSuspender::KillAllThreads() {
|
2017-04-18 04:29:38 +08:00
|
|
|
for (uptr i = 0; i < suspended_threads_list_.ThreadCount(); i++)
|
2013-03-15 22:37:21 +08:00
|
|
|
internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i),
|
2015-10-01 07:52:54 +08:00
|
|
|
nullptr, nullptr);
|
2013-03-15 22:37:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ThreadSuspender::SuspendAllThreads() {
|
2013-04-05 15:41:21 +08:00
|
|
|
ThreadLister thread_lister(pid_);
|
2018-05-15 09:39:13 +08:00
|
|
|
bool retry = true;
|
2018-05-10 05:21:26 +08:00
|
|
|
InternalMmapVector<tid_t> threads;
|
2018-05-08 07:29:48 +08:00
|
|
|
threads.reserve(128);
|
2018-05-15 09:39:13 +08:00
|
|
|
for (int i = 0; i < 30 && retry; ++i) {
|
2018-05-10 12:16:44 +08:00
|
|
|
retry = false;
|
2018-05-10 12:02:59 +08:00
|
|
|
switch (thread_lister.ListThreads(&threads)) {
|
|
|
|
case ThreadLister::Error:
|
|
|
|
ResumeAllThreads();
|
|
|
|
return false;
|
|
|
|
case ThreadLister::Incomplete:
|
2018-05-10 12:16:44 +08:00
|
|
|
retry = true;
|
2018-05-10 12:02:59 +08:00
|
|
|
break;
|
|
|
|
case ThreadLister::Ok:
|
|
|
|
break;
|
2018-05-08 07:29:48 +08:00
|
|
|
}
|
2019-09-12 10:20:36 +08:00
|
|
|
for (tid_t tid : threads) {
|
2013-03-15 22:37:21 +08:00
|
|
|
if (SuspendThread(tid))
|
2018-05-10 12:16:44 +08:00
|
|
|
retry = true;
|
2019-09-12 10:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 16:16:23 +08:00
|
|
|
return suspended_threads_list_.ThreadCount();
|
2013-03-15 22:37:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pointer to the ThreadSuspender instance for use in signal handler.
|
2015-10-01 07:52:54 +08:00
|
|
|
static ThreadSuspender *thread_suspender_instance = nullptr;
|
2013-03-15 22:37:21 +08:00
|
|
|
|
2015-03-05 22:37:28 +08:00
|
|
|
// Synchronous signals that should not be blocked.
|
|
|
|
static const int kSyncSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV, SIGBUS,
|
|
|
|
SIGXCPU, SIGXFSZ };
|
2013-03-15 22:37:21 +08:00
|
|
|
|
2015-08-25 06:21:44 +08:00
|
|
|
static void TracerThreadDieCallback() {
|
|
|
|
// Generally a call to Die() in the tracer thread should be fatal to the
|
|
|
|
// parent process as well, because they share the address space.
|
|
|
|
// This really only works correctly if all the threads are suspended at this
|
|
|
|
// point. So we correctly handle calls to Die() from within the callback, but
|
|
|
|
// not those that happen before or after the callback. Hopefully there aren't
|
|
|
|
// a lot of opportunities for that to happen...
|
|
|
|
ThreadSuspender *inst = thread_suspender_instance;
|
2015-10-01 07:52:54 +08:00
|
|
|
if (inst && stoptheworld_tracer_pid == internal_getpid()) {
|
2015-08-25 06:21:44 +08:00
|
|
|
inst->KillAllThreads();
|
2015-10-01 07:52:54 +08:00
|
|
|
thread_suspender_instance = nullptr;
|
2015-08-25 06:21:44 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-26 21:20:31 +08:00
|
|
|
|
2013-03-15 22:37:21 +08:00
|
|
|
// Signal handler to wake up suspended threads when the tracer thread dies.
|
2017-11-10 12:27:47 +08:00
|
|
|
static void TracerThreadSignalHandler(int signum, __sanitizer_siginfo *siginfo,
|
|
|
|
void *uctx) {
|
2017-09-14 10:48:41 +08:00
|
|
|
SignalContext ctx(siginfo, uctx);
|
2016-02-13 04:20:51 +08:00
|
|
|
Printf("Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n", signum,
|
|
|
|
ctx.addr, ctx.pc, ctx.sp);
|
2015-03-05 22:37:28 +08:00
|
|
|
ThreadSuspender *inst = thread_suspender_instance;
|
2015-10-01 07:52:54 +08:00
|
|
|
if (inst) {
|
2013-03-15 22:37:21 +08:00
|
|
|
if (signum == SIGABRT)
|
2015-03-05 22:37:28 +08:00
|
|
|
inst->KillAllThreads();
|
2013-03-15 22:37:21 +08:00
|
|
|
else
|
2015-03-05 22:37:28 +08:00
|
|
|
inst->ResumeAllThreads();
|
2015-08-25 06:21:44 +08:00
|
|
|
RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback));
|
2015-10-01 07:52:54 +08:00
|
|
|
thread_suspender_instance = nullptr;
|
2015-03-05 22:37:28 +08:00
|
|
|
atomic_store(&inst->arg->done, 1, memory_order_relaxed);
|
2013-03-15 22:37:21 +08:00
|
|
|
}
|
|
|
|
internal__exit((signum == SIGABRT) ? 1 : 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size of alternative stack for signal handlers in the tracer thread.
|
2017-10-15 12:18:29 +08:00
|
|
|
static const int kHandlerStackSize = 8192;
|
2013-03-15 22:37:21 +08:00
|
|
|
|
|
|
|
// This function will be run as a cloned task.
|
2013-03-18 14:27:13 +08:00
|
|
|
static int TracerThread(void* argument) {
|
2013-03-15 22:37:21 +08:00
|
|
|
TracerThreadArgument *tracer_thread_argument =
|
|
|
|
(TracerThreadArgument *)argument;
|
|
|
|
|
2013-10-09 02:01:03 +08:00
|
|
|
internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
|
|
|
|
// Check if parent is already dead.
|
2013-10-09 21:36:20 +08:00
|
|
|
if (internal_getppid() != tracer_thread_argument->parent_pid)
|
2013-10-09 02:01:03 +08:00
|
|
|
internal__exit(4);
|
|
|
|
|
2013-03-15 22:37:21 +08:00
|
|
|
// Wait for the parent thread to finish preparations.
|
2013-03-18 14:27:13 +08:00
|
|
|
tracer_thread_argument->mutex.Lock();
|
|
|
|
tracer_thread_argument->mutex.Unlock();
|
2013-03-15 22:37:21 +08:00
|
|
|
|
2015-08-25 06:21:44 +08:00
|
|
|
RAW_CHECK(AddDieCallback(TracerThreadDieCallback));
|
2013-08-26 21:20:31 +08:00
|
|
|
|
2015-03-05 22:37:28 +08:00
|
|
|
ThreadSuspender thread_suspender(internal_getppid(), tracer_thread_argument);
|
2013-03-15 22:37:21 +08:00
|
|
|
// Global pointer for the signal handler.
|
|
|
|
thread_suspender_instance = &thread_suspender;
|
|
|
|
|
|
|
|
// Alternate stack for signal handling.
|
2018-05-07 13:56:36 +08:00
|
|
|
InternalMmapVector<char> handler_stack_memory(kHandlerStackSize);
|
2017-07-14 05:59:01 +08:00
|
|
|
stack_t handler_stack;
|
2013-03-15 22:37:21 +08:00
|
|
|
internal_memset(&handler_stack, 0, sizeof(handler_stack));
|
|
|
|
handler_stack.ss_sp = handler_stack_memory.data();
|
|
|
|
handler_stack.ss_size = kHandlerStackSize;
|
2015-10-01 07:52:54 +08:00
|
|
|
internal_sigaltstack(&handler_stack, nullptr);
|
2013-03-15 22:37:21 +08:00
|
|
|
|
2015-03-05 22:37:28 +08:00
|
|
|
// Install our handler for synchronous signals. Other signals should be
|
|
|
|
// blocked by the mask we inherited from the parent thread.
|
|
|
|
for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++) {
|
|
|
|
__sanitizer_sigaction act;
|
|
|
|
internal_memset(&act, 0, sizeof(act));
|
|
|
|
act.sigaction = TracerThreadSignalHandler;
|
|
|
|
act.sa_flags = SA_ONSTACK | SA_SIGINFO;
|
|
|
|
internal_sigaction_norestorer(kSyncSignals[i], &act, 0);
|
2013-03-15 22:37:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int exit_code = 0;
|
|
|
|
if (!thread_suspender.SuspendAllThreads()) {
|
2013-12-05 20:04:51 +08:00
|
|
|
VReport(1, "Failed suspending threads.\n");
|
2013-03-15 22:37:21 +08:00
|
|
|
exit_code = 3;
|
|
|
|
} else {
|
|
|
|
tracer_thread_argument->callback(thread_suspender.suspended_threads_list(),
|
|
|
|
tracer_thread_argument->callback_argument);
|
|
|
|
thread_suspender.ResumeAllThreads();
|
|
|
|
exit_code = 0;
|
|
|
|
}
|
2015-08-25 06:21:44 +08:00
|
|
|
RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback));
|
2015-10-01 07:52:54 +08:00
|
|
|
thread_suspender_instance = nullptr;
|
2015-03-05 22:37:28 +08:00
|
|
|
atomic_store(&tracer_thread_argument->done, 1, memory_order_relaxed);
|
2013-03-15 22:37:21 +08:00
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
|
2013-04-01 22:38:56 +08:00
|
|
|
class ScopedStackSpaceWithGuard {
|
|
|
|
public:
|
|
|
|
explicit ScopedStackSpaceWithGuard(uptr stack_size) {
|
|
|
|
stack_size_ = stack_size;
|
|
|
|
guard_size_ = GetPageSizeCached();
|
|
|
|
// FIXME: Omitting MAP_STACK here works in current kernels but might break
|
|
|
|
// in the future.
|
|
|
|
guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_,
|
|
|
|
"ScopedStackWithGuard");
|
2015-04-10 23:02:19 +08:00
|
|
|
CHECK(MprotectNoAccess((uptr)guard_start_, guard_size_));
|
2013-04-01 22:38:56 +08:00
|
|
|
}
|
|
|
|
~ScopedStackSpaceWithGuard() {
|
|
|
|
UnmapOrDie((void *)guard_start_, stack_size_ + guard_size_);
|
|
|
|
}
|
|
|
|
void *Bottom() const {
|
|
|
|
return (void *)(guard_start_ + stack_size_ + guard_size_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uptr stack_size_;
|
|
|
|
uptr guard_size_;
|
|
|
|
uptr guard_start_;
|
|
|
|
};
|
|
|
|
|
2013-08-26 21:20:31 +08:00
|
|
|
// We have a limitation on the stack frame size, so some stuff had to be moved
|
|
|
|
// into globals.
|
2014-01-31 19:29:51 +08:00
|
|
|
static __sanitizer_sigset_t blocked_sigset;
|
|
|
|
static __sanitizer_sigset_t old_sigset;
|
2013-03-18 16:09:42 +08:00
|
|
|
|
2013-08-26 21:20:31 +08:00
|
|
|
class StopTheWorldScope {
|
|
|
|
public:
|
|
|
|
StopTheWorldScope() {
|
|
|
|
// Make this process dumpable. Processes that are not dumpable cannot be
|
|
|
|
// attached to.
|
|
|
|
process_was_dumpable_ = internal_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
|
|
|
|
if (!process_was_dumpable_)
|
|
|
|
internal_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
~StopTheWorldScope() {
|
|
|
|
// Restore the dumpable flag.
|
|
|
|
if (!process_was_dumpable_)
|
|
|
|
internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
|
2013-03-15 22:37:21 +08:00
|
|
|
}
|
2013-08-28 19:26:09 +08:00
|
|
|
|
2013-08-26 21:20:31 +08:00
|
|
|
private:
|
|
|
|
int process_was_dumpable_;
|
|
|
|
};
|
|
|
|
|
2013-12-04 22:37:01 +08:00
|
|
|
// When sanitizer output is being redirected to file (i.e. by using log_path),
|
|
|
|
// the tracer should write to the parent's log instead of trying to open a new
|
|
|
|
// file. Alert the logging code to the fact that we have a tracer.
|
|
|
|
struct ScopedSetTracerPID {
|
|
|
|
explicit ScopedSetTracerPID(uptr tracer_pid) {
|
|
|
|
stoptheworld_tracer_pid = tracer_pid;
|
|
|
|
stoptheworld_tracer_ppid = internal_getpid();
|
|
|
|
}
|
|
|
|
~ScopedSetTracerPID() {
|
|
|
|
stoptheworld_tracer_pid = 0;
|
|
|
|
stoptheworld_tracer_ppid = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-26 21:20:31 +08:00
|
|
|
void StopTheWorld(StopTheWorldCallback callback, void *argument) {
|
|
|
|
StopTheWorldScope in_stoptheworld;
|
2013-03-15 22:37:21 +08:00
|
|
|
// Prepare the arguments for TracerThread.
|
|
|
|
struct TracerThreadArgument tracer_thread_argument;
|
|
|
|
tracer_thread_argument.callback = callback;
|
|
|
|
tracer_thread_argument.callback_argument = argument;
|
2013-10-09 21:36:20 +08:00
|
|
|
tracer_thread_argument.parent_pid = internal_getpid();
|
2015-03-05 22:37:28 +08:00
|
|
|
atomic_store(&tracer_thread_argument.done, 0, memory_order_relaxed);
|
2013-04-01 22:38:56 +08:00
|
|
|
const uptr kTracerStackSize = 2 * 1024 * 1024;
|
|
|
|
ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize);
|
2013-03-18 14:27:13 +08:00
|
|
|
// Block the execution of TracerThread until after we have set ptrace
|
|
|
|
// permissions.
|
|
|
|
tracer_thread_argument.mutex.Lock();
|
2015-03-05 22:37:28 +08:00
|
|
|
// Signal handling story.
|
|
|
|
// We don't want async signals to be delivered to the tracer thread,
|
|
|
|
// so we block all async signals before creating the thread. An async signal
|
|
|
|
// handler can temporary modify errno, which is shared with this thread.
|
|
|
|
// We ought to use pthread_sigmask here, because sigprocmask has undefined
|
|
|
|
// behavior in multithreaded programs. However, on linux sigprocmask is
|
|
|
|
// equivalent to pthread_sigmask with the exception that pthread_sigmask
|
|
|
|
// does not allow to block some signals used internally in pthread
|
|
|
|
// implementation. We are fine with blocking them here, we are really not
|
|
|
|
// going to pthread_cancel the thread.
|
|
|
|
// The tracer thread should not raise any synchronous signals. But in case it
|
|
|
|
// does, we setup a special handler for sync signals that properly kills the
|
|
|
|
// parent as well. Note: we don't pass CLONE_SIGHAND to clone, so handlers
|
|
|
|
// in the tracer thread won't interfere with user program. Double note: if a
|
|
|
|
// user does something along the lines of 'kill -11 pid', that can kill the
|
|
|
|
// process even if user setup own handler for SEGV.
|
|
|
|
// Thing to watch out for: this code should not change behavior of user code
|
|
|
|
// in any observable way. In particular it should not override user signal
|
|
|
|
// handlers.
|
|
|
|
internal_sigfillset(&blocked_sigset);
|
|
|
|
for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++)
|
|
|
|
internal_sigdelset(&blocked_sigset, kSyncSignals[i]);
|
|
|
|
int rv = internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset);
|
|
|
|
CHECK_EQ(rv, 0);
|
2013-09-02 19:36:19 +08:00
|
|
|
uptr tracer_pid = internal_clone(
|
|
|
|
TracerThread, tracer_stack.Bottom(),
|
|
|
|
CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,
|
2015-10-01 07:52:54 +08:00
|
|
|
&tracer_thread_argument, nullptr /* parent_tidptr */,
|
|
|
|
nullptr /* newtls */, nullptr /* child_tidptr */);
|
2015-03-05 22:37:28 +08:00
|
|
|
internal_sigprocmask(SIG_SETMASK, &old_sigset, 0);
|
2013-09-02 19:36:19 +08:00
|
|
|
int local_errno = 0;
|
|
|
|
if (internal_iserror(tracer_pid, &local_errno)) {
|
2013-12-05 20:04:51 +08:00
|
|
|
VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno);
|
2013-03-18 14:27:13 +08:00
|
|
|
tracer_thread_argument.mutex.Unlock();
|
2013-03-15 22:37:21 +08:00
|
|
|
} else {
|
2013-12-04 22:37:01 +08:00
|
|
|
ScopedSetTracerPID scoped_set_tracer_pid(tracer_pid);
|
2013-03-15 22:37:21 +08:00
|
|
|
// On some systems we have to explicitly declare that we want to be traced
|
|
|
|
// by the tracer thread.
|
|
|
|
internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0);
|
|
|
|
// Allow the tracer thread to start.
|
2013-03-18 14:27:13 +08:00
|
|
|
tracer_thread_argument.mutex.Unlock();
|
2015-03-05 22:37:28 +08:00
|
|
|
// NOTE: errno is shared between this thread and the tracer thread.
|
2015-03-06 16:43:44 +08:00
|
|
|
// internal_waitpid() may call syscall() which can access/spoil errno,
|
2015-03-05 22:37:28 +08:00
|
|
|
// so we can't call it now. Instead we for the tracer thread to finish using
|
2015-03-06 16:43:44 +08:00
|
|
|
// the spin loop below. Man page for sched_yield() says "In the Linux
|
2015-03-05 22:37:28 +08:00
|
|
|
// implementation, sched_yield() always succeeds", so let's hope it does not
|
|
|
|
// spoil errno. Note that this spin loop runs only for brief periods before
|
|
|
|
// the tracer thread has suspended us and when it starts unblocking threads.
|
|
|
|
while (atomic_load(&tracer_thread_argument.done, memory_order_relaxed) == 0)
|
|
|
|
sched_yield();
|
|
|
|
// Now the tracer thread is about to exit and does not touch errno,
|
|
|
|
// wait for it.
|
|
|
|
for (;;) {
|
2015-10-01 07:52:54 +08:00
|
|
|
uptr waitpid_status = internal_waitpid(tracer_pid, nullptr, __WALL);
|
2015-03-05 22:37:28 +08:00
|
|
|
if (!internal_iserror(waitpid_status, &local_errno))
|
|
|
|
break;
|
|
|
|
if (local_errno == EINTR)
|
|
|
|
continue;
|
2013-12-05 20:04:51 +08:00
|
|
|
VReport(1, "Waiting on the tracer thread failed (errno %d).\n",
|
|
|
|
local_errno);
|
2015-03-05 22:37:28 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-03-15 22:37:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 21:36:42 +08:00
|
|
|
// Platform-specific methods from SuspendedThreadsList.
|
2013-05-13 18:35:20 +08:00
|
|
|
#if SANITIZER_ANDROID && defined(__arm__)
|
2013-04-03 15:06:10 +08:00
|
|
|
typedef pt_regs regs_struct;
|
2013-05-13 18:35:20 +08:00
|
|
|
#define REG_SP ARM_sp
|
|
|
|
|
|
|
|
#elif SANITIZER_LINUX && defined(__arm__)
|
|
|
|
typedef user_regs regs_struct;
|
|
|
|
#define REG_SP uregs[13]
|
|
|
|
|
|
|
|
#elif defined(__i386__) || defined(__x86_64__)
|
2013-04-03 15:06:10 +08:00
|
|
|
typedef user_regs_struct regs_struct;
|
2013-05-13 18:35:20 +08:00
|
|
|
#if defined(__i386__)
|
|
|
|
#define REG_SP esp
|
|
|
|
#else
|
|
|
|
#define REG_SP rsp
|
2013-04-03 15:06:10 +08:00
|
|
|
#endif
|
2020-09-18 03:15:00 +08:00
|
|
|
#define ARCH_IOVEC_FOR_GETREGSET
|
2020-10-20 13:44:34 +08:00
|
|
|
// Support ptrace extensions even when compiled without required kernel support
|
|
|
|
#ifndef NT_X86_XSTATE
|
|
|
|
#define NT_X86_XSTATE 0x202
|
|
|
|
#endif
|
2020-09-18 03:15:00 +08:00
|
|
|
// Compiler may use FP registers to store pointers.
|
|
|
|
static constexpr uptr kExtraRegs[] = {NT_X86_XSTATE, NT_FPREGSET};
|
2013-04-03 15:06:10 +08:00
|
|
|
|
2013-05-15 20:36:29 +08:00
|
|
|
#elif defined(__powerpc__) || defined(__powerpc64__)
|
|
|
|
typedef pt_regs regs_struct;
|
|
|
|
#define REG_SP gpr[PT_R1]
|
|
|
|
|
2013-06-03 22:49:25 +08:00
|
|
|
#elif defined(__mips__)
|
|
|
|
typedef struct user regs_struct;
|
2016-03-16 16:23:10 +08:00
|
|
|
# if SANITIZER_ANDROID
|
|
|
|
# define REG_SP regs[EF_R29]
|
|
|
|
# else
|
|
|
|
# define REG_SP regs[EF_REG29]
|
|
|
|
# endif
|
2013-06-03 22:49:25 +08:00
|
|
|
|
2015-08-05 23:17:59 +08:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
typedef struct user_pt_regs regs_struct;
|
|
|
|
#define REG_SP sp
|
2020-09-18 08:42:33 +08:00
|
|
|
static constexpr uptr kExtraRegs[] = {0};
|
2015-08-05 23:17:59 +08:00
|
|
|
#define ARCH_IOVEC_FOR_GETREGSET
|
|
|
|
|
2020-10-04 17:38:06 +08:00
|
|
|
#elif SANITIZER_RISCV64
|
|
|
|
typedef struct user_regs_struct regs_struct;
|
2020-11-25 08:03:34 +08:00
|
|
|
// sys/ucontext.h already defines REG_SP as 2. Undefine it first.
|
|
|
|
#undef REG_SP
|
2020-10-04 17:38:06 +08:00
|
|
|
#define REG_SP sp
|
|
|
|
static constexpr uptr kExtraRegs[] = {0};
|
|
|
|
#define ARCH_IOVEC_FOR_GETREGSET
|
|
|
|
|
2016-04-26 18:41:30 +08:00
|
|
|
#elif defined(__s390__)
|
|
|
|
typedef _user_regs_struct regs_struct;
|
|
|
|
#define REG_SP gprs[15]
|
2020-09-18 08:42:33 +08:00
|
|
|
static constexpr uptr kExtraRegs[] = {0};
|
2016-04-26 18:41:30 +08:00
|
|
|
#define ARCH_IOVEC_FOR_GETREGSET
|
|
|
|
|
2013-05-13 18:35:20 +08:00
|
|
|
#else
|
|
|
|
#error "Unsupported architecture"
|
|
|
|
#endif // SANITIZER_ANDROID && defined(__arm__)
|
|
|
|
|
2017-04-18 04:29:38 +08:00
|
|
|
tid_t SuspendedThreadsListLinux::GetThreadID(uptr index) const {
|
|
|
|
CHECK_LT(index, thread_ids_.size());
|
|
|
|
return thread_ids_[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr SuspendedThreadsListLinux::ThreadCount() const {
|
|
|
|
return thread_ids_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SuspendedThreadsListLinux::ContainsTid(tid_t thread_id) const {
|
|
|
|
for (uptr i = 0; i < thread_ids_.size(); i++) {
|
|
|
|
if (thread_ids_[i] == thread_id) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SuspendedThreadsListLinux::Append(tid_t tid) {
|
|
|
|
thread_ids_.push_back(tid);
|
|
|
|
}
|
|
|
|
|
|
|
|
PtraceRegistersStatus SuspendedThreadsListLinux::GetRegistersAndSP(
|
2020-09-16 16:14:55 +08:00
|
|
|
uptr index, InternalMmapVector<uptr> *buffer, uptr *sp) const {
|
2013-04-01 21:36:42 +08:00
|
|
|
pid_t tid = GetThreadID(index);
|
2020-09-18 03:15:00 +08:00
|
|
|
constexpr uptr uptr_sz = sizeof(uptr);
|
2013-05-08 22:43:49 +08:00
|
|
|
int pterrno;
|
2015-08-05 23:17:59 +08:00
|
|
|
#ifdef ARCH_IOVEC_FOR_GETREGSET
|
2020-09-18 03:15:00 +08:00
|
|
|
auto append = [&](uptr regset) {
|
|
|
|
uptr size = buffer->size();
|
|
|
|
// NT_X86_XSTATE requires 64bit alignment.
|
|
|
|
uptr size_up = RoundUpTo(size, 8 / uptr_sz);
|
|
|
|
buffer->reserve(Max<uptr>(1024, size_up));
|
|
|
|
struct iovec regset_io;
|
|
|
|
for (;; buffer->resize(buffer->capacity() * 2)) {
|
|
|
|
buffer->resize(buffer->capacity());
|
|
|
|
uptr available_bytes = (buffer->size() - size_up) * uptr_sz;
|
|
|
|
regset_io.iov_base = buffer->data() + size_up;
|
|
|
|
regset_io.iov_len = available_bytes;
|
|
|
|
bool fail =
|
|
|
|
internal_iserror(internal_ptrace(PTRACE_GETREGSET, tid,
|
|
|
|
(void *)regset, (void *)®set_io),
|
|
|
|
&pterrno);
|
|
|
|
if (fail) {
|
|
|
|
VReport(1, "Could not get regset %p from thread %d (errno %d).\n",
|
2020-09-18 16:17:54 +08:00
|
|
|
(void *)regset, tid, pterrno);
|
2020-09-18 03:15:00 +08:00
|
|
|
buffer->resize(size);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Far enough from the buffer size, no need to resize and repeat.
|
|
|
|
if (regset_io.iov_len + 64 < available_bytes)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer->resize(size_up + RoundUpTo(regset_io.iov_len, uptr_sz) / uptr_sz);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
buffer->clear();
|
|
|
|
bool fail = !append(NT_PRSTATUS);
|
|
|
|
if (!fail) {
|
|
|
|
// Accept the first available and do not report errors.
|
|
|
|
for (uptr regs : kExtraRegs)
|
2020-09-18 08:42:33 +08:00
|
|
|
if (regs && append(regs))
|
2020-09-18 03:15:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-08-05 23:17:59 +08:00
|
|
|
#else
|
2020-09-18 03:15:00 +08:00
|
|
|
buffer->resize(RoundUpTo(sizeof(regs_struct), uptr_sz) / uptr_sz);
|
|
|
|
bool fail = internal_iserror(
|
|
|
|
internal_ptrace(PTRACE_GETREGS, tid, nullptr, buffer->data()), &pterrno);
|
|
|
|
if (fail)
|
2013-12-05 20:04:51 +08:00
|
|
|
VReport(1, "Could not get registers from thread %d (errno %d).\n", tid,
|
|
|
|
pterrno);
|
2020-09-18 03:15:00 +08:00
|
|
|
#endif
|
|
|
|
if (fail) {
|
2017-04-06 15:42:27 +08:00
|
|
|
// ESRCH means that the given thread is not suspended or already dead.
|
|
|
|
// Therefore it's unsafe to inspect its data (e.g. walk through stack) and
|
|
|
|
// we should notify caller about this.
|
|
|
|
return pterrno == ESRCH ? REGISTERS_UNAVAILABLE_FATAL
|
|
|
|
: REGISTERS_UNAVAILABLE;
|
2013-04-01 21:36:42 +08:00
|
|
|
}
|
2013-05-13 18:35:20 +08:00
|
|
|
|
2020-09-18 03:15:00 +08:00
|
|
|
*sp = reinterpret_cast<regs_struct *>(buffer->data())[0].REG_SP;
|
2017-04-06 15:42:27 +08:00
|
|
|
return REGISTERS_AVAILABLE;
|
2013-04-01 21:36:42 +08:00
|
|
|
}
|
|
|
|
|
2015-10-01 07:52:54 +08:00
|
|
|
} // namespace __sanitizer
|
2013-03-15 22:37:21 +08:00
|
|
|
|
2015-12-09 05:54:39 +08:00
|
|
|
#endif // SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__)
|
|
|
|
// || defined(__aarch64__) || defined(__powerpc64__)
|
2017-04-11 22:58:26 +08:00
|
|
|
// || defined(__s390__) || defined(__i386__) || defined(__arm__)
|