#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
#![cfg(target_os = "linux")]
#[cfg(feature = "memory")]
mod memory;
#[cfg(feature = "notifier")]
mod notifier;
mod regs;
mod syscall_info;
#[cfg(test)]
mod test_fork;
mod waitid;
use core::mem::MaybeUninit;
use std::fmt;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use nix::sys::ptrace;
pub use nix::sys::ptrace::Options;
pub use nix::sys::signal::Signal;
use nix::sys::wait::WaitPidFlag;
use nix::sys::wait::WaitStatus;
pub use reverie_process::ExitStatus;
pub use reverie_process::Pid;
pub use syscalls::Errno;
use syscalls::Sysno;
use thiserror::Error;
#[cfg(feature = "notifier")]
pub use crate::notifier::ExitFuture;
#[cfg(feature = "notifier")]
pub use crate::notifier::GuardedPendingStatusReservation;
#[cfg(feature = "notifier")]
pub use crate::notifier::PendingStatusReservation;
#[cfg(feature = "notifier")]
pub use crate::notifier::ProcStatError;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerCleanupDriver;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerExitDriver;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerExitFuture;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerOwnedWaitFuture;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerPendingStatusReservation;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerSyncDriver;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerSyncWait;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerTerminalCleanup;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerThreadGuard;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerWaitDriver;
#[cfg(feature = "notifier")]
pub use crate::notifier::PtracerWaitFuture;
#[cfg(feature = "notifier")]
pub use crate::notifier::StopObservationError;
#[cfg(feature = "notifier")]
pub use crate::notifier::StopObservationSample;
#[cfg(feature = "notifier")]
pub use crate::notifier::StopSiginfo;
#[cfg(feature = "notifier")]
pub use crate::notifier::StoppedObservation;
#[cfg(feature = "notifier")]
pub use crate::notifier::SupersededStopRefusal;
#[cfg(feature = "notifier")]
pub use crate::notifier::TerminalCleanup;
pub use crate::regs::*;
pub use crate::syscall_info::SyscallInfo;
pub use crate::syscall_info::SyscallInfoError;
pub use crate::syscall_info::SyscallInfoOp;
use crate::waitid::IdType;
use crate::waitid::wait_status;
#[derive(Clone, Debug)]
struct TraceeToken {
#[cfg(feature = "notifier")]
event: notifier::EventHandle,
#[cfg(feature = "notifier")]
policy: notifier::WaitPolicy,
#[cfg(feature = "notifier")]
ptracer_owner: Option<std::sync::Arc<notifier::LegacyWaitOwner>>,
#[cfg(feature = "notifier")]
ptracer_wait_role: bool,
}
impl PartialEq for TraceeToken {
fn eq(&self, other: &Self) -> bool {
#[cfg(feature = "notifier")]
return self.event == other.event;
#[cfg(not(feature = "notifier"))]
{
let _ = other;
true
}
}
}
impl Eq for TraceeToken {}
impl std::hash::Hash for TraceeToken {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
#[cfg(feature = "notifier")]
{
std::hash::Hash::hash(&self.event, state);
}
#[cfg(not(feature = "notifier"))]
let _ = state;
}
}
impl TraceeToken {
fn new() -> Self {
Self {
#[cfg(feature = "notifier")]
event: notifier::EventHandle::new(),
#[cfg(feature = "notifier")]
policy: notifier::WaitPolicy::Native,
#[cfg(feature = "notifier")]
ptracer_owner: None,
#[cfg(feature = "notifier")]
ptracer_wait_role: false,
}
}
fn current_or_new(pid: Pid) -> Result<Self, Errno> {
#[cfg(not(feature = "notifier"))]
let _ = pid;
Ok(Self {
#[cfg(feature = "notifier")]
event: notifier::EventHandle::current_or_new(pid)?,
#[cfg(feature = "notifier")]
policy: notifier::WaitPolicy::Native,
#[cfg(feature = "notifier")]
ptracer_owner: None,
#[cfg(feature = "notifier")]
ptracer_wait_role: false,
})
}
fn current_or_fresh(pid: Pid) -> Self {
Self::current_or_new(pid).unwrap_or_else(|_| Self::new())
}
fn current_or_error(pid: Pid) -> Self {
#[cfg(not(feature = "notifier"))]
let _ = pid;
Self {
#[cfg(feature = "notifier")]
event: notifier::EventHandle::current_or_error(pid),
#[cfg(feature = "notifier")]
policy: notifier::WaitPolicy::Native,
#[cfg(feature = "notifier")]
ptracer_owner: None,
#[cfg(feature = "notifier")]
ptracer_wait_role: false,
}
}
#[cfg(feature = "notifier")]
fn from_event(event: notifier::EventHandle) -> Self {
Self::from_event_for_policy(event, notifier::WaitPolicy::Native)
}
#[cfg(feature = "notifier")]
fn from_event_for_policy(event: notifier::EventHandle, policy: notifier::WaitPolicy) -> Self {
let ptracer_owner = (policy == notifier::WaitPolicy::PtracerThread)
.then(|| event.capture_current_ptracer_owner().ok())
.flatten();
let ptracer_wait_role = ptracer_owner.is_some();
Self {
event,
policy,
ptracer_owner,
ptracer_wait_role,
}
}
#[cfg(feature = "notifier")]
fn current_on_ptracer_thread(pid: Pid) -> Result<Self, Errno> {
let mut token = Self::from_event_for_policy(
notifier::EventHandle::current_or_new_on_ptracer_thread(pid)?,
notifier::WaitPolicy::PtracerThread,
);
if token.ptracer_owner.is_none() && token.event().current_tracer_pid()?.as_raw() == 0 {
token.ptracer_owner = Some(token.event().capture_current_constructor_owner()?);
token.ptracer_wait_role = token
.event()
.authenticates_wait_role(token.ptracer_owner.as_ref().unwrap())
.unwrap_or(false);
}
Ok(token)
}
#[cfg(feature = "notifier")]
fn capture_child(&self, pid: Pid) -> Result<Running, Errno> {
notifier::note_waker_callbacks();
let child = if self.policy == notifier::WaitPolicy::PtracerThread {
Running::new_on_ptracer_thread(pid)
} else {
Running::from_current_or_new(pid)
};
notifier::note_waker_callbacks();
child
}
#[cfg(not(feature = "notifier"))]
fn capture_child(&self, pid: Pid) -> Result<Running, Errno> {
Running::from_current_or_new(pid)
}
#[cfg(feature = "notifier")]
fn event(&self) -> ¬ifier::EventHandle {
&self.event
}
fn on_held_tid<T>(&self, request: impl FnOnce() -> Result<T, Errno>) -> Result<T, Errno> {
#[cfg(feature = "notifier")]
if let Some(error) = self.event.initial_capture_error() {
return Err(error);
}
#[cfg(feature = "notifier")]
let Some(_held) = self.event.hold_tid() else {
return Err(Errno::ESRCH);
};
#[cfg(feature = "notifier")]
if self.policy == notifier::WaitPolicy::PtracerThread {
let owner = self.ptracer_owner.as_ref().ok_or(Errno::EPERM)?;
if !owner.is_current()? {
return Err(Errno::EPERM);
}
self.event.check_numeric_target_lifetime(owner)?;
}
request()
}
}
fn nix_errno(err: nix::Error) -> Errno {
Errno::new(err as i32)
}
#[cfg(target_arch = "x86_64")]
const NT_X86_XSTATE: i32 = 0x202;
#[derive(Error, Debug, Eq, PartialEq)]
pub enum Error {
#[error(transparent)]
Errno(#[from] Errno),
#[error("tracee {0} is a zombie")]
Died(Zombie),
}
impl From<nix::errno::Errno> for Error {
fn from(err: nix::errno::Errno) -> Self {
Self::Errno(Errno::new(err as i32))
}
}
#[cfg(feature = "notifier")]
pub use notifier::OwnedWaitError;
#[cfg(feature = "notifier")]
pub use notifier::OwnedWaitFuture;
#[derive(Debug, Eq, PartialEq)]
struct InvalidState(pub TryWait);
impl From<InvalidState> for TryWait {
fn from(error: InvalidState) -> TryWait {
error.0
}
}
impl fmt::Display for InvalidState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "got unexpected status {}", self.0)
}
}
impl std::error::Error for InvalidState {}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ChildOp {
Fork,
Vfork,
Clone,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AnySignal(i32);
impl AnySignal {
pub fn new(raw: i32) -> Option<Self> {
(1..=64).contains(&raw).then_some(Self(raw))
}
pub fn raw(self) -> i32 {
self.0
}
pub fn as_nix(self) -> Option<Signal> {
Signal::try_from(self.0).ok()
}
}
impl From<Signal> for AnySignal {
fn from(signal: Signal) -> Self {
Self(signal as i32)
}
}
impl PartialEq<Signal> for AnySignal {
fn eq(&self, other: &Signal) -> bool {
self.0 == *other as i32
}
}
impl PartialEq<AnySignal> for Signal {
fn eq(&self, other: &AnySignal) -> bool {
*self as i32 == other.0
}
}
impl std::fmt::Debug for AnySignal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.as_nix() {
Some(signal) => write!(f, "{signal:?}"),
None => write!(f, "SIG{}", self.0),
}
}
}
impl std::fmt::Display for AnySignal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.as_nix() {
Some(signal) => write!(f, "{signal}"),
None => write!(f, "SIG{}", self.0),
}
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum Event {
NewChild(ChildOp, Running),
Exec(Pid),
VforkDone,
Exit,
Seccomp,
Stop,
Syscall,
Signal(Signal),
RawSignal(i32),
}
impl Event {
pub fn signal(&self) -> Option<AnySignal> {
match *self {
Event::Signal(sig) => Some(sig.into()),
Event::RawSignal(raw) => AnySignal::new(raw),
_ => None,
}
}
pub fn from_signal(sig: AnySignal) -> Self {
match sig.as_nix() {
Some(sig) => Event::Signal(sig),
None => Event::RawSignal(sig.raw()),
}
}
fn from_ptrace_event(task: &Stopped, event: i32) -> Result<Self, Error> {
match event {
libc::PTRACE_EVENT_FORK => {
let child_pid = Pid::from_raw(task.getevent()? as i32);
#[cfg(all(test, feature = "notifier"))]
notifier::register_new_child_for_test_cleanup(task.1.event(), child_pid)?;
Ok(Self::NewChild(
ChildOp::Fork,
task.1.capture_child(child_pid)?,
))
}
libc::PTRACE_EVENT_VFORK => {
let child_pid = Pid::from_raw(task.getevent()? as i32);
#[cfg(all(test, feature = "notifier"))]
notifier::register_new_child_for_test_cleanup(task.1.event(), child_pid)?;
Ok(Self::NewChild(
ChildOp::Vfork,
task.1.capture_child(child_pid)?,
))
}
libc::PTRACE_EVENT_CLONE => {
let child_pid = Pid::from_raw(task.getevent()? as i32);
#[cfg(all(test, feature = "notifier"))]
notifier::register_new_child_for_test_cleanup(task.1.event(), child_pid)?;
Ok(Self::NewChild(
ChildOp::Clone,
task.1.capture_child(child_pid)?,
))
}
libc::PTRACE_EVENT_EXEC => {
let former_tid = Pid::from_raw(task.getevent()? as i32);
let siginfo = task.getsiginfo()?;
if siginfo.si_code != libc::SIGTRAP | (libc::PTRACE_EVENT_EXEC << 8) {
return Err(Error::Died(Zombie::from_token(task.0, task.1.clone())));
}
Ok(Self::Exec(former_tid))
}
libc::PTRACE_EVENT_VFORK_DONE => Ok(Self::VforkDone),
libc::PTRACE_EVENT_EXIT => {
Ok(Self::Exit)
}
libc::PTRACE_EVENT_SECCOMP => Ok(Self::Seccomp),
libc::PTRACE_EVENT_STOP => Ok(Self::Stop),
_ => unreachable!("unknown ptrace event {:#x}", event),
}
}
}
#[cfg(target_arch = "aarch64")]
const NT_ARM_HW_BREAK: i32 = 0x402;
#[cfg(target_arch = "aarch64")]
const NT_ARM_TLS: i32 = 0x401;
#[cfg(target_arch = "aarch64")]
const NT_ARM_SYSTEM_CALL: i32 = 0x404;
#[cfg(target_env = "musl")]
type PtraceRequest = libc::c_int;
#[cfg(not(target_env = "musl"))]
type PtraceRequest = libc::c_uint;
#[cfg(target_arch = "x86_64")]
const PTRACE_SINGLEBLOCK: PtraceRequest = 33;
fn ptrace_resume_raw(request: PtraceRequest, pid: Pid, sig: i32) -> Result<(), Errno> {
Errno::result(unsafe {
libc::ptrace(
request,
pid.as_raw(),
std::ptr::null_mut::<libc::c_void>(),
sig as libc::c_long as *mut libc::c_void,
)
})
.map(drop)
}
fn wait(id: IdType, flags: WaitPidFlag) -> Result<Option<(Pid, i32)>, Errno> {
loop {
let result = wait_status(id, flags).map(|changed| {
changed.map(|(pid, status)| {
if libc::WIFSTOPPED(status) && libc::WSTOPSIG(status) == 0 {
(pid.into(), status | (libc::SIGSTOP << 8))
} else {
(pid.into(), status)
}
})
});
if result == Err(Errno::EINTR) {
continue;
}
return result;
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum TryWait {
Wait(Wait),
Running(Running),
}
impl TryWait {
pub fn pid(&self) -> Pid {
match self {
Self::Wait(wait) => wait.pid(),
Self::Running(running) => running.pid(),
}
}
pub fn is_running(&self) -> bool {
matches!(self, Self::Running(_))
}
pub fn is_stopped(&self) -> bool {
matches!(self, Self::Wait(Wait::Stopped(_, _)))
}
pub fn assume_stopped(self) -> (Stopped, Event) {
match self {
Self::Wait(Wait::Stopped(stopped, event)) => (stopped, event),
status => panic!("{:?}", InvalidState(status)),
}
}
pub fn assume_running(self) -> Running {
match self {
Self::Running(running) => running,
status => panic!("{:?}", InvalidState(status)),
}
}
pub fn assume_exited(self) -> (Pid, ExitStatus) {
match self {
Self::Wait(Wait::Exited(pid, exit_status)) => (pid, exit_status),
status => panic!("{:?}", InvalidState(status)),
}
}
}
impl fmt::Display for TryWait {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Wait(wait) => write!(f, "{}", wait),
Self::Running(running) => write!(f, "pid {} is running", running.pid()),
}
}
}
impl From<Running> for TryWait {
fn from(status: Running) -> Self {
Self::Running(status)
}
}
impl From<Wait> for TryWait {
fn from(wait: Wait) -> Self {
Self::Wait(wait)
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum Wait {
Stopped(Stopped, Event),
Exited(Pid, ExitStatus),
}
impl Wait {
pub fn pid(&self) -> Pid {
match self {
Self::Stopped(stopped, _) => stopped.pid(),
Self::Exited(pid, _exit_status) => *pid,
}
}
pub fn assume_stopped(self) -> (Stopped, Event) {
match self {
Self::Stopped(stopped, event) => (stopped, event),
state => panic!("{:?}", InvalidState(state.into())),
}
}
pub fn assume_exited(self) -> (Pid, ExitStatus) {
match self {
Self::Exited(pid, exit_status) => (pid, exit_status),
state => panic!("{:?}", InvalidState(state.into())),
}
}
pub fn from_raw(pid: Pid, status: i32) -> Result<Self, Error> {
let token = if libc::WIFSTOPPED(status) {
TraceeToken::current_or_fresh(pid)
} else {
TraceeToken::new()
};
Self::from_raw_with_token(pid, status, token)
}
fn from_raw_with_token(pid: Pid, status: i32, token: TraceeToken) -> Result<Self, Error> {
#[cfg(feature = "notifier")]
token.event().invalidate_numeric_auth();
let wait = Self::decode_raw(pid, status, token)?;
#[cfg(all(test, feature = "notifier"))]
if let Wait::Stopped(task, Event::NewChild(..)) = &wait {
notifier::pause_sync_new_child_decode(task.1.event());
}
Ok(wait)
}
fn from_wait_helper(pid: Pid, status: i32) -> Result<Self, Error> {
let token = if libc::WIFSTOPPED(status) {
TraceeToken::current_or_fresh(pid)
} else {
TraceeToken::new()
};
Self::decode_raw(pid, status, token)
}
fn decode_raw(pid: Pid, status: i32, token: TraceeToken) -> Result<Self, Error> {
Ok(if libc::WIFEXITED(status) {
Wait::Exited(pid, ExitStatus::Exited(libc::WEXITSTATUS(status)))
} else if libc::WIFSIGNALED(status) {
Wait::Exited(
pid,
ExitStatus::signaled(libc::WTERMSIG(status), libc::WCOREDUMP(status)),
)
} else if libc::WIFSTOPPED(status) {
let task = Stopped::from_token(pid, token);
let event = if libc::WSTOPSIG(status) == libc::SIGTRAP | 0x80 {
Event::Syscall
} else if (status >> 16) == 0 {
let raw = libc::WSTOPSIG(status);
match (Signal::try_from(raw), AnySignal::new(raw)) {
(Ok(sig), _) => Event::Signal(sig),
(Err(_), Some(_)) => Event::RawSignal(raw),
(Err(_), None) => return Err(Errno::EINVAL.into()),
}
} else {
let event = status >> 16;
debug_assert!(
event == libc::PTRACE_EVENT_STOP || libc::WSTOPSIG(status) == libc::SIGTRAP
);
Event::from_ptrace_event(&task, event)?
};
Wait::Stopped(task, event)
} else if libc::WIFCONTINUED(status) {
unimplemented!("Continued status not yet handled")
} else {
panic!("PID {} got unexpected status: {:#x}", pid, status)
})
}
}
impl TryFrom<WaitStatus> for Wait {
type Error = Error;
fn try_from(wait_status: WaitStatus) -> Result<Self, Error> {
let token = match wait_status {
WaitStatus::Stopped(pid, _)
| WaitStatus::PtraceEvent(pid, ..)
| WaitStatus::PtraceSyscall(pid) => TraceeToken::current_or_fresh(pid.into()),
_ => TraceeToken::new(),
};
Self::from_wait_status_with_token(wait_status, token)
}
}
impl Wait {
fn from_wait_status_with_token(
wait_status: WaitStatus,
token: TraceeToken,
) -> Result<Self, Error> {
Ok(match wait_status {
WaitStatus::Exited(pid, code) => Self::Exited(pid.into(), ExitStatus::Exited(code)),
WaitStatus::Signaled(pid, sig, coredump) => {
Self::Exited(pid.into(), ExitStatus::Signaled(sig, coredump))
}
WaitStatus::Stopped(pid, sig) => {
let event = Event::Signal(sig);
Self::Stopped(Stopped::from_token(pid.into(), token), event)
}
WaitStatus::PtraceEvent(pid, sig, event) => {
debug_assert!(event == libc::PTRACE_EVENT_STOP || sig == Signal::SIGTRAP);
let task = Stopped::from_token(pid.into(), token);
let event = Event::from_ptrace_event(&task, event)?;
Self::Stopped(task, event)
}
WaitStatus::PtraceSyscall(pid) => {
let event = Event::Syscall;
Self::Stopped(Stopped::from_token(pid.into(), token), event)
}
WaitStatus::Continued(_pid) => {
unreachable!("unexpected WaitStatus::Continued");
}
WaitStatus::StillAlive => {
unreachable!("precondition violated with WaitStatus::StillAlive");
}
})
}
}
impl fmt::Display for Wait {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Stopped(stopped, event) => {
write!(f, "pid {} stopped ({:?})", stopped.pid(), event)
}
Self::Exited(pid, exit_status) => write!(f, "pid {} exited ({:?})", pid, exit_status),
}
}
}
#[repr(C)]
struct ptrace_peeksiginfo_args {
off: u64,
flags: u32,
nr: u32,
}
bitflags::bitflags! {
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct PeekSigInfoFlags: u32 {
const SHARED = 1;
}
}
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct TraceeGeneration(Pid, TraceeToken);
impl TraceeGeneration {
pub fn pid(&self) -> Pid {
self.0
}
#[cfg(feature = "notifier")]
pub fn terminal_cleanup(&self) -> TerminalCleanup {
TerminalCleanup::new_unregistered(self.0, &self.1)
}
#[cfg(feature = "notifier")]
pub fn ptracer_thread_guard(&self) -> Result<PtracerThreadGuard, Errno> {
if let Some(error) = self.1.event().initial_capture_error() {
return Err(error);
}
match &self.1.ptracer_owner {
Some(owner) => Ok(PtracerThreadGuard::from_owner(owner.clone())),
None => self.1.event().capture_host_thread_guard(),
}
}
#[cfg(feature = "notifier")]
pub fn retained_ptracer_thread_guard(&self) -> Result<Option<PtracerThreadGuard>, Errno> {
if let Some(error) = self.1.event().initial_capture_error() {
return Err(error);
}
Ok(self
.1
.ptracer_wait_role
.then(|| self.1.ptracer_owner.clone())
.flatten()
.map(PtracerThreadGuard::from_owner))
}
#[cfg(feature = "notifier")]
pub fn ptracer_thread_guard_for_wait(&self) -> Result<PtracerThreadGuard, Errno> {
let guard = self.ptracer_thread_guard()?;
guard.check_wait_role(self.1.event())?;
Ok(guard)
}
pub fn assume_stopped(&self) -> Stopped {
Stopped::from_token(self.0, self.1.clone())
}
}
#[derive(Hash, Eq, PartialEq)]
pub struct Stopped(Pid, TraceeToken, ExitStopMark);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExitProbe {
Full,
ReportedSiginfo,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Transition {
Cont(i32),
Syscall(i32),
Step(i32),
#[cfg(target_arch = "x86_64")]
BlockStep,
}
impl fmt::Debug for Stopped {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Stopped")
.field(&self.0)
.field(&self.1)
.finish()
}
}
#[derive(Clone, Copy, Default)]
#[cfg_attr(not(feature = "notifier"), allow(dead_code))]
struct ExitStopMark(Option<usize>);
impl PartialEq for ExitStopMark {
fn eq(&self, _: &Self) -> bool {
true
}
}
impl Eq for ExitStopMark {}
impl std::hash::Hash for ExitStopMark {
fn hash<H: std::hash::Hasher>(&self, _: &mut H) {}
}
#[repr(C)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct RseqConfiguration {
pub address: u64,
pub length: u32,
pub signature: u32,
pub flags: u32,
pub padding: u32,
}
impl Stopped {
fn map_err(&self, err: Errno) -> Error {
if err == Errno::ESRCH {
#[cfg(feature = "notifier")]
{
self.1.event().invalidate_regs();
self.1.event().stop_may_have_ended();
}
self.retire_statuses_before_exit_stop();
Error::Died(Zombie::from_token(self.0, self.1.clone()))
} else {
Error::Errno(err)
}
}
#[cfg(feature = "notifier")]
pub fn exit_event(&self) -> notifier::ExitFuture {
notifier::ExitFuture::new(self.0, &self.1)
}
#[cfg(feature = "notifier")]
pub fn exit_event_on_ptracer_thread(&self) -> PtracerExitFuture {
PtracerExitFuture::new(self.0, &self.1)
}
#[cfg(feature = "notifier")]
pub fn terminal_cleanup(&self) -> TerminalCleanup {
TerminalCleanup::new(self.0, &self.1)
}
#[cfg(feature = "notifier")]
pub fn terminal_cleanup_on_ptracer_thread(&self) -> PtracerTerminalCleanup {
PtracerTerminalCleanup::new(self.0, &self.1)
}
#[cfg(feature = "notifier")]
pub fn observation(&self) -> StoppedObservation {
StoppedObservation::new(self.0, &self.1)
}
pub fn new_unchecked(pid: Pid) -> Self {
Self::from_token(pid, TraceeToken::current_or_error(pid))
}
#[cfg(feature = "notifier")]
pub fn try_new_current_unchecked(pid: Pid) -> Result<Self, Errno> {
Ok(Self::from_token(pid, TraceeToken::current_or_new(pid)?))
}
#[cfg(feature = "notifier")]
pub fn new_unchecked_on_ptracer_thread(pid: Pid) -> Result<Self, Errno> {
Ok(Self::from_token(
pid,
TraceeToken::current_on_ptracer_thread(pid)?,
))
}
fn from_token(pid: Pid, token: TraceeToken) -> Self {
Self(pid, token, ExitStopMark(None))
}
#[cfg(feature = "notifier")]
fn from_exit_token(pid: Pid, token: TraceeToken, epoch: usize) -> Self {
Self(pid, token, ExitStopMark(Some(epoch)))
}
fn retire_statuses_before_exit_stop(&self) {
#[cfg(feature = "notifier")]
if let Some(epoch) = self.2.0 {
self.1.event().retire_statuses_before_exit_stop(epoch);
}
}
#[cfg(feature = "notifier")]
pub fn superseded_new_child(&self) -> Result<(), SupersededStopRefusal> {
match self.2.0 {
Some(epoch) => self.1.event().superseded_new_child(epoch),
None => Ok(()),
}
}
fn into_running(self) -> Running {
self.leave_stop(false)
}
fn leave_stop(self, resumed: bool) -> Running {
#[cfg(feature = "notifier")]
{
self.1.event().invalidate_regs();
self.1.event().invalidate_numeric_auth();
if resumed {
self.1.event().expect_stop_on_poll();
} else {
self.1.event().expect_stop();
}
}
#[cfg(not(feature = "notifier"))]
let _ = resumed;
self.retire_statuses_before_exit_stop();
Running::from_token(self.0, self.1)
}
fn into_running_resumed(self) -> Running {
#[cfg(feature = "notifier")]
let mark = ResumeMark(Some(
self.1.event().resume_proof(
self.0,
self.1
.ptracer_owner
.as_ref()
.filter(|_| self.1.policy == notifier::WaitPolicy::PtracerThread),
),
));
#[cfg(not(feature = "notifier"))]
let mark = ResumeMark::default();
let mut running = self.leave_stop(true);
running.2 = mark;
running
}
pub fn generation(&self) -> TraceeGeneration {
TraceeGeneration(self.0, self.1.clone())
}
pub fn pid(&self) -> Pid {
self.0
}
pub fn setoptions(&self, options: ptrace::Options) -> Result<(), Error> {
self.1
.on_held_tid(|| ptrace::setoptions(self.0.into(), options).map_err(nix_errno))
.map_err(|err| self.map_err(err))
}
fn getregset<T>(&self, which: i32) -> Result<T, Error> {
let mut regs = MaybeUninit::<T>::uninit();
let mut iov = libc::iovec {
iov_base: regs.as_mut_ptr() as *mut libc::c_void,
iov_len: core::mem::size_of_val(®s),
};
self.1
.on_held_tid(|| unsafe {
syscalls::syscall!(
Sysno::ptrace,
libc::PTRACE_GETREGSET,
self.0.as_raw(),
which,
&mut iov as *mut _
)
})
.map_err(|err| self.map_err(err))?;
if iov.iov_len != core::mem::size_of_val(®s) {
return Err(Error::Errno(Errno::EPROTO));
}
Ok(unsafe { regs.assume_init() })
}
fn setregset<T>(&self, which: i32, regs: &T) -> Result<(), Error> {
self.setregset_prefix(which, regs, core::mem::size_of::<T>())
}
fn setregset_prefix<T>(&self, which: i32, regs: &T, len: usize) -> Result<(), Error> {
assert!(len <= core::mem::size_of::<T>());
let iov = libc::iovec {
iov_base: regs as *const _ as *mut _,
iov_len: len,
};
self.1
.on_held_tid(|| unsafe {
syscalls::syscall!(
Sysno::ptrace,
libc::PTRACE_SETREGSET,
self.0.as_raw(),
which,
&iov as *const _
)
})
.map_err(|err| self.map_err(err))?;
Ok(())
}
pub fn rseq_configuration(&self) -> Result<RseqConfiguration, Errno> {
let mut config = RseqConfiguration::default();
let size = core::mem::size_of::<RseqConfiguration>();
let returned = self.1.on_held_tid(|| unsafe {
syscalls::syscall!(
Sysno::ptrace,
0x420f_u32,
self.0.as_raw(),
size,
&mut config as *mut _
)
})?;
if returned != size {
return Err(Errno::EPROTO);
}
Ok(config)
}
pub fn peekuser(&self, offset: usize) -> Result<u64, Error> {
self.flush_regs()?;
let mut word: u64 = 0;
self.1
.on_held_tid(|| unsafe {
syscalls::syscall!(
Sysno::ptrace,
libc::PTRACE_PEEKUSER,
self.0.as_raw(),
offset,
&mut word as *mut u64
)
})
.map_err(|err| self.map_err(err))?;
Ok(word)
}
pub fn pokeuser(&self, offset: usize, word: u64) -> Result<(), Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| unsafe {
syscalls::syscall!(
Sysno::ptrace,
libc::PTRACE_POKEUSER,
self.0.as_raw(),
offset,
word
)
})
.map_err(|err| self.map_err(err))?;
#[cfg(feature = "notifier")]
self.1.event().invalidate_regs();
Ok(())
}
pub fn getregs(&self) -> Result<Regs, Error> {
#[cfg(feature = "notifier")]
if self.1.event().regs_cacheable() {
if let Some(regs) = self.1.event().cached_regs() {
return self
.1
.on_held_tid(|| Ok(regs))
.map_err(|err| self.map_err(err));
}
let regs = self.getregset(libc::NT_PRSTATUS)?;
#[cfg(test)]
self.1.event().note_regs_kernel_read();
self.1.event().store_regs(regs);
return Ok(regs);
}
self.getregset(libc::NT_PRSTATUS)
}
fn flush_regs_retaining(&self) -> Result<(), Errno> {
self.flush_regs().map_err(|error| match error {
Error::Errno(errno) => errno,
Error::Died(_) => Errno::ESRCH,
})
}
pub(crate) fn flush_regs(&self) -> Result<(), Error> {
#[cfg(feature = "notifier")]
if self.1.event().regs_cacheable()
&& let Some(regs) = self.1.event().dirty_regs()
{
self.setregset(libc::NT_PRSTATUS, ®s)?;
#[cfg(test)]
self.1.event().note_regs_kernel_write();
self.1.event().mark_regs_clean();
}
Ok(())
}
pub fn setregs(&self, regs: &Regs) -> Result<(), Error> {
#[cfg(feature = "notifier")]
if self.1.event().regs_cacheable() {
let regs = *regs;
return self
.1
.on_held_tid(|| {
if !self.1.event().regs_already_current(®s) {
self.1.event().store_regs_dirty(regs);
}
Ok(())
})
.map_err(|err| self.map_err(err));
}
self.setregset(libc::NT_PRSTATUS, regs)?;
#[cfg(all(test, feature = "notifier"))]
self.1.event().note_regs_kernel_write();
Ok(())
}
pub fn getfpregs(&self) -> Result<FpRegs, Error> {
self.getregset(libc::NT_PRFPREG)
}
#[cfg(target_arch = "aarch64")]
pub fn get_thread_pointer(&self) -> Result<u64, Error> {
self.getregset(NT_ARM_TLS)
}
#[cfg(target_arch = "aarch64")]
pub fn set_thread_pointer(&self, value: u64) -> Result<(), Error> {
self.setregset(NT_ARM_TLS, &value)
}
#[cfg(target_arch = "aarch64")]
pub fn get_syscall_number(&self) -> Result<i32, Error> {
self.getregset(NT_ARM_SYSTEM_CALL)
}
pub fn setfpregs(&self, regs: &FpRegs) -> Result<(), Error> {
check_fpu_register_write()?;
self.setregset(libc::NT_PRFPREG, regs)
}
#[cfg(target_arch = "x86_64")]
pub fn getxstate(&self) -> Result<XState, Error> {
let maximum = core::arch::x86_64::__cpuid_count(0x0d, 0).ecx as usize;
let mut bytes = vec![0_u8; maximum.max(4096)];
let mut iov = libc::iovec {
iov_base: bytes.as_mut_ptr().cast(),
iov_len: bytes.len(),
};
self.1
.on_held_tid(|| unsafe {
syscalls::syscall!(
Sysno::ptrace,
libc::PTRACE_GETREGSET,
self.0.as_raw(),
NT_X86_XSTATE,
&mut iov as *mut _
)
})
.map_err(|err| self.map_err(err))?;
if iov.iov_len > bytes.len() {
return Err(Error::Errno(Errno::EOVERFLOW));
}
bytes.truncate(iov.iov_len);
Ok(XState(bytes))
}
#[cfg(target_arch = "x86_64")]
pub fn setxstate(&self, state: &XState) -> Result<(), Error> {
check_fpu_register_write()?;
let iov = libc::iovec {
iov_base: state.0.as_ptr() as *mut libc::c_void,
iov_len: state.0.len(),
};
self.1
.on_held_tid(|| unsafe {
syscalls::syscall!(
Sysno::ptrace,
libc::PTRACE_SETREGSET,
self.0.as_raw(),
NT_X86_XSTATE,
&iov as *const _
)
})
.map_err(|err| self.map_err(err))?;
Ok(())
}
pub fn resume<T: Into<Option<Signal>>>(self, sig: T) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| ptrace::cont(self.0.into(), sig).map_err(nix_errno))
.map_err(|err| self.map_err(err))?;
Ok(self.into_running_resumed())
}
pub fn resume_retaining<T: Into<Option<Signal>>>(
self,
sig: T,
) -> Result<Running, (Self, Errno)> {
if let Err(errno) = self.flush_regs_retaining() {
return Err((self, errno));
}
let result = self
.1
.on_held_tid(|| ptrace::cont(self.0.into(), sig).map_err(nix_errno));
self.finish_retained_resume_resumed(result)
}
pub fn syscall_retaining<T: Into<Option<Signal>>>(
self,
sig: T,
) -> Result<Running, (Self, Errno)> {
if let Err(errno) = self.flush_regs_retaining() {
return Err((self, errno));
}
let result = self
.1
.on_held_tid(|| ptrace::syscall(self.0.into(), sig).map_err(nix_errno));
self.finish_retained_resume_resumed(result)
}
pub fn syscall_raw_retaining(self, sig: i32) -> Result<Running, (Self, Errno)> {
if let Err(errno) = self.flush_regs_retaining() {
return Err((self, errno));
}
let result = self
.1
.on_held_tid(|| ptrace_resume_raw(libc::PTRACE_SYSCALL, self.0, sig));
self.finish_retained_resume_resumed(result)
}
#[cfg(feature = "notifier")]
pub fn wait_owned(self) -> notifier::OwnedWaitFuture {
notifier::OwnedWaitFuture::from_stopped(self)
}
#[cfg(feature = "notifier")]
pub fn wait_owned_on_ptracer_thread(self) -> PtracerOwnedWaitFuture {
PtracerOwnedWaitFuture::from_stopped(self)
}
pub(crate) fn finish_retained_resume(
self,
result: Result<(), Errno>,
) -> Result<Running, (Self, Errno)> {
self.finish_retained_transition(result, Self::into_running)
}
fn finish_retained_resume_resumed(
self,
result: Result<(), Errno>,
) -> Result<Running, (Self, Errno)> {
self.finish_retained_transition(result, Self::into_running_resumed)
}
fn finish_retained_transition(
self,
result: Result<(), Errno>,
into_running: fn(Self) -> Running,
) -> Result<Running, (Self, Errno)> {
match result {
Ok(()) => Ok(into_running(self)),
Err(error) => {
if error == Errno::ESRCH {
#[cfg(feature = "notifier")]
{
self.1.event().invalidate_regs();
self.1.event().stop_may_have_ended();
}
self.retire_statuses_before_exit_stop();
}
Err((self, error))
}
}
}
pub fn resume_raw(self, sig: i32) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| ptrace_resume_raw(libc::PTRACE_CONT, self.0, sig))
.map_err(|err| self.map_err(err))?;
Ok(self.into_running_resumed())
}
pub fn syscall_raw(self, sig: i32) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| ptrace_resume_raw(libc::PTRACE_SYSCALL, self.0, sig))
.map_err(|err| self.map_err(err))?;
Ok(self.into_running_resumed())
}
pub fn step_raw(self, sig: i32) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| ptrace_resume_raw(libc::PTRACE_SINGLESTEP, self.0, sig))
.map_err(|err| self.map_err(err))?;
Ok(self.into_running_resumed())
}
#[cfg(target_arch = "x86_64")]
pub fn block_step(self) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| ptrace_resume_raw(PTRACE_SINGLEBLOCK, self.0, 0))
.map_err(|err| self.map_err(err))?;
Ok(self.into_running_resumed())
}
pub fn step<T: Into<Option<Signal>>>(self, sig: T) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| ptrace::step(self.0.into(), sig).map_err(nix_errno))
.map_err(|err| self.map_err(err))?;
Ok(self.into_running_resumed())
}
pub fn syscall<T: Into<Option<Signal>>>(self, sig: T) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| ptrace::syscall(self.0.into(), sig).map_err(nix_errno))
.map_err(|err| self.map_err(err))?;
Ok(self.into_running_resumed())
}
#[cfg(target_arch = "aarch64")]
pub fn set_syscall(&self, nr: i32) -> Result<(), Error> {
const NT_ARM_SYSTEM_CALL: i32 = 0x404;
self.flush_regs()?;
self.setregset(NT_ARM_SYSTEM_CALL, &nr)?;
#[cfg(feature = "notifier")]
self.1.event().invalidate_regs();
Ok(())
}
#[cfg(target_arch = "aarch64")]
pub fn hw_breakpoints(&self) -> Result<HwBreakpoints, Error> {
self.getregset(NT_ARM_HW_BREAK)
}
#[cfg(target_arch = "aarch64")]
pub fn set_hw_breakpoints(
&self,
breakpoints: &HwBreakpoints,
count: usize,
) -> Result<(), Error> {
let count = count.min(breakpoints.regs.len());
let len = core::mem::offset_of!(HwBreakpoints, regs)
+ count * core::mem::size_of::<HwBreakpoint>();
self.setregset_prefix(NT_ARM_HW_BREAK, breakpoints, len)
}
pub fn getsiginfo(&self) -> Result<libc::siginfo_t, Error> {
self.1
.on_held_tid(|| ptrace::getsiginfo(self.0.into()).map_err(nix_errno))
.map_err(|err| self.map_err(err))
}
pub fn died_into_exit_stop(&self) -> Option<Error> {
match self.getsiginfo() {
Ok(siginfo)
if siginfo.si_signo == libc::SIGTRAP
&& siginfo.si_code == libc::SIGTRAP | (libc::PTRACE_EVENT_EXIT << 8) =>
{
Some(self.map_err(Errno::ESRCH))
}
Err(died @ Error::Died(_)) => Some(died),
_ => None,
}
}
#[cfg(feature = "notifier")]
pub fn died_into_exit_stop_before_resume(&self) -> Option<Error> {
self.died_into_exit_stop_before_resume_with(ExitProbe::Full)
}
#[cfg(feature = "notifier")]
pub fn died_into_exit_stop_before_resume_with(&self, probe: ExitProbe) -> Option<Error> {
match self.1.on_held_tid(|| self.left_for_exit_stop(probe)) {
Ok(true) | Err(Errno::ESRCH) => Some(self.map_err(Errno::ESRCH)),
Ok(false) => None,
Err(error) => Some(Error::Errno(error)),
}
}
#[cfg(feature = "notifier")]
pub fn transition_unless_left_for_exit(self, transition: Transition) -> Result<Running, Error> {
self.transition_unless_left_for_exit_with(transition, ExitProbe::Full)
}
#[cfg(feature = "notifier")]
pub fn transition_unless_left_for_exit_with(
self,
transition: Transition,
probe: ExitProbe,
) -> Result<Running, Error> {
let (request, signal) = match transition {
Transition::Cont(signal) => (libc::PTRACE_CONT, signal),
Transition::Syscall(signal) => (libc::PTRACE_SYSCALL, signal),
Transition::Step(signal) => (libc::PTRACE_SINGLESTEP, signal),
#[cfg(target_arch = "x86_64")]
Transition::BlockStep => (PTRACE_SINGLEBLOCK, 0),
};
self.flush_regs()?;
self.1
.on_held_tid(|| {
if self.left_for_exit_stop(probe)? {
return Err(Errno::ESRCH);
}
ptrace_resume_raw(request, self.0, signal)
})
.map_err(|err| self.map_err(err))?;
Ok(self.into_running_resumed())
}
#[cfg(feature = "notifier")]
fn left_for_exit_stop(&self, probe: ExitProbe) -> Result<bool, Errno> {
const PF_SIGNALED: u32 = 0x400;
let event = self.1.event();
let signaled = || -> Result<Option<bool>, Errno> {
match event.bound_stat_flags(self.0) {
None => Ok(None),
Some(Ok(flags)) => Ok(Some(flags & PF_SIGNALED != 0)),
Some(Err(notifier::ProcStatError::Io(Errno::ENOENT | Errno::ESRCH))) => {
match event.bound_pidfd_live(self.0) {
Some(Ok(false)) => Ok(Some(true)),
Some(Ok(true)) | None => Err(Errno::ENOENT),
Some(Err(error)) => Err(error),
}
}
Some(Err(notifier::ProcStatError::Io(error))) => Err(error),
Some(Err(_)) => Err(Errno::EIO),
}
};
if probe == ExitProbe::Full && signaled()? == Some(true) {
return Ok(true);
}
match ptrace::getsiginfo(self.0.into()) {
Err(nix::Error::ESRCH) => Ok(true),
Err(nix::Error::EINVAL) => Ok(false),
Err(error) => Err(nix_errno(error)),
Ok(info)
if info.si_signo == libc::SIGTRAP
&& info.si_code == libc::SIGTRAP | (libc::PTRACE_EVENT_EXIT << 8) =>
{
Ok(signaled()?.unwrap_or(true))
}
Ok(_) => Ok(false),
}
}
pub fn setsiginfo(&self, siginfo: &libc::siginfo_t) -> Result<(), Error> {
self.1
.on_held_tid(|| ptrace::setsiginfo(self.0.into(), siginfo).map_err(nix_errno))
.map_err(|err| self.map_err(err))
}
pub fn getsiginfo_bytes(&self) -> Result<[u8; SIGINFO_BYTES], Error> {
let mut bytes = [0u8; SIGINFO_BYTES];
self.1
.on_held_tid(|| {
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_GETSIGINFO,
self.0.as_raw(),
0,
bytes.as_mut_ptr(),
)
})
})
.map_err(|err| self.map_err(err))?;
Ok(bytes)
}
pub fn setsiginfo_bytes(&self, bytes: &[u8; SIGINFO_BYTES]) -> Result<(), Error> {
self.1
.on_held_tid(|| {
Errno::result(unsafe {
libc::ptrace(libc::PTRACE_SETSIGINFO, self.0.as_raw(), 0, bytes.as_ptr())
})
})
.map_err(|err| self.map_err(err))
.map(drop)
}
pub fn getsigmask(&self) -> Result<u64, Error> {
let mut mask: u64 = 0;
self.1
.on_held_tid(|| {
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_GETSIGMASK as _,
self.0.as_raw(),
core::mem::size_of::<u64>(),
&mut mask as *mut u64,
)
})
})
.map_err(|err| self.map_err(err))?;
Ok(mask)
}
pub fn setsigmask(&self, mask: u64) -> Result<(), Error> {
self.1
.on_held_tid(|| {
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_SETSIGMASK as _,
self.0.as_raw(),
core::mem::size_of::<u64>(),
&mask as *const u64,
)
})
})
.map_err(|err| self.map_err(err))?;
Ok(())
}
pub fn peeksiginfo<T: Into<Option<PeekSigInfoFlags>>>(
&self,
flags: T,
) -> Result<Vec<libc::siginfo_t>, Error> {
const SIGNAL_MAX: usize = 8 * core::mem::size_of::<u64>();
let mut data = MaybeUninit::<[libc::siginfo_t; SIGNAL_MAX]>::zeroed();
let mut siginfo_args = ptrace_peeksiginfo_args {
off: 0,
flags: flags.into().map_or(0, |x| x.bits()),
nr: SIGNAL_MAX as u32,
};
let count = self
.1
.on_held_tid(|| {
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_PEEKSIGINFO,
self.0.as_raw(),
&mut siginfo_args as *mut _,
data.as_mut_ptr() as *const _ as *const libc::c_void,
)
})
})
.map_err(|err| self.map_err(err))?;
Ok(unsafe { data.assume_init() }[0..count as usize].to_vec())
}
pub fn peeksiginfo_all<T: Into<Option<PeekSigInfoFlags>>>(
&self,
flags: T,
) -> Result<Vec<libc::siginfo_t>, Error> {
const CHUNK: usize = 64;
let flags = flags.into().map_or(0, |x| x.bits());
let mut data = MaybeUninit::<[libc::siginfo_t; CHUNK]>::zeroed();
let mut all = Vec::new();
loop {
let mut siginfo_args = ptrace_peeksiginfo_args {
off: all.len() as u64,
flags,
nr: CHUNK as u32,
};
let count = self
.1
.on_held_tid(|| {
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_PEEKSIGINFO,
self.0.as_raw(),
&mut siginfo_args as *mut _,
data.as_mut_ptr() as *const _ as *const libc::c_void,
)
})
})
.map_err(|err| self.map_err(err))? as usize;
if count == 0 {
return Ok(all);
}
all.extend_from_slice(&unsafe { data.assume_init_ref() }[..count]);
}
}
pub fn peeksiginfo_any<T, F>(&self, flags: T, mut matches: F) -> Result<bool, Error>
where
T: Into<Option<PeekSigInfoFlags>>,
F: FnMut(&libc::siginfo_t) -> bool,
{
const CHUNK: usize = 64;
let flags = flags.into().map_or(0, |x| x.bits());
let mut data = MaybeUninit::<[libc::siginfo_t; CHUNK]>::zeroed();
let mut off = 0u64;
loop {
let mut siginfo_args = ptrace_peeksiginfo_args {
off,
flags,
nr: CHUNK as u32,
};
let count = self
.1
.on_held_tid(|| {
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_PEEKSIGINFO,
self.0.as_raw(),
&mut siginfo_args as *mut _,
data.as_mut_ptr() as *const _ as *const libc::c_void,
)
})
})
.map_err(|err| self.map_err(err))? as usize;
if count == 0 {
return Ok(false);
}
if unsafe { data.assume_init_ref() }[..count]
.iter()
.any(&mut matches)
{
return Ok(true);
}
off += count as u64;
}
}
pub fn getevent(&self) -> Result<i64, Error> {
self.1
.on_held_tid(|| ptrace::getevent(self.0.into()).map_err(nix_errno))
.map_err(|err| self.map_err(err))
}
pub fn listen(self) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| {
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_LISTEN,
self.0.as_raw(),
std::ptr::null_mut::<libc::c_void>(),
std::ptr::null_mut::<libc::c_void>(),
)
})
.map(drop)
})
.map_err(|err| self.map_err(err))?;
Ok(self.into_running())
}
pub fn detach_retaining<T: Into<Option<Signal>>>(
self,
sig: T,
) -> Result<Running, (Self, Errno)> {
if let Err(errno) = self.flush_regs_retaining() {
return Err((self, errno));
}
let result = self.1.on_held_tid(|| {
#[cfg(feature = "notifier")]
notifier::note_detach(self.0);
ptrace::detach(self.0.into(), sig).map_err(nix_errno)
});
self.finish_retained_resume(result)
}
pub fn detach<T: Into<Option<Signal>>>(self, sig: T) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| {
#[cfg(feature = "notifier")]
notifier::note_detach(self.0);
ptrace::detach(self.0.into(), sig).map_err(nix_errno)
})
.map_err(|err| self.map_err(err))?;
Ok(self.into_running())
}
pub fn detach_raw(self, sig: i32) -> Result<Running, Error> {
self.flush_regs()?;
self.1
.on_held_tid(|| {
#[cfg(feature = "notifier")]
notifier::note_detach(self.0);
ptrace_resume_raw(libc::PTRACE_DETACH, self.0, sig)
})
.map_err(|err| self.map_err(err))?;
Ok(self.into_running())
}
}
pub fn wait_all() -> Result<Option<Wait>, Error> {
let result = wait(IdType::All, WaitPidFlag::WEXITED | WaitPidFlag::WSTOPPED)
.map_err(Error::from)
.and_then(|status| {
let (pid, status) = status.unwrap();
Wait::from_wait_helper(pid, status)
});
match result {
Ok(state) => Ok(Some(state)),
Err(Error::Errno(Errno::ECHILD)) => {
Ok(None)
}
Err(err) => Err(err),
}
}
pub fn try_wait_all() -> Result<Option<Wait>, Error> {
wait(
IdType::All,
WaitPidFlag::WEXITED | WaitPidFlag::WSTOPPED | WaitPidFlag::WNOHANG,
)?
.map(|(pid, status)| Wait::from_wait_helper(pid, status))
.transpose()
}
pub fn wait_group(pid: Pid) -> Result<Option<Wait>, Error> {
let result = wait(
IdType::Pgid(pid.into()),
WaitPidFlag::WEXITED | WaitPidFlag::WSTOPPED,
)
.map_err(Error::from)
.and_then(|status| {
let (pid, status) = status.unwrap();
Wait::from_wait_helper(pid, status)
});
match result {
Ok(state) => Ok(Some(state)),
Err(Error::Errno(Errno::ECHILD)) => {
Ok(None)
}
Err(err) => Err(err),
}
}
pub fn peek_all() -> Result<Option<Running>, Errno> {
let result = wait(
IdType::All,
WaitPidFlag::WEXITED | WaitPidFlag::WSTOPPED | WaitPidFlag::WNOWAIT,
)
.map(|state| {
state.unwrap()
});
match result {
Ok((pid, _)) => Ok(Some(Running::new(pid))),
Err(Errno::ECHILD) => {
Ok(None)
}
Err(err) => Err(err),
}
}
pub fn try_peek_all() -> Result<Option<Running>, Errno> {
let next = wait(
IdType::All,
WaitPidFlag::WEXITED | WaitPidFlag::WSTOPPED | WaitPidFlag::WNOHANG | WaitPidFlag::WNOWAIT,
)?;
Ok(next.map(|(pid, _)| Running::new(pid)))
}
#[derive(Debug, Hash, Eq, PartialEq)]
pub struct Running(Pid, TraceeToken, ResumeMark);
#[derive(Clone, Debug, Default)]
struct ResumeMark(#[cfg(feature = "notifier")] Option<notifier::ResumeProof>);
impl PartialEq for ResumeMark {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl Eq for ResumeMark {}
impl std::hash::Hash for ResumeMark {
fn hash<H: std::hash::Hasher>(&self, _state: &mut H) {}
}
impl Running {
pub fn new(pid: Pid) -> Self {
Self::from_token(pid, TraceeToken::current_or_fresh(pid))
}
#[cfg(feature = "notifier")]
pub fn try_new(pid: Pid) -> Result<Self, Errno> {
Self::from_current_or_new(pid)
}
#[cfg(feature = "notifier")]
pub fn new_on_ptracer_thread(pid: Pid) -> Result<Self, Errno> {
Ok(Self::from_token(
pid,
TraceeToken::current_on_ptracer_thread(pid)?,
))
}
fn from_token(pid: Pid, token: TraceeToken) -> Self {
Self(pid, token, ResumeMark::default())
}
fn from_current_or_new(pid: Pid) -> Result<Self, Errno> {
Ok(Self::from_token(pid, TraceeToken::current_or_new(pid)?))
}
fn newly_attached(pid: Pid) -> Self {
let running = Self::new(pid);
#[cfg(feature = "notifier")]
return Self::from_token(
pid,
TraceeToken::from_event(running.1.event().reconnect_attachment(pid)),
);
#[cfg(not(feature = "notifier"))]
running
}
pub fn attach(pid: Pid) -> Result<Self, Errno> {
ptrace::attach(pid.into()).map_err(|err| Errno::new(err as i32))?;
Ok(Self::newly_attached(pid))
}
pub fn seize(pid: Pid, options: Options) -> Result<Self, Errno> {
ptrace::seize(pid.into(), options).map_err(|err| Errno::new(err as i32))?;
Ok(Self::newly_attached(pid))
}
#[cfg(feature = "notifier")]
pub fn attach_on_ptracer_thread(pid: Pid) -> Result<Self, Errno> {
let owner = notifier::EventHandle::capture_current_host_owner()?;
let original = notifier::AttachmentAnchor::capture(pid)?;
ptrace::attach(pid.into()).map_err(nix_errno)?;
#[cfg(test)]
notifier::after_initial_attachment_for_test(pid, &original);
let mut token = TraceeToken::from_event_for_policy(
notifier::EventHandle::attachment_on_ptracer_thread(pid, &original),
notifier::WaitPolicy::PtracerThread,
);
token.ptracer_owner = Some(owner);
token.ptracer_wait_role = token.event().initial_capture_error().is_none();
Ok(Self::from_token(pid, token))
}
#[cfg(feature = "notifier")]
pub fn seize_on_ptracer_thread(pid: Pid, options: Options) -> Result<Self, Errno> {
let owner = notifier::EventHandle::capture_current_host_owner()?;
let original = notifier::AttachmentAnchor::capture(pid)?;
ptrace::seize(pid.into(), options).map_err(nix_errno)?;
#[cfg(test)]
notifier::after_initial_attachment_for_test(pid, &original);
let mut token = TraceeToken::from_event_for_policy(
notifier::EventHandle::attachment_on_ptracer_thread(pid, &original),
notifier::WaitPolicy::PtracerThread,
);
token.ptracer_owner = Some(owner);
token.ptracer_wait_role = token.event().initial_capture_error().is_none();
Ok(Self::from_token(pid, token))
}
pub fn interrupt(&self) -> Result<(), Errno> {
self.1.on_held_tid(|| {
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_INTERRUPT,
self.0.as_raw(),
std::ptr::null_mut::<libc::c_void>(),
std::ptr::null_mut::<libc::c_void>(),
)
})
.map(drop)
})?;
#[cfg(feature = "notifier")]
self.1.event().expect_stop_on_poll();
Ok(())
}
pub fn pid(&self) -> Pid {
self.0
}
pub fn generation(&self) -> TraceeGeneration {
TraceeGeneration(self.0, self.1.clone())
}
pub fn wait(self) -> Result<Wait, Error> {
let pid = self.0;
let token = self.1;
#[cfg(feature = "notifier")]
{
notifier::wait_sync(pid, token)
}
#[cfg(not(feature = "notifier"))]
{
wait(
IdType::Pid(pid.into()),
WaitPidFlag::WEXITED | WaitPidFlag::WSTOPPED,
)
.map_err(Error::from)
.and_then(|status| {
let (_, status) = status.unwrap();
Wait::decode_raw(pid, status, token)
})
}
}
#[cfg(feature = "notifier")]
pub fn wait_sync_on_ptracer_thread(self) -> PtracerSyncWait {
PtracerSyncWait::new(self)
}
#[cfg(feature = "notifier")]
pub async fn wait_until<F>(mut self, mut pred: F) -> Result<Wait, Error>
where
F: FnMut(&Event) -> bool,
{
loop {
match self.next_state().await? {
Wait::Stopped(stopped, event) => {
if pred(&event) {
break Ok(Wait::Stopped(stopped, event));
}
self = match event {
Event::Signal(sig) => stopped.resume(Some(sig))?,
Event::RawSignal(raw) => stopped.resume_raw(raw)?,
_ => stopped.resume(None)?,
};
}
task => break Ok(task),
}
}
}
#[cfg(feature = "notifier")]
pub async fn wait_for_signal(self, sig: Signal) -> Result<Wait, Error> {
self.wait_until(|event| event == &Event::Signal(sig)).await
}
#[cfg(feature = "notifier")]
pub fn exit_event(&self) -> notifier::ExitFuture {
notifier::ExitFuture::new(self.0, &self.1)
}
#[cfg(feature = "notifier")]
pub fn exit_event_on_ptracer_thread(&self) -> PtracerExitFuture {
PtracerExitFuture::new(self.0, &self.1)
}
#[cfg(feature = "notifier")]
pub fn terminal_cleanup(&self) -> TerminalCleanup {
TerminalCleanup::new(self.0, &self.1)
}
#[cfg(feature = "notifier")]
pub fn terminal_cleanup_on_ptracer_thread(&self) -> PtracerTerminalCleanup {
PtracerTerminalCleanup::new(self.0, &self.1)
}
#[cfg(feature = "notifier")]
pub fn wait_owned(self) -> notifier::OwnedWaitFuture {
notifier::OwnedWaitFuture::new(self)
}
#[cfg(feature = "notifier")]
pub fn wait_owned_on_ptracer_thread(self) -> PtracerOwnedWaitFuture {
PtracerOwnedWaitFuture::new(self)
}
#[cfg(feature = "notifier")]
pub fn next_state_on_ptracer_thread(self) -> PtracerWaitFuture {
self.wait_owned_on_ptracer_thread()
}
#[cfg(feature = "notifier")]
pub async fn next_state(self) -> Result<Wait, Error> {
notifier::WaitFuture::new(self).await
}
}
#[derive(Debug, Hash, Eq, PartialEq)]
pub struct Zombie(Running);
impl Zombie {
fn from_token(pid: Pid, token: TraceeToken) -> Self {
Zombie(Running::from_token(pid, token))
}
pub fn pid(&self) -> Pid {
self.0.pid()
}
pub fn generation(&self) -> TraceeGeneration {
self.0.generation()
}
#[cfg(feature = "notifier")]
pub fn wait_owned(self) -> notifier::OwnedWaitFuture {
self.0.wait_owned()
}
#[cfg(feature = "notifier")]
pub fn wait_owned_on_ptracer_thread(self) -> PtracerOwnedWaitFuture {
self.0.wait_owned_on_ptracer_thread()
}
#[cfg(feature = "notifier")]
pub async fn reap(self) -> Result<ExitStatus, Error> {
let mut next_state = self.0.next_state().await;
loop {
match next_state {
Ok(wait) => match wait {
Wait::Stopped(stopped, event) => {
if let Event::Exit = event {
next_state = match stopped.resume(None) {
Ok(task) => task.next_state().await,
Err(err) => Err(err),
};
} else {
panic!("Task {:?} unexpected stop event {:?}", stopped, event)
}
}
Wait::Exited(_pid, exit_status) => break Ok(exit_status),
},
Err(Error::Died(zombie)) => next_state = zombie.0.next_state().await,
Err(error) => break Err(error),
}
}
}
}
impl fmt::Display for Zombie {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.pid())
}
}
pub fn traceme_and_stop() -> Result<(), Errno> {
ptrace::traceme()
.and_then(|()| nix::sys::signal::raise(Signal::SIGSTOP))
.map_err(|e| Errno::new(e as i32))?;
Ok(())
}
pub const SIGINFO_BYTES: usize = 128;
static FPU_REGISTER_WRITES_FORBIDDEN: AtomicBool = AtomicBool::new(false);
pub fn forbid_fpu_register_writes() {
FPU_REGISTER_WRITES_FORBIDDEN.store(true, Ordering::SeqCst);
}
pub fn fpu_register_writes_forbidden() -> bool {
FPU_REGISTER_WRITES_FORBIDDEN.load(Ordering::SeqCst)
}
fn check_fpu_register_write() -> Result<(), Error> {
if fpu_register_writes_forbidden() {
Err(Error::Errno(Errno::EPERM))
} else {
Ok(())
}
}
#[cfg(test)]
mod test {
use std::io;
use std::os::fd::BorrowedFd;
use std::thread;
use nix::sys::signal;
use nix::sys::signal::Signal;
use nix::unistd::ForkResult;
use tokio as _;
use super::*;
use crate::test_fork::fork;
fn trace<F>(f: F, options: Options) -> Result<(Pid, Stopped), Error>
where
F: FnOnce() -> i32,
{
match unsafe { fork() }? {
ForkResult::Parent { child, .. } => {
let mut running = Running::seize(child.into(), options)?;
let stopped = loop {
match running.wait()? {
Wait::Stopped(stopped, event) => {
if event == Event::Signal(Signal::SIGSTOP) || event == Event::Stop {
break stopped;
} else if let Event::Signal(sig) = event {
running = stopped.resume(Some(sig))?;
} else if let Event::RawSignal(raw) = event {
running = stopped.resume_raw(raw)?;
} else {
running = stopped.resume(None)?;
}
}
task => panic!("Got unexpected exit: {:?}", task),
}
};
Ok((stopped.pid(), stopped))
}
ForkResult::Child => {
let _ = unsafe { libc::setpgid(0, 0) };
let limit = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
let _ = unsafe { libc::setrlimit(libc::RLIMIT_CORE, &limit) };
signal::raise(Signal::SIGSTOP).unwrap();
let exit_code = f();
unsafe { ::libc::_exit(exit_code) };
}
}
}
#[cfg(target_arch = "x86_64")]
fn exercise_retained_syscall_stops(raw: bool) -> Result<(), Box<dyn std::error::Error>> {
let (pid, mut stopped) = trace(
|| {
unsafe {
libc::syscall(libc::SYS_getpid);
}
23
},
Options::PTRACE_O_EXITKILL | Options::PTRACE_O_TRACESYSGOOD,
)?;
let generation = stopped.generation();
let mut found = false;
for _ in 0..8 {
let running = if raw {
stopped.syscall_raw_retaining(0)
} else {
stopped.syscall_retaining(None)
}
.map_err(|(_, error)| error)?;
let (next, event) = running.wait()?.assume_stopped();
stopped = next;
assert_eq!(event, Event::Syscall);
assert_eq!(stopped.generation(), generation);
let info = stopped.getsyscallinfo()?;
assert_eq!(info.architecture, 0xc000_003e);
if matches!(info.operation, SyscallInfoOp::Entry { number, .. } if number == libc::SYS_getpid as u64)
{
found = true;
break;
}
}
assert!(
found,
"did not observe the actual getpid entry within the fixture bound"
);
let entry = stopped.getsyscallinfo()?;
let running = if raw {
stopped.syscall_raw_retaining(0)
} else {
stopped.syscall_retaining(None)
}
.map_err(|(_, error)| error)?;
let (stopped, event) = running.wait()?.assume_stopped();
assert_eq!(event, Event::Syscall);
assert_eq!(stopped.generation(), generation);
let exit = stopped.getsyscallinfo()?;
assert_eq!(exit.architecture, entry.architecture);
assert_eq!(exit.instruction_pointer, entry.instruction_pointer);
assert_eq!(exit.stack_pointer, entry.stack_pointer);
assert_eq!(
exit.operation,
SyscallInfoOp::Exit {
return_value: i64::from(pid.as_raw()),
is_error: false,
}
);
assert_eq!(
stopped
.resume_retaining(None)
.map_err(|(_, error)| error)?
.wait()?,
Wait::Exited(pid, ExitStatus::Exited(23)),
);
Ok(())
}
#[cfg(target_arch = "x86_64")]
#[test]
fn syscall_retaining_reaches_actual_entry_and_exit() -> Result<(), Box<dyn std::error::Error>> {
exercise_retained_syscall_stops(false)
}
#[cfg(target_arch = "x86_64")]
#[test]
fn syscall_raw_retaining_reaches_actual_entry_and_exit()
-> Result<(), Box<dyn std::error::Error>> {
exercise_retained_syscall_stops(true)
}
#[cfg(target_arch = "x86_64")]
#[test]
fn syscall_raw_retaining_kernel_refusal_keeps_original_stop()
-> Result<(), Box<dyn std::error::Error>> {
let (pid, stopped) = trace(|| 23, Options::PTRACE_O_EXITKILL)?;
let generation = stopped.generation();
let before = stopped.getregs()?;
let mask = stopped.getsigmask()?;
let (stopped, error) = stopped.syscall_raw_retaining(i32::MAX).unwrap_err();
let after = stopped.getregs()?;
let after_mask = stopped.getsigmask()?;
let same_generation = stopped.generation() == generation;
let terminal = stopped
.resume_retaining(None)
.map_err(|(_, error)| error)?
.wait()?;
assert_eq!(terminal, Wait::Exited(pid, ExitStatus::Exited(23)));
assert_eq!(error, Errno::EIO);
assert!(same_generation);
assert_eq!(after, before);
assert_eq!(after_mask, mask);
Ok(())
}
#[test]
fn listen_keeps_group_stop_until_sigcont() -> Result<(), Box<dyn std::error::Error>> {
let (pid, stopped) = trace(
|| {
signal::kill(nix::unistd::Pid::this(), Signal::SIGSTOP).unwrap();
7
},
Options::PTRACE_O_EXITKILL,
)?;
let mut running = stopped.resume(None)?;
let stopped = loop {
match running.wait()?.assume_stopped() {
(stopped, Event::Stop) => break stopped,
(stopped, Event::Signal(sig)) => {
running = stopped.resume(Some(sig))?;
}
(_stopped, event) => panic!("expected group stop, got {event:?}"),
}
};
let mut running = stopped.listen()?;
let status = std::fs::read_to_string(format!("/proc/{}/status", pid.as_raw())).unwrap();
let state = status
.lines()
.find(|line| line.starts_with("State:"))
.expect("proc status state");
assert!(
state.contains("(stopped)") || state.contains("(tracing stop)"),
"listening task must stay stopped, got {state}"
);
signal::kill(nix::unistd::Pid::from_raw(pid.as_raw()), Signal::SIGCONT)?;
for _ in 0..8 {
match running.wait()? {
Wait::Exited(got, status) => {
assert_eq!((got, status), (pid, ExitStatus::Exited(7)));
return Ok(());
}
Wait::Stopped(task, Event::Signal(Signal::SIGCONT)) => {
running = task.resume(Some(Signal::SIGCONT))?;
}
Wait::Stopped(task, _) => {
running = task.resume(None)?;
}
}
}
panic!("listened child did not exit after SIGCONT");
}
#[cfg(feature = "notifier")]
#[test]
fn process_sigkill_kills_group_of_exited_leader_thread()
-> Result<(), Box<dyn std::error::Error>> {
let (pid, stopped) = trace(
|| {
extern "C" fn sleep_then_exit(_: *mut libc::c_void) -> *mut libc::c_void {
let mut left = 5;
while left > 0 {
left = unsafe { libc::sleep(left) };
}
unsafe { libc::_exit(0) }
}
let mut sibling = std::mem::MaybeUninit::<libc::pthread_t>::uninit();
let created = unsafe {
libc::pthread_create(
sibling.as_mut_ptr(),
std::ptr::null(),
sleep_then_exit,
std::ptr::null_mut(),
)
};
if created != 0 {
unsafe { libc::_exit(126) };
}
loop {
unsafe { libc::syscall(libc::SYS_exit, 0) };
}
},
Options::PTRACE_O_EXITKILL,
)?;
let cleanup = stopped.terminal_cleanup();
let running = stopped.resume(None)?;
let status_path = format!("/proc/{}/status", pid.as_raw());
let zombie = (0..200).any(|_| {
let state = std::fs::read_to_string(&status_path).unwrap_or_default();
if state
.lines()
.any(|line| line.starts_with("State:") && line.contains("zombie"))
{
return true;
}
thread::sleep(std::time::Duration::from_millis(10));
false
});
assert!(zombie, "traced leader thread did not become a zombie");
let started = std::time::Instant::now();
cleanup.request_process_sigkill()?;
match running.wait()? {
Wait::Exited(got, status) => {
assert_eq!(got, pid);
assert!(
matches!(status, ExitStatus::Signaled(Signal::SIGKILL, _)),
"unexpected status {status:?}"
);
}
other => panic!("expected the group's terminal status, got {other:?}"),
}
assert!(started.elapsed() < std::time::Duration::from_secs(3));
Ok(())
}
#[test]
fn fpu_register_writes_fail_once_forbidden() -> Result<(), Box<dyn std::error::Error + 'static>>
{
let (pid, tracee) = trace(|| 0, Options::PTRACE_O_EXITKILL)?;
let fpregs = tracee.getfpregs()?;
tracee.setfpregs(&fpregs)?;
#[cfg(target_arch = "x86_64")]
let xstate = tracee.getxstate()?;
#[cfg(target_arch = "x86_64")]
tracee.setxstate(&xstate)?;
forbid_fpu_register_writes();
assert!(fpu_register_writes_forbidden());
assert!(matches!(
tracee.setfpregs(&fpregs),
Err(Error::Errno(Errno::EPERM))
));
#[cfg(target_arch = "x86_64")]
assert!(matches!(
tracee.setxstate(&xstate),
Err(Error::Errno(Errno::EPERM))
));
tracee.getfpregs()?;
assert_eq!(
tracee.resume(None)?.wait()?,
Wait::Exited(pid, ExitStatus::Exited(0))
);
Ok(())
}
#[test]
fn basic() -> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(|| 42, Options::empty())?;
assert_eq!(
tracee.resume(None)?.wait()?,
Wait::Exited(pid, ExitStatus::Exited(42))
);
Ok(())
}
#[test]
fn stop_on_exit() -> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(
|| 42,
Options::PTRACE_O_EXITKILL | Options::PTRACE_O_TRACEEXIT,
)?;
let running = tracee.resume(None)?;
let (stopped, event) = running.wait()?.assume_stopped();
assert_eq!(event, Event::Exit);
assert_eq!(
stopped.resume(None)?.wait()?,
Wait::Exited(pid, ExitStatus::Exited(42))
);
Ok(())
}
#[cfg(target_arch = "x86_64")]
const PRSTATUS_BYTES: usize = core::mem::size_of::<libc::user_regs_struct>();
#[cfg(target_arch = "x86_64")]
const PRSTATUS_CS: usize = core::mem::offset_of!(libc::user_regs_struct, cs);
#[cfg(target_arch = "x86_64")]
fn raw_prstatus(tid: i32) -> Result<([u8; PRSTATUS_BYTES], usize), Errno> {
let mut bytes = [0xa5u8; PRSTATUS_BYTES];
let mut iov = libc::iovec {
iov_base: bytes.as_mut_ptr().cast(),
iov_len: bytes.len(),
};
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_GETREGSET,
tid,
libc::NT_PRSTATUS as usize as *mut libc::c_void,
&mut iov as *mut libc::iovec,
)
})?;
Ok((bytes, iov.iov_len))
}
#[cfg(target_arch = "x86_64")]
fn set_native_prstatus(tid: i32, bytes: &[u8; PRSTATUS_BYTES]) -> Result<(), Errno> {
Errno::result(unsafe {
libc::ptrace(
libc::PTRACE_SETREGS,
tid,
std::ptr::null_mut::<libc::c_void>(),
bytes.as_ptr(),
)
})
.map(|_| ())
}
#[cfg(target_arch = "x86_64")]
#[test]
fn generic_getregs_refuses_actual_compat_and_restores()
-> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(|| 42, Options::PTRACE_O_EXITKILL)?;
let tid = pid.as_raw();
let (original, length) = raw_prstatus(tid)?;
assert_eq!(length, PRSTATUS_BYTES);
assert_eq!(tracee.getregs()?.cs, 0x33);
let mut compat = original;
compat[PRSTATUS_CS..PRSTATUS_CS + 8].copy_from_slice(&0x23u64.to_ne_bytes());
set_native_prstatus(tid, &compat)?;
#[cfg(feature = "notifier")]
tracee.1.event().invalidate_regs();
let (reply, length) = raw_prstatus(tid)?;
assert_eq!(length, 68, "actual short kernel PRSTATUS, never a mock");
assert_eq!(u16::from_ne_bytes(reply[52..54].try_into().unwrap()), 0x23);
assert!(reply[length..].iter().all(|byte| *byte == 0xa5));
let result = tracee.getregs();
set_native_prstatus(tid, &original)?;
let (restored, length) = raw_prstatus(tid)?;
assert_eq!(length, PRSTATUS_BYTES);
assert_eq!(restored, original);
assert!(
matches!(result, Err(Error::Errno(Errno::EPROTO))),
"{result:?}"
);
assert_eq!(tracee.getregs()?.cs, 0x33);
assert_eq!(
tracee.resume(None)?.wait()?,
Wait::Exited(pid, ExitStatus::Exited(42))
);
Ok(())
}
#[test]
#[cfg(not(sanitized))]
fn serialized_threads() -> Result<(), Box<dyn std::error::Error + 'static>> {
const THREAD_COUNT: usize = 8;
let (pid, tracee) = trace(
move || {
extern "C" fn exit_at_once(_: *mut libc::c_void) -> *mut libc::c_void {
std::ptr::null_mut()
}
let threads = (0..THREAD_COUNT)
.map(|_| spawn_tracee_thread(exit_at_once, std::ptr::null_mut()))
.collect::<Vec<_>>();
for t in threads {
assert_eq!(unsafe { libc::pthread_join(t, std::ptr::null_mut()) }, 0);
}
42
},
Options::PTRACE_O_EXITKILL
| Options::PTRACE_O_TRACEEXIT
| ptrace::Options::PTRACE_O_TRACECLONE,
)?;
let mut parent = tracee.resume(None)?;
for _ in 0..THREAD_COUNT {
let (stopped, event) = parent.wait()?.assume_stopped();
let child = match event {
Event::NewChild(ChildOp::Clone, child) => child,
e => panic!("Expected clone event, got {:?}", e),
};
let (child, event) = child.wait()?.assume_stopped();
assert_eq!(event, Event::Stop);
let child = child.resume(None)?;
let (child, event) = child.wait()?.assume_stopped();
assert_eq!(event, Event::Exit);
let (_child_pid, exit_status) = child.resume(None)?.wait()?.assume_exited();
assert_eq!(exit_status, ExitStatus::Exited(0));
parent = stopped.resume(None)?;
}
let (parent, event) = parent.wait()?.assume_stopped();
assert_eq!(event, Event::Exit);
let parent = parent.resume(None)?;
assert_eq!(parent.wait()?, Wait::Exited(pid, ExitStatus::Exited(42)));
Ok(())
}
#[cfg(not(sanitized))]
fn spawn_tracee_thread(
start: extern "C" fn(*mut libc::c_void) -> *mut libc::c_void,
arg: *mut libc::c_void,
) -> libc::pthread_t {
let mut thread = std::mem::MaybeUninit::<libc::pthread_t>::uninit();
let rc = unsafe { libc::pthread_create(thread.as_mut_ptr(), std::ptr::null(), start, arg) };
assert_eq!(rc, 0, "pthread_create in a tracee failed");
unsafe { thread.assume_init() }
}
#[cfg(not(sanitized))]
fn group_exit(thread_count: usize) -> Result<(), Box<dyn std::error::Error + 'static>> {
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::time::Duration;
let (parent_pid, tracee) = trace(
move || {
let counter = Arc::new(AtomicUsize::new(0));
extern "C" fn count_and_sleep(counter: *mut libc::c_void) -> *mut libc::c_void {
let counter = unsafe { &*(counter as *const AtomicUsize) };
counter.fetch_add(1, Ordering::Relaxed);
unsafe { libc::sleep(Duration::from_mins(1).as_secs() as libc::c_uint) };
std::ptr::null_mut()
}
let _threads = (0..thread_count)
.map(|_i| {
let counter = Arc::into_raw(counter.clone()) as *mut libc::c_void;
spawn_tracee_thread(count_and_sleep, counter)
})
.collect::<Vec<_>>();
while counter.load(Ordering::Relaxed) != thread_count {
thread::yield_now();
}
let _ = unsafe { libc::syscall(libc::SYS_exit_group, 42) };
unreachable!()
},
Options::PTRACE_O_EXITKILL
| Options::PTRACE_O_TRACEEXIT
| ptrace::Options::PTRACE_O_TRACECLONE,
)?;
tracee.resume(None)?;
let mut exited = Vec::new();
while let Some(wait) = wait_group(parent_pid)? {
match wait {
Wait::Stopped(tracee, _event) => {
tracee.resume(None)?;
}
Wait::Exited(pid, exit_status) => {
exited.push((pid, exit_status));
}
}
}
assert_eq!(exited.pop(), Some((parent_pid, ExitStatus::Exited(42))));
assert_eq!(exited.len(), thread_count);
for (_pid, exit_status) in exited {
assert_eq!(exit_status, ExitStatus::Exited(42));
}
Ok(())
}
#[test]
#[cfg(not(sanitized))]
fn group_exit_stress() {
for i in 0..100 {
group_exit(i / 2).unwrap();
}
}
#[test]
fn trace_from_another_thread() -> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(|| 42, Options::empty()).unwrap();
let error = thread::spawn(move || tracee.resume(None))
.join()
.unwrap()
.unwrap_err();
assert!(matches!(error, Error::Died(zombie) if zombie.pid() == pid));
assert_eq!(
Stopped::new_unchecked(pid).resume(None)?.wait()?,
Wait::Exited(pid, ExitStatus::Exited(42))
);
Ok(())
}
#[test]
fn trace_killed_by_signal() -> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(
|| {
signal::raise(Signal::SIGILL).unwrap();
unreachable!()
},
Options::PTRACE_O_EXITKILL,
)?;
let running = tracee.resume(None)?;
let (stopped, event) = running.wait()?.assume_stopped();
assert_eq!(event, Event::Signal(Signal::SIGILL));
assert_eq!(
stopped.resume(Some(Signal::SIGILL))?.wait()?,
Wait::Exited(pid, ExitStatus::Signaled(Signal::SIGILL, true))
);
Ok(())
}
#[cfg(feature = "notifier")]
#[cfg(not(sanitized))]
#[tokio::test]
async fn notifier_basic() -> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(|| 42, Options::empty())?;
assert_eq!(
tracee.resume(None)?.next_state().await?,
Wait::Exited(pid, ExitStatus::Exited(42))
);
Ok(())
}
#[cfg(feature = "notifier")]
#[cfg(not(sanitized))]
#[tokio::test]
async fn notifier_generation_preserves_exit_and_signal_status()
-> Result<(), Box<dyn std::error::Error>> {
let (pid, tracee) = trace(|| 42, Options::empty())?;
let token = tracee.1.clone();
let stale_running = Running::from_token(pid, token.clone());
let stale_zombie = Zombie::from_token(pid, token);
let expected = ExitStatus::Exited(42);
assert_eq!(
tracee.resume(None)?.next_state().await?,
Wait::Exited(pid, expected)
);
assert_eq!(
stale_running.next_state().await?,
Wait::Exited(pid, expected),
"old Running rebound after terminal registry removal"
);
assert_eq!(stale_zombie.reap().await?, expected);
assert_eq!(
Running::new(pid).next_state().await,
Err(Error::Errno(Errno::ECHILD)),
"fresh generation inherited stale terminal status"
);
let (pid, tracee) = trace(
|| {
signal::raise(Signal::SIGILL).unwrap();
unreachable!()
},
Options::PTRACE_O_EXITKILL,
)?;
let token = tracee.1.clone();
let stale_running = Running::from_token(pid, token.clone());
let stale_zombie = Zombie::from_token(pid, token);
let expected = ExitStatus::Signaled(Signal::SIGILL, true);
let (stopped, event) = tracee.resume(None)?.next_state().await?.assume_stopped();
assert_eq!(event, Event::Signal(Signal::SIGILL));
assert_eq!(
stopped.resume(Some(Signal::SIGILL))?.next_state().await?,
Wait::Exited(pid, expected)
);
assert_eq!(
stale_running.next_state().await?,
Wait::Exited(pid, expected),
"old Running lost the terminating signal"
);
assert_eq!(stale_zombie.reap().await?, expected);
assert_eq!(
Running::new(pid).next_state().await,
Err(Error::Errno(Errno::ECHILD))
);
Ok(())
}
#[cfg(feature = "notifier")]
#[cfg(not(sanitized))]
#[tokio::test(flavor = "current_thread")]
async fn owned_wait_clone_parent_decode_refusal_retains_same_owner()
-> Result<(), Box<dyn std::error::Error>> {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(3);
for injected in [Errno::EMFILE, Errno::EIO] {
let (pid, tracee) = trace(
|| {
let flags = libc::CLONE_PARENT | libc::SIGCHLD;
let result = unsafe {
libc::syscall(libc::SYS_clone, flags, 0usize, 0usize, 0usize, 0usize)
};
if result == 0 {
unsafe { libc::_exit(0) };
}
i32::from(result == -1)
},
Options::PTRACE_O_EXITKILL | Options::PTRACE_O_TRACEFORK,
)?;
let running = tracee.resume_retaining(None).map_err(|(_, error)| error)?;
let cleanup = running.terminal_cleanup();
let mut owner = running.wait_owned();
notifier::inject_capture_error_for_current_thread(injected);
assert_eq!(
tokio::time::timeout(
deadline.saturating_duration_since(std::time::Instant::now()),
&mut owner
)
.await?,
Err(OwnedWaitError::Errno(injected)),
"first CLONE_PARENT decode did not surface the injected capture error"
);
let (parent, event) = tokio::time::timeout(
deadline.saturating_duration_since(std::time::Instant::now()),
&mut owner,
)
.await??
.assume_stopped();
assert_eq!(parent.pid(), pid);
assert!(parent.terminal_cleanup().same_generation(&cleanup));
assert!(matches!((&mut owner).await, Err(OwnedWaitError::Completed)));
let child = match event {
Event::NewChild(ChildOp::Fork, child) => child,
event => panic!("retry lost the CLONE_PARENT event: {event:?}"),
};
let (child, event) = tokio::time::timeout(
deadline.saturating_duration_since(std::time::Instant::now()),
child.wait_owned(),
)
.await??
.assume_stopped();
assert!(matches!(
event,
Event::Stop | Event::Signal(Signal::SIGSTOP)
));
assert_eq!(
tokio::time::timeout(
deadline.saturating_duration_since(std::time::Instant::now()),
child.resume(None)?.wait_owned()
)
.await??
.assume_exited()
.1,
ExitStatus::Exited(0)
);
assert_eq!(
tokio::time::timeout(
deadline.saturating_duration_since(std::time::Instant::now()),
parent.resume(None)?.wait_owned()
)
.await??
.assume_exited()
.1,
ExitStatus::Exited(0)
);
}
Ok(())
}
#[cfg(feature = "notifier")]
#[cfg(not(sanitized))]
#[tokio::test(flavor = "current_thread")]
async fn notifier_clone_parent_decode_error_preserves_fifo_front()
-> Result<(), Box<dyn std::error::Error>> {
for injected in [Errno::EMFILE, Errno::EIO] {
let (pid, tracee) = trace(
|| {
let flags = libc::CLONE_PARENT | libc::SIGCHLD;
let result = unsafe {
libc::syscall(libc::SYS_clone, flags, 0usize, 0usize, 0usize, 0usize)
};
if result == 0 {
unsafe { libc::_exit(0) };
}
i32::from(result == -1)
},
Options::PTRACE_O_EXITKILL | Options::PTRACE_O_TRACEFORK,
)?;
let token = tracee.1.clone();
let running = tracee.resume(None)?;
let retry = Running::from_token(pid, token);
let _cleanup = running.terminal_cleanup();
notifier::inject_capture_error_for_current_thread(injected);
assert_eq!(
running.next_state().await,
Err(Error::Errno(injected)),
"first CLONE_PARENT decode did not surface the injected capture error"
);
let (parent, event) = retry.next_state().await?.assume_stopped();
let child = match event {
Event::NewChild(ChildOp::Fork, child) => child,
event => panic!("retry lost the CLONE_PARENT event: {event:?}"),
};
let (child, event) = child.next_state().await?.assume_stopped();
assert!(matches!(
event,
Event::Stop | Event::Signal(Signal::SIGSTOP)
));
assert_eq!(
child.resume(None)?.next_state().await?.assume_exited().1,
ExitStatus::Exited(0)
);
assert_eq!(
parent.resume(None)?.next_state().await?.assume_exited().1,
ExitStatus::Exited(0)
);
}
Ok(())
}
#[cfg(feature = "notifier")]
#[cfg(not(sanitized))]
#[test]
fn synchronous_clone_parent_decode_error_preserves_fifo_front()
-> Result<(), Box<dyn std::error::Error>> {
for injected in [Errno::EMFILE, Errno::EIO] {
let (pid, tracee) = trace(
|| {
let flags = libc::CLONE_PARENT | libc::SIGCHLD;
let result = unsafe {
libc::syscall(libc::SYS_clone, flags, 0usize, 0usize, 0usize, 0usize)
};
if result == 0 {
unsafe { libc::_exit(0) };
}
i32::from(result == -1)
},
Options::PTRACE_O_EXITKILL | Options::PTRACE_O_TRACEFORK,
)?;
let token = tracee.1.clone();
let running = tracee.resume(None)?;
let retry = Running::from_token(pid, token);
notifier::inject_sync_decode_capture_error(pid, injected);
assert_eq!(
running.wait(),
Err(Error::Errno(injected)),
"first synchronous CLONE_PARENT decode did not surface the injected error"
);
let (parent, event) = retry.wait()?.assume_stopped();
let child = match event {
Event::NewChild(ChildOp::Fork, child) => child,
event => panic!("synchronous retry lost the CLONE_PARENT event: {event:?}"),
};
let (child, event) = child.wait()?.assume_stopped();
assert!(matches!(
event,
Event::Stop | Event::Signal(Signal::SIGSTOP)
));
assert_eq!(
child.resume(None)?.wait()?.assume_exited().1,
ExitStatus::Exited(0)
);
assert_eq!(
parent.resume(None)?.wait()?.assume_exited().1,
ExitStatus::Exited(0)
);
}
Ok(())
}
#[cfg(feature = "notifier")]
#[cfg(not(sanitized))]
#[tokio::test]
async fn late_waits_after_terminal_reap_return_echild() -> Result<(), Box<dyn std::error::Error>>
{
let (pid, tracee) = trace(|| 42, Options::empty())?;
assert_eq!(
tracee.resume(None)?.next_state().await?,
Wait::Exited(pid, ExitStatus::Exited(42))
);
assert_eq!(
tokio::time::timeout(
std::time::Duration::from_secs(1),
Running::new(pid).next_state(),
)
.await
.expect("late next_state hung after terminal reap"),
Err(Error::Errno(Errno::ECHILD))
);
assert_eq!(
tokio::time::timeout(
std::time::Duration::from_secs(1),
Running::new(pid).exit_event(),
)
.await
.expect("late exit_event hung after terminal reap"),
Err(Error::Errno(Errno::ECHILD))
);
Ok(())
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct KernelSigset(u64);
impl From<&[Signal]> for KernelSigset {
fn from(signals: &[Signal]) -> Self {
let mut set: u64 = 0;
for &sig in signals {
set |= 1u64 << (sig as usize - 1);
}
KernelSigset(set)
}
}
#[unsafe(no_mangle)]
extern "C" fn sigalrm_handler(
_sig: i32,
_siginfo: *mut libc::siginfo_t,
_ucontext: *const libc::c_void,
) {
nix::unistd::write(unsafe { BorrowedFd::borrow_raw(2) }, b"caught SIGALRM!").unwrap();
}
#[allow(dead_code)]
unsafe fn install_sigalrm_handler() -> i32 {
unsafe {
let mut sa: libc::sigaction = MaybeUninit::zeroed().assume_init();
sa.sa_flags = libc::SA_RESTART | libc::SA_SIGINFO | libc::SA_NODEFER;
sa.sa_sigaction = sigalrm_handler as *const () as _;
libc::sigaction(libc::SIGALRM, &sa as *const _, std::ptr::null_mut())
}
}
#[allow(dead_code)]
unsafe fn unblock_signals(signals: &[Signal]) -> io::Result<KernelSigset> {
unsafe {
let set = KernelSigset::from(signals);
let mut oldset = MaybeUninit::<u64>::uninit();
if libc::syscall(
libc::SYS_rt_sigprocmask,
libc::SIG_UNBLOCK,
&set as *const _,
oldset.as_mut_ptr(),
8,
) != 0
{
Err(io::Error::last_os_error())
} else {
Ok(KernelSigset(oldset.assume_init()))
}
}
}
#[allow(dead_code)]
unsafe fn block_signals(signals: &[Signal]) -> io::Result<KernelSigset> {
unsafe {
let set = KernelSigset::from(signals);
let mut oldset = MaybeUninit::<u64>::uninit();
if libc::syscall(
libc::SYS_rt_sigprocmask,
libc::SIG_BLOCK,
&set as *const _,
oldset.as_mut_ptr(),
8,
) != 0
{
Err(io::Error::last_os_error())
} else {
Ok(KernelSigset(oldset.assume_init()))
}
}
}
#[cfg(not(sanitized))]
fn own_sigmask() -> u64 {
let mut mask = 0u64;
let ret = unsafe {
libc::syscall(
libc::SYS_rt_sigprocmask,
libc::SIG_BLOCK,
std::ptr::null::<u64>(),
&mut mask as *mut u64,
8,
)
};
assert_eq!(ret, 0);
mask
}
#[cfg(not(sanitized))]
fn stop_self_with_raw_tgkill() {
let ret = unsafe {
let pid = libc::syscall(libc::SYS_getpid);
let tid = libc::syscall(libc::SYS_gettid);
libc::syscall(libc::SYS_tgkill, pid, tid, libc::SIGSTOP)
};
assert_eq!(ret, 0);
}
#[cfg(not(sanitized))]
fn expect_signal_stop(running: Running, signal: Signal) -> Stopped {
match running.wait().unwrap() {
Wait::Stopped(stopped, Event::Signal(got)) if got == signal => stopped,
other => panic!("expected a {signal:?} delivery stop, got {other:?}"),
}
}
#[cfg(not(sanitized))]
#[test]
fn getsigmask_and_setsigmask_read_and_replace_the_tracee_mask()
-> Result<(), Box<dyn std::error::Error + 'static>> {
let replacement = KernelSigset::from(&[Signal::SIGUSR1, Signal::SIGALRM][..]).0;
let unblockable = KernelSigset::from(&[Signal::SIGKILL, Signal::SIGSTOP][..]).0;
let (pid, tracee) = trace(
move || {
unsafe { block_signals(&[Signal::SIGUSR1, Signal::SIGUSR2]) }.unwrap();
stop_self_with_raw_tgkill();
if own_sigmask() == replacement { 0 } else { 1 }
},
Options::PTRACE_O_EXITKILL,
)?;
let stopped = expect_signal_stop(tracee.resume(None)?, Signal::SIGSTOP);
let blocked = KernelSigset::from(&[Signal::SIGUSR1, Signal::SIGUSR2][..]).0;
let mask = stopped.getsigmask()?;
assert_eq!(mask & blocked, blocked, "mask {mask:#x}");
stopped.setsigmask(replacement | unblockable)?;
assert_eq!(stopped.getsigmask()?, replacement);
assert_eq!(
stopped.resume(None)?.wait()?,
Wait::Exited(pid, ExitStatus::Exited(0))
);
Ok(())
}
#[cfg(not(sanitized))]
static SIGUSR1_HANDLED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
#[cfg(not(sanitized))]
extern "C" fn note_sigusr1(_signal: libc::c_int) {
SIGUSR1_HANDLED.store(true, std::sync::atomic::Ordering::SeqCst);
}
#[cfg(not(sanitized))]
#[test]
fn setsigmask_holds_a_signal_pending_until_the_mask_is_restored()
-> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(
|| {
let mut action: libc::sigaction = unsafe { MaybeUninit::zeroed().assume_init() };
action.sa_sigaction = note_sigusr1 as *const () as usize;
assert_eq!(
unsafe { libc::sigaction(libc::SIGUSR1, &action, std::ptr::null_mut()) },
0
);
unsafe { unblock_signals(&[Signal::SIGUSR1]) }.unwrap();
stop_self_with_raw_tgkill();
let mut pending = 0u64;
unsafe { libc::syscall(libc::SYS_rt_sigpending, &mut pending as *mut u64, 8) };
let usr1 = KernelSigset::from(&[Signal::SIGUSR1][..]).0;
if pending & usr1 == 0 {
return 2;
}
if SIGUSR1_HANDLED.load(std::sync::atomic::Ordering::SeqCst) {
return 3;
}
stop_self_with_raw_tgkill();
if SIGUSR1_HANDLED.load(std::sync::atomic::Ordering::SeqCst) {
0
} else {
4
}
},
Options::PTRACE_O_EXITKILL,
)?;
let stopped = expect_signal_stop(tracee.resume(None)?, Signal::SIGSTOP);
let saved = stopped.getsigmask()?;
stopped.setsigmask(!0)?;
signal::kill(pid.into(), Signal::SIGUSR1)?;
let stopped = expect_signal_stop(stopped.resume(None)?, Signal::SIGSTOP);
stopped.setsigmask(saved)?;
assert_eq!(stopped.getsigmask()?, saved);
let stopped = expect_signal_stop(stopped.resume(None)?, Signal::SIGUSR1);
assert_eq!(
stopped.resume(Some(Signal::SIGUSR1))?.wait()?,
Wait::Exited(pid, ExitStatus::Exited(0))
);
Ok(())
}
#[cfg(target_arch = "x86_64")]
#[test]
fn debug_registers_round_trip_through_peekuser_and_pokeuser()
-> Result<(), Box<dyn std::error::Error + 'static>> {
static WATCHED: u64 = 0;
let debugreg = |n: usize| core::mem::offset_of!(libc::user, u_debugreg) + 8 * n;
let (parent_pid, tracee) = trace(
|| {
let _ = unsafe { libc::syscall(libc::SYS_exit_group, 0) };
unreachable!()
},
Options::PTRACE_O_EXITKILL,
)?;
let addr = &WATCHED as *const u64 as u64;
tracee.pokeuser(debugreg(0), addr)?;
assert_eq!(tracee.peekuser(debugreg(0))?, addr);
tracee.pokeuser(debugreg(7), 0x0009_0001)?;
assert_eq!(tracee.peekuser(debugreg(7))?, 0x0009_0001);
let dr6 = tracee.peekuser(debugreg(6))?;
assert_eq!(dr6 & 0xf, 0, "{dr6:#x}");
tracee.pokeuser(debugreg(6), dr6 | 0b0100)?;
assert_eq!(tracee.peekuser(debugreg(6))?, dr6 | 0b0100);
tracee.pokeuser(debugreg(6), dr6)?;
tracee.pokeuser(debugreg(7), 0)?;
assert_eq!(
tracee.pokeuser(debugreg(4), 0),
Err(Error::Errno(Errno::EIO))
);
tracee.resume(None)?;
while wait_group(parent_pid)?.is_some() {}
Ok(())
}
#[cfg(not(sanitized))]
#[test]
fn peeksiginfo_returns_pending_siginfo() -> Result<(), Box<dyn std::error::Error + 'static>> {
let (parent_pid, tracee) = trace(
move || {
let _ = unsafe {
block_signals(&[Signal::SIGALRM, Signal::SIGVTALRM, Signal::SIGPROF])
};
assert!(signal::raise(Signal::SIGALRM).is_ok());
assert!(signal::raise(Signal::SIGVTALRM).is_ok());
assert!(signal::raise(Signal::SIGPROF).is_ok());
let _ = unsafe { libc::syscall(libc::SYS_exit_group, 0) };
unreachable!()
},
Options::PTRACE_O_EXITKILL
| Options::PTRACE_O_TRACEEXIT
| ptrace::Options::PTRACE_O_TRACECLONE,
)?;
tracee.resume(None)?;
let mut exited = Vec::new();
while let Some(wait) = wait_group(parent_pid)? {
match wait {
Wait::Stopped(tracee, Event::Exit) => {
let pending: Vec<_> = tracee
.peeksiginfo(None)?
.iter()
.map(|&si| Signal::try_from(si.si_signo).unwrap())
.collect();
assert_eq!(
pending,
[Signal::SIGALRM, Signal::SIGVTALRM, Signal::SIGPROF]
);
let pending: Vec<_> = tracee
.peeksiginfo(None)?
.iter()
.map(|&si| Signal::try_from(si.si_signo).unwrap())
.collect();
assert_eq!(
pending,
[Signal::SIGALRM, Signal::SIGVTALRM, Signal::SIGPROF]
);
tracee.resume(None)?;
}
Wait::Stopped(tracee, _event) => {
tracee.resume(None)?;
}
Wait::Exited(pid, exit_status) => {
exited.push((pid, exit_status));
}
}
}
assert_eq!(exited.pop(), Some((parent_pid, ExitStatus::Exited(0))));
Ok(())
}
#[cfg(not(sanitized))]
#[test]
fn peeksiginfo_all_returns_the_whole_queue() -> Result<(), Box<dyn std::error::Error + 'static>>
{
const QUEUED: usize = 150;
let (parent_pid, tracee) = trace(
move || {
let rt = libc::SIGRTMIN();
unsafe {
let mut set: libc::sigset_t = core::mem::zeroed();
libc::sigemptyset(&mut set);
libc::sigaddset(&mut set, rt);
libc::sigaddset(&mut set, libc::SIGALRM);
assert_eq!(
libc::sigprocmask(libc::SIG_BLOCK, &set, core::ptr::null_mut()),
0
);
for _ in 0..QUEUED {
assert_eq!(
libc::syscall(libc::SYS_tgkill, libc::getpid(), libc::gettid(), rt),
0
);
}
assert_eq!(
libc::syscall(
libc::SYS_tgkill,
libc::getpid(),
libc::gettid(),
libc::SIGALRM
),
0
);
libc::syscall(libc::SYS_exit_group, 0);
}
unreachable!()
},
Options::PTRACE_O_EXITKILL | Options::PTRACE_O_TRACEEXIT,
)?;
tracee.resume(None)?;
let mut peeked = false;
while let Some(wait) = wait_group(parent_pid)? {
match wait {
Wait::Stopped(tracee, Event::Exit) => {
let signos = |v: Vec<libc::siginfo_t>| -> Vec<i32> {
v.iter().map(|si| si.si_signo).collect()
};
let mut expected = vec![libc::SIGRTMIN(); QUEUED];
expected.push(libc::SIGALRM);
assert_eq!(signos(tracee.peeksiginfo(None)?), expected[..64]);
assert_eq!(signos(tracee.peeksiginfo_all(None)?), expected);
peeked = true;
tracee.resume(None)?;
}
Wait::Stopped(tracee, _event) => {
tracee.resume(None)?;
}
Wait::Exited(_, exit_status) => {
assert_eq!(exit_status, ExitStatus::Exited(0));
}
}
}
assert!(peeked, "the tracee never reached its exit stop");
Ok(())
}
#[cfg(not(sanitized))]
#[test]
fn getsiginfo_should_success() -> Result<(), Box<dyn std::error::Error + 'static>> {
let (parent_pid, tracee) = trace(
move || {
let _ = unsafe { unblock_signals(&[Signal::SIGALRM]) };
let _ = unsafe { block_signals(&[Signal::SIGVTALRM, Signal::SIGPROF]) };
assert_eq!(unsafe { install_sigalrm_handler() }, 0);
assert!(signal::raise(Signal::SIGALRM).is_ok());
let _ = unsafe { libc::syscall(libc::SYS_exit_group, 0) };
unreachable!()
},
Options::PTRACE_O_EXITKILL
| Options::PTRACE_O_TRACEEXIT
| ptrace::Options::PTRACE_O_TRACECLONE,
)?;
tracee.resume(None)?;
let mut exited = Vec::new();
while let Some(wait) = wait_group(parent_pid)? {
match wait {
Wait::Stopped(tracee, Event::Signal(Signal::SIGALRM)) => {
let siginfo = tracee.getsiginfo()?;
assert_eq!(siginfo.si_signo, Signal::SIGALRM as i32);
tracee.resume(Signal::SIGALRM)?;
}
Wait::Stopped(tracee, Event::Signal(other_signal)) => {
tracee.resume(other_signal)?;
}
Wait::Stopped(tracee, _event) => {
tracee.resume(None)?;
}
Wait::Exited(pid, exit_status) => {
exited.push((pid, exit_status));
}
}
}
assert_eq!(exited.pop(), Some((parent_pid, ExitStatus::Exited(0))));
Ok(())
}
#[cfg(not(sanitized))]
static RT_HANDLED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
#[cfg(not(sanitized))]
extern "C" fn note_realtime(signal: libc::c_int) {
RT_HANDLED.fetch_or(1 << (signal - 1), std::sync::atomic::Ordering::SeqCst);
}
#[cfg(not(sanitized))]
#[test]
fn realtime_signal_stops_decode_raw_and_resume_raw_delivers_them()
-> Result<(), Box<dyn std::error::Error + 'static>> {
let delivered = [34, 50, 64];
let (pid, tracee) = trace(
move || {
for signal in delivered {
let mut action: libc::sigaction =
unsafe { MaybeUninit::zeroed().assume_init() };
action.sa_sigaction = note_realtime as *const () as usize;
assert_eq!(
unsafe { libc::sigaction(signal, &action, std::ptr::null_mut()) },
0
);
}
for signal in [32, 33, 34, 50, 64] {
let ret = unsafe {
let pid = libc::syscall(libc::SYS_getpid);
let tid = libc::syscall(libc::SYS_gettid);
libc::syscall(libc::SYS_tgkill, pid, tid, signal)
};
assert_eq!(ret, 0);
}
stop_self_with_raw_tgkill();
let want = delivered.iter().fold(0, |bits, s| bits | 1 << (s - 1));
if RT_HANDLED.load(std::sync::atomic::Ordering::SeqCst) == want {
0
} else {
1
}
},
Options::PTRACE_O_EXITKILL,
)?;
let mut running = tracee.resume(None)?;
for signal in [32, 33, 34, 50, 64] {
let stopped = match running.wait()? {
Wait::Stopped(stopped, Event::RawSignal(got)) if got == signal => stopped,
other => panic!("expected a raw {signal} delivery stop, got {other:?}"),
};
let any = AnySignal::new(signal).unwrap();
assert_eq!(Event::RawSignal(signal).signal(), Some(any));
assert_eq!(Event::from_signal(any), Event::RawSignal(signal));
assert_eq!(any.as_nix(), None);
assert_eq!(format!("{any}"), format!("SIG{signal}"));
let deliver = if signal < 34 { 0 } else { signal };
running = stopped.resume_raw(deliver)?;
}
let stopped = expect_signal_stop(running, Signal::SIGSTOP);
assert_eq!(
Event::Signal(Signal::SIGSTOP).signal(),
Some(AnySignal::from(Signal::SIGSTOP))
);
assert_eq!(
stopped.resume(None)?.wait()?,
Wait::Exited(pid, ExitStatus::Exited(0))
);
Ok(())
}
#[cfg(not(sanitized))]
#[test]
fn detach_raw_delivers_a_realtime_signal() -> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(
|| {
RT_HANDLED.store(0, std::sync::atomic::Ordering::SeqCst);
let mut action: libc::sigaction = unsafe { MaybeUninit::zeroed().assume_init() };
action.sa_sigaction = note_realtime as *const () as usize;
assert_eq!(
unsafe { libc::sigaction(40, &action, std::ptr::null_mut()) },
0
);
let ret = unsafe {
let pid = libc::syscall(libc::SYS_getpid);
let tid = libc::syscall(libc::SYS_gettid);
libc::syscall(libc::SYS_tgkill, pid, tid, 40)
};
assert_eq!(ret, 0);
if RT_HANDLED.load(std::sync::atomic::Ordering::SeqCst) == 1 << 39 {
0
} else {
1
}
},
Options::PTRACE_O_EXITKILL,
)?;
let stopped = match tracee.resume(None)?.wait()? {
Wait::Stopped(stopped, Event::RawSignal(40)) => stopped,
other => panic!("expected a raw 40 delivery stop, got {other:?}"),
};
let running = stopped.detach_raw(40)?;
let _ = signal::kill(nix::unistd::Pid::from_raw(pid.as_raw()), Signal::SIGCONT);
assert_eq!(running.wait()?, Wait::Exited(pid, ExitStatus::Exited(0)));
Ok(())
}
#[test]
fn any_signal_names_only_1_to_64() {
assert_eq!(AnySignal::new(0), None);
assert_eq!(AnySignal::new(65), None);
assert_eq!(AnySignal::new(-1), None);
assert_eq!(AnySignal::new(31).unwrap().as_nix(), Some(Signal::SIGSYS));
assert_eq!(AnySignal::new(32).unwrap().as_nix(), None);
assert_eq!(AnySignal::new(64).unwrap().raw(), 64);
assert_eq!(AnySignal::from(Signal::SIGUSR1), Signal::SIGUSR1);
assert_eq!(Event::Seccomp.signal(), None);
assert_eq!(
Event::from_signal(Signal::SIGUSR1.into()),
Event::Signal(Signal::SIGUSR1)
);
}
#[cfg(not(sanitized))]
#[test]
fn wait_group_decodes_a_realtime_stop() -> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(
|| {
let mut action: libc::sigaction = unsafe { MaybeUninit::zeroed().assume_init() };
action.sa_sigaction = libc::SIG_IGN;
assert_eq!(
unsafe { libc::sigaction(40, &action, std::ptr::null_mut()) },
0
);
let ret = unsafe {
let pid = libc::syscall(libc::SYS_getpid);
let tid = libc::syscall(libc::SYS_gettid);
libc::syscall(libc::SYS_tgkill, pid, tid, 40)
};
if ret == 0 { 0 } else { 1 }
},
Options::PTRACE_O_EXITKILL,
)?;
let _running = tracee.resume(None)?;
let stopped = match wait_group(pid)? {
Some(Wait::Stopped(stopped, Event::RawSignal(40))) => stopped,
other => panic!("expected a raw 40 delivery stop, got {other:?}"),
};
let _running = stopped.resume_raw(40)?;
assert_eq!(
wait_group(pid)?,
Some(Wait::Exited(pid, ExitStatus::Exited(0)))
);
Ok(())
}
#[cfg(all(feature = "notifier", not(sanitized)))]
static FIRST_SIGNAL_DELIVERIES: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);
#[cfg(all(feature = "notifier", not(sanitized)))]
extern "C" fn count_first_signal(_signal: libc::c_int) {
FIRST_SIGNAL_DELIVERIES.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}
#[cfg(all(feature = "notifier", not(sanitized)))]
extern "C" fn ignore_target_signal(_signal: libc::c_int) {}
#[cfg(all(feature = "notifier", not(sanitized)))]
#[test]
fn wait_for_signal_forwards_an_intervening_signal_named_or_realtime()
-> Result<(), Box<dyn std::error::Error + 'static>> {
for first in [libc::SIGUSR2, 40] {
let (pid, tracee) = trace(
move || {
for (signal, handler) in [
(first, count_first_signal as *const () as usize),
(libc::SIGUSR1, ignore_target_signal as *const () as usize),
] {
let mut action: libc::sigaction =
unsafe { MaybeUninit::zeroed().assume_init() };
action.sa_sigaction = handler;
assert_eq!(
unsafe { libc::sigaction(signal, &action, std::ptr::null_mut()) },
0
);
}
for signal in [first, libc::SIGUSR1] {
let ret = unsafe {
let pid = libc::syscall(libc::SYS_getpid);
let tid = libc::syscall(libc::SYS_gettid);
libc::syscall(libc::SYS_tgkill, pid, tid, signal)
};
assert_eq!(ret, 0);
}
let calls = FIRST_SIGNAL_DELIVERIES.load(std::sync::atomic::Ordering::SeqCst);
if calls == 1 { 0 } else { 42 }
},
Options::PTRACE_O_EXITKILL,
)?;
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let status = runtime.block_on(async {
let selected = tracee
.resume(None)?
.wait_for_signal(Signal::SIGUSR1)
.await?;
let stopped = match selected {
Wait::Stopped(stopped, Event::Signal(Signal::SIGUSR1)) => stopped,
other => panic!("expected the SIGUSR1 stop, got {other:?}"),
};
stopped.resume(Some(Signal::SIGUSR1))?.next_state().await
})?;
assert_eq!(
status,
Wait::Exited(pid, ExitStatus::Exited(0)),
"first signal {first}"
);
}
Ok(())
}
#[cfg(not(sanitized))]
#[test]
fn a_tracee_killed_by_a_realtime_signal_exits_signaled_raw()
-> Result<(), Box<dyn std::error::Error + 'static>> {
let (pid, tracee) = trace(
|| {
unsafe { libc::raise(40) };
0
},
Options::PTRACE_O_EXITKILL,
)?;
let stopped = match tracee.resume(None)?.wait()? {
Wait::Stopped(stopped, Event::RawSignal(40)) => stopped,
other => panic!("expected a raw 40 delivery stop, got {other:?}"),
};
assert_eq!(
stopped.resume_raw(40)?.wait()?,
Wait::Exited(pid, ExitStatus::SignaledRaw(40, false))
);
Ok(())
}
}