Library QedkConcrete.vmprotectedframe

Empty-account-aware frame for the generated VM

CALL and STATICCALL resolve an empty recipient to its own empty code. CALLCODE and DELEGATECALL retain the current execution account. CREATE is distinguished so its Host collision check can protect native state. These facts let storage isolation use the actual calls the VM can issue, rather than assume preservation for arbitrary messages.
From Stdlib Require Import ZArith String.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vminvariant QedkConcrete.vmhostframe
  QedkConcrete.vmboundedframe QedkConcrete.vmcreateframe QedkConcrete.vmcallframe
  QedkConcrete.vmentryframe.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress
  MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress
  MonadExecutionSpecs.Termination.VmCallHandlerProgress.
Set Default Goal Selector "!".
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}.
Context (invariant : Host_t → Prop) (query : Chain.Ethereum.Address.t).

Ltac protected_pop_gas :=
  lazymatch goal with
  | |- gas_nonincreasing (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒
    apply map_nonincreasing; protected_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_protected_call Hprimitive Hread Hcall :=
  lazymatch goal with
  | |- preservesBoundedHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    apply bounded_frame_bind_prefix;
    [solve_host_invariant | protected_pop_gas | ];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_protected_call Hprimitive Hread Hcall
  | _ ⇒ unfold preservesBoundedHost; intros before Hbound Hinitial;
    eapply call_opcode_protects_empty_recipient with (pre := invariant) (post := invariant) (query := query);
    [intros; assumption|exact Hprimitive|exact Hprimitive|apply Hread| |exact Hbound|exact Hinitial];
    intros nested Hgas Hvalue [Hkind [Hsender Hrecipient]] Hcode;
    apply Hcall; [exact Hgas|unfold childCallFrom; rewrite Hkind; exact Hsender|];
    right; right; intro;
    match goal with
    | Hcode : ?premise → _ ∧ _, Heq : ?premise |- _ ⇒
      let Hpair := fresh "Hpair" in pose proof (Hcode Heq) as Hpair;
      exact (proj1 Hpair)
    end
  end.

Ltac solve_retained_context Hprimitive Hcall Hdifferent :=
  lazymatch goal with
  | |- preservesBoundedHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    apply bounded_frame_bind_prefix;
    [solve_host_invariant | protected_pop_gas | ];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_retained_context Hprimitive Hcall Hdifferent
  | _ ⇒ apply call_opcode_preserves_at_context; [exact Hprimitive|];
    intros nested Hgas Hvalue [Hkind [Hsender Hrecipient]];
    apply Hcall;
    [exact Hgas|unfold childCallFrom; rewrite Hkind; first [exact Hrecipient|split; assumption]|];
    right; right; intro Heq; exfalso; apply Hdifferent; congruence
  end.

Lemma call_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ size h, invariant h → (@V.Host Host_t F).(Evmc.HOST.copy_code)
    query 0%Z size h = (Byte_string.Bytes.empty, h)) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.call Host_t F E).
Proof.
  intros Hprimitive Hread Hcall. unfold V.Executor.call.
  solve_protected_call Hprimitive Hread Hcall.
Qed.

Lemma staticcall_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ size h, invariant h → (@V.Host Host_t F).(Evmc.HOST.copy_code)
    query 0%Z size h = (Byte_string.Bytes.empty, h)) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.staticcall Host_t F E).
Proof.
  intros Hprimitive Hread Hcall. unfold V.Executor.staticcall.
  solve_protected_call Hprimitive Hread Hcall.
Qed.

Lemma callcode_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  @V.Executor.self Host_t F E ≠ query →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.callcode Host_t F E).
Proof.
  intros Hprimitive Hdifferent Hcall. unfold V.Executor.callcode.
  solve_retained_context Hprimitive Hcall Hdifferent.
Qed.

Lemma delegatecall_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  @V.Executor.self Host_t F E ≠ query →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.delegatecall Host_t F E).
Proof.
  intros Hprimitive Hdifferent Hcall. unfold V.Executor.delegatecall.
  solve_retained_context Hprimitive Hcall Hdifferent.
Qed.

Lemma create_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.create Host_t F E).
Proof.
  intros Hprimitive Hcall. unfold V.Executor.create.
  apply bounded_frame_bind_prefix; [solve_host_invariant|apply pop3_nonincreasing|].
  intros [[value inputStart] inputSize].
  apply bounded_frame_bind_prefix.
  - unfold V.Executor.extend_memory_to. solve_host_invariant.
  - apply extend_memory_to_nonincreasing.
  - intro memoryGas. apply bounded_frame_bind_prefix.
    + solve_host_invariant.
    + apply spend_nonincreasing.
    + intros []. apply bounded_frame_bind_suffix.
      × apply generic_create_preserves_at_context; [exact Hprimitive|].
        intros msg Hgas Hvalue [Hkind Hsender].
        apply Hcall; [exact Hgas|unfold childCallFrom; rewrite Hkind; exact Hsender|].
        left. exact Hkind.
      × intros []. solve_host_invariant.
