concurrent_stack2.v 13.7 KB
Newer Older
1 2
(** THIS FILE CURRENTLY DOES NOT COMPILE because it has not been ported to the
stricter CAS requirements yet. *)
3 4 5
From iris.program_logic Require Export weakestpre hoare.
From iris.heap_lang Require Export lang proofmode notation.
From iris.algebra Require Import excl.
6 7 8

From iris_examples.concurrent_stacks Require Import spec.

9 10
Set Default Proof Using "Type".

11 12
(** Stack 2: With helping, bag spec. *)

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 102 103 104 105 106 107 108 109 110 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
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 mailbox : val :=
  λ: "_",
  let: "r" := ref NONEV in
  (rec: "put" "v" :=
     let: "off" := mk_offer "v" in
     "r" <- SOME "off";;
     revoke_offer "off",
   rec: "get" "n" :=
     let: "offopt" := !"r" in
     match: "offopt" with
       NONE => NONE
     | SOME "x" => take_offer "x"
     end
  ).

Definition mk_stack : val :=
  λ: "_",
  let: "mailbox" := mailbox #() in
  let: "put" := Fst "mailbox" in
  let: "get" := Snd "mailbox" in
  let: "r" := ref NONEV in
  (rec: "pop" "n" :=
     match: "get" #() with
       NONE =>
       (match: !"r" with
          NONE => NONE
        | SOME "hd" =>
          if: CAS "r" (SOME "hd") (Snd "hd")
          then SOME (Fst "hd")
          else "pop" "n"
        end)
     | SOME "x" => SOME "x"
     end,
    rec: "push" "n" :=
      match: "put" "n" with
        NONE => #()
      | SOME "n" =>
        let: "r'" := !"r" in
        let: "r''" := SOME ("n", "r'") in
        if: CAS "r" "r'" "r''"
        then #()
        else "push" "n"
      end).

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.

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

  (* A partial specification for revoke that will be useful later *)
  Lemma revoke_works N γ P l v :
    inv N (stages γ P l v)  own γ (Excl ()) -
    WP revoke_offer (v, #l)
      {{ v', ( v'' : val, v' = InjRV v''  P v'')  v' = InjLV #() }}.
  Proof.
    iIntros "[#Hinv Hγ]".
    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.
      iLeft.
      iExists v; iSplit; auto.
    - wp_cas_fail.
      iMod ("Hclose" with "[H]").
      iRight; iLeft; auto.
      iModIntro.
      wp_if.
      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".
    wp_lam.
    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.
      iLeft.
      auto.
    - wp_cas_fail.
      iMod ("Hclose" with "[H]").
      iRight; iLeft; done.
      iModIntro.
      wp_if.
      auto.
    - iDestruct "H" as "[Hl Hγ]".
      wp_cas_fail.
      iMod ("Hclose" with "[Hl Hγ]").
      iRight; iRight; iFrame.
      iModIntro.
      wp_if.
      auto.
  Qed.
End side_channel.

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

  Theorem mailbox_works {channelG0 : channelG Σ} (P : val  iProp Σ) (Φ : val  iProp Σ) :
    ( (v v : val),
      Closed [] v₁⌝
      Closed [] v₂⌝
      ( (v : val),  (P v - WP v v {{v',
          ( v'', v' = SOMEV v''  P v'')  v' = NONEV}}))
      ( (WP v #() {{v', ( v'', v' = SOMEV v''  P v'')  v' = NONEV}}))
     - Φ (v, v)%V)
    - WP mailbox #() {{ Φ }}.
  Proof.
    iIntros "HΦ".
    pose proof (nroot .@ "N") as N.
    rewrite -wp_fupd.
    wp_lam.
    wp_alloc l as "Hl".
    wp_let.
    iMod (inv_alloc N _ (mailbox_inv P #l) with "[Hl]") as "#Hinv".
    iExists l; iSplit; try iLeft; auto.
    iModIntro.
    iApply "HΦ"; repeat iSplit; try (iPureIntro; apply _).
    * iIntros (v) "!# HP".
      wp_rec.
      wp_bind (mk_offer v).
      pose proof (nroot .@ "N'") as N'.
      rewrite -wp_fupd.
      wp_lam.
      iMod (own_alloc (Excl ())) as (γ) "Hγ". done.
      wp_alloc l' as "Hl'".
      iMod (inv_alloc N' _ (stages γ P l' v) with "[HP Hl']") as "#Hinv'".
      iLeft; iFrame.
      iModIntro.
      wp_let.
      wp_bind (#l <- _)%E.
      iInv N as "Hmailbox" "Hclose".
      iDestruct "Hmailbox" as (l'') "[>% H]".
      injection H; intros; subst.
      iDestruct "H" as "[H | H]";
        [idtac | iDestruct "H" as (v' γ') "[Hl H]"];
        wp_store;
        [iMod ("Hclose" with "[H]") | iMod ("Hclose" with "[Hl H]")];
        try  (iExists l''; iSplit; try iRight; auto;
              iExists (v, #l')%V, γ; iFrame;
              iExists v, l'; auto);
        iModIntro;
        wp_let;
        iApply (revoke_works with "[Hγ Hinv]"); auto.
    * iIntros "!#".
      wp_rec.
      wp_bind (! _)%E.
      iInv N as "Hmailbox" "Hclose".
      iDestruct "Hmailbox" as (l') "[>% H]".
      injection H; intros; subst.
      iDestruct "H" as "[H | H]".
      + wp_load.
        iMod ("Hclose" with "[H]").
        iExists l'; iSplit; auto.
        iModIntro.
        wp_let.
        wp_match.
        iRight; auto.
      + iDestruct "H" as (v' γ) "[Hl' #Hoffer]".
        wp_load.
        iMod ("Hclose" with "[Hl' Hoffer]").
        { iExists l'; iSplit; auto.
          iRight; iExists v', γ; by iSplit. }
        iModIntro.
        wp_let.
        wp_match.
        iDestruct "Hoffer" as (v'' l'') "[% Hoffer]".
        iDestruct "Hoffer" as (ι) "Hinv'".
        subst.
        iApply take_works; auto.
  Qed.
End mailbox.

Section stack_works.
  Context `{!heapG Σ}.
  Implicit Types l : loc.

  Definition is_stack_pre (P : val  iProp Σ) (F : val -c> iProp Σ) :
     val -c> iProp Σ := λ v,
    (v  NONEV   (h t : val), v  SOMEV (h, t)%V  P h   F t)%I.

  Local Instance is_stack_contr (P : val  iProp Σ): Contractive (is_stack_pre P).
  Proof.
    rewrite /is_stack_pre => n f f' Hf v.
    repeat (f_contractive || f_equiv).
    apply Hf.
  Qed.

  Definition is_stack_def (P : val -> iProp Σ) := fixpoint (is_stack_pre P).
  Definition is_stack_aux P : seal (@is_stack_def P). by eexists. Qed.
  Definition is_stack P := unseal (is_stack_aux P).
  Definition is_stack_eq P : @is_stack P = @is_stack_def P := seal_eq (is_stack_aux P).

  Definition stack_inv P v :=
    ( l v', v = #l  l  v'  is_stack P v')%I.

  Lemma is_stack_unfold (P : val  iProp Σ) v :
      is_stack P v  is_stack_pre P (is_stack P) v.
  Proof.
    rewrite is_stack_eq. apply (fixpoint_unfold (is_stack_pre P)).
  Qed.

  Lemma is_stack_disj (P : val  iProp Σ) v :
      is_stack P v - is_stack P v  (v  NONEV   (h t : val), v  SOMEV (h, t)%V).
  Proof.
    iIntros "Hstack".
    iDestruct (is_stack_unfold with "Hstack") as "[#Hstack|Hstack]".
    - iSplit; try iApply is_stack_unfold; iLeft; auto.
    - iDestruct "Hstack" as (h t) "[#Heq rest]".
      iSplitL; try iApply is_stack_unfold; iRight; auto.
  Qed.

272
  (* Per-element invariant (i.e., bag spec). *)
273
  Theorem stack_works {channelG0 : channelG Σ} P Φ :
274
     ( (f f : val),
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
            ( WP f #() {{ v, ( (v' : val), v  SOMEV v'  P v')   v  NONEV }})
         - ( (v : val),  (P v - WP f v {{ v, True }}))
         - Φ (f, f)%V)%I
    - WP mk_stack #()  {{ Φ }}.
  Proof.
    iIntros "HΦ".
    wp_lam.
    wp_bind (mailbox _).
    iApply (mailbox_works P).
    iIntros (put get) "[% [% [#Hput #Hget]]]".
    wp_let; wp_proj; wp_let; wp_proj; wp_let; wp_alloc l' as "Hl'".
    pose proof (nroot .@ "N") as N.
    iMod (inv_alloc N _ (stack_inv P #l') with "[Hl']") as "#Hisstack".
    { iExists l', NONEV; iFrame; iSplit; auto. iApply is_stack_unfold; iLeft; done. }
    wp_let.
    iApply "HΦ".
    (* The verification of pop *)
    - iIntros "!#".
      iLöb as "IH".
      wp_rec;  wp_bind (get _).
      (* Switch from proving WP put #() {{ P }} to
       * Q -∗ P where Q is the spec we have already assumed for P
       *)
      iApply wp_wand; auto.
      iIntros (v) "Hv".
      iDestruct "Hv" as "[H | H]".
      * iDestruct "H" as (v') "[% HP]".
        subst.
        (* This is just some technical fidgetting to get wp_match to behave.
         * It is safe to ignore
         *)
        assert (to_val v' = Some v') by apply to_of_val.
        assert (is_Some (to_val v')) by (exists v'; auto).
        assert (to_val (InjRV v') = Some (InjRV v')) by apply to_of_val.
        assert (is_Some (to_val (InjRV v'))) by (exists (InjRV v'); auto).
        wp_match.
        iLeft; iExists v'; auto.
      * iDestruct "H" as "%"; subst.
        wp_match; wp_bind (! #l')%E.
        iInv N as "Hstack" "Hclose".
        iDestruct "Hstack" as (l'' v'') "[>% [Hl' Hstack]]".
        injection H1; intros; subst.
        wp_load.
        iDestruct (is_stack_disj with "Hstack") as "[Hstack #Heq]".
        iMod ("Hclose" with "[Hl' Hstack]").
        { iExists l'', v''; iFrame; auto. }
        iModIntro.
        iDestruct "Heq" as "[H | H]".
        + iRewrite "H"; wp_match; iRight; auto.
        + iDestruct "H" as (h t) "H"; iRewrite "H".
          (* For technical reasons, wp_match gets confused by this
           * position. Hence the assertions. They can be safely ignored
           *)
          assert (to_val (h, t)%V = Some (h, t)%V) by apply to_of_val.
          assert (is_Some (to_val (h, t)%V)) by (exists (h, t)%V; auto).
          wp_match. fold of_val.
          (* Now back to our regularly scheduled verification *)
          wp_bind (CAS _ _ _); wp_proj.
          iInv N as "Hstack" "Hclose".
          iDestruct "Hstack" as (l''' v''') "[>% [Hl'' Hstack]]".
          injection H4; intros; subst.
          (* Case on whether or not the stack has been updated *)
          destruct (decide (v''' = InjRV (h, t)%V)). 
          ++ (* If nothing has changed, the cas succeeds *)
            wp_cas_suc.
            iDestruct (is_stack_unfold with "Hstack") as "[Hstack | Hstack]".
            subst.
            iDestruct "Hstack" as "%"; discriminate.
            iDestruct "Hstack" as (h' t') "[% [HP Hstack]]".
            subst.
            injection H5.
            intros.
            subst.
            iMod ("Hclose" with "[Hl'' Hstack]").
            { iExists l''', t'; iFrame; auto. }
            iModIntro.
            wp_if; wp_proj; iLeft; auto.
          ++ (* The case in which we fail *)
            wp_cas_fail.
            iMod ("Hclose" with "[Hl'' Hstack]").
            iExists l''', v'''; iFrame; auto.
            iModIntro.
            wp_if.
            (* Now we use our IH to loop *)
            iApply "IH".
    (* The verification of push. This is actually markedly simpler. *)
    - iIntros (v) "!# HP".
      simpl in *.
      fold of_val in *.
      (* We grab an IH to be used in the case that we loop *)
      iLöb as "IH" forall (v).
      wp_rec.
      wp_bind (put _).
      (* Switch from proving WP put #() {{ P }} to
       * Q -∗ P where Q is the spec we have already assumed for put
       *)
      iApply (wp_wand with "[HP]").
      iApply "Hput"; auto.
      iIntros (v') "Hv'".
      iDestruct "Hv'" as "[H | H]".
      * iDestruct "H" as (v'') "[% HP]"; subst.
        wp_match.
        (* Push the substitution through the term *)
        wp_bind (! _)%E.
        iInv N as "Hstack" "Hclose".
        iDestruct "Hstack" as (l'' v') "[>% [Hl' Hstack]]"; simplify_eq.
        wp_load.
        iMod ("Hclose" with "[Hl' Hstack]").
        { by (iExists l'', v'; iFrame). }
        iModIntro.
        wp_let; wp_let; wp_bind (CAS _ _ _).
        iInv N as "Hstack" "Hclose".
        iDestruct "Hstack" as (l''' v''') "[>% [Hl'' Hstack]]"; simplify_eq.
        (* Now we case to see if the stack is unchanged *)
        destruct (decide (v''' = v'%V)).
        + wp_cas_suc.
          iMod ("Hclose" with "[Hl'' HP Hstack]").
          { iExists l''', (InjRV (v'', v')%V).
            iSplit; iFrame; auto.
            iApply is_stack_unfold; iRight.
            iExists v'', v'; iFrame; iSplit; subst; auto. }
          iModIntro.
          wp_if.
          done.
        + wp_cas_fail.
          iMod ("Hclose" with "[Hl'' Hstack]").
          iExists l''', v'''; iFrame; auto.
          iModIntro.
          wp_if.
          iApply ("IH" with "HP").
      * (* This is the case that our sidechannel offer succeeded *)
        iDestruct "H" as "%"; subst.
        wp_match.
        done.
  Qed.
End stack_works.
411 412 413 414 415 416 417

Program Definition is_concurrent_bag `{!heapG Σ, !channelG Σ} : concurrent_bag Σ :=
  {| spec.mk_bag := mk_stack |}.
Next Obligation.
  iIntros (???? P Φ) "_ HΦ". iApply stack_works.
  iNext. iIntros (f f) "Hpop Hpush". iApply "HΦ". iFrame.
Qed.