Library SetcodeConcrete.vmpc

Nonnegative program counters without a stack-word assumption

JUMP and JUMPI can assign a stack value to the program counter only if it occurs in the concrete set of scanned JUMPDEST positions. Those positions are unsigned words. This avoids assuming anything about a word returned by an unrelated Host callback.
From Stdlib Require Import List String ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml MonadVM.Vm.
Require Import QedkConcrete.wordrange QedkConcrete.vminvariant.
Set Default Goal Selector "!".
Import ListNotations.
Local Open Scope Z_scope.

Lemma set_add_forall {A : Set} (order : OCamlSet.OrderedType (t := A))
    (property : A → Prop) value values :
  property value → Forall property values →
  Forall property (OCamlSet.Make.add order value values).
Proof.
  intros Hvalue Hvalues. induction Hvalues as [|head tail Hhead Htail IH].
  - constructor; [exact Hvalue|constructor].
  - cbn [OCamlSet.Make.add].
    destruct (OCamlSet.Make.before order value head).
    + constructor; [exact Hvalue|constructor; assumption].
    + destruct (OCamlSet.Make.equal_elt order value head); constructor; assumption.
Qed.

Lemma word_set_member_range values value :
  Forall wordRange values →
  Numeric.U256._Set.(OCamlSet.S.mem) value values = true → wordRange value.
Proof.
  intro Hvalues.
  change (OCamlSet.Make.mem ({| OCamlMap.OrderedType.compare := Numeric.U256.compare |}) value values = true → wordRange value).
  unfold OCamlSet.Make.mem. induction Hvalues as [|head tail Hhead Htail IH];
    [discriminate|].
  cbn [OCamlSet.Make.find_opt].
  destruct (OCamlSet.Make.before ({| OCamlMap.OrderedType.compare := Numeric.U256.compare |}) value head); [discriminate|].
  destruct (OCamlSet.Make.equal_elt ({| OCamlMap.OrderedType.compare := Numeric.U256.compare |}) value head) eqn:Hequal.
  - intros _. unfold OCamlSet.Make.equal_elt, OCamlSet.Make.compare_elt in Hequal.
    change (Z.eqb (match Z.compare value head with Eq ⇒ 0 | Lt ⇒ -1 | Gt ⇒ 1 end) 0 = true) in Hequal.
    destruct (Z.compare_spec value head); cbn in Hequal; try discriminate; subst; exact Hhead.
  - exact IH.
Qed.

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

Lemma jump_destinations_range : Forall wordRange V.Executor.jump_destinations.
Proof.
  unfold V.Executor.jump_destinations.
  match goal with |- context [Byte_string.Bytes.fold_left ?body ?initial ?code] ⇒
    set (step := body); set (bytes := code)
  end.
  assert (Hstep : ∀ acc byte,
    wordRange (fst (fst acc)) ∧ Forall wordRange (snd acc) →
    wordRange (fst (fst (step acc byte))) ∧ Forall wordRange (snd (step acc byte))).
  { intros [[pc skip] positions] byte [Hpc Hpositions]. unfold step; cbn beta iota zeta.
    destruct (Z.gtb skip 0); cbn [fst snd]; split; try apply word_add; try exact Hpositions.
    destruct (OCamlChar.equal byte "["%char); [apply set_add_forall|]; assumption. }
  assert (Hfold : ∀ code acc,
    wordRange (fst (fst acc)) ∧ Forall wordRange (snd acc) →
    wordRange (fst (fst (OCamlString.fold_left step acc code))) ∧
      Forall wordRange (snd (OCamlString.fold_left step acc code))).
  { intro code. induction code as [|byte rest IH]; intros acc Hacc;
      rewrite OCamlString.fold_left_matches_structural.
    - exact Hacc.
    - cbn [OCamlString.fold_left_structural].
      rewrite <- OCamlString.fold_left_matches_structural. apply IH, Hstep, Hacc. }
  change (@Byte_string.Bytes.fold_left) with (@OCamlString.fold_left).
  match goal with |- context [OCamlString.fold_left ?f ?initial ?code] ⇒
    pose proof (Hfold code initial (conj word_zero (Forall_nil _))) as Hresult;
    destruct (OCamlString.fold_left f initial code) as [[pc skip] positions] eqn:Hrun;
    idtac
  end.
  exact (proj2 Hresult).
Qed.
End VM.

Section ProgramCounter.
Context {Host_t : Set} {F : @V.FArgs Host_t}.

