Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

Prove the interner unbounded in Dafny, where Kani cannot go at all (#198)

Fabio Rovai53f7fda996a0
diff --git a/dafny/Interner.dfy b/dafny/Interner.dfy
new file mode 100644
index 00000000..ce4626e9
--- /dev/null
+++ b/dafny/Interner.dfy
@@ -0,0 +1,186 @@
+// TCB-15, TCB-16 and TCB-17 without a bound: the interner.
+//
+// `docs/trusted-computing-base.md` states them as
+//
+// TCB-15 (round trip) `resolve(intern(s)) == s` for every string.
+// TCB-16 (injectivity) `intern(a) == intern(b)` if and only if `a == b`.
+// TCB-17 (stability) an identifier's resolution does not change as more
+// strings are interned.
+//
+// All three are property-tested, which samples, and none is proved. They are
+// worth more than that. The interner is the one place where a term stops being
+// bytes and becomes a number, and every rule the fixpoint evaluates runs on
+// those numbers. If two different terms could collide on one identifier, a rule
+// would fire on a premise nobody asserted, and the certificate would record a
+// derivation true of the INTERNED graph and not of the real one. The Lean side
+// cannot see that: it reads `asserted.tsv`, which is written from the strings.
+//
+// KANI CANNOT REACH THESE, and that was measured rather than assumed. A harness
+// over `Interner` returns
+//
+// 1 of 5109 failed (5108 undetermined)
+// Failed Checks: call to foreign "C" function `CCRandomGenerateBytes`
+// is not currently supported by Kani
+//
+// which is `std`'s `HashMap` seeding `RandomState` from the operating system.
+// Replacing the hasher with a fixed one removes that call and the harness still
+// fails, so the obstacle is `HashMap`'s internals rather than the seed. No bound
+// helps, because the obstacle is an unsupported construct and not a cost.
+//
+// So this file models the interner as a map and a sequence, exactly the two
+// fields `src/reason.rs` carries, and proves all three statements for every
+// string and every number of insertions.
+//
+// WHAT IT IS NOT. Decision 0014 applies here unchanged: this is a
+// REIMPLEMENTATION, so nothing proved here is evidence about `src/reason.rs`.
+// It is evidence that the DESIGN is sound, which is a different and smaller
+// claim, and the correspondence between this model and that Rust is read by a
+// person. The model is deliberately small so that reading is cheap: two fields,
+// two operations, and the same order of writes.
+
+datatype Interner = Interner(toId: map<string, nat>, toStr: seq<string>)
+
+// The invariant the Rust maintains by construction: the map and the sequence
+// agree, ids are exactly the positions of the sequence, and no string is
+// interned twice.
+predicate Wf(i: Interner)
+{
+ && (forall s :: s in i.toId ==> i.toId[s] < |i.toStr|)
+ && (forall s :: s in i.toId ==> i.toStr[i.toId[s]] == s)
+ && (forall k :: 0 <= k < |i.toStr| ==> i.toStr[k] in i.toId && i.toId[i.toStr[k]] == k)
+}
+
+function Empty(): Interner { Interner(map[], []) }
+
+lemma EmptyIsWf()
+ ensures Wf(Empty())
+{
+}
+
+// `intern` in `src/reason.rs`: return the existing id if there is one,
+// otherwise append and hand back the new position.
+function Intern(i: Interner, s: string): (Interner, nat)
+{
+ if s in i.toId then (i, i.toId[s])
+ else (Interner(i.toId[s := |i.toStr|], i.toStr + [s]), |i.toStr|)
+}
+
+// `resolve` in `src/reason.rs`, which indexes the sequence.
+function Resolve(i: Interner, id: nat): string
+ requires id < |i.toStr|
+{
+ i.toStr[id]
+}
+
+lemma InternPreservesWf(i: Interner, s: string)
+ requires Wf(i)
+ ensures Wf(Intern(i, s).0)
+ ensures Intern(i, s).1 < |Intern(i, s).0.toStr|
+{
+ var (j, id) := Intern(i, s);
+ if s in i.toId {
+ } else {
+ assert j.toStr == i.toStr + [s];
+ forall k | 0 <= k < |j.toStr|
+ ensures k < |j.toStr| && j.toStr[k] in j.toId && j.toId[j.toStr[k]] == k
+ {
+ if k < |i.toStr| {
+ assert j.toStr[k] == i.toStr[k];
+ } else {
+ assert k == |i.toStr|;
+ assert j.toStr[k] == s;
+ }
+ }
+ }
+}
+
+// TCB-15. For EVERY string, of every length, at every point in the interner's
+// life.
+lemma RoundTrip(i: Interner, s: string)
+ requires Wf(i)
+ ensures var (j, id) := Intern(i, s); id < |j.toStr| && Resolve(j, id) == s
+{
+ InternPreservesWf(i, s);
+}
+
+// TCB-16, both directions. The unsound direction is left to right: one
+// identifier for two different strings is what would let a rule fire on a
+// premise nobody asserted.
+lemma Injective(i: Interner, a: string, b: string)
+ requires Wf(i)
+ ensures var (j, ida) := Intern(i, a);
+ var (k, idb) := Intern(j, b);
+ (ida == idb) <==> (a == b)
+{
+ InternPreservesWf(i, a);
+ var (j, ida) := Intern(i, a);
+ InternPreservesWf(j, b);
+ var (k, idb) := Intern(j, b);
+ assert Resolve(k, ida) == a by { RoundTripSurvives(i, a, b); }
+ assert Resolve(k, idb) == b;
+}
+
+// TCB-17. Interning something else does not move what an issued identifier
+// resolves to. This is what makes the identifier a NAME rather than a position
+// that shifts, and it is what `Injective` above leans on.
+lemma RoundTripSurvives(i: Interner, a: string, b: string)
+ requires Wf(i)
+ ensures var (j, ida) := Intern(i, a);
+ var (k, _) := Intern(j, b);
+ ida < |k.toStr| && Resolve(k, ida) == a
+{
+ InternPreservesWf(i, a);
+ var (j, ida) := Intern(i, a);
+ InternPreservesWf(j, b);
+ var (k, idb) := Intern(j, b);
+ assert Resolve(j, ida) == a;
+ assert k.toStr == j.toStr || k.toStr == j.toStr + [b];
+}
+
+// The sequence only ever grows, and never at the front. A single lemma
+// covering both statements the two above rely on.
+lemma Monotone(i: Interner, s: string)
+ requires Wf(i)
+ ensures var (j, _) := Intern(i, s);
+ |i.toStr| <= |j.toStr|
+ && forall k :: 0 <= k < |i.toStr| ==> j.toStr[k] == i.toStr[k]
+{
+}
+
+// Stability over an ARBITRARY run of later insertions, not just one. This is
+// the statement the property test was sampling: intern a string, then intern
+// any number of other strings, and the first identifier still resolves to what
+// it did.
+lemma StableAcrossManyInterns(i: Interner, a: string, rest: seq<string>)
+ requires Wf(i)
+ ensures var (j, ida) := Intern(i, a);
+ var k := InternAll(j, rest);
+ ida < |k.toStr| && Resolve(k, ida) == a
+ decreases |rest|
+{
+ InternPreservesWf(i, a);
+ var (j, ida) := Intern(i, a);
+ InternAllKeepsPrefix(j, rest);
+}
+
+function InternAll(i: Interner, ss: seq<string>): Interner
+ decreases |ss|
+{
+ if |ss| == 0 then i else InternAll(Intern(i, ss[0]).0, ss[1..])
+}
+
+lemma InternAllKeepsPrefix(i: Interner, ss: seq<string>)
+ requires Wf(i)
+ ensures var j := InternAll(i, ss);
+ Wf(j)
+ && |i.toStr| <= |j.toStr|
+ && forall k :: 0 <= k < |i.toStr| ==> j.toStr[k] == i.toStr[k]
+ decreases |ss|
+{
+ if |ss| == 0 {
+ } else {
+ InternPreservesWf(i, ss[0]);
+ var (j, _) := Intern(i, ss[0]);
+ InternAllKeepsPrefix(j, ss[1..]);
+ }
+}
diff --git a/dafny/run.sh b/dafny/run.sh
index 0358ea2b..4375b7c1 100755
--- a/dafny/run.sh
+++ b/dafny/run.sh
@@ -28,6 +28,7 @@ set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
src="$here/RuleTable.dfy"
+interner="$here/Interner.dfy"
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
@@ -39,16 +40,17 @@ fi
echo "== dafny $(dafny --version)"
echo
-echo "== 1. the specification verifies"
+echo "== 1. the specifications verify"
dafny verify "$src"
+dafny verify "$interner"
echo
# Each mutation is a `sed`-able edit that removes something the proof depends on.
# The third column is what it is meant to break, so a reader can tell a real
# rejection from an incidental one.
-mutate () {
- local name="$1" from="$2" to="$3" breaks="$4"
- python3 - "$src" "$work/mut.dfy" "$from" "$to" <<'PY'
+mutate_in () {
+ local target="$1" name="$2" from="$3" to="$4" breaks="$5"
+ python3 - "$target" "$work/mut.dfy" "$from" "$to" <<'PY'
import sys
src, dst, old, new = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
s = open(src).read()
@@ -72,23 +74,57 @@ echo "== 2. the proof can fail"
# docs/trusted-computing-base.md and it is FALSE unconditionally: a rule name
# carrying a tab renders to a line that reads back as a different rule. Removing
# the hypothesis that says otherwise must break the round-trip theorem.
-mutate "drop the tab-freeness of a rule name" \
+mutate_in "$src" "drop the tab-freeness of a rule name" \
' && NoByte(r.name, TAB)
' '' \
"RuleStrRoundTrips and the counterexample lemmas"
# The classification. `pat_of` must not treat a bare '?' as a constant, or a
# variable and a constant stop being distinguishable by their first byte.
-mutate "let pat_of read a bare '?' as a constant" \
+mutate_in "$src" "let pat_of read a bare '?' as a constant" \
'if |f| == 1 then None else Some(Var(f[1..]))' \
'if |f| == 1 then Some(Const(f)) else Some(Var(f[1..]))' \
"PatOfClassifies and VariablesAndConstantsDoNotCollide"
# The empty-name refusal, which is what makes a parsed rule writable.
-mutate "drop the empty-name refusal from ParseRuleLine" \
+mutate_in "$src" "drop the empty-name refusal from ParseRuleLine" \
'else if |fields[0]| == 0 then None' \
'else if false then None' \
"ParsedRulesAreWritable"
+
+# ---------------------------------------------------------------------------
+# Interner.dfy: TCB-15, TCB-16 and TCB-17, which Kani cannot reach at all.
+#
+# A harness over the real `Interner` returns 5108 of 5109 checks undetermined,
+# because `std`'s HashMap seeds RandomState through `CCRandomGenerateBytes`, a
+# foreign C function Kani does not model. Fixing the hasher removes that call
+# and the harness still fails, so the obstacle is HashMap rather than the seed
+# and no bound helps. These mutations are what stop this file being a
+# restatement of its own conclusion.
+
+echo
+echo "== 3. the interner proof can fail"
+
+# Reuse an id instead of appending. This is the collision that would let a rule
+# fire on a premise nobody asserted, so injectivity must break.
+mutate_in "$interner" "let intern reuse identifier zero" \
+ 'i.toId[s := |i.toStr|], i.toStr + [s]), |i.toStr|)' \
+ 'i.toId[s := 0], i.toStr + [s]), 0)' \
+ "Injective and RoundTrip"
+
+# Drop the clause tying the map back to the sequence. Without it an id can
+# resolve to a string nobody interned under it.
+mutate_in "$interner" "drop the map-to-sequence agreement from Wf" \
+ 'i.toStr[i.toId[s]] == s)' \
+ 'true)' \
+ "RoundTrip"
+
+# Let a later intern rewrite the front of the sequence. Stability must break.
+mutate_in "$interner" "let a later intern rewrite the sequence" \
+ 'i.toStr + [s]), |i.toStr|)' \
+ '[s] + i.toStr), |i.toStr|)' \
+ "RoundTripSurvives and StableAcrossManyInterns"
+
echo
-echo "== done. The specification verifies and all three mutations were rejected."
+echo "== done. Both specifications verify and all six mutations were rejected."
diff --git a/docs/ci-gates.md b/docs/ci-gates.md
index 67f4f8a6..940c58ea 100644
--- a/docs/ci-gates.md
+++ b/docs/ci-gates.md
@@ -162,7 +162,7 @@ The other seven files under `python/tests/` have no skip path.
`.lake` including Mathlib. `docs/aeneas-boundary.md` reports what they produced
when they were run here, which is the same arrangement Kani is under and for
the same reason: a gate nobody can run locally is not a gate.
-- **Dafny.** `dafny/run.sh` verifies `dafny/RuleTable.dfy` and then mutates it three times and
+- **Dafny.** `dafny/run.sh` verifies `dafny/RuleTable.dfy` and `dafny/Interner.dfy` and then mutates them six times and
requires every mutation to be rejected, so the script fails both when the specification breaks and
when the proof turns out not to depend on what it claims to. No job runs it, and `make
verify-dafny` is the only thing that does. It is in the same position as Kani and Aeneas and for
diff --git a/docs/trusted-computing-base.md b/docs/trusted-computing-base.md
index 7578aafc..7cf15173 100644
--- a/docs/trusted-computing-base.md
+++ b/docs/trusted-computing-base.md
@@ -337,7 +337,7 @@ of the code with nothing checking it. Nothing here is unbounded-verified, and a
| TCB-12 | property | proptest | both the built-in and the Horn path |
| TCB-13 | property | proptest | the name list is read out of `lean/OOCert/Rules.lean` at test time |
| TCB-14 | **enforced** | `BUILTIN_RULES`, `tests/premise_order_test.rs` | 27 of 31 sites get the order from one table; that table is compared with the `checkStep` arms parsed out of `Rules.lean`. `tests/lean_certificate_test.rs` still runs the real checker over the corpus |
-| TCB-15, TCB-16, TCB-17 | property | `src/reason.rs` unit property tests | the interner is private |
+| TCB-15, TCB-16, TCB-17 | property, and **proved unbounded of a MODEL** | `src/reason.rs` unit property tests, `dafny/Interner.dfy` | the interner is private. Kani cannot reach these at all: a harness over `Interner` returns 5,108 of 5,109 checks undetermined on `CCRandomGenerateBytes`, which is `std`'s `HashMap` seeding `RandomState` from the OS, and fixing the hasher does not help because the obstacle is `HashMap` rather than the seed. Dafny proves all three for every string and every number of insertions, OF A REIMPLEMENTATION AND NOT OF THIS RUST, which is decision 0014 and the row means nothing without it |
| TCB-18 | **enforced** + property | in the code, and proptest | the engine re-parses what it wrote and refuses on mismatch |
| TCB-19, TCB-21 | property | proptest, plus the REAL checker | property against a transcription of `OOCert.HornParse` in the test file, because a Lean process per case is not a property test. The transcription's fidelity is then an assumption, so one deterministic test puts the adversarial shapes (a numeric rule name, a non-ASCII one, a `??x` variable, a literal spelled exactly like an IRI as a constant, a blank node, an empty-bodied rule) through `oo-horn check` itself and requires the conditional verdict |
| TCB-20 | **proved** at a bound + property, and **proved unbounded of a MODEL** | `kani_harnesses::pat_of_and_render_are_inverse_at_2` and `_at_3`, proptest, `dafny/RuleTable.dfy` | `pat_of` and `Pat::render` are inverse over every byte pattern at two and three bytes. This is the harness the previous version of this page reported as NOT TERMINATING. The WHOLE-LINE statement, which Kani cannot afford because asserting over `body.split('\t')` puts CBMC inside `CharSearcher`, is proved with no length bound and no field-count bound in Dafny, OF A REIMPLEMENTATION AND NOT OF THIS RUST. That distinction is the whole of decision 0014 and the row means nothing without it |