Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Type check term specs in isolation, and fix the over-constrained specs it finds. (#13998)

Alexa VanHattum9bdbd42cc4e0
diff --git a/cranelift/codegen/src/isa/aarch64/spec/mov_from_vec.isle b/cranelift/codegen/src/isa/aarch64/spec/mov_from_vec.isle
index 51989dc521..3d600bac7b 100644
--- a/cranelift/codegen/src/isa/aarch64/spec/mov_from_vec.isle
+++ b/cranelift/codegen/src/isa/aarch64/spec/mov_from_vec.isle
@@ -7,50 +7,31 @@
(provide
(match
size
- ((Size64)
- (and
- (=> (= idx #x00) (= rd (extract 63 0 (as rn (bv 128)))))
- (=> (= idx #x01) (= rd (extract 127 64 (as rn (bv 128)))))
- )
- )
+ ((Size64) (= rd (extract 63 0 (as rn (bv 64)))))
((Size32)
(and
- (=> (= idx #x00) (= rd (zero_ext 64 (extract 31 0 (as rn (bv 128))))))
- (=> (= idx #x01) (= rd (zero_ext 64 (extract 63 32 (as rn (bv 128))))))
- (=> (= idx #x02) (= rd (zero_ext 64 (extract 95 64 (as rn (bv 128))))))
- (=> (= idx #x03) (= rd (zero_ext 64 (extract 127 96 (as rn (bv 128))))))
+ (=> (= idx #x00) (= rd (zero_ext 64 (extract 31 0 (as rn (bv 64))))))
+ (=> (= idx #x01) (= rd (zero_ext 64 (extract 63 32 (as rn (bv 64))))))
)
)
((Size16)
(and
- (=> (= idx #x00) (= rd (zero_ext 64 (extract 15 0 (as rn (bv 128))))))
- (=> (= idx #x01) (= rd (zero_ext 64 (extract 31 16 (as rn (bv 128))))))
- (=> (= idx #x02) (= rd (zero_ext 64 (extract 47 32 (as rn (bv 128))))))
- (=> (= idx #x03) (= rd (zero_ext 64 (extract 63 48 (as rn (bv 128))))))
- (=> (= idx #x04) (= rd (zero_ext 64 (extract 79 64 (as rn (bv 128))))))
- (=> (= idx #x05) (= rd (zero_ext 64 (extract 95 80 (as rn (bv 128))))))
- (=> (= idx #x06) (= rd (zero_ext 64 (extract 111 96 (as rn (bv 128))))))
- (=> (= idx #x07) (= rd (zero_ext 64 (extract 127 112 (as rn (bv 128))))))
+ (=> (= idx #x00) (= rd (zero_ext 64 (extract 15 0 (as rn (bv 64))))))
+ (=> (= idx #x01) (= rd (zero_ext 64 (extract 31 16 (as rn (bv 64))))))
+ (=> (= idx #x02) (= rd (zero_ext 64 (extract 47 32 (as rn (bv 64))))))
+ (=> (= idx #x03) (= rd (zero_ext 64 (extract 63 48 (as rn (bv 64))))))
)
)
((Size8)
(and
- (=> (= idx #x00) (= rd (zero_ext 64 (extract 7 0 (as rn (bv 128))))))
- (=> (= idx #x01) (= rd (zero_ext 64 (extract 15 8 (as rn (bv 128))))))
- (=> (= idx #x02) (= rd (zero_ext 64 (extract 23 16 (as rn (bv 128))))))
- (=> (= idx #x03) (= rd (zero_ext 64 (extract 31 24 (as rn (bv 128))))))
- (=> (= idx #x04) (= rd (zero_ext 64 (extract 39 32 (as rn (bv 128))))))
- (=> (= idx #x05) (= rd (zero_ext 64 (extract 47 40 (as rn (bv 128))))))
- (=> (= idx #x06) (= rd (zero_ext 64 (extract 55 48 (as rn (bv 128))))))
- (=> (= idx #x07) (= rd (zero_ext 64 (extract 63 56 (as rn (bv 128))))))
- (=> (= idx #x08) (= rd (zero_ext 64 (extract 71 64 (as rn (bv 128))))))
- (=> (= idx #x09) (= rd (zero_ext 64 (extract 79 72 (as rn (bv 128))))))
- (=> (= idx #x0a) (= rd (zero_ext 64 (extract 87 80 (as rn (bv 128))))))
- (=> (= idx #x0b) (= rd (zero_ext 64 (extract 95 88 (as rn (bv 128))))))
- (=> (= idx #x0c) (= rd (zero_ext 64 (extract 103 96 (as rn (bv 128))))))
- (=> (= idx #x0d) (= rd (zero_ext 64 (extract 111 104 (as rn (bv 128))))))
- (=> (= idx #x0e) (= rd (zero_ext 64 (extract 119 112 (as rn (bv 128))))))
- (=> (= idx #x0f) (= rd (zero_ext 64 (extract 127 120 (as rn (bv 128))))))
+ (=> (= idx #x00) (= rd (zero_ext 64 (extract 7 0 (as rn (bv 64))))))
+ (=> (= idx #x01) (= rd (zero_ext 64 (extract 15 8 (as rn (bv 64))))))
+ (=> (= idx #x02) (= rd (zero_ext 64 (extract 23 16 (as rn (bv 64))))))
+ (=> (= idx #x03) (= rd (zero_ext 64 (extract 31 24 (as rn (bv 64))))))
+ (=> (= idx #x04) (= rd (zero_ext 64 (extract 39 32 (as rn (bv 64))))))
+ (=> (= idx #x05) (= rd (zero_ext 64 (extract 47 40 (as rn (bv 64))))))
+ (=> (= idx #x06) (= rd (zero_ext 64 (extract 55 48 (as rn (bv 64))))))
+ (=> (= idx #x07) (= rd (zero_ext 64 (extract 63 56 (as rn (bv 64))))))
)
)
)
@@ -58,29 +39,10 @@
(require
(match
size
- ((Size64) (or (= idx #x00) (= idx #x01)))
- ((Size32) (or (= idx #x00) (= idx #x01) (= idx #x02) (= idx #x03)))
- ((Size16) (or (= idx #x00) (= idx #x01) (= idx #x02) (= idx #x03) (= idx #x04) (= idx #x05) (= idx #x06) (= idx #x07)))
- ((Size8)
- (or
- (= idx #x00)
- (= idx #x01)
- (= idx #x02)
- (= idx #x03)
- (= idx #x04)
- (= idx #x05)
- (= idx #x06)
- (= idx #x07)
- (= idx #x08)
- (= idx #x09)
- (= idx #x0a)
- (= idx #x0b)
- (= idx #x0c)
- (= idx #x0d)
- (= idx #x0e)
- (= idx #x0f)
- )
- )
+ ((Size64) (= idx #x00))
+ ((Size32) (or (= idx #x00) (= idx #x01)))
+ ((Size16) (or (= idx #x00) (= idx #x01) (= idx #x02) (= idx #x03)))
+ ((Size8) (or (= idx #x00) (= idx #x01) (= idx #x02) (= idx #x03) (= idx #x04) (= idx #x05) (= idx #x06) (= idx #x07)))
)
)
)
diff --git a/cranelift/codegen/src/isa/x64/inst.isle b/cranelift/codegen/src/isa/x64/inst.isle
index 7f611a7b9b..57639f6ff0 100644
--- a/cranelift/codegen/src/isa/x64/inst.isle
+++ b/cranelift/codegen/src/isa/x64/inst.isle
@@ -379,7 +379,8 @@
(spec (RegMemImm.Imm simm32) (provide
(= result
- (bvor (bvshl #b10 62)(zero_ext 64 simm32)))))
+ (bvor
+ #x8000000000000000 (zero_ext 64 simm32)))))
(spec (RegMemImm.Mem addr) (provide
(= result
diff --git a/cranelift/codegen/src/prelude.isle b/cranelift/codegen/src/prelude.isle
index 2b779010ac..348c70a08c 100644
--- a/cranelift/codegen/src/prelude.isle
+++ b/cranelift/codegen/src/prelude.isle
@@ -770,7 +770,9 @@
(spec (i64_neg x) (provide (= result (bvneg x))))
(spec (u64_sub x y) (provide (= result (bvsub x y))))
-(spec (u64_shl x y) (provide (= result (bvshl x y))))
+(spec (u64_shl x y)
+ (match (bvult y #x00000040))
+ (provide (= result (bvshl x (zero_ext 64 y)))))
(spec (u64_eq x y) (provide (= result (= x y))))
(spec (u64_is_odd x) (provide (= result (= (conv_to 1 x) #b1))))
(spec (i64_checked_neg x)
@@ -801,7 +803,7 @@
(spec (u64_wrapping_shl x y)
(provide (= result
(bvshl x
- (zero_ext 32 (bvand y #x0000003f)))))) ; shift by y mod 64
+ (zero_ext 64 (bvand y #x0000003f)))))) ; shift by y mod 64
(spec (u64_wrapping_sub x y) (provide (= result (bvsub x y))))
(spec (u64_wrapping_add x y) (provide (= result (bvadd x y))))
(spec (u64_gt x y) (provide (bvugt x y)))
diff --git a/cranelift/isle/veri/isaspec/src/instructions.rs b/cranelift/isle/veri/isaspec/src/instructions.rs
index 5e1e25bc74..6c184abc5e 100644
--- a/cranelift/isle/veri/isaspec/src/instructions.rs
+++ b/cranelift/isle/veri/isaspec/src/instructions.rs
@@ -2076,9 +2076,18 @@ fn define_mov_from_vec() -> SpecConfig {
aarch64::gpreg(4),
Mapping::require(spec_var("rd".to_string())),
);
+ // The source is a 128-bit vector register, but the ISLE `rn` field is
+ // declared `Reg`, which is modelled as `(bv 64)`. Qualifying it as 128 bits
+ // contradicts that model, so the spec cannot be type checked; widening it
+ // instead would need a distinct vector register type, as x64 has for `Xmm`.
+ //
+ // Until then this specification covers only the lanes lying within the low
+ // 64 bits of the register. That is incomplete rather than unsound: reads of
+ // a higher lane are excluded by the `require` clause, so a lowering that
+ // needs one will fail to verify.
mappings.reads.insert(
aarch64::vreg(5),
- Mapping::require(spec_as_bit_vector_width(spec_var("rn".to_string()), 128)),
+ Mapping::require(spec_as_bit_vector_width(spec_var("rn".to_string()), 64)),
);
SpecConfig {
@@ -2091,7 +2100,8 @@ fn define_mov_from_vec() -> SpecConfig {
.iter()
.rev()
.map(|size| {
- let lanes = 128 / size.ty().bits();
+ // Lanes within the low 64 bits only; see above.
+ let lanes = 64 / size.ty().bits();
Arm {
variant: format!("{size:?}"),
args: Vec::new(),
diff --git a/cranelift/isle/veri/veri/filetests/spec_conflict/mismatched_shift_width.isle b/cranelift/isle/veri/veri/filetests/spec_conflict/mismatched_shift_width.isle
new file mode 100644
index 0000000000..142dd5a81e
--- /dev/null
+++ b/cranelift/isle/veri/veri/filetests/spec_conflict/mismatched_shift_width.isle
@@ -0,0 +1,29 @@
+; Regression test for silently unverified expansions.
+;
+; The spec below is ill-typed: type inference rejects every candidate
+; instantiation, so no query ever reaches the solver.
+
+; 8-bit value type
+(type Value (primitive Value))
+(model Value (type (bv 8)))
+
+; Top-level test term asserts equality
+(decl test (Value) Value)
+(spec (test arg) (provide (= result arg)))
+
+; Shift left.
+(decl shl (Value Value) Value)
+(extern extractor shl shl)
+(spec (shl x y) (provide (= result (bvshl x y))))
+
+; Shift left, with the amount widened first.
+;
+; The width operand of `zero_ext` is the *result* width, so the shift amount
+; here is 16 bits wide, while `bvshl` requires it to match the 8-bit value being
+; shifted. Both are bit vectors, so this is a width conflict between compatible
+; types: type inference reports it as inapplicable rather than a type error.
+(decl shl_ext (Value Value) Value)
+(extern constructor shl_ext shl_ext)
+(spec (shl_ext x y) (provide (= result (bvshl x (zero_ext 16 y)))))
+
+(rule test (test (shl x y)) (shl_ext x y))
diff --git a/cranelift/isle/veri/veri/src/lib.rs b/cranelift/isle/veri/veri/src/lib.rs
index 1d58ca862f..62bed12b60 100644
--- a/cranelift/isle/veri/veri/src/lib.rs
+++ b/cranelift/isle/veri/veri/src/lib.rs
@@ -25,6 +25,7 @@ pub mod reachability;
pub mod runner;
pub mod solver;
pub mod spec;
+pub mod spec_check;
pub mod trie;
pub mod type_inference;
pub mod types;
diff --git a/cranelift/isle/veri/veri/src/runner.rs b/cranelift/isle/veri/veri/src/runner.rs
index aa5dff822e..2819a3167c 100644
--- a/cranelift/isle/veri/veri/src/runner.rs
+++ b/cranelift/isle/veri/veri/src/runner.rs
@@ -23,6 +23,7 @@ use crate::{
expand::{Chaining, Expander, Expansion},
program::Program,
solver::{Applicability, Dialect, Solver, Verification},
+ spec_check,
type_inference::{self, Assignment, Choice, type_constraint_system},
veri::Conditions,
};
@@ -295,6 +296,9 @@ pub struct ExpansionReport {
pub solver: String,
/// Count of type instantiations that failed at type inference.
pub failed_type_inference: usize,
+ /// Count of type instantiations that type checked but were not sent to the
+ /// solver, because the solver was skipped.
+ pub skipped_type_instantiations: usize,
/// Solver reports from type instantiations.
pub type_instantiations: Vec<TypeInstantationReport>,
pub duration: Duration,
@@ -344,6 +348,7 @@ impl ExpansionReport {
tags,
solver: Default::default(),
failed_type_inference: 0,
+ skipped_type_instantiations: 0,
type_instantiations: Vec::new(),
duration: Default::default(),
})
@@ -443,6 +448,7 @@ pub struct RunSummary {
/// instantiation, so no query reached the solver. Only populated under
/// `--debug`, and only reported when populated.
pub no_instantiations: Option<usize>,
+ pub spec_conflicts: usize,
pub applicable: usize,
pub success: usize,
pub failure: usize,
@@ -466,6 +472,27 @@ impl RunSummary {
}
}
+#[derive(Debug, Clone)]
+pub struct RunFailure {
+ pub summary: RunSummary,
+ pub verification_failures: usize,
+ pub expansion_errors: usize,
+}
+
+impl std::fmt::Display for RunFailure {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(
+ f,
+ "verification failures: {failures}, expansion errors: {errors}, spec type conflicts: {conflicts}",
+ failures = self.verification_failures,
+ errors = self.expansion_errors,
+ conflicts = self.summary.spec_conflicts,
+ )
+ }
+}
+
+impl std::error::Error for RunFailure {}
+
#[derive(Serialize)]
pub struct Report {
build_profile: String,
@@ -486,6 +513,12 @@ pub struct Runner {
prog: Program,
term_rule_sets: HashMap<TermId, RuleSet>,
+ /// Type check findings for every spec in the program, computed once at
+ /// construction. The check needs only the parsed specs, so it does not
+ /// depend on the expansion scope: a spec that no configuration reaches is
+ /// covered the same as one that every configuration does.
+ spec_findings: Vec<spec_check::Finding>,
+
/// Optional single root term to scope expansion to. If `None`, expansion is
/// seeded from every term that has rules (all paths from all roots).
root_term: Option<String>,
@@ -507,9 +540,11 @@ impl Runner {
let expand_internal_extractors = false;
let prog = Program::from_files(inputs, expand_internal_extractors)?;
let term_rule_sets: HashMap<_, _> = prog.build_trie()?.into_iter().collect();
+ let spec_findings = check_all_specs(&prog);
Ok(Self {
prog,
term_rule_sets,
+ spec_findings,
root_term: None,
filters: Vec::new(),
default_solver_backend: SolverBackend::CVC5,
@@ -680,6 +715,25 @@ impl Runner {
let expansion_terms: Vec<Vec<TermId>> =
expansions.iter().map(|e| e.terms(&self.prog)).collect();
+ // Report the spec type conflicts found when the runner was built,
+ // before verifying anything.
+ let spec_conflicts = self.report_spec_findings();
+ if spec_conflicts > 0 {
+ let summary = RunSummary {
+ total_expansions: expansions.len(),
+ in_scope: included.iter().filter(|&&b| b).count(),
+ spec_conflicts,
+ ..Default::default()
+ };
+ summary.print();
+ return Err(RunFailure {
+ summary,
+ verification_failures: 0,
+ expansion_errors: 0,
+ }
+ .into());
+ }
+
// Set of "live" root terms: those reachable from a genuine top-level
// root via *included* expansion chains. An expansion error whose root
// term is not live is only reachable to the right of an excluded
@@ -892,9 +946,14 @@ impl Runner {
total_expansions,
in_scope,
no_instantiations,
+ spec_conflicts,
..Default::default()
};
for report in &expansion_reports {
+ // Instantiations that type checked but never reached the solver
+ // still count towards the total, so the count is meaningful with
+ // `--skip-solver`.
+ summary.total_instantiations += report.skipped_type_instantiations;
for instantiation in &report.type_instantiations {
summary.total_instantiations += 1;
match instantiation.verify.verdict {
@@ -946,16 +1005,16 @@ impl Runner {
if let Some(cache) = &self.cache {
cache.print_stats();
}
-
- // Verification failures and un-processable expansions are both overall
- // errors so that callers (the `veri` binary and tests) observe them via
- // the returned `Result`.
- if !verification_failures.is_empty() || !errors.is_empty() {
- bail!(
- "verification failures: {}, expansion errors: {}",
- verification_failures.len(),
- errors.len()
- );
+ // Verification failures, un-processable expansions and spec type
+ // conflicts are all overall errors, so that callers (the `veri` binary
+ // and tests) observe them via the returned `Result`.
+ if !verification_failures.is_empty() || !errors.is_empty() || spec_conflicts > 0 {
+ return Err(RunFailure {
+ summary,
+ verification_failures: verification_failures.len(),
+ expansion_errors: errors.len(),
+ }
+ .into());
}
Ok(summary)
@@ -989,6 +1048,49 @@ impl Runner {
.collect()
}
+ /// Report the spec type check findings, returning the number of conflicts.
+ fn report_spec_findings(&self) -> usize {
+ let (conflicts, unbuildable): (Vec<_>, Vec<_>) = self
+ .spec_findings
+ .iter()
+ .partition(|finding| finding.kind == spec_check::FindingKind::Conflict);
+
+ // Specs whose conditions could not be built at all are logged but do
+ // not fail the run: a spec shared across ISLE compilations may
+ // legitimately not apply in one of them, and types that verification
+ // never needs have no model by design.
+ if !unbuildable.is_empty() {
+ let mut log = Self::open_log_file(self.log_dir.clone(), "spec_unchecked.out").ok();
+ for finding in &unbuildable {
+ let line = finding.line();
+ log::info!("spec unchecked: {line}");
+ if let Some(f) = log.as_mut() {
+ let _ = writeln!(f, "{line}");
+ }
+ }
+ log::info!(
+ "specs whose conditions could not be built: {n}",
+ n = unbuildable.len()
+ );
+ }
+
+ if conflicts.is_empty() {
+ return 0;
+ }
+
+ let mut log = Self::open_log_file(self.log_dir.clone(), "spec_conflicts.out").ok();
+ eprintln!("=== SPEC TYPE CONFLICTS ({n}) ===", n = conflicts.len());
+ for conflict in &conflicts {
+ let line = conflict.line();
+ eprintln!("SPEC CONFLICT {line}");
+ if let Some(f) = log.as_mut() {
+ let _ = writeln!(f, "{line}");
+ }
+ }
+ log::warn!("spec type conflicts: {n}", n = conflicts.len());
+ conflicts.len()
+ }
+
fn should_verify(&self, expansion: &Expansion) -> Result<bool> {
// Include by default; each matching filter overrides the verdict, so
// the last matching filter wins. An `include` filter can therefore
@@ -1155,6 +1257,7 @@ impl Runner {
// Verify.
if self.skip_solver {
log::debug!("Skipping solver");
+ report.skipped_type_instantiations += 1;
continue;
}
@@ -1353,6 +1456,20 @@ impl Runner {
}
}
+/// Type check every term that has a spec, against nothing but its own declared
+/// types.
+///
+/// Run once for each program, as it is built: the check needs the parsed specs
+/// and nothing else -- no expansion, no solver -- so scoping it to the terms a
+/// particular run reaches would only lose coverage. A term used solely by
+/// another ISLE compilation, or by rules that this run filters out, is checked
+/// all the same.
+fn check_all_specs(prog: &Program) -> Vec<spec_check::Finding> {
+ let terms: BTreeSet<TermId> = prog.specenv.term_spec.keys().copied().collect();
+ log::info!("spec check: {total} specified terms", total = terms.len());
+ spec_check::check(prog, &terms)
+}
+
/// Compute the set of "live" root terms.
///
/// A term is live if it is reachable from a genuine top-level root (one never
diff --git a/cranelift/isle/veri/veri/src/spec_check.rs b/cranelift/isle/veri/veri/src/spec_check.rs
new file mode 100644
index 0000000000..9b0a770b5a
--- /dev/null
+++ b/cranelift/isle/veri/veri/src/spec_check.rs
@@ -0,0 +1,197 @@
+//! Type checking of term specifications in isolation, to help identify
+//! vacuously false type instantiations.
+//!
+//! Each term is checked on its own, against nothing but its own declared
+//! types. The check needs no solver, so it is cheap enough to run as part of
+//! every verification run, and to run as a test.
+
+use std::collections::{BTreeMap, BTreeSet, HashSet};
+
+use cranelift_isle::sema::TermId;
+
+use crate::{
+ program::Program,
+ spec::Signature,
+ type_inference::{self, Choice, type_constraint_system},
+ veri::{Conditions, TermKind},
+};
+
+/// What went wrong when checking one term.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum FindingKind {
+ Conflict,
+ Build,
+}
+
+/// A term whose spec does not type check on its own.
+#[derive(Debug, Clone)]
+pub struct Finding {
+ pub kind: FindingKind,
+ pub term: String,
+ pub signature: Option<String>,
+ pub message: String,
+}
+
+impl Finding {
+ pub fn line(&self) -> String {
+ match &self.signature {
+ Some(signature) => format!(
+ "{term}\t{signature}\t{message}",
+ term = self.term,
+ message = self.message
+ ),
+ None => format!(
+ "{term}\t{message}",
+ term = self.term,
+ message = self.message
+ ),
+ }
+ }
+}
+
+/// Type check each of `terms` against its own declared types.
+///
+/// Terms with no spec are skipped, so the caller may pass the whole set of
+/// terms a run reaches without filtering it first.
+///
+/// A term with an `(instantiate ...)` set gets the stronger check: those
+/// signatures are concrete, and each becomes one branch of the constraint
+/// system, so every one of them must be solvable. Without one, the argument
+/// types come from models alone and are often polymorphic, leaving inference
+/// underconstrained -- which is not an error, but does mean only outright
+/// conflicts are detectable.
+pub fn check(prog: &Program, terms: &BTreeSet<TermId>) -> Vec<Finding> {
+ let mut findings = Vec::new();
+ for &term in terms {
+ if !prog.specenv.has_spec(term) {
+ continue;
+ }
+ findings.extend(check_term(prog, term));
+ }
+ findings
+}
+
+fn check_term(prog: &Program, term: TermId) -> Vec<Finding> {
+ let term_name = prog.term_name(term).to_string();
+
+ // A term declared with both a constructor and an extractor is used in both
+ // directions, and its spec need only make sense in one of them: a `match`
+ // clause over `result`, for instance, is well formed for an extractor,
+ // where the result is an input, and not for a constructor. Report findings
+ // only when no direction checks out, and then the first direction's, since
+ // there is no basis for preferring one set of diagnostics over the other.
+ let term_data = prog.term(term);
+ let mut kinds = Vec::new();
+ if term_data.has_constructor() {
+ kinds.push(TermKind::Constructor);
+ }
+ if term_data.has_extractor() {
+ kinds.push(TermKind::Extractor);
+ }
+
+ let mut first: Option<Vec<Finding>> = None;
+ for kind in kinds {
+ let findings = check_term_kind(prog, term, kind, &term_name);
+ if findings.is_empty() {
+ return Vec::new();
+ }
+ first.get_or_insert(findings);
+ }
+ first.unwrap_or_default()
+}
+
+fn check_term_kind(prog: &Program, term: TermId, kind: TermKind, term_name: &str) -> Vec<Finding> {
+ let term_name = term_name.to_string();
+ let conditions = match Conditions::from_term(term, kind, prog) {
+ Ok(conditions) => conditions,
+ Err(err) => {
+ return vec![Finding {
+ kind: FindingKind::Build,
+ term: term_name,
+ signature: None,
+ message: format!("{err:#}"),
+ }];
+ }
+ };
+
+ // The constraint system branches over the term's declared instantiations,
+ // so a solution normally carries the signature it belongs to and failures
+ // can be attributed to individual signatures. A conflict found before any
+ // branching carries no signature: it comes from the constraints every
+ // candidate shares, so no choice of signature can rescue it.
+ let system = type_constraint_system(&conditions);
+ let solutions = type_inference::Solver::new().solve(&system);
+
+ let mut any_solved = false;
+ let mut solved: HashSet<String> = HashSet::new();
+ let mut failures: BTreeMap<String, String> = BTreeMap::new();
+ let mut common_failure: Option<String> = None;
+ for solution in &solutions {
+ let signature = solution
+ .choices
+ .iter()
+ .find_map(|choice| match choice {
+ Choice::TermInstantiation(choice_term, sig) if *choice_term == term => Some(sig),
+ _ => None,
+ })
+ .map(signature_string);
+
+ match &solution.status {
+ // Underconstrained is not a failure here.
+ type_inference::Status::Solved | type_inference::Status::Underconstrained => {
+ any_solved = true;
+ if let Some(signature) = signature {
+ solved.insert(signature);
+ }
+ }
+ type_inference::Status::Inapplicable(conflict)
+ | type_inference::Status::TypeError(conflict) => {
+ let diagnostic = conflict.diagnostic(&conditions, &prog.files);
+ match signature {
+ Some(signature) => {
+ failures.entry(signature).or_insert(diagnostic);
+ }
+ None => {
+ common_failure.get_or_insert(diagnostic);
+ }
+ }
+ }
+ }
+ }
+
+ if !any_solved && let Some(message) = common_failure {
+ return vec![Finding {
+ kind: FindingKind::Conflict,
+ term: term_name,
+ signature: None,
+ message,
+ }];
+ }
+
+ // A signature that appears in a failing branch may also appear in a solved
+ // one, since other calls in the same conditions fork independently. Only
+ // report the ones with no solution at all.
+ failures
+ .into_iter()
+ .filter(|(signature, _)| !solved.contains(signature))
+ .map(|(signature, message)| Finding {
+ kind: FindingKind::Conflict,
+ term: term_name.clone(),
+ signature: Some(signature),
+ message,
+ })
+ .collect()
+}
+
+fn signature_string(signature: &Signature) -> String {
+ format!(
+ "(args {args}) (ret {ret})",
+ args = signature
+ .args
+ .iter()
+ .map(ToString::to_string)
+ .collect::<Vec<_>>()
+ .join(" "),
+ ret = signature.ret,
+ )
+}
diff --git a/cranelift/isle/veri/veri/src/types.rs b/cranelift/isle/veri/veri/src/types.rs
index 925c997597..3cb34a3df4 100644
--- a/cranelift/isle/veri/veri/src/types.rs
+++ b/cranelift/isle/veri/veri/src/types.rs
@@ -332,7 +332,7 @@ impl Compound {
pub fn named_from_isle(ty: &sema::Type, tyenv: &TypeEnv) -> Self {
match ty {
sema::Type::Builtin(BuiltinType::Bool) => Self::Primitive(Type::Bool),
- sema::Type::Builtin(b) => Self::Primitive(Type::BitVector(Width::Bits(b.to_usize()))),
+ sema::Type::Builtin(b) => Self::Named(Ident(b.name().to_string(), Pos::default())),
_ => Self::Named(Ident(
ty.name(tyenv).to_string(),
ty.pos().expect("expected position"),
diff --git a/cranelift/isle/veri/veri/src/veri.rs b/cranelift/isle/veri/veri/src/veri.rs
index b04bf346cc..ed821bc45a 100644
--- a/cranelift/isle/veri/veri/src/veri.rs
+++ b/cranelift/isle/veri/veri/src/veri.rs
@@ -782,10 +782,21 @@ impl Conditions {
prog: &Program,
excluded_tags: &HashSet<String>,
) -> Result<Self> {
- let builder = ConditionsBuilder::new(expansion, prog, excluded_tags);
+ let builder = ConditionsBuilder::new(Some(expansion), prog, excluded_tags);
builder.build()
}
+ /// Verification conditions for a single term's spec, independent of any
+ /// expansion that might reach it.
+ ///
+ /// Checked against every declared instantiation: with no expansion there is
+ /// no run whose excluded tags could narrow the set.
+ pub fn from_term(term: TermId, kind: TermKind, prog: &Program) -> Result<Self> {
+ let excluded_tags = HashSet::new();
+ let builder = ConditionsBuilder::new(None, prog, &excluded_tags);
+ builder.build_term(term, kind)
+ }
+
pub fn pretty_print(&self, prog: &Program) {
println!("conditions {{");
@@ -939,11 +950,21 @@ impl Conditions {
}
}
-enum TermKind {
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+pub enum TermKind {
Constructor,
Extractor,
}
+fn is_fallible(term: &sema::Term, tyenv: &sema::TypeEnv, kind: TermKind) -> bool {
+ match kind {
+ TermKind::Constructor => term.is_partial(),
+ TermKind::Extractor => term
+ .extractor_sig(tyenv)
+ .is_some_and(|sig| sig.ret_kind == sema::ReturnKind::Option),
+ }
+}
+
#[derive(Copy, Clone)]
enum Invocation {
Caller,
@@ -996,11 +1017,14 @@ impl Variables {
}
struct ConditionsBuilder<'a> {
- expansion: &'a Expansion,
+ /// Expansion under verification, absent when building the conditions for a
+ /// single term in isolation.
+ expansion: Option<&'a Expansion>,
prog: &'a Program,
/// Tags excluded by this run, gating tagged instantiation sets.
excluded_tags: &'a HashSet<String>,
-
+ /// Term whose conditions are under construction, for diagnostics.
+ context_term: Option<TermId>,
state_modification_conds: HashMap<String, Vec<ExprId>>,
binding_value: HashMap<BindingId, Symbolic>,
expr_map: HashMap<Expr, ExprId>,
@@ -1010,7 +1034,7 @@ struct ConditionsBuilder<'a> {
impl<'a> ConditionsBuilder<'a> {
fn new(
- expansion: &'a Expansion,
+ expansion: Option<&'a Expansion>,
prog: &'a Program,
excluded_tags: &'a HashSet<String>,
) -> Self {
@@ -1018,6 +1042,7 @@ impl<'a> ConditionsBuilder<'a> {
expansion,
prog,
excluded_tags,
+ context_term: expansion.map(|e| e.term),
state_modification_conds: HashMap::new(),
binding_value: HashMap::new(),
expr_map: HashMap::new(),
@@ -1026,14 +1051,24 @@ impl<'a> ConditionsBuilder<'a> {
}
}
+ /// The expansion under verification.
+ ///
+ /// Panics when the conditions are for a single term in isolation, which has
+ /// no surrounding expansion. Only the `build` path reaches these uses.
+ fn expansion(&self) -> &'a Expansion {
+ self.expansion.expect("expansion should be set")
+ }
+
fn build(mut self) -> Result<Conditions> {
+ let expansion = self.expansion();
+
// State initialization.
for state in &self.prog.specenv.state {
self.init_state(state)?;
}
// Bindings.
- for (i, binding) in self.expansion.bindings.iter().enumerate() {
+ for (i, binding) in expansion.bindings.iter().enumerate() {
if let Some(binding) = binding {
self.add_binding(i.try_into().unwrap(), binding)?;
}
@@ -1041,20 +1076,20 @@ impl<'a> ConditionsBuilder<'a> {
// Callee contract for the term under expansion.
self.constructor(
- self.expansion.result,
- self.expansion.term,
- &self.expansion.parameters,
+ expansion.result,
+ expansion.term,
+ &expansion.parameters,
Invocation::Callee,
)?;
// Constraints.
- for constrain in &self.expansion.constraints {
+ for constrain in &expansion.constraints {
let holds = self.constrain(constrain)?;
self.conditions.assumptions.push(holds);
}
// Equals.
- for (a, b) in self.expansion.equalities() {
+ for (a, b) in expansion.equalities() {
let eq = self.bindings_equal(a, b)?;
self.conditions.assumptions.push(eq);
}
@@ -1070,6 +1105,57 @@ impl<'a> ConditionsBuilder<'a> {
Ok(self.conditions)
}
+ /// Build conditions for a single term's spec, applied to freshly allocated
+ /// arguments and return value of its declared types.
+ fn build_term(mut self, term: TermId, kind: TermKind) -> Result<Conditions> {
+ let prog = self.prog;
+ self.context_term = Some(term);
+
+ // State initialization.
+ for state in &prog.specenv.state {
+ self.init_state(state)?;
+ }
+
+ // Allocate a value per declared argument type, and one for the return.
+ let term_data = prog.term(term);
+ let mut args = Vec::new();
+ for (i, ty) in term_data.arg_tys.iter().enumerate() {
+ args.push(self.alloc_model(*ty, format!("arg{i}"))?);
+ }
+ let ret = self.alloc_model(term_data.ret_ty, "ret".to_string())?;
+
+ // A fallible term's outputs are wrapped in an option whose
+ // discriminant decides whether the spec's `provides` apply, and a spec
+ // with `match` clauses requires that domain. Which values are the
+ // outputs depends on the term kind: a constructor produces its return
+ // value, whereas an extractor consumes it and produces the arguments.
+ let outputs = match kind {
+ TermKind::Constructor => ret.clone(),
+ TermKind::Extractor => Symbolic::Tuple(args.clone()),
+ };
+ let outputs = if is_fallible(term_data, &prog.tyenv, kind) {
+ self.alloc_option(outputs)
+ } else {
+ outputs
+ };
+ let (domain, outputs) = Domain::from_return_value(&outputs);
+
+ // Evaluate the spec as the callee, the direction in which its
+ // `provides` are asserted.
+ let (args, result) = match kind {
+ TermKind::Constructor => (args.as_slice(), outputs.clone()),
+ TermKind::Extractor => (outputs.elements(), ret),
+ };
+ self.call(term, kind, args, result, Invocation::Callee, domain)?;
+
+ // State defaults.
+ for state in &prog.specenv.state {
+ self.state_default(state)?;
+ }
+
+ Ok(self.conditions)
+ }
+
fn init_state(&mut self, state: &State) -> Result<()> {
let name = &state.name.0;
let value = self.alloc_value(&state.ty, name.clone())?;
@@ -1115,7 +1201,7 @@ impl<'a> ConditionsBuilder<'a> {
// Ensure dependencies have been added.
for source in binding.sources() {
let source_binding = self
- .expansion
+ .expansion()
.binding(*source)
.expect("source binding should be defined");
self.add_binding(*source, source_binding)?;
@@ -1465,7 +1551,7 @@ impl<'a> ConditionsBuilder<'a> {
// Lookup enum type via corresponding constriant,
let tys: Vec<_> = self
- .expansion
+ .expansion()
.constraints
.iter()
.flat_map(|c| match c {
@@ -1551,7 +1637,7 @@ impl<'a> ConditionsBuilder<'a> {
// Lookup the struct type via the corresponding constraint on the source.
let tys: Vec<_> = self
- .expansion
+ .expansion()
.constraints
.iter()
.flat_map(|c| match c {
@@ -2342,14 +2428,24 @@ impl<'a> ConditionsBuilder<'a> {
/// Determine the type of the given binding in the context of the
/// [Expansion] we are constructing verification conditions for.
fn binding_type(&self, binding: &Binding) -> BindingType {
+ let expansion = self.expansion();
binding_type(
binding,
- self.expansion.term,
+ expansion.term,
self.prog,
- |binding_id: BindingId| self.expansion.bindings[binding_id.index()].clone().unwrap(),
+ |binding_id: BindingId| expansion.bindings[binding_id.index()].clone().unwrap(),
)
}
+ /// Wrap a fallible term's outputs in an option with a fresh discriminant.
+ fn alloc_option(&mut self, inner: Symbolic) -> Symbolic {
+ let some = self.alloc_variable(Type::Bool, Variable::component_name("ret", "some"));
+ Symbolic::Option(SymbolicOption {
+ some,
+ inner: Box::new(inner),
+ })
+ }
+
fn alloc_binding(&mut self, binding_type: &BindingType, name: String) -> Result<Symbolic> {
match binding_type {
BindingType::Base(type_id) => self.alloc_model(*type_id, name),
@@ -2458,7 +2554,9 @@ impl<'a> ConditionsBuilder<'a> {
fn alloc_model(&mut self, type_id: TypeId, name: String) -> Result<Symbolic> {
let type_name = self.prog.type_name(type_id);
- let term_name = self.prog.term_name(self.expansion.term);
+ let term_name = self
+ .prog
+ .term_name(self.context_term.expect("context term should be set"));
let ty = self
.prog
.specenv
diff --git a/cranelift/isle/veri/veri/tests/filetests.rs b/cranelift/isle/veri/veri/tests/filetests.rs
index fbbe4ad0d7..98cf79071f 100644
--- a/cranelift/isle/veri/veri/tests/filetests.rs
+++ b/cranelift/isle/veri/veri/tests/filetests.rs
@@ -1,27 +1,69 @@
use std::path::PathBuf;
-use cranelift_isle_veri::runner::Runner;
+use cranelift_isle_veri::runner::{RunFailure, RunSummary, Runner};
use cranelift_isle_veri_test_macros::file_tests;
use tempfile::tempdir;
-#[file_tests(path = "filetests/pass", ext = "isle")]
-fn pass(test_file: &str) {
+/// Run the verifier over a single test file, rooted at the `test` term.
+///
+/// Returns the run summary alongside the overall result, so that a test can
+/// assert on the summary of a run that failed.
+fn run(test_file: &str) -> anyhow::Result<(RunSummary, anyhow::Result<()>)> {
let inputs = vec![PathBuf::from(test_file)];
let mut runner = Runner::from_files(&inputs).expect("should be able to create runner");
let temp_dir = tempdir().expect("should be able to create temporary log directory");
runner.set_log_dir(temp_dir.path().join("log"));
runner.include_first_rule_named();
runner.set_root_term("test");
- runner.run().expect("verification should pass");
+ match runner.run() {
+ Ok(summary) => Ok((summary, Ok(()))),
+ Err(err) => {
+ let summary = err.downcast_ref::<RunFailure>().map(|f| f.summary.clone());
+ match summary {
+ Some(summary) => Ok((summary, Err(err))),
+ None => Err(err),
+ }
+ }
+ }
+}
+
+#[file_tests(path = "filetests/pass", ext = "isle")]
+fn pass(test_file: &str) {
+ let (summary, result) = run(test_file).expect("run should complete without errors");
+ result.expect("verification should pass");
+ assert!(
+ summary.success > 0,
+ "expected at least one successful verification, got summary {summary:?}"
+ );
+ assert_eq!(
+ summary.spec_conflicts, 0,
+ "expected no spec type conflicts, got summary {summary:?}"
+ );
}
#[file_tests(path = "filetests/broken", ext = "isle")]
fn broken(test_file: &str) {
- let inputs = vec![PathBuf::from(test_file)];
- let mut runner = Runner::from_files(&inputs).expect("should be able to create runner");
- let temp_dir = tempdir().expect("should be able to create temporary log directory");
- runner.set_log_dir(temp_dir.path().join("log"));
- runner.include_first_rule_named();
- runner.set_root_term("test");
- runner.run().expect_err("verification should fail");
+ let (_, result) = run(test_file).expect("run should complete without errors");
+ result.expect_err("verification should fail");
+}
+
+/// Test files containing a term whose spec does not type check on its own.
+#[file_tests(path = "filetests/spec_conflict", ext = "isle")]
+fn spec_conflict(test_file: &str) {
+ let (summary, result) = run(test_file).expect("run should complete without errors");
+ result.expect_err("the spec type conflict should fail the run");
+ assert!(
+ summary.spec_conflicts > 0,
+ "expected the spec check to report the conflict directly, rather than only as a missing instantiation, got summary {summary:?}"
+ );
+ // The run must stop at the check, before verifying anything: a spec that
+ // cannot be instantiated would otherwise silently shrink the query set.
+ assert_eq!(
+ summary.total_instantiations, 0,
+ "expected the run to fail before verification, got summary {summary:?}"
+ );
+ assert_eq!(
+ summary.applicable, 0,
+ "expected the run to fail before verification, got summary {summary:?}"
+ );
}