Definition pcSafe {A : Set} (action : @V.M.t Host_t F A) : Prop :=
  ∀ state, wordRange (MS.t.pc state) → wordRange (MS.t.pc (snd (action state))).

Lemma pc_bind {A B : Set} (action : @V.M.t Host_t F A) next :
  pcSafe action → (∀ value, pcSafe (next value)) →
  pcSafe (@V.M.op_letdollar Host_t F A B action next).
Proof.
  intros Hfirst Hnext state Hpc. specialize (Hfirst state Hpc).
  change (wordRange (MS.t.pc (snd (match action state with
    | (inl value, middle) ⇒ next value middle
    | (inr error, middle) ⇒ (inr error, middle) end)))).
  destruct (action state) as [[value|error] middle]; cbn [snd] in *;
    [apply Hnext|]; exact Hfirst.
Qed.
Lemma pc_return {A : Set} (value : A) : pcSafe (@V.M._return Host_t F A value).
Proof. intros state Hpc. exact Hpc. Qed.
Lemma pc_fail {A : Set} error : pcSafe (@V.M.fail Host_t F A error).
Proof. intros state Hpc. exact Hpc. Qed.
Lemma pc_read {A : Set} (read : @MS.t Host_t F → A) :
  pcSafe (fun state ⇒ (inl (read state), state)).
Proof. intros state Hpc. exact Hpc. Qed.
Lemma pc_lens_read {A : Set} (lens : Lens.t (@MS.t Host_t F) A) :
  pcSafe (@V.M.op_exclamation Host_t F A lens).
Proof. intros state Hpc. exact Hpc. Qed.
Lemma pc_write {A : Set} (lens : Lens.t (@MS.t Host_t F) A) value :
  (∀ state, MS.t.pc (Lens.t.set lens value state) = MS.t.pc state) →
  pcSafe (@V.M.op_coloneq Host_t F A lens value).
Proof. intros H state Hpc. change (wordRange (MS.t.pc (Lens.t.set lens value state))).
  rewrite H. exact Hpc. Qed.
Lemma pc_update {A : Set} (lens : Lens.t (@MS.t Host_t F) A) f :
  (∀ state value, MS.t.pc (Lens.t.set lens value state) = MS.t.pc state) →
  pcSafe (@V.M.update_field Host_t F A lens f).
Proof. intros H state Hpc. change (wordRange (MS.t.pc (Lens.t.set lens (f (Lens.t.get lens state)) state))).
  rewrite H. exact Hpc. Qed.
Lemma pc_lift {A : Set} (action : Host_t → A × Host_t) :
  pcSafe (@V.M.HostAPI.lift Host_t F A action).
Proof. intros state Hpc. unfold V.M.HostAPI.lift.
  destruct (action (MS.t.host state)). exact Hpc. Qed.
Lemma pc_fmap {A B : Set} (f : A → B) action :
  pcSafe action → pcSafe (@V.M.op_ltdollargt Host_t F A B f action).
Proof. intro H. change (pcSafe (@V.M.op_letdollar Host_t F A B action
    (fun value ⇒ V.M._return (f value)))). apply pc_bind; [exact H|].
  intro value. apply pc_return. Qed.
Lemma pc_when condition action :
  pcSafe action → pcSafe (@V.M.when_ Host_t F condition action).
Proof. intro H. destruct condition; [exact H|apply pc_return]. Qed.
Lemma pc_option {A : Set} error (value : option A) :
  pcSafe (@V.M.Option.or_fail Host_t F A error value).
Proof. destruct value; [apply pc_return|apply pc_fail]. Qed.
Lemma pc_pop : pcSafe (@V.pop Host_t F).
Proof. intros state Hpc. unfold V.pop. destruct (MS.t.stack state); exact Hpc. Qed.
Lemma pc_pop2 : pcSafe (@V.pop2 Host_t F).
Proof. intros state Hpc. unfold V.pop2. destruct (MS.t.stack state) as [|a [|b rest]]; exact Hpc. Qed.
Lemma pc_pop3 : pcSafe (@V.pop3 Host_t F).
Proof. intros state Hpc. unfold V.pop3. destruct (MS.t.stack state) as [|a [|b [|c rest]]]; exact Hpc. Qed.
Lemma pc_push value : pcSafe (@V.push Host_t F value).
Proof. intros state Hpc. unfold V.push. destruct (Z.geb (MS.t.stack_depth state) V.max_stack_depth); exact Hpc. Qed.
Lemma pc_spend amount : pcSafe (@V.spend Host_t F amount).
Proof. intros state Hpc. unfold V.spend. destruct (Gas.op_lt (MS.t.gas state) amount); exact Hpc. Qed.
Lemma pc_assign f :
  (∀ pc, wordRange pc → wordRange (f pc)) →
  pcSafe (@V.update_pc_and_continue Host_t F f).
