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