Qed.
Lemma create2_preserves_protected_recipient :
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (@V.Executor.self Host_t F E) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F E) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  preservesBoundedHost invariant (@V.Executor.create2 Host_t F E).
Proof.
  intros Hprimitive Hcall. unfold V.Executor.create2.
  apply bounded_frame_bind_prefix; [solve_host_invariant|apply pop3_nonincreasing|].
  intros [[value inputStart] inputSize].
  apply bounded_frame_bind_prefix; [solve_host_invariant| |].
  - apply map_nonincreasing. apply pop_nonincreasing.
  - intro salt. apply bounded_frame_bind_prefix.
    + unfold V.Executor.extend_memory_to. solve_host_invariant.
    + apply extend_memory_to_nonincreasing.
    + intro memoryGas. apply bounded_frame_bind_prefix.
      × solve_host_invariant.
      × apply spend_nonincreasing.
      × intros []. apply bounded_frame_bind_suffix.
        { apply generic_create_preserves_at_context; [exact Hprimitive|].
        intros msg Hgas Hvalue [Hkind Hsender].
        apply Hcall; [exact Hgas|unfold childCallFrom; rewrite Hkind; exact Hsender|].
        right; left. exact Hkind. }
        intros []. solve_host_invariant.
Qed.
End Environment.
End Opcodes.

Section Entry.

  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.

Lemma execute_preserves_protected_recipient
    {Uint64 : Basics.Unreachable MonadVM.Int64.t}
    {Uresult : Basics.Unreachable Evmc.Result.t}
    {Urun : Basics.Unreachable (sum unit Evmc.Result.StatusCode.t × State)}
    (invariant : Host_t → Prop) (query : Chain.Ethereum.Address.t) msg code host :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  (@V.Params Host_t F).(Vm.Make_Params_signature.debug_tstore) = false →
  PrimitiveHostFrameAt (@V.Host Host_t F) invariant (Evmc.Message.t.recipient msg) →
  preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.get_tx_context)) →
  Evmc.Message.t.recipient msg ≠ query →
  (∀ size h, invariant h → (@V.Host Host_t F).(Evmc.HOST.copy_code)
    query 0%Z size h = (Byte_string.Bytes.empty, h)) →
  (∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (Evmc.Message.t.recipient msg) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)) →
  (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z → invariant host →
  invariant (snd (@V.execute Host_t F
    msg code host)).
Proof.
  intros Hrevision Hdebug Hprimitive Hread Hdifferent Hcopy Hcall Hgas Hinitial.
  apply execute_preserves_from_calls_at; try assumption.
  intros env opcode before Hself Hopcode Hbound Hbefore.
  assert (Henv : PrimitiveHostFrameAt (@V.Host Host_t F) invariant
    (@V.Executor.self Host_t F env)) by (rewrite Hself; exact Hprimitive).
  assert (Hnested : ∀ nested, (0 ≤ Evmc.Message.t.gas nested < 2^63)%Z →
    childCallFrom (@V.Executor.self Host_t F env) nested →
    (Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create ∨
      Evmc.Message.t.kind nested = Evmc.Message.CallKind.Create2 ∨
      (Evmc.Message.t.recipient nested = query → Evmc.Message.t.code_address nested = query)) →
    preservesCallback invariant ((@V.Host Host_t F).(Evmc.HOST.call) nested)).
  { rewrite Hself. exact Hcall. }
  assert (Hneq : @V.Executor.self Host_t F env ≠ query) by (rewrite Hself; exact Hdifferent).
  match goal with |- invariant (MS.t.host (snd (?action before))) ⇒
    assert (Hframe : preservesBoundedHost invariant action)
  end.
  { destruct opcode; cbn [recursiveCallOpcode] in Hopcode; try discriminate.
    all: cbn [dispatch V.Executor.execute_opcode].
    - apply (@create_preserves_protected_recipient Host_t F) with (E := env) (invariant := invariant) (query := query); assumption.
    - apply (@call_preserves_protected_recipient Host_t F) with (E := env) (invariant := invariant) (query := query); assumption.
    - apply (@callcode_preserves_protected_recipient Host_t F) with (E := env) (invariant := invariant) (query := query); assumption.
    - apply (@delegatecall_preserves_protected_recipient Host_t F) with (E := env) (invariant := invariant) (query := query); assumption.
    - apply (@create2_preserves_protected_recipient Host_t F) with (E := env) (invariant := invariant) (query := query); assumption.
    - apply (@staticcall_preserves_protected_recipient Host_t F) with (E := env) (invariant := invariant) (query := query); assumption. }
  exact (Hframe before Hbound Hbefore).
Qed.
End Entry.

Print Assumptions call_preserves_protected_recipient.
Print Assumptions create_preserves_protected_recipient.

Print Assumptions execute_preserves_protected_recipient.