diff --git a/src/engines/pseudo/vcgen.js b/src/engines/pseudo/vcgen.js index ae295e5..9ac816e 100644 --- a/src/engines/pseudo/vcgen.js +++ b/src/engines/pseudo/vcgen.js @@ -625,11 +625,16 @@ class Exec { const H = this.havoc(E, modified, s.line); const x = this.fresh(s.var, sortOf(s.eachSym?.type)); H.env.set(X, x); - if (coll.k === 'app' && coll.f === 'RANGE') H.facts.push(and(cmp('<=', coll.args[0], x), cmp('<', x, coll.args[1]))); - else if (coll.t === 'list' || coll.t === 'set' || coll.t === 'map') H.facts.push(member(x, coll)); for (const inv of this.userInvariants(s, H)) H.facts.push(inv.term); + // X is in the collection only inside a pass. The loop also ends without + // any pass when the collection is empty, so the way out must not know it: + // for an empty list that fact is FALSE, and the checker would then prove + // anything after the loop (tests/prover/foreach-empty.pseudo). + let inPass = H; + if (coll.k === 'app' && coll.f === 'RANGE') inPass = H.assume(and(cmp('<=', coll.args[0], x), cmp('<', x, coll.args[1]))); + else if (coll.t === 'list' || coll.t === 'set' || coll.t === 'map') inPass = H.assume(member(x, coll)); const inner = { breaks: [], continues: [] }; - const ends = this.run(s.body, [H], inner); + const ends = this.run(s.body, [inPass], inner); for (const st of [...ends, ...inner.continues]) { for (const inv of this.userInvariants(s, st)) this.obligation('invariant-keep', inv.node, `each pass keeps the INVARIANT`, st, [], inv.term); } diff --git a/tests/prover-soundness.test.mjs b/tests/prover-soundness.test.mjs index 1b9bdb8..1635bca 100644 --- a/tests/prover-soundness.test.mjs +++ b/tests/prover-soundness.test.mjs @@ -123,3 +123,30 @@ test('nothing the prover proves is broken by a run or by generated inputs', () = assert.ok(proved >= 150, `only ${proved} proved obligations were checked`); assert.deepEqual(problems, []); }); + +test('canary: after a program that runs to its end, ASSERT 1 == 2 is never proved', () => { + // A false claim proved at a point every run reaches means the checker holds a contradiction + // there: some rule assumed a fact a run can break. This finds such a rule even when no + // program happens to assert something false after it (tests/prover/foreach-empty.pseudo). + const problems = []; + let canaries = 0; + for (const p of programs()) { + const st = studyOf(p.src); + if (!st.ok || /^\s*INPUT\b/m.test(p.src)) continue; + let run; + try { run = runPseudo(st, {}); } catch { continue; } + if (run.error || run.truncated) continue; + const lines = p.src.replace(/\s+$/, '').split('\n'); + const line = lines.length + 1; + const withCanary = studyOf(`${lines.join('\n')}\nASSERT 1 == 2\n`); + if (!withCanary.ok) continue; + const r = verify(withCanary.program.program, { budgetMs: 3000 }); + const main = r.units.find((u) => u.kind === 'main'); + const o = main?.obligations.find((x) => x.kind === 'assert' && x.line === line); + if (!o) continue; + canaries++; + if (o.status === 'proved') problems.push(`${p.name}: ASSERT 1 == 2 after the last line is proved`); + } + assert.ok(canaries >= 60, `only ${canaries} programs carried a canary`); + assert.deepEqual(problems, []); +}); diff --git a/tests/prover/foreach-empty.pseudo b/tests/prover/foreach-empty.pseudo new file mode 100644 index 0000000..a364e22 --- /dev/null +++ b/tests/prover/foreach-empty.pseudo @@ -0,0 +1,13 @@ +// The way out of a FOR EACH loop includes the case of no pass at all. A +// checker that kept "X is in Xs" on the way out held FALSE after looping over +// an empty list, and proved anything, even 1 == 2. +// MUST-NOT-PROVE: assert@12 +DECLARE Xs : LIST OF INTEGER +DECLARE S : INTEGER +Xs <- [] +S <- 0 +FOR EACH X IN Xs + S <- S + X +NEXT X +ASSERT 1 == 2 +OUTPUT S