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 | |
| 16 | #include "sanitizer_platform.h" |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 17 | #if SANITIZER_LINUX && defined(__x86_64__) |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 18 | |
| 19 | #include "sanitizer_stoptheworld.h" |
| 20 | |
| 21 | #include <errno.h> |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 22 | #include <sched.h> // for CLONE_* definitions |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 23 | #include <stddef.h> |
| 24 | #include <sys/prctl.h> // for PR_* definitions |
| 25 | #include <sys/ptrace.h> // for PTRACE_* definitions |
| 26 | #include <sys/types.h> // for pid_t |
Sergey Matveev | 115accb | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 27 | #if SANITIZER_ANDROID && defined(__arm__) |
Alexey Samsonov | bb090b5 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 28 | # include <linux/user.h> // for pt_regs |
| 29 | #else |
| 30 | # include <sys/user.h> // for user_regs_struct |
| 31 | #endif |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 32 | #include <sys/wait.h> // for signal-related stuff |
| 33 | |
| 34 | #include "sanitizer_common.h" |
| 35 | #include "sanitizer_libc.h" |
| 36 | #include "sanitizer_linux.h" |
| 37 | #include "sanitizer_mutex.h" |
Dmitry Vyukov | 49960be | 2013-03-18 08:09:42 +0000 | [diff] [blame] | 38 | #include "sanitizer_placement_new.h" |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 39 | |
| 40 | // This module works by spawning a Linux task which then attaches to every |
| 41 | // thread in the caller process with ptrace. This suspends the threads, and |
| 42 | // PTRACE_GETREGS can then be used to obtain their register state. The callback |
| 43 | // supplied to StopTheWorld() is run in the tracer task while the threads are |
| 44 | // suspended. |
| 45 | // The tracer task must be placed in a different thread group for ptrace to |
| 46 | // work, so it cannot be spawned as a pthread. Instead, we use the low-level |
| 47 | // clone() interface (we want to share the address space with the caller |
| 48 | // process, so we prefer clone() over fork()). |
| 49 | // |
Sergey Matveev | 2191fca | 2013-10-15 11:54:38 +0000 | [diff] [blame^] | 50 | // We don't use any libc functions, relying instead on direct syscalls. There |
| 51 | // are two reasons for this: |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 52 | // 1. calling a library function while threads are suspended could cause a |
| 53 | // deadlock, if one of the treads happens to be holding a libc lock; |
| 54 | // 2. it's generally not safe to call libc functions from the tracer task, |
| 55 | // because clone() does not set up a thread-local storage for it. Any |
| 56 | // thread-local variables used by libc will be shared between the tracer task |
| 57 | // and the thread which spawned it. |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 58 | |
| 59 | COMPILER_CHECK(sizeof(SuspendedThreadID) == sizeof(pid_t)); |
| 60 | |
| 61 | namespace __sanitizer { |
| 62 | // This class handles thread suspending/unsuspending in the tracer thread. |
| 63 | class ThreadSuspender { |
| 64 | public: |
| 65 | explicit ThreadSuspender(pid_t pid) |
| 66 | : pid_(pid) { |
| 67 | CHECK_GE(pid, 0); |
| 68 | } |
| 69 | bool SuspendAllThreads(); |
| 70 | void ResumeAllThreads(); |
| 71 | void KillAllThreads(); |
| 72 | SuspendedThreadsList &suspended_threads_list() { |
| 73 | return suspended_threads_list_; |
| 74 | } |
| 75 | private: |
| 76 | SuspendedThreadsList suspended_threads_list_; |
| 77 | pid_t pid_; |
| 78 | bool SuspendThread(SuspendedThreadID thread_id); |
| 79 | }; |
| 80 | |
| 81 | bool ThreadSuspender::SuspendThread(SuspendedThreadID thread_id) { |
| 82 | // Are we already attached to this thread? |
| 83 | // Currently this check takes linear time, however the number of threads is |
| 84 | // usually small. |
| 85 | if (suspended_threads_list_.Contains(thread_id)) |
| 86 | return false; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 87 | int pterrno; |
| 88 | if (internal_iserror(internal_ptrace(PTRACE_ATTACH, thread_id, NULL, NULL), |
| 89 | &pterrno)) { |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 90 | // Either the thread is dead, or something prevented us from attaching. |
| 91 | // Log this event and move on. |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 92 | Report("Could not attach to thread %d (errno %d).\n", thread_id, pterrno); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 93 | return false; |
| 94 | } else { |
| 95 | if (SanitizerVerbosity > 0) |
| 96 | Report("Attached to thread %d.\n", thread_id); |
| 97 | // The thread is not guaranteed to stop before ptrace returns, so we must |
| 98 | // wait on it. |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 99 | uptr waitpid_status; |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 100 | HANDLE_EINTR(waitpid_status, internal_waitpid(thread_id, NULL, __WALL)); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 101 | int wperrno; |
| 102 | if (internal_iserror(waitpid_status, &wperrno)) { |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 103 | // Got a ECHILD error. I don't think this situation is possible, but it |
| 104 | // doesn't hurt to report it. |
| 105 | Report("Waiting on thread %d failed, detaching (errno %d).\n", thread_id, |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 106 | wperrno); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 107 | internal_ptrace(PTRACE_DETACH, thread_id, NULL, NULL); |
| 108 | return false; |
| 109 | } |
| 110 | suspended_threads_list_.Append(thread_id); |
| 111 | return true; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void ThreadSuspender::ResumeAllThreads() { |
| 116 | for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++) { |
| 117 | pid_t tid = suspended_threads_list_.GetThreadID(i); |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 118 | int pterrno; |
| 119 | if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, NULL, NULL), |
| 120 | &pterrno)) { |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 121 | if (SanitizerVerbosity > 0) |
| 122 | Report("Detached from thread %d.\n", tid); |
| 123 | } else { |
| 124 | // Either the thread is dead, or we are already detached. |
| 125 | // The latter case is possible, for instance, if this function was called |
| 126 | // from a signal handler. |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 127 | Report("Could not detach from thread %d (errno %d).\n", tid, pterrno); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void ThreadSuspender::KillAllThreads() { |
| 133 | for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++) |
| 134 | internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i), |
| 135 | NULL, NULL); |
| 136 | } |
| 137 | |
| 138 | bool ThreadSuspender::SuspendAllThreads() { |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 139 | ThreadLister thread_lister(pid_); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 140 | bool added_threads; |
| 141 | do { |
| 142 | // Run through the directory entries once. |
| 143 | added_threads = false; |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 144 | pid_t tid = thread_lister.GetNextTID(); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 145 | while (tid >= 0) { |
| 146 | if (SuspendThread(tid)) |
| 147 | added_threads = true; |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 148 | tid = thread_lister.GetNextTID(); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 149 | } |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 150 | if (thread_lister.error()) { |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 151 | // Detach threads and fail. |
| 152 | ResumeAllThreads(); |
| 153 | return false; |
| 154 | } |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 155 | thread_lister.Reset(); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 156 | } while (added_threads); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | // Pointer to the ThreadSuspender instance for use in signal handler. |
| 161 | static ThreadSuspender *thread_suspender_instance = NULL; |
| 162 | |
| 163 | // Signals that should not be blocked (this is used in the parent thread as well |
| 164 | // as the tracer thread). |
| 165 | static const int kUnblockedSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV, |
| 166 | SIGBUS, SIGXCPU, SIGXFSZ }; |
| 167 | |
| 168 | // Structure for passing arguments into the tracer thread. |
| 169 | struct TracerThreadArgument { |
| 170 | StopTheWorldCallback callback; |
| 171 | void *callback_argument; |
Sergey Matveev | d41fa1b | 2013-10-09 13:36:20 +0000 | [diff] [blame] | 172 | // The tracer thread waits on this mutex while the parent finishes its |
Alexey Samsonov | 6d03606 | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 173 | // preparations. |
| 174 | BlockingMutex mutex; |
Sergey Matveev | d41fa1b | 2013-10-09 13:36:20 +0000 | [diff] [blame] | 175 | uptr parent_pid; |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 178 | static DieCallbackType old_die_callback; |
| 179 | |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 180 | // Signal handler to wake up suspended threads when the tracer thread dies. |
| 181 | void TracerThreadSignalHandler(int signum, siginfo_t *siginfo, void *) { |
| 182 | if (thread_suspender_instance != NULL) { |
| 183 | if (signum == SIGABRT) |
| 184 | thread_suspender_instance->KillAllThreads(); |
| 185 | else |
| 186 | thread_suspender_instance->ResumeAllThreads(); |
| 187 | } |
| 188 | internal__exit((signum == SIGABRT) ? 1 : 2); |
| 189 | } |
| 190 | |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 191 | static void TracerThreadDieCallback() { |
| 192 | // Generally a call to Die() in the tracer thread should be fatal to the |
| 193 | // parent process as well, because they share the address space. |
| 194 | // This really only works correctly if all the threads are suspended at this |
| 195 | // point. So we correctly handle calls to Die() from within the callback, but |
| 196 | // not those that happen before or after the callback. Hopefully there aren't |
| 197 | // a lot of opportunities for that to happen... |
| 198 | if (thread_suspender_instance) |
| 199 | thread_suspender_instance->KillAllThreads(); |
| 200 | if (old_die_callback) |
| 201 | old_die_callback(); |
| 202 | } |
| 203 | |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 204 | // Size of alternative stack for signal handlers in the tracer thread. |
| 205 | static const int kHandlerStackSize = 4096; |
| 206 | |
| 207 | // This function will be run as a cloned task. |
Alexey Samsonov | 6d03606 | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 208 | static int TracerThread(void* argument) { |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 209 | TracerThreadArgument *tracer_thread_argument = |
| 210 | (TracerThreadArgument *)argument; |
| 211 | |
Sergey Matveev | 23a7a43 | 2013-10-08 18:01:03 +0000 | [diff] [blame] | 212 | internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); |
| 213 | // Check if parent is already dead. |
Sergey Matveev | d41fa1b | 2013-10-09 13:36:20 +0000 | [diff] [blame] | 214 | if (internal_getppid() != tracer_thread_argument->parent_pid) |
Sergey Matveev | 23a7a43 | 2013-10-08 18:01:03 +0000 | [diff] [blame] | 215 | internal__exit(4); |
| 216 | |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 217 | // Wait for the parent thread to finish preparations. |
Alexey Samsonov | 6d03606 | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 218 | tracer_thread_argument->mutex.Lock(); |
| 219 | tracer_thread_argument->mutex.Unlock(); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 220 | |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 221 | SetDieCallback(TracerThreadDieCallback); |
| 222 | |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 223 | ThreadSuspender thread_suspender(internal_getppid()); |
| 224 | // Global pointer for the signal handler. |
| 225 | thread_suspender_instance = &thread_suspender; |
| 226 | |
| 227 | // Alternate stack for signal handling. |
| 228 | InternalScopedBuffer<char> handler_stack_memory(kHandlerStackSize); |
| 229 | struct sigaltstack handler_stack; |
| 230 | internal_memset(&handler_stack, 0, sizeof(handler_stack)); |
| 231 | handler_stack.ss_sp = handler_stack_memory.data(); |
| 232 | handler_stack.ss_size = kHandlerStackSize; |
| 233 | internal_sigaltstack(&handler_stack, NULL); |
| 234 | |
| 235 | // Install our handler for fatal signals. Other signals should be blocked by |
| 236 | // the mask we inherited from the caller thread. |
| 237 | for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals); |
| 238 | signal_index++) { |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 239 | kernel_sigaction_t new_sigaction; |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 240 | internal_memset(&new_sigaction, 0, sizeof(new_sigaction)); |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 241 | new_sigaction.sigaction = TracerThreadSignalHandler; |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 242 | new_sigaction.sa_flags = SA_ONSTACK | SA_SIGINFO; |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 243 | internal_sigfillset(&new_sigaction.sa_mask); |
| 244 | internal_sigaction(kUnblockedSignals[signal_index], &new_sigaction, NULL); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | int exit_code = 0; |
| 248 | if (!thread_suspender.SuspendAllThreads()) { |
| 249 | Report("Failed suspending threads.\n"); |
| 250 | exit_code = 3; |
| 251 | } else { |
| 252 | tracer_thread_argument->callback(thread_suspender.suspended_threads_list(), |
| 253 | tracer_thread_argument->callback_argument); |
| 254 | thread_suspender.ResumeAllThreads(); |
| 255 | exit_code = 0; |
| 256 | } |
| 257 | thread_suspender_instance = NULL; |
| 258 | handler_stack.ss_flags = SS_DISABLE; |
| 259 | internal_sigaltstack(&handler_stack, NULL); |
| 260 | return exit_code; |
| 261 | } |
| 262 | |
Alexander Potapenko | fd8726c | 2013-04-01 14:38:56 +0000 | [diff] [blame] | 263 | class ScopedStackSpaceWithGuard { |
| 264 | public: |
| 265 | explicit ScopedStackSpaceWithGuard(uptr stack_size) { |
| 266 | stack_size_ = stack_size; |
| 267 | guard_size_ = GetPageSizeCached(); |
| 268 | // FIXME: Omitting MAP_STACK here works in current kernels but might break |
| 269 | // in the future. |
| 270 | guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_, |
| 271 | "ScopedStackWithGuard"); |
| 272 | CHECK_EQ(guard_start_, (uptr)Mprotect((uptr)guard_start_, guard_size_)); |
| 273 | } |
| 274 | ~ScopedStackSpaceWithGuard() { |
| 275 | UnmapOrDie((void *)guard_start_, stack_size_ + guard_size_); |
| 276 | } |
| 277 | void *Bottom() const { |
| 278 | return (void *)(guard_start_ + stack_size_ + guard_size_); |
| 279 | } |
| 280 | |
| 281 | private: |
| 282 | uptr stack_size_; |
| 283 | uptr guard_size_; |
| 284 | uptr guard_start_; |
| 285 | }; |
| 286 | |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 287 | // We have a limitation on the stack frame size, so some stuff had to be moved |
| 288 | // into globals. |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 289 | static kernel_sigset_t blocked_sigset; |
| 290 | static kernel_sigset_t old_sigset; |
| 291 | static kernel_sigaction_t old_sigactions[ARRAY_SIZE(kUnblockedSignals)]; |
Dmitry Vyukov | 49960be | 2013-03-18 08:09:42 +0000 | [diff] [blame] | 292 | |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 293 | class StopTheWorldScope { |
| 294 | public: |
| 295 | StopTheWorldScope() { |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 296 | // Block all signals that can be blocked safely, and install |
| 297 | // default handlers for the remaining signals. |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 298 | // We cannot allow user-defined handlers to run while the ThreadSuspender |
| 299 | // thread is active, because they could conceivably call some libc functions |
| 300 | // which modify errno (which is shared between the two threads). |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 301 | internal_sigfillset(&blocked_sigset); |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 302 | for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals); |
| 303 | signal_index++) { |
| 304 | // Remove the signal from the set of blocked signals. |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 305 | internal_sigdelset(&blocked_sigset, kUnblockedSignals[signal_index]); |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 306 | // Install the default handler. |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 307 | kernel_sigaction_t new_sigaction; |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 308 | internal_memset(&new_sigaction, 0, sizeof(new_sigaction)); |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 309 | new_sigaction.handler = SIG_DFL; |
| 310 | internal_sigfillset(&new_sigaction.sa_mask); |
| 311 | internal_sigaction(kUnblockedSignals[signal_index], &new_sigaction, |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 312 | &old_sigactions[signal_index]); |
| 313 | } |
| 314 | int sigprocmask_status = |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 315 | internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset); |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 316 | CHECK_EQ(sigprocmask_status, 0); // sigprocmask should never fail |
| 317 | // Make this process dumpable. Processes that are not dumpable cannot be |
| 318 | // attached to. |
| 319 | process_was_dumpable_ = internal_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0); |
| 320 | if (!process_was_dumpable_) |
| 321 | internal_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); |
| 322 | old_die_callback = GetDieCallback(); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 323 | } |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 324 | |
| 325 | ~StopTheWorldScope() { |
| 326 | SetDieCallback(old_die_callback); |
| 327 | // Restore the dumpable flag. |
| 328 | if (!process_was_dumpable_) |
| 329 | internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); |
| 330 | // Restore the signal handlers. |
| 331 | for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals); |
| 332 | signal_index++) { |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 333 | internal_sigaction(kUnblockedSignals[signal_index], |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 334 | &old_sigactions[signal_index], NULL); |
| 335 | } |
Dmitry Vyukov | 0f7a2ac | 2013-10-15 11:31:51 +0000 | [diff] [blame] | 336 | internal_sigprocmask(SIG_SETMASK, &old_sigset, &old_sigset); |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 337 | } |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 338 | |
Sergey Matveev | 90629fb | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 339 | private: |
| 340 | int process_was_dumpable_; |
| 341 | }; |
| 342 | |
| 343 | void StopTheWorld(StopTheWorldCallback callback, void *argument) { |
| 344 | StopTheWorldScope in_stoptheworld; |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 345 | // Prepare the arguments for TracerThread. |
| 346 | struct TracerThreadArgument tracer_thread_argument; |
| 347 | tracer_thread_argument.callback = callback; |
| 348 | tracer_thread_argument.callback_argument = argument; |
Sergey Matveev | d41fa1b | 2013-10-09 13:36:20 +0000 | [diff] [blame] | 349 | tracer_thread_argument.parent_pid = internal_getpid(); |
Alexander Potapenko | fd8726c | 2013-04-01 14:38:56 +0000 | [diff] [blame] | 350 | const uptr kTracerStackSize = 2 * 1024 * 1024; |
| 351 | ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize); |
Alexey Samsonov | 6d03606 | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 352 | // Block the execution of TracerThread until after we have set ptrace |
| 353 | // permissions. |
| 354 | tracer_thread_argument.mutex.Lock(); |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 355 | uptr tracer_pid = internal_clone( |
| 356 | TracerThread, tracer_stack.Bottom(), |
| 357 | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED, |
| 358 | &tracer_thread_argument, 0 /* parent_tidptr */, 0 /* newtls */, 0 |
| 359 | /* child_tidptr */); |
| 360 | int local_errno = 0; |
| 361 | if (internal_iserror(tracer_pid, &local_errno)) { |
| 362 | Report("Failed spawning a tracer thread (errno %d).\n", local_errno); |
Alexey Samsonov | 6d03606 | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 363 | tracer_thread_argument.mutex.Unlock(); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 364 | } else { |
| 365 | // On some systems we have to explicitly declare that we want to be traced |
| 366 | // by the tracer thread. |
| 367 | #ifdef PR_SET_PTRACER |
| 368 | internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0); |
| 369 | #endif |
| 370 | // Allow the tracer thread to start. |
Alexey Samsonov | 6d03606 | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 371 | tracer_thread_argument.mutex.Unlock(); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 372 | // Since errno is shared between this thread and the tracer thread, we |
| 373 | // must avoid using errno while the tracer thread is running. |
| 374 | // At this point, any signal will either be blocked or kill us, so waitpid |
| 375 | // should never return (and set errno) while the tracer thread is alive. |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 376 | uptr waitpid_status = internal_waitpid(tracer_pid, NULL, __WALL); |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 377 | if (internal_iserror(waitpid_status, &local_errno)) |
| 378 | Report("Waiting on the tracer thread failed (errno %d).\n", local_errno); |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 379 | } |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Alexander Potapenko | 53c18d7 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 382 | // Platform-specific methods from SuspendedThreadsList. |
Sergey Matveev | 115accb | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 383 | #if SANITIZER_ANDROID && defined(__arm__) |
Alexey Samsonov | bb090b5 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 384 | typedef pt_regs regs_struct; |
Sergey Matveev | 115accb | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 385 | #define REG_SP ARM_sp |
| 386 | |
| 387 | #elif SANITIZER_LINUX && defined(__arm__) |
| 388 | typedef user_regs regs_struct; |
| 389 | #define REG_SP uregs[13] |
| 390 | |
| 391 | #elif defined(__i386__) || defined(__x86_64__) |
Alexey Samsonov | bb090b5 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 392 | typedef user_regs_struct regs_struct; |
Sergey Matveev | 115accb | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 393 | #if defined(__i386__) |
| 394 | #define REG_SP esp |
| 395 | #else |
| 396 | #define REG_SP rsp |
Alexey Samsonov | bb090b5 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 397 | #endif |
| 398 | |
Kostya Serebryany | f931da8 | 2013-05-15 12:36:29 +0000 | [diff] [blame] | 399 | #elif defined(__powerpc__) || defined(__powerpc64__) |
| 400 | typedef pt_regs regs_struct; |
| 401 | #define REG_SP gpr[PT_R1] |
| 402 | |
Kostya Serebryany | 40527a5 | 2013-06-03 14:49:25 +0000 | [diff] [blame] | 403 | #elif defined(__mips__) |
| 404 | typedef struct user regs_struct; |
| 405 | #define REG_SP regs[EF_REG29] |
| 406 | |
Sergey Matveev | 115accb | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 407 | #else |
| 408 | #error "Unsupported architecture" |
| 409 | #endif // SANITIZER_ANDROID && defined(__arm__) |
| 410 | |
Alexander Potapenko | 53c18d7 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 411 | int SuspendedThreadsList::GetRegistersAndSP(uptr index, |
| 412 | uptr *buffer, |
| 413 | uptr *sp) const { |
| 414 | pid_t tid = GetThreadID(index); |
Alexey Samsonov | bb090b5 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 415 | regs_struct regs; |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 416 | int pterrno; |
| 417 | if (internal_iserror(internal_ptrace(PTRACE_GETREGS, tid, NULL, ®s), |
| 418 | &pterrno)) { |
Alexander Potapenko | 53c18d7 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 419 | Report("Could not get registers from thread %d (errno %d).\n", |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 420 | tid, pterrno); |
Alexander Potapenko | 53c18d7 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 421 | return -1; |
| 422 | } |
Sergey Matveev | 115accb | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 423 | |
| 424 | *sp = regs.REG_SP; |
Alexander Potapenko | 53c18d7 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 425 | internal_memcpy(buffer, ®s, sizeof(regs)); |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | uptr SuspendedThreadsList::RegisterCount() { |
Alexey Samsonov | bb090b5 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 430 | return sizeof(regs_struct) / sizeof(uptr); |
Alexander Potapenko | 53c18d7 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 431 | } |
Alexander Potapenko | 3614c16 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 432 | } // namespace __sanitizer |
| 433 | |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 434 | #endif // SANITIZER_LINUX && defined(__x86_64__) |