Library SetcodeConcrete.vmcontextframe
VM frames with the actual execution account
From Stdlib Require Import ZArith NArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe
QedkConcrete.vmhostframe SetcodeConcrete.vmprogress SetcodeConcrete.vmframes
QedkConcrete.vmcallframe QedkConcrete.vmcreateframe QedkConcrete.vmboundedframe.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
MonadExecutionSpecs.Termination.VmUnsignedGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section VM.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Lemma setcode_opcode_preserves_at_context cold warm access write reserve invariant :
V.Params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
(∀ source static gas,
preservesCallback invariant
(V.Host.(Evmc.HOST.setcodefrom) V.Executor.self source static gas)) →
preservesHost invariant (dispatch Opcode.Setcodefrom).
Proof.
intros Hrevision Hcallback.
unfold dispatch, V.Executor.execute_opcode, V.Executor.setcodefrom.
rewrite Hrevision.
unfold V.M.HostAPI.setcodefrom, V.M.HostAPI.Base.
solve_host_invariant.
Qed.
Theorem run_preserves_at_context cold warm access write reserve
(invariant : Host_t → Prop) (state : @MS.t Host_t F) :
V.Params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
V.Params.(Vm.Make_Params_signature.debug_tstore) = false →
setcodeGasProgress →
PrimitiveHostFrameAt V.Host invariant V.Executor.self →
(∀ source static gas,
preservesCallback invariant
(V.Host.(Evmc.HOST.setcodefrom) V.Executor.self source static gas)) →
(∀ opcode before, recursiveCallOpcode opcode = true →
(Z.of_N (MS.t.gas before) < 2^63)%Z → invariant (MS.t.host before) →
invariant (MS.t.host (snd (dispatch opcode before)))) →
(Z.of_N (MS.t.gas state) < 2^63)%Z → invariant (MS.t.host state) →
invariant (MS.t.host (snd (@generated_run Host_t F E state))).
Proof.
intros Hrevision Hdebug Hgas Hprimitive Hsetcode Hcalls.
apply (setcode_run_preserves cold warm access write reserve
(fun before ⇒ invariant (MS.t.host before)) state Hrevision Hgas).
intros before Hbound Hbefore.
set (opcode := @run_selected_opcode Host_t F E before).
change (invariant (MS.t.host (snd (dispatch opcode before)))).
destruct (machineOnlyOpcode opcode) eqn:Hlocal.
- eapply machine_only_preserves_host_core; [| |exact Hdebug|exact Hlocal|exact Hbefore].
+ eapply setcode_clz_preserves_host; exact Hrevision.
+ intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
× eapply setcode_opcode_preserves_at_context; eassumption.
× unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined.
apply preserves_fail.
- destruct (primitiveHostOpcode opcode) eqn:Hprim.
+ eapply primitive_host_preserves_at_any_revision; eassumption.
+ apply Hcalls; [|exact Hbound|exact Hbefore].
destruct opcode; cbn [machineOnlyOpcode primitiveHostOpcode recursiveCallOpcode] in *;
congruence.
Qed.
End VM.
Section Entry.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Context (cold warm access write reserve : N).
Context (Hrevision : V.Params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hdebug : V.Params.(Vm.Make_Params_signature.debug_tstore) = false).
Context (Hprogress : @setcodeGasProgress Host_t F).
Context (Hcontext : ∀ host, snd (V.Host.(Evmc.HOST.get_tx_context) host) = host).
Context (Hsize : ∀ addr host, snd (V.Host.(Evmc.HOST.get_code_size) addr host) = host).
Context (Hcopy : ∀ addr offset size host, snd (V.Host.(Evmc.HOST.copy_code) addr offset size host) = host).
Definition entryDelegated addr host :=
if Uint64.op_eq (fst (V.Host.(Evmc.HOST.get_code_size) addr host))
(Uint64.of_int Delegation.eoa_delegated_code_length)
then Delegation.is_valid_delegation (fst (V.Host.(Evmc.HOST.copy_code)
addr 0 Delegation.eoa_delegated_code_length host))
else false.
Lemma execute_preserves_at_context (P : Host_t → Prop) msg code host :
PrimitiveHostFrameAt V.Host P (Evmc.Message.t.recipient msg) →
(∀ source static gas, preservesCallback P (V.Host.(Evmc.HOST.setcodefrom)
(Evmc.Message.t.recipient msg) source static gas)) →
(∀ (env : @V.Executor.FArgs Host_t F) opcode before,
@V.Executor.self Host_t F env = Evmc.Message.t.recipient msg →
(V.Executor.C (FArgs := env)).(V.Executor_C_signature.execution_environment)
.(V.ExecutionEnvironment.t.context_was_delegated) =
entryDelegated (Evmc.Message.t.recipient msg) host →
recursiveCallOpcode opcode = true →
(Z.of_N (MS.t.gas before) < 2^63)%Z → P (MS.t.host before) →
P (MS.t.host (snd (@dispatch Host_t F env opcode before)))) →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
P host → P (snd (@V.execute Host_t F msg code host)).
Proof.
intros Hprimitive Hsetcode Hcalls Hgas Hinitial.
unfold V.execute.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
pose proof (Hcontext host) as Hread.
destruct (V.Host.(Evmc.HOST.get_tx_context) host) as [context middle] eqn:Hctx.
cbn [snd] in Hread. subst middle.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
rewrite Hrevision.
pose proof (Hsize (Evmc.Message.t.recipient msg) host) as HreadSize.
destruct (V.Host.(Evmc.HOST.get_code_size) (Evmc.Message.t.recipient msg) host)
as [size middle] eqn:Hsz. cbn [snd] in HreadSize. subst middle.
unfold entryDelegated in Hcalls. rewrite Hsz in Hcalls. cbn [fst] in Hcalls.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hlength end.
2: cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
1: (pose proof (Hcopy (Evmc.Message.t.recipient msg) 0 Delegation.eoa_delegated_code_length host) as HreadCopy;
destruct (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0
Delegation.eoa_delegated_code_length host) as [ownCode middle] eqn:HownCode;
cbn [snd] in HreadCopy; subst middle;
change (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0 23 host = (ownCode, host)) in HownCode;
rewrite HownCode;
try rewrite HownCode in Hcalls; cbn [fst] in Hcalls;
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host]).
all: change (Uint64.of_int Delegation.eoa_delegated_code_length) with 23 in Hcalls.
all: rewrite Hlength in Hcalls.
all: match goal with |- context [@V.Executor.run _ _ ?env ?state] ⇒
assert (Hrun : P (MS.t.host (snd (@generated_run Host_t F env state)))) by
(apply (@run_preserves_at_context Host_t F env cold warm access write reserve P state);
[exact Hrevision|exact Hdebug|exact Hprogress|exact Hprimitive|exact Hsetcode|
exact (fun opcode before ⇒ Hcalls env opcode before eq_refl eq_refl)|
apply initial_gas_fits_signed_int64; exact Hgas|exact Hinitial]);
unfold generated_run in Hrun;
destruct (@V.Executor.run Host_t F env state) as [outcome after]
end.
all: destruct outcome as [[]|error]; [exact Hrun|].
all: destruct error; exact Hrun.
Qed.
End Entry.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe
QedkConcrete.vmhostframe SetcodeConcrete.vmprogress SetcodeConcrete.vmframes
QedkConcrete.vmcallframe QedkConcrete.vmcreateframe QedkConcrete.vmboundedframe.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
MonadExecutionSpecs.Termination.VmUnsignedGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Section VM.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Lemma setcode_opcode_preserves_at_context cold warm access write reserve invariant :
V.Params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
(∀ source static gas,
preservesCallback invariant
(V.Host.(Evmc.HOST.setcodefrom) V.Executor.self source static gas)) →
preservesHost invariant (dispatch Opcode.Setcodefrom).
Proof.
intros Hrevision Hcallback.
unfold dispatch, V.Executor.execute_opcode, V.Executor.setcodefrom.
rewrite Hrevision.
unfold V.M.HostAPI.setcodefrom, V.M.HostAPI.Base.
solve_host_invariant.
Qed.
Theorem run_preserves_at_context cold warm access write reserve
(invariant : Host_t → Prop) (state : @MS.t Host_t F) :
V.Params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
V.Params.(Vm.Make_Params_signature.debug_tstore) = false →
setcodeGasProgress →
PrimitiveHostFrameAt V.Host invariant V.Executor.self →
(∀ source static gas,
preservesCallback invariant
(V.Host.(Evmc.HOST.setcodefrom) V.Executor.self source static gas)) →
(∀ opcode before, recursiveCallOpcode opcode = true →
(Z.of_N (MS.t.gas before) < 2^63)%Z → invariant (MS.t.host before) →
invariant (MS.t.host (snd (dispatch opcode before)))) →
(Z.of_N (MS.t.gas state) < 2^63)%Z → invariant (MS.t.host state) →
invariant (MS.t.host (snd (@generated_run Host_t F E state))).
Proof.
intros Hrevision Hdebug Hgas Hprimitive Hsetcode Hcalls.
apply (setcode_run_preserves cold warm access write reserve
(fun before ⇒ invariant (MS.t.host before)) state Hrevision Hgas).
intros before Hbound Hbefore.
set (opcode := @run_selected_opcode Host_t F E before).
change (invariant (MS.t.host (snd (dispatch opcode before)))).
destruct (machineOnlyOpcode opcode) eqn:Hlocal.
- eapply machine_only_preserves_host_core; [| |exact Hdebug|exact Hlocal|exact Hbefore].
+ eapply setcode_clz_preserves_host; exact Hrevision.
+ intros extra Hextra. destruct extra; cbn in Hextra; try contradiction.
× eapply setcode_opcode_preserves_at_context; eassumption.
× unfold dispatch, V.Executor.execute_opcode, V.Executor.undefined.
apply preserves_fail.
- destruct (primitiveHostOpcode opcode) eqn:Hprim.
+ eapply primitive_host_preserves_at_any_revision; eassumption.
+ apply Hcalls; [|exact Hbound|exact Hbefore].
destruct opcode; cbn [machineOnlyOpcode primitiveHostOpcode recursiveCallOpcode] in *;
congruence.
Qed.
End VM.
Section Entry.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
(apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Context (cold warm access write reserve : N).
Context (Hrevision : V.Params.(Vm.Make_Params_signature.revision) =
Support.Variants.Revision.Setcode (cold, warm, access, write, reserve)).
Context (Hdebug : V.Params.(Vm.Make_Params_signature.debug_tstore) = false).
Context (Hprogress : @setcodeGasProgress Host_t F).
Context (Hcontext : ∀ host, snd (V.Host.(Evmc.HOST.get_tx_context) host) = host).
Context (Hsize : ∀ addr host, snd (V.Host.(Evmc.HOST.get_code_size) addr host) = host).
Context (Hcopy : ∀ addr offset size host, snd (V.Host.(Evmc.HOST.copy_code) addr offset size host) = host).
Definition entryDelegated addr host :=
if Uint64.op_eq (fst (V.Host.(Evmc.HOST.get_code_size) addr host))
(Uint64.of_int Delegation.eoa_delegated_code_length)
then Delegation.is_valid_delegation (fst (V.Host.(Evmc.HOST.copy_code)
addr 0 Delegation.eoa_delegated_code_length host))
else false.
Lemma execute_preserves_at_context (P : Host_t → Prop) msg code host :
PrimitiveHostFrameAt V.Host P (Evmc.Message.t.recipient msg) →
(∀ source static gas, preservesCallback P (V.Host.(Evmc.HOST.setcodefrom)
(Evmc.Message.t.recipient msg) source static gas)) →
(∀ (env : @V.Executor.FArgs Host_t F) opcode before,
@V.Executor.self Host_t F env = Evmc.Message.t.recipient msg →
(V.Executor.C (FArgs := env)).(V.Executor_C_signature.execution_environment)
.(V.ExecutionEnvironment.t.context_was_delegated) =
entryDelegated (Evmc.Message.t.recipient msg) host →
recursiveCallOpcode opcode = true →
(Z.of_N (MS.t.gas before) < 2^63)%Z → P (MS.t.host before) →
P (MS.t.host (snd (@dispatch Host_t F env opcode before)))) →
(0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
P host → P (snd (@V.execute Host_t F msg code host)).
Proof.
intros Hprimitive Hsetcode Hcalls Hgas Hinitial.
unfold V.execute.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
pose proof (Hcontext host) as Hread.
destruct (V.Host.(Evmc.HOST.get_tx_context) host) as [context middle] eqn:Hctx.
cbn [snd] in Hread. subst middle.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
rewrite Hrevision.
pose proof (Hsize (Evmc.Message.t.recipient msg) host) as HreadSize.
destruct (V.Host.(Evmc.HOST.get_code_size) (Evmc.Message.t.recipient msg) host)
as [size middle] eqn:Hsz. cbn [snd] in HreadSize. subst middle.
unfold entryDelegated in Hcalls. rewrite Hsz in Hcalls. cbn [fst] in Hcalls.
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
match goal with |- context [if ?test then _ else _] ⇒ destruct test eqn:Hlength end.
2: cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host].
1: (pose proof (Hcopy (Evmc.Message.t.recipient msg) 0 Delegation.eoa_delegated_code_length host) as HreadCopy;
destruct (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0
Delegation.eoa_delegated_code_length host) as [ownCode middle] eqn:HownCode;
cbn [snd] in HreadCopy; subst middle;
change (V.Host.(Evmc.HOST.copy_code) (Evmc.Message.t.recipient msg) 0 23 host = (ownCode, host)) in HownCode;
rewrite HownCode;
try rewrite HownCode in Hcalls; cbn [fst] in Hcalls;
cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host]).
all: change (Uint64.of_int Delegation.eoa_delegated_code_length) with 23 in Hcalls.
all: rewrite Hlength in Hcalls.
all: match goal with |- context [@V.Executor.run _ _ ?env ?state] ⇒
assert (Hrun : P (MS.t.host (snd (@generated_run Host_t F env state)))) by
(apply (@run_preserves_at_context Host_t F env cold warm access write reserve P state);
[exact Hrevision|exact Hdebug|exact Hprogress|exact Hprimitive|exact Hsetcode|
exact (fun opcode before ⇒ Hcalls env opcode before eq_refl eq_refl)|
apply initial_gas_fits_signed_int64; exact Hgas|exact Hinitial]);
unfold generated_run in Hrun;
destruct (@V.Executor.run Host_t F env state) as [outcome after]
end.
all: destruct outcome as [[]|error]; [exact Hrun|].
all: destruct error; exact Hrun.
Qed.
End Entry.