-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.v
3969 lines (3754 loc) · 123 KB
/
Memory.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 Common.
Require Import List.
Require Import BinPos.
Require Import Bool.
Require Import PeanoNat.
Require Import Omega.
Require Import sflib.
Require Import Sorting.Permutation.
Module Ir.
(* Pointer value is 16 bits.
The reason why it is not set as 32 or 64 is that sometimes
it makes simpl and some tactics (almost) stop :( *)
Parameter PTRSZ : nat.
Axiom PTRSZ_def: PTRSZ = 16.
(* The size of memory. *)
Definition MEMSZ := Nat.shiftl 1 PTRSZ.
(* # of twin blocks. *)
Definition TWINCNT := 3.
(* A maximum value of alignment that will guarantee
success of machine-level memory access in this target.
A pointer returned by malloc() will have this alignment. *)
Definition SYSALIGN := 4.
Definition blockid := nat.
Definition callid := nat.
Definition time := nat.
Inductive ptrval :=
(*
-- Log(l, o) where 0 <= o < MEMSZ
-- Note that 0 <= o < MEMSZ is kept as invariant of memory.
-- Note: no address space
*)
| plog: blockid -> nat -> ptrval
(*
- Phy(o, I, cid) where 0 <= o, i(¡ô I) < MEMSZ
-- Note that 0 <= o < MEMSZ is kept as invariant of memory.
-- Note: no address space
*)
| pphy: nat -> list nat -> option callid -> ptrval.
(* NULL pointer. *)
Definition NULL := pphy 0 nil None.
Lemma MEMSZ_pos:
0 < Ir.MEMSZ.
Proof.
unfold Ir.MEMSZ.
destruct (0 =? Nat.shiftl 1 Ir.PTRSZ) eqn:H.
{ rewrite PeanoNat.Nat.eqb_eq in H.
symmetry in H.
rewrite Nat.shiftl_eq_0_iff in H. congruence. }
{ rewrite PeanoNat.Nat.eqb_neq in H. omega. }
Qed.
Lemma MEMSZ_nonzero:
Ir.MEMSZ <> 0.
Proof.
unfold Ir.MEMSZ.
rewrite Ir.PTRSZ_def.
intros HH. simpl in HH.
repeat (rewrite Nat.double_twice in HH).
omega.
Qed.
Lemma PTRSZ_MEMSZ:
Nat.shiftl 2 (Ir.PTRSZ - 1) = Ir.MEMSZ.
Proof. unfold Ir.MEMSZ; rewrite Ir.PTRSZ_def. reflexivity. Qed.
Lemma PTRSZ_MEMSZ2:
Nat.double (Nat.shiftl 1 (Ir.PTRSZ - 1)) = Ir.MEMSZ.
Proof. unfold Ir.MEMSZ; rewrite Ir.PTRSZ_def. reflexivity. Qed.
(* Are two pointers equivalent? *)
Definition ptr_eqb (p1 p2:ptrval): bool :=
match (p1, p2) with
| (plog bid1 ofs1, plog bid2 ofs2) =>
Nat.eqb bid1 bid2 && Nat.eqb ofs1 ofs2
| (pphy ofs1 I1 cid1, pphy ofs2 I2 cid2) =>
Nat.eqb ofs1 ofs2 &&
@list_inclb nat Nat.eq_dec I1 I2 &&
@list_inclb nat Nat.eq_dec I2 I1 &&
match (cid1, cid2) with
| (Some c1, Some c2) => Nat.eqb c1 c2
| (None, None) => true
| (_, _) => false
end
| (_, _) => false
end.
Lemma ptr_eqb_refl:
forall (p:ptrval), ptr_eqb p p = true.
Proof.
intros.
destruct p; unfold ptr_eqb.
- repeat (rewrite Nat.eqb_refl). reflexivity.
- rewrite Nat.eqb_refl.
rewrite list_inclb_refl.
destruct o. rewrite Nat.eqb_refl. reflexivity. reflexivity.
Qed.
(* block types.
stack: a block in stack
heap: a block in heap
global: a global variable
function: a function variable *)
Inductive blockty :=
| stack | heap | global | function.
Module Bit.
(* Definition of a bit. *)
Inductive t :=
| bint: bool -> t
(* (p, i). Note that 0 <= i < PTRSZ is kept as invariant. *)
| baddr: ptrval -> nat -> t
| bpoison: t.
Definition zero := bint false.
Definition one := bint true.
Definition null (i:nat) := baddr NULL i.
Definition bools_to_bit (bs:list bool): list t :=
List.map (fun b => bint b) bs.
Fixpoint erase_lzerobits (bits:list t): list t :=
match bits with
| nil => nil
| (bint h)::t =>
match h with | false => erase_lzerobits t | true => bits
end
| x => x
end.
Definition erase_hzerobits (bits:list t): list t :=
List.rev (erase_lzerobits (List.rev bits)).
Definition add_lzerobits (bits:list t) (n:nat): list t :=
List.repeat (bint false) n ++ bits.
Definition add_hzerobits (bits:list t) (n:nat): list t :=
bits ++ List.repeat (bint false) n.
(**************************************************
Number <-> bits (list bool)
*************************************************)
Fixpoint pos_to_bits (n:positive): list t :=
match n with
| xH => (bint true)::nil
| xI n' => (bint true)::(pos_to_bits n')
| xO n' => (bint false)::(pos_to_bits n')
end.
Definition N_to_bits (n:N): list t :=
match n with
| N0 => nil
| Npos p => pos_to_bits p
end.
Eval compute in N_to_bits 0%N. (* nil *)
Eval compute in N_to_bits 10%N. (* [f,t,f,t] *)
Fixpoint _bits_to_pos (bits:list t): positive :=
match bits with
| nil => xH
| (bint true)::nil => xH
| (bint h)::t => (if h then xI else xO) (_bits_to_pos t)
| _ => xH
end.
Definition bits_to_pos (bits:list t): positive :=
_bits_to_pos (erase_hzerobits bits).
Definition bits_to_N (bits:list t): N :=
match bits with
| nil => N0
| _ => Npos (bits_to_pos bits)
end.
Definition nonzero (b:t): bool :=
match b with
| bint false => false
| _ => true
end.
Eval compute in bits_to_N nil. (* 0 *)
Eval compute in bits_to_N (bint true::bint false::bint true::nil). (* 5 *)
Eval compute in erase_lzerobits (bint false::bint false::bint true::nil).
Eval compute in erase_hzerobits (bint false::bint false::bint true::bint false::nil).
Eval compute in add_lzerobits (bint true::bint false::bint true::nil) 2.
Eval compute in add_hzerobits (bint true::bint false::bint true::nil) 2.
(**********************************************
Lemmas about bit.
**********************************************)
Lemma erase_lzerobits_app:
forall (l1 l2:list t)
(HNOTIN:List.existsb nonzero l1 = true),
erase_lzerobits (l1 ++ l2) = (erase_lzerobits l1) ++ l2.
Proof.
intros.
induction l1.
- simpl in HNOTIN. inversion HNOTIN.
- simpl in HNOTIN.
simpl.
destruct a.
+ destruct b. simpl. reflexivity.
simpl in HNOTIN.
apply IHl1 in HNOTIN.
assumption.
+ simpl. reflexivity.
+ simpl. reflexivity.
Qed.
Lemma pos_to_bits_nonzero:
forall (p:positive), List.existsb nonzero (pos_to_bits p) = true.
Proof.
intros.
induction p.
- unfold pos_to_bits.
simpl. reflexivity.
- unfold pos_to_bits. simpl. fold pos_to_bits. assumption.
- simpl. reflexivity.
Qed.
Lemma erase_hzerobits_pos_to_bits:
forall (p:positive), erase_hzerobits (pos_to_bits p) = pos_to_bits p.
Proof.
intros.
induction p.
- unfold erase_hzerobits.
unfold pos_to_bits.
simpl.
rewrite erase_lzerobits_app.
+ rewrite rev_app_distr.
simpl. fold pos_to_bits.
unfold erase_hzerobits in IHp.
rewrite IHp. reflexivity.
+ fold pos_to_bits. rewrite existsb_rev.
apply pos_to_bits_nonzero.
- unfold erase_hzerobits.
simpl.
rewrite erase_lzerobits_app.
+ rewrite rev_app_distr.
simpl. unfold erase_hzerobits in IHp. rewrite IHp. reflexivity.
+ rewrite existsb_rev. apply pos_to_bits_nonzero.
- reflexivity.
Qed.
Lemma pos_to_bits_nonnil:
forall (p:positive), ~ (pos_to_bits p = nil).
Proof.
intros.
destruct p; intros H; inversion H.
Qed.
Lemma pos_bits_pos:
forall (p:positive), bits_to_pos (pos_to_bits p) = p.
Proof.
intros.
unfold bits_to_pos.
rewrite erase_hzerobits_pos_to_bits.
induction p.
- remember (pos_to_bits p) as bs.
destruct bs. exfalso. eapply pos_to_bits_nonnil. rewrite <- Heqbs. reflexivity.
simpl.
rewrite <- Heqbs.
simpl in *.
destruct t0.
+ destruct b. rewrite IHp. reflexivity.
rewrite IHp. reflexivity.
+ rewrite <- IHp. reflexivity.
+ rewrite <- IHp. reflexivity.
- simpl.
rewrite IHp. reflexivity.
- reflexivity.
Qed.
Lemma N_bits_N:
forall (n:N), bits_to_N (N_to_bits n) = n.
Proof.
intros.
destruct n.
- reflexivity.
- unfold bits_to_N.
unfold N_to_bits.
rewrite pos_bits_pos.
assert (~ pos_to_bits p = nil).
{ apply pos_to_bits_nonnil. }
destruct (pos_to_bits p).
{ exfalso. apply H. reflexivity. }
{ reflexivity. }
Qed.
End Bit.
Module Byte.
(* Definition of a byte. *)
Structure t := mk
{b0:Bit.t;
b1:Bit.t;
b2:Bit.t;
b3:Bit.t;
b4:Bit.t;
b5:Bit.t;
b6:Bit.t;
b7:Bit.t}.
Definition zero := mk Bit.zero Bit.zero Bit.zero Bit.zero
Bit.zero Bit.zero Bit.zero Bit.zero.
Definition one := mk Bit.one Bit.zero Bit.zero Bit.zero
Bit.zero Bit.zero Bit.zero Bit.zero.
Definition mone := mk Bit.one Bit.one Bit.one Bit.one
Bit.one Bit.one Bit.one Bit.one.
Definition imax := mk Bit.zero Bit.one Bit.one Bit.one
Bit.one Bit.one Bit.one Bit.one.
Definition imin := mk Bit.one Bit.zero Bit.zero Bit.zero
Bit.zero Bit.zero Bit.zero Bit.zero.
Definition null i := mk (Bit.null (8*i)) (Bit.null (8*i+1)) (Bit.null (8*i+2))
(Bit.null (8*i+3)) (Bit.null (8*i+4)) (Bit.null (8*i+5))
(Bit.null (8*i+6)) (Bit.null (8*i+7)).
Definition poison := mk Bit.bpoison Bit.bpoison Bit.bpoison Bit.bpoison
Bit.bpoison Bit.bpoison Bit.bpoison Bit.bpoison.
Fixpoint from_bits (bs:list Bit.t): list t :=
match bs with
| nil => nil
| b1::nil =>
(mk b1 Bit.bpoison Bit.bpoison Bit.bpoison
Bit.bpoison Bit.bpoison Bit.bpoison Bit.bpoison)::nil
| b1::b2::nil =>
(mk b1 b2 Bit.bpoison Bit.bpoison
Bit.bpoison Bit.bpoison Bit.bpoison Bit.bpoison)::nil
| b1::b2::b3::nil =>
(mk b1 b2 b3 Bit.bpoison
Bit.bpoison Bit.bpoison Bit.bpoison Bit.bpoison)::nil
| b1::b2::b3::b4::nil =>
(mk b1 b2 b3 b4
Bit.bpoison Bit.bpoison Bit.bpoison Bit.bpoison)::nil
| b1::b2::b3::b4::b5::nil =>
(mk b1 b2 b3 b4
b5 Bit.bpoison Bit.bpoison Bit.bpoison)::nil
| b1::b2::b3::b4::b5::b6::nil =>
(mk b1 b2 b3 b4
b5 b6 Bit.bpoison Bit.bpoison)::nil
| b1::b2::b3::b4::b5::b6::b7::nil =>
(mk b1 b2 b3 b4
b5 b6 b7 Bit.bpoison)::nil
| b1::b2::b3::b4::b5::b6::b7::b8::t =>
(mk b1 b2 b3 b4
b5 b6 b7 b8)::from_bits t
end.
Definition to_bits (bs:list t): list Bit.t :=
List.concat (List.map (fun b =>
b.(b0)::b.(b1)::b.(b2)::b.(b3)::b.(b4)::b.(b5)::b.(b6)::b.(b7)::nil)
bs).
(* Check whether bs have all integer bits.
If it have, return the integer. *)
Definition getint (bs: list Byte.t) (bitsz:nat): option N :=
let bits := List.firstn bitsz (to_bits bs) in
if (List.forallb (fun b => match b with | Bit.bint _ => true | _ => false end)
bits) then
Some (Bit.bits_to_N bits)
else None.
Definition ofint (i:N) (bitsz:nat): list Byte.t :=
let bits := Bit.N_to_bits i in
from_bits (Bit.add_hzerobits bits (bitsz - List.length bits)).
Eval compute in getint (zero::zero::nil) 2.
Eval compute in getint (one::zero::nil) 2.
Eval compute in ofint (10%N) 9.
(* Check whether b has 8 pointer bits (p, i), (p, i + 1, ..)
If it has, this returns (p, i). *)
Definition getpbits (b: Byte.t): option (ptrval * nat) :=
match (b.(b0), b.(b1), b.(b2), b.(b3), b.(b4), b.(b5), b.(b6), b.(b7)) with
| (Bit.baddr p0 n0, Bit.baddr p1 n1,
Bit.baddr p2 n2, Bit.baddr p3 n3,
Bit.baddr p4 n4, Bit.baddr p5 n5,
Bit.baddr p6 n6, Bit.baddr p7 n7) =>
if (Nat.eqb n1 (1 + n0)) && (Nat.eqb n2 (2 + n0)) && (Nat.eqb n3 (3 + n0)) &&
(Nat.eqb n4 (4 + n0)) && (Nat.eqb n5 (5 + n0)) && (Nat.eqb n6 (6 + n0)) &&
(Nat.eqb n7 (7 + n0)) &&
ptr_eqb p0 p1 && ptr_eqb p0 p2 && ptr_eqb p0 p3 && ptr_eqb p0 p4 &&
ptr_eqb p0 p5 && ptr_eqb p0 p6 && ptr_eqb p0 p7 then
Some (p0, n0)
else None
| (_, _, _, _, _, _, _, _) => None
end.
(* Check whether bs have all pointer bits.
If it have, return the integer. *)
Definition getptr (bs:list Byte.t): option ptrval :=
if Nat.eqb (8 * List.length bs) PTRSZ then
(* Should be the size of pointer *)
match bs with
| nil => None (* This wouldn't happen. *)
| hd::tl => (* hd is the first byte of the pointer value. *)
let hdp := getpbits hd in
match (hdp) with
| Some (p0, 0) => (* lowest byte has the lowest portion of pointer p0. *)
let tl' := List.map getpbits tl in
(* Is i'th byte a byte of (p0, i)? *)
let alleq := List.fold_left
(fun i pb =>
match i, pb with
| _, None => None
| None, _ => None
| Some i, Some (p, ofs) =>
if ptr_eqb p p0 && Nat.eqb ofs (8 + i) then
Some ofs
else None
end)
tl' (Some 0) in
match alleq with
| None => None
| Some _ => Some p0
end
| _ => None (* Bits of the first byte isn't the zero-th byte of pointer value *)
end
end
else None.
Definition ofptr (bs:ptrval): list Byte.t
:= from_bits
(List.map (fun i => Bit.baddr i.(snd) i.(fst))
(number_list (List.repeat bs PTRSZ))).
Eval compute in getpbits (null 0).
Eval compute in getpbits (null 1).
Eval compute in getptr ((null 0)::nil). (* None *)
Eval compute in getptr ((null 0)::(null 1)::nil). (* Some (pphy (0, nil, None)). *)
Eval compute in getptr ((null 0)::(null 1)::(null 2)::nil). (* None *)
(********************************************
Lemmas about bits & bytes.
********************************************)
Lemma from_bits_nonnil:
forall b bs,
nil <> from_bits (b::bs).
Proof.
intros.
destruct bs as [| b1 bs].
{ intros H. simpl in H. inversion H. }
destruct bs as [| b2 bs].
{ intros H. simpl in H. inversion H. }
destruct bs as [| b3 bs].
{ intros H. simpl in H. inversion H. }
destruct bs as [| b4 bs].
{ intros H. simpl in H. inversion H. }
destruct bs as [| b5 bs].
{ intros H. simpl in H. inversion H. }
destruct bs as [| b6 bs].
{ intros H. simpl in H. inversion H. }
destruct bs as [| b7 bs].
{ intros H. simpl in H. inversion H. }
intros H. simpl in H. inversion H.
Qed.
Lemma from_bits_inv:
forall bytes l1 l2 a
(HLEN:List.length l1 = 8)
(H:a :: bytes = from_bits (l1 ++ l2)),
bytes = from_bits l2.
Proof.
intros.
destruct l1 as [| h0 l1]. inversion HLEN.
destruct l1 as [| h1 l1]. inversion HLEN.
destruct l1 as [| h2 l1]. inversion HLEN.
destruct l1 as [| h3 l1]. inversion HLEN.
destruct l1 as [| h4 l1]. inversion HLEN.
destruct l1 as [| h5 l1]. inversion HLEN.
destruct l1 as [| h6 l1]. inversion HLEN.
destruct l1 as [| h7 l1]. inversion HLEN.
destruct l1 as [| h8 l1].
simpl.
simpl in H. inversion H. reflexivity.
simpl in HLEN. omega.
Qed.
Lemma from_bits_to_bits:
forall (ls:list Bit.t),
to_bits (from_bits ls) =
ls ++ List.repeat (Bit.bpoison)
(Nat.modulo ((8 - Nat.modulo (List.length ls) 8)) 8).
Proof.
intros.
assert (HB:=list_segmentize8_r ls).
remember (from_bits ls) as bytes.
generalize dependent ls.
induction bytes.
- intros.
destruct ls.
+ simpl. reflexivity.
+ simpl in Heqbytes.
destruct ls as [ | h1 ls].
inversion Heqbytes.
destruct ls as [ | h2 ls].
inversion Heqbytes.
destruct ls as [ | h3 ls].
inversion Heqbytes.
destruct ls as [ | h4 ls].
inversion Heqbytes.
destruct ls as [ | h5 ls].
inversion Heqbytes.
destruct ls as [ | h6 ls].
inversion Heqbytes.
destruct ls as [ | h7 ls].
inversion Heqbytes.
inversion Heqbytes.
- intros.
assert (to_bits (a::bytes) = (to_bits (a::nil)) ++ to_bits bytes).
{ unfold to_bits.
simpl. reflexivity. }
rewrite H. clear H.
(* ls is a list of bit. *)
destruct HB as [ls1 [ls2 [HB1 [HB2 HB3]]]].
(* ls = ls1 ++ ls2.
|ls2| < 8 *)
assert (ls1 = nil \/
(exists ls1' ls1'', List.length ls1' = 8 /\ ls1 = ls1' ++ ls1'')).
{ remember (List.length ls1) as n1.
assert (HN1 := Nat.eq_dec n1 0).
destruct HN1.
- left. rewrite Heqn1 in e.
rewrite length_zero_iff_nil in e. congruence.
- assert (HSP:=list_split8_l ls1 n1 Heqn1 HB2).
destruct HSP as [ls1h [ls1t [HSP1 [HSP2 HSP3]]]].
assumption.
right.
exists ls1h.
exists ls1t.
split. assumption. assumption.
}
(* ls = ls1 ++ ls2
ls --from_bits--> a::bytes
|ls2| < 8 *)
destruct H as [H | H].
+ rewrite H in HB1.
(* ls1 = nil
bytes = nil *)
simpl in HB1.
rewrite HB1 in *.
clear HB1.
destruct ls2 as [| h20 ls2].
{ simpl in Heqbytes. inversion Heqbytes. }
destruct ls2 as [| h21 ls2].
{ simpl in Heqbytes.
inversion Heqbytes.
rewrite H2 in *. clear H2.
reflexivity. }
destruct ls2 as [| h22 ls2].
{ simpl in Heqbytes.
inversion Heqbytes.
rewrite H2 in *. clear H2.
reflexivity. }
destruct ls2 as [| h23 ls2].
{ simpl in Heqbytes.
inversion Heqbytes.
rewrite H2 in *. clear H2.
reflexivity. }
destruct ls2 as [| h24 ls2].
{ simpl in Heqbytes.
inversion Heqbytes.
rewrite H2 in *. clear H2.
reflexivity. }
destruct ls2 as [| h25 ls2].
{ simpl in Heqbytes.
inversion Heqbytes.
rewrite H2 in *. clear H2.
reflexivity. }
destruct ls2 as [| h26 ls2].
{ simpl in Heqbytes.
inversion Heqbytes.
rewrite H2 in *. clear H2.
reflexivity. }
destruct ls2 as [| h27 ls2].
{ simpl in Heqbytes.
inversion Heqbytes.
rewrite H2 in *. clear H2.
reflexivity. }
simpl in HB3.
omega.
+ destruct H as [ls1head [ls1tail H]].
destruct H as [H1 H2].
rewrite H2 in HB1.
rewrite HB1.
rewrite HB1 in Heqbytes.
(* l ++ m ++ n = (l ++ m) ++ n *)
rewrite <- app_assoc with (l := ls1head) (m := ls1tail)
(n := ls2).
rewrite <- app_assoc with (l := ls1head) (m := ls1tail ++ ls2).
replace (length (ls1head ++ ls1tail ++ ls2) mod 8) with
(length (ls1tail ++ ls2) mod 8).
rewrite <- IHbytes with (ls := ls1tail++ls2).
destruct ls1head as [| h0 ls1head].
{ simpl in H1. inversion H1. }
destruct ls1head as [| h1 ls1head].
{ simpl in H1. inversion H1. }
destruct ls1head as [| h2 ls1head].
{ simpl in H1. inversion H1. }
destruct ls1head as [| h3 ls1head].
{ simpl in H1. inversion H1. }
destruct ls1head as [| h4 ls1head].
{ simpl in H1. inversion H1. }
destruct ls1head as [| h5 ls1head].
{ simpl in H1. inversion H1. }
destruct ls1head as [| h6 ls1head].
{ simpl in H1. inversion H1. }
destruct ls1head as [| h7 ls1head].
{ simpl in H1. inversion H1. }
destruct ls1head as [| h8 ls1head].
{ simpl in Heqbytes.
inversion Heqbytes.
simpl. reflexivity. }
{ simpl in H1. inversion H1. }
exists ls1tail. exists ls2.
split. reflexivity.
split. rewrite H2 in HB2.
rewrite app_length in HB2.
rewrite H1 in HB2.
rewrite <- Nat.add_mod_idemp_l in HB2.
simpl in HB2.
simpl. assumption.
omega.
assumption.
eapply from_bits_inv.
eassumption. rewrite <- app_assoc in Heqbytes. eassumption.
rewrite app_length with (l := ls1head).
rewrite H1.
rewrite <- Nat.add_mod_idemp_l.
simpl. reflexivity.
omega.
Qed.
Lemma N_to_bits_notbaddr:
forall n b
(HIN:List.In b (Ir.Bit.N_to_bits n)),
forall p ofs, b <> Ir.Bit.baddr p ofs.
Proof.
intros n b HIN.
generalize dependent b.
induction n.
{ intros. simpl in HIN. inv HIN. }
{ intros.
simpl in HIN.
generalize dependent b.
induction p.
{ simpl. intros. destruct HIN. rewrite <- H. ss.
apply IHp in H. ss. }
{ simpl. intros. destruct HIN. rewrite <- H. ss.
apply IHp in H. ss. }
{ simpl. intros. inv HIN. ss. inv H. }
}
Qed.
Lemma add_hzerobits_notbaddr:
forall bits n
(HFORALL:List.Forall (fun b => forall p ofs, b <> Ir.Bit.baddr p ofs) bits),
List.Forall (fun b => forall p ofs, b <> Ir.Bit.baddr p ofs)
(Ir.Bit.add_hzerobits bits n).
Proof.
intros.
unfold Ir.Bit.add_hzerobits.
apply Forall_app2. ss.
apply Forall_repeat. ss.
Qed.
Lemma from_bits_notbaddr:
forall bits
(HFORALL:List.Forall (fun b => forall p ofs, b <> Ir.Bit.baddr p ofs) bits),
List.Forall (fun b =>
forall p ofs, b.(Ir.Byte.b0) <> Ir.Bit.baddr p ofs /\
b.(Ir.Byte.b1) <> Ir.Bit.baddr p ofs /\
b.(Ir.Byte.b2) <> Ir.Bit.baddr p ofs /\
b.(Ir.Byte.b3) <> Ir.Bit.baddr p ofs /\
b.(Ir.Byte.b4) <> Ir.Bit.baddr p ofs /\
b.(Ir.Byte.b5) <> Ir.Bit.baddr p ofs /\
b.(Ir.Byte.b6) <> Ir.Bit.baddr p ofs /\
b.(Ir.Byte.b7) <> Ir.Bit.baddr p ofs) (Ir.Byte.from_bits bits).
Proof.
intros.
remember (Ir.Byte.from_bits bits) as byt.
generalize dependent bits.
induction byt.
{ simpl. intros. constructor. }
{ intros.
destruct bits. simpl in Heqbyt. inv Heqbyt.
destruct bits.
{ simpl in Heqbyt. inv Heqbyt. constructor. simpl.
inv HFORALL.
repeat (split; try congruence).
constructor. }
destruct bits.
{ simpl in Heqbyt. inv Heqbyt. constructor. simpl.
inv HFORALL. inv H2.
repeat (split; try congruence).
constructor. }
destruct bits.
{ simpl in Heqbyt. inv Heqbyt. constructor. simpl.
inv HFORALL. inv H2. inv H4.
repeat (split; try congruence).
constructor. }
destruct bits.
{ simpl in Heqbyt. inv Heqbyt. constructor. simpl.
inv HFORALL. inv H2. inv H4. inv H5.
repeat (split; try congruence).
constructor. }
destruct bits.
{ simpl in Heqbyt. inv Heqbyt. constructor. simpl.
inv HFORALL. inv H2. inv H4. inv H5. inv H6.
repeat (split; try congruence).
constructor. }
destruct bits.
{ simpl in Heqbyt. inv Heqbyt. constructor. simpl.
inv HFORALL. inv H2. inv H4. inv H5. inv H6. inv H7.
repeat (split; try congruence).
constructor. }
destruct bits.
{ simpl in Heqbyt. inv Heqbyt. constructor. simpl.
inv HFORALL. inv H2. inv H4. inv H5. inv H6. inv H7 . inv H8.
repeat (split; try congruence).
constructor. }
{ simpl in Heqbyt. inv Heqbyt. constructor. simpl.
inv HFORALL. inv H2. inv H4. inv H5. inv H6. inv H7 . inv H8. inv H9.
repeat (split; try congruence).
remember (t0::t1::t2::t3::t4::t5::t6::t7::nil) as l.
assert (t0::t1::t2::t3::t4::t5::t6::t7::bits = l ++ bits).
{ rewrite Heql. reflexivity. }
rewrite H in *.
apply Forall_app in HFORALL. inv HFORALL.
exploit IHbyt. eassumption. ss. eauto.
}
}
Qed.
End Byte.
Module MemBlock.
(* Block := (t, r, n, a, c, P)
Note that |P| == twin# is kept as invariant. *)
Structure t := mk
{
bt: blockty; r:time * option time;
n: nat; a: nat; c:list (Byte.t);
P: list nat
}.
(* Returns (start_ofs, size)s that include all twin blocks. *)
Definition P_ranges (mb:t):list (nat * nat) :=
List.map (fun ofs => (ofs, mb.(n))) mb.(P).
(* Returns integer address of the block. *)
Definition addr (mb:t): nat :=
List.hd 0 mb.(P).
(* Returns (start_ofs, size) of the using one. *)
Definition P0_range (mb:t): nat * nat :=
(addr mb, mb.(n)).
(* Well-formendess of a memory block. *)
Structure wf (mb:t) := mkWf
{
(* wf_tcond: Wellformedness of lifetime of a block. *)
wf_tcond: forall t (FREED:mb.(r).(snd) = Some t), mb.(r).(fst) < t;
(* wf_clen: length of bytes is equivalent to n. *)
wf_clen: List.length mb.(c) = mb.(n);
(* wf_poslen: There's no zero-size block in the memory.
In this formalization, malloc(0) returns NULL, so this invariant
holds. *)
wf_poslen: no_empty_range (P_ranges mb) = true;
(* wf_align: alignment criteria *)
wf_align: forall p (HAS:List.In p mb.(P)), Nat.modulo p mb.(a) = 0;
(* wf_mem: Note that this is "<", not "<=", because p + n wouldn't
be representable in 2^32 bits *)
wf_inmem: forall p (HAS:List.In p mb.(P)), p + mb.(n) < MEMSZ;
(* wf_notnull: block starting offset cannot be 0
(because this formalization assumes that address space is always 0) *)
wf_notnull: forall p (HAS:List.In p mb.(P)), ~ (p = 0);
(* all twin blocks are disjoint *)
wf_disj: disjoint_ranges (P_ranges mb) = true;
(* has correct number of twin blocks. *)
wf_twin: List.length mb.(P) = TWINCNT
}.
(* is block t alive? *)
Definition alive (mb:t): bool :=
match mb.(r).(snd) with
| None => true | Some _ => false
end.
(* Is block t alive before time the_time? *)
Definition alive_before (the_time:nat) (mb:t): bool :=
Nat.ltb mb.(r).(fst) the_time.
Definition lifetime_to_range (cur_time:nat) (mb:t): nat * nat :=
(mb.(r).(fst),
match mb.(r).(snd) with
| None => cur_time | Some r => r
end - mb.(r).(fst)).
(* 0 <= ofs <= block size of mb? *)
Definition inbounds (ofs:nat) (mb:t): bool :=
Nat.leb ofs mb.(n).
(* start_ofs <= ofs <= start_ofs + block size of mb? *)
Definition inbounds_abs (ofs':nat) (mb:t): bool :=
in_range ofs' (P0_range mb).
(* Get bytes in (offset, offset +len) *)
Definition bytes (mb:t) (ofs len:nat): list (Byte.t) :=
List.firstn len (List.skipn ofs mb.(c)).
(* Update bytes. *)
Definition set_bytes (mb:t) (ofs:nat) (bytes:list (Byte.t)): t :=
mk mb.(bt) mb.(r) mb.(n) mb.(a)
(List.firstn ofs (mb.(c)) ++ bytes ++
List.skipn (ofs + List.length bytes) mb.(c))
mb.(P).
(* Set the end of lifetime if it was alive. *)
Definition set_lifetime_end (mb:t) (newt:time): option t :=
if alive mb then
Some (mk mb.(bt) (mb.(r).(fst), Some newt)
mb.(n) mb.(a) mb.(c) mb.(P))
else None.
(**********************************************
Lemmas&Theorems about MemBlock.
**********************************************)
Lemma P_P0_range_lsubseq:
forall mb (HWF:wf mb),
lsubseq (P_ranges mb) ((P0_range mb)::nil).
Proof.
intros.
unfold P_ranges.
unfold P0_range.
destruct (MemBlock.P mb) as [| P0 Pt] eqn:HP1.
{ (* cannot be nil. *)
assert (List.length (MemBlock.P mb) = 0).
{ rewrite HP1. reflexivity. }
rewrite (MemBlock.wf_twin) in H. inversion H. assumption.
}
unfold addr.
rewrite HP1. simpl.
constructor.
constructor.
Qed.
Lemma inbounds_inbounds_abs:
forall (mb:t) ofs ofs_abs
(HABS: ofs_abs = ofs + addr mb),
inbounds ofs mb = inbounds_abs ofs_abs mb.
Proof.
intros.
unfold inbounds.
unfold inbounds_abs.
rewrite HABS.
unfold Common.in_range.
unfold Ir.MemBlock.P0_range.
remember (Ir.MemBlock.addr mb) as addr.
remember (Ir.MemBlock.n mb) as n.
simpl.
assert (PeanoNat.Nat.leb addr (ofs + addr) = true).
{
rewrite PeanoNat.Nat.leb_le.
apply Plus.le_plus_r.
}
rewrite H.
simpl.
rewrite PeanoNat.Nat.add_comm with (n := ofs) (m := addr).
remember (PeanoNat.Nat.leb (addr + ofs) (addr + n)) as flag.
symmetry in Heqflag.
destruct flag.
- rewrite PeanoNat.Nat.leb_le in Heqflag.
apply Plus.plus_le_reg_l in Heqflag.
rewrite PeanoNat.Nat.leb_le.
assumption.
- rewrite PeanoNat.Nat.leb_nle in Heqflag.
rewrite PeanoNat.Nat.leb_nle.
intros H'.
apply Heqflag.
apply Plus.plus_le_compat_l.
assumption.
Qed.
Lemma inbounds_mod:
forall mb (HWF:Ir.MemBlock.wf mb) ofs
(HINB:Ir.MemBlock.inbounds ofs mb = true),
(Ir.MemBlock.addr mb + ofs) mod Ir.MEMSZ = Ir.MemBlock.addr mb + ofs.
Proof.
intros.
rewrite Nat.mod_small. reflexivity.
inv HWF.
unfold Ir.MemBlock.inbounds in HINB.
rewrite PeanoNat.Nat.leb_le in HINB.
eapply Nat.le_lt_trans with (m := Ir.MemBlock.addr mb +Ir.MemBlock.n mb).
omega.
apply wf_inmem0.
unfold Ir.MemBlock.addr.
destruct (Ir.MemBlock.P mb).
simpl in wf_twin0. inv wf_twin0.
simpl.
left. reflexivity.
Qed.
(* Thanks to twin blocks, size of a block cannot equal to or be larger than a half of
memory size. *)
Lemma blocksz_lt:
forall mb (HWF:Ir.MemBlock.wf mb),
~ (Ir.MemBlock.n mb >= Nat.shiftl 1 (Ir.PTRSZ - 1)).
Proof.
intros.
intros H.
inv HWF.
unfold Ir.MemBlock.P_ranges in wf_disj0.
destruct (Ir.MemBlock.P mb).
simpl in wf_twin0. inv wf_twin0.
destruct l. simpl in wf_twin0. inv wf_twin0.
simpl in wf_disj0.
rewrite andb_true_iff in wf_disj0.
rewrite andb_true_iff in wf_disj0.
rewrite andb_true_iff in wf_disj0.
destruct wf_disj0.
destruct H0. clear H1. clear H2.
unfold disjoint_range in H0.
rewrite orb_true_iff in H0.
rewrite PeanoNat.Nat.leb_le in H0.
rewrite PeanoNat.Nat.leb_le in H0.
assert (Ir.MEMSZ = (Nat.shiftl 1 (Ir.PTRSZ - 1)) +
(Nat.shiftl 1 (Ir.PTRSZ - 1))).
{ unfold MEMSZ. rewrite Ir.PTRSZ_def. reflexivity. }
destruct H0.
{ exploit wf_inmem0.
simpl. right. left. reflexivity.
intros HH.
omega.
}
{ exploit wf_inmem0.
simpl. left. reflexivity.
intros HH. omega.
}
Qed.
Lemma inbounds_abs_lt_MEMSZ:
forall mb i
(HWF:Ir.MemBlock.wf mb)
(HINB:Ir.MemBlock.inbounds_abs i mb = true),
i < Ir.MEMSZ.
Proof.
intros.
unfold Ir.MemBlock.inbounds_abs in HINB.
unfold in_range in HINB.
rewrite andb_true_iff in HINB.
destruct HINB.
rewrite PeanoNat.Nat.leb_le in H0.
unfold Ir.MemBlock.P0_range in *.
simpl in *.
destruct HWF.
eapply le_lt_trans.
eassumption.
apply wf_inmem0.
unfold Ir.MemBlock.addr.
destruct (Ir.MemBlock.P mb).
simpl in wf_twin0. unfold Ir.TWINCNT in wf_twin0. congruence.
simpl. left. reflexivity.
Qed.
Lemma bytes_In_c:
forall mb ofs len byt b
(HBYTES:Ir.MemBlock.bytes mb ofs len = byt)
(HIN:List.In b byt),
List.In b (Ir.MemBlock.c mb).
Proof.
intros.