Library QedkConcrete.vmcallmachinewords

Word preservation through CALL, CREATE and returned-data writes

The recursive call is used only with the forwarded gas and value that the instruction actually constructs; CREATE's endowment is read from the word-bounded stack. Returned bytes are written through the concrete memory routine. Its primitive-comparison failure is exposed as an alternative, not excluded by a new VM assumption.
From Stdlib Require Import ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.vmmachinewords QedkConcrete.vmmemorywords
  QedkConcrete.vmcreatewords QedkConcrete.vmcallframe QedkConcrete.vmbytewords
  QedkConcrete.vmhostframe QedkConcrete.vmhostwords QedkConcrete.vmmiscwords
  QedkConcrete.wordrange QedkConcrete.vmboundedframe QedkConcrete.vmcallchecked
  QedkConcrete.vmchecked.
Require Import MonadExecutionSpecs.Termination.VmOpcodeProgress
  MonadExecutionSpecs.Termination.VmCreateHandlerProgress
  MonadExecutionSpecs.Termination.VmCallHandlerProgress
  MonadExecutionSpecs.Termination.VmChildGasMerge
  MonadExecutionSpecs.Termination.VmUnsignedGas
  MonadExecutionSpecs.Termination.VmCallGas.
Set Default Goal Selector "!".
Local Open Scope Z_scope.

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).

Native copy arithmetic requires the machine's byte-size invariant. These word-only corollaries retain that input invariant instead of quantifying over arbitrarily large memory capacities. The recursive premise is the same bounded-message obligation used by the VM proof.
Definition sizedMemorySafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
  ∀ state, checkedMachine invariant state →
    (machineWords invariant (snd (action state)) ∧
      match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end) ∨
    badMemoryInitComparison.

Definition boundedSizedMemorySafe {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) : Prop :=
  ∀ state, (Z.of_N (MS.t.gas state) < 2^63)%Z →
    checkedMachine invariant state →
    (machineWords invariant (snd (action state)) ∧
      match fst (action state) with inl value ⇒ P value | inr _ ⇒ True end) ∨
    badMemoryInitComparison.

