Library QedkConcrete.vmstackwords
Stack and control-flow word invariants
From Stdlib Require Import List ZArith Lia.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.wordrange QedkConcrete.vmmachinewords QedkConcrete.nativeindices.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Lemma nth_word {UF : Basics.Unreachable (option U.t)} values index :
0 ≤ index → Forall wordRange values →
∀ value, @OCamlList.nth_opt U.t UF values index = Some value → wordRange value.
Proof.
intros Hindex Hvalues.
assert (Hnonnegative : Z.ltb index 0 = false) by (apply Z.ltb_ge; exact Hindex).
unfold OCamlList.nth_opt. rewrite Hnonnegative.
clear Hindex Hnonnegative. revert index.
induction Hvalues as [|head tail Hhead Htail IH]; intros index value Hlookup.
- discriminate.
- cbn beta iota zeta in Hlookup.
match type of Hlookup with (if ?test then _ else _) = _ ⇒ destruct test end.
+ inversion Hlookup; subst. exact Hhead.
+ eapply IH. exact Hlookup.
Qed.
Section Stack.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.t}.
Context {Uunit : Basics.Unreachable unit}.
Lemma replace_words values index replacement value result :
wordRange replacement → Forall wordRange values →
@V.Executor.replace_list Host_t F E U.t index replacement values = Some (value, result) →
wordRange value ∧ Forall wordRange result.
Proof.
intros Hreplacement Hvalues. revert index value result.
induction Hvalues as [|head tail Hhead Htail IH]; intros index value result Hreplace.
- destruct index; discriminate.
- destruct index; cbn [V.Executor.replace_list] in Hreplace.
+ inversion Hreplace; subst. split; [exact Hhead|constructor; assumption].
+ destruct (V.Executor.replace_list (OCamlInt.sub (Z.pos p) 1) replacement tail)
as [[old rest]|] eqn:HtailReplace; [|discriminate].
inversion Hreplace; subst. specialize (IH _ _ _ HtailReplace).
split; [exact (proj1 IH)|constructor; [exact Hhead|exact (proj2 IH)]].
+ destruct (V.Executor.replace_list (OCamlInt.sub (Z.neg p) 1) replacement tail)
as [[old rest]|] eqn:HtailReplace; [|discriminate].
inversion Hreplace; subst. specialize (IH _ _ _ HtailReplace).
split; [exact (proj1 IH)|constructor; [exact Hhead|exact (proj2 IH)]].
Qed.
Lemma stack_read_words : machineSafe invariant (Forall wordRange)
(@V.M.op_exclamation Host_t F _ MS.stack).
Proof. apply safe_read. intros state Hinitial. exact (proj1 Hinitial). Qed.
Lemma stack_write_words values : Forall wordRange values →
machineSafe invariant (fun _ ⇒ True) (@V.M.op_coloneq Host_t F _ MS.stack values).
Proof.
intro Hvalues. apply safe_write. intros state [_ [Hpc Hhost]].
split; [exact Hvalues|]. split; assumption.
Qed.
Lemma dup_preserves_words
{Uaction : Basics.Unreachable (@V.M.Option.t Host_t F U.t)}
{Uoption : Basics.Unreachable (option U.t)} index :
1 ≤ index ≤ 16 →
machineSafe invariant (fun _ ⇒ True)
(@V.Executor.dup Host_t F E index).
Proof.
intro Hindex. unfold V.Executor.dup. cbn beta iota zeta.
eapply safe_bind.
- eapply safe_bind.
+ apply safe_fmap, stack_read_words.
+ intros result [values [Hvalues Hresult]]. subst result.
apply (safe_option invariant wordRange). intros value Hlookup.
unfold V.M.List.nth_opt, Monad.Make_Monad.List.nth_opt in Hlookup.
unfold OCamlInt.sub in Hlookup. rewrite native_repr_small in Hlookup by lia.
lazymatch type of Hlookup with
| @OCamlList.nth_opt _ ?witness _ _ = _ ⇒
eapply (@nth_word witness values (index-1)); [lia|exact Hvalues|exact Hlookup]
end.
- intros value Hvalue. eapply safe_bind; [apply safe_spend|].
intros [] _. eapply safe_bind; [apply safe_push; exact Hvalue|].
intros [] _. apply safe_increase_pc.
Qed.
Lemma swap_preserves_words index :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.swap Host_t F E index).
Proof.
unfold V.Executor.swap. cbn beta iota zeta.
eapply safe_bind; [apply safe_pop|]. intros first Hfirst.
eapply safe_bind.
- eapply safe_bind.
+ apply safe_fmap, stack_read_words.
+ intros result [values [Hvalues Hresult]]. subst result.
apply (safe_option invariant (fun pair ⇒ wordRange (fst pair) ∧ Forall wordRange (snd pair))).
intros [value rest] Hreplace. eapply replace_words; eassumption.
- intros [value rest] [Hvalue Hrest].
eapply safe_bind; [apply stack_write_words; exact Hrest|]. intros [] _.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind; [apply safe_push; exact Hvalue|]. intros [] _.
apply safe_increase_pc.
Qed.
Lemma jump_preserves_words {Uchar : Basics.Unreachable ascii} :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.jump Host_t F E).
Proof.
unfold V.Executor.jump. eapply safe_bind; [apply safe_pop|]. intros pc Hpc.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- unfold V.Executor.check_jump_destination.
destruct (OCamlSet.S.mem _ _ _);
[apply (safe_return invariant (fun _ ⇒ True)); exact I|apply safe_fail].
- intros [] _. apply safe_update_pc. intros _ _. exact Hpc.
Qed.
Lemma jumpi_preserves_words {Uchar : Basics.Unreachable ascii} :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.jumpi Host_t F E).
Proof.
unfold V.Executor.jumpi. eapply safe_bind; [apply safe_pop2|]. intros [pc condition] [Hpc Hcondition].
eapply safe_bind; [apply safe_spend|]. intros [] _.
destruct (U.is_zero condition); [apply safe_increase_pc|].
eapply safe_bind.
- unfold V.Executor.check_jump_destination.
destruct (OCamlSet.S.mem _ _ _);
[apply (safe_return invariant (fun _ ⇒ True)); exact I|apply safe_fail].
- intros [] _. apply safe_update_pc. intros _ _. exact Hpc.
Qed.
Lemma pc_preserves_words :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.pc_ Host_t F E).
Proof.
unfold V.Executor.pc_, V.Executor.fetch_environment_variable_opcode_impl'.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- apply (safe_read invariant wordRange). intros state [_ [Hpc _]]. exact Hpc.
- intros pc Hpc. eapply safe_bind; [apply safe_push; exact Hpc|].
intros [] _. apply safe_increase_pc.
Qed.
Lemma pop_preserves_words :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.pop_ Host_t F E).
Proof.
unfold V.Executor.pop_. eapply safe_bind; [apply safe_pop|]. intros value _.
eapply safe_bind; [apply safe_spend|]. intros [] _. apply safe_increase_pc.
Qed.
Context {UR : Basics.Unreachable U.Repr.t} {UI : Basics.Unreachable int}
{UC : Basics.Unreachable ascii} {UP : Basics.Unreachable (int × int)}.
Lemma stop_preserves_words :
(@V.Params Host_t F).(Vm.Make_Params_signature.debug_tstore) = false →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.stop Host_t F E).
Proof.
intro Hdebug. unfold V.Executor.stop, V.Executor.debug_tstore_stack.
rewrite Hdebug. cbn [negb]. eapply safe_bind.
- apply (safe_return invariant (fun _ ⇒ True)). exact I.
- intros [] _. apply safe_finish.
Qed.
Lemma jumpdest_preserves_words :
(@V.Params Host_t F).(Vm.Make_Params_signature.debug_tstore) = false →
machineSafe invariant (fun _ ⇒ True)
(@V.Executor.jumpdest Host_t F E).
Proof.
intro Hdebug. unfold V.Executor.jumpdest, V.Executor.debug_tstore_stack.
rewrite Hdebug. cbn [negb]. eapply safe_bind.
- apply (safe_return invariant (fun _ ⇒ True)). exact I.
- intros [] _. eapply safe_bind; [apply safe_spend|]. intros [] _. apply safe_increase_pc.
Qed.
End Stack.
Print Assumptions dup_preserves_words.
Print Assumptions swap_preserves_words.
Print Assumptions jumpi_preserves_words.
Require Import RocqOfOCaml.RocqOfOCaml.
Require Import MonadVM.Vm QedkConcrete.wordrange QedkConcrete.vmmachinewords QedkConcrete.nativeindices.
Set Default Goal Selector "!".
Local Open Scope Z_scope.
Lemma nth_word {UF : Basics.Unreachable (option U.t)} values index :
0 ≤ index → Forall wordRange values →
∀ value, @OCamlList.nth_opt U.t UF values index = Some value → wordRange value.
Proof.
intros Hindex Hvalues.
assert (Hnonnegative : Z.ltb index 0 = false) by (apply Z.ltb_ge; exact Hindex).
unfold OCamlList.nth_opt. rewrite Hnonnegative.
clear Hindex Hnonnegative. revert index.
induction Hvalues as [|head tail Hhead Htail IH]; intros index value Hlookup.
- discriminate.
- cbn beta iota zeta in Hlookup.
match type of Hlookup with (if ?test then _ else _) = _ ⇒ destruct test end.
+ inversion Hlookup; subst. exact Hhead.
+ eapply IH. exact Hlookup.
Qed.
Section Stack.
Context {Host_t : Set} {F : @V.FArgs Host_t} {E : @V.Executor.FArgs Host_t F}.
Context (invariant : Host_t → Prop).
Context {UF : Basics.Unreachable U.t} {UG : Basics.Unreachable Gas.t}.
Context {Uunit : Basics.Unreachable unit}.
Lemma replace_words values index replacement value result :
wordRange replacement → Forall wordRange values →
@V.Executor.replace_list Host_t F E U.t index replacement values = Some (value, result) →
wordRange value ∧ Forall wordRange result.
Proof.
intros Hreplacement Hvalues. revert index value result.
induction Hvalues as [|head tail Hhead Htail IH]; intros index value result Hreplace.
- destruct index; discriminate.
- destruct index; cbn [V.Executor.replace_list] in Hreplace.
+ inversion Hreplace; subst. split; [exact Hhead|constructor; assumption].
+ destruct (V.Executor.replace_list (OCamlInt.sub (Z.pos p) 1) replacement tail)
as [[old rest]|] eqn:HtailReplace; [|discriminate].
inversion Hreplace; subst. specialize (IH _ _ _ HtailReplace).
split; [exact (proj1 IH)|constructor; [exact Hhead|exact (proj2 IH)]].
+ destruct (V.Executor.replace_list (OCamlInt.sub (Z.neg p) 1) replacement tail)
as [[old rest]|] eqn:HtailReplace; [|discriminate].
inversion Hreplace; subst. specialize (IH _ _ _ HtailReplace).
split; [exact (proj1 IH)|constructor; [exact Hhead|exact (proj2 IH)]].
Qed.
Lemma stack_read_words : machineSafe invariant (Forall wordRange)
(@V.M.op_exclamation Host_t F _ MS.stack).
Proof. apply safe_read. intros state Hinitial. exact (proj1 Hinitial). Qed.
Lemma stack_write_words values : Forall wordRange values →
machineSafe invariant (fun _ ⇒ True) (@V.M.op_coloneq Host_t F _ MS.stack values).
Proof.
intro Hvalues. apply safe_write. intros state [_ [Hpc Hhost]].
split; [exact Hvalues|]. split; assumption.
Qed.
Lemma dup_preserves_words
{Uaction : Basics.Unreachable (@V.M.Option.t Host_t F U.t)}
{Uoption : Basics.Unreachable (option U.t)} index :
1 ≤ index ≤ 16 →
machineSafe invariant (fun _ ⇒ True)
(@V.Executor.dup Host_t F E index).
Proof.
intro Hindex. unfold V.Executor.dup. cbn beta iota zeta.
eapply safe_bind.
- eapply safe_bind.
+ apply safe_fmap, stack_read_words.
+ intros result [values [Hvalues Hresult]]. subst result.
apply (safe_option invariant wordRange). intros value Hlookup.
unfold V.M.List.nth_opt, Monad.Make_Monad.List.nth_opt in Hlookup.
unfold OCamlInt.sub in Hlookup. rewrite native_repr_small in Hlookup by lia.
lazymatch type of Hlookup with
| @OCamlList.nth_opt _ ?witness _ _ = _ ⇒
eapply (@nth_word witness values (index-1)); [lia|exact Hvalues|exact Hlookup]
end.
- intros value Hvalue. eapply safe_bind; [apply safe_spend|].
intros [] _. eapply safe_bind; [apply safe_push; exact Hvalue|].
intros [] _. apply safe_increase_pc.
Qed.
Lemma swap_preserves_words index :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.swap Host_t F E index).
Proof.
unfold V.Executor.swap. cbn beta iota zeta.
eapply safe_bind; [apply safe_pop|]. intros first Hfirst.
eapply safe_bind.
- eapply safe_bind.
+ apply safe_fmap, stack_read_words.
+ intros result [values [Hvalues Hresult]]. subst result.
apply (safe_option invariant (fun pair ⇒ wordRange (fst pair) ∧ Forall wordRange (snd pair))).
intros [value rest] Hreplace. eapply replace_words; eassumption.
- intros [value rest] [Hvalue Hrest].
eapply safe_bind; [apply stack_write_words; exact Hrest|]. intros [] _.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind; [apply safe_push; exact Hvalue|]. intros [] _.
apply safe_increase_pc.
Qed.
Lemma jump_preserves_words {Uchar : Basics.Unreachable ascii} :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.jump Host_t F E).
Proof.
unfold V.Executor.jump. eapply safe_bind; [apply safe_pop|]. intros pc Hpc.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- unfold V.Executor.check_jump_destination.
destruct (OCamlSet.S.mem _ _ _);
[apply (safe_return invariant (fun _ ⇒ True)); exact I|apply safe_fail].
- intros [] _. apply safe_update_pc. intros _ _. exact Hpc.
Qed.
Lemma jumpi_preserves_words {Uchar : Basics.Unreachable ascii} :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.jumpi Host_t F E).
Proof.
unfold V.Executor.jumpi. eapply safe_bind; [apply safe_pop2|]. intros [pc condition] [Hpc Hcondition].
eapply safe_bind; [apply safe_spend|]. intros [] _.
destruct (U.is_zero condition); [apply safe_increase_pc|].
eapply safe_bind.
- unfold V.Executor.check_jump_destination.
destruct (OCamlSet.S.mem _ _ _);
[apply (safe_return invariant (fun _ ⇒ True)); exact I|apply safe_fail].
- intros [] _. apply safe_update_pc. intros _ _. exact Hpc.
Qed.
Lemma pc_preserves_words :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.pc_ Host_t F E).
Proof.
unfold V.Executor.pc_, V.Executor.fetch_environment_variable_opcode_impl'.
eapply safe_bind; [apply safe_spend|]. intros [] _.
eapply safe_bind.
- apply (safe_read invariant wordRange). intros state [_ [Hpc _]]. exact Hpc.
- intros pc Hpc. eapply safe_bind; [apply safe_push; exact Hpc|].
intros [] _. apply safe_increase_pc.
Qed.
Lemma pop_preserves_words :
machineSafe invariant (fun _ ⇒ True) (@V.Executor.pop_ Host_t F E).
Proof.
unfold V.Executor.pop_. eapply safe_bind; [apply safe_pop|]. intros value _.
eapply safe_bind; [apply safe_spend|]. intros [] _. apply safe_increase_pc.
Qed.
Context {UR : Basics.Unreachable U.Repr.t} {UI : Basics.Unreachable int}
{UC : Basics.Unreachable ascii} {UP : Basics.Unreachable (int × int)}.
Lemma stop_preserves_words :
(@V.Params Host_t F).(Vm.Make_Params_signature.debug_tstore) = false →
machineSafe invariant (fun _ ⇒ True) (@V.Executor.stop Host_t F E).
Proof.
intro Hdebug. unfold V.Executor.stop, V.Executor.debug_tstore_stack.
rewrite Hdebug. cbn [negb]. eapply safe_bind.
- apply (safe_return invariant (fun _ ⇒ True)). exact I.
- intros [] _. apply safe_finish.
Qed.
Lemma jumpdest_preserves_words :
(@V.Params Host_t F).(Vm.Make_Params_signature.debug_tstore) = false →
machineSafe invariant (fun _ ⇒ True)
(@V.Executor.jumpdest Host_t F E).
Proof.
intro Hdebug. unfold V.Executor.jumpdest, V.Executor.debug_tstore_stack.
rewrite Hdebug. cbn [negb]. eapply safe_bind.
- apply (safe_return invariant (fun _ ⇒ True)). exact I.
- intros [] _. eapply safe_bind; [apply safe_spend|]. intros [] _. apply safe_increase_pc.
Qed.
End Stack.
Print Assumptions dup_preserves_words.
Print Assumptions swap_preserves_words.
Print Assumptions jumpi_preserves_words.