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
91namespace __sanitizer {
92
93class SuspendedThreadsListLinux final : public SuspendedThreadsList {
94 public:
95 SuspendedThreadsListLinux() { thread_ids_.reserve(new_size: 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 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.
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(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
188void 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
204void 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
210bool ThreadSuspender::SuspendAllThreads() {
211 ThreadLister thread_lister(pid_);
212 bool retry = true;
213 InternalMmapVector<ThreadID> 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 (ThreadID 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.
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 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.
268static 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.
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, 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
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 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.
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 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.
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 and
407// then attempting ptrace.
408// This separate check is necessary because StopTheWorld() creates a thread
409// with a shared virtual address space and shared TLS, and therefore
410// cannot use waitpid() due to the shared errno.
411static void TestPTrace() {
412# if SANITIZER_SPARC
413 // internal_fork() on SPARC actually calls __fork(). We can't safely fork,
414 // because it's possible seccomp has been configured to disallow fork() but
415 // 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# else
424 // Heuristic: only check the first time this is called. This is not always
425 // correct (e.g., user manually triggers leak detection, then updates
426 // 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 calls
435 // SYSCALL(clone); thus, it is reasonable to use it because if seccomp kills
436 // TestPTrace(), it would have killed StopTheWorld() anyway.
437 int pid = internal_fork();
438
439 if (pid < 0) {
440 int rverrno;
441 if (internal_iserror(retval: pid, rverrno: &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 sanitizer
445 // try.
446 return;
447 }
448
449 if (pid == 0) {
450 // Child subprocess
451
452 // TODO: consider checking return value of internal_ptrace, to handle
453 // SCMP_ACT_ERRNO. However, be careful not to consume too many
454 // resources performing a proper ptrace.
455 internal_ptrace(request: PTRACE_ATTACH, pid: 0, addr: nullptr, data: nullptr);
456 internal__exit(exitcode: 0);
457 } else {
458 int wstatus;
459 internal_waitpid(pid, status: &wstatus, options: 0);
460
461 // Handle SCMP_ACT_KILL
462 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 sanitizer
468 // try.
469 }
470 }
471# endif
472}
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(a: &tracer_thread_argument.done, v: 0, mo: 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 ptrace
487 // 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 signal
492 // handler can temporary modify errno, which is shared with this thread.
493 // We ought to use pthread_sigmask here, because sigprocmask has undefined
494 // behavior in multithreaded programs. However, on linux sigprocmask is
495 // equivalent to pthread_sigmask with the exception that pthread_sigmask
496 // does not allow to block some signals used internally in pthread
497 // implementation. We are fine with blocking them here, we are really not
498 // going to pthread_cancel the thread.
499 // The tracer thread should not raise any synchronous signals. But in case it
500 // does, we setup a special handler for sync signals that properly kills the
501 // parent as well. Note: we don't pass CLONE_SIGHAND to clone, so handlers
502 // in the tracer thread won't interfere with user program. Double note: if a
503 // user does something along the lines of 'kill -11 pid', that can kill the
504 // process even if user setup own handler for SEGV.
505 // Thing to watch out for: this code should not change behavior of user code
506 // in any observable way. In particular it should not override user signal
507 // handlers.
508 internal_sigfillset(set: &blocked_sigset);
509 for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++)
510 internal_sigdelset(set: &blocked_sigset, signum: kSyncSignals[i]);
511 int rv = internal_sigprocmask(SIG_BLOCK, set: &blocked_sigset, oldset: &old_sigset);
512 CHECK_EQ(rv, 0);
513 uptr tracer_pid = internal_clone(
514 fn: TracerThread, child_stack: tracer_stack.Bottom(),
515 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,
516 arg: &tracer_thread_argument, parent_tidptr: nullptr /* parent_tidptr */,
517 newtls: nullptr /* newtls */, child_tidptr: nullptr /* child_tidptr */);
518 internal_sigprocmask(SIG_SETMASK, set: &old_sigset, oldset: 0);
519 int local_errno = 0;
520 if (internal_iserror(retval: tracer_pid, rverrno: &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 traced
526 // by the tracer thread.
527 internal_prctl(PR_SET_PTRACER, arg2: tracer_pid, arg3: 0, arg4: 0, arg5: 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 thread
531 // (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 using
534 // the spin loop below. Man page for sched_yield() says "In the Linux
535 // implementation, sched_yield() always succeeds", so let's hope it does not
536 // spoil errno. Note that this spin loop runs only for brief periods before
537 // the tracer thread has suspended us and when it starts unblocking threads.
538 while (atomic_load(a: &tracer_thread_argument.done, mo: 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(pid: tracer_pid, status: nullptr, __WALL);
544 if (!internal_iserror(retval: waitpid_status, rverrno: &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_sp
559
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 esp
568#else
569#define REG_SP rsp
570#endif
571#define ARCH_IOVEC_FOR_GETREGSET
572// Support ptrace extensions even when compiled without required kernel support
573#ifndef NT_X86_XSTATE
574#define NT_X86_XSTATE 0x202
575#endif
576#ifndef PTRACE_GETREGSET
577#define PTRACE_GETREGSET 0x4204
578#endif
579// 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 sp
593static constexpr uptr kExtraRegs[] = {0};
594#define ARCH_IOVEC_FOR_GETREGSET
595
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_GETREGSET
601
602#elif SANITIZER_RISCV64
603typedef struct user_regs_struct regs_struct;
604// sys/ucontext.h already defines REG_SP as 2. Undefine it first.
605#undef REG_SP
606#define REG_SP sp
607static constexpr uptr kExtraRegs[] = {0};
608#define ARCH_IOVEC_FOR_GETREGSET
609
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_GETREGSET
615
616#else
617#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(element: 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_GETREGSET
646 auto AppendF = [&](uptr regset) {
647 uptr size = buffer->size();
648 // NT_X86_XSTATE requires 64bit alignment.
649 uptr size_up = RoundUpTo(size, boundary: 8 / uptr_sz);
650 buffer->reserve(new_size: Max<uptr>(a: 1024, b: size_up));
651 struct iovec regset_io;
652 for (;; buffer->resize(new_size: buffer->capacity() * 2)) {
653 buffer->resize(new_size: 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(retval: internal_ptrace(PTRACE_GETREGSET, pid: tid,
659 addr: (void *)regset, data: (void *)&regset_io),
660 rverrno: &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(new_size: 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(new_size: size_up + RoundUpTo(size: regset_io.iov_len, boundary: 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#else
685 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#endif
692 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) and
695 // we should notify caller about this.
696 return pterrno == ESRCH ? REGISTERS_UNAVAILABLE_FATAL
697 : REGISTERS_UNAVAILABLE;
698 }
699
700 *sp = reinterpret_cast<regs_struct *>(buffer->data())[0].REG_SP;
701 return REGISTERS_AVAILABLE;
702}
703
704} // namespace __sanitizer
705
706#endif // SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__)
707 // || defined(__aarch64__) || defined(__powerpc64__)
708 // || defined(__s390__) || defined(__i386__) || defined(__arm__)
709 // || SANITIZER_LOONGARCH64
710