-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwinExecutionProof.v
1746 lines (1708 loc) · 61.5 KB
/
TwinExecutionProof.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 BinNat.
Require Import sflib.
Require Import Omega.
Require Import Permutation.
Require Import Common.
Require Import Lang.
Require Import Value.
Require Import Behaviors.
Require Import Memory.
Require Import State.
Require Import WellTyped.
Require Import LoadStore.
Require Import SmallStep.
Require Import SmallStepAux.
Require Import SmallStepWf.
Require Import Reordering.
Require Import TwinExecution.
Require Import TwinExecutionAux.
Import TwinExecution.
Import Ir.
Import TwinExecutionAux.
Import Ir.
Module Ir.
(*******************************************************
Main Theorems
*******************************************************)
(* If malloc succeeds, it always creates twin-state. *)
Theorem malloc_creates_twin_state:
forall (md:Ir.IRModule.t) (st st'1: Ir.Config.t) r retty opsz e'1
(HCUR: Some (Ir.Inst.imalloc r retty opsz) = Ir.Config.cur_inst md st)
(HNEXT: Ir.SmallStep.inst_step md st (Ir.SmallStep.sr_success e'1 st'1)),
(* malloc can return NULL, or *)
Some (Ir.ptr (Ir.NULL)) = Ir.Config.get_val st'1 (Ir.opreg r) \/
(* malloc reutrn (bid, 0), and malloc can nondeterministically make
twin state with respect to bid as well *)
exists st'2 bid,
Some (Ir.ptr (Ir.plog bid 0)) = Ir.Config.get_val st'1 (Ir.opreg r) /\
Ir.SmallStep.inst_step md st (Ir.SmallStep.sr_success e'1 st'2) /\
twin_state st'1 st'2 bid.
Proof.
intros.
assert (HSTACK:Ir.Config.s st <> []).
{ unfold Ir.Config.cur_inst in HCUR.
unfold Ir.Config.cur_fdef_pc in HCUR.
des_ifs. }
inv HNEXT.
{ unfold Ir.SmallStep.inst_det_step in HNEXT0.
des_ifs. }
{ left. rewrite <- HCUR in HCUR0. inv HCUR0. }
{ (* returned NULL*)
left.
rewrite <- HCUR in HCUR0. inv HCUR0.
rewrite Ir.SmallStep.get_val_update_reg_and_incrpc_samereg. reflexivity. assumption. }
{ (* malloc succeeded. *)
right.
rewrite <- HCUR in HCUR0.
inv HCUR0.
(* Let's make P' which is permutated P. *)
dup HMBWF.
assert (HMBWF' := HMBWF (Ir.Memory.mt (Ir.Config.m st))).
clear HMBWF.
inv HMBWF'.
rewrite Ir.SmallStep.get_val_update_reg_and_incrpc_samereg.
simpl in *.
destruct P as [ | p1 P]; try (inv wf_twin; fail).
destruct P as [ | p2 P]; try (inv wf_twin; fail).
exists (Ir.SmallStep.update_reg_and_incrpc md
(Ir.Config.update_m st
(fst (Ir.Memory.new (Ir.Config.m st) (Ir.heap) nsz
(Ir.SYSALIGN) (List.repeat Ir.Byte.poison nsz)
(p2::p1::P))))
r (Ir.ptr (Ir.plog l 0))).
exists l.
split.
{ reflexivity. }
split.
{ eapply Ir.SmallStep.s_malloc with (P := p2::p1::P).
eassumption. reflexivity. eassumption. eassumption. reflexivity.
{ clear wf_tcond.
clear wf_poslen wf_align wf_inmem wf_notnull wf_disj wf_twin.
intros.
assert (HMBWF0' := HMBWF0 begt).
inv HMBWF0'.
simpl in *.
split.
{ intros. simpl in *. congruence. }
{ simpl. assumption. }
{ simpl. assumption. }
{ simpl. intros.
apply wf_align. intuition. }
{ simpl. intros. apply wf_inmem. intuition. }
{ simpl. intros. apply wf_notnull. intuition. }
{ simpl. repeat (rewrite andb_true_iff).
repeat (rewrite andb_true_iff in wf_disj).
inv wf_disj. inv H. inv H0.
split.
split.
{ rewrite disjoint_range_sym. assumption. }
{ assumption. }
split.
{ assumption. }
{ assumption. }
}
{ simpl. assumption. }
}
{ unfold Ir.Memory.allocatable in *.
simpl in HDISJ.
simpl.
{ simpl. repeat (rewrite andb_true_iff).
repeat (rewrite andb_true_iff in HDISJ).
inv HDISJ. inv H. inv H0.
split.
split.
{ rewrite disjoint_range_sym. assumption. }
{ assumption. }
split.
{ assumption. }
{ assumption. }
}
}
{ unfold Ir.Memory.new.
simpl.
unfold Ir.Memory.new in HNEW.
inv HNEW.
reflexivity.
}
}
{ unfold Ir.Memory.new in HNEW.
inv HNEW.
simpl.
unfold twin_state.
split.
{
rewrite Ir.SmallStep.update_reg_and_incrpc_update_m.
rewrite Ir.SmallStep.update_reg_and_incrpc_update_m.
apply Ir.Config.eq_wom_update_m.
apply Ir.Config.eq_wom_refl.
}
{ repeat (rewrite Ir.SmallStep.update_reg_and_incrpc_update_m).
repeat (rewrite Ir.Config.m_update_m).
simpl.
split. reflexivity.
split. reflexivity.
split. reflexivity.
split.
{ intros H0.
subst bid'.
eexists. eexists.
split.
{ unfold Ir.Memory.get.
simpl. rewrite PeanoNat.Nat.eqb_refl. reflexivity. }
split.
{ unfold Ir.Memory.get. simpl.
rewrite PeanoNat.Nat.eqb_refl. reflexivity. }
simpl.
split. reflexivity.
split. reflexivity.
split. reflexivity.
split. reflexivity.
split. reflexivity.
split. apply perm_swap.
{ (* p1 <> p2 comes from wf_disj. *)
simpl in wf_disj.
rewrite andb_true_iff in wf_disj.
rewrite andb_true_iff in wf_disj.
rewrite andb_true_iff in wf_disj.
inv wf_disj. inv H.
unfold disjoint_range in H1.
rewrite orb_true_iff in H1.
rewrite Nat.leb_le in H1.
rewrite Nat.leb_le in H1.
destruct (nsz). omega.
rewrite Nat.add_succ_r in H1.
rewrite Nat.add_succ_r in H1.
rewrite Nat.le_succ_l in H1.
rewrite Nat.le_succ_l in H1.
omega.
}
}
{ intros.
unfold Ir.Memory.get.
simpl. apply not_eq_sym in HMATCH.
rewrite <- PeanoNat.Nat.eqb_neq in HMATCH.
rewrite HMATCH.
reflexivity.
}
}
}
unfold Ir.Config.update_m.
simpl. assumption.
}
{ rewrite <- HCUR in HCUR0. congruence. }
{ rewrite <- HCUR in HCUR0. congruence. }
Qed.
Ltac thats_it :=
try (apply twin_state_update_reg_and_incrpc;
assumption);
try (apply twin_state_incrpc; assumption);
fail.
Ltac coalesce_op Hop1 Hop2 st2 HTWIN :=
assert (Htmp := Hop1);
assert (Htmp2 := Hop2);
erewrite twin_state_get_val_eq with (st2 := st2) in Htmp;
try apply HTWIN;
rewrite Htmp in Htmp2; inv Htmp2; clear Htmp.
Ltac success_trivial :=
eapply ts_success; try reflexivity; thats_it.
Ltac goes_wrong :=
eapply ts_goes_wrong; reflexivity.
Ltac inst_det_step_op0 HINST :=
apply Ir.SmallStep.s_det; unfold Ir.SmallStep.inst_det_step;
rewrite <- HINST; reflexivity.
Ltac inst_det_step_op1 HINST HOP :=
apply Ir.SmallStep.s_det; unfold Ir.SmallStep.inst_det_step;
rewrite <- HINST; rewrite HOP; reflexivity.
Ltac inst_det_step_op2 HINST HOP HOP2 :=
apply Ir.SmallStep.s_det; unfold Ir.SmallStep.inst_det_step;
rewrite <- HINST; rewrite HOP; rewrite HOP2; reflexivity.
(* Lemma: If (1) input states are twin-state &
(2) current instruction don't do memory access from a guessed pointer &
(3) current instruction never observes the twin block id,
each step on the two input states make twin state as well.
Note that in this lemma instruction is limited to
non-phi, non-terminator instructions (by definition of inst_step).
For phi / terminator: see the next lemmas. *)
Lemma twin_execution_inst_unidir:
forall (md:Ir.IRModule.t) (blkid:Ir.blockid)
(st1 st2:Ir.Config.t) (sr1 sr2:Ir.SmallStep.step_res)
(HWF1:Ir.Config.wf md st1)
(HWF2:Ir.Config.wf md st2)
(* Input state st1 and st2 are twin-state. *)
(HTWIN:twin_state st1 st2 blkid)
(* Current instruction wouldn't do memory access
from a guessed pointer. *)
(HNOGUESSEDACCESS:~ memaccess_from_possibly_guessedptr md st1)
(* Current instruction never observes block id blkid. *)
(HNOTOBSERVE: ~observes_block md st1 blkid),
(* one way dir *)
forall sr1 (HSTEP1:Ir.SmallStep.inst_step md st1 sr1),
exists sr2, Ir.SmallStep.inst_step md st2 sr2 /\
twin_sresult sr1 sr2 blkid.
Proof.
intros.
inv HSTEP1.
{ unfold Ir.SmallStep.inst_det_step in HNEXT.
remember (Ir.Config.cur_inst md st1) as oi1.
destruct oi1 as [i1 | ].
{ erewrite twin_state_cur_inst_eq in Heqoi1; try (apply HTWIN; fail).
destruct i1; inv HNEXT.
{ (* binop *)
eexists. split.
{ apply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi1.
reflexivity. }
{ erewrite twin_state_get_val_eq with (st1 := st1);
try (apply HTWIN; fail).
erewrite twin_state_get_val_eq with (st1 := st1);
try (apply HTWIN; fail).
success_trivial.
}
}
{ (* freeze *)
des_ifs.
erewrite twin_state_get_val_eq with (st1 := st1) (st2 := st2) in Heq;
try (apply HTWIN; fail).
eexists. split.
inst_det_step_op1 Heqoi1 Heq.
success_trivial. }
{ (* select *)
eexists. split.
inst_det_step_op0 Heqoi1.
repeat (erewrite <- twin_state_get_val_eq with (st1 := st1) (st2 := st2);
try (apply HTWIN; fail)).
des_ifs; success_trivial.
}
{ (* psub *)
eexists. split.
{ apply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi1.
reflexivity. }
{ erewrite <- twin_state_get_val_eq with (st1 := st1) (st2 := st2);
try (apply HTWIN; fail).
erewrite <- twin_state_get_val_eq with (st1 := st1) (st2 := st2);
try (apply HTWIN; fail).
eapply ts_success.
{ reflexivity. }
{ reflexivity. }
{ reflexivity. }
{ destruct t; try thats_it.
destruct (Ir.Config.get_val st1 o) eqn:Hop1; try thats_it.
destruct v; try thats_it.
destruct (Ir.Config.get_val st1 o0) eqn:Hop2; try thats_it.
destruct v; try thats_it.
destruct p; destruct p0.
{ unfold Ir.SmallStep.psub. thats_it. }
{ destruct (b =? blkid) eqn:HBLKID.
{ (* observed. *)
rewrite PeanoNat.Nat.eqb_eq in HBLKID.
subst b.
assert (Heqoi2 := Heqoi1).
rewrite twin_state_cur_inst_eq with (st2 := st1) (blkid := blkid)
in Heqoi2; try (apply twin_state_sym in HTWIN; assumption).
assert (observes_block md st1 blkid).
{ eapply ob_by_psub_l.
{ rewrite Heqoi2. reflexivity. }
{ rewrite Hop1. reflexivity. }
{ rewrite Hop2. reflexivity. }
}
congruence.
}
{ unfold Ir.SmallStep.psub.
rewrite PeanoNat.Nat.eqb_neq in HBLKID.
rewrite twin_state_p2N_eq with (st2 := st2) (blkid := blkid);
try assumption.
thats_it.
}
}
{ destruct (b =? blkid) eqn:HBLKID.
{ (* observed. *)
rewrite PeanoNat.Nat.eqb_eq in HBLKID.
subst b.
assert (Heqoi2 := Heqoi1).
rewrite twin_state_cur_inst_eq with (st2 := st1) (blkid := blkid)
in Heqoi2; try (apply twin_state_sym in HTWIN; assumption).
assert (observes_block md st1 blkid).
{ eapply ob_by_psub_r.
{ rewrite Heqoi2. reflexivity. }
{ rewrite Hop2. reflexivity. }
{ rewrite Hop1. reflexivity. }
}
congruence.
}
{ unfold Ir.SmallStep.psub.
rewrite PeanoNat.Nat.eqb_neq in HBLKID.
rewrite twin_state_p2N_eq with (st2 := st2) (blkid := blkid);
try assumption.
thats_it.
}
}
{ unfold Ir.SmallStep.psub. thats_it. }
}
}
}
{ (* gep *)
eexists. split.
inst_det_step_op0 Heqoi1.
{ erewrite <- twin_state_get_val_eq with (st2 := st2);
try (apply HTWIN; fail).
erewrite <- twin_state_get_val_eq with (st2 := st2);
try (apply HTWIN; fail).
eapply ts_success.
{ reflexivity. }
{ reflexivity. }
{ reflexivity. }
{ destruct t; try thats_it.
destruct (Ir.Config.get_val st1 o) eqn:Hop1; try thats_it.
destruct v; try thats_it.
destruct (Ir.Config.get_val st1 o0) eqn:Hop2; try thats_it.
destruct v; try thats_it.
destruct p.
{ erewrite twin_state_gep_eq; try eassumption.
thats_it.
}
{ unfold Ir.SmallStep.gep. thats_it. }
}
}
}
{ (* load *)
destruct (Ir.Config.get_val st1 o) eqn:Hop11.
destruct v.
{ dup Hop11.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop0;
try apply HTWIN.
inv H0.
eexists. split;
[ inst_det_step_op1 Heqoi1 Hop0
| success_trivial ].
}
destruct p eqn:HP.
{ (* logical ptr: okay *)
destruct (Ir.deref (Ir.Config.m st1) (Ir.plog b n) (Ir.ty_bytesz t))
eqn:HDEREF.
{
dup HDEREF.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN.
rewrite twin_state_deref_eq with (st2 := st2) (blkid := blkid) in HDEREF0;
try assumption.
eexists. split. inv H0.
inst_det_step_op2 Heqoi1 Hop11 HDEREF0.
{ inv H0. eapply ts_success.
{reflexivity. } { reflexivity. } { reflexivity. }
{ erewrite twin_state_load_val_eq. thats_it.
eassumption. }
}
}
{ inv H0.
dup HDEREF.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN.
rewrite twin_state_deref_eq with (st2 := st2) (blkid := blkid) in HDEREF0;
try assumption.
eexists.
split.
inst_det_step_op2 Heqoi1 Hop11 HDEREF0.
goes_wrong.
}
}
{ (* physical ptr: no *)
assert (memaccess_from_possibly_guessedptr md st1).
{ eapply gp_load.
symmetry in Hop11. eapply Hop11.
econstructor. reflexivity.
rewrite <- twin_state_cur_inst_eq with (st1 := st1)
(blkid := blkid) in Heqoi1;
try assumption.
rewrite Heqoi1. reflexivity.
}
congruence.
}
{ (* deref. poison *)
inv H0.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN.
eexists . split;
[ inst_det_step_op1 Heqoi1 Hop11 | goes_wrong ].
}
{ (* ty is problematic *)
inv H0.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN.
eexists . split.
inst_det_step_op1 Heqoi1 Hop11.
success_trivial.
}
}
{ (* store *)
destruct (Ir.Config.get_val st1 o) eqn:Hop11;
destruct (Ir.Config.get_val st2 o) eqn:Hop21;
destruct (Ir.Config.get_val st1 o0) eqn:Hop12;
destruct (Ir.Config.get_val st2 o0) eqn:Hop22;
try(erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN; congruence);
try(erewrite twin_state_get_val_eq with (st2 := st2) in Hop12;
try apply HTWIN; congruence).
{ destruct v eqn:HV; inv H0;
try (erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN;
rewrite Hop21 in Hop11; inv Hop11;
eexists; split;
[ inst_det_step_op1 Heqoi1 Hop21 |
success_trivial ]).
destruct (Ir.deref (Ir.Config.m st1) p (Ir.ty_bytesz t))
eqn:HDEREF; inv H1; destruct p.
{
dup HDEREF.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN.
rewrite Hop21 in Hop11. inv Hop11.
rewrite twin_state_deref_eq with (st1 := st1) (st2 := st2)
(blkid := blkid) in HDEREF0;
try assumption.
eexists.
split.
{ apply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi1.
rewrite Hop21, Hop22.
rewrite HDEREF0. reflexivity. }
{ eapply ts_success.
{ reflexivity. }
{ reflexivity. }
{ reflexivity. }
{ eapply twin_state_incrpc.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop12;
try apply HTWIN.
rewrite Hop12 in Hop22. inv Hop22.
eapply twin_state_store_val; try eassumption.
{ apply Ir.ty_bytesz_pos. }
}
}
}
{ (* ptr is physical pointer. *)
assert (memaccess_from_possibly_guessedptr md st1).
{ econstructor. rewrite Hop11. reflexivity.
econstructor. reflexivity.
rewrite <- twin_state_cur_inst_eq with (st1 := st1) (blkid := blkid)
in Heqoi1; try assumption.
rewrite <- Heqoi1. reflexivity.
}
congruence.
}
{ (* UB *)
dup HDEREF.
rewrite twin_state_deref_eq with (st1 := st1) (st2 := st2)
(blkid := blkid) in HDEREF0;
try assumption.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN.
dup Hop21.
rewrite Hop11 in Hop21. inv Hop21.
eexists.
split.
{ apply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi1.
rewrite Hop0. rewrite Hop22.
rewrite HDEREF0. reflexivity. }
{ constructor; reflexivity. }
}
{ (* ptr is physical pointer. *)
assert (memaccess_from_possibly_guessedptr md st1).
{ econstructor. rewrite Hop11. reflexivity.
econstructor. reflexivity.
rewrite <- twin_state_cur_inst_eq with (st1 := st1) (blkid := blkid)
in Heqoi1; try assumption.
rewrite <- Heqoi1. reflexivity.
}
congruence.
}
{ (* ptr is poison. *)
erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN.
eexists.
split.
inst_det_step_op2 Heqoi1 Hop11 Hop22.
{ constructor; reflexivity. }
}
}
{ (* Hop22, Hop12 is none.*)
dup Hop11. dup Hop21.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop11;
try apply HTWIN.
rewrite Hop11 in Hop1. inv Hop1.
destruct v0; inv H0.
{ eexists. split.
inst_det_step_op1 Heqoi1 Hop21.
success_trivial.
}
{ eexists. split.
inst_det_step_op2 Heqoi1 Hop21 Hop22.
success_trivial.
}
{ eexists. split.
inst_det_step_op2 Heqoi1 Hop21 Hop22.
success_trivial.
}
}
{ (* Hop11, Hop21 is none.*)
dup Hop12. dup Hop22.
erewrite twin_state_get_val_eq with (st2 := st2) in Hop12;
try apply HTWIN.
rewrite Hop12 in Hop1. inv Hop1. inv H0.
eexists. split.
inst_det_step_op1 Heqoi1 Hop21.
success_trivial.
}
{ (* all ops are none.*)
inv H0.
eexists. split.
inst_det_step_op1 Heqoi1 Hop21.
success_trivial.
}
}
{ (* free *)
(* instruction Heqoi1 *)
assert (Heqoi2 := Heqoi1).
rewrite <- twin_state_cur_inst_eq with (st1 := st1)
(blkid := blkid) in Heqoi1;
try assumption.
destruct (Ir.Config.get_val st1 o) eqn:Hop1.
{ remember (Ir.Config.get_val st2 o) as op2 eqn:Hop2.
coalesce_op Hop1 Hop2 st2 HTWIN.
destruct v.
{ (* free (int) -_-; *)
inv H0.
eexists . split.
inst_det_step_op1 Heqoi2 H.
goes_wrong.
}
{ (* free (ptr) *)
destruct p.
{ (* free(log) *)
assert (HBIG:(exists m1 m2,
Some m1 = (Ir.SmallStep.free (Ir.plog b n) (Ir.Config.m st1)) /\
Some m2 = (Ir.SmallStep.free (Ir.plog b n) (Ir.Config.m st2)) /\
twin_state (Ir.Config.update_m st1 m1)
(Ir.Config.update_m st2 m2)
blkid) \/
(None = Ir.SmallStep.free (Ir.plog b n) (Ir.Config.m st1) /\
None = Ir.SmallStep.free (Ir.plog b n) (Ir.Config.m st2))).
{ eapply twin_state_free; eassumption. }
destruct HBIG.
{ destruct H1 as [m1 [m2 [H1 [H2 H3]]]].
rewrite <- H1 in H0. inv H0.
eexists.
split. symmetry in H2.
inst_det_step_op2 Heqoi2 H H2.
success_trivial.
}
{ destruct H1. rewrite <- H1 in H0.
inv H0.
eexists. split. symmetry in H2.
inst_det_step_op2 Heqoi2 H H2.
goes_wrong.
}
}
{ (* free(phy) *)
assert (memaccess_from_possibly_guessedptr md st1).
{ eapply gp_free. symmetry in Hop1. eassumption.
econstructor. reflexivity.
eassumption.
}
congruence.
}
}
{ (* free (int) -_-; *)
inv H0.
eexists . split.
inst_det_step_op1 Heqoi2 H. goes_wrong.
}
}
{ (* goes wrong *)
inv H0.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2.
remember (Ir.Config.get_val st2 o) as op2 eqn:Hop2.
coalesce_op Hop1 Hop2 st2 HTWIN.
reflexivity. }
goes_wrong.
}
}
{ (* bit cast *)
assert (Heqoi2 := Heqoi1).
rewrite <- twin_state_cur_inst_eq with (st1 := st1)
(blkid := blkid) in Heqoi1;
try assumption.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. reflexivity. }
{ assert (HOP:Ir.Config.get_val st1 o = Ir.Config.get_val st2 o).
{ eapply twin_state_get_val_eq. eassumption. }
rewrite HOP.
success_trivial.
}
}
{ (* ptrtoint *)
assert (Heqoi2 := Heqoi1).
rewrite <- twin_state_cur_inst_eq with (st1 := st1)
(blkid := blkid) in Heqoi1;
try assumption.
eexists. split.
inst_det_step_op0 Heqoi2.
{ assert (HOP:Ir.Config.get_val st1 o = Ir.Config.get_val st2 o).
{ eapply twin_state_get_val_eq. eassumption. }
rewrite HOP.
destruct t; try success_trivial.
destruct (Ir.Config.get_val st2 o) eqn:HOPVAL;
try success_trivial.
{ destruct v; try success_trivial.
destruct p.
{ (* p shouldn't be log (blkid, ..) *)
destruct (b =? blkid) eqn:HBLKID.
{ rewrite PeanoNat.Nat.eqb_eq in HBLKID.
assert (observes_block md st1 blkid).
{ eapply ob_by_ptrtoint.
eassumption. rewrite HOP. subst. reflexivity. }
congruence.
}
{ rewrite PeanoNat.Nat.eqb_neq in HBLKID.
dup HTWIN.
decompose_HTWIN HTWIN b.
destruct HTWIN5'. exploit H0. assumption.
intros HH. clear H H0.
unfold Ir.SmallStep.p2N.
unfold Ir.log_to_phy.
rewrite HH.
success_trivial.
}
}
{ (* p is phy is okay. *)
success_trivial.
}
}
}
}
{ (* inttoptr *)
assert (Heqoi2 := Heqoi1).
rewrite <- twin_state_cur_inst_eq with (st1 := st1)
(blkid := blkid) in Heqoi1;
try assumption.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. reflexivity. }
{ assert (HOP:Ir.Config.get_val st1 o = Ir.Config.get_val st2 o).
{ eapply twin_state_get_val_eq. eassumption. }
rewrite HOP.
success_trivial. }
}
{ (* ievent *)
assert (Heqoi2 := Heqoi1).
rewrite <- twin_state_cur_inst_eq with (st1 := st1)
(blkid := blkid) in Heqoi1;
try assumption.
assert (HOP:Ir.Config.get_val st1 o = Ir.Config.get_val st2 o).
{ eapply twin_state_get_val_eq. eassumption. }
destruct (Ir.Config.get_val st1 o) eqn:HOP1.
{ symmetry in HOP. destruct v; inv H0.
{ eexists. split.
inst_det_step_op1 Heqoi2 HOP. success_trivial.
}
{ eexists. split.
inst_det_step_op1 Heqoi2 HOP. goes_wrong.
}
{ eexists. split. inst_det_step_op1 Heqoi2 HOP. goes_wrong.
}
}
{ symmetry in HOP. inv H0.
eexists. split.
inst_det_step_op1 Heqoi2 HOP. goes_wrong.
}
}
{ (* icmp ule *)
assert (Heqoi2 := Heqoi1).
rewrite <- twin_state_cur_inst_eq with (st1 := st1)
(blkid := blkid) in Heqoi1;
try assumption.
assert (HOPEQ1:Ir.Config.get_val st1 o = Ir.Config.get_val st2 o).
{ eapply twin_state_get_val_eq. eassumption. }
assert (HOPEQ2:Ir.Config.get_val st1 o0 = Ir.Config.get_val st2 o0).
{ eapply twin_state_get_val_eq. eassumption. }
symmetry in HOPEQ1. symmetry in HOPEQ2.
destruct (Ir.Config.get_val st1 o) eqn:Hop11.
{ destruct v.
{
destruct (Ir.Config.get_val st1 o0) eqn:Hop12.
{ destruct v; inv H0;
(eexists; split;
[ inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2 | success_trivial ]).
}
{ inv H0.
eexists. split.
inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2.
success_trivial.
}
}
{ destruct (Ir.Config.get_val st1 o0) eqn:Hop12.
{ destruct v.
{ inv H0. eexists. split.
inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2.
success_trivial.
}
{ (* okay, the important case. *)
unfold Ir.SmallStep.icmp_eq_ptr in H0.
erewrite twin_state_icmp_eq_ptr_nondet_cond_eq in H0; try eassumption.
destruct p; destruct p0.
{ (* log, log *)
destruct (b =? b0) eqn:HBB0.
{ inv H0.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_eq_ptr. rewrite HBB0. reflexivity. }
success_trivial.
}
{ des_ifs.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_eq_ptr. rewrite HBB0.
rewrite Heq0. reflexivity. }
success_trivial.
}
}
{ (* log, phy *)
assert (b <> blkid).
{ intros HH. assert (observes_block md st1 blkid).
{ eapply ob_by_iicmpeq_l. rewrite Heqoi1. reflexivity.
rewrite Hop11. subst. reflexivity.
rewrite Hop12. reflexivity. }
congruence. }
erewrite twin_state_p2N_eq in H0; try eassumption.
inv H0.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_ule_ptr. reflexivity. }
success_trivial.
}
{ (* phy. log *)
assert (b <> blkid).
{ intros HH. assert (observes_block md st1 blkid).
{ eapply ob_by_iicmpeq_r. rewrite Heqoi1. reflexivity.
rewrite Hop12. subst. reflexivity.
rewrite Hop11. reflexivity. }
congruence. }
erewrite twin_state_p2N_eq in H0; try eassumption.
inv H0.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_ule_ptr. reflexivity. }
success_trivial.
}
{ (* phy phy *)
inversion H0.
inv H0.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_ule_ptr. reflexivity. }
success_trivial.
}
}
{ inv H0.
eexists. split.
inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2.
success_trivial.
}
}
{ inv H0.
eexists. split.
inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2.
success_trivial.
}
}
{ inv H0.
eexists. split.
inst_det_step_op1 Heqoi2 HOPEQ1.
success_trivial.
}
}
{ inv H0.
eexists. split.
inst_det_step_op1 Heqoi2 HOPEQ1.
success_trivial.
}
}
{ (* icmp ule *)
assert (Heqoi2 := Heqoi1).
rewrite <- twin_state_cur_inst_eq with (st1 := st1)
(blkid := blkid) in Heqoi1;
try assumption.
assert (HOPEQ1:Ir.Config.get_val st1 o = Ir.Config.get_val st2 o).
{ eapply twin_state_get_val_eq. eassumption. }
assert (HOPEQ2:Ir.Config.get_val st1 o0 = Ir.Config.get_val st2 o0).
{ eapply twin_state_get_val_eq. eassumption. }
symmetry in HOPEQ1. symmetry in HOPEQ2.
destruct (Ir.Config.get_val st1 o) eqn:Hop11.
{ destruct v.
{ destruct (Ir.Config.get_val st1 o0) eqn:Hop12.
{ destruct v; inv H0;
(eexists; split;
[ inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2 |
success_trivial ]).
}
{ inv H0.
eexists. split.
inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2.
success_trivial.
}
}
{ destruct (Ir.Config.get_val st1 o0) eqn:Hop12.
{ destruct v.
{ inv H0. eexists. split.
inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2.
success_trivial.
}
{ (* okay, the important case. *)
unfold Ir.SmallStep.icmp_ule_ptr in H0.
erewrite twin_state_icmp_ule_ptr_nondet_cond_eq in H0; try eassumption.
destruct p; destruct p0.
{ (* log, log *)
des_ifs.
{ eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_ule_ptr. rewrite Heq0. reflexivity. }
{ eapply ts_success; try reflexivity.
dup HTWIN.
decompose_HTWIN HTWIN b.
destruct (b =? blkid) eqn:HBLKID.
{ (* The twin block *)
rewrite PeanoNat.Nat.eqb_eq in HBLKID.
destruct HTWIN5'. clear H0. exploit H. congruence.
intros HH. decompose_mbs HH. subst b. rewrite <- HH1.
thats_it. }
{ (* not the twin block *)
rewrite PeanoNat.Nat.eqb_neq in HBLKID.
destruct HTWIN5'. clear H. exploit H0. congruence.
intros HH. rewrite <- HH. rewrite Heq.
thats_it. }
}
}
{
dup HTWIN.
decompose_HTWIN HTWIN b.
destruct (b =? blkid) eqn:HBLKID.
{ (* The twin block *)
(*Memory.get b cnanot be None. *)
rewrite PeanoNat.Nat.eqb_eq in HBLKID.
destruct HTWIN5'. clear H0. exploit H. congruence.
intros HH. decompose_mbs HH. subst b. congruence. }
{ (* not the twin block *)
rewrite PeanoNat.Nat.eqb_neq in HBLKID.
destruct HTWIN5'. clear H. exploit H0. congruence.
intros HH.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2.
rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_ule_ptr. rewrite Heq0. reflexivity. }
{ eapply ts_success; try reflexivity. rewrite <- HH.
rewrite Heq.
thats_it. }
}
}
}
{ (* log, phy *)
assert (b <> blkid).
{ intros HH. assert (observes_block md st1 blkid).
{ eapply ob_by_iicmpule_l. rewrite Heqoi1. reflexivity.
rewrite Hop11. subst. reflexivity.
rewrite Hop12. reflexivity. }
congruence. }
erewrite twin_state_p2N_eq in H0; try eassumption.
inv H0.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_ule_ptr. reflexivity. }
success_trivial.
}
{ (* phy. log *)
assert (b <> blkid).
{ intros HH. assert (observes_block md st1 blkid).
{ eapply ob_by_iicmpule_r. rewrite Heqoi1. reflexivity.
rewrite Hop12. subst. reflexivity.
rewrite Hop11. reflexivity. }
congruence. }
erewrite twin_state_p2N_eq in H0; try eassumption.
inv H0.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_ule_ptr. reflexivity. }
success_trivial.
}
{ (* phy phy *)
inv H0.
eexists. split.
{ eapply Ir.SmallStep.s_det. unfold Ir.SmallStep.inst_det_step.
rewrite <- Heqoi2. rewrite HOPEQ1, HOPEQ2.
unfold Ir.SmallStep.icmp_ule_ptr. reflexivity. }
success_trivial.
}
}
{ inv H0.
eexists. split.
inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2.
success_trivial.
}
}
{ inv H0.
eexists. split.
inst_det_step_op2 Heqoi2 HOPEQ1 HOPEQ2.
success_trivial.
}
}
{ inv H0.
eexists. split.
inst_det_step_op1 Heqoi2 HOPEQ1.
success_trivial.
}
}
{ inv H0.
eexists. split.
inst_det_step_op1 Heqoi2 HOPEQ1.
success_trivial.
}
}