Documentation

CslibFrontier.Computability.PointerMachine.SymbolicExec

Symbolic execution of structured pointer-machine programs #

Correctness proofs of structured programs follow the program text. WP c x Q is the weakest-precondition form of ExecWithin (Cslib.Program.WP at the pointer machine), and the tactic pm_exec runs its rules: it executes data instructions and conditionals on a symbolic state and stops at loops and subprograms, which continue with WP.loopUntil, WP.loop and WP.call.

Two kinds of facts never need stating in a specification, because they are read off the program text and decided by pm_frame:

For a small program with a fixed text, pm_frame evaluates these. For a large one, or one whose text depends on a parameter, they follow by simp from the frames of its subprograms (Code.dataDests_seq and the like).

Main definitions #

Tactics #

@[reducible, inline]
abbrev Cslib.PointerMachine.WP {M : Model} {L : Layout} (c : Code M L) (x : State M L) (Q : State M L → ℕ → Prop) :

WP c x Q: the program c leads from x to a state y, in n instructions, with Q y n.

theorem Cslib.PointerMachine.WP.op {M : Model} {L : Layout} {o : Op M L} {x : State M L} {Q : State M L → ℕ → Prop} (h : Q (o.effect x) 1) :

A data instruction, with the state it reaches.

theorem Cslib.PointerMachine.WP.exists_execWithin {M : Model} {L : Layout} {c : Code M L} {x : State M L} {T : ℕ} {P : State M L → Prop} (h : WP c x fun (y : State M L) (n : ℕ) => n ≤ T ∧ P y) :
∃ (y : State M L), ExecWithin c x y T ∧ P y

A specification is a weakest precondition whose postcondition bounds the count.

Data instructions as assignments #

