use std::collections::BTreeMap;
#[cfg(feature = "dbt")]
use std::collections::BTreeSet;
#[cfg(feature = "dbt")]
use std::env;
#[cfg(feature = "dbt")]
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fs;
#[cfg(feature = "dbt")]
use std::io::IsTerminal as _;
#[cfg(feature = "dbt")]
use std::io::Read;
#[cfg(feature = "dbt")]
use std::io::Seek as _;
#[cfg(feature = "dbt")]
use std::io::SeekFrom;
#[cfg(feature = "dbt")]
use std::io::Write;
#[cfg(feature = "dbt")]
use std::os::fd::AsRawFd;
#[cfg(feature = "dbt")]
use std::os::fd::FromRawFd;
use std::os::unix::fs::PermissionsExt;
#[cfg(any(feature = "dbt", test))]
use std::os::unix::process::ExitStatusExt as _;
use std::path::Path;
#[cfg(feature = "dbt")]
use std::path::PathBuf;
use std::process::Command as StdCommand;
#[cfg(any(feature = "dbt", test))]
use std::process::Output;
use detcore::Config;
#[cfg(feature = "dbt")]
use detcore_model::backend_engagement::BackendEngagement;
#[cfg(feature = "dbt")]
use detcore_model::backend_engagement::BackendEngagementReport;
use hermit::Error;
use hermit::ExitStatus;
#[cfg(feature = "dbt")]
use reverie_dbt::DbtEvidence;
#[cfg(feature = "dbt")]
use reverie_dbt::DbtEvidenceLogLevel;
#[cfg(feature = "dbt")]
use reverie_dbt::DbtRunner;
#[cfg(feature = "dbt")]
use reverie_dbt::backend_stats::DbtBackendStatsAggregator;
#[cfg(feature = "dbt")]
use reverie_dbt::backend_stats::DbtBackendStatsSnapshot;
use tracing::metadata::LevelFilter;
#[cfg(feature = "dbt")]
use super::record_envelope::RecordEnvelope;
use super::run::VerifyAllow;
#[cfg(feature = "dbt")]
use super::verify::ComparedRun;
#[cfg(feature = "dbt")]
use super::verify::ComparisonOptions;
#[cfg(feature = "dbt")]
use super::verify::DbtCountedBranchComparison;
#[cfg(feature = "dbt")]
use super::verify::LogCompareStrictness;
#[cfg(feature = "dbt")]
use super::verify::SecondRun;
#[cfg(feature = "dbt")]
use super::verify::Verdict;
#[cfg(feature = "dbt")]
use super::verify::VerificationOutcome;
#[cfg(feature = "dbt")]
use super::verify::announce_verification_outcome;
#[cfg(feature = "dbt")]
use super::verify::compare_two_runs;
#[cfg(feature = "dbt")]
use super::verify::retain_verification_logs;
#[cfg(feature = "dbt")]
use super::verify::temp_log_files_in;
#[cfg(feature = "dbt")]
use super::verify::verification_log_level;
#[cfg(feature = "dbt")]
use super::verify::write_pending_verification_json;
#[cfg(feature = "dbt")]
use super::verify::write_verification_json;
#[cfg(feature = "dbt")]
fn same_dbt_observable_behavior(
first: &DbtBackendStatsSnapshot,
second: &DbtBackendStatsSnapshot,
) -> bool {
first.intercepted_syscalls() == second.intercepted_syscalls()
&& first.rewritten_syscalls() == second.rewritten_syscalls()
&& first.stdin_reads() == second.stdin_reads()
&& first.memory_hash_fold() == second.memory_hash_fold()
}
#[cfg(feature = "dbt")]
fn dbt_branch_clock_mismatch(first: u64, second: u64) -> Option<String> {
(first != second).then(|| {
format!(
"DBT verification failed: counted-branch clocks differed between runs ({first} != {second}); \
the clock is a deterministic function of the executed instruction stream"
)
})
}
#[cfg(feature = "dbt")]
fn dbt_initialization_record_mismatch(first: usize, second: usize) -> Option<String> {
(first != second).then(|| {
format!(
"DBT verification failed: authenticated process-image initialization counts differed between runs ({first} != {second})"
)
})
}
#[cfg(feature = "dbt")]
#[derive(Debug)]
struct DbtSummaryComparison {
failure: Option<String>,
}
#[cfg(feature = "dbt")]
impl DbtSummaryComparison {
fn compare(
first_stats: &DbtBackendStatsSnapshot,
second_stats: &DbtBackendStatsSnapshot,
first_initialization_records: usize,
second_initialization_records: usize,
) -> Self {
let mut failures = Vec::new();
if !same_dbt_observable_behavior(first_stats, second_stats) {
failures.push(format!(
"DBT verification failed: typed native Detcore statistics differed \
({first_stats:?} != {second_stats:?})"
));
}
if let Some(failure) = dbt_initialization_record_mismatch(
first_initialization_records,
second_initialization_records,
) {
failures.push(failure);
}
Self {
failure: (!failures.is_empty()).then(|| failures.join("; ")),
}
}
fn requires_log_retention(&self) -> bool {
self.failure.is_some()
}
fn apply(self, outcome: &mut VerificationOutcome) -> Option<String> {
if self.failure.is_some() {
outcome.verdict = Verdict::Diverged;
outcome.no_result_reason = None;
}
self.failure
}
}
#[cfg(feature = "dbt")]
fn record_dbt_branch_clock_comparison(
outcome: &mut VerificationOutcome,
comparison: DbtCountedBranchComparison,
) -> Option<String> {
match dbt_branch_clock_mismatch(comparison.left, comparison.right) {
Some(message) => {
outcome.dbt_counted_branches = Some(comparison);
outcome.verdict = Verdict::Diverged;
outcome.no_result_reason = None;
Some(message)
}
None if outcome.verdict == Verdict::NoResult => {
outcome.dbt_counted_branches = None;
None
}
None => {
outcome.dbt_counted_branches = Some(comparison);
None
}
}
}
#[cfg(feature = "dbt")]
fn finalize_dbt_verification(
mut outcome: VerificationOutcome,
comparison: DbtCountedBranchComparison,
verify_json: Option<&Path>,
) -> Result<(VerificationOutcome, Option<String>), Error> {
let failure = record_dbt_branch_clock_comparison(&mut outcome, comparison);
if let Some(path) = verify_json {
write_verification_json(path, &outcome)?;
}
Ok((outcome, failure))
}
#[cfg(feature = "dbt")]
struct DbtStatsCapture {
_directory: tempfile::TempDir,
path: PathBuf,
}
#[cfg(feature = "dbt")]
impl DbtStatsCapture {
fn new() -> Result<Self, Error> {
let directory = tempfile::Builder::new()
.prefix("hermit-dbt-verify-stats-")
.tempdir()
.map_err(|error| {
Error::msg(format!(
"failed to create DBT whole-process statistics sink: {error}"
))
})?;
let path = directory.path().join("records.bin");
Ok(Self {
_directory: directory,
path,
})
}
fn configure(&self, runner: DbtRunner) -> DbtRunner {
runner
.client_argument("-stats_path")
.client_argument(self.path.clone().into_os_string())
}
fn finish(self) -> Result<DbtBackendStatsSnapshot, Error> {
let bytes = match fs::read(&self.path) {
Ok(bytes) if !bytes.is_empty() => bytes,
Ok(_) => {
return Err(Error::msg(
"DBT verification did not reach a verdict: typed whole-process statistics were empty",
));
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Err(Error::msg(
"DBT verification did not reach a verdict: typed whole-process statistics were missing",
));
}
Err(error) => {
return Err(Error::msg(format!(
"DBT verification did not reach a verdict: failed to read typed whole-process statistics: {error}"
)));
}
};
let mut aggregator = DbtBackendStatsAggregator::new();
let records = aggregator.absorb_wire_stream(&bytes).map_err(|error| {
Error::msg(format!(
"DBT verification did not reach a verdict: typed whole-process statistics were unreadable: {error}"
))
})?;
if records == 0 {
return Err(Error::msg(
"DBT verification did not reach a verdict: typed whole-process statistics contained no process records",
));
}
let snapshot = aggregator.snapshot();
if snapshot.counted_branches() == 0
|| snapshot.intercepted_syscalls() == 0
|| snapshot.rewritten_syscalls() > snapshot.intercepted_syscalls()
{
return Err(Error::msg(
"DBT typed native callback counters are inconsistent",
));
}
Ok(snapshot)
}
}
#[cfg(feature = "dbt")]
fn format_dbt_stats(summary: &DbtBackendStatsSnapshot) -> String {
format!(
"=== DBT backend stats (native DynamoRIO client) ===\n\
counted branches (deterministic branch clock): {}\n\
syscalls intercepted: {}\n\
syscall instructions rewritten: {}\n\
stdin (fd 0) reads: {}\n\
observed guest-memory hash: {}\n",
summary.counted_branches(),
summary.intercepted_syscalls(),
summary.rewritten_syscalls(),
summary.stdin_reads(),
format_args!("{:016x}", summary.memory_hash_fold()),
)
}
#[cfg(feature = "dbt")]
fn write_dbt_engagement(path: &Path, snapshot: &DbtBackendStatsSnapshot) -> Result<(), Error> {
let report = BackendEngagementReport::new(BackendEngagement::Dbt {
counted_branches: snapshot.counted_branches(),
});
report.validate().map_err(Error::msg)?;
let mut bytes = serde_json::to_vec(&report)?;
bytes.push(b'\n');
fs::write(path, bytes).map_err(|error| {
Error::msg(format!(
"writing DBT engagement {}: {error}",
path.display()
))
})
}
#[cfg(feature = "dbt")]
fn finish_single_run_dbt_stats(
capture: DbtStatsCapture,
summary: bool,
engagement_json: Option<&Path>,
) -> Result<(), Error> {
match capture.finish() {
Ok(stats) => {
if summary {
eprint!("{}", format_dbt_stats(&stats));
}
if let Some(path) = engagement_json {
write_dbt_engagement(path, &stats)?;
}
Ok(())
}
Err(error) if engagement_json.is_some() => Err(error),
Err(error) => {
eprintln!(":: DBT summary unavailable: {error}");
Ok(())
}
}
}
#[derive(Debug, Eq, PartialEq)]
#[cfg(feature = "dbt")]
struct DbtGuestCommand {
program: PathBuf,
args: Vec<OsString>,
}
#[cfg(feature = "dbt")]
fn executable_on_path(program: &OsStr, path: &OsStr, cwd: &Path) -> Option<PathBuf> {
executable_path_candidate(program, path, cwd).map(|candidate| cwd.join(candidate))
}
#[cfg(feature = "dbt")]
fn executable_path_candidate(program: &OsStr, path: &OsStr, cwd: &Path) -> Option<PathBuf> {
env::split_paths(path)
.map(|directory| directory.join(program))
.find(|candidate| {
cwd.join(candidate).metadata().is_ok_and(|metadata| {
metadata.is_file() && metadata.permissions().mode() & 0o111 != 0
})
})
}
#[cfg(feature = "dbt")]
fn prepare_dbt_guest_command(
program: &Path,
args: &[OsString],
path: Option<&OsStr>,
cwd: &Path,
) -> DbtGuestCommand {
let unchanged = || DbtGuestCommand {
program: program.to_path_buf(),
args: args.to_vec(),
};
let script_path =
if program.is_absolute() || program.as_os_str().as_encoded_bytes().contains(&b'/') {
program.to_path_buf()
} else if let Some(found) =
path.and_then(|path| executable_path_candidate(program.as_os_str(), path, cwd))
{
found
} else {
return unchanged();
};
let Some(shebang) = hermit::Shebang::new(cwd.join(&script_path)) else {
return unchanged();
};
let (interpreter, interpreter_args) = shebang.into_parts();
if interpreter.file_name() != Some(OsStr::new("env")) || interpreter_args.len() != 1 {
return unchanged();
}
let target = &interpreter_args[0];
let target_bytes = std::os::unix::ffi::OsStrExt::as_bytes(target.as_os_str());
if target_bytes.starts_with(b"-")
|| target_bytes.contains(&b'=')
|| target_bytes.contains(&b'/')
{
return unchanged();
}
let Some(target) = path.and_then(|path| executable_on_path(target, path, cwd)) else {
return unchanged();
};
let mut resolved_args = Vec::with_capacity(args.len() + 2);
resolved_args.push(target.into_os_string());
resolved_args.push(script_path.into_os_string());
resolved_args.extend_from_slice(args);
DbtGuestCommand {
program: interpreter,
args: resolved_args,
}
}
#[cfg(feature = "dbt")]
fn prepare_dbt_physical_command(guest: &StdCommand) -> Result<StdCommand, Error> {
let environment = guest
.get_envs()
.filter_map(|(key, value)| value.map(|value| (key.to_owned(), value.to_owned())))
.collect::<BTreeMap<_, _>>();
let args = guest.get_args().map(OsStr::to_owned).collect::<Vec<_>>();
let cwd = env::current_dir()?;
let cwd = guest
.get_current_dir()
.map_or(cwd.clone(), |workdir| cwd.join(workdir));
let prepared = prepare_dbt_guest_command(
Path::new(guest.get_program()),
&args,
environment.get(OsStr::new("PATH")).map(OsString::as_os_str),
&cwd,
);
let mut command = StdCommand::new(prepared.program);
command.args(prepared.args);
for (name, value) in guest.get_envs() {
if let Some(value) = value {
command.env(name, value);
} else {
command.env_remove(name);
}
}
if let Some(workdir) = guest.get_current_dir() {
command.current_dir(workdir);
}
Ok(command)
}
#[cfg(feature = "dbt")]
fn apply_exact_environment(command: &mut StdCommand, environment: &BTreeMap<OsString, OsString>) {
for (key, _) in env::vars_os() {
if !environment.contains_key(&key) {
command.env_remove(key);
}
}
command.envs(environment);
}
#[cfg(feature = "dbt")]
fn apply_dbt_workdir(command: &mut StdCommand, workdir: Option<&Path>) {
if let Some(workdir) = workdir {
command.current_dir(workdir);
}
}
#[cfg(feature = "dbt")]
struct InstalledFd {
target: i32,
backup: Option<i32>,
original_flags: Option<i32>,
}
#[cfg(feature = "dbt")]
impl InstalledFd {
fn install(source: i32, target: i32) -> std::io::Result<Self> {
let backup = unsafe {
libc::fcntl(
target,
libc::F_DUPFD_CLOEXEC,
detcore_dbt::UNSUPPORTED_SYSCALL_REPORT_FD + 1,
)
};
let backup = if backup == -1 {
let error = std::io::Error::last_os_error();
if error.raw_os_error() == Some(libc::EBADF) {
None
} else {
return Err(error);
}
} else {
Some(backup)
};
let original_flags = if let Some(backup_fd) = backup {
let flags = unsafe { libc::fcntl(target, libc::F_GETFD) };
if flags == -1 {
let error = std::io::Error::last_os_error();
let _ = unsafe { libc::close(backup_fd) };
return Err(error);
}
Some(flags)
} else {
None
};
let installed = Self {
target,
backup,
original_flags,
};
if unsafe { libc::dup2(source, target) } == -1 {
return Err(std::io::Error::last_os_error());
}
if unsafe { libc::fcntl(target, libc::F_SETFD, 0) } == -1 {
let error = std::io::Error::last_os_error();
drop(installed);
return Err(error);
}
Ok(installed)
}
}
#[cfg(feature = "dbt")]
impl Drop for InstalledFd {
fn drop(&mut self) {
if let Some(backup) = self.backup {
let _ = unsafe { libc::dup2(backup, self.target) };
if let Some(flags) = self.original_flags {
let _ = unsafe { libc::fcntl(self.target, libc::F_SETFD, flags) };
}
let _ = unsafe { libc::close(backup) };
} else {
let _ = unsafe { libc::close(self.target) };
}
}
}
#[cfg(feature = "dbt")]
struct DbtUnsupportedSyscallReport {
reader: std::fs::File,
_writer: std::fs::File,
_report_fd: InstalledFd,
}
#[cfg(feature = "dbt")]
impl DbtUnsupportedSyscallReport {
fn new() -> std::io::Result<Self> {
let mut descriptors = [-1; 2];
let result =
unsafe { libc::pipe2(descriptors.as_mut_ptr(), libc::O_CLOEXEC | libc::O_NONBLOCK) };
if result == -1 {
return Err(std::io::Error::last_os_error());
}
let reader = unsafe { std::fs::File::from_raw_fd(descriptors[0]) };
let writer = unsafe { std::fs::File::from_raw_fd(descriptors[1]) };
let report_fd = InstalledFd::install(
writer.as_raw_fd(),
detcore_dbt::UNSUPPORTED_SYSCALL_REPORT_FD,
)?;
Ok(Self {
reader,
_writer: writer,
_report_fd: report_fd,
})
}
fn emit(&mut self) -> std::io::Result<()> {
const MAX_REPORT_BYTES: usize = 1024 * 1024;
let mut contents = Vec::new();
let mut buffer = [0_u8; 4096];
loop {
match self.reader.read(&mut buffer) {
Ok(0) => break,
Ok(read) => {
if contents.len() + read > MAX_REPORT_BYTES {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"DBT unsupported-syscall report exceeded 1 MiB",
));
}
contents.extend_from_slice(&buffer[..read]);
}
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => break,
Err(error) => return Err(error),
}
}
let contents = String::from_utf8_lossy(&contents);
let syscalls = contents
.lines()
.filter_map(|line| {
if let Some(raw) = line.strip_prefix("@") {
let sysno = raw
.parse::<i32>()
.ok()
.map(reverie::syscalls::Sysno::from)?;
detcore::is_unsupported_syscall(sysno).then(|| sysno.to_string())
} else if !line.is_empty()
&& line.len() <= 64
&& line
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
{
Some(line.to_owned())
} else {
None
}
})
.take(512)
.collect::<BTreeSet<_>>();
if let Some(message) = detcore::format_unsupported_syscall_warning(&syscalls) {
eprintln!("WARNING: {message}");
}
Ok(())
}
}
#[cfg(feature = "dbt")]
impl Drop for DbtUnsupportedSyscallReport {
fn drop(&mut self) {
if let Err(error) = self.emit() {
eprintln!("WARNING: failed to read DBT unsupported-syscall report: {error}");
}
}
}
#[cfg(feature = "dbt")]
struct TeeReader<R, W> {
input: R,
replay: W,
}
#[cfg(feature = "dbt")]
impl<R: Read, W: Write> Read for TeeReader<R, W> {
fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
let read = self.input.read(buffer)?;
self.replay.write_all(&buffer[..read])?;
Ok(read)
}
}
#[cfg(feature = "dbt")]
fn dbt_evidence_log_level(
requested: Option<LevelFilter>,
diagnostic_full_trace: bool,
) -> DbtEvidenceLogLevel {
let level = verification_log_level(
requested,
LogCompareStrictness::Canonical,
diagnostic_full_trace,
);
if level >= LevelFilter::TRACE {
DbtEvidenceLogLevel::Trace
} else if level >= LevelFilter::DEBUG {
DbtEvidenceLogLevel::Debug
} else {
DbtEvidenceLogLevel::Info
}
}
#[cfg(feature = "dbt")]
fn decode_dbt_evidence(file: &mut std::fs::File) -> Result<DbtEvidence, Error> {
file.seek(SeekFrom::Start(0))?;
let mut encoded = Vec::new();
file.read_to_end(&mut encoded)?;
if encoded.is_empty() {
return Err(Error::msg("DBT canonical evidence was empty"));
}
reverie_dbt::decode_evidence(&encoded).map_err(|error| {
Error::msg(format!(
"DBT canonical evidence was malformed or truncated: {error}"
))
})
}
#[cfg(feature = "dbt")]
fn materialize_dbt_evidence_log(
evidence: &DbtEvidence,
log: std::fs::File,
path: &Path,
) -> Result<usize, Error> {
let published = evidence.all_records();
materialize_dbt_comparison_log(evidence.records(), &published, log, path)
}
#[cfg(feature = "dbt")]
fn materialize_dbt_comparison_log(
guest_records: &[Vec<u8>],
published: &[&[u8]],
mut log: std::fs::File,
path: &Path,
) -> Result<usize, Error> {
if guest_records.is_empty() {
return Err(Error::msg("DBT canonical evidence contained no records"));
}
let mut write_and_count = |records: &[&[u8]]| -> Result<usize, Error> {
write_dbt_comparison_records(&mut log, path, records)?;
detcore::logdiff::write_canonical_info(path, &mut std::io::sink()).map_err(|error| {
Error::msg(format!(
"DBT canonical evidence did not contain a valid log stream: {error}"
))
})
};
let guest_records: Vec<&[u8]> = guest_records.iter().map(Vec::as_slice).collect();
if write_and_count(guest_records.as_slice())? == 0 {
return Err(Error::msg(
"DBT canonical evidence contained no INFO records",
));
}
write_and_count(published)
}
#[cfg(feature = "dbt")]
fn write_dbt_comparison_records(
log: &mut std::fs::File,
path: &Path,
records: &[&[u8]],
) -> Result<(), Error> {
log.set_len(0)?;
log.seek(SeekFrom::Start(0))?;
for record in records {
let Some(payload) = record.strip_suffix(b"\n") else {
return Err(Error::msg(
"DBT canonical evidence record was missing its terminal newline",
));
};
if payload.contains(&b'\n') || payload.contains(&b'\r') {
return Err(Error::msg(
"DBT canonical evidence record contained an embedded line boundary",
));
}
log.write_all(record)?;
}
log.flush()?;
let materialized = std::fs::read(path)
.map_err(|error| Error::msg(format!("DBT canonical evidence log unreadable: {error}")))?
.iter()
.filter(|byte| **byte == b'\n')
.count();
if materialized != records.len() {
return Err(Error::msg(format!(
"DBT canonical evidence log holds {materialized} records but {} were decoded; the \
comparison publishes the all_records_v1 envelope and must not drop any",
records.len()
)));
}
Ok(())
}
#[cfg(feature = "dbt")]
fn dbt_verification_output(output: Output) -> reverie::process::Output {
reverie::process::Output {
status: process_status(output.status),
stdout: output.stdout,
stderr: output.stderr,
}
}
#[cfg(any(feature = "dbt", test))]
pub(super) const DBT_LOG_FILE_REFUSAL: &str =
"DBT --log-file is unavailable on the ordinary single-run adapter";
#[cfg(any(feature = "dbt", test))]
fn dbt_log_file_is_refused(log_file_requested: bool, verify: bool) -> bool {
log_file_requested && !verify
}
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "dbt")]
pub(super) fn run_dbt(
program: &Path,
args: &[String],
verify: bool,
verify_verbose: bool,
verify_allow: VerifyAllow,
print_verify_logs: bool,
keep_logs: bool,
verify_log_dir: Option<&Path>,
verify_json: Option<&Path>,
summary: bool,
backend_engagement_json: Option<&Path>,
log: Option<LevelFilter>,
log_file: Option<&Path>,
config: &Config,
mut environment: BTreeMap<OsString, OsString>,
workdir: Option<&Path>,
verification_stdin: Option<std::fs::File>,
) -> Result<ExitStatus, Error> {
if let Some(path) = verify_json.filter(|_| verify) {
write_pending_verification_json(path)?;
}
if dbt_log_file_is_refused(log_file.is_some(), verify) {
return Err(Error::msg(DBT_LOG_FILE_REFUSAL));
}
if !config.sequentialize_threads {
return Err(Error::msg(
"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| {
Error::msg(format!(
"failed to serialize the Detcore config for the DBT backend: {error}"
))
})?;
let panic_on_unsupported_syscalls = config.panic_on_unsupported_syscalls;
let marker = std::env::var_os(hermit_test_workdir::REQUEST_ENV);
let isolated_workdir = hermit_test_workdir::requested_workdir(marker.as_deref())?;
if isolated_workdir.is_some() && workdir != isolated_workdir {
return Err(Error::msg(
"HERMIT_E2E_EMPTY_WORKDIR=/test requires --workdir=/test for DBT",
));
}
let stdin_is_terminal = std::io::stdin().is_terminal();
let (drrun, client) = detcore_dbt::prepare_native_client().map_err(|error| {
Error::msg(format!(
"failed to prepare the Detcore DynamoRIO client: {error}"
))
})?;
let single_run_stats = (!verify && (summary || backend_engagement_json.is_some()))
.then(DbtStatsCapture::new)
.transpose()?;
let mut runner = DbtRunner::new(&drrun, &client)
.map_err(|error| {
Error::msg(format!(
"failed to configure the DynamoRIO DBT runner (drrun={}, client={}): {error}",
drrun.display(),
client.display()
))
})?
.summary(summary)
.isolated_process_group(panic_on_unsupported_syscalls);
if let Some(capture) = &single_run_stats {
runner = capture.configure(runner);
}
if panic_on_unsupported_syscalls {
runner = runner.client_argument("-panic-on-unsupported-syscalls");
}
eprintln!(
"hermit: [dbt backend] Detcore Tool active; running {program:?} under DynamoRIO ({})",
drrun.display()
);
let _unsupported_report = DbtUnsupportedSyscallReport::new()?;
let mut guest = StdCommand::new(program);
if !verify && let Some(level) = log {
environment.insert("HERMIT_LOG".into(), level.to_string().into());
}
environment.remove(OsStr::new("HERMIT_LOG_FILE"));
environment.insert(detcore_dbt::DETCONFIG_ENV.into(), config_json.into());
apply_exact_environment(&mut guest, &environment);
guest.args(args);
apply_dbt_workdir(&mut guest, workdir);
let execution = DbtExecution::new(isolated_workdir)?;
if !verify {
if stdin_is_terminal {
let status = run_status(&execution, &runner, &guest, &drrun, config)?;
if let Some(capture) = single_run_stats {
finish_single_run_dbt_stats(capture, summary, backend_engagement_json)?;
}
return Ok(process_status(status));
}
let output = run_once(
&execution,
&runner,
&guest,
&drrun,
config,
std::io::stdin(),
)?;
write_output(&output)?;
if let Some(capture) = single_run_stats {
finish_single_run_dbt_stats(capture, summary, backend_engagement_json)?;
}
return Ok(output_status(&output));
}
let (log1, log2) = temp_log_files_in("run1", "run2", verify_log_dir)
.map_err(|error| Error::msg(format!("failed to create DBT verification logs: {error}")))?;
let (log1_file, log1_path) = log1.into_parts();
let (log2_file, log2_path) = log2.into_parts();
let evidence_level = dbt_evidence_log_level(log, verify_verbose);
let mut evidence1 = tempfile::tempfile()?;
let stats1 = DbtStatsCapture::new()?;
let runner1 = stats1
.configure(runner.clone())
.evidence_file(&evidence1)
.map_err(|error| {
Error::msg(format!(
"failed to configure protected DBT run-1 evidence: {error}"
))
})?
.evidence_log_level(evidence_level);
let mut evidence2 = tempfile::tempfile()?;
let stats2 = DbtStatsCapture::new()?;
let runner2 = stats2
.configure(runner)
.evidence_file(&evidence2)
.map_err(|error| {
Error::msg(format!(
"failed to configure protected DBT run-2 evidence: {error}"
))
})?
.evidence_log_level(evidence_level);
let mut replay = tempfile::tempfile()?;
let terminal_stdin = verification_stdin.as_ref().is_some_and(|file| {
(unsafe { libc::isatty(file.as_raw_fd()) }) == 1
});
let replayable_stdin = verification_stdin.filter(|file| {
(unsafe { libc::isatty(file.as_raw_fd()) }) != 1
});
eprintln!(":: DBT Run1...");
let first_raw = if terminal_stdin {
run_once_with_terminal_input(&execution, &runner1, &guest, &drrun, config)
} else {
match replayable_stdin {
Some(input) => run_once(
&execution,
&runner1,
&guest,
&drrun,
config,
TeeReader {
input,
replay: replay.try_clone()?,
},
),
None => run_once(
&execution,
&runner1,
&guest,
&drrun,
config,
std::io::empty(),
),
}
};
let first_raw = match first_raw {
Ok(output) => output,
Err(error) => {
if keep_logs {
retain_verification_logs([("run 1", log1_path)])?;
}
return Err(error);
}
};
let first_evidence = match decode_dbt_evidence(&mut evidence1) {
Ok(evidence) => evidence,
Err(error) => {
if keep_logs {
retain_verification_logs([("run 1", log1_path)])?;
}
return Err(error);
}
};
if let Err(error) = materialize_dbt_evidence_log(&first_evidence, log1_file, &log1_path) {
if keep_logs {
retain_verification_logs([("run 1", log1_path)])?;
}
return Err(error);
}
if print_verify_logs {
std::io::stderr().write_all(&fs::read(&log1_path)?)?;
}
if !verify_allow.satisfies(process_status(first_raw.status)) {
let first = dbt_verification_output(first_raw);
eprintln!(
"First run errored during --verify, not continuing to a second. Stdout:\n{}\nStderr:\n{}",
String::from_utf8_lossy(&first.stdout),
String::from_utf8_lossy(&first.stderr),
);
if keep_logs {
retain_verification_logs([("run 1", log1_path)])?;
}
return Err(Error::msg("First run during --verify exited in error"));
}
let first_stats = match stats1.finish() {
Ok(stats) => stats,
Err(error) => {
if keep_logs {
retain_verification_logs([("run 1", log1_path)])?;
}
return Err(error);
}
};
if terminal_stdin && first_stats.stdin_reads() != 0 {
let first = dbt_verification_output(first_raw);
std::io::stdout().write_all(&first.stdout)?;
std::io::stderr().write_all(&first.stderr)?;
if keep_logs {
retain_verification_logs([("run 1", log1_path)])?;
}
return Err(Error::msg(format!(
"DBT verification cannot replay terminal stdin: guest attempted {} fd-0 read syscall(s)",
first_stats.stdin_reads()
)));
}
let first = dbt_verification_output(first_raw);
replay.seek(SeekFrom::Start(0))?;
eprintln!(":: DBT Run2...");
let second_raw = match if terminal_stdin {
run_once_with_terminal_input(&execution, &runner2, &guest, &drrun, config)
} else {
run_once(
&execution,
&runner2,
&guest,
&drrun,
config,
replay.try_clone()?,
)
} {
Ok(output) => output,
Err(error) => {
if keep_logs {
retain_verification_logs([("run 1", log1_path), ("run 2", log2_path)])?;
}
return Err(error);
}
};
let second_evidence = match decode_dbt_evidence(&mut evidence2) {
Ok(evidence) => evidence,
Err(error) => {
if keep_logs {
retain_verification_logs([("run 1", log1_path), ("run 2", log2_path)])?;
}
return Err(error);
}
};
if let Err(error) = materialize_dbt_evidence_log(&second_evidence, log2_file, &log2_path) {
if keep_logs {
retain_verification_logs([("run 1", log1_path), ("run 2", log2_path)])?;
}
return Err(error);
}
let second_stats = match stats2.finish() {
Ok(stats) => stats,
Err(error) => {
if keep_logs {
retain_verification_logs([("run 1", log1_path), ("run 2", log2_path)])?;
}
return Err(error);
}
};
let second = dbt_verification_output(second_raw);
let branch_clock_comparison = DbtCountedBranchComparison {
left: first_stats.counted_branches(),
right: second_stats.counted_branches(),
};
let branch_clock_diverged = !branch_clock_comparison.matched();
let summary_comparison = DbtSummaryComparison::compare(
&first_stats,
&second_stats,
first_evidence.initialization_records(),
second_evidence.initialization_records(),
);
let mut outcome = compare_two_runs(
ComparedRun {
output: &first,
log: log1_path,
label: "run 1",
},
ComparedRun {
output: &second,
log: log2_path,
label: "run 2",
},
ComparisonOptions {
verbose: verify_verbose,
strictness: LogCompareStrictness::Canonical,
compare_logs: true,
diagnostic_full_trace: verify_verbose,
compare_io_buffers: config.detlog_io_buffers,
virtualize_time: config.virtualize_time,
keep_logs: keep_logs
|| branch_clock_diverged
|| summary_comparison.requires_log_retention(),
match_overridden: branch_clock_diverged || summary_comparison.requires_log_retention(),
failed_log_retention: (!keep_logs)
.then(super::verify::default_failed_verify_log_retention),
record_envelope: RecordEnvelope::all_records_v1(),
},
)?;
let summary_failure = summary_comparison.apply(&mut outcome);
let (outcome, branch_clock_failure) =
finalize_dbt_verification(outcome, branch_clock_comparison, verify_json)?;
if let Some(message) = branch_clock_failure {
eprintln!(":: {message}");
}
if let Some(message) = summary_failure {
eprintln!(":: {message}");
}
eprintln!(":: DBT path confirmed: DynamoRIO client reported tool=Detcore");
let success_message = if config.detlog_io_buffers {
"Success: deterministic. Determinism verified."
} else {
"Success: deterministic. Determinism verified. NOTE: syscall \
output-buffer CONTENT was not compared because --no-detlog-io-buffers \
was given, so a divergence confined to a buffer whose length is stable \
would not have been seen; drop that flag to include it."
};
announce_verification_outcome(
&outcome,
SecondRun::Rerun,
success_message,
"Failure: nondeterministic.",
);
if !outcome.verified() {
return outcome.into_exit_status();
}
std::io::stdout().write_all(&first.stdout)?;
std::io::stderr().write_all(&first.stderr)?;
Ok(outcome.guest_status)
}
#[allow(clippy::too_many_arguments)]
#[cfg(not(feature = "dbt"))]
pub(super) fn run_dbt(
_program: &Path,
_args: &[String],
_verify: bool,
_verify_verbose: bool,
_verify_allow: VerifyAllow,
_print_verify_logs: bool,
_keep_logs: bool,
_verify_log_dir: Option<&Path>,
_verify_json: Option<&Path>,
_summary: bool,
_backend_engagement_json: Option<&Path>,
_log: Option<LevelFilter>,
_log_file: Option<&Path>,
_config: &Config,
_environment: BTreeMap<OsString, OsString>,
_workdir: Option<&Path>,
_verification_stdin: Option<std::fs::File>,
) -> Result<ExitStatus, Error> {
Err(Error::msg("DBT support was not included in this build"))
}
#[cfg(feature = "dbt")]
struct DbtExecution {
runtime: Option<tokio::runtime::Runtime>,
}
#[cfg(feature = "dbt")]
impl DbtExecution {
fn new(workdir: Option<&Path>) -> Result<Self, Error> {
Ok(Self {
runtime: if workdir.is_some() {
None
} else {
Some(Self::new_runtime()?)
},
})
}
fn new_runtime() -> Result<tokio::runtime::Runtime, Error> {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.map_err(|error| Error::msg(format!("failed to start the DBT coordinator: {error}")))
}
fn run<F, T>(&self, run: F) -> Result<T, Error>
where
F: FnOnce(&tokio::runtime::Runtime) -> Result<T, Error> + Send,
T: Send,
{
if let Some(runtime) = &self.runtime {
return run(runtime);
}
hermit_test_workdir::with_isolated_workdir(move || {
let runtime = Self::new_runtime()?;
run(&runtime)
})?
}
}
#[cfg(feature = "dbt")]
fn run_once<R: Read + Send + 'static>(
execution: &DbtExecution,
runner: &DbtRunner,
guest: &StdCommand,
drrun: &Path,
config: &Config,
input: R,
) -> Result<Output, Error> {
execution.run(move |runtime| {
let guest = prepare_dbt_physical_command(guest)?;
let (output, global) = runtime
.block_on(
runner.output_with_detached_reader_and_global::<detcore::GlobalState, _>(
&guest,
input,
config.clone(),
),
)
.map_err(|error| dbt_run_error(drrun, error))?;
runtime.block_on(clean_up_dbt_global(&output.status, global));
Ok(output)
})
}
#[cfg(feature = "dbt")]
fn run_once_with_terminal_input(
execution: &DbtExecution,
runner: &DbtRunner,
guest: &StdCommand,
drrun: &Path,
config: &Config,
) -> Result<Output, Error> {
execution.run(move |runtime| {
let guest = prepare_dbt_physical_command(guest)?;
let (output, global) = runtime
.block_on(
runner.output_with_inherited_stdin_and_global::<detcore::GlobalState>(
&guest,
config.clone(),
),
)
.map_err(|error| dbt_run_error(drrun, error))?;
runtime.block_on(clean_up_dbt_global(&output.status, global));
Ok(output)
})
}
#[cfg(feature = "dbt")]
fn run_status(
execution: &DbtExecution,
runner: &DbtRunner,
guest: &StdCommand,
drrun: &Path,
config: &Config,
) -> Result<std::process::ExitStatus, Error> {
execution.run(move |runtime| {
let guest = prepare_dbt_physical_command(guest)?;
let (status, global) = runtime
.block_on(runner.status_with_global::<detcore::GlobalState>(&guest, config.clone()))
.map_err(|error| dbt_run_error(drrun, error))?;
runtime.block_on(clean_up_dbt_global(&status, global));
Ok(status)
})
}
#[cfg(feature = "dbt")]
async fn clean_up_dbt_global(status: &std::process::ExitStatus, mut global: detcore::GlobalState) {
if !status.success() {
global.force_shutdown_with_error();
global.cancel_internal_scheduler().await;
}
global.clean_up(false, &None).await;
}
#[cfg(feature = "dbt")]
fn dbt_run_error(drrun: &Path, error: std::io::Error) -> Error {
use std::io::ErrorKind;
match error.kind() {
ErrorKind::NotFound | ErrorKind::PermissionDenied | ErrorKind::ExecutableFileBusy => {
Error::msg(format!(
"failed to launch drrun ({}): {error}",
drrun.display()
))
}
_ => Error::msg(format!(
"drrun started ({}) and the DBT run then failed: {error} \
-- this is NOT a launch failure; the drrun binary is not implicated. \
Read the text after the last ':' for the stage that actually failed.",
drrun.display()
)),
}
}
#[cfg(any(feature = "dbt", test))]
fn process_status(status: std::process::ExitStatus) -> ExitStatus {
ExitStatus::from_raw(status.into_raw())
}
#[cfg(feature = "dbt")]
fn write_output(output: &Output) -> Result<(), Error> {
std::io::stdout().write_all(&output.stdout)?;
std::io::stderr().write_all(&output.stderr)?;
Ok(())
}
#[cfg(any(feature = "dbt", test))]
fn output_status(output: &Output) -> ExitStatus {
ExitStatus::from_raw(output.status.into_raw())
}
fn sabre_artifact(variable: &str, description: &str, executable: bool) -> Result<OsString, Error> {
let value = std::env::var_os(variable).ok_or_else(|| {
Error::msg(format!(
"the sabre backend needs {variable}=<path-to-{description}>"
))
})?;
validate_sabre_artifact(Path::new(&value), variable, executable)
}
fn validate_sabre_artifact(
requested_path: &Path,
variable: &str,
executable: bool,
) -> Result<OsString, Error> {
let path = fs::canonicalize(requested_path).map_err(|error| {
Error::msg(format!(
"the sabre backend cannot access {variable}={}: {error}",
requested_path.display()
))
})?;
let metadata = fs::metadata(&path).map_err(|error| {
Error::msg(format!(
"the sabre backend cannot inspect {variable}={}: {error}",
path.display()
))
})?;
if !metadata.is_file() {
return Err(Error::msg(format!(
"the sabre backend needs {variable}={} to be a regular file",
path.display()
)));
}
if executable && metadata.permissions().mode() & 0o111 == 0 {
return Err(Error::msg(format!(
"the sabre backend needs {variable}={} to be executable",
path.display()
)));
}
Ok(path.into_os_string())
}
const SABRE_QUIET_ENV: &str = "REVERIE_SABRE_STRACE_QUIET";
fn sabre_command(
runner: &OsString,
sabre: &OsString,
plugin: &OsString,
program: &Path,
args: &[String],
quiet: bool,
log: Option<LevelFilter>,
) -> StdCommand {
let mut command = StdCommand::new(runner);
command
.arg("--sabre")
.arg(sabre)
.arg("--plugin")
.arg(plugin)
.arg("--")
.arg(program)
.args(args);
if quiet {
command.env(SABRE_QUIET_ENV, "1");
}
if let Some(level) = log {
command.env("HERMIT_LOG", level.to_string());
}
command
}
fn sabre_artifacts() -> Result<(OsString, OsString, OsString), Error> {
Ok((
sabre_artifact("HERMIT_SABRE_RUNNER", "reverie-sabre-strace", true)?,
sabre_artifact("HERMIT_SABRE_BINARY", "sabre", true)?,
sabre_artifact(
"HERMIT_SABRE_PLUGIN",
"libreverie_sabre_strace_plugin.so",
false,
)?,
))
}
pub fn run_sabre_strace(program: &Path, args: &[String]) -> Result<ExitStatus, Error> {
let (runner, sabre, plugin) = sabre_artifacts()?;
eprintln!("hermit: [sabre backend] tracing {program:?} with the shared Reverie tool");
let status = sabre_command(&runner, &sabre, &plugin, program, args, false, None)
.status()
.map_err(|error| {
Error::msg(format!(
"failed to launch the SaBRe runner {}: {error}",
Path::new(&runner).display()
))
})?;
Ok(status.into())
}
#[cfg(test)]
mod tests {
#[cfg(feature = "dbt")]
#[tokio::test]
async fn dbt_abnormal_cleanup_ends_an_unregistered_scheduler() {
use std::os::unix::process::ExitStatusExt;
use reverie::GlobalTool;
let config = detcore::Config {
sequentialize_threads: true,
..detcore::Config::default()
};
let mut outcomes = Vec::new();
for (name, raw_status) in [("exit255", 255 << 8), ("SIGKILL", libc::SIGKILL)] {
let global = detcore::GlobalState::init_global_state(&config).await;
let status = std::process::ExitStatus::from_raw(raw_status);
let completed = tokio::time::timeout(
std::time::Duration::from_millis(100),
super::clean_up_dbt_global(&status, global),
)
.await
.is_ok();
assert_eq!(status.into_raw(), raw_status);
outcomes.push((name, completed));
}
assert_eq!(
outcomes,
[("exit255", true), ("SIGKILL", true)],
"DBT cleanup waited for a scheduler whose physical guest had already failed"
);
}
#[cfg(feature = "dbt")]
#[tokio::test]
async fn dbt_successful_cleanup_preserves_normal_scheduler_completion() {
use std::os::unix::process::ExitStatusExt;
use reverie::GlobalTool;
let status = std::process::ExitStatus::from_raw(0);
let config = detcore::Config {
sequentialize_threads: false,
..detcore::Config::default()
};
let global = detcore::GlobalState::init_global_state(&config).await;
assert!(
tokio::time::timeout(
std::time::Duration::from_millis(100),
super::clean_up_dbt_global(&status, global),
)
.await
.is_ok(),
"cleanup without an owned scheduler did not complete"
);
let config = detcore::Config {
sequentialize_threads: true,
..detcore::Config::default()
};
let global = detcore::GlobalState::init_global_state(&config).await;
assert!(
tokio::time::timeout(
std::time::Duration::from_millis(100),
super::clean_up_dbt_global(&status, global),
)
.await
.is_err(),
"successful DBT cleanup silently cancelled a scheduler that had not completed"
);
assert!(status.success());
}
#[cfg(feature = "dbt")]
#[test]
fn dbt_workdir_reaches_the_dynamorio_guest_command() {
let mut command = std::process::Command::new("/bin/true");
super::apply_dbt_workdir(&mut command, Some(std::path::Path::new("/test")));
assert_eq!(
command.get_current_dir(),
Some(std::path::Path::new("/test"))
);
}
#[cfg(feature = "dbt")]
#[test]
fn materialization_keeps_every_decoded_comparable_record() {
let directory = tempfile::tempdir().expect("tempdir");
let path = directory.path().join("evidence.log");
let file = std::fs::File::create(&path).expect("create log");
let records: Vec<Vec<u8>> = vec![
b"1970-01-01T00:00:00.000000Z INFO detcore: DETLOG first\n".to_vec(),
b"1970-01-01T00:00:00.000000Z INFO detcore: DETLOG second\n".to_vec(),
];
let published = [
DBT_INITIALIZATION_RECORD,
records[0].as_slice(),
records[1].as_slice(),
];
super::materialize_dbt_comparison_log(&records, &published, file, &path)
.expect("materialization must accept comparable records");
assert_eq!(std::fs::read(&path).expect("read back"), published.concat());
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_initialization_record_counts_control_the_terminal_outcome() {
use super::super::record_envelope::RecordEnvelopePolicy;
use super::super::verify::ComparedLogCounts;
use super::super::verify::verification_report;
fn dbt_verdict(directory: &Path, left: &[&[u8]], right: &[&[u8]]) -> VerificationOutcome {
let output = reverie::process::Output {
status: ExitStatus::Exited(0),
stdout: Vec::new(),
stderr: Vec::new(),
};
let log = |records: &[&[u8]]| {
let (file, path) = tempfile::NamedTempFile::new_in(directory)
.unwrap()
.into_parts();
materialize_dbt_evidence_log(&decoded_dbt_evidence(records), file, &path).unwrap();
path
};
compare_two_runs(
ComparedRun {
output: &output,
log: log(left),
label: "run 1",
},
ComparedRun {
output: &output,
log: log(right),
label: "run 2",
},
ComparisonOptions {
verbose: false,
strictness: LogCompareStrictness::Canonical,
compare_logs: true,
diagnostic_full_trace: false,
compare_io_buffers: true,
keep_logs: false,
match_overridden: false,
failed_log_retention: None,
record_envelope: RecordEnvelope::all_records_v1(),
virtualize_time: true,
},
)
.unwrap()
}
let stats = dbt_stats(10, 20, 5, 2, 99);
let equal = DbtSummaryComparison::compare(&stats, &stats, 2, 2);
assert!(!equal.requires_log_retention());
let mut matched = typed_outcome(Verdict::Matched, 0);
assert!(equal.apply(&mut matched).is_none());
assert_eq!(matched.verdict, Verdict::Matched);
let unequal = DbtSummaryComparison::compare(&stats, &stats, 2, 3);
assert!(
unequal.requires_log_retention(),
"an initialization mismatch must retain both evidence logs"
);
let mut outcome = typed_outcome(Verdict::Matched, 0);
let failure = unequal.apply(&mut outcome).unwrap();
assert_eq!(outcome.verdict, Verdict::Diverged);
assert!(!outcome.verified());
assert!(
!outcome
.into_exit_status()
.expect("divergence has a terminal status")
.success()
);
assert!(
failure.contains("initialization counts differed"),
"{failure}"
);
assert!(failure.contains("2 != 3"), "{failure}");
let parent_first: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: parent record 1\n";
let parent_second: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: parent record 2\n";
let child: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: child record 1\n";
let arrived = [
DBT_INITIALIZATION_RECORD,
parent_first,
DBT_INITIALIZATION_RECORD,
parent_second,
child,
];
let moved = [
DBT_INITIALIZATION_RECORD,
parent_first,
parent_second,
DBT_INITIALIZATION_RECORD,
child,
];
let directory = tempfile::tempdir().unwrap();
let matched = dbt_verdict(directory.path(), &arrived, &arrived);
assert_eq!(matched.verdict, Verdict::Matched);
assert_eq!(
matched.compared_log_messages,
Some(ComparedLogCounts { left: 5, right: 5 }),
"the initialization records must be compared with the guest records"
);
assert_eq!(
matched.comparison.record_envelope,
RecordEnvelopePolicy::AllRecordsV1
);
let report = serde_json::to_value(verification_report(&matched)).unwrap();
assert_eq!(report["comparison"]["record_envelope"], "all_records_v1");
assert_eq!(report["bitwise_parity"], true);
let diverged = dbt_verdict(directory.path(), &arrived, &moved);
assert_eq!(diverged.verdict, Verdict::Diverged);
assert_eq!(
diverged.compared_log_messages,
Some(ComparedLogCounts { left: 5, right: 5 })
);
}
use super::*;
#[cfg(feature = "dbt")]
fn write_executable(path: &Path, contents: &[u8]) {
fs::write(path, contents).unwrap();
let mut permissions = fs::metadata(path).unwrap().permissions();
permissions.set_mode(0o755);
fs::set_permissions(path, permissions).unwrap();
}
#[cfg(feature = "dbt")]
fn dbt_stats(
branches: u64,
syscalls: u64,
rewritten: u64,
stdin_reads: u64,
memory_hash: u64,
) -> DbtBackendStatsSnapshot {
use reverie_dbt::backend_stats::DbtProcessRecord;
let record = DbtProcessRecord {
branches,
syscalls,
rewritten,
stdin_reads,
memory_hash,
..Default::default()
};
let mut aggregator = DbtBackendStatsAggregator::new();
aggregator.record(&record);
aggregator.snapshot()
}
#[cfg(feature = "dbt")]
fn typed_outcome(verdict: Verdict, guest_exit: i32) -> VerificationOutcome {
use super::super::verify::ComparedLogCounts;
use super::super::verify::ComparisonSpec;
VerificationOutcome {
verdict,
no_result_reason: (verdict == Verdict::NoResult).then(|| {
super::super::verify::NoResultReason::ComparisonRefused {
detail: "fixture comparator refusal".into(),
}
}),
guest_status: ExitStatus::Exited(guest_exit),
comparison: ComparisonSpec::new(
LogCompareStrictness::Canonical,
true,
false,
true,
RecordEnvelope::all_records_v1().policy(),
true,
),
compared_log_messages: Some(ComparedLogCounts { left: 4, right: 4 }),
compared_outputs: {
let output = hermit::canonical_verdict::ComparedOutput {
exit_code: Some(guest_exit),
signal: None,
stdout_sha256: "a".repeat(64),
stdout_bytes: 0,
stderr_sha256: "b".repeat(64),
stderr_bytes: 0,
};
hermit::canonical_verdict::ComparedOutputs {
left: output.clone(),
right: output,
}
},
dbt_counted_branches: None,
runtime: None,
first_divergent_scheduler_turn: None,
first_divergent_virtual_nanoseconds: None,
first_divergent_record: None,
first_divergent_syscall: None,
first_divergent_left_message: None,
first_divergent_right_message: None,
}
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_verification_run_path_binds_terminal_verdict_to_branch_stats() {
let source = include_str!("backends.rs");
let finalizer = source
.split_once("fn finalize_dbt_verification(")
.expect("typed DBT finalizer")
.1
.split_once("/// Own the typed statistics stream")
.expect("end of typed DBT finalizer")
.0;
let attach = finalizer
.find("record_dbt_branch_clock_comparison(")
.expect("attach branch-clock comparison");
let publish = finalizer
.find("write_verification_json(path, &outcome)")
.expect("publish terminal typed verdict");
let return_outcome = finalizer
.find("Ok((outcome, failure))")
.expect("return finalized outcome");
assert!(
attach < publish && publish < return_outcome,
"the finalizer must attach the branch comparison and publish JSON before returning it"
);
let canonical_marker = concat!("#[cfg(feature = \"dbt\")]\n", "pub(super) fn ", "run_dbt(");
let canonical = source
.split_once(canonical_marker)
.expect("canonical DBT run path")
.1
.split_once(r#"#[cfg(not(feature = "dbt"))]"#)
.expect("end of canonical DBT run path")
.0;
assert!(
!canonical.contains("if verify_json.is_none()"),
"DBT verification must not bypass protected INFO evidence when no JSON output path \
was requested"
);
let first_stats = canonical
.find("let first_stats = match stats1.finish()")
.expect("run-1 typed stats");
let second_stats = canonical
.find("let second_stats = match stats2.finish()")
.expect("run-2 typed stats");
let comparison = canonical
.find("let branch_clock_comparison = DbtCountedBranchComparison")
.expect("typed branch-clock comparison");
let summary_comparison = canonical
.find("let summary_comparison = DbtSummaryComparison::compare(")
.expect("typed summary and initialization comparison");
let force_branch_logs = canonical
.find("|| branch_clock_diverged")
.expect("branch divergence forces log retention");
let force_summary_logs = canonical
.find("|| summary_comparison.requires_log_retention()")
.expect("summary divergence forces log retention");
let apply_summary = canonical
.find("let summary_failure = summary_comparison.apply(&mut outcome)")
.expect("summary divergence updates the terminal outcome");
let finalize = canonical
.find("finalize_dbt_verification(outcome, branch_clock_comparison, verify_json)")
.expect("terminal typed finalization");
let announce = canonical
.find("if let Some(message) = branch_clock_failure")
.expect("branch-specific terminal announcement");
let exit = canonical
.find("return outcome.into_exit_status()")
.expect("terminal nonzero conversion");
assert!(
first_stats < second_stats
&& second_stats < comparison
&& comparison < summary_comparison
&& summary_comparison < force_branch_logs
&& force_branch_logs < force_summary_logs
&& force_summary_logs < apply_summary
&& apply_summary < finalize
&& finalize < announce
&& announce < exit,
"canonical verification must collect both stats and authenticated initialization \
counts, compute both comparisons, force logs, apply summary divergence, publish \
through the finalizer, and only then announce or convert the terminal status"
);
for (stats, next) in [
(
"let first_stats = match stats1.finish()",
"let first = dbt_verification_output",
),
(
"let second_stats = match stats2.finish()",
"let second = dbt_verification_output",
),
] {
let failure_arm = canonical
.split_once(stats)
.expect("typed stats collection")
.1
.split_once(next)
.expect("end of typed stats failure arm")
.0;
assert!(failure_arm.contains("Err(error) =>"));
assert!(
failure_arm.contains("return Err(error);"),
"unreadable typed stats must return while the pre-stamped no_result is still current"
);
}
}
#[test]
#[cfg(feature = "dbt")]
fn equal_branch_clocks_do_not_authorize_a_no_result() {
let outcome = typed_outcome(Verdict::NoResult, 23);
let comparison = DbtCountedBranchComparison {
left: 563_145,
right: 563_145,
};
let verdict_file = tempfile::NamedTempFile::new().unwrap();
let (outcome, failure) =
finalize_dbt_verification(outcome, comparison, Some(verdict_file.path())).unwrap();
let json: serde_json::Value =
serde_json::from_slice(&fs::read(verdict_file.path()).unwrap()).unwrap();
assert!(failure.is_none());
assert_eq!(outcome.verdict, Verdict::NoResult);
assert!(outcome.dbt_counted_branches.is_none());
assert_eq!(json["verdict"], "no_result");
assert_eq!(json["verified"], false);
assert!(json.get("dbt_counted_branches").is_none());
let error = outcome.into_exit_status().unwrap_err().to_string();
assert!(error.contains("did not reach a verdict"), "{error}");
}
#[test]
#[cfg(feature = "dbt")]
fn branch_clock_divergence_is_retained_in_the_serialized_typed_verdict() {
use super::super::verify::ComparedLogCounts;
for initial_verdict in [Verdict::Matched, Verdict::NoResult] {
let outcome = typed_outcome(initial_verdict, 0);
let comparison = DbtCountedBranchComparison {
left: 563_145,
right: 563_103,
};
let verdict_file = tempfile::NamedTempFile::new().unwrap();
let (outcome, message) =
finalize_dbt_verification(outcome, comparison, Some(verdict_file.path())).unwrap();
let message = message.unwrap();
let json: serde_json::Value =
serde_json::from_slice(&fs::read(verdict_file.path()).unwrap()).unwrap();
assert!(message.contains("563145 != 563103"), "{message}");
assert_eq!(outcome.verdict, Verdict::Diverged);
assert_eq!(outcome.dbt_counted_branches, Some(comparison));
assert_eq!(json["verdict"], "diverged");
assert_eq!(json["verified"], false);
assert_eq!(json["bitwise_parity"], false);
assert_eq!(json["dbt_counted_branches"]["left"], 563_145);
assert_eq!(json["dbt_counted_branches"]["right"], 563_103);
assert_eq!(
outcome.compared_log_messages,
Some(ComparedLogCounts { left: 4, right: 4 })
);
assert_eq!(
outcome.into_exit_status().unwrap(),
ExitStatus::Exited(hermit::HERMIT_VERIFICATION_DIVERGENCE_EXIT)
);
}
}
#[test]
#[cfg(feature = "dbt")]
fn equal_branch_clocks_preserve_match_and_nonzero_guest_status() {
let outcome = typed_outcome(Verdict::Matched, 23);
let comparison = DbtCountedBranchComparison {
left: 563_145,
right: 563_145,
};
let (outcome, failure) = finalize_dbt_verification(outcome, comparison, None).unwrap();
assert!(failure.is_none());
let report = super::super::verify::verification_report(&outcome);
assert_eq!(outcome.verdict, Verdict::Matched);
assert_eq!(report.dbt_counted_branches, Some(comparison));
assert!(report.verified);
assert!(report.bitwise_parity);
assert_eq!(outcome.into_exit_status().unwrap(), ExitStatus::Exited(23));
}
#[test]
#[cfg(feature = "dbt")]
fn typed_stats_capture_aggregates_the_whole_process_tree() {
use reverie_dbt::backend_stats::DbtProcessRecord;
use reverie_dbt::backend_stats::encode_process_record;
let capture = DbtStatsCapture::new().unwrap();
let root = DbtProcessRecord {
branches: 400,
syscalls: 7,
..Default::default()
};
let child = DbtProcessRecord {
branches: 23,
syscalls: 2,
..Default::default()
};
let bytes = [encode_process_record(&root), encode_process_record(&child)].concat();
fs::write(&capture.path, bytes).unwrap();
let snapshot = capture.finish().unwrap();
assert_eq!(snapshot.process_images(), 2);
assert_eq!(snapshot.counted_branches(), 423);
assert_eq!(snapshot.intercepted_syscalls(), 9);
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_engagement_record_follows_the_typed_branch_count() {
fn record(branches: u64) -> serde_json::Value {
let capture = DbtStatsCapture::new().unwrap();
let row = reverie_dbt::backend_stats::DbtProcessRecord {
branches,
syscalls: 1,
rewritten: 1,
..Default::default()
};
fs::write(
&capture.path,
reverie_dbt::backend_stats::encode_process_record(&row),
)
.unwrap();
let output = tempfile::NamedTempFile::new().unwrap();
finish_single_run_dbt_stats(capture, false, Some(output.path())).unwrap();
serde_json::from_slice(&fs::read(output.path()).unwrap()).unwrap()
}
let first = record(18);
let second = record(19);
assert_eq!(first["engagement"]["backend"], "dbt");
assert_eq!(first["engagement"]["counted_branches"], 18);
assert_eq!(second["engagement"]["counted_branches"], 19);
assert_ne!(
first, second,
"mutating the producer count must move the record"
);
}
#[test]
#[cfg(feature = "dbt")]
fn typed_stats_capture_refuses_missing_empty_or_truncated_evidence() {
let missing = DbtStatsCapture::new().unwrap();
let error = missing.finish().unwrap_err();
assert!(
error.to_string().contains("statistics were missing"),
"{error}"
);
let empty = DbtStatsCapture::new().unwrap();
fs::write(&empty.path, b"").unwrap();
let error = empty.finish().unwrap_err();
assert!(
error.to_string().contains("statistics were empty"),
"{error}"
);
let truncated = DbtStatsCapture::new().unwrap();
fs::write(&truncated.path, b"truncated").unwrap();
let error = truncated.finish().unwrap_err();
assert!(
error.to_string().contains("statistics were unreadable"),
"{error}"
);
assert!(error.to_string().contains("truncated"), "{error}");
let inconsistent = DbtStatsCapture::new().unwrap();
let record = reverie_dbt::backend_stats::DbtProcessRecord {
branches: 18,
syscalls: 1,
rewritten: 2,
..Default::default()
};
fs::write(
&inconsistent.path,
reverie_dbt::backend_stats::encode_process_record(&record),
)
.unwrap();
let error = inconsistent.finish().unwrap_err();
assert!(
error
.to_string()
.contains("typed native callback counters are inconsistent"),
"{error}"
);
}
#[test]
#[cfg(feature = "dbt")]
fn typed_stats_failure_leaves_the_pre_stamped_no_result() {
let verdict_file = tempfile::NamedTempFile::new().unwrap();
write_pending_verification_json(verdict_file.path()).unwrap();
let empty = DbtStatsCapture::new().unwrap();
fs::write(&empty.path, b"").unwrap();
assert!(empty.finish().is_err());
let json: serde_json::Value =
serde_json::from_slice(&fs::read(verdict_file.path()).unwrap()).unwrap();
assert_eq!(json["verdict"], "no_result");
assert_eq!(json["verified"], false);
assert_eq!(json["bitwise_parity"], false);
assert!(json.get("dbt_counted_branches").is_none());
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_canonical_evidence_materializes_records_unchanged() {
let log = tempfile::NamedTempFile::new().unwrap();
let (file, path) = log.into_parts();
let records = vec![
b"1970-01-01T00:00:00.000000Z INFO detcore: DETLOG first\n".to_vec(),
b"1970-01-01T00:00:00.000000Z INFO detcore::scheduler: second\n".to_vec(),
];
let published = [
DBT_INITIALIZATION_RECORD,
records[0].as_slice(),
records[1].as_slice(),
];
let compared = materialize_dbt_comparison_log(&records, &published, file, &path).unwrap();
assert_eq!(compared, 3);
assert_eq!(fs::read(&path).unwrap(), published.concat());
let parent_first: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: parent record 1\n";
let parent_second: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: parent record 2\n";
let child: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: child record 1\n";
let arrived = [
DBT_INITIALIZATION_RECORD,
parent_first,
DBT_INITIALIZATION_RECORD,
parent_second,
child,
];
let evidence = decoded_dbt_evidence(&arrived);
assert_eq!(evidence.all_records(), arrived);
let (file, path) = tempfile::NamedTempFile::new().unwrap().into_parts();
let compared = materialize_dbt_evidence_log(&evidence, file, &path).unwrap();
assert_eq!(compared, 5, "both initialization records are INFO records");
assert_eq!(fs::read(&path).unwrap(), arrived.concat());
let front_loaded = decoded_dbt_evidence(&[
DBT_INITIALIZATION_RECORD,
DBT_INITIALIZATION_RECORD,
parent_first,
parent_second,
child,
]);
assert_eq!(front_loaded.records(), evidence.records());
assert_eq!(
front_loaded.initialization_records(),
evidence.initialization_records()
);
let (file, path) = tempfile::NamedTempFile::new().unwrap().into_parts();
materialize_dbt_evidence_log(&front_loaded, file, &path).unwrap();
assert_ne!(fs::read(&path).unwrap(), arrived.concat());
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_canonical_evidence_fails_closed_on_empty_or_unframed_records() {
let empty = tempfile::NamedTempFile::new().unwrap();
let (file, path) = empty.into_parts();
assert!(materialize_dbt_comparison_log(&[], &[], file, &path).is_err());
let unframed_record = b"1970-01-01T00:00:00.000000Z INFO detcore: missing newline";
let unframed = tempfile::NamedTempFile::new().unwrap();
let (file, path) = unframed.into_parts();
assert!(
materialize_dbt_comparison_log(
&[unframed_record.to_vec()],
&[DBT_INITIALIZATION_RECORD, unframed_record],
file,
&path,
)
.is_err()
);
let framed_record = b"1970-01-01T00:00:00.000000Z INFO detcore: framed\n";
let unframed_published = tempfile::NamedTempFile::new().unwrap();
let (file, path) = unframed_published.into_parts();
assert!(
materialize_dbt_comparison_log(
&[framed_record.to_vec()],
&[framed_record, unframed_record],
file,
&path,
)
.is_err()
);
let startup_only = decoded_dbt_evidence(&[DBT_INITIALIZATION_RECORD]);
assert!(startup_only.records().is_empty());
assert_eq!(startup_only.all_records(), [DBT_INITIALIZATION_RECORD]);
let (file, path) = tempfile::NamedTempFile::new().unwrap().into_parts();
let error = materialize_dbt_evidence_log(&startup_only, file, &path).unwrap_err();
assert_eq!(
error.to_string(),
"DBT canonical evidence contained no records"
);
let no_guest_info = decoded_dbt_evidence(&[
DBT_INITIALIZATION_RECORD,
b"1970-01-01T00:00:00.000000Z DEBUG detcore: guest diagnostic\n",
DBT_INITIALIZATION_RECORD,
]);
assert_eq!(no_guest_info.records().len(), 1);
assert_eq!(no_guest_info.initialization_records(), 2);
let (file, path) = tempfile::NamedTempFile::new().unwrap().into_parts();
let error = materialize_dbt_evidence_log(&no_guest_info, file, &path).unwrap_err();
assert_eq!(
error.to_string(),
"DBT canonical evidence contained no INFO records"
);
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_canonical_evidence_fails_closed_on_empty_or_malformed_artifact() {
let mut empty = tempfile::tempfile().unwrap();
assert!(decode_dbt_evidence(&mut empty).is_err());
let mut malformed = tempfile::tempfile().unwrap();
malformed.write_all(b"not framed evidence").unwrap();
assert!(decode_dbt_evidence(&mut malformed).is_err());
}
#[cfg(feature = "dbt")]
const DBT_INITIALIZATION_RECORD: &[u8] =
b"1970-01-01T00:00:00.000000Z INFO reverie_dbt::evidence: protected evidence initialized\n";
#[cfg(feature = "dbt")]
fn encode_dbt_evidence(records: &[&[u8]]) -> Vec<u8> {
let mut body = Vec::new();
for record in records {
body.extend_from_slice(&u32::try_from(record.len()).unwrap().to_le_bytes());
body.extend_from_slice(record);
}
let payload_bytes: usize = records.iter().map(|record| record.len()).sum();
let digest = body.iter().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
(hash ^ u64::from(*byte)).wrapping_mul(0x0000_0100_0000_01b3)
});
let mut encoded = Vec::with_capacity(40 + body.len());
encoded.extend_from_slice(b"RVDBTEF1");
encoded.extend_from_slice(&1_u16.to_le_bytes());
encoded.extend_from_slice(&40_u16.to_le_bytes());
encoded.extend_from_slice(&0_u32.to_le_bytes());
encoded.extend_from_slice(&u64::try_from(records.len()).unwrap().to_le_bytes());
encoded.extend_from_slice(&u64::try_from(payload_bytes).unwrap().to_le_bytes());
encoded.extend_from_slice(&digest.to_le_bytes());
encoded.extend_from_slice(&body);
encoded
}
#[cfg(feature = "dbt")]
fn decoded_dbt_evidence(records: &[&[u8]]) -> DbtEvidence {
let mut file = tempfile::tempfile().unwrap();
file.write_all(&encode_dbt_evidence(records)).unwrap();
decode_dbt_evidence(&mut file).expect("fixture evidence must decode")
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_stats_block_labels_counters_honestly() {
let rendered = format_dbt_stats(&dbt_stats(563_145, 169, 168, 0, 0x4b5e0e70f3050157));
assert!(rendered.contains("counted branches (deterministic branch clock): 563145"));
assert!(rendered.contains("syscalls intercepted: 169"));
assert!(rendered.contains("syscall instructions rewritten: 168"));
assert!(rendered.contains("stdin (fd 0) reads: 0"));
assert!(
rendered.contains("observed guest-memory hash: 4b5e0e70f3050157")
);
assert!(!rendered.to_lowercase().contains("basic block"));
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_stats_block_renders_the_typed_wire_snapshot() {
let rendered = format_dbt_stats(&dbt_stats(42, 7, 6, 0, 0xcbf29ce484222325));
assert!(rendered.contains("counted branches (deterministic branch clock): 42"));
assert!(rendered.contains("syscalls intercepted: 7"));
assert!(
rendered.contains("observed guest-memory hash: cbf29ce484222325")
);
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_summary_compares_every_non_branch_observation() {
let expected = dbt_stats(100, 169, 168, 0, 0x4b5e0e70f3050157);
assert!(!same_dbt_observable_behavior(
&expected,
&dbt_stats(100, 170, 168, 0, 0x4b5e0e70f3050157)
));
assert!(!same_dbt_observable_behavior(
&expected,
&dbt_stats(100, 169, 167, 0, 0x4b5e0e70f3050157)
));
assert!(!same_dbt_observable_behavior(
&expected,
&dbt_stats(100, 169, 168, 1, 0x4b5e0e70f3050157)
));
assert!(!same_dbt_observable_behavior(
&expected,
&dbt_stats(100, 169, 168, 0, 0)
));
let different_branch_clock = dbt_stats(101, 169, 168, 0, 0x4b5e0e70f3050157);
assert!(
same_dbt_observable_behavior(&expected, &different_branch_clock),
"the counted-branch clock has its own typed comparison and must not be folded into \
the opaque summary mismatch"
);
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_physical_command_preserves_explicit_environment_removals() {
let inherited = env::vars_os()
.map(|(key, _)| key)
.find(|key| key != "DBT_PHYSICAL_KEPT")
.expect("native fixture requires an inherited environment entry");
let environment = BTreeMap::from([(
OsString::from("DBT_PHYSICAL_KEPT"),
OsString::from("literal guest value"),
)]);
let mut command = StdCommand::new("/usr/bin/env");
apply_exact_environment(&mut command, &environment);
command.env_remove("DBT_PHYSICAL_EXPLICIT_REMOVAL");
let original = command
.get_envs()
.map(|(key, value)| (key.to_owned(), value.map(OsStr::to_owned)))
.collect::<BTreeMap<_, _>>();
assert_eq!(original.get(&inherited), Some(&None));
assert_eq!(
original.get(OsStr::new("DBT_PHYSICAL_EXPLICIT_REMOVAL")),
Some(&None)
);
let prepared = prepare_dbt_physical_command(&command).unwrap();
let transported = prepared
.get_envs()
.map(|(key, value)| (key.to_owned(), value.map(OsStr::to_owned)))
.collect::<BTreeMap<_, _>>();
assert_eq!(
transported, original,
"physical command preparation must preserve each removal for DbtRunner"
);
assert_eq!(prepared.get_program(), command.get_program());
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_resolves_simple_env_shebang_target_to_absolute_path() {
let root = tempfile::tempdir().unwrap();
let bin = root.path().join("bin");
fs::create_dir(&bin).unwrap();
let python = bin.join("python3");
write_executable(&python, b"\x7fELFplaceholder");
let script = root.path().join("guest.py");
write_executable(&script, b"#!/usr/bin/env python3\n");
let prepared = prepare_dbt_guest_command(
&script,
&[OsString::from("argument")],
Some(bin.as_os_str()),
root.path(),
);
assert_eq!(prepared.program, Path::new("/usr/bin/env"));
assert_eq!(
prepared.args,
[
python.into_os_string(),
script.into_os_string(),
OsString::from("argument"),
]
);
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_leaves_complex_env_shebang_for_launcher() {
let root = tempfile::tempdir().unwrap();
let script = root.path().join("guest.py");
write_executable(&script, b"#!/usr/bin/env -S python3 -u\n");
let prepared =
prepare_dbt_guest_command(&script, &[], Some(OsStr::new("/usr/bin")), root.path());
assert_eq!(prepared.program, script);
assert!(prepared.args.is_empty());
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_relative_shebang_uses_guest_cwd_and_preserves_script_arguments() {
let root = tempfile::Builder::new()
.prefix("dbt-workdir-")
.tempdir_in(".")
.unwrap();
let relative = PathBuf::from(root.path().file_name().unwrap()).join("guest.py");
let guest_cwd = fs::canonicalize(root.path()).unwrap().join("guest-cwd");
let guest_script = guest_cwd.join(&relative);
fs::create_dir_all(guest_script.parent().unwrap()).unwrap();
write_executable(&relative, b"#!/usr/bin/env caller-only\n");
write_executable(&guest_script, b"#!/usr/bin/env guest-only\n");
let relative_bin = PathBuf::from(root.path().file_name().unwrap()).join("bin");
let guest_bin = guest_cwd.join(&relative_bin);
fs::create_dir_all(&relative_bin).unwrap();
fs::create_dir_all(&guest_bin).unwrap();
write_executable(&relative_bin.join("caller-only"), b"\x7fELFcaller");
write_executable(&guest_bin.join("guest-only"), b"\x7fELFguest");
let mut command = StdCommand::new(&relative);
command
.current_dir(&guest_cwd)
.env_clear()
.env("PATH", &relative_bin)
.env("KEPT", "literal value")
.env_remove("EXPLICIT_REMOVAL")
.args(["literal argument", ""]);
let prepared = prepare_dbt_physical_command(&command).unwrap();
assert_eq!(prepared.get_program(), OsStr::new("/usr/bin/env"));
assert_eq!(
prepared.get_args().map(OsStr::to_owned).collect::<Vec<_>>(),
[
guest_bin.join("guest-only").into_os_string(),
relative.into_os_string(),
OsString::from("literal argument"),
OsString::new()
]
);
assert_eq!(prepared.get_current_dir(), Some(guest_cwd.as_path()));
assert_eq!(
prepared.get_envs().collect::<Vec<_>>(),
command.get_envs().collect::<Vec<_>>()
);
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_path_selected_script_keeps_the_selected_script_path() {
let root = tempfile::tempdir().unwrap();
let bin = root.path().join("bin");
fs::create_dir(&bin).unwrap();
write_executable(
&bin.join("guest.py"),
b"#!/usr/bin/env chosen-interpreter\n",
);
let interpreter = bin.join("chosen-interpreter");
write_executable(&interpreter, b"\x7fELFguest");
for (cwd, path, script, target) in [
(
root.path(),
OsStr::new("bin"),
PathBuf::from("bin/guest.py"),
interpreter.clone(),
),
(
root.path(),
bin.as_os_str(),
bin.join("guest.py"),
interpreter.clone(),
),
(
root.path(),
OsStr::new(":bin"),
PathBuf::from("bin/guest.py"),
interpreter.clone(),
),
(
bin.as_path(),
OsStr::new(""),
PathBuf::from("guest.py"),
interpreter.clone(),
),
(
bin.as_path(),
OsStr::new("."),
PathBuf::from("./guest.py"),
bin.join("./chosen-interpreter"),
),
] {
let mut command = StdCommand::new("guest.py");
command
.current_dir(cwd)
.env_clear()
.env("PATH", path)
.args(["", "two words"]);
let prepared = prepare_dbt_physical_command(&command).unwrap();
assert_eq!(prepared.get_program(), OsStr::new("/usr/bin/env"));
assert_eq!(
prepared.get_args().map(OsStr::to_owned).collect::<Vec<_>>(),
[
target.into_os_string(),
script.into_os_string(),
OsString::new(),
OsString::from("two words"),
],
"PATH {path:?} in {cwd:?} must identify the selected script"
);
assert_eq!(prepared.get_current_dir(), command.get_current_dir());
assert_eq!(
prepared.get_envs().collect::<Vec<_>>(),
command.get_envs().collect::<Vec<_>>()
);
}
}
#[test]
#[cfg(feature = "dbt")]
fn dbt_empty_path_component_and_hidden_script_use_physical_cwd() {
let root = tempfile::Builder::new()
.prefix("dbt-physical-cwd-")
.tempdir_in(".")
.unwrap();
let relative = PathBuf::from(root.path().file_name().unwrap()).join("guest.py");
let guest_cwd = fs::canonicalize(root.path()).unwrap().join("empty-workdir");
fs::create_dir(&guest_cwd).unwrap();
write_executable(&relative, b"#!/usr/bin/env chosen-interpreter\n");
let caller_bin = root.path().join("bin");
fs::create_dir(&caller_bin).unwrap();
write_executable(&caller_bin.join("chosen-interpreter"), b"\x7fELFcaller");
let mut command = StdCommand::new(&relative);
command
.current_dir(&guest_cwd)
.env_clear()
.env("PATH", &caller_bin)
.arg("argument");
let absent = prepare_dbt_physical_command(&command).unwrap();
assert_eq!(absent.get_program(), relative.as_os_str());
assert_eq!(
absent.get_args().collect::<Vec<_>>(),
[OsStr::new("argument")]
);
let script = guest_cwd.join("script");
write_executable(&script, b"#!/usr/bin/env chosen-interpreter\n");
let interpreter = guest_cwd.join("chosen-interpreter");
write_executable(&interpreter, b"\x7fELFguest");
let mut command = StdCommand::new(&script);
command.current_dir(&guest_cwd).env_clear().env("PATH", "");
let prepared = prepare_dbt_physical_command(&command).unwrap();
assert_eq!(prepared.get_program(), OsStr::new("/usr/bin/env"));
assert_eq!(
prepared.get_args().collect::<Vec<_>>(),
[interpreter.as_os_str(), script.as_os_str()]
);
}
#[test]
fn sabre_artifact_returns_the_validated_absolute_path() {
let file = tempfile::NamedTempFile::new_in(".").unwrap();
let relative_path = file.path().file_name().unwrap();
let resolved = validate_sabre_artifact(Path::new(relative_path), "test-artifact", false)
.map(std::path::PathBuf::from)
.unwrap();
assert!(resolved.is_absolute());
assert_eq!(resolved, fs::canonicalize(file.path()).unwrap());
}
#[test]
fn a_signalled_guest_is_not_reported_as_a_normal_exit() {
use std::os::unix::process::ExitStatusExt as _;
let raw = 11i32;
let native = std::process::ExitStatus::from_raw(raw);
assert!(native.code().is_none(), "SIGSEGV death has no exit code");
let converted = process_status(native);
assert!(
!matches!(converted, ExitStatus::Exited(_)),
"a signalled guest must not read as a normal exit, got {converted:?}"
);
let output = Output {
status: native,
stdout: Vec::new(),
stderr: Vec::new(),
};
let from_output = output_status(&output);
assert!(
!matches!(from_output, ExitStatus::Exited(_)),
"output_status must not report a signalled guest as a normal exit, got {from_output:?}"
);
assert_eq!(
converted, from_output,
"output_status and process_status must agree; they diverged once and \
only one of them was fixed"
);
}
#[cfg(feature = "dbt")]
#[test]
fn a_spawn_failure_still_names_the_launch() {
let drrun = Path::new("/nonexistent/dynamorio/bin64/drrun");
for kind in [
std::io::ErrorKind::NotFound,
std::io::ErrorKind::PermissionDenied,
] {
let rendered = dbt_run_error(drrun, std::io::Error::new(kind, "boom")).to_string();
assert!(
rendered.contains("failed to launch drrun"),
"a {kind:?} spawn failure must be reported as a launch failure: {rendered}"
);
}
}
#[cfg(feature = "dbt")]
#[test]
fn a_post_launch_failure_does_not_blame_the_binary() {
let drrun = Path::new("/real/dynamorio/bin64/drrun");
let inner = "DBT guest exited with status Some(1) while protected evidence failed: \
DBT evidence collector thread panicked";
let rendered = dbt_run_error(drrun, std::io::Error::other(inner)).to_string();
assert!(
!rendered.contains("failed to launch drrun"),
"a post-launch failure must not be reported as a launch failure: {rendered}"
);
assert!(
rendered.contains("drrun started"),
"the message must say the launch succeeded: {rendered}"
);
assert!(
rendered.contains("NOT a launch failure"),
"the message must rule the binary out explicitly: {rendered}"
);
assert!(
rendered.contains("protected evidence failed"),
"the underlying error must be preserved in full: {rendered}"
);
}
#[test]
fn dbt_status_preserves_normal_exit_codes() {
for code in [0, 1, 42, 255] {
let raw = std::process::ExitStatus::from_raw(code << 8);
assert_eq!(
process_status(raw),
ExitStatus::Exited(code),
"a normal exit with code {code} must round-trip"
);
}
}
#[test]
fn dbt_status_preserves_death_by_signal() {
for (signum, signal) in [
(libc::SIGABRT, reverie::Signal::SIGABRT),
(libc::SIGSEGV, reverie::Signal::SIGSEGV),
(libc::SIGFPE, reverie::Signal::SIGFPE),
(libc::SIGILL, reverie::Signal::SIGILL),
(libc::SIGTERM, reverie::Signal::SIGTERM),
(libc::SIGKILL, reverie::Signal::SIGKILL),
(libc::SIGTRAP, reverie::Signal::SIGTRAP),
] {
let raw = std::process::ExitStatus::from_raw(signum);
assert_eq!(
process_status(raw),
ExitStatus::Signaled(signal, false),
"death by signal {signum} must be reported as Signaled, not Exited"
);
let raw = std::process::ExitStatus::from_raw(signum | 0x80);
assert_eq!(
process_status(raw),
ExitStatus::Signaled(signal, true),
"the core-dump flag for signal {signum} must survive the conversion"
);
}
}
#[test]
fn dbt_log_file_is_refused_on_the_single_run_adapter_and_permitted_under_verify() {
assert!(
dbt_log_file_is_refused(true, false),
"a requested --log-file must be refused on the ordinary single-run adapter"
);
assert!(
!dbt_log_file_is_refused(true, true),
"--verify must keep accepting --log-file; the verification adapter owns the sink"
);
assert!(!dbt_log_file_is_refused(false, false));
assert!(!dbt_log_file_is_refused(false, true));
}
#[test]
fn dbt_log_file_refusal_names_the_adapter_it_is_about() {
assert_eq!(
DBT_LOG_FILE_REFUSAL,
"DBT --log-file is unavailable on the ordinary single-run adapter"
);
}
}