Library SetcodeConcrete.vmframes

Host invariants for the SETCODE instruction set

Machine-only instructions and the original nonrecursive Host operations reuse the common proofs. Code adoption has its own callback obligation. The six recursive CALL/CREATE instructions are left as induction steps, not assumed properties of complete VM execution.
From Stdlib Require Import ZArith.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import QedkConcrete.vminvariant QedkConcrete.vmopcodeframe
  QedkConcrete.vmhostframe SetcodeConcrete.vmprogress.
Require Import MonadExecutionSpecs.Termination.VmDispatchProgress.
Set Default Goal Selector "!".

Section VM.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.

Lemma setcode_opcode_preserves_host cold warm access write reserve invariant :
  V.Params.(Vm.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  (∀ current source static gas,
    preservesCallback invariant
      (V.Host.(Evmc.HOST.setcodefrom) current 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.

Lemma setcode_clz_preserves_host cold warm access write reserve invariant :
  V.Params.(Vm.Make_Params_signature.revision) =
    Support.Variants.Revision.Setcode (cold, warm, access, write, reserve) →
  preservesHost invariant (dispatch Opcode.Clz).
Proof.
  intro Hrevision. unfold dispatch, V.Executor.execute_opcode, V.Executor.clz.
  rewrite Hrevision. solve_host_invariant.
Qed.

Theorem setcode_run_preserves_from_calls 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 →
  (∀ current source static gas,
    preservesCallback invariant
      (V.Host.(Evmc.HOST.setcodefrom) current 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_host; 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.