Lemma sized_memory_from_checked {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
  checkedSafe invariant P action → sizedMemorySafe P action.
Proof.
  intros Hsafe state Hinitial. specialize (Hsafe state Hinitial).
  unfold checkedMachine in Hsafe. tauto.
Qed.

Lemma sized_memory_from_memory {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
  memorySafe invariant P action → sizedMemorySafe P action.
Proof. intros Hsafe state [Hwords _]. apply Hsafe. exact Hwords. Qed.

Lemma bounded_sized_memory_from_checked {A : Set} (P : A → Prop) (action : @V.M.t Host_t F A) :
  boundedCheckedSafe invariant P action → boundedSizedMemorySafe P action.
Proof.
  intros Hsafe state Hgas Hinitial. specialize (Hsafe state Hgas Hinitial).
  unfold checkedMachine in Hsafe. tauto.
Qed.

Lemma generic_call_preserves_words_or_bad_comparison
    kind forwarded value sender recipient codeAddr delegation static
    inputStart inputSize outputStart outputSize :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  Z.of_N forwarded < 2^63 → wordRange value → 0 ≤ U.to_z inputSize < 2^32 →
  0 ≤ U.to_z outputSize < 2^32 →
  (∀ msg, @vmMessageBounds msg → ∀ host, invariant host →
    let result := (@V.Host Host_t F).(Evmc.HOST.call) msg host in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  sizedMemorySafe (fun _ ⇒ True) (@V.Executor.generic_call_impl Host_t F E
    kind (@U.of_uint_exn forwarded) value sender recipient codeAddr delegation static
    inputStart inputSize outputStart outputSize).
Proof.
  intros Hrevision Hgas Hvalue Hinput HoutBound Hchild.
  apply sized_memory_from_checked.
  eapply generic_call_checked_from_child; eassumption.
Qed.

Lemma call_opcode_preserves_words_or_bad_comparison
    kind requested value sender recipient codeAddr
    inputStart inputSize outputStart outputSize static :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  wordRange value → 0 ≤ U.to_z inputStart → 0 ≤ U.to_z inputSize →
  0 ≤ U.to_z outputStart → 0 ≤ U.to_z outputSize →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, @vmMessageBounds msg → ∀ host, invariant host →
    let result := (@V.Host Host_t F).(Evmc.HOST.call) msg host in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  boundedSizedMemorySafe (fun _ ⇒ True) (@V.Executor.call_opcode_impl Host_t F E
    kind requested value sender recipient codeAddr inputStart inputSize outputStart outputSize static).
Proof.
  intros Hrevision Hvalue Hstart Hsize HoutStart HoutSize Hprimitive Hchild.
  apply bounded_sized_memory_from_checked.
  eapply call_opcode_checked_from_child; eassumption.
Qed.

Lemma call_preserves_words_or_bad_comparison :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, @vmMessageBounds msg → ∀ host, invariant host →
    let result := (@V.Host Host_t F).(Evmc.HOST.call) msg host in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  boundedSizedMemorySafe (fun _ ⇒ True) (@V.Executor.call Host_t F E).
Proof.
  intros Hrevision Hprimitive Hchild.
  apply bounded_sized_memory_from_checked.
  eapply call_checked_from_child; eassumption.
Qed.

Lemma callcode_preserves_words_or_bad_comparison :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, @vmMessageBounds msg → ∀ host, invariant host →
    let result := (@V.Host Host_t F).(Evmc.HOST.call) msg host in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  boundedSizedMemorySafe (fun _ ⇒ True) (@V.Executor.callcode Host_t F E).
Proof.
  intros Hrevision Hprimitive Hchild.
  apply bounded_sized_memory_from_checked.
  eapply callcode_checked_from_child; eassumption.
Qed.

Lemma staticcall_preserves_words_or_bad_comparison :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, @vmMessageBounds msg → ∀ host, invariant host →
    let result := (@V.Host Host_t F).(Evmc.HOST.call) msg host in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  boundedSizedMemorySafe (fun _ ⇒ True) (@V.Executor.staticcall Host_t F E).
Proof.
  intros Hrevision Hprimitive Hchild.
  apply bounded_sized_memory_from_checked.
  eapply staticcall_checked_from_child; eassumption.
Qed.

Lemma delegatecall_preserves_words_or_bad_comparison :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  wordRange (V.ExecutionEnvironment.t.value
    ((@V.Executor.C Host_t F E).(V.Executor_C_signature.execution_environment))) →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, @vmMessageBounds msg → ∀ host, invariant host →
    let result := (@V.Host Host_t F).(Evmc.HOST.call) msg host in
    (invariant (snd result) ∧ OCamlString.length (Evmc.Result.t.output_data (fst result)) < 2^256) ∨
    badMemoryInitComparison) →
  boundedSizedMemorySafe (fun _ ⇒ True) (@V.Executor.delegatecall Host_t F E).
Proof.
  intros Hrevision Hvalue Hprimitive Hchild.
  apply bounded_sized_memory_from_checked.
  eapply delegatecall_checked_from_child; eassumption.
Qed.

Ltac solve_create_memory Hchild :=
  lazymatch goal with
  | |- memorySafe _ _ (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    eapply memory_safe_bind with (P := fun _ ⇒ True);
    [solve_create_memory Hchild|];
    let v := fresh "value" in intros v _;
    repeat match goal with p : _ × _ |- _ ⇒ destruct p end;
    cbn beta iota zeta; solve_create_memory Hchild
  | |- memorySafe _ _ (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒
    apply memory_safe_fmap_ignore; solve_create_memory Hchild
  | |- memorySafe _ _ (@V.M.HostAPI.call _ _ _) ⇒
    apply memory_safe_call; apply Hchild;
    [cbn [Evmc.Message.t.gas]; unfold UintN.to_uint64, UintN.to_z;
      rewrite int64_unsigned_small_identity by lia; lia|assumption]
  | |- memorySafe _ _ (if ?test then _ else _) ⇒ destruct test; solve_create_memory Hchild
  | |- memorySafe _ _ (match ?value with _ ⇒ _ end) ⇒ destruct value; cbn beta iota zeta; solve_create_memory Hchild
  | _ ⇒ apply memory_safe_lift; solve_create_words Hchild
  end.

Lemma generic_create_preserves_words_or_bad_comparison kind salt value inputStart inputSize :
  (0 ≤ U.to_z value)%Z →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z →
    ∀ host, invariant host →
    invariant (snd ((@V.Host Host_t F).(Evmc.HOST.call) msg host)) ∨ badMemoryInitComparison) →
  boundedMemorySafe invariant (fun _ ⇒ True) (@V.Executor.generic_create_impl Host_t F E
    kind salt value inputStart inputSize).
Proof.
  intros Hvalue Hprimitive Hchild. destruct Hprimitive.
  unfold V.Executor.generic_create_impl.
  eapply bounded_memory_bind_prefix with (P := fun _ ⇒ True).
  - solve_create_words Hchild.
  - apply stipend_guard_nonincreasing.
  - intros [] _. eapply bounded_memory_bind_prefix with (P := fun _ ⇒ True).
    + unfold V.Executor.check_write_permissions. solve_create_words Hchild.
    + apply check_write_permissions_nonincreasing.
    + intros [] _. eapply bounded_memory_bind_prefix with (P := fun _ ⇒ True).
      × unfold V.M.HostAPI.get_balance, V.M.HostAPI.Base. solve_create_words Hchild.
      × apply get_balance_nonincreasing.
      × intros selfBalance _. eapply bounded_memory_bind_prefix with (P := fun _ ⇒ True).
        { unfold V.Executor.access_delegation, V.M.HostAPI.get_code_size,
            V.M.HostAPI.copy_code, V.M.HostAPI.access_account, V.M.HostAPI.Base.
          solve_create_words Hchild. }
        { first [apply create_delegation_guard_nonincreasing |
            destruct (@V.Params Host_t F).(Vm.Make_Params_signature.revision);
            try apply create_delegation_guard_nonincreasing;
            match goal with |- gas_nonincreasing (@V.M.when_ _ _ ?condition _) ⇒
              unfold V.M.when_; destruct condition end;
            [apply fail_nonincreasing | apply return_nonincreasing]]. }
        intros [] _.
        match goal with |- boundedMemorySafe _ _ (if ?skip then _ else _) ⇒ destruct skip end.
        { apply bounded_memory_from_unbounded. solve_create_memory Hchild. }
        intros state Hgas Hwords.
        pose proof (@create_forwarded_le (MS.t.gas state)) as Hforwarded.
        change (@UintN.minus_1_64th (MS.t.gas state) ≤ MS.t.gas state)%N in Hforwarded.
        match goal with |- (machineWords _ (snd
          (@V.M.op_letdollar _ _ _ _ _ ?next state)) ∧ _) ∨ _ ⇒
          let forwarded := constr:(@UintN.minus_1_64th (MS.t.gas state)) in
          assert (Hnext : memorySafe invariant (fun _ ⇒ True) (next forwarded)) by
            (unfold V.Executor.merge_child_gas_and_refund; solve_create_memory Hchild);
          exact (Hnext state Hwords)
        end.
Qed.

Lemma create_preserves_words_or_bad_comparison :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z →
    ∀ host, invariant host →
    invariant (snd ((@V.Host Host_t F).(Evmc.HOST.call) msg host)) ∨ badMemoryInitComparison) →
  boundedMemorySafe invariant (fun _ ⇒ True) (@V.Executor.create Host_t F E).
Proof.
  intros Hrevision Hprimitive Hchild. unfold V.Executor.create.
  eapply bounded_memory_bind_prefix; [apply safe_pop3|apply pop3_nonincreasing|].
  intros [[value start] size] [Hvalue _]. cbn [fst] in Hvalue.
  eapply bounded_memory_bind_prefix.
  - apply (extend_memory_preserves_words invariant ). exact Hrevision.
  - apply extend_memory_to_nonincreasing.
  - intros memoryGas _. eapply bounded_memory_bind_prefix.
    + apply safe_spend.
    + apply spend_nonincreasing.
    + intros [] _. eapply bounded_memory_bind_suffix.
      × apply generic_create_preserves_words_or_bad_comparison; [exact (proj1 Hvalue)|exact Hprimitive|exact Hchild].
      × intros [] _. apply memory_safe_lift, safe_increase_pc.
Qed.

Lemma create2_preserves_words_or_bad_comparison :
  (@V.Params Host_t F).(Vm.Make_Params_signature.revision) = Support.Variants.Revision.Eleven →
  PrimitiveHostFrame (@V.Host Host_t F) invariant →
  (∀ msg, (0 ≤ Evmc.Message.t.gas msg < 2^63)%Z →
    (0 ≤ U.to_z (Evmc.Message.t.value msg))%Z →
    ∀ host, invariant host →
    invariant (snd ((@V.Host Host_t F).(Evmc.HOST.call) msg host)) ∨ badMemoryInitComparison) →
  boundedMemorySafe invariant (fun _ ⇒ True) (@V.Executor.create2 Host_t F E).
Proof.
  intros Hrevision Hprimitive Hchild. unfold V.Executor.create2.
  eapply bounded_memory_bind_prefix; [apply safe_pop3|apply pop3_nonincreasing|].
  intros [[value start] size] [Hvalue _]. cbn [fst] in Hvalue.
  eapply bounded_memory_bind_prefix.
  - apply safe_fmap, safe_pop.
  - apply map_nonincreasing, pop_nonincreasing.
  - intros salt _. eapply bounded_memory_bind_prefix.
    + apply (extend_memory_preserves_words invariant ). exact Hrevision.
    + apply extend_memory_to_nonincreasing.
    + intros memoryGas _. eapply bounded_memory_bind_prefix.
      × apply safe_spend.
      × apply spend_nonincreasing.
      × intros [] _. eapply bounded_memory_bind_suffix.
        { apply generic_create_preserves_words_or_bad_comparison; [exact (proj1 Hvalue)|exact Hprimitive|exact Hchild]. }
        intros [] _. apply memory_safe_lift, safe_increase_pc.
Qed.
End Environment.
End Opcodes.

Print Assumptions generic_call_preserves_words_or_bad_comparison.
Print Assumptions delegatecall_preserves_words_or_bad_comparison.
Print Assumptions create2_preserves_words_or_bad_comparison.