Skip to content

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

ISLE verifier: reject rules whose RHS is ill-typed (#14964)

Yage Hu70521b31b93c
diff --git a/cranelift/codegen/src/isa/aarch64/inst.isle b/cranelift/codegen/src/isa/aarch64/inst.isle
index a9b0058a1c..0686a81cbe 100644
--- a/cranelift/codegen/src/isa/aarch64/inst.isle
+++ b/cranelift/codegen/src/isa/aarch64/inst.isle
@@ -2573,7 +2573,9 @@
(decl writable_link_reg () WritableReg)
(extern constructor writable_link_reg writable_link_reg)
-(spec (writable_zero_reg) (provide (= result #x00000000)))
+; The zero register is a write sink here: it is only used as the destination of
+; flag-setting instructions, so its value is not modeled.
+(spec (writable_zero_reg) (provide true))
(decl writable_zero_reg () WritableReg)
(extern constructor writable_zero_reg writable_zero_reg)
@@ -3987,21 +3989,22 @@
(attr trap_if_div_overflow (veri chain))
(decl trap_if_div_overflow (Type Reg Reg Reg) Reg)
(rule (trap_if_div_overflow ty xcheck x y)
- (let (
- ;; Check RHS is -1.
- (_ Unit (emit (MInst.AluRRImm12 (ALUOp.AddS) (operand_size ty) (writable_zero_reg) y (u8_into_imm12 1))))
-
- ;; Check LHS is min_value, by subtracting 1 from the possibly-shifted
- ;; value and branching if there is overflow.
- (_ Unit (emit (MInst.CCmpImm (size_from_ty ty)
- xcheck
- (u8_into_uimm5 1)
- (nzcv false false false false)
- (Cond.Eq))))
- (_ Unit (emit (MInst.TrapIf (cond_br_cond (Cond.Vs))
- (trap_code_integer_overflow))))
- )
- x))
+ ;; Check RHS is -1, then check LHS is min_value by subtracting 1 from the
+ ;; possibly-shifted value, and trap if there is overflow.
+ ;; The checks are chained through the flags, so the trap is conditioned on both.
+ ;; The trap defines no register: `x` is passed through unchanged.
+ (with_flags_reg
+ (ProducesFlags.ProducesFlagsTwiceSideEffect
+ (MInst.AluRRImm12 (ALUOp.AddS) (operand_size ty) (writable_zero_reg) y (u8_into_imm12 1))
+ (MInst.CCmpImm (size_from_ty ty)
+ xcheck
+ (u8_into_uimm5 1)
+ (nzcv false false false false)
+ (Cond.Eq)))
+ (ConsumesFlags.ConsumesFlagsReturnsReg
+ (MInst.TrapIf (cond_br_cond (Cond.Vs))
+ (trap_code_integer_overflow))
+ x)))
;; In the cases narrower than a register width, subtracting 1 from the
;; min_value will not cause overflow (e.g., I8's min_value of -128 stored in
@@ -6045,7 +6048,41 @@
(attr uadd_overflow (tag TODO))
(attr usub_overflow (tag TODO))
(attr vhigh_bits (tag TODO))
-(attr with_flags (tag TODO))
+;; `with_flags` is verified by its spec, but its rules are not: `ProducesFlags` and
+;; `ConsumesFlags` are modeled as structs, so the rules cannot match on their
+;; variants.
+(attr rule with_flags_paired_results (tag TODO))
+(attr rule with_flags_producer_result_only (tag TODO))
+(attr rule with_flags_producer_result_only_noop (tag TODO))
+(attr rule with_flags_producer_reg_noop (tag TODO))
+(attr rule with_flags_consumer_reg (tag TODO))
+(attr rule with_flags_opportunistic_def_consumer_reg (tag TODO))
+(attr rule with_flags_consumer_twice (tag TODO))
+(attr rule with_flags_opportunistic_def_consumer_twice (tag TODO))
+(attr rule with_flags_consumer_four (tag TODO))
+(attr rule with_flags_opportunistic_def_consumer_four (tag TODO))
+(attr rule with_flags_opportunistic_def2_consumer_reg (tag TODO))
+(attr rule with_flags_opportunistic_def2_consumer_twice (tag TODO))
+(attr rule with_flags_opportunistic_def2_consumer_four (tag TODO))
+(attr rule with_flags_producer_twice_reg (tag TODO))
+(attr rule with_flags_producer_twice_consumer_twice (tag TODO))
+(attr rule with_flags_producer_twice_consumer_four (tag TODO))
+;; Terms without specs, used by flag-consuming lowerings.
+(attr first_result (tag TODO))
+(attr fpu_csel (tag TODO))
+(attr ConsumesFlags.ConsumesFlagsTwiceReturnsValueRegs (tag TODO))
+(attr vany_true (tag TODO))
+(attr ashr_from_u64 (tag TODO))
+;; The lowerings below fail verification, or reach terms that do not have a spec
+;; yet. Previously they were excluded as a side effect of `with_flags` being
+;; `TODO`.
+(attr rule uextend_icmp_int (tag TODO))
+(attr rule icmp_int (tag TODO))
+(attr rule umul_overflow_fits_in_16 (tag TODO))
+(attr rule umul_overflow_i32 (tag TODO))
+(attr rule umul_overflow_i64 (tag TODO))
+(attr rule smul_overflow_fits_in_16 (tag TODO))
+(attr rule smul_overflow_i32 (tag TODO))
(attr get_exception_handler_address (tag TODO))
(attr lo_reg (tag TODO))
(attr produces_flags_ignore (tag TODO))
diff --git a/cranelift/codegen/src/isa/aarch64/lower.isle b/cranelift/codegen/src/isa/aarch64/lower.isle
index 9fc11625c1..d03f1527fc 100644
--- a/cranelift/codegen/src/isa/aarch64/lower.isle
+++ b/cranelift/codegen/src/isa/aarch64/lower.isle
@@ -1334,7 +1334,7 @@
;;
;; This does not hold true for vector comparisons, which instead produce a
;; per-lane mask of either all ones or zeroes. rather than 0 or 1.
-(rule 1 (lower (uextend (fits_in_64 _) (icmp _ cond x @ (value_type (ty_int _)) y)))
+(rule uextend_icmp_int 1 (lower (uextend (fits_in_64 _) (icmp _ cond x @ (value_type (ty_int _)) y)))
(lower_cond_result_bool (emit_icmp cond x y)))
(rule 2 (lower (uextend (fits_in_64 _) (fcmp _ cond x @ (value_type $F16) y)))
(lower_cond_result_bool (emit_fcmp cond x y)))
@@ -2308,7 +2308,7 @@
(rm Reg (put_in_reg y)))
(vec_cmp rn rm in_ty cond)))
-(rule -2 (lower (icmp _ cond x @ (value_type (ty_int _)) y))
+(rule icmp_int -2 (lower (icmp _ cond x @ (value_type (ty_int _)) y))
(lower_cond_result_bool (emit_icmp cond x y)))
;; Helper to put a `CondResult` into a general register.
@@ -3259,7 +3259,7 @@
;; mul out, a_ext, b_ext
;; cmp out, out, uxt{b,h}
;; cset of, ne
-(rule 1 (lower (umul_overflow (fits_in_16 ty) a b))
+(rule umul_overflow_fits_in_16 1 (lower (umul_overflow (fits_in_16 ty) a b))
(let ((extend ExtendOp (lower_extend_op ty (ArgumentExtension.Uext)))
(a_uext Reg (put_in_reg_zext32 a))
@@ -3275,7 +3275,7 @@
;; umull out, a, b
;; cmp out, out, uxtw
;; cset of, ne
-(rule 2 (lower (umul_overflow $I32 a b))
+(rule umul_overflow_i32 2 (lower (umul_overflow $I32 a b))
(let (
(out Reg (umaddl a b (zero_reg)))
(out_of Reg (with_flags_reg
@@ -3289,7 +3289,7 @@
;; umulh tmp, a, b
;; cmp tmp, #0
;; cset of, ne
-(rule 2 (lower (umul_overflow $I64 a b))
+(rule umul_overflow_i64 2 (lower (umul_overflow $I64 a b))
(let (
(out Reg (madd $I64 a b (zero_reg)))
(tmp Reg (umulh $I64 a b))
@@ -3323,7 +3323,7 @@
;; mul out, a_ext, b_ext
;; cmp out, out, sxt{b,h}
;; cset of, ne
-(rule 1 (lower (smul_overflow (fits_in_16 ty) a b))
+(rule smul_overflow_fits_in_16 1 (lower (smul_overflow (fits_in_16 ty) a b))
(let ((extend ExtendOp (lower_extend_op ty (ArgumentExtension.Sext)))
(a_sext Reg (put_in_reg_sext32 a))
@@ -3339,7 +3339,7 @@
;; smull out, a, b
;; cmp out, out, sxtw
;; cset of, ne
-(rule 2 (lower (smul_overflow $I32 a b))
+(rule smul_overflow_i32 2 (lower (smul_overflow $I32 a b))
(let (
(out Reg (smaddl a b (zero_reg)))
(out_of Reg (with_flags_reg
diff --git a/cranelift/codegen/src/isa/aarch64/spec/primitives.isle b/cranelift/codegen/src/isa/aarch64/spec/primitives.isle
index 2d32cc4df5..c262bdf4ab 100644
--- a/cranelift/codegen/src/isa/aarch64/spec/primitives.isle
+++ b/cranelift/codegen/src/isa/aarch64/spec/primitives.isle
@@ -595,10 +595,14 @@
;; Use the SMTLIB conversion functions to accomplish the same behavior
(let
(
+ ;; The operand is converted at its full precision: it is widened to
+ ;; 64 bits and rounded to the destination width `N`. Narrowing it to
+ ;; `N` first would convert only the low bits of a 64-bit operand
+ ;; to a 32-bit float.
(result
(if unsigned
- (to_fp_unsigned N (conv_to N (zero_ext 64 op)))
- (to_fp N (conv_to N (sign_ext 64 op)))))
+ (to_fp_unsigned N (zero_ext 64 op))
+ (to_fp N (sign_ext 64 op))))
)
(if (and
;; fbits of 0 means we can use pure integer logic
diff --git a/cranelift/codegen/src/prelude_lower.isle b/cranelift/codegen/src/prelude_lower.isle
index 7ebedc6ff9..a439dc592e 100644
--- a/cranelift/codegen/src/prelude_lower.isle
+++ b/cranelift/codegen/src/prelude_lower.isle
@@ -276,7 +276,10 @@
(value_list_slice (value_slice_unwrap head1 (value_slice_unwrap head2 tail))))
;; Turn a `Writable<Reg>` into a `Reg` via `Writable::to_reg`.
-(spec (writable_reg_to_reg reg) (provide (= result reg)))
+;; `Reg` is modeled with 64 bits, but a temporary may be allocated with a wider
+;; type, for example a 128-bit vector register for a scalar floating-point
+;; value. Only the low bits are modeled in that case.
+(spec (writable_reg_to_reg reg) (provide (= result (conv_to (widthof result) reg))))
(decl pure writable_reg_to_reg (WritableReg) Reg)
(extern constructor writable_reg_to_reg writable_reg_to_reg)
diff --git a/cranelift/codegen/src/spec/inst_specs.isle b/cranelift/codegen/src/spec/inst_specs.isle
index 68c44141b8..654839e383 100644
--- a/cranelift/codegen/src/spec/inst_specs.isle
+++ b/cranelift/codegen/src/spec/inst_specs.isle
@@ -631,6 +631,8 @@
; Activate and set parameters of a CLIF load effect.
(macro (clif_load_activate clif_load size_bits p offset)
(and
+ ; The address is a scalar.
+ (= (lanesof p) 1)
(:active clif_load)
(= (:size_bits clif_load) size_bits)
(= (:addr clif_load) (effective_address! p offset))))
@@ -758,6 +760,9 @@
; Activate and set parameters of a CLIF store effect.
(macro (clif_store_activate clif_store value p offset)
(and
+ ; The address is a scalar.
+ (= (lanesof p) 1)
+
; Activate the CLIF store effect
(:active clif_store)
diff --git a/cranelift/isle/veri/veri/filetests/rhs_type_error/operand_widths_differ.isle b/cranelift/isle/veri/veri/filetests/rhs_type_error/operand_widths_differ.isle
new file mode 100644
index 0000000000..550d06b1a5
--- /dev/null
+++ b/cranelift/isle/veri/veri/filetests/rhs_type_error/operand_widths_differ.isle
@@ -0,0 +1,43 @@
+; Regression test for a right-hand side that is ill-typed for some of the type
+; assignments that the left-hand side accepts.
+;
+; The rule fires whenever its left-hand side matches. Here both `ext` results
+; are 32 bits wide, but nothing ties the widths of their operands together: `x`
+; may be 8 bits and `y` 16 bits.
+
+(type Value (primitive Value))
+(model Value (type (bv)))
+
+(decl test (Value) Value)
+(spec (test arg) (provide (= result arg)))
+(instantiate test
+ ((args (bv 8)) (ret (bv 8)))
+)
+
+(decl ext (Value) Value)
+(extern extractor ext ext)
+(spec (ext x) (provide (= result (sign_ext 32 x))))
+(instantiate ext
+ ((args (bv 8)) (ret (bv 32)))
+ ((args (bv 16)) (ret (bv 32)))
+)
+
+(decl add32 (Value Value) Value)
+(extern extractor add32 add32)
+(spec (add32 x y) (provide (= result (bvadd x y))))
+(instantiate add32
+ ((args (bv 32) (bv 32)) (ret (bv 32)))
+)
+
+(decl low8 (Value) Value)
+(extern extractor low8 low8)
+(spec (low8 x) (provide (= result (extract 7 0 x))))
+(instantiate low8
+ ((args (bv 32)) (ret (bv 8)))
+)
+
+(decl add (Value Value) Value)
+(extern constructor add add)
+(spec (add x y) (provide (= result (bvadd x y))))
+
+(rule test (test (low8 (add32 (ext x) (ext y)))) (add x y))
diff --git a/cranelift/isle/veri/veri/src/expand.rs b/cranelift/isle/veri/veri/src/expand.rs
index 6dfb820b62..4795e3bd42 100644
--- a/cranelift/isle/veri/veri/src/expand.rs
+++ b/cranelift/isle/veri/veri/src/expand.rs
@@ -15,7 +15,7 @@ pub enum Constrain {
}
impl Constrain {
- fn bindings(&self) -> Vec<BindingId> {
+ pub fn bindings(&self) -> Vec<BindingId> {
match self {
Constrain::Match(binding_id, _) => vec![*binding_id],
Constrain::Equal(a, b) => vec![*a, *b],
diff --git a/cranelift/isle/veri/veri/src/runner.rs b/cranelift/isle/veri/veri/src/runner.rs
index 6542b87a85..01d7755983 100644
--- a/cranelift/isle/veri/veri/src/runner.rs
+++ b/cranelift/isle/veri/veri/src/runner.rs
@@ -1292,6 +1292,57 @@ impl Runner {
confict.reason.clone(),
));
}
+ type_inference::Status::RhsTypeError(conflict) => {
+ // The type assignment is only a problem if the left-hand
+ // side can actually match under it: value constraints on
+ // the left-hand side may rule it out. Type inference does
+ // not see those, so the solver is needed to decide whether
+ // they are satisfiable. Without it, we cannot tell, and
+ // skip the assignment as before.
+ let feasibility = if self.skip_solver {
+ None
+ } else {
+ let log_dir = log_dir.join(format!("{i:03}_lhs"));
+ Some(self.check_lhs_feasibility(
+ &conditions,
+ &solution.assignment,
+ solver_backend,
+ log_dir,
+ ))
+ };
+ match feasibility {
+ Some(Ok(Applicability::Inapplicable)) => {
+ writeln!(output, "\t\tlhs infeasible, rhs type error ignored")?;
+ report.failed_type_inference += 1;
+ continue;
+ }
+ None => {
+ log::debug!("Skipping solver: not checking rhs type error");
+ report.failed_type_inference += 1;
+ continue;
+ }
+ // Feasible, or feasibility could not be determined.
+ Some(Ok(applicability)) => writeln!(
+ output,
+ "\t\tlhs applicability = {applicability}, rhs type error"
+ )?,
+ Some(Err(err)) => {
+ log::debug!("lhs feasibility check failed: {err:#}");
+ }
+ }
+ return Err(conditions.error_at_expr(
+ &self.prog,
+ conflict.x,
+ format!(
+ "right-hand side is ill-typed for a type assignment accepted by \
+ the left-hand side [{choices}]: {reason} (e{x} = {expr})",
+ choices = choices.join(", "),
+ reason = conflict.reason,
+ x = conflict.x.index(),
+ expr = conditions.exprs[conflict.x.index()],
+ ),
+ ));
+ }
}
// Verify.
@@ -1341,6 +1392,44 @@ impl Runner {
Ok(self.default_solver_backend)
}
+ fn smt_context(
+ &self,
+ solver_backend: SolverBackend,
+ log_dir: &std::path::Path,
+ ) -> Result<caching::Context> {
+ let binary = solver_backend.prog();
+ let args = solver_backend.args(self.timeout);
+ let replay_file = Self::open_log_file(log_dir.to_path_buf(), "solver.smt2")?;
+ let mut smt_builder = caching::ContextBuilder::new();
+
+ smt_builder
+ .solver(binary)
+ .solver_args(&args)
+ .replay_file(Some(replay_file));
+
+ if let Some(cache) = &self.cache {
+ smt_builder.cache(cache.clone());
+ }
+
+ Ok(smt_builder.build()?)
+ }
+
+ /// Whether the left-hand side of the expansion can match under the type
+ /// assignment, ignoring its right-hand side.
+ fn check_lhs_feasibility(
+ &self,
+ conditions: &Conditions,
+ assignment: &Assignment,
+ solver_backend: SolverBackend,
+ log_dir: std::path::PathBuf,
+ ) -> Result<Applicability> {
+ let smt = self.smt_context(solver_backend, &log_dir)?;
+ let mut solver = Solver::new(smt, &self.prog, conditions, assignment)?;
+
+ solver.set_dialect(solver_backend.dialect());
+ solver.check_lhs_feasibility()
+ }
+
#[allow(clippy::too_many_arguments, reason = "verification code")]
fn verify_expansion_type_instantiation(
&self,
@@ -1357,22 +1446,7 @@ impl Runner {
) -> Result<VerifyReport> {
let start = time::Instant::now();
- // Build the SMT context through the caching layer: commands are
- // recorded as they are issued, queries are answered from the cache
- // when possible, and a solver subprocess is spawned — with the
- // recorded state played into it — only on a cache miss.
- let binary = solver_backend.prog();
- let args = solver_backend.args(self.timeout);
- let replay_file = Self::open_log_file(log_dir.clone(), "solver.smt2")?;
- let mut smt_builder = caching::ContextBuilder::new();
- smt_builder
- .solver(binary)
- .solver_args(&args)
- .replay_file(Some(replay_file));
- if let Some(cache) = &self.cache {
- smt_builder.cache(cache.clone());
- }
- let smt = smt_builder.build()?;
+ let smt = self.smt_context(solver_backend, &log_dir)?;
let mut solver = Solver::new(smt, &self.prog, conditions, assignment)?;
solver.set_dialect(solver_backend.dialect());
diff --git a/cranelift/isle/veri/veri/src/solver.rs b/cranelift/isle/veri/veri/src/solver.rs
index ced9525558..8d3b5eb634 100644
--- a/cranelift/isle/veri/veri/src/solver.rs
+++ b/cranelift/isle/veri/veri/src/solver.rs
@@ -161,13 +161,18 @@ impl<'a> Solver<'a> {
}
pub fn encode(&mut self) -> Result<()> {
+ self.encode_exprs(&HashSet::new())
+ }
+
+ /// Declare and define the expressions, other than those in `skip`.
+ fn encode_exprs(&mut self, skip: &HashSet<ExprId>) -> Result<()> {
// Simplifies the scalar case of specs to just the body
self.resolve_single_lane_maps();
// Expressions
for (i, expr) in self.conditions.exprs.iter().enumerate() {
let x = ExprId(i);
- if self.alias.contains_key(&x) {
+ if self.alias.contains_key(&x) || skip.contains(&x) {
continue;
}
self.declare_expr(x)?;
@@ -179,6 +184,40 @@ impl<'a> Solver<'a> {
Ok(())
}
+ /// Check whether the left-hand side of the rule can match at all under this
+ /// type assignment, ignoring the right-hand side.
+ ///
+ /// The assignment need only type the left-hand side expressions.
+ pub fn check_lhs_feasibility(&mut self) -> Result<Applicability> {
+ // Encode left-hand side expressions.
+ let rhs = self.conditions.rhs_dependent_exprs();
+ self.encode_exprs(&rhs)?;
+
+ // Check left-hand side assumptions, together with the typing facts of
+ // the term's contract.
+ self.smt.push()?;
+ let assumptions: Vec<ExprId> = self
+ .conditions
+ .assumptions
+ .iter()
+ .chain(&self.conditions.contract_facts)
+ .copied()
+ .filter(|a| !rhs.contains(a))
+ .collect();
+ let assumptions = self.all(&assumptions);
+ self.smt.assert(assumptions)?;
+
+ let verdict = match self.check()? {
+ Response::Sat => Applicability::Applicable,
+ Response::Unsat => Applicability::Inapplicable,
+ Response::Unknown => Applicability::Unknown,
+ };
+
+ self.smt.pop()?;
+
+ Ok(verdict)
+ }
+
pub fn check_assumptions_feasibility(&mut self) -> Result<Applicability> {
// Enter solver context frame.
self.smt.push()?;
diff --git a/cranelift/isle/veri/veri/src/spec_check.rs b/cranelift/isle/veri/veri/src/spec_check.rs
index 9b0a770b5a..222ebdc02f 100644
--- a/cranelift/isle/veri/veri/src/spec_check.rs
+++ b/cranelift/isle/veri/veri/src/spec_check.rs
@@ -145,7 +145,8 @@ fn check_term_kind(prog: &Program, term: TermId, kind: TermKind, term_name: &str
}
}
type_inference::Status::Inapplicable(conflict)
- | type_inference::Status::TypeError(conflict) => {
+ | type_inference::Status::TypeError(conflict)
+ | type_inference::Status::RhsTypeError(conflict) => {
let diagnostic = conflict.diagnostic(&conditions, &prog.files);
match signature {
Some(signature) => {
diff --git a/cranelift/isle/veri/veri/src/type_inference.rs b/cranelift/isle/veri/veri/src/type_inference.rs
index 1c43b5390e..4ee5d25026 100644
--- a/cranelift/isle/veri/veri/src/type_inference.rs
+++ b/cranelift/isle/veri/veri/src/type_inference.rs
@@ -167,10 +167,18 @@ impl std::fmt::Display for Choice {
}
}
+/// The side of the rule that a constraint comes from.
+#[derive(Copy, Clone, PartialEq, Eq)]
+enum Side {
+ Lhs,
+ Rhs,
+}
+
#[derive(Clone)]
pub struct Arm {
choice: Choice,
- constraints: Vec<Constraint>,
+ lhs_constraints: Vec<Constraint>,
+ rhs_constraints: Vec<Constraint>,
}
#[derive(Default, Clone)]
@@ -181,9 +189,17 @@ pub struct Branch {
#[derive(Default)]
pub struct System {
choices: Vec<Choice>,
- constraints: Vec<Constraint>,
branches: Vec<Branch>,
- width_domains: Vec<(ExprId, Vec<usize>)>,
+
+ /// Constraints and width domains from the left-hand side of the rule.
+ lhs_constraints: Vec<Constraint>,
+ lhs_width_domains: Vec<(ExprId, Vec<usize>)>,
+
+ /// Constraints and width domains from the right-hand side of the rule.
+ /// These are applied only once the left-hand side type assignment has been
+ /// chosen, so that a conflict can be blamed on the right-hand side.
+ rhs_constraints: Vec<Constraint>,
+ rhs_width_domains: Vec<(ExprId, Vec<usize>)>,
}
impl System {
@@ -193,8 +209,11 @@ impl System {
let mut children = Vec::new();
for arm in &branch.arms {
- let mut constraints = self.constraints.clone();
- constraints.extend(arm.constraints.iter().cloned());
+ let mut lhs_constraints = self.lhs_constraints.clone();
+ lhs_constraints.extend(arm.lhs_constraints.iter().cloned());
+
+ let mut rhs_constraints = self.rhs_constraints.clone();
+ rhs_constraints.extend(arm.rhs_constraints.iter().cloned());
let mut choices = self.choices.clone();
// Only record the choice if there are multiple branches.
@@ -203,10 +222,12 @@ impl System {
}
children.push(System {
- constraints,
choices,
branches: branches.clone(),
- width_domains: self.width_domains.clone(),
+ lhs_constraints,
+ lhs_width_domains: self.lhs_width_domains.clone(),
+ rhs_constraints,
+ rhs_width_domains: self.rhs_width_domains.clone(),
})
}
@@ -224,8 +245,15 @@ impl System {
println!("\t]");
// Constraints
- println!("\tconstraints = [");
- for constraint in &self.constraints {
+ println!("\tlhs_constraints = [");
+ for constraint in &self.lhs_constraints {
+ println!("\t\t{constraint}");
+ }
+ println!("\t]");
+
+ // Right-hand side constraints
+ println!("\trhs_constraints = [");
+ for constraint in &self.rhs_constraints {
println!("\t\t{constraint}");
}
println!("\t]");
@@ -235,7 +263,7 @@ impl System {
println!("\tbranch {{");
for arm in &branch.arms {
println!("\t\t{choice} => [", choice = arm.choice);
- for constraint in &arm.constraints {
+ for constraint in &arm.lhs_constraints {
println!("\t\t\t{constraint}");
}
println!("\t\t]");
@@ -257,6 +285,9 @@ struct SystemBuilder<'a> {
system: System,
arm: Option<Arm>,
+ /// Side of the rule that new constraints belong to. Only changed through
+ /// [`SystemBuilder::on_side`], which restores it afterwards.
+ side: Side,
}
impl<'a> SystemBuilder<'a> {
@@ -265,23 +296,28 @@ impl<'a> SystemBuilder<'a> {
conditions,
system: System::default(),
arm: None,
+ side: Side::Lhs,
}
}
fn build(mut self) -> System {
+ // Constraints are attributed to the side of the rule that introduced the
+ // expression they are about.
+
// Expression constraints.
for (i, expr) in self.conditions.exprs.iter().enumerate() {
- self.veri_expr(ExprId(i), expr);
+ let x = ExprId(i);
+ self.on_side(self.side_of(x), |b| b.veri_expr(x, expr));
}
// Assumptions.
for a in &self.conditions.assumptions {
- self.boolean_value(*a, true);
+ self.on_side(self.side_of(*a), |b| b.boolean_value(*a, true));
}
// Assertions.
for a in &self.conditions.assertions {
- self.boolean(*a);
+ self.on_side(self.side_of(*a), |b| b.boolean(*a));
}
// Calls.
@@ -291,19 +327,47 @@ impl<'a> SystemBuilder<'a> {
// Qualifiers.
for qualifier in &self.conditions.qualifiers {
- self.qualifier(qualifier);
+ let side = if qualifier.rhs { Side::Rhs } else { Side::Lhs };
+ self.on_side(side, |b| b.qualifier(qualifier));
}
// Lanes.
for (x, n) in &self.conditions.lane_counts {
- self.constraint(Constraint::Lanes { x: *x, n: *n });
+ self.on_side(self.side_of(*x), |b| {
+ b.constraint(Constraint::Lanes { x: *x, n: *n })
+ });
}
- self.system.width_domains = self.conditions.width_domains.clone();
+ let (rhs, lhs): (Vec<_>, Vec<_>) = self
+ .conditions
+ .width_domains
+ .iter()
+ .cloned()
+ .partition(|(x, _)| self.side_of(*x) == Side::Rhs);
+ self.system.lhs_width_domains = lhs;
+ self.system.rhs_width_domains = rhs;
self.system
}
+ /// Side of the rule that an expression was introduced by.
+ fn side_of(&self, x: ExprId) -> Side {
+ if self.conditions.rhs_exprs.contains(&x) {
+ Side::Rhs
+ } else {
+ Side::Lhs
+ }
+ }
+
+ /// Run `f` with new constraints attributed to `side`, then restore the
+ /// previous side.
+ fn on_side<R>(&mut self, side: Side, f: impl FnOnce(&mut Self) -> R) -> R {
+ let previous = std::mem::replace(&mut self.side, side);
+ let result = f(self);
+ self.side = previous;
+ result
+ }
+
fn veri_expr(&mut self, x: ExprId, expr: &Expr) {
match expr {
Expr::Const(c) => {
@@ -589,14 +653,25 @@ impl<'a> SystemBuilder<'a> {
// Branch arm for
self.push_arm(Choice::TermInstantiation(call.term, sig.clone()));
- // Arguments.
+ // Arguments come from the left-hand side.
assert_eq!(call.args.len(), sig.args.len());
- for (a, ty) in zip(&call.args, &sig.args) {
- self.signature_type(a, ty.clone());
- }
+ self.on_side(Side::Lhs, |b| {
+ for (a, ty) in zip(&call.args, &sig.args) {
+ b.signature_type(a, ty.clone());
+ }
+ });
- // Return.
- self.signature_type(&call.ret, sig.ret.clone());
+ // The return value belongs to the right-hand side for the term
+ // under expansion.
+ let ret_side = if call.ret_rhs { Side::Rhs } else { Side::Lhs };
+ self.on_side(ret_side, |b| b.signature_type(&call.ret, sig.ret.clone()));
+
+ // The contract's fresh return value has the same signature.
+ if let Some(contract_ret) = &call.contract_ret {
+ self.on_side(Side::Lhs, |b| {
+ b.signature_type(contract_ret, sig.ret.clone())
+ });
+ }
// Pop branch arm.
self.pop();
@@ -695,9 +770,11 @@ impl<'a> SystemBuilder<'a> {
}
fn constraint(&mut self, constraint: Constraint) {
- let current = match self.arm.as_mut() {
- Some(arm) => &mut arm.constraints,
- None => &mut self.system.constraints,
+ let current = match (self.arm.as_mut(), self.side) {
+ (Some(arm), Side::Lhs) => &mut arm.lhs_constraints,
+ (Some(arm), Side::Rhs) => &mut arm.rhs_constraints,
+ (None, Side::Lhs) => &mut self.system.lhs_constraints,
+ (None, Side::Rhs) => &mut self.system.rhs_constraints,
};
current.push(constraint)
}
@@ -710,7 +787,8 @@ impl<'a> SystemBuilder<'a> {
assert!(self.arm.is_none());
self.arm = Some(Arm {
choice,
- constraints: Vec::new(),
+ lhs_constraints: Vec::new(),
+ rhs_constraints: Vec::new(),
});
}
@@ -1010,6 +1088,9 @@ pub enum Status {
Inapplicable(Conflict),
Underconstrained,
TypeError(Conflict),
+ /// The left-hand side type assignment is valid, but it leaves the
+ /// right-hand side ill-typed.
+ RhsTypeError(Conflict),
}
impl std::fmt::Display for Status {
@@ -1019,6 +1100,7 @@ impl std::fmt::Display for Status {
Status::Inapplicable(..) => write!(f, "inapplicable"),
Status::Underconstrained => write!(f, "underconstrained"),
Status::TypeError(..) => write!(f, "type error"),
+ Status::RhsTypeError(..) => write!(f, "right-hand side type error"),
}
}
}
@@ -1049,7 +1131,7 @@ impl Solver {
pub fn solve(mut self, system: &System) -> Vec<Solution> {
// Deduce assignments from constraints.
- let result = self.propagate(&system.constraints);
+ let result = self.propagate(&system.lhs_constraints);
if let Err(status) = result {
return vec![Solution {
status,
@@ -1060,37 +1142,42 @@ impl Solver {
// Done?
if system.branches.is_empty() {
- // Branch over the declared widths of an undetermined bit-vector.
- let undetermined = system
- .width_domains
- .iter()
- .find(|(x, _)| self.assignment.bit_vector_width(*x).is_none());
- if let Some((x, widths)) = undetermined {
- let x = *x;
- let mut solutions = Vec::new();
- for &width in widths {
- let mut choices = system.choices.clone();
- choices.push(Choice::BitVectorWidth(x, width));
- let mut constraints = system.constraints.clone();
- constraints.push(Constraint::Type {
- x,
- ty: Type::BitVector(Width::Bits(width)),
- });
- let child = System {
- choices,
- constraints,
- branches: Vec::new(),
- width_domains: system.width_domains.clone(),
- };
- solutions.extend(self.clone().solve(&child));
- }
+ // Settle the left-hand side type assignment first, branching over
+ // the declared widths of any undetermined bit-vector.
+ if let Some(solutions) = self.branch_width(system, &system.lhs_width_domains) {
+ return solutions;
+ }
+
+ // The rule fires whenever its left-hand side matches, so the
+ // right-hand side must type-check under the assignment: a conflict
+ // here is an error in the rule, not an inapplicable type
+ // assignment.
+ if let Err(status) = self.propagate(&system.rhs_constraints) {
+ let conflict = match status {
+ Status::Inapplicable(conflict)
+ | Status::TypeError(conflict)
+ | Status::RhsTypeError(conflict) => conflict,
+ Status::Solved | Status::Underconstrained => {
+ unreachable!("propagation only fails with a conflict")
+ }
+ };
+ return vec![Solution {
+ status: Status::RhsTypeError(conflict),
+ choices: system.choices.clone(),
+ assignment: self.assignment,
+ }];
+ }
+
+ // Then widths that only the right-hand side leaves undetermined.
+ if let Some(solutions) = self.branch_width(system, &system.rhs_width_domains) {
return solutions;
}
// Bit-vectors whose lane count is still undetermined are scalars.
let undetermined: Vec<_> = system
- .constraints
+ .lhs_constraints
.iter()
+ .chain(&system.rhs_constraints)
.filter_map(|c| match c {
Constraint::LanesOf { x, .. } | Constraint::LaneOf { v: x, .. }
if self.assignment.lanes(*x).is_none() =>
@@ -1101,13 +1188,15 @@ impl Solver {
})
.collect();
if !undetermined.is_empty() {
- let mut constraints = system.constraints.clone();
- constraints.extend(undetermined);
+ let mut lhs_constraints = system.lhs_constraints.clone();
+ lhs_constraints.extend(undetermined);
let child = System {
choices: system.choices.clone(),
- constraints,
branches: Vec::new(),
- width_domains: system.width_domains.clone(),
+ lhs_constraints,
+ lhs_width_domains: system.lhs_width_domains.clone(),
+ rhs_constraints: system.rhs_constraints.clone(),
+ rhs_width_domains: system.rhs_width_domains.clone(),
};
return self.solve(&child);
}
@@ -1134,6 +1223,39 @@ impl Solver {
solutions
}
+ /// Branch over the declared widths of the first undetermined bit-vector in
+ /// `domains`, if there is one.
+ fn branch_width(
+ &self,
+ system: &System,
+ domains: &[(ExprId, Vec<usize>)],
+ ) -> Option<Vec<Solution>> {
+ let (x, widths) = domains
+ .iter()
+ .find(|(x, _)| self.assignment.bit_vector_width(*x).is_none())?;
+
+ let mut solutions = Vec::new();
+ for &width in widths {
+ let mut choices = system.choices.clone();
+ choices.push(Choice::BitVectorWidth(*x, width));
+ let mut lhs_constraints = system.lhs_constraints.clone();
+ lhs_constraints.push(Constraint::Type {
+ x: *x,
+ ty: Type::BitVector(Width::Bits(width)),
+ });
+ let child = System {
+ choices,
+ branches: Vec::new(),
+ lhs_constraints,
+ lhs_width_domains: system.lhs_width_domains.clone(),
+ rhs_constraints: system.rhs_constraints.clone(),
+ rhs_width_domains: system.rhs_width_domains.clone(),
+ };
+ solutions.extend(self.clone().solve(&child));
+ }
+ Some(solutions)
+ }
+
fn propagate(&mut self, constraints: &[Constraint]) -> Result<(), Status> {
// Iterate until no changes.
while self.iterate(constraints)? {}
diff --git a/cranelift/isle/veri/veri/src/veri.rs b/cranelift/isle/veri/veri/src/veri.rs
index 93d01982fd..5dac6b94ab 100644
--- a/cranelift/isle/veri/veri/src/veri.rs
+++ b/cranelift/isle/veri/veri/src/veri.rs
@@ -793,6 +793,13 @@ pub struct Call {
pub args: Vec<Symbolic>,
pub ret: Symbolic,
pub signatures: Vec<Signature>,
+
+ /// The return value belongs to the right-hand side of the rule. See
+ /// [`Conditions::rhs_exprs`].
+ pub ret_rhs: bool,
+ /// A second value standing for the return value, constrained by the same
+ /// signature. See [`Conditions::contract_facts`].
+ pub contract_ret: Option<Symbolic>,
}
// Type qualifier, for example derived from an `(as ...)` expression.
@@ -800,6 +807,9 @@ pub struct Call {
pub struct Qualifier {
pub value: Symbolic,
pub ty: Compound,
+ /// The qualifier was introduced by the right-hand side of the rule. See
+ /// [`Conditions::rhs_exprs`].
+ pub rhs: bool,
}
/// Verification conditions for an expansion.
@@ -816,6 +826,17 @@ pub struct Conditions {
pub width_domains: Vec<(ExprId, Vec<usize>)>,
/// Bit-vectors with a known lane count.
pub lane_counts: Vec<(ExprId, usize)>,
+
+ /// Expressions introduced while evaluating the right-hand side of the
+ /// rule: the constructor calls that build the result, and the contract
+ /// tying that result to the term under expansion.
+ /// Type inference treats these separately from the left-hand side.
+ pub rhs_exprs: HashSet<ExprId>,
+
+ /// The contract of the term under expansion, stated of a fresh return
+ /// value in place of the rule's right-hand side result.
+ /// Specs record typing facts as provides.
+ pub contract_facts: Vec<ExprId>,
}
impl Conditions {
@@ -907,6 +928,21 @@ impl Conditions {
println!("}}");
}
+ /// Expressions that depend on the right-hand side: those introduced by it,
+ /// and any computed from them. For example, a state default depends on
+ /// every call in the expansion.
+ pub fn rhs_dependent_exprs(&self) -> HashSet<ExprId> {
+ let mut dependent = HashSet::new();
+ // Sources are always created before the expressions that use them.
+ for (i, expr) in self.exprs.iter().enumerate() {
+ let x = ExprId(i);
+ if self.rhs_exprs.contains(&x) || expr.sources().iter().any(|y| dependent.contains(y)) {
+ dependent.insert(x);
+ }
+ }
+ dependent
+ }
+
pub fn validate(&self) -> Result<()> {
// Ensure there are no dangling expressions.
let reachable = self.reachable();
@@ -928,6 +964,7 @@ impl Conditions {
let mut stack: Vec<ExprId> = Vec::new();
stack.extend(&self.assumptions);
stack.extend(&self.assertions);
+ stack.extend(&self.contract_facts);
while let Some(x) = stack.pop() {
if reach.contains(&x) {
@@ -1037,6 +1074,52 @@ pub enum TermKind {
Extractor,
}
+/// Bindings that belong to the right-hand side of the rule.
+///
+/// These are the bindings that nothing on the left-hand side depends on: every
+/// other binding is, directly or transitively, an extractor call, a constraint
+/// on the match, or an input to one of those.
+fn rhs_bindings(expansion: &Expansion) -> HashSet<BindingId> {
+ let mut lhs: HashSet<BindingId> = HashSet::new();
+ let mut work: Vec<BindingId> = Vec::new();
+
+ // Roots of the left-hand side.
+ for constrain in &expansion.constraints {
+ work.extend(constrain.bindings());
+ }
+ for (a, b) in expansion.equalities() {
+ work.push(a);
+ work.push(b);
+ }
+ for (i, binding) in expansion.bindings.iter().enumerate() {
+ if matches!(
+ binding,
+ Some(Binding::Extractor { .. } | Binding::Argument { .. } | Binding::Iterator { .. })
+ ) {
+ work.push(i.try_into().unwrap());
+ }
+ }
+
+ // Everything they depend on.
+ while let Some(id) = work.pop() {
+ if !lhs.insert(id) {
+ continue;
+ }
+ if let Some(binding) = expansion.binding(id) {
+ work.extend(binding.sources().iter().copied());
+ }
+ }
+
+ expansion
+ .bindings
+ .iter()
+ .enumerate()
+ .filter(|(_, binding)| binding.is_some())
+ .map(|(i, _)| i.try_into().unwrap())
+ .filter(|id| !lhs.contains(id))
+ .collect()
+}
+
fn is_fallible(term: &sema::Term, tyenv: &sema::TypeEnv, kind: TermKind) -> bool {
match kind {
TermKind::Constructor => term.is_partial(),
@@ -1050,6 +1133,9 @@ fn is_fallible(term: &sema::Term, tyenv: &sema::TypeEnv, kind: TermKind) -> bool
enum Invocation {
Caller,
Callee,
+
+ /// The callee's contract, evaluated of a fresh return value.
+ Contract,
}
#[derive(Copy, Clone)]
@@ -1113,6 +1199,16 @@ struct ConditionsBuilder<'a> {
state_modification_conds: HashMap<String, Vec<ExprId>>,
binding_value: HashMap<BindingId, Symbolic>,
expr_map: HashMap<Expr, ExprId>,
+
+ /// Expression deduplication for right-hand side expressions, kept apart
+ /// from `expr_map` so that an expression is never shared between the two
+ /// sides of the rule.
+ rhs_expr_map: HashMap<Expr, ExprId>,
+ /// Bindings that belong to the right-hand side of the rule.
+ rhs_bindings: HashSet<BindingId>,
+ /// Whether expressions are currently being built for the right-hand side.
+ rhs: bool,
+
conditions: Conditions,
position_stack: Vec<Pos>,
in_lane_map: bool,
@@ -1132,6 +1228,9 @@ impl<'a> ConditionsBuilder<'a> {
state_modification_conds: HashMap::new(),
binding_value: HashMap::new(),
expr_map: HashMap::new(),
+ rhs_expr_map: HashMap::new(),
+ rhs_bindings: HashSet::new(),
+ rhs: false,
conditions: Conditions::default(),
position_stack: Vec::new(),
in_lane_map: false,
@@ -1155,19 +1254,24 @@ impl<'a> ConditionsBuilder<'a> {
}
// Bindings.
+ self.rhs_bindings = rhs_bindings(expansion);
for (i, binding) in expansion.bindings.iter().enumerate() {
if let Some(binding) = binding {
self.add_binding(i.try_into().unwrap(), binding)?;
}
}
- // Callee contract for the term under expansion.
+ // Callee contract for the term under expansion. This ties the
+ // right-hand side result to the term's signature.
+ self.rhs = true;
self.constructor(
expansion.result,
expansion.term,
&expansion.parameters,
Invocation::Callee,
)?;
+ self.rhs = false;
+ self.contract_facts()?;
// Constraints.
for constrain in &expansion.constraints {
@@ -1192,6 +1296,53 @@ impl<'a> ConditionsBuilder<'a> {
Ok(self.conditions)
}
+ /// Evaluate the contract of the term under expansion of a fresh return
+ /// value, in place of the result of the rule's right-hand side.
+ fn contract_facts(&mut self) -> Result<()> {
+ let expansion = self.expansion();
+ let callee = self
+ .conditions
+ .calls
+ .len()
+ .checked_sub(1)
+ .expect("callee call should be recorded");
+ let result = expansion
+ .binding(expansion.result)
+ .expect("result binding should be defined");
+ let binding_type = self.binding_type(result);
+ let fresh = self.alloc_binding(&binding_type, "contract_result".to_string())?;
+ let (domain, ret) = Domain::from_return_value(&fresh);
+ let mut args = Vec::new();
+
+ for parameter in &expansion.parameters {
+ args.push(self.binding_value[parameter].clone());
+ }
+
+ self.call(
+ expansion.term,
+ TermKind::Constructor,
+ &args,
+ ret.clone(),
+ Invocation::Contract,
+ domain,
+ )
+ .with_context(|| {
+ format!(
+ "expanding contract of '{name}'",
+ name = self.prog.term_name(expansion.term)
+ )
+ })?;
+
+ // The fresh value shares the type of the callee's return value.
+ let call = &mut self.conditions.calls[callee];
+
+ assert_eq!(call.term, expansion.term);
+
+ call.contract_ret = Some(ret);
+
+ Ok(())
+ }
+
/// 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> {
@@ -1279,6 +1430,14 @@ impl<'a> ConditionsBuilder<'a> {
return Ok(());
}
+ // Build the binding's conditions on the side of the rule it belongs to.
+ let rhs = std::mem::replace(&mut self.rhs, self.rhs_bindings.contains(&id));
+ let result = self.add_new_binding(id, binding);
+ self.rhs = rhs;
+ result
+ }
+
+ fn add_new_binding(&mut self, id: BindingId, binding: &Binding) -> Result<()> {
// Allocate a value.
let binding_type = self.binding_type(binding);
let name = format!("b{}", id.index());
@@ -1505,10 +1664,12 @@ impl<'a> ConditionsBuilder<'a> {
self.boolean(true)
};
// Record condition to determine when the default spec applies.
- self.state_modification_conds
- .entry(modifies.state.0.clone())
- .or_default()
- .push(cond);
+ if !matches!(invocation, Invocation::Contract) {
+ self.state_modification_conds
+ .entry(modifies.state.0.clone())
+ .or_default()
+ .push(cond);
+ }
}
let (requires, matches, mut provides) =
@@ -1520,7 +1681,10 @@ impl<'a> ConditionsBuilder<'a> {
// Matches describe when the function applies.
let all_matches = self.all(matches);
let eq = self.exprs_equal(p, all_matches);
- self.conditions.assumptions.push(eq);
+ match invocation {
+ Invocation::Contract => self.conditions.contract_facts.push(eq),
+ _ => self.conditions.assumptions.push(eq),
+ }
// Provides are conditioned on the match.
let all_provides = self.all(provides);
@@ -1540,6 +1704,11 @@ impl<'a> ConditionsBuilder<'a> {
self.conditions.assumptions.extend(requires);
self.conditions.assertions.extend(provides);
}
+ Invocation::Contract => {
+ self.conditions.contract_facts.extend(requires);
+ self.conditions.contract_facts.extend(provides);
+ return Ok(());
+ }
}
// Record callsite. Instantiations do not constrain constructor/RHS terms.
@@ -1635,6 +1804,8 @@ impl<'a> ConditionsBuilder<'a> {
args,
ret,
signatures,
+ ret_rhs: self.rhs,
+ contract_ret: None,
});
Ok(())
}
@@ -2096,6 +2267,7 @@ impl<'a> ConditionsBuilder<'a> {
self.conditions.qualifiers.push(Qualifier {
value: x.clone(),
ty: ty.clone(),
+ rhs: self.rhs,
});
Ok(x)
}
@@ -2767,8 +2939,13 @@ impl<'a> ConditionsBuilder<'a> {
fn dedup_expr(&mut self, expr: Expr) -> ExprId {
// Dedupe, if pure.
+ let expr_map = if self.rhs {
+ &mut self.rhs_expr_map
+ } else {
+ &mut self.expr_map
+ };
let maybe_id = if expr.pure() {
- self.expr_map.get(&expr)
+ expr_map.get(&expr)
} else {
None
};
@@ -2779,7 +2956,10 @@ impl<'a> ConditionsBuilder<'a> {
} else {
let id = ExprId(self.conditions.exprs.len());
self.conditions.exprs.push(expr.clone());
- self.expr_map.insert(expr, id);
+ expr_map.insert(expr, id);
+ if self.rhs {
+ self.conditions.rhs_exprs.insert(id);
+ }
id
};
diff --git a/cranelift/isle/veri/veri/tests/filetests.rs b/cranelift/isle/veri/veri/tests/filetests.rs
index 94f8c45b4c..1ac98276d8 100644
--- a/cranelift/isle/veri/veri/tests/filetests.rs
+++ b/cranelift/isle/veri/veri/tests/filetests.rs
@@ -76,3 +76,19 @@ fn spec_conflict(test_file: &str) {
"expected the run to fail before verification, got summary {summary:?}"
);
}
+
+/// Test files whose rule has a right-hand side that is ill-typed for some type
+/// assignment accepted by the left-hand side.
+#[file_tests(path = "filetests/rhs_type_error", ext = "isle")]
+fn rhs_type_error(test_file: &str) {
+ let (summary, result) = run(test_file).expect("run should complete without errors");
+ result.expect_err("the right-hand side type error should fail the run");
+ assert!(
+ summary.expansion_errors > 0,
+ "expected the right-hand side type error to be reported, got summary {summary:?}"
+ );
+ assert_eq!(
+ summary.success, 0,
+ "expected no successful verification of an ill-typed rule, got summary {summary:?}"
+ );
+}