use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use detcore_model::bundled_signal_facts;
use procfs::process::Process;
use rand::Rng;
use reverie::BackendCapabilities;
use reverie::Error;
use reverie::Guest;
use reverie::Pid;
use reverie::Stack;
use reverie::syscalls;
use reverie::syscalls::Addr;
use reverie::syscalls::AddrMut;
use reverie::syscalls::CloneFlags;
use reverie::syscalls::Errno;
use reverie::syscalls::MemoryAccess;
use reverie::syscalls::ReadAddr;
use reverie::syscalls::Syscall;
use reverie::syscalls::SyscallInfo;
use reverie::syscalls::Timespec;
use reverie::syscalls::WaitPidFlag;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::trace;
use crate::SharedMemoryObjectId;
use crate::config::BlockingMode;
use crate::config::Config;
use crate::config::SchedHeuristic;
use crate::memory::MemoryMetadata;
use crate::record_or_replay::RecordOrReplay;
use crate::resources::ExternalOpId;
use crate::resources::Permission;
use crate::resources::ResourceID;
use crate::resources::Resources;
use crate::scheduler::FutexSignalWatch;
use crate::scheduler::HostTimedSignalScope;
use crate::scheduler::SchedValue;
use crate::scheduler::pid_namespaces::GuestPidAnswer;
use crate::syscalls::helpers::NonblockableSyscall;
use crate::syscalls::helpers::RestartCall;
use crate::syscalls::helpers::keep_restart_block;
use crate::syscalls::helpers::record_retry_event;
use crate::syscalls::helpers::retry_nonblocking_syscall;
use crate::syscalls::helpers::retry_nonblocking_syscall_with_timeout;
use crate::syscalls::robust_list;
use crate::tool_global::FutexAction;
use crate::tool_global::ResumeStatus;
use crate::tool_global::await_exact_child_physical_exit;
use crate::tool_global::cancel_exec;
use crate::tool_global::child_tid_clear_address;
use crate::tool_global::consume_child_wait;
use crate::tool_global::create_child_thread;
use crate::tool_global::futex_action;
use crate::tool_global::hold_turn_for_group_exit;
use crate::tool_global::host_timed_signals;
use crate::tool_global::prepare_exec;
use crate::tool_global::process_group;
use crate::tool_global::ready_child_wait;
use crate::tool_global::record_host_timed_signals;
use crate::tool_global::resource_request;
use crate::tool_global::set_child_tid_address;
use crate::tool_global::thread_is_live;
use crate::tool_global::thread_observe_time;
use crate::tool_global::wait_for_child_lifecycle;
use crate::tool_global::yield_once;
use crate::tool_local::Detcore;
use crate::tool_local::PendingVfork;
use crate::tool_local::RobustListExit;
use crate::tool_local::RobustListOwner;
use crate::tool_local::RobustListWake;
use crate::types::ChildWaitExitClass;
use crate::types::ChildWaitSelector;
use crate::types::ChildWaitSpec;
use crate::types::DetPid;
use crate::types::DetTid;
use crate::types::ExactChildWaitState;
use crate::types::FutexID;
use crate::types::LogicalTime;
use crate::types::SigWrapper;
#[path = "emulated_waitid.rs"]
mod emulated_waitid;
pub(crate) const VIRTUAL_CPUSET_BYTES: usize = 16;
const IOPRIO_WHO_PROCESS: libc::c_int = 1;
const IOPRIO_WHO_PGRP: libc::c_int = 2;
const IOPRIO_WHO_USER: libc::c_int = 3;
const IOPRIO_CLASS_SHIFT: libc::c_int = 13;
const IOPRIO_CLASS_BE: libc::c_int = 2;
const IOPRIO_BE_NORM: libc::c_int = 4;
const IOPRIO_DEFAULT_EFFECTIVE: libc::c_int =
(IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | IOPRIO_BE_NORM;
const SCHED_ATTR_SIZE_VER0: u32 = 48; const SCHED_ATTR_SIZE_VER1: u32 = 56; const SCHED_ATTR_KERNEL_SIZE: u32 = SCHED_ATTR_SIZE_VER1;
const SCHED_ATTR_MAX_SIZE: u32 = 4096;
const VIRTUAL_CURRENT_POLICY: u32 = libc::SCHED_OTHER as u32;
#[derive(Debug, Eq, PartialEq)]
enum TailVerdict {
AllZero,
NotZeroed,
Faulted,
}
const MIN_PROTECTION_RESPECTING_READ: usize = std::mem::size_of::<u64>() + 1;
fn scan_tail_is_zeroed<M: MemoryAccess>(
memory: &M,
base: AddrMut<u8>,
tail_off: usize,
tail_len: usize,
) -> TailVerdict {
const CHUNK: usize = 256;
let mut done = 0usize;
let mut buf = [0u8; CHUNK];
while done < tail_len {
let remaining = tail_len - done;
let want = remaining.min(CHUNK);
let back = MIN_PROTECTION_RESPECTING_READ
.saturating_sub(want)
.min(done + tail_off);
let read_len = want + back;
let start = tail_off + done - back;
let Some(addr) = base
.as_raw()
.checked_add(start)
.and_then(AddrMut::<u8>::from_raw)
else {
return TailVerdict::Faulted;
};
let got = memory.read(addr, &mut buf[..read_len]).unwrap_or(0);
let new_from = back.min(got);
if buf[new_from..got].iter().any(|byte| *byte != 0) {
return TailVerdict::NotZeroed;
}
if got < read_len {
return TailVerdict::Faulted;
}
done += want;
}
TailVerdict::AllZero
}
const SCHED_FIFO: u32 = 1;
const SCHED_RR: u32 = 2;
const SCHED_DEADLINE: u32 = 6;
const SCHED_EXT: u32 = 7;
const MAX_RT_PRIO: u32 = 100;
const SCHED_ATTR_OFF_POLICY: usize = 4;
const SCHED_ATTR_OFF_FLAGS: usize = 8;
const SCHED_ATTR_OFF_PRIORITY: usize = 20;
const SCHED_ATTR_OFF_RUNTIME: usize = 24;
const SCHED_ATTR_OFF_DEADLINE: usize = 32;
const SCHED_ATTR_OFF_PERIOD: usize = 40;
const SCHED_FLAG_RESET_ON_FORK: u64 = 0x01;
const SCHED_FLAG_RECLAIM: u64 = 0x02;
const SCHED_FLAG_DL_OVERRUN: u64 = 0x04;
const SCHED_FLAG_KEEP_POLICY: u64 = 0x08;
const SCHED_FLAG_KEEP_PARAMS: u64 = 0x10;
const SCHED_FLAG_UTIL_CLAMP_MIN: u64 = 0x20;
const SCHED_FLAG_UTIL_CLAMP_MAX: u64 = 0x40;
const SCHED_FLAG_UTIL_CLAMP: u64 = SCHED_FLAG_UTIL_CLAMP_MIN | SCHED_FLAG_UTIL_CLAMP_MAX;
const SCHED_FLAG_ALL: u64 = SCHED_FLAG_RESET_ON_FORK
| SCHED_FLAG_RECLAIM
| SCHED_FLAG_DL_OVERRUN
| SCHED_FLAG_KEEP_POLICY
| SCHED_FLAG_KEEP_PARAMS
| SCHED_FLAG_UTIL_CLAMP;
fn is_valid_sched_policy(policy: u32) -> bool {
matches!(
policy,
0 | 1 | 2 | 3
| 5
| SCHED_DEADLINE
| SCHED_EXT
)
}
fn is_rt_policy(policy: u32) -> bool {
policy == SCHED_FIFO || policy == SCHED_RR
}
fn deadline_params_are_valid(runtime: u64, deadline: u64, period: u64) -> bool {
if deadline == 0 {
return false;
}
if runtime < (1u64 << 10) {
return false;
}
if deadline & (1u64 << 63) != 0 || period & (1u64 << 63) != 0 {
return false;
}
let period = if period == 0 { deadline } else { period };
runtime <= deadline && deadline <= period
}
fn sched_attr_effective_size(declared: u32) -> Result<u32, ()> {
let size = if declared == 0 {
SCHED_ATTR_SIZE_VER0
} else {
declared
};
if !(SCHED_ATTR_SIZE_VER0..=SCHED_ATTR_MAX_SIZE).contains(&size) {
return Err(());
}
Ok(size)
}
#[derive(Clone, Copy, Debug)]
struct SchedAttrFields {
policy: u32,
sched_flags: u64,
priority: u32,
runtime: u64,
deadline: u64,
period: u64,
}
fn validate_sched_attr_before_lookup(size: u32, attr: &SchedAttrFields) -> Result<(), Errno> {
if attr.sched_flags & SCHED_FLAG_UTIL_CLAMP != 0 && size < SCHED_ATTR_SIZE_VER1 {
return Err(Errno::EINVAL);
}
if (attr.policy as i32) < 0 {
return Err(Errno::EINVAL);
}
Ok(())
}
fn validate_sched_attr_after_lookup(attr: &SchedAttrFields) -> Result<(), Errno> {
let policy = if attr.sched_flags & SCHED_FLAG_KEEP_POLICY != 0 {
Some(VIRTUAL_CURRENT_POLICY)
} else {
if !is_valid_sched_policy(attr.policy) {
return Err(Errno::EINVAL);
}
Some(attr.policy)
};
if attr.sched_flags & !SCHED_FLAG_ALL != 0 {
return Err(Errno::EINVAL);
}
if attr.priority > MAX_RT_PRIO - 1 {
return Err(Errno::EINVAL);
}
if let Some(policy) = policy {
if is_rt_policy(policy) != (attr.priority != 0) {
return Err(Errno::EINVAL);
}
if policy == SCHED_DEADLINE
&& !deadline_params_are_valid(attr.runtime, attr.deadline, attr.period)
{
return Err(Errno::EINVAL);
}
}
Ok(())
}
fn virtual_ioprio(which: libc::c_int) -> Result<i64, Errno> {
match which {
IOPRIO_WHO_PROCESS => Ok(0),
IOPRIO_WHO_PGRP | IOPRIO_WHO_USER => Ok(i64::from(IOPRIO_DEFAULT_EFFECTIVE)),
_ => Err(Errno::EINVAL),
}
}
#[repr(C)]
#[derive(Clone, Copy)]
struct WaitidSigchldFields {
pid: libc::pid_t,
uid: libc::uid_t,
status: libc::c_int,
utime: libc::c_long,
stime: libc::c_long,
}
#[repr(C)]
union WaitidSiginfoFields {
_alignment: *mut libc::c_void,
sigchld: WaitidSigchldFields,
}
#[repr(C)]
struct WaitidSiginfoHead {
_base: [libc::c_int; 3],
fields: WaitidSiginfoFields,
}
fn wait_status_is_termination(status: libc::c_int) -> bool {
libc::WIFEXITED(status) || libc::WIFSIGNALED(status)
}
fn waitid_code_is_termination(code: libc::c_int) -> bool {
matches!(code, libc::CLD_EXITED | libc::CLD_KILLED | libc::CLD_DUMPED)
}
fn canonicalize_waitid_siginfo(info: &mut libc::siginfo_t) {
debug_assert!(
std::mem::size_of::<WaitidSiginfoHead>() <= std::mem::size_of::<libc::siginfo_t>()
);
let sigchld = unsafe {
&mut (*(info as *mut libc::siginfo_t).cast::<WaitidSiginfoHead>())
.fields
.sigchld
};
sigchld.utime = 0;
sigchld.stime = 0;
}
fn finish_waitid_result<T, G>(
guest: &mut G,
call: syscalls::Waitid,
value: i64,
mut info_value: libc::siginfo_t,
child: DetPid,
) -> Result<i64, Error>
where
T: RecordOrReplay,
G: Guest<Detcore<T>>,
{
let child_pid = unsafe { info_value.si_pid() };
if child_pid != 0 {
canonicalize_waitid_siginfo(&mut info_value);
guest.memory().write_value(
call.info().expect("waitid infop checked before execution"),
&info_value,
)?;
if call.options() & libc::WNOWAIT == 0 && waitid_code_is_termination(info_value.si_code) {
guest.thread_state_mut().reap_child_process_cpu_time(child);
}
if let Some(rusage) = call.rusage() {
let usage: libc::rusage = unsafe { std::mem::zeroed() };
guest.memory().write_value(rusage, &usage)?;
}
}
Ok(value)
}
#[derive(Debug, Eq, PartialEq)]
enum ExactWaitPollDecision {
ChildReady,
AwaitPhysicalExit,
ReapAfterLogicalExit,
Interrupted,
Retry,
}
fn exact_wait_poll_decision(
child_ready: bool,
signaled: bool,
lifecycle: Option<ExactChildWaitState>,
) -> ExactWaitPollDecision {
if child_ready {
ExactWaitPollDecision::ChildReady
} else if lifecycle == Some(ExactChildWaitState::PhysicalExitPending) {
ExactWaitPollDecision::AwaitPhysicalExit
} else if matches!(
lifecycle,
Some(ExactChildWaitState::LogicallyExited | ExactChildWaitState::PhysicallyExited)
) {
ExactWaitPollDecision::ReapAfterLogicalExit
} else if signaled {
ExactWaitPollDecision::Interrupted
} else {
ExactWaitPollDecision::Retry
}
}
fn stale_any_wait_must_interrupt(signaled: bool, next_ready: Option<DetPid>) -> bool {
signaled && next_ready.is_none()
}
fn terminal_child_wait_spec(
selector: ChildWaitSelector,
caller: DetTid,
options: libc::c_int,
) -> ChildWaitSpec {
let exit_class = if options & libc::__WALL != 0 {
ChildWaitExitClass::Any
} else if options & libc::__WCLONE != 0 {
ChildWaitExitClass::Clone
} else {
ChildWaitExitClass::Sigchld
};
ChildWaitSpec {
selector,
owner: (options & libc::__WNOTHREAD != 0).then_some(caller),
exit_class,
}
}
fn validate_wait4_arguments(pid: libc::pid_t, options: WaitPidFlag) -> Result<(), Errno> {
let allowed_options = WaitPidFlag::WNOHANG
| WaitPidFlag::WUNTRACED
| WaitPidFlag::WCONTINUED
| WaitPidFlag::__WNOTHREAD
| WaitPidFlag::__WCLONE
| WaitPidFlag::__WALL;
if options.bits() & !allowed_options.bits() != 0 {
return Err(Errno::EINVAL);
}
if pid == libc::pid_t::MIN {
return Err(Errno::ESRCH);
}
Ok(())
}
pub(super) fn wait4_uses_terminal_selector(
options: WaitPidFlag,
serial_emulated_wait: bool,
) -> bool {
if serial_emulated_wait {
options.bits() & !(libc::WNOHANG | libc::WUNTRACED | libc::__WNOTHREAD) == 0
} else {
!options.intersects(
WaitPidFlag::WUNTRACED
| WaitPidFlag::WCONTINUED
| WaitPidFlag::__WCLONE
| WaitPidFlag::__WALL,
)
}
}
pub(super) async fn await_selected_child_cpu_publication<G, T>(
guest: &mut G,
child: DetPid,
call: &str,
) -> Result<(), Error>
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
{
if guest
.thread_state()
.wait_for_child_cpu_publication(child)
.await
{
Ok(())
} else {
Err(Error::Tool(anyhow::anyhow!(
"emulated {call} selected child has no owned final CPU publication"
)))
}
}
pub(super) async fn retire_selected_child<G, T>(
guest: &mut G,
child: DetPid,
call: &str,
) -> Result<(), Error>
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
{
guest.thread_state_mut().reap_child_process_cpu_time(child);
if consume_child_wait(guest, child).await {
Ok(())
} else {
Err(Error::Tool(anyhow::anyhow!(
"emulated {call} selected child was not logically consumed"
)))
}
}
pub(super) async fn complete_selected_emulated_wait4<G, T>(
guest: &mut G,
call: syscalls::Wait4,
child: DetPid,
) -> Result<i64, Error>
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
{
await_selected_child_cpu_publication(guest, child, "wait4").await?;
let exact = call
.with_pid(child.as_raw())
.with_options(WaitPidFlag::from_bits_retain(
call.options().bits() & (libc::WNOHANG | libc::WUNTRACED),
));
let result = guest.inject(exact).await;
match result {
Ok(pid) if pid == i64::from(child.as_raw()) => {}
Err(Errno::EFAULT) => {}
unexpected => {
return Err(Error::Tool(anyhow::anyhow!(
"emulated wait4 selected-child invariant failed: {unexpected:?} for {child}"
)));
}
}
retire_selected_child(guest, child, "wait4").await?;
result.map_err(Error::from)
}
fn child_wait_can_retry_after_stale(spec: ChildWaitSpec) -> bool {
!matches!(spec.selector, ChildWaitSelector::Exact(_))
}
pub(super) type KernelSigset = u64;
pub(super) const KERNEL_SIGSET_SIZE: usize = std::mem::size_of::<KernelSigset>();
fn signal_is_blocked(mask: &KernelSigset, signal: SigWrapper) -> bool {
let raw_signal = signal.raw();
(1..=KernelSigset::BITS as i32).contains(&raw_signal)
&& mask & (1_u64 << (raw_signal as u32 - 1)) != 0
}
#[repr(C)]
#[derive(Clone, Copy)]
pub(super) struct KernelSigaction {
pub(super) handler: u64,
pub(super) flags: u64,
pub(super) restorer: u64,
pub(super) mask: KernelSigset,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum WaitSignalDisposition {
Interrupt,
Restart,
}
fn signal_default_disposition_does_not_interrupt_child_wait(signal: SigWrapper) -> bool {
matches!(
signal.raw(),
libc::SIGCHLD | libc::SIGCONT | libc::SIGURG | libc::SIGWINCH
)
}
fn signal_default_action_ends_the_process(signal: SigWrapper) -> bool {
!matches!(
signal.raw(),
libc::SIGCHLD
| libc::SIGCONT
| libc::SIGURG
| libc::SIGWINCH
| libc::SIGSTOP
| libc::SIGTSTP
| libc::SIGTTIN
| libc::SIGTTOU
| libc::SIGKILL
)
}
fn signal_has_uncatchable_default_disposition(signal: SigWrapper) -> bool {
matches!(signal.raw(), libc::SIGKILL | libc::SIGSTOP)
}
pub(super) async fn wait_signal_disposition<G, T>(
guest: &mut G,
status: ResumeStatus,
guest_signal_mask: &KernelSigset,
action_addr: AddrMut<'_, KernelSigaction>,
inspect_action: bool,
) -> Result<Option<WaitSignalDisposition>, Error>
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
{
let ResumeStatus::Signaled(signals) = status else {
return Ok(None);
};
let Some(mut signals) = signals else {
return Ok(Some(WaitSignalDisposition::Interrupt));
};
signals.sort_by_key(|signal| signal.raw());
for signal in signals {
if signal_is_blocked(guest_signal_mask, signal) {
continue;
}
if signal_has_uncatchable_default_disposition(signal) {
return Ok(Some(WaitSignalDisposition::Interrupt));
}
if !inspect_action {
return Ok(Some(WaitSignalDisposition::Interrupt));
}
let call = syscalls::RtSigaction::new()
.with_signum(signal.raw())
.with_action(None)
.with_old_action(Some(action_addr.cast()))
.with_sigsetsize(std::mem::size_of::<u64>());
guest.inject_with_retry(call).await?;
let action: KernelSigaction = guest.memory().read_value(action_addr)?;
if action.handler == libc::SIG_IGN as u64
|| action.handler == libc::SIG_DFL as u64
&& signal_default_disposition_does_not_interrupt_child_wait(signal)
{
continue;
}
return Ok(Some(
if action.handler != libc::SIG_DFL as u64 && action.flags & libc::SA_RESTART as u64 != 0
{
WaitSignalDisposition::Restart
} else {
WaitSignalDisposition::Interrupt
},
));
}
Ok(None)
}
pub(crate) fn futex_wait_held_after_wake(
host_timed: KernelSigset,
committed: KernelSigset,
) -> KernelSigset {
(kernel_sigset_bit(libc::SIGCHLD) | host_timed) & !(committed & !host_timed)
}
pub(crate) async fn futex_wait_pending_signals<G, T>(
guest: &mut G,
state: &KernelSignalState,
interrupting: KernelSigset,
held: KernelSigset,
host_timed: KernelSigset,
committed: KernelSigset,
) -> KernelSigset
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
{
let pending = without_discarded(guest, state, interrupting & !held, committed).await;
let sigchld = kernel_sigset_bit(libc::SIGCHLD) & !host_timed;
if pending == 0
&& interrupting & state.thread_pending & held & sigchld != 0
&& matches!(
crate::tool_global::child_exit_sigchld(
guest,
crate::scheduler::child_exit_sigchld::ChildExitSigchldControl::PrivatePending,
)
.await,
crate::scheduler::child_exit_sigchld::ChildExitSigchldAnswer::Placed(true)
)
{
return sigchld;
}
pending
}
pub(crate) async fn without_discarded<G, T>(
guest: &mut G,
state: &KernelSignalState,
pending: KernelSigset,
committed: KernelSigset,
) -> KernelSigset
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
{
use crate::scheduler::discarded_at_send::DiscardedSignalAnswer;
use crate::scheduler::discarded_at_send::DiscardedSignalControl;
use crate::scheduler::discarded_at_send::STANDARD_SIGNALS;
if pending & STANDARD_SIGNALS == 0
|| !crate::scheduler::discarded_at_send::records(guest.config())
{
return pending;
}
let DiscardedSignalAnswer::Bits { thread, process } =
crate::tool_global::discarded_signal(guest, DiscardedSignalControl::Bits).await
else {
return pending;
};
with_kept_copies(state, thread, process, pending, committed)
}
pub(crate) fn with_kept_copies(
state: &KernelSignalState,
thread: u64,
process: u64,
pending: KernelSigset,
committed: KernelSigset,
) -> KernelSigset {
let _ = committed;
let kept = (state.thread_pending & !thread) | (state.shared_pending & !process);
pending & kept
}
pub(crate) fn kernel_sigset_bit(raw_signal: i32) -> KernelSigset {
if (1..=KernelSigset::BITS as i32).contains(&raw_signal) {
1_u64 << (raw_signal as u32 - 1)
} else {
0
}
}
pub(crate) fn clone_host_timed_signals(
flags: CloneFlags,
exit_signal: libc::c_int,
) -> KernelSigset {
if flags.intersects(CloneFlags::CLONE_THREAD | CloneFlags::CLONE_PARENT)
|| exit_signal == libc::SIGCHLD
{
0
} else {
kernel_sigset_bit(exit_signal)
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct KernelSignalState {
pub(crate) pending: KernelSigset,
pub(crate) thread_pending: KernelSigset,
pub(crate) shared_pending: KernelSigset,
pub(crate) blocked: KernelSigset,
pub(crate) ignored: KernelSigset,
pub(crate) caught: KernelSigset,
}
pub(crate) fn default_action_ends_process(
ignored: KernelSigset,
caught: KernelSigset,
signal: i32,
) -> bool {
let bit = kernel_sigset_bit(signal);
let stops_or_continues = [
libc::SIGSTOP,
libc::SIGTSTP,
libc::SIGTTIN,
libc::SIGTTOU,
libc::SIGCONT,
]
.into_iter()
.fold(0, |set, signal| set | kernel_sigset_bit(signal));
signal == libc::SIGKILL
|| ((ignored | caught | default_ignored_signals() | stops_or_continues) & bit) == 0
}
pub(crate) fn ignored_at_generation(ignored: KernelSigset, caught: KernelSigset) -> KernelSigset {
ignored | (default_ignored_signals() & !caught)
}
impl KernelSignalState {
pub(crate) fn parse(status: &str) -> Option<Self> {
let mut thread_pending = None;
let mut shared_pending = None;
let mut state = KernelSignalState::default();
let mut blocked = None;
let mut ignored = None;
let mut caught = None;
for line in status.lines() {
let Some((key, value)) = line.split_once(':') else {
continue;
};
let slot = match key {
"SigPnd" => &mut thread_pending,
"ShdPnd" => &mut shared_pending,
"SigBlk" => &mut blocked,
"SigIgn" => &mut ignored,
"SigCgt" => &mut caught,
_ => continue,
};
*slot = Some(KernelSigset::from_str_radix(value.trim(), 16).ok()?);
}
state.thread_pending = thread_pending?;
state.shared_pending = shared_pending?;
state.pending = state.thread_pending | state.shared_pending;
state.blocked = blocked?;
state.ignored = ignored?;
state.caught = caught?;
Some(state)
}
pub(crate) fn interrupting(&self, mask: KernelSigset) -> KernelSigset {
let default_action = !(self.ignored | self.caught);
(self.caught | (default_action & !default_ignored_signals()))
& !mask
& !kernel_sigset_bit(reverie::PERF_EVENT_SIGNAL as i32)
}
pub(crate) fn discarded_at_generation(&self) -> KernelSigset {
self.ignored_at_generation() & !self.blocked
}
pub(crate) fn ignored_at_generation(&self) -> KernelSigset {
ignored_at_generation(self.ignored, self.caught)
}
pub(crate) fn default_action_ends_process(&self, signal: i32) -> bool {
default_action_ends_process(self.ignored, self.caught, signal)
}
pub(crate) fn default_job_control_stops(&self) -> KernelSigset {
[libc::SIGTSTP, libc::SIGTTIN, libc::SIGTTOU]
.into_iter()
.fold(0, |set, signal| set | kernel_sigset_bit(signal))
& !(self.ignored | self.caught)
}
pub(crate) fn interrupting_wait(
&self,
mask: KernelSigset,
defers_default_stops: bool,
) -> KernelSigset {
let interrupting = self.interrupting(mask);
if defers_default_stops {
interrupting & !self.default_job_control_stops()
} else {
interrupting
}
}
pub(crate) fn pending_interrupting(
&self,
mask: KernelSigset,
defers_default_stops: bool,
) -> KernelSigset {
self.pending & self.interrupting_wait(mask, defers_default_stops)
}
pub(crate) fn next_dequeued(&self, mask: KernelSigset) -> Option<(i32, SignalQueue)> {
let synchronous = SYNCHRONOUS_SIGNALS
.into_iter()
.fold(0, |set, signal| set | kernel_sigset_bit(signal));
let first = |queued: KernelSigset| {
let deliverable = queued & !mask;
let preferred = if deliverable & synchronous != 0 {
deliverable & synchronous
} else {
deliverable
};
(preferred != 0).then(|| preferred.trailing_zeros() as i32 + 1)
};
first(self.thread_pending)
.map(|signal| (signal, SignalQueue::Thread))
.or_else(|| first(self.shared_pending).map(|signal| (signal, SignalQueue::Shared)))
}
pub(crate) fn queued(&self, queue: SignalQueue) -> KernelSigset {
match queue {
SignalQueue::Thread => self.thread_pending,
SignalQueue::Shared => self.shared_pending,
}
}
}
fn default_ignored_signals() -> KernelSigset {
[libc::SIGCHLD, libc::SIGCONT, libc::SIGURG, libc::SIGWINCH]
.into_iter()
.fold(0, |set, signal| set | kernel_sigset_bit(signal))
}
const SYNCHRONOUS_SIGNALS: [i32; 6] = [
libc::SIGSEGV,
libc::SIGBUS,
libc::SIGILL,
libc::SIGTRAP,
libc::SIGFPE,
libc::SIGSYS,
];
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SignalQueue {
Thread,
Shared,
}
#[cfg(not(test))]
static HOST_SIGNAL_STATE_READS_REFUSED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
#[cfg(test)]
thread_local! {
static HOST_SIGNAL_STATE_READS_REFUSED_FOR_TEST: std::cell::Cell<bool> =
const { std::cell::Cell::new(false) };
}
pub(crate) fn refuses_host_signal_state_reads(backend: &BackendCapabilities) -> bool {
!crate::procfs_inode::guest_tids_name_host_tasks(backend, None)
}
pub(crate) fn refuse_host_signal_state_reads(backend: &BackendCapabilities) {
if !refuses_host_signal_state_reads(backend) {
return;
}
#[cfg(not(test))]
HOST_SIGNAL_STATE_READS_REFUSED.store(true, std::sync::atomic::Ordering::Relaxed);
#[cfg(test)]
HOST_SIGNAL_STATE_READS_REFUSED_FOR_TEST.with(|refused| refused.set(true));
}
pub(crate) fn host_signal_state_reads_refused() -> bool {
#[cfg(not(test))]
{
HOST_SIGNAL_STATE_READS_REFUSED.load(std::sync::atomic::Ordering::Relaxed)
}
#[cfg(test)]
{
HOST_SIGNAL_STATE_READS_REFUSED_FOR_TEST.with(|refused| refused.get())
}
}
pub(crate) fn read_kernel_signal_state(pid: Pid, tid: Pid) -> Result<KernelSignalState, Errno> {
#[cfg(test)]
if let Some(result) = signal_state_read_seam::scripted(pid, tid) {
return result;
}
if host_signal_state_reads_refused() {
static REPORTED: std::sync::Once = std::sync::Once::new();
REPORTED.call_once(|| {
error!(
"refused a host /proc signal state read of guest thread {} (process {}): this \
backend's guest thread IDs do not name host tasks",
tid.as_raw(),
pid.as_raw()
);
});
return Err(Errno::ENOSYS);
}
let path = format!("/proc/{}/task/{}/status", pid.as_raw(), tid.as_raw());
let status = std::fs::read_to_string(path).map_err(|_| Errno::ESRCH)?;
KernelSignalState::parse(&status).ok_or(Errno::EIO)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BlockedWaitSignalError {
StateUnreadable { pid: Pid, tid: Pid, errno: Errno },
MaskNotRestored {
pid: Pid,
tid: Pid,
attempts: usize,
last_error: Option<Errno>,
},
}
impl std::fmt::Display for BlockedWaitSignalError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::StateUnreadable { pid, tid, errno } => write!(
f,
"cannot read the signal state of guest thread {tid} (process {pid}) from \
/proc while it waits: {errno}"
),
Self::MaskNotRestored {
pid,
tid,
attempts,
last_error,
} => {
write!(
f,
"cannot restore the signal mask of guest thread {tid} (process {pid}) \
after a blocked wait ({attempts} attempts"
)?;
if let Some(errno) = last_error {
write!(f, ", last error {errno}")?;
}
f.write_str("); it would resume with every signal blocked")
}
}
}
}
impl std::error::Error for BlockedWaitSignalError {}
pub(crate) fn guest_thread_is_gone(pid: Pid, tid: Pid) -> bool {
let result = unsafe { libc::syscall(libc::SYS_tgkill, pid.as_raw(), tid.as_raw(), 0) };
result == -1 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH)
}
pub(crate) fn read_thread_signal_state(pid: Pid, tid: Pid) -> Result<KernelSignalState, Errno> {
read_kernel_signal_state(pid, tid).or_else(|error| {
if pid == tid {
Err(error)
} else {
read_kernel_signal_state(tid, tid)
}
})
}
pub(crate) fn thread_is_gone(pid: Pid, tid: Pid) -> bool {
if !guest_thread_is_gone(pid, tid) {
return false;
}
let result = unsafe { libc::syscall(libc::SYS_tkill, tid.as_raw(), 0) };
result == -1 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH)
}
pub(crate) fn read_thread_wait_signal_state(
pid: Pid,
tid: Pid,
) -> Result<KernelSignalState, Error> {
match read_thread_signal_state(pid, tid) {
Ok(state) => Ok(state),
Err(_) if thread_is_gone(pid, tid) => Err(Errno::ERESTARTNOINTR.into()),
Err(errno) => Err(Error::Tool(anyhow::Error::new(
BlockedWaitSignalError::StateUnreadable { pid, tid, errno },
))),
}
}
pub(crate) fn read_wait_signal_state(pid: Pid, tid: Pid) -> Result<KernelSignalState, Error> {
match read_kernel_signal_state(pid, tid) {
Ok(state) => Ok(state),
Err(_) if guest_thread_is_gone(pid, tid) => Err(Errno::ERESTARTNOINTR.into()),
Err(errno) => Err(Error::Tool(anyhow::Error::new(
BlockedWaitSignalError::StateUnreadable { pid, tid, errno },
))),
}
}
#[cfg(test)]
pub(crate) mod signal_state_read_seam {
use std::cell::RefCell;
use reverie::Pid;
use reverie::syscalls::Errno;
use super::KernelSignalState;
type Script = Box<dyn FnMut(Pid, Pid) -> Option<Result<KernelSignalState, Errno>>>;
thread_local! {
static SCRIPT: RefCell<Option<Script>> = const { RefCell::new(None) };
}
pub(crate) struct Installed(());
impl Drop for Installed {
fn drop(&mut self) {
SCRIPT.with(|script| script.borrow_mut().take());
}
}
pub(crate) fn install(
script: impl FnMut(Pid, Pid) -> Option<Result<KernelSignalState, Errno>> + 'static,
) -> Installed {
SCRIPT.with(|slot| *slot.borrow_mut() = Some(Box::new(script)));
Installed(())
}
pub(super) fn scripted(pid: Pid, tid: Pid) -> Option<Result<KernelSignalState, Errno>> {
SCRIPT.with(|slot| {
slot.borrow_mut()
.as_mut()
.and_then(|script| script(pid, tid))
})
}
}
pub(super) fn blocked_signal_mask() -> KernelSigset {
let mut libc_mask: libc::sigset_t = unsafe { std::mem::zeroed() };
unsafe {
libc::sigfillset(&mut libc_mask);
libc::sigdelset(&mut libc_mask, reverie::PERF_EVENT_SIGNAL as i32);
}
(1..=KernelSigset::BITS as i32).fold(0, |mask, raw_signal| {
if unsafe { libc::sigismember(&libc_mask, raw_signal) } == 1 {
mask | (1_u64 << (raw_signal as u32 - 1))
} else {
mask
}
})
}
pub(super) async fn block_signals_for_disposition<G, T>(
guest: &mut G,
blocked_mask_addr: Addr<'_, KernelSigset>,
old_mask_addr: AddrMut<'_, KernelSigset>,
) -> Result<KernelSigset, Error>
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
{
let block_signals = syscalls::RtSigprocmask::new()
.with_how(libc::SIG_SETMASK)
.with_set(
(!guest
.config()
.backend
.requires_thread_directed_process_signals)
.then_some(blocked_mask_addr.cast()),
)
.with_oldset(Some(old_mask_addr.cast()))
.with_sigsetsize(KERNEL_SIGSET_SIZE);
guest.inject_with_retry(block_signals).await?;
let guest_mask: KernelSigset = guest.memory().read_value(old_mask_addr)?;
if guest.config().backend.reports_child_exit_publication
|| crate::scheduler::discarded_at_send::records(guest.config())
{
crate::tool_global::set_saved_guest_sigmask(guest, Some(guest_mask)).await;
}
Ok(guest_mask)
}
pub(super) async fn restore_signals_after_disposition<G, T>(
guest: &mut G,
old_mask_addr: AddrMut<'_, KernelSigset>,
) -> Result<(), Error>
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
{
if !guest
.config()
.backend
.requires_thread_directed_process_signals
{
let old_mask: Addr<'_, KernelSigset> = old_mask_addr.into();
let restore_signals = syscalls::RtSigprocmask::new()
.with_how(libc::SIG_SETMASK)
.with_set(Some(old_mask.cast()))
.with_oldset(None)
.with_sigsetsize(KERNEL_SIGSET_SIZE);
guest.inject_with_retry(restore_signals).await?;
}
if guest.config().backend.reports_child_exit_publication
|| crate::scheduler::discarded_at_send::records(guest.config())
{
crate::tool_global::set_saved_guest_sigmask(guest, None).await;
}
Ok(())
}
async fn interrupted_child_wait_result<G, T, S>(
guest: &mut G,
call: S,
disposition: WaitSignalDisposition,
) -> Result<i64, Error>
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
S: SyscallInfo,
{
if !guest
.config()
.backend
.requires_thread_directed_process_signals
{
return Err(Errno::ERESTARTSYS.into());
}
if disposition == WaitSignalDisposition::Interrupt {
return Err(Errno::EINTR.into());
}
guest.tail_inject(call).await
}
fn snapshot_process_group(pid: Pid) -> Result<libc::pid_t, Errno> {
let pgrp = Process::new(pid.as_raw())
.and_then(|process| process.stat())
.map(|stat| stat.pgrp)
.map_err(|_| Errno::ESRCH)?;
if pgrp == 0 {
Err(Errno::EOPNOTSUPP)
} else {
Ok(pgrp)
}
}
fn guest_fd_status_flags(pid: Pid, fd: libc::c_int) -> Result<libc::c_int, Errno> {
let path = format!("/proc/{}/fdinfo/{}", pid.as_raw(), fd);
let contents = std::fs::read_to_string(path).map_err(|_| Errno::EBADF)?;
let flags = contents
.lines()
.find_map(|line| line.strip_prefix("flags:"))
.map(str::trim)
.ok_or(Errno::EINVAL)?;
libc::c_int::from_str_radix(flags, 8).map_err(|_| Errno::EINVAL)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FutexTimeout {
Relative(u64),
Absolute(LogicalTime),
}
fn futex_timespec_is_valid(timeout: Timespec) -> bool {
timeout.tv_sec >= 0 && (0..1_000_000_000).contains(&timeout.tv_nsec)
}
const FUTEX_KTIME_MAX_NANOS: u64 = i64::MAX as u64;
fn parse_futex_timeout(futex_op: i32, timeout: Timespec) -> Result<FutexTimeout, Errno> {
if !futex_timespec_is_valid(timeout) {
return Err(Errno::EINVAL);
}
let seconds = timeout.tv_sec as u64;
let nanoseconds = timeout.tv_nsec as u64;
let timeout_nanos = if seconds >= FUTEX_KTIME_MAX_NANOS / 1_000_000_000 {
FUTEX_KTIME_MAX_NANOS
} else {
seconds * 1_000_000_000 + nanoseconds
};
if futex_op & libc::FUTEX_CMD_MASK == libc::FUTEX_WAIT_BITSET {
Ok(FutexTimeout::Absolute(LogicalTime::from_nanos(
timeout_nanos,
)))
} else {
Ok(FutexTimeout::Relative(timeout_nanos))
}
}
fn rebase_absolute_timeout(
deadline: LogicalTime,
clock_now: LogicalTime,
logical_now: LogicalTime,
) -> LogicalTime {
logical_now + Duration::from_nanos(deadline.as_nanos().saturating_sub(clock_now.as_nanos()))
}
fn absolute_timeout_uses_host_clock(
deadline: LogicalTime,
host_clock_now: LogicalTime,
logical_now: LogicalTime,
) -> bool {
deadline.as_nanos().abs_diff(host_clock_now.as_nanos())
< deadline.as_nanos().abs_diff(logical_now.as_nanos())
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FutexDeadline {
FromCall,
Restart(Option<LogicalTime>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SchedYieldRoute {
ChaosReprioritize,
ReplayStrongYield,
ChaosYieldEpoch,
WeakYield,
EndTimeslice,
}
fn sched_yield_route(cfg: &Config) -> SchedYieldRoute {
if cfg.chaos && cfg.max_timeslice.is_none() {
SchedYieldRoute::ChaosReprioritize
} else if !cfg.chaos && cfg.replay_preemptions_from.is_some() {
SchedYieldRoute::ReplayStrongYield
} else if cfg.chaos
&& cfg.replay_schedule_from.is_none()
&& cfg.backend.delivers_preemption_timer_events
&& cfg.sched_heuristic != SchedHeuristic::Random
{
SchedYieldRoute::ChaosYieldEpoch
} else if cfg.chaos || cfg.replay_schedule_from.is_some() {
SchedYieldRoute::WeakYield
} else {
SchedYieldRoute::EndTimeslice
}
}
impl<T: RecordOrReplay> Detcore<T> {
async fn futex_timeout_deadline<G: Guest<Self>>(
&self,
guest: &mut G,
futex_flags: i32,
timeout: Option<Addr<'_, Timespec>>,
) -> Result<Option<LogicalTime>, Error> {
let Some(timeout) = timeout else {
return Ok(None);
};
let timeout = parse_futex_timeout(futex_flags, guest.memory().read_value(timeout)?)?;
match timeout {
FutexTimeout::Relative(nanos) => {
let now = thread_observe_time(guest).await;
Ok(Some(now + Duration::from_nanos(nanos)))
}
FutexTimeout::Absolute(deadline)
if self.cfg.virtualize_time
&& !self.cfg.backend.guest_clock_reads_bypass_backend =>
{
Ok(Some(deadline))
}
FutexTimeout::Absolute(deadline) => {
let clockid = if futex_flags & libc::FUTEX_CLOCK_REALTIME != 0 {
syscalls::ClockId::CLOCK_REALTIME
} else {
syscalls::ClockId::CLOCK_MONOTONIC
};
let mut stack = guest.stack().await;
let clock_output = syscalls::TimespecMutPtr(stack.reserve());
let _stack_guard = stack.commit()?;
let clock_call = syscalls::ClockGettime::new()
.with_clockid(clockid)
.with_tp(Some(clock_output));
if self.cfg.virtualize_time && self.cfg.backend.guest_clock_reads_bypass_backend {
guest.inject(Syscall::from(clock_call)).await?;
} else {
self.record_or_replay(guest, clock_call).await?;
}
let clock_now = match parse_futex_timeout(
libc::FUTEX_WAIT_BITSET,
guest.memory().read_value(clock_output.0)?,
)? {
FutexTimeout::Absolute(time) => time,
FutexTimeout::Relative(_) => unreachable!(),
};
let logical_now = thread_observe_time(guest).await;
if self.cfg.virtualize_time
&& !absolute_timeout_uses_host_clock(deadline, clock_now, logical_now)
{
return Ok(Some(deadline));
}
Ok(Some(rebase_absolute_timeout(
deadline,
clock_now,
logical_now,
)))
}
}
}
async fn futex_deadline<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Futex,
deadline: FutexDeadline,
) -> Result<Option<LogicalTime>, Error> {
match deadline {
FutexDeadline::FromCall => {
self.futex_timeout_deadline(guest, call.futex_op(), call.timeout())
.await
}
FutexDeadline::Restart(deadline) => Ok(deadline),
}
}
fn promote_before_native_lifecycle<G: Guest<Self>>(&self, guest: &G) {
let backend = &guest.config().backend;
if self.cfg.sequentialize_threads
&& backend.promotes_open_files_before_native_lifecycle
&& !crate::syscalls::timerfd::detcore_runs_outside_the_guest(backend)
{
guest.thread_state().promote_shared_open_files().expect(
"could not publish shared open file descriptions before a native fork or exec",
);
}
}
pub async fn handle_clone_family<G: Guest<Self>>(
&self,
guest: &mut G,
clone_family: syscalls::family::CloneFamily,
) -> Result<i64, Error> {
if let syscalls::family::CloneFamily::Clone3(clone) = &clone_family
&& let Some(refusal) = self.clone3_set_tid_refusal(guest, clone).await
{
return refusal;
}
let (flags, exit_signal, child_tid) = match &clone_family {
syscalls::family::CloneFamily::Clone3(clone) => {
let memory = guest.memory();
(
clone3_field(&memory, clone, 0)
.map_or_else(CloneFlags::empty, CloneFlags::from_bits_truncate),
clone3_field(&memory, clone, 32).map_or(0, |signal| signal as libc::c_int),
clone3_field(&memory, clone, 16).map_or(0, |address| address as usize),
)
}
syscalls::family::CloneFamily::Clone(clone) => (
clone_family.flags(&guest.memory()),
(clone.flags().bits() & 0xff) as libc::c_int,
clone_family.child_tid(&guest.memory()),
),
#[cfg(not(target_arch = "aarch64"))]
_ => (
clone_family.flags(&guest.memory()),
libc::SIGCHLD,
clone_family.child_tid(&guest.memory()),
),
};
let ctid = child_tid_clear_address(flags, child_tid);
let pidfd_address = guest
.config()
.sequentialize_threads
.then(|| clone_pidfd_address(&clone_family, &guest.memory(), flags))
.flatten();
let is_vfork = flags.contains(CloneFlags::CLONE_VFORK);
let ts = guest.thread_state_mut();
assert_eq!(ts.clone_flags, None);
assert!(ts.pending_vfork.is_none());
ts.clone_flags = Some(flags);
let parent_dettid = ts.dettid;
let child_priority_entropy = if is_vfork
&& self.cfg.chaos
&& self.cfg.replay_preemptions_from.is_none()
&& self.cfg.replay_schedule_from.is_none()
{
let mut parent_chaos_prng = ts.chaos_prng.clone();
Some(parent_chaos_prng.next_u64())
} else {
None
};
if is_vfork {
ts.pending_vfork = Some(PendingVfork {
parent_dettid,
parent_detpid: ts.detpid.expect("detpid unset"),
child_tid_addr: ctid,
flags,
exit_signal,
child_priority_entropy,
});
}
let exit_signals = clone_host_timed_signals(flags, exit_signal);
if exit_signals != 0
&& bundled_signal_facts::host_timed_signal_sources_are_physical(&guest.config().backend)
{
record_host_timed_signals(guest, HostTimedSignalScope::Caller, exit_signals).await;
}
if !flags.contains(CloneFlags::CLONE_THREAD) {
self.promote_before_native_lifecycle(guest);
}
super::signal_shadow::before_clone(guest, flags.bits()).await;
trace!("[detcore, dtid {}] parent invoking clone.", parent_dettid);
let blocking_child_op_id =
ExternalOpId::new(parent_dettid, guest.thread_state().stats.syscall_count);
if is_vfork && self.cfg.sequentialize_threads {
let mut resources = Resources::new(parent_dettid);
resources.insert(
ResourceID::BlockingVfork(blocking_child_op_id),
Permission::RW,
);
resources.fyi("clone_vfork");
resource_request(guest, resources).await;
}
let call = Syscall::from(clone_family);
let sysno = call.number();
let maybe_res = guest.inject(call).await;
let refusal = if maybe_res.is_err() {
guest.take_unsupported_refusal()
} else {
None
};
if is_vfork && self.cfg.sequentialize_threads {
let mut resources = Resources::new(parent_dettid);
if maybe_res.is_err() {
resources.insert(
ResourceID::VforkFailed(blocking_child_op_id),
Permission::RW,
);
resources.fyi("clone_vfork_failed");
} else {
resources.insert(
ResourceID::BlockedExternalContinue(blocking_child_op_id),
Permission::RW,
);
resources.fyi("clone_vfork");
}
resource_request(guest, resources).await;
}
let ts = guest.thread_state_mut();
ts.clone_flags = None; ts.pending_vfork = None;
if let (Some(refusal), Err(errno)) = (refusal, &maybe_res) {
tracing::error!(
"[detcore, dtid {parent_dettid}] {}; no child was created",
refusal.diagnostic
);
if !self.cfg.panic_on_unsupported_syscalls {
crate::tool_global::report_unsupported_syscall(guest, sysno).await;
}
return self
.refuse_unserviceable_operation_because(guest, sysno, *errno, refusal.diagnostic)
.await;
}
let res = maybe_res?;
if !flags.contains(CloneFlags::CLONE_THREAD) {
guest.thread_state().forget_flock_modes();
}
if is_vfork
&& self.cfg.chaos
&& self.cfg.replay_preemptions_from.is_none()
&& self.cfg.replay_schedule_from.is_none()
{
let _ = guest
.thread_state_mut()
.chaos_prng_next_u64("child_priority");
}
let child_tid = Pid::from_raw(res as i32);
let child_dettid = DetTid::from_raw(child_tid.into()); trace!(
"[detcore] dtid {} cloned, continuing parent + register new thread.",
child_dettid
);
if !is_vfork {
create_child_thread(guest, child_dettid, ctid, Some(flags), exit_signal, None).await;
}
if let Some(address) = pidfd_address {
self.register_clone_pidfd(guest, address, child_dettid)
.await;
}
{
let parent_pedigree = &mut guest.thread_state_mut().pedigree;
let child_pedigree = parent_pedigree.fork_mut();
debug!(
"[dtid {}] after creating child thread (tid {}, pedigree {}) parents pedigree becomes {}",
parent_dettid, child_dettid, child_pedigree, parent_pedigree,
);
}
if guest.thread_state().nested_pid_namespace {
let refusal = match crate::tool_global::local_pid(guest, child_dettid).await {
Ok(Some(local)) => return Ok(local as i64),
Ok(None) => {
format!("child {child_dettid} has no pid recorded in its creator's namespace")
}
Err(reason) => reason,
};
tracing::error!(
"[detcore, dtid {}] the pid a clone in a nested PID namespace returns cannot be translated: {} (https://github.com/rrnewton/hermit/issues/4090)",
parent_dettid,
refusal,
);
return self
.refuse_unserviceable_operation(
guest,
reverie::syscalls::Sysno::clone,
Errno::ENOSYS,
)
.await;
}
Ok(child_dettid.as_raw() as i64)
}
pub async fn handle_set_tid_address<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::SetTidAddress,
) -> Result<i64, Error> {
let address = call.tidptr().map_or(0, |pointer| pointer.as_raw());
let result = self
.record_or_replay(guest, Syscall::SetTidAddress(call))
.await?;
if guest.config().sequentialize_threads {
set_child_tid_address(guest, address).await;
}
trace!(
"[detcore, dtid {}] child TID clear address registered: {address:#x}",
guest.thread_state().dettid,
);
Ok(result)
}
pub async fn handle_set_robust_list<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::SetRobustList,
) -> Result<i64, Error> {
let head = call.head().map(AddrMut::as_raw);
let len = call.len();
let res = self
.record_or_replay(guest, Syscall::SetRobustList(call))
.await?;
let recorded = match head {
None => None,
Some(_) if len != robust_list::ROBUST_LIST_HEAD_LEN => None,
Some(addr) => Some(addr),
};
guest.thread_state_mut().record_robust_list_head(recorded);
trace!(
"[detcore, dtid {}] robust-list head registered: {:?}",
guest.thread_state().dettid,
recorded,
);
Ok(res)
}
async fn run_robust_list_owner_death<G: Guest<Self>>(&self, guest: &mut G) {
let Some(path) = self.robust_owner_death_path() else {
return;
};
let Some(head) = guest.thread_state().robust_list_head else {
return;
};
let dettid = guest.thread_state().dettid;
let owner_tid = dettid.as_raw() as u32;
let mut marked = HashMap::new();
let mut effects = GuestRobustEffects::<'_, G, T> {
guest,
dettid,
staged_wakes: None,
marked: &mut marked,
tool: PhantomData,
};
let outcome = robust_list::exit_robust_list(&mut effects, head, owner_tid).await;
if path == OwnerDeathPath::Barrier {
guest.thread_state().mark_robust_list_walked_by_tool();
}
if outcome.head_unreadable {
trace!(
"[detcore, dtid {}] unreadable robust-list head at {:#x}; no owner-death wakeups",
dettid, head,
);
} else if outcome.aborted || outcome.next_faulted || outcome.truncated {
trace!(
"[detcore, dtid {}] robust-list walk stopped early after {} entr(ies): {:?}",
dettid, outcome.entries_visited, outcome,
);
}
}
pub(crate) async fn stage_thread_group_robust_list_wakes<G: Guest<Self>>(
&self,
guest: &mut G,
reason: RobustListExit,
) {
match self.robust_owner_death_path() {
None | Some(OwnerDeathPath::Barrier) => {}
Some(OwnerDeathPath::Kernel) => {
let staged = self
.read_thread_group_robust_list_wakes(guest, OwnerDeath::Exit)
.await;
guest.thread_state().stage_robust_list_wakes(reason, staged);
}
}
}
async fn run_thread_group_robust_list_owner_death<G: Guest<Self>>(
&self,
guest: &mut G,
heads: Vec<RobustListOwner>,
) {
let mut marked = HashMap::new();
for RobustListOwner {
dettid: owner,
head,
} in heads
{
let mut effects = GuestRobustEffects::<'_, G, T> {
guest,
dettid: owner,
staged_wakes: None,
marked: &mut marked,
tool: PhantomData,
};
let outcome =
robust_list::exit_robust_list(&mut effects, head, owner.as_raw() as u32).await;
if outcome.head_unreadable || outcome.aborted {
trace!(
"[detcore, dtid {}] robust-list owner death stopped early: {:?}",
owner, outcome,
);
}
}
}
fn robust_owner_death_path(&self) -> Option<OwnerDeathPath> {
owner_death_path(&self.cfg)
}
pub async fn exec_robust_list_wakes<G: Guest<Self>>(
&self,
guest: &mut G,
) -> Vec<(DetTid, FutexID)> {
if self.robust_owner_death_path().is_none() {
return Vec::new();
}
let wakes = self
.read_thread_group_robust_list_wakes(guest, OwnerDeath::Exec)
.await;
crate::tool_local::flatten_exec_robust_list_wakes(wakes)
.into_iter()
.map(|(owner, wake)| (owner, wake.futex))
.collect()
}
async fn read_thread_group_robust_list_wakes<G: Guest<Self>>(
&self,
guest: &mut G,
death: OwnerDeath,
) -> Vec<(DetTid, Vec<RobustListWake>)> {
let caller = guest.thread_state().dettid;
let tgid = guest.thread_state().detpid.expect("detpid unset");
let mut heads: Vec<(DetTid, usize)> = guest
.thread_state()
.robust_list_owners_for_exit_group()
.into_iter()
.map(|owner| (owner.dettid, owner.head))
.collect();
if death == OwnerDeath::Exec {
heads.sort_by_key(|(owner, _)| *owner == caller);
}
let mut staged = Vec::with_capacity(heads.len());
let mut marked = HashMap::new();
for (owner, head) in heads {
let walked_as = if death == OwnerDeath::Exec && owner == caller {
tgid.as_raw()
} else {
owner.as_raw()
};
let mut wakes = Vec::new();
let mut effects = GuestRobustEffects::<'_, G, T> {
guest,
dettid: owner,
staged_wakes: Some(&mut wakes),
marked: &mut marked,
tool: PhantomData,
};
let outcome = robust_list::exit_robust_list(&mut effects, head, walked_as as u32).await;
if outcome.head_unreadable || outcome.aborted {
trace!(
"[detcore, dtid {}] could not stage complete robust-list owner-death effects: {:?}",
owner, outcome,
);
}
staged.push((owner, wakes));
}
staged
}
pub async fn handle_exit<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Exit,
) -> Result<i64, Error> {
crate::guest_log::flush_unsent();
if self.cfg.backend.reports_kernel_child_exit_signals {
let uid = real_uid(guest.tid().as_raw()).unwrap_or_else(|| unsafe { libc::getuid() });
crate::tool_global::child_exit_sigchld(
guest,
crate::scheduler::child_exit_sigchld::ChildExitSigchldControl::RawExitStatus {
code: libc::CLD_EXITED,
status: call.status() & 0xff,
uid,
},
)
.await;
}
let request = guest.thread_state().mk_request(
ResourceID::Exit {
group: false,
process: guest.thread_state().detpid.expect("detpid unset"),
mm: guest.thread_state().mm_id,
},
Permission::RW,
);
resource_request(guest, request).await;
self.run_robust_list_owner_death(guest).await;
let state = guest.thread_state();
if self.cfg.sequentialize_threads
&& state
.detpid
.is_some_and(|pid| pid.as_raw() == state.dettid.as_raw())
&& crate::tool_global::retire_exited_leader(guest).await
&& self.cfg.replay_data.is_some()
{
let message = format!(
"hermit: thread-group leader {} exited while its threads run on, which record \
and replay cannot yet capture, so the run is refused \
(https://github.com/rrnewton/hermit/issues/4244)",
guest.thread_state().dettid
);
tracing::error!("{message}");
let _ = std::io::Write::write_fmt(
&mut crate::util::RetryingStderr,
format_args!("{message}\n"),
);
crate::tool_global::unrecoverable_shutdown(
guest,
detcore_model::HERMIT_POLICY_REFUSAL_EXIT,
)
.await;
}
guest.tail_inject(call).await
}
pub async fn handle_exit_group<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::ExitGroup,
) -> Result<i64, Error> {
crate::guest_log::flush_unsent();
if self.cfg.backend.reports_kernel_child_exit_signals {
let uid = real_uid(guest.tid().as_raw()).unwrap_or_else(|| unsafe { libc::getuid() });
crate::tool_global::child_exit_sigchld(
guest,
crate::scheduler::child_exit_sigchld::ChildExitSigchldControl::ExitStatus {
code: libc::CLD_EXITED,
status: call.status() & 0xff,
uid,
},
)
.await;
}
let barrier = self.robust_owner_death_path() == Some(OwnerDeathPath::Barrier);
if barrier {
guest.thread_state().file_exit_group_for_robust_lists();
}
let request = guest.thread_state().mk_request(
ResourceID::Exit {
group: true,
process: guest.thread_state().detpid.expect("detpid unset"),
mm: guest.thread_state().mm_id,
},
Permission::RW,
);
resource_request(guest, request).await;
if barrier {
guest.thread_state().begin_exit_group_robust_walk();
let owners = guest.thread_state().robust_list_owners_for_exit_group();
self.run_thread_group_robust_list_owner_death(guest, owners)
.await;
guest.thread_state().finish_exit_group_robust_walk();
} else {
self.stage_thread_group_robust_list_wakes(guest, RobustListExit::ExitGroup)
.await
}
guest.tail_inject(call).await
}
pub(crate) async fn end_thread_group_before_fatal_delivery<G: Guest<Self>>(
&self,
guest: &mut G,
signal: SigWrapper,
) -> bool {
if !(self.cfg.sequentialize_threads
&& self.cfg.backend.requires_thread_directed_process_signals)
|| !signal_default_action_ends_the_process(signal)
{
return false;
}
match self.signal_is_left_at_default(guest, signal).await {
Ok(true) => {}
Ok(false) => return false,
Err(error) => {
tracing::warn!(
"[detcore, dtid {}] cannot read the disposition of {}: {}",
guest.thread_state().dettid,
signal,
error
);
return false;
}
}
info!(
"[detcore, dtid {}] {} ends its thread group by default",
guest.thread_state().dettid,
signal
);
let barrier = self.robust_owner_death_path() == Some(OwnerDeathPath::Barrier);
if barrier {
guest.thread_state().file_exit_group_for_robust_lists();
}
let dettid = guest.thread_state().dettid;
if !hold_turn_for_group_exit(guest, dettid).await {
tracing::warn!(
"[detcore, dtid {}] the scheduler did not hold the turn for the group exit of {}",
dettid,
signal
);
}
let request = guest.thread_state().mk_request(
ResourceID::Exit {
group: true,
process: guest.thread_state().detpid.expect("detpid unset"),
mm: guest.thread_state().mm_id,
},
Permission::RW,
);
resource_request(guest, request).await;
match self.signal_is_left_at_default(guest, signal).await {
Ok(true) => {}
changed => {
info!(
"[detcore, dtid {}] the action for {} changed after the delivery decision \
({:?}); restoring SIG_DFL in the dying process",
guest.thread_state().dettid,
signal,
changed.map(|default| !default)
);
if let Err(error) = self.restore_default_action(guest, signal).await {
tracing::warn!(
"[detcore, dtid {}] cannot restore SIG_DFL for {}: {}",
guest.thread_state().dettid,
signal,
error
);
}
}
}
if barrier {
guest.thread_state().begin_exit_group_robust_walk();
let owners = guest.thread_state().robust_list_owners_for_exit_group();
self.run_thread_group_robust_list_owner_death(guest, owners)
.await;
guest.thread_state().finish_exit_group_robust_walk();
}
true
}
async fn restore_default_action<G: Guest<Self>>(
&self,
guest: &mut G,
signal: SigWrapper,
) -> Result<(), Error> {
let default = KernelSigaction {
handler: libc::SIG_DFL as u64,
flags: 0,
restorer: 0,
mask: 0,
};
let mut stack = guest.stack().await;
let action_addr = stack.push(default);
let _guard = stack.commit()?;
let call = syscalls::RtSigaction::new()
.with_signum(signal.raw())
.with_action(Some(action_addr.cast()))
.with_old_action(None)
.with_sigsetsize(std::mem::size_of::<u64>());
guest.inject_with_retry(call).await?;
super::signal_shadow::give_up(
guest,
"Detcore reset an action to the default before a fatal delivery",
);
Ok(())
}
async fn signal_is_left_at_default<G: Guest<Self>>(
&self,
guest: &mut G,
signal: SigWrapper,
) -> Result<bool, Error> {
let mut stack = guest.stack().await;
let action_addr = stack.reserve::<KernelSigaction>();
let _guard = stack.commit()?;
let call = syscalls::RtSigaction::new()
.with_signum(signal.raw())
.with_action(None)
.with_old_action(Some(action_addr.cast()))
.with_sigsetsize(std::mem::size_of::<u64>());
guest.inject_with_retry(call).await?;
let action: KernelSigaction = guest.memory().read_value(action_addr)?;
Ok(action.handler == libc::SIG_DFL as u64)
}
pub async fn handle_futex<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Futex,
) -> Result<i64, Error> {
self.handle_futex_from(guest, call, FutexDeadline::FromCall)
.await
}
pub(crate) async fn handle_futex_from<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Futex,
deadline: FutexDeadline,
) -> Result<i64, Error> {
let dettid = guest.thread_state().dettid;
if self.cfg.sequentialize_threads
&& !matches!(self.cfg.debug_futex_mode, BlockingMode::External)
{
match classify_futex_command(call.futex_op()) {
FutexCommand::Served => {}
FutexCommand::NoSys => return Err(Error::Errno(Errno::ENOSYS)),
FutexCommand::PriorityInheritance(name) => {
return self
.refuse_futex_command(
guest,
name,
"Detcore does not emulate priority-inheritance futexes",
)
.await;
}
}
}
if self.cfg.sequentialize_threads
&& !matches!(self.cfg.debug_futex_mode, BlockingMode::External)
{
let command = call.futex_op() & libc::FUTEX_CMD_MASK;
if matches!(command, libc::FUTEX_WAKE | libc::FUTEX_WAKE_BITSET)
&& matches!(self.cfg.debug_futex_mode, BlockingMode::Precise)
{
return self.handle_futex_wake(guest, call, command).await;
}
let name = match command {
libc::FUTEX_REQUEUE => Some("FUTEX_REQUEUE"),
libc::FUTEX_CMP_REQUEUE => Some("FUTEX_CMP_REQUEUE"),
libc::FUTEX_WAKE_OP => Some("FUTEX_WAKE_OP"),
_ => None,
};
if let Some(name) = name {
if matches!(self.cfg.debug_futex_mode, BlockingMode::Polling) {
return self
.refuse_futex_command(guest, name, POLLING_REFUSAL)
.await;
}
return if command == libc::FUTEX_WAKE_OP {
self.handle_futex_wake_op(guest, call).await
} else {
self.handle_futex_requeue(guest, call, command).await
};
}
}
let ptr = match call.uaddr() {
None => {
return Ok(guest.inject(call).await?);
}
Some(x) => x,
};
if self.cfg.sequentialize_threads
&& !matches!(self.cfg.debug_futex_mode, BlockingMode::External)
&& !AddrMut::as_raw(ptr).is_multiple_of(std::mem::size_of::<u32>())
{
return Err(Error::Errno(Errno::EINVAL));
}
if !self.cfg.sequentialize_threads {
return Ok(guest.inject(call).await?);
}
let polling = match self.cfg.debug_futex_mode {
BlockingMode::External => {
return self.record_or_replay_blocking(guest, call.into()).await;
}
BlockingMode::Precise => false,
BlockingMode::Polling => true,
};
let init_val = match call.futex_op() & libc::FUTEX_CMD_MASK {
command @ (libc::FUTEX_WAIT | libc::FUTEX_WAIT_BITSET) => {
if matches!(deadline, FutexDeadline::FromCall)
&& let Some(timeout) = call.timeout()
{
let timeout = read_user_timespec(guest, timeout.as_raw())?;
if !futex_timespec_is_valid(timeout) {
return Err(Error::Errno(Errno::EINVAL));
}
}
if command == libc::FUTEX_WAIT_BITSET && call.val3() == 0 {
return Err(Error::Errno(Errno::EINVAL));
}
read_futex_word(guest, AddrMut::as_raw(ptr))?
}
_ => 0,
};
trace!(
"[detcore, dtid {}] futex op with memory address containing value {}",
&dettid, init_val
);
if polling {
self.handle_futex_polling(guest, call, init_val, deadline)
.await
} else {
self.handle_futex_blocking(guest, call, init_val, deadline)
.await
}
}
pub(crate) async fn handle_futex_blocking<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Futex,
init_val: i32,
deadline: FutexDeadline,
) -> Result<i64, Error> {
let ptr = call.uaddr().unwrap();
let futexid = guest.thread_state().futex_id(
AddrMut::as_raw(ptr),
call.futex_op() & libc::FUTEX_PRIVATE_FLAG != 0,
);
let futex_op = call.futex_op() & libc::FUTEX_CMD_MASK;
let bitset = match futex_op {
libc::FUTEX_WAKE_BITSET | libc::FUTEX_WAIT_BITSET => call.val3() as u32,
_ => u32::MAX,
};
if bitset == 0 {
return Err(Error::Errno(Errno::EINVAL));
}
let dettid = guest.thread_state().dettid;
match futex_op {
libc::FUTEX_WAIT | libc::FUTEX_WAIT_BITSET => {
if init_val != call.val() {
info!(
"[detcore, dtid {}] Futex wait running immediately because it will fizzle ({} != {}).",
&dettid,
init_val,
call.val()
);
Err(Error::Errno(Errno::EAGAIN))
} else {
let maybe_timeout_lt = self.futex_deadline(guest, call, deadline).await?;
let signal_interruption = guest
.config()
.backend
.supports_blocked_wait_signal_interruption;
let mut rewait = false;
let mut committed_wake: KernelSigset = 0;
let mut requested = false;
let res = loop {
let signal_watch = if signal_interruption {
let state = match read_wait_signal_state(guest.pid(), guest.tid()) {
Ok(state) => state,
Err(error) => break Err(error),
};
let defers_default_stops = call.restart_keeps_deadline();
let host_timed = host_timed_signals(guest).await;
let committed = std::mem::take(&mut committed_wake);
let held = futex_wait_held_after_wake(host_timed, committed);
let pending = futex_wait_pending_signals(
guest,
&state,
state.pending_interrupting(state.blocked, defers_default_stops),
held,
host_timed,
committed,
)
.await;
if pending != 0 {
let errno = call.kernel_restart_errno();
trace!(
"[detcore, dtid {}] futex wait interrupted by pending signals {:#x}: {:?}",
&dettid, pending, errno
);
break Err(Error::Errno(errno));
}
Some(FutexSignalWatch {
unblocked: !state.blocked
& !kernel_sigset_bit(reverie::PERF_EVENT_SIGNAL as i32),
defers_default_stops,
pid: guest.pid().as_raw(),
tid: guest.tid().as_raw(),
})
} else {
None
};
if rewait {
let observed = match read_futex_word(guest, AddrMut::as_raw(ptr)) {
Ok(observed) => observed,
Err(error) => break Err(error),
};
if observed != call.val() {
trace!(
"[detcore, dtid {}] futex value changed while its wait was woken for a signal ({} != {}); EAGAIN",
&dettid,
observed,
call.val()
);
break Err(Error::Errno(Errno::EAGAIN));
}
debug!(
"[detcore, dtid {}] futex wait woken for a signal that is not pending for it; waiting again until {:?}",
&dettid, maybe_timeout_lt
);
}
requested = true;
let ans = futex_action(
guest,
FutexAction::WaitRequest(maybe_timeout_lt, signal_watch),
&futexid,
init_val,
bitset,
)
.await;
if signal_interruption && let Some(SchedValue::Signaled(committed)) = ans {
committed_wake = committed;
rewait = true;
continue;
}
break if ans != Some(SchedValue::TimeOut) {
let expected = call.val();
match guest.memory().read_value(ptr) {
Ok(observed) => {
trace!(
"[detcore, dtid {}] after (emulated) futex wait, memory value is {}, expected {}",
&dettid, observed, expected,
);
if expected == observed {
debug!(
"WARNING: fishy that the futex value did not change before wakeup. Weird application-level protocol.\n"
);
}
}
Err(error) => trace!(
"[detcore, dtid {}] skipped post-wait futex memory diagnostic: {}",
&dettid, error,
),
}
Ok(0)
} else {
trace!("[detcore, dtid {}] futex wait timed out", &dettid);
Err(Error::Errno(Errno::ETIMEDOUT))
};
};
if requested {
futex_action(guest, FutexAction::WaitFinished, &futexid, init_val, bitset)
.await;
}
keep_restart_block(guest, &res, RestartCall::Futex(call), maybe_timeout_lt)
.await;
res
}
}
_ => Err(Error::Errno(Errno::ENOSYS)),
}
}
async fn handle_futex_wake<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Futex,
command: i32,
) -> Result<i64, Error> {
let bitset = if command == libc::FUTEX_WAKE_BITSET {
call.val3() as u32
} else {
u32::MAX
};
if bitset == 0 {
return Err(Error::Errno(Errno::EINVAL));
}
let private = call.futex_op() & libc::FUTEX_PRIVATE_FLAG != 0;
let futexid = futex_key(guest, futex_uaddr(&call), private, KeyAccess::Read).await?;
let num = match futex_action(
guest,
FutexAction::WakeRequest(call.val()),
&futexid,
0,
bitset,
)
.await
.expect("futex wake must return value")
{
SchedValue::Value(num) => num,
other => panic!("impossible answer to a futex wake: {:?}", other),
};
let _ = futex_action(guest, FutexAction::WakeFinished(0), &futexid, 0, bitset).await;
if self.cfg.yields_after_futex_wake(num) {
guest.thread_state_mut().yield_after_futex_wake = true;
}
Ok(num as i64)
}
async fn handle_futex_requeue<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Futex,
command: i32,
) -> Result<i64, Error> {
let nr_wake = call.val();
let nr_requeue = futex_val2(&call) as i32;
if nr_wake < 0 || nr_requeue < 0 {
return Err(Error::Errno(Errno::EINVAL));
}
let private = call.futex_op() & libc::FUTEX_PRIVATE_FLAG != 0;
let uaddr = futex_uaddr(&call);
let futexid = futex_key(guest, uaddr, private, KeyAccess::Read).await?;
let futexid2 = futex_key(guest, futex_uaddr2(&call), private, KeyAccess::Read).await?;
let mut current = 0;
if command == libc::FUTEX_CMP_REQUEUE {
current = read_futex_word(guest, uaddr)?;
if current != call.val3() {
return Err(Error::Errno(Errno::EAGAIN));
}
}
let woken_and_moved = match futex_action(
guest,
FutexAction::RequeueRequest(futexid2, nr_wake as u32, nr_requeue as u32),
&futexid,
current,
u32::MAX,
)
.await
{
Some(SchedValue::Requeued { woken, moved }) => {
if self.cfg.yields_after_futex_wake(woken) {
guest.thread_state_mut().yield_after_futex_wake = true;
}
woken + moved
}
Some(SchedValue::Value(num)) => num,
other => panic!("impossible answer to a futex requeue: {:?}", other),
};
let _ = futex_action(
guest,
FutexAction::WakeFinished(0),
&futexid,
current,
u32::MAX,
)
.await;
Ok(woken_and_moved as i64)
}
async fn handle_futex_wake_op<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Futex,
) -> Result<i64, Error> {
let nr_wake = call.val();
let nr_wake2 = futex_val2(&call) as i32;
let private = call.futex_op() & libc::FUTEX_PRIVATE_FLAG != 0;
let uaddr2 = futex_uaddr2(&call);
let futexid = futex_key(guest, futex_uaddr(&call), private, KeyAccess::Read).await?;
let futexid2 = futex_key(guest, uaddr2, private, KeyAccess::Write).await?;
let op = FutexWakeOp::decode(call.val3() as u32);
if !op.has_known_operation() {
return Err(Error::Errno(Errno::ENOSYS));
}
if matches!(
guest.thread_state().futex_id(uaddr2, false),
FutexID::Shared {
object: SharedMemoryObjectId::File { .. } | SharedMemoryObjectId::OpenFile { .. },
..
}
) {
return self
.refuse_futex_command(
guest,
"FUTEX_WAKE_OP",
"its word is in a file-backed shared mapping that a process outside Hermit \
could update concurrently, and Detcore cannot apply the operation atomically",
)
.await;
}
let old = read_futex_word(guest, uaddr2)?;
let new = op.apply(old).expect("operation was checked to be known");
let word2 = AddrMut::<libc::c_int>::from_raw(uaddr2).ok_or(Errno::EFAULT)?;
guest
.memory()
.write_value(word2, &new)
.map_err(|_| Error::Errno(Errno::EFAULT))?;
let Some(wake2) = op.compare(old) else {
return Err(Error::Errno(Errno::ENOSYS));
};
let mut woken = 0;
for (futex, count, wake) in [(futexid, nr_wake, true), (futexid2, nr_wake2, wake2)] {
if !wake {
continue;
}
woken += match futex_action(guest, FutexAction::WakeRequest(count), &futex, 0, u32::MAX)
.await
{
Some(SchedValue::Value(num)) => num,
other => panic!("impossible answer to a futex wake: {:?}", other),
};
let _ = futex_action(guest, FutexAction::WakeFinished(0), &futex, 0, u32::MAX).await;
}
if self.cfg.yields_after_futex_wake(woken) {
guest.thread_state_mut().yield_after_futex_wake = true;
}
Ok(woken as i64)
}
async fn refuse_futex_command<G: Guest<Self>>(
&self,
guest: &mut G,
name: &'static str,
reason: &'static str,
) -> Result<i64, Error> {
tracing::error!(
"[detcore, dtid {}] futex command {} is not supported: {}",
guest.thread_state().dettid,
name,
reason,
);
self.refuse_unserviceable_operation(guest, reverie::syscalls::Sysno::futex, Errno::ENOSYS)
.await
}
pub(crate) async fn handle_futex_polling<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Futex,
init_val: i32,
deadline: FutexDeadline,
) -> Result<i64, Error> {
fn make_futex_wake_request(dettid: DetTid) -> Resources {
let mut rsrc = Resources::new(dettid);
rsrc.fyi("futex_wake");
rsrc
}
fn make_futex_wait_request(dettid: DetTid) -> Resources {
let mut rsrc = Resources::new(dettid);
rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
rsrc.fyi("futex_wait");
rsrc
}
let dettid = guest.thread_state().dettid;
let futex_op = call.futex_op() & libc::FUTEX_CMD_MASK;
match futex_op {
libc::FUTEX_WAKE | libc::FUTEX_WAKE_BITSET => {
let rsrc = make_futex_wake_request(dettid);
resource_request(guest, rsrc.clone()).await; let res = guest.inject(call).await;
Ok(res?)
}
libc::FUTEX_WAIT | libc::FUTEX_WAIT_BITSET => {
if init_val != call.val() {
info!(
"[detcore, dtid {}] Futex wait running immediately because it will fizzle ({} != {}).",
dettid,
init_val,
call.val()
);
let res = guest.inject(call).await;
Ok(res?)
} else {
let rsrc = make_futex_wait_request(dettid);
let deadline = self.futex_deadline(guest, call, deadline).await?;
let res =
retry_nonblocking_syscall_with_timeout(guest, call, rsrc, deadline).await;
keep_restart_block(guest, &res, RestartCall::Futex(call), deadline).await;
let res = res?;
trace!(
"[detcore, dtid {}] after futex wait, memory value is {}",
&dettid,
guest.memory().read_value(call.uaddr().unwrap()).unwrap()
);
Ok(res)
}
}
_ => Err(Error::Errno(Errno::ENOSYS)),
}
}
fn exec_image<G: Guest<Self>>(
&self,
guest: &G,
call: &syscalls::Execveat,
) -> Option<crate::exec_record::ExecRecord> {
let path: std::path::PathBuf = call.path()?.read(&guest.memory()).ok()?;
self.exec_image_at(guest, call.dirfd(), &path)
}
pub(crate) fn exec_image_from_execfn<G: Guest<Self>>(
&self,
guest: &G,
) -> Option<crate::exec_record::ExecRecord> {
use reverie::syscalls::FromToRaw;
let address = guest.auxv().at_execfn()?.as_raw();
let path: std::path::PathBuf = Option::<syscalls::PathPtr>::from_raw(address)?
.read(&guest.memory())
.ok()?;
self.exec_image_at(guest, libc::AT_FDCWD, &path)
}
pub fn exec_image_at<G: Guest<Self>>(
&self,
guest: &G,
dirfd: i32,
path: &std::path::Path,
) -> Option<crate::exec_record::ExecRecord> {
let task = self.guest_task_directory(guest)?;
let root = std::fs::read_link(task.join("root")).ok()?;
let inside = |link: std::path::PathBuf| {
Some(std::path::Path::new("/").join(link.strip_prefix(&root).ok()?))
};
let cwd = inside(std::fs::read_link(task.join("cwd")).ok()?)?;
let spelled = if path.is_absolute() || dirfd == libc::AT_FDCWD {
cwd.join(path)
} else {
let descriptor = task.join("fd").join(dirfd.to_string());
let directory = inside(std::fs::read_link(descriptor).ok()?)?;
if path.as_os_str().is_empty() {
directory
} else {
directory.join(path)
}
};
match self.resolve_exe_subject(guest, &spelled, true) {
Some(crate::syscalls::namespace::ExeLinkSubject::Own) => {
return guest.thread_state().exe.as_deref().cloned();
}
Some(crate::syscalls::namespace::ExeLinkSubject::Other(_)) => return None,
None => {}
}
crate::exec_record::ExecRecord::resolve(&root, &cwd, &spelled)
}
pub async fn handle_execveat<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Execveat,
) -> Result<i64, Error> {
self.promote_before_native_lifecycle(guest);
let (old_metadata, old_memory_metadata, table_is_shared, dettid, detpid, old_mm_id) = {
let thread_state = guest.thread_state();
(
Arc::clone(&thread_state.file_metadata),
Arc::clone(&thread_state.memory_metadata),
Arc::strong_count(&thread_state.file_metadata) > 1,
thread_state.dettid,
thread_state.detpid.expect("detpid unset"),
thread_state.mm_id,
)
};
let (new_metadata, closed_open_files, exec_fd_blocking) = {
let metadata = old_metadata.lock().unwrap();
let new_metadata = metadata.for_exec(dettid);
(
new_metadata.clone(),
metadata.open_files_closed_on_exec(table_is_shared),
new_metadata.exec_blocking_overrides(),
)
};
let preserve_exec_fd_status = guest.config().backend.rediscovers_descriptors_after_exec;
let exe = self.exec_image(guest, &call);
prepare_exec(
guest,
old_mm_id,
if preserve_exec_fd_status {
exec_fd_blocking
} else {
Default::default()
},
Vec::new(),
exe,
)
.await;
let mut released_ports = Vec::new();
for open_file_id in closed_open_files {
if let Some(port) = self.release_port_for_open_file(guest, open_file_id).await {
released_ports.push((open_file_id, port));
}
}
let exec_robust_wakes = match self.robust_owner_death_path() {
Some(OwnerDeathPath::Kernel) => Some(
self.read_thread_group_robust_list_wakes(guest, OwnerDeath::Exec)
.await,
),
Some(OwnerDeathPath::Barrier) | None => None,
};
let old_robust_list_head;
let old_process_files = guest.thread_state().process_files;
{
let thread_state = guest.thread_state_mut();
thread_state.file_metadata = Arc::new(Mutex::new(new_metadata));
thread_state.adopt_process_files();
thread_state.memory_metadata = Arc::new(Mutex::new(MemoryMetadata::new()));
thread_state.mm_id = old_mm_id.for_exec(detpid);
old_robust_list_head = thread_state.take_robust_list_for_exec();
if let Some(wakes) = exec_robust_wakes {
thread_state.stage_robust_list_wakes_for_exec(wakes);
}
}
let errno = self.record_or_replay(guest, call).await.unwrap_err();
let refusal = guest.take_unsupported_refusal();
{
let thread_state = guest.thread_state_mut();
thread_state.file_metadata = old_metadata;
thread_state.process_files = old_process_files;
thread_state.memory_metadata = old_memory_metadata;
thread_state.mm_id = old_mm_id;
thread_state.restore_robust_list_after_failed_exec(old_robust_list_head);
thread_state.take_robust_list_wakes_for_exec();
}
cancel_exec(guest).await;
for (open_file_id, port) in released_ports {
self.restore_port_for_open_file(guest, open_file_id, port)
.await;
}
if let Some(refusal) = refusal {
tracing::error!(
"[detcore, dtid {dettid}] {}; the replacement image did not run",
refusal.diagnostic
);
if !self.cfg.panic_on_unsupported_syscalls {
crate::tool_global::report_unsupported_syscall(guest, call.number()).await;
}
return self
.refuse_unserviceable_operation_because(
guest,
call.number(),
errno,
refusal.diagnostic,
)
.await;
}
Err(errno.into())
}
pub async fn handle_sched_yield<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::SchedYield,
) -> Result<i64, Error> {
if self.cfg.sequentialize_threads {
match sched_yield_route(&self.cfg) {
SchedYieldRoute::ChaosReprioritize => {
let change_time = guest.thread_state().thread_logical_time.as_nanos();
let request = Self::random_priority_changepoint_request(guest, change_time);
resource_request(guest, request).await;
}
SchedYieldRoute::ReplayStrongYield => {
if self.cfg.max_timeslice.is_some() {
guest
.thread_state_mut()
.reset_timeslice_for_explicit_yield();
}
let request = Self::sched_yield_request(guest);
resource_request(guest, request).await;
}
SchedYieldRoute::ChaosYieldEpoch => {
let request = Self::sched_yield_request(guest);
resource_request(guest, request).await;
}
SchedYieldRoute::WeakYield => {
let request = Self::yield_request(guest);
resource_request(guest, request).await;
}
SchedYieldRoute::EndTimeslice => self.end_timeslice_for_sched_yield(guest).await,
}
trace!("sched_yield yielded to the scheduler; NOT performing actual syscall");
Ok(0)
} else {
Ok(self.record_or_replay(guest, call).await?)
}
}
async fn guest_visible_pid<G: Guest<Self>>(
&self,
guest: &mut G,
sysno: reverie::syscalls::Sysno,
child: DetPid,
) -> Result<i32, Error> {
if !guest.thread_state().nested_pid_namespace {
return Ok(child.as_raw());
}
let refusal = match crate::tool_global::local_pid(guest, child).await {
Ok(Some(local)) => return Ok(local),
Ok(None) => format!("child {child} is not visible in the caller's namespace"),
Err(reason) => reason,
};
Err(self.refuse_nested_pid(guest, sysno, &refusal).await)
}
async fn task_of_guest_pid<G: Guest<Self>>(
&self,
guest: &mut G,
sysno: reverie::syscalls::Sysno,
pid: i32,
) -> Result<DetPid, Error> {
if !guest.thread_state().nested_pid_namespace || pid <= 0 {
return Ok(DetPid::from_raw(pid));
}
let refusal = match crate::tool_global::guest_pid(guest, pid).await {
GuestPidAnswer::Task { task, .. } => return Ok(task),
GuestPidAnswer::NoSuchPid => {
format!("the kernel answered pid {pid}, which no recorded task has")
}
GuestPidAnswer::Untranslatable(reason) => reason,
};
Err(self.refuse_nested_pid(guest, sysno, &refusal).await)
}
async fn register_clone_pidfd<G: Guest<Self>>(
&self,
guest: &mut G,
address: usize,
child: DetTid,
) {
let dettid = guest.thread_state().dettid;
let skip = |reason: String| {
tracing::error!(
"[detcore, dtid {}] the pidfd of CLONE_PIDFD child {} is not tracked: {}; pidfd \
calls through it answer EBADF (https://github.com/rrnewton/hermit/issues/4430)",
dettid,
child,
reason,
);
};
let fd: libc::c_int = match Addr::<libc::c_int>::from_raw(address)
.map(|address| guest.memory().read_value(address))
{
Some(Ok(fd)) => fd,
_ => return skip(format!("its word at {address:#x} cannot be read back")),
};
if guest.thread_state().with_detfd(fd, |_| ()).is_ok() {
return skip(format!(
"its word names descriptor {fd}, which Detcore already tracks"
));
}
if let Err(errno) = self
.track_held_fd(
guest,
fd,
nix::fcntl::OFlag::O_CLOEXEC,
crate::fd::FdType::Pidfd,
)
.await
{
return skip(format!("tracking descriptor {fd} failed with {errno}"));
}
if let Err(error) = guest.thread_state().with_detfd(fd, |detfd| {
detfd.set_pidfd_target(DetPid::from_raw(child.as_raw()))
}) {
skip(format!("descriptor {fd} has no Detcore entry: {error}"));
}
}
async fn clone3_set_tid_refusal<G: Guest<Self>>(
&self,
guest: &mut G,
clone: &syscalls::Clone3,
) -> Option<Result<i64, Error>> {
const CLONE_ARGS_SIZE_VER1: usize = 80;
const MAX_SET_TID: u64 = 32;
let size = clone.size();
let page = usize::try_from(unsafe { libc::sysconf(libc::_SC_PAGESIZE) }).ok()?;
if size < CLONE_ARGS_SIZE_VER1 || size > page {
return None;
}
let mut raw = vec![0u8; size];
let memory = guest.memory();
memory
.read_exact(Addr::<u8>::from_raw(clone.args()?.as_raw())?, &mut raw)
.ok()?;
let field = |offset: usize| u64::from_ne_bytes(raw[offset..offset + 8].try_into().unwrap());
let flags = field(0);
let exit_signal = field(32);
let set_tid = field(64);
let set_tid_size = field(72);
if exit_signal > 64 || flags & 0xff != 0 {
return None;
}
if set_tid_size == 0 || set_tid_size > MAX_SET_TID {
return None;
}
let mut tids = vec![0 as libc::pid_t; set_tid_size as usize];
memory
.read_values(Addr::<libc::pid_t>::from_raw(set_tid as usize)?, &mut tids)
.ok()?;
let newpid = flags & libc::CLONE_NEWPID as u64 != 0;
if tids.iter().any(|tid| *tid < 1) || (newpid && tids[0] != 1) {
return None;
}
const CAP_SYS_ADMIN: u32 = 21;
const CAP_CHECKPOINT_RESTORE: u32 = 40;
let thread = guest.thread_state();
let status = format!(
"/proc/{}/task/{}/status",
thread.detpid.expect("detpid unset"),
thread.dettid
);
let capabilities = std::fs::read_to_string(status).ok().and_then(|status| {
status
.lines()
.find_map(|line| line.strip_prefix("CapEff:"))
.and_then(|mask| u64::from_str_radix(mask.trim(), 16).ok())
});
let pid_capabilities = (1u64 << CAP_SYS_ADMIN) | (1u64 << CAP_CHECKPOINT_RESTORE);
if capabilities.is_some_and(|mask| mask & pid_capabilities == 0) {
return None;
}
if guest.thread_state().nested_pid_namespace {
return Some(Err(self
.refuse_nested_pid(
guest,
reverie::syscalls::Sysno::clone3,
"clone3 set_tid names pids in a namespace Detcore does not translate",
)
.await));
}
if set_tid_size <= u64::from(newpid) {
return None;
}
let tid = tids[usize::from(newpid)];
if !crate::tool_global::thread_id_refused(guest, DetTid::from_raw(tid)).await {
return None;
}
let message = format!(
"hermit: clone3 set_tid asks for id {tid}, which an earlier task of this run had; \
Detcore keeps per-task state by that id and cannot give it to a new task \
(https://github.com/rrnewton/hermit/issues/4370)"
);
tracing::error!(
"[detcore, dtid {}] {}",
guest.thread_state().dettid,
message
);
if guest.config().panic_on_unsupported_syscalls {
use std::io::Write as _;
let _ = writeln!(crate::util::RetryingStderr, "{message}");
}
Some(
self.refuse_unserviceable_operation(
guest,
reverie::syscalls::Sysno::clone3,
Errno::EEXIST,
)
.await,
)
}
async fn refuse_nested_pid<G: Guest<Self>>(
&self,
guest: &mut G,
sysno: reverie::syscalls::Sysno,
reason: &str,
) -> Error {
tracing::error!(
"[detcore, dtid {}] {} from a nested PID namespace is not supported: {} (https://github.com/rrnewton/hermit/issues/4155)",
guest.thread_state().dettid,
sysno,
reason,
);
match self
.refuse_unserviceable_operation(guest, sysno, Errno::ENOSYS)
.await
{
Err(error) => error,
Ok(_) => Errno::ENOSYS.into(),
}
}
pub async fn handle_wait4<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::Wait4,
) -> Result<i64, Error> {
let serial_emulated_wait =
guest.config().backend.emulates_child_waits && guest.config().sequentialize_threads;
let mut emulated_wait_consumed = false;
let dettid = guest.thread_state().dettid;
let mut rsrc = Resources::new(dettid);
rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
rsrc.fyi("wait4");
validate_wait4_arguments(call.pid(), call.options())?;
let parent = guest.thread_state().detpid.expect("detpid unset");
let nested = guest.thread_state().nested_pid_namespace;
if nested && (call.pid() == 0 || call.pid() < -1 || serial_emulated_wait) {
return Err(self
.refuse_nested_pid(
guest,
reverie::syscalls::Sysno::wait4,
"a process-group wait, or a wait on a backend that emulates child waits",
)
.await);
}
let named_child = if call.pid() > 0 && nested {
match crate::tool_global::guest_pid(guest, call.pid()).await {
GuestPidAnswer::Task { task, .. } => task,
GuestPidAnswer::NoSuchPid => return Err(Errno::ECHILD.into()),
GuestPidAnswer::Untranslatable(reason) => {
return Err(self
.refuse_nested_pid(guest, reverie::syscalls::Sysno::wait4, &reason)
.await);
}
}
} else {
DetPid::from_raw(call.pid())
};
let selector = if !wait4_uses_terminal_selector(call.options(), serial_emulated_wait) {
None
} else {
match call.pid() {
pid if pid > 0 => Some(ChildWaitSelector::Exact(named_child)),
-1 => Some(ChildWaitSelector::Any),
0 => process_group(guest, parent)
.await
.map(ChildWaitSelector::ProcessGroup),
pid if pid < -1 => Some(ChildWaitSelector::ProcessGroup(DetPid::from_raw(-pid))),
_ => unreachable!(),
}
};
let spec = selector
.map(|selector| terminal_child_wait_spec(selector, dettid, call.options().bits()));
let complete_lineage = guest.config().backend.tracks_process_children;
let managed_spec = if let Some(spec) = spec {
let (_, has_child) = ready_child_wait(guest, spec).await;
(has_child || complete_lineage).then_some(spec)
} else {
None
};
let value = if call.options().contains(WaitPidFlag::WNOHANG) {
resource_request(guest, rsrc.clone()).await;
info!(
"[dtid {}] Executing non-blocking wait4 in one shot.",
dettid
);
if let Some(spec) = spec {
'select_child: loop {
let (ready, has_child) = ready_child_wait(guest, spec).await;
let Some(child) = ready else {
break if has_child {
0
} else if complete_lineage {
return Err(Errno::ECHILD.into());
} else {
guest.inject_with_retry(call).await?
};
};
let _ = await_exact_child_physical_exit(guest, child).await;
let exact_call = call.with_pid(
self.guest_visible_pid(guest, reverie::syscalls::Sysno::wait4, child)
.await?,
);
loop {
let result = if serial_emulated_wait {
complete_selected_emulated_wait4(guest, exact_call, child).await
} else {
guest
.inject_with_retry(exact_call)
.await
.map_err(Error::from)
};
emulated_wait_consumed =
serial_emulated_wait && matches!(result, Ok(value) if value > 0);
match result {
Ok(value) if value != 0 => break 'select_child value,
Ok(_) => yield_once().await,
Err(Error::Errno(Errno::ECHILD)) => {
let _ = consume_child_wait(guest, child).await;
if child_wait_can_retry_after_stale(spec) {
continue 'select_child;
}
return Err(Errno::ECHILD.into());
}
Err(error) => return Err(error),
}
}
}
} else {
guest.inject_with_retry(call).await?
}
} else if let Some(spec) = managed_spec {
{
let blocked_mask = blocked_signal_mask();
let mut stack = guest.stack().await;
let blocked_mask_addr = stack.push(blocked_mask);
let old_mask_addr = stack.reserve::<KernelSigset>();
let action_addr = stack.reserve::<KernelSigaction>();
let _mask_guard = stack.commit()?;
let guest_signal_mask =
block_signals_for_disposition(guest, blocked_mask_addr, old_mask_addr).await?;
let inspect_signal_action = guest
.config()
.backend
.requires_thread_directed_process_signals;
let poll_call = call.with_options(call.options() | WaitPidFlag::WNOHANG);
let mut pending_signal = None;
let result: Result<i64, Error> = loop {
let status = wait_for_child_lifecycle(guest, spec).await;
if pending_signal.is_none() {
pending_signal = wait_signal_disposition(
guest,
status,
&guest_signal_mask,
action_addr,
inspect_signal_action,
)
.await?;
}
let (ready, has_child) = ready_child_wait(guest, spec).await;
if let Some(child) = ready {
let _ = await_exact_child_physical_exit(guest, child).await;
let result = if serial_emulated_wait {
complete_selected_emulated_wait4(guest, call, child).await
} else {
match self
.guest_visible_pid(guest, reverie::syscalls::Sysno::wait4, child)
.await
{
Ok(pid) => guest
.inject_with_retry(call.with_pid(pid))
.await
.map_err(Error::from),
Err(error) => Err(error),
}
};
emulated_wait_consumed =
serial_emulated_wait && matches!(result, Ok(value) if value > 0);
match result {
Ok(value) => break Ok(value),
Err(Error::Errno(Errno::ECHILD)) => {
let _ = consume_child_wait(guest, child).await;
if child_wait_can_retry_after_stale(spec) {
let (next_ready, _) = ready_child_wait(guest, spec).await;
if stale_any_wait_must_interrupt(
pending_signal.is_some(),
next_ready,
) {
break interrupted_child_wait_result(
guest,
call,
pending_signal.expect("signal checked above"),
)
.await;
}
continue;
}
break Err(Errno::ECHILD.into());
}
Err(error) => break Err(error),
}
}
if !has_child {
break Err(Errno::ECHILD.into());
}
if serial_emulated_wait {
if let Some(disposition) = pending_signal {
break interrupted_child_wait_result(guest, call, disposition).await;
}
continue;
}
match guest.inject(poll_call).await {
Ok(value) => {
if value > 0 {
break Ok(value);
}
if let Some(disposition) = pending_signal {
break interrupted_child_wait_result(guest, call, disposition)
.await;
}
}
Err(errno) => break Err(errno.into()),
}
};
restore_signals_after_disposition(guest, old_mask_addr).await?;
result?
}
} else {
retry_nonblocking_syscall(guest, call, rsrc, None).await?
};
let reaped = if value > 0 {
self.task_of_guest_pid(guest, reverie::syscalls::Sysno::wait4, value as i32)
.await?
} else {
DetPid::from_raw(value as i32)
};
let consumed_termination = if emulated_wait_consumed || value <= 0 {
false
} else if let Some(status) = call.wstatus() {
wait_status_is_termination(guest.memory().read_value(status)?)
} else if !call
.options()
.intersects(WaitPidFlag::WUNTRACED | WaitPidFlag::WCONTINUED)
{
true
} else {
guest
.thread_state()
.has_exited_child_process_cpu_time(reaped)
|| matches!(
crate::tool_global::exact_child_wait_state(guest, reaped).await,
ExactChildWaitState::LogicallyExited
| ExactChildWaitState::PhysicalExitPending
| ExactChildWaitState::PhysicallyExited
)
};
if consumed_termination {
guest.thread_state_mut().reap_child_process_cpu_time(reaped);
let _ = consume_child_wait(guest, reaped).await;
}
if value > 0
&& let Some(rusage) = call.rusage()
{
let usage: libc::rusage = unsafe { std::mem::zeroed() };
guest.memory().write_value(rusage, &usage)?;
}
Ok(value)
}
pub async fn handle_waitid<G: Guest<Self>>(
&self,
guest: &mut G,
mut call: syscalls::Waitid,
) -> Result<i64, Error> {
if guest.config().backend.emulates_child_waits && guest.config().sequentialize_threads {
if guest.thread_state().nested_pid_namespace {
return Err(self
.refuse_nested_pid(
guest,
reverie::syscalls::Sysno::waitid,
"a wait on a backend that emulates child waits",
)
.await);
}
return emulated_waitid::handle(guest, call).await;
}
let nested = guest.thread_state().nested_pid_namespace;
if nested && call.which() == libc::P_PGID as i32 {
return Err(self
.refuse_nested_pid(
guest,
reverie::syscalls::Sysno::waitid,
"a process-group wait",
)
.await);
}
let named_child = if nested && call.which() == libc::P_PID as i32 && call.pid() > 0 {
match crate::tool_global::guest_pid(guest, call.pid()).await {
GuestPidAnswer::Task { task, .. } => task,
GuestPidAnswer::NoSuchPid => return Err(Errno::ECHILD.into()),
GuestPidAnswer::Untranslatable(reason) => {
return Err(self
.refuse_nested_pid(guest, reverie::syscalls::Sysno::waitid, &reason)
.await);
}
}
} else {
DetPid::from_raw(call.pid())
};
let dettid = guest.thread_state().dettid;
let mut rsrc = Resources::new(dettid);
rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
rsrc.fyi("waitid");
let event_options = libc::WEXITED | libc::WSTOPPED | libc::WCONTINUED;
let allowed_options = event_options
| libc::WNOHANG
| libc::WNOWAIT
| libc::__WNOTHREAD
| libc::__WALL
| libc::__WCLONE;
if call.options() & event_options == 0 || call.options() & !allowed_options != 0 {
return Err(Errno::EINVAL.into());
}
if call.info().is_none() {
return Err(Errno::EFAULT.into());
}
let terminal_events_only = call.options() & libc::WEXITED != 0
&& call.options() & (libc::WSTOPPED | libc::WCONTINUED | libc::__WCLONE | libc::__WALL)
== 0;
if !terminal_events_only && call.which() == libc::P_PGID as i32 && call.pid() == 0 {
call = call.with_pid(snapshot_process_group(guest.pid())?);
}
let pidfd_nonblocking =
if call.which() == libc::P_PIDFD as i32 && call.options() & libc::WNOHANG == 0 {
resource_request(guest, rsrc.clone()).await;
guest_fd_status_flags(guest.pid(), call.pid())? & libc::O_NONBLOCK != 0
} else {
false
};
if call.which() == libc::P_PIDFD as i32
&& call.options() & libc::WNOHANG == 0
&& !pidfd_nonblocking
{
return Err(Errno::EOPNOTSUPP.into());
}
let info = call.info().expect("waitid infop checked above");
let empty_info: libc::siginfo_t = unsafe { std::mem::zeroed() };
let terminal_spec = if terminal_events_only {
let selector = match call.which() {
which if which == libc::P_PID as i32 => Some(ChildWaitSelector::Exact(named_child)),
which if which == libc::P_ALL as i32 => Some(ChildWaitSelector::Any),
which if which == libc::P_PGID as i32 => {
let group = if call.pid() == 0 {
process_group(guest, guest.thread_state().detpid.expect("detpid unset"))
.await
} else {
Some(DetPid::from_raw(call.pid()))
};
group.map(ChildWaitSelector::ProcessGroup)
}
_ => None,
};
selector.map(|selector| terminal_child_wait_spec(selector, dettid, call.options()))
} else {
None
};
let complete_lineage = guest.config().backend.tracks_process_children;
let managed_terminal_spec = if let Some(spec) = terminal_spec {
let (_, has_child) = ready_child_wait(guest, spec).await;
(has_child || complete_lineage).then_some(spec)
} else {
None
};
if call.options() & libc::WNOHANG != 0 || pidfd_nonblocking {
if !pidfd_nonblocking {
resource_request(guest, rsrc).await;
}
info!(
"[dtid {}] Executing non-blocking waitid in one shot.",
dettid
);
'select_child: loop {
let selected = if let Some(spec) = terminal_spec {
let (ready, has_child) = ready_child_wait(guest, spec).await;
if ready.is_none() && has_child {
guest.memory().write_value(info, &empty_info)?;
return Ok(0);
}
if ready.is_none() && complete_lineage {
return Err(Errno::ECHILD.into());
}
ready
} else {
None
};
if let Some(child) = selected {
let _ = await_exact_child_physical_exit(guest, child).await;
}
let effective_call = match selected {
Some(child) => call.with_which(libc::P_PID as i32).with_pid(
self.guest_visible_pid(guest, reverie::syscalls::Sysno::waitid, child)
.await?,
),
None => call,
};
loop {
guest.memory().write_value(info, &empty_info)?;
let value = match guest.inject_with_retry(effective_call).await {
Ok(value) => value,
Err(Errno::ECHILD) if selected.is_some() => {
let child = selected.expect("selected child checked above");
let _ = consume_child_wait(guest, child).await;
if terminal_spec.is_some_and(child_wait_can_retry_after_stale) {
continue 'select_child;
}
return Err(Errno::ECHILD.into());
}
Err(errno) => return Err(errno.into()),
};
let info_value: libc::siginfo_t = guest.memory().read_value(info)?;
let child_pid = unsafe { info_value.si_pid() };
if child_pid == 0 && selected.is_some() {
yield_once().await;
continue;
}
let consumed = child_pid != 0
&& call.options() & libc::WNOWAIT == 0
&& waitid_code_is_termination(info_value.si_code);
let child = self
.task_of_guest_pid(guest, reverie::syscalls::Sysno::waitid, child_pid)
.await?;
let result = finish_waitid_result(guest, call, value, info_value, child)?;
if consumed {
let _ = consume_child_wait(guest, child).await;
}
return Ok(result);
}
}
}
{
let blocked_mask = blocked_signal_mask();
let mut stack = guest.stack().await;
let blocked_mask_addr = stack.push(blocked_mask);
let old_mask_addr = stack.reserve::<KernelSigset>();
let action_addr = stack.reserve::<KernelSigaction>();
let _mask_guard = stack.commit()?;
let guest_signal_mask =
block_signals_for_disposition(guest, blocked_mask_addr, old_mask_addr).await?;
let inspect_signal_action = guest
.config()
.backend
.requires_thread_directed_process_signals;
let poll_call = call.with_options(call.options() | libc::WNOHANG);
let mut pending_signal = None;
let result: Result<i64, Error> = loop {
let managed_spec = managed_terminal_spec;
let status = if let Some(spec) = managed_spec {
wait_for_child_lifecycle(guest, spec).await
} else {
resource_request(guest, rsrc.clone()).await
};
if pending_signal.is_none() {
pending_signal = wait_signal_disposition(
guest,
status,
&guest_signal_mask,
action_addr,
inspect_signal_action,
)
.await?;
}
let (ready, has_child) = if let Some(spec) = managed_spec {
ready_child_wait(guest, spec).await
} else {
(None, true)
};
if let Some(child) = ready {
let _ = await_exact_child_physical_exit(guest, child).await;
if let Err(error) = guest.memory().write_value(info, &empty_info) {
break Err(error.into());
}
let pid = match self
.guest_visible_pid(guest, reverie::syscalls::Sysno::waitid, child)
.await
{
Ok(pid) => pid,
Err(error) => break Err(error),
};
let exact_call = call.with_which(libc::P_PID as i32).with_pid(pid);
match guest.inject_with_retry(exact_call).await {
Ok(value) => {
let info_value: libc::siginfo_t = match guest.memory().read_value(info)
{
Ok(value) => value,
Err(error) => break Err(error.into()),
};
let reported = match self
.task_of_guest_pid(
guest,
reverie::syscalls::Sysno::waitid,
unsafe { info_value.si_pid() },
)
.await
{
Ok(task) => task,
Err(error) => break Err(error),
};
break finish_waitid_result(guest, call, value, info_value, reported);
}
Err(Errno::ECHILD) => {
let _ = consume_child_wait(guest, child).await;
if managed_spec.is_some_and(child_wait_can_retry_after_stale) {
let (next_ready, _) =
ready_child_wait(guest, managed_spec.expect("managed spec"))
.await;
if stale_any_wait_must_interrupt(
pending_signal.is_some(),
next_ready,
) {
break interrupted_child_wait_result(
guest,
call,
pending_signal.expect("signal checked above"),
)
.await;
}
continue;
}
break Err(Errno::ECHILD.into());
}
Err(errno) => break Err(errno.into()),
}
}
if managed_spec.is_some() && !has_child {
break Err(Errno::ECHILD.into());
}
if let Err(error) = guest.memory().write_value(info, &empty_info) {
break Err(error.into());
}
let result = guest.inject(poll_call).await;
match result {
Ok(value) => {
let info_value: libc::siginfo_t = match guest.memory().read_value(info) {
Ok(value) => value,
Err(error) => break Err(error.into()),
};
let child_pid = unsafe { info_value.si_pid() };
match exact_wait_poll_decision(
child_pid != 0,
pending_signal.is_some(),
None,
) {
ExactWaitPollDecision::ChildReady => {
let reported = match self
.task_of_guest_pid(
guest,
reverie::syscalls::Sysno::waitid,
child_pid,
)
.await
{
Ok(task) => task,
Err(error) => break Err(error),
};
break finish_waitid_result(
guest, call, value, info_value, reported,
);
}
ExactWaitPollDecision::Interrupted => {
break interrupted_child_wait_result(
guest,
call,
pending_signal.expect("signal checked above"),
)
.await;
}
ExactWaitPollDecision::Retry => {}
ExactWaitPollDecision::AwaitPhysicalExit
| ExactWaitPollDecision::ReapAfterLogicalExit => unreachable!(),
}
if managed_spec.is_some() {
if !has_child {
break Ok(value);
}
continue;
}
rsrc.poll_attempt += 1;
trace!(
"Retry #{} for waitid because no child state is ready",
rsrc.poll_attempt
);
record_retry_event(guest, poll_call).await;
}
Err(Errno::ERESTARTSYS) if pending_signal.is_some() => {
break Err(Errno::EINTR.into());
}
Err(errno) => break Err(errno.into()),
}
};
restore_signals_after_disposition(guest, old_mask_addr).await?;
if result.is_ok() && call.options() & libc::WNOWAIT == 0 {
let info_value: libc::siginfo_t = guest.memory().read_value(info)?;
let child_pid = unsafe { info_value.si_pid() };
if child_pid != 0 && waitid_code_is_termination(info_value.si_code) {
let child = self
.task_of_guest_pid(guest, reverie::syscalls::Sysno::waitid, child_pid)
.await?;
let _ = consume_child_wait(guest, child).await;
}
}
result
}
}
pub async fn handle_sched_setaffinity<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::SchedSetaffinity,
) -> Result<i64, Error> {
let size_bytes = call.len() as usize;
if size_bytes == 0 {
return Err(Errno::EINVAL.into());
}
let mask = call.mask().ok_or(Errno::EFAULT)?;
let mask: Addr<u8> = mask.cast();
let mut requested = [0u8; VIRTUAL_CPUSET_BYTES];
let bytes_to_read = size_bytes.min(VIRTUAL_CPUSET_BYTES);
guest
.memory()
.read_exact(mask, &mut requested[..bytes_to_read])?;
info!(
"Suppressing sched_setaffinity mask {:?}; affinity remains virtual CPU 0",
requested
);
Ok(0)
}
pub async fn handle_sched_getaffinity<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::SchedGetaffinity,
) -> Result<i64, Error> {
let size_bytes: usize = call.len() as usize;
if size_bytes == 0 || !size_bytes.is_multiple_of(std::mem::size_of::<libc::c_ulong>()) {
return Err(Errno::EINVAL.into());
}
let copied = size_bytes.min(VIRTUAL_CPUSET_BYTES);
let mut cpu_set = [0u8; VIRTUAL_CPUSET_BYTES];
cpu_set[0] = 1;
info!(
"Suppressing sched_getaffinity and returning {}-byte virtualized result, {:?}",
copied,
&cpu_set[..copied]
);
if let Some(mask) = call.mask() {
let mask: AddrMut<u8> = mask.cast();
guest.memory().write_exact(mask, &cpu_set[..copied])?;
Ok(copied as i64)
} else {
Err(Error::Errno(Errno::EFAULT))
}
}
pub async fn handle_sched_getparam<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::SchedGetparam,
) -> Result<i64, Error> {
if let Some(param) = call.param() {
let p = libc::sched_param { sched_priority: 0 };
guest.memory().write_value(param, &p)?;
}
Ok(0)
}
pub async fn handle_sched_rr_get_interval<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::SchedRrGetInterval,
) -> Result<i64, Error> {
if let Some(tp) = call.tp() {
let t = Timespec {
tv_sec: 0,
tv_nsec: 0,
};
guest.memory().write_value(tp, &t)?;
}
Ok(0)
}
pub async fn handle_sched_getattr<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::SchedGetattr,
) -> Result<i64, Error> {
if call.flags() != 0 {
return Err(Errno::EINVAL.into());
}
let attr_size = std::mem::size_of::<libc::sched_attr>();
if (call.size() as usize) < attr_size {
return Err(Errno::EINVAL.into());
}
let attr = call.attr().ok_or(Errno::EINVAL)?;
let mut sa: libc::sched_attr = unsafe { std::mem::zeroed() };
sa.size = attr_size as u32;
sa.sched_policy = libc::SCHED_OTHER as u32;
let bytes = unsafe {
std::slice::from_raw_parts(&sa as *const libc::sched_attr as *const u8, attr_size)
};
let dst: AddrMut<u8> = attr.cast();
guest.memory().write_exact(dst, bytes)?;
info!(
"Emulating sched_getattr(pid={}): fixed SCHED_OTHER, nice 0, priority 0",
call.pid()
);
Ok(0)
}
pub async fn handle_sched_setattr<G: Guest<Self>>(
&self,
guest: &mut G,
call: syscalls::SchedSetattr,
) -> Result<i64, Error> {
if call.flags() != 0 || call.pid() < 0 {
return Err(Errno::EINVAL.into());
}
let attr = call.attr().ok_or(Errno::EINVAL)?;
let declared: u32 = guest
.memory()
.read_value(attr.cast())
.map_err(|_| Errno::EFAULT)?;
fn refuse_too_big<S: MemoryAccess>(mut memory: S, attr: AddrMut<libc::c_void>) -> Error {
let _ = memory.write_value(attr.cast::<u32>(), &SCHED_ATTR_KERNEL_SIZE);
Errno::E2BIG.into()
}
let size = match sched_attr_effective_size(declared) {
Ok(size) => size,
Err(()) => {
let back = call.attr().ok_or(Errno::EINVAL)?;
return Err(refuse_too_big(guest.memory(), back));
}
};
if size > SCHED_ATTR_KERNEL_SIZE {
let base: AddrMut<u8> = call.attr().ok_or(Errno::EINVAL)?.cast();
match scan_tail_is_zeroed(
&guest.memory(),
base,
SCHED_ATTR_KERNEL_SIZE as usize,
(size - SCHED_ATTR_KERNEL_SIZE) as usize,
) {
TailVerdict::AllZero => {}
TailVerdict::NotZeroed => {
let back = call.attr().ok_or(Errno::EINVAL)?;
return Err(refuse_too_big(guest.memory(), back));
}
TailVerdict::Faulted => return Err(Errno::EFAULT.into()),
}
}
let copied = std::cmp::min(size, SCHED_ATTR_KERNEL_SIZE) as usize;
let mut raw = [0u8; SCHED_ATTR_KERNEL_SIZE as usize];
let base: AddrMut<u8> = call.attr().ok_or(Errno::EINVAL)?.cast();
guest
.memory()
.read_exact(base, &mut raw[..copied])
.map_err(|_| Errno::EFAULT)?;
let field32 = |offset: usize| -> u32 {
u32::from_ne_bytes(raw[offset..offset + 4].try_into().expect("4 bytes"))
};
let field64 = |offset: usize| -> u64 {
u64::from_ne_bytes(raw[offset..offset + 8].try_into().expect("8 bytes"))
};
let fields = SchedAttrFields {
policy: field32(SCHED_ATTR_OFF_POLICY),
sched_flags: field64(SCHED_ATTR_OFF_FLAGS),
priority: field32(SCHED_ATTR_OFF_PRIORITY),
runtime: field64(SCHED_ATTR_OFF_RUNTIME),
deadline: field64(SCHED_ATTR_OFF_DEADLINE),
period: field64(SCHED_ATTR_OFF_PERIOD),
};
validate_sched_attr_before_lookup(size, &fields)?;
let named = if call.pid() > 0 && guest.thread_state().nested_pid_namespace {
match crate::tool_global::guest_pid(guest, call.pid()).await {
GuestPidAnswer::Task { task, .. } => Some(task),
GuestPidAnswer::NoSuchPid => None,
GuestPidAnswer::Untranslatable(reason) => {
return Err(self
.refuse_nested_pid(guest, reverie::syscalls::Sysno::sched_setattr, &reason)
.await);
}
}
} else {
Some(DetTid::from_raw(call.pid()))
};
if call.pid() != 0 {
match named {
Some(task) if thread_is_live(guest, task).await => {}
_ => return Err(Errno::ESRCH.into()),
}
}
validate_sched_attr_after_lookup(&fields)?;
info!(
"Suppressing sched_setattr(pid={}, flags={}); Linux scheduler attributes are virtual",
call.pid(),
call.flags()
);
Ok(0)
}
pub async fn handle_ioprio_set<G: Guest<Self>>(
&self,
_guest: &mut G,
call: syscalls::IoprioSet,
) -> Result<i64, Error> {
info!(
"Suppressing ioprio_set(which={}, who={}, priority={}); I/O priority is virtual",
call.which(),
call.who(),
call.priority()
);
Ok(0)
}
pub async fn handle_ioprio_get<G: Guest<Self>>(
&self,
_guest: &mut G,
call: syscalls::IoprioGet,
) -> Result<i64, Error> {
let priority = virtual_ioprio(call.which())?;
info!(
"Emulating ioprio_get(which={}, who={}): fixed priority {}",
call.which(),
call.who(),
priority
);
Ok(priority)
}
}
struct GuestRobustEffects<'a, G, T> {
guest: &'a mut G,
dettid: DetTid,
staged_wakes: Option<&'a mut Vec<RobustListWake>>,
marked: &'a mut HashMap<FutexID, u32>,
tool: PhantomData<T>,
}
impl<G, T> robust_list::RobustDeathEffects for GuestRobustEffects<'_, G, T>
where
G: Guest<Detcore<T>>,
T: RecordOrReplay,
{
fn read_u64(&mut self, address: usize) -> Option<u64> {
let at = Addr::<u64>::from_raw(address)?;
self.guest.memory().read_value::<_, u64>(at).ok()
}
fn read_u32(&mut self, address: usize) -> Option<u32> {
let at = Addr::<u32>::from_raw(address)?;
let value = self.guest.memory().read_value::<_, u32>(at).ok()?;
let futexid = self.guest.thread_state().futex_id(address, false);
Some(self.marked.get(&futexid).copied().unwrap_or(value))
}
fn compare_and_swap(
&mut self,
address: usize,
expected: u32,
desired: u32,
) -> robust_list::FutexCasOutcome {
use robust_list::FutexCasOutcome;
let Some(read_at) = Addr::<u32>::from_raw(address) else {
return FutexCasOutcome::Faulted;
};
let observed = match self.guest.memory().read_value::<_, u32>(read_at) {
Ok(value) => value,
Err(_) => return FutexCasOutcome::Faulted,
};
let futexid = self.guest.thread_state().futex_id(address, false);
let observed = self.marked.get(&futexid).copied().unwrap_or(observed);
if observed != expected {
return FutexCasOutcome::Changed(observed);
}
self.marked.insert(futexid, desired);
debug!(
"[detcore, dtid {}] robust-list owner death: leaving futex word {:#x} for backend exit cleanup",
self.dettid, address,
);
FutexCasOutcome::Matched
}
async fn wake_one(&mut self, address: usize, observed: u32) {
let futexid = self.guest.thread_state().futex_id(address, false);
if let Some(wakes) = self.staged_wakes.as_mut() {
wakes.push(RobustListWake { futex: futexid });
debug!(
"[detcore, dtid {}] staged robust-list owner-death wake until physical exit",
self.dettid,
);
return;
}
let woken = match futex_action(
self.guest,
FutexAction::WakeRequest(1),
&futexid,
observed as i32,
u32::MAX,
)
.await
{
Some(SchedValue::Value(count)) => count,
Some(SchedValue::TimeOut)
| Some(SchedValue::Signaled(_))
| Some(SchedValue::Requeued { .. })
| None => 0,
};
info!(
"[detcore, dtid {}] robust-list owner death woke {} waiter(s) on futex {:?}",
self.dettid, woken, futexid,
);
let _ = futex_action(
self.guest,
FutexAction::WakeFinished(0),
&futexid,
observed as i32,
u32::MAX,
)
.await;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OwnerDeath {
Exit,
Exec,
}
fn owner_death_path(cfg: &Config) -> Option<OwnerDeathPath> {
if !cfg.sequentialize_threads || cfg.debug_futex_mode != BlockingMode::Precise {
return None;
}
if cfg.backend.runs_exit_robust_list {
Some(OwnerDeathPath::Kernel)
} else if cfg.backend.requires_thread_directed_process_signals {
Some(OwnerDeathPath::Barrier)
} else {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OwnerDeathPath {
Kernel,
Barrier,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FutexCommand {
Served,
NoSys,
PriorityInheritance(&'static str),
}
pub(crate) fn classify_futex_command(futex_op: i32) -> FutexCommand {
let cmd = futex_op & libc::FUTEX_CMD_MASK;
if futex_op & libc::FUTEX_CLOCK_REALTIME != 0
&& !matches!(
cmd,
libc::FUTEX_WAIT_BITSET | libc::FUTEX_WAIT_REQUEUE_PI | libc::FUTEX_LOCK_PI2
)
{
return FutexCommand::NoSys;
}
match cmd {
libc::FUTEX_WAIT
| libc::FUTEX_WAIT_BITSET
| libc::FUTEX_WAKE
| libc::FUTEX_WAKE_BITSET
| libc::FUTEX_REQUEUE
| libc::FUTEX_CMP_REQUEUE
| libc::FUTEX_WAKE_OP => FutexCommand::Served,
libc::FUTEX_LOCK_PI => FutexCommand::PriorityInheritance("FUTEX_LOCK_PI"),
libc::FUTEX_LOCK_PI2 => FutexCommand::PriorityInheritance("FUTEX_LOCK_PI2"),
libc::FUTEX_UNLOCK_PI => FutexCommand::PriorityInheritance("FUTEX_UNLOCK_PI"),
libc::FUTEX_TRYLOCK_PI => FutexCommand::PriorityInheritance("FUTEX_TRYLOCK_PI"),
libc::FUTEX_WAIT_REQUEUE_PI => FutexCommand::PriorityInheritance("FUTEX_WAIT_REQUEUE_PI"),
libc::FUTEX_CMP_REQUEUE_PI => FutexCommand::PriorityInheritance("FUTEX_CMP_REQUEUE_PI"),
_ => FutexCommand::NoSys,
}
}
const POLLING_REFUSAL: &str = "--debug-futex-mode polling does not model futex waiter lists";
fn futex_val2(call: &syscalls::Futex) -> u32 {
call.timeout().map_or(0, Addr::as_raw) as u32
}
fn futex_uaddr2(call: &syscalls::Futex) -> usize {
call.uaddr2().map_or(0, AddrMut::as_raw)
}
fn futex_uaddr(call: &syscalls::Futex) -> usize {
call.uaddr().map_or(0, AddrMut::as_raw)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum KeyAccess {
Read,
Write,
}
fn user_ptr_max() -> usize {
static MAX: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
*MAX.get_or_init(|| {
let accepted = |address: usize| {
let ret = unsafe {
libc::syscall(
libc::SYS_futex,
address,
libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
0,
0,
0,
0,
)
};
ret >= 0
};
let (mut ok, mut bad) = (0usize, 1usize << 63);
while bad - ok > 4 {
let mid = (ok + (bad - ok) / 2) & !3;
if accepted(mid) { ok = mid } else { bad = mid }
}
ok
})
}
async fn futex_key<G: Guest<Detcore<T>>, T: RecordOrReplay>(
guest: &mut G,
address: usize,
private: bool,
access: KeyAccess,
) -> Result<FutexID, Error> {
if !address.is_multiple_of(std::mem::size_of::<u32>()) {
return Err(Error::Errno(Errno::EINVAL));
}
if address > user_ptr_max() {
return Err(Error::Errno(Errno::EFAULT));
}
if !private {
probe_shared_futex_key(guest, address, access).await?;
}
Ok(guest.thread_state().futex_id(address, private))
}
async fn probe_shared_futex_key<G: Guest<Detcore<T>>, T: RecordOrReplay>(
guest: &mut G,
address: usize,
access: KeyAccess,
) -> Result<(), Error> {
let probe = shared_futex_key_probe(address, access);
match run_shared_futex_key_probe(guest, probe, access, |guest, probe| guest.inject(probe)).await
{
SharedKeyProbe::Admitted => Ok(()),
SharedKeyProbe::Refused(errno) => Err(Error::Errno(errno)),
SharedKeyProbe::Interrupted(errno) => {
refuse_interrupted_shared_key_probe(guest, address, errno).await
}
}
}
fn shared_futex_key_probe(address: usize, access: KeyAccess) -> syscalls::Futex {
let word = AddrMut::<libc::c_int>::from_raw(address);
match access {
KeyAccess::Read => syscalls::Futex::new()
.with_uaddr(word)
.with_futex_op(libc::FUTEX_REQUEUE)
.with_val(0)
.with_uaddr2(word),
KeyAccess::Write => syscalls::Futex::new()
.with_uaddr(word)
.with_futex_op(libc::FUTEX_WAKE_OP)
.with_val(0)
.with_uaddr2(word)
.with_val3((7 << 28) | (1 << 12)),
}
}
type KeyProbeAnswer<'a> =
std::pin::Pin<Box<dyn std::future::Future<Output = Result<i64, Errno>> + Send + 'a>>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SharedKeyProbe {
Admitted,
Refused(Errno),
Interrupted(Errno),
}
async fn run_shared_futex_key_probe<C>(
context: &mut C,
probe: syscalls::Futex,
access: KeyAccess,
inject: for<'a> fn(&'a mut C, syscalls::Futex) -> KeyProbeAnswer<'a>,
) -> SharedKeyProbe {
match inject(context, probe).await {
Ok(_) => SharedKeyProbe::Admitted,
Err(errno @ (Errno::ERESTARTSYS | Errno::EINTR)) => SharedKeyProbe::Interrupted(errno),
Err(Errno::ENOSYS) if access == KeyAccess::Write => SharedKeyProbe::Admitted,
Err(errno) => SharedKeyProbe::Refused(errno),
}
}
async fn refuse_interrupted_shared_key_probe<G: Guest<Detcore<T>>, T: RecordOrReplay>(
guest: &mut G,
address: usize,
errno: Errno,
) -> ! {
{
use std::io::Write;
let _ = writeln!(
crate::util::RetryingStderr,
"hermit: a signal interrupted Detcore's shared futex key check for word {:#x} in thread {} ({}); \
refusing the run rather than retrying it, which could lose that signal \
(https://github.com/rrnewton/hermit/pull/4020)",
address,
guest.thread_state().dettid,
errno,
);
}
crate::tool_global::unrecoverable_shutdown(guest, detcore_model::HERMIT_POLICY_REFUSAL_EXIT)
.await
}
fn read_futex_word<G: Guest<Detcore<T>>, T: RecordOrReplay>(
guest: &mut G,
address: usize,
) -> Result<i32, Error> {
let word = Addr::<u8>::from_raw(address).ok_or(Errno::EFAULT)?;
let mut bytes = [0u8; 4];
if guest
.memory()
.read_exact_with_user_access(word, &mut bytes)
.is_ok()
{
return Ok(i32::from_ne_bytes(bytes));
}
if !bundled_signal_facts::host_task_reads_enabled(&guest.config().backend)
|| !mapping_is_writable(guest.pid().as_raw(), address)
{
return Err(Error::Errno(Errno::EFAULT));
}
let aligned = address & !(std::mem::size_of::<u64>() - 1);
let pair: u64 = guest
.memory()
.read_value(Addr::<u64>::from_raw(aligned).ok_or(Errno::EFAULT)?)
.map_err(|_| Error::Errno(Errno::EFAULT))?;
let offset = address - aligned;
let pair = pair.to_ne_bytes();
bytes.copy_from_slice(&pair[offset..offset + 4]);
Ok(i32::from_ne_bytes(bytes))
}
fn mapping_is_writable(pid: i32, address: usize) -> bool {
mapping_permissions(pid, address).is_some_and(|perms| perms[1] == b'w')
}
fn mapping_permissions(pid: i32, address: usize) -> Option<[u8; 4]> {
let maps = std::fs::read_to_string(format!("/proc/{pid}/maps")).ok()?;
maps.lines().find_map(|line| {
let mut fields = line.split_whitespace();
let (Some(range), Some(perms)) = (fields.next(), fields.next()) else {
return None;
};
let (start, end) = range.split_once('-')?;
let (Ok(start), Ok(end)) = (
usize::from_str_radix(start, 16),
usize::from_str_radix(end, 16),
) else {
return None;
};
if !(start..end).contains(&address) {
return None;
}
perms.as_bytes().get(..4)?.try_into().ok()
})
}
fn read_user_timespec<G: Guest<Detcore<T>>, T: RecordOrReplay>(
guest: &mut G,
address: usize,
) -> Result<Timespec, Error> {
let size = std::mem::size_of::<Timespec>();
let last = address.checked_add(size - 1).ok_or(Errno::EFAULT)?;
if last > user_ptr_max() {
return Err(Error::Errno(Errno::EFAULT));
}
if !bundled_signal_facts::host_task_reads_enabled(&guest.config().backend) {
let mut bytes = [0u8; std::mem::size_of::<Timespec>()];
guest
.memory()
.read_exact_with_user_access(
Addr::<u8>::from_raw(address).ok_or(Errno::EFAULT)?,
&mut bytes,
)
.map_err(|_| Error::Errno(Errno::EFAULT))?;
let field = |at: usize| -> i64 {
let mut raw = [0u8; 8];
raw.copy_from_slice(&bytes[at..at + 8]);
i64::from_ne_bytes(raw)
};
return Ok(Timespec {
tv_sec: field(0) as libc::time_t,
tv_nsec: field(8) as libc::c_long,
});
}
let page = 4096;
let pid = guest.pid().as_raw();
let mut cursor = address & !(page - 1);
while cursor <= last {
let probe = cursor.max(address);
let readable = mapping_permissions(pid, probe)
.is_some_and(|perms| perms[0] == b'r' || perms[1] == b'w');
if !readable {
return Err(Error::Errno(Errno::EFAULT));
}
cursor += page;
}
let start = address & !7;
let mut bytes = Vec::with_capacity(last + 1 - start);
let mut word = start;
while word <= last {
let value: u64 = guest
.memory()
.read_value(Addr::<u64>::from_raw(word).ok_or(Errno::EFAULT)?)
.map_err(|_| Error::Errno(Errno::EFAULT))?;
bytes.extend_from_slice(&value.to_ne_bytes());
word += 8;
}
let field = |at: usize| -> i64 {
let mut raw = [0u8; 8];
raw.copy_from_slice(&bytes[address - start + at..address - start + at + 8]);
i64::from_ne_bytes(raw)
};
Ok(Timespec {
tv_sec: field(0) as libc::time_t,
tv_nsec: field(8) as libc::c_long,
})
}
const FUTEX_OP_SET: u32 = 0;
const FUTEX_OP_ADD: u32 = 1;
const FUTEX_OP_OR: u32 = 2;
const FUTEX_OP_ANDN: u32 = 3;
const FUTEX_OP_XOR: u32 = 4;
const FUTEX_OP_OPARG_SHIFT: u32 = 8;
const FUTEX_OP_CMP_EQ: u32 = 0;
const FUTEX_OP_CMP_NE: u32 = 1;
const FUTEX_OP_CMP_LT: u32 = 2;
const FUTEX_OP_CMP_GE: u32 = 3;
const FUTEX_OP_CMP_LE: u32 = 4;
const FUTEX_OP_CMP_GT: u32 = 5;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct FutexWakeOp {
op: u32,
cmp: u32,
oparg: i32,
cmparg: i32,
}
impl FutexWakeOp {
pub(crate) fn decode(encoded: u32) -> Self {
let sign_extend_12 = |v: u32| ((v << 20) as i32) >> 20;
let op = (encoded & 0x7000_0000) >> 28;
let cmp = (encoded & 0x0f00_0000) >> 24;
let mut oparg = sign_extend_12((encoded & 0x00ff_f000) >> 12);
let cmparg = sign_extend_12(encoded & 0x0000_0fff);
if encoded & (FUTEX_OP_OPARG_SHIFT << 28) != 0 {
if !(0..=31).contains(&oparg) {
oparg &= 31;
}
oparg = 1i32.wrapping_shl(oparg as u32);
}
FutexWakeOp {
op,
cmp,
oparg,
cmparg,
}
}
pub(crate) fn has_known_operation(&self) -> bool {
self.apply(0).is_some()
}
pub(crate) fn apply(&self, old: i32) -> Option<i32> {
Some(match self.op {
FUTEX_OP_SET => self.oparg,
FUTEX_OP_ADD => old.wrapping_add(self.oparg),
FUTEX_OP_OR => old | self.oparg,
FUTEX_OP_ANDN => old & !self.oparg,
FUTEX_OP_XOR => old ^ self.oparg,
_ => return None,
})
}
pub(crate) fn compare(&self, old: i32) -> Option<bool> {
Some(match self.cmp {
FUTEX_OP_CMP_EQ => old == self.cmparg,
FUTEX_OP_CMP_NE => old != self.cmparg,
FUTEX_OP_CMP_LT => old < self.cmparg,
FUTEX_OP_CMP_GE => old >= self.cmparg,
FUTEX_OP_CMP_LE => old <= self.cmparg,
FUTEX_OP_CMP_GT => old > self.cmparg,
_ => return None,
})
}
}
#[cfg(test)]
mod tests {
#[test]
fn a_discarded_copy_is_left_out_even_when_committed() {
let chld = kernel_sigset_bit(libc::SIGCHLD);
let usr1 = kernel_sigset_bit(libc::SIGUSR1);
let state = KernelSignalState {
pending: chld | usr1,
thread_pending: chld,
shared_pending: usr1,
..Default::default()
};
assert_eq!(with_kept_copies(&state, chld, 0, chld | usr1, chld), usr1);
assert_eq!(with_kept_copies(&state, 0, usr1, chld | usr1, usr1), chld);
assert_eq!(with_kept_copies(&state, 0, 0, chld | usr1, 0), chld | usr1);
}
use std::io::Read;
use std::io::Seek;
use std::os::fd::AsRawFd;
use reverie::GlobalRPC;
use reverie::GlobalTool;
use reverie::Tid;
use reverie::Tool;
use super::*;
#[test]
fn owner_death_path_is_tied_to_the_backends_that_have_its_properties() {
let path = |backend: reverie::BackendCapabilities| {
let mut cfg = Config::default().with_backend(|capabilities| *capabilities = backend);
cfg.sequentialize_threads = true;
cfg.debug_futex_mode = BlockingMode::Precise;
owner_death_path(&cfg)
};
assert_eq!(
path(reverie::BackendCapabilities::PTRACE),
Some(OwnerDeathPath::Kernel)
);
assert_eq!(
path(reverie::BackendCapabilities::DBT),
Some(OwnerDeathPath::Barrier)
);
for backend in [
reverie::BackendCapabilities::KVM,
reverie::BackendCapabilities::SABRE,
reverie::BackendCapabilities::LITEINST_IN_GUEST,
] {
assert_eq!(path(backend), None, "{backend:?}");
}
}
#[test]
fn each_configuration_takes_its_sched_yield_route() {
let path = || Some(std::path::PathBuf::from("record.json"));
let cases = [
(Config::default(), SchedYieldRoute::EndTimeslice),
(
Config {
..Config::default()
}
.with_backend(|backend| {
backend.delivers_preemption_timer_events = true;
backend.runs_guest_code_between_injections = false;
}),
SchedYieldRoute::EndTimeslice,
),
(
Config {
chaos: true,
replay_schedule_from: path(),
..Config::default()
}
.with_backend(|backend| {
backend.delivers_preemption_timer_events = true;
backend.runs_guest_code_between_injections = false;
}),
SchedYieldRoute::WeakYield,
),
(
Config {
chaos: true,
..Config::default()
}
.with_backend(|backend| {
backend.delivers_preemption_timer_events = true;
backend.runs_guest_code_between_injections = false;
}),
SchedYieldRoute::ChaosYieldEpoch,
),
(
Config {
chaos: true,
sched_heuristic: SchedHeuristic::Random,
..Config::default()
}
.with_backend(|backend| {
backend.delivers_preemption_timer_events = true;
backend.runs_guest_code_between_injections = false;
}),
SchedYieldRoute::WeakYield,
),
(
Config {
chaos: true,
sched_heuristic: SchedHeuristic::StickyRandom,
..Config::default()
}
.with_backend(|backend| {
backend.delivers_preemption_timer_events = true;
backend.runs_guest_code_between_injections = false;
}),
SchedYieldRoute::ChaosYieldEpoch,
),
(
Config {
chaos: true,
sched_heuristic: SchedHeuristic::ConnectBind,
..Config::default()
}
.with_backend(|backend| {
backend.delivers_preemption_timer_events = true;
backend.runs_guest_code_between_injections = false;
}),
SchedYieldRoute::ChaosYieldEpoch,
),
(
Config {
chaos: true,
max_timeslice: None,
sched_heuristic: SchedHeuristic::Random,
..Config::default()
},
SchedYieldRoute::ChaosReprioritize,
),
(
Config {
chaos: true,
..Config::default()
}
.with_backend(|backend| backend.delivers_preemption_timer_events = false),
SchedYieldRoute::WeakYield,
),
(
Config {
chaos: true,
max_timeslice: None,
..Config::default()
},
SchedYieldRoute::ChaosReprioritize,
),
(
Config {
chaos: true,
replay_preemptions_from: path(),
..Config::default()
}
.with_backend(|backend| {
backend.delivers_preemption_timer_events = true;
backend.runs_guest_code_between_injections = false;
}),
SchedYieldRoute::ChaosYieldEpoch,
),
(
Config {
chaos: true,
replay_schedule_from: path(),
..Config::default()
},
SchedYieldRoute::WeakYield,
),
(
Config {
replay_schedule_from: path(),
..Config::default()
},
SchedYieldRoute::WeakYield,
),
(
Config {
replay_preemptions_from: path(),
..Config::default()
},
SchedYieldRoute::ReplayStrongYield,
),
];
for (cfg, route) in cases {
assert_eq!(
sched_yield_route(&cfg),
route,
"chaos={} timer delivered={} heuristic={:?} max_timeslice={:?} preemptions={:?} schedule={:?}",
cfg.chaos,
cfg.backend.delivers_preemption_timer_events,
cfg.sched_heuristic,
cfg.max_timeslice,
cfg.replay_preemptions_from,
cfg.replay_schedule_from
);
}
}
#[test]
fn a_thread_reported_under_another_pid_is_read_and_alive() {
let tid = Pid::from_raw(unsafe { libc::syscall(libc::SYS_gettid) } as i32);
let wrong = Pid::from_raw(unsafe { libc::getppid() });
assert_ne!(wrong, tid);
assert!(read_kernel_signal_state(wrong, tid).is_err());
assert!(read_thread_signal_state(wrong, tid).is_ok());
assert!(!thread_is_gone(wrong, tid));
assert!(read_thread_wait_signal_state(wrong, tid).is_ok());
}
#[test]
fn host_signal_state_reads_are_refused_only_where_guest_tids_are_not_host_tasks() {
assert!(refuses_host_signal_state_reads(&BackendCapabilities::KVM));
for backend in [
BackendCapabilities::PTRACE,
BackendCapabilities::E9PATCH,
BackendCapabilities::SABRE,
BackendCapabilities::DBT,
BackendCapabilities::LITEINST_IN_GUEST,
] {
assert!(!refuses_host_signal_state_reads(&backend));
}
}
#[test]
fn a_latched_refusal_never_reads_a_host_task_signal_state() {
let pid = Pid::from_raw(unsafe { libc::getpid() });
let tid = Pid::from_raw(unsafe { libc::syscall(libc::SYS_gettid) } as i32);
refuse_host_signal_state_reads(&BackendCapabilities::PTRACE);
assert!(!host_signal_state_reads_refused());
assert!(read_kernel_signal_state(pid, tid).is_ok());
refuse_host_signal_state_reads(&BackendCapabilities::KVM);
assert!(host_signal_state_reads_refused());
assert_eq!(read_kernel_signal_state(pid, tid), Err(Errno::ENOSYS));
assert_eq!(read_thread_signal_state(pid, tid), Err(Errno::ENOSYS));
}
use crate::config::Config;
use crate::tool_global::GlobalRequest;
use crate::tool_global::GlobalState;
use crate::types::MmId;
#[test]
fn a_child_exit_signal_other_than_sigchld_is_host_timed_for_its_creator() {
let usr1 = 1_u64 << (libc::SIGUSR1 - 1);
let none = CloneFlags::empty();
assert_eq!(clone_host_timed_signals(none, libc::SIGUSR1), usr1);
assert_eq!(
clone_host_timed_signals(CloneFlags::CLONE_VFORK, libc::SIGUSR1),
usr1
);
assert_eq!(clone_host_timed_signals(none, libc::SIGCHLD), 0);
assert_eq!(clone_host_timed_signals(none, 0), 0);
assert_eq!(clone_host_timed_signals(none, 65), 0);
let thread = CloneFlags::CLONE_THREAD | CloneFlags::CLONE_SIGHAND | CloneFlags::CLONE_VM;
assert_eq!(clone_host_timed_signals(thread, libc::SIGUSR1), 0);
assert_eq!(
clone_host_timed_signals(CloneFlags::CLONE_PARENT, libc::SIGUSR1),
0
);
}
struct FailedExecStack;
struct FailedExecStackGuard;
impl Drop for FailedExecStackGuard {
fn drop(&mut self) {}
}
impl reverie::Stack for FailedExecStack {
type StackGuard = FailedExecStackGuard;
fn size(&self) -> usize {
panic!("failed exec must not use the guest stack")
}
fn capacity(&self) -> usize {
panic!("failed exec must not use the guest stack")
}
fn push<'stack, T>(&mut self, _: T) -> Addr<'stack, T> {
panic!("failed exec must not use the guest stack")
}
fn reserve<'stack, T>(&mut self) -> AddrMut<'stack, T> {
panic!("failed exec must not use the guest stack")
}
fn commit(self) -> Result<Self::StackGuard, Errno> {
panic!("failed exec must not use the guest stack")
}
}
struct FailedExecGuest<'a> {
config: &'a Config,
global: &'a GlobalState,
thread: crate::ThreadState<()>,
sender: Tid,
process: Tid,
old_mm: MmId,
errno: Errno,
backend_refuses: bool,
refusal: Option<reverie::UnsupportedRefusal>,
refusal_takes: usize,
injections: usize,
requests: Mutex<Vec<GlobalRequest>>,
}
#[reverie::tool]
impl GlobalRPC<GlobalState> for FailedExecGuest<'_> {
async fn send_rpc(
&self,
message: <GlobalState as GlobalTool>::Request,
) -> <GlobalState as GlobalTool>::Response {
assert_eq!(message.1, self.old_mm, "RPC follows restored exec identity");
self.requests.lock().unwrap().push(message.2.clone());
self.global.receive_rpc(self.sender, message).await
}
fn config(&self) -> &Config {
self.config
}
}
#[reverie::tool]
impl Guest<Detcore> for FailedExecGuest<'_> {
type Memory = reverie::syscalls::LocalMemory;
type Stack = FailedExecStack;
fn tid(&self) -> Tid {
self.sender
}
fn pid(&self) -> Tid {
self.process
}
fn ppid(&self) -> Option<Tid> {
None
}
fn memory(&self) -> Self::Memory {
panic!("failed exec rollback must not read guest memory")
}
fn thread_state(&self) -> &crate::ThreadState<()> {
&self.thread
}
fn thread_state_mut(&mut self) -> &mut crate::ThreadState<()> {
&mut self.thread
}
async fn regs(&mut self) -> libc::user_regs_struct {
panic!("failed exec rollback must not read registers")
}
async fn stack(&mut self) -> Self::Stack {
panic!("failed exec rollback must not use a guest stack")
}
async fn daemonize(&mut self) {
panic!("failed exec rollback must not daemonize")
}
async fn inject<S: SyscallInfo>(&mut self, call: S) -> Result<i64, Errno> {
assert_eq!(call.number(), syscalls::Sysno::execveat);
assert_eq!(
self.thread.mm_id,
self.old_mm.for_exec(self.thread.detpid.unwrap())
);
assert_eq!(self.thread.robust_list_head, None);
self.injections += 1;
self.refusal = self.backend_refuses.then(|| {
reverie::UnsupportedRefusal::new(
reverie::UnsupportedOperation::NonLeaderExec,
self.errno,
"KVM nonleader exec is unsupported",
)
});
Err(self.errno)
}
fn take_unsupported_refusal(&mut self) -> Option<reverie::UnsupportedRefusal> {
assert_eq!(
self.thread.mm_id,
self.old_mm.for_exec(self.thread.detpid.unwrap()),
"the refusal is taken before the failed exec is rolled back"
);
self.refusal_takes += 1;
self.refusal.take()
}
async fn tail_inject<S: SyscallInfo>(&mut self, _: S) -> reverie::Never {
panic!("failed exec must not retire a live guest")
}
fn set_timer(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
panic!("failed exec rollback must not replace the timer")
}
fn set_timer_precise(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
panic!("failed exec rollback must not replace the timer")
}
fn read_clock(&mut self) -> Result<u64, Error> {
panic!("failed exec rollback must not sample the clock")
}
}
#[tokio::test]
async fn backend_exec_refusal_preserves_failed_exec_rollback_and_policy() {
let process = DetPid::from_raw(17);
let worker = DetTid::from_raw(18);
for (kvm_behaviours, caller, errno, backend_refuses, fail_closed, refused, reported) in [
(true, worker, Errno::ENOSYS, true, true, true, false),
(true, worker, Errno::ENOSYS, true, false, false, true),
(true, worker, Errno::ENOENT, false, true, false, false),
(true, worker, Errno::ENOEXEC, false, true, false, false),
(true, worker, Errno::EFAULT, false, true, false, false),
(true, worker, Errno::EACCES, false, true, false, false),
(true, worker, Errno::EOPNOTSUPP, false, true, false, false),
(true, process, Errno::ENOSYS, false, true, false, false),
(false, worker, Errno::ENOSYS, false, true, false, false),
(true, worker, Errno::ENOSYS, false, true, false, false),
] {
let mut report = tempfile::tempfile().unwrap();
let config = Config {
sequentialize_threads: false,
panic_on_unsupported_syscalls: fail_closed,
exit_on_unsupported_syscall: true,
shutdown_on_unsupported_syscall: false,
unsupported_syscall_report_fd: Some(report.as_raw_fd()),
..Config::default().with_backend(|backend| {
if kvm_behaviours {
backend.provides_process_signal_control = true;
backend.emulates_child_waits = true;
}
})
};
let global = GlobalState::init_global_state(&config).await;
let tool = Detcore::new(Tid::from_raw(process.as_raw()), &config);
let mut thread = crate::ThreadState::new(caller, &config, ());
thread.detpid = Some(process);
thread.mm_id = MmId::initial(process);
thread.record_robust_list_head(Some(0x12340));
thread.thread_logical_time.add_syscall_with_cost(123);
let old_time = thread.thread_logical_time.as_nanos();
let old_files = Arc::clone(&thread.file_metadata);
let old_memory = Arc::clone(&thread.memory_metadata);
let old_mm = thread.mm_id;
let mut guest = FailedExecGuest {
config: &config,
global: &global,
thread,
sender: Tid::from_raw(caller.as_raw()),
process: Tid::from_raw(process.as_raw()),
old_mm,
errno,
backend_refuses,
refusal: None,
refusal_takes: 0,
injections: 0,
requests: Mutex::new(Vec::new()),
};
let result = tool
.handle_execveat(&mut guest, syscalls::Execveat::new())
.await;
match result {
Err(Error::Tool(error)) if refused => assert_eq!(
error
.downcast_ref::<crate::UnsupportedSyscallError>()
.unwrap()
.0,
syscalls::Sysno::execveat
),
Err(Error::Errno(actual)) if !refused => assert_eq!(actual, errno),
result => panic!("wrong failed-exec policy: {result:?}"),
}
assert_eq!(guest.injections, 1, "backend preflight must run first");
assert_eq!(guest.refusal_takes, 1, "the refusal must be asked for once");
assert_eq!(guest.refusal, None, "a reported refusal must be taken");
assert_eq!(guest.thread.dettid, caller);
assert_eq!(guest.thread.detpid, Some(process));
assert_eq!(guest.thread.mm_id, old_mm);
assert_eq!(guest.thread.robust_list_head, Some(0x12340));
assert_eq!(guest.thread.thread_logical_time.as_nanos(), old_time);
assert!(Arc::ptr_eq(&guest.thread.file_metadata, &old_files));
assert!(Arc::ptr_eq(&guest.thread.memory_metadata, &old_memory));
let requests = guest.requests.lock().unwrap();
assert!(matches!(requests[0], GlobalRequest::PrepareExec(..)));
assert!(matches!(requests[1], GlobalRequest::CancelExec(..)));
assert_eq!(requests.len(), if reported { 3 } else { 2 });
if reported {
assert!(
matches!(&requests[2], GlobalRequest::ReportUnsupportedSyscall(name) if name == "execveat")
);
}
report.rewind().unwrap();
let mut aggregate = String::new();
report.read_to_string(&mut aggregate).unwrap();
assert_eq!(aggregate, if reported { "execveat\n" } else { "" });
}
}
struct FailedCloneGuest<'a> {
config: &'a Config,
global: &'a GlobalState,
thread: crate::ThreadState<()>,
sender: Tid,
errno: Errno,
backend_refuses: bool,
refusal: Option<reverie::UnsupportedRefusal>,
refusal_takes: usize,
injections: usize,
requests: Mutex<Vec<GlobalRequest>>,
}
#[reverie::tool]
impl GlobalRPC<GlobalState> for FailedCloneGuest<'_> {
async fn send_rpc(
&self,
message: <GlobalState as GlobalTool>::Request,
) -> <GlobalState as GlobalTool>::Response {
self.requests.lock().unwrap().push(message.2.clone());
self.global.receive_rpc(self.sender, message).await
}
fn config(&self) -> &Config {
self.config
}
}
#[reverie::tool]
impl Guest<Detcore> for FailedCloneGuest<'_> {
type Memory = reverie::syscalls::LocalMemory;
type Stack = FailedExecStack;
fn tid(&self) -> Tid {
self.sender
}
fn pid(&self) -> Tid {
self.sender
}
fn ppid(&self) -> Option<Tid> {
None
}
fn memory(&self) -> Self::Memory {
reverie::syscalls::LocalMemory::new()
}
fn thread_state(&self) -> &crate::ThreadState<()> {
&self.thread
}
fn thread_state_mut(&mut self) -> &mut crate::ThreadState<()> {
&mut self.thread
}
async fn regs(&mut self) -> libc::user_regs_struct {
panic!("a failed clone must not read registers")
}
async fn stack(&mut self) -> Self::Stack {
panic!("a failed clone must not use a guest stack")
}
async fn daemonize(&mut self) {
panic!("a failed clone must not daemonize")
}
async fn inject<S: SyscallInfo>(&mut self, call: S) -> Result<i64, Errno> {
assert_eq!(call.number(), syscalls::Sysno::clone);
self.injections += 1;
self.refusal = self.backend_refuses.then(|| {
reverie::UnsupportedRefusal::new(
reverie::UnsupportedOperation::NativeExec,
self.errno,
"this backend cannot create a thread",
)
});
Err(self.errno)
}
fn take_unsupported_refusal(&mut self) -> Option<reverie::UnsupportedRefusal> {
self.refusal_takes += 1;
self.refusal.take()
}
async fn tail_inject<S: SyscallInfo>(&mut self, _: S) -> reverie::Never {
panic!("a failed clone must not retire a live guest")
}
fn set_timer(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
panic!("a failed clone must not replace the timer")
}
fn set_timer_precise(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
panic!("a failed clone must not replace the timer")
}
fn read_clock(&mut self) -> Result<u64, Error> {
panic!("a failed clone must not sample the clock")
}
}
#[tokio::test]
async fn backend_clone_refusal_follows_the_unsupported_operation_policy() {
let process = DetPid::from_raw(23);
for (errno, backend_refuses, fail_closed, refused, reported) in [
(Errno::EOPNOTSUPP, true, true, true, false),
(Errno::EOPNOTSUPP, true, false, false, true),
(Errno::EAGAIN, false, true, false, false),
(Errno::EOPNOTSUPP, false, true, false, false),
] {
let mut report = tempfile::tempfile().unwrap();
let config = Config {
sequentialize_threads: false,
panic_on_unsupported_syscalls: fail_closed,
exit_on_unsupported_syscall: true,
shutdown_on_unsupported_syscall: false,
unsupported_syscall_report_fd: Some(report.as_raw_fd()),
..Config::default()
};
let global = GlobalState::init_global_state(&config).await;
let tool = Detcore::new(Tid::from_raw(process.as_raw()), &config);
let mut thread = crate::ThreadState::new(process, &config, ());
thread.detpid = Some(process);
let mut guest = FailedCloneGuest {
config: &config,
global: &global,
thread,
sender: Tid::from_raw(process.as_raw()),
errno,
backend_refuses,
refusal: None,
refusal_takes: 0,
injections: 0,
requests: Mutex::new(Vec::new()),
};
let flags = (libc::CLONE_VM
| libc::CLONE_FS
| libc::CLONE_FILES
| libc::CLONE_SIGHAND
| libc::CLONE_THREAD
| libc::CLONE_SYSVSEM) as u64;
let clone = syscalls::Clone::new()
.with_flags(nix::sched::CloneFlags::from_bits_truncate(flags as _));
let result = tool
.handle_clone_family(&mut guest, syscalls::family::CloneFamily::Clone(clone))
.await;
match result {
Err(Error::Tool(error)) if refused => assert_eq!(
error
.downcast_ref::<crate::UnsupportedSyscallError>()
.unwrap()
.0,
syscalls::Sysno::clone
),
Err(Error::Errno(actual)) if !refused => assert_eq!(actual, errno),
result => panic!("wrong failed-clone policy: {result:?}"),
}
assert_eq!(guest.injections, 1);
assert_eq!(guest.refusal_takes, 1, "the refusal must be asked for once");
assert_eq!(guest.refusal, None, "a reported refusal must be taken");
assert_eq!(guest.thread.clone_flags, None);
let requests = guest.requests.lock().unwrap();
assert_eq!(
requests
.iter()
.filter(|request| matches!(request, GlobalRequest::ReportUnsupportedSyscall(name) if name == "clone"))
.count(),
usize::from(reported)
);
report.rewind().unwrap();
let mut aggregate = String::new();
report.read_to_string(&mut aggregate).unwrap();
assert_eq!(aggregate, if reported { "clone\n" } else { "" });
}
}
fn bits(signals: &[i32]) -> KernelSigset {
signals
.iter()
.fold(0, |set, &signal| set | kernel_sigset_bit(signal))
}
#[test]
fn a_precise_futex_wait_holds_a_committed_signal_only_if_a_host_timed_source_can_post_it() {
let chld = kernel_sigset_bit(libc::SIGCHLD);
let usr1 = kernel_sigset_bit(libc::SIGUSR1);
assert_eq!(futex_wait_held_after_wake(0, 0), chld);
assert_eq!(futex_wait_held_after_wake(usr1, 0), chld | usr1);
assert_eq!(futex_wait_held_after_wake(0, chld), 0);
assert_eq!(futex_wait_held_after_wake(usr1, chld), usr1);
assert_eq!(futex_wait_held_after_wake(chld, chld), chld);
assert_eq!(futex_wait_held_after_wake(usr1, usr1 | chld), usr1);
}
#[test]
fn kernel_signal_state_parses_proc_status() {
let status = "Name:\tguest\nSigQ:\t1/1024\nSigPnd:\t0000000000000200\n\
ShdPnd:\t0000000000000400\nSigBlk:\t0000000000000800\n\
SigIgn:\t0000000000001000\nSigCgt:\t0000000000000400\n";
assert_eq!(
KernelSignalState::parse(status),
Some(KernelSignalState {
pending: bits(&[libc::SIGUSR1, libc::SIGSEGV]),
thread_pending: bits(&[libc::SIGUSR1]),
shared_pending: bits(&[libc::SIGSEGV]),
blocked: bits(&[libc::SIGUSR2]),
ignored: bits(&[libc::SIGPIPE]),
caught: bits(&[libc::SIGSEGV]),
})
);
assert_eq!(
KernelSignalState::parse("SigPnd:\t0\nShdPnd:\t0\nSigBlk:\t0\nSigIgn:\t0\n"),
None
);
assert_eq!(
KernelSignalState::parse(
"SigPnd:\tzz\nShdPnd:\t0\nSigBlk:\t0\nSigIgn:\t0\nSigCgt:\t0\n"
),
None
);
}
#[test]
fn only_unblocked_caught_or_fatal_signals_interrupt_a_wait() {
let state = KernelSignalState {
pending: bits(&[
libc::SIGUSR1,
libc::SIGUSR2,
libc::SIGPIPE,
libc::SIGTERM,
libc::SIGCHLD,
libc::SIGWINCH,
libc::SIGURG,
libc::SIGCONT,
]),
blocked: bits(&[libc::SIGUSR2]),
ignored: bits(&[libc::SIGPIPE]),
caught: bits(&[libc::SIGUSR1, libc::SIGUSR2, libc::SIGWINCH]),
..Default::default()
};
assert_eq!(
state.pending_interrupting(state.blocked, false),
bits(&[libc::SIGUSR1, libc::SIGWINCH, libc::SIGTERM])
);
assert_eq!(
state.pending_interrupting(0, false),
bits(&[libc::SIGUSR1, libc::SIGUSR2, libc::SIGWINCH, libc::SIGTERM])
);
let interrupting = state.interrupting(state.blocked);
assert_ne!(interrupting & kernel_sigset_bit(libc::SIGKILL), 0);
assert_ne!(interrupting & kernel_sigset_bit(libc::SIGSTOP), 0);
assert_eq!(
interrupting & kernel_sigset_bit(reverie::PERF_EVENT_SIGNAL as i32),
0
);
assert_eq!(kernel_sigset_bit(0), 0);
assert_eq!(kernel_sigset_bit(65), 0);
}
#[test]
fn a_rearming_wait_is_not_ended_by_a_default_job_control_stop() {
let stops = bits(&[libc::SIGTSTP, libc::SIGTTIN, libc::SIGTTOU]);
let state = KernelSignalState {
pending: stops | bits(&[libc::SIGSTOP, libc::SIGTERM]),
..Default::default()
};
assert_eq!(state.default_job_control_stops(), stops);
assert_eq!(state.pending_interrupting(0, false), state.pending);
assert_eq!(
state.pending_interrupting(0, true),
bits(&[libc::SIGSTOP, libc::SIGTERM])
);
let rearming = state.interrupting_wait(0, true);
assert_eq!(rearming & stops, 0);
assert_eq!(rearming, state.interrupting(0) & !stops);
let state = KernelSignalState {
pending: stops,
caught: bits(&[libc::SIGTSTP]),
ignored: bits(&[libc::SIGTTIN]),
..Default::default()
};
assert_eq!(state.default_job_control_stops(), bits(&[libc::SIGTTOU]));
assert_eq!(state.pending_interrupting(0, true), bits(&[libc::SIGTSTP]));
assert_eq!(
state.pending_interrupting(0, false),
bits(&[libc::SIGTSTP, libc::SIGTTOU])
);
assert_eq!(state.pending_interrupting(bits(&[libc::SIGTSTP]), true), 0);
}
#[test]
fn kernel_blocked_mask_preserves_libc_signal_membership() {
let mask = blocked_signal_mask();
let mut libc_mask: libc::sigset_t = unsafe { std::mem::zeroed() };
unsafe {
libc::sigfillset(&mut libc_mask);
libc::sigdelset(&mut libc_mask, reverie::PERF_EVENT_SIGNAL as i32);
}
for raw_signal in 1..=KernelSigset::BITS as i32 {
assert_eq!(
signal_is_blocked(&mask, SigWrapper(raw_signal)),
unsafe { libc::sigismember(&libc_mask, raw_signal) == 1 },
"signal {raw_signal} membership changed while converting to the kernel ABI"
);
}
}
#[test]
fn clone_without_child_cleartid_does_not_register_the_pointer() {
assert_eq!(
child_tid_clear_address(CloneFlags::CLONE_CHILD_SETTID, 0x1234),
0
);
}
#[test]
fn clone_with_child_cleartid_registers_the_pointer() {
assert_eq!(
child_tid_clear_address(CloneFlags::CLONE_CHILD_CLEARTID, 0x1234),
0x1234
);
}
#[test]
fn linux_default_dispositions_that_do_not_interrupt_child_waits() {
for signal in [libc::SIGCHLD, libc::SIGCONT, libc::SIGURG, libc::SIGWINCH] {
assert!(signal_default_disposition_does_not_interrupt_child_wait(
SigWrapper(signal)
));
}
for signal in [libc::SIGALRM, libc::SIGSTOP, libc::SIGUSR1] {
assert!(!signal_default_disposition_does_not_interrupt_child_wait(
SigWrapper(signal)
));
}
}
#[test]
fn default_actions_that_end_the_process() {
for signal in [
libc::SIGTERM,
libc::SIGINT,
libc::SIGHUP,
libc::SIGQUIT,
libc::SIGABRT,
libc::SIGSEGV,
libc::SIGBUS,
libc::SIGFPE,
libc::SIGILL,
libc::SIGPIPE,
libc::SIGALRM,
libc::SIGUSR1,
libc::SIGUSR2,
libc::SIGRTMIN(),
libc::SIGRTMAX(),
] {
assert!(
signal_default_action_ends_the_process(SigWrapper(signal)),
"{signal}"
);
}
for signal in [
libc::SIGCHLD,
libc::SIGCONT,
libc::SIGURG,
libc::SIGWINCH,
libc::SIGSTOP,
libc::SIGTSTP,
libc::SIGTTIN,
libc::SIGTTOU,
libc::SIGKILL,
] {
assert!(
!signal_default_action_ends_the_process(SigWrapper(signal)),
"{signal}"
);
}
}
#[test]
fn uncatchable_signals_do_not_require_a_sigaction_query() {
assert!(signal_has_uncatchable_default_disposition(SigWrapper(
libc::SIGKILL
)));
assert!(signal_has_uncatchable_default_disposition(SigWrapper(
libc::SIGSTOP
)));
assert!(!signal_has_uncatchable_default_disposition(SigWrapper(
libc::SIGUSR1
)));
}
#[test]
fn waitid_ready_child_wins_when_scheduler_also_reports_a_signal() {
assert_eq!(
exact_wait_poll_decision(true, true, Some(ExactChildWaitState::Running)),
ExactWaitPollDecision::ChildReady
);
assert_eq!(
exact_wait_poll_decision(false, true, Some(ExactChildWaitState::LogicallyExited)),
ExactWaitPollDecision::ReapAfterLogicalExit
);
assert_eq!(
exact_wait_poll_decision(false, true, Some(ExactChildWaitState::PhysicalExitPending)),
ExactWaitPollDecision::AwaitPhysicalExit
);
assert_eq!(
exact_wait_poll_decision(false, true, Some(ExactChildWaitState::Running)),
ExactWaitPollDecision::Interrupted
);
assert_eq!(
exact_wait_poll_decision(false, false, Some(ExactChildWaitState::Running)),
ExactWaitPollDecision::Retry
);
}
#[test]
fn wait4_argument_validation_follows_linux_precedence() {
let valid_bits = [0, 1, 3, 29, 30, 31];
for bit in valid_bits {
let options = WaitPidFlag::from_bits_retain((1_u32 << bit) as libc::c_int);
assert_eq!(validate_wait4_arguments(-1, options), Ok(()), "bit {bit}");
}
for bit in (0..u32::BITS).filter(|bit| !valid_bits.contains(bit)) {
let options = WaitPidFlag::from_bits_retain((1_u32 << bit) as libc::c_int);
assert_eq!(
validate_wait4_arguments(-1, options),
Err(Errno::EINVAL),
"bit {bit}"
);
}
for pid in [libc::pid_t::MIN + 1, -2, 0, 1, libc::pid_t::MAX] {
assert_eq!(
validate_wait4_arguments(pid, WaitPidFlag::empty()),
Ok(()),
"pid {pid}"
);
}
assert_eq!(
validate_wait4_arguments(libc::pid_t::MIN, WaitPidFlag::empty()),
Err(Errno::ESRCH)
);
assert_eq!(
validate_wait4_arguments(libc::pid_t::MIN, WaitPidFlag::from_bits_retain(0x10)),
Err(Errno::EINVAL),
"invalid options must win over the INT_MIN selector"
);
}
#[test]
fn stale_any_child_preserves_interrupt_until_no_ready_child_remains() {
let next_child = DetPid::from_raw(200);
assert!(
!stale_any_wait_must_interrupt(true, Some(next_child)),
"another ready child must retain child-ready precedence"
);
assert!(
stale_any_wait_must_interrupt(true, None),
"a pending signal must interrupt before the wait parks again"
);
assert!(!stale_any_wait_must_interrupt(false, None));
}
#[test]
fn ioprio_query_reports_fixed_raw_and_effective_defaults() {
assert_eq!(virtual_ioprio(IOPRIO_WHO_PROCESS), Ok(0));
assert_eq!(
virtual_ioprio(IOPRIO_WHO_PGRP),
Ok(i64::from(IOPRIO_DEFAULT_EFFECTIVE))
);
assert_eq!(
virtual_ioprio(IOPRIO_WHO_USER),
Ok(i64::from(IOPRIO_DEFAULT_EFFECTIVE))
);
assert_eq!(virtual_ioprio(0), Err(Errno::EINVAL));
assert_eq!(virtual_ioprio(4), Err(Errno::EINVAL));
}
#[test]
fn waitid_siginfo_canonicalization_clears_only_cpu_accounting() {
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
info.si_signo = libc::SIGCHLD;
info.si_code = libc::CLD_EXITED;
let fields = unsafe {
&mut (*(std::ptr::addr_of_mut!(info)).cast::<WaitidSiginfoHead>())
.fields
.sigchld
};
fields.pid = 123;
fields.uid = 456;
fields.status = 7;
fields.utime = 8;
fields.stime = 9;
canonicalize_waitid_siginfo(&mut info);
assert_eq!(info.si_signo, libc::SIGCHLD);
assert_eq!(info.si_code, libc::CLD_EXITED);
assert_eq!(unsafe { info.si_pid() }, 123);
assert_eq!(unsafe { info.si_uid() }, 456);
assert_eq!(unsafe { info.si_status() }, 7);
assert_eq!(unsafe { info.si_utime() }, 0);
assert_eq!(unsafe { info.si_stime() }, 0);
}
#[test]
fn wait_status_rollup_only_accepts_process_termination() {
assert!(wait_status_is_termination(0));
assert!(wait_status_is_termination(libc::SIGTERM));
assert!(!wait_status_is_termination((libc::SIGSTOP << 8) | 0x7f));
assert!(!wait_status_is_termination(0xffff));
assert!(waitid_code_is_termination(libc::CLD_EXITED));
assert!(waitid_code_is_termination(libc::CLD_KILLED));
assert!(waitid_code_is_termination(libc::CLD_DUMPED));
assert!(!waitid_code_is_termination(libc::CLD_STOPPED));
assert!(!waitid_code_is_termination(libc::CLD_CONTINUED));
assert!(!waitid_code_is_termination(libc::CLD_TRAPPED));
}
#[test]
fn futex_commands_are_classified_as_linux_dispatches_them() {
use FutexCommand::*;
let private = libc::FUTEX_PRIVATE_FLAG;
let realtime = libc::FUTEX_CLOCK_REALTIME;
for cmd in [
libc::FUTEX_WAIT,
libc::FUTEX_WAIT_BITSET,
libc::FUTEX_WAKE,
libc::FUTEX_WAKE_BITSET,
libc::FUTEX_REQUEUE,
libc::FUTEX_CMP_REQUEUE,
libc::FUTEX_WAKE_OP,
] {
assert_eq!(classify_futex_command(cmd), Served, "cmd {cmd}");
assert_eq!(classify_futex_command(cmd | private), Served, "cmd {cmd}");
}
assert_eq!(
classify_futex_command(libc::FUTEX_WAIT_BITSET | realtime),
Served
);
for cmd in [
libc::FUTEX_WAIT,
libc::FUTEX_WAKE,
libc::FUTEX_REQUEUE,
libc::FUTEX_WAKE_OP,
] {
assert_eq!(classify_futex_command(cmd | realtime), NoSys, "cmd {cmd}");
}
for cmd in [libc::FUTEX_FD, 14, 99, libc::FUTEX_CMD_MASK] {
assert_eq!(classify_futex_command(cmd), NoSys, "cmd {cmd}");
}
assert_eq!(
classify_futex_command(libc::FUTEX_LOCK_PI | private),
PriorityInheritance("FUTEX_LOCK_PI")
);
assert_eq!(
classify_futex_command(libc::FUTEX_LOCK_PI2 | realtime),
PriorityInheritance("FUTEX_LOCK_PI2")
);
assert_eq!(
classify_futex_command(libc::FUTEX_LOCK_PI | realtime),
NoSys
);
for (cmd, name) in [
(libc::FUTEX_UNLOCK_PI, "FUTEX_UNLOCK_PI"),
(libc::FUTEX_TRYLOCK_PI, "FUTEX_TRYLOCK_PI"),
(libc::FUTEX_WAIT_REQUEUE_PI, "FUTEX_WAIT_REQUEUE_PI"),
(libc::FUTEX_CMP_REQUEUE_PI, "FUTEX_CMP_REQUEUE_PI"),
] {
assert_eq!(classify_futex_command(cmd), PriorityInheritance(name));
}
}
type ProbeArgs = (i32, usize, usize, i32, i32);
struct ScriptedProbes {
answers: std::collections::VecDeque<Result<i64, Errno>>,
probes: Vec<ProbeArgs>,
}
impl ScriptedProbes {
fn inject(&mut self, probe: syscalls::Futex) -> KeyProbeAnswer<'_> {
self.probes.push((
probe.futex_op(),
probe.uaddr().map_or(0, AddrMut::as_raw),
probe.uaddr2().map_or(0, AddrMut::as_raw),
probe.val(),
probe.val3(),
));
let answer = self
.answers
.pop_front()
.expect("the probe was injected more often than scripted");
Box::pin(async move { answer })
}
}
fn run_scripted(
access: KeyAccess,
answer: Result<i64, Errno>,
) -> (SharedKeyProbe, Vec<ProbeArgs>) {
let mut guest = ScriptedProbes {
answers: [answer].into_iter().collect(),
probes: Vec::new(),
};
let result = futures::executor::block_on(run_shared_futex_key_probe(
&mut guest,
shared_futex_key_probe(0x1000, access),
access,
|guest, probe| guest.inject(probe),
));
(result, guest.probes)
}
#[test]
fn an_interrupted_shared_key_probe_is_never_retried_or_taken_as_an_answer() {
let read = (libc::FUTEX_REQUEUE, 0x1000, 0x1000, 0, 0);
let write = (
libc::FUTEX_WAKE_OP,
0x1000,
0x1000,
0,
(7 << 28) | (1 << 12),
);
for (access, args) in [(KeyAccess::Read, read), (KeyAccess::Write, write)] {
for errno in [Errno::ERESTARTSYS, Errno::EINTR] {
assert_eq!(
run_scripted(access, Err(errno)),
(SharedKeyProbe::Interrupted(errno), vec![args]),
"{access:?} {errno}"
);
}
assert_eq!(
run_scripted(access, Ok(0)),
(SharedKeyProbe::Admitted, vec![args])
);
assert_eq!(
run_scripted(access, Err(Errno::EFAULT)),
(SharedKeyProbe::Refused(Errno::EFAULT), vec![args])
);
}
assert_eq!(
run_scripted(KeyAccess::Read, Err(Errno::ENOSYS)),
(SharedKeyProbe::Refused(Errno::ENOSYS), vec![read])
);
assert_eq!(
run_scripted(KeyAccess::Write, Err(Errno::ENOSYS)),
(SharedKeyProbe::Admitted, vec![write])
);
}
#[test]
fn futex_wake_op_decodes_and_applies_as_linux() {
let encode = |op: u32, cmp: u32, oparg: u32, cmparg: u32| {
((op & 0xf) << 28) | ((cmp & 0xf) << 24) | ((oparg & 0xfff) << 12) | (cmparg & 0xfff)
};
let set5_eq0 = FutexWakeOp::decode(encode(FUTEX_OP_SET, FUTEX_OP_CMP_EQ, 5, 0));
assert_eq!(set5_eq0.apply(0), Some(5));
assert_eq!(set5_eq0.compare(0), Some(true));
assert_eq!(set5_eq0.compare(1), Some(false));
let add_minus1 = FutexWakeOp::decode(encode(FUTEX_OP_ADD, FUTEX_OP_CMP_LT, 0xfff, 0x800));
assert_eq!(add_minus1.apply(10), Some(9));
assert_eq!(add_minus1.apply(i32::MIN), Some(i32::MAX));
assert_eq!(add_minus1.compare(-2048), Some(false));
assert_eq!(add_minus1.compare(-2049), Some(true));
let or = FutexWakeOp::decode(encode(FUTEX_OP_OR, FUTEX_OP_CMP_NE, 0b100, 7));
assert_eq!(or.apply(0b011), Some(0b111));
assert_eq!(or.compare(7), Some(false));
let andn = FutexWakeOp::decode(encode(FUTEX_OP_ANDN, FUTEX_OP_CMP_GE, 0b110, 3));
assert_eq!(andn.apply(0b111), Some(0b001));
assert_eq!(andn.compare(3), Some(true));
let xor = FutexWakeOp::decode(encode(FUTEX_OP_XOR, FUTEX_OP_CMP_LE, 0xff, 0));
assert_eq!(xor.apply(0x0f), Some(0xf0));
assert_eq!(xor.compare(1), Some(false));
let shift = FutexWakeOp::decode(encode(
FUTEX_OP_OPARG_SHIFT | FUTEX_OP_OR,
FUTEX_OP_CMP_GT,
3,
0,
));
assert_eq!(shift.apply(0), Some(8));
assert_eq!(shift.compare(1), Some(true));
let masked = FutexWakeOp::decode(encode(FUTEX_OP_OPARG_SHIFT | FUTEX_OP_SET, 0, 0xfff, 0));
assert_eq!(masked.apply(0), Some(i32::MIN));
let unknown_op = FutexWakeOp::decode(encode(7, FUTEX_OP_CMP_EQ, 1, 0));
assert!(!unknown_op.has_known_operation());
assert_eq!(unknown_op.apply(0), None);
let unknown_cmp = FutexWakeOp::decode(encode(FUTEX_OP_SET, 6, 1, 0));
assert!(unknown_cmp.has_known_operation());
assert_eq!(unknown_cmp.compare(0), None);
}
#[test]
fn futex_timeout_units_and_modes_match_linux() {
let timeout = Timespec {
tv_sec: 2,
tv_nsec: 3,
};
assert_eq!(
parse_futex_timeout(libc::FUTEX_WAIT, timeout),
Ok(FutexTimeout::Relative(2_000_000_003))
);
assert_eq!(
parse_futex_timeout(libc::FUTEX_WAIT_BITSET, timeout),
Ok(FutexTimeout::Absolute(LogicalTime::from_nanos(
2_000_000_003
)))
);
assert_eq!(
parse_futex_timeout(libc::FUTEX_WAIT_BITSET | libc::FUTEX_PRIVATE_FLAG, timeout),
Ok(FutexTimeout::Absolute(LogicalTime::from_nanos(
2_000_000_003
)))
);
assert_eq!(
parse_futex_timeout(libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG, timeout),
Ok(FutexTimeout::Relative(2_000_000_003))
);
}
#[test]
fn absolute_futex_timeout_is_rebased_to_logical_time() {
let logical_now = LogicalTime::from_secs(100);
let clock_now = LogicalTime::from_secs(5_000);
let deadline = clock_now + Duration::from_millis(100);
assert_eq!(
rebase_absolute_timeout(deadline, clock_now, logical_now),
logical_now + Duration::from_millis(100)
);
assert_eq!(
rebase_absolute_timeout(
clock_now - LogicalTime::from_nanos(1),
clock_now,
logical_now
),
logical_now
);
}
#[test]
fn absolute_futex_timeout_detects_host_and_logical_clock_domains() {
let host_monotonic_now = LogicalTime::from_secs(374_766);
let logical_now = LogicalTime::from_secs(1_640_995_199);
let delta = Duration::from_millis(100);
assert!(absolute_timeout_uses_host_clock(
host_monotonic_now + delta,
host_monotonic_now,
logical_now
));
assert!(!absolute_timeout_uses_host_clock(
logical_now + delta,
host_monotonic_now,
logical_now
));
let host_realtime_now = LogicalTime::from_secs(1_785_142_800);
assert!(absolute_timeout_uses_host_clock(
host_realtime_now + delta,
host_realtime_now,
logical_now
));
}
#[test]
fn futex_timeout_clamps_a_huge_valid_timespec_like_ktime() {
let huge = Timespec {
tv_sec: libc::time_t::MAX,
tv_nsec: 0,
};
assert!(futex_timespec_is_valid(huge));
assert_eq!(
parse_futex_timeout(libc::FUTEX_WAIT, huge),
Ok(FutexTimeout::Relative(FUTEX_KTIME_MAX_NANOS))
);
assert_eq!(
parse_futex_timeout(libc::FUTEX_WAIT_BITSET, huge),
Ok(FutexTimeout::Absolute(LogicalTime::from_nanos(
FUTEX_KTIME_MAX_NANOS
)))
);
let at_ktime_sec_max = Timespec {
tv_sec: (FUTEX_KTIME_MAX_NANOS / 1_000_000_000) as libc::time_t,
tv_nsec: 0,
};
assert_eq!(
parse_futex_timeout(libc::FUTEX_WAIT, at_ktime_sec_max),
Ok(FutexTimeout::Relative(FUTEX_KTIME_MAX_NANOS))
);
let just_below = Timespec {
tv_sec: (FUTEX_KTIME_MAX_NANOS / 1_000_000_000 - 1) as libc::time_t,
tv_nsec: 999_999_999,
};
assert_eq!(
parse_futex_timeout(libc::FUTEX_WAIT, just_below),
Ok(FutexTimeout::Relative(
(FUTEX_KTIME_MAX_NANOS / 1_000_000_000) * 1_000_000_000 - 1
))
);
for invalid in [(-1, 0), (0, -1), (0, 1_000_000_000)] {
assert!(!futex_timespec_is_valid(Timespec {
tv_sec: invalid.0,
tv_nsec: invalid.1,
}));
}
}
#[test]
fn futex_timeout_rejects_invalid_timespecs() {
assert_eq!(
parse_futex_timeout(
libc::FUTEX_WAIT,
Timespec {
tv_sec: -1,
tv_nsec: 0,
},
),
Err(Errno::EINVAL)
);
assert_eq!(
parse_futex_timeout(
libc::FUTEX_WAIT_BITSET,
Timespec {
tv_sec: 0,
tv_nsec: 1_000_000_000,
},
),
Err(Errno::EINVAL)
);
}
fn plain_attr() -> SchedAttrFields {
SchedAttrFields {
policy: 0,
sched_flags: 0,
priority: 0,
runtime: 0,
deadline: 0,
period: 0,
}
}
#[test]
fn size_zero_is_a_well_formed_ver0_request() {
assert_eq!(sched_attr_effective_size(0), Ok(SCHED_ATTR_SIZE_VER0));
}
#[test]
fn size_below_ver0_or_past_a_page_is_too_big() {
assert_eq!(sched_attr_effective_size(1), Err(()));
assert_eq!(sched_attr_effective_size(SCHED_ATTR_SIZE_VER0 - 1), Err(()));
assert_eq!(sched_attr_effective_size(SCHED_ATTR_MAX_SIZE + 1), Err(()));
}
#[test]
fn size_from_ver0_through_one_page_is_accepted_unchanged() {
for size in [
SCHED_ATTR_SIZE_VER0,
SCHED_ATTR_SIZE_VER1,
SCHED_ATTR_SIZE_VER1 + 1,
SCHED_ATTR_MAX_SIZE,
] {
assert_eq!(sched_attr_effective_size(size), Ok(size), "size {}", size);
}
}
#[test]
fn sched_ext_is_a_valid_policy_and_sched_iso_is_not() {
assert!(is_valid_sched_policy(SCHED_EXT), "SCHED_EXT is accepted");
assert!(!is_valid_sched_policy(4), "SCHED_ISO is reserved");
for policy in [0, 1, 2, 3, 5, 6] {
assert!(is_valid_sched_policy(policy), "policy {}", policy);
}
for policy in [8, 99] {
assert!(!is_valid_sched_policy(policy), "policy {}", policy);
}
}
#[test]
fn util_clamp_size_rule_is_decided_before_the_pid_lookup() {
let mut attr = plain_attr();
attr.sched_flags = SCHED_FLAG_UTIL_CLAMP_MIN;
assert_eq!(
validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &attr),
Err(Errno::EINVAL)
);
assert_eq!(
validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER1, &attr),
Ok(())
);
assert_eq!(validate_sched_attr_after_lookup(&attr), Ok(()));
}
#[test]
fn a_negative_policy_is_decided_before_the_pid_lookup() {
let mut attr = plain_attr();
attr.policy = 0x8000_0000;
assert_eq!(
validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &attr),
Err(Errno::EINVAL)
);
attr.sched_flags = SCHED_FLAG_KEEP_POLICY;
assert_eq!(
validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &attr),
Err(Errno::EINVAL),
"KEEP_POLICY must not hide a negative policy"
);
}
#[test]
fn policy_and_flag_rules_are_decided_after_the_pid_lookup() {
let mut bad_policy = plain_attr();
bad_policy.policy = 99;
assert_eq!(
validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &bad_policy),
Ok(()),
"an undefined policy must survive the near side so ESRCH can win"
);
assert_eq!(
validate_sched_attr_after_lookup(&bad_policy),
Err(Errno::EINVAL)
);
let mut bad_flag = plain_attr();
bad_flag.sched_flags = 0x80;
assert_eq!(
validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &bad_flag),
Ok(()),
"an undefined sched_flags bit must survive the near side"
);
assert_eq!(
validate_sched_attr_after_lookup(&bad_flag),
Err(Errno::EINVAL)
);
}
#[test]
fn keep_policy_makes_the_policy_field_irrelevant() {
let mut attr = plain_attr();
attr.policy = 99;
assert_eq!(validate_sched_attr_after_lookup(&attr), Err(Errno::EINVAL));
attr.sched_flags = SCHED_FLAG_KEEP_POLICY;
assert_eq!(validate_sched_attr_after_lookup(&attr), Ok(()));
}
#[test]
fn defined_sched_flags_bits_are_accepted_and_undefined_ones_are_not() {
let mut attr = plain_attr();
attr.sched_flags = 0x80;
assert_eq!(validate_sched_attr_after_lookup(&attr), Err(Errno::EINVAL));
for flag in [
SCHED_FLAG_RESET_ON_FORK,
SCHED_FLAG_RECLAIM,
SCHED_FLAG_DL_OVERRUN,
SCHED_FLAG_KEEP_PARAMS,
SCHED_FLAG_KEEP_POLICY,
] {
let mut ok = plain_attr();
ok.sched_flags = flag;
assert_eq!(
validate_sched_attr_after_lookup(&ok),
Ok(()),
"flag {:#x}",
flag
);
}
let mut all = plain_attr();
all.sched_flags = SCHED_FLAG_ALL;
assert_eq!(
validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &all),
Err(Errno::EINVAL)
);
assert_eq!(
validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER1, &all),
Ok(())
);
assert_eq!(validate_sched_attr_after_lookup(&all), Ok(()));
}
#[test]
fn priority_must_agree_with_the_policy() {
for policy in [0u32, 3, 5] {
let mut attr = plain_attr();
attr.policy = policy;
attr.priority = 1;
assert_eq!(
validate_sched_attr_after_lookup(&attr),
Err(Errno::EINVAL),
"policy {} with priority 1",
policy
);
attr.priority = 0;
assert_eq!(
validate_sched_attr_after_lookup(&attr),
Ok(()),
"policy {} with priority 0",
policy
);
}
for policy in [SCHED_FIFO, SCHED_RR] {
let mut attr = plain_attr();
attr.policy = policy;
attr.priority = 0;
assert_eq!(
validate_sched_attr_after_lookup(&attr),
Err(Errno::EINVAL),
"rt policy {} with priority 0",
policy
);
attr.priority = 1;
assert_eq!(validate_sched_attr_after_lookup(&attr), Ok(()));
}
}
#[test]
fn a_priority_past_max_rt_prio_is_refused() {
let mut attr = plain_attr();
attr.policy = SCHED_FIFO;
attr.priority = MAX_RT_PRIO;
assert_eq!(validate_sched_attr_after_lookup(&attr), Err(Errno::EINVAL));
attr.priority = MAX_RT_PRIO - 1;
assert_eq!(validate_sched_attr_after_lookup(&attr), Ok(()));
}
#[test]
fn deadline_parameters_are_checked() {
let mut zeroed = plain_attr();
zeroed.policy = SCHED_DEADLINE;
assert_eq!(
validate_sched_attr_after_lookup(&zeroed),
Err(Errno::EINVAL),
"all-zero deadline parameters"
);
let mut inverted = plain_attr();
inverted.policy = SCHED_DEADLINE;
inverted.runtime = 90_000_000;
inverted.deadline = 30_000_000;
inverted.period = 30_000_000;
assert_eq!(
validate_sched_attr_after_lookup(&inverted),
Err(Errno::EINVAL),
"runtime > deadline"
);
let mut sane = plain_attr();
sane.policy = SCHED_DEADLINE;
sane.runtime = 10_000_000;
sane.deadline = 30_000_000;
sane.period = 30_000_000;
assert_eq!(validate_sched_attr_after_lookup(&sane), Ok(()));
}
#[test]
fn deadline_parameter_edges_follow_checkparam_dl() {
assert!(!deadline_params_are_valid(1 << 20, 0, 0));
assert!(!deadline_params_are_valid((1 << 10) - 1, 1 << 20, 1 << 20));
assert!(deadline_params_are_valid(1 << 10, 1 << 20, 1 << 20));
assert!(!deadline_params_are_valid(1 << 20, 1 << 63, 0));
assert!(!deadline_params_are_valid(1 << 20, 1 << 20, 1 << 63));
assert!(deadline_params_are_valid(1 << 20, 1 << 21, 0));
assert!(!deadline_params_are_valid(1 << 20, 1 << 22, 1 << 21));
}
#[test]
fn the_size_written_back_on_refusal_is_the_kernel_struct_not_the_libc_mirror() {
assert_eq!(SCHED_ATTR_KERNEL_SIZE, 56);
assert_eq!(std::mem::size_of::<libc::sched_attr>(), 48);
assert_ne!(
SCHED_ATTR_KERNEL_SIZE as usize,
std::mem::size_of::<libc::sched_attr>()
);
}
struct BoundedMemory {
bytes: Vec<u8>,
readable_len: usize,
reads: std::cell::RefCell<Vec<usize>>,
}
impl reverie::syscalls::MemoryAccess for BoundedMemory {
fn read_vectored(
&self,
read_from: &[std::io::IoSlice<'_>],
write_to: &mut [std::io::IoSliceMut<'_>],
) -> Result<usize, Errno> {
let start = read_from[0].as_ptr() as usize;
let want = read_from.iter().map(|slice| slice.len()).sum::<usize>();
self.reads.borrow_mut().push(want);
if start >= self.readable_len {
return Err(Errno::EFAULT);
}
let avail = (self.readable_len - start).min(want);
let mut copied = 0;
for out in write_to.iter_mut() {
if copied == avail {
break;
}
let take = out.len().min(avail - copied);
out[..take].copy_from_slice(&self.bytes[start + copied..start + copied + take]);
copied += take;
}
Ok(copied)
}
fn write_vectored(
&mut self,
_read_from: &[std::io::IoSlice<'_>],
_write_to: &mut [std::io::IoSliceMut<'_>],
) -> Result<usize, Errno> {
unimplemented!("this fixture never writes")
}
}
fn bounded(bytes: Vec<u8>, readable_len: usize) -> BoundedMemory {
BoundedMemory {
bytes,
readable_len,
reads: std::cell::RefCell::new(Vec::new()),
}
}
fn tail_base() -> AddrMut<'static, u8> {
AddrMut::<u8>::from_raw(1).expect("nonzero base")
}
#[test]
fn a_nonzero_byte_before_a_fault_is_e2big_not_efault() {
let off = SCHED_ATTR_KERNEL_SIZE as usize;
let mut bytes = vec![0u8; off + 4096];
bytes[off + 1] = 0xAA; let readable = off + 512; let memory = bounded(bytes, readable);
assert_eq!(
scan_tail_is_zeroed(&memory, tail_base(), off, 4096),
TailVerdict::NotZeroed,
"a non-zero byte the scan reaches first must win over a later fault"
);
}
#[test]
fn a_fault_before_any_nonzero_byte_is_efault() {
let off = SCHED_ATTR_KERNEL_SIZE as usize;
let bytes = vec![0u8; off + 4096];
let memory = bounded(bytes, off + 512);
assert_eq!(
scan_tail_is_zeroed(&memory, tail_base(), off, 4096),
TailVerdict::Faulted,
"an unreadable byte reached before anything non-zero must be EFAULT"
);
}
#[test]
fn tail_reads_are_never_small_enough_to_bypass_guest_protection() {
let off = SCHED_ATTR_KERNEL_SIZE as usize;
for tail_len in 1..=16usize {
let bytes = vec![0u8; off + tail_len + 64];
let memory = bounded(bytes, off + tail_len + 64);
assert_eq!(
scan_tail_is_zeroed(&memory, tail_base(), off, tail_len),
TailVerdict::AllZero
);
let reads = memory.reads.borrow().clone();
assert!(!reads.is_empty(), "tail_len {tail_len} issued no read");
for length in reads {
assert!(
length > std::mem::size_of::<u64>(),
"tail_len {tail_len} issued a {length}-byte read, which safeptrace \
serves with PTRACE_PEEKDATA and which therefore bypasses guest \
page protections"
);
}
}
}
#[test]
fn keep_policy_validates_against_the_virtual_current_policy() {
let attr = SchedAttrFields {
policy: 99, sched_flags: SCHED_FLAG_KEEP_POLICY,
priority: 1,
runtime: 0,
deadline: 0,
period: 0,
};
assert_eq!(
validate_sched_attr_after_lookup(&attr),
Err(Errno::EINVAL),
"priority 1 under the virtual SCHED_OTHER current policy must be refused"
);
let ok = SchedAttrFields {
priority: 0,
..attr
};
assert_eq!(
validate_sched_attr_after_lookup(&ok),
Ok(()),
"KEEP_POLICY must still ignore the policy field itself"
);
}
#[test]
fn the_virtual_current_policy_is_what_sched_getattr_reports() {
assert_eq!(
VIRTUAL_CURRENT_POLICY,
libc::SCHED_OTHER as u32,
"handle_sched_getattr writes SCHED_OTHER into sched_policy for every thread; \
KEEP_POLICY must substitute that same value"
);
}
}
pub(crate) fn real_uid(tid: i32) -> Option<u32> {
let status = std::fs::read_to_string(format!("/proc/{tid}/status")).ok()?;
status
.lines()
.find_map(|line| line.strip_prefix("Uid:"))?
.split_whitespace()
.next()?
.parse()
.ok()
}
fn clone3_field<M: MemoryAccess>(
memory: &M,
clone: &syscalls::Clone3,
offset: usize,
) -> Option<u64> {
const CLONE_ARGS_SIZE_VER0: usize = 64;
if clone.size() < CLONE_ARGS_SIZE_VER0 || offset + 8 > clone.size() {
return None;
}
let base = clone.args()?.as_raw();
memory
.read_value(Addr::<u64>::from_raw(base + offset)?)
.ok()
}
fn clone_pidfd_address<M: MemoryAccess>(
clone_family: &syscalls::family::CloneFamily,
memory: &M,
flags: CloneFlags,
) -> Option<usize> {
let (flags, address) = match clone_family {
syscalls::family::CloneFamily::Clone(clone) => {
(flags, clone.ptid().map(|ptid| ptid.as_raw()))
}
syscalls::family::CloneFamily::Clone3(clone) => (
flags,
Some(usize::try_from(clone3_field(memory, clone, 8)?).ok()?),
),
#[cfg(not(target_arch = "aarch64"))]
_ => return None,
};
if !flags.contains(CloneFlags::CLONE_PIDFD)
|| flags
.intersects(CloneFlags::CLONE_VM | CloneFlags::CLONE_FILES | CloneFlags::CLONE_VFORK)
{
return None;
}
address
}