Library QedkConcrete.vmmemorywords
Memory instructions with a concrete allocation-length check
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.memorywords QedkConcrete.vmmachinewords
QedkConcrete.vmbytewords QedkConcrete.wordrange
QedkConcrete.vmmiscwords QedkConcrete.vmhostwords QedkConcrete.vmhostframe QedkConcrete.vmchecked QedkConcrete.memorycopybounds QedkConcrete.bytewords.
Require Export QedkConcrete.memorycomposition.
Require Import RocqOfOCaml.Unreachable.StringBounds.
Require Import QedkConcrete.memorymodel.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Instructions.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UN : Basics.Unreachable Support.UintN.t} {UR : Basics.Unreachable U.Repr.t}
{UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.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)}
{Ucost : Basics.Unreachable (Support.UintN.t → Support.UintN.t)}
{UZ : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}
{UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))}.
Lemma mload_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mload Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.mload.
eapply memory_safe_bind; [apply memory_safe_lift, safe_pop|]. intros pos Hpos.
eapply memory_safe_bind; [apply memory_safe_lift, (@extend_memory_preserves_words_bounded Host_t F E invariant UN Ucost UE); exact Hrevision|].
intros cost _.
eapply memory_safe_bind; [apply memory_safe_lift, safe_spend|]. intros [] _.
eapply memory_safe_bind; [apply (@memory_safe_read_word Host_t F invariant UN UR UF US UU UL UI UC UZ)|]. intros value Hvalue.
eapply memory_safe_bind; [apply memory_safe_lift, safe_push; exact Hvalue|]. intros [] _.
apply memory_safe_lift, safe_increase_pc.
Qed.
Lemma mload_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mload Host_t F E).
Proof. intros; eapply mload_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma mstore_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mstore Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.mstore.
eapply memory_safe_bind; [apply memory_safe_lift, safe_pop2|]. intros [pos value] Hvalues.
eapply memory_safe_bind; [apply memory_safe_lift, (@extend_memory_preserves_words_bounded Host_t F E invariant UN Ucost UE); exact Hrevision|].
intros cost _.
eapply memory_safe_bind; [apply memory_safe_lift, safe_spend|]. intros [] _.
eapply memory_safe_bind.
- apply memory_safe_update. intros memory Hmemory.
apply write_word_chunks_or_bad_comparison. exact Hmemory.
- intros [] _. apply memory_safe_lift, safe_increase_pc.
Qed.
Lemma mstore_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mstore Host_t F E).
Proof. intros; eapply mstore_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma mstore8_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mstore8 Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.mstore8.
eapply memory_safe_bind; [apply memory_safe_lift, safe_pop2|]. intros [pos value] Hvalues.
eapply memory_safe_bind; [apply memory_safe_lift, (@extend_memory_preserves_words_bounded Host_t F E invariant UN Ucost UE); exact Hrevision|].
intros cost _.
eapply memory_safe_bind; [apply memory_safe_lift, safe_spend|]. intros [] _.
eapply memory_safe_bind.
- apply memory_safe_update. intros memory Hmemory.
exact (@write_byte_chunks_or_bad_comparison Host_t F UU ULI UZ pos (@U.byte 0 value) memory Hmemory).
- intros [] _. apply memory_safe_lift, safe_increase_pc.
Qed.
Lemma mstore8_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mstore8 Host_t F E).
Proof. intros; eapply mstore8_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma copy_input_preserves_words_or_bad_comparison_bounded data :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.copy_input_data_opcode_impl Host_t F E data).
Proof.
intro Hrevision. unfold V.Executor.copy_input_data_opcode_impl.
eapply checked_safe_bind; [apply checked_safe_pop3|].
intros [[dst src] size] [Hdst [Hsrc Hsize]].
cbn [fst snd] in Hdst,Hsrc,Hsize. unfold wordRange in Hdst,Hsrc,Hsize.
eapply checked_safe_bind; [exact (@checked_safe_extend_input_bounded Host_t F invariant E UF UG Ucost UE dst size Hrevision (proj1 Hdst) (proj1 Hsize))|].
intros cost Hbound. cbn beta in Hbound.
eapply checked_safe_bind; [apply (@checked_safe_spend Host_t F invariant UG)|]. intros [] _.
eapply checked_safe_bind.
- cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block. rewrite copy_input_length by exact Hbound. lia.
- intros [] _. apply (@checked_safe_increase_pc Host_t F invariant UF).
Qed.
Lemma copy_input_preserves_words_or_bad_comparison data :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.copy_input_data_opcode_impl Host_t F E data).
Proof. intros; eapply copy_input_preserves_words_or_bad_comparison_bounded with (data := data); eauto using eleven_bounded_memory. Qed.
Lemma calldatacopy_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.calldatacopy Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.calldatacopy.
apply copy_input_preserves_words_or_bad_comparison_bounded. exact Hrevision.
Qed.
Lemma calldatacopy_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.calldatacopy Host_t F E).
Proof. intros; eapply calldatacopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma codecopy_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.codecopy Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.codecopy.
apply copy_input_preserves_words_or_bad_comparison_bounded. exact Hrevision.
Qed.
Lemma codecopy_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.codecopy Host_t F E).
Proof. intros; eapply codecopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma mcopy_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.mcopy Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.mcopy.
eapply checked_safe_bind; [apply checked_safe_pop3|].
intros [[dst src] size] [Hdst [Hsrc Hsize]].
cbn [fst snd] in Hdst,Hsrc,Hsize. unfold wordRange in Hdst,Hsrc,Hsize.
eapply checked_safe_bind.
- apply (@checked_safe_extend_input_bounded Host_t F invariant E UF UG Ucost UE); try exact Hrevision.
+ change (0 ≤ U.to_z (if U.op_lt src dst then dst else src)).
destruct (U.op_lt src dst); [exact (proj1 Hdst)|exact (proj1 Hsrc)].
+ exact (proj1 Hsize).
- intros cost Hbound. cbn beta in Hbound.
eapply checked_safe_bind; [apply (@checked_safe_spend Host_t F invariant UG)|]. intros [] _.
eapply checked_safe_bind with (P := fun bytes ⇒ OCamlString.length bytes < 2^32).
+ intros state Hstate.
destruct (@vmchildsizes.read_small_input_bounded Host_t F UI src size
(MS.t.memory state) Hbound (proj2 (proj2 (proj2 (proj1 Hstate))))) as [Hlength|Hbad].
× left. split; [exact Hstate|exact Hlength].
× right. exact Hbad.
+ intros bytes Hlength.
eapply checked_safe_bind.
× cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block. exact Hlength.
× intros [] _. apply (@checked_safe_increase_pc Host_t F invariant UF).
Qed.
Lemma mcopy_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.mcopy Host_t F E).
Proof. intros; eapply mcopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma returndatacopy_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.returndatacopy Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.returndatacopy.
eapply checked_safe_bind; [apply checked_safe_pop3|].
intros [[dst src] size] [Hdst [Hsrc Hsize]].
cbn [fst snd] in Hdst,Hsrc,Hsize. unfold wordRange in Hdst,Hsrc,Hsize.
eapply checked_safe_bind with (P := fun _ ⇒ True); [apply checked_safe_read; intros; exact I|].
intros data _.
eapply checked_safe_bind with (P := fun p ⇒ fst p = U.to_z src ∧
snd p = U.to_z size ∧ U.to_z src + U.to_z size ≤ OCamlString.length data).
- destruct (U.to_int_opt src) as [start|] eqn:Hstart;
[|apply checked_safe_fail].
destruct (U.to_int_opt size) as [count|] eqn:Hcount;
[|apply checked_safe_fail].
apply word_to_int_opt in Hstart. apply word_to_int_opt in Hcount. subst start count.
destruct (orb _ _) eqn:Hguard; [apply checked_safe_fail|].
apply Bool.orb_false_iff in Hguard. destruct Hguard as [Hstart Hcount].
rewrite Z.gtb_ltb in Hstart,Hcount.
apply Z.ltb_ge in Hstart. apply Z.ltb_ge in Hcount.
change (U.to_z src ≤ OCamlString.length data) in Hstart.
apply checked_safe_return. cbn [fst snd]. split; [reflexivity|]. split; [reflexivity|].
change (U.to_z size ≤ OCamlInt.repr (OCamlString.length data - U.to_z src)) in Hcount.
pose proof (nativeindices.native_repr_le_nonnegative
(OCamlString.length data - U.to_z src) ltac:(lia)). lia.
- intros [start count] [Hstart [Hcount Hinside]]. cbn [fst snd] in Hstart,Hcount. subst start count.
eapply checked_safe_bind; [exact (@checked_safe_extend_input_bounded Host_t F invariant E UF UG Ucost UE dst size Hrevision (proj1 Hdst) (proj1 Hsize))|].
intros cost Hbound. cbn beta in Hbound.
eapply checked_safe_bind; [apply (@checked_safe_spend Host_t F invariant UG)|]. intros [] _.
eapply checked_safe_bind.
+ cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block.
change (OCamlString.length (@OCamlString.sub
(Opaque.legacy_witness (@V.Executor._rocq_fallback_returndatacopy_0 Host_t F E))
data (U.to_z src) (U.to_z size)) < 2^32).
rewrite StringBounds.sub_length_in_bounds by lia. lia.
+ intros [] _. apply (@checked_safe_increase_pc Host_t F invariant UF).
Qed.
Lemma returndatacopy_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.returndatacopy Host_t F E).
Proof. intros; eapply returndatacopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.memorywords QedkConcrete.vmmachinewords
QedkConcrete.vmbytewords QedkConcrete.wordrange
QedkConcrete.vmmiscwords QedkConcrete.vmhostwords QedkConcrete.vmhostframe QedkConcrete.vmchecked QedkConcrete.memorycopybounds QedkConcrete.bytewords.
Require Export QedkConcrete.memorycomposition.
Require Import RocqOfOCaml.Unreachable.StringBounds.
Require Import QedkConcrete.memorymodel.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section Instructions.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UN : Basics.Unreachable Support.UintN.t} {UR : Basics.Unreachable U.Repr.t}
{UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.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)}
{Ucost : Basics.Unreachable (Support.UintN.t → Support.UintN.t)}
{UZ : Basics.Unreachable Byte_string.B32.(Byte_string.Fixed.Fixed_result.t)}
{UE : Basics.Unreachable (U.t → U.t → @Mem.t Host_t F → option (@Mem.t Host_t F))}.
Lemma mload_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mload Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.mload.
eapply memory_safe_bind; [apply memory_safe_lift, safe_pop|]. intros pos Hpos.
eapply memory_safe_bind; [apply memory_safe_lift, (@extend_memory_preserves_words_bounded Host_t F E invariant UN Ucost UE); exact Hrevision|].
intros cost _.
eapply memory_safe_bind; [apply memory_safe_lift, safe_spend|]. intros [] _.
eapply memory_safe_bind; [apply (@memory_safe_read_word Host_t F invariant UN UR UF US UU UL UI UC UZ)|]. intros value Hvalue.
eapply memory_safe_bind; [apply memory_safe_lift, safe_push; exact Hvalue|]. intros [] _.
apply memory_safe_lift, safe_increase_pc.
Qed.
Lemma mload_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mload Host_t F E).
Proof. intros; eapply mload_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma mstore_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mstore Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.mstore.
eapply memory_safe_bind; [apply memory_safe_lift, safe_pop2|]. intros [pos value] Hvalues.
eapply memory_safe_bind; [apply memory_safe_lift, (@extend_memory_preserves_words_bounded Host_t F E invariant UN Ucost UE); exact Hrevision|].
intros cost _.
eapply memory_safe_bind; [apply memory_safe_lift, safe_spend|]. intros [] _.
eapply memory_safe_bind.
- apply memory_safe_update. intros memory Hmemory.
apply write_word_chunks_or_bad_comparison. exact Hmemory.
- intros [] _. apply memory_safe_lift, safe_increase_pc.
Qed.
Lemma mstore_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mstore Host_t F E).
Proof. intros; eapply mstore_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma mstore8_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mstore8 Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.mstore8.
eapply memory_safe_bind; [apply memory_safe_lift, safe_pop2|]. intros [pos value] Hvalues.
eapply memory_safe_bind; [apply memory_safe_lift, (@extend_memory_preserves_words_bounded Host_t F E invariant UN Ucost UE); exact Hrevision|].
intros cost _.
eapply memory_safe_bind; [apply memory_safe_lift, safe_spend|]. intros [] _.
eapply memory_safe_bind.
- apply memory_safe_update. intros memory Hmemory.
exact (@write_byte_chunks_or_bad_comparison Host_t F UU ULI UZ pos (@U.byte 0 value) memory Hmemory).
- intros [] _. apply memory_safe_lift, safe_increase_pc.
Qed.
Lemma mstore8_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
memorySafe invariant (fun _ ⇒ True)
(@V.Executor.mstore8 Host_t F E).
Proof. intros; eapply mstore8_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma copy_input_preserves_words_or_bad_comparison_bounded data :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.copy_input_data_opcode_impl Host_t F E data).
Proof.
intro Hrevision. unfold V.Executor.copy_input_data_opcode_impl.
eapply checked_safe_bind; [apply checked_safe_pop3|].
intros [[dst src] size] [Hdst [Hsrc Hsize]].
cbn [fst snd] in Hdst,Hsrc,Hsize. unfold wordRange in Hdst,Hsrc,Hsize.
eapply checked_safe_bind; [exact (@checked_safe_extend_input_bounded Host_t F invariant E UF UG Ucost UE dst size Hrevision (proj1 Hdst) (proj1 Hsize))|].
intros cost Hbound. cbn beta in Hbound.
eapply checked_safe_bind; [apply (@checked_safe_spend Host_t F invariant UG)|]. intros [] _.
eapply checked_safe_bind.
- cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block. rewrite copy_input_length by exact Hbound. lia.
- intros [] _. apply (@checked_safe_increase_pc Host_t F invariant UF).
Qed.
Lemma copy_input_preserves_words_or_bad_comparison data :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.copy_input_data_opcode_impl Host_t F E data).
Proof. intros; eapply copy_input_preserves_words_or_bad_comparison_bounded with (data := data); eauto using eleven_bounded_memory. Qed.
Lemma calldatacopy_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.calldatacopy Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.calldatacopy.
apply copy_input_preserves_words_or_bad_comparison_bounded. exact Hrevision.
Qed.
Lemma calldatacopy_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.calldatacopy Host_t F E).
Proof. intros; eapply calldatacopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma codecopy_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.codecopy Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.codecopy.
apply copy_input_preserves_words_or_bad_comparison_bounded. exact Hrevision.
Qed.
Lemma codecopy_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.codecopy Host_t F E).
Proof. intros; eapply codecopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma mcopy_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.mcopy Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.mcopy.
eapply checked_safe_bind; [apply checked_safe_pop3|].
intros [[dst src] size] [Hdst [Hsrc Hsize]].
cbn [fst snd] in Hdst,Hsrc,Hsize. unfold wordRange in Hdst,Hsrc,Hsize.
eapply checked_safe_bind.
- apply (@checked_safe_extend_input_bounded Host_t F invariant E UF UG Ucost UE); try exact Hrevision.
+ change (0 ≤ U.to_z (if U.op_lt src dst then dst else src)).
destruct (U.op_lt src dst); [exact (proj1 Hdst)|exact (proj1 Hsrc)].
+ exact (proj1 Hsize).
- intros cost Hbound. cbn beta in Hbound.
eapply checked_safe_bind; [apply (@checked_safe_spend Host_t F invariant UG)|]. intros [] _.
eapply checked_safe_bind with (P := fun bytes ⇒ OCamlString.length bytes < 2^32).
+ intros state Hstate.
destruct (@vmchildsizes.read_small_input_bounded Host_t F UI src size
(MS.t.memory state) Hbound (proj2 (proj2 (proj2 (proj1 Hstate))))) as [Hlength|Hbad].
× left. split; [exact Hstate|exact Hlength].
× right. exact Hbad.
+ intros bytes Hlength.
eapply checked_safe_bind.
× cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block. exact Hlength.
× intros [] _. apply (@checked_safe_increase_pc Host_t F invariant UF).
Qed.
Lemma mcopy_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.mcopy Host_t F E).
Proof. intros; eapply mcopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
Lemma returndatacopy_preserves_words_or_bad_comparison_bounded :
BoundedMemory F →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.returndatacopy Host_t F E).
Proof.
intro Hrevision. unfold V.Executor.returndatacopy.
eapply checked_safe_bind; [apply checked_safe_pop3|].
intros [[dst src] size] [Hdst [Hsrc Hsize]].
cbn [fst snd] in Hdst,Hsrc,Hsize. unfold wordRange in Hdst,Hsrc,Hsize.
eapply checked_safe_bind with (P := fun _ ⇒ True); [apply checked_safe_read; intros; exact I|].
intros data _.
eapply checked_safe_bind with (P := fun p ⇒ fst p = U.to_z src ∧
snd p = U.to_z size ∧ U.to_z src + U.to_z size ≤ OCamlString.length data).
- destruct (U.to_int_opt src) as [start|] eqn:Hstart;
[|apply checked_safe_fail].
destruct (U.to_int_opt size) as [count|] eqn:Hcount;
[|apply checked_safe_fail].
apply word_to_int_opt in Hstart. apply word_to_int_opt in Hcount. subst start count.
destruct (orb _ _) eqn:Hguard; [apply checked_safe_fail|].
apply Bool.orb_false_iff in Hguard. destruct Hguard as [Hstart Hcount].
rewrite Z.gtb_ltb in Hstart,Hcount.
apply Z.ltb_ge in Hstart. apply Z.ltb_ge in Hcount.
change (U.to_z src ≤ OCamlString.length data) in Hstart.
apply checked_safe_return. cbn [fst snd]. split; [reflexivity|]. split; [reflexivity|].
change (U.to_z size ≤ OCamlInt.repr (OCamlString.length data - U.to_z src)) in Hcount.
pose proof (nativeindices.native_repr_le_nonnegative
(OCamlString.length data - U.to_z src) ltac:(lia)). lia.
- intros [start count] [Hstart [Hcount Hinside]]. cbn [fst snd] in Hstart,Hcount. subst start count.
eapply checked_safe_bind; [exact (@checked_safe_extend_input_bounded Host_t F invariant E UF UG Ucost UE dst size Hrevision (proj1 Hdst) (proj1 Hsize))|].
intros cost Hbound. cbn beta in Hbound.
eapply checked_safe_bind; [apply (@checked_safe_spend Host_t F invariant UG)|]. intros [] _.
eapply checked_safe_bind.
+ cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block.
change (OCamlString.length (@OCamlString.sub
(Opaque.legacy_witness (@V.Executor._rocq_fallback_returndatacopy_0 Host_t F E))
data (U.to_z src) (U.to_z size)) < 2^32).
rewrite StringBounds.sub_length_in_bounds by lia. lia.
+ intros [] _. apply (@checked_safe_increase_pc Host_t F invariant UF).
Qed.
Lemma returndatacopy_preserves_words_or_bad_comparison :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
checkedSafe invariant (fun _ ⇒ True)
(@V.Executor.returndatacopy Host_t F E).
Proof. intros; eapply returndatacopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
The caller must discharge the callback size bound for the concrete Host.
It must not become a VM contract in the end-to-end theorem.
Lemma extcodecopy_preserves_words_or_bad_comparison_bounded
{UA : Basics.Unreachable Chain.Ethereum.Address.t} :
BoundedMemory F →
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ addr offset count host, 0 ≤ offset → 0 ≤ count < 2^32 → invariant host →
OCamlString.length (fst ((@V.Host Host_t F).(Evmc.HOST.copy_code) addr offset count host)) ≤ count) →
checkedSafe invariant (fun _ ⇒ True) (@V.Executor.extcodecopy Host_t F E).
Proof.
intros Hrevision Hhost Hcopy. unfold V.Executor.extcodecopy.
eapply checked_safe_bind; [apply checked_safe_fmap; apply checked_safe_pop|].
intros addr _.
eapply checked_safe_bind; [apply checked_safe_pop3|].
intros [[dst src] size] [Hdst [Hsrc Hsize]].
cbn [fst snd] in Hdst,Hsrc,Hsize. unfold wordRange in Hdst,Hsrc,Hsize.
eapply checked_safe_bind with (P := fun _ ⇒ True).
- apply checked_safe_fmap_ignore. unfold V.M.HostAPI.access_account.
apply checked_safe_host. intros host Hinitial. left. split.
+ apply (frame_access_account _ _ Hhost). exact Hinitial.
+ exact I.
- intros accessGas _.
eapply checked_safe_bind; [exact (@checked_safe_extend_input_bounded Host_t F invariant E UF UG Ucost UE dst size Hrevision (proj1 Hdst) (proj1 Hsize))|].
intros cost Hbound. cbn beta in Hbound.
eapply checked_safe_bind; [apply (@checked_safe_spend Host_t F invariant UG)|]. intros [] _.
eapply checked_safe_bind with (P := fun p ⇒ 0 ≤ fst p < 2^32 ∧
OCamlString.length (snd p) ≤ fst p).
+ rewrite (small_word_to_int_opt size Hbound).
destruct (U.to_int_opt src) as [offset|] eqn:Hoffset;
destruct (U.to_z size) as [|positive|negative] eqn:Hcount;
try (exfalso; lia).
all: try solve [apply checked_safe_return; cbn; lia].
× eapply checked_safe_bind with (P := fun bytes ⇒ OCamlString.length bytes ≤ Z.pos positive).
-- unfold V.M.HostAPI.copy_code. apply checked_safe_host.
intros host Hinitial. left. split.
++ apply (frame_copy_code _ _ Hhost). exact Hinitial.
++ apply Hcopy; try assumption; try lia.
apply word_to_int_opt in Hoffset. rewrite Hoffset. exact (proj1 Hsrc).
-- intros bytes Hlength. apply checked_safe_return. cbn. split; [lia|exact Hlength].
× apply checked_safe_return. cbn [fst snd]. split; [lia|].
rewrite copy_padding_length by lia. lia.
+ intros [count bytes] [Hcount Hlength]. cbn [fst snd] in Hcount,Hlength.
eapply checked_safe_bind.
× cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block. lia.
× intros [] _. eapply checked_safe_bind.
-- destruct (Z.ltb (Byte_string.Bytes.length bytes) count) eqn:Hpad.
++ apply Z.ltb_lt in Hpad.
cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block. rewrite copy_padding_length.
** unfold OCamlInt.sub. rewrite nativeindices.native_repr_small by
(unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia).
unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia.
** unfold OCamlInt.sub. rewrite nativeindices.native_repr_small by
(unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia).
unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia.
++ apply checked_safe_return. exact I.
-- intros [] _. apply (@checked_safe_increase_pc Host_t F invariant UF).
Qed.
Lemma extcodecopy_preserves_words_or_bad_comparison
{UA : Basics.Unreachable Chain.Ethereum.Address.t} :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ addr offset count host, 0 ≤ offset → 0 ≤ count < 2^32 → invariant host →
OCamlString.length (fst ((@V.Host Host_t F).(Evmc.HOST.copy_code) addr offset count host)) ≤ count) →
checkedSafe invariant (fun _ ⇒ True) (@V.Executor.extcodecopy Host_t F E).
Proof. intros; eapply extcodecopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
End Instructions.
Print Assumptions mload_preserves_words_or_bad_comparison.
Print Assumptions mstore_preserves_words_or_bad_comparison.
Print Assumptions mcopy_preserves_words_or_bad_comparison.
Print Assumptions extcodecopy_preserves_words_or_bad_comparison.
{UA : Basics.Unreachable Chain.Ethereum.Address.t} :
BoundedMemory F →
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ addr offset count host, 0 ≤ offset → 0 ≤ count < 2^32 → invariant host →
OCamlString.length (fst ((@V.Host Host_t F).(Evmc.HOST.copy_code) addr offset count host)) ≤ count) →
checkedSafe invariant (fun _ ⇒ True) (@V.Executor.extcodecopy Host_t F E).
Proof.
intros Hrevision Hhost Hcopy. unfold V.Executor.extcodecopy.
eapply checked_safe_bind; [apply checked_safe_fmap; apply checked_safe_pop|].
intros addr _.
eapply checked_safe_bind; [apply checked_safe_pop3|].
intros [[dst src] size] [Hdst [Hsrc Hsize]].
cbn [fst snd] in Hdst,Hsrc,Hsize. unfold wordRange in Hdst,Hsrc,Hsize.
eapply checked_safe_bind with (P := fun _ ⇒ True).
- apply checked_safe_fmap_ignore. unfold V.M.HostAPI.access_account.
apply checked_safe_host. intros host Hinitial. left. split.
+ apply (frame_access_account _ _ Hhost). exact Hinitial.
+ exact I.
- intros accessGas _.
eapply checked_safe_bind; [exact (@checked_safe_extend_input_bounded Host_t F invariant E UF UG Ucost UE dst size Hrevision (proj1 Hdst) (proj1 Hsize))|].
intros cost Hbound. cbn beta in Hbound.
eapply checked_safe_bind; [apply (@checked_safe_spend Host_t F invariant UG)|]. intros [] _.
eapply checked_safe_bind with (P := fun p ⇒ 0 ≤ fst p < 2^32 ∧
OCamlString.length (snd p) ≤ fst p).
+ rewrite (small_word_to_int_opt size Hbound).
destruct (U.to_int_opt src) as [offset|] eqn:Hoffset;
destruct (U.to_z size) as [|positive|negative] eqn:Hcount;
try (exfalso; lia).
all: try solve [apply checked_safe_return; cbn; lia].
× eapply checked_safe_bind with (P := fun bytes ⇒ OCamlString.length bytes ≤ Z.pos positive).
-- unfold V.M.HostAPI.copy_code. apply checked_safe_host.
intros host Hinitial. left. split.
++ apply (frame_copy_code _ _ Hhost). exact Hinitial.
++ apply Hcopy; try assumption; try lia.
apply word_to_int_opt in Hoffset. rewrite Hoffset. exact (proj1 Hsrc).
-- intros bytes Hlength. apply checked_safe_return. cbn. split; [lia|exact Hlength].
× apply checked_safe_return. cbn [fst snd]. split; [lia|].
rewrite copy_padding_length by lia. lia.
+ intros [count bytes] [Hcount Hlength]. cbn [fst snd] in Hcount,Hlength.
eapply checked_safe_bind.
× cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block. lia.
× intros [] _. eapply checked_safe_bind.
-- destruct (Z.ltb (Byte_string.Bytes.length bytes) count) eqn:Hpad.
++ apply Z.ltb_lt in Hpad.
cbn [V.Memory V.Memory.module V.Memory.Memory_signature.write_block_at].
apply checked_safe_write_block. rewrite copy_padding_length.
** unfold OCamlInt.sub. rewrite nativeindices.native_repr_small by
(unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia).
unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia.
** unfold OCamlInt.sub. rewrite nativeindices.native_repr_small by
(unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia).
unfold Byte_string.Bytes.length, MonadVM.String.length, OCamlString.length in *; lia.
++ apply checked_safe_return. exact I.
-- intros [] _. apply (@checked_safe_increase_pc Host_t F invariant UF).
Qed.
Lemma extcodecopy_preserves_words_or_bad_comparison
{UA : Basics.Unreachable Chain.Ethereum.Address.t} :
(@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ addr offset count host, 0 ≤ offset → 0 ≤ count < 2^32 → invariant host →
OCamlString.length (fst ((@V.Host Host_t F).(Evmc.HOST.copy_code) addr offset count host)) ≤ count) →
checkedSafe invariant (fun _ ⇒ True) (@V.Executor.extcodecopy Host_t F E).
Proof. intros; eapply extcodecopy_preserves_words_or_bad_comparison_bounded; eauto using eleven_bounded_memory. Qed.
End Instructions.
Print Assumptions mload_preserves_words_or_bad_comparison.
Print Assumptions mstore_preserves_words_or_bad_comparison.
Print Assumptions mcopy_preserves_words_or_bad_comparison.
Print Assumptions extcodecopy_preserves_words_or_bad_comparison.