Proof. intros H state Hpc. apply H, Hpc. Qed.
Lemma pc_finish output : pcSafe (@V.finish_execution Host_t F output).
Proof. unfold V.finish_execution. apply pc_bind.
  - apply pc_when, pc_write. reflexivity.
  - intros []. apply pc_return.
Qed.
Lemma pc_seq_map {A B : Set} (f : A → @V.M.t Host_t F B) :
  (∀ x, pcSafe (f x)) → ∀ values, pcSafe (@V.M.Seq.mapM Host_t F A B f values).
Proof.
  intro Hf. induction values as [|value values IH].
  all: unfold V.M.Seq.mapM, Monad.Make_Monad.Seq.mapM, Monad.Make_Monad.Seq.sequence.
  all: rewrite Init.Wf.Fix_eq; [|intros seq left right Heq;
    destruct seq; cbn [OCamlSeq.uncons]; [reflexivity|rewrite Heq; reflexivity]].
  - apply pc_return.
  - cbn [OCamlSeq.uncons]. apply pc_bind; [apply Hf|]. intro result.
    apply pc_bind; [exact IH|]. intro results. apply pc_return.
Qed.
End ProgramCounter.

Ltac solve_pc_safe :=
  lazymatch goal with
  | |- pcSafe (@V.M.op_letdollar _ _ _ _ _ _) ⇒
    apply pc_bind; [solve_pc_safe|]; intro;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_pc_safe
  | |- pcSafe (@V.M.op_gtgteq _ _ _ _ _ _) ⇒
    apply pc_bind; [solve_pc_safe|]; intro;
    repeat match goal with x : _ × _ |- _ ⇒ destruct x end;
    cbn beta iota zeta; solve_pc_safe
  | |- pcSafe (@V.M.op_ltdollargt _ _ _ _ _ _) ⇒ apply pc_fmap; solve_pc_safe
  | |- pcSafe (@V.M.fmap _ _ _ _ _ _) ⇒ apply pc_fmap; solve_pc_safe
  | |- pcSafe (@V.M.when_ _ _ _ _) ⇒ apply pc_when; solve_pc_safe
  | |- pcSafe (if ?test then _ else _) ⇒ destruct test; solve_pc_safe
  | |- pcSafe (match ?value with Some _ ⇒ _ | None ⇒ _ end) ⇒ destruct value; solve_pc_safe
  | |- pcSafe (@V.M._return _ _ _ _) ⇒ apply pc_return
  | |- pcSafe (@V.M.fail _ _ _ _) ⇒ apply pc_fail
  | |- pcSafe (@V.pop _ _) ⇒ apply pc_pop
  | |- pcSafe (@V.pop2 _ _) ⇒ apply pc_pop2
  | |- pcSafe (@V.pop3 _ _) ⇒ apply pc_pop3
  | |- pcSafe (@V.push _ _ _) ⇒ apply pc_push
  | |- pcSafe (@V.spend _ _ _) ⇒ apply pc_spend
  | |- pcSafe (@V.update_pc_and_continue _ _ _) ⇒ apply pc_assign; intros; apply word_add
  | |- pcSafe (@V.increase_pc_and_continue _ _) ⇒ apply pc_assign; intros; apply word_add
  | |- pcSafe (@V.finish_execution _ _ _) ⇒ apply pc_finish
  | |- pcSafe (@V.M.op_exclamation _ _ _ _) ⇒ apply pc_lens_read
  | |- pcSafe (@V.M.Option.or_fail _ _ _ _ _) ⇒ apply pc_option
  | |- pcSafe (@V.M.op_coloneq _ _ _ _ _) ⇒ apply pc_write; reflexivity
  | |- pcSafe (@V.M.update_field _ _ _ _ _) ⇒ apply pc_update; reflexivity
  | |- pcSafe (@V.M.HostAPI.lift _ _ _ _) ⇒ apply pc_lift
  | |- pcSafe (@V.M.Seq.mapM _ _ _ _ _ _) ⇒ apply pc_seq_map; intro; solve_pc_safe
  | |- pcSafe (fun state ⇒ _) ⇒ apply pc_read
  | |- pcSafe (match ?value with _ ⇒ _ end) ⇒ destruct value; solve_pc_safe
  end.