-
Notifications
You must be signed in to change notification settings - Fork 0
/
Equiv.v
3033 lines (2844 loc) · 140 KB
/
Equiv.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 Coq.Relations.Relation_Operators.
Require Import Coq.Wellfounded.Transitive_Closure.
Require Import coqutil.sanity coqutil.Macros.subst coqutil.Macros.unique coqutil.Byte.
Require Import coqutil.Datatypes.PrimitivePair coqutil.Datatypes.HList.
Require Import coqutil.Decidable.
Require Import coqutil.Tactics.fwd.
Require Import coqutil.Map.Properties.
Require Import bedrock2.Syntax coqutil.Map.Interface coqutil.Map.OfListWord.
Require Import BinIntDef coqutil.Word.Interface coqutil.Word.Bitwidth.
Require Import bedrock2.MetricLogging.
Require Export bedrock2.Memory.
Require Import Coq.Logic.ClassicalFacts.
Require Import Coq.Classes.Morphisms.
Require Import Coq.Wellfounded.Union.
Require Import Relation_Operators.
Require Import Relation_Definitions.
Require Import Transitive_Closure.
Require Import Coq.Logic.ChoiceFacts.
(* not sure where to put these lemmas *)
Lemma align_trace_cons {T} x xs cont t (H : xs = List.app cont t) : @List.cons T x xs = List.app (cons x cont) t.
Proof. intros. cbn. congruence. Qed.
Lemma align_trace_app {T} x xs cont t (H : xs = List.app cont t) : @List.app T x xs = List.app (List.app x cont) t.
Proof. intros. cbn. subst. rewrite List.app_assoc; trivial. Qed.
Ltac trace_alignment :=
repeat match goal with
| t := cons _ _ |- _ => subst t
end;
repeat (eapply align_trace_app
|| eapply align_trace_cons
|| exact (eq_refl (List.app nil _))).
Lemma app_one_l {A} (a : A) ll : (a :: ll = (cons a nil) ++ ll)%list.
Proof. reflexivity. Qed.
Require Import Coq.Lists.List.
Module ShortTheorems.
Section ShortTheorems.
Context (L B : Type).
Context (B_inhabited : B).
Inductive event :=
| leak (val : L)
| branch (val : B).
Inductive qevent : Type :=
| qleak (val : L)
| qbranch
| qend.
Definition q (e : event) : qevent :=
match e with
| leak l => qleak l
| branch b => qbranch
end.
(*Defn 2.3 of paper*)
Definition predicts' (pred : list event -> qevent) (k : list event) :=
(forall k1 x k2, k = k1 ++ leak x :: k2 -> pred k1 = qleak x)/\
(forall k1 x k2, k = k1 ++ branch x :: k2 -> pred k1 = qbranch) /\
pred k = qend.
(*an equivalent inductive definition*)
Inductive predicts : (list event -> qevent) -> list event -> Prop :=
| predicts_nil f : f nil = qend -> predicts f nil
| predicts_cons f e k : f nil = q e -> predicts (fun k_ => f (e :: k_)) k -> predicts f (e :: k).
(*Defn 2.5 of paper*)
Definition compat' (oracle : list event -> B) (k : list event) :=
forall k1 x k2, k = k1 ++ branch x :: k2 -> oracle k1 = x.
(*an equivalent inductive definition*)
Inductive compat : (list event -> B) -> list event -> Prop :=
| compat_nil o : compat o nil
| compat_cons_branch o k b : o nil = b -> compat (fun k_ => o (branch b :: k_)) k -> compat o (branch b :: k)
| compat_cons_leak o k l : compat (fun k_ => o (leak l :: k_)) k -> compat o (leak l :: k).
Lemma predicts'_iff_predicts pred k : predicts' pred k <-> predicts pred k.
Proof.
split.
- revert pred.
induction k as [|e k']; [|destruct e as [l|b]]; intros pred H; unfold predicts' in H; fwd.
+ constructor. assumption.
+ constructor.
-- eapply Hp0. trace_alignment.
-- eapply IHk'. cbv [predicts']. split; [|split].
++ intros. subst. eapply Hp0. trace_alignment.
++ intros. subst. eapply Hp1. trace_alignment.
++ assumption.
+ constructor.
-- eapply Hp1. trace_alignment.
-- eapply IHk'. cbv [predicts']. split; [|split].
++ intros. subst. eapply Hp0. trace_alignment.
++ intros. subst. eapply Hp1. trace_alignment.
++ assumption.
- intros H. induction H.
+ split; [|split].
-- intros. destruct k1; simpl in H0; congruence.
-- intros. destruct k1; simpl in H0; congruence.
-- assumption.
+ destruct IHpredicts as [H1 [H2 H3]]. split; [|split].
-- intros. destruct k1; inversion H4; subst; simpl in *; try congruence.
eapply H1. trace_alignment.
-- intros. destruct k1; inversion H4; subst; simpl in *; try congruence.
eapply H2. trace_alignment.
-- assumption.
Qed.
Lemma compat'_iff_compat o k : compat' o k <-> compat o k.
Proof.
split.
- intros H. revert o H. induction k; intros o H.
+ constructor.
+ destruct a.
-- constructor. apply IHk. cbv [compat']. intros. subst. eapply H. trace_alignment.
-- constructor.
++ eapply H. trace_alignment.
++ apply IHk. cbv [compat']. intros. subst. eapply H. trace_alignment.
- intros H. cbv [compat']. induction H; intros.
+ destruct k1; simpl in H; congruence.
+ destruct k1; simpl in H1; try congruence. inversion H1. subst.
eapply IHcompat. trace_alignment.
+ destruct k1; simpl in H0; try congruence. inversion H0. subst.
eapply IHcompat. trace_alignment.
Qed.
Inductive trace_tree : Type :=
| tree_leaf
| tree_leak (l : L) (rest : trace_tree)
| tree_branch (rest : B -> trace_tree).
(*Defn 2.8 of paper*)
Inductive path : trace_tree -> list event -> Prop :=
| nil_path : path tree_leaf nil
| leak_path x k tree : path tree k -> path (tree_leak x tree) (leak x :: k)
| branch_path k f x : path (f x) k -> path (tree_branch f) (branch x :: k).
Fixpoint predictor_of_trace_tree (tree : trace_tree) : (list event -> qevent) :=
fun k =>
match tree, k with
| tree_leaf, nil => qend
| tree_leak l tree', nil => qleak l
| tree_branch tree', nil => qbranch
| tree_leak l1 tree', leak l2 :: k' => predictor_of_trace_tree tree' k'
| tree_branch tree', branch b :: k' => predictor_of_trace_tree (tree' b) k'
| _, _ => (*input is garbage, return whatever*) qend
end.
Theorem trace_trees_are_predictors :
forall tree, exists pred, forall k,
path tree k <-> predicts' pred k.
Proof.
intros. exists (predictor_of_trace_tree tree). intros. rewrite predicts'_iff_predicts.
split; intros H.
- induction H.
+ constructor. reflexivity.
+ constructor; [reflexivity|]. assumption.
+ constructor; [reflexivity|]. assumption.
- revert k H. induction tree; intros k H'.
+ simpl in H'. inversion H'; simpl in *; subst.
-- constructor.
-- destruct e; simpl in H; congruence.
+ destruct k as [|e k'].
{ simpl in H'. inversion H'; subst. congruence. }
destruct e.
-- inversion H'. subst. simpl in H2. inversion H2. subst. constructor.
apply IHtree. simpl in H3. apply H3.
-- inversion H'. subst. simpl in H2. inversion H2.
+ destruct k as [|e k'].
{ simpl in H'. inversion H'; subst. congruence. }
destruct e.
-- inversion H'. subst. simpl in H3. inversion H3.
-- inversion H'. subst. simpl in H3. inversion H3. subst. constructor.
apply H. simpl in H4. apply H4.
Qed.
Print option_map.
Fixpoint trace_of_predictor_and_oracle pred o fuel : option (list event) :=
match fuel with
| O => None
| S fuel' =>
match pred nil with
| qend => Some nil
| qleak l => option_map (cons (leak l)) (trace_of_predictor_and_oracle
(fun k_ => pred (leak l :: k_))
(fun k_ => o (leak l :: k_))
fuel')
| qbranch => option_map (cons (branch (o nil))) (trace_of_predictor_and_oracle
(fun k_ => pred (branch (o nil) :: k_))
(fun k_ => o (branch (o nil) :: k_))
fuel')
end
end.
Lemma predictor_plus_oracle_equals_trace :
excluded_middle ->
FunctionalChoice_on ((list event -> B) * (list event -> qevent)) (option (list event)) ->
exists trace,
forall o pred k,
compat o k ->
(predicts pred k <-> Some k = trace (o, pred)).
Proof.
intros em choice. cbv [FunctionalChoice_on] in choice.
specialize (choice (fun o_pred tr => let '(o, pred) := o_pred in forall k, compat o k -> predicts pred k <-> Some k = tr)).
destruct choice as [trace choice].
2: { exists trace. intros. specialize (choice (o, pred) k H). apply choice. }
intros [o pred]. destruct (em (exists fuel, trace_of_predictor_and_oracle pred o fuel <> None)) as [H | H].
- destruct H as [fuel H]. exists (match trace_of_predictor_and_oracle pred o fuel with
| Some k => Some k
| None => Some nil
end).
intros. destruct (trace_of_predictor_and_oracle pred o fuel) eqn:E; try congruence.
clear H. revert l k pred o H0 E. induction fuel.
+ intros. simpl in E. congruence.
+ intros. simpl in E. split.
-- intros H. destruct k as [|e k'].
++ inversion H. subst. rewrite H1 in E. inversion E. subst. reflexivity.
++ inversion H. subst. rewrite H4 in E. destruct e; simpl in E.
--- destruct (trace_of_predictor_and_oracle _ _ _) eqn:E'; simpl in E; try congruence.
inversion E. subst. f_equal. inversion H0. subst. f_equal.
enough (Some k' = Some l0) by congruence. eapply IHfuel; eassumption.
--- destruct (trace_of_predictor_and_oracle _ _ _) eqn:E'; simpl in E; try congruence.
inversion E. subst. inversion H0. subst. f_equal. f_equal.
enough (Some k' = Some l0) by congruence. eapply IHfuel; eassumption.
-- intros H. inversion H. subst. clear H. destruct l as [|e l].
++ constructor. destruct (pred nil).
--- destruct (trace_of_predictor_and_oracle _ _ _) eqn:E'; simpl in E; congruence.
--- destruct (trace_of_predictor_and_oracle _ _ _) eqn:E'; simpl in E; congruence.
--- reflexivity.
++ destruct (pred nil) eqn:E''.
--- destruct (trace_of_predictor_and_oracle _ _ _) eqn:E'; simpl in E; try congruence.
inversion E. subst. inversion H0. subst. constructor.
+++ assumption.
+++ eapply IHfuel; try eassumption. reflexivity.
--- destruct (trace_of_predictor_and_oracle _ _ _) eqn:E'; simpl in E; try congruence.
inversion E. subst. inversion H0. subst. constructor.
+++ assumption.
+++ eapply IHfuel; try eassumption. reflexivity.
--- inversion E.
- exists None. intros. split; intros H1; try congruence. exfalso. apply H. clear H.
revert o pred H0 H1. induction k as [|e k'].
+ intros. exists (S O). simpl. inversion H1. rewrite H. congruence.
+ intros. destruct e.
-- inversion H0. inversion H1. subst. specialize IHk' with (1 := H3) (2 := H9).
destruct IHk' as [fuel IHk']. exists (S fuel). simpl. rewrite H8. simpl.
destruct (trace_of_predictor_and_oracle _ _ _); try congruence. simpl.
congruence.
-- inversion H0. inversion H1. subst. specialize IHk' with (1 := H5) (2 := H10).
destruct IHk' as [fuel IHk']. exists (S fuel). simpl. rewrite H9. simpl.
destruct (trace_of_predictor_and_oracle _ _ _); try congruence. simpl. congruence.
Qed.
Fixpoint oracle_of_trace (k k_ : list event) : B :=
match k, k_ with
| branch b :: k', nil => b
| _ :: k', _ :: k_' => oracle_of_trace k' k_'
| _, _ => B_inhabited
end.
Lemma compat_exists :
forall k, exists o, compat o k.
Proof.
intros k. exists (oracle_of_trace k). induction k.
- constructor.
- destruct a; constructor; assumption || reflexivity.
Qed.
Theorem predictors_to_oracles {T T' : Type} :
excluded_middle ->
FunctionalChoice_on ((list event -> B) * (list event -> qevent)) (option (list event)) ->
forall pred (g : T -> T'), exists f, forall k t,
predicts (pred (g t)) k <-> (forall o, (compat o k -> Some k = f o (g t))).
Proof.
intros. specialize predictor_plus_oracle_equals_trace with (1 := H) (2 := H0).
clear H H0. intros [trace H]. exists (fun o gt => trace (o, pred gt)).
intros. split. 1: intros; apply H; assumption. intros.
specialize (compat_exists k). intros [o Ho]. specialize (H0 o Ho). rewrite H; eassumption.
Qed.
Fixpoint p' (p1 : list event -> qevent) (p2 : list event -> list event -> qevent) (k : list event) :=
match (p1 nil) with
| qend => p2 nil k
| _ => match k with
| nil => (p1 nil)
| x :: k' => p' (fun kk => p1 (x :: kk)) (fun kk => p2 (x :: kk)) k'
end
end.
Fixpoint p (p1 : list event -> qevent) (p2 : list event -> list event -> qevent) (k : list event) :=
match k with
| nil => match (p1 nil) with
| qend => p2 nil k
| _ => (p1 nil)
end
| x :: k' => match (p1 nil) with
| qend => p2 nil k
| _ => p (fun kk => p1 (x :: kk)) (fun kk => p2 (x :: kk)) k'
end
end.
Lemma append_predictors p1 p2 : exists p,
forall k1 k2, predicts p1 k1 -> predicts (p2 k1) k2 -> predicts p (k1 ++ k2).
Proof.
exists (p p1 p2). intros k1. revert p1 p2. induction k1; intros.
- simpl. inversion H. subst. destruct k2; simpl.
+ inversion H0. subst. constructor. simpl. rewrite H1. assumption.
+ inversion H0. subst. constructor.
-- simpl. rewrite H1. assumption.
-- simpl. rewrite H1. assumption.
- simpl. inversion H. subst. clear H.
constructor.
-- simpl. rewrite H4. destruct a; reflexivity.
-- simpl. rewrite H4. destruct a.
++ simpl. apply IHk1. 1: assumption. assumption.
++ simpl. apply IHk1. 1: assumption. assumption.
Qed.
End ShortTheorems.
End ShortTheorems.
(* BW is not needed on the rhs, but helps infer width *)
Definition io_event {width: Z}{BW: Bitwidth width}{word: word.word width}{mem: map.map word byte} : Type :=
(mem * String.string * list word) * (mem * list word).
(*could reduce this to many fewer cases, at the cost of being a bit more confusing.*)
(*actually no, it wouldn't even be that confusing. It's very tempting to just let
event := bool | word | unit. *)
(*should I name this leakage_event, now that it doesn't contain the IO events?*)
Inductive event {width: Z}{BW: Bitwidth width}{word: word.word width} : Type :=
| leak_unit : event
| leak_bool : bool -> event
| leak_word : word -> event
| leak_list : list word -> event
(* ^we need this, because sometimes it's convenient that one io call leaks only one event
See Interact case of spilling transform_trace function for an example. *)
| consume_word : word -> event.
(*This looks pretty, but it seems hard to work with. Can't even use the inversion tactic?
Inductive event : Type :=
| leak : forall {A : Type}, A -> event
| consume : forall {A : Type}, A -> event.*)
Section WithIOEvent.
Context {width: Z}{BW: Bitwidth width}{word: word.word width}{mem: map.map word byte}.
(*should I call this leakage_trace, now that it doesn't contain io events?
shame to lengthen the name. No, I shouldn't call it a leakage trace, since
it contains the sources of nondeterminism as well as leakage events.*)
Definition trace : Type := list event.
Definition io_trace : Type := list io_event.
Definition need_to_predict e :=
match e with
| consume_word _ => True
| _ => False
end.
Inductive compat : (trace -> event) -> trace -> Prop :=
| compat_cons :
forall f e k,
(need_to_predict e -> f nil = e) ->
compat (fun k' => f (e :: k')) k ->
compat f (e :: k)
| compat_nil :
forall f,
compat f nil.
Lemma compat_ext f k g :
(forall k', f k' = g k') ->
compat f k ->
compat g k.
Proof.
intros H1 H2. revert H1. revert g. induction H2.
- intros g0 Hfg0. econstructor.
+ rewrite <- Hfg0. apply H.
+ apply IHcompat. intros. apply Hfg0.
- intros. constructor.
Qed.
Lemma predict_cons f k1 k2 e :
compat f (k1 ++ e :: k2) ->
need_to_predict e ->
f k1 = e.
Proof.
revert k2. revert e. revert f. induction k1.
- intros. inversion H. subst. auto.
- intros. inversion H. subst. apply IHk1 with (1 := H5) (2 := H0).
Qed.
End WithIOEvent. (*maybe extend this to the end?*)
Definition ExtSpec{width: Z}{BW: Bitwidth width}{word: word.word width}{mem: map.map word byte} :=
(* Given a trace of what happened so far,
the given-away memory, an action label and a list of function call arguments, *)
io_trace -> mem -> String.string -> list word ->
(* and a postcondition on the received memory, function call results, and leakage trace, *)
(mem -> list word -> list word -> Prop) ->
(* tells if this postcondition will hold *)
Prop.
Existing Class ExtSpec.
Definition PickSp {width: Z}{BW: Bitwidth width}{word: word.word width}{mem: map.map word byte} : Type :=
trace -> word.
Existing Class PickSp.
Module ext_spec.
Class ok{width: Z}{BW: Bitwidth width}{word: word.word width}{mem: map.map word byte}
{ext_spec: ExtSpec}: Prop :=
{
(* The action name and arguments uniquely determine the footprint of the given-away memory. *)
unique_mGive_footprint: forall t1 t2 mGive1 mGive2 a args
(post1 post2: mem -> list word -> list word -> Prop),
ext_spec t1 mGive1 a args post1 ->
ext_spec t2 mGive2 a args post2 ->
map.same_domain mGive1 mGive2;
weaken :: forall t mGive act args,
Morphisms.Proper
(Morphisms.respectful
(Morphisms.pointwise_relation Interface.map.rep
(Morphisms.pointwise_relation (list word)
(Morphisms.pointwise_relation (list word) Basics.impl))) Basics.impl)
(ext_spec t mGive act args);
intersect: forall t mGive a args,
ext_spec t mGive a args (fun mReceive resvals klist =>
forall mid, ext_spec t mGive a args mid ->
mid mReceive resvals klist);
}.
End ext_spec.
Arguments ext_spec.ok {_ _ _ _} _.
Section binops.
Context {width : Z} {BW: Bitwidth width} {word : Word.Interface.word width}.
Definition interp_binop (bop : bopname) : word -> word -> word :=
match bop with
| bopname.add => word.add
| bopname.sub => word.sub
| bopname.mul => word.mul
| bopname.mulhuu => word.mulhuu
| bopname.divu => word.divu
| bopname.remu => word.modu
| bopname.and => word.and
| bopname.or => word.or
| bopname.xor => word.xor
| bopname.sru => word.sru
| bopname.slu => word.slu
| bopname.srs => word.srs
| bopname.lts => fun a b =>
if word.lts a b then word.of_Z 1 else word.of_Z 0
| bopname.ltu => fun a b =>
if word.ltu a b then word.of_Z 1 else word.of_Z 0
| bopname.eq => fun a b =>
if word.eqb a b then word.of_Z 1 else word.of_Z 0
end.
Definition leak_binop (bop : bopname) (x1 : word) (x2 : word) : trace :=
match bop with
| bopname.divu | bopname.remu => cons (leak_word x2) (cons (leak_word x1) nil)
| bopname.sru | bopname.slu | bopname.srs => cons (leak_word x2) nil
| _ => nil
end.
End binops.
Section semantics.
Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word byte}.
Context {locals: map.map String.string word}.
Context {env: map.map String.string (list String.string * list String.string * cmd)}.
Local Notation metrics := MetricLog.
(* this is the expr evaluator that is used to verify execution time, the just-correctness-oriented version is below *)
Section WithMemAndLocals.
Context (m : mem) (l : locals).
Local Notation "' x <- a | y ; f" := (match a with x => f | _ => y end)
(right associativity, at level 70, x pattern).
Fixpoint eval_expr (e : expr) (mc : metrics) (tr : trace) : option (word * metrics * trace) :=
match e with
| expr.literal v => Some (
word.of_Z v,
addMetricInstructions 8 (addMetricLoads 8 mc),
tr)
| expr.var x => match map.get l x with
| Some v => Some (
v,
addMetricInstructions 1 (addMetricLoads 2 mc),
tr)
| None => None
end
| expr.inlinetable aSize t index =>
'Some (index', mc', tr') <- eval_expr index mc tr | None;
'Some v <- load aSize (map.of_list_word t) index' | None;
Some (
v,
(addMetricInstructions 3 (addMetricLoads 4 (addMetricJumps 1 mc'))),
leak_word index' :: tr')
| expr.load aSize a =>
'Some (a', mc', tr') <- eval_expr a mc tr | None;
'Some v <- load aSize m a' | None;
Some (
v,
addMetricInstructions 1 (addMetricLoads 2 mc'),
leak_word a' :: tr')
| expr.op op e1 e2 =>
'Some (v1, mc', tr') <- eval_expr e1 mc tr | None;
'Some (v2, mc'', tr'') <- eval_expr e2 mc' tr' | None;
Some (
interp_binop op v1 v2,
addMetricInstructions 2 (addMetricLoads 2 mc''),
leak_binop op v1 v2 ++ tr'')
| expr.ite c e1 e2 =>
'Some (vc, mc', tr') <- eval_expr c mc tr | None;
eval_expr
(if word.eqb vc (word.of_Z 0) then e2 else e1)
(addMetricInstructions 2 (addMetricLoads 2 (addMetricJumps 1 mc')))
((if word.eqb vc (word.of_Z 0) then leak_bool false else leak_bool true) :: tr')
end.
Fixpoint eval_expr_old (e : expr) : option word :=
match e with
| expr.literal v => Some (word.of_Z v)
| expr.var x => map.get l x
| expr.inlinetable aSize t index =>
'Some index' <- eval_expr_old index | None;
load aSize (map.of_list_word t) index'
| expr.load aSize a =>
'Some a' <- eval_expr_old a | None;
load aSize m a'
| expr.op op e1 e2 =>
'Some v1 <- eval_expr_old e1 | None;
'Some v2 <- eval_expr_old e2 | None;
Some (interp_binop op v1 v2)
| expr.ite c e1 e2 =>
'Some vc <- eval_expr_old c | None;
eval_expr_old (if word.eqb vc (word.of_Z 0) then e2 else e1)
end.
Fixpoint evaluate_call_args_log (arges : list expr) (mc : metrics) (tr : trace) :=
match arges with
| e :: tl =>
'Some (v, mc', tr') <- eval_expr e mc tr | None;
'Some (args, mc'', tr'') <- evaluate_call_args_log tl mc' tr' | None;
Some (v :: args, mc'', tr'')
| _ => Some (nil, mc, tr)
end.
Lemma eval_expr_extends_trace :
forall e0 mc k v mc' k',
eval_expr e0 mc k = Some (v, mc', k') ->
exists k'', k' = k'' ++ k /\ forall x, ~In (consume_word x) k''.
Proof.
intros e0. induction e0; intros; simpl in *;
repeat match goal with
| H: (let (_, _) := ?x in _) = _ |- _ =>
destruct x
| H: match ?x with
| Some _ => _
| None => _
end = Some (_, _, _) |- _ =>
destruct x eqn:?; try congruence
| H: Some (?v1, ?mc1, ?t1) = Some (?v2, ?mc2, ?t2) |- _ =>
injection H; intros; subst
end.
- eexists. split; [trace_alignment|]. auto.
- eexists. split; [trace_alignment|]. auto.
- specialize IHe0 with (1 := Heqo). fwd. eexists. split; [trace_alignment|].
simpl. intros x H. destruct H; [congruence|]. rewrite app_nil_r in H.
eapply IHe0p1. eassumption.
- specialize IHe0 with (1 := Heqo). fwd. eexists. split; [trace_alignment|].
simpl. intros x H. destruct H; [congruence|]. rewrite app_nil_r in H.
(*why does eauto not work here:( *) eapply IHe0p1. eassumption.
- specialize IHe0_1 with (1 := Heqo). specialize IHe0_2 with (1 := Heqo0). fwd.
eexists. split; [trace_alignment|]. intros x H. rewrite app_nil_r in H.
assert (In (consume_word x) (k'' ++ k''0)).
+ destruct op; simpl in H; try assumption.
all: destruct H; [congruence|]; try assumption.
all: destruct H; [congruence|]; assumption.
+ apply in_app_or in H0. destruct H0.
-- eapply IHe0_2p1. eassumption.
-- eapply IHe0_1p1. eassumption.
- specialize IHe0_1 with (1 := Heqo). destruct (word.eqb _ _).
+ specialize IHe0_3 with (1 := H). fwd. eexists. split; [trace_alignment|].
intros x H'. rewrite app_nil_r in H'. apply in_app_or in H'. destruct H'.
-- eapply IHe0_3p1. eassumption.
-- destruct H0; [congruence|]. eapply IHe0_1p1. eassumption.
+ specialize IHe0_2 with (1 := H). fwd. eexists. split; [trace_alignment|].
intros x H'. rewrite app_nil_r in H'. apply in_app_or in H'. destruct H'.
-- eapply IHe0_2p1. eassumption.
-- destruct H0; [congruence|]. eapply IHe0_1p1. eassumption.
Qed.
Lemma evaluate_call_args_log_extends_trace :
forall arges mc k args mc' k',
evaluate_call_args_log arges mc k = Some (args, mc', k') ->
exists k'', k' = k'' ++ k /\ forall x, ~In (consume_word x) k''.
Proof.
intros arges. induction arges.
- simpl. intros. injection H. intros. subst. eexists. split; [trace_alignment|]. auto.
- simpl. intros. destruct (eval_expr _ _ _) eqn:E1; try congruence.
destruct p. destruct p. destruct (evaluate_call_args_log _ _ _) eqn:E2; try congruence.
destruct p. destruct p. injection H. intros. subst.
apply eval_expr_extends_trace in E1. specialize IHarges with (1 := E2).
fwd. eexists. split; [trace_alignment|]. intros x H. rewrite app_nil_r in H.
apply in_app_or in H. destruct H.
+ eapply IHargesp1. eassumption.
+ eapply E1p1. eassumption.
Qed.
Lemma expr_to_other_trace e mc mc' k1 k1' v :
eval_expr e mc k1 = Some (v, mc', k1') ->
exists k'',
k1' = k'' ++ k1 /\
forall k2,
eval_expr e mc k2 = Some (v, mc', k'' ++ k2).
Proof.
revert v. revert mc. revert k1. revert k1'. revert mc'. clear.
induction e; intros ? ? ? ? ? H; simpl in H; try (inversion H; subst; clear H).
- exists nil. auto.
- destruct (map.get l x) as [v0|] eqn:E; [|congruence]. inversion H1; subst; clear H1.
exists nil. simpl. rewrite E. auto.
- destruct (eval_expr _ _ _) as [v0|] eqn:E1; [|congruence].
destruct v0. destruct p. destruct (load _ _ _) as [v0|] eqn:E2; [|congruence].
inversion H1; subst; clear H1. eapply IHe in E1. destruct E1 as [k'' [E1 E3] ]. subst.
eexists (_ :: _). intuition. simpl. rewrite E3. rewrite E2. reflexivity.
- destruct (eval_expr _ _ _) as [v0|] eqn:E1; [|congruence].
destruct v0. destruct p. destruct (load _ _ _) as [v0|] eqn:E2; [|congruence].
inversion H1; subst; clear H1. eapply IHe in E1. destruct E1 as [k'' [E1 E3] ]. subst.
eexists (_ :: _). intuition. simpl. rewrite E3. rewrite E2. reflexivity.
- destruct (eval_expr e1 _ _) as [ [ [v0 mc0] p0]|] eqn:E1; [|congruence].
destruct (eval_expr e2 _ _) as [ [ [v1 mc1] p1]|] eqn:E2; [|congruence].
inversion H1; subst; clear H1.
eapply IHe1 in E1. destruct E1 as [k''1 [H1 H2] ]. eapply IHe2 in E2.
destruct E2 as [k''2 [H3 H4] ]. subst.
eexists (_ ++ _ ++ _). repeat rewrite <- (app_assoc _ _ k1). intuition.
simpl. rewrite H2. rewrite H4. f_equal. f_equal. repeat rewrite <- (app_assoc _ _ k2).
reflexivity.
- destruct (eval_expr e1 _ _) as [ [ [v0 mc0] p0]|] eqn:E1; [|congruence].
eapply IHe1 in E1. destruct E1 as [k''1 [H2 H3] ]. subst. simpl.
destruct (word.eqb _ _) eqn:E.
+ eapply IHe3 in H1. destruct H1 as [k''3 [H1 H2] ]. subst.
eexists (_ ++ _ :: _). repeat rewrite <- (app_assoc _ _ k1).
intuition. rewrite H3. rewrite E. rewrite H2.
repeat rewrite <- (app_assoc _ _ k2). reflexivity.
+ eapply IHe2 in H1. destruct H1 as [k''2 [H1 H2] ]. subst.
eexists (_ ++ _ :: _). repeat rewrite <- (app_assoc _ _ k1).
intuition. rewrite H3. rewrite E. rewrite H2.
repeat rewrite <- (app_assoc _ _ k2). reflexivity.
Qed.
Lemma call_args_to_other_trace arges mc k1 vs mc' k1' :
evaluate_call_args_log arges mc k1 = Some (vs, mc', k1') ->
exists k'',
k1' = k'' ++ k1 /\
forall k2,
evaluate_call_args_log arges mc k2 = Some (vs, mc', k'' ++ k2).
Proof.
revert mc. revert k1. revert vs. revert mc'. revert k1'. induction arges; intros.
- cbn [evaluate_call_args_log] in H. inversion H. subst.
exists nil. intuition.
- cbn [evaluate_call_args_log] in *.
destruct (eval_expr _ _ _) as [ [ [v0 mc0] p0]|] eqn:E1; [|congruence].
destruct (evaluate_call_args_log _ _ _) as [ [ [v1 mc1] p1]|] eqn:E2; [|congruence].
apply expr_to_other_trace in E1. apply IHarges in E2. fwd.
eexists (_ ++ _).
repeat rewrite <- (app_assoc _ _ k1). intuition. repeat rewrite <- (app_assoc _ _ k2).
rewrite E1p1. rewrite E2p1. reflexivity.
Qed.
End WithMemAndLocals.
End semantics.
Ltac subst_exprs :=
repeat match goal with
| H : eval_expr _ _ _ _ _ = Some _ |- _ =>
apply eval_expr_extends_trace in H; destruct H as [? [? ?] ]; subst
| H : evaluate_call_args_log _ _ _ _ _ = Some _ |- _ =>
apply evaluate_call_args_log_extends_trace in H; destruct H as [? [? ?] ]; subst
end.
Ltac subst_expr H :=
(apply eval_expr_extends_trace in H; destruct H as [? [? ?] ]; subst) ||
( apply evaluate_call_args_log_extends_trace in H; destruct H as [? [? ?] ]; subst).
Require Import Lia.
Module exec. Section WithEnv.
Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word byte}.
Context {locals: map.map String.string word}.
Context {env: map.map String.string (list String.string * list String.string * cmd)}.
Context {ext_spec: ExtSpec}.
Context (e: env).
Local Notation metrics := MetricLog.
Implicit Types post : trace -> io_trace -> mem -> locals -> metrics -> Prop. (* COQBUG(unification finds Type instead of Prop and fails to downgrade *)
Implicit Types fpost : PickSp -> trace -> io_trace -> mem -> locals -> metrics -> Prop.
Section WithDet.
Context (salloc_det : bool).
Context {pick_sp : PickSp}.
(*I really want to do the semantics like this:
cmd -> io_trace -> mem -> locals -> metrics ->
(trace -> io_trace -> mem -> locals -> metrics -> Prop) -> Prop.
But it would be ugly. Using app, screwing up simple postconditions
(e.g., in seq case) in semantics.
So I suppose I'll do
cmd -> trace -> io_trace -> mem -> locals -> metrics ->
(trace -> io_trace -> mem -> locals -> metrics -> Prop) -> Prop.
Then we can state a lemma, saying that exec c nil t m l mc post -> exec c k t m l mc (fun k' => post (k' ++ k)).
Then use that wherever we want, and it's *almost* like leakage trace isn't passed as a parameter to exec.
Still ugly though. But better? No, not really. Would be horribly obnoxious to apply that lemma. Hm.
I suppose I had better keep the append-style for leakage traces? :(
Is it still worthwhile to split up the io trace and leakage trace then?
I think so.
It still should be less of a pain to deal with them if they're separated.
*)
Inductive exec :
cmd -> trace -> io_trace -> mem -> locals -> metrics ->
(trace -> io_trace -> mem -> locals -> metrics -> Prop) -> Prop :=
| skip
k t m l mc post
(_ : post k t m l mc)
: exec cmd.skip k t m l mc post
| set x e
m l mc post
k t v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
(_ : post k' t m (map.put l x v) (addMetricInstructions 1
(addMetricLoads 1 mc')))
: exec (cmd.set x e) k t m l mc post
| unset x
k t m l mc post
(_ : post k t m (map.remove l x) mc)
: exec (cmd.unset x) k t m l mc post
| store sz ea ev
k t m l mc post
a mc' k' (_ : eval_expr m l ea mc k = Some (a, mc', k'))
v mc'' k'' (_ : eval_expr m l ev mc' k' = Some (v, mc'', k''))
m' (_ : store sz m a v = Some m')
(_ : post (leak_word a :: k'') t m' l (addMetricInstructions 1
(addMetricLoads 1
(addMetricStores 1 mc''))))
: exec (cmd.store sz ea ev) k t m l mc post
| stackalloc x n body
k t mSmall l mc post
(_ : Z.modulo n (bytes_per_word width) = 0)
(_ : forall a mStack mCombined,
(salloc_det = true -> a = pick_sp k) ->
anybytes a n mStack ->
map.split mCombined mSmall mStack ->
exec body (consume_word a :: k) t mCombined (map.put l x a) (addMetricInstructions 1 (addMetricLoads 1 mc))
(fun k' t' mCombined' l' mc' =>
exists mSmall' mStack',
anybytes a n mStack' /\
map.split mCombined' mSmall' mStack' /\
post k' t' mSmall' l' mc'))
: exec (cmd.stackalloc x n body) k t mSmall l mc post
| if_true k t m l mc e c1 c2 post
v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
(_ : word.unsigned v <> 0)
(_ : exec c1 (leak_bool true :: k') t m l (addMetricInstructions 2 (addMetricLoads 2 (addMetricJumps 1 mc'))) post)
: exec (cmd.cond e c1 c2) k t m l mc post
| if_false e c1 c2
k t m l mc post
v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
(_ : word.unsigned v = 0)
(_ : exec c2 (leak_bool false :: k') t m l (addMetricInstructions 2 (addMetricLoads 2 (addMetricJumps 1 mc'))) post)
: exec (cmd.cond e c1 c2) k t m l mc post
| seq c1 c2
k t m l mc post
mid (_ : exec c1 k t m l mc mid)
(_ : forall k' t' m' l' mc', mid k' t' m' l' mc' -> exec c2 k' t' m' l' mc' post)
: exec (cmd.seq c1 c2) k t m l mc post
| while_false e c
k t m l mc post
v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
(_ : word.unsigned v = 0)
(_ : post (leak_bool false :: k') t m l (addMetricInstructions 1
(addMetricLoads 1
(addMetricJumps 1 mc'))))
: exec (cmd.while e c) k t m l mc post
| while_true e c
k t m l mc post
v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
(_ : word.unsigned v <> 0)
mid (_ : exec c (leak_bool true :: k') t m l mc' mid)
(_ : forall k'' t' m' l' mc'', mid k'' t' m' l' mc'' ->
exec (cmd.while e c) k'' t' m' l' (addMetricInstructions 2
(addMetricLoads 2
(addMetricJumps 1 mc''))) post)
: exec (cmd.while e c) k t m l mc post
| call binds fname arges
k t m l mc post
params rets fbody (_ : map.get e fname = Some (params, rets, fbody))
args mc' k' (_ : evaluate_call_args_log m l arges mc k = Some (args, mc', k'))
lf (_ : map.of_list_zip params args = Some lf)
mid (_ : exec fbody (leak_unit :: k') t m lf (addMetricInstructions 100 (addMetricJumps 100 (addMetricLoads 100 (addMetricStores 100 mc')))) mid)
(_ : forall k'' t' m' st1 mc'', mid k'' t' m' st1 mc'' ->
exists retvs, map.getmany_of_list st1 rets = Some retvs /\
exists l', map.putmany_of_list_zip binds retvs l = Some l' /\
post k'' t' m' l' (addMetricInstructions 100 (addMetricJumps 100 (addMetricLoads 100 (addMetricStores 100 mc'')))))
: exec (cmd.call binds fname arges) k t m l mc post
| interact binds action arges
k t m l mc post
mKeep mGive (_: map.split m mKeep mGive)
args mc' k' (_ : evaluate_call_args_log m l arges mc k = Some (args, mc', k'))
mid (_ : ext_spec t mGive action args mid)
(_ : forall mReceive resvals klist, mid mReceive resvals klist ->
(*it is a mystery to me why we treat l' and m' differently here.
why not both having the exists quantifier? or both having the forall?*)
exists l', map.putmany_of_list_zip binds resvals l = Some l' /\
forall m', map.split m' mKeep mReceive ->
post (leak_list klist :: k')%list (((mGive, action, args), (mReceive, resvals)) :: t) m' l'
(addMetricInstructions 1
(addMetricStores 1
(addMetricLoads 2 mc'))))
: exec (cmd.interact binds action arges) k t m l mc post
.
Definition state : Type := trace * io_trace * mem * locals * metrics.
Notation ami := addMetricInstructions.
Notation aml := addMetricLoads.
Notation ams := addMetricStores. Check locals.
Notation amj := addMetricJumps.
Inductive scmd :=
| sskip
| sset (lhs : String.string) (rhs : expr)
| sunset (lhs : String.string)
| sstore (_ : access_size) (address : expr) (value : expr)
| sstackalloc (lhs : String.string) (nbytes : Z) (body : scmd)
| end_stackalloc (nbytes : Z) (a : word)
(* { lhs = alloca(nbytes); body; /*allocated memory freed right here*/ } *)
| scond (condition : expr) (nonzero_branch zero_branch : scmd)
| sseq (s1 s2: scmd)
| swhile (test : expr) (body : scmd)
| jump_back
| scall (binds : list String.string) (function : String.string) (args: list expr)
| start_call (binds : list String.string) (params : list String.string) (rets: list String.string) (fbody: scmd) (args: list expr)
| end_call (binds : list String.string) (rets: list String.string) (l : locals)
| sinteract (binds : list String.string) (action : String.string) (args: list expr)
| end_interact (binds : list String.string) (action : String.string) (args : list word) (mKeep mGive mReceive : mem) (resvals klist : list word).
Fixpoint inclusion (s : cmd) :=
match s with
| cmd.skip => sskip
| cmd.set x1 x2 => sset x1 x2
| cmd.unset x1 => sunset x1
| cmd.store x1 x2 x3 => sstore x1 x2 x3
| cmd.stackalloc x1 x2 x3 => sstackalloc x1 x2 (inclusion x3)
| cmd.cond x1 x2 x3 => scond x1 (inclusion x2) (inclusion x3)
| cmd.seq x1 x2 => sseq (inclusion x1) (inclusion x2)
| cmd.while x1 x2 => swhile x1 (inclusion x2)
| cmd.call x1 x2 x3 => scall x1 x2 x3
| cmd.interact x1 x2 x3 => sinteract x1 x2 x3
end.
Inductive step :
scmd -> trace -> io_trace -> mem -> locals -> metrics ->
scmd -> trace -> io_trace -> mem -> locals -> metrics -> Prop :=
| set_step x e
m l mc
k t v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
: step (sset x e) k t m l mc
sskip k' t m (map.put l x v) (ami 1 (aml 1 mc'))
| unset_step x
k t m l mc
: step (sunset x) k t m l mc
sskip k t m (map.remove l x) mc
| store_step sz ea ev
k t m l mc
a mc' k' (_ : eval_expr m l ea mc k = Some (a, mc', k'))
v mc'' k'' (_ : eval_expr m l ev mc' k' = Some (v, mc'', k''))
m' (_ : Memory.store sz m a v = Some m')
: step (sstore sz ea ev) k t m l mc
sskip (leak_word a :: k'') t m' l (ami 1 (aml 1 (ams 1 mc'')))
| stackalloc_step x n body a
k t mSmall l mc
mStack mCombined
(_ : Z.modulo n (bytes_per_word width) = 0)
(_ : salloc_det = true -> a = pick_sp k)
(_ : anybytes a n mStack)
(_ : map.split mCombined mSmall mStack)
: step (sstackalloc x n body) k t mSmall l mc
(sseq body (end_stackalloc n a)) (consume_word a :: k) t mCombined (map.put l x a) (ami 1 (aml 1 mc))
| stackalloc_end_step n a
k t mCombined l mc
mSmall mStack
(_ : anybytes a n mStack)
(_ : map.split mCombined mSmall mStack)
: step (end_stackalloc n a) k t mCombined l mc
sskip k t mSmall l mc
| if_true_step k t m l mc e c1 c2
v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
(_ : word.unsigned v <> 0)
: step (scond e c1 c2) k t m l mc
c1 (leak_bool true :: k') t m l (ami 2 (aml 2 (amj 1 mc')))
| if_false_step k t m l mc e c1 c2
v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
(_ : word.unsigned v = 0)
: step (scond e c1 c2) k t m l mc
c2 (leak_bool false :: k') t m l (ami 2 (aml 2 (amj 1 mc')))
| seq_step c1 c2
k t m l mc
c1' k' t' m' l' mc'
(_ : step c1 k t m l mc c1' k' t' m' l' mc')
: step (sseq c1 c2) k t m l mc
(sseq c1' c2) k' t' m' l' mc'
| seq_done_step c2
k t m l mc
: step (sseq sskip c2) k t m l mc
c2 k t m l mc
| while_false_step e c
k t m l mc
v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
(_ : word.unsigned v = 0)
: step (swhile e c) k t m l mc
sskip (leak_bool false :: k') t m l (ami 1 (aml 1 (amj 1 mc')))
| while_true_step e c
k t m l mc post
v mc' k' (_ : eval_expr m l e mc k = Some (v, mc', k'))
(_ : word.unsigned v <> 0)
: step (swhile e c) k t m l mc
(sseq c (sseq jump_back (swhile e c))) (leak_bool true :: k') t m l mc'
| jump_back_step
k t m l mc
: step jump_back k t m l mc
sskip k t m l (ami 2 (aml 2 (amj 1 mc)))
| call_step binds fname arges
k t m l mc
params rets fbody (_ : map.get e fname = Some (params, rets, fbody))
: step (scall binds fname arges) k t m l mc
(sseq (start_call binds params rets (inclusion fbody) arges) (end_call binds rets l)) k t m l mc
| start_call_step binds params rets sfbody arges
k t m l mc
args mc' k' (_ : evaluate_call_args_log m l arges mc k = Some (args, mc', k'))
lf (_ : map.of_list_zip params args = Some lf)
: step (start_call binds params rets sfbody arges) k t m l mc
sfbody (leak_unit :: k') t m lf (ami 100 (amj 100 (aml 100 (ams 100 mc'))))
| end_call_step binds rets (l : locals)
k t m (st1 : locals) mc l'
(_ : exists retvs,
map.getmany_of_list st1 rets = Some retvs /\
map.putmany_of_list_zip binds retvs l = Some l')
: step (end_call binds rets l) k t m st1 mc
sskip k t m l' (ami 100 (amj 100 (aml 100 (ams 100 mc))))
| interact_step binds action arges
k t m l mc
mKeep mGive (_: map.split m mKeep mGive)
args mc' k' (_ : evaluate_call_args_log m l arges mc k = Some (args, mc', k'))
(_ : ext_spec t mGive action args (fun _ _ _ => True))
mReceive resvals klist
(_ : forall mid, ext_spec t mGive action args mid -> mid mReceive resvals klist)
: step (sinteract binds action arges) k t m l mc
(end_interact binds action args mKeep mGive mReceive resvals klist) k' t m l mc'
| end_interact_step (binds : list String.string) (action : String.string) (args : list word)
(k' : trace) (mc' : MetricLog) (mKeep mGive mReceive : mem) (resvals klist : list word)
k t m l mc
l' (_ : map.putmany_of_list_zip binds resvals l = Some l')
m' (_ : map.split m' mKeep mReceive)
: step (end_interact binds action args mKeep mGive mReceive resvals klist) k t m l mc
sskip (leak_list klist :: k)%list (((mGive, action, args), (mReceive, resvals)) :: t) m' l' (ami 1 (ams 1 (aml 2 mc))).
Definition sstate : Type := scmd * trace * io_trace * mem * locals * metrics.
Definition get_scmd (st : sstate) : scmd :=
let '(s, k, t, m, l, mc) := st in s.
Definition other_inclusion st : sstate :=
let '(s, k, t, m, l, mc) := st in
(inclusion s, k, t, m, l, mc).
Definition state_step st st' : Prop :=
let '(s, k, t, m, l, mc) := st in
let '(s', k', t', m', l', mc') := st' in
step s k t m l mc s' k' t' m' l' mc'.
(*Definition done_state f i :=
get_scmd (f i) = sskip /\ get_scmd (f (S i)) = terminated.*)
Definition o1 {X : Type} (R : X -> Prop) (x : option X) : Prop :=
match x with
| Some x => R x
| _ => False
end.
Definition o2 {X Y : Type} (R : X -> Y -> Prop) x y : Prop :=
match x, y with
| Some x, Some y => R x y
| _, _ => False
end.
Definition state_stuck st :=
~exists st', state_step st st'.
Definition stuck_ostate f i :=
o1 state_stuck (f i) /\ f (S i) = None.