-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadStore.v
1453 lines (1392 loc) · 47.2 KB
/
LoadStore.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 Bool.
Require Import List.
Require Import Omega.
Require Import BinNatDef.
Require Import sflib.
Require Import Sorting.Permutation.
Require Import Common.
Require Import Lang.
Require Import Value.
Require Import Memory.
Module Ir.
(* The list of dereferenced blocks, given a physical pointer. *)
Definition get_deref_blks_phyptr (m:Ir.Memory.t) (o:nat) (Is:list nat)
(cid:option Ir.callid) (sz:nat)
: list (Ir.blockid * Ir.MemBlock.t) :=
match (Ir.Memory.inbounds_blocks2 m (o::(o+sz)::Is)) with
| nil => nil (* No such block *)
| blks =>
match cid with
| None => blks (* No cid constraint *)
| Some c =>
match Ir.Memory.calltime m c with
| None => blks (* The function is finished. *)
| Some t => List.filter
(fun b => Ir.MemBlock.alive_before t b.(snd))
blks
end
end
end.
(* get_deref returns (blockid, block, offset) list which will be dereferenced
from pointer p and access size sz.
We'll show that the list of (block, offset) is a singleton later. *)
Definition get_deref (m:Ir.Memory.t) (p:Ir.ptrval) (sz:nat)
: list (Ir.blockid * Ir.MemBlock.t * nat) :=
match p with
| Ir.plog bid ofs => (* Logical pointer *)
match (Ir.Memory.get m bid) with
| None => nil (* No such block *)
| Some blk =>
if Ir.MemBlock.alive blk && Ir.MemBlock.inbounds ofs blk &&
Ir.MemBlock.inbounds (ofs + sz) blk then
(bid, blk, ofs)::nil
else nil
end
| Ir.pphy o Is cid => (* Physical pointer *)
List.map (fun mb => (mb.(fst), mb.(snd), o - Ir.MemBlock.addr mb.(snd)))
(get_deref_blks_phyptr m o Is cid sz)
end.
(* Boolean version of get_deref.
If get_deref returns [], this returns false, true otherwise. *)
Definition deref (m:Ir.Memory.t) (p:Ir.ptrval) (sz:nat): bool :=
match (get_deref m p sz) with
| nil => false | _=> true
end.
(* Returns a list of bytes, after dereferencing p with
sz bytes. *)
Definition load_bytes (m:Ir.Memory.t) (p:Ir.ptrval) (sz:nat): list Ir.Byte.t :=
match (get_deref m p sz) with
| nil => nil
| (bid, blk, ofs)::_ => Ir.MemBlock.bytes blk ofs sz
end.
(* Returns a value, after dereferencing p with sz btes. *)
Definition load_val (m:Ir.Memory.t) (p:Ir.ptrval) (t:Ir.ty): Ir.val :=
let bytes := load_bytes m p (Ir.ty_bytesz t) in
match t with
| Ir.ity bitsz =>
match Ir.Byte.getint bytes bitsz with
| None => Ir.poison
| Some n => Ir.num (Nat.modulo (N.to_nat n) (Nat.shiftl 1 bitsz))
end
| Ir.ptrty _ =>
match Ir.Byte.getptr bytes with
| None => Ir.poison
| Some p => Ir.ptr p
end
end.
(* Store bytes bs into p. *)
Definition store_bytes (m:Ir.Memory.t) (p:Ir.ptrval) (bs:list Ir.Byte.t)
:Ir.Memory.t :=
match get_deref m p (List.length bs) with
| nil => m
| (bid, blk, ofs)::_ =>
if (Ir.MemBlock.n blk) <? (ofs + length bs) then
m (* it does not change memory. *)
else
Ir.Memory.set m bid (Ir.MemBlock.set_bytes blk ofs bs)
end.
(* Store value v into p. *)
Definition store_val (m:Ir.Memory.t) (p:Ir.ptrval) (v:Ir.val) (t:Ir.ty)
: Ir.Memory.t :=
match (t, v) with
| (Ir.ity bitsz, Ir.num n) =>
let bs := Ir.Byte.ofint (N.of_nat n) bitsz in
if (Ir.ty_bytesz (Ir.ity bitsz)) =? List.length bs then
store_bytes m p bs
else m (* Wrongly typed. *)
| (Ir.ptrty pty, Ir.ptr pv) =>
let bs := Ir.Byte.ofptr pv in
if (Ir.ty_bytesz (Ir.ptrty pty)) =? List.length bs then
store_bytes m p bs
else m (*Wrongly typed*)
| _ => m (*Wrongly typed*)
end.
(* Convert a logical pointer to physical pointer. *)
Definition log_to_phy (m:Ir.Memory.t) (bid:Ir.blockid) (ofs:nat): option Ir.ptrval :=
match Ir.Memory.get m bid with
| None => None
| Some bb => Some (Ir.pphy
(Nat.modulo (Ir.MemBlock.addr bb + ofs) Ir.MEMSZ)
nil
None)
end.
(* Convert a pointer to physical pointer. *)
Definition ptr_to_phy (m:Ir.Memory.t) (p:Ir.ptrval): option Ir.ptrval :=
match p with
| Ir.plog bid ofs => log_to_phy m bid ofs
| Ir.pphy o Is cid => Some (Ir.pphy o nil None)
end.
(***********************************************
Lemmas about get_deref
***********************************************)
(* get_deref log(bid, ofs) either returns the input (bid, block, ofs)
or returns nothing. *)
Lemma get_deref_log:
forall (m:Ir.Memory.t) bid ofs sz bos blk
(HDEREF: get_deref m (Ir.plog bid ofs) sz = bos)
(HBLK: Ir.Memory.get m bid = Some blk),
bos = (bid, blk, ofs)::nil \/ bos = nil.
Proof.
intros.
unfold get_deref in HDEREF.
rewrite HBLK in HDEREF.
destruct (Ir.MemBlock.alive blk && Ir.MemBlock.inbounds ofs blk &&
Ir.MemBlock.inbounds (ofs + sz) blk).
- left. congruence.
- right. congruence.
Qed.
Lemma get_deref_log_inv:
forall (m:Ir.Memory.t) bid ofs sz blk
(HDEREF: exists e, get_deref m (Ir.plog bid ofs) sz = e::nil)
(HBLK: Ir.Memory.get m bid = Some blk),
Ir.MemBlock.alive blk &&
Ir.MemBlock.inbounds ofs blk &&
Ir.MemBlock.inbounds (ofs + sz) blk = true.
Proof.
intros.
remember (Ir.MemBlock.alive blk) as b1.
remember (Ir.MemBlock.inbounds ofs blk) as b2.
remember (Ir.MemBlock.inbounds (ofs + sz) blk) as b3.
destruct HDEREF as [bo HDEREF].
unfold get_deref in HDEREF.
rewrite HBLK in HDEREF.
rewrite <- Heqb1,<- Heqb2, <- Heqb3 in HDEREF.
destruct b1; destruct b2 ; destruct b3; simpl in HDEREF; try (inversion HDEREF; fail).
reflexivity.
Qed.
Lemma get_deref_log_alive:
forall m l o p mb sz
(HGET:Some mb = Ir.Memory.get m l)
(HDEREF:Ir.get_deref m (Ir.plog l o) sz = [p]),
Ir.MemBlock.alive mb = true.
Proof.
intros.
assert (exists p, Ir.get_deref m (Ir.plog l o) sz = [p]).
{ eexists. rewrite HDEREF. reflexivity. }
eapply Ir.get_deref_log_inv in H.
2: rewrite <- HGET. 2: reflexivity.
rewrite andb_true_iff in H.
destruct H.
rewrite andb_true_iff in H.
intuition.
Qed.
Lemma get_deref_inv:
forall (m:Ir.Memory.t) p bid ofs sz blk
(HSZ:sz > 0)
(HWF:Ir.Memory.wf m)
(HDEREF: get_deref m p sz = (bid, blk, ofs)::nil)
(HBLK: Ir.Memory.get m bid = Some blk),
Ir.MemBlock.alive blk &&
Ir.MemBlock.inbounds ofs blk &&
Ir.MemBlock.inbounds (ofs + sz) blk = true.
Proof.
intros.
destruct p as [b n | o Is cid].
- apply get_deref_log_inv with (m := m) (bid := bid).
assert (b = bid /\ n = ofs).
{ unfold get_deref in HDEREF.
destruct (Ir.Memory.get m b).
destruct (Ir.MemBlock.alive t && Ir.MemBlock.inbounds n t &&
Ir.MemBlock.inbounds (n + sz) t).
inversion HDEREF. split; reflexivity.
inversion HDEREF. inversion HDEREF. }
destruct H as [H1 H2]. rewrite H1, H2 in HDEREF.
exists (bid, blk, ofs). assumption.
assumption.
- unfold get_deref in HDEREF.
unfold get_deref_blks_phyptr in HDEREF.
remember (Ir.Memory.inbounds_blocks2 m (o::o+sz::Is)) as res.
symmetry in Heqres.
assert (HFORALL := Ir.Memory.inbounds_blocks2_forallb2 m
(o::o+sz::Is) res Heqres).
simpl in HFORALL.
assert (List.length res < 2).
{ apply Ir.Memory.inbounds_blocks2_singleton2 with (m := m)
(ofs1 := o) (ofs2 := o+sz) (ofs' := Is).
assumption.
apply Nat.lt_neq.
apply Nat.lt_add_pos_r. assumption.
assumption. }
destruct res.
+ simpl in HDEREF. inversion HDEREF.
+ destruct res.
{
destruct cid.
{
remember (Ir.Memory.calltime m c) as t'.
destruct t'.
{
simpl in HDEREF.
destruct p.
{
simpl in *.
destruct (Ir.MemBlock.alive_before t t0) eqn:HAB.
{
simpl in HDEREF.
inversion HDEREF.
rewrite H1, H2, H3 in *. clear H1 H2.
repeat (rewrite andb_true_r in HFORALL).
repeat (rewrite andb_true_iff in HFORALL).
destruct HFORALL as [HFORALL1 [HFORALL2 HFORALL3]].
assert (HFORALL1' := HFORALL1).
rewrite <- Ir.MemBlock.inbounds_inbounds_abs with (ofs := ofs) in HFORALL1.
rewrite <- Ir.MemBlock.inbounds_inbounds_abs with (ofs := ofs + sz) in HFORALL2.
rewrite HFORALL1, HFORALL2.
assert (HALIVE: List.forallb (fun b=> Ir.MemBlock.alive b.(snd))
((bid, blk)::nil) = true).
{ eapply Ir.Memory.inbounds_blocks2_alive.
eassumption. }
simpl in HALIVE. rewrite HALIVE. reflexivity.
rewrite Ir.MemBlock.inbounds_abs_addr
with (o := o) (blk := blk) (ofs := ofs).
omega. assumption. assumption.
rewrite Nat.add_comm.
apply Ir.MemBlock.inbounds_abs_addr; assumption.
}
{ simpl in HDEREF. inversion HDEREF. }
}
}
{ destruct p.
simpl in *.
simpl in HDEREF.
inversion HDEREF.
rewrite H1, H2, H3 in *. clear H1 H2.
repeat (rewrite andb_true_r in HFORALL).
repeat (rewrite andb_true_iff in HFORALL).
destruct HFORALL as [HFORALL1 [HFORALL2 HFORALL3]].
assert (HFORALL1' := HFORALL1).
rewrite <- Ir.MemBlock.inbounds_inbounds_abs with (ofs := ofs) in HFORALL1.
rewrite <- Ir.MemBlock.inbounds_inbounds_abs with (ofs := ofs + sz) in HFORALL2.
rewrite HFORALL1, HFORALL2.
assert (HALIVE: List.forallb (fun b=> Ir.MemBlock.alive b.(snd))
((bid, blk)::nil) = true).
{ eapply Ir.Memory.inbounds_blocks2_alive.
eassumption. }
simpl in HALIVE. rewrite HALIVE. reflexivity.
rewrite Ir.MemBlock.inbounds_abs_addr
with (o := o) (blk := blk) (ofs := ofs).
omega. assumption. assumption.
rewrite Nat.add_comm.
apply Ir.MemBlock.inbounds_abs_addr; assumption.
}
}
{ destruct p.
simpl in *.
simpl in HDEREF.
inversion HDEREF.
rewrite H1, H2, H3 in *. clear H1 H2.
repeat (rewrite andb_true_r in HFORALL).
repeat (rewrite andb_true_iff in HFORALL).
destruct HFORALL as [HFORALL1 [HFORALL2 HFORALL3]].
assert (HFORALL1' := HFORALL1).
rewrite <- Ir.MemBlock.inbounds_inbounds_abs with (ofs := ofs) in HFORALL1.
rewrite <- Ir.MemBlock.inbounds_inbounds_abs with (ofs := ofs + sz) in HFORALL2.
rewrite HFORALL1, HFORALL2.
assert (HALIVE: List.forallb (fun b=> Ir.MemBlock.alive b.(snd))
((bid, blk)::nil) = true).
{ eapply Ir.Memory.inbounds_blocks2_alive.
eassumption. }
simpl in HALIVE. rewrite HALIVE. reflexivity.
rewrite Ir.MemBlock.inbounds_abs_addr
with (o := o) (blk := blk) (ofs := ofs).
omega. assumption. assumption.
rewrite Nat.add_comm.
apply Ir.MemBlock.inbounds_abs_addr; assumption.
}
}
{ simpl in H.
omega.
}
Qed.
(* Lemma: get_deref_blks_byaddrs returns at most one alive block. *)
Lemma get_deref_blks_phyptr_singleton:
forall (m:Ir.Memory.t) (m_wf:Ir.Memory.wf m) o Is cid sz bos
(HSZ: 0 < sz)
(HDEREF: get_deref_blks_phyptr m o Is cid sz = bos),
(exists bo, bos = bo::nil /\ Ir.Memory.get m bo.(fst) = Some bo.(snd))
\/ (bos = nil).
Proof.
intros.
unfold get_deref_blks_phyptr in HDEREF.
remember (Ir.Memory.inbounds_blocks2 m (o::o+sz::Is)) as blks.
assert (List.length blks < 2).
{
apply (Ir.Memory.inbounds_blocks2_singleton2 m o (o+sz) blks Is).
assumption.
intros H.
destruct sz.
- inversion HSZ.
- rewrite PeanoNat.Nat.add_succ_r in H.
rewrite PeanoNat.Nat.add_comm in H.
apply PeanoNat.Nat.succ_add_discr in H.
assumption.
- congruence.
}
destruct blks as [| b1 blks].
{ right. simpl in HDEREF. congruence. }
simpl in H.
destruct blks as [| b2 blks].
{
destruct cid as [cid |].
- destruct (Ir.Memory.calltime m cid) as [cid' |] eqn:HCT.
+ simpl in HDEREF.
destruct (Ir.MemBlock.alive_before cid' (snd b1)).
* left. eexists. split.
rewrite HDEREF. reflexivity.
eapply Ir.Memory.blocks_get. assumption. reflexivity.
eapply Ir.Memory.inbounds_blocks2_In2.
eassumption.
* right. congruence.
+ left. eexists. rewrite HDEREF.
split. reflexivity.
eapply Ir.Memory.blocks_get. assumption. reflexivity.
eapply Ir.Memory.inbounds_blocks2_In2. eassumption.
- left. eexists. rewrite HDEREF.
split. reflexivity. eapply Ir.Memory.blocks_get. assumption. reflexivity.
eapply Ir.Memory.inbounds_blocks2_In2. eassumption.
}
{
simpl in H.
assert (S (S (length blks)) = 2 + length blks). (* -_-; *)
{ reflexivity. }
rewrite H0 in H.
apply Lt.lt_not_le in H.
exfalso.
apply H.
apply PeanoNat.Nat.le_add_r.
}
Qed.
Lemma get_deref_phy_nil_same:
forall m1 m2 bid mb bwid o cid ofs
(HGET:Some mb = Ir.Memory.get m1 bid)
(HWF1:Ir.Memory.wf m1)
(HWF2:Ir.Memory.wf m2)
(HSAME:Ir.Memory.get m1 bid = Ir.Memory.get m2 bid)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m1 (Ir.pphy o nil cid) bwid = (bid, mb, ofs)::nil)
(HCALLTIME:Ir.Memory.calltimes m1 = Ir.Memory.calltimes m2),
Ir.get_deref m1 (Ir.pphy o nil cid) bwid =
Ir.get_deref m2 (Ir.pphy o nil cid) bwid.
Proof.
intros.
unfold Ir.get_deref.
unfold Ir.get_deref_blks_phyptr.
assert (Ir.Memory.inbounds_blocks2 m1 [o; o + bwid] =
Ir.Memory.inbounds_blocks2 m2 [o; o + bwid]).
{ eapply Ir.Memory.inbounds_blocks2_same; try eassumption.
remember (Ir.Memory.inbounds_blocks2 m1 [o; o + bwid]) as l.
symmetry in Heql.
dup Heql.
apply Ir.Memory.inbounds_blocks2_singleton in Heql0.
unfold Ir.get_deref in HDEREF.
unfold Ir.get_deref_blks_phyptr in HDEREF.
rewrite Heql in HDEREF.
destruct l.
{ inv HDEREF. }
{ destruct l.
{ des_ifs. simpl in HDEREF. des_ifs. simpl in HDEREF. inv HDEREF.
destruct p. reflexivity.
simpl in HDEREF. inv HDEREF. destruct p. reflexivity.
simpl in HDEREF. inv HDEREF. destruct p. reflexivity.
}
{ simpl in Heql0. omega. }
}
assumption. omega.
}
rewrite H.
destruct (Ir.Memory.inbounds_blocks2 m2 [o; o + bwid]); try reflexivity.
destruct cid; try reflexivity.
unfold Ir.Memory.calltime. rewrite HCALLTIME.
des_ifs.
Qed.
Lemma get_deref_phy_I_cons:
forall m bid mb bwid o cid ofs I i
(HGET:Some mb = Ir.Memory.get m bid)
(HWF1:Ir.Memory.wf m)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m (Ir.pphy o I cid) bwid = (bid, mb, ofs)::nil),
(in_range i (Ir.MemBlock.P0_range mb) = true ->
Ir.get_deref m (Ir.pphy o (i::I) cid) bwid = (bid, mb, ofs)::nil) /\
(in_range i (Ir.MemBlock.P0_range mb) = false ->
Ir.get_deref m (Ir.pphy o (i::I) cid) bwid = nil).
Proof.
intros.
unfold Ir.get_deref in *.
unfold Ir.get_deref_blks_phyptr in *.
remember (Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: I)) as inbs1.
remember (Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: i :: I)) as inbs2.
assert (Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: i :: I) =
Ir.Memory.inbounds_blocks2 m (i :: o :: o + bwid :: I)).
{ eapply Ir.Memory.inbounds_blocks2_Permutation with (I := i::o::(o+bwid)::I).
apply perm_trans with (l' := o::i::o+bwid::I).
constructor. apply perm_skip. constructor.
reflexivity. }
rewrite H in *. clear H.
dup Heqinbs1. (* make sigleton *)
symmetry in Heqinbs0.
apply Ir.Memory.inbounds_blocks2_singleton2 in Heqinbs0.
destruct inbs1. inv HDEREF.
destruct inbs1; try (simpl in Heqinbs0; omega).
(* okay, it is singleton. *)
dup Heqinbs1.
symmetry in Heqinbs1.
destruct p.
apply Ir.Memory.inbounds_blocks2_cons with (i := i) in Heqinbs1.
destruct Heqinbs1.
(* okay, let's destruct cid now. *)
destruct cid.
{ destruct (Ir.Memory.calltime m c) eqn:HCT.
{ simpl in HDEREF.
destruct (Ir.MemBlock.alive_before t0 t) eqn:HALIVE.
{ (* okay, ready. *)
simpl in HDEREF. inv HDEREF.
split.
{ (* inbounds *)
intros HINB. apply H in HINB. rewrite HINB.
simpl. rewrite HALIVE. simpl. reflexivity. }
{ (* ot inbounds *)
intros HNOTINB. apply H0 in HNOTINB. rewrite HNOTINB.
reflexivity. }
}
{ (* okay, ready. *)
simpl in HDEREF. inv HDEREF. }
}
{
simpl in HDEREF. inv HDEREF.
split.
{ (* inbounds *)
intros HINB. apply H in HINB. rewrite HINB.
simpl. reflexivity. }
{ (* ot inbounds *)
intros HNOTINB. apply H0 in HNOTINB. rewrite HNOTINB.
reflexivity. }
}
}
{
simpl in HDEREF. inv HDEREF.
split.
{ (* inbounds *)
intros HINB. apply H in HINB. rewrite HINB.
simpl. reflexivity. }
{ (* ot inbounds *)
intros HNOTINB. apply H0 in HNOTINB. rewrite HNOTINB.
reflexivity. }
}
assumption.
omega.
Qed.
Lemma get_deref_phy_I_cons2:
forall m bwid o cid I i
(HWF1:Ir.Memory.wf m)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m (Ir.pphy o I cid) bwid = nil),
Ir.get_deref m (Ir.pphy o (i::I) cid) bwid = nil.
Proof.
intros.
unfold Ir.get_deref in *.
unfold Ir.get_deref_blks_phyptr in *.
assert ( Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: i :: I) =
Ir.Memory.inbounds_blocks2 m (i :: o :: o + bwid :: I)).
{ apply Ir.Memory.inbounds_blocks2_Permutation with (I := i::o::o+bwid::I).
eapply perm_trans. eapply perm_swap. eapply perm_skip.
eapply perm_swap. reflexivity. }
rewrite H in *. clear H.
destruct (Ir.Memory.inbounds_blocks2 m (o::o+bwid::I)) eqn:Hib2.
{ apply Ir.Memory.inbounds_blocks2_cons2 with (i := i) in Hib2. rewrite Hib2.
reflexivity.
}
{ dup Hib2.
apply Ir.Memory.inbounds_blocks2_singleton2 in Hib0; try congruence; try omega.
destruct l; try (simpl in Hib0; omega).
destruct p.
apply Ir.Memory.inbounds_blocks2_cons with (i := i) in Hib2.
destruct Hib2.
destruct (in_range i (Ir.MemBlock.P0_range t)).
{ exploit H. reflexivity. intros HH. rewrite HH.
rewrite HDEREF. reflexivity. }
{ exploit H0. reflexivity. intros HH. rewrite HH.
reflexivity. }
}
Qed.
Lemma get_deref_phy_I:
forall m bid mb bwid o cid ofs I
(HGET:Some mb = Ir.Memory.get m bid)
(HWF1:Ir.Memory.wf m)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m (Ir.pphy o nil cid) bwid = (bid, mb, ofs)::nil),
Ir.get_deref m (Ir.pphy o I cid) bwid = (bid, mb, ofs)::nil \/
Ir.get_deref m (Ir.pphy o I cid) bwid = nil.
Proof.
induction I.
{ intros. left. assumption. }
{ intros. exploit IHI; try assumption. intros HH.
destruct HH.
{ eapply get_deref_phy_I_cons with (i := a) in H; try assumption.
destruct H.
destruct (in_range a (Ir.MemBlock.P0_range mb)) eqn:HIN.
{ exploit H. reflexivity. intros. left. assumption. }
{ exploit H0. reflexivity. intros. right. assumption. }
}
{ right.
eapply get_deref_phy_I_cons2 in H; eassumption.
}
}
Qed.
Lemma get_deref_phy_I2:
forall m bwid o cid I
(HWF1:Ir.Memory.wf m)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m (Ir.pphy o nil cid) bwid = nil),
Ir.get_deref m (Ir.pphy o I cid) bwid = nil.
Proof. induction I.
{ intros. assumption. }
{ intros. exploit IHI; try assumption. intros HH.
eapply get_deref_phy_I_cons2 with (i := a) in HH; try assumption.
}
Qed.
Lemma get_deref_phy_I3:
forall m bid mb bwid o ofs I
(HGET:Some mb = Ir.Memory.get m bid)
(HALIVE:Ir.MemBlock.alive mb = true)
(HWF:Ir.Memory.wf m)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m (Ir.pphy o nil None) bwid = [(bid, mb, ofs)])
(HFORALLB:List.forallb (fun i => Ir.MemBlock.inbounds_abs i mb) I = true),
Ir.get_deref m (Ir.pphy o I None) bwid = [(bid, mb, ofs)].
Proof.
intros.
unfold Ir.get_deref in *.
unfold Ir.get_deref_blks_phyptr in *.
destruct (Ir.Memory.inbounds_blocks2 m [o; o + bwid]) eqn:HINB; try simpl in HDEREF;
try congruence.
destruct p.
simpl in *.
destruct l; simpl in HDEREF; try (inv HDEREF; fail).
inv HDEREF.
assert (Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: I) = [(bid, mb)]).
{ eapply Ir.Memory.inbounds_blocks2_singleton4; try assumption.
omega.
simpl.
simpl in HINB.
apply Ir.Memory.inbounds_blocks2_forallb2 in HINB.
simpl in HINB.
repeat (rewrite andb_true_r in HINB).
rewrite andb_true_iff in HINB.
destruct HINB.
rewrite H. rewrite H0.
simpl. assumption.
}
rewrite H. simpl. reflexivity.
Qed.
Lemma get_deref_phy_I_subseq:
forall m bid mb bwid o ofs I1 I2
(HGET:Some mb = Ir.Memory.get m bid)
(HWF:Ir.Memory.wf m)
(HSZ:bwid > 0)
(HLSS:lsubseq I1 I2)
(HDEREF:Ir.get_deref m (Ir.pphy o I1 None) bwid = [(bid, mb, ofs)]),
Ir.get_deref m (Ir.pphy o I2 None) bwid = [(bid, mb, ofs)].
Proof.
intros.
unfold Ir.get_deref in *.
unfold Ir.get_deref_blks_phyptr in *.
remember (Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: I1)) as blks1.
remember (Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: I2)) as blks2.
symmetry in Heqblks1.
symmetry in Heqblks2.
dup Heqblks1.
apply Ir.Memory.inbounds_blocks2_singleton2 in Heqblks1; try assumption; try omega.
destruct blks1. ss.
destruct blks1; simpl in HDEREF; try (inv HDEREF; fail).
destruct p. simpl in HDEREF. inversion HDEREF. subst b. subst t. subst ofs.
clear HDEREF.
dup Heqblks0.
eapply Ir.Memory.inbounds_blocks2_forallb2 in Heqblks3.
simpl in Heqblks3.
repeat (rewrite andb_true_r in Heqblks3).
rewrite andb_true_iff in Heqblks3. destruct Heqblks3.
rewrite andb_true_iff in H0. destruct H0.
assert (lsubseq blks2 [(bid, mb)]).
{ eapply Ir.Memory.inbounds_blocks2_lsubseq2.
eassumption.
eassumption.
constructor. constructor. assumption. }
apply Ir.Memory.inbounds_blocks2_singleton2 in Heqblks2; try assumption; try omega.
destruct blks2. inv H2.
destruct blks2.
inv H2. reflexivity.
inv H6.
simpl in Heqblks2. omega.
Qed.
Lemma get_deref_phy_cid:
forall m bid mb bwid o cid ofs I
(HGET:Some mb = Ir.Memory.get m bid)
(HWF1:Ir.Memory.wf m)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m (Ir.pphy o I None) bwid = (bid, mb, ofs)::nil),
Ir.get_deref m (Ir.pphy o I cid) bwid = (bid, mb, ofs)::nil \/
Ir.get_deref m (Ir.pphy o I cid) bwid = nil.
Proof.
intros.
unfold get_deref in *.
unfold get_deref_blks_phyptr in *.
destruct (Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: I)) eqn:HH;
try (inv HDEREF; fail).
simpl in HDEREF.
destruct l; try (inv HDEREF; fail).
simpl in HDEREF. inv HDEREF.
destruct cid.
{ destruct (Ir.Memory.calltime m c).
{ simpl.
destruct (Ir.MemBlock.alive_before t (snd p)).
{ simpl. left. reflexivity. }
{ right. reflexivity. }
}
{ left. simpl. reflexivity. }
}
{ left. reflexivity. }
Qed.
Lemma get_deref_phy_cid2:
forall m bwid o cid I
(HWF1:Ir.Memory.wf m)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m (Ir.pphy o I None) bwid = nil),
Ir.get_deref m (Ir.pphy o I cid) bwid = nil.
Proof.
intros.
unfold get_deref in *.
unfold get_deref_blks_phyptr in *.
destruct (Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: I)) eqn:HH;
try (inv HDEREF; fail).
reflexivity.
Qed.
Lemma get_deref_phy_cid3:
forall m bid mb bwid o cid ofs I
(HGET:Some mb = Ir.Memory.get m bid)
(HWF:Ir.Memory.wf m)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m (Ir.pphy o I cid) bwid = [(bid, mb, ofs)]),
Ir.get_deref m (Ir.pphy o I None) bwid = [(bid, mb, ofs)].
Proof.
intros.
unfold Ir.get_deref in *.
unfold Ir.get_deref_blks_phyptr in *.
remember (Ir.Memory.inbounds_blocks2 m (o :: o + bwid :: I)) as blks.
symmetry in Heqblks.
apply Ir.Memory.inbounds_blocks2_singleton2 in Heqblks; try assumption; try omega.
destruct blks. ss.
destruct blks.
des_ifs.
simpl in HDEREF. des_ifs.
simpl in Heqblks. omega.
Qed.
Lemma get_deref_blks_phyptr_inbounds_blocks2:
forall b t m o Is cid sz
(HIN:List.In (b, t) (Ir.get_deref_blks_phyptr m o Is cid sz)),
List.In (b, t) (Ir.Memory.inbounds_blocks2 m (o::o+sz::Is)).
Proof.
intros.
unfold Ir.get_deref_blks_phyptr in HIN.
des_ifs.
{ eapply List.filter_In. eassumption. }
Qed.
Lemma get_deref_phy_singleton:
forall (m:Ir.Memory.t) (m_wf:Ir.Memory.wf m) o Is cid (sz:nat) bos
(HSZ: 0 < sz)
(HDEREF: Ir.get_deref m (Ir.pphy o Is cid) sz = bos),
(exists bo, bos = bo::nil /\
Ir.Memory.get m bo.(fst).(fst) = Some bo.(fst).(snd) /\
o = Ir.MemBlock.addr (bo.(fst).(snd)) + (bo.(snd)))
\/ (bos = nil).
Proof.
intros.
unfold Ir.get_deref in HDEREF.
remember (Ir.get_deref_blks_phyptr m o Is cid sz) as blks.
assert ((exists bo0, blks = bo0::nil /\
Ir.Memory.get m bo0.(fst) = Some bo0.(snd))
\/ (blks = nil)).
{ eapply Ir.get_deref_blks_phyptr_singleton.
eassumption.
eassumption.
rewrite <- Heqblks. reflexivity. }
destruct H.
{ destruct H.
destruct H.
rewrite H in HDEREF.
simpl in HDEREF.
left. eexists. split. rewrite <- HDEREF.
reflexivity. simpl.
split. assumption.
destruct x. simpl in *.
assert (Ir.MemBlock.addr t <= o).
{ assert (List.In (b, t) blks).
{ rewrite H. simpl. left. reflexivity. }
rewrite Heqblks in H1.
apply get_deref_blks_phyptr_inbounds_blocks2 in H1.
remember (Ir.Memory.inbounds_blocks2 m (o :: o + sz :: Is)) as lt.
symmetry in Heqlt.
dup Heqlt.
apply In_pair_split_snd in H1.
apply Ir.Memory.inbounds_blocks2_forallb in Heqlt.
eapply forallb_In in Heqlt; try eassumption.
eapply Ir.MemBlock.inbounds_abs_addr in Heqlt. omega. reflexivity.
}
omega.
}
{ right. rewrite H in HDEREF. simpl in HDEREF. congruence. }
Qed.
(* Theorem: get_deref always returns at most one block. *)
Theorem get_deref_singleton:
forall (m:Ir.Memory.t) (m_wf:Ir.Memory.wf m) (p:Ir.ptrval) (sz:nat) bos
(HSZ: 0 < sz)
(HDEREF: get_deref m p sz = bos),
(exists bo, bos = bo::nil /\ Ir.Memory.get m bo.(fst).(fst) = Some bo.(fst).(snd))
\/ (bos = nil).
Proof.
intros.
destruct p as [bid ofs | o Is cid].
- (* logical ptr *)
unfold get_deref in HDEREF.
destruct (Ir.Memory.get m bid) eqn:HGET.
remember (Ir.MemBlock.alive t && Ir.MemBlock.inbounds ofs t &&
Ir.MemBlock.inbounds (ofs + sz) t) as cond in HDEREF.
destruct cond; rewrite <- HDEREF.
+ left. eexists. split. reflexivity.
simpl. assumption.
+ right. reflexivity.
+ right. congruence.
- unfold get_deref in HDEREF.
remember (get_deref_blks_phyptr m o Is cid sz) as blks.
assert ((exists bo0, blks = bo0::nil /\
Ir.Memory.get m bo0.(fst) = Some bo0.(snd))
\/ (blks = nil)).
{ eapply get_deref_blks_phyptr_singleton.
eassumption.
eassumption.
rewrite <- Heqblks. reflexivity. }
destruct H.
{ destruct H.
destruct H.
rewrite H in HDEREF.
simpl in HDEREF.
left. eexists. split. rewrite <- HDEREF.
reflexivity. simpl. assumption.
}
right. rewrite H in HDEREF. simpl in HDEREF. congruence.
Qed.
Lemma get_deref_phy_same:
forall m1 m2 bid mb bwid o cid ofs I
(HGET:Some mb = Ir.Memory.get m1 bid)
(HWF1:Ir.Memory.wf m1)
(HWF2:Ir.Memory.wf m2)
(HSAME:Ir.Memory.get m1 bid = Ir.Memory.get m2 bid)
(HSZ:bwid > 0)
(HDEREF:Ir.get_deref m1 (Ir.pphy o I cid) bwid = (bid, mb, ofs)::nil)
(HCALLTIME:Ir.Memory.calltimes m1 = Ir.Memory.calltimes m2),
Ir.get_deref m1 (Ir.pphy o I cid) bwid =
Ir.get_deref m2 (Ir.pphy o I cid) bwid.
Proof.
intros.
induction I.
{ eapply get_deref_phy_nil_same; try eassumption. }
{ remember (Ir.get_deref m1 (Ir.pphy o I cid) bwid) as bb.
symmetry in Heqbb.
dup Heqbb.
apply get_deref_singleton in Heqbb.
destruct Heqbb.
{ (* get m1 (fst (fst bo)) is [x]. *)
destruct H. destruct H.
destruct bb. inv H.
destruct bb.
{ inv H.
destruct x. destruct p. simpl in H0.
apply get_deref_phy_I_cons with (i := a) in Heqbb0; try congruence.
destruct Heqbb0.
destruct (in_range a (Ir.MemBlock.P0_range t)) eqn:HINR.
{ exploit H. reflexivity. intros HH.
dup HDEREF. rewrite HH in HDEREF0. inv HDEREF0.
rewrite HDEREF.
exploit IHI. reflexivity. intros HH'. symmetry in HH'.
apply get_deref_phy_I_cons with (i := a) in HH'; try congruence.
destruct HH'. rewrite H2. reflexivity. congruence.
}
{ exploit H1. reflexivity. intros HH.
dup HDEREF. rewrite HH in HDEREF0. inv HDEREF0. }
}
{ inv H. }
}
{ rewrite H in Heqbb0.
eapply get_deref_phy_I_cons2 with (i := a) in Heqbb0; try eassumption.
congruence.
}
{ eassumption. }
{ omega. }
}
Qed.
Lemma get_deref_ofs_lt_MEMSZ:
forall (m:Ir.Memory.t) (m_wf:Ir.Memory.wf m) p sz bid mb ofs
(HSZ:0 < sz)
(HDEREF:get_deref m p sz = (bid, mb, ofs)::nil),
Ir.MemBlock.addr mb + ofs < Ir.MEMSZ.
Proof.
intros.
assert (HS := get_deref_singleton m m_wf p sz ((bid, mb, ofs)::nil) HSZ HDEREF).
destruct HS.
- destruct H.
destruct H.
inversion H.
destruct x.
destruct p0.
inversion H2.
rewrite <- H3, <- H4, <- H5 in *. clear H3 H4 H5.
simpl in H0.
(* Now we have Ir.Memory.get m bid = Some mb. *)
assert (HINV := get_deref_inv m p bid ofs sz mb HSZ m_wf HDEREF H0).
rewrite andb_true_iff in HINV.
rewrite andb_true_iff in HINV.
destruct HINV as [[HINV1 HINV2] HINV3].
assert (Ir.MemBlock.addr mb + Ir.MemBlock.n mb < Ir.MEMSZ).
{
assert (Ir.MemBlock.wf mb).
{ eapply Ir.Memory.wf_blocks.
eassumption.
eapply Ir.Memory.get_In. symmetry. eassumption. reflexivity. }
apply Ir.MemBlock.wf_inmem.
{ assumption. }
unfold Ir.MemBlock.addr.
remember (Ir.MemBlock.P mb) as Ps.
assert (List.length Ps = Ir.TWINCNT).
{ rewrite HeqPs. apply Ir.MemBlock.wf_twin. assumption. }
destruct Ps.
{ simpl in H3. inversion H3. }
{ simpl. auto. }
}
assert (ofs < Ir.MemBlock.n mb).
{ unfold Ir.MemBlock.inbounds in HINV3.
rewrite Nat.leb_le in HINV3.
omega. }
omega.
- inversion H.
Qed.
(* Theorem: A logical pointer (bid, ofs) and a
physical pointer which is converted from (bid, ofs) both
access a same memory block with same offset, if
access size is larger than zero. *)
Theorem get_deref_ptr_phy_same:
forall (m:Ir.Memory.t) (m_wf:Ir.Memory.wf m) p p' (sz:nat) bo
(HSZ: 0 < sz)
(HDEREF: get_deref m p sz = bo::nil)
(HP':ptr_to_phy m p = Some p'),
get_deref m p' sz = bo::nil.
Proof.
intros.
destruct p as [bid ofs | o Is cid].
{
unfold ptr_to_phy in HP'.
unfold log_to_phy in HP'.
remember (Ir.Memory.get m bid) as blk.
symmetry in Heqblk.
destruct blk as [blk | ].
+ remember (Ir.MemBlock.alive blk) as alive.
remember (Ir.MemBlock.inbounds ofs blk) as inb1.
remember (Ir.MemBlock.inbounds (ofs + sz) blk) as inb2.
assert (HLOG := get_deref_log m bid ofs sz (bo::nil) blk HDEREF Heqblk).
destruct HLOG as [HLOG | HLOG]; try (inversion HLOG; fail).
inversion HLOG. rewrite H0 in *. clear H0 HLOG.
assert (HMOD: (Ir.MemBlock.addr blk + ofs) mod Ir.MEMSZ =
Ir.MemBlock.addr blk + ofs).
{ apply Nat.mod_small.
destruct bo. destruct p.
eapply get_deref_ofs_lt_MEMSZ.
eassumption. eassumption. eassumption. }
rewrite HMOD in HP'.
inversion HP'.
assert (HCOND: Ir.MemBlock.alive blk && Ir.MemBlock.inbounds ofs blk &&
Ir.MemBlock.inbounds (ofs + sz) blk = true).
{
eapply get_deref_log_inv.
eexists.
apply HDEREF.
assumption.
}
rewrite <- Heqalive, <- Heqinb1, <- Heqinb2 in HCOND.
destruct alive; destruct inb1; destruct inb2;
simpl in HCOND; try (inversion HCOND; fail).
rewrite Ir.MemBlock.inbounds_inbounds_abs
with (ofs_abs := Ir.MemBlock.addr blk + ofs)
in Heqinb1.
rewrite Ir.MemBlock.inbounds_inbounds_abs
with (ofs_abs := Ir.MemBlock.addr blk + ofs + sz)
in Heqinb2.
unfold get_deref.
unfold get_deref_blks_phyptr.
remember (Ir.Memory.inbounds_blocks2 m
(Ir.MemBlock.addr blk + ofs :: Ir.MemBlock.addr blk + ofs + sz :: nil))
as blks'.
assert (List.In (bid, blk) blks').
{ apply Ir.Memory.inbounds_blocks2_In with (m := m)
(abs_ofs1 := Ir.MemBlock.addr blk + ofs)
(abs_ofs2 := Ir.MemBlock.addr blk + ofs + sz).
- congruence.
- assumption.
- congruence.
- congruence.
- congruence.
- apply PeanoNat.Nat.lt_neq.
apply PeanoNat.Nat.lt_add_pos_r.
assumption.
}
assert (List.length blks' < 2).
{ eapply Ir.Memory.inbounds_blocks2_singleton with
(ofs1 := Ir.MemBlock.addr blk + ofs)
(ofs2 := Ir.MemBlock.addr blk + ofs + sz).
eassumption.
apply PeanoNat.Nat.lt_neq.
apply PeanoNat.Nat.lt_add_pos_r.
assumption.
congruence.
}
destruct blks' as [| blk1' blks'].
* inversion H.
* destruct blks'.
simpl.
simpl in H.
destruct H as [H | H].
-- rewrite H. simpl.
rewrite Minus.minus_plus.
reflexivity.
-- inversion H.
-- simpl in H1.
assert (2 + length blks' >= 2).
{ apply PeanoNat.Nat.le_add_r. }