sanitizer_stoptheworld_linux_libcdep.cc [plain text]
#include "sanitizer_platform.h"
#if SANITIZER_LINUX && defined(__x86_64__)
#include "sanitizer_stoptheworld.h"
#include "sanitizer_platform_limits_posix.h"
#include <errno.h>
#include <sched.h> // for CLONE_* definitions
#include <stddef.h>
#include <sys/prctl.h> // for PR_* definitions
#include <sys/ptrace.h> // for PTRACE_* definitions
#include <sys/types.h> // for pid_t
#if SANITIZER_ANDROID && defined(__arm__)
# include <linux/user.h> // for pt_regs
#else
# include <sys/user.h> // for user_regs_struct
#endif
#include <sys/wait.h> // for signal-related stuff
#ifdef sa_handler
# undef sa_handler
#endif
#ifdef sa_sigaction
# undef sa_sigaction
#endif
#include "sanitizer_common.h"
#include "sanitizer_flags.h"
#include "sanitizer_libc.h"
#include "sanitizer_linux.h"
#include "sanitizer_mutex.h"
#include "sanitizer_placement_new.h"
COMPILER_CHECK(sizeof(SuspendedThreadID) == sizeof(pid_t));
namespace __sanitizer {
class ThreadSuspender {
public:
explicit ThreadSuspender(pid_t pid)
: pid_(pid) {
CHECK_GE(pid, 0);
}
bool SuspendAllThreads();
void ResumeAllThreads();
void KillAllThreads();
SuspendedThreadsList &suspended_threads_list() {
return suspended_threads_list_;
}
private:
SuspendedThreadsList suspended_threads_list_;
pid_t pid_;
bool SuspendThread(SuspendedThreadID thread_id);
};
bool ThreadSuspender::SuspendThread(SuspendedThreadID thread_id) {
if (suspended_threads_list_.Contains(thread_id))
return false;
int pterrno;
if (internal_iserror(internal_ptrace(PTRACE_ATTACH, thread_id, NULL, NULL),
&pterrno)) {
VReport(1, "Could not attach to thread %d (errno %d).\n", thread_id,
pterrno);
return false;
} else {
VReport(1, "Attached to thread %d.\n", thread_id);
uptr waitpid_status;
HANDLE_EINTR(waitpid_status, internal_waitpid(thread_id, NULL, __WALL));
int wperrno;
if (internal_iserror(waitpid_status, &wperrno)) {
VReport(1, "Waiting on thread %d failed, detaching (errno %d).\n",
thread_id, wperrno);
internal_ptrace(PTRACE_DETACH, thread_id, NULL, NULL);
return false;
}
suspended_threads_list_.Append(thread_id);
return true;
}
}
void ThreadSuspender::ResumeAllThreads() {
for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++) {
pid_t tid = suspended_threads_list_.GetThreadID(i);
int pterrno;
if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, NULL, NULL),
&pterrno)) {
VReport(1, "Detached from thread %d.\n", tid);
} else {
VReport(1, "Could not detach from thread %d (errno %d).\n", tid, pterrno);
}
}
}
void ThreadSuspender::KillAllThreads() {
for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++)
internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i),
NULL, NULL);
}
bool ThreadSuspender::SuspendAllThreads() {
ThreadLister thread_lister(pid_);
bool added_threads;
do {
added_threads = false;
pid_t tid = thread_lister.GetNextTID();
while (tid >= 0) {
if (SuspendThread(tid))
added_threads = true;
tid = thread_lister.GetNextTID();
}
if (thread_lister.error()) {
ResumeAllThreads();
return false;
}
thread_lister.Reset();
} while (added_threads);
return true;
}
static ThreadSuspender *thread_suspender_instance = NULL;
static const int kUnblockedSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV,
SIGBUS, SIGXCPU, SIGXFSZ };
struct TracerThreadArgument {
StopTheWorldCallback callback;
void *callback_argument;
BlockingMutex mutex;
uptr parent_pid;
};
static DieCallbackType old_die_callback;
void TracerThreadSignalHandler(int signum, void *siginfo, void *) {
if (thread_suspender_instance != NULL) {
if (signum == SIGABRT)
thread_suspender_instance->KillAllThreads();
else
thread_suspender_instance->ResumeAllThreads();
}
internal__exit((signum == SIGABRT) ? 1 : 2);
}
static void TracerThreadDieCallback() {
if (thread_suspender_instance)
thread_suspender_instance->KillAllThreads();
if (old_die_callback)
old_die_callback();
}
static const int kHandlerStackSize = 4096;
static int TracerThread(void* argument) {
TracerThreadArgument *tracer_thread_argument =
(TracerThreadArgument *)argument;
internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
if (internal_getppid() != tracer_thread_argument->parent_pid)
internal__exit(4);
tracer_thread_argument->mutex.Lock();
tracer_thread_argument->mutex.Unlock();
SetDieCallback(TracerThreadDieCallback);
ThreadSuspender thread_suspender(internal_getppid());
thread_suspender_instance = &thread_suspender;
InternalScopedBuffer<char> handler_stack_memory(kHandlerStackSize);
struct sigaltstack handler_stack;
internal_memset(&handler_stack, 0, sizeof(handler_stack));
handler_stack.ss_sp = handler_stack_memory.data();
handler_stack.ss_size = kHandlerStackSize;
internal_sigaltstack(&handler_stack, NULL);
for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals);
signal_index++) {
__sanitizer_kernel_sigaction_t new_sigaction;
internal_memset(&new_sigaction, 0, sizeof(new_sigaction));
new_sigaction.sigaction = TracerThreadSignalHandler;
new_sigaction.sa_flags = SA_ONSTACK | SA_SIGINFO;
internal_sigfillset(&new_sigaction.sa_mask);
internal_sigaction(kUnblockedSignals[signal_index], &new_sigaction, NULL);
}
int exit_code = 0;
if (!thread_suspender.SuspendAllThreads()) {
VReport(1, "Failed suspending threads.\n");
exit_code = 3;
} else {
tracer_thread_argument->callback(thread_suspender.suspended_threads_list(),
tracer_thread_argument->callback_argument);
thread_suspender.ResumeAllThreads();
exit_code = 0;
}
thread_suspender_instance = NULL;
handler_stack.ss_flags = SS_DISABLE;
internal_sigaltstack(&handler_stack, NULL);
return exit_code;
}
class ScopedStackSpaceWithGuard {
public:
explicit ScopedStackSpaceWithGuard(uptr stack_size) {
stack_size_ = stack_size;
guard_size_ = GetPageSizeCached();
guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_,
"ScopedStackWithGuard");
CHECK_EQ(guard_start_, (uptr)Mprotect((uptr)guard_start_, guard_size_));
}
~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_;
};
static __sanitizer_kernel_sigset_t blocked_sigset;
static __sanitizer_kernel_sigset_t old_sigset;
static __sanitizer_kernel_sigaction_t old_sigactions
[ARRAY_SIZE(kUnblockedSignals)];
class StopTheWorldScope {
public:
StopTheWorldScope() {
internal_sigfillset(&blocked_sigset);
for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals);
signal_index++) {
internal_sigdelset(&blocked_sigset, kUnblockedSignals[signal_index]);
__sanitizer_kernel_sigaction_t new_sigaction;
internal_memset(&new_sigaction, 0, sizeof(new_sigaction));
new_sigaction.handler = SIG_DFL;
internal_sigfillset(&new_sigaction.sa_mask);
internal_sigaction(kUnblockedSignals[signal_index], &new_sigaction,
&old_sigactions[signal_index]);
}
int sigprocmask_status =
internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset);
CHECK_EQ(sigprocmask_status, 0); 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);
old_die_callback = GetDieCallback();
}
~StopTheWorldScope() {
SetDieCallback(old_die_callback);
if (!process_was_dumpable_)
internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals);
signal_index++) {
internal_sigaction(kUnblockedSignals[signal_index],
&old_sigactions[signal_index], NULL);
}
internal_sigprocmask(SIG_SETMASK, &old_sigset, &old_sigset);
}
private:
int process_was_dumpable_;
};
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;
}
};
void StopTheWorld(StopTheWorldCallback callback, void *argument) {
StopTheWorldScope in_stoptheworld;
struct TracerThreadArgument tracer_thread_argument;
tracer_thread_argument.callback = callback;
tracer_thread_argument.callback_argument = argument;
tracer_thread_argument.parent_pid = internal_getpid();
const uptr kTracerStackSize = 2 * 1024 * 1024;
ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize);
tracer_thread_argument.mutex.Lock();
uptr tracer_pid = internal_clone(
TracerThread, tracer_stack.Bottom(),
CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,
&tracer_thread_argument, 0 , 0 , 0
);
int local_errno = 0;
if (internal_iserror(tracer_pid, &local_errno)) {
VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno);
tracer_thread_argument.mutex.Unlock();
} else {
ScopedSetTracerPID scoped_set_tracer_pid(tracer_pid);
#ifdef PR_SET_PTRACER
internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0);
#endif
tracer_thread_argument.mutex.Unlock();
uptr waitpid_status = internal_waitpid(tracer_pid, NULL, __WALL);
if (internal_iserror(waitpid_status, &local_errno))
VReport(1, "Waiting on the tracer thread failed (errno %d).\n",
local_errno);
}
}
#if SANITIZER_ANDROID && defined(__arm__)
typedef pt_regs regs_struct;
#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__)
typedef user_regs_struct regs_struct;
#if defined(__i386__)
#define REG_SP esp
#else
#define REG_SP rsp
#endif
#elif defined(__powerpc__) || defined(__powerpc64__)
typedef pt_regs regs_struct;
#define REG_SP gpr[PT_R1]
#elif defined(__mips__)
typedef struct user regs_struct;
#define REG_SP regs[EF_REG29]
#else
#error "Unsupported architecture"
#endif // SANITIZER_ANDROID && defined(__arm__)
int SuspendedThreadsList::GetRegistersAndSP(uptr index,
uptr *buffer,
uptr *sp) const {
pid_t tid = GetThreadID(index);
regs_struct regs;
int pterrno;
if (internal_iserror(internal_ptrace(PTRACE_GETREGS, tid, NULL, ®s),
&pterrno)) {
VReport(1, "Could not get registers from thread %d (errno %d).\n", tid,
pterrno);
return -1;
}
*sp = regs.REG_SP;
internal_memcpy(buffer, ®s, sizeof(regs));
return 0;
}
uptr SuspendedThreadsList::RegisterCount() {
return sizeof(regs_struct) / sizeof(uptr);
}
}
#endif // SANITIZER_LINUX && defined(__x86_64__)