use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use dive_config::ModelConfig;
use dive_ir::{Effects, Ir};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::{CheckOptions, CheckResult, Verdict};
#[doc(hidden)]
pub const HEADER: usize = 48;
pub const MAX_PAYLOAD_BYTES: usize = 64 << 20;
pub const MAX_STATES: usize = 65_536;
pub const MAX_STATE_BYTES: usize = 1 << 20;
pub const MAX_LEVEL: u64 = 65_536;
pub const MAX_COUNTER: u64 = 9_223_372_036_854_775_807;
static SERIAL: AtomicU64 = AtomicU64::new(0);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum Counters {
#[default]
Tlc,
Retain,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Action {
PauseAfterLevels(u64),
Recover,
}
#[derive(Clone, Debug)]
pub struct Request {
pub path: PathBuf,
pub action: Action,
pub counters: Counters,
pub config_source_sha256: Option<String>,
}
#[derive(Clone, Debug, Serialize)]
pub struct Receipt {
pub status: &'static str,
pub payload_sha256: String,
pub transient_storage_cleanup_completed: bool,
pub archive_bytes: u64,
pub next_level: u64,
pub discovered: u64,
pub queued: u64,
pub counters: Counters,
}
impl Receipt {
pub fn finish_storage(
&mut self,
frontier: &crate::FrontierUsage,
predecessor: &crate::result::PredecessorUsage,
) {
self.transient_storage_cleanup_completed = frontier.failures.is_empty()
&& predecessor.failures.is_empty()
&& predecessor.cleanup_completed;
}
}
#[derive(Clone, Debug)]
pub struct Result {
pub check: CheckResult,
pub receipt: Option<Receipt>,
}
#[doc(hidden)]
pub type Fallible<T> = std::result::Result<T, String>;
#[doc(hidden)]
#[allow(
missing_docs,
reason = "serialized identity fields named by the format"
)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Identity {
pub schema: u32,
pub semantics: String,
pub executable_sha256: String,
pub ir_sha256: String,
pub config_sha256: String,
pub config_source_sha256: Option<String>,
pub seed: i64,
pub fp: usize,
pub options: String,
}
#[doc(hidden)]
pub fn failure(message: impl Into<String>) -> Verdict {
Verdict::ResourceLimit {
message: format!("DIVE-S0400: native checkpoint {}", message.into()),
}
}
#[doc(hidden)]
pub fn unsupported(feature: impl Into<String>) -> Verdict {
Verdict::Unsupported {
feature: format!("native checkpoint {}", feature.into()),
}
}
#[doc(hidden)]
#[must_use]
pub fn hex(bytes: &[u8]) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
bytes
.iter()
.flat_map(|b| {
[
char::from(HEX[usize::from(b >> 4)]),
char::from(HEX[usize::from(b & 15)]),
]
})
.collect()
}
#[doc(hidden)]
#[must_use]
pub fn hash(bytes: &[u8]) -> String {
hex(&Sha256::digest(bytes))
}
#[doc(hidden)]
pub fn executable_hash() -> Fallible<String> {
let mut file = File::open(std::env::current_exe().map_err(|e| e.to_string())?)
.map_err(|e| e.to_string())?;
let mut digest = Sha256::new();
let mut bytes = [0u8; 8192];
loop {
let n = file.read(&mut bytes).map_err(|e| e.to_string())?;
if n == 0 {
break;
}
digest.update(&bytes[..n]);
}
Ok(hex(&digest.finalize()))
}
#[doc(hidden)]
pub fn admit_source_inner(
ir: &Ir,
cfg: &ModelConfig,
allow_temporal: bool,
) -> std::result::Result<(), Verdict> {
let mut targets: std::collections::BTreeSet<&str> = cfg
.constants
.iter()
.filter_map(|entry| {
if let dive_config::ConstantEntry::Substitution { target, .. } = entry {
Some(target.text.as_str())
} else {
None
}
})
.collect();
for name in [&cfg.init, &cfg.next, &cfg.view, &cfg.symmetry, &cfg.alias]
.into_iter()
.flatten()
{
targets.insert(name.text.as_str());
}
for names in [
&cfg.invariants,
&cfg.constraints,
&cfg.action_constraints,
&cfg.postconditions,
] {
targets.extend(names.iter().map(|name| name.text.as_str()));
}
let mut roots: Vec<_> = ir
.defs()
.iter()
.enumerate()
.filter_map(|(index, def)| {
let startup = !ir.module(def.module).standard
|| (def.params.is_empty() && matches!(def.scope, dive_ir::DefScope::TopLevel));
let user = startup && def.std.is_none();
(user || targets.contains(def.name.as_str()))
.then(|| dive_ir::DefId(u32::try_from(index).unwrap_or(u32::MAX)))
})
.collect();
roots.extend(ir.modules().iter().flat_map(|module| {
module.context.iter().filter_map(|(name, symbol)| {
if targets.contains(name.as_str())
&& let dive_ir::Symbol::Def(def) = symbol
{
Some(*def)
} else {
None
}
})
}));
let reached = ir.reachable_defs(&roots);
let mut seen = std::collections::BTreeSet::new();
for (index, def) in ir.defs().iter().enumerate() {
if !reached[index] {
continue;
}
if def
.effects
.intersects(
Effects::IMPURE
.union(Effects::LIVE_TLCGET)
.union(if allow_temporal {
Effects::NONE
} else {
Effects::TEMPORAL
}),
)
{
return Err(unsupported(format!(
"runtime effects or temporal effects in {}",
def.name
)));
}
if let Some(op) = def.std {
admit_native(op)?;
continue;
}
let mut pending = vec![def.body];
while let Some(id) = pending.pop() {
if !seen.insert(id) {
continue;
}
let expression = ir.expr(id);
for reference in expression.op_refs() {
if let dive_ir::OpRef::Std(op) = reference {
admit_native(*op)?;
}
}
pending.extend(expression.children());
}
}
Ok(())
}
fn admit_native(op: dive_ir::StdOp) -> std::result::Result<(), Verdict> {
if op.impure()
|| matches!(
op,
dive_ir::StdOp::TlcTLCGet
| dive_ir::StdOp::TlcTLCSet
| dive_ir::StdOp::TlcExtTLCGetOrDefault
)
{
Err(unsupported(format!(
"runtime effects or checker/register reads in {} are not recoverable in this slice",
op.name()
)))
} else {
Ok(())
}
}
pub fn read_payload(path: &Path, magic: &[u8; 8]) -> Fallible<(Vec<u8>, String, u64)> {
let mut file = File::open(path).map_err(|e| e.to_string())?;
let len = file.metadata().map_err(|e| e.to_string())?.len();
if len < HEADER as u64 || len > (HEADER + MAX_PAYLOAD_BYTES) as u64 {
return Err("archive byte bound".into());
}
let mut header = [0u8; HEADER];
file.read_exact(&mut header).map_err(|e| e.to_string())?;
if &header[..8] != magic {
return Err("native magic/schema mismatch".into());
}
let mut n = [0u8; 8];
n.copy_from_slice(&header[8..16]);
let size = usize::try_from(u64::from_le_bytes(n)).map_err(|e| e.to_string())?;
if size > MAX_PAYLOAD_BYTES || size as u64 + HEADER as u64 != len {
return Err("payload length/trailing bytes mismatch".into());
}
let mut payload = Vec::new();
payload.try_reserve_exact(size).map_err(|e| e.to_string())?;
payload.resize(size, 0);
file.read_exact(&mut payload).map_err(|e| e.to_string())?;
let mut trailing = [0];
if file.read(&mut trailing).map_err(|e| e.to_string())? != 0 {
return Err("archive changed or has trailing bytes".into());
}
let sha = Sha256::digest(&payload);
if sha.as_slice() != &header[16..48] {
return Err("payload checksum mismatch".into());
}
Ok((payload, hex(&sha), len))
}
pub fn write_payload(path: &Path, magic: &[u8; 8], payload: &[u8]) -> Fallible<()> {
if payload.len() > MAX_PAYLOAD_BYTES {
return Err("archive payload byte bound".into());
}
let parent = path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let stem = path
.file_name()
.ok_or_else(|| "checkpoint needs a file name".to_owned())?
.to_string_lossy();
let staging = parent.join(format!(
".{stem}.{}-{}.tmp",
std::process::id(),
SERIAL.fetch_add(1, Ordering::Relaxed)
));
let result = (|| {
let mut options = OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let mut file = options.open(&staging).map_err(|e| e.to_string())?;
file.write_all(magic)
.and_then(|()| file.write_all(&(payload.len() as u64).to_le_bytes()))
.and_then(|()| file.write_all(&Sha256::digest(payload)))
.and_then(|()| file.write_all(payload))
.and_then(|()| file.sync_all())
.map_err(|e| e.to_string())?;
drop(file);
fs::hard_link(&staging, path).map_err(|e| e.to_string())?;
fs::remove_file(&staging)
.map_err(|e| format!("committed but staging cleanup failed: {e}"))?;
File::open(parent)
.and_then(|f| f.sync_all())
.map_err(|e| format!("committed but directory sync failed: {e}"))?;
Ok(())
})();
match result {
Ok(()) => Ok(()),
Err(original) => {
failed_staging_cleanup(original, staging.try_exists(), || fs::remove_file(&staging))
}
}
}
#[doc(hidden)]
pub fn failed_staging_cleanup(
original: String,
inspection: std::io::Result<bool>,
cleanup: impl FnOnce() -> std::io::Result<()>,
) -> Fallible<()> {
match inspection {
Ok(false) => Err(original),
Ok(true) => match cleanup() {
Ok(()) => Err(original),
Err(error) => Err(format!(
"original failure: {original}; staging cleanup failed: {error}"
)),
},
Err(error) => Err(format!(
"original failure: {original}; staging inspection failed: {error}"
)),
}
}
#[must_use]
pub fn config_source_hash(bytes: &[u8]) -> String {
hash(bytes)
}
pub fn owned_identity(
ir: &Ir,
cfg: &ModelConfig,
opts: &CheckOptions,
request: &Request,
core_options: &str,
) -> std::result::Result<Vec<u8>, Verdict> {
if ir.vars().len() > 1024 {
return Err(unsupported("state variable arity above 1024"));
}
if cfg.periodic.is_some() || cfg.rl_reward.is_some() || !cfg.possible.is_empty() {
return Err(unsupported(
"periodic/reward/possible hooks in owned Core recovery",
));
}
admit_source_inner(ir, cfg, true)?;
let mut ir_bytes = serde_json::to_vec(ir).map_err(|e| failure(e.to_string()))?;
let local_classes = dive_model::local_java_class_digests(ir).map_err(unsupported)?;
ir_bytes.extend(serde_json::to_vec(&local_classes).map_err(|e| failure(e.to_string()))?);
let identity = Identity {
schema: 1,
semantics: "owned-core-safety-0141-v1".into(),
executable_sha256: executable_hash().map_err(failure)?,
ir_sha256: hash(&ir_bytes),
config_sha256: hash(format!("{cfg:?}").as_bytes()),
config_source_sha256: request.config_source_sha256.clone(),
seed: opts.seed,
fp: opts.fp_index,
options: format!(
"{:?}/{:?}/{:?}/{}/{}/{}/{}/{}/{}/{:?}/{:?}/{core_options}",
opts.profile,
opts.eval.ctx,
opts.eval.exploration,
opts.eval.max_depth,
opts.eval.warnings,
opts.fp_index,
opts.tlc_fingerprints,
opts.seed,
opts.no_deadlock,
opts.store,
opts.eval.comparisons
),
};
serde_json::to_vec(&identity).map_err(|e| failure(e.to_string()))
}
pub fn owned_identity_seed_fp(bytes: &[u8]) -> Fallible<(i64, usize)> {
let identity: Identity = serde_json::from_slice(bytes).map_err(|e| e.to_string())?;
if identity.schema != 1
|| identity.semantics != "owned-core-safety-0141-v1"
|| identity.fp >= dive_value::fp64::POLYS.len()
{
return Err("owned identity schema/semantics/fingerprint mismatch".into());
}
Ok((identity.seed, identity.fp))
}