Documentation

Footprint

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.

  1. 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.
  2. 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.
  3. 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).
  4. 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), and mir_schedule_sound is check_sound with no hind left: 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 #

Places and prefixes #

@[reducible, inline]

A place: a local and a path of projections (_3.0.1 is (3, [0, 1])).

Equations
Instances For

    lpre a b: the list a is a prefix of b.

    Equations
    Instances For
      theorem PsyncFootprint.lpre_iff (a b : List Nat) :
      lpre a b = true ↔ ∃ (t : List Nat), b = a ++ t

      $$\mathrm{lpre}(a, b) = \mathsf{true} \iff \exists t,\ b = a \mathbin{+\!\!+} t$$

      pre p q: place p contains place q (_3 contains _3.0.1).

      Equations
      Instances For
        theorem PsyncFootprint.pre_iff (p q : Pl) :
        pre p q = true ↔ p.fst = q.fst ∧ ∃ (t : List Nat), q.snd = p.snd ++ t

        $$p \sqsubseteq q \iff p_1 = q_1 \wedge \exists t,\ q_2 = p_2 \mathbin{+\!\!+} t$$

        theorem PsyncFootprint.pre_refl (p : Pl) :
        pre p p = true

        $$p \sqsubseteq p$$

        theorem PsyncFootprint.pre_trans {p q r : Pl} (h1 : pre p q = true) (h2 : pre q r = true) :
        pre p r = true

        $$p \sqsubseteq q \wedge q \sqsubseteq r \implies p \sqsubseteq r$$

        def PsyncFootprint.under (ps : List Pl) (l : Pl) :

        Some place of ps contains l.

        Equations
        Instances For

          Every place of ys is contained in a place of xs.

          Equations
          Instances For
            theorem PsyncFootprint.under_of_covers {xs ys : List Pl} (h : covers xs ys) {l : Pl} (hl : under ys l) :
            under xs l

            $$\mathrm{covers}(xs, ys) \wedge l \in {\downarrow}ys \implies l \in {\downarrow}xs$$

            theorem PsyncFootprint.under_append {xs ys : List Pl} {l : Pl} :
            under (xs ++ ys) l ↔ under xs l ∨ under ys l

            $$l \in {\downarrow}(xs \mathbin{+\!\!+} ys) \iff l \in {\downarrow}xs \vee l \in {\downarrow}ys$$

            theorem PsyncFootprint.under_mono {xs ys : List Pl} (h : ∀ (p : Pl), p ∈ ys → p ∈ xs) {l : Pl} (hl : under ys l) :
            under xs l

            $$ys \subseteq xs \wedge l \in {\downarrow}ys \implies l \in {\downarrow}xs$$

            $$l \in {\downarrow}[p] \iff p \sqsubseteq l$$

            Substitution: a callee's places in the caller's terms #

            def PsyncFootprint.subst (σ : Nat → Pl) (p : Pl) :

            σ 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.

            Equations
            Instances For
              theorem PsyncFootprint.subst_comp (σ τ : Nat → Pl) (p : Pl) :
              subst σ (subst τ p) = subst (fun (k : Nat) => subst σ (τ k)) p

              $$p[\tau][\sigma] = p\bigl[k \mapsto \tau(k)[\sigma]\bigr]$$

              theorem PsyncFootprint.pre_subst (σ : Nat → Pl) {p q : Pl} (h : pre p q = true) :
              pre (subst σ p) (subst σ q) = true

              $$p \sqsubseteq q \implies p[\sigma] \sqsubseteq q[\sigma]$$

              theorem PsyncFootprint.covers_map (σ : Nat → Pl) {xs ys : List Pl} (h : covers xs ys) :
              covers (List.map (subst σ) xs) (List.map (subst σ) ys)

              $$\mathrm{covers}(xs, ys) \implies \mathrm{covers}(xs[\sigma], ys[\sigma])$$

              The fragment #

              An operand: a copy of a (scalar) place, or a constant.

              Instances For
                @[reducible, inline]
                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 σ.

                    Instances For
                      def PsyncFootprint.exec (P : Nat → Stmt) :
                      Nat → Stmt → Mem → Mem

                      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
                      Instances For

                        Summaries #

                        @[reducible, inline]

                        A footprint: the places a statement may read, and those it may write.

                        Equations
                        Instances For
                          def PsyncFootprint.fp (S : Nat → Fp) :
                          Stmt → Fp

                          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
                          Instances For

                            $$R(o[\sigma]) = R(o)[\sigma]$$

                            theorem PsyncFootprint.fp_subst (S : Nat → Fp) (σ : Nat → Pl) (s : Stmt) :
                            fp S (Stmt.subst σ s) = (List.map (subst σ) (fp S s).fst, List.map (subst σ) (fp S s).snd)

                            $$\mathrm{fp}_S(s[\sigma]) = \bigl(R_S(s)[\sigma],\ W_S(s)[\sigma]\bigr)$$

                            The footprint of a substituted statement is the substituted footprint.

                            def PsyncFootprint.Valid (S : Nat → Fp) (P : Nat → Stmt) :

                            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
                            Instances For
                              def PsyncFootprint.touched (S : Nat → Fp) (s : Stmt) :

                              Reads and writes together: a statement may leave a place it might write unchanged, so its result there depends on the old value.

                              Equations
                              Instances For
                                theorem PsyncFootprint.call_covers {S : Nat → Fp} {P : Nat → Stmt} (hv : Valid S P) (f : Nat) (σ : Nat → Pl) :
                                covers (fp S (Stmt.call f σ)).snd (fp S (Stmt.subst σ (P f))).snd ∧ covers (touched S (Stmt.call f σ)) (touched S (Stmt.subst σ (P f)))

                                $$\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
                                Instances For

                                  The two theorems #

                                  theorem PsyncFootprint.frame {S : Nat → Fp} {P : Nat → Stmt} (hv : Valid S P) (n : Nat) (s : Stmt) (m : Mem) (l : Pl) :
                                  ¬under (fp S s).snd l → exec P n s m l = m l

                                  $$\mathrm{Valid}(S, P) \wedge l \notin {\downarrow}W_S(s) \implies \mathrm{exec}^n_P(s, m)(l) = m(l)$$

                                  Frame. A statement changes no place outside its writes.

                                  theorem PsyncFootprint.dep {S : Nat → Fp} {P : Nat → Stmt} (hv : Valid S P) (n : Nat) (s : Stmt) (X : Pl → Prop) :
                                  (∀ (l : Pl), under (touched S s) l → X l) → ∀ (m m' : Mem), (∀ (l : Pl), X l → m l = m' l) → ∀ (l : Pl), X l → exec P n s m l = exec P n s m' l

                                  $$\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 #

                                  def PsyncFootprint.Disjoint (S : Nat → Fp) (a b : Stmt) :

                                  Disjoint footprints, the independence of Schedule.lean: neither writes what the other touches.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    theorem PsyncFootprint.under_touched_of_write {S : Nat → Fp} {s : Stmt} {l : Pl} (h : under (fp S s).snd l) :
                                    under (touched S s) l

                                    $$l \in {\downarrow}W_S(s) \implies l \in {\downarrow}T_S(s)$$

                                    def PsyncFootprint.toCOp {S : Nat → Fp} {P : Nat → Stmt} (hv : Valid S P) (n : Nat) (s : Stmt) :

                                    A statement as a client step of World.lean: it sends no request.

                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      def PsyncFootprint.memSys {S : Nat → Fp} {P : Nat → Stmt} (hv : Valid S P) (n : Nat) :

                                      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
                                      Instances For

                                        From the certificate to hind #

                                        A place as the certificate writes it: the local, then the projections.

                                        Equations
                                        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
                                            theorem PsyncFootprint.overlap_common (holes a b t t' : List Nat) :
                                            a ++ t = b ++ t' → PsyncCert.overlap holes a b = true

                                            $$a \mathbin{+\!\!+} t = b \mathbin{+\!\!+} t' \implies \mathrm{overlap}_{\mathit{holes}}(a, b) = \mathsf{true}$$

                                            theorem PsyncFootprint.enc_ext {q : PsyncCert.Place} {p l : Pl} (hq : lpre q (enc p) = true) (hp : pre p l = true) :

                                            $$\mathrm{lpre}(q, \mathrm{enc}(p)) \wedge p \sqsubseteq l \implies \exists t,\ \mathrm{enc}(l) = q \mathbin{+\!\!+} t$$

                                            theorem PsyncFootprint.write_place {nd : PsyncCert.Node} {f : Fp} (hc : CoversNode nd f) {l : Pl} (h : under f.snd l) :

                                            $$\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$$

                                            theorem PsyncFootprint.touch_place {nd : PsyncCert.Node} {f : Fp} (hc : CoversNode nd f) {l : Pl} (h : under (f.fst ++ f.snd) l) :

                                            $$\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$$

                                            theorem PsyncFootprint.anyOverlap_of {holes : List Nat} {xs ys : List PsyncCert.Place} {a b : PsyncCert.Place} (ha : a ∈ xs) (hb : b ∈ ys) {t t' : List Nat} (h : a ++ t = b ++ t') :

                                            $$a \in xs \wedge b \in ys \wedge a \mathbin{+\!\!+} t = b \mathbin{+\!\!+} t' \implies \mathrm{anyOverlap}_{\mathit{holes}}(xs, ys) = \mathsf{true}$$

                                            theorem PsyncFootprint.hind_of_cover {S : Nat → Fp} {c : PsyncCert.Cert} {st : Nat → Stmt} (hcov : ∀ (i : Nat), CoversNode (c.node i) (fp S (st i))) (i j : Nat) :
                                            c.ind i j = true → Disjoint S (st i) (st j)

                                            $$\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.

                                            theorem PsyncFootprint.mir_schedule_sound {S : Nat → Fp} {P : Nat → Stmt} (hv : Valid S P) (n : Nat) (c : PsyncCert.Cert) (st : Nat → Stmt) (hcov : ∀ (i : Nat), CoversNode (c.node i) (fp S (st i))) (hc : PsyncCert.check c = true) {ms : List (List Stmt)} (hx : PsyncCert.Execs (List.map (List.map (List.map st)) c.levels) ms) (m : Mem) :

                                            $$\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.

                                            A finite program: each function's body and its summary (from its crate's summary file). Functions past the end are empty.

                                            Instances For
                                              Equations
                                              Instances For
                                                theorem PsyncFootprint.covers_of_coversB {xs ys : List Pl} (h : coversB xs ys = true) :
                                                covers xs ys

                                                $$\mathrm{coversB}(xs, ys) = \mathsf{true} \implies \mathrm{covers}(xs, ys)$$

                                                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

                                                  $$p.\mathrm{validB} = \mathsf{true} \implies \mathrm{Valid}(p.\mathrm{sum}, p.\mathrm{body})$$

                                                  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

                                                    $$\mathtt{prog}.\mathrm{validB} = \mathsf{true}$$

                                                    $$\mathrm{validB}\bigl(\mathtt{drain} : R = \emptyset,\ W = \{\_0\}\bigr) = \mathsf{false}$$

                                                    A summary that forgets the write to _1 is rejected by the link check.

                                                    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
                                                    Instances For
                                                      Equations
                                                      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.