-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocking.v
1525 lines (1509 loc) · 55.7 KB
/
Locking.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
Require Import MetaProp.
Require Import SyntaxProp.
Require Import DynamicProp.
Require Import TypesProp.
Require Import WellFormednessProp.
Require Import Shared.
Hint Constructors is_econtext.
Hint Constructors disjointLocks.
Ltac name_step :=
let Hstep := fresh "Hstep" in
match goal with
| H: step _ _ _ |- _ => rename H into Hstep
| _ : _ |- _ => idtac
end.
Ltac inv_step :=
match goal with
| H: step _ _ _ |- _ => inv H; name_step
| _ : _ |- _ => idtac
end.
Ltac trivial_step_case := abstract(inv_step; try(malformed_context); eauto).
Lemma stepCannotSteal :
forall P H H' V V' n n' T1 T1' T2,
wfLocking H T1 ->
wfLocking H T2 ->
disjointLocks T1 T2 ->
P / (H, V, n, T1) ==> (H', V', n', T1') ->
disjointLocks T1' T2.
Proof with eauto.
introv wfL1 wfL2 Hdisj Hstep.
gen T1'.
induction T1; intros.
+ inv Hstep.
+ gen T1'.
inverts Hdisj as Hdisj1 Hdisj2.
expr_cases (induction e) Case; intros; simpls;
try(trivial_step_case).
- Case "ECall".
apply wfLocking_econtext in wfL1...
inv_step; try(malformed_context); try(inv_eq)...
* econstructor; simpl.
introv HIn.
apply in_app_or in HIn as []...
introv HIn. apply not_in_app...
* apply IHe in Hstep as []...
- Case "EUpdate".
apply wfLocking_econtext in wfL1...
inv_step; try(malformed_context); try(inv_eq)...
* econstructor; simpl.
introv HIn.
apply in_app_or in HIn as []...
introv HIn. apply not_in_app...
* apply IHe in Hstep as []...
- Case "ELet".
apply wfLocking_econtext with (ctx := ctx_let s f e2) in wfL1...
inv_step; try(malformed_context); try(inv_eq)...
* econstructor; simpl.
introv HIn.
apply in_app_or in HIn as []...
introv HIn. apply not_in_app...
* apply IHe1 in Hstep as []...
- Case "ECast".
apply wfLocking_econtext in wfL1...
inv_step; try(malformed_context); try(inv_eq)...
* econstructor; simpl.
introv HIn.
apply in_app_or in HIn as []...
introv HIn. apply not_in_app...
* apply IHe in Hstep as []...
- Case "EPar".
inv_step; try(malformed_context)...
econstructor; simpl...
* introv HIn. apply in_app_or in HIn as []...
* introv HIn. apply not_in_app...
- Case "EWlock".
inv_step; try(malformed_context)...
constructor.
* introv HIn. simpls. inv HIn...
intros contra.
eapply wfHeldLocks_taken in contra...
rewrite_and_invert.
apply wfLocking_wfHeldLocks...
* introv HIn. intros contra.
{inv contra.
+ eapply wfHeldLocks_taken in HIn...
rewrite_and_invert.
apply wfLocking_wfHeldLocks...
+ apply Hdisj1 in HIn...
}
- Case "EWlocked".
apply wfLocking_econtext in wfL1...
inv_step; try(malformed_context); try(inv_eq)...
* econstructor; simpl.
introv HIn.
apply in_app_or in HIn as []...
introv HIn. apply not_in_app...
* apply IHe in Hstep as []...
* {constructor; simpl.
+ introv HIn. apply remove_in in HIn...
+ introv HIn. apply remove_not_in...
}
- Case "ERlocked".
apply wfLocking_econtext in wfL1...
inv_step; try(malformed_context); try(inv_eq)...
* econstructor; simpl.
introv HIn.
apply in_app_or in HIn as []...
introv HIn. apply not_in_app...
* apply IHe in Hstep as []...
+ apply disjointLocks_async in Hdisj as (Hdisj1 & Hdisj2 & Hdisj3).
inv wfL1. inv Hdisj1. inv_step;
try(apply disjointLocks_async);
try(apply disjointLocks_leftmost)...
Qed.
Hint Constructors wfLocking.
Lemma wfLocking_async_preservation :
forall P Gamma H H' V V' n n' T1 T1' T2 t,
wfConfiguration P Gamma (H, V, n, T1) t ->
wfLocking H T2 ->
disjointLocks T1 T2 ->
(forall l r,
In (l, r) (t_rlocks T1 ++ t_rlocks T2) ->
(exists c F RL n,
heapLookup H l = Some (c, F, RL) /\
RL r = Some (LReaders n) /\
length (filter (lock_eq (l, r)) (t_rlocks T1 ++ t_rlocks T2)) <= n)) ->
P / (H, V, n, T1) ==> (H', V', n', T1') ->
wfLocking H' T2.
Proof with eauto using wfLocking_heapExtend, wfLocking_heapUpdate.
introv wfCfg wfL2 Hdisj wfRl Hstep.
assert(wfLs2: wfHeldLocks H (heldLocks T2))
by eauto using wfLocking_wfHeldLocks.
inverts wfLs2 as wfLs2.
assert(wfRl2: wfRLocks H T2)
by eauto using wfLocking_wfRLocks.
inverts wfRl2 as wfRl2.
rewrite Forall_forall in wfLs2.
inverts wfCfg as Hlt wfH wfV wfT wfL.
inverts Hdisj as Hdisj1 Hdisj2.
gen T1' t.
induction T1; intros.
+ inv_step.
+ inverts wfT as Hfree Hfresh hasType.
gen T1'. hasType_cases (induction hasType) Case;
intros; simpls;
try(destruct_var; try(congruence));
try(trivial_step_case).
- Case "T_New".
inv_step; try(malformed_context)...
- Case "T_Call".
inv_step; try(malformed_context)...
inv Hfresh.
apply wfLocking_econtext in wfL...
inv_eq. apply IHhasType in Hstep...
- Case "T_ConsmueField".
inv_step; try(malformed_context)...
eapply wfLocking_heapUpdate...
* introv HIn. apply wfLs2 in HIn...
inv HIn... rewrite_and_invert.
* introv HIn.
apply wfRl2 in HIn as (c' & F' & RL' & n & Hlookup & HRL & Hle)...
rewrite_and_invert...
- Case "T_Update".
inv_step; try(malformed_context)...
inv Hfresh.
apply wfLocking_econtext in wfL...
inv_eq. apply IHhasType2 in Hstep...
eapply wfLocking_heapUpdate...
* introv HIn. apply wfLs2 in HIn...
inv HIn... rewrite_and_invert.
* introv HIn.
apply wfRl2 in HIn as (c' & F' & RL' & n & Hlookup & HRL & Hle)...
rewrite_and_invert...
- Case "T_Let".
inv_step; try(malformed_context)...
inv Hfresh.
apply wfLocking_econtext with (ctx := ctx_let x frame body) in wfL...
apply app_eq_nil in Hfree as [].
inv_eq. apply IHhasType1 in Hstep...
introv HIn. apply in_app_or in HIn...
inv HIn.
* assert (HIn': In (l0, r) ((rlocks e ++ rlocks body) ++ t_rlocks T2))
by eauto using in_or_app...
apply wfRl in HIn' as (c & F & RL & n'' & Hlookup & HRL & Hle)...
exists c F RL n''.
splits...
inverts H0 as _ no_rlocks. rewrite no_rlocks in Hle.
rewrite app_nil_r in Hle...
* assert (HIn': In (l0, r) ((rlocks e ++ rlocks body) ++ t_rlocks T2))
by eauto using in_or_app...
apply wfRl in HIn' as (c & F & RL & n'' & Hlookup & HRL & Hle)...
exists c F RL n''.
splits...
inverts H0 as _ no_rlocks. rewrite no_rlocks in Hle.
rewrite app_nil_r in Hle...
- Case "T_Cast".
inv_step; try(malformed_context)...
inv Hfresh.
apply wfLocking_econtext in wfL...
inv_eq. apply IHhasType in Hstep...
- Case "T_WLock".
inv_step; try(malformed_context)...
eapply wfLocking_heapUpdate...
* introv HIn. apply wfLs2 in HIn...
inv HIn... rewrite_and_invert...
case_extend.
* introv HIn.
pose proof (wfRl2 _ _ HIn) as (c' & F' & RL' & n & Hlookup & HRL & Hle).
apply filter_in with (p := lock_eq) in HIn; eauto using lock_eq_eq.
assert (Hlen: length (filter (lock_eq (l0, r0)) (t_rlocks T2)) > 0)
by (destruct (filter (lock_eq (l0, r0)) (t_rlocks T2)); simpls; try(inv HIn); omega).
rewrite_and_invert.
case_extend...
rewrite_and_invert.
repeat eexists... omega.
- Case "T_RLock".
inv_step; try(malformed_context)...
eapply wfLocking_heapUpdate...
* introv HIn. apply wfLs2 in HIn...
inv HIn... rewrite_and_invert.
case_extend.
* introv HIn. apply wfRl2 in HIn as (c' & F' & RL' & n & Hlookup & HRL & Hle)...
rewrite_and_invert.
case_extend...
rewrite_and_invert.
eexists...
- Case "T_WLocked".
inv Hfresh.
apply wfLocking_econtext in wfL...
inv_step; try(malformed_context); try(inv_eq)...
eapply wfLocking_heapUpdate...
* introv HIn.
{case_extend.
+ apply Hdisj1 in HIn... inv HIn.
+ apply wfLs2 in HIn. inv HIn. rewrite_and_invert.
}
* introv HIn. apply wfRl2 in HIn as (c' & F' & RL' & n & Hlookup & HRL & Hle)...
rewrite_and_invert.
case_extend...
- Case "T_RLocked".
inv Hfresh.
apply wfLocking_econtext in wfL...
inv_step; try(malformed_context); try(inv_eq)...
* apply IHhasType2 in Hstep...
introv HIn.
assert (HIn': (l0, r) = (l1, r0) \/ In (l1, r0) (rlocks e ++ t_rlocks T2))...
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle)...
repeat eexists...
cases_if; simpls;
omega.
* {eapply wfLocking_heapUpdate...
+ introv HIn.
case_extend.
- apply wfLs2 in HIn. inv HIn. rewrite_and_invert.
congruence.
- apply wfLs2 in HIn. inv HIn. rewrite_and_invert.
+ introv HIn.
simpls.
assert(HIn': (l0, r) = (l0, r0) \/ In (l0, r0) (t_rlocks T2))...
apply wfRl in HIn' as (c' & F' & RL' & n & Hlookup & HRL & Hle)...
case_extend.
- rewrite_and_invert. rewrite_and_invert.
rewrite <- beq_nat_refl in Hle.
destruct r0; simpls.
rewrite <- beq_nat_refl in Hle.
exists (pred m). split...
simpls. omega.
- rewrite_and_invert.
exists n. split...
cases_if; simpls;
omega.
}
+ inverts wfT.
inverts wfL.
inv_step...
- eapply IHT1_1; simpls...
* introv HIn.
assert(HIn': In (l, r) ((t_rlocks T1_1 ++ t_rlocks T1_2 ++ rlocks e) ++ t_rlocks T2)).
apply in_app_or in HIn. inv HIn; eauto using in_or_app...
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle)...
repeat eexists...
rewrite filter_app.
rewrite app_length.
rewrite filter_app in Hle.
rewrite filter_app in Hle.
rewrite app_length in Hle.
rewrite app_length in Hle. omega.
* introv HIn.
apply Hdisj1...
apply in_or_app...
* introv HIn.
apply Hdisj2 in HIn.
apply not_in_app in HIn as []...
- eapply IHT1_2; simpls...
* introv HIn.
assert(HIn': In (l, r) ((t_rlocks T1_1 ++ t_rlocks T1_2 ++ rlocks e) ++ t_rlocks T2)).
apply in_app_or in HIn. inv HIn; eauto using in_or_app...
apply in_or_app; left.
apply in_or_app; right.
eauto using in_or_app.
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle)...
repeat eexists...
rewrite filter_app.
rewrite app_length.
rewrite filter_app in Hle.
rewrite filter_app in Hle.
rewrite filter_app in Hle.
rewrite app_length in Hle.
rewrite app_length in Hle.
rewrite app_length in Hle. omega.
* introv HIn.
apply Hdisj1...
apply in_or_app...
* introv HIn.
apply Hdisj2 in HIn.
apply not_in_app in HIn as []...
Qed.
Lemma spawn_no_locks :
forall P Gamma e t H V n Ls e1 e2 e3,
P; Gamma |- e \in t ->
P / (H, V, n, T_Thread Ls e) ==>
(H, V, n, T_Async (T_Thread Ls e1) (T_Thread nil e2) e3) ->
no_locks e1 /\ no_locks e2.
Proof with eauto.
introv hasType Hstep.
gen e1 e2 e3.
hasType_cases (induction hasType) Case; intros;
inv_step; try(malformed_context); try(inv_eq)...
Qed.
Lemma spawn_locks :
forall P Gamma e t H V n Ls e1 e2 e3,
P; Gamma |- e \in t ->
P / (H, V, n, T_Thread Ls e) ==>
(H, V, n, T_Async (T_Thread Ls e1) (T_Thread nil e2) e3) ->
wlocks e = wlocks e3 /\ rlocks e = rlocks e3.
Proof with eauto.
introv hasType Hstep.
gen e1 e2 e3.
hasType_cases (induction hasType) Case; intros;
try(solve[inv_step; malformed_context; simpls; inv_eq; eauto]).
- inv_step; try(malformed_context); simpls...
inv_eq. apply IHhasType1 in Hstep...
inverts H0 as no_wlocks no_rlocks. rewrite no_wlocks. rewrite no_rlocks.
repeat rewrite app_nil_r...
- inv_step; try(malformed_context)...
simpls. inverts H1 as no_wlocks1 no_rlocks1.
inverts H2 as no_wlocks2 no_rlocks2. inverts H3 as no_wlocks3 no_rlocks3.
rewrite no_wlocks1. rewrite no_wlocks2. rewrite no_wlocks3.
rewrite no_rlocks1. rewrite no_rlocks2. rewrite no_rlocks3...
- inv_step; try(malformed_context)...
inv_eq. simpls. apply IHhasType2 in Hstep...
inverts Hstep as Hwlocks Hrlocks.
rewrite Hwlocks. rewrite Hrlocks...
- inv_step; try(malformed_context)...
inv_eq. simpls. apply IHhasType2 in Hstep...
inverts Hstep as Hwlocks Hrlocks.
rewrite Hwlocks. rewrite Hrlocks...
Qed.
Lemma noLostLocks :
forall P Gamma l r H H' V V' n n' T T' t,
In (l, r) (leftmost_locks T) ->
~ In (l, r) (t_wlocks T) ->
wfConfiguration P Gamma (H, V, n, T) t ->
P / (H, V, n, T) ==> (H', V', n', T') ->
In (l, r) (leftmost_locks T').
Proof with eauto.
introv HIn HnIn wfCfg Hstep.
gen T' t. induction T; intros.
+ inv_step.
+ inverts wfCfg as Hlt wfH wfV wfT wfL.
inverts wfL as wfLs Hdup wfWl wfRl.
inverts wfT as Hfree Hfresh hasType.
gen T'.
hasType_cases (induction hasType) Case; intros;
try(trivial_step_case).
- Case "T_Call".
inv_step; try(malformed_context)...
inv_eq; simpl...
inv Hfree. destruct_var; try(congruence).
inv Hfresh.
apply wfWLocks_econtext in wfWl...
apply wfRLocks_econtext in wfRl...
eapply IHhasType in Hstep...
- Case "T_Update".
inv_step; try(malformed_context)...
inv_eq; simpl...
inv Hfree. destruct_var; try(congruence).
inv Hfresh.
apply wfWLocks_econtext in wfWl...
apply wfRLocks_econtext in wfRl...
eapply IHhasType2 in Hstep...
- Case "T_Let".
inv_step; try(malformed_context)...
inv_eq; simpls...
inv Hfree. apply app_eq_nil in H2 as [].
inv Hfresh.
apply wfWLocks_econtext with (ctx := ctx_let x frame body) in wfWl...
apply wfRLocks_econtext with (ctx := ctx_let x frame body) in wfRl...
apply not_in_app in HnIn as [].
eapply IHhasType1 in Hstep...
- Case "T_Cast".
inv_step; try(malformed_context)...
inv_eq; simpls...
inv Hfree.
inv Hfresh.
apply wfWLocks_econtext in wfWl...
apply wfRLocks_econtext in wfRl...
eapply IHhasType in Hstep...
- Case "T_WLock".
inv_step; try(malformed_context)...
simpls...
- Case "T_WLocked".
inv_step; try(malformed_context); simpls...
* inv_eq.
inv Hfree. inv Hfresh.
apply wfWLocks_econtext in wfWl...
apply wfRLocks_econtext in wfRl...
eapply IHhasType2 in Hstep...
* clear IHhasType1. clear IHhasType2.
assert ((l1, r0) <> (l, r))...
apply remove_in_neq...
- Case "T_RLocked".
inv_step; try(malformed_context); simpls...
inv_eq; simpl...
inv Hfree. inv Hfresh.
eapply wfWLocks_econtext in wfWl...
eapply wfRLocks_econtext in wfRl...
apply IHhasType2 in Hstep...
+ inverts wfCfg as Hlt wfH wfV wfT wfL.
inverts wfT as Hfree Hfresh hasType wfT1 wfT2.
inverts wfL as wfWl HWdisj wfRl Hdisj wfL1 wfL2.
inv_step...
- simpls...
eapply IHT1 in Hstep...
apply not_in_app in HnIn as []...
Qed.
Lemma noDuplicatedLocks :
forall P t' Gamma l r H H' V V' n n' T T' t c F RL,
wfProgram P t' ->
heapLookup H l = Some (c, F, RL) ->
RL r = Some LLocked ->
~ In (l, r) (t_wlocks T) ->
wfConfiguration P Gamma (H, V, n, T) t ->
P / (H, V, n, T) ==> (H', V', n', T') ->
~ In (l, r) (t_wlocks T').
Proof with eauto.
introv wfP Hlookup HRL HnIn wfCfg Hstep.
gen T' t. induction T; intros.
+ inv_step.
+ rename l0 into Ls.
inverts wfCfg as Hlt wfH wfV wfT wfL.
inverts wfL as wfLs Hdup wfWl wfRl.
inverts wfT as Hfree Hfresh hasType.
gen T'.
hasType_cases (induction hasType) Case; intros;
try(trivial_step_case).
- Case "T_Call".
inv Hfree. destruct_var; try(congruence).
inv Hfresh.
apply wfWLocks_econtext in wfWl...
apply wfRLocks_econtext in wfRl...
inv_step; try(malformed_context); try(inv_eq); simpls...
* eapply IHhasType in Hstep...
* eapply IHhasType in Hstep...
* rewrite <- wlocks_subst. rewrite <- wlocks_subst.
rewrite <- wlocks_sigma.
eapply wfProgram_wfMethodDecl in H21...
inv H21. apply wlocks_static in H15. rewrite H15.
contra...
- Case "T_Update".
inv Hfree. destruct_var; try(congruence).
inv Hfresh.
apply wfWLocks_econtext in wfWl...
apply wfRLocks_econtext in wfRl...
inv_step; try(malformed_context); try(inv_eq); simpls...
* eapply IHhasType2 in Hstep...
* eapply IHhasType2 in Hstep...
- Case "T_Let".
inv Hfree. inv Hfresh.
apply wfWLocks_econtext with (ctx := ctx_let x frame body) in wfWl...
apply wfRLocks_econtext with (ctx := ctx_let x frame body) in wfRl...
inv H0.
apply not_in_app in HnIn as [].
apply app_eq_nil in H2 as [].
inv_step; try(malformed_context); try(inv_eq); simpls...
* eapply IHhasType1 in Hstep...
rewrite H1. rewrite app_nil_r...
* eapply IHhasType1 in Hstep...
rewrite H1. rewrite app_nil_r...
* rewrite <- wlocks_subst. rewrite H1.
contra...
- Case "T_Cast".
inv Hfree. inv Hfresh.
apply wfWLocks_econtext in wfWl...
apply wfRLocks_econtext in wfRl...
inv_step; try(malformed_context); try(inv_eq); simpls...
* eapply IHhasType in Hstep...
* eapply IHhasType in Hstep...
- Case "T_WLock".
inv_step; try(malformed_context)...
clears IHhasType1 IHhasType2.
simpls... contra...
inv_eq. rewrite_and_invert.
congruence.
- Case "T_WLocked".
inv Hfree. inv Hfresh.
apply wfWLocks_econtext in wfWl...
apply wfRLocks_econtext in wfRl...
inv_step; try(malformed_context); try(inv_eq); simpls...
* eapply IHhasType2 in Hstep...
simpls. apply not_in_app in Hstep as [].
apply not_in_app in H0 as [].
apply not_in_app.
split...
apply not_in_app.
split...
contra...
* eapply IHhasType2 in Hstep...
contra...
- Case "T_RLocked".
inv Hfree. inv Hfresh.
apply wfWLocks_econtext in wfWl...
apply wfRLocks_econtext in wfRl...
inv_step; try(malformed_context); try(inv_eq); simpls...
* eapply IHhasType2 in Hstep...
* eapply IHhasType2 in Hstep...
+ inverts wfCfg as Hlt wfH wfV wfT wfL.
inverts wfT as Hfree Hfresh hasType wfT1 wfT2.
inverts wfL as wfWl HWdisj wfRl Hdisj wfL1 wfL2.
inv_step...
- simpls...
apply not_in_app in HnIn as []...
apply not_in_app...
- simpls...
apply not_in_app in HnIn as []...
apply not_in_app...
split...
apply not_in_app in H1 as []...
apply not_in_app...
- simpls.
apply not_in_app in HnIn as [_ HnIn'].
apply not_in_app in HnIn' as []...
Qed.
Lemma noLostRLocks :
forall P t' Gamma l r c F RL n'' RLs H H' V V' n n' Ls Ls' e e' t m,
wfProgram P t' ->
heapLookup H l = Some (c, F, RL) ->
RL r = Some (LReaders n'') ->
0 < m ->
m + (length (filter (lock_eq (l, r)) (rlocks e ++ RLs))) <= n'' ->
wfThreads P Gamma n (T_Thread Ls e) t ->
P / (H, V, n, T_Thread Ls e) ==> (H', V', n', T_Thread Ls' e') ->
exists F' RL' n''',
heapLookup H' l = Some (c, F', RL') /\
RL' r = Some (LReaders n''') /\
m + (length (filter (lock_eq (l, r)) (rlocks e' ++ RLs))) <= n'''.
Proof with eauto.
introv wfP Hlookup HRL Hm Hle wfT Hstep.
assert(l < length H)
by (eapply heapLookup_lt; eauto).
inverts wfT as Hfree Hfresh hasType.
gen e' m. hasType_cases (induction hasType) Case; simpls; intros;
try(trivial_step_case); repeat(destruct_var; try(congruence)).
+ Case "T_Var".
inv_step; try(malformed_context)...
repeat eexists...
+ Case "T_Consume".
inv_step; try(malformed_context)...
repeat eexists...
+ Case "T_New".
inv_step; try(malformed_context)...
rewrite heapExtend_lookup_nlen...
repeat eexists...
omega.
+ Case "T_Call".
inv Hfresh.
inv_step; try(malformed_context);
simpls; try(inv_eq)...
rewrite <- rlocks_subst.
rewrite <- rlocks_subst.
rewrite <- rlocks_sigma.
assert (wfMtd: wfMethodDecl P c0 (Method m (y0, t0) t'0 body))
by eauto using wfProgram_wfMethodDecl.
inv wfMtd.
rewrite rlocks_static...
simpl. repeat eexists...
+ Case "T_Select".
inv_step; try(malformed_context);
simpls; try(inv_eq)...
repeat eexists...
+ Case "T_ConsmueField".
inv_step; try(malformed_context);
simpls; try(inv_eq)...
destruct (id_eq_dec l0 l).
- subst. rewrite lookup_heapUpdate_eq...
rewrite_and_invert.
repeat eexists...
- rewrite lookup_heapUpdate_neq...
repeat eexists...
+ Case "T_Update".
inv Hfresh.
inv_step; try(malformed_context);
simpls; try(inv_eq)...
destruct (id_eq_dec l0 l).
- subst. rewrite lookup_heapUpdate_eq...
rewrite_and_invert.
repeat eexists...
- rewrite lookup_heapUpdate_neq...
repeat eexists...
+ Case "T_Let".
inv Hfresh.
inverts H1 as _ no_rlocks.
rewrite no_rlocks in Hle. rewrite app_nil_r in Hle...
apply app_eq_nil in Hfree as [].
inv_step; try(malformed_context);
simpls; try(inv_eq)...
- rewrite no_rlocks. rewrite app_nil_r.
eapply IHhasType1 in Hstep...
- rewrite <- rlocks_subst.
rewrite no_rlocks.
repeat eexists...
+ Case "T_Cast".
inv Hfresh.
inv_step; try(malformed_context);
simpls; try(inv_eq)...
repeat eexists...
+ Case "T_WLock".
inv Hfresh.
inv_step; try(malformed_context);
simpls; try(inv_eq)...
- destruct (id_eq_dec l0 l).
* subst. rewrite_and_invert.
rewrite lookup_heapUpdate_eq...
repeat eexists...
case_extend. rewrite_and_invert.
destruct m; try
omega.
* rewrite lookup_heapUpdate_neq...
repeat eexists...
- repeat eexists...
+ Case "T_RLock".
inv Hfresh.
inv_step; try(malformed_context);
simpls; try(inv_eq)...
- destruct (id_eq_dec l l0).
* subst. rewrite_and_invert.
rewrite lookup_heapUpdate_eq...
{destruct (id_eq_dec r0 r).
+ subst. rewrite_and_invert.
folds (lock_eq (l0, r) (l0, r)).
rewrite lock_eq_refl. simpls.
repeat eexists...
omega.
+ replaces (region_id_eq r r0) with false;
try(solve[destruct r; destruct r0; simpls;
apply beq_nat_false_iff; eauto]).
rewrite <- beq_nat_refl.
simpl. repeat eexists... case_extend.
}
* rewrite lookup_heapUpdate_neq...
repeat eexists...
apply beq_nat_false_iff in n.
rewrite n. simpl.
omega.
- repeat eexists...
+ Case "T_WLocked".
inv Hfresh.
inv_step; try(malformed_context);
simpls; try(inv_eq)...
destruct (id_eq_dec l l0).
- subst. rewrite_and_invert.
rewrite lookup_heapUpdate_eq...
destruct (id_eq_dec r0 r).
* subst. rewrite_and_invert.
* repeat eexists... case_extend.
- rewrite lookup_heapUpdate_neq...
repeat eexists...
+ Case "T_RLocked".
inv Hfresh.
inv_step; try(malformed_context);
simpls; try(inv_eq)...
- cases_if; simpls.
* apply IHhasType2 with (m := S m) in Hstep as (F' & RL' & n''' & Hlookup' & HRL' & Hle');
try omega...
repeat eexists... omega.
* eapply IHhasType2 in Hstep...
- folds (lock_eq (l, r) (l0, r0)).
cases_if.
* apply lock_eq_eq in H1.
inv_eq. simpls.
rewrite lookup_heapUpdate_eq...
rewrite_and_invert.
rewrite_and_invert.
repeat eexists...
omega.
* {destruct (id_eq_dec l l0).
+ subst. rewrite_and_invert.
rewrite lookup_heapUpdate_eq...
destruct (id_eq_dec r0 r).
- subst. rewrite_and_invert.
repeat eexists...
assert (lock_eq (l0, r) (l0, r) = true)
by (eapply lock_eq_eq; eauto).
congruence.
- repeat eexists... case_extend.
+ rewrite lookup_heapUpdate_neq...
repeat eexists...
}
+ Case "T_Assert".
inv_step; try(malformed_context);
simpls; try(inv_eq)...
repeat eexists...
Qed.
Lemma wfRLocks_async_preservation :
forall P t' Gamma H H' V V' n n' T T' t RLs l r,
wfProgram P t' ->
wfConfiguration P Gamma (H, V, n, T) t ->
(In (l, r) (t_rlocks T ++ RLs) ->
exists c F RL n,
heapLookup H l = Some (c, F, RL) /\
RL r = Some (LReaders n) /\
length (filter (lock_eq (l, r)) (t_rlocks T ++ RLs)) <= n) ->
P / (H, V, n, T) ==> (H', V', n', T') ->
(In (l, r) (t_rlocks T' ++ RLs) ->
exists c F RL n,
heapLookup H' l = Some (c, F, RL) /\
RL r = Some (LReaders n) /\
length (filter (lock_eq (l, r)) (t_rlocks T' ++ RLs)) <= n).
Proof with eauto.
introv wfP wfCfg wfRl Hstep HIn.
gen T' t RLs. induction T; intros; simpls.
+ inv_step.
+ rename l0 into Ls.
inverts wfCfg as Hlt wfH wfV wfT wfL.
inverts wfL as wfLs Hdup wfWl wfRl'.
inversion wfWl. inversion wfRl'.
inverts wfT as Hfree Hfresh hasType.
gen T'.
hasType_cases (induction hasType) Case; intros; simpls;
try(trivial_step_case).
- Case "T_New".
inv_step; try(malformed_context)... simpls.
apply wfRl in HIn as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
assert(l < length H)
by (eapply heapLookup_lt; eauto).
assert(l <> length H)
by omega.
rewrite heapExtend_lookup_nlen... repeat eexists...
- Case "T_Call".
destruct_var; try(congruence).
inv Hfresh.
inv_step; try(malformed_context); simpls; try inv_eq...
* apply IHhasType in Hstep...
* apply IHhasType in Hstep...
* assert (wfMtd: wfMethodDecl P c (Method m (y0, t0) t'0 body)) by eauto using wfProgram_wfMethodDecl.
inv wfMtd.
rewrite <- rlocks_subst. rewrite <- rlocks_subst.
rewrite <- rlocks_sigma. rewrite rlocks_static...
rewrite <- rlocks_subst in HIn. rewrite <- rlocks_subst in HIn.
rewrite <- rlocks_sigma in HIn. rewrite rlocks_static in HIn...
* assert(HIn': In (l, r) (rlocks e ++ RLs))
by eauto using in_or_app.
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
rewrite filter_app in Hle.
rewrite app_length in Hle.
repeat eexists... omega.
- Case "T_ConsmueField".
destruct_var; try(congruence).
inv_step; try(malformed_context); simpls; try inv_eq...
destruct (id_eq_dec l0 l).
* subst. rewrite lookup_heapUpdate_eq; try eapply heapLookup_lt...
apply wfRl in HIn as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
rewrite_and_invert.
repeat eexists...
* rewrite lookup_heapUpdate_neq...
- Case "T_Update".
destruct_var; try(congruence).
inv Hfresh.
inv_step; try(malformed_context); simpls; try inv_eq...
* apply IHhasType2 in Hstep...
* apply IHhasType2 in Hstep...
* {destruct (id_eq_dec l0 l).
+ subst. rewrite lookup_heapUpdate_eq; try eapply heapLookup_lt...
apply wfRl in HIn as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
rewrite_and_invert.
repeat eexists...
+ rewrite lookup_heapUpdate_neq...
}
* assert(HIn': In (l, r) (rlocks e ++ RLs))
by eauto using in_or_app.
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
rewrite filter_app in Hle.
rewrite app_length in Hle.
repeat eexists... omega.
- Case "T_Let".
inv Hfresh. inverts H3 as _ no_rlocks.
rewrite no_rlocks in wfRl. rewrite app_nil_r in wfRl.
apply app_eq_nil in Hfree as [].
apply wfWLocks_econtext with (ctx := ctx_let x frame body) in wfWl...
apply wfRLocks_econtext with (ctx := ctx_let x frame body) in wfRl'...
inv wfWl.
inv wfRl'.
inv_step; try(malformed_context); simpls; try inv_eq...
* rewrite no_rlocks. rewrite app_nil_r.
rewrite no_rlocks in HIn. rewrite app_nil_r in HIn.
apply IHhasType1 in Hstep...
* rewrite no_rlocks. rewrite app_nil_r.
rewrite no_rlocks in HIn. rewrite app_nil_r in HIn.
apply IHhasType1 in Hstep...
* rewrite <- rlocks_subst. rewrite no_rlocks. simpl.
rewrite <- rlocks_subst in HIn.
rewrite no_rlocks in HIn. simpls.
apply wfRl in HIn as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
repeat eexists...
* assert(HIn': In (l, r) (rlocks e ++ RLs))
by eauto using in_or_app.
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
rewrite filter_app in Hle.
rewrite app_length in Hle.
repeat eexists... omega.
- Case "T_Cast".
inv Hfresh.
inv_step; try(malformed_context); simpls; try inv_eq...
* apply IHhasType in Hstep...
* apply IHhasType in Hstep...
* assert(HIn': In (l, r) (rlocks e ++ RLs))
by eauto using in_or_app.
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
rewrite filter_app in Hle.
rewrite app_length in Hle.
repeat eexists... omega.
- Case "T_WLock".
clears IHhasType1 IHhasType2.
inv_step; try(malformed_context); simpls...
pose proof (wfRl HIn) as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
* {destruct (id_eq_dec l0 l).
+ subst.
rewrite lookup_heapUpdate_eq; try eapply heapLookup_lt...
repeat eexists...
case_extend. rewrite_and_invert.
rewrite_and_invert.
assert (length (filter (lock_eq (l, r)) (rlocks e ++ RLs)) = 0) by omega.
assert (Hnil: filter (lock_eq (l, r)) (rlocks e ++ RLs) = nil) by
(destruct(filter (lock_eq (l, r)) (rlocks e ++ RLs)); simpls; congruence).
rewrite filter_in with (p := lock_eq) in HIn.
rewrite Hnil in HIn. inv HIn. apply lock_eq_eq.
+ rewrite lookup_heapUpdate_neq...
}
* assert(HIn': In (l, r) (rlocks e ++ RLs))
by eauto using in_or_app.
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
repeat eexists...
rewrite filter_app in Hle.
rewrite app_length in Hle. omega.
- Case "T_RLock".
clears IHhasType1 IHhasType2.
inv_step; try(malformed_context); simpls...
* {inv HIn.
+ inv_eq. rewrite <- beq_nat_refl. rewrite region_id_eq_refl.
simpl. rewrite lookup_heapUpdate_eq; try(apply heapLookup_lt)...
repeat eexists...
assert(InOrNot: {In (l, r) (rlocks e ++ RLs)} + {~In (l, r) (rlocks e ++ RLs)})
by (eapply in_dec; eapply id_eq_dec).
inv InOrNot...
- apply wfRl in H6 as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
rewrite_and_invert. rewrite_and_invert. omega.
- rewrite filter_not_in in H6; try(apply lock_eq_eq).
replaces (filter (lock_eq (l, r)) (rlocks e ++ RLs)) with (@nil lock)...
simpl. omega.
+ apply wfRl in H6 as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
destruct (id_eq_dec l l0).
- subst. rewrite_and_invert.
rewrite lookup_heapUpdate_eq; try(apply heapLookup_lt)...
rewrite <- beq_nat_refl.
destruct (id_eq_dec r r0).
* subst. rewrite_and_invert.
rewrite region_id_eq_refl.
simpl. repeat eexists... omega.
* replaces (region_id_eq r r0) with false;
try(solve[destruct r; destruct r0; simpls;
apply beq_nat_false_iff; eauto]).
simpl. exists c' F' (extend RL' r0 (LReaders (S m))) n''.
splits... case_extend.
- rewrite lookup_heapUpdate_neq...
replaces (beq_nat l l0) with false;
try(apply beq_nat_false_iff; eauto).
simpl. repeat eexists...
}
* assert(HIn': In (l, r) (rlocks e ++ RLs))
by eauto using in_or_app.
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
repeat eexists...
rewrite filter_app in Hle.
rewrite app_length in Hle. omega.
- Case "T_WLocked".
inv Hfresh.
assert (wfRLocks H (T_Thread Ls e))
by (apply wfRLocks_econtext in wfRl'; eauto).
assert (NoDup (wlocks e))
by (inv H1; eauto).
inv_step; try(malformed_context); simpls...
* inv_eq.
assert(Hno_locks: no_locks e1 /\ no_locks e2)
by eauto using spawn_no_locks.
assert(Hlocks: rlocks (ERlocked (l0, r0) e) = rlocks (ERlocked (l0, r0) e3))
by (eapply spawn_locks; eauto).
inverts Hno_locks as no_locks1 no_locks2.
inverts no_locks1 as _ no_rlocks1.
inverts no_locks2 as _ no_rlocks2.
inverts Hlocks as Hlocks.
rewrite no_rlocks1.
rewrite no_rlocks2.
rewrite <- Hlocks in HIn.
rewrite no_rlocks1 in HIn.
rewrite no_rlocks2 in HIn.
simpls.
apply wfRl in HIn...
* inv_eq. apply IHhasType2 in Hstep...
* clears IHhasType1 IHhasType2.
apply wfRl in HIn as (c' & F' & RL' & n'' & Hlookup & HRL & Hle).
{destruct(id_eq_dec l0 l).
+ subst. rewrite lookup_heapUpdate_eq; try eapply heapLookup_lt...
repeat eexists...
case_extend.
+ rewrite lookup_heapUpdate_neq...
repeat eexists...
}
* inv_eq.
assert(HIn': In (l, r) (rlocks e ++ RLs))
by eauto using in_or_app.
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle)...
repeat eexists...
rewrite filter_app in Hle.
rewrite app_length in Hle.
omega.
- Case "T_RLocked".
inv Hfresh.
assert (wfRLocks H (T_Thread Ls e))
by (apply wfRLocks_econtext in wfRl'; eauto).
assert (NoDup (wlocks e))
by (inv H1; eauto).
inv_step; try(malformed_context); simpls...
* inv_eq.
assert(Hno_locks: no_locks e1 /\ no_locks e2)
by eauto using spawn_no_locks.
assert(Hlocks: rlocks (ERlocked (l0, r0) e) = rlocks (ERlocked (l0, r0) e3))
by (eapply spawn_locks; eauto).
inverts Hno_locks as no_locks1 no_locks2.
inverts no_locks1 as _ no_rlocks1.
inverts no_locks2 as _ no_rlocks2.
inverts Hlocks as Hlocks.
rewrite no_rlocks1.
rewrite no_rlocks2.
rewrite <- Hlocks in HIn.
rewrite no_rlocks1 in HIn.
rewrite no_rlocks2 in HIn.
simpls.
apply wfRl in HIn...
* inv_eq.
{inv HIn.
+ inv_eq.
folds (lock_eq (l, r) (l, r)).
rewrite lock_eq_refl.
rewrite lock_eq_refl in wfRl.
simpls.
assert(HIn': (l, r) = (l, r) \/ In (l, r) (rlocks e ++ RLs))...
apply wfRl in HIn' as (c & F & RL & n'' & Hlookup & HRL & Hle)...
eapply noLostRLocks with (m := 1) in Hstep...
+ assert(Hstep': P / (H, V, n, T_Thread Ls e) ==> (H', V', n', T_Thread Ls' e'))...
apply IHhasType2 in Hstep as (c' & F' & RL' & n'' & Hlookup & HRL & Hle)...
- cases_if; simpls.
* folds (lock_eq (l, r) (l0, r0)).
apply lock_eq_eq in H7. inv_eq.
assert (HIn': (l0, r0) = (l0, r0) \/ In (l0, r0) (rlocks e ++ RLs))...
apply wfRl in HIn' as (c & F & RL & n''' & Hlookup' & HRL' & Hle').
eapply noLostRLocks with (m := 1) in Hstep'...
* repeat eexists...
- introv HIn.
assert(HIn': (l0, r0) = (l, r) \/ In (l, r) (rlocks e ++ RLs))...
apply wfRl in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle)...
repeat eexists...
cases_if; simpls; omega.
- introv HIn. inverts wfRl' as wfRl'. simpls.
assert(HIn': (l0, r0) = (l1, r1) \/ In (l1, r1) (rlocks e))...
apply wfRl' in HIn' as (c' & F' & RL' & n'' & Hlookup & HRL & Hle)...
repeat eexists...
cases_if; simpls; omega.
}