use std::collections::BTreeMap;
use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use detcore::BlockingMode;
use detcore_model::config::MountInfoRootRewrite;
use reverie::process::Command;
use serde::Deserialize;
use serde::Serialize;
use crate::consts::METADATA_NAME;
use crate::error::Context;
use crate::error::Error;
#[derive(
Debug,
Clone,
Copy,
Eq,
PartialEq,
Ord,
PartialOrd,
Deserialize,
Serialize
)]
#[repr(transparent)]
pub struct RecordVersion(u32);
impl RecordVersion {
pub fn compatible_with(&self, other: &RecordVersion) -> bool {
self == other
}
}
pub(crate) const RECORD_VERSION: RecordVersion = RecordVersion(0x120);
const HIGHEST_SHIPPED_RECORD_VERSION: u32 = 0x120;
const _: () = assert!(
RECORD_VERSION.0 >= HIGHEST_SHIPPED_RECORD_VERSION,
"RECORD_VERSION is BELOW HIGHEST_SHIPPED_RECORD_VERSION. A recording made by this \
build would carry a label an older reader already claims. If this fired during a \
rebase, the version constant was resolved BACKWARD -- merge it forward."
);
#[derive(Debug, Serialize, Deserialize)]
pub struct Metadata {
pub exe: PathBuf,
pub program: String,
pub arg0: String,
pub args: Vec<String>,
pub current_dir: PathBuf,
pub hostname: Option<String>,
pub domainname: Option<String>,
pub envs: BTreeMap<String, String>,
pub version: RecordVersion,
#[serde(default)]
pub proc_readonly: Option<bool>,
#[serde(default)]
pub mountinfo_root_rewrites: Vec<MountInfoRootRewrite>,
#[serde(default)]
pub mountinfo_mount_ids: Vec<u64>,
#[serde(default)]
pub mountinfo_mount_ids_captured: bool,
#[serde(default)]
pub fdinfo_unlisted_mount_ids: Vec<u64>,
#[serde(default)]
pub local_networking: Option<bool>,
}
impl Metadata {
pub fn new(command: &Command) -> Result<Self, Error> {
let exe = command.find_program()?;
let program = command.get_program().to_string_lossy().into_owned();
let arg0 = command.get_arg0().to_string_lossy().into_owned();
let args = command
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())
.collect();
let envs = command
.get_captured_envs()
.into_iter()
.map(|(k, v)| {
(
k.to_string_lossy().into_owned(),
v.to_string_lossy().into_owned(),
)
})
.collect();
let current_dir = command
.get_current_dir()
.map_or_else(|| env::current_dir().unwrap(), ToOwned::to_owned);
let hostname = command
.get_hostname()
.map(|s| s.to_string_lossy().into_owned());
let domainname = command
.get_domainname()
.map(|s| s.to_string_lossy().into_owned());
Ok(Self {
exe,
program,
arg0,
args,
current_dir,
hostname,
domainname,
envs,
version: RECORD_VERSION,
proc_readonly: None,
mountinfo_root_rewrites: Vec::new(),
mountinfo_mount_ids: Vec::new(),
mountinfo_mount_ids_captured: false,
fdinfo_unlisted_mount_ids: Vec::new(),
local_networking: None,
})
}
pub fn load(dir: &Path) -> Result<Self, Error> {
let metadata_path = dir.join(METADATA_NAME);
serde_json::from_reader(
fs::File::open(&metadata_path)
.with_context(|| format!("Failed to open {:?}", metadata_path))?,
)
.with_context(|| format!("Failed to parse {:?}", metadata_path))
}
pub fn command(&self) -> Command {
let mut command = Command::new(&self.exe);
command.arg0(&self.arg0);
command.args(&self.args);
command.env_clear();
command.envs(&self.envs);
command.current_dir(&self.current_dir);
if let Some(hostname) = &self.hostname {
command.hostname(hostname);
}
if let Some(domainname) = &self.domainname {
command.domainname(domainname);
}
command
}
}
pub fn record_or_replay_config(data: &Path) -> detcore::Config {
let default_config: detcore::Config = Default::default();
let mut config = detcore::Config {
panic_on_unsupported_syscalls: true,
exit_on_unsupported_syscall: true,
shutdown_on_unsupported_syscall: false,
unsupported_syscall_report_fd: None,
panic_on_rcb_overshoot: false,
sequentialize_threads: true,
runs_post_fork: default_config.runs_post_fork,
passthru_opt: true,
deterministic_io: false,
virtualize_time: crate::RECORD_REPLAY_VIRTUALIZES_TIME,
virtualize_metadata: false,
mountinfo_root_rewrites: Vec::new(),
mountinfo_device_rewrites: Vec::new(),
mountinfo_mount_ids: Vec::new(),
mountinfo_mount_ids_captured: false,
fdinfo_unlisted_mount_ids: Vec::new(),
virtualize_cpuid: true,
cpuid_virtualized_by_backend: false,
backend_supports_madvise: true,
discover_live_file_metadata: false,
use_thread_local_clock_reads: false,
detect_host_clock_futex_timeouts: false,
syscall_clobbers_virtualized_by_backend: false,
cancel_killed_thread_rpcs: false,
backend_reports_physical_process_exits: false,
backend_serializes_fork_children: false,
backend_dispatches_thread_tools: true,
backend_tracks_process_children: true,
backend_runs_exit_robust_list: true,
backend_requires_thread_directed_process_signals: false,
backend_is_kvm: false,
kvm_shared_dequeue_timers: false,
backend_supports_parked_write_signal_interruption: true,
backend_virtualizes_capability_prctls: false,
backend_defers_vfork_child_registration: false,
has_uts_namespace: true,
replay_data: Some(data.to_path_buf()),
clock_multiplier: None,
epoch: default_config.epoch,
gdbserver: false,
gdbserver_port: default_config.gdbserver_port,
kill_daemons: default_config.kill_daemons,
max_timeslice: default_config.max_timeslice,
target_timeslice: default_config.target_timeslice,
seed: default_config.seed,
rng_seed: default_config.rng_seed,
imprecise_timers: false,
chaos: false,
sigint_instakill: false,
warn_non_zero_binds: false,
sched_heuristic: Default::default(),
sched_seed: default_config.sched_seed,
network_trace: Default::default(),
recordreplay_modes: true,
record_preemptions: false,
record_preemptions_to: None,
replay_preemptions_from: None,
replay_schedule_from: None,
replay_exhausted_panic: false,
die_on_desync: true,
stacktrace_event: Vec::new(),
stacktrace_signal: None,
preemption_stacktrace: false,
preemption_stacktrace_log_file: None,
stop_after_turn: None,
stop_after_iter: None,
debug_externalize_sockets: false,
debug_futex_mode: BlockingMode::Precise,
sched_sticky_random_param: 0.0,
no_rcb_time: false,
detlog_heap: false,
detlog_stack: false,
detlog_regs: false,
detlog_io_buffers: crate::RECORD_REPLAY_HASHES_IO_BUFFERS,
detlog_regs_cadence: 1,
sysinfo_uptime_offset: 120,
memory: default_config.memory,
interrupt_at: vec![],
happens_before: None,
fuzz_futexes: false,
chaos_target_races: false,
chaos_per_thread_slowdown: false,
chaos_slowdown_max_factor: 10.0,
chaos_epoch_length_ns: 0,
fuzz_seed: None,
};
if config.max_timeslice.is_some() && !reverie_ptrace::is_perf_supported() {
tracing::warn!(
"Hardware perf counters are not supported on this machine. Records/Replays may randomly fail!"
);
config.max_timeslice = None;
}
config
}
#[cfg(test)]
mod tests {
use reverie::Tool;
use reverie::syscalls::Sysno;
use super::*;
#[test]
fn record_version_requires_an_exact_match() {
assert!(RECORD_VERSION.compatible_with(&RECORD_VERSION));
let current = RECORD_VERSION.0;
for delta in 1..=16u32 {
let older = RecordVersion(current - delta);
assert!(
!RECORD_VERSION.compatible_with(&older),
"recorder at {current:#x} must refuse a recording made at {:#x}",
older.0
);
let newer = RecordVersion(current + delta);
assert!(
!RECORD_VERSION.compatible_with(&newer),
"recorder at {current:#x} must refuse a recording made at {:#x}",
newer.0
);
}
}
#[test]
fn record_and_replay_preserve_partial_subscriptions_and_fail_closed() {
let config = record_or_replay_config(Path::new("replay-data"));
assert!(config.passthru_opt);
assert!(config.panic_on_unsupported_syscalls);
assert!(config.exit_on_unsupported_syscall);
assert!(!config.shutdown_on_unsupported_syscall);
}
#[test]
fn record_and_replay_use_run_default_memory() {
let run_default = detcore::Config::default();
assert_eq!(run_default.memory, 1_000_000_000);
assert_eq!(
record_or_replay_config(Path::new("replay-data")).memory,
run_default.memory
);
}
#[test]
fn recording_does_not_virtualize_time_as_documented() {
let config = record_or_replay_config(Path::new("replay-data"));
assert!(
!config.virtualize_time,
"record/replay must not virtualize time; a green replay verdict would \
otherwise be mistaken for a determinism result"
);
assert_eq!(
config.virtualize_time,
crate::RECORD_REPLAY_VIRTUALIZES_TIME,
"the config the run uses and the constant the report discloses must be \
the same decision, not two that happen to agree"
);
}
#[test]
fn record_and_replay_subscribe_every_determinized_syscall() {
let config = record_or_replay_config(Path::new("replay-data"));
let record = <detcore::Detcore<crate::recorder::Recorder> as Tool>::subscriptions(&config);
let replay = <detcore::Detcore<crate::replayer::Replayer> as Tool>::subscriptions(&config);
for (phase, subscriptions) in [("record", record), ("replay", replay)] {
let delivered = subscriptions.iter_syscalls().collect::<Vec<_>>();
let missing = detcore::all_pinned_syscalls()
.filter(|sysno| detcore::is_determinized_syscall(*sysno))
.filter(|sysno| !delivered.contains(sysno))
.collect::<Vec<_>>();
assert!(
missing.is_empty(),
"{phase} lets Determinized syscalls bypass Detcore: {}",
missing
.iter()
.map(|sysno| sysno.to_string())
.collect::<Vec<_>>()
.join(" ")
);
assert!(
delivered.contains(&Sysno::syslog),
"{phase} must deliver syslog to its deterministic Detcore handler"
);
assert!(
delivered.contains(&Sysno::chdir) && delivered.contains(&Sysno::getcwd),
"{phase} must deliver chdir and getcwd so replay uses recorded results"
);
for sysno in [
Sysno::fsync,
Sysno::fdatasync,
Sysno::syncfs,
Sysno::fchmod,
Sysno::fchown,
Sysno::fsetxattr,
Sysno::fremovexattr,
Sysno::setxattr,
Sysno::lsetxattr,
Sysno::removexattr,
Sysno::lremovexattr,
Sysno::getxattr,
Sysno::lgetxattr,
Sysno::fgetxattr,
Sysno::listxattr,
Sysno::llistxattr,
Sysno::flistxattr,
] {
assert!(
delivered.contains(&sysno),
"{phase} must deliver {sysno} so replay uses its recorded result"
);
}
assert!(
!delivered.contains(&Sysno::umask),
"{phase} must leave unlisted PassThrough umask unsubscribed"
);
}
}
#[test]
fn record_version_rejects_pre_flock_streams() {
assert!(!RECORD_VERSION.compatible_with(&RecordVersion(0x10b)));
}
#[test]
fn record_version_rejects_previous_memory_configuration() {
assert!(!RECORD_VERSION.compatible_with(&RecordVersion(0x10a)));
}
#[test]
fn record_version_rejects_pre_complete_determinized_subscription_streams() {
assert!(!RECORD_VERSION.compatible_with(&RecordVersion(0x109)));
assert!(!RECORD_VERSION.compatible_with(&RecordVersion(0x104)));
assert!(!RECORD_VERSION.compatible_with(&RecordVersion(0x102)));
}
#[test]
fn metadata_without_a_network_choice_loads_as_unknown() {
let metadata: Metadata = serde_json::from_value(serde_json::json!({
"exe": "/bin/true",
"program": "true",
"arg0": "true",
"args": [],
"current_dir": "/",
"hostname": null,
"domainname": null,
"envs": {},
"version": RECORD_VERSION,
}))
.unwrap();
assert_eq!(metadata.local_networking, None);
}
}