#![deny(clippy::all)]
#![allow(clippy::uninlined_format_args)]
mod backend_stats;
pub mod build_info;
pub mod canonical_verdict;
mod chroot;
mod consts;
mod desync;
pub mod logdiff_report;
pub mod e9patch;
mod error;
mod event;
mod event_stream;
mod fd;
pub mod happens_before;
mod id;
pub mod instruction_map;
mod interp;
pub mod liteinst_bootstrap;
pub mod liteinst_record;
mod metadata;
pub mod proc_mount;
mod ptrace_completion;
pub mod run_evidence;
pub use canonical_verdict::Verdict;
pub const RECORD_REPLAY_HASHES_IO_BUFFERS: bool = true;
pub const RECORD_REPLAY_VIRTUALIZES_TIME: bool = false;
pub const HERMIT_VERIFICATION_DIVERGENCE_EXIT: i32 = 1;
pub const HERMIT_INTERNAL_FAILURE_EXIT: i32 = 125;
pub const HERMIT_DEADLINE_EXIT: i32 = 124;
pub const GUEST_PROGRAM_NOT_FOUND_EXIT: i32 = 127;
pub const GUEST_PROGRAM_NOT_EXECUTABLE_EXIT: i32 = 126;
const _: () = assert!(
HERMIT_VERIFICATION_DIVERGENCE_EXIT == 1,
"1 is the established diff-style result for a completed comparison that diverged"
);
const _: () = assert!(
HERMIT_INTERNAL_FAILURE_EXIT == 125,
"125 is the GNU wrapper-failed convention; 1 is the commonest guest status and 0 is success"
);
const _: () = assert!(
GUEST_PROGRAM_NOT_FOUND_EXIT == 127,
"127 is the GNU command-not-found convention and must stay distinct from 125 and 126"
);
const _: () = assert!(
GUEST_PROGRAM_NOT_EXECUTABLE_EXIT == 126,
"126 is the GNU found-but-not-executable convention and must stay distinct from 125 and 127"
);
const _: () = assert!(
HERMIT_VERIFICATION_DIVERGENCE_EXIT != 0
&& HERMIT_INTERNAL_FAILURE_EXIT != 0
&& GUEST_PROGRAM_NOT_FOUND_EXIT != 0
&& GUEST_PROGRAM_NOT_EXECUTABLE_EXIT != 0,
"a failure code of 0 would invert every assert_eq!(code, Some(CONST)) into demanding success"
);
mod record;
mod record_replay_path;
mod recorder;
mod replay;
mod replayer;
mod sabre_bootstrap;
mod sabre_ptrace;
mod script;
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::io::Seek;
use std::io::SeekFrom;
use std::io::Write;
use std::os::fd::AsRawFd;
use std::os::fd::FromRawFd;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use std::time::Duration;
use std::time::Instant;
use anyhow::anyhow;
use clap::ValueEnum;
use consts::METADATA_NAME;
pub use detcore::Config as DetConfig;
pub use detcore::Detcore;
pub use detcore::RecordOrReplay;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_background_init_v2;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_name;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_pre_syscall;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_ready;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_thread_exit;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_thread_init;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_totals;
pub use error::Context;
pub use error::Error;
pub use error::FailureKind;
pub use error::SerializableError;
use goblin::elf::Elf;
use goblin::elf::header;
use goblin::elf::section_header;
pub use id::Id;
use metadata::Metadata;
pub use ptrace_completion::HermitCleanupStage;
pub use ptrace_completion::HermitCleanupUnconfirmed;
pub use ptrace_completion::HermitPtraceFailure;
pub use ptrace_completion::RecoveryRefusal;
use record::Record;
use replay::Replay;
pub use reverie::ExitStatus;
use reverie::GlobalTool;
pub use reverie::process;
pub use reverie::process::Command;
pub use reverie::process::Mount;
pub use reverie::process::Namespace;
pub use reverie::process::Output;
pub use reverie::process::Stdio;
pub use script::Shebang;
use serde::Deserialize;
use serde::Serialize;
fn read_iovecs<M: reverie::syscalls::MemoryAccess>(
memory: &M,
message: &libc::msghdr,
) -> Result<Vec<libc::iovec>, reverie::Errno> {
if message.msg_iovlen == 0 {
return Ok(Vec::new());
}
let address = reverie::syscalls::Addr::from_raw(message.msg_iov as usize)
.ok_or(reverie::Errno::EFAULT)?;
let mut iovecs = vec![
libc::iovec {
iov_base: std::ptr::null_mut(),
iov_len: 0,
};
message.msg_iovlen
];
memory.read_values(address, &mut iovecs)?;
Ok(iovecs)
}
fn vectored_offset(low: u64, high: u64) -> i64 {
if std::mem::size_of::<usize>() == 8 {
low as i64
} else {
((high << 32) | (low & u32::MAX as u64)) as i64
}
}
enum KvmStdinReservation {
Open(fs::File),
Closed,
}
static KVM_STDIN_RESERVATION: Mutex<Option<KvmStdinReservation>> = Mutex::new(None);
pub fn reserve_kvm_stdin(stdin: Option<fs::File>) -> io::Result<()> {
let mut reservation = KVM_STDIN_RESERVATION
.lock()
.map_err(|_| io::Error::other("KVM stdin reservation lock is poisoned"))?;
if reservation.is_some() {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
"KVM stdin is already reserved",
));
}
*reservation = Some(match stdin {
Some(file) => KvmStdinReservation::Open(file),
None => KvmStdinReservation::Closed,
});
Ok(())
}
fn duplicate_current_stdin() -> io::Result<Option<fs::File>> {
let duplicate = unsafe { libc::fcntl(libc::STDIN_FILENO, libc::F_DUPFD_CLOEXEC, 3) };
if duplicate >= 0 {
return Ok(Some(unsafe { fs::File::from_raw_fd(duplicate) }));
}
let error = io::Error::last_os_error();
if error.raw_os_error() == Some(libc::EBADF) {
Ok(None)
} else {
Err(error)
}
}
fn ensure_kvm_stdin_reserved() -> io::Result<()> {
let mut reservation = KVM_STDIN_RESERVATION
.lock()
.map_err(|_| io::Error::other("KVM stdin reservation lock is poisoned"))?;
if reservation.is_none() {
*reservation = Some(match duplicate_current_stdin()? {
Some(file) => KvmStdinReservation::Open(file),
None => KvmStdinReservation::Closed,
});
}
Ok(())
}
fn reserved_kvm_stdin() -> Result<Option<fs::File>, Error> {
ensure_kvm_stdin_reserved()?;
let reservation = KVM_STDIN_RESERVATION
.lock()
.map_err(|_| io::Error::other("KVM stdin reservation lock is poisoned"))?;
match reservation.as_ref() {
Some(KvmStdinReservation::Open(file)) => Ok(Some(file.try_clone()?)),
Some(KvmStdinReservation::Closed) => Ok(None),
None => unreachable!("stdin reservation was initialized above"),
}
}
enum StdinSnapshot {
Seekable(fs::File),
None,
}
static OUTPUT_STDIN_SNAPSHOT: Mutex<Option<StdinSnapshot>> = Mutex::new(None);
pub fn reserve_output_stdin_snapshot(stdin: Option<fs::File>) -> io::Result<()> {
let snapshot = match stdin {
None => StdinSnapshot::None,
Some(mut file) => {
let is_tty = unsafe { libc::isatty(file.as_raw_fd()) } == 1;
if is_tty {
StdinSnapshot::None
} else {
let replay = if file.stream_position().is_ok() {
file
} else {
eprintln!(
"hermit: --verify is buffering stdin from a non-seekable stream so both \
runs receive identical input. If this appears to hang, stdin has not \
reached end-of-file; pass `< /dev/null` when the guest needs no input."
);
let mut buffered = tempfile::tempfile()?;
io::copy(&mut file, &mut buffered)?;
buffered.seek(SeekFrom::Start(0))?;
buffered
};
let replay_path = format!("/proc/self/fd/{}", replay.as_raw_fd());
StdinSnapshot::Seekable(fs::File::open(replay_path)?)
}
}
};
let mut reservation = OUTPUT_STDIN_SNAPSHOT
.lock()
.map_err(|_| io::Error::other("output stdin snapshot lock is poisoned"))?;
if reservation.is_some() {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
"output stdin snapshot is already reserved",
));
}
*reservation = Some(snapshot);
Ok(())
}
fn output_backend_stdin_reservation() -> Result<(bool, Option<fs::File>), Error> {
let mut reservation = OUTPUT_STDIN_SNAPSHOT
.lock()
.map_err(|_| io::Error::other("output stdin snapshot lock is poisoned"))?;
match reservation.as_mut() {
Some(StdinSnapshot::Seekable(file)) => {
file.seek(SeekFrom::Start(0))?;
Ok((true, Some(file.try_clone()?)))
}
Some(StdinSnapshot::None) => Ok((true, None)),
None => Ok((false, None)),
}
}
fn output_backend_stdin_file() -> Result<Option<fs::File>, Error> {
Ok(output_backend_stdin_reservation()?.1)
}
fn output_backend_stdin() -> Result<Stdio, Error> {
match output_backend_stdin_file()? {
Some(file) => Ok(Stdio::from(file)),
None => Ok(Stdio::null()),
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Recording {
pub id: Id,
pub exit_status: ExitStatus,
}
#[derive(Clone, Copy)]
enum CapabilityProbe {
Namespaces,
Ptrace,
Seccomp,
}
fn run_capability_probe(probe: CapabilityProbe) -> Result<bool, Error> {
let pid = unsafe { libc::fork() };
if pid == -1 {
return Err(std::io::Error::last_os_error()).context("Failed to fork capability probe");
}
if pid == 0 {
let supported = match probe {
CapabilityProbe::Namespaces => unsafe {
libc::unshare(libc::CLONE_NEWUSER | libc::CLONE_NEWPID) == 0
},
CapabilityProbe::Ptrace => {
unsafe {
libc::ptrace(
libc::PTRACE_TRACEME,
0,
std::ptr::null_mut::<libc::c_void>(),
std::ptr::null_mut::<libc::c_void>(),
) != -1
}
}
CapabilityProbe::Seccomp => {
let mut filter = libc::sock_filter {
code: 0x06, jt: 0,
jf: 0,
k: 0x7fff0000, };
let program = libc::sock_fprog {
len: 1,
filter: &mut filter,
};
unsafe {
libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == 0
&& libc::syscall(
libc::SYS_seccomp,
1, 0,
&program,
) == 0
}
}
};
unsafe { libc::_exit(i32::from(!supported)) }
}
let mut status = 0;
loop {
let result = unsafe { libc::waitpid(pid, &mut status, 0) };
if result == pid {
return Ok(libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0);
}
if result == -1 {
let error = std::io::Error::last_os_error();
if error.kind() == std::io::ErrorKind::Interrupted {
continue;
}
return Err(error).context("Failed to wait for capability probe");
}
}
}
fn validate_tracing_environment() -> Result<(), Error> {
if !run_capability_probe(CapabilityProbe::Namespaces)? {
anyhow::bail!(
"Hermit cannot create its required user and PID namespaces: \
unshare(CLONE_NEWUSER | CLONE_NEWPID) was denied. Allow unprivileged user namespaces \
and the unshare syscall in the host/container policy."
);
}
if !run_capability_probe(CapabilityProbe::Ptrace)? {
anyhow::bail!(
"Hermit cannot use ptrace in this environment: a child PTRACE_TRACEME probe was \
denied. Allow same-UID parent-child ptrace in the container seccomp and host \
Yama/LSM policy; CAP_SYS_PTRACE is normally not required. Use --namespace-only for \
a sandbox smoke test without syscall interception."
);
}
if !run_capability_probe(CapabilityProbe::Seccomp)? {
anyhow::bail!(
"Hermit cannot install its tracee seccomp filter: \
seccomp(SECCOMP_SET_MODE_FILTER) was denied. Allow seccomp and \
prctl(PR_SET_NO_NEW_PRIVS) in the container policy, or use --namespace-only for a \
sandbox smoke test without syscall interception."
);
}
Ok(())
}
#[cfg(feature = "dbt")]
fn is_dynamorio_sdk(path: &Path) -> bool {
path.join("include/dr_api.h").is_file()
|| path.join("DynamoRIOConfig.cmake").is_file()
|| path.join("cmake/DynamoRIOConfig.cmake").is_file()
}
#[cfg(feature = "dbt")]
fn dynamorio_sdk_available() -> bool {
if hermit_resources::resource("dynamorio/bin64/drrun")
.is_ok_and(|path| path.is_some_and(|path| path.is_file()))
{
return true;
}
if reverie_dbt::bundled_drrun_path().is_file() {
return true;
}
const DEFAULT_ROOTS: [&str; 3] = [
"/usr/lib/cmake/DynamoRIO",
"/usr/local/lib/cmake/DynamoRIO",
"/opt/dynamorio",
];
["DYNAMORIO_HOME", "DynamoRIO_DIR"]
.into_iter()
.filter_map(std::env::var_os)
.map(PathBuf::from)
.chain(DEFAULT_ROOTS.into_iter().map(PathBuf::from))
.any(|path| is_dynamorio_sdk(&path))
}
#[cfg(feature = "dbt")]
fn dbt_runtime_unavailable_reason() -> Option<String> {
dbt_runtime_unavailable_reason_from(detcore_dbt::runtime_library_path())
}
#[cfg(feature = "dbt")]
fn dbt_runtime_unavailable_reason_from(runtime: io::Result<PathBuf>) -> Option<String> {
runtime.err().map(|error| {
format!(
"the Detcore DBT runtime is unavailable: {error}; build the hermit binary and \
cdylib in the same target directory"
)
})
}
fn liteinst_runtime_pin_matches(path: &Path) -> io::Result<()> {
let expected = env!("HERMIT_REVERIE_PIN");
if expected == "unknown" {
return Ok(());
}
let marker = PathBuf::from(format!("{}.revision", path.display()));
let staged = match fs::read_to_string(&marker) {
Ok(text) => text.trim().to_owned(),
Err(error) if error.kind() == io::ErrorKind::NotFound => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"the staged LiteInst runtime {} records no Reverie revision, so it cannot be \
shown to match the pin this binary was built from ({expected}). It was staged \
by a build that predates revision recording, which is exactly the case where a \
stale runtime went unnoticed. Restage it with `cargo build --release -p \
hermit-install` -- staging is release-only.",
path.display()
),
));
}
Err(error) => return Err(error),
};
if staged != expected {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"the staged LiteInst runtime {} was built from Reverie {staged}, but this binary \
was built from {expected}. Running it would measure the OLD runtime and report a \
verdict about the NEW pin. Restage with `cargo build --release -p hermit-install`.",
path.display()
),
));
}
Ok(())
}
fn validate_liteinst_runtime_library(path: &Path) -> io::Result<PathBuf> {
liteinst_runtime_pin_matches(path)?;
validate_preload_constructor_library(
path,
"LiteInst runtime",
&[
"reverie_liteinst_initialize",
"reverie_liteinst_site_trap_count",
"reverie_liteinst_site_hook_count",
],
"reverie_liteinst_initialize",
)
}
fn validate_preload_constructor_library(
path: &Path,
label: &str,
required: &[&str],
initializer_name: &str,
) -> io::Result<PathBuf> {
let bytes = fs::read(path).map_err(|error| {
io::Error::new(
error.kind(),
format!("failed to read {label} {}: {error}", path.display()),
)
})?;
let elf = Elf::parse(&bytes).map_err(|error| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("{label} {} is not an ELF DSO: {error}", path.display()),
)
})?;
if elf.header.e_type != header::ET_DYN || elf.header.e_machine != header::EM_X86_64 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{label} {} is not an x86-64 shared object", path.display()),
));
}
for &name in required {
if !elf
.dynsyms
.iter()
.any(|symbol| elf.dynstrtab.get_at(symbol.st_name) == Some(name))
{
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"{label} {} is missing required export {name}",
path.display()
),
));
}
}
let (initializer_index, initializer) = elf
.dynsyms
.iter()
.enumerate()
.find(|(_, symbol)| elf.dynstrtab.get_at(symbol.st_name) == Some(initializer_name))
.ok_or_else(|| {
io::Error::other(format!(
"checked {label} initializer {initializer_name} disappeared"
))
})?;
let init_array = elf
.section_headers
.iter()
.find(|section| section.sh_type == section_header::SHT_INIT_ARRAY)
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("{label} {} has no constructor array", path.display()),
)
})?;
let init_start = init_array.sh_addr;
let init_end = init_start.saturating_add(init_array.sh_size);
let relocated_initializer = elf
.dynrelas
.iter()
.chain(elf.dynrels.iter())
.any(|relocation| {
(init_start..init_end).contains(&relocation.r_offset)
&& relocation.r_sym == initializer_index
});
let init_bytes = usize::try_from(init_array.sh_offset)
.ok()
.and_then(|start| {
usize::try_from(init_array.sh_size)
.ok()
.and_then(|size| bytes.get(start..start.checked_add(size)?))
})
.unwrap_or_default();
let direct_initializer = init_bytes
.as_chunks::<8>()
.0
.iter()
.any(|entry| u64::from_le_bytes(*entry) == initializer.st_value);
if !relocated_initializer && !direct_initializer {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!(
"{label} {} does not register {initializer_name} as a preload constructor",
path.display()
),
));
}
path.canonicalize()
}
#[doc(hidden)]
pub fn liteinst_runtime_library_path() -> io::Result<PathBuf> {
if let Some(path) = std::env::var_os("HERMIT_LITEINST_RUNTIME") {
let path = PathBuf::from(path);
if !path.is_file() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"HERMIT_LITEINST_RUNTIME does not name a regular file",
));
}
return validate_liteinst_runtime_library(&path).map_err(|error| {
io::Error::new(
error.kind(),
format!("HERMIT_LITEINST_RUNTIME is invalid: {error}"),
)
});
}
let executable = std::env::current_exe()?;
let directory = executable.parent().ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"Hermit executable has no parent directory",
)
})?;
if let Some(path) = [
directory.join("libreverie_liteinst.so"),
directory.join("deps/libreverie_liteinst.so"),
]
.into_iter()
.find(|path| path.is_file())
{
return validate_liteinst_runtime_library(&path);
}
if let Some(path) = hermit_resources::resource("libreverie_liteinst.so")?
&& path.is_file()
{
return validate_liteinst_runtime_library(&path);
}
Err(io::Error::new(
io::ErrorKind::NotFound,
format!(
"libreverie_liteinst.so was not built beside {} or staged as an installed resource",
executable.display()
),
))
}
pub const LITEINST_IN_GUEST_ENV: &str = "HERMIT_LITEINST_IN_GUEST";
pub const LITEINST_TOOL_RUNTIME_ENV: &str = "HERMIT_LITEINST_TOOL_RUNTIME";
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum LiteinstRuntime {
HostHybrid,
InGuest,
}
impl LiteinstRuntime {
pub fn parse(value: Option<&OsStr>) -> Result<Self, Error> {
match value {
None => Ok(Self::HostHybrid),
Some(value) if value == OsStr::new("1") => Ok(Self::InGuest),
Some(value) => Err(anyhow!(
"{LITEINST_IN_GUEST_ENV}={:?} is not a LiteInst runtime selection: leave it unset \
for the ptrace-hosted hybrid, or set it to 1 for in-guest Detcore",
value
)),
}
}
pub fn from_env() -> Result<Self, Error> {
Self::parse(std::env::var_os(LITEINST_IN_GUEST_ENV).as_deref())
}
}
#[doc(hidden)]
pub fn liteinst_tool_runtime_library_path() -> io::Result<PathBuf> {
if let Some(path) = std::env::var_os(LITEINST_TOOL_RUNTIME_ENV) {
let path = PathBuf::from(path);
if !path.is_file() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("{LITEINST_TOOL_RUNTIME_ENV} does not name a regular file"),
));
}
return validate_liteinst_tool_runtime_library(&path).map_err(|error| {
io::Error::new(
error.kind(),
format!("{LITEINST_TOOL_RUNTIME_ENV} is invalid: {error}"),
)
});
}
let executable = std::env::current_exe()?;
let directory = executable.parent().ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"Hermit executable has no parent directory",
)
})?;
if let Some(path) = [
directory.join("libdetcore_liteinst.so"),
directory.join("deps/libdetcore_liteinst.so"),
]
.into_iter()
.find(|path| path.is_file())
{
return validate_liteinst_tool_runtime_library(&path);
}
Err(io::Error::new(
io::ErrorKind::NotFound,
format!(
"libdetcore_liteinst.so was not built beside {}",
executable.display()
),
))
}
fn validate_liteinst_tool_runtime_library(path: &Path) -> io::Result<PathBuf> {
validate_preload_constructor_library(
path,
"in-guest Detcore runtime",
&["detcore_liteinst_initialize"],
"detcore_liteinst_initialize",
)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LiteinstInGuestRefusal {
reason: String,
}
impl LiteinstInGuestRefusal {
pub fn new(reason: impl Into<String>) -> Self {
Self {
reason: reason.into(),
}
}
}
impl std::fmt::Display for LiteinstInGuestRefusal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{LITEINST_IN_GUEST_ENV}=1 (in-guest LiteInst) refuses this run: {}",
self.reason
)
}
}
impl std::error::Error for LiteinstInGuestRefusal {}
#[cfg(feature = "liteinst")]
fn refuse_in_guest_liteinst_run(command: &mut Command, config: &DetConfig) -> Result<(), Error> {
let reason = if config.max_timeslice.is_some() {
Some(
"it cannot deliver Detcore's preemption timer yet (the in-guest Tool host refuses \
set_timer with ENOSYS), so the run would fail at its first timeslice; pass \
--max-timeslice=disabled"
.to_owned(),
)
} else {
in_guest_liteinst_program_gap(command)?
};
match reason {
Some(reason) => Err(Error::new(LiteinstInGuestRefusal::new(reason))),
None => Ok(()),
}
}
#[cfg(feature = "liteinst")]
const MAX_INTERPRETER_DEPTH: usize = 4;
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_program_gap(command: &mut Command) -> Result<Option<String>, Error> {
use std::io::Read;
use std::os::unix::fs::OpenOptionsExt;
use script::KernelScript;
let mut path = pin_spawn_program(command)?;
for _ in 0..=MAX_INTERPRETER_DEPTH {
let shown = path.display().to_string();
let not_regular = || {
Ok(Some(format!(
"{shown} is not a regular file, and the kernel executes only regular files"
)))
};
if !fs::metadata(&path)
.with_context(|| format!("cannot read guest program {shown}"))?
.is_file()
{
return not_regular();
}
let mut file = fs::OpenOptions::new()
.read(true)
.custom_flags(libc::O_NONBLOCK | libc::O_NOCTTY)
.open(&path)
.with_context(|| format!("cannot read guest program {shown}"))?;
if !file
.metadata()
.with_context(|| format!("cannot read guest program {shown}"))?
.is_file()
{
return not_regular();
}
let mut header = Vec::new();
(&file)
.take(256)
.read_to_end(&mut header)
.with_context(|| format!("cannot read guest program {shown}"))?;
match KernelScript::from_buf(&header) {
KernelScript::Interpreter(interpreter) => {
path = match command.get_current_dir() {
Some(directory) => directory.join(interpreter),
None => interpreter,
};
continue;
}
KernelScript::Declined => {
return Ok(Some(format!(
"{shown} starts with #! but the kernel's script handler declines it (it \
names no interpreter, or the interpreter path fills the first 256 bytes), \
so the kernel would fail the execve or hand the file to another handler \
that need not load the runtime"
)));
}
KernelScript::NotScript => {}
}
let Some(startup) = interp::elf_startup(&mut file)
.with_context(|| format!("cannot read the ELF headers of guest program {shown}"))?
else {
return Ok(Some(format!(
"{shown} is neither an ELF executable nor a #! script, so the kernel would hand \
it to a handler that need not load the runtime"
)));
};
let reason = if !startup.x86_64 {
"is not a 64-bit x86-64 ELF image, so the dynamic loader cannot load the 64-bit \
runtime into it"
} else if !startup.has_interp {
"is statically linked (it has no PT_INTERP), so no dynamic loader runs to load the \
runtime"
} else if is_secure_execution_image(&file, &shown)? {
"is set-user-ID, set-group-ID or has file capabilities, so the kernel runs it in \
secure-execution mode, where the dynamic loader ignores LD_PRELOAD"
} else if startup.has_preinit_array {
"has a DT_PREINIT_ARRAY, whose functions run before the runtime's constructor"
} else {
return Ok(None);
};
return Ok(Some(format!(
"guest program {shown} {reason}, so it would run unmonitored if Hermit launched it"
)));
}
Ok(Some(format!(
"the #! interpreters of guest program {} nest more than {MAX_INTERPRETER_DEPTH} deep",
Path::new(command.get_program()).display()
)))
}
#[cfg(feature = "liteinst")]
fn pin_spawn_program(command: &mut Command) -> Result<PathBuf, Error> {
let arg0 = command.get_arg0().to_owned();
let program = command.find_program().with_context(|| {
format!(
"cannot find guest program {}",
Path::new(command.get_program()).display()
)
})?;
command.program(&program).arg0(arg0);
Ok(program)
}
#[cfg(feature = "liteinst")]
fn is_secure_execution_image(file: &fs::File, shown: &str) -> Result<bool, Error> {
let mode = file
.metadata()
.with_context(|| format!("cannot stat guest program {shown}"))?
.permissions()
.mode();
if mode & 0o4000 != 0 || mode & 0o2010 == 0o2010 {
return Ok(true);
}
let size = unsafe {
libc::fgetxattr(
file.as_raw_fd(),
c"security.capability".as_ptr(),
std::ptr::null_mut(),
0,
)
};
if size >= 0 {
return Ok(true);
}
match io::Error::last_os_error().raw_os_error() {
Some(libc::ENODATA) | Some(libc::ENOTSUP) => Ok(false),
_ => Err(Error::new(io::Error::last_os_error()).context(format!(
"cannot read the file capabilities of guest program {shown}"
))),
}
}
#[cfg(feature = "liteinst")]
fn refuse_in_guest_liteinst_timeout(
backend: Backend,
timeout: Option<Duration>,
) -> Result<(), Error> {
if backend == Backend::Liteinst
&& timeout.is_some()
&& LiteinstRuntime::from_env()? == LiteinstRuntime::InGuest
{
return Err(Error::new(LiteinstInGuestRefusal::new(
"a wall-clock timeout has not been qualified for the in-guest runtime, whose run \
future does not kill the guest when it is dropped",
)));
}
Ok(())
}
#[cfg(not(feature = "liteinst"))]
fn refuse_in_guest_liteinst_timeout(
_backend: Backend,
_timeout: Option<Duration>,
) -> Result<(), Error> {
Ok(())
}
#[cfg(feature = "liteinst")]
fn liteinst_unavailable_reason() -> Option<String> {
liteinst_runtime_unavailable_reason()
}
#[cfg(not(feature = "liteinst"))]
fn liteinst_unavailable_reason() -> Option<String> {
Some("this build was compiled without the liteinst backend: the `liteinst` feature is not enabled; rebuild with `--features liteinst` (or `--features third-party-backends`). This says nothing about whether LiteInst works on this machine -- it has not been checked".to_owned())
}
#[cfg(feature = "liteinst")]
fn liteinst_runtime_unavailable_reason() -> Option<String> {
match LiteinstRuntime::from_env() {
Err(error) => Some(error.to_string()),
Ok(LiteinstRuntime::InGuest) => liteinst_tool_runtime_library_path().err().map(|error| {
format!(
"the in-guest Detcore runtime is unavailable: {error}; build it with `cargo build -p detcore-liteinst` in the target directory of this hermit binary, or name it with {LITEINST_TOOL_RUNTIME_ENV}"
)
}),
Ok(LiteinstRuntime::HostHybrid) => liteinst_runtime_library_path().err().map(|error| {
format!(
"the LiteInst preload runtime is unavailable: {error}; build the locked liteinst-runtime-build manifest and stage its constructor-enabled DSO beside hermit"
)
}),
}
}
fn kvm_device_unavailable_reason(path: &Path) -> Option<String> {
fs::OpenOptions::new()
.read(true)
.write(true)
.open(path)
.err()
.map(|error| {
format!(
"cannot open {} read-write: {error}; grant access through the device owner/group \
or root",
path.display()
)
})
}
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, ValueEnum)]
pub enum Backend {
#[default]
Ptrace,
Dbt,
Liteinst,
Sabre,
Kvm,
E9patch,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct BackendUnavailable {
backend: Backend,
reason: String,
}
impl BackendUnavailable {
pub fn new(backend: Backend, reason: impl Into<String>) -> Self {
Self {
backend,
reason: reason.into(),
}
}
pub fn backend(&self) -> Backend {
self.backend
}
pub fn reason(&self) -> &str {
&self.reason
}
}
impl std::fmt::Display for BackendUnavailable {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
formatter,
"backend `{}` is unavailable: {}",
self.backend.as_str(),
self.reason
)
}
}
impl std::error::Error for BackendUnavailable {}
impl Backend {
const ALL: [Self; 6] = [
Self::Ptrace,
Self::Dbt,
Self::Liteinst,
Self::Sabre,
Self::Kvm,
Self::E9patch,
];
pub const fn as_str(self) -> &'static str {
match self {
Self::Ptrace => "ptrace",
Self::Dbt => "dbt",
Self::Liteinst => "liteinst",
Self::Sabre => "sabre",
Self::Kvm => "kvm",
Self::E9patch => "e9patch",
}
}
fn uses_ptrace_pmu_timers(self) -> bool {
let liteinst_runtime = if self == Self::Liteinst {
LiteinstRuntime::from_env().unwrap_or(LiteinstRuntime::HostHybrid)
} else {
LiteinstRuntime::HostHybrid
};
self.uses_ptrace_pmu_timers_for_liteinst_runtime(liteinst_runtime)
}
fn uses_ptrace_pmu_timers_for_liteinst_runtime(
self,
liteinst_runtime: LiteinstRuntime,
) -> bool {
match self {
Self::Ptrace | Self::E9patch => true,
Self::Liteinst => liteinst_runtime != LiteinstRuntime::InGuest,
Self::Dbt | Self::Sabre | Self::Kvm => false,
}
}
pub fn available() -> impl Iterator<Item = Self> {
Self::ALL
.into_iter()
.filter(|backend| backend.is_available())
}
pub fn is_available(self) -> bool {
self.unavailable_reason().is_none()
}
pub fn ensure_available(self) -> Result<(), Error> {
if let Some(reason) = self.unavailable_reason() {
Err(Error::new(BackendUnavailable::new(self, reason)))
} else {
Ok(())
}
}
fn unavailable_reason(self) -> Option<String> {
match self {
Self::Ptrace => validate_tracing_environment()
.err()
.map(|error| error.to_string()),
Self::Dbt => dbt_unavailable_reason(),
Self::Liteinst => liteinst_unavailable_reason(),
Self::Sabre => sabre_unavailable_reason(),
Self::Kvm => kvm_device_unavailable_reason(Path::new("/dev/kvm")),
Self::E9patch => e9patch_unavailable_reason(),
}
}
}
struct SkidOvershootReport {
enabled: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SkidOvershootError {
count: u64,
}
impl SkidOvershootError {
pub fn new(count: u64) -> Self {
assert!(
count > 0,
"a skid-overshoot refusal requires a positive count"
);
Self { count }
}
pub fn count(&self) -> u64 {
self.count
}
}
impl std::fmt::Display for SkidOvershootError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"observed {} {} report(s); refusing the result because precise PMU timer \
delivery passed its target and deterministic execution was not established",
self.count,
reverie::SKID_OVERSHOOT_MARKER
)
}
}
impl std::error::Error for SkidOvershootError {}
fn take_skid_overshoot_error() -> Option<SkidOvershootError> {
let count = reverie::take_skid_overshoot_count();
(count > 0).then(|| SkidOvershootError::new(count))
}
impl SkidOvershootReport {
fn begin(enabled: bool) -> Self {
if enabled {
let _ = reverie::take_skid_overshoot_count();
}
Self { enabled }
}
fn finish<T>(self, result: Result<T, Error>) -> Result<T, Error> {
if !self.enabled {
return result;
}
let Some(overshoot) = take_skid_overshoot_error() else {
return result;
};
match result {
Ok(_) => Err(Error::new(overshoot)),
Err(error) => Err(error.context(overshoot)),
}
}
fn finish_with_count<T>(self, result: Result<T, Error>) -> Result<(T, u64), Error> {
if !self.enabled {
return result.map(|value| (value, 0));
}
let count = reverie::take_skid_overshoot_count();
match result {
Ok(value) => Ok((value, count)),
Err(error) if count > 0 => Err(error.context(SkidOvershootError::new(count))),
Err(error) => Err(error),
}
}
}
#[cfg(feature = "sabre")]
fn sabre_unavailable_reason() -> Option<String> {
sabre_runtime_unavailable_reason()
}
#[cfg(not(feature = "sabre"))]
fn sabre_unavailable_reason() -> Option<String> {
Some("the `sabre` feature is not enabled in this build; rebuild with `--features sabre` (or `--features third-party-backends`). This says nothing about whether SaBRe works on this machine -- it has not been checked".to_owned())
}
#[cfg(feature = "e9patch")]
fn e9patch_unavailable_reason() -> Option<String> {
validate_tracing_environment()
.err()
.map(|error| error.to_string())
.or_else(e9patch::unavailable_reason)
}
#[cfg(not(feature = "e9patch"))]
fn e9patch_unavailable_reason() -> Option<String> {
Some("the `e9patch` feature is not enabled in this build; rebuild with `--features e9patch` (or `--features third-party-backends`). This says nothing about whether e9patch works on this machine -- it has not been checked".to_owned())
}
#[cfg(feature = "dbt")]
fn dbt_unavailable_reason() -> Option<String> {
if !dynamorio_sdk_available() {
return Some(
"the DynamoRIO runtime was not found; build target/install_pkg, set HERMIT_INSTALL_DIR, or set DYNAMORIO_HOME/DynamoRIO_DIR to a valid SDK"
.to_owned(),
);
}
dbt_runtime_unavailable_reason()
}
#[cfg(not(feature = "dbt"))]
fn dbt_unavailable_reason() -> Option<String> {
Some("the `dbt` feature is not enabled in this build; rebuild with `--features dbt` (or `--features third-party-backends`). This says nothing about whether DynamoRIO works on this machine -- it has not been checked".to_owned())
}
const SABRE_BINARY_ENV: &str = "HERMIT_SABRE_BINARY";
fn is_executable_file(path: &Path) -> bool {
fs::metadata(path)
.is_ok_and(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
}
fn resolve_sabre_binary_from(
override_path: Option<&OsStr>,
packaged_path: Option<&Path>,
executable: &Path,
path_env: &OsStr,
) -> Result<PathBuf, Error> {
if let Some(requested) = override_path {
if requested.is_empty() {
return Err(anyhow!("{SABRE_BINARY_ENV} is empty"));
}
let path = PathBuf::from(requested);
return is_executable_file(&path)
.then_some(path.clone())
.ok_or_else(|| {
anyhow!(
"{SABRE_BINARY_ENV}={} is not an executable file",
path.display()
)
});
}
if let Some(path) = packaged_path
&& is_executable_file(path)
{
return Ok(path.to_path_buf());
}
let directory = executable
.parent()
.ok_or_else(|| anyhow!("Hermit executable has no parent directory"))?;
let sibling = directory.join("sabre");
let target_build = directory.parent().map(|target| target.join("sabre/sabre"));
if is_executable_file(&sibling) {
return Ok(sibling);
}
if let Some(candidate) = &target_build
&& is_executable_file(candidate)
{
return Ok(candidate.clone());
}
if !path_env.is_empty()
&& let Some(candidate) = std::env::split_paths(path_env)
.map(|directory| directory.join("sabre"))
.find(|candidate| is_executable_file(candidate))
{
return Ok(candidate);
}
Err(anyhow!(
"SaBRe executable was not found in the Hermit installation, beside {}, or in PATH; set {} or {SABRE_BINARY_ENV}",
executable.display(),
hermit_resources::INSTALL_DIR_ENV
))
}
fn resolve_sabre_binary() -> Result<PathBuf, Error> {
let executable =
std::env::current_exe().context("failed to locate running Hermit executable")?;
let override_path = std::env::var_os(SABRE_BINARY_ENV);
let packaged_path = hermit_resources::resource("sabre")?;
let path_env = std::env::var_os("PATH").unwrap_or_default();
resolve_sabre_binary_from(
override_path.as_deref(),
packaged_path.as_deref(),
&executable,
&path_env,
)
}
const SABRE_RPC_SOCKET_ENV: &str = "REVERIE_SABRE_HERMIT_RPC_SOCKET";
const SABRE_DETLOG_FORWARD_ENV: &str = "REVERIE_SABRE_HERMIT_FORWARD_DETLOG";
const SABRE_PATH_EVIDENCE_ENV: &str = "HERMIT_SABRE_PATH_EVIDENCE";
const SABRE_STAGING_DIRECTORY: &str = "/dev/shm";
struct StagedSabreProgram {
path: PathBuf,
}
impl Drop for StagedSabreProgram {
fn drop(&mut self) {
let _ = fs::remove_file(&self.path);
}
}
fn sabre_program_needs_neutral_name(program: &Path) -> bool {
program
.file_name()
.is_some_and(|name| name.as_bytes().starts_with(b"ld"))
}
fn stage_sabre_program_in(
program: &Path,
staging_directory: &Path,
) -> Result<Option<StagedSabreProgram>, Error> {
if !sabre_program_needs_neutral_name(program) {
return Ok(None);
}
let path = staging_directory.join(format!("hermit-sabre-program-{}", std::process::id()));
let mut source = fs::File::open(program).map_err(|error| {
anyhow!(
"failed to open SaBRe guest executable {} for neutral-name staging: {error}",
program.display()
)
})?;
let mut staged = fs::OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o700)
.open(&path)
.map_err(|error| {
anyhow!(
"failed to stage SaBRe guest executable {} as {}: {error}",
program.display(),
path.display()
)
})?;
if let Err(error) = io::copy(&mut source, &mut staged) {
let _ = fs::remove_file(&path);
return Err(anyhow!(
"failed to stage SaBRe guest executable {} as {}: {error}",
program.display(),
path.display()
));
}
drop(staged);
Ok(Some(StagedSabreProgram { path }))
}
fn sabre_runtime_library_path() -> io::Result<PathBuf> {
if let Some(path) = hermit_resources::resource("libdetcore_sabre.so")?
&& path.is_file()
{
return Ok(path);
}
let executable = std::env::current_exe()?;
let directory = executable.parent().ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"Hermit executable has no parent directory",
)
})?;
[
directory.join("libdetcore_sabre.so"),
directory.join("deps/libdetcore_sabre.so"),
]
.into_iter()
.find(|path| path.is_file())
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
format!(
"libdetcore_sabre.so was not built beside {}",
executable.display()
),
)
})
}
#[cfg(feature = "sabre")]
fn sabre_runtime_unavailable_reason() -> Option<String> {
if let Err(error) = resolve_sabre_binary() {
return Some(error.to_string());
}
sabre_runtime_library_path().err().map(|error| {
format!(
"the Detcore SaBRe plugin is unavailable: {error}; build detcore-sabre and hermit in the same target directory"
)
})
}
const SABRE_RPC_DISCONNECT_TIMEOUT: Duration = Duration::from_secs(1);
async fn wait_for_sabre_rpc_disconnects<T>(
global: &Arc<T>,
timeout: Duration,
) -> Result<(), usize> {
let disconnected = tokio::time::timeout(timeout, async {
while Arc::strong_count(global) > 1 {
tokio::time::sleep(Duration::from_millis(1)).await;
}
})
.await;
if disconnected.is_ok() {
return Ok(());
}
let live_references = Arc::strong_count(global).saturating_sub(1);
if live_references == 0 {
Ok(())
} else {
Err(live_references)
}
}
async fn stop_sabre_rpc_server<E>(
server_task: tokio::task::JoinHandle<Result<(), E>>,
) -> Result<(), Error>
where
E: std::fmt::Display,
{
server_task.abort();
match server_task.await {
Err(error) if error.is_cancelled() => Ok(()),
Err(error) => Err(anyhow!("SaBRe coordinator task failed: {error}")),
Ok(Err(error)) => Err(anyhow!("SaBRe coordinator server failed: {error}")),
Ok(Ok(())) => Err(anyhow!("SaBRe coordinator server stopped unexpectedly")),
}
}
async fn shutdown_sabre_rpc<T, E>(
server_task: tokio::task::JoinHandle<Result<(), E>>,
global: &Arc<T>,
timeout: Duration,
) -> Result<(), Error>
where
E: std::fmt::Display,
{
let server_result = stop_sabre_rpc_server(server_task).await;
let disconnect_result = wait_for_sabre_rpc_disconnects(global, timeout).await;
server_result?;
disconnect_result.map_err(|live_references| {
anyhow!("SaBRe coordinator stopped with {live_references} live RPC reference(s)")
})
}
fn ensure_backend_dispatch(backend: Backend) -> Result<(), Error> {
if backend == Backend::Ptrace {
return Ok(());
}
if backend == Backend::E9patch {
return Err(anyhow!(
"backend `e9patch` requires CLI preprocessing; library callers must use \
e9patch::prepare and then select `ptrace`"
));
}
backend.ensure_available()?;
Err(anyhow!(
"backend `{}` has no Hermit dispatch implementation",
backend.as_str()
))
}
async fn run_sabre(
mut command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
capture_output: bool,
) -> Result<Output, Error> {
let path_evidence_file = std::env::var_os(SABRE_PATH_EVIDENCE_ENV).map(PathBuf::from);
let sabre = resolve_sabre_binary()?;
let plugin = sabre_runtime_library_path()
.map_err(|error| anyhow!("failed to locate the Detcore SaBRe plugin: {error}"))?;
let program = command.find_program().map_err(|error| {
anyhow!(
"failed to resolve SaBRe guest executable {:?}: {error}",
command.get_program()
)
})?;
let staged_program = stage_sabre_program_in(&program, Path::new(SABRE_STAGING_DIRECTORY))?;
let launch_program = staged_program
.as_ref()
.map_or(program.as_path(), |staged| staged.path.as_path());
let bootstrap_launch =
sabre_bootstrap::Launch::new(Path::new(&sabre), launch_program, &config)?;
let socket_dir = tempfile::Builder::new()
.prefix("hermit-sabre-rpc-")
.tempdir_in(Path::new("/tmp"))?;
let socket_path = socket_dir.path().join("coordinator.sock");
let fallback_ready = Arc::new(AtomicBool::new(false));
let global = Arc::new(detcore::GlobalState::init_global_state(&config).await);
let server = reverie_rpc_transport::RpcServer::bind_with_readiness(
&socket_path,
global.clone(),
config.clone(),
fallback_ready.clone(),
)
.map_err(|error| anyhow!("failed to start SaBRe coordinator RPC: {error}"))?;
let server_task = tokio::spawn(async move { server.serve().await });
command.prepend_args([
plugin.as_os_str(),
OsStr::new("--"),
launch_program.as_os_str(),
]);
command.program(&sabre);
command.env(SABRE_RPC_SOCKET_ENV, &socket_path);
command.env_remove(sabre_bootstrap::ENVIRONMENT);
if bootstrap_launch.initializes_random() {
command.env(sabre_bootstrap::ENVIRONMENT, "1");
}
command.env(
detcore::CONFIG_FINGERPRINT_ENV,
detcore::config_wire_fingerprint(),
);
command.env_remove(SABRE_PATH_EVIDENCE_ENV);
command.env_remove(SABRE_DETLOG_FORWARD_ENV);
if tracing::enabled!(target: "detcore", tracing::Level::INFO) {
command.env(SABRE_DETLOG_FORWARD_ENV, "1");
}
command.env_remove("SABRE_BINARY");
command.env_remove("SABRE_PLUGIN");
let stats = reverie_sabre_stats::SabreStats::create(reverie::BackendStatsRequest::ENABLED)
.map_err(|error| anyhow!("failed to create the SaBRe statistics page: {error}"))?
.expect("an enabled request creates the statistics page");
command.env(
reverie_sabre_stats::BACKEND_STATS_ENV,
stats.raw_fd().to_string(),
);
tracing::debug!(
target: "hermit::sabre",
socket = %socket_path.display(),
"SaBRe coordinator RPC socket (per-launch temp path, deliberately not at INFO)",
);
tracing::info!(
target: "hermit::sabre",
guest = %program.display(),
plugin = %plugin.display(),
"launching Detcore guest through SaBRe with coordinator RPC",
);
let supervised = match sabre_ptrace::run(
command.into_std_lossy(),
PathBuf::from(&sabre),
plugin.clone(),
fallback_ready,
global.clone(),
bootstrap_launch,
stats.clone(),
capture_output,
)
.await
{
Ok(supervised) => supervised,
Err(error) => {
global.release_all_physical_process_exits();
global.force_shutdown_with_error();
let _ = shutdown_sabre_rpc(server_task, &global, SABRE_RPC_DISCONNECT_TIMEOUT).await;
return Err(error);
}
};
global.release_all_physical_process_exits();
let detcore_never_engaged = !supervised.path_evidence.guest_rpc_observed;
let requires_forced_shutdown = !supervised.status.success() || detcore_never_engaged;
if requires_forced_shutdown {
global.force_shutdown_with_error();
}
let backend_evidence = sabre_backend_evidence_line(&supervised.path_evidence);
tracing::warn!(target: "hermit::sabre", "{backend_evidence}");
if let Some(path) = path_evidence_file {
let mut file = fs::OpenOptions::new()
.create(true)
.append(true)
.mode(0o600)
.open(&path)
.map_err(|error| {
anyhow!(
"failed to open SaBRe path evidence {}: {error}",
path.display()
)
})?;
serde_json::to_writer(&mut file, &supervised.path_evidence)?;
writeln!(file)?;
}
let output = Output {
status: supervised.status.into(),
stdout: supervised.stdout,
stderr: supervised.stderr,
};
shutdown_sabre_rpc(server_task, &global, SABRE_RPC_DISCONNECT_TIMEOUT).await?;
let mut global = Arc::try_unwrap(global).map_err(|global| {
anyhow!(
"SaBRe coordinator stopped with {} live RPC reference(s)",
Arc::strong_count(&global) - 1
)
})?;
if requires_forced_shutdown {
global.cancel_internal_scheduler().await;
}
let dispatch_stats = backend_stats::report(
Backend::Sabre,
backend_stats::request(print_summary_to_json_file),
&stats,
);
global
.clean_up_with_dispatch_stats(print_summary, print_summary_to_json_file, dispatch_stats)
.await;
tracing::info!(
target: "hermit::sabre::fallback",
ptrace_fallback_sites = supervised.path_evidence.ptrace_fallback_sites,
trusted_shared_object_sites = supervised.path_evidence.trusted_shared_object_sites,
guest_rpc_observed = supervised.path_evidence.guest_rpc_observed,
"SaBRe ptrace fallback completed",
);
if detcore_never_engaged {
return Err(anyhow!(
"{}",
sabre_uninstrumented_guest_message(&output.status)
));
}
Ok(output)
}
fn sabre_reach_state(guest_rpc_observed: bool, ptrace_fallback_sites: usize) -> &'static str {
match (guest_rpc_observed, ptrace_fallback_sites) {
(false, _) => "no-detcore-reached",
(true, 1..) => "degraded-ptrace-fallback",
(true, 0) => "sabre-exercised",
}
}
fn sabre_backend_evidence_line(evidence: &sabre_ptrace::PathEvidence) -> String {
format!(
":: Backend: sabre static rewriting + ptrace runtime; run_mode=run; \
evidence_schema={}; preplugin_coverage=absent; ptrace_fallback_sites={}; \
trusted_shared_object_sites={}; guest_rpc_observed={}; reach_state={}",
evidence.schema,
evidence.ptrace_fallback_sites,
evidence.trusted_shared_object_sites,
evidence.guest_rpc_observed,
sabre_reach_state(evidence.guest_rpc_observed, evidence.ptrace_fallback_sites),
)
}
fn sabre_uninstrumented_guest_message(status: &ExitStatus) -> String {
format!(
"the SaBRe backend finished ({status:?}) without the guest ever reaching the Detcore \
coordinator: no syscall was intercepted, so this run applied no determinization at all \
and its result is not a Hermit guarantee. This means SaBRe rewrote no syscall site in \
the guest -- most often a statically linked ELF, whose syscall sites SaBRe must patch \
in the client image itself because there is no dynamic loader to intercept."
)
}
const KVM_GUEST_MEMORY_BYTES: usize = 1024 * 1024 * 1024;
const MAX_SHEBANG_DEPTH: usize = 4;
fn resolve_kvm_shebang(
resolved_program: &Path,
mut argv: Vec<String>,
) -> Result<(PathBuf, Vec<String>, Vec<u8>), Error> {
let mut load_path = resolved_program.to_path_buf();
let mut image = fs::read(&load_path)
.map_err(|error| anyhow!("failed to read KVM guest executable {load_path:?}: {error}"))?;
let mut depth = 0;
while image.starts_with(b"#!") {
depth += 1;
if depth > MAX_SHEBANG_DEPTH {
return Err(anyhow!(
"too many levels of `#!` interpreter indirection loading {resolved_program:?}"
));
}
let (interpreter, shebang_args) = Shebang::from_buf(&image)
.ok_or_else(|| anyhow!("malformed `#!` interpreter line in {load_path:?}"))?
.into_parts();
let interpreter_str = interpreter
.to_str()
.ok_or_else(|| anyhow!("non-UTF-8 `#!` interpreter path in {load_path:?}"))?
.to_owned();
let mut rewritten = Vec::with_capacity(argv.len() + shebang_args.len() + 2);
rewritten.push(interpreter_str);
for arg in &shebang_args {
rewritten.push(
arg.to_str()
.ok_or_else(|| anyhow!("non-UTF-8 `#!` interpreter argument in {load_path:?}"))?
.to_owned(),
);
}
rewritten.push(load_path.to_string_lossy().into_owned());
rewritten.extend_from_slice(&argv[1..]);
argv = rewritten;
load_path = interpreter;
image = fs::read(&load_path).map_err(|error| {
anyhow!(
"failed to read `#!` interpreter {load_path:?} for {resolved_program:?}: {error}"
)
})?;
}
Ok((load_path, argv, image))
}
const CONTAINER_REPLACED_TMP: &str = "/tmp";
fn kvm_cwd_resolution_error(requested_cwd: &Path, error: &std::io::Error) -> Error {
let under_replaced_tmp = requested_cwd.starts_with(CONTAINER_REPLACED_TMP)
&& requested_cwd != Path::new(CONTAINER_REPLACED_TMP);
if error.kind() == std::io::ErrorKind::NotFound && under_replaced_tmp {
anyhow!(
"failed to resolve KVM guest working directory {:?}: {error}\n\
\n\
This directory may well exist on the host. The container replaces \
{CONTAINER_REPLACED_TMP} with a private, empty tmpfs, so nothing beneath \
{CONTAINER_REPLACED_TMP} is visible to the guest, and resolving the working \
directory happens inside that mount namespace.\n\
Either run from a directory outside {CONTAINER_REPLACED_TMP}, or pass \
--workdir with a path that exists inside the guest.",
requested_cwd
)
} else {
anyhow!(
"failed to resolve KVM guest working directory {:?}: {error}",
requested_cwd
)
}
}
fn prepare_kvm_mountinfo_config(
config: &mut DetConfig,
capture: impl FnOnce() -> Result<Vec<u64>, Error>,
) -> Result<(), Error> {
if !config.mountinfo_mount_ids_captured
&& (!config.mountinfo_mount_ids.is_empty() || !config.fdinfo_unlisted_mount_ids.is_empty())
{
anyhow::bail!("KVM mount identities have no producer-captured namespace");
}
let order = capture().context("failed to capture the KVM backend mount namespace")?;
if config.mountinfo_mount_ids_captured {
if config.mountinfo_mount_ids != order {
anyhow::bail!(
"KVM producer-captured mount identities do not match the backend namespace"
);
}
} else {
config.mountinfo_mount_ids = order;
config.mountinfo_mount_ids_captured = true;
}
config.mountinfo_device_rewrites.clear();
Ok(())
}
fn kvm_reports_stored_metadata_timestamps(config: &DetConfig) -> bool {
config.virtualize_metadata
&& config.backend_dispatches_thread_tools
&& config.sequentialize_threads
}
async fn run_kvm(
command: &Command,
mut config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
capture_output: bool,
) -> Result<Output, Error> {
let dispatch_started = Instant::now();
prepare_kvm_mountinfo_config(&mut config, capture_mountinfo_identity_order)?;
let stdin = if capture_output {
let (snapshot_reserved, snapshot) = output_backend_stdin_reservation()?;
if snapshot_reserved {
snapshot
} else {
reserved_kvm_stdin()?
}
} else {
reserved_kvm_stdin()?
};
let requested_cwd = command
.get_current_dir()
.map(Path::to_owned)
.unwrap_or(std::env::current_dir()?);
let cwd = fs::canonicalize(&requested_cwd)
.map_err(|error| kvm_cwd_resolution_error(&requested_cwd, &error))?;
let program = command
.get_program()
.to_str()
.ok_or_else(|| anyhow!("KVM guest executable path is not valid UTF-8"))?
.to_owned();
if !cwd.is_dir() {
return Err(anyhow!(
"KVM guest working directory is not a directory: {:?}",
cwd
));
}
let resolved_program = command.find_program().map_err(|error| {
anyhow!("failed to resolve KVM guest executable {program:?} in the guest PATH: {error}")
})?;
let mut argv = Vec::with_capacity(1 + command.get_args().count());
argv.push(program.clone());
for argument in command.get_args() {
argv.push(
argument
.to_str()
.ok_or_else(|| anyhow!("KVM guest argument is not valid UTF-8"))?
.to_owned(),
);
}
let (_interpreter_path, argv, image) = resolve_kvm_shebang(&resolved_program, argv)?;
let program = argv.first().cloned().unwrap_or(program);
let envp = command
.get_captured_envs()
.into_iter()
.map(|(key, value)| {
let key = key
.to_str()
.ok_or_else(|| anyhow!("KVM guest environment key is not valid UTF-8"))?;
let value = value
.to_str()
.ok_or_else(|| anyhow!("KVM guest environment value is not valid UTF-8"))?;
Ok(format!("{key}={value}"))
})
.collect::<Result<Vec<_>, Error>>()?;
tracing::info!(
target: "hermit::kvm",
program = %program,
argv = ?argv,
cwd = %cwd.display(),
env_count = envp.len(),
"launching guest through reverie-kvm",
);
let argv = argv.iter().map(String::as_str).collect::<Vec<_>>();
let envp = envp.iter().map(String::as_str).collect::<Vec<_>>();
let setup_started = Instant::now();
config.cpuid_virtualized_by_backend = true;
config.backend_supports_madvise = false;
config.has_uts_namespace = false;
let random_seed = config.rng_seed();
let mut backend = reverie_kvm::KvmBackend::new_with_stdin(KVM_GUEST_MEMORY_BYTES, stdin)
.map_err(|error| anyhow!("failed to initialize reverie-kvm: {error}"))?;
backend
.set_root_pid(detcore::ROOT_DETPID.as_raw())
.map_err(|error| anyhow!("failed to configure KVM root PID: {error}"))?;
backend
.install_static_elf_with_context(&image, &argv, &envp, &cwd)
.map_err(|error| anyhow!("failed to load KVM guest executable {program:?}: {error}"))?;
backend
.set_random_seed(random_seed)
.map_err(|error| anyhow!("failed to configure KVM guest random seed: {error}"))?;
if !config.backend_dispatches_thread_tools {
backend.unmonitored_threads();
}
if kvm_reports_stored_metadata_timestamps(&config) {
backend
.set_host_metadata_timestamps(true)
.map_err(|error| anyhow!("failed to configure KVM metadata timestamps: {error}"))?;
}
let execution_started = Instant::now();
let completion = backend
.run_static_elf_with_tool_completion::<Detcore>(config, capture_output)
.await
.map_err(|error| kvm_execution_error(error, None))?;
let cleanup_started = Instant::now();
let result =
finish_kvm_tool_completion(completion, print_summary, print_summary_to_json_file).await;
let teardown_started = Instant::now();
drop(backend);
let teardown_finished = Instant::now();
let (code, stdout, stderr) = result?;
tracing::debug!(
target: "hermit::kvm",
prepare_us = setup_started.duration_since(dispatch_started).as_micros() as u64,
setup_us = execution_started.duration_since(setup_started).as_micros() as u64,
execution_us = cleanup_started.duration_since(execution_started).as_micros() as u64,
cleanup_us = teardown_started.duration_since(cleanup_started).as_micros() as u64,
teardown_us = teardown_finished.duration_since(teardown_started).as_micros() as u64,
lifecycle_us = teardown_finished.duration_since(dispatch_started).as_micros() as u64,
"reverie-kvm lifecycle phase timings",
);
if !capture_output {
std::io::stdout().write_all(&stdout)?;
std::io::stderr().write_all(&stderr)?;
}
Ok(Output {
status: ExitStatus::Exited(code),
stdout,
stderr,
})
}
async fn finish_kvm_tool_completion(
completion: reverie_kvm::ToolRunCompletion<detcore::GlobalState>,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
) -> Result<(i32, Vec<u8>, Vec<u8>), Error> {
match completion.result {
Ok(output) => {
completion
.global_state
.clean_up(print_summary, print_summary_to_json_file)
.await;
Ok(output)
}
Err(error) => {
let cleanup = completion
.global_state
.clean_up_after_backend_failure()
.await;
Err(kvm_execution_error(error, Some(cleanup)))
}
}
}
fn kvm_execution_error(
primary: reverie_kvm::Error,
cleanup: Option<detcore::BackendFailureCleanup>,
) -> Error {
let message = format!("KVM guest execution failed: {primary}");
let mut error = Error::new(primary);
if let Some(cleanup) = cleanup {
if let Err(recording) = cleanup.preemption_recording {
error = error.context(format!("partial preemption recording failed: {recording}"));
}
if let Err(scheduler) = cleanup.scheduler {
error = error.context(scheduler);
}
}
error.context(message)
}
#[cfg(test)]
mod kvm_failure_tests;
#[cfg(all(test, feature = "kvm-execution-tests"))]
mod kvm_execution_tests;
#[cfg(feature = "dbt")]
fn dbt_client_thread_start_failed(status: &std::process::ExitStatus) -> bool {
status.code() == Some(reverie_dbt::CLIENT_THREAD_START_FAILURE_EXIT_CODE)
}
#[cfg(feature = "dbt")]
async fn run_dbt(
command: Command,
config: DetConfig,
print_summary: bool,
capture_output: bool,
) -> Result<Output, Error> {
if !config.sequentialize_threads {
return Err(anyhow!(
"the dbt backend requires sequentialized threads; remove \
--no-sequentialize-threads (or --strace-only) to run under --backend dbt"
));
}
let config_json = serde_json::to_string(&config)
.map_err(|error| anyhow!("failed to serialize the Detcore config for DBT: {error}"))?;
let panic_on_unsupported_syscalls = config.panic_on_unsupported_syscalls;
let (drrun, client) = detcore_dbt::prepare_native_client()
.map_err(|error| anyhow!("failed to prepare the Detcore DynamoRIO client: {error}"))?;
let mut runner = reverie_dbt::DbtRunner::new(&drrun, &client)
.map_err(|error| {
anyhow!(
"failed to configure the DynamoRIO DBT runner (drrun={}, client={}): {error}",
drrun.display(),
client.display()
)
})?
.summary(print_summary)
.isolated_process_group(panic_on_unsupported_syscalls);
if panic_on_unsupported_syscalls {
runner = runner.client_argument("-panic-on-unsupported-syscalls");
}
let program = command.get_program().to_owned();
let mut environment = command.get_captured_envs();
environment.insert(detcore_dbt::DETCONFIG_ENV.into(), config_json.into());
let guest = command.into_std_lossy();
tracing::info!(
target: "hermit::dbt",
program = ?program,
drrun = %drrun.display(),
client = %client.display(),
"launching guest through reverie-dbt with Detcore<DbtGuest>",
);
let (status, stdout, stderr, mut global) = if capture_output {
let launch = || {
runner.output_with_environment_and_global::<detcore::GlobalState>(
&guest,
&environment,
config.clone(),
)
};
let (mut output, mut global) = launch()
.await
.map_err(|error| anyhow!("failed to launch drrun ({}): {error}", drrun.display()))?;
if dbt_client_thread_start_failed(&output.status) {
tracing::warn!(
target: "hermit::dbt",
"DynamoRIO client thread failed before guest start; retrying once",
);
global.force_shutdown_with_error();
global.cancel_internal_scheduler().await;
global.clean_up(false, &None).await;
(output, global) = launch().await.map_err(|error| {
anyhow!("failed to launch drrun ({}): {error}", drrun.display())
})?;
}
(output.status, output.stdout, output.stderr, global)
} else {
let launch = || {
runner.status_with_environment_and_global::<detcore::GlobalState>(
&guest,
&environment,
config.clone(),
)
};
let (mut status, mut global) = launch()
.await
.map_err(|error| anyhow!("failed to launch drrun ({}): {error}", drrun.display()))?;
if dbt_client_thread_start_failed(&status) {
tracing::warn!(
target: "hermit::dbt",
"DynamoRIO client thread failed before guest start; retrying once",
);
global.force_shutdown_with_error();
global.cancel_internal_scheduler().await;
global.clean_up(false, &None).await;
(status, global) = launch().await.map_err(|error| {
anyhow!("failed to launch drrun ({}): {error}", drrun.display())
})?;
}
(status, Vec::new(), Vec::new(), global)
};
if !status.success() {
global.force_shutdown_with_error();
global.cancel_internal_scheduler().await;
}
global.clean_up(print_summary, &None).await;
Ok(Output {
status: status.into(),
stdout,
stderr,
})
}
pub fn run(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
) -> Result<ExitStatus, Error> {
run_with_backend(
command,
config,
print_summary,
print_summary_to_json_file,
Backend::Ptrace,
)
}
pub fn run_with_backend(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
backend: Backend,
) -> Result<ExitStatus, Error> {
run_with_backend_timeout(
command,
config,
print_summary,
print_summary_to_json_file,
backend,
None,
)
}
pub fn run_with_backend_timeout(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
backend: Backend,
timeout: Option<Duration>,
) -> Result<ExitStatus, Error> {
if backend == Backend::Ptrace {
let summary_path = print_summary_to_json_file.clone();
return ptrace_completion::run(timeout, move |control| async move {
let report = SkidOvershootReport::begin(true);
let config = prepare_backend_config(config, backend);
let result = dispatch_backend(
command,
config,
print_summary,
&summary_path,
backend,
Some(control),
)
.await;
report.finish(result)
});
}
let skid_overshoot_report = SkidOvershootReport::begin(backend.uses_ptrace_pmu_timers());
if backend == Backend::Kvm {
ensure_kvm_stdin_reserved()?;
}
let config = prepare_backend_config(config, backend);
let result = run_with_backend_inner(
command,
config,
print_summary,
print_summary_to_json_file,
backend,
timeout,
);
skid_overshoot_report.finish(result)
}
#[doc(hidden)]
pub fn prepare_backend_config(config: DetConfig, backend: Backend) -> DetConfig {
let liteinst_runtime = if backend == Backend::Liteinst {
LiteinstRuntime::from_env().unwrap_or(LiteinstRuntime::HostHybrid)
} else {
LiteinstRuntime::HostHybrid
};
prepare_backend_config_for_liteinst_runtime(config, backend, liteinst_runtime)
}
#[doc(hidden)]
pub fn prepare_backend_config_for_liteinst_runtime(
mut config: DetConfig,
backend: Backend,
liteinst_runtime: LiteinstRuntime,
) -> DetConfig {
let in_guest_liteinst =
backend == Backend::Liteinst && liteinst_runtime == LiteinstRuntime::InGuest;
config.discover_live_file_metadata = backend == Backend::Sabre;
config.use_thread_local_clock_reads = false;
config.detect_host_clock_futex_timeouts = backend == Backend::Sabre;
config.syscall_clobbers_virtualized_by_backend = backend == Backend::Sabre;
config.cancel_killed_thread_rpcs =
in_guest_liteinst || matches!(backend, Backend::Sabre | Backend::Dbt | Backend::Kvm);
config.backend_reports_physical_process_exits = backend == Backend::Sabre;
config.backend_serializes_fork_children = false;
config.backend_dispatches_thread_tools = true;
config.backend_tracks_process_children = backend != Backend::Dbt;
config.backend_runs_exit_robust_list = backend == Backend::Ptrace;
config.backend_requires_thread_directed_process_signals = backend == Backend::Dbt;
config.backend_is_kvm = backend == Backend::Kvm;
config.kvm_shared_dequeue_timers = config.backend_is_kvm && config.sequentialize_threads;
config.backend_supports_parked_write_signal_interruption =
matches!(backend, Backend::Ptrace | Backend::E9patch);
config.backend_virtualizes_capability_prctls = backend == Backend::Kvm;
config.backend_defers_vfork_child_registration = backend == Backend::Kvm;
config
}
#[cfg(feature = "liteinst")]
fn liteinst_requires_forced_shutdown(status: ExitStatus) -> bool {
match status {
ExitStatus::Signaled(_, _) => true,
ExitStatus::Exited(code) => {
(122..=127).contains(&code) || detcore_model::signal_from_exit_status(code).is_some()
}
}
}
#[derive(Debug)]
pub struct GuestTimedOut {
pub limit: Duration,
}
impl std::fmt::Display for GuestTimedOut {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Guest exceeded the --timeout bound of {} seconds",
self.limit.as_secs()
)
}
}
impl std::error::Error for GuestTimedOut {}
async fn with_run_deadline<F>(timeout: Option<Duration>, guest: F) -> Result<ExitStatus, Error>
where
F: std::future::Future<Output = Result<ExitStatus, Error>>,
{
match timeout {
None => guest.await,
Some(limit) => match tokio::time::timeout(limit, guest).await {
Ok(result) => result,
Err(_) => Err(Error::new(GuestTimedOut { limit })),
},
}
}
#[tokio::main(flavor = "current_thread")]
async fn run_with_backend_inner(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
backend: Backend,
timeout: Option<Duration>,
) -> Result<ExitStatus, Error> {
refuse_in_guest_liteinst_timeout(backend, timeout)?;
let guest = Box::pin(dispatch_backend(
command,
config,
print_summary,
print_summary_to_json_file,
backend,
None,
));
with_run_deadline(timeout, guest).await
}
async fn dispatch_backend(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
backend: Backend,
control: Option<std::rc::Rc<ptrace_completion::Control>>,
) -> Result<ExitStatus, Error> {
if backend == Backend::Kvm {
return Ok(run_kvm(
&command,
config,
print_summary,
print_summary_to_json_file,
false,
)
.await?
.status);
}
if backend == Backend::Dbt {
#[cfg(feature = "dbt")]
{
return Ok(run_dbt(command, config, print_summary, false).await?.status);
}
#[cfg(not(feature = "dbt"))]
{
backend.ensure_available()?;
unreachable!("DBT availability must fail when the feature is disabled");
}
}
if backend == Backend::Sabre {
return Ok(run_sabre(
command,
config,
print_summary,
print_summary_to_json_file,
false,
)
.await?
.status);
}
if backend == Backend::Liteinst {
#[cfg(feature = "liteinst")]
{
let stats_request = backend_stats::request(print_summary_to_json_file);
let (exit_status, mut global_state, dispatch_stats) = match LiteinstRuntime::from_env()?
{
LiteinstRuntime::InGuest => {
let mut command = command;
refuse_in_guest_liteinst_run(&mut command, &config)?;
let preload = liteinst_tool_runtime_library_path()?;
if stats_request.is_enabled() {
let (exit_status, global_state, source) =
reverie_liteinst::LiteinstBackend::run_with_preload_and_stats::<Detcore>(
command, config, preload,
)
.await?;
let dispatch_stats = backend_stats::report(backend, stats_request, &source);
(exit_status, global_state, dispatch_stats)
} else {
let (exit_status, global_state) =
reverie_liteinst::LiteinstBackend::run_with_preload::<Detcore>(
command, config, preload,
)
.await?;
(exit_status, global_state, None)
}
}
LiteinstRuntime::HostHybrid => {
let preload = liteinst_runtime_library_path()?;
if stats_request.is_enabled() {
let (exit_status, global_state, source) =
reverie_liteinst::LiteinstBackend::run_host_with_preload_and_stats::<
Detcore,
>(command, config, preload)
.await?;
let dispatch_stats = backend_stats::report(backend, stats_request, &source);
(exit_status, global_state, dispatch_stats)
} else {
let (exit_status, global_state) =
reverie_liteinst::LiteinstBackend::run_host_with_preload::<Detcore>(
command, config, preload,
)
.await?;
(exit_status, global_state, None)
}
}
};
if liteinst_requires_forced_shutdown(exit_status) {
global_state.force_shutdown_with_error();
global_state.cancel_internal_scheduler().await;
}
global_state
.clean_up_with_dispatch_stats(
print_summary,
print_summary_to_json_file,
dispatch_stats,
)
.await;
return Ok(exit_status);
}
#[cfg(not(feature = "liteinst"))]
{
backend.ensure_available()?;
unreachable!("LiteInst availability must fail when the feature is disabled");
}
}
ensure_backend_dispatch(backend)?;
let stats_request = backend_stats::request(print_summary_to_json_file);
let mut builder = reverie_ptrace::TracerBuilder::<Detcore>::new(command).config(config.clone());
if config.gdbserver {
builder = builder.gdbserver(config.gdbserver_port);
if config.sequentialize_threads {
builder = builder.sequentialized_guest();
}
}
let control = control
.ok_or_else(|| Error::msg("ordinary ptrace requires its retained operation owner"))?;
let tracer = builder.backend_stats(stats_request).spawn().await?;
let stats_source = tracer.backend_stats();
let (exit_status, global_state) = ptrace_completion::wait(tracer, control).await?;
let dispatch_stats =
backend_stats::report_if_collected(backend, stats_request, stats_source.as_ref());
global_state
.clean_up_with_dispatch_stats(print_summary, print_summary_to_json_file, dispatch_stats)
.await; Ok(exit_status)
}
pub fn run_with_output(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
) -> Result<Output, Error> {
run_with_output_backend(
command,
config,
print_summary,
print_summary_to_json_file,
Backend::Ptrace,
)
}
pub fn run_with_output_backend(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
backend: Backend,
) -> Result<Output, Error> {
if backend == Backend::Kvm {
ensure_kvm_stdin_reserved()?;
}
run_with_output_backend_timeout(
command,
config,
print_summary,
print_summary_to_json_file,
backend,
None,
)
}
pub fn run_with_output_backend_timeout(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
backend: Backend,
timeout: Option<Duration>,
) -> Result<Output, Error> {
if backend == Backend::Ptrace {
let summary_path = print_summary_to_json_file.clone();
return ptrace_completion::run(timeout, move |control| async move {
let report = SkidOvershootReport::begin(true);
let result = dispatch_output_backend(
command,
prepare_backend_config(config, backend),
print_summary,
&summary_path,
backend,
Some(control),
)
.await;
report.finish(result)
});
}
let (output, skid_overshoots) = run_with_output_backend_timeout_and_skid_overshoots(
command,
config,
print_summary,
print_summary_to_json_file,
backend,
timeout,
)?;
if skid_overshoots > 0 {
return Err(Error::new(SkidOvershootError::new(skid_overshoots)));
}
Ok(output)
}
#[doc(hidden)]
pub fn run_with_output_backend_timeout_and_skid_overshoots(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
backend: Backend,
timeout: Option<Duration>,
) -> Result<(Output, u64), Error> {
if backend == Backend::Ptrace {
let summary_path = print_summary_to_json_file.clone();
return ptrace_completion::run(timeout, move |control| async move {
let report = SkidOvershootReport::begin(true);
let result = dispatch_output_backend(
command,
prepare_backend_config(config, backend),
print_summary,
&summary_path,
backend,
Some(control),
)
.await;
report.finish_with_count(result)
});
}
let skid_overshoot_report = SkidOvershootReport::begin(backend.uses_ptrace_pmu_timers());
if backend == Backend::Kvm {
ensure_kvm_stdin_reserved()?;
}
let config = prepare_backend_config(config, backend);
let result = run_with_output_backend_inner(
command,
config,
print_summary,
print_summary_to_json_file,
backend,
timeout,
);
skid_overshoot_report.finish_with_count(result)
}
#[tokio::main(flavor = "current_thread")]
async fn run_with_output_backend_inner(
command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
backend: Backend,
timeout: Option<Duration>,
) -> Result<Output, Error> {
refuse_in_guest_liteinst_timeout(backend, timeout)?;
let Some(limit) = timeout else {
return dispatch_output_backend(
command,
config,
print_summary,
print_summary_to_json_file,
backend,
None,
)
.await;
};
match tokio::time::timeout(
limit,
dispatch_output_backend(
command,
config,
print_summary,
print_summary_to_json_file,
backend,
None,
),
)
.await
{
Ok(result) => result,
Err(_elapsed) => Err(Error::new(GuestTimedOut { limit })),
}
}
async fn dispatch_output_backend(
mut command: Command,
config: DetConfig,
print_summary: bool,
print_summary_to_json_file: &Option<PathBuf>,
backend: Backend,
control: Option<std::rc::Rc<ptrace_completion::Control>>,
) -> Result<Output, Error> {
if backend == Backend::Kvm {
return run_kvm(
&command,
config,
print_summary,
print_summary_to_json_file,
true,
)
.await;
}
if backend == Backend::Dbt {
#[cfg(feature = "dbt")]
{
return run_dbt(command, config, print_summary, true).await;
}
#[cfg(not(feature = "dbt"))]
{
backend.ensure_available()?;
unreachable!("DBT availability must fail when the feature is disabled");
}
}
if backend == Backend::Sabre {
command.stdin(output_backend_stdin()?);
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
return run_sabre(
command,
config,
print_summary,
print_summary_to_json_file,
true,
)
.await;
}
if backend == Backend::Liteinst {
#[cfg(feature = "liteinst")]
{
command.stdin(output_backend_stdin()?);
let stats_request = backend_stats::request(print_summary_to_json_file);
let (output, mut global_state, dispatch_stats) = match LiteinstRuntime::from_env()? {
LiteinstRuntime::InGuest => {
refuse_in_guest_liteinst_run(&mut command, &config)?;
let preload = liteinst_tool_runtime_library_path()?;
let (output, global_state, dispatch_stats) = if stats_request.is_enabled() {
let (output, global_state, source) =
reverie_liteinst::LiteinstBackend::run_with_output_and_preload_and_stats::<
Detcore,
>(command, config, preload)
.await?;
let dispatch_stats = backend_stats::report(backend, stats_request, &source);
(output, global_state, dispatch_stats)
} else {
let (output, global_state) =
reverie_liteinst::LiteinstBackend::run_with_output_and_preload::<
Detcore,
>(command, config, preload)
.await?;
(output, global_state, None)
};
let output = Output {
status: output.status.into(),
stdout: output.stdout,
stderr: output.stderr,
};
(output, global_state, dispatch_stats)
}
LiteinstRuntime::HostHybrid => {
let preload = liteinst_runtime_library_path()?;
if stats_request.is_enabled() {
let (output, global_state, source) =
reverie_liteinst::LiteinstBackend::run_host_with_output_and_preload_and_stats::<
Detcore,
>(command, config, preload)
.await?;
let dispatch_stats = backend_stats::report(backend, stats_request, &source);
(output, global_state, dispatch_stats)
} else {
let (output, global_state) =
reverie_liteinst::LiteinstBackend::run_host_with_output_and_preload::<
Detcore,
>(command, config, preload)
.await?;
(output, global_state, None)
}
}
};
let status = output.status;
if liteinst_requires_forced_shutdown(status) {
global_state.force_shutdown_with_error();
global_state.cancel_internal_scheduler().await;
}
global_state
.clean_up_with_dispatch_stats(
print_summary,
print_summary_to_json_file,
dispatch_stats,
)
.await;
return Ok(Output {
status,
stdout: output.stdout,
stderr: output.stderr,
});
}
#[cfg(not(feature = "liteinst"))]
{
backend.ensure_available()?;
unreachable!("LiteInst availability must fail when the feature is disabled");
}
}
ensure_backend_dispatch(backend)?;
let stats_request = backend_stats::request(print_summary_to_json_file);
command.stdin(output_backend_stdin()?);
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
let mut builder = reverie_ptrace::TracerBuilder::<Detcore>::new(command).config(config.clone());
if config.gdbserver {
builder = builder.gdbserver(config.gdbserver_port);
if config.sequentialize_threads {
builder = builder.sequentialized_guest();
}
}
let control = control
.ok_or_else(|| Error::msg("ordinary ptrace requires its retained operation owner"))?;
let tracer = builder.backend_stats(stats_request).spawn().await?;
let stats_source = tracer.backend_stats();
let (output, global_state) = ptrace_completion::wait_with_output(tracer, control).await?;
let dispatch_stats =
backend_stats::report_if_collected(backend, stats_request, stats_source.as_ref());
global_state
.clean_up_with_dispatch_stats(print_summary, print_summary_to_json_file, dispatch_stats)
.await;
Ok(output)
}
pub struct HermitData {
data_dir: PathBuf,
}
fn collect_recording_ids(
entries: impl IntoIterator<Item = io::Result<fs::DirEntry>>,
data_dir: &Path,
mut file_type: impl FnMut(&fs::DirEntry) -> io::Result<fs::FileType>,
) -> Result<Vec<Id>, Error> {
let mut recordings = Vec::new();
for entry in entries {
let entry = entry.with_context(|| {
format!(
"Failed to read an entry in recording inventory {}",
data_dir.display()
)
})?;
let path = entry.path();
if file_type(&entry)
.with_context(|| {
format!(
"Failed to inspect recording inventory entry {}",
path.display()
)
})?
.is_dir()
&& let Some(id) = entry
.file_name()
.to_str()
.and_then(|name| name.parse::<Id>().ok())
{
recordings.push(id);
}
}
Ok(recordings)
}
impl Default for HermitData {
fn default() -> Self {
Self::new()
}
}
impl HermitData {
pub fn new() -> Self {
Self::with_dir(
dirs::cache_dir()
.map_or_else(|| PathBuf::from("/tmp/hermit"), |dir| dir.join("hermit")),
)
}
pub fn with_dir<P>(data_dir: P) -> Self
where
P: Into<PathBuf>,
{
Self {
data_dir: data_dir.into(),
}
}
pub fn data_dir(&self) -> &Path {
&self.data_dir
}
pub fn record(&self, command: Command) -> Result<Recording, Error> {
let store = Self {
data_dir: self.data_dir.clone(),
};
ptrace_completion::run(None, move |control| async move {
let data = store.create_recording_dir()?;
let exit_status =
record_to_async(command, data.path(), Vec::new(), None, None, control).await?;
store.commit_recording(data, exit_status)
})
}
pub fn record_with_mountinfo(
&self,
command: Command,
mountinfo_root_rewrites: Vec<detcore_model::config::MountInfoRootRewrite>,
) -> Result<Recording, Error> {
let store = Self {
data_dir: self.data_dir.clone(),
};
ptrace_completion::run(None, move |control| async move {
let data = store.create_recording_dir()?;
let mount_ids = capture_mountinfo_identity_order()?;
let exit_status = record_to_async(
command,
data.path(),
mountinfo_root_rewrites,
Some(mount_ids),
None,
control,
)
.await?;
store.commit_recording(data, exit_status)
})
}
pub fn create_recording_dir(&self) -> Result<tempfile::TempDir, Error> {
let tmp_data_dir = self.data_dir.join("tmp");
fs::create_dir_all(&tmp_data_dir).with_context(|| {
format!(
"Failed to create recording directory: {}",
self.data_dir.display()
)
})?;
Ok(tempfile::TempDir::new_in(tmp_data_dir)?)
}
pub fn commit_recording(
&self,
data: tempfile::TempDir,
exit_status: ExitStatus,
) -> Result<Recording, Error> {
let id = Id::unique();
fs::rename(data.keep(), self.data_dir.join(id.to_string()))?;
self.update_last_id(&id)
.with_context(|| format!("Failed to update {:?}", self.data_dir.join("last")))?;
Ok(Recording { id, exit_status })
}
pub fn replay(&self, id: Id) -> Result<ExitStatus, Error> {
let data = self.data_dir.join(id.to_string());
replay_from(&data)
}
pub fn replay_with_gdbserver(&self, id: Id, port: u16) -> Result<ExitStatus, Error> {
let data = self.data_dir.join(id.to_string());
replay_with_gdbserver(&data, port)
}
pub fn recordings(&self) -> impl Iterator<Item = Id> + use<> {
fs::read_dir(&self.data_dir)
.ok()
.into_iter()
.flatten()
.filter_map(|entry| {
let entry = entry.ok()?;
if entry.file_type().ok()?.is_dir() {
Some(entry.file_name().to_str()?.parse::<Id>().ok()?)
} else {
None
}
})
}
pub fn try_recordings(&self) -> Result<Vec<Id>, Error> {
let entries = match fs::read_dir(&self.data_dir) {
Ok(entries) => entries,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(error) => {
return Err(error).with_context(|| {
format!(
"Failed to read recording inventory in {}",
self.data_dir.display()
)
});
}
};
collect_recording_ids(entries, &self.data_dir, fs::DirEntry::file_type)
}
pub fn recording_metadata(&self, id: Id) -> Result<Metadata, Error> {
Metadata::load(&self.data_dir.join(id.to_string()))
}
pub fn remove(&self, id: Id) -> Result<(), Error> {
let path = self.data_dir.join(id.to_string());
let metadata_path = path.join(METADATA_NAME);
let metadata = fs::metadata(&metadata_path)
.with_context(|| format!("Failed to find {:?}", metadata_path))?;
if !metadata.is_file() {
return Err(anyhow!("{:?} is not a file", metadata_path));
}
fs::remove_dir_all(path)?;
Ok(())
}
pub fn last_id(&self) -> Result<Id, Error> {
Ok(fs::read_to_string(self.data_dir.join("last"))?.parse()?)
}
fn update_last_id(&self, id: &Id) -> Result<(), Error> {
let mut file = tempfile::NamedTempFile::new_in(self.data_dir.join("tmp"))?;
write!(file, "{}", id)?;
file.persist(self.data_dir.join("last"))?;
Ok(())
}
}
pub fn capture_mountinfo_identity_order() -> Result<Vec<u64>, Error> {
mountinfo_identity_order_from(&fs::read("/proc/self/mountinfo")?)
}
fn mountinfo_identity_order_from(contents: &[u8]) -> Result<Vec<u64>, Error> {
use std::collections::BTreeSet;
let rows = detcore_model::procfs::parse_mountinfo(contents)
.ok_or_else(|| anyhow!("malformed /proc/self/mountinfo"))?;
let mut visible = Vec::new();
let mut parents = Vec::new();
let mut seen = BTreeSet::new();
for row in rows {
seen.insert(row.raw_mount_id);
visible.push(row.raw_mount_id);
parents.push(row.raw_parent_id);
}
for parent in parents {
if seen.insert(parent) {
visible.push(parent);
}
}
Ok(visible)
}
impl<'a> From<Option<&'a PathBuf>> for HermitData {
fn from(data_dir: Option<&'a PathBuf>) -> Self {
data_dir.map_or_else(Self::new, Self::with_dir)
}
}
pub fn record_to(command: Command, dir: &Path) -> Result<ExitStatus, Error> {
let dir = dir.to_owned();
ptrace_completion::run(None, move |control| async move {
record_to_async(command, &dir, Vec::new(), None, None, control).await
})
}
pub fn record_to_with_mountinfo(
command: Command,
dir: &Path,
mountinfo_root_rewrites: Vec<detcore_model::config::MountInfoRootRewrite>,
local_networking: bool,
) -> Result<ExitStatus, Error> {
let dir = dir.to_owned();
ptrace_completion::run(None, move |control| async move {
let mount_ids = capture_mountinfo_identity_order()?;
record_to_async(
command,
&dir,
mountinfo_root_rewrites,
Some(mount_ids),
Some(local_networking),
control,
)
.await
})
}
async fn record_to_async(
command: Command,
dir: &Path,
mountinfo_root_rewrites: Vec<detcore_model::config::MountInfoRootRewrite>,
mountinfo_mount_ids: Option<Vec<u64>>,
local_networking: Option<bool>,
control: std::rc::Rc<ptrace_completion::Control>,
) -> Result<ExitStatus, Error> {
let report = SkidOvershootReport::begin(true);
let result = async {
Record::spawn_with_mountinfo(
command,
dir,
mountinfo_root_rewrites,
mountinfo_mount_ids,
local_networking,
)
.await?
.wait(control)
.await
}
.await;
report.finish(result)
}
pub fn record_with_output(command: Command, dir: &Path) -> Result<Output, Error> {
let dir = dir.to_owned();
ptrace_completion::run(None, move |control| async move {
record_with_output_async(command, &dir, Vec::new(), None, None, control).await
})
}
pub fn record_with_output_with_mountinfo(
command: Command,
dir: &Path,
mountinfo_root_rewrites: Vec<detcore_model::config::MountInfoRootRewrite>,
local_networking: bool,
) -> Result<Output, Error> {
let dir = dir.to_owned();
ptrace_completion::run(None, move |control| async move {
let mount_ids = capture_mountinfo_identity_order()?;
record_with_output_async(
command,
&dir,
mountinfo_root_rewrites,
Some(mount_ids),
Some(local_networking),
control,
)
.await
})
}
async fn record_with_output_async(
mut command: Command,
dir: &Path,
mountinfo_root_rewrites: Vec<detcore_model::config::MountInfoRootRewrite>,
mountinfo_mount_ids: Option<Vec<u64>>,
local_networking: Option<bool>,
control: std::rc::Rc<ptrace_completion::Control>,
) -> Result<Output, Error> {
let report = SkidOvershootReport::begin(true);
command.stdin(Stdio::null());
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
let result = async {
Record::spawn_with_mountinfo(
command,
dir,
mountinfo_root_rewrites,
mountinfo_mount_ids,
local_networking,
)
.await?
.wait_with_output(control)
.await
}
.await;
report.finish(result)
}
pub fn replay_from(dir: &Path) -> Result<ExitStatus, Error> {
replay_plain(dir, None, &[])
}
pub fn replay_with_gdbserver(dir: &Path, port: u16) -> Result<ExitStatus, Error> {
replay_plain(dir, Some(port), &[])
}
pub fn replay_with_gdbserver_and_mounts(
dir: &Path,
port: u16,
mounts: &[Mount],
) -> Result<ExitStatus, Error> {
replay_plain(dir, Some(port), mounts)
}
fn replay_plain(dir: &Path, port: Option<u16>, mounts: &[Mount]) -> Result<ExitStatus, Error> {
let dir = dir.to_owned();
let mounts = mounts.to_vec();
ptrace_completion::run(None, move |control| async move {
let report = SkidOvershootReport::begin(true);
let result = async {
Replay::spawn(&dir, false, port, &mounts)
.await?
.wait(control)
.await
}
.await;
report.finish(result)
})
}
pub fn replay_with_output(dir: &Path) -> Result<Output, Error> {
replay_with_output_and_mounts(dir, &[])
}
pub fn replay_with_output_and_mounts(dir: &Path, mounts: &[Mount]) -> Result<Output, Error> {
let dir = dir.to_owned();
let mounts = mounts.to_vec();
ptrace_completion::run(None, move |control| async move {
let report = SkidOvershootReport::begin(true);
let result = async {
Replay::spawn(&dir, true, None, &mounts)
.await?
.wait_with_output(control)
.await
}
.await;
report.finish(result)
})
}
#[cfg(test)]
mod tests {
use super::*;
static SKID_OVERSHOOT_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[test]
fn kvm_mountinfo_config_preserves_captured_provenance() {
let rewrite = detcore_model::config::MountInfoRootRewrite {
raw_mount_id: 20,
deterministic_root: b"/".to_vec(),
raw_root_prefix: Some(b"/private-source".to_vec()),
deterministic_root_prefix: Some(b"/tmp".to_vec()),
raw_mountpoint_prefix: Some(b"/private-source".to_vec()),
deterministic_mountpoint_prefix: Some(b"/tmp".to_vec()),
};
let mut config = DetConfig {
mountinfo_mount_ids: vec![10, 20, 30],
mountinfo_mount_ids_captured: true,
fdinfo_unlisted_mount_ids: vec![700, 701],
mountinfo_root_rewrites: vec![rewrite],
mountinfo_device_rewrites: vec![(libc::makedev(0, 1), libc::makedev(8, 1))],
..DetConfig::default()
};
let mut expected = config.clone();
expected.mountinfo_device_rewrites.clear();
let mut captures = 0;
prepare_kvm_mountinfo_config(&mut config, || {
captures += 1;
mountinfo_identity_order_from(
b"10 30 8:1 / / rw - ext4 /dev/root rw\n\
20 10 0:7 / /test rw - tmpfs tmpfs rw\n",
)
})
.unwrap();
assert_eq!(
captures, 1,
"captured provenance must be checked before any guest read"
);
assert_eq!(
serde_json::to_value(&config).unwrap(),
serde_json::to_value(&expected).unwrap(),
"root rewrites, listed/unlisted order and every other setting must remain intact"
);
let mut empty = DetConfig {
mountinfo_mount_ids_captured: true,
..DetConfig::default()
};
let mut captures = 0;
prepare_kvm_mountinfo_config(&mut empty, || {
captures += 1;
mountinfo_identity_order_from(b"")
})
.unwrap();
assert_eq!(captures, 1);
assert!(empty.mountinfo_mount_ids.is_empty());
assert!(empty.mountinfo_mount_ids_captured);
for actual in [
vec![10, 20],
vec![10, 20, 30, 40],
vec![20, 10, 30],
vec![10, 20, 31],
vec![10, 20, 20, 30],
vec![],
] {
let mut stale = config.clone();
stale.mountinfo_device_rewrites = vec![(libc::makedev(0, 1), libc::makedev(8, 1))];
let before = serde_json::to_value(&stale).unwrap();
let error = prepare_kvm_mountinfo_config(&mut stale, || Ok(actual)).unwrap_err();
assert!(
error
.to_string()
.contains("do not match the backend namespace")
);
assert_eq!(serde_json::to_value(&stale).unwrap(), before);
}
let before = serde_json::to_value(&empty).unwrap();
let error = prepare_kvm_mountinfo_config(&mut empty, || Ok(vec![10])).unwrap_err();
assert!(
error
.to_string()
.contains("do not match the backend namespace")
);
assert_eq!(serde_json::to_value(&empty).unwrap(), before);
}
#[test]
fn kvm_mountinfo_config_captures_validated_ambient_namespace() {
let contents = b"80 20 8:1 / / rw - ext4 /dev/root rw\n\
10 80 0:7 / /test rw - tmpfs tmpfs rw\n";
let mut config = DetConfig::default();
let mut expected = config.clone();
expected.mountinfo_mount_ids = vec![80, 10, 20];
expected.mountinfo_mount_ids_captured = true;
prepare_kvm_mountinfo_config(&mut config, || mountinfo_identity_order_from(contents))
.unwrap();
assert_eq!(
serde_json::to_value(&config).unwrap(),
serde_json::to_value(&expected).unwrap(),
"visible rows precede parent-only IDs and no guest setting changes"
);
assert!(detcore_model::procfs::mount_ids_are_ordered_subset(
&[80, 10],
&config.mountinfo_mount_ids
));
assert!(!detcore_model::procfs::mount_ids_are_ordered_subset(
&[10, 80],
&config.mountinfo_mount_ids
));
assert!(!detcore_model::procfs::mount_ids_are_ordered_subset(
&[80, 99],
&config.mountinfo_mount_ids
));
assert!(!detcore_model::procfs::mount_ids_are_ordered_subset(
&[80, 80],
&config.mountinfo_mount_ids
));
let mut empty = DetConfig::default();
prepare_kvm_mountinfo_config(&mut empty, || mountinfo_identity_order_from(b"")).unwrap();
assert!(empty.mountinfo_mount_ids.is_empty());
assert!(empty.mountinfo_mount_ids_captured);
}
#[test]
fn kvm_mountinfo_config_refuses_missing_or_malformed_capture() {
let valid = b"10 20 8:1 / / rw - ext4 /dev/root rw\n";
for captured in [false, true] {
let config = DetConfig {
mountinfo_mount_ids: if captured { vec![10, 20] } else { vec![] },
mountinfo_mount_ids_captured: captured,
fdinfo_unlisted_mount_ids: if captured { vec![700, 701] } else { vec![] },
mountinfo_device_rewrites: vec![(libc::makedev(0, 1), libc::makedev(8, 1))],
..DetConfig::default()
};
for contents in [
b"malformed".as_slice(),
b"10 20 8:1 / / rw - ext4 /dev/root rw\n10 20 8:1 / / rw - ext4 /dev/root rw\n",
b"10 20 8:1 / / rw unknown:7 - ext4 /dev/root rw\n",
] {
let mut candidate = config.clone();
let before = serde_json::to_value(&candidate).unwrap();
let error = prepare_kvm_mountinfo_config(&mut candidate, || {
mountinfo_identity_order_from(contents)
})
.unwrap_err();
assert!(format!("{error:#}").contains("malformed /proc/self/mountinfo"));
assert_eq!(serde_json::to_value(&candidate).unwrap(), before);
}
let mut candidate = config;
let before = serde_json::to_value(&candidate).unwrap();
let error = prepare_kvm_mountinfo_config(&mut candidate, || {
Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied).into())
})
.unwrap_err();
assert!(
error
.downcast_ref::<std::io::Error>()
.is_some_and(|error| error.kind() == std::io::ErrorKind::PermissionDenied)
);
assert_eq!(serde_json::to_value(&candidate).unwrap(), before);
}
assert_eq!(mountinfo_identity_order_from(valid).unwrap(), [10, 20]);
}
#[test]
fn kvm_mountinfo_config_refuses_uncaptured_provenance_vectors() {
for (listed, unlisted) in [(vec![10], vec![]), (vec![], vec![700])] {
let mut config = DetConfig {
mountinfo_mount_ids: listed,
fdinfo_unlisted_mount_ids: unlisted,
..DetConfig::default()
};
let before = serde_json::to_value(&config).unwrap();
let error = prepare_kvm_mountinfo_config(&mut config, || {
panic!("must not overwrite incomplete provenance")
})
.unwrap_err();
assert!(error.to_string().contains("no producer-captured namespace"));
assert_eq!(serde_json::to_value(&config).unwrap(), before);
}
}
#[test]
fn skid_overshoot_report_covers_success_error_and_disabled_backends() {
let _lock = SKID_OVERSHOOT_TEST_LOCK.lock().unwrap();
let _ = reverie::take_skid_overshoot_count();
reverie::record_skid_overshoot();
let empty_run = SkidOvershootReport::begin(true);
assert_eq!(
reverie::take_skid_overshoot_count(),
0,
"begin must clear evidence from a previous invocation"
);
empty_run.finish(Ok::<_, Error>(())).unwrap();
let success = SkidOvershootReport::begin(true);
reverie::record_skid_overshoot();
reverie::record_skid_overshoot();
let error = success.finish(Ok::<_, Error>(7)).unwrap_err();
assert_eq!(
error
.downcast_ref::<SkidOvershootError>()
.expect("skid refusal must remain typed")
.count(),
2
);
assert!(
error
.to_string()
.contains("observed 2 HERMIT_SKID_OVERSHOOT report(s)"),
"{error:#}"
);
assert!(
error
.to_string()
.contains("deterministic execution was not established"),
"{error:#}"
);
assert_eq!(reverie::take_skid_overshoot_count(), 0);
let error = SkidOvershootReport::begin(true);
reverie::record_skid_overshoot();
let error = error
.finish(Err::<(), _>(anyhow!("guest failed")))
.unwrap_err();
assert!(error.downcast_ref::<SkidOvershootError>().is_some());
let error = format!("{error:#}");
assert!(error.contains("guest failed"), "{error}");
assert!(
error.contains("observed 1 HERMIT_SKID_OVERSHOOT report(s)"),
"{error}"
);
assert_eq!(reverie::take_skid_overshoot_count(), 0);
reverie::record_skid_overshoot();
let disabled = SkidOvershootReport::begin(false);
disabled.finish(Ok::<_, Error>(())).unwrap();
assert_eq!(
reverie::take_skid_overshoot_count(),
1,
"a backend that cannot produce ptrace PMU overshoots must not consume another run's evidence"
);
}
#[test]
fn only_ptrace_hosted_backends_consume_skid_overshoot_reports() {
for runtime in [LiteinstRuntime::HostHybrid, LiteinstRuntime::InGuest] {
for backend in [Backend::Ptrace, Backend::E9patch] {
assert!(
backend.uses_ptrace_pmu_timers_for_liteinst_runtime(runtime),
"{backend:?} {runtime:?}"
);
}
for backend in [Backend::Dbt, Backend::Sabre, Backend::Kvm] {
assert!(
!backend.uses_ptrace_pmu_timers_for_liteinst_runtime(runtime),
"{backend:?} {runtime:?}"
);
}
}
assert!(
Backend::Liteinst
.uses_ptrace_pmu_timers_for_liteinst_runtime(LiteinstRuntime::HostHybrid)
);
assert!(
!Backend::Liteinst
.uses_ptrace_pmu_timers_for_liteinst_runtime(LiteinstRuntime::InGuest)
);
}
#[cfg(feature = "liteinst")]
fn write_guest_program(directory: &Path, name: &str, bytes: &[u8], mode: u32) -> PathBuf {
let path = directory.join(name);
fs::write(&path, bytes).unwrap();
fs::set_permissions(&path, fs::Permissions::from_mode(mode)).unwrap();
assert_eq!(
fs::metadata(&path).unwrap().permissions().mode() & 0o7777,
mode,
"{}",
path.display()
);
path
}
#[cfg(feature = "liteinst")]
fn shebang_to(interpreter: &Path) -> Vec<u8> {
let mut script = b"#!".to_vec();
script.extend_from_slice(interpreter.as_os_str().as_bytes());
script.extend_from_slice(b" -e\necho script\n");
script
}
#[test]
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_runs_a_dynamically_linked_x86_64_program() {
use interp::startup_fixtures::dynamic_executable;
let directory = tempfile::tempdir().unwrap();
let program = write_guest_program(
directory.path(),
"dynamic",
&dynamic_executable(false),
0o755,
);
assert_eq!(
in_guest_liteinst_program_gap(&mut Command::new(&program)).unwrap(),
None
);
}
#[test]
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_refuses_programs_that_start_before_the_runtime() {
use interp::startup_fixtures::*;
let directory = tempfile::tempdir().unwrap();
let cases: [(&str, Vec<u8>, &str); 4] = [
(
"static",
elf(EM_X86_64, &[(PT_LOAD, b"code")]),
"is statically linked",
),
(
"preinit",
dynamic_executable(true),
"has a DT_PREINIT_ARRAY",
),
(
"aarch64",
elf(EM_AARCH64, &[(PT_INTERP, INTERP)]),
"is not a 64-bit x86-64 ELF image",
),
(
"text",
b"plain text, no #! line\n".to_vec(),
"is neither an ELF executable nor a #! script",
),
];
for (name, bytes, reason) in cases {
let program = write_guest_program(directory.path(), name, &bytes, 0o755);
let gap = in_guest_liteinst_program_gap(&mut Command::new(&program))
.unwrap()
.unwrap_or_else(|| panic!("{name} was not refused"));
assert!(
gap.contains(reason) && gap.contains(&program.display().to_string()),
"{name}: {gap}"
);
}
}
#[test]
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_refuses_secure_execution_images() {
use interp::startup_fixtures::dynamic_executable;
let directory = tempfile::tempdir().unwrap();
let bytes = dynamic_executable(false);
let gap = |name: &str, mode: u32| {
let program = write_guest_program(directory.path(), name, &bytes, mode);
in_guest_liteinst_program_gap(&mut Command::new(&program)).unwrap()
};
for (name, mode) in [("setuid", 0o4755), ("setgid", 0o2755)] {
let refusal = gap(name, mode).unwrap_or_else(|| panic!("{name} was not refused"));
assert!(refusal.contains("secure-execution mode"), "{refusal}");
}
assert_eq!(gap("setgid-no-group-exec", 0o2744), None);
}
#[test]
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_checks_scripts_through_their_interpreter() {
use interp::startup_fixtures::*;
let directory = tempfile::tempdir().unwrap();
let dynamic = write_guest_program(
directory.path(),
"dynamic",
&dynamic_executable(false),
0o755,
);
let static_elf = write_guest_program(
directory.path(),
"static",
&elf(EM_X86_64, &[(PT_LOAD, b"code")]),
0o755,
);
let script = write_guest_program(
directory.path(),
"to-static",
&shebang_to(&static_elf),
0o755,
);
let gap = in_guest_liteinst_program_gap(&mut Command::new(&script))
.unwrap()
.expect("a script run by a static interpreter was not refused");
assert!(
gap.contains(&static_elf.display().to_string()) && gap.contains("statically linked"),
"{gap}"
);
let relative = write_guest_program(directory.path(), "relative", b"#!static\n", 0o755);
let gap =
in_guest_liteinst_program_gap(Command::new(&relative).current_dir(directory.path()))
.unwrap()
.expect("a relative static interpreter was not refused");
assert!(gap.contains("statically linked"), "{gap}");
write_guest_program(
directory.path(),
"interp",
&dynamic_executable(false),
0o755,
);
let interp_cr = write_guest_program(
directory.path(),
"interp\r",
&elf(EM_X86_64, &[(PT_LOAD, b"code")]),
0o755,
);
let mut crlf = b"#!".to_vec();
crlf.extend_from_slice(directory.path().join("interp").as_os_str().as_bytes());
crlf.extend_from_slice(b"\r\necho script\r\n");
let crlf = write_guest_program(directory.path(), "crlf", &crlf, 0o755);
let gap = in_guest_liteinst_program_gap(&mut Command::new(&crlf))
.unwrap()
.expect("a CRLF script run by a static interp\\r was not refused");
assert!(
gap.contains(&interp_cr.display().to_string()) && gap.contains("statically linked"),
"{gap}"
);
let mut truncated = b"#!/".to_vec();
truncated.resize(256, b'a');
for (name, bytes) in [
("empty", b"#! \t\nbody\n".to_vec()),
("truncated", truncated),
] {
let program = write_guest_program(directory.path(), name, &bytes, 0o755);
let gap = in_guest_liteinst_program_gap(&mut Command::new(&program))
.unwrap()
.unwrap_or_else(|| panic!("{name} was not refused"));
assert!(
gap.contains("the kernel's script handler declines it"),
"{name}: {gap}"
);
}
let mut target = dynamic;
for level in 1..=5 {
target = write_guest_program(
directory.path(),
&format!("level-{level}"),
&shebang_to(&target),
0o755,
);
let gap = in_guest_liteinst_program_gap(&mut Command::new(&target)).unwrap();
if level <= MAX_INTERPRETER_DEPTH {
assert_eq!(gap, None, "level {level}");
} else {
let gap = gap.expect("five nested scripts were not refused");
assert!(gap.contains("nest more than 4 deep"), "{gap}");
}
}
}
#[test]
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_refuses_a_fifo_without_blocking() {
let directory = tempfile::tempdir().unwrap();
let fifo = directory.path().join("fifo");
let fifo_c = std::ffi::CString::new(fifo.as_os_str().as_bytes()).unwrap();
assert_eq!(unsafe { libc::mkfifo(fifo_c.as_ptr(), 0o700) }, 0);
let script = write_guest_program(directory.path(), "to-fifo", &shebang_to(&fifo), 0o755);
let socket = directory.path().join("socket");
let _listener = std::os::unix::net::UnixListener::bind(&socket).unwrap();
let to_socket =
write_guest_program(directory.path(), "to-socket", &shebang_to(&socket), 0o755);
let device = PathBuf::from("/dev/null");
let to_device =
write_guest_program(directory.path(), "to-device", &shebang_to(&device), 0o755);
let cases = [
(
"program",
PathBuf::from("./fifo"),
Some(directory.path().to_owned()),
fs::canonicalize(&fifo).unwrap(),
),
("interpreter", script, None, fifo.clone()),
("socket interpreter", to_socket, None, socket.clone()),
("device interpreter", to_device, None, device),
];
for (name, program, current_dir, shown) in cases {
let (sender, receiver) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let mut command = Command::new(&program);
if let Some(directory) = current_dir {
command.current_dir(directory);
}
let gap = in_guest_liteinst_program_gap(&mut command)
.map_err(|error| format!("{error:#}"));
let _ = sender.send(gap);
});
let gap = receiver
.recv_timeout(std::time::Duration::from_secs(10))
.unwrap_or_else(|_| panic!("{name}: the check blocked on the FIFO"))
.unwrap()
.unwrap_or_else(|| panic!("{name}: the FIFO was not refused"));
assert!(
gap.contains(&format!("{} is not a regular file", shown.display())),
"{name}: {gap}"
);
}
}
#[test]
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_pins_the_program_the_launch_resolves() {
use interp::startup_fixtures::*;
let directory = tempfile::tempdir().unwrap();
let name = "hermit-3520-static";
let static_elf = write_guest_program(
directory.path(),
name,
&elf(EM_X86_64, &[(PT_LOAD, b"code")]),
0o755,
);
let search = format!("/nonexistent-directory:{}", directory.path().display());
let check = |build: &dyn Fn() -> Command| {
let launch = build().find_program();
let mut command = build();
let arg0 = command.get_arg0().to_owned();
let gap = in_guest_liteinst_program_gap(&mut command);
match launch {
Ok(program) => {
let gap = gap.unwrap().expect("the static image was not refused");
assert!(gap.contains("is statically linked"), "{gap}");
assert_eq!(Path::new(command.get_program()), program);
assert_eq!(command.get_arg0(), arg0);
assert_eq!(command.find_program().unwrap(), program);
assert_eq!(
fs::canonicalize(&program).unwrap(),
fs::canonicalize(&static_elf).unwrap()
);
true
}
Err(_) => {
let error = gap.expect_err("the check found what the launch cannot");
assert!(
format!("{error:#}").contains("cannot find guest program"),
"{error:#}"
);
false
}
}
};
assert!(check(&|| {
let mut command = Command::new(format!("./{name}"));
command.current_dir(directory.path());
command
}));
assert!(check(&|| {
let mut command = Command::new(name);
command.env_clear().env("PATH", &search);
command
}));
assert!(!check(&|| {
let mut command = Command::new(name);
command.env("PATH", &search);
command
}));
assert!(!check(&|| {
let mut command = Command::new(name);
command.env_clear().current_dir(directory.path());
command
}));
fs::set_permissions(&static_elf, fs::Permissions::from_mode(0o644)).unwrap();
assert!(!check(&|| {
let mut command = Command::new(name);
command.env_clear().env("PATH", &search);
command
}));
assert!(!check(&|| Command::new(&static_elf)));
assert!(!check(&|| Command::new("hermit-no-such-program-3520")));
}
#[test]
fn skid_overshoot_error_refuses_a_successful_result() {
let _lock = SKID_OVERSHOOT_TEST_LOCK.lock().unwrap();
let _ = reverie::take_skid_overshoot_count();
assert!(take_skid_overshoot_error().is_none());
reverie::record_skid_overshoot();
let error = take_skid_overshoot_error().expect("recorded overshoot must be reported");
assert!(
error
.to_string()
.contains("observed 1 HERMIT_SKID_OVERSHOOT report(s)")
);
assert!(
error
.to_string()
.contains("deterministic execution was not established")
);
assert_eq!(reverie::take_skid_overshoot_count(), 0);
}
#[test]
fn skid_overshoot_report_preserves_success_without_an_overshoot() {
let _lock = SKID_OVERSHOOT_TEST_LOCK.lock().unwrap();
let _ = reverie::take_skid_overshoot_count();
let report = SkidOvershootReport::begin(true);
assert_eq!(report.finish(Ok::<_, Error>(7)).unwrap(), 7);
assert_eq!(reverie::take_skid_overshoot_count(), 0);
}
#[test]
fn skid_overshoot_count_can_cross_the_verify_boundary_with_the_output() {
let _lock = SKID_OVERSHOOT_TEST_LOCK.lock().unwrap();
let _ = reverie::take_skid_overshoot_count();
let report = SkidOvershootReport::begin(true);
reverie::record_skid_overshoot();
let (value, count) = report.finish_with_count(Ok::<_, Error>(7)).unwrap();
assert_eq!(value, 7);
assert_eq!(count, 1);
assert_eq!(reverie::take_skid_overshoot_count(), 0);
}
#[test]
#[cfg(feature = "liteinst")]
fn liteinst_forced_shutdown_covers_every_status_hermit_reserves() {
assert!(
liteinst_requires_forced_shutdown(ExitStatus::Exited(130)),
"128+SIGINT"
);
assert!(
liteinst_requires_forced_shutdown(ExitStatus::Exited(143)),
"128+SIGTERM"
);
assert!(
liteinst_requires_forced_shutdown(ExitStatus::Exited(129)),
"128+SIGHUP"
);
assert!(liteinst_requires_forced_shutdown(ExitStatus::Exited(122)));
assert!(liteinst_requires_forced_shutdown(ExitStatus::Exited(125)));
assert!(liteinst_requires_forced_shutdown(ExitStatus::Exited(127)));
assert!(liteinst_requires_forced_shutdown(ExitStatus::Signaled(
nix::sys::signal::Signal::SIGKILL,
false
)));
assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(0)));
assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(1)));
assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(121)));
assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(128)));
assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(200)));
assert!(
liteinst_requires_forced_shutdown(ExitStatus::Exited(192)),
"128 + SIGRTMAX is still a signal death and must force shutdown"
);
assert!(
!liteinst_requires_forced_shutdown(ExitStatus::Exited(193)),
"above SIGRTMAX the status is the guest's own and must NOT force shutdown"
);
}
#[test]
fn a_cwd_under_tmp_is_explained_by_the_container_tmpfs_not_by_a_missing_directory() {
let error = std::io::Error::from(std::io::ErrorKind::NotFound);
let rendered = format!(
"{}",
super::kvm_cwd_resolution_error(std::path::Path::new("/tmp/agent-checkout"), &error)
);
assert!(
rendered.contains("private, empty tmpfs"),
"must name the mechanism: {rendered}"
);
assert!(
rendered.contains("--workdir"),
"must name a way out: {rendered}"
);
assert!(
rendered.contains("/tmp/agent-checkout"),
"must still name the path asked for: {rendered}"
);
}
#[test]
fn the_tmpfs_explanation_is_withheld_where_it_does_not_apply() {
let not_found = std::io::Error::from(std::io::ErrorKind::NotFound);
let outside_path = std::path::Path::new("/nonexistent-home/wt");
let outside = format!(
"{}",
super::kvm_cwd_resolution_error(outside_path, ¬_found)
);
assert!(!outside.contains("tmpfs"), "not a /tmp path: {outside}");
let tmp_itself = format!(
"{}",
super::kvm_cwd_resolution_error(std::path::Path::new("/tmp"), ¬_found)
);
assert!(!tmp_itself.contains("tmpfs"), "/tmp itself: {tmp_itself}");
let denied = std::io::Error::from(std::io::ErrorKind::PermissionDenied);
let other = format!(
"{}",
super::kvm_cwd_resolution_error(std::path::Path::new("/tmp/x"), &denied)
);
assert!(!other.contains("tmpfs"), "not NotFound: {other}");
}
#[test]
fn an_unbuilt_backend_names_the_flag_and_claims_nothing_about_the_machine() {
let feature_disabled_backends: &[(Backend, &str)] = &[
#[cfg(not(feature = "dbt"))]
(Backend::Dbt, "dbt"),
#[cfg(not(feature = "sabre"))]
(Backend::Sabre, "sabre"),
#[cfg(not(feature = "e9patch"))]
(Backend::E9patch, "e9patch"),
#[cfg(not(feature = "liteinst"))]
(Backend::Liteinst, "liteinst"),
];
for &(backend, feature) in feature_disabled_backends {
let reason = backend
.unavailable_reason()
.expect("a feature-disabled backend must report why it is unavailable");
assert!(
reason.contains(feature),
"{backend:?}: a build-flag message must name the flag to rebuild with, got: {reason}"
);
assert!(
reason.contains("has not been checked"),
"{backend:?}: a build-flag message must not imply the machine was tested, got: {reason}"
);
}
}
#[test]
#[cfg(feature = "dbt")]
fn a_cold_dbt_runtime_diagnostic_is_not_feature_absence() {
let reason = dbt_runtime_unavailable_reason_from(Err(io::Error::new(
io::ErrorKind::NotFound,
"Hermit DBT runtime was not built beside /cold-target/hermit or in its deps directory",
)))
.expect("a missing cold-target runtime must remain unavailable");
assert!(reason.contains("was not built beside"));
assert!(reason.contains("build the hermit binary and cdylib"));
assert!(!reason.contains("not enabled in this build"));
assert!(!reason.contains("has not been checked"));
}
use std::ffi::OsStr;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use super::Backend;
use super::ExitStatus;
use super::HermitData;
use super::Id;
use super::LITEINST_IN_GUEST_ENV;
use super::LiteinstRuntime;
use super::SABRE_RPC_SOCKET_ENV;
use super::collect_recording_ids;
#[cfg(feature = "dbt")]
use super::dbt_runtime_unavailable_reason;
#[cfg(feature = "dbt")]
use super::dbt_runtime_unavailable_reason_from;
#[cfg(feature = "dbt")]
use super::dynamorio_sdk_available;
use super::ensure_backend_dispatch;
#[cfg(feature = "dbt")]
use super::is_dynamorio_sdk;
use super::kvm_device_unavailable_reason;
#[cfg(feature = "liteinst")]
use super::liteinst_requires_forced_shutdown;
#[cfg(feature = "dbt")]
use super::liteinst_unavailable_reason;
use super::output_backend_stdin_file;
use super::prepare_backend_config;
use super::prepare_backend_config_for_liteinst_runtime;
use super::reserve_output_stdin_snapshot;
use super::resolve_kvm_shebang;
use super::resolve_sabre_binary_from;
use super::sabre_backend_evidence_line;
use super::sabre_program_needs_neutral_name;
use super::sabre_reach_state;
use super::sabre_uninstrumented_guest_message;
use super::shutdown_sabre_rpc;
use super::stage_sabre_program_in;
use super::stop_sabre_rpc_server;
use super::wait_for_sabre_rpc_disconnects;
#[test]
fn recording_inventory_preserves_missing_and_non_recording_cases() {
let parent = tempfile::tempdir().expect("failed to create recording inventory parent");
let missing = parent.path().join("missing");
assert!(
HermitData::with_dir(&missing)
.try_recordings()
.expect("a missing data directory should be empty")
.is_empty()
);
let inventory = parent.path().join("inventory");
fs::create_dir(&inventory).expect("failed to create recording inventory");
let recording: Id = "0123456789abcdef0123456789abcdef"
.parse()
.expect("valid recording ID");
fs::create_dir(inventory.join(recording.to_string()))
.expect("failed to create recording directory");
fs::create_dir(inventory.join("not-a-recording"))
.expect("failed to create invalid-name directory");
fs::write(
inventory.join("fedcba9876543210fedcba9876543210"),
b"not a directory",
)
.expect("failed to create valid-looking non-directory");
assert_eq!(
HermitData::with_dir(&inventory)
.try_recordings()
.expect("recording inventory should be readable"),
vec![recording]
);
assert_eq!(
HermitData::with_dir(&inventory)
.recordings()
.collect::<Vec<_>>(),
vec![recording]
);
}
#[test]
fn recording_inventory_rejects_an_error_after_a_valid_entry() {
let inventory = tempfile::tempdir().expect("failed to create recording inventory");
let recording: Id = "0123456789abcdef0123456789abcdef"
.parse()
.expect("valid recording ID");
fs::create_dir(inventory.path().join(recording.to_string()))
.expect("failed to create recording directory");
let valid_entry = fs::read_dir(inventory.path())
.expect("failed to open recording inventory")
.next()
.expect("missing recording entry")
.expect("failed to read recording entry");
let entries = vec![
Ok(valid_entry),
Err(std::io::Error::other("injected entry failure")),
];
let error = collect_recording_ids(entries, inventory.path(), fs::DirEntry::file_type)
.expect_err("a partial inventory must not be returned");
assert!(
error
.to_string()
.contains("Failed to read an entry in recording inventory"),
"unexpected error: {error:#}"
);
}
#[test]
fn recording_inventory_rejects_an_entry_inspection_error() {
let inventory = tempfile::tempdir().expect("failed to create recording inventory");
let recording = inventory.path().join("0123456789abcdef0123456789abcdef");
fs::create_dir(&recording).expect("failed to create recording directory");
let entry = fs::read_dir(inventory.path())
.expect("failed to open recording inventory")
.next()
.expect("missing recording entry")
.expect("failed to read recording entry");
let error = collect_recording_ids([Ok(entry)], inventory.path(), |_| {
Err(std::io::Error::other("injected inspection failure"))
})
.expect_err("an uninspectable entry must fail the inventory");
assert!(
error
.to_string()
.contains("Failed to inspect recording inventory entry"),
"unexpected error: {error:#}"
);
}
#[test]
fn output_stdin_snapshot_replays_pipe_to_repeated_runs() {
use std::io::Read;
use std::io::Write;
use std::os::fd::FromRawFd;
let mut fds = [0i32; 2];
assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);
let read_end = unsafe { fs::File::from_raw_fd(fds[0]) };
let mut write_end = unsafe { fs::File::from_raw_fd(fds[1]) };
let payload = b"int main(){return 0;}\n";
write_end.write_all(payload).unwrap();
drop(write_end);
reserve_output_stdin_snapshot(Some(read_end)).unwrap();
for run in 0..2 {
let mut file = output_backend_stdin_file()
.unwrap()
.unwrap_or_else(|| panic!("run {run}: expected a replayable stdin snapshot"));
let mut got = Vec::new();
file.read_to_end(&mut got).unwrap();
assert_eq!(got, payload, "run {run} stdin replay mismatch");
}
let mut file = output_backend_stdin_file().unwrap().unwrap();
let error = file
.write_all(b"guest-must-not-change-the-snapshot")
.expect_err("the replayed stdin must be read-only");
assert_eq!(error.raw_os_error(), Some(libc::EBADF));
}
#[test]
#[cfg(feature = "liteinst")]
fn liteinst_reserved_failures_require_scheduler_cancellation() {
for status in 122..=127 {
assert!(liteinst_requires_forced_shutdown(ExitStatus::Exited(
status
)));
}
assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(121)));
assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(128)));
}
#[test]
fn uninstrumented_sabre_guest_is_reported_as_no_determinization() {
let message = sabre_uninstrumented_guest_message(&ExitStatus::Exited(0));
assert!(
message.contains("no determinization at all"),
"must not present an uninstrumented run as a weaker guarantee: {message}"
);
assert!(
message.contains("Detcore coordinator"),
"must name the authority that was never reached: {message}"
);
assert!(
message.contains("statically linked ELF"),
"must name the dominant cause so the reader can act: {message}"
);
assert!(
message.contains("Exited(0)"),
"must carry the observed status: {message}"
);
assert!(
sabre_uninstrumented_guest_message(&ExitStatus::Exited(139)).contains("Exited(139)"),
"must carry a failing status too"
);
}
#[test]
fn sabre_reach_states_are_pairwise_distinct() {
let no_detcore = sabre_reach_state(false, 0);
let degraded = sabre_reach_state(true, 1);
let exercised = sabre_reach_state(true, 0);
assert_eq!(no_detcore, "no-detcore-reached");
assert_eq!(degraded, "degraded-ptrace-fallback");
assert_eq!(exercised, "sabre-exercised");
assert_ne!(no_detcore, degraded);
assert_ne!(no_detcore, exercised);
assert_ne!(degraded, exercised);
assert_eq!(
sabre_reach_state(false, 9),
"no-detcore-reached",
"absence of a guest RPC must dominate any fallback count"
);
}
#[test]
fn sabre_backend_fact_is_versioned_and_names_preplugin_coverage() {
let exercised = super::sabre_ptrace::PathEvidence {
schema: 1,
guest_rpc_observed: true,
ptrace_fallback_sites: 0,
trusted_shared_object_sites: 2,
trusted_shared_objects: vec!["/usr/lib/libc.so.6".to_owned()],
};
assert_eq!(
sabre_backend_evidence_line(&exercised),
":: Backend: sabre static rewriting + ptrace runtime; run_mode=run; \
evidence_schema=1; preplugin_coverage=absent; ptrace_fallback_sites=0; \
trusted_shared_object_sites=2; guest_rpc_observed=true; \
reach_state=sabre-exercised"
);
let unengaged = super::sabre_ptrace::PathEvidence {
guest_rpc_observed: false,
..exercised
};
let fact = sabre_backend_evidence_line(&unengaged);
assert!(fact.contains("preplugin_coverage=absent"));
assert!(fact.contains("reach_state=no-detcore-reached"));
assert!(!fact.contains("reach_state=sabre-exercised"));
}
#[test]
fn liteinst_host_backend_preserves_ptrace_rcb_timeslices() {
let config = super::DetConfig::default();
assert!(config.max_timeslice.is_some());
assert!(
prepare_backend_config(config.clone(), Backend::Liteinst)
.max_timeslice
.is_some()
);
assert!(
prepare_backend_config(config, Backend::Ptrace)
.max_timeslice
.is_some()
);
}
#[test]
fn liteinst_runtime_selector_accepts_only_unset_or_one() {
assert_eq!(
LiteinstRuntime::parse(None).unwrap(),
LiteinstRuntime::HostHybrid
);
assert_eq!(
LiteinstRuntime::parse(Some(std::ffi::OsStr::new("1"))).unwrap(),
LiteinstRuntime::InGuest
);
for value in ["", "0", "true", "yes", " 1", "1 "] {
let error = LiteinstRuntime::parse(Some(std::ffi::OsStr::new(value))).unwrap_err();
let message = error.to_string();
assert!(
message.contains(LITEINST_IN_GUEST_ENV) && message.contains(&format!("{value:?}")),
"{value:?}: {message}"
);
}
}
#[test]
fn in_guest_liteinst_cancels_killed_thread_rpcs() {
let config = super::DetConfig::default();
assert!(
prepare_backend_config_for_liteinst_runtime(
config.clone(),
Backend::Liteinst,
LiteinstRuntime::InGuest,
)
.cancel_killed_thread_rpcs
);
assert!(
!prepare_backend_config_for_liteinst_runtime(
config.clone(),
Backend::Liteinst,
LiteinstRuntime::HostHybrid,
)
.cancel_killed_thread_rpcs
);
assert!(
!prepare_backend_config_for_liteinst_runtime(
config,
Backend::Ptrace,
LiteinstRuntime::InGuest,
)
.cancel_killed_thread_rpcs
);
}
#[test]
fn sabre_backend_configures_process_local_capabilities() {
let config = super::DetConfig::default();
let sabre = prepare_backend_config(config.clone(), Backend::Sabre);
assert!(sabre.discover_live_file_metadata);
assert!(!sabre.use_thread_local_clock_reads);
assert!(sabre.detect_host_clock_futex_timeouts);
assert!(sabre.syscall_clobbers_virtualized_by_backend);
assert!(sabre.cancel_killed_thread_rpcs);
assert!(sabre.backend_reports_physical_process_exits);
assert!(!sabre.backend_serializes_fork_children);
assert!(sabre.backend_dispatches_thread_tools);
assert!(sabre.backend_tracks_process_children);
assert!(!sabre.backend_runs_exit_robust_list);
assert!(!sabre.backend_requires_thread_directed_process_signals);
assert!(!sabre.backend_virtualizes_capability_prctls);
assert!(!sabre.backend_defers_vfork_child_registration);
let ptrace = prepare_backend_config(config, Backend::Ptrace);
assert!(!ptrace.discover_live_file_metadata);
assert!(!ptrace.use_thread_local_clock_reads);
assert!(!ptrace.detect_host_clock_futex_timeouts);
assert!(!ptrace.syscall_clobbers_virtualized_by_backend);
assert!(!ptrace.cancel_killed_thread_rpcs);
assert!(!ptrace.backend_reports_physical_process_exits);
assert!(!ptrace.backend_serializes_fork_children);
assert!(ptrace.backend_dispatches_thread_tools);
assert!(ptrace.backend_tracks_process_children);
assert!(ptrace.backend_runs_exit_robust_list);
assert!(!ptrace.backend_requires_thread_directed_process_signals);
assert!(!ptrace.backend_virtualizes_capability_prctls);
assert!(!ptrace.backend_defers_vfork_child_registration);
}
#[test]
fn kvm_reports_stored_metadata_timestamps_only_for_sequential_tool_threads() {
let sequential = prepare_backend_config(
super::DetConfig {
sequentialize_threads: true,
virtualize_metadata: true,
..super::DetConfig::default()
},
Backend::Kvm,
);
assert!(sequential.backend_dispatches_thread_tools);
assert!(super::kvm_reports_stored_metadata_timestamps(&sequential));
let concurrent = super::DetConfig {
sequentialize_threads: false,
..sequential.clone()
};
assert!(!super::kvm_reports_stored_metadata_timestamps(&concurrent));
let host_metadata = super::DetConfig {
virtualize_metadata: false,
..sequential.clone()
};
assert!(!super::kvm_reports_stored_metadata_timestamps(
&host_metadata
));
let host_threads = super::DetConfig {
backend_dispatches_thread_tools: false,
..sequential
};
assert!(!super::kvm_reports_stored_metadata_timestamps(
&host_threads
));
}
#[test]
fn kvm_backend_config_marks_concurrent_process_children() {
let defaults = super::DetConfig::default();
assert!(!defaults.sequentialize_threads);
let nonsequential = prepare_backend_config(defaults, Backend::Kvm);
assert!(nonsequential.backend_is_kvm);
assert!(!nonsequential.kvm_shared_dequeue_timers);
let config = super::DetConfig {
sequentialize_threads: true,
..super::DetConfig::default()
};
let kvm = prepare_backend_config(config, Backend::Kvm);
assert!(kvm.cancel_killed_thread_rpcs);
assert!(kvm.kvm_shared_dequeue_timers);
assert!(!kvm.backend_serializes_fork_children);
assert!(kvm.backend_dispatches_thread_tools);
assert!(kvm.backend_tracks_process_children);
assert!(!kvm.backend_runs_exit_robust_list);
assert!(!kvm.backend_requires_thread_directed_process_signals);
assert!(kvm.backend_virtualizes_capability_prctls);
assert!(kvm.backend_defers_vfork_child_registration);
}
#[test]
fn dbt_backend_config_translates_process_signals_to_host_threads() {
let config = prepare_backend_config(super::DetConfig::default(), Backend::Dbt);
assert!(config.cancel_killed_thread_rpcs);
assert!(!config.backend_tracks_process_children);
assert!(!config.backend_runs_exit_robust_list);
assert!(config.backend_requires_thread_directed_process_signals);
assert!(!config.backend_defers_vfork_child_registration);
}
#[test]
fn backend_write_signal_contract_is_explicit() {
for (backend, supports_interruption) in [
(Backend::Ptrace, true),
(Backend::Dbt, false),
(Backend::Kvm, false),
(Backend::Sabre, false),
(Backend::Liteinst, false),
(Backend::E9patch, true),
] {
let config = prepare_backend_config(super::DetConfig::default(), backend);
assert_eq!(
config.backend_supports_parked_write_signal_interruption, supports_interruption,
"unexpected parked-write signal support for {backend:?}"
);
}
}
#[test]
fn liteinst_public_dispatch_runs_ptrace_host_hybrid() {
if Backend::Liteinst.ensure_available().is_err() {
return;
}
let mut command = super::Command::new("/bin/echo");
command.arg("hello");
let output = super::run_with_output_backend(
command,
super::DetConfig::default(),
false,
&None,
Backend::Liteinst,
)
.expect("run /bin/echo through the ptrace-hosted LiteInst hybrid");
assert_eq!(
output.status,
super::ExitStatus::Exited(0),
"guest did not exit 0.\n--- guest stderr ---\n{}\n--- guest stdout ---\n{}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout),
);
assert_eq!(output.stdout, b"hello\n");
let status = super::run_with_backend(
super::Command::new("/bin/true"),
super::DetConfig::default(),
false,
&None,
Backend::Liteinst,
)
.expect("run /bin/true through the ptrace-hosted LiteInst hybrid");
assert_eq!(status, super::ExitStatus::Exited(0));
}
#[test]
#[cfg(feature = "dbt")]
fn default_and_available_backends_reflect_host_probes() {
assert_eq!(Backend::default(), Backend::Ptrace);
let available = Backend::available().collect::<Vec<_>>();
assert_eq!(
available.contains(&Backend::Ptrace),
Backend::Ptrace.is_available()
);
assert_eq!(
available.contains(&Backend::Dbt),
dynamorio_sdk_available() && dbt_runtime_unavailable_reason().is_none()
);
assert_eq!(
available.contains(&Backend::Liteinst),
liteinst_unavailable_reason().is_none()
);
assert_eq!(
available.contains(&Backend::Sabre),
Backend::Sabre.is_available()
);
assert_eq!(
available.contains(&Backend::Kvm),
kvm_device_unavailable_reason(std::path::Path::new("/dev/kvm")).is_none(),
);
assert_eq!(
available.contains(&Backend::E9patch),
Backend::E9patch.is_available()
);
}
#[test]
#[cfg(feature = "dbt")]
fn dependency_probes_require_usable_paths() {
let temp = tempfile::tempdir().unwrap();
assert!(!is_dynamorio_sdk(temp.path()));
fs::create_dir(temp.path().join("include")).unwrap();
fs::write(temp.path().join("include/dr_api.h"), b"/* marker */").unwrap();
assert!(is_dynamorio_sdk(temp.path()));
let reason = kvm_device_unavailable_reason(temp.path())
.expect("a directory must not pass the read-write KVM device probe");
assert!(reason.contains("read-write"));
}
fn write_test_executable(path: &std::path::Path) {
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, b"test loader").unwrap();
let mut permissions = fs::metadata(path).unwrap().permissions();
permissions.set_mode(0o700);
fs::set_permissions(path, permissions).unwrap();
}
#[test]
fn sabre_rpc_socket_uses_private_exec_environment() {
assert!(SABRE_RPC_SOCKET_ENV.starts_with("REVERIE_SABRE_"));
}
#[test]
fn sabre_stages_program_names_that_collide_with_loader_prefix() {
assert!(sabre_program_needs_neutral_name(
PathBuf::from("/usr/bin/ld").as_path()
));
assert!(sabre_program_needs_neutral_name(
PathBuf::from("/usr/bin/ld.bfd").as_path()
));
assert!(!sabre_program_needs_neutral_name(
PathBuf::from("/usr/bin/gold").as_path()
));
}
#[test]
fn sabre_neutral_name_staging_preserves_program_bytes_and_cleans_up() {
let temp = tempfile::tempdir().unwrap();
let program = temp.path().join("ld.test");
fs::write(&program, b"linker image").unwrap();
let staged = stage_sabre_program_in(&program, temp.path())
.unwrap()
.expect("ld-prefixed program should be staged");
let staged_path = staged.path.clone();
assert_eq!(fs::read(&staged_path).unwrap(), b"linker image");
assert!(
staged_path
.file_name()
.unwrap()
.as_encoded_bytes()
.starts_with(b"hermit-sabre-program-")
);
drop(staged);
assert!(!staged_path.exists());
}
#[tokio::test(start_paused = true)]
async fn sabre_rpc_disconnect_wait_observes_delayed_release() {
let global = Arc::new(());
let connection = global.clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(10)).await;
drop(connection);
});
assert_eq!(
wait_for_sabre_rpc_disconnects(&global, Duration::from_millis(50)).await,
Ok(())
);
}
#[tokio::test(start_paused = true)]
async fn sabre_rpc_disconnect_wait_reports_stuck_connection() {
let global = Arc::new(());
let _connection = global.clone();
assert_eq!(
wait_for_sabre_rpc_disconnects(&global, Duration::from_millis(10)).await,
Err(1)
);
}
#[tokio::test]
async fn sabre_rpc_server_intentional_abort_is_clean() {
let server_task = tokio::spawn(std::future::pending::<Result<(), &'static str>>());
assert!(stop_sabre_rpc_server(server_task).await.is_ok());
}
#[tokio::test]
async fn sabre_rpc_server_failure_is_reported() {
let server_task = tokio::spawn(async { Err::<(), _>("accept failed") });
while !server_task.is_finished() {
tokio::task::yield_now().await;
}
let error = stop_sabre_rpc_server(server_task).await.unwrap_err();
assert!(error.to_string().contains("accept failed"));
}
#[tokio::test(start_paused = true)]
async fn sabre_rpc_shutdown_drains_connections_after_server_failure() {
let global = Arc::new(());
let connection = global.clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(10)).await;
drop(connection);
});
let server_task = tokio::spawn(async { Err::<(), _>("accept failed") });
while !server_task.is_finished() {
tokio::task::yield_now().await;
}
let error = shutdown_sabre_rpc(server_task, &global, Duration::from_millis(50))
.await
.unwrap_err();
assert!(error.to_string().contains("accept failed"));
assert_eq!(Arc::strong_count(&global), 1);
}
#[test]
fn sabre_binary_resolver_finds_cargo_target_build() {
let temp = tempfile::tempdir().unwrap();
let executable = temp.path().join("target/release/hermit");
let loader = temp.path().join("target/sabre/sabre");
write_test_executable(&loader);
assert_eq!(
resolve_sabre_binary_from(None, None, &executable, OsStr::new("")).unwrap(),
loader
);
}
#[test]
fn sabre_binary_resolver_uses_packaged_loader() {
let temp = tempfile::tempdir().unwrap();
let executable = temp.path().join("target/release/hermit");
let packaged = temp.path().join("install_pkg/rsrcs/sabre");
write_test_executable(&packaged);
assert_eq!(
resolve_sabre_binary_from(None, Some(&packaged), &executable, OsStr::new("")).unwrap(),
packaged
);
}
#[test]
fn sabre_binary_resolver_prefers_and_validates_override() {
let temp = tempfile::tempdir().unwrap();
let executable = temp.path().join("target/release/hermit");
let discovered = temp.path().join("target/sabre/sabre");
let requested = temp.path().join("requested-sabre");
write_test_executable(&discovered);
write_test_executable(&requested);
assert_eq!(
resolve_sabre_binary_from(
Some(requested.as_os_str()),
Some(&discovered),
&executable,
OsStr::new(""),
)
.unwrap(),
requested
);
let mut permissions = fs::metadata(&requested).unwrap().permissions();
permissions.set_mode(0o600);
fs::set_permissions(&requested, permissions).unwrap();
let error = resolve_sabre_binary_from(
Some(requested.as_os_str()),
Some(&discovered),
&executable,
OsStr::new(""),
)
.unwrap_err();
assert!(error.to_string().contains("is not an executable file"));
}
#[test]
#[cfg(feature = "dbt")]
fn optional_backends_report_accurate_availability() {
match Backend::Dbt.ensure_available() {
Ok(()) => assert!(
dynamorio_sdk_available() && dbt_runtime_unavailable_reason().is_none(),
"DBT reported available without its SDK and runtime"
),
Err(dbt_error) => {
let message = dbt_error.to_string();
assert!(
message.contains("DynamoRIO runtime")
|| message.contains("Detcore DBT runtime"),
"unexpected DBT availability error: {message}"
);
}
}
assert_eq!(
Backend::Liteinst.ensure_available().is_ok(),
liteinst_unavailable_reason().is_none()
);
match Backend::Kvm.ensure_available() {
Ok(()) => assert!(
kvm_device_unavailable_reason(std::path::Path::new("/dev/kvm")).is_none(),
"KVM reported available without a usable /dev/kvm",
),
Err(kvm_error) => {
let message = kvm_error.to_string();
assert!(
message.contains("/dev/kvm"),
"unexpected KVM availability error: {message}",
);
assert!(!message.contains("requires root privileges"));
}
}
}
#[test]
fn public_backend_dispatch_rejects_unprepared_e9patch() {
let error = ensure_backend_dispatch(Backend::E9patch).unwrap_err();
assert!(
error.to_string().contains("requires CLI preprocessing"),
"unexpected error: {error}"
);
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_retries_only_the_pre_guest_bootstrap_failure() {
use std::os::unix::process::ExitStatusExt as _;
let failure = std::process::ExitStatus::from_raw(
reverie_dbt::CLIENT_THREAD_START_FAILURE_EXIT_CODE << 8,
);
assert!(super::dbt_client_thread_start_failed(&failure));
assert!(!super::dbt_client_thread_start_failed(
&std::process::ExitStatus::from_raw(1 << 8)
));
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_public_dispatch_requires_sequentialized_threads() {
let command = super::Command::new("/bin/true");
let config = super::DetConfig {
sequentialize_threads: false,
..Default::default()
};
let error = super::run_with_output_backend(command, config, false, &None, Backend::Dbt)
.expect_err("DBT must reject non-sequentialized execution");
assert!(
error
.to_string()
.contains("dbt backend requires sequentialized threads"),
"unexpected error: {error}"
);
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_public_dispatch_runs_echo_through_detcore() {
use clap::Parser;
if Backend::Dbt.ensure_available().is_err() {
return;
}
let mut command = super::Command::new("/bin/echo");
command.arg("hello");
let mut config = super::DetConfig::parse_from(["hermit-dbt-test"]);
config.sequentialize_threads = true;
config.validate();
let output = super::run_with_output_backend(command, config, true, &None, Backend::Dbt)
.expect("run /bin/echo through DbtGuest<Detcore>");
assert_eq!(
output.status,
super::ExitStatus::Exited(0),
"guest did not exit 0.\n--- guest stderr ---\n{}\n--- guest stdout ---\n{}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout),
);
assert_eq!(output.stdout, b"hello\n");
assert!(
String::from_utf8_lossy(&output.stderr)
.lines()
.any(|line| line.starts_with("reverie-dbt: tool=Detcore ")),
"DBT native summary did not prove Detcore dispatch: {}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_public_status_dispatch_runs_true_through_detcore() {
use clap::Parser;
if Backend::Dbt.ensure_available().is_err() {
return;
}
let command = super::Command::new("/bin/true");
let mut config = super::DetConfig::parse_from(["hermit-dbt-test"]);
config.sequentialize_threads = true;
config.validate();
let status = super::run_with_backend(command, config, true, &None, Backend::Dbt)
.expect("run /bin/true through DbtGuest<Detcore>");
assert_eq!(status, super::ExitStatus::Exited(0));
}
#[test]
fn kvm_runs_dynamic_echo_through_detcore() {
use clap::Parser;
if kvm_device_unavailable_reason(std::path::Path::new("/dev/kvm")).is_some() {
return;
}
let mut command = super::Command::new("/bin/echo");
command.arg("hello");
let mut config = super::DetConfig::parse_from(["hermit-kvm-test"]);
config.validate();
let output = super::run_with_output_backend(command, config, false, &None, Backend::Kvm)
.expect("run dynamic /bin/echo through KvmGuest<Detcore>");
assert_eq!(
output.status,
super::ExitStatus::Exited(0),
"guest did not exit 0.\n--- guest stderr ---\n{}\n--- guest stdout ---\n{}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout),
);
assert_eq!(output.stdout, b"hello\n");
assert!(output.stderr.is_empty());
}
#[test]
fn detcore_drives_kvm_guest_for_synthetic_syscall() {
use clap::Parser;
const MEMORY_SIZE: usize = 0x10_000;
const ENTRY_POINT: u64 = 0x1000;
const FRAME_ADDRESS: u64 = 0x2000;
let mut backend = match reverie_kvm::KvmBackend::new(MEMORY_SIZE) {
Ok(backend) => backend,
Err(error) => {
eprintln!("skipping KVM Detcore experiment: cannot init VM: {error}");
return;
}
};
backend
.install_syscall(
ENTRY_POINT,
FRAME_ADDRESS,
reverie_kvm::SyscallRequest::new(libc::SYS_getpid as u64, [0; 6]),
)
.expect("install synthetic getpid guest");
let mut config =
super::DetConfig::parse_from(["hermit-kvm-test", "--max-timeslice=disabled"]);
config.validate();
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("build tokio runtime");
let outcome = runtime.block_on(async {
backend
.run_with_tool::<super::Detcore, _>(
config,
|request: &reverie_kvm::SyscallRequest, _memory: &reverie_kvm::GuestMemory| {
unsafe {
libc::syscall(
request.number() as libc::c_long,
request.args()[0],
request.args()[1],
request.args()[2],
request.args()[3],
request.args()[4],
request.args()[5],
) as i64
}
},
)
.await
});
outcome.expect("Detcore drove the synthetic KVM guest to completion");
}
const FAKE_ELF: &[u8] = b"\x7fELF\x02\x01\x01\x00 fake elf body";
fn shebang_tmpdir(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!(
"hermit-shebang-test-{}-{}",
std::process::id(),
tag
));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
dir
}
#[test]
fn resolve_shebang_plain_elf_is_unchanged() {
let dir = shebang_tmpdir("plain");
let prog = dir.join("prog");
fs::write(&prog, FAKE_ELF).unwrap();
let argv = vec!["prog".to_owned(), "-a".to_owned()];
let (path, out_argv, image) = resolve_kvm_shebang(&prog, argv).unwrap();
assert_eq!(path, prog);
assert_eq!(out_argv, vec!["prog".to_owned(), "-a".to_owned()]);
assert_eq!(image, FAKE_ELF);
fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn resolve_shebang_single_level_kernel_order() {
let dir = shebang_tmpdir("single");
let interp = dir.join("fakebash");
fs::write(&interp, FAKE_ELF).unwrap();
let script = dir.join("script");
fs::write(&script, format!("#!{} -x\necho hi\n", interp.display())).unwrap();
let argv = vec!["script".to_owned(), "arg1".to_owned()];
let (path, out_argv, image) = resolve_kvm_shebang(&script, argv).unwrap();
assert_eq!(path, interp);
assert_eq!(
out_argv,
vec![
interp.to_string_lossy().into_owned(),
"-x".to_owned(),
script.to_string_lossy().into_owned(),
"arg1".to_owned(),
]
);
assert_eq!(image, FAKE_ELF);
fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn resolve_shebang_nested_accumulates_like_binfmt_script() {
let dir = shebang_tmpdir("nested");
let interp = dir.join("fakebash");
fs::write(&interp, FAKE_ELF).unwrap();
let mid = dir.join("mid"); fs::write(&mid, format!("#!{}\n", interp.display())).unwrap();
let script = dir.join("script");
fs::write(&script, format!("#!{} -e\n", mid.display())).unwrap();
let argv = vec!["script".to_owned(), "arg1".to_owned()];
let (path, out_argv, image) = resolve_kvm_shebang(&script, argv).unwrap();
assert_eq!(path, interp);
assert_eq!(
out_argv,
vec![
interp.to_string_lossy().into_owned(),
mid.to_string_lossy().into_owned(),
"-e".to_owned(),
script.to_string_lossy().into_owned(),
"arg1".to_owned(),
]
);
assert_eq!(image, FAKE_ELF);
fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn resolve_shebang_rejects_infinite_recursion() {
let dir = shebang_tmpdir("loop");
let a = dir.join("a");
let b = dir.join("b");
fs::write(&a, format!("#!{}\n", b.display())).unwrap();
fs::write(&b, format!("#!{}\n", a.display())).unwrap();
let argv = vec!["a".to_owned()];
assert!(resolve_kvm_shebang(&a, argv).is_err());
fs::remove_dir_all(&dir).unwrap();
}
mod liteinst_pin_guard {
use std::fs;
use crate::liteinst_runtime_pin_matches;
fn pin_or_skip() -> Option<&'static str> {
let pin = env!("HERMIT_REVERIE_PIN");
(pin != "unknown").then_some(pin)
}
#[test]
fn a_runtime_with_no_recorded_revision_is_refused() {
let Some(_) = pin_or_skip() else { return };
let dir = tempfile::tempdir().unwrap();
let so = dir.path().join("libreverie_liteinst.so");
let error = liteinst_runtime_pin_matches(&so).unwrap_err();
let text = error.to_string();
assert!(
text.contains("records no Reverie revision"),
"must say the runtime carries no revision, said: {text}"
);
assert!(
text.contains("cargo build --release -p hermit-install"),
"must name the restage command, said: {text}"
);
}
#[test]
fn a_runtime_from_another_revision_is_refused_naming_both() {
let Some(pin) = pin_or_skip() else { return };
let dir = tempfile::tempdir().unwrap();
let so = dir.path().join("libreverie_liteinst.so");
let stale = "0".repeat(40);
fs::write(dir.path().join("libreverie_liteinst.so.revision"), &stale).unwrap();
let text = liteinst_runtime_pin_matches(&so).unwrap_err().to_string();
assert!(
text.contains(&stale),
"must name the staged revision: {text}"
);
assert!(text.contains(pin), "must name the binary's pin: {text}");
}
#[test]
fn a_matching_runtime_is_accepted() {
let Some(pin) = pin_or_skip() else { return };
let dir = tempfile::tempdir().unwrap();
let so = dir.path().join("libreverie_liteinst.so");
fs::write(
dir.path().join("libreverie_liteinst.so.revision"),
format!("{pin}\n"),
)
.unwrap();
assert!(
liteinst_runtime_pin_matches(&so).is_ok(),
"the guard must not block a correctly staged runtime"
);
}
#[test]
fn the_loader_consults_the_guard_before_anything_else() {
let Some(pin) = pin_or_skip() else { return };
let dir = tempfile::tempdir().unwrap();
let so = dir.path().join("libreverie_liteinst.so");
fs::write(&so, b"not an elf").unwrap();
let stale = "0".repeat(40);
fs::write(dir.path().join("libreverie_liteinst.so.revision"), &stale).unwrap();
let text = crate::validate_liteinst_runtime_library(&so)
.unwrap_err()
.to_string();
assert!(
text.contains(&stale) && text.contains(pin),
"the loader must refuse on the PIN, naming both revisions, before it \
reaches the export check; said: {text}"
);
}
#[test]
fn a_versioned_soname_finds_its_marker() {
let Some(pin) = pin_or_skip() else { return };
let dir = tempfile::tempdir().unwrap();
let so = dir.path().join("libreverie_liteinst.so.1");
fs::write(
dir.path().join("libreverie_liteinst.so.1.revision"),
format!("{pin}\n"),
)
.unwrap();
assert!(
liteinst_runtime_pin_matches(&so).is_ok(),
"the marker beside a versioned soname must be the one consulted"
);
}
}
mod reverie_pin_reader {
include!("../reverie_pin.rs");
const A: &str = "1111111111111111111111111111111111111111";
const B: &str = "2222222222222222222222222222222222222222";
fn dep(rev: &str) -> String {
format!(
"reverie = {{ git = \"https://github.com/rrnewton/reverie.git\", rev = \"{rev}\" }}\n"
)
}
#[test]
fn a_single_revision_is_read() {
assert_eq!(parse_reverie_pin(&dep(A)).as_deref(), Some(A));
}
#[test]
fn agreeing_revisions_are_read_once() {
let text = format!("{}{}", dep(A), dep(A));
assert_eq!(parse_reverie_pin(&text).as_deref(), Some(A));
}
#[test]
fn disagreeing_revisions_are_refused() {
let text = format!("{}{}", dep(A), dep(B));
assert_eq!(parse_reverie_pin(&text), None);
}
#[test]
fn a_commented_out_dependency_is_not_the_pin() {
let text = format!("# {}{}", dep(B), dep(A));
assert_eq!(
parse_reverie_pin(&text).as_deref(),
Some(A),
"a commented-out revision must not win"
);
}
#[test]
fn a_manifest_naming_no_revision_yields_none() {
assert_eq!(parse_reverie_pin("[dependencies]\nlibc = \"0.2\"\n"), None);
}
#[test]
fn an_unparseable_revision_is_ambiguous() {
let text = format!(
"{}reverie-x = {{ git = \"https://github.com/rrnewton/reverie.git\", rev = \"beef\" }}\n",
dep(A)
);
assert_eq!(parse_reverie_pin(&text), None);
}
}
}