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