Library SetcodeConcrete.vmcreationguard

CREATE is disabled in a frame entered through delegation

The flag is stored in the immutable execution environment. Even if SETCODEFROM succeeds during this frame, neither CREATE instruction can increment its account's nonce. A later frame may execute adopted code.
From Stdlib Require Import ZArith NArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.vminvariant QedkConcrete.vmhostframe
  QedkConcrete.vmcallframe QedkConcrete.vmcreateframe QedkConcrete.vmboundedframe.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress.
Set Default Goal Selector "!".
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.
Section VM.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
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 (Hdelegated : (V.Executor.C (FArgs := E)).(V.Executor_C_signature.execution_environment)
  .(V.ExecutionEnvironment.t.context_was_delegated) = true).

Lemma generic_create_entry_delegated (P : Host_t → Prop) kind salt value start size :
  PrimitiveHostFrameAt V.Host P V.Executor.self →
  preservesHost P (V.Executor.generic_create_impl kind salt value start size).
Proof.
  intro Hprimitive. destruct Hprimitive.
  unfold V.Executor.generic_create_impl. rewrite Hrevision, Hdelegated.
  apply preserves_bind; [solve_host_invariant|]. intros [].
  apply preserves_bind; [unfold V.Executor.check_write_permissions; solve_host_invariant|]. intros [].
  apply preserves_bind; [expose_primitive_calls; solve_host_invariant|]. intro balance.
  unfold V.M.when_, Monad.Make_Monad.when_. cbn.
  intros state HP. exact HP.
Qed.

Lemma create_entry_delegated (P : Host_t → Prop) :
  PrimitiveHostFrameAt V.Host P V.Executor.self →
  preservesHost P V.Executor.create.
Proof.
  intro Hprimitive. unfold V.Executor.create.
  apply preserves_bind; [solve_host_invariant|]. intros [[value start] size].
  apply preserves_bind; [unfold V.Executor.extend_memory_to; solve_host_invariant|]. intro gas.
  apply preserves_bind; [solve_host_invariant|]. intros [].
  apply preserves_bind; [apply generic_create_entry_delegated; exact Hprimitive|].
  intros []. solve_host_invariant.
Qed.

Lemma create2_entry_delegated (P : Host_t → Prop) :
  PrimitiveHostFrameAt V.Host P V.Executor.self →
  preservesHost P V.Executor.create2.
Proof.
  intro Hprimitive. unfold V.Executor.create2.
  apply preserves_bind; [solve_host_invariant|]. intros [[value start] size].
  apply preserves_bind; [solve_host_invariant|]. intro salt.
  apply preserves_bind; [unfold V.Executor.extend_memory_to; solve_host_invariant|]. intro gas.
  apply preserves_bind; [solve_host_invariant|]. intros [].
  apply preserves_bind; [apply generic_create_entry_delegated; exact Hprimitive|].
  intros []. solve_host_invariant.
Qed.
End VM.

Section Noncreating.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Ltac noncreating_pop_gas :=
  lazymatch goal with
  | |- gas_nonincreasing (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒
    apply map_nonincreasing; noncreating_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_noncreating_call Hprimitive Hcall :=
  lazymatch goal with
  | |- preservesBoundedHost _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    apply bounded_frame_bind_prefix;
    [solve_host_invariant | noncreating_pop_gas | ];
    let value := fresh "value" in intro value;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_noncreating_call 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]|
      rewrite Hkind; discriminate|rewrite Hkind; discriminate]
  end.

Lemma call_preserves_without_creation (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 →
    Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create →
    Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create2 →
    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_noncreating_call Hprimitive Hcall.
Qed.

Lemma callcode_preserves_without_creation (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 →
    Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create →
    Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create2 →
    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_noncreating_call Hprimitive Hcall.
Qed.

Lemma delegatecall_preserves_without_creation (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 →
    Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create →
    Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create2 →
    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_noncreating_call Hprimitive Hcall.
Qed.

Lemma staticcall_preserves_without_creation (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 →
    Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create →
    Evmc.Message.t.kind msg ≠ Evmc.Message.CallKind.Create2 →
    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_noncreating_call Hprimitive Hcall.
Qed.

End Noncreating.