Library SetcodeConcrete.vmsupply
Supply and representation invariants through the SETCODE VM loop
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.recursiveassembly QedkConcrete.wordrange
QedkConcrete.vmmemorywords QedkConcrete.vmsupplywords QedkConcrete.vmchecked
QedkConcrete.vmdispatchwords QedkConcrete.vmmachinewords
QedkConcrete.vmcallchecked QedkConcrete.vmcreatechecked
QedkConcrete.vmlocalsizes QedkConcrete.vmsizes QedkConcrete.vmenvironmentwords
QedkConcrete.hostchecked QedkConcrete.hostframe QedkConcrete.hostsupply
QedkConcrete.hostaccountformat QedkConcrete.hostcontextwords
QedkConcrete.vmopcodeframe QedkConcrete.vmhostframe QedkConcrete.vminvariant
QedkConcrete.vmrun QedkConcrete.memorymodel QedkConcrete.hashwords.
Require Import SetcodeConcrete.adoptionsupply SetcodeConcrete.vmchecked
SetcodeConcrete.vmframes SetcodeConcrete.vmprogress.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Section Concrete.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs HF)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
(@Evmc.Instantiate.T S.t (@recursiveArgs HF)))).
Context (params : Vm.Make_Params_signature) (limit : Z).
Local Abbreviation F := (V.Build_FArgs params (HostF vm)).
Local Abbreviation words := (hostSupplyWords limit).
Local Abbreviation invariant := (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).
Lemma adoption_preserves_supply_words current source static gas :
preservesCallback words ((HostF vm).(Evmc.HOST.setcodefrom) current source static gas).
Proof.
intros s [[Hbalance Hlimit] Hformat].
pose proof (@setcodefrom_preserves_supply
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
current source static gas s Hbalance) as [Hbalance' Htotal].
split; [split; [exact Hbalance'|eapply Z.le_trans; [exact Htotal|exact Hlimit]]|].
apply setcodefrom_preserves_format. exact Hformat.
Qed.
Lemma adoption_preserves_supply_context current source static gas :
preservesCallback invariant ((HostF vm).(Evmc.HOST.setcodefrom) current source static gas).
Proof.
apply host_supply_context_frame.
- apply setcodefrom_preserves_supply.
- apply setcodefrom_preserves_format.
- apply setcodefrom_preserves_context.
Qed.
Context (cold warm access write reserve : N).
Context (Hrevision : params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hdebug : params.(Vm.Make_Params_signature.debug_tstore) = false).
Context (Hhash : keccak256OutputSize).
Context {E : @V.Executor.FArgs S.t F}.
Local Lemma bounded_memory : BoundedMemory F.
Proof. eapply setcode_bounded_memory. exact Hrevision. Qed.
Local Lemma extension_sizes opcode : extension_opcode opcode →
sizesSafe (fun _ ⇒ True) (@dispatch S.t F E opcode).
Proof.
intro Hop. destruct opcode; cbn in Hop; try contradiction.
- eapply setcode_opcode_sizes. exact Hrevision.
- unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined.
apply sizes_safe_fail.
Qed.
Lemma nonrecursive_supply_words opcode state :
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
opcodeWordParameters opcode → recursiveCallOpcode opcode = false →
memoryFits (MS.t.memory state) → OCamlString.length (MS.t.output_buffer state) < 2^256 →
machineWords words state →
machineWords words (snd (@dispatch S.t F E opcode state)) ∨ badMemoryInitComparison.
Proof.
intros Henv Hparams Hnonrecursive Hfits Hbuffer Hinitial.
destruct (machineOnlyOpcode opcode) eqn:Hlocal.
- eapply machine_only_preserves_words_core; try eassumption.
+ exact bounded_memory.
+ eapply setcode_clz_words. exact Hrevision.
+ intros extra _ Hextra. destruct extra; cbn in Hextra; try contradiction.
× left. apply (proj1 (@setcode_opcode_words S.t F E cold warm access write reserve
Hrevision words adoption_preserves_supply_words state Hinitial)).
× left. exact Hinitial.
- destruct (primitiveHostOpcode opcode) eqn:Hprimitive.
+ pose proof (primitive_host_preserves_supply_words_bounded vm params limit opcode
bounded_memory Hdebug Hhash Hprimitive state
(conj Hinitial (conj Hfits Hbuffer))) as Hsafe. tauto.
+ destruct opcode; cbn [machineOnlyOpcode primitiveHostOpcode recursiveCallOpcode]
in *; congruence.
Qed.
Theorem dispatch_supply_context opcode state :
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
opcodeWordParameters opcode →
(∀ msg, vmMessageBounds msg → ∀ host, invariant host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
Z.of_N (MS.t.gas state) < 2^63 → checkedMachine invariant state →
checkedMachine invariant (snd (@dispatch S.t F E opcode state)) ∨ badMemoryInitComparison.
Proof.
intros Henv Hparams Hchild Hgas Hinitial.
pose proof bounded_memory as Hmemory.
destruct (recursiveCallOpcode opcode) eqn:Hrecursive.
- assert (Hvalue : wordRange (Env.t.value
((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)))).
{ exact (List.Forall_inv (List.Forall_inv_tail (proj1 Henv))). }
assert (Hlift : ∀ action : @V.M.t S.t F bool,
boundedCheckedSafe invariant (fun _ ⇒ True) action →
checkedMachine invariant (snd (action state)) ∨ badMemoryInitComparison).
{ intros action Hsafe. specialize (Hsafe state Hgas Hinitial). tauto. }
destruct opcode; cbn [recursiveCallOpcode] in Hrecursive; try discriminate.
all: cbn [dispatch V.Executor.execute_opcode]; apply Hlift.
all: lazymatch goal with
| |- context [@V.Executor.call] ⇒ apply call_checked_from_child_bounded
| |- context [@V.Executor.callcode] ⇒ apply callcode_checked_from_child_bounded
| |- context [@V.Executor.staticcall] ⇒ apply staticcall_checked_from_child_bounded
| |- context [@V.Executor.delegatecall] ⇒ apply delegatecall_checked_from_child_bounded
| |- context [@V.Executor.create] ⇒ apply create_checked_from_child_bounded
| |- context [@V.Executor.create2] ⇒ apply create2_checked_from_child_bounded
end.
all: first [assumption|apply supply_context_primitives|
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat].
- assert (Hwords : machineWords words state).
{ unfold checkedMachine, machineWords, hostSupplyContext in Hinitial |- ×. tauto. }
pose proof (proj2 Hinitial) as Hsizes.
destruct (nonrecursive_supply_words opcode state Henv Hparams Hrecursive
(proj1 Hsizes) (proj2 Hsizes) Hwords) as [HafterWords|Hbad];
[|right; exact Hbad].
assert (HafterSizes : machineByteSizes (snd (@dispatch S.t F E opcode state)) ∨ badMemoryInitComparison).
{ eapply nonrecursive_preserves_sizes_bounded; try eassumption.
- eapply setcode_clz_sizes. exact Hrevision.
- exact extension_sizes.
- exact (proj1 Hwords).
- exact (proj2 (proj2 (proj2 Hwords))). }
assert (Hcontext : preservesHost (hostContextWords HF.(Host.Instantiate.ChainParams))
(@dispatch S.t F E opcode)).
{ destruct (machineOnlyOpcode opcode) eqn:Hlocal.
- eapply machine_only_preserves_host_core; try eassumption.
+ eapply setcode_clz_preserves_host. exact Hrevision.
+ intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
× eapply setcode_opcode_preserves_host; [exact Hrevision|].
intros current source static gas host Hctx.
eapply host_context_words_preserved;
[apply setcodefrom_preserves_context|exact Hctx].
× unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined.
apply preserves_fail.
- apply primitive_host_preserves_at_any_revision; try assumption.
+ apply primitive_frame_at, context_words_primitives.
+ destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *;
congruence. }
specialize (Hcontext state (proj2 (proj1 (proj2 (proj2 (proj1 Hinitial)))))).
unfold checkedMachine, machineWords, hostSupplyContext in ×. tauto.
Qed.
Theorem run_supply_context state :
@setcodeGasProgress S.t F →
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
(∀ msg, vmMessageBounds msg → ∀ host, invariant host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
Z.of_N (MS.t.gas state) < 2^63 → checkedMachine invariant state →
checkedMachine invariant (snd (@generated_run S.t F E state)) ∨ badMemoryInitComparison.
Proof.
intros Hprogress Henv Hchild Hgas Hinitial.
apply (setcode_run_preserves cold warm access write reserve
(fun st ⇒ checkedMachine invariant st ∨ badMemoryInitComparison) state Hrevision Hprogress).
- intros before Hbound [Hbefore|Hbad]; [|right; exact Hbad].
apply dispatch_supply_context; try assumption. apply selected_opcode_word_parameters.
- exact Hgas.
- left; exact Hinitial.
Qed.
End Concrete.
Print Assumptions run_supply_context.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.recursiveassembly QedkConcrete.wordrange
QedkConcrete.vmmemorywords QedkConcrete.vmsupplywords QedkConcrete.vmchecked
QedkConcrete.vmdispatchwords QedkConcrete.vmmachinewords
QedkConcrete.vmcallchecked QedkConcrete.vmcreatechecked
QedkConcrete.vmlocalsizes QedkConcrete.vmsizes QedkConcrete.vmenvironmentwords
QedkConcrete.hostchecked QedkConcrete.hostframe QedkConcrete.hostsupply
QedkConcrete.hostaccountformat QedkConcrete.hostcontextwords
QedkConcrete.vmopcodeframe QedkConcrete.vmhostframe QedkConcrete.vminvariant
QedkConcrete.vmrun QedkConcrete.memorymodel QedkConcrete.hashwords.
Require Import SetcodeConcrete.adoptionsupply SetcodeConcrete.vmchecked
SetcodeConcrete.vmframes SetcodeConcrete.vmprogress.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Local Open Scope Z_scope.
Section Concrete.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs HF)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
(@Evmc.Instantiate.T S.t (@recursiveArgs HF)))).
Context (params : Vm.Make_Params_signature) (limit : Z).
Local Abbreviation F := (V.Build_FArgs params (HostF vm)).
Local Abbreviation words := (hostSupplyWords limit).
Local Abbreviation invariant := (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).
Lemma adoption_preserves_supply_words current source static gas :
preservesCallback words ((HostF vm).(Evmc.HOST.setcodefrom) current source static gas).
Proof.
intros s [[Hbalance Hlimit] Hformat].
pose proof (@setcodefrom_preserves_supply
(H.Build_FArgs HF.(Host.Instantiate.ChainParams) vm)
current source static gas s Hbalance) as [Hbalance' Htotal].
split; [split; [exact Hbalance'|eapply Z.le_trans; [exact Htotal|exact Hlimit]]|].
apply setcodefrom_preserves_format. exact Hformat.
Qed.
Lemma adoption_preserves_supply_context current source static gas :
preservesCallback invariant ((HostF vm).(Evmc.HOST.setcodefrom) current source static gas).
Proof.
apply host_supply_context_frame.
- apply setcodefrom_preserves_supply.
- apply setcodefrom_preserves_format.
- apply setcodefrom_preserves_context.
Qed.
Context (cold warm access write reserve : N).
Context (Hrevision : params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hdebug : params.(Vm.Make_Params_signature.debug_tstore) = false).
Context (Hhash : keccak256OutputSize).
Context {E : @V.Executor.FArgs S.t F}.
Local Lemma bounded_memory : BoundedMemory F.
Proof. eapply setcode_bounded_memory. exact Hrevision. Qed.
Local Lemma extension_sizes opcode : extension_opcode opcode →
sizesSafe (fun _ ⇒ True) (@dispatch S.t F E opcode).
Proof.
intro Hop. destruct opcode; cbn in Hop; try contradiction.
- eapply setcode_opcode_sizes. exact Hrevision.
- unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined.
apply sizes_safe_fail.
Qed.
Lemma nonrecursive_supply_words opcode state :
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
opcodeWordParameters opcode → recursiveCallOpcode opcode = false →
memoryFits (MS.t.memory state) → OCamlString.length (MS.t.output_buffer state) < 2^256 →
machineWords words state →
machineWords words (snd (@dispatch S.t F E opcode state)) ∨ badMemoryInitComparison.
Proof.
intros Henv Hparams Hnonrecursive Hfits Hbuffer Hinitial.
destruct (machineOnlyOpcode opcode) eqn:Hlocal.
- eapply machine_only_preserves_words_core; try eassumption.
+ exact bounded_memory.
+ eapply setcode_clz_words. exact Hrevision.
+ intros extra _ Hextra. destruct extra; cbn in Hextra; try contradiction.
× left. apply (proj1 (@setcode_opcode_words S.t F E cold warm access write reserve
Hrevision words adoption_preserves_supply_words state Hinitial)).
× left. exact Hinitial.
- destruct (primitiveHostOpcode opcode) eqn:Hprimitive.
+ pose proof (primitive_host_preserves_supply_words_bounded vm params limit opcode
bounded_memory Hdebug Hhash Hprimitive state
(conj Hinitial (conj Hfits Hbuffer))) as Hsafe. tauto.
+ destruct opcode; cbn [machineOnlyOpcode primitiveHostOpcode recursiveCallOpcode]
in *; congruence.
Qed.
Theorem dispatch_supply_context opcode state :
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
opcodeWordParameters opcode →
(∀ msg, vmMessageBounds msg → ∀ host, invariant host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
Z.of_N (MS.t.gas state) < 2^63 → checkedMachine invariant state →
checkedMachine invariant (snd (@dispatch S.t F E opcode state)) ∨ badMemoryInitComparison.
Proof.
intros Henv Hparams Hchild Hgas Hinitial.
pose proof bounded_memory as Hmemory.
destruct (recursiveCallOpcode opcode) eqn:Hrecursive.
- assert (Hvalue : wordRange (Env.t.value
((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)))).
{ exact (List.Forall_inv (List.Forall_inv_tail (proj1 Henv))). }
assert (Hlift : ∀ action : @V.M.t S.t F bool,
boundedCheckedSafe invariant (fun _ ⇒ True) action →
checkedMachine invariant (snd (action state)) ∨ badMemoryInitComparison).
{ intros action Hsafe. specialize (Hsafe state Hgas Hinitial). tauto. }
destruct opcode; cbn [recursiveCallOpcode] in Hrecursive; try discriminate.
all: cbn [dispatch V.Executor.execute_opcode]; apply Hlift.
all: lazymatch goal with
| |- context [@V.Executor.call] ⇒ apply call_checked_from_child_bounded
| |- context [@V.Executor.callcode] ⇒ apply callcode_checked_from_child_bounded
| |- context [@V.Executor.staticcall] ⇒ apply staticcall_checked_from_child_bounded
| |- context [@V.Executor.delegatecall] ⇒ apply delegatecall_checked_from_child_bounded
| |- context [@V.Executor.create] ⇒ apply create_checked_from_child_bounded
| |- context [@V.Executor.create2] ⇒ apply create2_checked_from_child_bounded
end.
all: first [assumption|apply supply_context_primitives|
apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat].
- assert (Hwords : machineWords words state).
{ unfold checkedMachine, machineWords, hostSupplyContext in Hinitial |- ×. tauto. }
pose proof (proj2 Hinitial) as Hsizes.
destruct (nonrecursive_supply_words opcode state Henv Hparams Hrecursive
(proj1 Hsizes) (proj2 Hsizes) Hwords) as [HafterWords|Hbad];
[|right; exact Hbad].
assert (HafterSizes : machineByteSizes (snd (@dispatch S.t F E opcode state)) ∨ badMemoryInitComparison).
{ eapply nonrecursive_preserves_sizes_bounded; try eassumption.
- eapply setcode_clz_sizes. exact Hrevision.
- exact extension_sizes.
- exact (proj1 Hwords).
- exact (proj2 (proj2 (proj2 Hwords))). }
assert (Hcontext : preservesHost (hostContextWords HF.(Host.Instantiate.ChainParams))
(@dispatch S.t F E opcode)).
{ destruct (machineOnlyOpcode opcode) eqn:Hlocal.
- eapply machine_only_preserves_host_core; try eassumption.
+ eapply setcode_clz_preserves_host. exact Hrevision.
+ intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
× eapply setcode_opcode_preserves_host; [exact Hrevision|].
intros current source static gas host Hctx.
eapply host_context_words_preserved;
[apply setcodefrom_preserves_context|exact Hctx].
× unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined.
apply preserves_fail.
- apply primitive_host_preserves_at_any_revision; try assumption.
+ apply primitive_frame_at, context_words_primitives.
+ destruct opcode; cbn [machineOnlyOpcode recursiveCallOpcode primitiveHostOpcode] in *;
congruence. }
specialize (Hcontext state (proj2 (proj1 (proj2 (proj2 (proj1 Hinitial)))))).
unfold checkedMachine, machineWords, hostSupplyContext in ×. tauto.
Qed.
Theorem run_supply_context state :
@setcodeGasProgress S.t F →
environmentWords ((@V.Executor.C S.t F E).(V.Executor_C_signature.execution_environment)) →
(∀ msg, vmMessageBounds msg → ∀ host, invariant host →
let result := (HostF vm).(Evmc.HOST.call) msg host in
(invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
badMemoryInitComparison) →
Z.of_N (MS.t.gas state) < 2^63 → checkedMachine invariant state →
checkedMachine invariant (snd (@generated_run S.t F E state)) ∨ badMemoryInitComparison.
Proof.
intros Hprogress Henv Hchild Hgas Hinitial.
apply (setcode_run_preserves cold warm access write reserve
(fun st ⇒ checkedMachine invariant st ∨ badMemoryInitComparison) state Hrevision Hprogress).
- intros before Hbound [Hbefore|Hbad]; [|right; exact Hbad].
apply dispatch_supply_context; try assumption. apply selected_opcode_word_parameters.
- exact Hgas.
- left; exact Hinitial.
Qed.
End Concrete.
Print Assumptions run_supply_context.