Library QedkConcrete.memorycomposition

Memory instructions with a concrete allocation-length check

The existing composition lemmas retain their failure alternative. Since List.init now uses integer comparison, that alternative is impossible; the assembled executor discharges it with the lemma below.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.memorywords QedkConcrete.vmmachinewords
  QedkConcrete.wordrange.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

Definition badMemoryInitComparison : Prop :=
  ∃ count : Z, 0 ≤ count ∧ Z.ltb count 0 = true.

Lemma no_bad_memory_init_comparison : ¬ badMemoryInitComparison.
Proof. intros [count [Hcount Hbad]]. apply Z.ltb_lt in Hbad. lia. Qed.

Section Memory.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context {UN : Basics.Unreachable Support.UintN.t} {UR : Basics.Unreachable U.Repr.t}
  {UF : Basics.Unreachable U.t} {UB : Basics.Unreachable Byte_string.Bytes.t}
  {UU : Basics.Unreachable unit} {US : Basics.Unreachable string}
  {UL : Basics.Unreachable (list string)} {ULI : Basics.Unreachable (list int)}
  {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
  {UP : Basics.Unreachable (int × int)}
  {UZ : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}.

Lemma read_word_range_or_bad_comparison start memory :
  memoryWordChunks memory →
  wordRange (@Mem.read_word_at Host_t F start memory) ∨
  badMemoryInitComparison.
Proof.
  intro Hmemory.
  set (count := (snd (@Mem.align Host_t F (@U.to_int start)) + 32 + 31) ÷ 32).
  pose proof (align_offset_range (@U.to_int start)) as Hoffset.
  pose proof (chunks_cover _ 32 (proj1 Hoffset) ltac:(lia)) as [Hcount _].
  fold count in Hcount.
  destruct (Z.ltb count 0) eqn:Hguard.
  - right. ∃ count. split; assumption.
  - left. apply read_word_range; assumption.
Qed.

A successful VM memory extension supplies this size bound; it is not an extra assumption on VM execution.
Lemma write_block_chunks_or_bad_comparison start bytes memory :
  memoryWordChunks memory → OCamlString.length bytes < 2^32 →
  memoryWordChunks (@Mem.write_block_at Host_t F start bytes memory) ∨
  badMemoryInitComparison.
Proof.
  intros Hmemory Hsize. left.
  apply write_block_word_chunks; try assumption.
  apply Z.ltb_ge.
  pose proof (align_offset_range (@U.to_int start)) as Hoffset.
  pose proof (chunks_cover _ (OCamlString.length bytes) (proj1 Hoffset)
    ltac:(unfold OCamlString.length; lia)) as [Hcount _]. exact Hcount.
Qed.

Lemma write_word_chunks_or_bad_comparison start value memory :
  memoryWordChunks memory →
  memoryWordChunks (@Mem.write_word_at Host_t F start value memory) ∨
  badMemoryInitComparison.
Proof.
  intro Hmemory.
  set (count := (snd (@Mem.align Host_t F (@U.to_int start)) + 32 + 31) ÷ 32).
  pose proof (align_offset_range (@U.to_int start)) as Hoffset.
  pose proof (chunks_cover _ 32 (proj1 Hoffset) ltac:(lia)) as [Hcount _].
  fold count in Hcount.
  destruct (Z.ltb count 0) eqn:Hguard.
  - right. ∃ count. split; assumption.
  - left. apply write_word_chunks; assumption.
Qed.

Lemma write_byte_chunks_or_bad_comparison start byte memory :
  memoryWordChunks memory →
  memoryWordChunks (@Mem.write_byte_at Host_t F start byte memory) ∨
  badMemoryInitComparison.
Proof.
  intro Hmemory.
  set (count := (snd (@Mem.align Host_t F (@U.to_int start)) + 1 + 31) ÷ 32).
  pose proof (align_offset_range (@U.to_int start)) as Hoffset.
  pose proof (chunks_cover _ 1 (proj1 Hoffset) ltac:(lia)) as [Hcount _].
  fold count in Hcount.
  destruct (Z.ltb count 0) eqn:Hguard.
  - right. ∃ count. split; assumption.
  - left. apply write_byte_chunks; assumption.
Qed.
End Memory.

Section Composition.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (invariant : Host_t → Prop).

Definition memorySafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
  ∀ state, machineWords invariant state →
    (machineWords invariant (snd (action state)) ∧
      match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end) ∨
    badMemoryInitComparison.

Lemma memory_safe_lift {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
  machineSafe invariant P action → memorySafe P action.
Proof. intros Hsafe state Hwords. left. apply Hsafe. exact Hwords. Qed.

Lemma memory_safe_bind {A B : Set} (P : A → Prop) (Q : B → Prop)
    (action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
  memorySafe P action → (∀ value, P value → memorySafe Q (next value)) →
  memorySafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Haction Hnext state Hwords.
  destruct (Haction state Hwords) as [Hprefix|Hfailure]; [|right; exact Hfailure].
  change ((machineWords invariant (snd
    (match action state with (inl value, middle) ⇒ next value middle | (inr err, middle) ⇒ (inr err, middle) end)) ∧
    match fst
    (match action state with (inl value, middle) ⇒ next value middle | (inr err, middle) ⇒ (inr err, middle) end)
    with inl value ⇒ Q value | inr _ ⇒ True end) ∨ badMemoryInitComparison).
  destruct (action state) as [[value|error] middle]; cbn [fst snd] in Hprefix.
  - apply Hnext; [exact (proj2 Hprefix)|exact (proj1 Hprefix)].
  - left. exact Hprefix.
Qed.

Lemma memory_safe_read_word
    {UN : Basics.Unreachable Support.UintN.t} {UR : Basics.Unreachable U.Repr.t}
    {UF : Basics.Unreachable U.t} {US : Basics.Unreachable string} {UU : Basics.Unreachable unit}
    {UL : Basics.Unreachable (list string)} {UI : Basics.Unreachable int} {UC : Basics.Unreachable ascii}
    {UZ : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)} start :
  memorySafe wordRange (@V.M.op_ltdollargt Host_t F _ _
    (@Mem.read_word_at Host_t F start)
    (@V.M.op_exclamation Host_t F _ MS.memory)).
Proof.
  intros state Hwords.
  destruct (@read_word_range_or_bad_comparison Host_t F UC UZ start (MS.t.memory state)
    (proj2 (proj2 (proj2 Hwords)))) as [Hword|Hfailure].
  - left. split; assumption.
  - right. exact Hfailure.
Qed.

Lemma memory_safe_fmap_ignore {A B : Set} (f : A → B) (action : @V.M.t Host_t F A) :
  memorySafe (fun _ ⇒ True) action →
  memorySafe (fun _ ⇒ True) (@V.M.op_ltdollargt Host_t F A B f action).
Proof.
  intro Haction. change (memorySafe (fun _ ⇒ True)
    (@V.M.op_letdollar Host_t F A B action (fun value ⇒ @V.M._return Host_t F B (f value)))).
  eapply memory_safe_bind; [exact Haction|]. intros value _.
  apply memory_safe_lift, safe_return. exact I.
Qed.

Lemma memory_safe_update f :
  (∀ memory, memoryWordChunks memory → memoryWordChunks (f memory) ∨ badMemoryInitComparison) →
  memorySafe (fun _ ⇒ True) (@V.M.update_field Host_t F _ MS.memory f).
Proof.
  intros Hupdate state [Hstack [Hpc [Hhost Hmemory]]].
  destruct (Hupdate (MS.t.memory state) Hmemory) as [Hafter|Hfailure].
  - left. split; [exact (conj Hstack (conj Hpc (conj Hhost Hafter)))|exact I].
  - right. exact Hfailure.
Qed.

Lemma memory_safe_host_frame {A : Set} (action : Host_t → A × Host_t) :
  (∀ host, invariant host → invariant (snd (action host)) ∨ badMemoryInitComparison) →
  memorySafe (fun _ ⇒ True) (@V.M.HostAPI.lift Host_t F A action).
Proof.
  intros Haction state [Hstack [Hpc [Hhost Hmemory]]].
  destruct (Haction (MS.t.host state) Hhost) as [Hafter|Hfailure].
  - left. unfold V.M.HostAPI.lift.
    destruct (action (MS.t.host state)) as [value host]. cbn [snd] in Hafter.
    split; [exact (conj Hstack (conj Hpc (conj Hafter Hmemory)))|exact I].
  - right. exact Hfailure.
Qed.

Lemma memory_safe_call msg :
  (∀ host, invariant host →
    invariant (snd ((@V.Host Host_t F).(Evmc.HOST.call) msg host)) ∨ badMemoryInitComparison) →
  memorySafe (fun _ ⇒ True) (@V.M.HostAPI.call Host_t F msg).
Proof.
  intro Hcall. unfold V.M.HostAPI.call.
  eapply memory_safe_bind; [apply memory_safe_host_frame; exact Hcall|]. intros result _.
  match goal with |- memorySafe _ (if ?test then _ else _) ⇒ destruct test end.
  - apply memory_safe_lift, safe_fail.
  - apply memory_safe_lift, safe_return. exact I.
Qed.

Definition boundedMemorySafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
  ∀ state, Z.of_N (MS.t.gas state) < 2^63 → machineWords invariant state →
    (machineWords invariant (snd (action state)) ∧
      match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end) ∨
    badMemoryInitComparison.

Lemma bounded_memory_from_unbounded {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
  memorySafe P action → boundedMemorySafe P action.
Proof. intros Haction state _ Hwords. apply Haction. exact Hwords. Qed.

Lemma bounded_memory_bind_prefix {A B : Set} (P : A → Prop) (Q : B → Prop)
    (action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
  machineSafe invariant P action → VmOpcodeProgress.gas_nonincreasing action →
  (∀ value, P value → boundedMemorySafe Q (next value)) →
  boundedMemorySafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Haction Hgas Hnext state Hbound Hwords.
  pose proof (Haction state Hwords) as Hprefix.
  change ((machineWords invariant (snd
    (match action state with (inl value, middle) ⇒ next value middle | (inr err, middle) ⇒ (inr err, middle) end)) ∧
    match fst
    (match action state with (inl value, middle) ⇒ next value middle | (inr err, middle) ⇒ (inr err, middle) end)
    with inl value ⇒ Q value | inr _ ⇒ True end) ∨ badMemoryInitComparison).
  destruct (action state) as [[value|error] middle] eqn:Hrun; cbn [fst snd] in Hprefix.
  - apply Hnext; [exact (proj2 Hprefix)| |exact (proj1 Hprefix)].
    pose proof (Hgas state value middle Hrun). lia.
  - left. exact Hprefix.
Qed.

Lemma bounded_memory_bind_suffix {A B : Set} (P : A → Prop) (Q : B → Prop)
    (action : @V.M.t Host_t F A) (next : A → @V.M.t Host_t F B) :
  boundedMemorySafe P action →
  (∀ value, P value → memorySafe Q (next value)) →
  boundedMemorySafe Q (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Haction Hnext state Hbound Hwords.
  destruct (Haction state Hbound Hwords) as [Hprefix|Hfailure]; [|right; exact Hfailure].
  change ((machineWords invariant (snd
    (match action state with (inl value, middle) ⇒ next value middle | (inr err, middle) ⇒ (inr err, middle) end)) ∧
    match fst
    (match action state with (inl value, middle) ⇒ next value middle | (inr err, middle) ⇒ (inr err, middle) end)
    with inl value ⇒ Q value | inr _ ⇒ True end) ∨ badMemoryInitComparison).
  destruct (action state) as [[value|error] middle]; cbn [fst snd] in Hprefix.
  - apply Hnext; [exact (proj2 Hprefix)|exact (proj1 Hprefix)].
  - left. exact Hprefix.
Qed.
End Composition.