mailbox.v 7.76 KB
Newer Older
Dan Frumin's avatar
Dan Frumin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
From iris.proofmode Require Import tactics.
From iris.algebra Require Import excl.
From iris_logrel Require Export logrel.

Definition mk_offer : val :=
  λ: "v", ("v", ref #0).
Definition revoke_offer : val :=
  λ: "v", if: CAS (Snd "v") #0 #2 then SOME (Fst "v") else NONE.
Definition take_offer : val :=
  λ: "v", if: CAS (Snd "v") #0 #1 then SOME (Fst "v") else NONE.

Definition put_mail : val := λ: "r" "v",
  let: "off" := mk_offer "v" in
  "r" <- SOME "off";;
  revoke_offer "off".
Definition get_mail : val := λ: "r",
  match: !"r" with
    NONE => NONE
  | SOME "x" => take_offer "x"
  end.
Definition new_mb : val := λ: <>, ref NONE.
Definition mailbox : val :=
  PackV (new_mb, put_mail, get_mail).

Section typing.
  Variable τ : type.
  Definition offerτ := TProd τ (Tref TNat).
  Definition mbτ := Tref (MAYBE offerτ).
  Lemma new_mb_typed Γ :
    Γ ⊢ₜ new_mb : TArrow TUnit mbτ.
  Proof. solve_typed. Qed.
  Hint Resolve new_mb_typed : typeable.
  Lemma get_mail_typed Γ :
    Γ ⊢ₜ get_mail : TArrow mbτ (MAYBE τ).
  Proof.
    unlock get_mail.
    unlock take_offer.
    solve_typed.
  Qed.
  Hint Resolve get_mail_typed : typeable.
  Lemma put_mail_typed Γ :
    Γ ⊢ₜ put_mail : TArrow mbτ (TArrow τ (MAYBE τ)).
  Proof.
    unlock put_mail.
    unlock revoke_offer mk_offer.
    solve_typed.
  Qed.
  Hint Resolve put_mail_typed : typeable.
  Lemma mailbox_typed Γ :
    Γ ⊢ₜ mailbox : TExists (TProd (TProd (TArrow TUnit (TVar 0))
                                         (TArrow (TVar 0) (TArrow τ.[ren (+1)] (MAYBE τ.[ren (+1)]))))
                                         (TArrow (TVar 0) (MAYBE τ.[ren (+1)]))).
  Proof.
    unlock mailbox; simpl.
    econstructor.
    econstructor; [ econstructor | ]; asimpl;
      eauto with typeable.
  Qed.
End typing.

Definition channelR := exclR unitR.
Class channelG Σ := { channel_inG :> inG Σ channelR }.
Definition channelΣ : gFunctors := #[GFunctor channelR].
Instance subG_channelΣ {Σ} : subG channelΣ Σ  channelG Σ.
Proof. solve_inG. Qed.

Section side_channel.
  Context `{!heapG Σ, !channelG Σ}.
  Implicit Types l : loc.

  Definition stages γ (P : val  iProp Σ) l v :=
    ((l ↦ᵢ #0  P v)
      (l ↦ᵢ #1)
      (l ↦ᵢ #2  own γ (Excl ())))%I.

  Definition is_offer γ (P : val  iProp Σ) (v : val) : iProp Σ :=
    ( v' l, v = (v', # l)%V   ι, inv ι (stages γ P l v'))%I.

  (* A partial specification for revoke that will be useful later *)
  Lemma revoke_works γ P v :
    is_offer γ P v  own γ (Excl ()) -
    WP revoke_offer v
      {{ v', ( v'' : val, v' = InjRV v''⌝  P v'')  v' = InjLV #() }}.
  Proof.
    iIntros "[#Hinv Hγ]".
    rewrite /is_offer.
    iDestruct "Hinv" as (v' l) "[% Hinv]"; simplify_eq.
    iDestruct "Hinv" as (N) "#Hinv".
    unlock revoke_offer.
    wp_let.
    wp_proj.
    wp_bind (CAS _ _ _).
    iInv N as "Hstages" "Hclose".
    iDestruct "Hstages" as "[H | [H | H]]".
    - iDestruct "H" as "[Hl HP]".      
      wp_cas_suc.
      iMod ("Hclose" with "[Hl Hγ]").
      iRight; iRight; iFrame.
      iModIntro.
      wp_if.
      wp_proj.
Dan Frumin's avatar
Dan Frumin committed
102
      iApply wp_value.
Dan Frumin's avatar
Dan Frumin committed
103 104 105 106 107 108 109
      iLeft.
      iExists v'; iSplit; auto.
    - wp_cas_fail.
      iMod ("Hclose" with "[H]").
      iRight; iLeft; auto.
      iModIntro.
      wp_if.
Dan Frumin's avatar
Dan Frumin committed
110
      iApply wp_value.
Dan Frumin's avatar
Dan Frumin committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      iRight; auto.
    - iDestruct "H" as "[Hl H]".
      wp_cas_fail.
      by iDestruct (own_valid_2 with "H Hγ") as %?.
  Qed.

  (* A partial specification for take that will be useful later *)
  Lemma take_works γ N P v l :
    inv N (stages γ P l v) -
    WP take_offer (v, LitV l)%V
      {{ v', ( v'' : val, v' = InjRV v''⌝  P v'')  v' = InjLV #() }}.
  Proof.
    iIntros "#Hinv".
    unlock take_offer.
    wp_rec.
    wp_proj.
    wp_bind (CAS _ _ _).
    iInv N as "Hstages" "Hclose".
    iDestruct "Hstages" as "[H | [H | H]]".
    - iDestruct "H" as "[H HP]".
      wp_cas_suc.
      iMod ("Hclose" with "[H]").
      iRight; iLeft; done.
      iModIntro.
      wp_if.
      wp_proj.
Dan Frumin's avatar
Dan Frumin committed
137
      iApply wp_value.
Dan Frumin's avatar
Dan Frumin committed
138 139 140 141 142 143 144
      iLeft.
      auto.
    - wp_cas_fail.
      iMod ("Hclose" with "[H]").
      iRight; iLeft; done.
      iModIntro.
      wp_if.
Dan Frumin's avatar
Dan Frumin committed
145
      iApply wp_value; auto.
Dan Frumin's avatar
Dan Frumin committed
146 147 148 149 150 151
    - iDestruct "H" as "[Hl Hγ]".
      wp_cas_fail.
      iMod ("Hclose" with "[Hl Hγ]").
      iRight; iRight; iFrame.
      iModIntro.
      wp_if.
Dan Frumin's avatar
Dan Frumin committed
152
      iApply wp_value; auto.
Dan Frumin's avatar
Dan Frumin committed
153 154 155 156 157 158 159 160 161 162 163
  Qed.

  Lemma mk_offer_works P (v : val) :
    P v -
    WP mk_offer v {{ v',  γ, own γ (Excl ())  is_offer γ P v' }}.
  Proof.
    iIntros "HP".
    unlock mk_offer.
    wp_rec.
    wp_alloc l as "Hl".
    iApply wp_fupd.
Dan Frumin's avatar
Dan Frumin committed
164
    iApply wp_value.
Dan Frumin's avatar
Dan Frumin committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    pose proof (nroot .@ "N'") as N'.    
    iMod (own_alloc (Excl ())) as (γ) "Hγ". done.
    iMod (inv_alloc N' _ (stages γ P l v) with "[HP Hl]") as "#Hinv'".
    { iNext. rewrite /stages. iLeft. iFrame. }
    iModIntro.
    iExists γ; iFrame.
    unfold is_offer.
    iExists _, _; iSplitR; eauto.
  Qed.
    
End side_channel.

Section mailbox.
  Context `{!heapG Σ, !channelG Σ}.
  Implicit Types l : loc.

  Definition mailbox_inv (P : val  iProp Σ) (v : val) : iProp Σ :=
    ( l : loc, v = # l  (l ↦ᵢ NONEV  ( v' γ, l ↦ᵢ SOMEV v'  is_offer γ P v')))%I.

  Theorem new_mb_works (P : val  iProp Σ) (Φ : val  iProp Σ) :
    ( v N, inv N (mailbox_inv P v) - Φ v)
    - WP new_mb #() {{ Φ }}.
  Proof.
    iIntros "HΦ".
    unlock new_mb.
    wp_rec.
    iApply wp_fupd.
    wp_alloc r as "Hr".
    pose proof (nroot .@ "N") as N.
    iMod (inv_alloc N _ (mailbox_inv P (# r)) with "[Hr]") as "#Hinv".
    { iExists r; iSplit; try iLeft; auto. }
    iModIntro.
    by iApply "HΦ".
  Qed.
  
  Theorem put_mail_works (P : val  iProp Σ) (Φ : val  iProp Σ) N (mb v : val) :
    inv N (mailbox_inv P mb) -
    P v -
    ( v', (( v'', v' = SOMEV v''⌝  P v'')  v' = NONEV) - Φ v')
    - WP put_mail mb v {{ Φ }}.
  Proof.
    iIntros "#Hinv HP HΦ".
    unlock put_mail.
    wp_rec.
    wp_let.
    wp_bind (mk_offer v).
    iApply (wp_wand with "[HP]").
    { iApply (mk_offer_works with "HP"). }
    iIntros (off). iDestruct 1 as (γ) "[Hγ #Hoffer]".
    wp_let.
    wp_bind (mb <- _)%E.
    iInv N as "Hmailbox" "Hclose".
    iDestruct "Hmailbox" as (l) "[>% Hl]". simplify_eq/=.
    iDestruct "Hl" as "[>Hl | Hl]";
      [ | iDestruct "Hl" as (off' γ') "[Hl Hoff']"]; (* off' - already existing offer *)
      wp_store;
      [iMod ("Hclose" with "[Hl]") | iMod ("Hclose" with "[Hl Hoff']")];
      try (iExists _; iNext; iSplit; eauto);
      iModIntro;
      wp_let;
      iApply (wp_wand with "[Hγ Hoffer] HΦ");
      iApply (revoke_works with "[$]").
  Qed.
  
  Theorem get_mail_works (P : val  iProp Σ) (Φ : val  iProp Σ) N (mb : val) :
    inv N (mailbox_inv P mb) -
    ( v', (( v'', v' = SOMEV v''⌝  P v'')  v' = NONEV) - Φ v')
    - WP get_mail mb {{ Φ }}.
  Proof.
    iIntros "#Hinv HΦ".
    unlock get_mail.
    wp_rec.
    wp_bind (! _)%E.
    iInv N as "Hmailbox" "Hclose".
    iDestruct "Hmailbox" as (l') "[>% H]"; simplify_eq/=.
    iDestruct "H" as "[H | H]".
    + wp_load.
      iMod ("Hclose" with "[H]").
      iExists l'; iSplit; auto.
      iModIntro.
      wp_case_inl.
      wp_seq.
Dan Frumin's avatar
Dan Frumin committed
247
      iApply wp_value.
Dan Frumin's avatar
Dan Frumin committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      iApply "HΦ"; auto.
    + iDestruct "H" as (v' γ) "[Hl' #Hoffer]".
      wp_load.
      iMod ("Hclose" with "[Hl' Hoffer]").
      { iExists l'; iSplit; auto.
        iRight; iExists v', γ; by iSplit. }
      iModIntro.
      simpl. wp_case_inr. (* TODO: [simpl] is require here *)
      wp_let.
      iDestruct "Hoffer" as (v'' l'') "[% Hoffer]"; simplify_eq.
      iDestruct "Hoffer" as (ι) "Hinv'".        
      iApply (wp_wand with "[] HΦ").
      iApply take_works; auto.
  Qed.

End mailbox.