Library QedkConcrete.vmcallframe
Frame through CALL-family instructions
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vminvariant QedkConcrete.vmhostframe
QedkConcrete.vmboundedframe.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress
MonadExecutionSpecs.Termination.VmCreateHandlerProgress
MonadExecutionSpecs.Termination.VmCallHandlerProgress
MonadExecutionSpecs.Termination.VmChildGasMerge
MonadExecutionSpecs.Termination.VmUnsignedGas
MonadExecutionSpecs.Termination.VmCallGas.
Set Default Goal Selector "!".
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vminvariant QedkConcrete.vmhostframe
QedkConcrete.vmboundedframe.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress
MonadExecutionSpecs.Termination.VmCreateHandlerProgress
MonadExecutionSpecs.Termination.VmCallHandlerProgress
MonadExecutionSpecs.Termination.VmChildGasMerge
MonadExecutionSpecs.Termination.VmUnsignedGas
MonadExecutionSpecs.Termination.VmCallGas.
Set Default Goal Selector "!".
CALL and creation spend from the current execution account. DELEGATECALL
keeps that account as recipient but preserves the parent's sender;
treating its sender as the debited account would be incorrect.
Definition childCallFrom (self : Chain.Ethereum.Address.t) (msg : Evmc.Message.t) : Prop :=
match Evmc.Message.t.kind msg with
| Evmc.Message.CallKind.DelegateCall ⇒ Evmc.Message.t.recipient msg = self
| Evmc.Message.CallKind.CallCode ⇒
Evmc.Message.t.sender msg = self ∧ Evmc.Message.t.recipient msg = self
| _ ⇒ Evmc.Message.t.sender msg = self
end.
Ltac expose_call_primitives :=
unfold V.M.HostAPI.get_balance, V.M.HostAPI.account_exists,
V.M.HostAPI.copy_code, V.M.HostAPI.access_account, V.M.HostAPI.Base.
Ltac solve_call_frame_extra :=
lazymatch goal with
| Hcall : ∀ msg, _ → _ → _ → preservesCallback _ _
|- preservesHost _ (@V.M.HostAPI.call _ _ _) ⇒
apply host_call_preserves; apply Hcall;
[cbn [Evmc.Message.t.gas]; rewrite u256_forwarded_gas_small_identity by lia; lia
|reflexivity|repeat split; reflexivity]
| Hcall : ∀ msg, _ → _ → preservesCallback _ _
|- preservesHost _ (@V.M.HostAPI.call _ _ _) ⇒
apply host_call_preserves; apply Hcall;
[cbn [Evmc.Message.t.gas]; rewrite u256_forwarded_gas_small_identity by lia; lia
|reflexivity]
| Hcall : ∀ msg, _ → preservesCallback _ _
|- preservesHost _ (@V.M.HostAPI.call _ _ _) ⇒
apply host_call_preserves; apply Hcall; cbn [Evmc.Message.t.gas];
rewrite u256_forwarded_gas_small_identity by lia; lia
| |- preservesHost _ (@V.M.HostAPI.lift _ _ _ _) ⇒
apply preserves_lift; unfold preservesCallback in *; eauto
| |- preservesHost _ (match ?value with _ ⇒ _ end) ⇒
destruct value; solve_host_invariant
| |- ?g ⇒ fail "Unhandled call frame goal:" g
end.
Ltac solve_host_invariant_extra ::= solve_call_frame_extra.
Section Opcodes.
Context {Host_t : Set} {F : @MonadVM.Vm.Make.FArgs Host_t}.
Context `{U0 : RocqOfOCaml.Basics.Unreachable Z.t}.
Context `{U1 :
RocqOfOCaml.Basics.Unreachable (RocqOfOCaml.OCamlSeq.t int)}.
Context `{U2 : RocqOfOCaml.Basics.Unreachable RocqOfOCaml.OCamlInt32.t}.
Context `{U3 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}.
Context `{U4 :
RocqOfOCaml.Basics.Unreachable MonadVM.Support.UintN.t}.
Context `{U5 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}.
Context `{U7 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.Integer.t}.
Context `{U8 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.I256.t}.
Context `{U9 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}.
Context `{U10 :
RocqOfOCaml.Basics.Unreachable MonadVM.Delegation.Address.t}.
Context `{U11 :
RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}.
Context `{U12 :
RocqOfOCaml.Basics.Unreachable MonadVM.Byte_string.Bytes.t}.
Context `{U13 :
RocqOfOCaml.Basics.Unreachable
(@MonadVM.Vm.Make.M.Option.t Host_t F MonadVM.Numeric.U256.t)}.
Context `{U15 : RocqOfOCaml.Basics.Unreachable unit}.
Context `{U16 : RocqOfOCaml.Basics.Unreachable string}.
Context `{U17 :
RocqOfOCaml.Basics.Unreachable (option MonadVM.Numeric.U256.t)}.
Context `{U18 : RocqOfOCaml.Basics.Unreachable (option string)}.
Context `{U19 :
RocqOfOCaml.Basics.Unreachable
(list MonadVM.Byte_string.Bytes.t)}.
Context `{U20 : RocqOfOCaml.Basics.Unreachable (list int)}.
Context `{U21 : RocqOfOCaml.Basics.Unreachable int}.
Context `{U22 : RocqOfOCaml.Basics.Unreachable ascii}.
Context `{U23 : RocqOfOCaml.Basics.Unreachable (int × int)}.
Context `{U24 :
RocqOfOCaml.Basics.Unreachable
(Z.t → MonadVM.Numeric.U256.t)}.
Context `{U25 :
RocqOfOCaml.Basics.Unreachable
(MonadVM.Support.UintN.t → MonadVM.Support.UintN.t)}.
Context `{U26 :
RocqOfOCaml.Basics.Unreachable
(unit → RocqOfOCaml.OCamlSeq.node int)}.
Context `{U27 :
RocqOfOCaml.Basics.Unreachable
(@MonadVM.Vm.Make.opcode_impl Host_t F)}.
Context `{U28 :
RocqOfOCaml.Basics.Unreachable
MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.t)}.
Context `{U31 :
RocqOfOCaml.Basics.Unreachable
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.t)}.
Context `{U32 :
RocqOfOCaml.Basics.Unreachable
(MonadVM.Numeric.U256.t → MonadVM.Numeric.U256.t →
@MonadVM.Vm.Make.Memory.t Host_t F →
@MonadVM.Vm.Make.M.Option.t Host_t F
(@MonadVM.Vm.Make.Memory.t Host_t F))}.
Context `{U33 :
RocqOfOCaml.Basics.Unreachable
(@MonadVM.Vm.Make.Memory.t Host_t F →
MonadVM.Support.UintN.t)}.
Let State := @MonadVM.Vm.Make.MachineState.t Host_t F.
Section Environment.
Context {E : @V.Executor.FArgs Host_t F}.
match Evmc.Message.t.kind msg with
| Evmc.Message.CallKind.DelegateCall ⇒ Evmc.Message.t.recipient msg = self
| Evmc.Message.CallKind.CallCode ⇒
Evmc.Message.t.sender msg = self ∧ Evmc.Message.t.recipient msg = self
| _ ⇒ Evmc.Message.t.sender msg = self
end.
Ltac expose_call_primitives :=
unfold V.M.HostAPI.get_balance, V.M.HostAPI.account_exists,
V.M.HostAPI.copy_code, V.M.HostAPI.access_account, V.M.HostAPI.Base.
Ltac solve_call_frame_extra :=
lazymatch goal with
| Hcall : ∀ msg, _ → _ → _ → preservesCallback _ _
|- preservesHost _ (@V.M.HostAPI.call _ _ _) ⇒
apply host_call_preserves; apply Hcall;
[cbn [Evmc.Message.t.gas]; rewrite u256_forwarded_gas_small_identity by lia; lia
|reflexivity|repeat split; reflexivity]
| Hcall : ∀ msg, _ → _ → preservesCallback _ _
|- preservesHost _ (@V.M.HostAPI.call _ _ _) ⇒
apply host_call_preserves; apply Hcall;
[cbn [Evmc.Message.t.gas]; rewrite u256_forwarded_gas_small_identity by lia; lia
|reflexivity]
| Hcall : ∀ msg, _ → preservesCallback _ _
|- preservesHost _ (@V.M.HostAPI.call _ _ _) ⇒
apply host_call_preserves; apply Hcall; cbn [Evmc.Message.t.gas];
rewrite u256_forwarded_gas_small_identity by lia; lia
| |- preservesHost _ (@V.M.HostAPI.lift _ _ _ _) ⇒
apply preserves_lift; unfold preservesCallback in *; eauto
| |- preservesHost _ (match ?value with _ ⇒ _ end) ⇒
destruct value; solve_host_invariant
| |- ?g ⇒ fail "Unhandled call frame goal:" g
end.
Ltac solve_host_invariant_extra ::= solve_call_frame_extra.
Section Opcodes.
Context {Host_t : Set} {F : @MonadVM.Vm.Make.FArgs Host_t}.
Context `{U0 : RocqOfOCaml.Basics.Unreachable Z.t}.
Context `{U1 :
RocqOfOCaml.Basics.Unreachable (RocqOfOCaml.OCamlSeq.t int)}.
Context `{U2 : RocqOfOCaml.Basics.Unreachable RocqOfOCaml.OCamlInt32.t}.
Context `{U3 : RocqOfOCaml.Basics.Unreachable MonadVM.Uint64.t}.
Context `{U4 :
RocqOfOCaml.Basics.Unreachable MonadVM.Support.UintN.t}.
Context `{U5 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.U256.Repr.t}.
Context `{U7 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.Integer.t}.
Context `{U8 :
RocqOfOCaml.Basics.Unreachable MonadVM.Numeric.I256.t}.
Context `{U9 : RocqOfOCaml.Basics.Unreachable MonadVM.Gas.t}.
Context `{U10 :
RocqOfOCaml.Basics.Unreachable MonadVM.Delegation.Address.t}.
Context `{U11 :
RocqOfOCaml.Basics.Unreachable MonadVM.Chain.Ethereum.Address.t}.
Context `{U12 :
RocqOfOCaml.Basics.Unreachable MonadVM.Byte_string.Bytes.t}.
Context `{U13 :
RocqOfOCaml.Basics.Unreachable
(@MonadVM.Vm.Make.M.Option.t Host_t F MonadVM.Numeric.U256.t)}.
Context `{U15 : RocqOfOCaml.Basics.Unreachable unit}.
Context `{U16 : RocqOfOCaml.Basics.Unreachable string}.
Context `{U17 :
RocqOfOCaml.Basics.Unreachable (option MonadVM.Numeric.U256.t)}.
Context `{U18 : RocqOfOCaml.Basics.Unreachable (option string)}.
Context `{U19 :
RocqOfOCaml.Basics.Unreachable
(list MonadVM.Byte_string.Bytes.t)}.
Context `{U20 : RocqOfOCaml.Basics.Unreachable (list int)}.
Context `{U21 : RocqOfOCaml.Basics.Unreachable int}.
Context `{U22 : RocqOfOCaml.Basics.Unreachable ascii}.
Context `{U23 : RocqOfOCaml.Basics.Unreachable (int × int)}.
Context `{U24 :
RocqOfOCaml.Basics.Unreachable
(Z.t → MonadVM.Numeric.U256.t)}.
Context `{U25 :
RocqOfOCaml.Basics.Unreachable
(MonadVM.Support.UintN.t → MonadVM.Support.UintN.t)}.
Context `{U26 :
RocqOfOCaml.Basics.Unreachable
(unit → RocqOfOCaml.OCamlSeq.node int)}.
Context `{U27 :
RocqOfOCaml.Basics.Unreachable
(@MonadVM.Vm.Make.opcode_impl Host_t F)}.
Context `{U28 :
RocqOfOCaml.Basics.Unreachable
MonadVM.Numeric.IntegerBase.(MonadVM.Numeric.Make.Make_result.t)}.
Context `{U31 :
RocqOfOCaml.Basics.Unreachable
MonadVM.Byte_string.B32.(MonadVM.Byte_string.Fixed.Fixed_result.t)}.
Context `{U32 :
RocqOfOCaml.Basics.Unreachable
(MonadVM.Numeric.U256.t → MonadVM.Numeric.U256.t →
@MonadVM.Vm.Make.Memory.t Host_t F →
@MonadVM.Vm.Make.M.Option.t Host_t F
(@MonadVM.Vm.Make.Memory.t Host_t F))}.
Context `{U33 :
RocqOfOCaml.Basics.Unreachable
(@MonadVM.Vm.Make.Memory.t Host_t F →
MonadVM.Support.UintN.t)}.
Let State := @MonadVM.Vm.Make.MachineState.t Host_t F.
Section Environment.
Context {E : @V.Executor.FArgs Host_t F}.
Child messages retain the call kind, sender and execution-context
address selected by the opcode. Restricting the callback premise to
those fields lets nondebit proofs distinguish CALL from DELEGATECALL;
requiring preservation for arbitrary callers would be too strong.
Lemma generic_call_preserves_at_context (invariant : Host_t → Prop)
kind forwarded value sender recipient codeAddr delegation static
inputStart inputSize outputStart outputSize :
(Z.of_N forwarded < 2^63)%Z →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = kind ∧ Evmc.Message.t.sender msg = sender ∧
Evmc.Message.t.recipient msg = recipient) →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesHost invariant (@V.Executor.generic_call_impl Host_t F E
kind (@Numeric.U256.of_uint_exn forwarded) value
sender recipient codeAddr delegation static inputStart inputSize outputStart outputSize).
Proof.
intros Hgas Hcall.
unfold V.Executor.generic_call_impl, V.Executor.merge_child_gas_and_refund.
solve_host_invariant.
Qed.
kind forwarded value sender recipient codeAddr delegation static
inputStart inputSize outputStart outputSize :
(Z.of_N forwarded < 2^63)%Z →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = kind ∧ Evmc.Message.t.sender msg = sender ∧
Evmc.Message.t.recipient msg = recipient) →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesHost invariant (@V.Executor.generic_call_impl Host_t F E
kind (@Numeric.U256.of_uint_exn forwarded) value
sender recipient codeAddr delegation static inputStart inputSize outputStart outputSize).
Proof.
intros Hgas Hcall.
unfold V.Executor.generic_call_impl, V.Executor.merge_child_gas_and_refund.
solve_host_invariant.
Qed.
The recursive callback is needed only at the value actually placed in
the child message; callers can derive its nonnegativity from the stack.
Lemma generic_call_preserves_at_value (invariant : Host_t → Prop)
kind forwarded value sender recipient codeAddr delegation static
inputStart inputSize outputStart outputSize :
(Z.of_N forwarded < 2^63)%Z →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesHost invariant (@V.Executor.generic_call_impl Host_t F E
kind (@Numeric.U256.of_uint_exn forwarded) value
sender recipient codeAddr delegation static inputStart inputSize outputStart outputSize).
Proof.
intros Hgas Hcall. apply generic_call_preserves_at_context; [exact Hgas|].
intros msg Hbound Hvalue _. apply Hcall; assumption.
Qed.
Lemma generic_call_preserves (invariant : Host_t → Prop)
kind forwarded value sender recipient codeAddr delegation static
inputStart inputSize outputStart outputSize :
(Z.of_N forwarded < 2^63)%Z →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesHost invariant (@V.Executor.generic_call_impl Host_t F E
kind (@Numeric.U256.of_uint_exn forwarded) value
sender recipient codeAddr delegation static inputStart inputSize outputStart outputSize).
Proof.
intros Hgas Hcall. apply generic_call_preserves_at_value; [exact Hgas|].
intros msg Hbound _. apply Hcall. exact Hbound.
Qed.
Ltac solve_host_invariant_extra ::=
lazymatch goal with
| Hcall : ∀ msg, _ → _ → _ → preservesCallback _ _
|- preservesHost _ (@V.Executor.generic_call_impl _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) ⇒
apply generic_call_preserves_at_context; [assumption|exact Hcall]
| Hcall : ∀ msg, _ → _ → preservesCallback _ _
|- preservesHost _ (@V.Executor.generic_call_impl _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) ⇒
apply generic_call_preserves_at_value; [assumption|exact Hcall]
| _ ⇒ solve_call_frame_extra
end.
Lemma call_opcode_preserves_at_context (invariant : Host_t → Prop)
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = kind ∧ Evmc.Message.t.sender msg = sender ∧
Evmc.Message.t.recipient msg = recipient) →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call_opcode_impl Host_t F E
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static).
Proof.
intros Hprimitive Hcall. destruct Hprimitive.
unfold V.Executor.call_opcode_impl.
apply bounded_frame_bind_prefix.
- unfold V.Executor.extend_memory_to. solve_host_invariant.
- apply extend_memory_to_nonincreasing.
- intro inputGas. apply bounded_frame_bind_prefix.
+ unfold V.Executor.extend_memory_to. solve_host_invariant.
+ apply extend_memory_to_nonincreasing.
+ intro outputGas. apply bounded_frame_bind_map_prefix.
× expose_call_primitives. solve_host_invariant.
× apply access_account_nonincreasing.
× intro accessStatus. apply bounded_frame_bind_prefix.
{ unfold V.Executor.access_delegation. expose_call_primitives. solve_host_invariant. }
{ apply access_delegation_nonincreasing. }
intro delegation. apply bounded_frame_bind_prefix.
{ expose_call_primitives. solve_host_invariant. }
{ apply account_exists_nonincreasing. }
intro targetAlive. intros state Hgas Hinitial.
match goal with |- invariant (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ _ ?next state))) ⇒
change (invariant (MS.t.host (snd (next (MS.t.gas state) state))))
end.
cbn beta.
set (transfer := andb (Evmc.Message.CallKind.op_ltgt kind
Evmc.Message.CallKind.DelegateCall)
(Numeric.U256.op_ltgt value (@Numeric.U256.zero))) in ×.
set (memoryGas := @Gas.op_plus inputGas outputGas) in ×.
match goal with |- context [@Gas.call_gas_value transfer requested
(MS.t.gas state) memoryGas ?cost] ⇒ set (extra := cost) in × end.
assert (Hextra : (100 + (if transfer then @Gas.call_value else 0) ≤ extra)%N).
{ unfold extra.
pose proof (@account_access_min accessStatus) as Haccess.
destruct transfer, delegation, targetAlive;
cbn [andb negb]; unfold Gas.op_plus, Gas.zero, UintN.op_plus, UintN.zero;
lia. }
match goal with |- invariant (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ (@V.spend _ _ ?amount) ?next state))) ⇒
set (nextCall := next); set (spentGas := amount)
end.
pose proof (@preserves_spend Host_t F invariant spentGas state Hinitial) as Hframe.
change (invariant (MS.t.host (snd
(match @V.spend Host_t F spentGas state with
| (inl value, middle) ⇒ nextCall value middle
| (inr error, middle) ⇒ (inr error, middle)
end)))).
destruct (@V.spend Host_t F spentGas state) as [[[]|error] charged] eqn:Hspent.
2: exact Hframe.
pose proof (spend_success_exact _ _ _ Hspent) as [Hafford Hcharged].
pose proof (@call_quote_total_decreases transfer requested
(MS.t.gas state) memoryGas extra Hextra Hafford) as Hchild.
cbn zeta in Hchild.
assert (Hforwarded : (Z.of_N
(@Gas.call_gas_value transfer requested (MS.t.gas state) memoryGas extra)
.(Gas.call_gas.callee_available_gas) < 2^63)%Z) by lia.
assert (Hnext : preservesHost invariant (nextCall tt)).
{ unfold nextCall, V.Executor.check_write_permissions.
expose_call_primitives. solve_host_invariant. }
apply Hnext. exact Hframe.
Qed.
Lemma call_opcode_preserves_at_value (invariant : Host_t → Prop)
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call_opcode_impl Host_t F E
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static).
Proof.
intros Hprimitive Hcall. apply call_opcode_preserves_at_context;
[apply primitive_frame_at; exact Hprimitive|].
intros msg Hbound Hvalue _. apply Hcall; assumption.
Qed.
Lemma call_opcode_preserves (invariant : Host_t → Prop)
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call_opcode_impl Host_t F E
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static).
Proof.
intros Hprimitive Hcall. apply call_opcode_preserves_at_value; [exact Hprimitive|].
intros msg Hbound _. apply Hcall. exact Hbound.
Qed.
Ltac solve_call_pop_gas :=
lazymatch goal with
| |- gas_nonincreasing (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒
apply map_nonincreasing; solve_call_pop_gas
| |- gas_nonincreasing (@V.pop _ _) ⇒ apply pop_nonincreasing
| |- gas_nonincreasing (@V.pop2 _ _) ⇒ apply pop2_nonincreasing
| |- gas_nonincreasing (@V.pop3 _ _) ⇒ apply pop3_nonincreasing
end.
Ltac solve_call_wrapper :=
lazymatch goal with
| |- preservesBoundedHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
apply bounded_frame_bind_prefix;
[solve_host_invariant | solve_call_pop_gas | ];
let value := fresh "value" in intro value;
repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
cbn beta iota zeta; solve_call_wrapper
| _ ⇒ apply call_opcode_preserves; assumption
end.
Lemma call_preserves (invariant : Host_t → Prop) :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.call. solve_call_wrapper.
Qed.
Lemma callcode_preserves (invariant : Host_t → Prop) :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.callcode Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.callcode. solve_call_wrapper.
Qed.
Lemma delegatecall_preserves (invariant : Host_t → Prop) :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.delegatecall Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.delegatecall. solve_call_wrapper.
Qed.
Lemma staticcall_preserves (invariant : Host_t → Prop) :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.staticcall Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.staticcall. solve_call_wrapper.
Qed.
kind forwarded value sender recipient codeAddr delegation static
inputStart inputSize outputStart outputSize :
(Z.of_N forwarded < 2^63)%Z →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesHost invariant (@V.Executor.generic_call_impl Host_t F E
kind (@Numeric.U256.of_uint_exn forwarded) value
sender recipient codeAddr delegation static inputStart inputSize outputStart outputSize).
Proof.
intros Hgas Hcall. apply generic_call_preserves_at_context; [exact Hgas|].
intros msg Hbound Hvalue _. apply Hcall; assumption.
Qed.
Lemma generic_call_preserves (invariant : Host_t → Prop)
kind forwarded value sender recipient codeAddr delegation static
inputStart inputSize outputStart outputSize :
(Z.of_N forwarded < 2^63)%Z →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesHost invariant (@V.Executor.generic_call_impl Host_t F E
kind (@Numeric.U256.of_uint_exn forwarded) value
sender recipient codeAddr delegation static inputStart inputSize outputStart outputSize).
Proof.
intros Hgas Hcall. apply generic_call_preserves_at_value; [exact Hgas|].
intros msg Hbound _. apply Hcall. exact Hbound.
Qed.
Ltac solve_host_invariant_extra ::=
lazymatch goal with
| Hcall : ∀ msg, _ → _ → _ → preservesCallback _ _
|- preservesHost _ (@V.Executor.generic_call_impl _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) ⇒
apply generic_call_preserves_at_context; [assumption|exact Hcall]
| Hcall : ∀ msg, _ → _ → preservesCallback _ _
|- preservesHost _ (@V.Executor.generic_call_impl _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) ⇒
apply generic_call_preserves_at_value; [assumption|exact Hcall]
| _ ⇒ solve_call_frame_extra
end.
Lemma call_opcode_preserves_at_context (invariant : Host_t → Prop)
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = kind ∧ Evmc.Message.t.sender msg = sender ∧
Evmc.Message.t.recipient msg = recipient) →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call_opcode_impl Host_t F E
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static).
Proof.
intros Hprimitive Hcall. destruct Hprimitive.
unfold V.Executor.call_opcode_impl.
apply bounded_frame_bind_prefix.
- unfold V.Executor.extend_memory_to. solve_host_invariant.
- apply extend_memory_to_nonincreasing.
- intro inputGas. apply bounded_frame_bind_prefix.
+ unfold V.Executor.extend_memory_to. solve_host_invariant.
+ apply extend_memory_to_nonincreasing.
+ intro outputGas. apply bounded_frame_bind_map_prefix.
× expose_call_primitives. solve_host_invariant.
× apply access_account_nonincreasing.
× intro accessStatus. apply bounded_frame_bind_prefix.
{ unfold V.Executor.access_delegation. expose_call_primitives. solve_host_invariant. }
{ apply access_delegation_nonincreasing. }
intro delegation. apply bounded_frame_bind_prefix.
{ expose_call_primitives. solve_host_invariant. }
{ apply account_exists_nonincreasing. }
intro targetAlive. intros state Hgas Hinitial.
match goal with |- invariant (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ _ ?next state))) ⇒
change (invariant (MS.t.host (snd (next (MS.t.gas state) state))))
end.
cbn beta.
set (transfer := andb (Evmc.Message.CallKind.op_ltgt kind
Evmc.Message.CallKind.DelegateCall)
(Numeric.U256.op_ltgt value (@Numeric.U256.zero))) in ×.
set (memoryGas := @Gas.op_plus inputGas outputGas) in ×.
match goal with |- context [@Gas.call_gas_value transfer requested
(MS.t.gas state) memoryGas ?cost] ⇒ set (extra := cost) in × end.
assert (Hextra : (100 + (if transfer then @Gas.call_value else 0) ≤ extra)%N).
{ unfold extra.
pose proof (@account_access_min accessStatus) as Haccess.
destruct transfer, delegation, targetAlive;
cbn [andb negb]; unfold Gas.op_plus, Gas.zero, UintN.op_plus, UintN.zero;
lia. }
match goal with |- invariant (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ (@V.spend _ _ ?amount) ?next state))) ⇒
set (nextCall := next); set (spentGas := amount)
end.
pose proof (@preserves_spend Host_t F invariant spentGas state Hinitial) as Hframe.
change (invariant (MS.t.host (snd
(match @V.spend Host_t F spentGas state with
| (inl value, middle) ⇒ nextCall value middle
| (inr error, middle) ⇒ (inr error, middle)
end)))).
destruct (@V.spend Host_t F spentGas state) as [[[]|error] charged] eqn:Hspent.
2: exact Hframe.
pose proof (spend_success_exact _ _ _ Hspent) as [Hafford Hcharged].
pose proof (@call_quote_total_decreases transfer requested
(MS.t.gas state) memoryGas extra Hextra Hafford) as Hchild.
cbn zeta in Hchild.
assert (Hforwarded : (Z.of_N
(@Gas.call_gas_value transfer requested (MS.t.gas state) memoryGas extra)
.(Gas.call_gas.callee_available_gas) < 2^63)%Z) by lia.
assert (Hnext : preservesHost invariant (nextCall tt)).
{ unfold nextCall, V.Executor.check_write_permissions.
expose_call_primitives. solve_host_invariant. }
apply Hnext. exact Hframe.
Qed.
Lemma call_opcode_preserves_at_value (invariant : Host_t → Prop)
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call_opcode_impl Host_t F E
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static).
Proof.
intros Hprimitive Hcall. apply call_opcode_preserves_at_context;
[apply primitive_frame_at; exact Hprimitive|].
intros msg Hbound Hvalue _. apply Hcall; assumption.
Qed.
Lemma call_opcode_preserves (invariant : Host_t → Prop)
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call_opcode_impl Host_t F E
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static).
Proof.
intros Hprimitive Hcall. apply call_opcode_preserves_at_value; [exact Hprimitive|].
intros msg Hbound _. apply Hcall. exact Hbound.
Qed.
Ltac solve_call_pop_gas :=
lazymatch goal with
| |- gas_nonincreasing (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒
apply map_nonincreasing; solve_call_pop_gas
| |- gas_nonincreasing (@V.pop _ _) ⇒ apply pop_nonincreasing
| |- gas_nonincreasing (@V.pop2 _ _) ⇒ apply pop2_nonincreasing
| |- gas_nonincreasing (@V.pop3 _ _) ⇒ apply pop3_nonincreasing
end.
Ltac solve_call_wrapper :=
lazymatch goal with
| |- preservesBoundedHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
apply bounded_frame_bind_prefix;
[solve_host_invariant | solve_call_pop_gas | ];
let value := fresh "value" in intro value;
repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
cbn beta iota zeta; solve_call_wrapper
| _ ⇒ apply call_opcode_preserves; assumption
end.
Lemma call_preserves (invariant : Host_t → Prop) :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.call. solve_call_wrapper.
Qed.
Lemma callcode_preserves (invariant : Host_t → Prop) :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.callcode Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.callcode. solve_call_wrapper.
Qed.
Lemma delegatecall_preserves (invariant : Host_t → Prop) :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.delegatecall Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.delegatecall. solve_call_wrapper.
Qed.
Lemma staticcall_preserves (invariant : Host_t → Prop) :
PrimitiveHostFrame (@V.Host Host_t F) invariant →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.staticcall Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.staticcall. solve_call_wrapper.
Qed.
These are premises only for child messages the opcode can actually
issue. They are proved from the generated handlers, not assumed as an
EVM authorization rule. Other primitive callbacks still use their
existing frame premises.
Ltac solve_call_origin_wrapper Hprimitive Hcall :=
lazymatch goal with
| |- preservesBoundedHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
apply bounded_frame_bind_prefix;
[solve_host_invariant | solve_call_pop_gas | ];
let value := fresh "value" in intro value;
repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
cbn beta iota zeta; solve_call_origin_wrapper Hprimitive Hcall
| _ ⇒ apply call_opcode_preserves_at_context; [exact Hprimitive|];
intros msg Hgas Hvalue [Hkind [Hsender Hrecipient]];
apply Hcall; [exact Hgas|]; unfold childCallFrom; rewrite Hkind;
first [exact Hsender|exact Hrecipient|split; assumption]
end.
Lemma call_preserves_from_self (invariant : Host_t → Prop) :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
childCallFrom (@V.Executor.self Host_t F E) msg →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.call. solve_call_origin_wrapper Hprimitive Hcall.
Qed.
Lemma callcode_preserves_from_self (invariant : Host_t → Prop) :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
childCallFrom (@V.Executor.self Host_t F E) msg →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.callcode Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.callcode. solve_call_origin_wrapper Hprimitive Hcall.
Qed.
Lemma delegatecall_preserves_from_self (invariant : Host_t → Prop) :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
childCallFrom (@V.Executor.self Host_t F E) msg →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.delegatecall Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.delegatecall. solve_call_origin_wrapper Hprimitive Hcall.
Qed.
Lemma staticcall_preserves_from_self (invariant : Host_t → Prop) :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
childCallFrom (@V.Executor.self Host_t F E) msg →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.staticcall Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.staticcall. solve_call_origin_wrapper Hprimitive Hcall.
Qed.
lazymatch goal with
| |- preservesBoundedHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
apply bounded_frame_bind_prefix;
[solve_host_invariant | solve_call_pop_gas | ];
let value := fresh "value" in intro value;
repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
cbn beta iota zeta; solve_call_origin_wrapper Hprimitive Hcall
| _ ⇒ apply call_opcode_preserves_at_context; [exact Hprimitive|];
intros msg Hgas Hvalue [Hkind [Hsender Hrecipient]];
apply Hcall; [exact Hgas|]; unfold childCallFrom; rewrite Hkind;
first [exact Hsender|exact Hrecipient|split; assumption]
end.
Lemma call_preserves_from_self (invariant : Host_t → Prop) :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
childCallFrom (@V.Executor.self Host_t F E) msg →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.call Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.call. solve_call_origin_wrapper Hprimitive Hcall.
Qed.
Lemma callcode_preserves_from_self (invariant : Host_t → Prop) :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
childCallFrom (@V.Executor.self Host_t F E) msg →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.callcode Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.callcode. solve_call_origin_wrapper Hprimitive Hcall.
Qed.
Lemma delegatecall_preserves_from_self (invariant : Host_t → Prop) :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
childCallFrom (@V.Executor.self Host_t F E) msg →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.delegatecall Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.delegatecall. solve_call_origin_wrapper Hprimitive Hcall.
Qed.
Lemma staticcall_preserves_from_self (invariant : Host_t → Prop) :
PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
childCallFrom (@V.Executor.self Host_t F E) msg →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesBoundedHost invariant (@V.Executor.staticcall Host_t F E).
Proof.
intros Hprimitive Hcall. unfold V.Executor.staticcall. solve_call_origin_wrapper Hprimitive Hcall.
Qed.
Empty code is resolved as a direct call, not a delegated call. This
connects the concrete code read to the child code-address argument.
Lemma access_delegation_empty query state :
(@V.Host Host_t F).(Evmc.HOST.copy_code) query 0%Z
(@Delegation.eoa_delegated_code_length) (MS.t.host state) =
(Byte_string.Bytes.empty, MS.t.host state) →
@V.Executor.access_delegation Host_t F E query state =
(inl (V.Executor.Direct
{| V.Executor.delegation.Direct.code := Byte_string.Bytes.empty |}), state).
Proof.
intro Hread.
unfold V.Executor.access_delegation, V.M.HostAPI.copy_code, V.M.HostAPI.Base,
V.M.HostAPI.lift, V.M.op_letdollar.
cbn -[Delegation.get_delegated_address Delegation.eoa_delegated_code_length].
rewrite Hread. destruct state. reflexivity.
Qed.
(@V.Host Host_t F).(Evmc.HOST.copy_code) query 0%Z
(@Delegation.eoa_delegated_code_length) (MS.t.host state) =
(Byte_string.Bytes.empty, MS.t.host state) →
@V.Executor.access_delegation Host_t F E query state =
(inl (V.Executor.Direct
{| V.Executor.delegation.Direct.code := Byte_string.Bytes.empty |}), state).
Proof.
intro Hread.
unfold V.Executor.access_delegation, V.M.HostAPI.copy_code, V.M.HostAPI.Base,
V.M.HostAPI.lift, V.M.op_letdollar.
cbn -[Delegation.get_delegated_address Delegation.eoa_delegated_code_length].
rewrite Hread. destruct state. reflexivity.
Qed.
Direct resolution retains the requested code address. This extra fact
rules out executing some other account's code in an empty recipient.
Lemma generic_call_direct_preserves (invariant : Host_t → Prop)
kind forwarded value sender recipient codeAddr code static
inputStart inputSize outputStart outputSize :
(Z.of_N forwarded < 2^63)%Z →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = kind ∧ Evmc.Message.t.sender msg = sender ∧
Evmc.Message.t.recipient msg = recipient ∧
Evmc.Message.t.code_address msg = codeAddr ∧ Evmc.Message.t.delegated msg = false) →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesHost invariant (@V.Executor.generic_call_impl Host_t F E
kind (@Numeric.U256.of_uint_exn forwarded) value sender recipient codeAddr
(V.Executor.Direct {| V.Executor.delegation.Direct.code := code |}) static
inputStart inputSize outputStart outputSize).
Proof.
intros Hgas Hcall.
unfold V.Executor.generic_call_impl, V.Executor.merge_child_gas_and_refund.
solve_host_invariant.
Qed.
Ltac solve_host_invariant_extra ::=
first [apply generic_call_direct_preserves; [assumption|assumption]
| lazymatch goal with
| Hcall : ∀ msg, _ → _ → _ → preservesCallback _ _
|- preservesHost _ (@V.Executor.generic_call_impl _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) ⇒
apply generic_call_preserves_at_context; [assumption|exact Hcall]
| Hcall : ∀ msg, _ → _ → preservesCallback _ _
|- preservesHost _ (@V.Executor.generic_call_impl _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) ⇒
apply generic_call_preserves_at_value; [assumption|exact Hcall]
| _ ⇒ solve_call_frame_extra
end].
kind forwarded value sender recipient codeAddr code static
inputStart inputSize outputStart outputSize :
(Z.of_N forwarded < 2^63)%Z →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = kind ∧ Evmc.Message.t.sender msg = sender ∧
Evmc.Message.t.recipient msg = recipient ∧
Evmc.Message.t.code_address msg = codeAddr ∧ Evmc.Message.t.delegated msg = false) →
preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
preservesHost invariant (@V.Executor.generic_call_impl Host_t F E
kind (@Numeric.U256.of_uint_exn forwarded) value sender recipient codeAddr
(V.Executor.Direct {| V.Executor.delegation.Direct.code := code |}) static
inputStart inputSize outputStart outputSize).
Proof.
intros Hgas Hcall.
unfold V.Executor.generic_call_impl, V.Executor.merge_child_gas_and_refund.
solve_host_invariant.
Qed.
Ltac solve_host_invariant_extra ::=
first [apply generic_call_direct_preserves; [assumption|assumption]
| lazymatch goal with
| Hcall : ∀ msg, _ → _ → _ → preservesCallback _ _
|- preservesHost _ (@V.Executor.generic_call_impl _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) ⇒
apply generic_call_preserves_at_context; [assumption|exact Hcall]
| Hcall : ∀ msg, _ → _ → preservesCallback _ _
|- preservesHost _ (@V.Executor.generic_call_impl _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) ⇒
apply generic_call_preserves_at_value; [assumption|exact Hcall]
| _ ⇒ solve_call_frame_extra
end].
Empty code read before the child call fixes its code address and rules
out delegation. The prefix may require "not yet created" while the child
is permitted to create the tracked address, hence two Host invariants.
Lemma call_opcode_empty_code (pre post : Host_t → Prop)
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static :
(∀ h, pre h → post h) →
PrimitiveHostFrameAt (@V.Host Host_t F) pre (@V.Executor.self Host_t F E) →
PrimitiveHostFrameAt (@V.Host Host_t F) post (@V.Executor.self Host_t F E) →
(∀ h, pre h → (@V.Host Host_t F).(Evmc.HOST.copy_code) codeAddr 0%Z
(@Delegation.eoa_delegated_code_length) h = (Byte_string.Bytes.empty, h)) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = kind ∧ Evmc.Message.t.sender msg = sender ∧
Evmc.Message.t.recipient msg = recipient ∧
Evmc.Message.t.code_address msg = codeAddr ∧ Evmc.Message.t.delegated msg = false) →
preservesCallback post ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
post (MS.t.host (snd (@V.Executor.call_opcode_impl Host_t F E
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static state))).
Proof.
intros Hweaken Hpre Hpost Hempty Hcall. destruct Hpre. destruct Hpost.
unfold V.Executor.call_opcode_impl.
apply bounded_frame_bind_between with (pre := pre); [exact Hweaken| | |].
- unfold V.Executor.extend_memory_to. solve_host_invariant.
- apply extend_memory_to_nonincreasing.
- intro inputGas. apply bounded_frame_bind_between with (pre := pre); [exact Hweaken| | |].
+ unfold V.Executor.extend_memory_to. solve_host_invariant.
+ apply extend_memory_to_nonincreasing.
+ intro outputGas. apply bounded_frame_bind_map_between with (pre := pre); [exact Hweaken| | |].
× expose_call_primitives. solve_host_invariant.
× apply access_account_nonincreasing.
× intros accessStatus beforeRead Hbound Hprefix.
match goal with |- post (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ ?action ?next beforeRead))) ⇒
change (post (MS.t.host (snd
(match action beforeRead with
| (inl d, middle) ⇒ next d middle
| (inr err, middle) ⇒ (inr err, middle)
end))))
end.
rewrite (access_delegation_empty codeAddr beforeRead (Hempty _ Hprefix)).
cbn beta iota zeta delta [snd].
match goal with |- post (MS.t.host (snd (?action beforeRead))) ⇒
assert (Hrest : preservesBoundedHost post action)
end.
{ apply bounded_frame_bind_prefix.
{ expose_call_primitives. solve_host_invariant. }
{ apply account_exists_nonincreasing. }
intro targetAlive. intros state Hgas Hinitial.
match goal with |- post (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ _ ?next state))) ⇒
change (post (MS.t.host (snd (next (MS.t.gas state) state))))
end.
cbn beta.
set (transfer := andb (Evmc.Message.CallKind.op_ltgt kind
Evmc.Message.CallKind.DelegateCall)
(Numeric.U256.op_ltgt value (@Numeric.U256.zero))) in ×.
set (memoryGas := @Gas.op_plus inputGas outputGas) in ×.
match goal with |- context [@Gas.call_gas_value transfer requested
(MS.t.gas state) memoryGas ?cost] ⇒ set (extra := cost) in × end.
assert (Hextra : (100 + (if transfer then @Gas.call_value else 0) ≤ extra)%N).
{ unfold extra.
pose proof (@account_access_min accessStatus) as Haccess.
destruct transfer, targetAlive;
cbn [andb negb]; unfold Gas.op_plus, Gas.zero, UintN.op_plus, UintN.zero;
lia. }
match goal with |- post (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ (@V.spend _ _ ?amount) ?next state))) ⇒
set (nextCall := next); set (spentGas := amount)
end.
pose proof (@preserves_spend Host_t F post spentGas state Hinitial) as Hframe.
change (post (MS.t.host (snd
(match @V.spend Host_t F spentGas state with
| (inl value, middle) ⇒ nextCall value middle
| (inr error, middle) ⇒ (inr error, middle)
end)))).
destruct (@V.spend Host_t F spentGas state) as [[[]|error] charged] eqn:Hspent.
2: exact Hframe.
pose proof (spend_success_exact _ _ _ Hspent) as [Hafford Hcharged].
pose proof (@call_quote_total_decreases transfer requested
(MS.t.gas state) memoryGas extra Hextra Hafford) as Hchild.
cbn zeta in Hchild.
assert (Hforwarded : (Z.of_N
(@Gas.call_gas_value transfer requested (MS.t.gas state) memoryGas extra)
.(Gas.call_gas.callee_available_gas) < 2^63)%Z) by lia.
assert (Hnext : preservesHost post (nextCall tt)).
{ unfold nextCall, V.Executor.check_write_permissions.
expose_call_primitives. solve_host_invariant. }
apply Hnext. exact Hframe. }
apply Hrest; [exact Hbound|apply Hweaken; exact Hprefix].
Qed.
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static :
(∀ h, pre h → post h) →
PrimitiveHostFrameAt (@V.Host Host_t F) pre (@V.Executor.self Host_t F E) →
PrimitiveHostFrameAt (@V.Host Host_t F) post (@V.Executor.self Host_t F E) →
(∀ h, pre h → (@V.Host Host_t F).(Evmc.HOST.copy_code) codeAddr 0%Z
(@Delegation.eoa_delegated_code_length) h = (Byte_string.Bytes.empty, h)) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = kind ∧ Evmc.Message.t.sender msg = sender ∧
Evmc.Message.t.recipient msg = recipient ∧
Evmc.Message.t.code_address msg = codeAddr ∧ Evmc.Message.t.delegated msg = false) →
preservesCallback post ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
post (MS.t.host (snd (@V.Executor.call_opcode_impl Host_t F E
kind requested value sender recipient codeAddr
inputStart inputSize outputStart outputSize static state))).
Proof.
intros Hweaken Hpre Hpost Hempty Hcall. destruct Hpre. destruct Hpost.
unfold V.Executor.call_opcode_impl.
apply bounded_frame_bind_between with (pre := pre); [exact Hweaken| | |].
- unfold V.Executor.extend_memory_to. solve_host_invariant.
- apply extend_memory_to_nonincreasing.
- intro inputGas. apply bounded_frame_bind_between with (pre := pre); [exact Hweaken| | |].
+ unfold V.Executor.extend_memory_to. solve_host_invariant.
+ apply extend_memory_to_nonincreasing.
+ intro outputGas. apply bounded_frame_bind_map_between with (pre := pre); [exact Hweaken| | |].
× expose_call_primitives. solve_host_invariant.
× apply access_account_nonincreasing.
× intros accessStatus beforeRead Hbound Hprefix.
match goal with |- post (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ ?action ?next beforeRead))) ⇒
change (post (MS.t.host (snd
(match action beforeRead with
| (inl d, middle) ⇒ next d middle
| (inr err, middle) ⇒ (inr err, middle)
end))))
end.
rewrite (access_delegation_empty codeAddr beforeRead (Hempty _ Hprefix)).
cbn beta iota zeta delta [snd].
match goal with |- post (MS.t.host (snd (?action beforeRead))) ⇒
assert (Hrest : preservesBoundedHost post action)
end.
{ apply bounded_frame_bind_prefix.
{ expose_call_primitives. solve_host_invariant. }
{ apply account_exists_nonincreasing. }
intro targetAlive. intros state Hgas Hinitial.
match goal with |- post (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ _ ?next state))) ⇒
change (post (MS.t.host (snd (next (MS.t.gas state) state))))
end.
cbn beta.
set (transfer := andb (Evmc.Message.CallKind.op_ltgt kind
Evmc.Message.CallKind.DelegateCall)
(Numeric.U256.op_ltgt value (@Numeric.U256.zero))) in ×.
set (memoryGas := @Gas.op_plus inputGas outputGas) in ×.
match goal with |- context [@Gas.call_gas_value transfer requested
(MS.t.gas state) memoryGas ?cost] ⇒ set (extra := cost) in × end.
assert (Hextra : (100 + (if transfer then @Gas.call_value else 0) ≤ extra)%N).
{ unfold extra.
pose proof (@account_access_min accessStatus) as Haccess.
destruct transfer, targetAlive;
cbn [andb negb]; unfold Gas.op_plus, Gas.zero, UintN.op_plus, UintN.zero;
lia. }
match goal with |- post (MS.t.host (snd
(@V.M.op_letdollar _ _ _ _ (@V.spend _ _ ?amount) ?next state))) ⇒
set (nextCall := next); set (spentGas := amount)
end.
pose proof (@preserves_spend Host_t F post spentGas state Hinitial) as Hframe.
change (post (MS.t.host (snd
(match @V.spend Host_t F spentGas state with
| (inl value, middle) ⇒ nextCall value middle
| (inr error, middle) ⇒ (inr error, middle)
end)))).
destruct (@V.spend Host_t F spentGas state) as [[[]|error] charged] eqn:Hspent.
2: exact Hframe.
pose proof (spend_success_exact _ _ _ Hspent) as [Hafford Hcharged].
pose proof (@call_quote_total_decreases transfer requested
(MS.t.gas state) memoryGas extra Hextra Hafford) as Hchild.
cbn zeta in Hchild.
assert (Hforwarded : (Z.of_N
(@Gas.call_gas_value transfer requested (MS.t.gas state) memoryGas extra)
.(Gas.call_gas.callee_available_gas) < 2^63)%Z) by lia.
assert (Hnext : preservesHost post (nextCall tt)).
{ unfold nextCall, V.Executor.check_write_permissions.
expose_call_primitives. solve_host_invariant. }
apply Hnext. exact Hframe. }
apply Hrest; [exact Hbound|apply Hweaken; exact Hprefix].
Qed.
For CALL/STATICCALL the recipient is also the requested code address.
An empty protected recipient therefore cannot be given unrelated code,
even though other recipients may legitimately use delegation.
Lemma call_opcode_protects_empty_recipient (pre post : Host_t → Prop)
query requested value sender recipient inputStart inputSize outputStart outputSize static :
(∀ h, pre h → post h) →
PrimitiveHostFrameAt (@V.Host Host_t F) pre (@V.Executor.self Host_t F E) →
PrimitiveHostFrameAt (@V.Host Host_t F) post (@V.Executor.self Host_t F E) →
(∀ h, pre h → (@V.Host Host_t F).(Evmc.HOST.copy_code) query 0%Z
(@Delegation.eoa_delegated_code_length) h = (Byte_string.Bytes.empty, h)) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call ∧
Evmc.Message.t.sender msg = sender ∧ Evmc.Message.t.recipient msg = recipient) →
(Evmc.Message.t.recipient msg = query →
Evmc.Message.t.code_address msg = query ∧ Evmc.Message.t.delegated msg = false) →
preservesCallback post ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
post (MS.t.host (snd (@V.Executor.call_opcode_impl Host_t F E
Evmc.Message.CallKind.Call requested value sender recipient recipient
inputStart inputSize outputStart outputSize static state))).
Proof.
intros Hweaken Hpre Hpost Hempty Hchild.
destruct (String.string_dec recipient query) as [Heq|Hneq].
- subst recipient. apply call_opcode_empty_code; try assumption.
intros msg Hgas Hvalue [Hkind [Hsender [Hrecipient [Hcode Hdelegated]]]].
apply Hchild; try assumption.
+ repeat split; assumption.
+ intros _. split; assumption.
- intros state Hgas Hinitial.
match goal with |- post (MS.t.host (snd (?action state))) ⇒
assert (Hframe : preservesBoundedHost post action)
end.
{ apply call_opcode_preserves_at_context; [exact Hpost|].
intros msg Hbound Hvalue [Hkind [Hsender Hrecipient]].
apply Hchild; try assumption.
- repeat split; assumption.
- intro Heq. exfalso. apply Hneq. congruence. }
apply Hframe; [exact Hgas|apply Hweaken; exact Hinitial].
Qed.
End Environment.
End Opcodes.
Print Assumptions call_preserves.
Print Assumptions delegatecall_preserves.
Print Assumptions call_preserves_from_self.
Print Assumptions delegatecall_preserves_from_self.
Print Assumptions access_delegation_empty.
Print Assumptions generic_call_direct_preserves.
Print Assumptions call_opcode_empty_code.
Print Assumptions call_opcode_protects_empty_recipient.
query requested value sender recipient inputStart inputSize outputStart outputSize static :
(∀ h, pre h → post h) →
PrimitiveHostFrameAt (@V.Host Host_t F) pre (@V.Executor.self Host_t F E) →
PrimitiveHostFrameAt (@V.Host Host_t F) post (@V.Executor.self Host_t F E) →
(∀ h, pre h → (@V.Host Host_t F).(Evmc.HOST.copy_code) query 0%Z
(@Delegation.eoa_delegated_code_length) h = (Byte_string.Bytes.empty, h)) →
(∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
Evmc.Message.t.value msg = value →
(Evmc.Message.t.kind msg = Evmc.Message.CallKind.Call ∧
Evmc.Message.t.sender msg = sender ∧ Evmc.Message.t.recipient msg = recipient) →
(Evmc.Message.t.recipient msg = query →
Evmc.Message.t.code_address msg = query ∧ Evmc.Message.t.delegated msg = false) →
preservesCallback post ((@V.Host Host_t F).(Evmc.HOST.call) msg)) →
∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z → pre (MS.t.host state) →
post (MS.t.host (snd (@V.Executor.call_opcode_impl Host_t F E
Evmc.Message.CallKind.Call requested value sender recipient recipient
inputStart inputSize outputStart outputSize static state))).
Proof.
intros Hweaken Hpre Hpost Hempty Hchild.
destruct (String.string_dec recipient query) as [Heq|Hneq].
- subst recipient. apply call_opcode_empty_code; try assumption.
intros msg Hgas Hvalue [Hkind [Hsender [Hrecipient [Hcode Hdelegated]]]].
apply Hchild; try assumption.
+ repeat split; assumption.
+ intros _. split; assumption.
- intros state Hgas Hinitial.
match goal with |- post (MS.t.host (snd (?action state))) ⇒
assert (Hframe : preservesBoundedHost post action)
end.
{ apply call_opcode_preserves_at_context; [exact Hpost|].
intros msg Hbound Hvalue [Hkind [Hsender Hrecipient]].
apply Hchild; try assumption.
- repeat split; assumption.
- intro Heq. exfalso. apply Hneq. congruence. }
apply Hframe; [exact Hgas|apply Hweaken; exact Hinitial].
Qed.
End Environment.
End Opcodes.
Print Assumptions call_preserves.
Print Assumptions delegatecall_preserves.
Print Assumptions call_preserves_from_self.
Print Assumptions delegatecall_preserves_from_self.
Print Assumptions access_delegation_empty.
Print Assumptions generic_call_direct_preserves.
Print Assumptions call_opcode_empty_code.
Print Assumptions call_opcode_protects_empty_recipient.