Footprint soundness #
check_sound (Cert.lean) proves a certified schedule correct given hind: that the
certificate's places describe what the statements really touch. This file proves hind for a
fragment of MIR, so that for programs in the fragment nothing about footprints stays assumed.
- The fragment (
Stmt): places (a local and a path of field projections), assignments from operands, moves of whole aggregates, sequencing,if,while, and calls that pass places by reference. Execution has fuel (exec), so loops and recursion are total; every theorem holds for every fuel, so for every terminating run. - Summaries (
fp): the places a statement may read and write, computed from the statement's syntax and, for a call, from the callee's summary with parameters replaced by the arguments. This is psync's cross-crate scheme: a crate's summaries are computed from its MIR and stored per function; a caller uses them without seeing the callee's body. - The link check (
Valid): every summary covers the footprint of its function's body, computed with the summaries. It is a local check per function, so it handles recursion and functions from other crates alike; it is decidable for a finite program (Program.validB). - Theorems: if the summaries are valid, a statement changes nothing outside its writes
(
frame), and what it writes depends only on what it reads and writes (dep). So statements with disjoint footprints commute (memSys), certificate independence implies real independence (hind_of_cover), andmir_schedule_soundischeck_soundwith nohindleft: for a valid program and a certificate that covers the footprints, every run of the schedule ends as the sequential program does.
What the fragment leaves out, and so stays assumed for real Rust: references stored in memory and
dereferenced (*p: the borrow checker's aliasing guarantees are what lets psync name places, and
they are not modelled), interior mutability and unsafe, panics and unwinding, and the
correspondence between real MIR and this fragment.
lake build PsyncVerify
Notation used in the theorem statements #
- A place $p = (p_1, p_2)$ is a local and a path of projections; $p \sqsubseteq q$ (
pre): $p$ is a prefix of $q$, so writing $p$ may change $q$. - ${\downarrow}xs$ (
under): the places at or below some place of $xs$; $\mathrm{covers}(xs, ys)$: ${\downarrow}ys \subseteq {\downarrow}xs$. - $p[\sigma]$ substitutes the arguments $\sigma$ for a callee's parameters.
- $\mathrm{fp}_S(s) = (R_S(s), W_S(s))$ is statement $s$'s footprint computed with the summaries $S$, and $T_S(s) = R_S(s) \mathbin{+\!\!+} W_S(s)$ is everything it touches.
- $\mathrm{exec}^n_P(s, m)$ runs $s$ from memory $m$ with fuel $n$ in program $P$; $\mathrm{Valid}(S, P)$ is the link check: every summary covers its body's footprint.
Places and prefixes #
A place: a local and a path of projections (_3.0.1 is (3, [0, 1])).
Instances For
pre p q: place p contains place q (_3 contains _3.0.1).
Equations
- PsyncFootprint.pre p q = (p.fst == q.fst && PsyncFootprint.lpre p.snd q.snd)
Instances For
Some place of ps contains l.
Equations
- PsyncFootprint.under ps l = ∃ (p : PsyncFootprint.Pl), p ∈ ps ∧ PsyncFootprint.pre p l = true
Instances For
Every place of ys is contained in a place of xs.
Equations
- PsyncFootprint.covers xs ys = ∀ (y : PsyncFootprint.Pl), y ∈ ys → ∃ (x : PsyncFootprint.Pl), x ∈ xs ∧ PsyncFootprint.pre x y = true
Instances For
Substitution: a callee's places in the caller's terms #
σ k is the caller's place passed for the callee's local k (its parameters, and fresh
caller places for its temporaries). The callee's _k.f becomes σ k followed by .f.
Instances For
The fragment #
Equations
Instances For
Equations
- (PsyncFootprint.Op.place p).eval x✝ = x✝ p
- (PsyncFootprint.Op.const v).eval x✝ = v
Instances For
Equations
Instances For
Equations
Instances For
A MIR-like statement. assign d f a b is d = f(a, b) for any pure operation f (a binary
operator, a cast, a plain copy). move d s copies the whole aggregate at s to d. call f σ
runs function f's body with its locals mapped to the caller's places by σ.
- nop : Stmt
- assign (d : Pl) (f : Int → Int → Int) (a b : Op) : Stmt
- move (d s : Pl) : Stmt
- seq (a b : Stmt) : Stmt
- ite (c : Pl) (t e : Stmt) : Stmt
- while (c : Pl) (body : Stmt) : Stmt
- call (f : Nat) (σ : Nat → Pl) : Stmt
Instances For
A statement of a callee, in the caller's terms.
Equations
- PsyncFootprint.Stmt.subst σ PsyncFootprint.Stmt.nop = PsyncFootprint.Stmt.nop
- PsyncFootprint.Stmt.subst σ (PsyncFootprint.Stmt.assign d f a b) = PsyncFootprint.Stmt.assign (PsyncFootprint.subst σ d) f (PsyncFootprint.Op.subst σ a) (PsyncFootprint.Op.subst σ b)
- PsyncFootprint.Stmt.subst σ (PsyncFootprint.Stmt.move d s) = PsyncFootprint.Stmt.move (PsyncFootprint.subst σ d) (PsyncFootprint.subst σ s)
- PsyncFootprint.Stmt.subst σ (a.seq b) = (PsyncFootprint.Stmt.subst σ a).seq (PsyncFootprint.Stmt.subst σ b)
- PsyncFootprint.Stmt.subst σ (PsyncFootprint.Stmt.ite c t e) = PsyncFootprint.Stmt.ite (PsyncFootprint.subst σ c) (PsyncFootprint.Stmt.subst σ t) (PsyncFootprint.Stmt.subst σ e)
- PsyncFootprint.Stmt.subst σ (PsyncFootprint.Stmt.while c b) = PsyncFootprint.Stmt.while (PsyncFootprint.subst σ c) (PsyncFootprint.Stmt.subst σ b)
- PsyncFootprint.Stmt.subst σ (PsyncFootprint.Stmt.call g τ) = PsyncFootprint.Stmt.call g fun (k : Nat) => PsyncFootprint.subst σ (τ k)
Instances For
Execution with fuel. P f is the body of function f. Out of fuel, a statement stops where it
is; the theorems hold for every fuel, so in particular for every run that terminates.
Equations
- PsyncFootprint.exec P 0 x✝¹ x✝ = x✝
- PsyncFootprint.exec P n.succ PsyncFootprint.Stmt.nop x✝ = x✝
- PsyncFootprint.exec P n.succ (PsyncFootprint.Stmt.assign d f a b) x✝ = fun (l : PsyncFootprint.Pl) => if l = d then f (a.eval x✝) (b.eval x✝) else x✝ l
- PsyncFootprint.exec P n.succ (PsyncFootprint.Stmt.move d s) x✝ = fun (l : PsyncFootprint.Pl) => if PsyncFootprint.pre d l = true then x✝ (s.fst, s.snd ++ List.drop d.snd.length l.snd) else x✝ l
- PsyncFootprint.exec P n.succ (a.seq b) x✝ = PsyncFootprint.exec P n b (PsyncFootprint.exec P n a x✝)
- PsyncFootprint.exec P n.succ (PsyncFootprint.Stmt.ite c t e) x✝ = if x✝ c ≠ 0 then PsyncFootprint.exec P n t x✝ else PsyncFootprint.exec P n e x✝
- PsyncFootprint.exec P n.succ (PsyncFootprint.Stmt.while c b) x✝ = if x✝ c ≠ 0 then PsyncFootprint.exec P n (PsyncFootprint.Stmt.while c b) (PsyncFootprint.exec P n b x✝) else x✝
- PsyncFootprint.exec P n.succ (PsyncFootprint.Stmt.call f σ) x✝ = PsyncFootprint.exec P n (PsyncFootprint.Stmt.subst σ (P f)) x✝
Instances For
Summaries #
A footprint: the places a statement may read, and those it may write.
Equations
Instances For
The footprint of a statement, given a summary S f for every function, in the callee's terms.
This is what psync computes from MIR, per function, and stores per crate.
Equations
- PsyncFootprint.fp S PsyncFootprint.Stmt.nop = ([], [])
- PsyncFootprint.fp S (PsyncFootprint.Stmt.assign d f a b) = (a.reads ++ b.reads, [d])
- PsyncFootprint.fp S (PsyncFootprint.Stmt.move d s) = ([s], [d])
- PsyncFootprint.fp S (a.seq b) = ((PsyncFootprint.fp S a).fst ++ (PsyncFootprint.fp S b).fst, (PsyncFootprint.fp S a).snd ++ (PsyncFootprint.fp S b).snd)
- PsyncFootprint.fp S (PsyncFootprint.Stmt.ite c t e) = (c :: ((PsyncFootprint.fp S t).fst ++ (PsyncFootprint.fp S e).fst), (PsyncFootprint.fp S t).snd ++ (PsyncFootprint.fp S e).snd)
- PsyncFootprint.fp S (PsyncFootprint.Stmt.while c b) = (c :: (PsyncFootprint.fp S b).fst, (PsyncFootprint.fp S b).snd)
- PsyncFootprint.fp S (PsyncFootprint.Stmt.call g τ) = (List.map (PsyncFootprint.subst τ) (S g).fst, List.map (PsyncFootprint.subst τ) (S g).snd)
Instances For
The link check. Every function's summary covers the footprint of its body, computed with the summaries: writes by writes, reads by reads or writes.
Equations
- PsyncFootprint.Valid S P = ∀ (f : Nat), PsyncFootprint.covers (S f).snd (PsyncFootprint.fp S (P f)).snd ∧ PsyncFootprint.covers ((S f).fst ++ (S f).snd) (PsyncFootprint.fp S (P f)).fst
Instances For
Reads and writes together: a statement may leave a place it might write unchanged, so its result there depends on the old value.
Equations
- PsyncFootprint.touched S s = (PsyncFootprint.fp S s).fst ++ (PsyncFootprint.fp S s).snd
Instances For
$$\mathrm{Valid}(S, P) \implies \mathrm{covers}\bigl(W_S(\mathrm{call}\ f\ \sigma),\ W_S(P_f[\sigma])\bigr) \wedge \mathrm{covers}\bigl(T_S(\mathrm{call}\ f\ \sigma),\ T_S(P_f[\sigma])\bigr)$$
Membership in a footprint that is part of another one.
Equations
- PsyncFootprint.tacticMem_tac = Lean.ParserDescr.node `PsyncFootprint.tacticMem_tac 1024 (Lean.ParserDescr.nonReservedSymbol "mem_tac" false)
Instances For
The two theorems #
$$\mathrm{Valid}(S, P) \wedge {\downarrow}T_S(s) \subseteq X \wedge m|_X = m'|_X \implies \mathrm{exec}^n_P(s, m)|_X = \mathrm{exec}^n_P(s, m')|_X$$
Dependence. Take any set of places X containing everything a statement touches. If two
memories agree on X before the statement, they agree on X after it.
Disjoint footprints commute #
The statements of the fragment, with fuel n, as a system whose independence is disjoint
footprints. Commutation is upd_comm of World.lean, from frame and dep.
Equations
- PsyncFootprint.memSys hv n = { run := PsyncFootprint.exec P n, Ind := PsyncFootprint.Disjoint S, comm := ⋯ }
Instances For
From the certificate to hind #
A place as the certificate writes it: the local, then the projections.
Equations
- PsyncFootprint.enc p = p.fst :: p.snd
Instances For
The certificate's node covers a footprint: every written place extends a place the node writes, every read place one it reads or writes.
Equations
- One or more equations did not get rendered due to their size.
Instances For
$$a \mathbin{+\!\!+} t = b \mathbin{+\!\!+} t' \implies \mathrm{overlap}_{\mathit{holes}}(a, b) = \mathsf{true}$$
$$\mathrm{CoversNode}(nd, f) \wedge l \in {\downarrow}W_f \implies \exists q \in nd.\mathrm{writes},\ \exists t,\ \mathrm{enc}(l) = q \mathbin{+\!\!+} t$$
$$\mathrm{CoversNode}(nd, f) \wedge l \in {\downarrow}(R_f \mathbin{+\!\!+} W_f) \implies \exists q \in nd.\mathrm{reads} \mathbin{+\!\!+} nd.\mathrm{writes},\ \exists t,\ \mathrm{enc}(l) = q \mathbin{+\!\!+} t$$
$$a \in xs \wedge b \in ys \wedge a \mathbin{+\!\!+} t = b \mathbin{+\!\!+} t' \implies \mathrm{anyOverlap}_{\mathit{holes}}(xs, ys) = \mathsf{true}$$
$$\bigl(\forall i,\ \mathrm{CoversNode}(c_i, \mathrm{fp}_S(st_i))\bigr) \implies \bigl(\forall i\,j,\ c.\mathrm{ind}(i, j) \Rightarrow \mathrm{Disjoint}_S(st_i, st_j)\bigr)$$
hind, proved. If every certificate node covers its statement's footprint, the
certificate's independence implies disjoint footprints.
$$\mathrm{Valid}(S, P) \wedge \bigl(\forall i,\ \mathrm{CoversNode}(c_i, \mathrm{fp}_S(st_i))\bigr) \wedge \mathrm{check}(c) = \mathsf{true} \wedge \mathrm{Execs}(st(c.\mathit{levels}), ms) \implies [\![ \mathrm{flatten}(ms) ]\!]_{\mathrm{mem}}(m) = [\![ st_0, \ldots, st_{n-1} ]\!]_{\mathrm{mem}}(m)$$
Certified schedules of MIR, with nothing about footprints assumed. For a program whose
summaries pass the link check, and a certificate whose nodes cover the statements' footprints, if
check accepts the certificate then every run of its schedule, in any interleaving of each
level, ends with the same memory as the statements run in order.
The link check, decided #
Equations
- PsyncFootprint.coversB xs ys = ys.all fun (y : PsyncFootprint.Pl) => xs.any fun (x : PsyncFootprint.Pl) => PsyncFootprint.pre x y
Instances For
The link step's check: for every function, the summary covers the body's footprint.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Example: a recursive callee, called on two fields #
fn drain(_0: &mut u64, _1: &mut u64) { if *_0 != 0 { *_1 += 1; *_0 -= 1; drain(_0, _1) } }.
It is recursive, so its summary is checked against its own body.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Instances For
The caller: drain(&mut s.a, &mut s.b) and drain(&mut s.c, &mut s.d), with s local 5.
The certificate puts them in one level; it covers the footprints, and check accepts it.
Equations
- PsyncFootprint.callers 0 = PsyncFootprint.Stmt.call 0 fun (k : Nat) => (5, [k])
- PsyncFootprint.callers 1 = PsyncFootprint.Stmt.call 0 fun (k : Nat) => (5, [k + 2])
- PsyncFootprint.callers x✝ = PsyncFootprint.Stmt.nop
Instances For
$$\mathrm{check}(\mathtt{callersCert}) = \mathsf{true}$$
$$\forall i,\ \mathrm{CoversNode}\bigl(\mathtt{callersCert}_i,\ \mathrm{fp}(\mathtt{callers}_i)\bigr)$$
$$\mathrm{Execs}(\mathtt{callers}(\mathtt{callersCert}.\mathit{levels}), ms) \implies [\![ \mathrm{flatten}(ms) ]\!]_{\mathrm{mem}}(m) = [\![ \mathtt{callers}_0, \mathtt{callers}_1 ]\!]_{\mathrm{mem}}(m) \quad \text{for every fuel}$$
Both calls in parallel equal both in order, for every fuel, with no hypothesis left.