Documentation

Cert

Certified schedules #

The theorems of Schedule.lean and World.lean are about every schedule that respects independence. This file turns them into a check of one schedule: the one psync generated for a given function. psync writes a certificate for each rewritten function (its statements' footprints and effects, and the levels of chains it chose), and check decides whether that schedule is safe. check_sound proves that when check says yes, every concurrent execution of the schedule equals the program as written, for any meaning of the statements whose real independence includes what the certificate claims.

This is translation validation, as in CompCert: rather than proving the macro correct for all inputs, each build's output carries a certificate that a small, proven checker accepts.

What remains assumed is exactly the hypothesis hind of check_sound: that the certificate's footprints and effects describe the statements (checked against MIR by cargo psync check, and the subject of the footprint-soundness work), and, for the outside world, that the commutativity specification is sound (World.lean).

Notation used in the theorem statements #

Certificates #

@[reducible, inline]

A client place, as a path of interned segments: self.groups might be [3, 7]. Two places overlap when one is a prefix of the other (self and self.groups).

Some segments are holes: the placeholders of a format! target, such as {dir} in format!("{dir}/cache.json"). A hole may stand for any text, so two places that first differ where either has a hole overlap; two that first differ at ordinary segments do not. Without holes this is exactly the prefix relation.

Equations
Instances For
    def PsyncCert.overlap (holes : List Nat) :
    Place → Place → Bool
    Equations
    Instances For
      def PsyncCert.anyOverlap (holes : List Nat) (xs ys : List Place) :
      Equations
      Instances For
        structure PsyncCert.Node :

        One statement as the certificate describes it: the places it may read and write. Every one of psync's edge rules is a place (schedule::depends; the emitter is cert_nodes in schedule.rs):

        • n/x: the binding x. A statement that introduces it writes it, one that mentions it reads it (data flow, rebinding, shadowing).
        • p/a/b: the access path a.b (conflicting accesses; a overlaps a.b).
        • w/<domain>/<target>: an effect on the outside world. An unknown target is the domain alone, which overlaps every target in it; a process or unknown callee is w itself.
        • x/i: the exit of statement i. Statement i writes it if it may exit or panic, and a later statement with a write effect (not speculate) reads it ("may exit before").
        • e: effect order. Every ordinary effect statement writes it.

        Where psync's own rule is finer than prefixes (string-prefix targets, services named by type), the emitter rounds towards conflict, so the checker may reject a schedule psync accepted, never the reverse.

        Instances For
          def PsyncCert.indep (holes : List Nat) (a b : Node) :

          No edge between two statements: neither writes a place the other reads or writes.

          Equations
          Instances For
            structure PsyncCert.Cert :

            A certificate: the statements in program order (node i is the i-th), and the schedule as levels of chains of node indices.

            Instances For
              def PsyncCert.Cert.node (c : Cert) (i : Nat) :
              Equations
              Instances For
                def PsyncCert.Cert.ind (c : Cert) (i j : Nat) :
                Equations
                Instances For

                  The checker #

                  The first occurrence of x in L, splitting L around it.

                  Equations
                  Instances For
                    def PsyncCert.reorderable (ind : Nat → Nat → Bool) :
                    List Nat → List Nat → Bool

                    L is P with statements moved earlier only past ones independent of them.

                    Equations
                    Instances For
                      def PsyncCert.pairwiseOK (ind : Nat → Nat → Bool) :

                      The chains of one level are pairwise independent.

                      Equations
                      Instances For

                        The checker: every level's chains are pairwise independent, and running the levels' chains one after another is a reordering of the program past independent statements only.

                        Equations
                        Instances For

                          Soundness #

                          theorem PsyncCert.split_spec {x : Nat} {L A B : List Nat} (h : split x L = some (A, B)) :
                          L = A ++ x :: B

                          $$\mathrm{split}(x, L) = (A, B) \implies L = A \mathbin{+\!\!+} [x] \mathbin{+\!\!+} B$$

                          theorem PsyncCert.reorderable_sound {S A : Type} (T : PsyncWorld.Sys S A) (op : Nat → A) (ind : Nat → Nat → Bool) (hind : ∀ (i j : Nat), ind i j = true → T.Ind (op i) (op j)) (P L : List Nat) :
                          reorderable ind P L = true → T.Reorders (List.map op P) (List.map op L)

                          $$\bigl(\forall i\,j,\ \mathrm{ind}(i,j) \Rightarrow \mathrm{op}_i \mathrel{I_T} \mathrm{op}_j\bigr) \implies \bigl(\mathrm{reorderable}(P, L) = \mathsf{true} \Rightarrow \mathrm{Reorders}_T(\mathrm{op}(P), \mathrm{op}(L))\bigr)$$

                          theorem PsyncCert.pairwiseOK_sound {S A : Type} (T : PsyncWorld.Sys S A) (op : Nat → A) (ind : Nat → Nat → Bool) (hind : ∀ (i j : Nat), ind i j = true → T.Ind (op i) (op j)) (chs : List (List Nat)) :
                          pairwiseOK ind chs = true → T.PairwiseInd (List.map (List.map op) chs)

                          $$\bigl(\forall i\,j,\ \mathrm{ind}(i,j) \Rightarrow \mathrm{op}_i \mathrel{I_T} \mathrm{op}_j\bigr) \implies \bigl(\mathrm{pairwiseOK}(chs) = \mathsf{true} \Rightarrow \mathrm{PairwiseInd}_T(\mathrm{op}(chs))\bigr)$$

                          inductive PsyncCert.Execs {A : Type} :
                          List (List (List A)) → List (List A) → Prop

                          What can happen at run time: each level's chains interleave in any way (the executor's choice), and levels run one after another. No independence is assumed here.

                          Instances For
                            theorem PsyncCert.execs_runs {S A : Type} (T : PsyncWorld.Sys S A) {levels : List (List (List A))} {ms : List (List A)} (hp : ∀ (cs : List (List A)), cs ∈ levels → T.PairwiseInd cs) (h : Execs levels ms) :
                            T.Runs levels ms

                            $$(\forall cs \in \mathit{levels},\ \mathrm{PairwiseInd}_T(cs)) \;\wedge\; \mathrm{Execs}(\mathit{levels}, ms) \implies \mathrm{Runs}_T(\mathit{levels}, ms)$$

                            $$\mathrm{flatten}\bigl(\mathrm{map}\ \mathrm{flatten}\ (\mathrm{op}(\mathit{levels}))\bigr) = \mathrm{op}\bigl(\mathrm{flatten}(\mathrm{map}\ \mathrm{flatten}\ \mathit{levels})\bigr)$$

                            theorem PsyncCert.check_sound {S A : Type} (T : PsyncWorld.Sys S A) (c : Cert) (op : Nat → A) (hind : ∀ (i j : Nat), c.ind i j = true → T.Ind (op i) (op j)) (hc : check c = true) {ms : List (List A)} (hx : Execs (List.map (List.map (List.map op)) c.levels) ms) (s : S) :

                            $$\bigl(\forall i\,j,\ c.\mathrm{ind}(i,j) \Rightarrow \mathrm{op}_i \mathrel{I_T} \mathrm{op}_j\bigr) \;\wedge\; \mathrm{check}(c) = \mathsf{true} \;\wedge\; \mathrm{Execs}(\mathrm{op}(c.\mathit{levels}), ms) \implies [\![ \mathrm{flatten}(ms) ]\!]_T(s) = [\![ \mathrm{op}_0, \ldots, \mathrm{op}_{n-1} ]\!]_T(s)$$

                            Soundness of the checker. Interpret each node i as a step op i of any system T (client memory as in Schedule.lean, or the client and server world of World.lean). Suppose the certificate's independence implies real independence (hind). Then if check accepts the certificate, every run of its schedule, in any interleaving of each level, ends exactly as the program run in order does.

                            Examples #

                            A certificate the checker accepts: profile_page (Basic concepts). user (node 0) is read by avatar (node 2); posts and friends (1, 3) are free. psync's schedule is one level of three chains: 0 -> 2, 1, 3. Places: user is [10], posts [11], avatar [12], friends [13], api [20], and the service behind api [30].

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For

                              $$\mathrm{check}(\mathtt{profilePage}) = \mathsf{true}$$

                              And one it rejects: the same statements with avatar scheduled beside user, which it reads.

                              Equations
                              Instances For

                                $$\mathrm{check}(\mathtt{profilePageWrong}) = \mathsf{false}$$

                                Holes: two writes to format!("{dir}/a") and format!("{dir}/b") (places t {dir} / a / and t {dir} / b /, with {dir} the hole [7]) may run side by side; a write to format!("{other}/a") may not run beside the first, since {other} may equal {dir}.

                                Equations
                                Instances For

                                  $$\mathrm{check}(\mathtt{holesOK}) = \mathsf{true}$$

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For

                                    $$\mathrm{check}(\mathtt{holesClash}) = \mathsf{false}$$