Library SetcodeConcrete.vmsupplyentry

Supply safety through the actual SETCODE VM entry point

Entry-time delegation lookup is read-only. The loaded code and message establish the word/byte invariants, which the loop preserves through success, revert and exceptional exits. Child calls remain the recursive assembly's induction obligation.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Host.
Require Import QedkConcrete.recursiveassembly QedkConcrete.hostframe
  QedkConcrete.hostcontextwords QedkConcrete.vmenvironmentwords
  QedkConcrete.vmsupplywords QedkConcrete.vmchecked QedkConcrete.vmcallchecked
  QedkConcrete.vmmachinewords QedkConcrete.vmsizes QedkConcrete.hashwords
  QedkConcrete.vmmemorywords QedkConcrete.vmrun.
Require Import SetcodeConcrete.vmsupply SetcodeConcrete.vmprogress SetcodeConcrete.vmerrors.
Require Import MonadExecutionSpecs.Termination.VmUnsignedGas
  MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
#[local] Hint Extern 100 (Basics.Unreachable _) ⇒
  (apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat) : typeclass_instances.

Section Entry.
Context {Host_t : Set} {F : @V.FArgs Host_t}.
Context (invariant : Host_t → Prop).
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 (HerrorCallback : @setcodeNoSuccessError 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).
Context (Hloop : ∀ (env : @V.Executor.FArgs Host_t F) state,
  environmentWords ((@V.Executor.C Host_t F env).(V.Executor_C_signature.execution_environment)) →
  Z.of_N (MS.t.gas state) < 2^63 → checkedMachine invariant state →
  checkedMachine invariant (snd (@generated_run Host_t F env state)) ∨ badMemoryInitComparison).

Lemma execute_checked_at_context msg code host :
  (∀ (env : @V.Executor.FArgs Host_t F) state,
    @V.Executor.self Host_t F env = Evmc.Message.t.recipient msg →
    environmentWords ((@V.Executor.C Host_t F env).(V.Executor_C_signature.execution_environment)) →
    Z.of_N (MS.t.gas state) < 2^63 → checkedMachine invariant state →
    checkedMachine invariant (snd (@generated_run Host_t F env state)) ∨ badMemoryInitComparison) →
  contextWords (fst (V.Host.(Evmc.HOST.get_tx_context) host)) →
  vmMessageBounds msg → OCamlString.length code < 2^256 → invariant host →
  let result := @V.execute Host_t F msg code host in
  (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
  badMemoryInitComparison.
Proof.
  intros Hactual Hctx [Hgas [Hvalue [Hdata Hcapacity]]] Hcode 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:HreadCtx.
  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.
  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: (assert (HreadCopy : snd (V.Host.(Evmc.HOST.copy_code)
       (Evmc.Message.t.recipient msg) 0 Delegation.eoa_delegated_code_length host) = host)
       by (apply Hcopy);
    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;
    cbn -[V.Executor.run V.ExecutionEnvironment.make MS.initial V.Host]).
  all: match goal with |- context [@V.Executor.run _ _ ?env ?state] ⇒
    assert (Hrun : checkedMachine invariant (snd (@generated_run Host_t F env state)) ∨ badMemoryInitComparison) by
      (unshelve (apply Hactual;
       [reflexivity|apply environment_frame_words; try assumption; lia
       |apply initial_gas_fits_signed_int64; exact Hgas
       |split; [apply initial_machine_words; exact Hinitial|apply initial_machine_sizes; apply Hcapacity]]);
       apply Opaque.legacy_witness; Opaque.opaque_inhabit 64%nat);
    pose proof (@setcode_run_no_success_error Host_t F env cold warm access write reserve state
      Hrevision Hdebug HerrorCallback Hprogress
      ltac:(apply initial_gas_fits_signed_int64; exact Hgas)) as Herror;
    unfold generated_run in Hrun, Herror;
    destruct (@V.Executor.run Host_t F env state) as [outcome after]
  end.
  all: destruct Hrun as [[Hwords Hsizes]|Hbad]; [|right; exact Hbad].
  all: left; destruct outcome as [[]|error].
  all: try (destruct error).
  all: try (exfalso; apply Herror; reflexivity).
  all: split; [exact (proj1 (proj2 (proj2 Hwords)))|].
  all: first [exact (proj2 Hsizes)|cbn; lia].
Qed.

Lemma execute_checked_from_loop msg code host :
  contextWords (fst (V.Host.(Evmc.HOST.get_tx_context) host)) →
  vmMessageBounds msg → OCamlString.length code < 2^256 → invariant host →
  let result := @V.execute Host_t F msg code host in
  (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
  badMemoryInitComparison.
Proof.
  intros Hctx Hmsg Hcode Hinitial. eapply execute_checked_at_context; try eassumption.
  intros env state Hself. apply Hloop.
Qed.
End Entry.

Section Concrete.
Context {HF : Host.Instantiate.FArgs}.
Local Abbreviation HostF := (@Evmc.Instantiate.HostF S.t (@recursiveArgs HF)).
Context (vm : Evmc.Vm.SIG (_fargs := Evmc.Vm.Build_FArgs
  (@Evmc.Instantiate.T S.t (@recursiveArgs HF)))).
Context (params : Vm.Make_Params_signature) (limit : Z).
Local Abbreviation F := (V.Build_FArgs params (HostF vm)).
Local Abbreviation invariant := (hostSupplyContext HF.(Host.Instantiate.ChainParams) limit).

Theorem execute_supply_context cold warm access write reserve msg code host :
  params.(Vm.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  params.(Vm.Make_Params_signature.debug_tstore) = false →
  keccak256OutputSize → @setcodeGasProgress S.t F →
  vmMessageBounds msg → OCamlString.length code < 2^256 →
  (∀ child, vmMessageBounds child → ∀ before, invariant before →
    let result := (HostF vm).(Evmc.HOST.call) child before in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) → invariant host →
  let result := @V.execute S.t F msg code host in
  (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
  badMemoryInitComparison.
Proof.
  intros Hrevision Hdebug Hhash Hprogress Hmsg Hcode Hchild Hinitial.
  eapply execute_checked_from_loop; try eassumption.
  - apply assembled_setcode_no_success_error.
  - intros; reflexivity.
  - intros; reflexivity.
  - intros; apply copy_code_read_only.
  - intros env state Henv Hgas Hstate.
    exact (@run_supply_context HF vm params limit cold warm access write reserve
      Hrevision Hdebug Hhash env state Hprogress Henv Hchild Hgas Hstate).
  - apply get_tx_context_words. exact (proj2 Hinitial).
Qed.
End Concrete.
Print Assumptions execute_supply_context.