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