Nico Weber | 65492d9 | 2019-07-31 18:51:27 +0000 | [diff] [blame] | 1 | //===-- sanitizer_stoptheworld_linux_libcdep.cpp --------------------------===// |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // See sanitizer_stoptheworld.h for details. |
| 10 | // This implementation was inspired by Markus Gutschke's linuxthreads.cc. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Evgeniy Stepanov | 0af6723 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 14 | #include "sanitizer_platform.h" |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 15 | |
Alexey Baturo | 7ce4dfb | 2020-10-04 12:38:06 +0300 | [diff] [blame] | 16 | #if SANITIZER_LINUX && \ |
| 17 | (defined(__x86_64__) || defined(__mips__) || defined(__aarch64__) || \ |
| 18 | defined(__powerpc64__) || defined(__s390__) || defined(__i386__) || \ |
| 19 | defined(__arm__) || SANITIZER_RISCV64) |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 20 | |
| 21 | #include "sanitizer_stoptheworld.h" |
| 22 | |
Dmitry Vyukov | f54835f | 2013-10-15 12:57:59 +0000 | [diff] [blame] | 23 | #include "sanitizer_platform_limits_posix.h" |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 24 | #include "sanitizer_atomic.h" |
Dmitry Vyukov | f54835f | 2013-10-15 12:57:59 +0000 | [diff] [blame] | 25 | |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Sergey Matveev | 69931c5 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 27 | #include <sched.h> // for CLONE_* definitions |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 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 |
Adhemerval Zanella | d798471 | 2015-08-05 15:17:59 +0000 | [diff] [blame] | 32 | #include <sys/uio.h> // for iovec |
| 33 | #include <elf.h> // for NT_PRSTATUS |
Alexey Baturo | 7ce4dfb | 2020-10-04 12:38:06 +0300 | [diff] [blame] | 34 | #if (defined(__aarch64__) || SANITIZER_RISCV64) && !SANITIZER_ANDROID |
Renato Golin | 4481fe0 | 2015-08-05 18:34:20 +0000 | [diff] [blame] | 35 | // GLIBC 2.20+ sys/user does not include asm/ptrace.h |
Maxim Ostapenko | 3546060 | 2017-04-18 07:22:26 +0000 | [diff] [blame] | 36 | # include <asm/ptrace.h> |
| 37 | #endif |
| 38 | #include <sys/user.h> // for user_regs_struct |
| 39 | #if SANITIZER_ANDROID && SANITIZER_MIPS |
| 40 | # include <asm/reg.h> // for mips SP register in sys/user.h |
Alexey Samsonov | 8d18cc3 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 41 | #endif |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 42 | #include <sys/wait.h> // for signal-related stuff |
| 43 | |
Dmitry Vyukov | f54835f | 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 | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 52 | #include "sanitizer_common.h" |
Dmitry Vyukov | 21e9931 | 2013-10-15 15:35:56 +0000 | [diff] [blame] | 53 | #include "sanitizer_flags.h" |
Alexander Potapenko | 845b575 | 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 | 6f7ca81 | 2013-03-18 08:09:42 +0000 | [diff] [blame] | 57 | #include "sanitizer_placement_new.h" |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 58 | |
Sylvestre Ledru | a8c5460 | 2017-11-08 07:25:19 +0000 | [diff] [blame] | 59 | // Sufficiently old kernel headers don't provide this value, but we can still |
| 60 | // call prctl with it. If the runtime kernel is new enough, the prctl call will |
| 61 | // have the desired effect; if the kernel is too old, the call will error and we |
| 62 | // can ignore said error. |
| 63 | #ifndef PR_SET_PTRACER |
| 64 | #define PR_SET_PTRACER 0x59616d61 |
| 65 | #endif |
| 66 | |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 67 | // This module works by spawning a Linux task which then attaches to every |
| 68 | // thread in the caller process with ptrace. This suspends the threads, and |
| 69 | // PTRACE_GETREGS can then be used to obtain their register state. The callback |
| 70 | // supplied to StopTheWorld() is run in the tracer task while the threads are |
| 71 | // suspended. |
| 72 | // The tracer task must be placed in a different thread group for ptrace to |
| 73 | // work, so it cannot be spawned as a pthread. Instead, we use the low-level |
| 74 | // clone() interface (we want to share the address space with the caller |
| 75 | // process, so we prefer clone() over fork()). |
| 76 | // |
Sergey Matveev | 14b9924 | 2013-10-15 11:54:38 +0000 | [diff] [blame] | 77 | // We don't use any libc functions, relying instead on direct syscalls. There |
| 78 | // are two reasons for this: |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 79 | // 1. calling a library function while threads are suspended could cause a |
| 80 | // deadlock, if one of the treads happens to be holding a libc lock; |
| 81 | // 2. it's generally not safe to call libc functions from the tracer task, |
| 82 | // because clone() does not set up a thread-local storage for it. Any |
| 83 | // thread-local variables used by libc will be shared between the tracer task |
| 84 | // and the thread which spawned it. |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 85 | |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 86 | namespace __sanitizer { |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 87 | |
Vitaly Buka | d48f2d7 | 2020-11-02 17:33:22 -0800 | [diff] [blame] | 88 | class SuspendedThreadsListLinux final : public SuspendedThreadsList { |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 89 | public: |
Vitaly Buka | 44f55509 | 2018-05-07 05:56:24 +0000 | [diff] [blame] | 90 | SuspendedThreadsListLinux() { thread_ids_.reserve(1024); } |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 91 | |
Logan Smith | 8ed0213 | 2020-07-20 15:13:22 -0700 | [diff] [blame] | 92 | tid_t GetThreadID(uptr index) const override; |
Logan Smith | 8b6179f | 2020-07-20 14:38:10 -0700 | [diff] [blame] | 93 | uptr ThreadCount() const override; |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 94 | bool ContainsTid(tid_t thread_id) const; |
| 95 | void Append(tid_t tid); |
| 96 | |
Vitaly Buka | cd13476 | 2020-09-16 01:14:55 -0700 | [diff] [blame] | 97 | PtraceRegistersStatus GetRegistersAndSP(uptr index, |
| 98 | InternalMmapVector<uptr> *buffer, |
Logan Smith | 8b6179f | 2020-07-20 14:38:10 -0700 | [diff] [blame] | 99 | uptr *sp) const override; |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | InternalMmapVector<tid_t> thread_ids_; |
| 103 | }; |
| 104 | |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 105 | // Structure for passing arguments into the tracer thread. |
| 106 | struct TracerThreadArgument { |
| 107 | StopTheWorldCallback callback; |
| 108 | void *callback_argument; |
| 109 | // The tracer thread waits on this mutex while the parent finishes its |
| 110 | // preparations. |
Dmitry Vyukov | 4e15ee2 | 2021-07-29 09:44:48 +0200 | [diff] [blame] | 111 | Mutex mutex; |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 112 | // Tracer thread signals its completion by setting done. |
| 113 | atomic_uintptr_t done; |
| 114 | uptr parent_pid; |
| 115 | }; |
| 116 | |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 117 | // This class handles thread suspending/unsuspending in the tracer thread. |
| 118 | class ThreadSuspender { |
| 119 | public: |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 120 | explicit ThreadSuspender(pid_t pid, TracerThreadArgument *arg) |
| 121 | : arg(arg) |
| 122 | , pid_(pid) { |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 123 | CHECK_GE(pid, 0); |
| 124 | } |
| 125 | bool SuspendAllThreads(); |
| 126 | void ResumeAllThreads(); |
| 127 | void KillAllThreads(); |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 128 | SuspendedThreadsListLinux &suspended_threads_list() { |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 129 | return suspended_threads_list_; |
| 130 | } |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 131 | TracerThreadArgument *arg; |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 132 | private: |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 133 | SuspendedThreadsListLinux suspended_threads_list_; |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 134 | pid_t pid_; |
Kuba Mracek | ceb30b0 | 2017-04-17 18:17:38 +0000 | [diff] [blame] | 135 | bool SuspendThread(tid_t thread_id); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
Kuba Mracek | ceb30b0 | 2017-04-17 18:17:38 +0000 | [diff] [blame] | 138 | bool ThreadSuspender::SuspendThread(tid_t tid) { |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 139 | // Are we already attached to this thread? |
| 140 | // Currently this check takes linear time, however the number of threads is |
| 141 | // usually small. |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 142 | if (suspended_threads_list_.ContainsTid(tid)) return false; |
Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 143 | int pterrno; |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 144 | if (internal_iserror(internal_ptrace(PTRACE_ATTACH, tid, nullptr, nullptr), |
Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 145 | &pterrno)) { |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 146 | // Either the thread is dead, or something prevented us from attaching. |
| 147 | // Log this event and move on. |
Evgeniy Stepanov | 9fd4e9e | 2017-04-18 01:08:00 +0000 | [diff] [blame] | 148 | VReport(1, "Could not attach to thread %zu (errno %d).\n", (uptr)tid, |
| 149 | pterrno); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 150 | return false; |
| 151 | } else { |
Evgeniy Stepanov | 9fd4e9e | 2017-04-18 01:08:00 +0000 | [diff] [blame] | 152 | VReport(2, "Attached to thread %zu.\n", (uptr)tid); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 153 | // The thread is not guaranteed to stop before ptrace returns, so we must |
Dmitry Vyukov | c0e912d | 2015-02-19 09:02:29 +0000 | [diff] [blame] | 154 | // wait on it. Note: if the thread receives a signal concurrently, |
| 155 | // we can get notification about the signal before notification about stop. |
| 156 | // In such case we need to forward the signal to the thread, otherwise |
| 157 | // the signal will be missed (as we do PTRACE_DETACH with arg=0) and |
| 158 | // any logic relying on signals will break. After forwarding we need to |
| 159 | // continue to wait for stopping, because the thread is not stopped yet. |
| 160 | // We do ignore delivery of SIGSTOP, because we want to make stop-the-world |
| 161 | // as invisible as possible. |
| 162 | for (;;) { |
| 163 | int status; |
| 164 | uptr waitpid_status; |
| 165 | HANDLE_EINTR(waitpid_status, internal_waitpid(tid, &status, __WALL)); |
| 166 | int wperrno; |
| 167 | if (internal_iserror(waitpid_status, &wperrno)) { |
| 168 | // Got a ECHILD error. I don't think this situation is possible, but it |
| 169 | // doesn't hurt to report it. |
Evgeniy Stepanov | 9fd4e9e | 2017-04-18 01:08:00 +0000 | [diff] [blame] | 170 | VReport(1, "Waiting on thread %zu failed, detaching (errno %d).\n", |
| 171 | (uptr)tid, wperrno); |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 172 | internal_ptrace(PTRACE_DETACH, tid, nullptr, nullptr); |
Dmitry Vyukov | c0e912d | 2015-02-19 09:02:29 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | if (WIFSTOPPED(status) && WSTOPSIG(status) != SIGSTOP) { |
Vedant Kumar | f997bd8 | 2015-10-01 00:48:07 +0000 | [diff] [blame] | 176 | internal_ptrace(PTRACE_CONT, tid, nullptr, |
| 177 | (void*)(uptr)WSTOPSIG(status)); |
Dmitry Vyukov | c0e912d | 2015-02-19 09:02:29 +0000 | [diff] [blame] | 178 | continue; |
| 179 | } |
| 180 | break; |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 181 | } |
Dmitry Vyukov | c0e912d | 2015-02-19 09:02:29 +0000 | [diff] [blame] | 182 | suspended_threads_list_.Append(tid); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void ThreadSuspender::ResumeAllThreads() { |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 188 | for (uptr i = 0; i < suspended_threads_list_.ThreadCount(); i++) { |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 189 | pid_t tid = suspended_threads_list_.GetThreadID(i); |
Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 190 | int pterrno; |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 191 | if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, nullptr, nullptr), |
Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 192 | &pterrno)) { |
Dmitry Vyukov | b79ac88 | 2015-03-02 17:36:02 +0000 | [diff] [blame] | 193 | VReport(2, "Detached from thread %d.\n", tid); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 194 | } else { |
| 195 | // Either the thread is dead, or we are already detached. |
| 196 | // The latter case is possible, for instance, if this function was called |
| 197 | // from a signal handler. |
Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 198 | VReport(1, "Could not detach from thread %d (errno %d).\n", tid, pterrno); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void ThreadSuspender::KillAllThreads() { |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 204 | for (uptr i = 0; i < suspended_threads_list_.ThreadCount(); i++) |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 205 | internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i), |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 206 | nullptr, nullptr); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | bool ThreadSuspender::SuspendAllThreads() { |
Alexey Samsonov | 46b8665 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 210 | ThreadLister thread_lister(pid_); |
Vitaly Buka | 3f1fd79 | 2018-05-15 01:39:13 +0000 | [diff] [blame] | 211 | bool retry = true; |
Vitaly Buka | 34794a9 | 2018-05-09 21:21:26 +0000 | [diff] [blame] | 212 | InternalMmapVector<tid_t> threads; |
Vitaly Buka | e2953dc | 2018-05-07 23:29:48 +0000 | [diff] [blame] | 213 | threads.reserve(128); |
Vitaly Buka | 3f1fd79 | 2018-05-15 01:39:13 +0000 | [diff] [blame] | 214 | for (int i = 0; i < 30 && retry; ++i) { |
Vitaly Buka | 5dcc94c | 2018-05-10 04:16:44 +0000 | [diff] [blame] | 215 | retry = false; |
Vitaly Buka | e0c6ead | 2018-05-10 04:02:59 +0000 | [diff] [blame] | 216 | switch (thread_lister.ListThreads(&threads)) { |
| 217 | case ThreadLister::Error: |
| 218 | ResumeAllThreads(); |
| 219 | return false; |
| 220 | case ThreadLister::Incomplete: |
Vitaly Buka | 5dcc94c | 2018-05-10 04:16:44 +0000 | [diff] [blame] | 221 | retry = true; |
Vitaly Buka | e0c6ead | 2018-05-10 04:02:59 +0000 | [diff] [blame] | 222 | break; |
| 223 | case ThreadLister::Ok: |
| 224 | break; |
Vitaly Buka | e2953dc | 2018-05-07 23:29:48 +0000 | [diff] [blame] | 225 | } |
Vitaly Buka | d2af368 | 2019-09-12 02:20:36 +0000 | [diff] [blame] | 226 | for (tid_t tid : threads) { |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 227 | if (SuspendThread(tid)) |
Vitaly Buka | 5dcc94c | 2018-05-10 04:16:44 +0000 | [diff] [blame] | 228 | retry = true; |
Vitaly Buka | d2af368 | 2019-09-12 02:20:36 +0000 | [diff] [blame] | 229 | } |
| 230 | } |
Vitaly Buka | 327f5f3 | 2018-05-10 08:16:23 +0000 | [diff] [blame] | 231 | return suspended_threads_list_.ThreadCount(); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Pointer to the ThreadSuspender instance for use in signal handler. |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 235 | static ThreadSuspender *thread_suspender_instance = nullptr; |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 236 | |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 237 | // Synchronous signals that should not be blocked. |
| 238 | static const int kSyncSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV, SIGBUS, |
| 239 | SIGXCPU, SIGXFSZ }; |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 240 | |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 241 | static void TracerThreadDieCallback() { |
| 242 | // Generally a call to Die() in the tracer thread should be fatal to the |
| 243 | // parent process as well, because they share the address space. |
| 244 | // This really only works correctly if all the threads are suspended at this |
| 245 | // point. So we correctly handle calls to Die() from within the callback, but |
| 246 | // not those that happen before or after the callback. Hopefully there aren't |
| 247 | // a lot of opportunities for that to happen... |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 248 | ThreadSuspender *inst = thread_suspender_instance; |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 249 | if (inst && stoptheworld_tracer_pid == internal_getpid()) { |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 250 | inst->KillAllThreads(); |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 251 | thread_suspender_instance = nullptr; |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 252 | } |
Alexey Samsonov | b92aa0f | 2015-08-24 22:21:44 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Signal handler to wake up suspended threads when the tracer thread dies. |
Vitaly Buka | 8b689f4 | 2017-11-10 04:27:47 +0000 | [diff] [blame] | 256 | static void TracerThreadSignalHandler(int signum, __sanitizer_siginfo *siginfo, |
| 257 | void *uctx) { |
Vitaly Buka | 73c1016 | 2017-09-14 02:48:41 +0000 | [diff] [blame] | 258 | SignalContext ctx(siginfo, uctx); |
Alexey Samsonov | eb649bc | 2016-02-12 20:20:51 +0000 | [diff] [blame] | 259 | Printf("Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n", signum, |
| 260 | ctx.addr, ctx.pc, ctx.sp); |
Alexey Samsonov | b92aa0f | 2015-08-24 22:21:44 +0000 | [diff] [blame] | 261 | ThreadSuspender *inst = thread_suspender_instance; |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 262 | if (inst) { |
Alexey Samsonov | b92aa0f | 2015-08-24 22:21:44 +0000 | [diff] [blame] | 263 | if (signum == SIGABRT) |
| 264 | inst->KillAllThreads(); |
| 265 | else |
| 266 | inst->ResumeAllThreads(); |
| 267 | RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback)); |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 268 | thread_suspender_instance = nullptr; |
Alexey Samsonov | b92aa0f | 2015-08-24 22:21:44 +0000 | [diff] [blame] | 269 | atomic_store(&inst->arg->done, 1, memory_order_relaxed); |
| 270 | } |
| 271 | internal__exit((signum == SIGABRT) ? 1 : 2); |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 274 | // Size of alternative stack for signal handlers in the tracer thread. |
Vitaly Buka | ac03fb6 | 2017-10-15 04:18:29 +0000 | [diff] [blame] | 275 | static const int kHandlerStackSize = 8192; |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 276 | |
| 277 | // This function will be run as a cloned task. |
Alexey Samsonov | f9dbbda | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 278 | static int TracerThread(void* argument) { |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 279 | TracerThreadArgument *tracer_thread_argument = |
| 280 | (TracerThreadArgument *)argument; |
| 281 | |
Sergey Matveev | adef754 | 2013-10-08 18:01:03 +0000 | [diff] [blame] | 282 | internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); |
| 283 | // Check if parent is already dead. |
Sergey Matveev | 6f7fb43 | 2013-10-09 13:36:20 +0000 | [diff] [blame] | 284 | if (internal_getppid() != tracer_thread_argument->parent_pid) |
Sergey Matveev | adef754 | 2013-10-08 18:01:03 +0000 | [diff] [blame] | 285 | internal__exit(4); |
| 286 | |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 287 | // Wait for the parent thread to finish preparations. |
Alexey Samsonov | f9dbbda | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 288 | tracer_thread_argument->mutex.Lock(); |
| 289 | tracer_thread_argument->mutex.Unlock(); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 290 | |
Alexey Samsonov | b92aa0f | 2015-08-24 22:21:44 +0000 | [diff] [blame] | 291 | RAW_CHECK(AddDieCallback(TracerThreadDieCallback)); |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 292 | |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 293 | ThreadSuspender thread_suspender(internal_getppid(), tracer_thread_argument); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 294 | // Global pointer for the signal handler. |
| 295 | thread_suspender_instance = &thread_suspender; |
| 296 | |
| 297 | // Alternate stack for signal handling. |
Vitaly Buka | 2a20955 | 2018-05-07 05:56:36 +0000 | [diff] [blame] | 298 | InternalMmapVector<char> handler_stack_memory(kHandlerStackSize); |
Kostya Serebryany | c56d444 | 2017-07-13 21:59:01 +0000 | [diff] [blame] | 299 | stack_t handler_stack; |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 300 | internal_memset(&handler_stack, 0, sizeof(handler_stack)); |
| 301 | handler_stack.ss_sp = handler_stack_memory.data(); |
| 302 | handler_stack.ss_size = kHandlerStackSize; |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 303 | internal_sigaltstack(&handler_stack, nullptr); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 304 | |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 305 | // Install our handler for synchronous signals. Other signals should be |
| 306 | // blocked by the mask we inherited from the parent thread. |
| 307 | for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++) { |
| 308 | __sanitizer_sigaction act; |
| 309 | internal_memset(&act, 0, sizeof(act)); |
| 310 | act.sigaction = TracerThreadSignalHandler; |
| 311 | act.sa_flags = SA_ONSTACK | SA_SIGINFO; |
| 312 | internal_sigaction_norestorer(kSyncSignals[i], &act, 0); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | int exit_code = 0; |
| 316 | if (!thread_suspender.SuspendAllThreads()) { |
Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 317 | VReport(1, "Failed suspending threads.\n"); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 318 | exit_code = 3; |
| 319 | } else { |
| 320 | tracer_thread_argument->callback(thread_suspender.suspended_threads_list(), |
| 321 | tracer_thread_argument->callback_argument); |
| 322 | thread_suspender.ResumeAllThreads(); |
| 323 | exit_code = 0; |
| 324 | } |
Alexey Samsonov | b92aa0f | 2015-08-24 22:21:44 +0000 | [diff] [blame] | 325 | RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback)); |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 326 | thread_suspender_instance = nullptr; |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 327 | atomic_store(&tracer_thread_argument->done, 1, memory_order_relaxed); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 328 | return exit_code; |
| 329 | } |
| 330 | |
Alexander Potapenko | 1d021bf | 2013-04-01 14:38:56 +0000 | [diff] [blame] | 331 | class ScopedStackSpaceWithGuard { |
| 332 | public: |
| 333 | explicit ScopedStackSpaceWithGuard(uptr stack_size) { |
| 334 | stack_size_ = stack_size; |
| 335 | guard_size_ = GetPageSizeCached(); |
| 336 | // FIXME: Omitting MAP_STACK here works in current kernels but might break |
| 337 | // in the future. |
| 338 | guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_, |
| 339 | "ScopedStackWithGuard"); |
Timur Iskhodzhanov | ea1f332 | 2015-04-10 15:02:19 +0000 | [diff] [blame] | 340 | CHECK(MprotectNoAccess((uptr)guard_start_, guard_size_)); |
Alexander Potapenko | 1d021bf | 2013-04-01 14:38:56 +0000 | [diff] [blame] | 341 | } |
| 342 | ~ScopedStackSpaceWithGuard() { |
| 343 | UnmapOrDie((void *)guard_start_, stack_size_ + guard_size_); |
| 344 | } |
| 345 | void *Bottom() const { |
| 346 | return (void *)(guard_start_ + stack_size_ + guard_size_); |
| 347 | } |
| 348 | |
| 349 | private: |
| 350 | uptr stack_size_; |
| 351 | uptr guard_size_; |
| 352 | uptr guard_start_; |
| 353 | }; |
| 354 | |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 355 | // We have a limitation on the stack frame size, so some stuff had to be moved |
| 356 | // into globals. |
Alexander Potapenko | d5802fe | 2014-01-31 11:29:51 +0000 | [diff] [blame] | 357 | static __sanitizer_sigset_t blocked_sigset; |
| 358 | static __sanitizer_sigset_t old_sigset; |
Dmitry Vyukov | 6f7ca81 | 2013-03-18 08:09:42 +0000 | [diff] [blame] | 359 | |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 360 | class StopTheWorldScope { |
| 361 | public: |
| 362 | StopTheWorldScope() { |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 363 | // Make this process dumpable. Processes that are not dumpable cannot be |
| 364 | // attached to. |
| 365 | process_was_dumpable_ = internal_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0); |
| 366 | if (!process_was_dumpable_) |
| 367 | internal_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 368 | } |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 369 | |
| 370 | ~StopTheWorldScope() { |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 371 | // Restore the dumpable flag. |
| 372 | if (!process_was_dumpable_) |
| 373 | internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 374 | } |
Alexey Samsonov | ef643ce | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 375 | |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 376 | private: |
| 377 | int process_was_dumpable_; |
| 378 | }; |
| 379 | |
Sergey Matveev | 7bc300c | 2013-12-04 14:37:01 +0000 | [diff] [blame] | 380 | // When sanitizer output is being redirected to file (i.e. by using log_path), |
| 381 | // the tracer should write to the parent's log instead of trying to open a new |
| 382 | // file. Alert the logging code to the fact that we have a tracer. |
| 383 | struct ScopedSetTracerPID { |
| 384 | explicit ScopedSetTracerPID(uptr tracer_pid) { |
| 385 | stoptheworld_tracer_pid = tracer_pid; |
| 386 | stoptheworld_tracer_ppid = internal_getpid(); |
| 387 | } |
| 388 | ~ScopedSetTracerPID() { |
| 389 | stoptheworld_tracer_pid = 0; |
| 390 | stoptheworld_tracer_ppid = 0; |
| 391 | } |
| 392 | }; |
| 393 | |
Sergey Matveev | ef7db73 | 2013-08-26 13:20:31 +0000 | [diff] [blame] | 394 | void StopTheWorld(StopTheWorldCallback callback, void *argument) { |
| 395 | StopTheWorldScope in_stoptheworld; |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 396 | // Prepare the arguments for TracerThread. |
| 397 | struct TracerThreadArgument tracer_thread_argument; |
| 398 | tracer_thread_argument.callback = callback; |
| 399 | tracer_thread_argument.callback_argument = argument; |
Sergey Matveev | 6f7fb43 | 2013-10-09 13:36:20 +0000 | [diff] [blame] | 400 | tracer_thread_argument.parent_pid = internal_getpid(); |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 401 | atomic_store(&tracer_thread_argument.done, 0, memory_order_relaxed); |
Alexander Potapenko | 1d021bf | 2013-04-01 14:38:56 +0000 | [diff] [blame] | 402 | const uptr kTracerStackSize = 2 * 1024 * 1024; |
| 403 | ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize); |
Alexey Samsonov | f9dbbda | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 404 | // Block the execution of TracerThread until after we have set ptrace |
| 405 | // permissions. |
| 406 | tracer_thread_argument.mutex.Lock(); |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 407 | // Signal handling story. |
| 408 | // We don't want async signals to be delivered to the tracer thread, |
| 409 | // so we block all async signals before creating the thread. An async signal |
| 410 | // handler can temporary modify errno, which is shared with this thread. |
| 411 | // We ought to use pthread_sigmask here, because sigprocmask has undefined |
| 412 | // behavior in multithreaded programs. However, on linux sigprocmask is |
| 413 | // equivalent to pthread_sigmask with the exception that pthread_sigmask |
| 414 | // does not allow to block some signals used internally in pthread |
| 415 | // implementation. We are fine with blocking them here, we are really not |
| 416 | // going to pthread_cancel the thread. |
| 417 | // The tracer thread should not raise any synchronous signals. But in case it |
| 418 | // does, we setup a special handler for sync signals that properly kills the |
| 419 | // parent as well. Note: we don't pass CLONE_SIGHAND to clone, so handlers |
| 420 | // in the tracer thread won't interfere with user program. Double note: if a |
| 421 | // user does something along the lines of 'kill -11 pid', that can kill the |
| 422 | // process even if user setup own handler for SEGV. |
| 423 | // Thing to watch out for: this code should not change behavior of user code |
| 424 | // in any observable way. In particular it should not override user signal |
| 425 | // handlers. |
| 426 | internal_sigfillset(&blocked_sigset); |
| 427 | for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++) |
| 428 | internal_sigdelset(&blocked_sigset, kSyncSignals[i]); |
| 429 | int rv = internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset); |
| 430 | CHECK_EQ(rv, 0); |
Sergey Matveev | 69931c5 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 431 | uptr tracer_pid = internal_clone( |
| 432 | TracerThread, tracer_stack.Bottom(), |
| 433 | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED, |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 434 | &tracer_thread_argument, nullptr /* parent_tidptr */, |
| 435 | nullptr /* newtls */, nullptr /* child_tidptr */); |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 436 | internal_sigprocmask(SIG_SETMASK, &old_sigset, 0); |
Sergey Matveev | 69931c5 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 437 | int local_errno = 0; |
| 438 | if (internal_iserror(tracer_pid, &local_errno)) { |
Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 439 | VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno); |
Alexey Samsonov | f9dbbda | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 440 | tracer_thread_argument.mutex.Unlock(); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 441 | } else { |
Sergey Matveev | 7bc300c | 2013-12-04 14:37:01 +0000 | [diff] [blame] | 442 | ScopedSetTracerPID scoped_set_tracer_pid(tracer_pid); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 443 | // On some systems we have to explicitly declare that we want to be traced |
| 444 | // by the tracer thread. |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 445 | internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0); |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 446 | // Allow the tracer thread to start. |
Alexey Samsonov | f9dbbda | 2013-03-18 06:27:13 +0000 | [diff] [blame] | 447 | tracer_thread_argument.mutex.Unlock(); |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 448 | // NOTE: errno is shared between this thread and the tracer thread. |
Dmitry Vyukov | 0fc13a9 | 2015-03-06 08:43:44 +0000 | [diff] [blame] | 449 | // internal_waitpid() may call syscall() which can access/spoil errno, |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 450 | // so we can't call it now. Instead we for the tracer thread to finish using |
Dmitry Vyukov | 0fc13a9 | 2015-03-06 08:43:44 +0000 | [diff] [blame] | 451 | // the spin loop below. Man page for sched_yield() says "In the Linux |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 452 | // implementation, sched_yield() always succeeds", so let's hope it does not |
| 453 | // spoil errno. Note that this spin loop runs only for brief periods before |
| 454 | // the tracer thread has suspended us and when it starts unblocking threads. |
| 455 | while (atomic_load(&tracer_thread_argument.done, memory_order_relaxed) == 0) |
| 456 | sched_yield(); |
| 457 | // Now the tracer thread is about to exit and does not touch errno, |
| 458 | // wait for it. |
| 459 | for (;;) { |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 460 | uptr waitpid_status = internal_waitpid(tracer_pid, nullptr, __WALL); |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 461 | if (!internal_iserror(waitpid_status, &local_errno)) |
| 462 | break; |
| 463 | if (local_errno == EINTR) |
| 464 | continue; |
Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 465 | VReport(1, "Waiting on the tracer thread failed (errno %d).\n", |
| 466 | local_errno); |
Dmitry Vyukov | 72f1697 | 2015-03-05 14:37:28 +0000 | [diff] [blame] | 467 | break; |
| 468 | } |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 469 | } |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Alexander Potapenko | 9cc2e43 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 472 | // Platform-specific methods from SuspendedThreadsList. |
Sergey Matveev | 37432e8 | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 473 | #if SANITIZER_ANDROID && defined(__arm__) |
Alexey Samsonov | 8d18cc3 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 474 | typedef pt_regs regs_struct; |
Sergey Matveev | 37432e8 | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 475 | #define REG_SP ARM_sp |
| 476 | |
| 477 | #elif SANITIZER_LINUX && defined(__arm__) |
| 478 | typedef user_regs regs_struct; |
| 479 | #define REG_SP uregs[13] |
| 480 | |
| 481 | #elif defined(__i386__) || defined(__x86_64__) |
Alexey Samsonov | 8d18cc3 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 482 | typedef user_regs_struct regs_struct; |
Sergey Matveev | 37432e8 | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 483 | #if defined(__i386__) |
| 484 | #define REG_SP esp |
| 485 | #else |
| 486 | #define REG_SP rsp |
Alexey Samsonov | 8d18cc3 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 487 | #endif |
Vitaly Buka | 5813fca | 2020-09-17 12:15:00 -0700 | [diff] [blame] | 488 | #define ARCH_IOVEC_FOR_GETREGSET |
Vitaly Buka | f97ca48 | 2020-10-19 22:44:34 -0700 | [diff] [blame] | 489 | // Support ptrace extensions even when compiled without required kernel support |
| 490 | #ifndef NT_X86_XSTATE |
| 491 | #define NT_X86_XSTATE 0x202 |
| 492 | #endif |
Jeroen Dobbelaere | 8d33f08 | 2020-12-09 00:59:47 -0800 | [diff] [blame] | 493 | #ifndef PTRACE_GETREGSET |
| 494 | #define PTRACE_GETREGSET 0x4204 |
| 495 | #endif |
Vitaly Buka | 5813fca | 2020-09-17 12:15:00 -0700 | [diff] [blame] | 496 | // Compiler may use FP registers to store pointers. |
| 497 | static constexpr uptr kExtraRegs[] = {NT_X86_XSTATE, NT_FPREGSET}; |
Alexey Samsonov | 8d18cc3 | 2013-04-03 07:06:10 +0000 | [diff] [blame] | 498 | |
Kostya Serebryany | 2b42716 | 2013-05-15 12:36:29 +0000 | [diff] [blame] | 499 | #elif defined(__powerpc__) || defined(__powerpc64__) |
| 500 | typedef pt_regs regs_struct; |
| 501 | #define REG_SP gpr[PT_R1] |
| 502 | |
Kostya Serebryany | c1aa0e8 | 2013-06-03 14:49:25 +0000 | [diff] [blame] | 503 | #elif defined(__mips__) |
| 504 | typedef struct user regs_struct; |
Mohit K. Bhakkad | beb155b | 2016-03-16 08:23:10 +0000 | [diff] [blame] | 505 | # if SANITIZER_ANDROID |
| 506 | # define REG_SP regs[EF_R29] |
| 507 | # else |
| 508 | # define REG_SP regs[EF_REG29] |
| 509 | # endif |
Kostya Serebryany | c1aa0e8 | 2013-06-03 14:49:25 +0000 | [diff] [blame] | 510 | |
Adhemerval Zanella | d798471 | 2015-08-05 15:17:59 +0000 | [diff] [blame] | 511 | #elif defined(__aarch64__) |
| 512 | typedef struct user_pt_regs regs_struct; |
| 513 | #define REG_SP sp |
Vitaly Buka | 03358be | 2020-09-17 17:42:33 -0700 | [diff] [blame] | 514 | static constexpr uptr kExtraRegs[] = {0}; |
Adhemerval Zanella | d798471 | 2015-08-05 15:17:59 +0000 | [diff] [blame] | 515 | #define ARCH_IOVEC_FOR_GETREGSET |
| 516 | |
Alexey Baturo | 7ce4dfb | 2020-10-04 12:38:06 +0300 | [diff] [blame] | 517 | #elif SANITIZER_RISCV64 |
| 518 | typedef struct user_regs_struct regs_struct; |
Luís Marques | 1bc85cb | 2020-11-25 00:03:34 +0000 | [diff] [blame] | 519 | // sys/ucontext.h already defines REG_SP as 2. Undefine it first. |
| 520 | #undef REG_SP |
Alexey Baturo | 7ce4dfb | 2020-10-04 12:38:06 +0300 | [diff] [blame] | 521 | #define REG_SP sp |
| 522 | static constexpr uptr kExtraRegs[] = {0}; |
| 523 | #define ARCH_IOVEC_FOR_GETREGSET |
| 524 | |
Marcin Koscielnicki | 7ecdeb7 | 2016-04-26 10:41:30 +0000 | [diff] [blame] | 525 | #elif defined(__s390__) |
| 526 | typedef _user_regs_struct regs_struct; |
| 527 | #define REG_SP gprs[15] |
Vitaly Buka | 03358be | 2020-09-17 17:42:33 -0700 | [diff] [blame] | 528 | static constexpr uptr kExtraRegs[] = {0}; |
Marcin Koscielnicki | 7ecdeb7 | 2016-04-26 10:41:30 +0000 | [diff] [blame] | 529 | #define ARCH_IOVEC_FOR_GETREGSET |
| 530 | |
Sergey Matveev | 37432e8 | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 531 | #else |
| 532 | #error "Unsupported architecture" |
| 533 | #endif // SANITIZER_ANDROID && defined(__arm__) |
| 534 | |
Francis Ricci | 5989dd2 | 2017-04-17 20:29:38 +0000 | [diff] [blame] | 535 | tid_t SuspendedThreadsListLinux::GetThreadID(uptr index) const { |
| 536 | CHECK_LT(index, thread_ids_.size()); |
| 537 | return thread_ids_[index]; |
| 538 | } |
| 539 | |
| 540 | uptr SuspendedThreadsListLinux::ThreadCount() const { |
| 541 | return thread_ids_.size(); |
| 542 | } |
| 543 | |
| 544 | bool SuspendedThreadsListLinux::ContainsTid(tid_t thread_id) const { |
| 545 | for (uptr i = 0; i < thread_ids_.size(); i++) { |
| 546 | if (thread_ids_[i] == thread_id) return true; |
| 547 | } |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | void SuspendedThreadsListLinux::Append(tid_t tid) { |
| 552 | thread_ids_.push_back(tid); |
| 553 | } |
| 554 | |
| 555 | PtraceRegistersStatus SuspendedThreadsListLinux::GetRegistersAndSP( |
Vitaly Buka | cd13476 | 2020-09-16 01:14:55 -0700 | [diff] [blame] | 556 | uptr index, InternalMmapVector<uptr> *buffer, uptr *sp) const { |
Alexander Potapenko | 9cc2e43 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 557 | pid_t tid = GetThreadID(index); |
Vitaly Buka | 5813fca | 2020-09-17 12:15:00 -0700 | [diff] [blame] | 558 | constexpr uptr uptr_sz = sizeof(uptr); |
Peter Collingbourne | 6f4be19 | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 559 | int pterrno; |
Adhemerval Zanella | d798471 | 2015-08-05 15:17:59 +0000 | [diff] [blame] | 560 | #ifdef ARCH_IOVEC_FOR_GETREGSET |
Vitaly Buka | 5813fca | 2020-09-17 12:15:00 -0700 | [diff] [blame] | 561 | auto append = [&](uptr regset) { |
| 562 | uptr size = buffer->size(); |
| 563 | // NT_X86_XSTATE requires 64bit alignment. |
| 564 | uptr size_up = RoundUpTo(size, 8 / uptr_sz); |
| 565 | buffer->reserve(Max<uptr>(1024, size_up)); |
| 566 | struct iovec regset_io; |
| 567 | for (;; buffer->resize(buffer->capacity() * 2)) { |
| 568 | buffer->resize(buffer->capacity()); |
| 569 | uptr available_bytes = (buffer->size() - size_up) * uptr_sz; |
| 570 | regset_io.iov_base = buffer->data() + size_up; |
| 571 | regset_io.iov_len = available_bytes; |
| 572 | bool fail = |
| 573 | internal_iserror(internal_ptrace(PTRACE_GETREGSET, tid, |
| 574 | (void *)regset, (void *)®set_io), |
| 575 | &pterrno); |
| 576 | if (fail) { |
| 577 | VReport(1, "Could not get regset %p from thread %d (errno %d).\n", |
Vitaly Buka | c0e7f64 | 2020-09-18 01:17:54 -0700 | [diff] [blame] | 578 | (void *)regset, tid, pterrno); |
Vitaly Buka | 5813fca | 2020-09-17 12:15:00 -0700 | [diff] [blame] | 579 | buffer->resize(size); |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | // Far enough from the buffer size, no need to resize and repeat. |
| 584 | if (regset_io.iov_len + 64 < available_bytes) |
| 585 | break; |
| 586 | } |
| 587 | buffer->resize(size_up + RoundUpTo(regset_io.iov_len, uptr_sz) / uptr_sz); |
| 588 | return true; |
| 589 | }; |
| 590 | |
| 591 | buffer->clear(); |
| 592 | bool fail = !append(NT_PRSTATUS); |
| 593 | if (!fail) { |
| 594 | // Accept the first available and do not report errors. |
| 595 | for (uptr regs : kExtraRegs) |
Vitaly Buka | 03358be | 2020-09-17 17:42:33 -0700 | [diff] [blame] | 596 | if (regs && append(regs)) |
Vitaly Buka | 5813fca | 2020-09-17 12:15:00 -0700 | [diff] [blame] | 597 | break; |
| 598 | } |
Adhemerval Zanella | d798471 | 2015-08-05 15:17:59 +0000 | [diff] [blame] | 599 | #else |
Vitaly Buka | 5813fca | 2020-09-17 12:15:00 -0700 | [diff] [blame] | 600 | buffer->resize(RoundUpTo(sizeof(regs_struct), uptr_sz) / uptr_sz); |
| 601 | bool fail = internal_iserror( |
| 602 | internal_ptrace(PTRACE_GETREGS, tid, nullptr, buffer->data()), &pterrno); |
| 603 | if (fail) |
Sergey Matveev | 9be70fb | 2013-12-05 12:04:51 +0000 | [diff] [blame] | 604 | VReport(1, "Could not get registers from thread %d (errno %d).\n", tid, |
| 605 | pterrno); |
Vitaly Buka | 5813fca | 2020-09-17 12:15:00 -0700 | [diff] [blame] | 606 | #endif |
| 607 | if (fail) { |
Maxim Ostapenko | fe863a6 | 2017-04-06 07:42:27 +0000 | [diff] [blame] | 608 | // ESRCH means that the given thread is not suspended or already dead. |
| 609 | // Therefore it's unsafe to inspect its data (e.g. walk through stack) and |
| 610 | // we should notify caller about this. |
| 611 | return pterrno == ESRCH ? REGISTERS_UNAVAILABLE_FATAL |
| 612 | : REGISTERS_UNAVAILABLE; |
Alexander Potapenko | 9cc2e43 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 613 | } |
Sergey Matveev | 37432e8 | 2013-05-13 10:35:20 +0000 | [diff] [blame] | 614 | |
Vitaly Buka | 5813fca | 2020-09-17 12:15:00 -0700 | [diff] [blame] | 615 | *sp = reinterpret_cast<regs_struct *>(buffer->data())[0].REG_SP; |
Maxim Ostapenko | fe863a6 | 2017-04-06 07:42:27 +0000 | [diff] [blame] | 616 | return REGISTERS_AVAILABLE; |
Alexander Potapenko | 9cc2e43 | 2013-04-01 13:36:42 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Vedant Kumar | 552c011 | 2015-09-30 23:52:54 +0000 | [diff] [blame] | 619 | } // namespace __sanitizer |
Alexander Potapenko | 845b575 | 2013-03-15 14:37:21 +0000 | [diff] [blame] | 620 | |
Bill Schmidt | 2979162 | 2015-12-08 21:54:39 +0000 | [diff] [blame] | 621 | #endif // SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__) |
| 622 | // || defined(__aarch64__) || defined(__powerpc64__) |
Maxim Ostapenko | de3b9a2 | 2017-04-11 14:58:26 +0000 | [diff] [blame] | 623 | // || defined(__s390__) || defined(__i386__) || defined(__arm__) |