use verif::mem::{declare, Mem, MAX_CELLS};
use verif::smt::{self, var, Verdict};
use verif::wp::{self, Stmt};
fn main() {
println!("=== a bounded memory: 4 cells of 8 bits ===\n");
smt::with_fresh(|| {
let mut buf = [var(8); 4];
let n = declare(2, 8, &mut buf);
let m = Mem::new(&buf[..n], 8);
println!("address width : {}", m.addr_width());
println!("cells : {}", m.len());
println!("element width : {} bits\n", m.elem_width());
let addr = var(2);
let val = var(8);
let at = var(2);
println!("frame lemma (all inputs):");
println!(
" read(store(m, a, v), b) == ite(a == b, v, read(m, b)) -> {}",
verdict(&smt::prove(m.frame_lemma(addr, val, at)))
);
let readback = m.store_wp(addr, val, smt::eq(m.read(addr), val));
println!(
" write then read back -> {}",
verdict(&smt::prove(readback))
);
let other = m.read_const(3);
let frame = m.store_wp(addr, val, smt::eq(other, m.read_const(3)));
let goal = smt::implies(smt::not(smt::eq_const(addr, 3)), frame);
println!(
" write to a != 3 leaves cell 3 alone -> {}",
verdict(&smt::prove(goal))
);
println!();
});
println!("=== a swap through a temporary, verified as a program ===\n");
smt::with_fresh(|| {
let mut buf = [var(8); 4];
let n = declare(2, 8, &mut buf);
let m = Mem::new(&buf[..n], 8);
let i = var(2);
let j = var(2);
let t = var(8);
let oi = var(8);
let oj = var(8);
let save_i = Stmt::Assign { var: oi, expr: m.read(i) };
let save_j = Stmt::Assign { var: oj, expr: m.read(j) };
let save_t = Stmt::Assign { var: t, expr: oi };
let mut b1 = [(i, t); MAX_CELLS];
let n1 = m.store_binds(i, oj, &mut b1);
let mut b2 = [(i, t); MAX_CELLS];
let n2 = m.store_binds(j, t, &mut b2);
let write_i = Stmt::AssignMany { binds: &b1[..n1] };
let write_j = Stmt::AssignMany { binds: &b2[..n2] };
let prog = Stmt::Seq {
first: &save_i,
second: &Stmt::Seq {
first: &save_j,
second: &Stmt::Seq {
first: &save_t,
second: &Stmt::Seq {
first: &write_i,
second: &write_j,
},
},
},
};
let post = smt::and(smt::eq(m.read(i), oj), smt::eq(m.read(j), oi));
let report = wp::verify(&prog, smt::truth(true), post);
println!("obligations : {}", report.count);
println!("contract : {}", verdict(&report.contract.discharge()));
println!("verified : {}\n", report.is_verified());
let k = smt::word(3, 2);
let kept = smt::eq(m.read(k), smt::word(0, 8));
let untouched = smt::and(
smt::eq_const(m.read_const(3), 0),
smt::and(smt::not(smt::eq(i, k)), smt::not(smt::eq(j, k))),
);
let frame = smt::implies(untouched, wp::wp(kept, &prog));
println!("frame obligation (cell 3 untouched when i, j != 3):");
println!(" {}", verdict(&smt::prove(frame)));
println!();
});
println!("=== a loop over addresses, which needs a quantifier ===\n");
println!("A fill loop's invariant has to say \"every cell below k is zero\". That is");
println!("a quantifier, which a bit-vector decision procedure does not have -- but");
println!("it is *bounded*, and a bounded quantifier is a finite conjunction, so it");
println!("can simply be expanded: (0 < k => m[0] == 0) && (1 < k => m[1] == 0) && ...");
println!("`Mem::forall_below` does that, which is what makes this loop verifiable at");
println!("all instead of needing a hand unroll.\n");
smt::with_fresh(|| {
let mut buf = [var(8); 4];
let n = declare(2, 8, &mut buf);
let m = Mem::new(&buf[..n], 8);
let k = var(3);
let zero = smt::word(0, 8);
let addr = m.addr_of(k);
let mut binds = [(addr, zero); MAX_CELLS];
let nb = m.store_binds(addr, zero, &mut binds);
let store = Stmt::AssignMany { binds: &binds[..nb] };
let step = Stmt::Assign { var: k, expr: k + 1u32 };
let body = Stmt::Seq { first: &store, second: &step };
let inv = m.forall_below(k, |cell| smt::eq_const(cell, 0));
let loop_ = Stmt::While {
cond: smt::ult_const(k, 4),
invariant: inv,
variant: Some(smt::sub(smt::word(4, 3), k)),
body: &body,
};
let init = Stmt::Assign { var: k, expr: smt::word(0, 3) };
let prog = Stmt::Seq { first: &init, second: &loop_ };
let post = m.forall_cells(|cell| smt::eq_const(cell, 0));
let report = wp::verify(&prog, smt::truth(true), post);
println!("for k := 0; k < 4; k += 1 {{ m[k] := 0 }}");
println!(" invariant: forall j < k {{ m[j] == 0 }}");
println!(" obligations : {}", report.count);
println!(" verified : {}", report.is_verified());
println!(" termination : {:?}", report.correctness());
if let Some((ob, _)) = report.first_failure() {
println!(" FAILED : {:?}", ob.rule);
}
});
smt::with_fresh(|| {
let mut buf = [var(8); 4];
let n = declare(2, 8, &mut buf);
let m = Mem::new(&buf[..n], 8);
let k = var(3);
let zero = smt::word(0, 8);
let addr = m.addr_of(k);
let mut binds = [(addr, zero); MAX_CELLS];
let nb = m.store_binds(addr, zero, &mut binds);
let store = Stmt::AssignMany { binds: &binds[..nb] };
let step = Stmt::Assign { var: k, expr: k + 1u32 };
let body = Stmt::Seq { first: &store, second: &step };
let loop_ = Stmt::While {
cond: smt::ult_const(k, 4),
invariant: smt::eq_const(m.read(addr), 0),
variant: None,
body: &body,
};
let init = Stmt::Assign { var: k, expr: smt::word(0, 3) };
let prog = Stmt::Seq { first: &init, second: &loop_ };
let post = m.forall_cells(|cell| smt::eq_const(cell, 0));
let report = wp::verify(&prog, smt::truth(true), post);
println!();
println!("same loop, invariant weakened to \"m[k] == 0\" only:");
println!(" verified : {} (as it must be: one cell is not four)", report.is_verified());
});
println!();
println!("=== what is still out of reach ===\n");
println!("The bound has to be a number the tool can count to, because the expansion");
println!("has one conjunct per cell. `forall k < 1000` is a thousand copies of the");
println!("predicate, and `MAX_CELLS` caps a memory at 64 cells for that reason. So:");
println!("quantifiers over a *fixed* range are fine, unbounded ones are not, and the");
println!("cost grows linearly with the range either way. A true `forall` over an");
println!("unknown domain would need a different decision procedure -- that is the line");
println!("between this crate and a real SMT solver, and it is not a small one.\n");
println!("done");
}
fn verdict(v: &Verdict) -> &'static str {
match v {
Verdict::Valid => "valid",
Verdict::Counterexample(_) => "refuted",
Verdict::Unknown => "unknown",
Verdict::Unsat => "unsat",
}
}