skylabs.lang.cpp.logic.operator
(*
* Copyright (c) 2020 BlueRock Security, Inc.
* This software is distributed under the terms of the BedRock Open-Source License.
* See the LICENSE-BedRock file in the repository root for details.
*)
Require Import skylabs.iris.extra.proofmode.proofmode.
Require Import skylabs.prelude.base.
Require Import skylabs.prelude.option.
Require Import skylabs.lang.cpp.syntax.
Require Import skylabs.lang.cpp.semantics.values.
Require Import skylabs.lang.cpp.semantics.operator.
Require Import skylabs.lang.cpp.logic.pred.
Implicit Type (σ : genv).
* Copyright (c) 2020 BlueRock Security, Inc.
* This software is distributed under the terms of the BedRock Open-Source License.
* See the LICENSE-BedRock file in the repository root for details.
*)
Require Import skylabs.iris.extra.proofmode.proofmode.
Require Import skylabs.prelude.base.
Require Import skylabs.prelude.option.
Require Import skylabs.lang.cpp.syntax.
Require Import skylabs.lang.cpp.semantics.values.
Require Import skylabs.lang.cpp.semantics.operator.
Require Import skylabs.lang.cpp.logic.pred.
Implicit Type (σ : genv).
Pointer p' is not at the beginning of a block.
Definition non_beginning_ptr `{cpp_logic} {σ} p' : mpred :=
∃ p o, [| p' = p ,, o /\
(* ensure that o is > 0 *)
some_Forall2 N.lt (ptr_vaddr p) (ptr_vaddr p') |] ∧ valid_ptr p.
#[global] Hint Opaque non_beginning_ptr : sl_opacity.
Section non_beginning_ptr.
Context `{cpp_logic} {σ}.
#[global] Instance non_beginning_ptr_persistent p : Persistent (non_beginning_ptr p) := _.
#[global] Instance non_beginning_ptr_affine p : Affine (non_beginning_ptr p) := _.
#[global] Instance non_beginning_ptr_timeless p : Timeless (non_beginning_ptr p) := _.
End non_beginning_ptr.
#[global] Typeclasses Opaque non_beginning_ptr.
Section comparable.
Context `{cpp_logic} {σ}.
∃ p o, [| p' = p ,, o /\
(* ensure that o is > 0 *)
some_Forall2 N.lt (ptr_vaddr p) (ptr_vaddr p') |] ∧ valid_ptr p.
#[global] Hint Opaque non_beginning_ptr : sl_opacity.
Section non_beginning_ptr.
Context `{cpp_logic} {σ}.
#[global] Instance non_beginning_ptr_persistent p : Persistent (non_beginning_ptr p) := _.
#[global] Instance non_beginning_ptr_affine p : Affine (non_beginning_ptr p) := _.
#[global] Instance non_beginning_ptr_timeless p : Timeless (non_beginning_ptr p) := _.
End non_beginning_ptr.
#[global] Typeclasses Opaque non_beginning_ptr.
Section comparable.
Context `{cpp_logic} {σ}.
Definition ptr_ord_comparable p1 p2 (f : vaddr -> vaddr -> bool) res : mpred :=
[| same_alloc p1 p2 |] ∗
[| forall va1 va2, ptr_vaddr p1 = Some va1 -> ptr_vaddr p2 = Some va2 -> f va1 va2 = res |] ∗
valid_ptr p1 ∗ valid_ptr p2.
[| same_alloc p1 p2 |] ∗
[| forall va1 va2, ptr_vaddr p1 = Some va1 -> ptr_vaddr p2 = Some va2 -> f va1 va2 = res |] ∗
valid_ptr p1 ∗ valid_ptr p2.
Two pointers into the same object are ptr_ord_comparable. The caller
shows the result from the addresses, without help from this lemma; see
ptr_ord_comparable_off_off_eval for a side condition on offsets.
Lemma ptr_ord_comparable_off_off o1 o2 base p1 p2 f res :
p1 = base ,, o1 ->
p2 = base ,, o2 ->
(forall va1 va2, ptr_vaddr p1 = Some va1 -> ptr_vaddr p2 = Some va2 -> f va1 va2 = res) ->
valid_ptr p1 ∗ valid_ptr p2 ⊢ ptr_ord_comparable p1 p2 f res.
Proof.
iIntros (-> -> Hres) "#[V1 V2]". iFrame (Hres) "V1 V2" => {Hres}; rewrite !valid_ptr_alloc_id.
iRevert "V1 V2"; iIntros "!%".
exact: same_alloc_offset_2.
Qed.
p1 = base ,, o1 ->
p2 = base ,, o2 ->
(forall va1 va2, ptr_vaddr p1 = Some va1 -> ptr_vaddr p2 = Some va2 -> f va1 va2 = res) ->
valid_ptr p1 ∗ valid_ptr p2 ⊢ ptr_ord_comparable p1 p2 f res.
Proof.
iIntros (-> -> Hres) "#[V1 V2]". iFrame (Hres) "V1 V2" => {Hres}; rewrite !valid_ptr_alloc_id.
iRevert "V1 V2"; iIntros "!%".
exact: same_alloc_offset_2.
Qed.
Two offsets p1 and p2 of a pointer base are ptr_ord_comparable.
In the side condition on f, va ranges over the address of base, so
that p1 and p2 are at va + z1 and va + z2.
Relating the addresses needs valid_ptr base: the validity of p1 alone
does not give base an address (see offset_inv_pinned_ptr_pure).
Lemma ptr_ord_comparable_off_off_eval o1 o2 z1 z2 base p1 p2 f res :
p1 = base ,, o1 ->
p2 = base ,, o2 ->
eval_offset σ o1 = Some z1 ->
eval_offset σ o2 = Some z2 ->
(forall va va1 va2 : N,
Z.of_N va1 = Z.of_N va + z1 -> Z.of_N va2 = Z.of_N va + z2 ->
f va1 va2 = res)%Z ->
valid_ptr base ∗ valid_ptr p1 ∗ valid_ptr p2 ⊢ ptr_ord_comparable p1 p2 f res.
Proof.
iIntros (-> -> Ho1 Ho2 Hres) "#(V & V1 & V2)".
(* The validity of the three pointers fixes the two addresses. *)
iAssert ⌜forall va1 va2, ptr_vaddr (base ,, o1) = Some va1 ->
ptr_vaddr (base ,, o2) = Some va2 -> f va1 va2 = res⌝%I as %Hf; last first.
{ iApply (ptr_ord_comparable_off_off o1 o2 base); [done..|iFrame "V1 V2"]. }
destruct (ptr_vaddr (base ,, o1)) as [va1|] eqn:Hva1; last by iPureIntro.
iDestruct (offset_inv_pinned_ptr_pure _ _ _ _ Ho1 Hva1 with "V") as %[Hle Hva].
iDestruct (offset_pinned_ptr_pure Ho2 Hva with "V2") as %[Hle2 Hva2].
iPureIntro. intros ? va2 [= <-]. rewrite Hva2 => -[<-].
apply (Hres (Z.to_N (va1 - z1))); lia.
Qed.
p1 = base ,, o1 ->
p2 = base ,, o2 ->
eval_offset σ o1 = Some z1 ->
eval_offset σ o2 = Some z2 ->
(forall va va1 va2 : N,
Z.of_N va1 = Z.of_N va + z1 -> Z.of_N va2 = Z.of_N va + z2 ->
f va1 va2 = res)%Z ->
valid_ptr base ∗ valid_ptr p1 ∗ valid_ptr p2 ⊢ ptr_ord_comparable p1 p2 f res.
Proof.
iIntros (-> -> Ho1 Ho2 Hres) "#(V & V1 & V2)".
(* The validity of the three pointers fixes the two addresses. *)
iAssert ⌜forall va1 va2, ptr_vaddr (base ,, o1) = Some va1 ->
ptr_vaddr (base ,, o2) = Some va2 -> f va1 va2 = res⌝%I as %Hf; last first.
{ iApply (ptr_ord_comparable_off_off o1 o2 base); [done..|iFrame "V1 V2"]. }
destruct (ptr_vaddr (base ,, o1)) as [va1|] eqn:Hva1; last by iPureIntro.
iDestruct (offset_inv_pinned_ptr_pure _ _ _ _ Ho1 Hva1 with "V") as %[Hle Hva].
iDestruct (offset_pinned_ptr_pure Ho2 Hva with "V2") as %[Hle2 Hva2].
iPureIntro. intros ? va2 [= <-]. rewrite Hva2 => -[<-].
apply (Hres (Z.to_N (va1 - z1))); lia.
Qed.
Two pointers into one array of a positive-size type have their addresses
ordered as their indices are. Hence a comparison f that is determined
by the order of its arguments is determined by the order of the indices.
Lemma ptr_ord_comparable_valid_idx ty p1 p2 (p : ptr) (sz : N) (i j : Z) f res :
size_of σ ty = Some sz ->
(0 < sz)%N ->
p1 = p .[ ty ! i ] ->
p2 = p .[ ty ! j ] ->
(forall va1 va2 : vaddr, (va1 ?= va2)%N = (i ?= j)%Z -> f va1 va2 = res) ->
valid_ptr p1 ∗ valid_ptr p2 ⊢ ptr_ord_comparable p1 p2 f res.
Proof.
iIntros (Hsz Hpos -> -> Hres) "#[V1 V2]".
(*
iApply (ptr_ord_comparable_off_off_eval (. ty ! i ) (. ty ! j )
(Z.of_N sz * i) (Z.of_N sz * j) p).
^^ Would require valid_ptr p as well.
*)
iApply (ptr_ord_comparable_off_off_eval (.[ ty ! 0 ]) (.[ ty ! j - i ])
(Z.of_N sz * 0) (Z.of_N sz * (j - i)) (p .[ ty ! i ])); last iFrame "V1 V2".
{ by rewrite offset_ptr_sub_0. }
{ rewrite o_sub_sub. f_equal. f_equal. lia. }
1-2: exact: eval_o_sub'.
intros * Hva1 Hva2. apply Hres. rewrite -N2Z.inj_compare Hva1 Hva2.
rewrite Z.add_compare_mono_l -Zmult_compare_compat_l; last lia.
by rewrite [(0 ?= _)%Z]Z.compare_antisym -Z.compare_sub -Z.compare_antisym.
Qed.
size_of σ ty = Some sz ->
(0 < sz)%N ->
p1 = p .[ ty ! i ] ->
p2 = p .[ ty ! j ] ->
(forall va1 va2 : vaddr, (va1 ?= va2)%N = (i ?= j)%Z -> f va1 va2 = res) ->
valid_ptr p1 ∗ valid_ptr p2 ⊢ ptr_ord_comparable p1 p2 f res.
Proof.
iIntros (Hsz Hpos -> -> Hres) "#[V1 V2]".
(*
iApply (ptr_ord_comparable_off_off_eval (. ty ! i ) (. ty ! j )
(Z.of_N sz * i) (Z.of_N sz * j) p).
^^ Would require valid_ptr p as well.
*)
iApply (ptr_ord_comparable_off_off_eval (.[ ty ! 0 ]) (.[ ty ! j - i ])
(Z.of_N sz * 0) (Z.of_N sz * (j - i)) (p .[ ty ! i ])); last iFrame "V1 V2".
{ by rewrite offset_ptr_sub_0. }
{ rewrite o_sub_sub. f_equal. f_equal. lia. }
1-2: exact: eval_o_sub'.
intros * Hva1 Hva2. apply Hres. rewrite -N2Z.inj_compare Hva1 Hva2.
rewrite Z.add_compare_mono_l -Zmult_compare_compat_l; last lia.
by rewrite [(0 ?= _)%Z]Z.compare_antisym -Z.compare_sub -Z.compare_antisym.
Qed.
Skeleton for Beq and Bneq axioms on pointers.
This specification follows the C++ standard
(https://eel.is/c++draft/expr.eq3), and is inspired by Cerberus's pointer
provenance semantics for C, and Krebbers's thesis. We forbid cases where
comparisons have undefined or unspecified behavior.
As a deviation, we assume compilers do not perform lifetime-end pointer
zapping (see http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2443.pdf).
(1) We always require both pointers to be valid.
(2) Crucially, in all these semantics comparing pointers with distinct
provenances (allocation ID) but the same address has non-deterministic
results, so we forbid it. Hence, we must prevent ambiguous results.
(a) Comparing two pointers with the same allocation ID is never
ambiguous.
(b) We assume non-null pointers can be reliably distinguished from
[nullptr], because we don't support pointer zapping (unlike
Krebbers and the standard).
(c) Otherwise, we require that both pointers are jointly live, and
prevent remaining potential confusion via [ptr_unambiguous_cmp].
- Joint liveness is required in clause (c) because a dangling pointer and
a non-null live pointer have different provenances but can have the same
address, because the allocator could have reused the address of the
dangling pointer.
- Via [ptr_unambiguous_cmp], we forbid comparing a one-past-the-end
pointer to an object with a pointer to the "beginning" of a different
object, because that gives unspecified results [1], so we choose not to
support this case. We use [non_beginning_ptr] to ensure a pointer is not
to the beginning of a complete object.
[1] From https://eel.is/c++draft/expr.eq3.1:
> If one pointer represents the address of a complete object, and another
pointer represents the address one past the last element of a different
complete object, the result of the comparison is unspecified.
#[local] Definition ptr_unambiguous_cmp vt1 p2 : mpred :=
[| vt1 = Strict |] ∨ non_beginning_ptr p2.
[| vt1 = Strict |] ∨ non_beginning_ptr p2.
Can these pointers be validly compared?
(* Written to ease showing ptr_comparable_symm. *)
Definition ptr_comparable_def p1 p2 res : mpred :=
(* These premises let you assume that that p1 and p2 have an address. *)
[| is_Some (ptr_vaddr p1) /\ is_Some (ptr_vaddr p2) |] -∗
∃ vt1 vt2,
[| same_address_bool p1 p2 = res |] ∗
(_valid_ptr vt1 p1 ∗ _valid_ptr vt2 p2) ∗
([| same_alloc p1 p2 |] ∨
([| p1 = nullptr |] ∨ [| p2 = nullptr |]) ∨
((live_ptr p1 ∧ live_ptr p2) ∗
ptr_unambiguous_cmp vt1 p2 ∗ ptr_unambiguous_cmp vt2 p1)).
Definition ptr_comparable_aux : seal ptr_comparable_def. Proof. by eexists. Qed.
Definition ptr_comparable := ptr_comparable_aux.(unseal).
Definition ptr_comparable_eq : ptr_comparable = _ := ptr_comparable_aux.(seal_eq).
Lemma ptr_ord_comparable_comparable p1 p2 res :
ptr_ord_comparable p1 p2 (λ va1 va2, bool_decide (va1 = va2)) res ⊢ ptr_comparable p1 p2 res.
Proof.
rewrite ptr_comparable_eq /ptr_comparable_def.
iIntros "($ & %Hi & ? & ?)" ([[va1 Hs1] [va2 Hs2]]);
iExists Relaxed, Relaxed; iFrame.
by rewrite -(Hi _ _ Hs1 Hs2) (same_address_bool_eq Hs1 Hs2).
Qed.
Lemma ptr_comparable_equiv p1 p2 res1 res2 :
(is_Some (ptr_vaddr p1) -> is_Some (ptr_vaddr p2) -> res1 = res2) ->
ptr_comparable p1 p2 res1 -|- ptr_comparable p1 p2 res2.
Proof.
rewrite ptr_comparable_eq /ptr_comparable_def !only_provable_wand_forall.
intros Heq. f_equiv => -[Hp1 Hp2]. by move: Heq => /(_ Hp1 Hp2) ->.
Qed.
Lemma ptr_ord_comparable_valid ty p1 p2 (p : ptr) (sz : N) (i j : Z) :
size_of σ ty = Some sz ->
(0 < sz)%N ->
p1 = p .[ ty ! i ] ->
p2 = p .[ ty ! j ] ->
valid_ptr p1 ∗ valid_ptr p2 ⊢
ptr_ord_comparable p1 p2 (λ va1 va2, bool_decide (va1 = va2)) (bool_decide (p1 = p2)).
Proof.
intros; subst.
iApply (ptr_ord_comparable_off_off _ _ p) => //.
intros va1 va2 Hva1 Hva2. apply bool_decide_ext. split.
- intros ->. have -> : i = j.
{ apply (same_address_o_sub_eq p ty i j sz); [done|lia|].
eapply same_address_intro; eassumption. }
reflexivity.
- intros E. rewrite E in Hva1. congruence.
Qed.
Lemma ptr_comparable_valid ty p1 p2 (p : ptr) (sz : N) (i j : Z) :
size_of σ ty = Some sz ->
(0 < sz)%N ->
p1 = p .[ ty ! i ] ->
p2 = p .[ ty ! j ] ->
valid_ptr p1 ∗ valid_ptr p2 ⊢
ptr_comparable p1 p2 (bool_decide (p1 = p2)).
Proof. intros. rewrite -ptr_ord_comparable_comparable. exact: ptr_ord_comparable_valid. Qed.
Lemma ptr_comparable_valid_idx ty p1 p2 (p : ptr) (sz : N) (i j : Z) :
size_of σ ty = Some sz ->
(0 < sz)%N ->
p1 = p .[ ty ! i ] ->
p2 = p .[ ty ! j ] ->
valid_ptr p1 ∗ valid_ptr p2 ⊢
ptr_comparable p1 p2 (bool_decide (i = j)).
Proof.
intros. rewrite ptr_comparable_equiv; first exact /ptr_comparable_valid.
intros; subst; apply bool_decide_ext. rewrite o_sub_eq_inj //; lia.
Qed.
Lemma ptr_comparable_symm p1 p2 res :
ptr_comparable p1 p2 res ⊢ ptr_comparable p2 p1 res.
Proof.
rewrite ptr_comparable_eq /ptr_comparable_def.
rewrite (comm and (is_Some (ptr_vaddr p2))); f_equiv.
iDestruct 1 as (vt1 vt2) "H"; iExists vt2, vt1.
(* To ease rearranging conjuncts or changing connectives, we repeat the
body (which is easy to update), not the nesting structure. *)
rewrite !(comm and (is_Some (ptr_vaddr p2)), comm same_address_bool p2, comm _ (_valid_ptr vt2 _),
symmetry_iff same_alloc p2, comm _ [| p2 = _ |]%I, comm _ (live_ptr p2), comm _ (ptr_unambiguous_cmp vt2 _)).
iStopProof. repeat f_equiv.
Qed.
Lemma nullptr_ptr_comparable {p res} :
(is_Some (ptr_vaddr p) -> bool_decide (p = nullptr) = res) ->
valid_ptr p ⊢ ptr_comparable p nullptr res.
Proof.
rewrite ptr_comparable_eq /ptr_comparable_def.
iIntros "%HresI V" ([Haddr _]); iExists Relaxed, Relaxed.
iDestruct (same_address_bool_null with "V") as %->.
iFrame ((HresI Haddr) (eq_refl nullptr)) "V".
iApply valid_ptr_nullptr.
Qed.
Lemma self_ptr_comparable p :
valid_ptr p ⊢ ptr_comparable p p true.
Proof.
rewrite -ptr_ord_comparable_comparable /ptr_ord_comparable; iIntros "#V".
iDestruct (same_alloc_refl with "V") as "$"; iFrame "V"; iIntros "!%".
intros; simplify_eq. exact: bool_decide_true.
Qed.
Lemma ptr_comparable_off_off o1 o2 base p1 p2 res :
p1 = base ,, o1 ->
p2 = base ,, o2 ->
same_address_bool p1 p2 = res ->
valid_ptr p1 ∗ valid_ptr p2 ⊢ ptr_comparable p1 p2 res.
Proof.
intros ->-> Hs. rewrite -ptr_ord_comparable_comparable.
apply: ptr_ord_comparable_off_off; [done..|].
move=> va1 va2 Hs1 Hs2. by rewrite -(same_address_bool_eq Hs1 Hs2).
Qed.
Lemma ptr_comparable_off o1 base p1 res :
p1 = base ,, o1 ->
same_address_bool p1 base = res ->
valid_ptr p1 ∗ valid_ptr base ⊢ ptr_comparable p1 base res.
Proof.
intros -> Hres. eapply (ptr_comparable_off_off o1 o_id base) => //.
by rewrite offset_ptr_id.
Qed.
End comparable.
Definition ptr_comparable_def p1 p2 res : mpred :=
(* These premises let you assume that that p1 and p2 have an address. *)
[| is_Some (ptr_vaddr p1) /\ is_Some (ptr_vaddr p2) |] -∗
∃ vt1 vt2,
[| same_address_bool p1 p2 = res |] ∗
(_valid_ptr vt1 p1 ∗ _valid_ptr vt2 p2) ∗
([| same_alloc p1 p2 |] ∨
([| p1 = nullptr |] ∨ [| p2 = nullptr |]) ∨
((live_ptr p1 ∧ live_ptr p2) ∗
ptr_unambiguous_cmp vt1 p2 ∗ ptr_unambiguous_cmp vt2 p1)).
Definition ptr_comparable_aux : seal ptr_comparable_def. Proof. by eexists. Qed.
Definition ptr_comparable := ptr_comparable_aux.(unseal).
Definition ptr_comparable_eq : ptr_comparable = _ := ptr_comparable_aux.(seal_eq).
Lemma ptr_ord_comparable_comparable p1 p2 res :
ptr_ord_comparable p1 p2 (λ va1 va2, bool_decide (va1 = va2)) res ⊢ ptr_comparable p1 p2 res.
Proof.
rewrite ptr_comparable_eq /ptr_comparable_def.
iIntros "($ & %Hi & ? & ?)" ([[va1 Hs1] [va2 Hs2]]);
iExists Relaxed, Relaxed; iFrame.
by rewrite -(Hi _ _ Hs1 Hs2) (same_address_bool_eq Hs1 Hs2).
Qed.
Lemma ptr_comparable_equiv p1 p2 res1 res2 :
(is_Some (ptr_vaddr p1) -> is_Some (ptr_vaddr p2) -> res1 = res2) ->
ptr_comparable p1 p2 res1 -|- ptr_comparable p1 p2 res2.
Proof.
rewrite ptr_comparable_eq /ptr_comparable_def !only_provable_wand_forall.
intros Heq. f_equiv => -[Hp1 Hp2]. by move: Heq => /(_ Hp1 Hp2) ->.
Qed.
Lemma ptr_ord_comparable_valid ty p1 p2 (p : ptr) (sz : N) (i j : Z) :
size_of σ ty = Some sz ->
(0 < sz)%N ->
p1 = p .[ ty ! i ] ->
p2 = p .[ ty ! j ] ->
valid_ptr p1 ∗ valid_ptr p2 ⊢
ptr_ord_comparable p1 p2 (λ va1 va2, bool_decide (va1 = va2)) (bool_decide (p1 = p2)).
Proof.
intros; subst.
iApply (ptr_ord_comparable_off_off _ _ p) => //.
intros va1 va2 Hva1 Hva2. apply bool_decide_ext. split.
- intros ->. have -> : i = j.
{ apply (same_address_o_sub_eq p ty i j sz); [done|lia|].
eapply same_address_intro; eassumption. }
reflexivity.
- intros E. rewrite E in Hva1. congruence.
Qed.
Lemma ptr_comparable_valid ty p1 p2 (p : ptr) (sz : N) (i j : Z) :
size_of σ ty = Some sz ->
(0 < sz)%N ->
p1 = p .[ ty ! i ] ->
p2 = p .[ ty ! j ] ->
valid_ptr p1 ∗ valid_ptr p2 ⊢
ptr_comparable p1 p2 (bool_decide (p1 = p2)).
Proof. intros. rewrite -ptr_ord_comparable_comparable. exact: ptr_ord_comparable_valid. Qed.
Lemma ptr_comparable_valid_idx ty p1 p2 (p : ptr) (sz : N) (i j : Z) :
size_of σ ty = Some sz ->
(0 < sz)%N ->
p1 = p .[ ty ! i ] ->
p2 = p .[ ty ! j ] ->
valid_ptr p1 ∗ valid_ptr p2 ⊢
ptr_comparable p1 p2 (bool_decide (i = j)).
Proof.
intros. rewrite ptr_comparable_equiv; first exact /ptr_comparable_valid.
intros; subst; apply bool_decide_ext. rewrite o_sub_eq_inj //; lia.
Qed.
Lemma ptr_comparable_symm p1 p2 res :
ptr_comparable p1 p2 res ⊢ ptr_comparable p2 p1 res.
Proof.
rewrite ptr_comparable_eq /ptr_comparable_def.
rewrite (comm and (is_Some (ptr_vaddr p2))); f_equiv.
iDestruct 1 as (vt1 vt2) "H"; iExists vt2, vt1.
(* To ease rearranging conjuncts or changing connectives, we repeat the
body (which is easy to update), not the nesting structure. *)
rewrite !(comm and (is_Some (ptr_vaddr p2)), comm same_address_bool p2, comm _ (_valid_ptr vt2 _),
symmetry_iff same_alloc p2, comm _ [| p2 = _ |]%I, comm _ (live_ptr p2), comm _ (ptr_unambiguous_cmp vt2 _)).
iStopProof. repeat f_equiv.
Qed.
Lemma nullptr_ptr_comparable {p res} :
(is_Some (ptr_vaddr p) -> bool_decide (p = nullptr) = res) ->
valid_ptr p ⊢ ptr_comparable p nullptr res.
Proof.
rewrite ptr_comparable_eq /ptr_comparable_def.
iIntros "%HresI V" ([Haddr _]); iExists Relaxed, Relaxed.
iDestruct (same_address_bool_null with "V") as %->.
iFrame ((HresI Haddr) (eq_refl nullptr)) "V".
iApply valid_ptr_nullptr.
Qed.
Lemma self_ptr_comparable p :
valid_ptr p ⊢ ptr_comparable p p true.
Proof.
rewrite -ptr_ord_comparable_comparable /ptr_ord_comparable; iIntros "#V".
iDestruct (same_alloc_refl with "V") as "$"; iFrame "V"; iIntros "!%".
intros; simplify_eq. exact: bool_decide_true.
Qed.
Lemma ptr_comparable_off_off o1 o2 base p1 p2 res :
p1 = base ,, o1 ->
p2 = base ,, o2 ->
same_address_bool p1 p2 = res ->
valid_ptr p1 ∗ valid_ptr p2 ⊢ ptr_comparable p1 p2 res.
Proof.
intros ->-> Hs. rewrite -ptr_ord_comparable_comparable.
apply: ptr_ord_comparable_off_off; [done..|].
move=> va1 va2 Hs1 Hs2. by rewrite -(same_address_bool_eq Hs1 Hs2).
Qed.
Lemma ptr_comparable_off o1 base p1 res :
p1 = base ,, o1 ->
same_address_bool p1 base = res ->
valid_ptr p1 ∗ valid_ptr base ⊢ ptr_comparable p1 base res.
Proof.
intros -> Hres. eapply (ptr_comparable_off_off o1 o_id base) => //.
by rewrite offset_ptr_id.
Qed.
End comparable.
Alignment is preserved by pointer arithmetic
Section aligned_sub.
Context `{cpp_logic} {σ}.
Lemma aligned_ptr_ty_sub (p : ptr) (n : Z) ty `{!RealignOk ty} :
is_Some (size_of σ ty) ->
[| aligned_ptr_ty ty p |] ∗ valid_ptr p
⊢ [| aligned_ptr_ty ty (p ,, _sub ty n) |].
Proof.
iIntros ([sz Hsz]) "[%Hp V]".
destruct (align_of_size_of' _ _ Hsz) as (al & Hal & Hal0 & Hdvd).
destruct (ptr_vaddr (p ,, _sub ty n)) as [va'|] eqn:Hva'.
2: { iPureIntro. exists al. split; first done. by right. }
iDestruct (offset_inv_pinned_ptr_pure (_sub ty n) (Z.of_N sz * n) va' p
(eval_o_sub' sz Hsz) Hva' with "V") as %[Hge Hpva].
iPureIntro. exists al. split; first done. left. exists va'. split; first done.
(* al divides the base address and Z.of_N sz * n, hence their sum. *)
move: Hp => [al2 [Hal2 Hpal]].
have Halq : al2 = al by congruence.
rewrite Halq in Hpal.
destruct Hpal as [[va [Hva Hdva]]|Hnone]; last by rewrite Hnone in Hpva.
rewrite Hpva in Hva. injection Hva as Hva. rewrite -Hva in Hdva.
have Hz : (Z.of_N al | Z.of_N va')%Z.
{ have -> : (Z.of_N va' = Z.of_N (Z.to_N (Z.of_N va' - Z.of_N sz * n))
+ Z.of_N sz * n)%Z by rewrite Z2N.id//; lia.
apply Z.divide_add_r.
- exact: N2Z_inj_divide.
- apply Z.divide_mul_l. exact: N2Z_inj_divide. }
have Hpos : (0 < Z.of_N al)%Z by lia.
have Hnn : (0 <= Z.of_N va')%Z by lia.
move: (Z2N_inj_divide _ _ Hpos Hnn Hz). by rewrite !N2Z.id.
Qed.
Context `{cpp_logic} {σ}.
Lemma aligned_ptr_ty_sub (p : ptr) (n : Z) ty `{!RealignOk ty} :
is_Some (size_of σ ty) ->
[| aligned_ptr_ty ty p |] ∗ valid_ptr p
⊢ [| aligned_ptr_ty ty (p ,, _sub ty n) |].
Proof.
iIntros ([sz Hsz]) "[%Hp V]".
destruct (align_of_size_of' _ _ Hsz) as (al & Hal & Hal0 & Hdvd).
destruct (ptr_vaddr (p ,, _sub ty n)) as [va'|] eqn:Hva'.
2: { iPureIntro. exists al. split; first done. by right. }
iDestruct (offset_inv_pinned_ptr_pure (_sub ty n) (Z.of_N sz * n) va' p
(eval_o_sub' sz Hsz) Hva' with "V") as %[Hge Hpva].
iPureIntro. exists al. split; first done. left. exists va'. split; first done.
(* al divides the base address and Z.of_N sz * n, hence their sum. *)
move: Hp => [al2 [Hal2 Hpal]].
have Halq : al2 = al by congruence.
rewrite Halq in Hpal.
destruct Hpal as [[va [Hva Hdva]]|Hnone]; last by rewrite Hnone in Hpva.
rewrite Hpva in Hva. injection Hva as Hva. rewrite -Hva in Hdva.
have Hz : (Z.of_N al | Z.of_N va')%Z.
{ have -> : (Z.of_N va' = Z.of_N (Z.to_N (Z.of_N va' - Z.of_N sz * n))
+ Z.of_N sz * n)%Z by rewrite Z2N.id//; lia.
apply Z.divide_add_r.
- exact: N2Z_inj_divide.
- apply Z.divide_mul_l. exact: N2Z_inj_divide. }
have Hpos : (0 < Z.of_N al)%Z by lia.
have Hnn : (0 <= Z.of_N va')%Z by lia.
move: (Z2N_inj_divide _ _ Hpos Hnn Hz). by rewrite !N2Z.id.
Qed.
Hence the has_type the result needs.
Lemma has_type_ptr_sub (p : ptr) (n : Z) ty `{!RealignOk ty} :
is_Some (size_of σ ty) ->
has_type (Vptr p) (Tptr ty) ∗ valid_ptr (p ,, _sub ty n)
⊢ has_type (Vptr (p ,, _sub ty n)) (Tptr ty).
Proof.
rewrite !has_type_ptr'.
iIntros (Hsz) "[[#Vp #A] #V]". iFrame "V".
by iApply (aligned_ptr_ty_sub p n ty Hsz); iFrame "A Vp".
Qed.
is_Some (size_of σ ty) ->
has_type (Vptr p) (Tptr ty) ∗ valid_ptr (p ,, _sub ty n)
⊢ has_type (Vptr (p ,, _sub ty n)) (Tptr ty).
Proof.
rewrite !has_type_ptr'.
iIntros (Hsz) "[[#Vp #A] #V]". iFrame "V".
by iApply (aligned_ptr_ty_sub p n ty Hsz); iFrame "A Vp".
Qed.
eval_ptr_int_op offsets by the qualifier-erased type while still typing
the result at ty; aligned_ptr_ty_erase_qualifiers reconciles the two.
Lemma has_type_ptr_sub_erase (p : ptr) (n : Z) ty `{!RealignOk ty} :
is_Some (size_of σ ty) ->
has_type (Vptr p) (Tptr ty) ∗ valid_ptr (p ,, _sub (erase_qualifiers ty) n)
⊢ has_type (Vptr (p ,, _sub (erase_qualifiers ty) n)) (Tptr ty).
Proof.
rewrite !has_type_ptr'.
iIntros (Hsz) "[[#Vp %A] #V]". iFrame "V".
have Hsz' : is_Some (size_of σ (erase_qualifiers ty)).
{ by rewrite (size_of_erase_qualifiers σ ty). }
iAssert [| aligned_ptr_ty (erase_qualifiers ty) (p ,, _sub (erase_qualifiers ty) n) |]%I
as %A'.
{ iApply (aligned_ptr_ty_sub p n (erase_qualifiers ty) Hsz'). iFrame "Vp".
iPureIntro. by rewrite -aligned_ptr_ty_erase_qualifiers. }
iPureIntro. by rewrite aligned_ptr_ty_erase_qualifiers.
Qed.
Lemma has_type_ptr_valid (p : ptr) ty : has_type (Vptr p) (Tptr ty) ⊢ valid_ptr p.
Proof. by rewrite has_type_ptr' bi.sep_elim_l. Qed.
End aligned_sub.
is_Some (size_of σ ty) ->
has_type (Vptr p) (Tptr ty) ∗ valid_ptr (p ,, _sub (erase_qualifiers ty) n)
⊢ has_type (Vptr (p ,, _sub (erase_qualifiers ty) n)) (Tptr ty).
Proof.
rewrite !has_type_ptr'.
iIntros (Hsz) "[[#Vp %A] #V]". iFrame "V".
have Hsz' : is_Some (size_of σ (erase_qualifiers ty)).
{ by rewrite (size_of_erase_qualifiers σ ty). }
iAssert [| aligned_ptr_ty (erase_qualifiers ty) (p ,, _sub (erase_qualifiers ty) n) |]%I
as %A'.
{ iApply (aligned_ptr_ty_sub p n (erase_qualifiers ty) Hsz'). iFrame "Vp".
iPureIntro. by rewrite -aligned_ptr_ty_erase_qualifiers. }
iPureIntro. by rewrite aligned_ptr_ty_erase_qualifiers.
Qed.
Lemma has_type_ptr_valid (p : ptr) ty : has_type (Vptr p) (Tptr ty) ⊢ valid_ptr p.
Proof. by rewrite has_type_ptr' bi.sep_elim_l. Qed.
End aligned_sub.
Skeletons for the rules of the impure fragment
Section skeletons.
Context `{cpp_logic} {σ}.
#[local] Notation EVAL :=
(translation_unit -> BinOp -> type -> type -> type -> val -> val -> val -> mpred)
(only parsing).
Context `{cpp_logic} {σ}.
#[local] Notation EVAL :=
(translation_unit -> BinOp -> type -> type -> type -> val -> val -> val -> mpred)
(only parsing).
Operands and result are values of the types they are given.
Definition eval_binop_impure_well_typed_op (E : EVAL) tu : Prop :=
forall bo ty1 ty2 ty3 v1 v2 v3,
E tu bo ty1 ty2 ty3 v1 v2 v3 ⊢
has_type v1 ty1 ∗ has_type v2 ty2 ∗ has_type v3 ty3.
forall bo ty1 ty2 ty3 v1 v2 v3,
E tu bo ty1 ty2 ty3 v1 v2 v3 ⊢
has_type v1 ty1 ∗ has_type v2 ty2 ∗ has_type v3 ty3.
Definition eval_ptr_eq_cmp_op (E : EVAL) tu (bo : BinOp) ty p1 p2 res : mpred :=
E tu bo
(Tptr ty) (Tptr ty) Tbool
(Vptr p1) (Vptr p2) (Vbool res) ∗ True.
E tu bo
(Tptr ty) (Tptr ty) Tbool
(Vptr p1) (Vptr p2) (Vbool res) ∗ True.
Skeleton for the comparison axioms on pointers. P is the comparability
premise: ptr_comparable for Beq, ptr_ord_comparable _ _ f for the
ordering operators.
Definition eval_ptr_cmp_op (E : EVAL) tu (bo : BinOp)
(P : ptr -> ptr -> bool -> mpred) : Prop :=
forall ty p1 p2 res,
P p1 p2 res ∗ has_type (Vptr p1) (Tptr ty) ∗ has_type (Vptr p2) (Tptr ty) ⊢
E tu bo
(Tptr ty) (Tptr ty) Tbool
(Vptr p1) (Vptr p2) (Vbool res) ∗ True.
(P : ptr -> ptr -> bool -> mpred) : Prop :=
forall ty p1 p2 res,
P p1 p2 res ∗ has_type (Vptr p1) (Tptr ty) ∗ has_type (Vptr p2) (Tptr ty) ⊢
E tu bo
(Tptr ty) (Tptr ty) Tbool
(Vptr p1) (Vptr p2) (Vbool res) ∗ True.
For non-comparison operations, we do not require liveness, unlike Krebbers.
We require validity of the result to prevent over/underflow.
(This is because we don't do pointer zapping, following Cerberus).
Supporting pointer zapping would require adding live_ptr preconditions to
these operators.
https://eel.is/c++draft/basic.compound3.1
Skeletons for ptr/int operators. The result's has_type is not demanded:
it follows from the operand's by has_type_ptr_sub.
Definition eval_ptr_int_op (E : EVAL) tu (bo : BinOp) (f : Z -> Z) : Prop :=
forall w s p1 p2 o ty,
is_Some (size_of σ ty) ->
RealignOk ty ->
p2 = p1 ,, _sub (erase_qualifiers ty) (f o) ->
has_type (Vptr p1) (Tptr ty) ∗ has_type (Vint o) (Tnum w s) ∗
valid_ptr p2 ⊢
E tu bo
(Tptr ty) (Tnum w s) (Tptr ty)
(Vptr p1) (Vint o) (Vptr p2).
Definition eval_int_ptr_op (E : EVAL) tu (bo : BinOp) (f : Z -> Z) : Prop :=
forall w s p1 p2 o ty,
is_Some (size_of σ ty) ->
RealignOk ty ->
p2 = p1 ,, _sub (erase_qualifiers ty) (f o) ->
has_type (Vint o) (Tnum w s) ∗ has_type (Vptr p1) (Tptr ty) ∗
valid_ptr p2 ⊢
E tu bo
(Tnum w s) (Tptr ty) (Tptr ty)
(Vint o) (Vptr p1) (Vptr p2).
Skeleton for the ptr/ptr subtraction axiom. The result's typing comes
from the no-overflow side condition the rule already carried.
Definition eval_ptr_ptr_sub_op (E : EVAL) tu : Prop :=
forall w p1 p2 o1 o2 base ty,
is_Some (size_of σ ty) ->
p1 = base ,, _sub (erase_qualifiers ty) o1 ->
p2 = base ,, _sub (erase_qualifiers ty) o2 ->
(* Side condition to prevent overflow; needed per https://eel.is/c++draft/expr.addnote-1 *)
has_type_prop (Vint (o1 - o2)) (Tnum w Signed) ->
has_type (Vptr p1) (Tptr ty) ∗ has_type (Vptr p2) (Tptr ty) ⊢
E tu Bsub
(Tptr ty) (Tptr ty) (Tnum w Signed)
(Vptr p1) (Vptr p2) (Vint (o1 - o2)).
End skeletons.
forall w p1 p2 o1 o2 base ty,
is_Some (size_of σ ty) ->
p1 = base ,, _sub (erase_qualifiers ty) o1 ->
p2 = base ,, _sub (erase_qualifiers ty) o2 ->
(* Side condition to prevent overflow; needed per https://eel.is/c++draft/expr.addnote-1 *)
has_type_prop (Vint (o1 - o2)) (Tnum w Signed) ->
has_type (Vptr p1) (Tptr ty) ∗ has_type (Vptr p2) (Tptr ty) ⊢
E tu Bsub
(Tptr ty) (Tptr ty) (Tnum w Signed)
(Vptr p1) (Vptr p2) (Vint (o1 - o2)).
End skeletons.
The impure fragment of binary-operator evaluation, depending on the abstract machine state.
Module Type EVAL_BINOP_IMPURE.
Parameter eval_binop_impure : forall `{cpp_logic} {σ},
translation_unit -> BinOp -> forall (lhsT rhsT resT : type) (lhs rhs res : val), mpred.
Section axioms.
Context `{cpp_logic} {σ}.
Variable tu : translation_unit.
#[local] Notation EBI := (@eval_binop_impure _ _ _ _) (only parsing).
Axiom eval_binop_impure_erase_all : forall bo lhsT rhsT resT lhs rhs res,
EBI tu bo (erase_qualifiers lhsT) (erase_qualifiers rhsT) (erase_qualifiers resT) lhs rhs res -|-
EBI tu bo lhsT rhsT resT lhs rhs res.
Axiom eval_binop_impure_well_typed :
Unfold (@eval_binop_impure_well_typed_op) (eval_binop_impure_well_typed_op EBI tu).
Axiom eval_ptr_eq :
Unfold (@eval_ptr_cmp_op) (eval_ptr_cmp_op EBI tu Beq ptr_comparable).
Axiom eval_ptr_neq : forall ty p1 p2 res,
Unfold (@eval_ptr_eq_cmp_op)
(eval_ptr_eq_cmp_op EBI tu Beq ty p1 p2 res
⊢ eval_ptr_eq_cmp_op EBI tu Bneq ty p1 p2 (negb res)).
Axiom eval_ptr_le : Unfold (@eval_ptr_cmp_op)
(eval_ptr_cmp_op EBI tu Ble (fun p1 p2 r => ptr_ord_comparable p1 p2 N.leb r)).
Axiom eval_ptr_lt : Unfold (@eval_ptr_cmp_op)
(eval_ptr_cmp_op EBI tu Blt (fun p1 p2 r => ptr_ord_comparable p1 p2 N.ltb r)).
Axiom eval_ptr_ge : Unfold (@eval_ptr_cmp_op)
(eval_ptr_cmp_op EBI tu Bge (fun p1 p2 r => ptr_ord_comparable p1 p2 (fun x y => y <=? x)%N r)).
Axiom eval_ptr_gt : Unfold (@eval_ptr_cmp_op)
(eval_ptr_cmp_op EBI tu Bgt (fun p1 p2 r => ptr_ord_comparable p1 p2 (fun x y => y <? x)%N r)).
Axiom eval_ptr_int_add :
Unfold (@eval_ptr_int_op) (eval_ptr_int_op EBI tu Badd (fun x => x)).
Axiom eval_int_ptr_add :
Unfold (@eval_int_ptr_op) (eval_int_ptr_op EBI tu Badd (fun x => x)).
Axiom eval_ptr_int_sub :
Unfold (@eval_ptr_int_op) (eval_ptr_int_op EBI tu Bsub Z.opp).
Axiom eval_ptr_ptr_sub :
Unfold (@eval_ptr_ptr_sub_op) (eval_ptr_ptr_sub_op EBI tu).
End axioms.
End EVAL_BINOP_IMPURE.
Declare Module Export EVAL_BINOP_IMPURE_AXIOM : EVAL_BINOP_IMPURE.
Section derived.
Context `{cpp_logic} {σ}.
Variable tu : translation_unit.
#[local] Notation EBI := (@eval_binop_impure _ _ _ _) (only parsing).
Parameter eval_binop_impure : forall `{cpp_logic} {σ},
translation_unit -> BinOp -> forall (lhsT rhsT resT : type) (lhs rhs res : val), mpred.
Section axioms.
Context `{cpp_logic} {σ}.
Variable tu : translation_unit.
#[local] Notation EBI := (@eval_binop_impure _ _ _ _) (only parsing).
Axiom eval_binop_impure_erase_all : forall bo lhsT rhsT resT lhs rhs res,
EBI tu bo (erase_qualifiers lhsT) (erase_qualifiers rhsT) (erase_qualifiers resT) lhs rhs res -|-
EBI tu bo lhsT rhsT resT lhs rhs res.
Axiom eval_binop_impure_well_typed :
Unfold (@eval_binop_impure_well_typed_op) (eval_binop_impure_well_typed_op EBI tu).
Axiom eval_ptr_eq :
Unfold (@eval_ptr_cmp_op) (eval_ptr_cmp_op EBI tu Beq ptr_comparable).
Axiom eval_ptr_neq : forall ty p1 p2 res,
Unfold (@eval_ptr_eq_cmp_op)
(eval_ptr_eq_cmp_op EBI tu Beq ty p1 p2 res
⊢ eval_ptr_eq_cmp_op EBI tu Bneq ty p1 p2 (negb res)).
Axiom eval_ptr_le : Unfold (@eval_ptr_cmp_op)
(eval_ptr_cmp_op EBI tu Ble (fun p1 p2 r => ptr_ord_comparable p1 p2 N.leb r)).
Axiom eval_ptr_lt : Unfold (@eval_ptr_cmp_op)
(eval_ptr_cmp_op EBI tu Blt (fun p1 p2 r => ptr_ord_comparable p1 p2 N.ltb r)).
Axiom eval_ptr_ge : Unfold (@eval_ptr_cmp_op)
(eval_ptr_cmp_op EBI tu Bge (fun p1 p2 r => ptr_ord_comparable p1 p2 (fun x y => y <=? x)%N r)).
Axiom eval_ptr_gt : Unfold (@eval_ptr_cmp_op)
(eval_ptr_cmp_op EBI tu Bgt (fun p1 p2 r => ptr_ord_comparable p1 p2 (fun x y => y <? x)%N r)).
Axiom eval_ptr_int_add :
Unfold (@eval_ptr_int_op) (eval_ptr_int_op EBI tu Badd (fun x => x)).
Axiom eval_int_ptr_add :
Unfold (@eval_int_ptr_op) (eval_int_ptr_op EBI tu Badd (fun x => x)).
Axiom eval_ptr_int_sub :
Unfold (@eval_ptr_int_op) (eval_ptr_int_op EBI tu Bsub Z.opp).
Axiom eval_ptr_ptr_sub :
Unfold (@eval_ptr_ptr_sub_op) (eval_ptr_ptr_sub_op EBI tu).
End axioms.
End EVAL_BINOP_IMPURE.
Declare Module Export EVAL_BINOP_IMPURE_AXIOM : EVAL_BINOP_IMPURE.
Section derived.
Context `{cpp_logic} {σ}.
Variable tu : translation_unit.
#[local] Notation EBI := (@eval_binop_impure _ _ _ _) (only parsing).
has_type (Vptr nullptr) (Tptr ty) is a premise rather than a side
condition: it fails when ty has no alignment, e.g. an incomplete type.
Lemma eval_ptr_nullptr_eq_l {ty vp res} :
(is_Some (ptr_vaddr vp) -> bool_decide (vp = nullptr) = res) ->
has_type (Vptr vp) (Tptr ty) ∗ has_type (Vptr nullptr) (Tptr ty)
⊢ Unfold (@eval_ptr_eq_cmp_op) (eval_ptr_eq_cmp_op EBI tu Beq ty vp nullptr res).
Proof.
rewrite -eval_ptr_eq. iIntros (Hres) "#[Hv Hn]". iFrame "Hv Hn".
iApply (nullptr_ptr_comparable Hres). by iApply (has_type_ptr_valid with "Hv").
Qed.
Lemma eval_ptr_nullptr_eq_r {ty vp res} :
(is_Some (ptr_vaddr vp) -> bool_decide (vp = nullptr) = res) ->
has_type (Vptr nullptr) (Tptr ty) ∗ has_type (Vptr vp) (Tptr ty)
⊢ Unfold (@eval_ptr_eq_cmp_op) (eval_ptr_eq_cmp_op EBI tu Beq ty nullptr vp res).
Proof.
rewrite -eval_ptr_eq. iIntros (Hres) "#[Hn Hv]". iFrame "Hn Hv".
rewrite -ptr_comparable_symm.
iApply (nullptr_ptr_comparable Hres). by iApply (has_type_ptr_valid with "Hv").
Qed.
Lemma eval_ptr_self_eq ty p :
has_type (Vptr p) (Tptr ty)
⊢ Unfold (@eval_ptr_eq_cmp_op) (eval_ptr_eq_cmp_op EBI tu Beq ty p p true).
Proof.
rewrite -eval_ptr_eq. iIntros "#$".
iApply self_ptr_comparable. by iApply has_type_ptr_valid.
Qed.
End derived.
Section with_Σ.
Context `{cpp_logic} {σ}.
(is_Some (ptr_vaddr vp) -> bool_decide (vp = nullptr) = res) ->
has_type (Vptr vp) (Tptr ty) ∗ has_type (Vptr nullptr) (Tptr ty)
⊢ Unfold (@eval_ptr_eq_cmp_op) (eval_ptr_eq_cmp_op EBI tu Beq ty vp nullptr res).
Proof.
rewrite -eval_ptr_eq. iIntros (Hres) "#[Hv Hn]". iFrame "Hv Hn".
iApply (nullptr_ptr_comparable Hres). by iApply (has_type_ptr_valid with "Hv").
Qed.
Lemma eval_ptr_nullptr_eq_r {ty vp res} :
(is_Some (ptr_vaddr vp) -> bool_decide (vp = nullptr) = res) ->
has_type (Vptr nullptr) (Tptr ty) ∗ has_type (Vptr vp) (Tptr ty)
⊢ Unfold (@eval_ptr_eq_cmp_op) (eval_ptr_eq_cmp_op EBI tu Beq ty nullptr vp res).
Proof.
rewrite -eval_ptr_eq. iIntros (Hres) "#[Hn Hv]". iFrame "Hn Hv".
rewrite -ptr_comparable_symm.
iApply (nullptr_ptr_comparable Hres). by iApply (has_type_ptr_valid with "Hv").
Qed.
Lemma eval_ptr_self_eq ty p :
has_type (Vptr p) (Tptr ty)
⊢ Unfold (@eval_ptr_eq_cmp_op) (eval_ptr_eq_cmp_op EBI tu Beq ty p p true).
Proof.
rewrite -eval_ptr_eq. iIntros "#$".
iApply self_ptr_comparable. by iApply has_type_ptr_valid.
Qed.
End derived.
Section with_Σ.
Context `{cpp_logic} {σ}.
eval_binop is the semantics of a binary operator in the logic: either
the operator is pure, or evaluating it consults the abstract machine
state.
Definition eval_binop tu (b : BinOp) (lhsT rhsT resT : type) (lhs rhs res : val) : mpred :=
[| eval_binop_pure tu b lhsT rhsT resT lhs rhs res |] ∨
eval_binop_impure tu b lhsT rhsT resT lhs rhs res.
Lemma eval_binop_erase_all tu bo ty1 ty2 ty3 v1 v2 v3 :
eval_binop tu bo (erase_qualifiers ty1) (erase_qualifiers ty2) (erase_qualifiers ty3) v1 v2 v3 -|-
eval_binop tu bo ty1 ty2 ty3 v1 v2 v3.
Proof.
by rewrite /eval_binop eval_binop_pure_erase_all eval_binop_impure_erase_all.
Qed.
Lemma eval_binop_impure_well_typed_prop tu bo ty1 ty2 ty3 v1 v2 v3 :
eval_binop_impure tu bo ty1 ty2 ty3 v1 v2 v3
|-- [| has_type_prop v1 ty1 /\ has_type_prop v2 ty2 /\ has_type_prop v3 ty3 |].
Proof.
rewrite eval_binop_impure_well_typed.
by rewrite !has_type_has_type_prop !only_provable_sep.
Qed.
Theorem eval_binop_well_typed tu bo ty1 ty2 ty3 v1 v2 v3 :
tu ⊧ σ ->
eval_binop tu bo ty1 ty2 ty3 v1 v2 v3
|-- [| has_type_prop v1 ty1 /\ has_type_prop v2 ty2 /\ has_type_prop v3 ty3 |].
Proof.
iDestruct 1 as "[% | X]".
- eauto using eval_binop_pure_well_typed.
- by iApply eval_binop_impure_well_typed_prop.
Qed.
End with_Σ.
[| eval_binop_pure tu b lhsT rhsT resT lhs rhs res |] ∨
eval_binop_impure tu b lhsT rhsT resT lhs rhs res.
Lemma eval_binop_erase_all tu bo ty1 ty2 ty3 v1 v2 v3 :
eval_binop tu bo (erase_qualifiers ty1) (erase_qualifiers ty2) (erase_qualifiers ty3) v1 v2 v3 -|-
eval_binop tu bo ty1 ty2 ty3 v1 v2 v3.
Proof.
by rewrite /eval_binop eval_binop_pure_erase_all eval_binop_impure_erase_all.
Qed.
Lemma eval_binop_impure_well_typed_prop tu bo ty1 ty2 ty3 v1 v2 v3 :
eval_binop_impure tu bo ty1 ty2 ty3 v1 v2 v3
|-- [| has_type_prop v1 ty1 /\ has_type_prop v2 ty2 /\ has_type_prop v3 ty3 |].
Proof.
rewrite eval_binop_impure_well_typed.
by rewrite !has_type_has_type_prop !only_provable_sep.
Qed.
Theorem eval_binop_well_typed tu bo ty1 ty2 ty3 v1 v2 v3 :
tu ⊧ σ ->
eval_binop tu bo ty1 ty2 ty3 v1 v2 v3
|-- [| has_type_prop v1 ty1 /\ has_type_prop v2 ty2 /\ has_type_prop v3 ty3 |].
Proof.
iDestruct 1 as "[% | X]".
- eauto using eval_binop_pure_well_typed.
- by iApply eval_binop_impure_well_typed_prop.
Qed.
End with_Σ.