Source file cfgWP.ml
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
open LogicUsage
open Cil_types
open Cil_datatype
open WpPropId
open Clabels
open Qed
open Lang
open Lang.F
open Sigs
open Wpo
module type VCgen =
sig
include Mcfg.S
val register_lemma : logic_lemma -> unit
val compile_lemma : logic_lemma -> Wpo.t
val compile_wp : Wpo.index -> t_prop -> Wpo.t Bag.t
end
module VC( C : Sigs.Compiler ) : VCgen =
struct
open C
open C.M
module V = Vars
module P = WpPropId.PropId
let state = Mstate.create (module M)
type target =
| Gprop of P.t
| Geffect of P.t * Stmt.t * Mcfg.effect_source
| Gterminates of P.t * Stmt.t * Mcfg.terminates_source
| Gposteffect of P.t
module TARGET =
struct
type t = target
let hsrc = function
| Mcfg.FromCode -> 1 | FromCall -> 2 | FromReturn -> 3
let hterm = function
| Mcfg.Loop -> 1
| Terminates -> 2
| Decreases -> 3
| MissingDecreases -> 4
| MissingTerminates -> 5
| Dependencies -> 6
let hash = function
| Gprop p | Gposteffect p -> P.hash p
| Geffect(p,s,e) -> P.hash p * 37 + 41 * Stmt.hash s + hsrc e
| Gterminates(p,s,e) -> P.hash p * 37 + 41 * Stmt.hash s + hterm e
let compare g1 g2 =
if g1 == g2 then 0 else
match g1,g2 with
| Gprop p1 , Gprop p2 -> P.compare p1 p2
| Gprop _ , _ -> (-1)
| _ , Gprop _ -> 1
| Geffect(p1,s1,e1) , Geffect(p2,s2,e2) ->
let c = P.compare p1 p2 in
if c <> 0 then c else
let c = Stmt.compare s1 s2 in
if c <> 0 then c else
hsrc e1 - hsrc e2
| Geffect _ , _ -> (-1)
| _ , Geffect _ -> 1
| Gterminates(p1,s1,e1) , Gterminates(p2,s2,e2) ->
let c = P.compare p1 p2 in
if c <> 0 then c else
let c = Stmt.compare s1 s2 in
if c <> 0 then c else
hterm e1 - hterm e2
| Gterminates _ , _ -> (-1)
| _ , Gterminates _ -> 1
| Gposteffect p1 , Gposteffect p2 -> P.compare p1 p2
let equal g1 g2 = (compare g1 g2 = 0)
let prop_id = function
| Gprop p | Gposteffect p | Geffect(p,_,_) | Gterminates(p,_,_) -> p
let source = function
| Gprop _ | Gposteffect _ -> None
| Geffect(_,s,e) -> Some(s, Mcfg.Effect e)
| Gterminates (_,s,e) -> Some(s, Mcfg.Terminates e)
let is_smoke_test = function
| Gprop p -> WpPropId.is_smoke_test p
| Gposteffect _ | Geffect _ | Gterminates _ -> false
let pp_terminates_source fmt = function
| Mcfg.Loop -> Format.fprintf fmt "Loop terminates"
| Terminates -> Format.fprintf fmt "Terminates"
| Decreases -> Format.fprintf fmt "Decreases"
| MissingTerminates -> Format.fprintf fmt "Missing terminates"
| MissingDecreases -> Format.fprintf fmt "Missing decreases"
| Dependencies -> Format.fprintf fmt "Terminates dependencies"
let pretty fmt = function
| Gprop p -> WpPropId.pretty fmt p
| Geffect(p,s,FromCode) ->
Format.fprintf fmt "%a at sid:%d" WpPropId.pretty p s.sid
| Geffect(p,s,FromCall) ->
Format.fprintf fmt "Call %a at sid:%d" WpPropId.pretty p s.sid
| Geffect(p,s,FromReturn) ->
Format.fprintf fmt "Return %a at sid:%d" WpPropId.pretty p s.sid
| Gposteffect p -> Format.fprintf fmt "%a post-effect" WpPropId.pretty p
| Gterminates(p,s,src) ->
Format.fprintf fmt
"%a %a at sid:%d"
pp_terminates_source src WpPropId.pretty p s.sid
end
type assigns_effect = {
e_pid : P.t ;
e_post : bool ;
e_label : c_label ;
e_valid : L.sigma ;
e_region : L.region ;
e_warn : Warning.Set.t ;
}
module EFFECT =
struct
type t = assigns_effect
let compare e1 e2 = P.compare e1.e_pid e2.e_pid
let pretty fmt e =
Format.fprintf fmt "@[<hov 2>EFFECT %a:@ %a@]"
P.pretty e.e_pid (Cvalues.pp_region M.pretty) e.e_region
[@@ warning "-32"]
end
module G = Qed.Collection.Make(TARGET)
module W = Warning.Set
module D = Property.Set
module S = Stmt.Set
module Eset = Set.Make(EFFECT)
module Gset = G.Set
module Gmap = G.Map
type vc = {
hyps : Conditions.bundle ;
goal : F.pred ;
vars : Vars.t ;
warn : W.t ;
deps : D.t ;
path : S.t ;
}
type t_env = {
frame : L.frame ;
main : L.env ;
}
type t_prop = {
sigma : L.sigma option ;
effects : Eset.t ;
vcs : vc Splitter.t Gmap.t ;
}
let pp_vc fmt vc =
if Wp_parameters.debug_atleast 2 then
begin
List.iter
(Format.fprintf fmt "Have @[<hov 2>%a@]@." F.pp_pred)
(Conditions.extract vc.hyps) ;
Format.fprintf fmt "Goal @[<hov 2>%a@]@]@." F.pp_pred vc.goal ;
end
else
Pcond.dump_bundle ~clause:"Context" ~goal:vc.goal fmt vc.hyps
let pp_vcs fmt vcs =
let k = ref 0 in
Splitter.iter
(fun tags vc ->
incr k ;
begin
match tags with
| [] -> ()
| t::ts ->
Format.fprintf fmt " (%a" Splitter.pretty t ;
List.iter (fun t -> Format.fprintf fmt ",%a" Splitter.pretty t) ts ;
Format.fprintf fmt ")@\n" ;
end ;
Format.fprintf fmt "@[<hov 5> (%d) %a@]@\n" !k pp_vc vc)
vcs
let pp_gvcs fmt gvcs =
Gmap.iter_sorted
(fun goal vcs ->
let n = Splitter.length vcs in
Format.fprintf fmt "Goal %a: (%d)@\n" TARGET.pretty goal n ;
pp_vcs fmt vcs ;
) gvcs
let pretty fmt wp =
begin
(match wp.sigma with None -> () | Some s ->
Format.fprintf fmt "Sigma:@[<hov 2>%a@]@\n" Sigma.pretty s) ;
pp_gvcs fmt wp.vcs ;
end
let empty_vc = {
hyps = Conditions.nil ;
goal = p_true ;
vars = V.empty ;
warn = W.empty ;
deps = D.empty ;
path = S.empty ;
}
let sigma_opt = function None -> Sigma.create () | Some s -> s
let sigma_at w = sigma_opt w.sigma
let sigma_union s1 s2 =
match s1 , s2 with
| None , s | s , None -> sigma_opt s , Passive.empty , Passive.empty
| Some s1 , Some s2 -> Sigma.merge s1 s2
let merge_sigma s1 s2 =
match s1 , s2 with
| None , s | s , None -> s , Passive.empty , Passive.empty
| Some s1 , Some s2 -> let s,p1,p2 = Sigma.merge s1 s2 in Some s,p1,p2
let join_with s = function None -> Passive.empty | Some s' -> Sigma.join s s'
let occurs_vc vc x =
Vars.mem x vc.vars || Conditions.occurs x vc.hyps
let intersect_vc vc p =
Vars.intersect (F.varsp p) vc.vars || Conditions.intersect p vc.hyps
let state_vc ?descr ?stmt sigma state vc =
let path = match stmt with
| None -> vc.path
| Some s -> S.add s vc.path in
let hyps =
if not (Wp_parameters.RTE.get()) then vc.hyps
else Conditions.domain [M.is_well_formed sigma] vc.hyps
in
let hyps = Conditions.state ?stmt ?descr state hyps in
{ vc with path ; hyps }
let assume_vc ?descr ?hpid ?stmt ?warn
?(filter=false) ?(domain=false) ?(init=false)
hs vc =
if (hs = [] && warn = None) ||
(filter && not (List.exists (intersect_vc vc) hs))
then vc else
let path = match stmt with
| None -> vc.path
| Some s -> S.add s vc.path in
let deps = match hpid with
| None -> [] | Some p -> [WpPropId.property_of_id p] in
let dset = List.fold_right D.add deps vc.deps in
let wrns = match warn with
| None -> vc.warn
| Some w -> Warning.Set.union w vc.warn in
let hyps = Conditions.assume
?descr ?stmt ?warn ~deps ~init ~domain
(F.p_conj hs) vc.hyps
in {
hyps = hyps ;
goal = vc.goal ;
vars = vc.vars ;
warn = wrns ;
deps = dset ;
path = path ;
}
let probe_vc ~loc ?descr ?stmt ?warn ~name term vc =
let vars = F.vars term in
let hyps = Conditions.probe ~loc ?descr ?stmt ~name term vc.hyps in
let wrns = match warn with
| None -> vc.warn
| Some w -> Warning.Set.union w vc.warn in
{ hyps = hyps ;
vars = vars ;
warn = wrns ;
goal = vc.goal ;
deps = vc.deps ;
path = vc.path }
let branch_vc ~stmt ~warn cond vc1 vc2 =
let hyps , goal =
if F.eqp vc1.goal vc2.goal then
begin
Conditions.branch ~stmt ~warn cond vc1.hyps vc2.hyps ,
vc1.goal
end
else
let k = F.e_var (Lang.freshvar ~basename:"K" Logic.Bool) in
let p = F.p_equal k F.e_true in
let q = F.p_equal k F.e_false in
let h1 = Conditions.assume p vc1.hyps in
let h2 = Conditions.assume q vc2.hyps in
(Conditions.branch ~stmt ~warn cond h1 h2 , F.p_if p vc1.goal vc2.goal)
in
{
hyps = hyps ;
goal = goal ;
vars = V.union vc1.vars vc2.vars ;
deps = D.union vc1.deps vc2.deps ;
warn = W.union vc1.warn vc2.warn ;
path = S.union vc1.path vc2.path ;
}
let merge_vc vc1 vc2 =
let hyps , goal =
if F.eqp vc1.goal vc2.goal then
Conditions.merge [vc1.hyps;vc2.hyps] , vc1.goal
else
let k = F.e_var (Lang.freshvar ~basename:"K" Logic.Bool) in
let p = F.p_equal k F.e_true in
let q = F.p_equal k F.e_false in
let h1 = Conditions.assume ~descr:"Merge Left" p vc1.hyps in
let h2 = Conditions.assume ~descr:"Merge Right" q vc2.hyps in
(Conditions.merge [h1 ; h2] , F.p_if p vc1.goal vc2.goal)
in
{
hyps = hyps ;
goal = goal ;
vars = V.union vc1.vars vc2.vars ;
deps = D.union vc1.deps vc2.deps ;
warn = W.union vc1.warn vc2.warn ;
path = S.union vc1.path vc2.path ;
}
let merge_vcs = function
| [] -> empty_vc
| [vc] -> vc
| vcs ->
let hyps = Conditions.merge (List.map (fun vc -> vc.hyps) vcs) in
let goal = p_all (fun vc -> vc.goal) vcs in
let vars = List.fold_left (fun d vc -> V.union d vc.vars) V.empty vcs in
let deps = List.fold_left (fun d vc -> D.union d vc.deps) D.empty vcs in
let warn = List.fold_left (fun d vc -> W.union d vc.warn) W.empty vcs in
let path = List.fold_left (fun d vc -> S.union d vc.path) S.empty vcs in
{ hyps ; goal ; vars ; deps ; warn ; path }
let gmerge = Gmap.union (fun _gid -> Splitter.union merge_vc)
let gmap phi vcs =
Gmap.map (Splitter.map phi) vcs
let gbranch ~left ~both ~right vcs1 vcs2 =
Gmap.merge
(fun _g w1 w2 ->
match w1 , w2 with
| None , None -> None
| Some vcs1 , None ->
Some (Splitter.map left vcs1)
| None , Some vcs2 ->
Some (Splitter.map right vcs2)
| Some vcs1 , Some vcs2 ->
Some (Splitter.merge ~left ~both ~right vcs1 vcs2)
) vcs1 vcs2
let merge_all_vcs : vc Splitter.t Gmap.t list -> vc Splitter.t Gmap.t =
fun cases ->
let targets = List.fold_left
(fun goals vcs -> Gset.union goals (Gmap.domain vcs))
Gset.empty cases in
let goal g vcs =
try
let vcs = Gmap.find g vcs in
if TARGET.is_smoke_test g
then Splitter.unmark merge_vcs vcs
else vcs
with Not_found -> Splitter.empty in
Gset.mapping
(fun g -> Splitter.merge_all merge_vcs (List.map (goal g) cases))
targets
let passify_vc pa vc =
let hs = Passive.conditions pa (occurs_vc vc) in
assume_vc hs vc
let passify_vcs pa vcs =
if Passive.is_empty pa then vcs
else gmap (passify_vc pa) vcs
let empty = {
sigma = None ;
effects = Eset.empty ;
vcs = Gmap.empty ;
}
let has_init wenv =
let frame = wenv.frame in
let init = L.mem_at_frame frame Clabels.init in
let domain = Sigma.domain init in
not (M.Heap.Set.is_empty domain)
let merge wenv wp1 wp2 =
L.in_frame wenv.frame
(fun () ->
let sigma,pa1,pa2 = merge_sigma wp1.sigma wp2.sigma in
let effects = Eset.union wp1.effects wp2.effects in
let vcs1 = passify_vcs pa1 wp1.vcs in
let vcs2 = passify_vcs pa2 wp2.vcs in
let vcs = gmerge vcs1 vcs2 in
{ sigma = sigma ; vcs = vcs ; effects = effects }
) ()
let new_env ?lvars kf =
let frame = L.frame kf in
let env = L.in_frame frame (L.mk_env ?lvars) () in
{ frame = frame ; main = env }
let in_wenv
(wenv:t_env) (wp:t_prop)
(phi:L.env -> t_prop -> 'a) : 'a =
L.in_frame wenv.frame
(fun wp ->
match wp.sigma with
| None ->
let s = Sigma.create () in
phi (L.move_at wenv.main s) { wp with sigma = Some s }
| Some s ->
phi (L.move_at wenv.main s) wp) wp
let introduction pred =
let hs , goal = Conditions.forall_intro pred in
let xs = List.fold_left
(fun xs h -> Vars.union xs (F.varsp h))
(F.varsp goal) hs
in xs , hs , goal
let add_vc
target ?(warn=Warning.Set.empty) ?(deps=Property.Set.empty) pred vcs =
let xs , hs , goal = introduction pred in
if Gmap.mem target vcs then
Wp_parameters.failure
"Multiple goals for the same target (%a)" TARGET.pretty target ;
let hyps = Conditions.intros hs Conditions.nil in
let vc = { empty_vc with goal ; vars=xs ; hyps ; warn ; deps } in
Gmap.add target (Splitter.singleton vc) vcs
let cc_effect env pid (ainfo:WpPropId.assigns_desc) : assigns_effect option =
let from = ainfo.WpPropId.a_label in
let sigma = L.mem_frame from in
let authorized_region =
L.assigned_of_assigns
(match ainfo.a_kind with
| StmtAssigns -> L.move_at env sigma
| LoopAssigns -> env)
ainfo.a_assigns
in match authorized_region with
| None -> None
| Some region ->
let post = match ainfo.a_kind with
| LoopAssigns -> true
| StmtAssigns -> NormAtLabels.has_postassigns ainfo.a_assigns
in Some {
e_pid = pid ;
e_post = post ;
e_label = from ;
e_valid = sigma ;
e_region = region ;
e_warn = Warning.Set.empty ;
}
let cc_posteffect e vcs =
if not e.e_post then vcs else
let vc = { empty_vc with vars = L.vars e.e_region } in
Gmap.add (Gposteffect e.e_pid) (Splitter.singleton vc) vcs
let add_axiom _id _l = ()
let add_probe wenv ?stmt probe term wp = in_wenv wenv wp
(fun env wp ->
let outcome =
Warning.catch
~severe:false ~fallback:"Skip probe"
(L.term env) term in
match outcome with
| Warning.Failed _warn -> wp
| Warning.Result(warn,value) ->
let add_probe_vc =
probe_vc ~loc:term.term_loc ?stmt ~warn ~name:probe value in
{ wp with vcs = gmap add_probe_vc wp.vcs })
let add_hyp ?for_pid wenv (hpid,predicate) wp = in_wenv wenv wp
(fun env wp ->
let outcome = Warning.catch
~severe:false ~fallback:"Skip hypothesis"
(L.pred `Negative env) predicate in
let warn,hs = match outcome with
| Warning.Result(warn,p) -> warn , [p]
| Warning.Failed warn -> warn , []
in
let assume_vc target vcs = match for_pid with
| Some id when not @@ PropId.equal id (TARGET.prop_id target) -> vcs
| _ -> Splitter.map (assume_vc ~hpid ~warn hs) vcs
in
let vcs = Gmap.mapi assume_vc wp.vcs in
{ wp with vcs = vcs })
let add_goal wenv (gpid,predicate) wp = in_wenv wenv wp
(fun env wp ->
let outcome = Warning.catch
~severe:true ~fallback:"Degenerated goal"
(L.pred `Positive env) predicate in
let warn,goal = match outcome with
| Warning.Result(warn,goal) -> warn,goal
| Warning.Failed warn -> warn,F.p_false
in
let vcs = add_vc (Gprop gpid) ~warn goal wp.vcs in
{ wp with vcs = vcs })
let add_terminates_subgoal wenv (gpid,_) ?deps predicate stmt src wp =
in_wenv wenv wp
(fun env wp ->
let outcome = Warning.catch
~severe:true ~fallback:"Degenerated goal"
(L.pred `Positive env) predicate in
let warn,goal = match outcome with
| Warning.Result(warn,goal) -> warn,goal
| Warning.Failed warn -> warn,F.p_false
in
let vcs =
add_vc (Gterminates(gpid, stmt, src)) ~warn ?deps goal wp.vcs in
{ wp with vcs = vcs })
let add_assigns wenv (gpid,ainfo) wp = in_wenv wenv wp
begin fun env wp ->
let outcome = Warning.catch
~severe:true ~fallback:"Degenerated goal"
(cc_effect env gpid) ainfo
in match outcome with
| Warning.Result (_,None) -> wp
| Warning.Result (warn,Some e) ->
let e = { e with e_warn = warn } in
let effects = Eset.add e wp.effects in
let vcs = cc_posteffect e wp.vcs in
{ wp with effects = effects ; vcs = vcs }
| Warning.Failed warn ->
let vcs = add_vc (Gprop gpid) ~warn p_false wp.vcs in
{ wp with vcs = vcs }
end
let add_warnings wrns vcs =
gmap (fun vc -> { vc with warn = W.union wrns vc.warn }) vcs
let assigns_condition (region : L.region) (e:assigns_effect) : F.pred =
let unfold = Wp_parameters.UnfoldAssigns.get () in
L.check_assigns ~unfold e.e_valid ~written:region ~assignable:e.e_region
exception COLLECTED
let is_collected vcs p =
try
Gmap.iter
(fun target vcs ->
let q = TARGET.prop_id target in
if P.equal p q && Splitter.length vcs > 0 then raise COLLECTED
) vcs ;
false
with COLLECTED -> true
let check_nothing effects vcs =
Eset.fold
(fun e vcs ->
if is_collected vcs e.e_pid then vcs else
Gmap.add (Gprop e.e_pid) (Splitter.singleton empty_vc) vcs
) effects vcs
let check_assigns sloc source ?(warn=Warning.Set.empty) region effects vcs =
Eset.fold
(fun e vcs ->
let xs,hs,goal = introduction (assigns_condition region e) in
let warn = Warning.Set.union warn e.e_warn in
let setup vc =
{ vc with
warn = warn ;
hyps = Conditions.intros hs vc.hyps ;
goal = goal ;
vars = xs }
in
let group =
if not e.e_post then
Splitter.singleton (setup empty_vc)
else
try Splitter.map setup (Gmap.find (Gposteffect e.e_pid) vcs)
with Not_found ->
Wp_parameters.fatal "Missing post-effect for %a"
WpPropId.pretty e.e_pid
in
let target = match sloc with
| None -> Gprop e.e_pid
| Some stmt -> Geffect(e.e_pid,stmt,source)
in
Gmap.add target group vcs
) effects vcs
let do_assigns ?descr ?stmt ~source ?hpid ?warn sequence
~assigned effects vcs =
let vcs = check_assigns stmt source ?warn assigned effects vcs in
let eqmem = A.apply_assigns sequence assigned in
gmap (assume_vc ?descr ?hpid ?stmt ?warn eqmem) vcs
let do_assigns_everything ?stmt ?warn effects vcs =
Eset.fold
(fun e vcs ->
let target = match stmt with
| None -> Gprop e.e_pid
| Some s -> Geffect(e.e_pid,s,FromCode)
in
add_vc target ?warn F.p_false vcs)
effects vcs
let cc_assigned env kind froms =
let dummy = Sigma.create () in
let r0 = L.assigned_of_froms (L.move_at env dummy) froms in
let d0 = A.domain r0 in
let s1 = L.current env in
let s0 = Sigma.havoc s1 d0 in
let sref = match kind with
| StmtAssigns -> s0
| LoopAssigns -> s1
in
let cc_assigned = L.assigned_of_froms (L.move_at env sref) in
let assigned = cc_assigned froms in
let sequence = { pre=s0 ; post=s1 } in
sequence , assigned
let use_assigns wenv hpid ainfo wp = in_wenv wenv wp
begin fun env wp ->
let stmt = ainfo.a_stmt in
match ainfo.a_assigns with
| WritesAny ->
let sigma = Sigma.havoc_any ~call:false (L.current env) in
let vcs = do_assigns_everything ?stmt wp.effects wp.vcs in
{ sigma = Some sigma ; vcs=vcs ; effects = wp.effects }
| Writes froms ->
let kind = ainfo.WpPropId.a_kind in
let outcome =
Warning.catch ~severe:true ~fallback:"Assigns everything"
(cc_assigned env kind) froms
in
match outcome with
| Warning.Result(warn,(sequence,assigned)) ->
let vcs =
do_assigns ~source:FromCode
?hpid ?stmt ~warn sequence
~assigned
wp.effects wp.vcs in
{ sigma = Some sequence.pre ; vcs=vcs ; effects = wp.effects }
| Warning.Failed warn ->
let sigma = Sigma.havoc_any ~call:false (L.current env) in
let vcs = do_assigns_everything ?stmt ~warn wp.effects wp.vcs in
{ sigma = Some sigma ; vcs=vcs ; effects = wp.effects }
end
let is_stopeffect l e = Clabels.equal l e.e_label
let not_posteffect es target _vcs = match target with
| Gposteffect p -> not (Eset.exists (fun e -> P.equal p e.e_pid) es)
| _ -> true
let state_vcs stmt sigma vcs =
try
let descr : string option = match stmt with
| None | Some { labels=[] } -> None
| Some { labels = lbl::_ } ->
Some (Pretty_utils.to_string Printer.pp_label lbl) in
let state = Mstate.state state sigma in
gmap (state_vc ?descr ?stmt sigma state) vcs
with Not_found -> vcs
let label wenv stmt label wp =
if Clabels.is_here label then wp else
in_wenv wenv wp
(fun env wp ->
let frame = L.get_frame () in
let s_here = L.current env in
let s_frame =
if L.has_at_frame frame label then
L.mem_at_frame frame label
else
(L.set_at_frame frame label s_here ; s_here) in
let pa = Sigma.join s_here s_frame in
let stop,effects = Eset.partition (is_stopeffect label) wp.effects in
let vcs = Gmap.filter (not_posteffect stop) wp.vcs in
let vcs = passify_vcs pa vcs in
let vcs = check_nothing stop vcs in
let vcs = state_vcs stmt s_here vcs in
{ sigma = Some s_frame ; vcs=vcs ; effects=effects })
let cc_lval env lv =
let obj = Ctypes.object_of (Cil.typeOfLval lv) in
let dummy = Sigma.create () in
let l0 = C.lval dummy lv in
let s2 = L.current env in
let domain = M.domain obj l0 in
let s1 = Sigma.havoc s2 domain in
let loc = C.lval s1 lv in
let seq = { pre=s1 ; post=s2 } in
obj , domain , seq , loc
let cc_stored lv seq loc obj expr =
let intercept_volatile kind lv =
let warn = "unsafe " ^ kind ^ "-access to volatile l-value" in
Cil.isVolatileLval lv && Cvalues.volatile ~warn ()
in
if intercept_volatile "write" lv then None
else
let value = match expr.enode with
| Lval lv when not @@ intercept_volatile "read" lv ->
M.copied seq obj loc (C.lval seq.pre lv)
| _ ->
M.stored seq obj loc (C.val_of_exp seq.pre expr)
in
let init = match expr.enode with
| Lval lv when intercept_volatile "read" lv ->
M.stored_init seq obj loc (Cvalues.initialized_obj obj)
| Lval lv when Cil.(isStructOrUnionType @@ typeOfLval lv) ->
M.copied_init seq obj loc (C.lval seq.pre lv)
| _ ->
M.stored_init seq obj loc (Cvalues.initialized_obj obj)
in
Some (value @ init)
let assign wenv stmt lv expr wp = in_wenv wenv wp
begin fun env wp ->
let outcome = Warning.catch
~severe:true ~fallback:"Assigns everything (unknown l-value)"
(cc_lval env) lv in
match outcome with
| Warning.Failed warn ->
let sigma = Sigma.havoc_any ~call:false (L.current env) in
let vcs = do_assigns_everything ~stmt ~warn wp.effects wp.vcs in
{ sigma = Some sigma ; vcs=vcs ; effects = wp.effects }
| Warning.Result(l_warn,(obj,dom,seq,loc)) ->
let assigned = [obj,Sloc loc] in
let outcome = Warning.catch
~severe:false ~fallback:"Havoc l-value (unknown r-value)"
(cc_stored lv seq loc obj) expr in
match outcome with
| Warning.Failed r_warn
| Warning.Result(r_warn,None) ->
let warn = Warning.Set.union l_warn r_warn in
let vcs = do_assigns ~source:FromCode
~stmt ~warn seq ~assigned wp.effects wp.vcs in
{ sigma = Some seq.pre ; vcs=vcs ; effects = wp.effects }
| Warning.Result(r_warn,Some stored) ->
let warn = Warning.Set.union l_warn r_warn in
let ft = M.Heap.Set.fold_sorted
(fun chunk ft -> M.Sigma.get seq.post chunk :: ft) dom []
in
let update vc =
if List.exists (occurs_vc vc) ft
then
let eqs = List.map Cvalues.equation stored in
assume_vc ~stmt ~warn eqs vc
else vc in
let vcs = gmap update wp.vcs in
let vcs =
check_assigns (Some stmt) FromCode assigned wp.effects vcs in
{ sigma = Some seq.pre ; vcs=vcs ; effects = wp.effects }
end
let return wenv stmt result wp =
match result with
| None -> wp
| Some exp ->
in_wenv wenv wp
begin fun env wp ->
let compile () =
let sigma = L.current env in
let vr = L.result () in
let tr = L.return () in
p_equal (C.result sigma tr vr) (C.return sigma tr exp) in
let outcome = Warning.catch
~severe:false ~fallback:"Result value discarded (unknown)"
compile () in
let warn, condition =
match outcome with
| Warning.Failed warn ->
warn , p_true
| Warning.Result(warn,condition) ->
warn , condition in
let vcs = gmap (
assume_vc ~descr:"Return" ~stmt ~warn [condition]
) wp.vcs in
{ wp with vcs = vcs }
end
let condition ~descr ?stmt ?warn pa h vc =
passify_vc pa (assume_vc ?stmt ?warn ~descr h vc)
let split_branch ~smoke tag = function
| None -> Splitter.empty
| Some s -> if smoke then s else Splitter.apply tag merge_vcs s
let random () =
let v = Lang.freshvar ~basename:"cond" Logic.Bool in
F.p_bool (F.e_var v)
let weight vcs =
Gmap.fold (fun _g s n -> n + Splitter.length s) vcs 0
let test wenv stmt exp wp1 wp2 = L.in_frame wenv.frame
(fun () ->
let sigma,pa1,pa2 = sigma_union wp1.sigma wp2.sigma in
let warn,cond =
match Warning.catch ~source:"Condition"
~severe:false ~fallback:"Skip condition value"
(C.cond sigma) exp
with
| Warning.Result(warn,cond) -> warn,cond
| Warning.Failed(warn) -> warn,random()
in
let effects = Eset.union wp1.effects wp2.effects in
let dosplit =
Wp_parameters.SplitBranch.get () &&
let n1 = weight wp1.vcs in
let n2 = weight wp2.vcs in
let nm = Wp_parameters.SplitMax.get () in
n1 + n2 <= nm in
let vcs =
if dosplit then
let cneg = p_not cond in
let vcs1 =
gmap (condition pa1 ~stmt ~warn ~descr:"Then" [cond]) wp1.vcs in
let vcs2 =
gmap (condition pa2 ~stmt ~warn ~descr:"Else" [cneg]) wp2.vcs in
Gmap.merge
(fun g w1 w2 ->
let smoke = TARGET.is_smoke_test g in
let s1 = split_branch ~smoke (Splitter.if_then stmt) w1 in
let s2 = split_branch ~smoke (Splitter.if_else stmt) w2 in
Some (Splitter.union (merge_vc) s1 s2)
) vcs1 vcs2
else
let vcs1 = passify_vcs pa1 wp1.vcs in
let vcs2 = passify_vcs pa2 wp2.vcs in
gbranch
~left:(assume_vc ~descr:"Then" ~stmt ~warn [cond])
~right:(assume_vc ~descr:"Else" ~stmt ~warn [p_not cond])
~both:(branch_vc ~stmt ~warn cond)
vcs1 vcs2
in
{ sigma = Some sigma ; vcs=vcs ; effects=effects }) ()
let rec cc_case_values ks vs sigma = function
| [] -> List.rev ks , List.rev vs
| e::es ->
match Ctypes.get_int64 e with
| Some k ->
cc_case_values (k::ks) (F.e_int64 k::vs) sigma es
| None ->
cc_case_values ks (C.val_of_exp sigma e::vs) sigma es
let cc_group_case stmt warn descr tag pa cond vcs : vc Splitter.t Gmap.t =
let split =
Wp_parameters.SplitSwitch.get () &&
weight vcs < Wp_parameters.SplitMax.get ()
in
Gmap.mapi
(fun g s ->
let smoke = TARGET.is_smoke_test g in
Splitter.map
(condition ~descr ~warn ~stmt pa cond)
(if smoke || not split then s else Splitter.apply tag merge_vcs s)
) vcs
let cc_case stmt warn sigma v (es,wp) =
let ks,vs = cc_case_values [] [] sigma es in
let pa = join_with sigma wp.sigma in
let eq = p_any (p_equal v) vs in
let msg = match ks with
| [k] -> "Case " ^ Int64.to_string k
| _ -> "Cases " ^ String.concat "," (List.map Int64.to_string ks) in
let tag = Splitter.switch_cases stmt ks in
vs , cc_group_case stmt warn msg tag pa [eq] wp.vcs
let cc_default stmt sigma neq default =
let pa = join_with sigma default.sigma in
cc_group_case stmt W.empty "Default"
(Splitter.switch_default stmt) pa neq default.vcs
let switch wenv stmt exp cases default = L.in_frame wenv.frame
(fun () ->
let domain =
List.fold_left (fun d (_,wp) ->
match wp.sigma with
| None -> d
| Some s -> Sigma.union d (Sigma.domain s)
) Sigma.empty cases in
let sigma = Sigma.havoc (Sigma.create ()) domain in
let warn,value =
match Warning.catch ~source:"Switch"
~severe:false ~fallback:"Skip switched value"
(C.val_of_exp sigma) exp
with
| Warning.Result(warn,value) -> warn,value
| Warning.Failed(warn) ->
let tau = Lang.tau_of_ctype (Cil.typeOf exp) in
warn,e_var (Lang.freshvar tau)
in
let vcs_cases = List.map (cc_case stmt warn sigma value) cases in
let neq = List.map (fun (vs,_) -> p_all (p_neq value) vs) vcs_cases in
let vcs_default = cc_default stmt sigma neq default in
let vcs = merge_all_vcs ( vcs_default :: List.rev_map snd vcs_cases ) in
let effects = List.fold_left
(fun es (_,wp) -> Eset.union es wp.effects)
default.effects cases in
{ sigma = Some sigma ; effects = effects ; vcs = vcs }) ()
let const wenv v wp = in_wenv wenv wp
(fun env wp ->
let shere = L.current env in
let sinit = L.mem_at env Clabels.init in
let const_vc = assume_vc
~init:true ~filter:true
~descr:"Global Constant"
[C.unchanged shere sinit v]
in { wp with vcs = gmap const_vc wp.vcs })
let init wenv var opt_init wp = in_wenv wenv wp
(fun env wp ->
let assume = assume_vc ~descr:"Initializer" ~filter:true ~init:true in
let sigma = L.current env in
let init_vc vc =
List.fold_left
(fun vc (warn,(hv,hi)) -> assume ~warn [hi] (assume ~warn [hv] vc))
vc (C.init ~sigma var opt_init)
in { wp with vcs = gmap init_vc wp.vcs })
let loop_step wp = wp
let loop_entry wp = wp
let call_pointer sigma fct =
let outcome = Warning.catch
~severe:true ~fallback:"Degenerated goal"
(C.call sigma) fct in
match outcome with
| Warning.Failed warn -> warn,None
| Warning.Result(warn,floc) -> warn,Some floc
let call_instance_of gpid (warn,fopt) calls vcs =
let goal = match fopt with
| None -> F.p_false
| Some floc -> F.p_any (C.instance_of floc) calls
in add_vc (Gprop gpid) ~warn goal vcs
let call_contract stmt sigma hpid (warn,fopt) (kf,wp) : vc Splitter.t Gmap.t =
let pa = join_with sigma wp.sigma in
let tag = Splitter.call stmt kf in
let descr =
Printf.sprintf "Instance of '%s'" (Kernel_function.get_name kf) in
let instance_of vc =
let hyp = match fopt with
| None -> F.p_true
| Some floc -> C.instance_of floc kf
in assume_vc ~stmt ~warn ~descr ~hpid [hyp] vc
in
Gmap.map
(fun s ->
Splitter.map
(fun vc -> passify_vc pa (instance_of vc))
(Splitter.apply tag merge_vcs s)
) wp.vcs
let call_dynamic wenv stmt gpid fct calls = L.in_frame wenv.frame
begin fun () ->
let sigma = Sigma.create () in
let called = call_pointer sigma fct in
let vcs_calls = List.map (call_contract stmt sigma gpid called) calls in
let vcs = merge_all_vcs vcs_calls in
let vcs = call_instance_of gpid called (List.map fst calls) vcs in
let effects = List.fold_left
(fun es (_,wp) -> Eset.union es wp.effects) Eset.empty calls in
{ sigma = Some sigma ; vcs = vcs ; effects = effects }
end ()
let call_goal_precond wenv _stmt kf es ~pre wp = in_wenv wenv wp
(fun env wp ->
let sigma = L.current env in
let outcome = Warning.catch
~severe:true ~fallback:"Can not prove call preconditions"
(List.map (C.exp sigma)) es in
match outcome with
| Warning.Failed warn ->
let vcs = List.fold_left
(fun vcs (gid,_) -> add_vc (Gprop gid) ~warn p_false vcs)
wp.vcs pre
in { wp with vcs = vcs }
| Warning.Result(warn,vs) ->
let init = L.mem_at env Clabels.init in
let call = L.call kf vs in
let call_e = L.mk_env ~here:sigma () in
let call_f = L.call_pre init call sigma in
let vcs = List.fold_left
(fun vcs (gid,p) ->
let outcome = Warning.catch
~severe:true ~fallback:"Can not prove call precondition"
(L.in_frame call_f (L.pred `Positive call_e)) p in
match outcome with
| Warning.Result(warn2,goal) ->
let warn = W.union warn warn2 in
add_vc (Gprop gid) ~warn goal vcs
| Warning.Failed warn2 ->
let warn = W.union warn warn2 in
add_vc (Gprop gid) ~warn p_false vcs
) wp.vcs pre
in { wp with vcs = vcs })
let call_terminates wenv stmt ~kind ?kf args (id, caller_t) ~callee_t wp =
in_wenv wenv wp
(fun env wp ->
let outcome = Warning.catch
~severe:true
~fallback:"Considering that call must always terminate"
(L.pred `Positive env) caller_t
in
let warn, caller_t = match outcome with
| Warning.Failed warn -> warn, p_true
| Warning.Result (warn, p) -> warn, p
in
let prove_terminates ~warn p =
add_vc (Gterminates(id, stmt, kind)) ~warn (p_imply caller_t p)
in
let sigma = L.current env in
let outcome = Warning.catch
~severe:true
~fallback:"Considering non terminating callee"
(List.map (C.exp sigma)) args in
match outcome with
| Warning.Failed warn2 ->
let warn = W.union warn warn2 in
let vcs = prove_terminates ~warn p_false wp.vcs in
{ wp with vcs = vcs }
| Warning.Result(warn2, args) ->
let warn = W.union warn warn2 in
let compile_callee p =
if Logic_utils.is_same_predicate Logic_const.pfalse p then
Lang.F.p_false
else
let init = L.mem_at env Clabels.init in
let call = L.call (Option.get kf) args in
let call_e = L.mk_env ~here:sigma () in
let call_f = L.call_pre init call sigma in
L.in_frame call_f (L.pred `Positive call_e) callee_t
in
let outcome =
Warning.catch
~severe:true ~fallback:"Considering non terminating callee"
compile_callee callee_t
in
let warn2, callee_t = match outcome with
| Warning.Failed warn -> warn, p_false
| Warning.Result(warn,callee_t) -> warn, callee_t
in
let warn = W.union warn warn2 in
let vcs = prove_terminates ~warn callee_t wp.vcs in
{ wp with vcs = vcs })
let call_decreases wenv stmt ?kf args (id, caller_d) ?caller_t ?callee_d wp =
in_wenv wenv wp
(fun env wp ->
let compile_caller_t caller_t =
if not @@ Wp_parameters.TerminatesVariantHyp.get () then p_true
else match caller_t with
| None -> p_true
| Some t -> (L.pred `Positive env) t
in
let outcome = Warning.catch
~severe:true
~fallback:"Considering that call must always decrease"
compile_caller_t caller_t
in
let warn, caller_t = match outcome with
| Warning.Failed warn -> warn, p_true
| Warning.Result (warn, p) -> warn, p
in
let prove_decreases ~warn p =
add_vc (Gterminates(id, stmt, Decreases)) ~warn (p_imply caller_t p)
in
let sigma = L.current env in
let outcome = Warning.catch
~severe:true
~fallback:"Considering non decreasing call"
(List.map (C.exp sigma)) args in
match outcome with
| Warning.Failed warn2 ->
let warn = W.union warn warn2 in
let vcs = prove_decreases ~warn p_false wp.vcs in
{ wp with vcs = vcs }
| Warning.Result(warn2, args) ->
let warn = W.union warn warn2 in
let compile_decreases (caller_d, callee_d) =
let pp_opt_kf =
Pretty_utils.pp_opt
~none:"(unknown function)" Kernel_function.pretty in
match caller_d, callee_d with
| _, None ->
Warning.error "No decreases clause for %a"
pp_opt_kf kf
| (_, r), Some (_, r')
when not @@ Option.equal Logic_utils.is_same_logic_info r r' ->
let none : Pretty_utils.sformat = "<None>" in
Warning.error
"On call to %a, relation (%a) does not match caller (%a)"
pp_opt_kf kf
(Pretty_utils.pp_opt ~none Cil_printer.pp_logic_info) r
(Pretty_utils.pp_opt ~none Cil_printer.pp_logic_info) r'
| (caller_d, rel), Some (callee_d,_ ) ->
let init = L.mem_at env Clabels.init in
let call = L.call (Option.get kf) args in
let call_e = L.mk_env ~here:sigma () in
let call_f = L.call_pre init call sigma in
let rel caller callee = match rel with
| None ->
p_and (p_leq e_zero caller) (p_lt callee caller)
| Some rel ->
(L.in_frame call_f (L.call_pred call_e))
rel [] [caller ; callee]
in
let caller_d = L.term env caller_d in
let callee_d =
(L.in_frame call_f (L.term call_e)) callee_d in
rel caller_d callee_d
in
let outcome =
Warning.catch
~severe:true ~fallback:"Considering non decreasing call"
compile_decreases (caller_d, callee_d)
in
let warn2, pred = match outcome with
| Warning.Failed warn -> warn, p_false
| Warning.Result (warn, p) -> warn, p
in
let warn = W.union warn warn2 in
let vcs = prove_decreases ~warn pred wp.vcs in
{ wp with vcs = vcs })
type callenv = {
sigma_pre : sigma ;
seq_post : sigma sequence ;
seq_exit : sigma sequence ;
seq_result : sigma sequence ;
loc_result : (typ * Ctypes.c_object * loc) option ;
frame_pre : L.frame ;
frame_post : L.frame ;
frame_exit : L.frame ;
}
let cc_result_domain = function
| Some lv ->
let dummy = Sigma.create () in
let tr = Cil.typeOfLval lv in
let lr = C.lval dummy lv in
Some (M.domain (Ctypes.object_of tr) lr)
| None -> Some (M.Heap.Set.empty)
let cc_call_domain env0 kf es = function
| WritesAny -> None
| Writes froms ->
let dummy = Sigma.create () in
let vs = List.map (C.exp dummy) es in
let env = L.move_at env0 dummy in
let init = L.mem_at env0 Clabels.init in
let frame = L.call_pre init (L.call kf vs) dummy in
let cc_froms = L.assigned_of_froms env in
Some (A.domain (L.in_frame frame cc_froms froms))
let cc_havoc d s = match d with
| None -> { pre = Sigma.havoc_any ~call:true s ; post = s }
| Some domain -> { pre = Sigma.havoc s domain ; post = s }
let cc_callenv env0 lvr kf es assigns wpost wexit =
let init = L.mem_at env0 Clabels.init in
let dom_call = cc_call_domain env0 kf es assigns in
let dom_vret = cc_result_domain lvr in
let seq_result = cc_havoc dom_vret (sigma_at wpost) in
let seq_post = cc_havoc dom_call seq_result.pre in
let seq_exit = cc_havoc dom_call (sigma_at wexit) in
let sigma_pre, _, _ = Sigma.merge seq_post.pre seq_exit.pre in
let formals = List.map (C.exp sigma_pre) es in
let call = L.call kf formals in
let result = match lvr with
| None -> None
| Some lv ->
let tr = Cil.typeOfLval lv in
let obj = Ctypes.object_of tr in
let loc = C.lval sigma_pre lv in
Some (tr,obj,loc)
in
{
sigma_pre = sigma_pre ;
seq_post = seq_post ;
seq_exit = seq_exit ;
seq_result = seq_result ;
loc_result = result ;
frame_pre = L.call_pre init call sigma_pre ;
frame_post = L.call_post init call seq_post ;
frame_exit = L.call_post init call seq_exit ;
}
type call_vcs = {
vcs_post : vc Splitter.t Gmap.t ;
vcs_exit : vc Splitter.t Gmap.t ;
}
let cc_call_effects stmt cenv env0 assigns wpost wexit =
match assigns with
| WritesAny ->
{
vcs_post = do_assigns_everything ~stmt wpost.effects wpost.vcs ;
vcs_exit = do_assigns_everything ~stmt wexit.effects wexit.vcs ;
}
| Writes froms ->
let env = L.move_at env0 cenv.sigma_pre in
let cc_region = L.assigned_of_froms env in
let vcs_post =
let assigned = L.in_frame cenv.frame_post cc_region froms in
do_assigns ~descr:"Call Effects" ~source:FromCall
~stmt cenv.seq_post ~assigned wpost.effects wpost.vcs in
let vcs_exit =
let assigned = L.in_frame cenv.frame_exit cc_region froms in
do_assigns ~descr:"Exit Effects" ~source:FromCall
~stmt cenv.seq_exit ~assigned wexit.effects wexit.vcs in
let vcs_result =
match cenv.loc_result with
| None -> vcs_post
| Some(_,obj,loc) ->
let assigned = [obj,Sloc loc] in
do_assigns ~descr:"Return Effects"
~source:FromReturn ~stmt cenv.seq_result
~assigned wpost.effects vcs_post
in
{ vcs_post = vcs_result ; vcs_exit = vcs_exit }
let cc_contract_hyp frame env contract =
L.in_frame frame
(List.map (fun (_,p) -> L.pred `Negative env p)) contract
let cc_result call = match call.loc_result with
| None -> []
| Some(tr,obj,loc) ->
let handler () = [ p_true ] in
let compile () =
let vr = M.load call.seq_result.post obj loc in
let re = L.in_frame call.frame_post L.result () in
let te = L.in_frame call.frame_post L.return () in
let value = C.result call.sigma_pre tr re in
[ C.equal_typ tr vr (C.cast tr te (Val value)) ]
in
Warning.handle ~handler ~severe:false ~fallback:"Hide \\result" compile ()
let cc_status f_caller f_callee =
p_equal
(e_var (L.in_frame f_caller L.status ()))
(e_var (L.in_frame f_callee L.status ()))
let call_proper wenv stmt lvr kf es ~pre ~post ~pexit ~assigns ~p_post ~p_exit () =
let call = cc_callenv wenv.main lvr kf es assigns p_post p_exit in
let env_pre = L.move_at wenv.main call.sigma_pre in
let env_post = L.move_at wenv.main call.seq_post.post in
let env_exit = L.move_at wenv.main call.seq_exit.post in
let hs_pre = cc_contract_hyp call.frame_pre env_pre pre in
let hs_post = cc_contract_hyp call.frame_post env_post post in
let hs_exit = cc_contract_hyp call.frame_exit env_exit pexit in
let hs_post = cc_result call @ hs_post in
let hs_exit = cc_status wenv.frame call.frame_exit :: hs_exit in
let ceff = cc_call_effects stmt call wenv.main assigns p_post p_exit in
let fname = Kernel_function.get_name kf in
let apply outcome pa hs vcs =
let descr = Printf.sprintf "%s '%s'" outcome fname in
gmap (condition ~descr ~stmt pa hs) vcs in
let pa_post = Sigma.join call.sigma_pre call.seq_post.pre in
let pa_exit = Sigma.join call.sigma_pre call.seq_exit.pre in
let hs_pre = if Wp_parameters.CalleePreCond.get () then hs_pre else [] in
let cond_post = apply "Call" pa_post (hs_pre @ hs_post) ceff.vcs_post in
let cond_exit = apply "Exit" pa_exit (hs_pre @ hs_exit) ceff.vcs_exit in
let vcs = gmerge cond_post cond_exit in
let effects = Eset.union p_post.effects p_exit.effects in
{ sigma = Some call.sigma_pre ; effects=effects ; vcs=vcs }
let call wenv stmt lvr kf es ~pre ~post ~pexit ~assigns ~p_post ~p_exit
= L.in_frame wenv.frame
(fun () ->
let outcome = Warning.catch
~severe:true ~fallback:"Call assigns everything"
(call_proper wenv stmt lvr kf es
~pre ~post ~pexit ~assigns ~p_post ~p_exit) () in
match outcome with
| Warning.Result(warn , wp) -> { wp with vcs = add_warnings warn wp.vcs }
| Warning.Failed warn ->
let v_post = do_assigns_everything ~stmt ~warn p_post.effects p_post.vcs in
let v_exit = do_assigns_everything ~stmt ~warn p_exit.effects p_exit.vcs in
let effects = Eset.union p_post.effects p_exit.effects in
let vcs = gmerge v_post v_exit in
let sigma = Sigma.create () in
{ sigma = Some sigma ; vcs = vcs ; effects = effects }
) ()
let wp_scope env wp ~descr scope xs =
let sigma = L.current env in
let pre = M.alloc sigma xs in
let hs = M.scope { pre ; post = sigma } scope xs in
let vcs = gmap (assume_vc ~descr hs) wp.vcs in
{ wp with sigma = Some pre ; vcs = vcs }
let scope wenv xs sc wp = in_wenv wenv wp
begin fun env wp ->
match sc with
| Mcfg.SC_Global ->
let hs = M.frame (L.current env) in
let vcs = gmap (assume_vc ~descr:"Heap" ~domain:true hs) wp.vcs in
{ wp with vcs }
| Mcfg.SC_Frame_in ->
wp_scope env wp ~descr:"Frame In" Enter xs
| Mcfg.SC_Frame_out ->
wp_scope env wp ~descr:"Frame Out" Leave xs
| Mcfg.SC_Block_in ->
wp_scope env wp ~descr:"Block In" Enter xs
| Mcfg.SC_Block_out ->
wp_scope env wp ~descr:"Block Out" Leave xs
end
let close wenv wp =
let guards = L.guards wenv.frame in
let vcs = gmap
(fun vc ->
let gdom = List.filter (intersect_vc vc) guards in
let hyps = Conditions.domain gdom vc.hyps in
{ vc with hyps = hyps ; vars = Vars.empty }
) wp.vcs
in
{ wp with vcs = vcs }
let cc_from deps hs vc =
let guards = Lang.get_hypotheses () in
let hyps = Conditions.assume ~descr:"Bisimulation" (p_conj guards) vc.hyps in
let p = F.p_hyps (Conditions.extract hyps) vc.goal in
let alpha = Lang.alpha () in
let a_hs = List.map (F.p_subst alpha) hs in
let a_p = F.p_subst alpha p in
let p = p_hyps a_hs a_p in
{ vc with
goal = p ; vars = F.varsp p ;
hyps = Conditions.nil ;
deps = D.union deps vc.deps ;
}
let build_prop_of_from wenv preconds wp = in_wenv wenv wp
(fun env wp ->
let sigma = L.mem_frame Clabels.pre in
let env_pre = L.move_at env sigma in
let hs = List.map
(fun (_,p) -> L.pred `Negative env_pre p)
preconds in
let ds = List.fold_left
(fun ds (pid,_) -> D.add (WpPropId.property_of_id pid) ds)
D.empty preconds in
let vcs = gmap (cc_from ds hs) wp.vcs in
{ sigma = Some sigma ; effects = Eset.empty ; vcs=vcs })
let is_trivial vc = F.eqp vc.goal F.p_true
let is_empty vc =
is_trivial vc &&
D.is_empty vc.deps &&
S.is_empty vc.path &&
W.is_empty vc.warn
let make_vcqs target tags vc =
let vcq = {
VC_Annot.source = TARGET.source target ;
VC_Annot.axioms = None ;
VC_Annot.goal = GOAL.dummy ;
VC_Annot.tags = tags ;
VC_Annot.deps = vc.deps ;
VC_Annot.path = vc.path ;
VC_Annot.warn = W.elements vc.warn ;
} in
let hyps = Conditions.bundle vc.hyps in
let goal g = { vcq with VC_Annot.goal = GOAL.make (hyps,g) } in
match F.p_expr vc.goal with
| Logic.And gs when Wp_parameters.SplitConj.get () -> Bag.list (List.map goal gs)
| _ -> Bag.elt (goal vc.goal)
let make_trivial vc =
{
VC_Annot.source = None ;
VC_Annot.axioms = None ;
VC_Annot.goal = GOAL.trivial ;
VC_Annot.tags = [] ;
VC_Annot.deps = vc.deps ;
VC_Annot.path = vc.path ;
VC_Annot.warn = W.elements vc.warn ;
}
let make_oblig index pid vcq =
{
po_model = WpContext.get_model () ;
po_pid = pid ;
po_sid = "" ;
po_gid = "" ;
po_name = "" ;
po_idx = index ;
po_formula = vcq ;
}
module PMAP = Map.Make(P)
type group = {
mutable verifs : VC_Annot.t Bag.t ;
mutable trivial : vc ;
}
let group_vc groups target tags vc =
let pid = TARGET.prop_id target in
let group =
try PMAP.find pid !groups
with Not_found ->
let g = { verifs = Bag.empty ; trivial = empty_vc } in
groups := PMAP.add pid g !groups ; g
in
if is_trivial vc
then
group.trivial <- merge_vc group.trivial vc
else
group.verifs <- Bag.concat group.verifs (make_vcqs target tags vc)
let compile_wp index (wp : t_prop) =
let groups = ref PMAP.empty in
let collection = ref Bag.empty in
Gmap.iter_sorted
(fun target -> Splitter.iter (group_vc groups target))
wp.vcs ;
let model = WpContext.get_model () in
PMAP.iter
begin fun pid group ->
let trivial_wpo =
let vcq = make_trivial group.trivial in
Bag.elt (make_oblig index pid vcq)
in
let provers_wpo =
Bag.map (make_oblig index pid) group.verifs
in
let mid = WpContext.MODEL.id model in
let group =
if is_empty group.trivial then
if Bag.is_empty provers_wpo
then trivial_wpo
else provers_wpo
else
Bag.concat trivial_wpo provers_wpo
in
WpPropId.split_bag
begin fun po_pid wpo ->
let po_sid = WpPropId.get_propid po_pid in
let po_gid = Printf.sprintf "%s_%s" mid po_sid in
let po_name = Pretty_utils.to_string WpPropId.pretty_local pid in
let wpo =
{ wpo with po_pid ; po_sid ; po_gid ; po_name }
in
Wpo.add wpo ;
collection := Bag.append !collection wpo ;
end
pid group
end !groups ;
!collection
let register_lemma l = ignore (L.lemma l)
let compile_lemma l =
begin
let id = WpPropId.mk_lemma_id l in
let def = L.lemma l in
let model = WpContext.get_model () in
let sequent = Conditions.lemma ~loc:l.lem_loc def.l_lemma in
let vca = {
Wpo.VC_Annot.axioms = Some (def.l_cluster, l.lem_depends) ;
goal = GOAL.make sequent ;
tags = [] ;
warn = [] ;
deps = Property.Set.empty ;
path = Stmt.Set.empty ;
source = None ;
} in
let index = match LogicUsage.section_of_lemma l.lem_name with
| LogicUsage.Toplevel _ -> Wpo.Axiomatic None
| LogicUsage.Axiomatic a -> Wpo.Axiomatic (Some a.ax_name) in
let mid = WpContext.MODEL.id model in
let sid = WpPropId.get_propid id in
let wpo = {
Wpo.po_model = model ;
Wpo.po_gid = Printf.sprintf "%s_%s" mid sid ;
Wpo.po_sid = sid ;
Wpo.po_name = Printf.sprintf "Lemma '%s'" l.lem_name ;
Wpo.po_idx = index ;
Wpo.po_pid = id ;
Wpo.po_formula = vca ;
} in
Wpo.add wpo ; wpo
end
end
let vcgenerators = WpContext.MINDEX.create 1
let vcgen setup driver : (module VCgen) =
let model = Factory.instance setup driver in
try WpContext.MINDEX.find vcgenerators model
with Not_found ->
let module M = (val Factory.(compiler setup.mheap setup.mvar)) in
let vcgen = (module VC(M) : VCgen) in
WpContext.MINDEX.add vcgenerators model vcgen ;
vcgen