theorem Cslib.PointerMachine.Op.effect_null {M : Model} {L : Layout} (x : State M L) (r : Fin L.ptrRegs) :
(null r).effect x = x.setPtr r none
theorem Cslib.PointerMachine.Op.effect_movePtr {M : Model} {L : Layout} (x : State M L) (r r' : Fin L.ptrRegs) :
(movePtr r r').effect x = x.setPtr r (x.ptr r')
theorem Cslib.PointerMachine.Op.effect_moveData {M : Model} {L : Layout} (x : State M L) (s s' : Fin L.dataRegs) :
(moveData s s').effect x = x.setData s (x.data s')
theorem Cslib.PointerMachine.Op.effect_op {M : Model} {L : Layout} (x : State M L) (o : M.Op) (s s₁ s₂ : Fin L.dataRegs) :
(op o s s₁ s₂).effect x = x.setData s (M.evalOp o (x.data s₁) (x.data s₂))
theorem Cslib.PointerMachine.Op.effect_loadPtr {M : Model} {L : Layout} (x : State M L) (r : Fin L.ptrRegs) (f : Fin L.ptrFields) (base : Fin L.ptrRegs) :
(loadPtr r f base).effect x = (x.deref base).elim x fun (n : Record M L) => x.setPtr r (n.ptr f)
theorem Cslib.PointerMachine.Op.effect_loadData {M : Model} {L : Layout} (x : State M L) (s : Fin L.dataRegs) (f : Fin L.dataFields) (base : Fin L.ptrRegs) :
(loadData s f base).effect x = (x.deref base).elim x fun (n : Record M L) => x.setData s (n.data f)
theorem Cslib.PointerMachine.Op.effect_storePtr {M : Model} {L : Layout} (x : State M L) (f : Fin L.ptrFields) (base r : Fin L.ptrRegs) :
(storePtr f base r).effect x = x.modify base fun (n : Record M L) => { ptr := Function.update n.ptr f (x.ptr r), data := n.data }
theorem Cslib.PointerMachine.Op.effect_storeData {M : Model} {L : Layout} (x : State M L) (f : Fin L.dataFields) (base : Fin L.ptrRegs) (s : Fin L.dataRegs) :
(storeData f base s).effect x = x.modify base fun (n : Record M L) => { ptr := n.ptr, data := Function.update n.data f (x.data s) }
theorem Cslib.PointerMachine.Op.effect_create {M : Model} {L : Layout} (x : State M L) (r : Fin L.ptrRegs) :
(create r).effect x = have __src := x.setPtr r (some x.heap.length); { pc := __src.pc, ptr := __src.ptr, data := __src.data, heap := x.heap ++ [Record.blank] }
theorem Cslib.PointerMachine.Test.holds_ptrEq {M : Model} {L : Layout} (x : State M L) (r₁ r₂ : Fin L.ptrRegs) :
Holds x (ptrEq r₁ r₂) ↔ x.ptr r₁ = x.ptr r₂
theorem Cslib.PointerMachine.Test.holds_data {M : Model} {L : Layout} (x : State M L) (p : M.Test) (s₁ s₂ : Fin L.dataRegs) :
Holds x (data p s₁ s₂) ↔ M.evalTest p (x.data s₁) (x.data s₂) = true
theorem Cslib.PointerMachine.State.heap_modify {M : Model} {L : Layout} (x : State M L) {r : Fin L.ptrRegs} {a : ℕ} (h : x.ptr r = some a) (g : Record M L → Record M L) :
(x.modify r g).heap = x.heap.modify a g

A record store through a register that specifies address a modifies the record there.

Frames read off the program text #

The pointer register a data instruction assigns, if any.

The data register a data instruction assigns, if any.

Whether a data instruction stores into a record or creates one.

theorem Cslib.PointerMachine.Op.ptr_effect_of_ne {M : Model} {L : Layout} {o : Op M L} {r : Fin L.ptrRegs} (h : o.ptrDest ≠ some r) (x : State M L) :
(o.effect x).ptr r = x.ptr r
theorem Cslib.PointerMachine.Op.data_effect_of_ne {M : Model} {L : Layout} {o : Op M L} {s : Fin L.dataRegs} (h : o.dataDest ≠ some s) (x : State M L) :
(o.effect x).data s = x.data s
theorem Cslib.PointerMachine.Op.heap_effect {M : Model} {L : Layout} {o : Op M L} (h : o.writesHeap = false) (x : State M L) :
(o.effect x).heap = x.heap

The data field a data instruction stores into, if any.

Whether a data instruction creates a record.

theorem Cslib.PointerMachine.Op.length_effect {M : Model} {L : Layout} {o : Op M L} (h : o.creates = false) (x : State M L) :

The data fields that the instructions of the program store into.

No instruction of the program creates a record.

Instances For
    @[instance_reducible]

    The pointer registers that the instructions of the program assign.

    The data registers that the instructions of the program assign.

    No instruction of the program stores into a record or creates one.

    Instances For
      @[instance_reducible]

      Frames of composite programs #

      The frames of a program are those of its parts, so a large program, or one whose text is not fixed, such as a store per field of a record, gets its frames by simp from lemmas about its subprograms instead of by evaluating its whole text.

      theorem Cslib.PointerMachine.ExecWithin.ptr_keep {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {T : ℕ} (run : ExecWithin c x y T) (ws : List (Fin L.ptrRegs)) (sub : c.ptrDests ⊆ ws) {r : Fin L.ptrRegs} (hr : r ∉ ws) :
      y.ptr r = x.ptr r

      Executions keep the pointer registers outside any list containing those the program assigns.

      theorem Cslib.PointerMachine.ExecWithin.data_keep {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {T : ℕ} (run : ExecWithin c x y T) (ws : List (Fin L.dataRegs)) (sub : c.dataDests ⊆ ws) {s : Fin L.dataRegs} (hs : s ∉ ws) :
      y.data s = x.data s

      Executions keep the data registers outside any list containing those the program assigns.

      theorem Cslib.PointerMachine.ExecWithin.length_eq {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {T : ℕ} (run : ExecWithin c x y T) (keep : c.KeepsSize) :

      Executions of a program that creates no record keep the number of records.

      theorem Cslib.PointerMachine.ExecWithin.field_keep {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {T : ℕ} (run : ExecWithin c x y T) (keep : c.KeepsSize) {f : Fin L.dataFields} (hf : f ∉ c.fieldDests) (a : ℕ) :
      Option.map (fun (r : Record M L) => r.data f) y.heap[a]? = Option.map (fun (r : Record M L) => r.data f) x.heap[a]?

      Executions of a program that creates no record keep every data field it does not store into, at every address.

      theorem Cslib.PointerMachine.ExecWithin.heap_eq {M : Model} {L : Layout} {c : Code M L} {x y : State M L} {T : ℕ} (run : ExecWithin c x y T) (keep : c.KeepsHeap) :
      y.heap = x.heap

      Executions of a program that neither stores nor creates keep all records.

      Tactics #

      pm_frame decides a frame condition c.ptrDests ⊆ ws, c.dataDests ⊆ ws or c.KeepsHeap from the program text, by evaluation.

      pm_exec executes the data instructions and conditionals of the program in a goal WP c x Q, keeping the state reached in assignment form with the simp set pm_simps and the given lemmas. It stops at loops and at programs given by name, unless the name is among the given lemmas, in which case the program is unfolded and executed; at the end of the program it leaves the postcondition. A conditional takes the alternative that the simplified test decides, and otherwise splits into both, with the test or its negation as hypothesis ht.