use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use anyhow::Context;
use anyhow::bail;
use clap::Parser;
use colored::Colorize;
use detcore::preemptions::PreemptionReader;
use detcore::preemptions::PreemptionRecord;
use detcore::preemptions::strip_times_from_events_file;
use detcore::types::SchedEvent;
use hermit::process::Bind;
use reverie::process::Output;
use tracing::metadata::LevelFilter;
use crate::analyze::consts::*;
use crate::analyze::types::AnalyzeOpts;
use crate::global_opts::GlobalOpts;
use crate::run::RunOpts;
pub struct RunData {
runname: String,
analyze_opts: AnalyzeOpts,
pub runopts: RunOpts,
preempts_path_in: Option<PathBuf>,
sched_path_in: Option<PathBuf>,
sched_path_out: Option<PathBuf>,
log_level: LevelFilter,
log_path: Option<PathBuf>,
in_mem_preempts_in: Option<PreemptionRecord>,
in_mem_sched_out: Option<PreemptionRecord>,
is_a_match: Option<bool>,
}
impl RunData {
pub fn root_path(&self) -> PathBuf {
let tmp_dir = self.analyze_opts.tmp_dir.as_ref().unwrap();
tmp_dir.join(&self.runname)
}
fn out_path(&self) -> PathBuf {
let tmp_dir = self.analyze_opts.tmp_dir.as_ref().unwrap();
tmp_dir.join(self.runname.clone() + "_out")
}
#[allow(dead_code)]
pub fn preempts_path_in(&mut self) -> &Path {
if self.preempts_path_in.is_none() {
let path = if let Some(p) = &self.runopts.det_opts.det_config.replay_preemptions_from {
p.to_owned()
} else {
self.root_path().with_extension(PREEMPTS_EXT)
};
self.preempts_path_in = Some(path);
}
self.preempts_path_in.as_ref().unwrap()
}
pub fn preempts_path_out(&mut self) -> &Path {
self.sched_path_out()
}
pub fn preempts_out(&mut self) -> &PreemptionRecord {
if self.in_mem_sched_out.is_none() {
let path = self.sched_path_out();
let pr = PreemptionReader::new(path);
self.in_mem_sched_out = Some(pr.load_all());
}
self.in_mem_sched_out.as_ref().unwrap()
}
pub fn sched_path_out(&mut self) -> &Path {
if self.sched_path_out.is_none() {
let path = if let Some(p) = &self.runopts.det_opts.det_config.record_preemptions_to {
p.to_owned()
} else {
self.out_path().with_extension(PREEMPTS_EXT)
};
self.sched_path_out = Some(path);
}
self.sched_path_out.as_ref().unwrap()
}
pub fn sched_path_in(&mut self) -> &Path {
if self.sched_path_in.is_none() {
let path = if let Some(p) = &self.runopts.det_opts.det_config.replay_schedule_from {
p.to_owned()
} else {
self.root_path().with_extension(PREEMPTS_EXT)
};
self.sched_path_in = Some(path);
}
self.sched_path_in.as_ref().unwrap()
}
pub fn sched_out_file_name(&mut self) -> String {
self.sched_path_out()
.file_name()
.unwrap()
.to_string_lossy()
.to_string()
}
pub fn sched_out(&mut self) -> &Vec<SchedEvent> {
let pr = self.preempts_out();
pr.schedevents()
}
pub fn log_path(&mut self) -> Option<&PathBuf> {
if self.has_launched() {
if self.log_path.is_none() {
self.log_path = Some(self.root_path().with_extension(LOG_EXT))
}
self.log_path.as_ref()
} else {
None
}
}
pub fn is_a_match(&self) -> bool {
self.is_a_match.expect("only called after launch method")
}
pub fn has_launched(&self) -> bool {
self.is_a_match.is_some()
}
pub fn normalize_preempts_out(&mut self) {
assert!(self.has_launched());
let preempts_path = self.preempts_path_out();
let normalized_path = preempts_path.with_extension("normalized");
let normalized = self.preempts_out().normalize();
normalized
.write_to_disk(&normalized_path)
.expect("write of preempts file to succeed");
self.in_mem_sched_out = Some(normalized);
self.sched_path_out = Some(normalized_path);
}
fn trial_global_opts(&self) -> GlobalOpts {
let log_path = self.root_path().with_extension(LOG_EXT);
GlobalOpts {
log: Some(self.log_level),
log_file: if self.analyze_opts.verbose || self.analyze_opts.selfcheck {
Some(log_path)
} else {
None
},
backend: self.analyze_opts.backend,
log_file_handle: None,
run_evidence_log_handle: None,
run_evidence_write_error: None,
}
}
pub fn launch(&mut self) -> anyhow::Result<()> {
let root = self.root_path();
let gopts = self.trial_global_opts();
let mut gopts = gopts;
gopts.open_log_file()?;
let final_record_path = self
.runopts
.det_opts
.det_config
.record_preemptions_to
.take();
if final_record_path.is_some() {
let temp_path = root.with_extension(PREEMPTS_EXT_PRESTRIPPED);
self.runopts.det_opts.det_config.record_preemptions_to = Some(temp_path);
}
self.runopts.validate_args()?;
let repro_file = self.root_path().with_extension("repro");
std::fs::write(repro_file, self.to_repro() + "\n")?;
let (_, output) = self.runopts.run(&gopts, true)?;
let output: Output = output.context("expected captured output")?;
File::create(root.with_extension("stdout"))
.unwrap()
.write_all(&output.stdout)
.unwrap();
File::create(root.with_extension("stderr"))
.unwrap()
.write_all(&output.stderr)
.unwrap();
let temp_path = self
.runopts
.det_opts
.det_config
.record_preemptions_to
.take();
if let Some(temp_path) = temp_path {
Self::post_process_events_file(&temp_path, final_record_path.as_ref().unwrap())?;
self.runopts.det_opts.det_config.record_preemptions_to = final_record_path;
}
self.is_a_match = Some(self.analyze_opts.output_matches(&output));
if self.analyze_opts.verbose {
println!(
"Guest stdout:\n{}",
String::from_utf8(output.stdout).unwrap()
);
println!(
"Guest stderr:\n{}",
String::from_utf8(output.stderr).unwrap()
);
}
Ok(())
}
pub fn post_process_events_file(sched_path: &Path, dest: &Path) -> anyhow::Result<()> {
let _ = strip_times_from_events_file(sched_path, Some(dest.to_owned()))?;
Ok(())
}
fn log_level(aopts: &AnalyzeOpts) -> LevelFilter {
let mut lvl = if let Some(l) = &aopts.guest_log {
*l
} else {
LevelFilter::WARN
};
if aopts.verbose || aopts.selfcheck && lvl < LevelFilter::DEBUG {
if aopts.guest_log.is_some() {
eprintln!(
"WARNING: manually set log lvl to {} but need DEBUG for selfcheck/verbose functionality",
lvl
);
}
lvl = LevelFilter::DEBUG;
}
lvl
}
pub fn new(aopts: &AnalyzeOpts, runname: String, runopts: RunOpts) -> Self {
let log_level: LevelFilter = Self::log_level(aopts);
let mut rd = RunData {
runname,
analyze_opts: aopts.clone(),
runopts,
preempts_path_in: None,
sched_path_in: None,
sched_path_out: None,
log_path: None,
in_mem_preempts_in: None,
in_mem_sched_out: None,
is_a_match: None,
log_level,
};
let conf_file = rd.root_path().with_extension("config");
rd.runopts.save_config = Some(conf_file);
let summary_file = rd.root_path().with_extension("summary");
rd.runopts.summary_json = Some(summary_file);
rd
}
pub fn new_baseline(aopts: &AnalyzeOpts, runname: String) -> anyhow::Result<Self> {
let ro = Self::get_base_runopts(aopts)?;
Ok(Self::new(aopts, runname, ro))
}
pub fn new_run1_target(aopts: &AnalyzeOpts, runname: String) -> anyhow::Result<Self> {
let ro = Self::get_run1_runopts(aopts)?;
Ok(Self::new(aopts, runname, ro))
}
pub fn new_run1_replay(
aopts: &AnalyzeOpts,
runname: String,
selected: &RunOpts,
) -> anyhow::Result<Self> {
let mut replay = Self::new_run1_target(aopts, runname)?;
let target = &selected.det_opts.det_config;
let config = &mut replay.runopts.det_opts.det_config;
config.seed = target.seed;
config.rng_seed = target.rng_seed;
config.fuzz_seed = target.fuzz_seed;
config.sched_seed = target.sched_seed;
Ok(replay)
}
fn get_base_runopts(aopts: &AnalyzeOpts) -> anyhow::Result<RunOpts> {
let mut ro = Self::get_raw_runopts(aopts);
if ro.no_sequentialize_threads {
bail!(
"Error, cannot search through executions with --no-sequentialize-threads. Determinism required.",
)
}
ro.det_opts.det_config.chaos = true;
ro.validate_args()?;
assert!(ro.det_opts.det_config.sequentialize_threads);
if aopts.run1_seed.is_some() && !ro.det_opts.det_config.chaos {
eprintln!(
"{}",
"WARNING: --chaos not in supplied hermit run args, but --run1-seed is. Usually this is an error."
.bold()
.red()
)
}
Self::runopts_add_binds(aopts, &mut ro)?;
Ok(ro)
}
fn runopts_add_binds(aopts: &AnalyzeOpts, runopts: &mut RunOpts) -> anyhow::Result<()> {
let bind_dir: Bind = Bind::from_str(aopts.get_tmp()?.to_str().unwrap())?;
runopts.bind.push(bind_dir);
runopts.validate_args()?;
Ok(())
}
fn get_raw_runopts(aopts: &AnalyzeOpts) -> RunOpts {
let mut run_cmd: Vec<String> = vec!["hermit-run".to_string()];
for arg in &aopts.run_arg {
run_cmd.push(arg.to_string());
}
for arg in &aopts.run_args {
run_cmd.push(arg.to_string());
}
let mut runopts = RunOpts::try_parse_from(run_cmd.iter()).unwrap_or_else(|error| {
if crate::misplaced_backend_argument(&error).is_some() {
let value = run_cmd
.iter()
.enumerate()
.find_map(|(index, arg)| {
if arg == "--backend" {
Some(run_cmd.get(index + 1).map(String::as_str).unwrap_or(""))
} else {
arg.strip_prefix("--backend=")
}
})
.filter(|value| !value.is_empty() && !value.starts_with('-'))
.unwrap_or("<BACKEND>");
clap::Error::raw(
clap::error::ErrorKind::UnknownArgument,
format!(
"`--backend` is not a `run` option, so it cannot be passed through \
the run arguments of `hermit analyze` or `hermit bisect`. It is a \
global option: select the backend for every trial with `hermit \
--backend={value} analyze ...` or `hermit --backend={value} bisect \
...`.\n"
),
)
.exit()
}
error.exit()
});
runopts.set_backend(aopts.backend);
runopts
}
fn get_run1_runopts(aopts: &AnalyzeOpts) -> anyhow::Result<RunOpts> {
let mut ro = Self::get_base_runopts(aopts)?;
if let Some(seed) = aopts.run1_seed {
ro.det_opts.det_config.seed = seed;
} else if let Some(path) = &aopts.run1_preemptions {
ro.det_opts.det_config.replay_preemptions_from = Some(path.clone());
}
Ok(ro)
}
pub fn from_minimize_output(
aopts: &AnalyzeOpts,
runname: String,
runopts: RunOpts,
in_mem_preempts: PreemptionRecord,
preempts_path: PathBuf,
log_path: PathBuf,
) -> Self {
let log_level: LevelFilter = Self::log_level(aopts);
RunData {
runname,
analyze_opts: aopts.clone(),
runopts,
preempts_path_in: None,
sched_path_in: None,
sched_path_out: Some(preempts_path),
log_path: Some(log_path),
log_level,
in_mem_sched_out: Some(in_mem_preempts),
in_mem_preempts_in: None,
is_a_match: Some(true),
}
}
pub fn from_schedule_trace(
aopts: &AnalyzeOpts,
runname: String,
runopts: RunOpts,
sched_path: PathBuf,
) -> Self {
let log_level: LevelFilter = Self::log_level(aopts);
RunData {
runname,
analyze_opts: aopts.clone(),
runopts,
preempts_path_in: None,
sched_path_in: None,
sched_path_out: Some(sched_path),
log_path: None,
log_level,
in_mem_sched_out: None,
in_mem_preempts_in: None,
is_a_match: None,
}
}
pub fn with_preempts_path_in(mut self, path: PathBuf) -> Self {
self.runopts.det_opts.det_config.replay_preemptions_from = Some(path);
self
}
pub fn with_preempts_in(mut self, pr: PreemptionRecord) -> Self {
let path = self.preempts_path_in().to_path_buf();
pr.write_to_disk(&path)
.expect("write of preempts file to succeed");
self.in_mem_preempts_in = Some(pr);
self.with_preempts_path_in(path)
}
pub fn with_preemption_recording(self) -> Self {
let path = self.out_path().with_extension(PREEMPTS_EXT);
self.with_preemption_recording_to(path)
}
pub fn with_preemption_recording_to(mut self, path: PathBuf) -> Self {
self.runopts.det_opts.det_config.record_preemptions_to = Some(path);
self
}
pub fn with_schedule_recording(self) -> Self {
self.with_preemption_recording()
}
pub fn with_schedule_recording_to(self, path: PathBuf) -> Self {
self.with_preemption_recording_to(path)
}
pub fn with_schedule_replay(mut self) -> Self {
let path = self.sched_path_in().to_owned();
self.with_schedule_replay_from(path)
}
pub fn with_schedule_replay_from(mut self, path: PathBuf) -> Self {
self.runopts.det_opts.det_config.replay_schedule_from = Some(path);
self
}
pub fn to_repro(&self) -> String {
let logging = if let Some(path) = &self.log_path {
format!(" --log=debug --log-file={}", path.display())
} else {
"".to_string()
};
format!(
"hermit{}{} run {}",
logging,
self.runopts.global_backend_arg(),
self.runopts
)
}
pub fn into_runopts(self) -> RunOpts {
self.runopts
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn global_backend_reaches_every_trial_run() {
for flag in ["ptrace", "kvm"] {
let argv = [
"hermit".to_owned(),
format!("--backend={flag}"),
"analyze".to_owned(),
"--".to_owned(),
"/bin/true".to_owned(),
];
let args = crate::Args::try_parse_from(&argv).unwrap();
args.command
.validate_backend_scope(args.global.backend)
.unwrap_or_else(|error| panic!("{argv:?} must be in scope: {error:#}"));
let crate::Subcommand::Analyze(mut options) = args.command else {
panic!("{argv:?} is not analyze")
};
options.apply_global(&args.global);
let workspace = tempfile::tempdir().unwrap();
options.tmp_dir = Some(workspace.path().to_path_buf());
let run = RunData::new_baseline(&options, "backend".to_owned())
.unwrap_or_else(|error| panic!("{flag}: {error:#}"));
assert_eq!(
run.trial_global_opts().backend,
args.global.backend,
"{flag}"
);
assert_eq!(
run.runopts.global_backend_arg(),
format!(" --backend={flag}")
);
let repro = run.to_repro();
assert!(
repro.contains(&format!(" --backend={flag} run ")),
"{flag}: reproducer does not select the backend: {repro}"
);
}
}
#[test]
fn analyzer_reproducer_retains_actual_default_and_fractional_epoch() {
use clap::CommandFactory;
for epoch in ["2026-01-01T00:00:00Z", "2000-12-31T23:59:59.123456789Z"] {
let mut options = AnalyzeOpts::try_parse_from([
"analyze",
"--",
"--epoch",
epoch,
"--seed=71",
"/bin/true",
])
.unwrap();
let workspace = tempfile::tempdir().unwrap();
options.tmp_dir = Some(workspace.path().to_path_buf());
let run = RunData::new_baseline(&options, "epoch-repro".to_owned()).unwrap();
let repro = run.to_repro();
assert_eq!(repro.matches("--epoch=").count(), 1, "{repro}");
let matches = crate::Args::command()
.try_get_matches_from(shell_words::split(&repro).unwrap())
.unwrap();
let args = crate::args_from_matches_with_clock(&matches, || {
panic!("analyzer reproduction must retain its actual fixed input")
})
.unwrap();
let crate::Subcommand::Run(parsed) = args.command else {
panic!("expected run")
};
assert_eq!(
parsed.det_opts.det_config.epoch,
run.runopts.det_opts.det_config.epoch
);
assert_eq!(
parsed.det_opts.det_config.seed,
run.runopts.det_opts.det_config.seed
);
}
}
#[test]
fn preemption_replay_preserves_selected_seeds_and_original_timer_policy() {
for explicit_imprecise_timers in [false, true] {
let mut args = vec![
"analyze",
"--selfcheck",
"--search",
"--imprecise-search",
"--run1-seed=211",
"--",
];
if explicit_imprecise_timers {
args.push("--imprecise-timers");
}
args.push("/bin/true");
let mut options = AnalyzeOpts::try_parse_from(args).unwrap();
let workspace = tempfile::tempdir().unwrap();
options.tmp_dir = Some(workspace.path().to_path_buf());
let mut selected = RunData::new_baseline(&options, "selected".to_owned()).unwrap();
let selected_config = &mut selected.runopts.det_opts.det_config;
selected_config.seed = 101;
selected_config.rng_seed = Some(102);
selected_config.fuzz_seed = Some(103);
selected_config.sched_seed = Some(104);
selected_config.imprecise_timers = true;
let preempts = workspace.path().join("selected.preempts");
let replay = RunData::new_run1_replay(&options, "replay".to_owned(), &selected.runopts)
.unwrap()
.with_preempts_path_in(preempts.clone())
.with_preemption_recording();
let config = &replay.runopts.det_opts.det_config;
assert_eq!(config.seed, 101);
assert_eq!(config.rng_seed, Some(102));
assert_eq!(config.fuzz_seed, Some(103));
assert_eq!(config.sched_seed, Some(104));
assert_eq!(config.imprecise_timers, explicit_imprecise_timers);
assert_eq!(config.replay_preemptions_from.as_ref(), Some(&preempts));
assert_ne!(replay.runopts.save_config, selected.runopts.save_config);
assert_ne!(replay.runopts.summary_json, selected.runopts.summary_json);
assert_eq!(
config.record_preemptions_to.as_ref(),
Some(&workspace.path().join("replay_out.preempts")),
);
}
}
}