#![forbid(unsafe_code)]
mod actions;
mod bind;
mod control;
mod defer;
mod error;
pub mod intern;
pub mod level;
mod model;
mod periodic;
mod randomness;
mod reach;
mod spec;
pub mod symmetry;
mod tables;
mod tlc_names;
mod walk;
use std::sync::Arc;
use dive_config::ModelConfig;
use dive_ir::Ir;
pub use bind::{BindOptions, HANOI_BITS_CLASS_SHA256, local_java_class_digests};
pub use control::ControlledBindError;
pub use error::{BindError, Ec, ErrorKind, Failure, Message};
pub use model::{Init, Model, Named, Pred, Settings, Unsupported, UnsupportedKind};
pub use randomness::transformed_randomness;
pub use reach::TLC_CDOT;
pub use symmetry::{SymmetryLiveness, SymmetryLivenessResolution};
#[derive(Clone, Debug)]
pub struct Recipe {
ir: Arc<Ir>,
config: ModelConfig,
options: dive_eval::Options,
no_deadlock: bool,
symmetry_liveness: SymmetryLiveness,
}
impl Recipe {
#[must_use]
pub fn new(
ir: Arc<Ir>,
config: ModelConfig,
options: dive_eval::Options,
no_deadlock: bool,
) -> Self {
Self {
ir,
config,
options,
no_deadlock,
symmetry_liveness: SymmetryLiveness::Refuse,
}
}
#[must_use]
pub fn has_randomness(&self) -> bool {
randomness::source_randomness(&self.ir, &self.config)
}
#[must_use]
pub fn with_symmetry_liveness(mut self, policy: SymmetryLiveness) -> Self {
self.symmetry_liveness = policy;
self
}
pub fn bind(&self, host: Arc<dyn dive_eval::Host>) -> Result<Model, Box<Failure>> {
bind_in(
self.ir.clone(),
&self.config,
&BindOptions {
eval: self.options,
symmetry_liveness: self.symmetry_liveness,
no_deadlock: self.no_deadlock,
host: Some(host),
},
true,
None,
false,
)
.map_err(ControlledBindError::into_uncontrolled)
}
pub fn bind_controlled(
&self,
host: Arc<dyn dive_eval::Host>,
control: Arc<dyn dive_eval::HostControl>,
) -> Result<Model, ControlledBindError> {
bind_in(
self.ir.clone(),
&self.config,
&BindOptions {
eval: self.options,
symmetry_liveness: self.symmetry_liveness,
no_deadlock: self.no_deadlock,
host: Some(host),
},
true,
Some(control),
false,
)
}
#[must_use]
pub fn model_values(&self) -> &[String] {
&self.config.model_values
}
pub fn bind_fallible(
&self,
host: Arc<dyn dive_eval::Host>,
control: Option<Arc<dyn dive_eval::HostControl>>,
) -> Result<Model, ControlledBindError> {
self.bind_replay(host, control, true)
}
pub fn bind_next_fallible(
&self,
host: Arc<dyn dive_eval::Host>,
control: Option<Arc<dyn dive_eval::HostControl>>,
) -> Result<Model, ControlledBindError> {
self.bind_replay(host, control, false)
}
fn bind_replay(
&self,
host: Arc<dyn dive_eval::Host>,
control: Option<Arc<dyn dive_eval::HostControl>>,
predicate_only: bool,
) -> Result<Model, ControlledBindError> {
bind_in(
self.ir.clone(),
&self.config,
&BindOptions {
eval: self.options,
symmetry_liveness: self.symmetry_liveness,
no_deadlock: self.no_deadlock,
host: Some(host),
},
predicate_only,
control,
true,
)
}
}
pub const DUMP_TRACE_JSON_MODULES: &[&str] = &["_JsonTrace"];
pub fn bind(ir: Arc<Ir>, cfg: &ModelConfig, opts: &BindOptions) -> Result<Model, Box<Failure>> {
bind_in(ir, cfg, opts, false, None, false).map_err(ControlledBindError::into_uncontrolled)
}
pub fn bind_deferring_constants(
ir: Arc<Ir>,
cfg: &ModelConfig,
opts: &BindOptions,
) -> Result<Model, Box<Failure>> {
bind_in_deferring(ir, cfg, opts, false, None, false, true)
.map_err(ControlledBindError::into_uncontrolled)
}
pub fn bind_controlled(
ir: Arc<Ir>,
cfg: &ModelConfig,
opts: &BindOptions,
control: Arc<dyn dive_eval::HostControl>,
) -> Result<Model, ControlledBindError> {
bind_in(ir, cfg, opts, false, Some(control), false)
}
fn bind_in(
ir: Arc<Ir>,
cfg: &ModelConfig,
opts: &BindOptions,
predicates_only: bool,
control: Option<Arc<dyn dive_eval::HostControl>>,
fallible_errors: bool,
) -> Result<Model, ControlledBindError> {
bind_in_deferring(
ir,
cfg,
opts,
predicates_only,
control,
fallible_errors,
false,
)
}
#[allow(
clippy::too_many_arguments,
reason = "existing binder ownership inputs plus the core route's deferral switch"
)]
fn bind_in_deferring(
ir: Arc<Ir>,
cfg: &ModelConfig,
opts: &BindOptions,
predicates_only: bool,
control: Option<Arc<dyn dive_eval::HostControl>>,
fallible_errors: bool,
defer: bool,
) -> Result<Model, ControlledBindError> {
dive_eval::validate_ir(&ir).map_err(|error| {
Box::new(Failure {
error: BindError::unsupported(error.to_string()),
output: Vec::new(),
})
})?;
if (predicates_only || fallible_errors)
&& let Some(feature) = transformed_randomness(&ir, cfg)
{
return Err(Box::new(Failure {
error: BindError::unsupported(feature),
output: Vec::new(),
})
.into());
}
let walk = {
let t = tables::Tables::new(ir.clone()).map_err(|error| {
Box::new(Failure {
error,
output: Vec::new(),
})
})?;
walk::process_constants(&t, opts.eval.ctx.int_mode)
};
let ir: Arc<Ir> = ir;
if let Some(error) = walk.error {
return Err(Box::new(Failure {
error,
output: Vec::new(),
})
.into());
}
let feed = intern::feed(&ir, cfg);
let names = Arc::new(feed);
let mut b = bind::Binder::new(ir, cfg, opts, names, control, fallible_errors)
.map_err(|error| error.with_output(Vec::new()))?;
if defer && !predicates_only {
b.defer = Some(defer::Deferral {
skip: std::collections::HashSet::new(),
evaluate: std::collections::HashSet::new(),
assumptions: Vec::new(),
});
}
let result = run(&mut b, predicates_only);
b.drain_prints();
match result {
Ok(model) => Ok(model),
Err(error) => Err(error.with_output(std::mem::take(&mut b.output))),
}
}
#[allow(clippy::too_many_lines, reason = "ordered binding phases")]
fn run(b: &mut bind::Binder<'_>, predicates_only: bool) -> bind::R<Model> {
b.process_spec()?;
let periodic_definition = b
.cfg
.periodic
.as_ref()
.and_then(|n| b.defns.get(&n.text).cloned());
let symmetry_definition = b
.cfg
.symmetry
.as_ref()
.and_then(|n| b.defns.get(&n.text).cloned());
b.build_evaluator()?;
if let Some(feature) = b.unsupported.first() {
return Err(BindError::unsupported(feature.clone()).into());
}
let _control = b
.control
.as_ref()
.map(|control| b.ev().host_control(Some(control.clone())))
.transpose()?;
if let Some(control) = &b.control {
control.check().map_err(|interruption| {
control::StepError::Interrupted(dive_eval::EvalError::interrupted(interruption))
})?;
}
b.process_constant_defns()?;
b.process_config()?;
if let Some(name) = &b.cfg.periodic {
periodic::resolve(b, &name.text, periodic_definition.as_ref())?;
return Err(BindError::unsupported("_PERIODIC").into());
}
let view = b.process_view()?;
let (init_actions, actions) = if predicates_only {
(Vec::new(), Vec::new())
} else {
(b.init_actions()?, b.expand_actions()?)
};
b.drain_prints();
let ir = b.t.ir.clone();
let assumptions = non_axiom_assumptions(&ir);
let alias = b
.process_alias()
.map(|result| match result {
Ok(alias) => Ok(Ok(alias)),
Err(error) => error.ordinary().map(Err),
})
.transpose()?;
let postconditions = match b.process_postconditions() {
Ok(predicates) => Ok(predicates),
Err(error) => Err(error.ordinary()?),
};
let symmetry_liveness = SymmetryLivenessResolution::new(
b.opts.symmetry_liveness,
b.cfg.symmetry.as_ref().map(|name| name.text.clone()),
!b.implied_temporals.is_empty(),
);
let mut model = Model {
ir: ir.clone(),
evaluator: b.ev().clone(),
names: b.names.clone(),
output: Vec::new(),
init: std::mem::take(&mut b.init),
next: b.next.clone(),
init_actions,
actions,
invariants: std::mem::take(&mut b.invariants),
implied_inits: std::mem::take(&mut b.implied_inits),
implied_actions: std::mem::take(&mut b.implied_actions),
temporals: std::mem::take(&mut b.temporals),
implied_temporals: std::mem::take(&mut b.implied_temporals),
implied_temporal_action_names: std::mem::take(&mut b.implied_temporal_action_names),
configured_properties: std::mem::take(&mut b.configured_properties),
constraints: std::mem::take(&mut b.constraints),
action_constraints: std::mem::take(&mut b.action_constraints),
view,
alias,
postconditions,
symmetry: b.cfg.symmetry.as_ref().map(|n| n.text.clone()),
symmetry_definition,
symmetry_liveness,
symmetry_reduction: None,
settings: model::Settings {
check_deadlock: b.cfg.checks_deadlock() && !b.opts.no_deadlock,
},
assumptions,
unsupported: Vec::new(),
sequential: Vec::new(),
sequential_names_only: false,
runtime_name_order: false,
stateful_checking: None,
deferred_constants: std::mem::take(&mut b.deferred),
deferred_assumptions: b
.defer
.as_ref()
.map(|d| d.assumptions.clone())
.unwrap_or_default(),
};
for d in b.keep_temporal_startup_values(&model)? {
model.deferred_constants.remove(&d);
}
model.unsupported = reach::unsupported(b, &model);
let sequential = reach::sequential(b, &model)?;
model.sequential = sequential.reasons;
model.sequential_names_only = sequential.names_only;
model.runtime_name_order = sequential.runtime_names;
model.stateful_checking = sequential.stateful_checking;
model.output = std::mem::take(&mut b.output);
model.seal_startup_names();
Ok(model)
}
fn non_axiom_assumptions(ir: &Ir) -> Vec<dive_ir::DefId> {
let root = ir.module(ir.root());
root.assumptions
.iter()
.zip(root.axioms.iter().chain(std::iter::repeat(&false)))
.filter(|(_, ax)| !**ax)
.map(|(d, _)| *d)
.collect()
}