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