-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof_hazptr.v
2065 lines (1865 loc) · 86.9 KB
/
proof_hazptr.v
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
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
272
273
274
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From iris.algebra Require Import auth gset.
From iris.base_logic.lib Require Import invariants ghost_map ghost_var.
From smr.base_logic.lib Require Import coP_ghost_map ghost_vars coP_cancellable_invariants.
From smr.program_logic Require Import atomic.
From smr.logic Require Import token2.
From smr.lang Require Import proofmode notation.
From smr Require Import hazptr.spec_hazptr spec_slot_bag_oloc spec_retired_list.
From smr Require Import hazptr.code_hazptr code_slot_bag_oloc code_retired_list.
From smr Require Import helpers logic.reclamation.
From iris.prelude Require Import options.
Set Printing Projections.
Section hazptr.
Context `{!heapGS Σ, !reclamationG Σ} (N : namespace).
Implicit Types (R : resource Σ).
Notation iProp := (iProp Σ).
Context (sbs : slot_bag_spec Σ) (rls : retired_list_spec Σ).
Implicit Types
(γsb γtok γinfo γdata γptrs γU γR γV : gname)
(info : gmap positive (alloc * gname))
(data : gmap positive gname)
(ptrs : gmap blk positive)
(sbvmap : gmap loc (bool * option blk))
(slist : list loc)
(rs : list (blk * nat * nat)).
Local Ltac exfr := (repeat iExists _); iFrame "∗#"; eauto.
Local Ltac tspd := try (iSplit; [done|]); try (iSplit; [|done]).
(*
| | slist | ⊤∖slist |
|--------|-------|---------|
| info | CC | CU |
| ⊤∖info | UC | UU |
*)
Definition CC γtok γinfo γptrs γU γR γV info sbvmap slist : iProp :=
[∗ map] i ↦ info_i ∈ info, ∃ (U : bool),
i ↦p[γU]{ 1/2/2 } U ∗
[∗ list] si ↦ slot ∈ slist, ∃ (b : bool) v (V R : bool),
⌜ sbvmap !! slot = Some (b, v) ⌝ ∗
(i,sid si) ↦p2[γV]{ 1/2 } V ∗
(i,sid si) ↦p2[γR]{ 1/2 } R ∗
⌜ V → b = true ∧ v = Some info_i.1.(addr) ⌝ ∗
⌜ R → U ⌝ ∗
match V, R with
| true, true => False
| true,_ | _,true => emp
| _,_ => stok γtok i si
end
.
Definition CU γtok γinfo γptrs γU γR γV info sbvmap slist : iProp :=
[∗ map] i ↦ info_i ∈ info, ∃ (U R : bool),
i ↦p[γU]{ 1/2/2 } U ∗
({[i]},sids_from (length slist)) ↦P2[γR]{ 1/2 } R ∗
({[i]},sids_from (length slist)) ↦P2[γV] false ∗
(if R then emp else toks γtok {[i]} (sids_from (length slist))) ∗
⌜R → U⌝.
Definition UC γtok γinfo γptrs γU γR γV info sbvmap slist : iProp :=
([∗ list] si ↦ slot ∈ slist, ∃ (b : bool) v,
⌜ sbvmap !! slot = Some (b, v) ⌝) ∗
(⊤ ∖ gset_to_coPset (dom info)) ↦P[γU]{ 1/2 } false ∗
(⊤ ∖ gset_to_coPset (dom info),sids_to (length slist)) ↦P2[γR] false ∗
(⊤ ∖ gset_to_coPset (dom info),sids_to (length slist)) ↦P2[γV]{ 1/2 } false ∗
toks γtok (⊤ ∖ gset_to_coPset (dom info)) (sids_to (length slist)).
Definition UU γtok γinfo γptrs γU γR γV info sbvmap slist : iProp :=
(⊤ ∖ gset_to_coPset (dom info)) ↦P[γU]{ 1/2 } false ∗
(⊤ ∖ gset_to_coPset (dom info),sids_from (length slist)) ↦P2[γR] false ∗
(⊤ ∖ gset_to_coPset (dom info),sids_from (length slist)) ↦P2[γV] false ∗
toks γtok (⊤ ∖ gset_to_coPset (dom info)) (sids_from (length slist)).
Definition GhostQuadrants γtok γinfo γptrs γU γR γV info sbvmap slist : iProp :=
CC γtok γinfo γptrs γU γR γV info sbvmap slist ∗
CU γtok γinfo γptrs γU γR γV info sbvmap slist ∗
UC γtok γinfo γptrs γU γR γV info sbvmap slist ∗
UU γtok γinfo γptrs γU γR γV info sbvmap slist.
Definition InactiveShield γV sbvmap slist : iProp :=
[∗ list] si ↦ slot ∈ slist,
match sbvmap !! slot with
| Some (false, _) => (⊤,{[sid si]}) ↦P2[γV]{ 1/2 } false
| _ => emp
end.
Definition HazardDomain γsb γtok γinfo γdata γptrs γU γR γV (d hBag rSet : loc) : iProp :=
∃ info data ptrs sbvmap slist rs,
ghost_map_auth γinfo 1 info ∗
ghost_map_auth γdata 1 data ∗
coP_ghost_map_auth γptrs 1 ptrs ∗
sbs.(SlotBag) γsb hBag sbvmap slist ∗
rls.(RetiredList) rSet rs ∗
GhostQuadrants γtok γinfo γptrs γU γR γV info sbvmap slist ∗
InactiveShield γV sbvmap slist ∗
([∗ map] p ↦ i ∈ ptrs, ∃ info_i,
⌜info !! i = Some info_i⌝ ∗
(* Permission for freeing the pointer with this length.
This is used for proving [hazard_domain_register]. *)
†(blk_to_loc p)…info_i.1.(len)) ∗
([∗ list] rle ∈ rs, let '(r,len,_) := rle in ∃ i R,
Retired (mgmtN N) (ptrsN N) γtok γinfo γdata γptrs γU γR r i len R) ∗
⌜dom info = dom data⌝ ∗
⌜∀ p i, ptrs !! p = Some i → ∃ z, info !! i = Some z ∧ z.1.(addr) = p ⌝
.
Definition hazptrInvN := mgmtN N .@ "inv".
Definition IsHazardDomain γz (d : loc) : iProp :=
∃ γsb γtok γinfo γdata γptrs γU γR γV (hBag rSet : loc),
⌜γz = encode (γsb, γtok, γinfo, γdata, γptrs, γU, γR, γV)⌝ ∗
(d +ₗ domHBag) ↦□ #hBag ∗
(d +ₗ domRSet) ↦□ #rSet ∗
inv hazptrInvN (HazardDomain γsb γtok γinfo γdata γptrs γU γR γV d hBag rSet).
Global Instance IsHazardDomain_Persistent γz d : Persistent (IsHazardDomain γz d).
Proof. apply _. Qed.
Definition Managed γz (p : blk) (data_i : gname) (size_i : nat) R : iProp :=
∃ γsb γtok γinfo γdata γptrs γU γR γV (i : positive),
⌜γz = encode (γsb, γtok, γinfo, γdata, γptrs, γU, γR, γV)⌝ ∗
i ↪[γdata]□ data_i ∗
ManagedBase (mgmtN N) (ptrsN N) γtok γinfo γptrs γU γR p i size_i (wrap_resource γdata R data_i).
Definition Protected γtok γinfo γdata γptrs γV (s : nat) (i : positive) (p : blk) (data_i : gname) R size_i : iProp :=
i ↪[γdata]□ data_i ∗
ProtectedBase (mgmtN N) (ptrsN N) γtok γinfo γptrs γV s i p (wrap_resource γdata R data_i) size_i.
Definition Shield γz (s : loc) (st : shield_state Σ) : iProp :=
∃ γsb γtok γinfo γdata γptrs γU γR γV
(slot : loc) (idx : nat) (v : option blk),
⌜γz = encode (γsb, γtok, γinfo, γdata, γptrs, γU, γR, γV)⌝ ∗
(s +ₗ shieldSlot) ↦ #slot ∗
†s…shieldSize ∗
sbs.(Slot) γsb slot idx v ∗
match st with
| Deactivated =>
⌜v = None⌝ ∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2 } false
| NotValidated p =>
⌜v = Some p⌝ ∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2 } false
| Validated p data_i R size_i => ∃ i,
⌜v = Some p⌝ ∗
Protected γtok γinfo γdata γptrs γV idx i p data_i R size_i ∗
(⊤ ∖ {[i]},{[sid idx]}) ↦P2[γV]{ 1/2 } false
end.
(* Helper *)
Lemma big_sepL_seq {A} (Φ : nat → iProp) (l : list A) :
([∗ list] i ∈ seq 0 (length l), Φ i) ⊣⊢
([∗ list] i ↦ _ ∈ l, Φ i).
Proof.
induction l as [|x l IH] using rev_ind; auto.
rewrite app_length seq_app; simpl.
do 2 rewrite big_sepL_app. rewrite IH.
do 2 rewrite big_sepL_singleton. rewrite -plus_n_O.
auto.
Qed.
(* Timelessnesses *)
Global Instance GhostQuadrants_timeless γtok γinfo γptrs γU γR γV info sbvmap slist:
Timeless (GhostQuadrants γtok γinfo γptrs γU γR γV info sbvmap slist).
Proof.
repeat (
intros ||
apply bi.sep_timeless || apply bi.exist_timeless ||
apply big_sepM_timeless || apply big_sepL_timeless ||
apply _ || case_match
).
Qed.
Global Instance inactive_shield_timeless γV sbvmap slist :
Timeless (InactiveShield γV sbvmap slist).
Proof. apply big_sepL_timeless; intros; repeat case_match; apply _. Qed.
(* Ghost maintenance lemmas *)
Lemma ghost_quadrants_alloc γinfo γptrs :
⊢ |==> ∃ γtok γU γR γV,
GhostQuadrants γtok γinfo γptrs γU γR γV ∅ ∅ [].
Proof.
iIntros.
iMod (ghost_vars_top_alloc false) as (γU) "[●γUC ●γUU]".
iMod (ghost_vars2_top_alloc false) as (γR) "●γR".
iMod (ghost_vars2_top_alloc false) as (γV) "●γV".
iMod (token2_alloc ⊤ ⊤) as (γtok) "●γtok".
iExists γtok, γU, γR, γV.
unfold GhostQuadrants, CC, CU, UC, UU.
rewrite !big_sepM_empty /= !sids_to_0 !sids_from_0
dom_empty_L gset_to_coPset_empty difference_empty_L.
iFrame.
(* Create empty ghosts *)
do 2 (iMod ghost_vars2_get_empty_2 as "$").
iApply token2_get_empty_2.
Qed.
Lemma ghost_quadrants_register i p γc_i size_p γtok γinfo γptrs γU γR γV info hmap slist :
info !! i = None →
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist ==∗
GhostQuadrants γtok γinfo γptrs γU γR γV (<[i := ({|addr:=p; len:=size_p|}, γc_i)]> info) hmap slist ∗
i ↦p[γU]{1/2} false ∗
({[i]},⊤) ↦P2[γR]{ 1/2 } false ∗
toks γtok {[i]} ∅.
Proof.
iIntros (NotIn) "(●CC & ●CU & ●UC & ●UU)".
(** Move from [UC] to [CC] *)
iAssert (|==> CC _ _ _ _ _ _ _ _ _ ∗ UC _ _ _ _ _ _ _ _ _ ∗ toks γtok {[i]} ∅ ∗
({[i]},sids_to (length slist)) ↦P2[γR]{ 1/2 } false ∗
i ↦p[γU]{1/2/2} false)%I with "[●CC ●UC]" as ">($ & $ & $ & ●Cr & ●Cu)".
{ iDestruct "●UC" as "(#hmap & ●UCu & ●UCr & ●UCv & ●toks)".
rewrite -(top_difference_dom_union_not_in_singleton i info); last done.
rewrite !ghost_vars2_insert_1; [|set_solver..].
rewrite ghost_vars_insert; [|set_solver].
unfold toks.
rewrite token2_insert_1; [|set_solver].
rewrite -gset_to_coPset_singleton -gset_to_coPset_union.
rewrite -(dom_insert_L info i ({|addr:=p; len:=size_p|}, γc_i)).
iDestruct "●UCr" as "[$ [●CCr $]]". iDestruct "●UCv" as "[$ ●CCv]".
iDestruct "●UCu" as "[$ [●CCu $]]". iDestruct "●toks" as "[$ ●toks_i]".
rewrite gset_to_coPset_singleton.
iMod token2_get_empty_2 as "$".
unfold CC. rewrite big_sepM_insert; last by done. iFrame "∗#".
iExists false. iFrame.
iInduction slist as [|si slist] "IH" using rev_ind; [by rewrite !big_sepL_nil|].
rewrite app_length Nat.add_1_r sids_to_S.
rewrite !big_sepL_snoc. iDestruct "hmap" as "[hmap %Hsi]".
rewrite -!ghost_vars2_union_2; [|by apply sids_to_sid_disjoint..].
rewrite -token2_union_2; [|by apply sids_to_sid_disjoint].
unfold stok, toks.
iDestruct "●CCr" as "[●CCr_s ●CCr]". iDestruct "●CCv" as "[●CCv_s ●CCv]".
iDestruct "●toks_i" as "[●toks_i_s ●toks_i]".
iDestruct ("IH" with "hmap ●CCr_s ●CCv_s ●toks_i_s") as ">$". iClear "IH".
destruct Hsi as (b & oloc & ?). repeat iExists _. repeat iFrame.
iPureIntro. split_and!; [by simplify_map_eq|inversion 1..].
}
(** Move from [UU] to [CU] *)
iAssert (CU _ _ _ _ _ _ _ _ _ ∗ UU _ _ _ _ _ _ _ _ _ ∗
({[i]},sids_from (length slist)) ↦P2[γR]{ 1/2 } false ∗
i ↦p[γU]{1/2/2} false)%I with "[●CU ●UU]" as "($ & $ & ●Ur & ●Uu)".
{ unfold CU. rewrite big_sepM_insert; last by done. iFrame.
iDestruct "●UU" as "(●UUu & ●UUr & ●UUv & ●UUt)". unfold toks.
do 2 (rewrite bi.sep_exist_r; iExists _).
rewrite -(top_difference_dom_union_not_in_singleton i info); last by done.
rewrite -ghost_vars_union; [|set_solver].
rewrite -!ghost_vars2_union_1; [|set_solver..].
rewrite -!token2_union_1; [|set_solver..].
rewrite -gset_to_coPset_singleton -gset_to_coPset_union.
rewrite -(dom_insert_L info i ({|addr:=p; len:=size_p|}, γc_i)).
iDestruct "●UUr" as "[$ [$ $]]". iDestruct "●UUv" as "[$ $]".
iDestruct "●UUt" as "[$ $]".
rewrite gset_to_coPset_singleton.
iDestruct "●UUu" as "[$ [$ $]]".
done.
}
iCombine "●Cu ●Uu" as "$". iCombine "●Cr ●Ur" as "●r".
rewrite ghost_vars2_union_2; last apply sids_to_sids_from_disjoint.
rewrite sids_to_sids_from_union. by iFrame.
Qed.
Lemma ghost_quadrants_new_slot slot γtok γinfo γptrs γU γR γV info hmap slist :
slot ∉ slist →
hmap !! slot = None →
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist -∗
GhostQuadrants γtok γinfo γptrs γU γR γV info (<[slot := (true, None)]> hmap) (slist ++ [slot]) ∗
(⊤,{[sid (length slist)]}) ↦P2[γV]{ 1/2 } false.
Proof.
iIntros (NotIn NotElem) "(●CC & ●CU & ●UC & ●UU)".
(** Move from [UU] to [UC] *)
iAssert (UC _ _ _ _ _ _ _ _ _ ∗ UU _ _ _ _ _ _ _ _ _ ∗
(⊤ ∖ gset_to_coPset (dom info),{[sid (length slist)]}) ↦P2[γV]{ 1/2 } false
)%I with "[●UC ●UU]" as "($ & $ & ●Uv)".
{ unfold UC, UU.
iDestruct "●UC" as "(#hmap & $ & ●UCr & ●UCv & ●UCt)".
iDestruct "●UU" as "($ & ●UUr & ●UUv & ●UUt)".
(* Move validation flag for [slot] from [UU] to [UC] *)
iAssert(
(⊤ ∖ gset_to_coPset (dom info),sids_to (length (slist ++ [slot]))) ↦P2[γV]{ 1/2 } false ∗
(⊤ ∖ gset_to_coPset (dom info),sids_from (length (slist ++ [slot]))) ↦P2[γV] false ∗
(⊤ ∖ gset_to_coPset (dom info),{[sid (length slist)]}) ↦P2[γV]{ 1/2 } false
)%I with "[●UCv ●UUv]" as "($ & $ & $)".
{ rewrite app_length /= Nat.add_1_r sids_from_S.
rewrite ghost_vars2_insert_2; [|apply sids_from_not_elem_of; lia].
iDestruct "●UUv" as "[$ [$ ●UUv]]".
iCombine "●UCv ●UUv" as "●Uv".
rewrite -ghost_vars2_insert_2; [|apply sids_to_not_elem_of; lia].
by rewrite sids_to_S.
}
(* Move reclaim flag for [slot] from [UU] to [UC] *)
iAssert(
(⊤ ∖ gset_to_coPset (dom info),sids_to (length (slist ++ [slot]))) ↦P2[γR] false ∗
(⊤ ∖ gset_to_coPset (dom info),sids_from (length (slist ++ [slot]))) ↦P2[γR] false
)%I with "[●UCr ●UUr]" as "[$ $]".
{ rewrite app_length /= Nat.add_1_r sids_from_S.
rewrite ghost_vars2_insert_2; [|apply sids_from_not_elem_of; lia].
iDestruct "●UUr" as "[$ ●UUr]".
iCombine "●UCr ●UUr" as "●Ur".
rewrite -ghost_vars2_insert_2; [|apply sids_to_not_elem_of; lia].
by rewrite sids_to_S.
}
(* Move token for [slot] from [UU] to [UC] *)
iAssert(
toks γtok (⊤ ∖ gset_to_coPset (dom info)) (sids_to (length (slist ++ [slot]))) ∗
toks γtok (⊤ ∖ gset_to_coPset (dom info)) (sids_from (length (slist ++ [slot])))
)%I with "[●UCt ●UUt]" as "[$ $]".
{ rewrite app_length /= Nat.add_1_r sids_from_S.
unfold toks.
rewrite token2_insert_2; [|apply sids_from_not_elem_of; lia].
iDestruct "●UUt" as "[$ ●UUt]".
iCombine "●UCt ●UUt" as "●Ut".
rewrite -token2_insert_2; [|apply sids_to_not_elem_of; lia].
by rewrite sids_to_S.
}
(* Restore Information about [hmap] *)
rewrite big_sepL_snoc. iSplit; last first.
{ repeat iExists _. by simplify_map_eq. }
iApply (big_sepL_mono with "hmap"). iIntros (si p Hsi) "%Hp".
iPureIntro. destruct Hp as [? [? ?]]. repeat eexists.
rewrite lookup_insert_ne; [done|intros <-; congruence].
}
(** Move from [CU] to [CC] *)
rewrite -assoc.
iInduction info as [|loc v info] "IH" using map_ind.
{ unfold CC, CU. rewrite dom_empty_L gset_to_coPset_empty difference_empty_L !big_sepM_empty. iFrame. }
iDestruct (big_sepM_delete with "●CC") as "[●CCloc ●CC]"; [by simplify_map_eq|].
rewrite delete_insert; [|done].
iDestruct (big_sepM_delete with "●CU") as "[●CUloc ●CU]"; [by simplify_map_eq|].
rewrite delete_insert; [|done].
iSpecialize ("IH" with "●CC ●CU").
iDestruct "●CCloc" as (U) "[●loc↦pC ●CCloc]".
iDestruct "●CUloc" as (? R) "(●loc↦pU & ●CUr & ●CUv & ●CUt & %CUru)".
iDestruct (ghost_vars_agree with "●loc↦pC ●loc↦pU") as %<-; [set_solver|].
iAssert(
({[loc]},sids_from (length (slist ++ [slot]))) ↦P2[γV] false ∗
(loc,sid (length slist)) ↦p2[γV]{ 1/2 } false ∗
(loc,sid (length slist)) ↦p2[γV]{ 1/2 } false
)%I with "[●CUv]" as "(●CUv & ●v & ●CCv)".
{ rewrite app_length /= Nat.add_1_r sids_from_S.
rewrite ghost_vars2_insert_2; [|apply sids_from_not_elem_of; lia].
iDestruct "●CUv" as "[$ [$ $]]".
}
iCombine "●Uv ●v" as "●Uv".
rewrite dom_insert_L.
rewrite -ghost_vars2_insert_1; last first.
{ rewrite gset_to_coPset_union gset_to_coPset_singleton. set_solver. }
rewrite gset_to_coPset_union gset_to_coPset_singleton.
rewrite top_difference_dom_union_not_in_singleton; last done.
iDestruct ("IH" with "●Uv") as "(●CC & ●CU & $)".
iEval (rewrite sids_from_S) in "●CUr".
rewrite ghost_vars2_insert_2; last by apply sids_from_not_elem_of; lia.
iDestruct "●CUr" as "[●CUr ●CCr]".
iAssert (
(if R then emp else toks γtok {[loc]} ({[sid (length slist)]})) ∗
if R then emp else toks γtok {[loc]} (sids_from (length (slist ++ [slot])))
)%I with "[●CUt]" as "[●CCt ●CUt]".
{ iEval (rewrite sids_from_S) in "●CUt".
destruct R; [done|]. unfold toks.
rewrite app_length Nat.add_1_r.
rewrite token2_insert_2; last by apply sids_from_not_elem_of; lia.
iDestruct "●CUt" as "[$ $]".
}
iSplitR "●loc↦pU ●CUt ●CU ●CUv ●CUr".
- unfold CC. rewrite big_sepM_insert; last by done.
iSplitR "●CC"; [|iFrame].
iExists U. iFrame "∗#%". rewrite big_sepL_snoc.
iSplitL "●CCloc"; last first.
+ repeat iExists _. simplify_map_eq. iSplit; [done|].
iFrame. iFrame "●CCt". iPureIntro. done.
+ iApply (big_sepL_mono with "●CCloc"). iIntros (i slot' Hslot') "●loc".
iDestruct "●loc" as (?? V R') "(%&?&?&%F&%&?)".
destruct (decide (slot' = slot)) as [->|NE].
* iExists true,None. exfr. iPureIntro. simplify_map_eq.
* exfr. iPureIntro. rewrite lookup_insert_ne; done.
- unfold CU. rewrite big_sepM_insert; last by done.
iSplitR "●CU"; [|iFrame].
iExists U, R. iFrame "∗#%".
by rewrite app_length /= Nat.add_1_r.
Qed.
Lemma ghost_quadrants_activate slot v γtok γinfo γptrs γU γR γV info hmap slist :
slot ∈ slist →
NoDup slist →
hmap !! slot = Some (false, None) →
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist -∗
GhostQuadrants γtok γinfo γptrs γU γR γV info (<[slot:=(true, v)]> hmap) slist.
Proof.
iIntros (ElemOf NoDup Hslot) "(●CC & $ & (#hmap & $ & $) & $)".
iSplit; last first.
{ iApply (big_sepL_mono with "hmap"). iIntros (si p Hsi) "%Hp".
iPureIntro. destruct Hp as [? [? ?]].
destruct (decide (p = slot)) as [->|NE]; repeat eexists; [by simplify_map_eq|].
rewrite lookup_insert_ne; [done|intros <-; congruence]. }
iClear "hmap". unfold CC.
iInduction info as [|loc v' info] "IH" using map_ind; first by rewrite !big_sepM_empty.
rewrite big_sepM_insert; last done.
iDestruct "●CC" as "[●CCloc ●CC]".
iSpecialize ("IH" with "●CC").
rewrite big_sepM_insert; last done.
iSplitR "IH"; last iFrame.
iDestruct "●CCloc" as (U) "[loc↦pC ●CCloc]".
iExists U. iFrame.
iApply (big_sepL_mono with "●CCloc"). iIntros (i slot' Hslot') "●loc".
iDestruct "●loc" as (?? V R) "(%&?&?&%F&%&?)".
destruct (decide (slot' = slot)) as [->|NE].
- exfr. iPureIntro. simplify_map_eq. split_and!; [done| |done].
intro. destruct F as [_ F]; [done|inversion F].
- exfr. iPureIntro. rewrite lookup_insert_ne; [|done].
split_and!; [by simplify_map_eq|done..].
Qed.
Lemma ghost_quadrants_validate E γtok γinfo γptrs γU γR γV
info hmap slist i idx slot z :
info !! i = Some z →
slist !! idx = Some slot →
hmap !! slot = Some (true, Some z.1.(addr)) →
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist -∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2 } false -∗
(i,sid idx) ↦p2[γR]{ 1/2 } false -∗
|={E}=>
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist ∗
(⊤ ∖ {[i]},{[sid idx]}) ↦P2[γV]{ 1/2 } false ∗
(i,sid idx) ↦p2[γV]{ 1/2 } true ∗
(i,sid idx) ↦p2[γR]{ 1/2 } false ∗
stok γtok i idx.
Proof.
iIntros (Hi Hl Hm) "(●CC & ●CU & ●UC & ●UU) ●TI R".
iDestruct (big_sepM_delete with "●CC") as "[●Ci ●CC]"; eauto.
iDestruct "●Ci" as (U) "[U ●Ci]".
iDestruct (big_sepL_delete with "●Ci") as "[●Cis ●Ci]"; eauto.
iDestruct "●Cis" as (b v V R) "(%Hm' & ●II & R2 & %HV & %HRU & T)".
rewrite Hm in Hm'. injection Hm' as [= <- <-].
(* agree & update *)
iDestruct (ghost_vars2_agree with "R R2") as %<-; [set_solver..|].
iDestruct (ghost_vars2_delete_1 _ i with "●TI") as "[●II2 ●TxI]"; [done|].
iDestruct (ghost_vars2_agree with "●II ●II2") as %->; [set_solver..|].
iMod (ghost_vars2_update_halves' true with "●II ●II2") as "[●II ●II2]".
(* close *)
iModIntro. iFrame. unfold CC.
iApply big_sepM_delete; eauto. do 2 exfr.
iApply big_sepL_delete; eauto. do 2 exfr.
Qed.
Lemma ghost_quadrants_set idx slot v b v' b' γtok γinfo γptrs γU γR γV info hmap slist :
NoDup slist →
slist !! idx = Some slot →
hmap !! slot = Some (b', v') →
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist -∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2 } false -∗
GhostQuadrants γtok γinfo γptrs γU γR γV info (<[slot:=(b, v)]> hmap) slist ∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2 } false.
Proof.
iIntros (NoDup Hidx Hslot) "(●CC & $ & (#hmap & $ & $) & $) ●v".
rewrite -assoc bi.sep_comm -assoc.
iSplit.
{ iApply (big_sepL_mono with "hmap"). iIntros (si p Hsi) "%Hp".
iPureIntro. destruct Hp as [? [? ?]].
destruct (decide (p = slot)) as [->|NE]; repeat eexists; [by simplify_map_eq|].
rewrite lookup_insert_ne; [done|intros <-; congruence]. }
iClear "hmap". unfold CC.
iInduction info as [|i info_i info] "IH" using map_ind.
{ rewrite !big_sepM_empty. iFrame. }
rewrite big_sepM_insert; last done.
iDestruct "●CC" as "[●CCloc ●CC]".
iSpecialize ("IH" with "●CC ●v").
iDestruct "IH" as "[●v IH]".
rewrite big_sepM_insert; last done.
rewrite bi.sep_comm -!assoc.
iDestruct "●CCloc" as (U) "[●loc↦pC ●CCloc]".
rewrite bi.sep_exist_r. iExists U. iFrame "●loc↦pC IH".
iInduction slist as [|slot' slist] "IH" using rev_ind.
{ rewrite !big_sepL_nil. iFrame. }
rewrite -NoDup_snoc in NoDup. destruct NoDup as [NoDup NotIn].
destruct (decide (slot' = slot)) as [->|NE].
- iClear "IH". rewrite !big_sepL_snoc.
iDestruct "●CCloc" as "[●CCloc ●loc_slot]".
rewrite -assoc. iSplitL "●CCloc".
{ iApply (big_sepL_mono with "●CCloc"). iIntros (idx' slot' Hslot') "●CCloc".
iDestruct "●CCloc" as (b'' oloc V R) "(%&?&?&%&%&?)".
exfr. iPureIntro. rewrite lookup_insert_ne; last first.
{ intros <-. apply NotIn. rewrite elem_of_list_lookup. eauto. }
by simplify_map_eq.
}
iDestruct "●loc_slot" as (b'' oloc V R) "(%&●iv&●ir&%&%&IF)".
assert (idx = (length slist)) as ->.
{ apply lookup_lt_Some in Hidx as LE. rewrite app_length /= in LE.
assert (¬ (idx < (length slist))).
{ intro LT. rewrite lookup_app_l in Hidx; last done.
apply NotIn. rewrite elem_of_list_lookup. eauto. }
lia.
}
iDestruct (ghost_vars2_agree with "●iv ●v") as %->; [set_solver..|].
iFrame. iExists b,v,false,R. iFrame "∗#%".
iPureIntro. split; [by simplify_map_eq|inversion 1].
- apply lookup_snoc_ne in Hidx; last done.
rewrite !big_sepL_snoc.
iDestruct "●CCloc" as "[●CCloc ●loc_slot]".
iSpecialize ("IH" $! NoDup Hidx with "●CCloc ●v").
iDestruct "IH" as "[$ $]".
iDestruct "●loc_slot" as (b'' oloc V R) "(%&?&?&%&%&?)".
iExists b'',oloc,V,R. iFrame "∗#%".
iPureIntro. rewrite lookup_insert_ne; [by simplify_map_eq|done].
Qed.
Lemma ghost_quadrants_invalidate E γtok γinfo γptrs γU γR γV info hmap slist idx slot i info_i :
info !! i = Some info_i →
slist !! idx = Some slot →
hmap !! slot = Some (true, Some info_i.1.(addr)) →
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist -∗
(i,sid idx) ↦p2[γV]{ 1/2 } true -∗
stok γtok i idx -∗
|={E}=>
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist ∗
(i,sid idx) ↦p2[γV]{ 1/2 } false.
Proof.
iIntros (Hi Hidx Hslot) "[●CC $] ●v T". unfold CC.
iDestruct (big_sepM_delete with "●CC") as "[●Ci ●CC]"; eauto.
iDestruct "●Ci" as (U) "[U ●Ci]".
iDestruct (big_sepL_delete with "●Ci") as "[●Cis ●Ci]"; eauto.
iDestruct "●Cis" as (????) "(%Hslot' & ●iv & ●R2 & %HV & %HRU & OT)".
rewrite Hslot in Hslot'. injection Hslot' as [= <- <-].
(* agree & update *)
iDestruct (ghost_vars2_agree with "●v ●iv") as %<-; [set_solver..|].
iMod (ghost_vars2_update_halves' false with "●v ●iv") as "[●v ●iv]".
(* close *)
iModIntro. iFrame.
iApply big_sepM_delete; eauto. do 2 exfr.
iApply big_sepL_delete; eauto. do 2 exfr.
destruct R; [iAssumption|].
iFrame. iPureIntro. split_and!; [by simplify_map_eq|inversion 1..].
Qed.
Lemma ghost_quadrants_retire E γtok γinfo γptrs γU γR γV
info hmap slist i :
NoDup slist →
is_Some (info !! i) →
({[i]},⊤) ↦P2[γR]{ 1/2 } false -∗
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist -∗
i ↦p[γU]{1/2} false -∗
|={E}=>
({[i]},⊤) ↦P2[γR]{ 1/2 } false ∗
GhostQuadrants γtok γinfo γptrs γU γR γV info hmap slist ∗
i ↦p[γU]{1/2} true.
Proof.
iIntros (ND [info_i Hinfo_i]) "●IT (●CC & ●CU & ●UC & ●UU) U".
iDestruct (big_sepM_delete with "●CC") as "[CCi ●CC]"; eauto.
iDestruct "CCi" as (U) "[U2 CCi]".
iDestruct (big_sepM_delete with "●CU") as "[CUi ●CU]"; eauto.
iDestruct "CUi" as (U' R) "[U3 CUi]".
iDestruct (ghost_vars_agree with "U U2") as %<-; [set_solver|].
iDestruct (ghost_vars_agree with "U U3") as %<-; [set_solver|].
iCombine "U U2 U3" as "U".
iMod (ghost_vars_update true with "U") as "[U [U2 U3]]".
iModIntro.
iFrame "●UC ●UU U".
rewrite (top_union_difference (sids_to (length slist))).
iDestruct (ghost_vars2_union_2 with "●IT") as "[●IE ●IL]"; first set_solver.
iApply bi.wand_frame_r.
{ iIntros. iApply ghost_vars2_union_2; first set_solver. eauto. }
assert ( ∀ (P Q R S : iProp),
((Q ∗ R) ∗ (P ∗ S)) ⊢ ((P ∗ Q) ∗ R ∗ S)
) as COMM.
{ iIntros (????) "([$ $]&$&$)". }
iApply COMM.
iSplitL "U2 ●CC CCi ●IL".
- destruct (decide (slist = [])) as [->|NonNill].
{ unfold CC; simpl. exfr.
iApply big_sepM_delete; eauto. exfr. }
iDestruct (ghost_vars2_big_sepM_2 with "●IL") as "●IL".
{ apply NoDup_fmap; [apply sid_injective|apply NoDup_seq]. }
iApply bi.wand_frame_r.
{ iApply ghost_vars2_big_sepM_2'.
- apply NoDup_fmap; [apply sid_injective|apply NoDup_seq].
- intro sids. apply fmap_nil_inv, seq_nil_inv in sids.
by destruct slist. }
iApply bi.wand_frame_l.
{ iIntros. iApply big_sepM_delete; eauto. }
iFrame "●CC".
iApply bi.sep_exist_l. iExists true. iFrame "U2".
rewrite big_sepL_fmap big_sepL_seq.
iCombine "CCi ●IL" as "CC". do 2 rewrite -big_sepL_sep.
iApply big_sepL_mono; last auto.
iIntros (i' p' Hp) "[CCi R]".
iDestruct "CCi" as (????) "(% & ● & R2 & % & % & T)".
iDestruct (ghost_vars2_agree with "R R2") as %<-; [set_solver..|].
repeat exfr.
- exfr.
iApply big_sepM_delete; eauto. repeat exfr.
iDestruct "CUi" as "(C1 & C2 & C3 & %)". exfr.
Qed.
(* Inactive shield lemmas *)
Lemma inactive_shield_alloc γV :
⊢ InactiveShield γV ∅ [].
Proof.
iIntros.
unfold InactiveShield.
by rewrite big_sepL_nil.
Qed.
Lemma inactive_shield_insert γV sbvmap slist slot :
sbvmap !! slot = None →
InactiveShield γV sbvmap slist -∗
InactiveShield γV (<[slot:=(true, None)]> sbvmap) (slist ++ [slot]).
Proof.
iIntros "%Hslot ISh".
iApply big_sepL_snoc. iSplitL "ISh".
- iApply big_sepL_mono; last auto.
iIntros (i p Hp) "SS".
destruct (decide (p = slot)) as [->|NE].
+ by rewrite lookup_insert.
+ by rewrite lookup_insert_ne.
- by rewrite lookup_insert.
Qed.
Lemma inactive_shield_unset γV sbvmap slist si slot v :
NoDup slist →
slist !! si = Some slot →
sbvmap !! slot = Some (true, v) →
InactiveShield γV sbvmap slist -∗
InactiveShield γV (<[slot:=(true, None)]> sbvmap) slist.
Proof.
iIntros "%NoDup %HL %HM ISh".
unfold InactiveShield.
iDestruct (big_sepL_delete with "ISh") as "[ISlot ISh]"; first apply HL.
iApply big_sepL_delete; [apply HL|].
iSplitR "ISh"; last first.
{ iApply (big_sepL_mono with "ISh"). iIntros (si' slot' Hsi) "Sh".
case_decide as Eqn; auto. rewrite lookup_insert_ne; [iFrame|].
intros <-. destruct Eqn. rewrite NoDup_ListNoDup NoDup_nth in NoDup.
apply (NoDup si' si); [by apply (lookup_lt_Some _ _ slot)..|].
apply (nth_lookup_Some _ _ slot _) in HL.
apply (nth_lookup_Some _ _ slot _) in Hsi.
rewrite -{2}HL in Hsi. done.
}
by rewrite lookup_insert.
Qed.
Lemma inactive_shield_drop γV sbvmap slist si slot v :
NoDup slist →
slist !! si = Some slot →
sbvmap !! slot = Some (true, v) →
InactiveShield γV sbvmap slist -∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2 } false -∗
InactiveShield γV (<[slot:=(false, None)]> sbvmap) slist.
Proof.
iIntros "%NoDup %HL %HM ISh ●si".
unfold InactiveShield.
iDestruct (big_sepL_delete with "ISh") as "[ISlot ISh]"; first apply HL.
iApply big_sepL_delete; [apply HL|].
iSplitR "ISh"; last first.
{ iApply (big_sepL_mono with "ISh"). iIntros (si' slot' Hsi) "Sh".
case_decide as Eqn; auto. rewrite lookup_insert_ne; [iFrame|].
intros <-. destruct Eqn. rewrite NoDup_ListNoDup NoDup_nth in NoDup.
apply (NoDup si' si); [by apply (lookup_lt_Some _ _ slot)..|].
apply (nth_lookup_Some _ _ slot _) in HL.
apply (nth_lookup_Some _ _ slot _) in Hsi.
rewrite -{2}HL in Hsi. done.
}
by rewrite lookup_insert.
Qed.
Lemma inactive_shield_activate γV sbvmap slist si slot v :
slist !! si = Some slot →
sbvmap !! slot = Some (false, None) →
InactiveShield γV sbvmap slist -∗
InactiveShield γV (<[slot:=(true, v)]> sbvmap) slist ∗
(⊤,{[sid si]}) ↦P2[γV]{ 1/2 } false.
Proof.
iIntros "%HL %HM ISh".
iDestruct (big_sepL_delete with "ISh") as "[ISlot ISh]"; first apply HL.
rewrite HM. iFrame. iApply big_sepL_mono; last auto.
iIntros (k y Hky) "SS".
destruct (decide (y = slot)).
- subst. by rewrite lookup_insert.
- rewrite lookup_insert_ne; auto.
destruct (decide (k = si)); auto.
subst. rewrite HL in Hky. naive_solver.
Qed.
Lemma inactive_shield_reactivate v γV sbvmap slist si slot v' :
slist !! si = Some slot →
sbvmap !! slot = Some (true, v') →
InactiveShield γV sbvmap slist -∗
InactiveShield γV (<[slot:=(true, v)]> sbvmap) slist.
Proof.
iIntros "%HL %HM ISh".
iApply big_sepL_mono; last auto.
iIntros (k y Hky) "SS".
destruct (decide (y = slot)).
- subst. by rewrite lookup_insert.
- rewrite lookup_insert_ne; auto.
Qed.
(** Domain specs *)
Lemma hazard_domain_new_spec :
hazard_domain_new_spec' N hazard_domain_new IsHazardDomain.
Proof.
intros ??.
iIntros "_ HΦ".
wp_lam. wp_alloc d as "d↦" "†d".
rewrite array_cons array_singleton.
iDestruct "d↦" as "[d.s↦ d.r↦]".
wp_let.
wp_apply sbs.(slot_bag_new_spec); auto.
iIntros (γsb slotbag) "SlotBag".
wp_pures. rewrite loc_add_0. wp_store.
wp_apply rls.(retired_list_new_spec); auto.
iIntros (rList) "RList".
wp_store.
(* Prepare rest of ghosts. *)
iMod (ghost_map_alloc_empty (K:=positive) (V:=alloc*gname)) as (γinfo) "●γinfo".
iMod (ghost_map_alloc_empty (K:=positive) (V:=gname)) as (γdata) "●γdata".
iMod coP_ghost_map_alloc_empty as (γptrs) "●γptrs".
iMod (ghost_quadrants_alloc γinfo γptrs) as (γtok γU γR γV) "GQ".
iDestruct (inactive_shield_alloc γV) as "InactSh".
remember (encode (γsb, γtok, γinfo, γdata, γptrs, γU, γR, γV)) as γz eqn:Hγz.
iAssert (HazardDomain _ _ _ _ _ _ _ _ _ _ _)%I with "[SlotBag RList ●γinfo ●γdata ●γptrs GQ]" as "HD".
{ repeat iExists _. iFrame "∗#". rewrite big_sepM_empty big_sepL_nil.
iPureIntro. split_and!; [done..|by rewrite !dom_empty_L|]. intros l i Hli.
rewrite lookup_empty in Hli. congruence. }
iMod (inv_alloc hazptrInvN _ (HazardDomain _ _ _ _ _ _ _ _ _ _ _) with "HD") as "#HDInv".
iMod (mapsto_persist with "d.s↦") as "#d.s↦".
iMod (mapsto_persist with "d.r↦") as "#d.r↦".
iModIntro. iApply "HΦ".
repeat iExists _. rewrite loc_add_0. iFrame "∗#%".
Qed.
Lemma hazard_domain_register :
hazard_domain_register' N IsHazardDomain Managed.
Proof.
intros ????????. intros.
iIntros "#IHD (p↦ & †p & R)".
iDestruct "IHD" as (??????????) "(%Hγ & ↦hBag & ↦rSet & InvHD)".
iInv "InvHD" as (??? hmap slist rs)
"(>M & >datM & >coM & >B & >RS & >●Q & >●X & >Reg & Ret & >%Hdom & >%HInfo)".
(* Get a new id for p *)
set i := fresh (dom info).
assert (info !! i = None) as Freshi.
{ rewrite eq_None_not_Some -elem_of_dom. apply is_fresh. }
(* Prove that [p ∉ dom ptrs] using two freeables, one in [Reg] and [†p]. *)
iAssert (⌜p ∉ dom ptrs⌝)%I as %PNew.
{ iIntros (In). rewrite elem_of_dom in In.
destruct In as [idx Some].
rewrite big_sepM_lookup_acc; last done.
iDestruct "Reg" as "[Reg_p _]".
iDestruct "Reg_p" as (?) "(_ & †p')".
iApply (heap_freeable_valid with "†p †p'").
}
rewrite elem_of_dom -eq_None_not_Some in PNew.
(* get [p] element from [coM] and split. *)
iMod ((coP_ghost_map_insert p i) with "coM") as "[coM p↪i]"; first by apply PNew.
rewrite (top_complement_singleton gid).
rewrite coP_ghost_map_elem_fractional; last first.
{ rewrite -coPneset_disj_elem_of. set_solver. }
iDestruct "p↪i" as "[p↪i_gid p↪i]".
(* Allocate and split [coP_cinv]. *)
iMod (ghost_map_insert_persist i γ_p with "datM") as "[datM #i↪γdata]".
{ rewrite -not_elem_of_dom -Hdom not_elem_of_dom //. }
iMod ((coP_cinv_alloc _ (resN (ptrsN N) p i) (∃ vl, ⌜length vl = length lv⌝ ∗ (wrap_resource γdata R γ_p) _ vl _ ∗ p ↦∗ vl)) with "[R p↦]") as "●γc_i".
{ iNext. iExists lv. iFrame "∗#". auto. }
iDestruct "●γc_i" as (γc_i) "[#Inv ●γc_i]".
rewrite (top_complement_singleton gid).
rewrite coP_cinv_own_fractional; last first.
{ rewrite -coPneset_disj_elem_of. set_solver. }
iDestruct "●γc_i" as "[●γc_i_gid ●γc_i]".
(* Update ghost quadrants *)
iMod (ghost_quadrants_register with "●Q") as "(●Q & ●u_i & ●r_i & ●toks_∅)"; [done|].
(* Update [M] *)
iMod (ghost_map_insert_persist i ({|addr:=p; len:=length lv|},γc_i) with "M") as "[M #i↪γinfo]"; [done|].
(* Make [Exchanges'] *)
iAssert (Exchanges' _ _ _ _ _)%I with "[●toks_∅ ●γc_i p↪i]" as "Exchanges'".
{ unfold Exchanges'. iRight. iLeft.
iExists ∅. rewrite union_empty_l_L. rewrite set_map_empty gset_to_coPset_empty. iFrame.
}
(* Make [Exchanges] *)
iMod (inv_alloc (exchN (mgmtN N) p i) _ (Exchanges' _ _ _ _ _) with "Exchanges'") as "Exchanges".
(* Close invariant *)
iModIntro. iSplitR "i↪γinfo p↪i_gid ●γc_i_gid ●u_i ●r_i Exchanges".
{ iNext. repeat iExists _. iFrame. iSplit; last first.
{ iPureIntro. split.
{ rewrite !dom_insert_L Hdom //. }
intros p' i' Hp'. destruct (decide (p = p')) as [<-|NE].
- eexists. by simplify_map_eq.
- rewrite lookup_insert_ne in Hp'; last done.
specialize (HInfo p' i' Hp'). destruct HInfo as [info_i' [? ?]].
exists info_i'.
rewrite lookup_insert_ne; [done|congruence].
}
rewrite big_sepM_insert; last done.
iSplitR "Reg".
(* Coq is not smart enough to do partial resolution of evars,
so we need to give it two evars as a pair to work with. *)
- iExists ({|addr:=p; len:=length lv|},_). iFrame "†p". by simplify_map_eq.
- iApply (big_sepM_mono with "Reg"). iIntros (p' i' Hp') "Reg".
iDestruct "Reg" as (info_i') "[%Hi' †p']". iExists info_i'. iFrame.
iPureIntro. rewrite lookup_insert_ne; [auto|congruence].
}
(* Make managed *)
iModIntro.
repeat iExists _. iFrame (Hγ) "i↪γdata".
repeat iExists _. iFrame "∗#%".
Qed.
(** Shield specs *)
Lemma shield_new_spec :
shield_new_spec' N shield_new IsHazardDomain Shield.
Proof.
intros ????.
iIntros "#IHD" (Φ) "!> _ HΦ".
iDestruct "IHD" as (??????????) "(%Hγ & ↦hBag & ↦rSet & IHD)".
wp_lam. wp_pures. wp_load. wp_let.
awp_apply sbs.(slot_bag_acquire_slot_spec) without "HΦ".
iInv "IHD" as (??? hmap slist rs)
"(>M & >datM & >coM & >B & >RS & >●Q & >●X & >Reg & Ret & >%Hdom & >%HInfo)".
iAaccIntro with "B".
{ iIntros "? !>". tspd. exfr. }
iIntros (slist' slot idx) "(B & S & %Hslist')".
destruct Hslist' as [[-> [Hm ->]]|[-> [Hm Hl]]].
- (* new slot added *)
iModIntro.
iDestruct (SlotBag_NoDup with "B") as "%ND".
iDestruct (ghost_quadrants_new_slot slot with "●Q") as "[●Q ●TS]"; [|done|].
{ rewrite -NoDup_snoc in ND. by destruct ND. }
iDestruct (inactive_shield_insert with "●X") as "●X"; eauto.
iSplitR "●TS S"; [exfr|].
iIntros "_ HΦ". wp_pures.
wp_alloc shield as "Sh" "†shield". wp_pures.
rewrite -{1}(loc_add_0 shield) array_singleton.
wp_store.
iModIntro. iApply "HΦ". exfr.
- (* old slot reacquired *)
iModIntro.
iDestruct (SlotBag_NoDup with "B") as "%ND".
iDestruct (ghost_quadrants_activate with "●Q") as "●Q"; eauto.
{ rewrite elem_of_list_lookup. eauto. }
iDestruct (inactive_shield_activate with "●X") as "[●X ●TI]"; eauto.
iSplitR "S ●TI"; [exfr|].
iIntros "_ HΦ". wp_pures.
wp_alloc shield as "Sh" "†shield".
wp_pures.
rewrite -{1}(loc_add_0 shield) array_singleton.
wp_store.
iModIntro. iApply "HΦ". unfold Shield.
repeat iExists _. iFrame. iSplit; auto.
Qed.
Lemma shield_set_spec :
shield_set_spec' N shield_set IsHazardDomain Shield.
Proof.
intros ???????.
iIntros "#IHD". iIntros (Φ) "!> Sh HΦ".
iDestruct "IHD" as (??????????) "(%Hγ & ↦hBag & ↦rSet & IHD)".
iDestruct "Sh" as (????????) "Sh".
iDestruct "Sh" as (slot idx v) "(%Hγ' & Shs↦ & †Sh & ShSlot & ShSt)".
encode_agree Hγ'.
wp_lam. wp_pures. wp_load. wp_let.
iAssert ((s +ₗ 0) ↦ #slot -∗ †s…shieldSize -∗
sbs.(Slot) γsb slot idx v -∗
(⊤,{[sid idx]}) ↦P2[γV]{ 1/2 } false -∗
(Shield γd s
match p with
| Some p' => NotValidated p'
| None => Deactivated
end -∗ Φ #()) -∗
WP slot_set #slot #(oblk_to_lit p) @ E {{ v, Φ v }}
)%I as "slot_set".
{ iIntros "Shs↦ †Sh ShSlot ●TI HΦ".
awp_apply (sbs.(slot_set_spec) with "ShSlot") without "HΦ".
iInv "IHD" as (??? hmap1 slist1 rs1)
"(>M & >datM & >coM & >B & >RS & >●Q & >●X & >Reg & Ret & >%Hdom & >%HInfo)".
iAaccIntro with "B".
{ iIntros "? !>". do 2 exfr. }
iIntros "([%Hl %Hm] & B & ShSlot)".
iDestruct (SlotBag_NoDup with "B") as "%ND".
iModIntro.
iDestruct (ghost_quadrants_set idx slot p with "●Q ●TI") as "[●Q ●TI]"; [done..|].
iSplitR "Shs↦ †Sh ShSlot ●TI"; last first.
{ iIntros "_ HΦ". iApply "HΦ". exfr. iSplit; auto. destruct p; auto. }
iNext. exfr. iSplit; auto.
iApply inactive_shield_reactivate; done.
}
destruct s_st as [|p'|p']; simpl.
- (* was Deactivated *)
iDestruct "ShSt" as "[-> ●TI]".
iApply ("slot_set" with "Shs↦ †Sh ShSlot ●TI HΦ").
- (* was NotValidated *)
iDestruct "ShSt" as "[-> ●TI]".
iApply ("slot_set" with "Shs↦ †Sh ShSlot ●TI HΦ").
- (* was Validated *)
iDestruct "ShSt" as (i) "(-> & Prot & ●ShX)".
awp_apply (sbs.(slot_set_spec) with "ShSlot") without "HΦ".
iInv "IHD" as (??? hmap1 slist1 rs1)
"(>M & >datM & >coM & >B & >RS & >●Q & >●X & >Reg & Ret & >%Hdom & >%HInfo)".
iAaccIntro with "B".
{ iIntros "? !>". do 2 exfr. }
iIntros "([%Hl %Hm] & B & ShSlot)".
iDestruct (SlotBag_NoDup with "B") as "%ND".
iDestruct "Prot" as "[#i_data Prot]".
iDestruct "Prot" as (γc_i) "(#i□ & pc & cinv & #Ex & #RCI & ●Shi)".
iDestruct (coP_ghost_map_lookup with "coM pc") as %Hp.
apply HInfo in Hp as [info_i [Hinfo_i info_i_eq]].
iMod (exchange_stok_get with "Ex pc cinv") as "T"; first solve_ndisj.
iMod (ghost_quadrants_invalidate with "●Q ●Shi T") as "[●Q ●Shi]"; [done..|subst p'; done|].
iModIntro.
iDestruct (ghost_quadrants_set idx slot p with "●Q [●Shi ●ShX]") as "[●Q ●ShiX]"; [done..| |].
{ iCombine "●ShX ●Shi" as "●TI". simpl.
rewrite -ghost_vars2_insert_1; last by set_solver.
by rewrite -top_union_difference. }
(* Get the resource for old validated pointer *)
iSplitR "●ShiX Shs↦ †Sh ShSlot"; last first.
{ iIntros "_ HΦ". iApply "HΦ". exfr. iSplit; auto. destruct p; by iFrame. }
iExists info, data, ptrs, _, _, _. iFrame. iNext.
iSplit; [|done].
iApply inactive_shield_reactivate; done.
Qed.
Lemma shield_validate :
shield_validate' N IsHazardDomain Managed Shield.
Proof.
intros ?????????.
iIntros "#IHD G Sh".
iDestruct "IHD" as (??????????) "(%Hγ & ↦hBag & ↦rSet & IHD)".
iDestruct "G" as (?????????) "(%Hγ' & #i_data & G)". encode_agree Hγ'.
iDestruct "G" as (?) "(Gcinv & #G□ & Gpi & #Ex & #Cinv & GU & GR)".
iDestruct "Sh" as (????????) "Sh".
iDestruct "Sh" as (slot idx v) "(%Hγ' & ShH & †Sh & S & Shst)".
iDestruct "Shst" as "[-> ●Sh]".
encode_agree Hγ.
iInv "IHD" as (??? hmap1 slist1 rs1)
"(>M & >datM & >coM & >B & >RS & >●Q & >●X & >Reg & Ret & >%Hdom & >%HInfo)".
iDestruct (SlotBag_lookup with "B S") as "[%Hl %Hm]".
iDestruct (coP_ghost_map_lookup with "coM Gpi") as %Hp.
apply HInfo in Hp as [info_i [Hinfo_i <-]].
iDestruct (ghost_vars2_delete_2 _ (sid idx) with "GR") as "[R Rx]"; [done|].
iMod (ghost_quadrants_validate with "●Q ●Sh R") as
"(●Q & ●TxI & ●II & R & T)"; eauto.
iCombine "R Rx" as "GR".
rewrite -ghost_vars2_delete_2; [|done].
iMod (exchange_stok_give with "Ex T") as "[zc cinv]"; [solve_ndisj|].
iModIntro. iSplitL "M datM coM B RS ●Q ●X Reg Ret"; [exfr|].
iModIntro. iSplitL "Gcinv Gpi GU GR"; do 3 (exfr; tspd).
Qed.