710 lines · cpp
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-exception6//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_* definitions28#include <stddef.h>29#include <sys/prctl.h> // for PR_* definitions30#include <sys/ptrace.h> // for PTRACE_* definitions31#include <sys/types.h> // for pid_t32#include <sys/uio.h> // for iovec33#include <elf.h> // for NT_PRSTATUS34#if (defined(__aarch64__) || defined(__powerpc64__) || \35 SANITIZER_RISCV64 || SANITIZER_LOONGARCH64) && \36 !SANITIZER_ANDROID37// GLIBC 2.20+ sys/user does not include asm/ptrace.h38# include <asm/ptrace.h>39#endif40#include <sys/user.h> // for user_regs_struct41# if SANITIZER_MIPS42// clang-format off43# include <asm/sgidefs.h> // <asm/sgidefs.h> must be included before <asm/reg.h>44# include <asm/reg.h> // for mips SP register45// clang-format on46# endif47# include <sys/wait.h> // for signal-related stuff48 49# ifdef sa_handler50# undef sa_handler51# endif52 53# ifdef sa_sigaction54# undef sa_sigaction55# endif56 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 still65// call prctl with it. If the runtime kernel is new enough, the prctl call will66// have the desired effect; if the kernel is too old, the call will error and we67// can ignore said error.68#ifndef PR_SET_PTRACER69#define PR_SET_PTRACER 0x59616d6170#endif71 72// This module works by spawning a Linux task which then attaches to every73// thread in the caller process with ptrace. This suspends the threads, and74// PTRACE_GETREGS can then be used to obtain their register state. The callback75// supplied to StopTheWorld() is run in the tracer task while the threads are76// suspended.77// The tracer task must be placed in a different thread group for ptrace to78// work, so it cannot be spawned as a pthread. Instead, we use the low-level79// clone() interface (we want to share the address space with the caller80// process, so we prefer clone() over fork()).81//82// We don't use any libc functions, relying instead on direct syscalls. There83// are two reasons for this:84// 1. calling a library function while threads are suspended could cause a85// 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. Any88// thread-local variables used by libc will be shared between the tracer task89// and the thread which spawned it.90 91namespace __sanitizer {92 93class SuspendedThreadsListLinux final : public SuspendedThreadsList {94 public:95 SuspendedThreadsListLinux() { thread_ids_.reserve(1024); }96 97 ThreadID GetThreadID(uptr index) const override;98 uptr ThreadCount() const override;99 bool ContainsTid(ThreadID thread_id) const;100 void Append(ThreadID tid);101 102 PtraceRegistersStatus GetRegistersAndSP(uptr index,103 InternalMmapVector<uptr> *buffer,104 uptr *sp) const override;105 106 private:107 InternalMmapVector<ThreadID> thread_ids_;108};109 110// Structure for passing arguments into the tracer thread.111struct TracerThreadArgument {112 StopTheWorldCallback callback;113 void *callback_argument;114 // The tracer thread waits on this mutex while the parent finishes its115 // 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.123class 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(ThreadID thread_id);141};142 143bool ThreadSuspender::SuspendThread(ThreadID tid) {144 int pterrno;145 if (internal_iserror(internal_ptrace(PTRACE_ATTACH, tid, nullptr, nullptr),146 &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 must155 // 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, otherwise158 // the signal will be missed (as we do PTRACE_DETACH with arg=0) and159 // any logic relying on signals will break. After forwarding we need to160 // 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-world162 // 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(waitpid_status, &wperrno)) {169 // Got a ECHILD error. I don't think this situation is possible, but it170 // 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(PTRACE_DETACH, tid, nullptr, nullptr);174 return false;175 }176 if (WIFSTOPPED(status) && WSTOPSIG(status) != SIGSTOP) {177 internal_ptrace(PTRACE_CONT, tid, nullptr,178 (void*)(uptr)WSTOPSIG(status));179 continue;180 }181 break;182 }183 suspended_threads_list_.Append(tid);184 return true;185 }186}187 188void ThreadSuspender::ResumeAllThreads() {189 for (uptr i = 0; i < suspended_threads_list_.ThreadCount(); i++) {190 pid_t tid = suspended_threads_list_.GetThreadID(i);191 int pterrno;192 if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, nullptr, nullptr),193 &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 called198 // from a signal handler.199 VReport(1, "Could not detach from thread %d (errno %d).\n", tid, pterrno);200 }201 }202}203 204void ThreadSuspender::KillAllThreads() {205 for (uptr i = 0; i < suspended_threads_list_.ThreadCount(); i++)206 internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i),207 nullptr, nullptr);208}209 210bool ThreadSuspender::SuspendAllThreads() {211 ThreadLister thread_lister(pid_);212 bool retry = true;213 InternalMmapVector<ThreadID> threads;214 threads.reserve(128);215 for (int i = 0; i < 30 && retry; ++i) {216 retry = false;217 switch (thread_lister.ListThreads(&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 (ThreadID tid : threads) {230 // Are we already attached to this thread?231 // Currently this check takes linear time, however the number of threads232 // is usually small.233 if (suspended_threads_list_.ContainsTid(tid))234 continue;235 if (SuspendThread(tid))236 retry = true;237 else238 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.247static ThreadSuspender *thread_suspender_instance = nullptr;248 249// Synchronous signals that should not be blocked.250static const int kSyncSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV, SIGBUS,251 SIGXCPU, SIGXFSZ };252 253static void TracerThreadDieCallback() {254 // Generally a call to Die() in the tracer thread should be fatal to the255 // parent process as well, because they share the address space.256 // This really only works correctly if all the threads are suspended at this257 // point. So we correctly handle calls to Die() from within the callback, but258 // not those that happen before or after the callback. Hopefully there aren't259 // 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.268static void TracerThreadSignalHandler(int signum, __sanitizer_siginfo *siginfo,269 void *uctx) {270 SignalContext ctx(siginfo, uctx);271 Printf("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 else278 inst->ResumeAllThreads();279 RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback));280 thread_suspender_instance = nullptr;281 atomic_store(&inst->arg->done, 1, memory_order_relaxed);282 }283 internal__exit((signum == SIGABRT) ? 1 : 2);284}285 286// Size of alternative stack for signal handlers in the tracer thread.287static const int kHandlerStackSize = 8192;288 289// This function will be run as a cloned task.290static int TracerThread(void* argument) {291 TracerThreadArgument *tracer_thread_argument =292 (TracerThreadArgument *)argument;293 294 internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);295 // Check if parent is already dead.296 if (internal_getppid() != tracer_thread_argument->parent_pid)297 internal__exit(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(&handler_stack, 0, sizeof(handler_stack));313 handler_stack.ss_sp = handler_stack_memory.data();314 handler_stack.ss_size = kHandlerStackSize;315 internal_sigaltstack(&handler_stack, nullptr);316 317 // Install our handler for synchronous signals. Other signals should be318 // 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(&act, 0, sizeof(act));322 act.sigaction = TracerThreadSignalHandler;323 act.sa_flags = SA_ONSTACK | SA_SIGINFO;324 internal_sigaction_norestorer(kSyncSignals[i], &act, 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(&tracer_thread_argument->done, 1, memory_order_relaxed);340 return exit_code;341}342 343class 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 break349 // in the future.350 guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_,351 "ScopedStackWithGuard");352 CHECK(MprotectNoAccess((uptr)guard_start_, guard_size_));353 }354 ~ScopedStackSpaceWithGuard() {355 UnmapOrDie((void *)guard_start_, 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 moved368// into globals.369static __sanitizer_sigset_t blocked_sigset;370static __sanitizer_sigset_t old_sigset;371 372class StopTheWorldScope {373 public:374 StopTheWorldScope() {375 // Make this process dumpable. Processes that are not dumpable cannot be376 // attached to.377 process_was_dumpable_ = internal_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);378 if (!process_was_dumpable_)379 internal_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);380 }381 382 ~StopTheWorldScope() {383 // Restore the dumpable flag.384 if (!process_was_dumpable_)385 internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 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 new394// file. Alert the logging code to the fact that we have a tracer.395struct 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// This detects whether ptrace is blocked (e.g., by seccomp), by forking and407// then attempting ptrace.408// This separate check is necessary because StopTheWorld() creates a thread409// with a shared virtual address space and shared TLS, and therefore410// cannot use waitpid() due to the shared errno.411static void TestPTrace() {412# if SANITIZER_SPARC413 // internal_fork() on SPARC actually calls __fork(). We can't safely fork,414 // because it's possible seccomp has been configured to disallow fork() but415 // allow clone().416 VReport(1, "WARNING: skipping TestPTrace() because this is SPARC\n");417 VReport(1,418 "If seccomp blocks ptrace, LeakSanitizer may hang without further "419 "notice\n");420 VReport(421 1,422 "If seccomp does not block ptrace, you can safely ignore this warning\n");423# else424 // Heuristic: only check the first time this is called. This is not always425 // correct (e.g., user manually triggers leak detection, then updates426 // seccomp, then leak detection is triggered again).427 static bool checked = false;428 if (checked)429 return;430 checked = true;431 432 // Hopefully internal_fork() is not too expensive, thanks to copy-on-write.433 // Besides, this is only called the first time.434 // Note that internal_fork() on non-SPARC Linux actually calls435 // SYSCALL(clone); thus, it is reasonable to use it because if seccomp kills436 // TestPTrace(), it would have killed StopTheWorld() anyway.437 int pid = internal_fork();438 439 if (pid < 0) {440 int rverrno;441 if (internal_iserror(pid, &rverrno))442 VReport(0, "WARNING: TestPTrace() failed to fork (errno %d)\n", rverrno);443 444 // We don't abort the sanitizer - it's still worth letting the sanitizer445 // try.446 return;447 }448 449 if (pid == 0) {450 // Child subprocess451 452 // TODO: consider checking return value of internal_ptrace, to handle453 // SCMP_ACT_ERRNO. However, be careful not to consume too many454 // resources performing a proper ptrace.455 internal_ptrace(PTRACE_ATTACH, 0, nullptr, nullptr);456 internal__exit(0);457 } else {458 int wstatus;459 internal_waitpid(pid, &wstatus, 0);460 461 // Handle SCMP_ACT_KILL462 if (WIFSIGNALED(wstatus)) {463 VReport(0,464 "WARNING: ptrace appears to be blocked (is seccomp enabled?). "465 "LeakSanitizer may hang.\n");466 VReport(0, "Child exited with signal %d.\n", WTERMSIG(wstatus));467 // We don't abort the sanitizer - it's still worth letting the sanitizer468 // try.469 }470 }471# endif472}473 474void StopTheWorld(StopTheWorldCallback callback, void *argument) {475 TestPTrace();476 477 StopTheWorldScope in_stoptheworld;478 // Prepare the arguments for TracerThread.479 struct TracerThreadArgument tracer_thread_argument;480 tracer_thread_argument.callback = callback;481 tracer_thread_argument.callback_argument = argument;482 tracer_thread_argument.parent_pid = internal_getpid();483 atomic_store(&tracer_thread_argument.done, 0, memory_order_relaxed);484 const uptr kTracerStackSize = 2 * 1024 * 1024;485 ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize);486 // Block the execution of TracerThread until after we have set ptrace487 // permissions.488 tracer_thread_argument.mutex.Lock();489 // Signal handling story.490 // We don't want async signals to be delivered to the tracer thread,491 // so we block all async signals before creating the thread. An async signal492 // handler can temporary modify errno, which is shared with this thread.493 // We ought to use pthread_sigmask here, because sigprocmask has undefined494 // behavior in multithreaded programs. However, on linux sigprocmask is495 // equivalent to pthread_sigmask with the exception that pthread_sigmask496 // does not allow to block some signals used internally in pthread497 // implementation. We are fine with blocking them here, we are really not498 // going to pthread_cancel the thread.499 // The tracer thread should not raise any synchronous signals. But in case it500 // does, we setup a special handler for sync signals that properly kills the501 // parent as well. Note: we don't pass CLONE_SIGHAND to clone, so handlers502 // in the tracer thread won't interfere with user program. Double note: if a503 // user does something along the lines of 'kill -11 pid', that can kill the504 // process even if user setup own handler for SEGV.505 // Thing to watch out for: this code should not change behavior of user code506 // in any observable way. In particular it should not override user signal507 // handlers.508 internal_sigfillset(&blocked_sigset);509 for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++)510 internal_sigdelset(&blocked_sigset, kSyncSignals[i]);511 int rv = internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset);512 CHECK_EQ(rv, 0);513 uptr tracer_pid = internal_clone(514 TracerThread, tracer_stack.Bottom(),515 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,516 &tracer_thread_argument, nullptr /* parent_tidptr */,517 nullptr /* newtls */, nullptr /* child_tidptr */);518 internal_sigprocmask(SIG_SETMASK, &old_sigset, 0);519 int local_errno = 0;520 if (internal_iserror(tracer_pid, &local_errno)) {521 VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno);522 tracer_thread_argument.mutex.Unlock();523 } else {524 ScopedSetTracerPID scoped_set_tracer_pid(tracer_pid);525 // On some systems we have to explicitly declare that we want to be traced526 // by the tracer thread.527 internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0);528 // Allow the tracer thread to start.529 tracer_thread_argument.mutex.Unlock();530 // NOTE: errno is shared between this thread and the tracer thread531 // (clone was called without CLONE_SETTLS / newtls).532 // internal_waitpid() may call syscall() which can access/spoil errno,533 // so we can't call it now. Instead we for the tracer thread to finish using534 // the spin loop below. Man page for sched_yield() says "In the Linux535 // implementation, sched_yield() always succeeds", so let's hope it does not536 // spoil errno. Note that this spin loop runs only for brief periods before537 // the tracer thread has suspended us and when it starts unblocking threads.538 while (atomic_load(&tracer_thread_argument.done, memory_order_relaxed) == 0)539 sched_yield();540 // Now the tracer thread is about to exit and does not touch errno,541 // wait for it.542 for (;;) {543 uptr waitpid_status = internal_waitpid(tracer_pid, nullptr, __WALL);544 if (!internal_iserror(waitpid_status, &local_errno))545 break;546 if (local_errno == EINTR)547 continue;548 VReport(1, "Waiting on the tracer thread failed (errno %d).\n",549 local_errno);550 break;551 }552 }553}554 555// Platform-specific methods from SuspendedThreadsList.556#if SANITIZER_ANDROID && defined(__arm__)557typedef pt_regs regs_struct;558#define REG_SP ARM_sp559 560#elif SANITIZER_LINUX && defined(__arm__)561typedef user_regs regs_struct;562#define REG_SP uregs[13]563 564#elif defined(__i386__) || defined(__x86_64__)565typedef user_regs_struct regs_struct;566#if defined(__i386__)567#define REG_SP esp568#else569#define REG_SP rsp570#endif571#define ARCH_IOVEC_FOR_GETREGSET572// Support ptrace extensions even when compiled without required kernel support573#ifndef NT_X86_XSTATE574#define NT_X86_XSTATE 0x202575#endif576#ifndef PTRACE_GETREGSET577#define PTRACE_GETREGSET 0x4204578#endif579// Compiler may use FP registers to store pointers.580static constexpr uptr kExtraRegs[] = {NT_X86_XSTATE, NT_FPREGSET};581 582#elif defined(__powerpc__) || defined(__powerpc64__)583typedef pt_regs regs_struct;584#define REG_SP gpr[PT_R1]585 586#elif defined(__mips__)587typedef struct user regs_struct;588# define REG_SP regs[EF_R29]589 590#elif defined(__aarch64__)591typedef struct user_pt_regs regs_struct;592#define REG_SP sp593static constexpr uptr kExtraRegs[] = {0};594#define ARCH_IOVEC_FOR_GETREGSET595 596#elif defined(__loongarch__)597typedef struct user_pt_regs regs_struct;598#define REG_SP regs[3]599static constexpr uptr kExtraRegs[] = {0};600#define ARCH_IOVEC_FOR_GETREGSET601 602#elif SANITIZER_RISCV64603typedef struct user_regs_struct regs_struct;604// sys/ucontext.h already defines REG_SP as 2. Undefine it first.605#undef REG_SP606#define REG_SP sp607static constexpr uptr kExtraRegs[] = {0};608#define ARCH_IOVEC_FOR_GETREGSET609 610#elif defined(__s390__)611typedef _user_regs_struct regs_struct;612#define REG_SP gprs[15]613static constexpr uptr kExtraRegs[] = {0};614#define ARCH_IOVEC_FOR_GETREGSET615 616#else617#error "Unsupported architecture"618#endif // SANITIZER_ANDROID && defined(__arm__)619 620ThreadID SuspendedThreadsListLinux::GetThreadID(uptr index) const {621 CHECK_LT(index, thread_ids_.size());622 return thread_ids_[index];623}624 625uptr SuspendedThreadsListLinux::ThreadCount() const {626 return thread_ids_.size();627}628 629bool SuspendedThreadsListLinux::ContainsTid(ThreadID thread_id) const {630 for (uptr i = 0; i < thread_ids_.size(); i++) {631 if (thread_ids_[i] == thread_id) return true;632 }633 return false;634}635 636void SuspendedThreadsListLinux::Append(ThreadID tid) {637 thread_ids_.push_back(tid);638}639 640PtraceRegistersStatus SuspendedThreadsListLinux::GetRegistersAndSP(641 uptr index, InternalMmapVector<uptr> *buffer, uptr *sp) const {642 pid_t tid = GetThreadID(index);643 constexpr uptr uptr_sz = sizeof(uptr);644 int pterrno;645#ifdef ARCH_IOVEC_FOR_GETREGSET646 auto AppendF = [&](uptr regset) {647 uptr size = buffer->size();648 // NT_X86_XSTATE requires 64bit alignment.649 uptr size_up = RoundUpTo(size, 8 / uptr_sz);650 buffer->reserve(Max<uptr>(1024, size_up));651 struct iovec regset_io;652 for (;; buffer->resize(buffer->capacity() * 2)) {653 buffer->resize(buffer->capacity());654 uptr available_bytes = (buffer->size() - size_up) * uptr_sz;655 regset_io.iov_base = buffer->data() + size_up;656 regset_io.iov_len = available_bytes;657 bool fail =658 internal_iserror(internal_ptrace(PTRACE_GETREGSET, tid,659 (void *)regset, (void *)®set_io),660 &pterrno);661 if (fail) {662 VReport(1, "Could not get regset %p from thread %d (errno %d).\n",663 (void *)regset, tid, pterrno);664 buffer->resize(size);665 return false;666 }667 668 // Far enough from the buffer size, no need to resize and repeat.669 if (regset_io.iov_len + 64 < available_bytes)670 break;671 }672 buffer->resize(size_up + RoundUpTo(regset_io.iov_len, uptr_sz) / uptr_sz);673 return true;674 };675 676 buffer->clear();677 bool fail = !AppendF(NT_PRSTATUS);678 if (!fail) {679 // Accept the first available and do not report errors.680 for (uptr regs : kExtraRegs)681 if (regs && AppendF(regs))682 break;683 }684#else685 buffer->resize(RoundUpTo(sizeof(regs_struct), uptr_sz) / uptr_sz);686 bool fail = internal_iserror(687 internal_ptrace(PTRACE_GETREGS, tid, nullptr, buffer->data()), &pterrno);688 if (fail)689 VReport(1, "Could not get registers from thread %d (errno %d).\n", tid,690 pterrno);691#endif692 if (fail) {693 // ESRCH means that the given thread is not suspended or already dead.694 // Therefore it's unsafe to inspect its data (e.g. walk through stack) and695 // we should notify caller about this.696 return pterrno == ESRCH ? REGISTERS_UNAVAILABLE_FATAL697 : REGISTERS_UNAVAILABLE;698 }699 700 *sp = reinterpret_cast<regs_struct *>(buffer->data())[0].REG_SP;701 return REGISTERS_AVAILABLE;702}703 704} // namespace __sanitizer705 706#endif // SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__)707 // || defined(__aarch64__) || defined(__powerpc64__)708 // || defined(__s390__) || defined(__i386__) || defined(__arm__)709 // || SANITIZER_LOONGARCH64710