-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathintDomain.ml
3869 lines (3277 loc) · 148 KB
/
intDomain.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
open GobConfig
open GoblintCil
open Pretty
open PrecisionUtil
module M = Messages
module BI = IntOps.BigIntOps
let (%) = Batteries.(%)
let (|?) = Batteries.(|?)
exception IncompatibleIKinds of string
exception Unknown
exception Error
exception ArithmeticOnIntegerBot of string
(** Define records that hold mutable variables representing different Configuration values.
* These values are used to keep track of whether or not the corresponding Config values are en-/disabled *)
type ana_int_config_values = {
mutable interval_threshold_widening : bool option;
mutable interval_narrow_by_meet : bool option;
mutable def_exc_widen_by_join : bool option;
mutable interval_threshold_widening_constants : string option;
mutable refinement : string option;
}
let ana_int_config: ana_int_config_values = {
interval_threshold_widening = None;
interval_narrow_by_meet = None;
def_exc_widen_by_join = None;
interval_threshold_widening_constants = None;
refinement = None;
}
let get_interval_threshold_widening () =
if ana_int_config.interval_threshold_widening = None then
ana_int_config.interval_threshold_widening <- Some (get_bool "ana.int.interval_threshold_widening");
Option.get ana_int_config.interval_threshold_widening
let get_interval_narrow_by_meet () =
if ana_int_config.interval_narrow_by_meet = None then
ana_int_config.interval_narrow_by_meet <- Some (get_bool "ana.int.interval_narrow_by_meet");
Option.get ana_int_config.interval_narrow_by_meet
let get_def_exc_widen_by_join () =
if ana_int_config.def_exc_widen_by_join = None then
ana_int_config.def_exc_widen_by_join <- Some (get_bool "ana.int.def_exc_widen_by_join");
Option.get ana_int_config.def_exc_widen_by_join
let get_interval_threshold_widening_constants () =
if ana_int_config.interval_threshold_widening_constants = None then
ana_int_config.interval_threshold_widening_constants <- Some (get_string "ana.int.interval_threshold_widening_constants");
Option.get ana_int_config.interval_threshold_widening_constants
let get_refinement () =
if ana_int_config.refinement = None then
ana_int_config.refinement <- Some (get_string "ana.int.refinement");
Option.get ana_int_config.refinement
(** Whether for a given ikind, we should compute with wrap-around arithmetic.
* Always for unsigned types, for signed types if 'sem.int.signed_overflow' is 'assume_wraparound' *)
let should_wrap ik = not (Cil.isSigned ik) || get_string "sem.int.signed_overflow" = "assume_wraparound"
(** Whether for a given ikind, we should assume there are no overflows.
* Always false for unsigned types, true for signed types if 'sem.int.signed_overflow' is 'assume_none' *)
let should_ignore_overflow ik = Cil.isSigned ik && get_string "sem.int.signed_overflow" = "assume_none"
let widening_thresholds = ResettableLazy.from_fun WideningThresholds.thresholds
let widening_thresholds_desc = ResettableLazy.from_fun (List.rev % WideningThresholds.thresholds)
type overflow_info = { overflow: bool; underflow: bool;}
let set_overflow_flag ~cast ~underflow ~overflow ik =
let signed = Cil.isSigned ik in
if !AnalysisState.postsolving && signed && not cast then
AnalysisState.svcomp_may_overflow := true;
let sign = if signed then "Signed" else "Unsigned" in
match underflow, overflow with
| true, true ->
M.warn ~category:M.Category.Integer.overflow ~tags:[CWE 190; CWE 191] "%s integer overflow and underflow" sign
| true, false ->
M.warn ~category:M.Category.Integer.overflow ~tags:[CWE 191] "%s integer underflow" sign
| false, true ->
M.warn ~category:M.Category.Integer.overflow ~tags:[CWE 190] "%s integer overflow" sign
| false, false -> assert false
let reset_lazy () =
ResettableLazy.reset widening_thresholds;
ResettableLazy.reset widening_thresholds_desc;
ana_int_config.interval_threshold_widening <- None;
ana_int_config.interval_narrow_by_meet <- None;
ana_int_config.def_exc_widen_by_join <- None;
ana_int_config.interval_threshold_widening_constants <- None;
ana_int_config.refinement <- None
module type Arith =
sig
type t
val neg: t -> t
val add: t -> t -> t
val sub: t -> t -> t
val mul: t -> t -> t
val div: t -> t -> t
val rem: t -> t -> t
val lt: t -> t -> t
val gt: t -> t -> t
val le: t -> t -> t
val ge: t -> t -> t
val eq: t -> t -> t
val ne: t -> t -> t
val bitnot: t -> t
val bitand: t -> t -> t
val bitor : t -> t -> t
val bitxor: t -> t -> t
val shift_left : t -> t -> t
val shift_right: t -> t -> t
val lognot: t -> t
val logand: t -> t -> t
val logor : t -> t -> t
end
module type ArithIkind =
sig
type t
val neg: Cil.ikind -> t -> t
val add: Cil.ikind -> t -> t -> t
val sub: Cil.ikind -> t -> t -> t
val mul: Cil.ikind -> t -> t -> t
val div: Cil.ikind -> t -> t -> t
val rem: Cil.ikind -> t -> t -> t
val lt: Cil.ikind -> t -> t -> t
val gt: Cil.ikind -> t -> t -> t
val le: Cil.ikind -> t -> t -> t
val ge: Cil.ikind -> t -> t -> t
val eq: Cil.ikind -> t -> t -> t
val ne: Cil.ikind -> t -> t -> t
val bitnot: Cil.ikind -> t -> t
val bitand: Cil.ikind -> t -> t -> t
val bitor : Cil.ikind -> t -> t -> t
val bitxor: Cil.ikind -> t -> t -> t
val shift_left : Cil.ikind -> t -> t -> t
val shift_right: Cil.ikind -> t -> t -> t
val lognot: Cil.ikind -> t -> t
val logand: Cil.ikind -> t -> t -> t
val logor : Cil.ikind -> t -> t -> t
end
(* Shared functions between S and Z *)
module type B =
sig
include Lattice.S
type int_t
val bot_of: Cil.ikind -> t
val top_of: Cil.ikind -> t
val to_int: t -> int_t option
val equal_to: int_t -> t -> [`Eq | `Neq | `Top]
val to_bool: t -> bool option
val to_excl_list: t -> (int_t list * (int64 * int64)) option
val of_excl_list: Cil.ikind -> int_t list -> t
val is_excl_list: t -> bool
val to_incl_list: t -> int_t list option
val maximal : t -> int_t option
val minimal : t -> int_t option
val cast_to: ?torg:Cil.typ -> Cil.ikind -> t -> t
end
(** Interface of IntDomain implementations that do not take ikinds for arithmetic operations yet. TODO: Should be ported to S in the future. *)
module type IkindUnawareS =
sig
include B
include Arith with type t := t
val starting : ?suppress_ovwarn:bool -> Cil.ikind -> int_t -> t
val ending : ?suppress_ovwarn:bool -> Cil.ikind -> int_t -> t
val of_int: int_t -> t
val of_bool: bool -> t
val of_interval: ?suppress_ovwarn:bool -> Cil.ikind -> int_t * int_t -> t
val of_congruence: Cil.ikind -> int_t * int_t -> t
val arbitrary: unit -> t QCheck.arbitrary
val invariant: Cil.exp -> t -> Invariant.t
end
(** Interface of IntDomain implementations taking an ikind for arithmetic operations *)
module type S =
sig
include B
include ArithIkind with type t:= t
val add : ?no_ov:bool -> Cil.ikind -> t -> t -> t
val sub : ?no_ov:bool -> Cil.ikind -> t -> t -> t
val mul : ?no_ov:bool -> Cil.ikind -> t -> t -> t
val div : ?no_ov:bool -> Cil.ikind -> t -> t -> t
val neg : ?no_ov:bool -> Cil.ikind -> t -> t
val cast_to : ?torg:Cil.typ -> ?no_ov:bool -> Cil.ikind -> t -> t
val join: Cil.ikind -> t -> t -> t
val meet: Cil.ikind -> t -> t -> t
val narrow: Cil.ikind -> t -> t -> t
val widen: Cil.ikind -> t -> t -> t
val starting : ?suppress_ovwarn:bool -> Cil.ikind -> int_t -> t
val ending : ?suppress_ovwarn:bool -> Cil.ikind -> int_t -> t
val of_int: Cil.ikind -> int_t -> t
val of_bool: Cil.ikind -> bool -> t
val of_interval: ?suppress_ovwarn:bool -> Cil.ikind -> int_t * int_t -> t
val of_congruence: Cil.ikind -> int_t * int_t -> t
val is_top_of: Cil.ikind -> t -> bool
val invariant_ikind : Cil.exp -> Cil.ikind -> t -> Invariant.t
val refine_with_congruence: Cil.ikind -> t -> (int_t * int_t) option -> t
val refine_with_interval: Cil.ikind -> t -> (int_t * int_t) option -> t
val refine_with_excl_list: Cil.ikind -> t -> (int_t list * (int64 * int64)) option -> t
val refine_with_incl_list: Cil.ikind -> t -> int_t list option -> t
val project: Cil.ikind -> int_precision -> t -> t
val arbitrary: Cil.ikind -> t QCheck.arbitrary
end
module type SOverflow =
sig
include S
val add : ?no_ov:bool -> Cil.ikind -> t -> t -> t * overflow_info
val sub : ?no_ov:bool -> Cil.ikind -> t -> t -> t * overflow_info
val mul : ?no_ov:bool -> Cil.ikind -> t -> t -> t * overflow_info
val div : ?no_ov:bool -> Cil.ikind -> t -> t -> t * overflow_info
val neg : ?no_ov:bool -> Cil.ikind -> t -> t * overflow_info
val cast_to : ?torg:Cil.typ -> ?no_ov:bool -> Cil.ikind -> t -> t * overflow_info
val of_int : Cil.ikind -> int_t -> t * overflow_info
val of_interval: ?suppress_ovwarn:bool -> Cil.ikind -> int_t * int_t -> t * overflow_info
val starting : ?suppress_ovwarn:bool -> Cil.ikind -> int_t -> t * overflow_info
val ending : ?suppress_ovwarn:bool -> Cil.ikind -> int_t -> t * overflow_info
val shift_left : Cil.ikind -> t -> t -> t * overflow_info
val shift_right : Cil.ikind -> t -> t -> t * overflow_info
end
module type Y =
sig
(* include B *)
include B
include Arith with type t:= t
val of_int: Cil.ikind -> int_t -> t
val of_bool: Cil.ikind -> bool -> t
val of_interval: ?suppress_ovwarn:bool -> Cil.ikind -> int_t * int_t -> t
val of_congruence: Cil.ikind -> int_t * int_t -> t
val starting : ?suppress_ovwarn:bool -> Cil.ikind -> int_t -> t
val ending : ?suppress_ovwarn:bool -> Cil.ikind -> int_t -> t
val is_top_of: Cil.ikind -> t -> bool
val project: int_precision -> t -> t
val invariant: Cil.exp -> t -> Invariant.t
end
module type Z = Y with type int_t = BI.t
module OldDomainFacade (Old : IkindUnawareS with type int_t = int64) : S with type int_t = BI.t and type t = Old.t =
struct
include Old
type int_t = BI.t
let neg ?no_ov _ik = Old.neg
let add ?no_ov _ik = Old.add
let sub ?no_ov _ik = Old.sub
let mul ?no_ov _ik = Old.mul
let div ?no_ov _ik = Old.div
let rem _ik = Old.rem
let lt _ik = Old.lt
let gt _ik = Old.gt
let le _ik = Old.le
let ge _ik = Old.ge
let eq _ik = Old.eq
let ne _ik = Old.ne
let bitnot _ik = bitnot
let bitand _ik = bitand
let bitor _ik = bitor
let bitxor _ik = bitxor
let shift_left _ik = shift_left
let shift_right _ik = shift_right
let lognot _ik = lognot
let logand _ik = logand
let logor _ik = logor
let to_int a = Option.map BI.of_int64 (Old.to_int a)
let equal_to (x: int_t) (a: t)=
try
Old.equal_to (BI.to_int64 x) a
with Z.Overflow | Failure _ -> `Top
let to_excl_list a = Option.map (BatTuple.Tuple2.map1 (List.map BI.of_int64)) (Old.to_excl_list a)
let of_excl_list ik xs =
let xs' = List.map BI.to_int64 xs in
Old.of_excl_list ik xs'
let to_incl_list a = Option.map (List.map BI.of_int64) (Old.to_incl_list a)
let maximal a = Option.map BI.of_int64 (Old.maximal a)
let minimal a = Option.map BI.of_int64 (Old.minimal a)
let of_int ik x =
(* If we cannot convert x to int64, we have to represent it with top in the underlying domain*)
try
Old.of_int (BI.to_int64 x)
with
Failure _ -> top_of ik
let of_bool ik b = Old.of_bool b
let of_interval ?(suppress_ovwarn=false) ik (l, u) =
try
Old.of_interval ~suppress_ovwarn ik (BI.to_int64 l, BI.to_int64 u)
with
Failure _ -> top_of ik
let of_congruence ik (c, m) =
try
Old.of_congruence ik (BI.to_int64 c, BI.to_int64 m)
with
Failure _ -> top_of ik
let starting ?(suppress_ovwarn=false) ik x =
try Old.starting ~suppress_ovwarn ik (BI.to_int64 x) with Failure _ -> top_of ik
let ending ?(suppress_ovwarn=false) ik x =
try Old.ending ~suppress_ovwarn ik (BI.to_int64 x) with Failure _ -> top_of ik
let join _ik = Old.join
let meet _ik = Old.meet
let narrow _ik = Old.narrow
let widen _ik = Old.widen
let is_top_of _ik = Old.is_top
let invariant_ikind e ik t = Old.invariant e t
let cast_to ?torg ?no_ov = Old.cast_to ?torg
let refine_with_congruence ik a b = a
let refine_with_interval ik a b = a
let refine_with_excl_list ik a b = a
let refine_with_incl_list ik a b = a
let project ik p t = t
let arbitrary _ik = Old.arbitrary ()
end
module IntDomLifter (I : S) =
struct
open Cil
type int_t = I.int_t
type t = { v : I.t; ikind : CilType.Ikind.t } [@@deriving eq, ord, hash]
let ikind {ikind; _} = ikind
(* Helper functions *)
let check_ikinds x y = if x.ikind <> y.ikind then raise (IncompatibleIKinds (GobPretty.sprintf "ikinds %a and %a are incompatible. Values: %a and %a" CilType.Ikind.pretty x.ikind CilType.Ikind.pretty y.ikind I.pretty x.v I.pretty y.v))
let lift op x = {x with v = op x.ikind x.v }
(* For logical operations the result is of type int *)
let lift_logical op x = {v = op x.ikind x.v; ikind = Cil.IInt}
let lift2 op x y = check_ikinds x y; {x with v = op x.ikind x.v y.v }
let lift2_cmp op x y = check_ikinds x y; {v = op x.ikind x.v y.v; ikind = Cil.IInt}
let bot_of ikind = { v = I.bot_of ikind; ikind}
let bot () = failwith "bot () is not implemented for IntDomLifter."
let is_bot x = I.is_bot x.v
let top_of ikind = { v = I.top_of ikind; ikind}
let top () = failwith "top () is not implemented for IntDomLifter."
let is_top x = I.is_top x.v
(* Leq does not check for ikind, because it is used in invariant with arguments of different type.
TODO: check ikinds here and fix invariant to work with right ikinds *)
let leq x y = I.leq x.v y.v
let join = lift2 I.join
let meet = lift2 I.meet
let widen = lift2 I.widen
let narrow = lift2 I.narrow
let show x =
if I.is_top_of x.ikind x.v then
"⊤"
else
I.show x.v (* TODO add ikind to output *)
let pretty () x =
if I.is_top_of x.ikind x.v then
Pretty.text "⊤"
else
I.pretty () x.v (* TODO add ikind to output *)
let pretty_diff () (x, y) = I.pretty_diff () (x.v, y.v) (* TODO check ikinds, add them to output *)
let printXml o x = I.printXml o x.v (* TODO add ikind to output *)
(* This is for debugging *)
let name () = "IntDomLifter(" ^ (I.name ()) ^ ")"
let to_yojson x = I.to_yojson x.v
let invariant e x =
let e' = Cilfacade.mkCast ~e ~newt:(TInt (x.ikind, [])) in
I.invariant_ikind e' x.ikind x.v
let tag x = I.tag x.v
let arbitrary ik = failwith @@ "Arbitrary not implement for " ^ (name ()) ^ "."
let to_int x = I.to_int x.v
let of_int ikind x = { v = I.of_int ikind x; ikind}
let equal_to i x = I.equal_to i x.v
let to_bool x = I.to_bool x.v
let of_bool ikind b = { v = I.of_bool ikind b; ikind}
let to_excl_list x = I.to_excl_list x.v
let of_excl_list ikind is = {v = I.of_excl_list ikind is; ikind}
let is_excl_list x = I.is_excl_list x.v
let to_incl_list x = I.to_incl_list x.v
let of_interval ?(suppress_ovwarn=false) ikind (lb,ub) = {v = I.of_interval ~suppress_ovwarn ikind (lb,ub); ikind}
let of_congruence ikind (c,m) = {v = I.of_congruence ikind (c,m); ikind}
let starting ?(suppress_ovwarn=false) ikind i = {v = I.starting ~suppress_ovwarn ikind i; ikind}
let ending ?(suppress_ovwarn=false) ikind i = {v = I.ending ~suppress_ovwarn ikind i; ikind}
let maximal x = I.maximal x.v
let minimal x = I.minimal x.v
let neg = lift I.neg
let add = lift2 I.add
let sub = lift2 I.sub
let mul = lift2 I.mul
let div = lift2 I.div
let rem = lift2 I.rem
let lt = lift2_cmp I.lt
let gt = lift2_cmp I.gt
let le = lift2_cmp I.le
let ge = lift2_cmp I.ge
let eq = lift2_cmp I.eq
let ne = lift2_cmp I.ne
let bitnot = lift I.bitnot
let bitand = lift2 I.bitand
let bitor = lift2 I.bitor
let bitxor = lift2 I.bitxor
let shift_left x y = {x with v = I.shift_left x.ikind x.v y.v } (* TODO check ikinds*)
let shift_right x y = {x with v = I.shift_right x.ikind x.v y.v } (* TODO check ikinds*)
let lognot = lift_logical I.lognot
let logand = lift2 I.logand
let logor = lift2 I.logor
let cast_to ?torg ikind x = {v = I.cast_to ~torg:(TInt(x.ikind,[])) ikind x.v; ikind}
let is_top_of ik x = ik = x.ikind && I.is_top_of ik x.v
let relift x = { v = I.relift x.v; ikind = x.ikind }
let project p v = { v = I.project v.ikind p v.v; ikind = v.ikind }
end
module type Ikind =
sig
val ikind: unit -> Cil.ikind
end
module PtrDiffIkind : Ikind =
struct
let ikind = Cilfacade.ptrdiff_ikind
end
module IntDomWithDefaultIkind (I: Y) (Ik: Ikind) : Y with type t = I.t and type int_t = I.int_t =
struct
include I
let top () = I.top_of (Ik.ikind ())
let bot () = I.bot_of (Ik.ikind ())
end
module Size = struct (* size in bits as int, range as int64 *)
open Cil
let sign x = if BI.compare x BI.zero < 0 then `Signed else `Unsigned
let top_typ = TInt (ILongLong, [])
let min_for x = intKindForValue x (sign x = `Unsigned)
let bit = function (* bits needed for representation *)
| IBool -> 1
| ik -> bytesSizeOfInt ik * 8
let is_int64_big_int x = Z.fits_int64 x
let card ik = (* cardinality *)
let b = bit ik in
Z.shift_left Z.one b
let bits ik = (* highest bits for neg/pos values *)
let s = bit ik in
if isSigned ik then s-1, s-1 else 0, s
let bits_i64 ik = BatTuple.Tuple2.mapn Int64.of_int (bits ik)
let range ik =
let a,b = bits ik in
let x = if isSigned ik then Z.neg (Z.shift_left Z.one a) (* -2^a *) else Z.zero in
let y = Z.pred (Z.shift_left Z.one b) in (* 2^b - 1 *)
x,y
let is_cast_injective ~from_type ~to_type =
let (from_min, from_max) = range (Cilfacade.get_ikind from_type) in
let (to_min, to_max) = range (Cilfacade.get_ikind to_type) in
if M.tracing then M.trace "int" "is_cast_injective %a (%s, %s) -> %a (%s, %s)\n" CilType.Typ.pretty from_type (BI.to_string from_min) (BI.to_string from_max) CilType.Typ.pretty to_type (BI.to_string to_min) (BI.to_string to_max);
BI.compare to_min from_min <= 0 && BI.compare from_max to_max <= 0
let cast t x = (* TODO: overflow is implementation-dependent! *)
if t = IBool then
(* C11 6.3.1.2 Boolean type *)
if Z.equal x Z.zero then Z.zero else Z.one
else
let a,b = range t in
let c = card t in
let y = Z.erem x c in
let y = if Z.gt y b then Z.sub y c
else if Z.lt y a then Z.add y c
else y
in
if M.tracing then M.tracel "cast" "Cast %s to range [%s, %s] (%s) = %s (%s in int64)\n" (Z.to_string x) (Z.to_string a) (Z.to_string b) (Z.to_string c) (Z.to_string y) (if is_int64_big_int y then "fits" else "does not fit");
y
let min_range_sign_agnostic x =
let size ik =
let a,b = bits_i64 ik in
Int64.neg a,b
in
if sign x = `Signed then
size (min_for x)
else
let a, b = size (min_for x) in
if b <= 64L then
let upper_bound_less = Int64.sub b 1L in
let max_one_less = BI.(pred @@ shift_left BI.one (Int64.to_int upper_bound_less)) in
if x <= max_one_less then
a, upper_bound_less
else
a,b
else
a, b
(* From the number of bits used to represent a positive value, determines the maximal representable value *)
let max_from_bit_range pos_bits = BI.(pred @@ shift_left BI.one (to_int (BI.of_int64 pos_bits)))
(* From the number of bits used to represent a non-positive value, determines the minimal representable value *)
let min_from_bit_range neg_bits = BI.(if neg_bits = 0L then BI.zero else neg @@ shift_left BI.one (to_int (neg (BI.of_int64 neg_bits))))
end
module StdTop (B: sig type t val top_of: Cil.ikind -> t end) = struct
open B
(* these should be overwritten for better precision if possible: *)
let to_excl_list x = None
let of_excl_list ik x = top_of ik
let is_excl_list x = false
let to_incl_list x = None
let of_interval ?(suppress_ovwarn=false) ik x = top_of ik
let of_congruence ik x = top_of ik
let starting ?(suppress_ovwarn=false) ik x = top_of ik
let ending ?(suppress_ovwarn=false) ik x = top_of ik
let maximal x = None
let minimal x = None
end
module Std (B: sig
type t
val name: unit -> string
val top_of: Cil.ikind -> t
val bot_of: Cil.ikind -> t
val show: t -> string
val equal: t -> t -> bool
end) = struct
include Printable.StdLeaf
let name = B.name (* overwrite the one from Printable.Std *)
open B
let is_top x = failwith "is_top not implemented for IntDomain.Std"
let is_bot x = B.equal x (bot_of Cil.IInt) (* Here we assume that the representation of bottom is independent of the ikind
This may be true for intdomain implementations, but not e.g. for IntDomLifter. *)
let is_top_of ik x = B.equal x (top_of ik)
(* all output is based on B.show *)
include Printable.SimpleShow (
struct
type nonrec t = t
let show = show
end
)
let pretty_diff () (x,y) = dprintf "%s: %a instead of %a" (name ()) pretty x pretty y
include StdTop (B)
end
(* Textbook interval arithmetic, without any overflow handling etc. *)
module IntervalArith(Ints_t : IntOps.IntOps) = struct
let min4 a b c d = Ints_t.min (Ints_t.min a b) (Ints_t.min c d)
let max4 a b c d = Ints_t.max (Ints_t.max a b) (Ints_t.max c d)
let mul (x1, x2) (y1, y2) =
let x1y1 = (Ints_t.mul x1 y1) in
let x1y2 = (Ints_t.mul x1 y2) in
let x2y1 = (Ints_t.mul x2 y1) in
let x2y2 = (Ints_t.mul x2 y2) in
(min4 x1y1 x1y2 x2y1 x2y2, max4 x1y1 x1y2 x2y1 x2y2)
let shift_left (x1,x2) (y1,y2) =
let y1p = Ints_t.shift_left Ints_t.one y1 in
let y2p = Ints_t.shift_left Ints_t.one y2 in
mul (x1, x2) (y1p, y2p)
let div (x1, x2) (y1, y2) =
let x1y1n = (Ints_t.div x1 y1) in
let x1y2n = (Ints_t.div x1 y2) in
let x2y1n = (Ints_t.div x2 y1) in
let x2y2n = (Ints_t.div x2 y2) in
let x1y1p = (Ints_t.div x1 y1) in
let x1y2p = (Ints_t.div x1 y2) in
let x2y1p = (Ints_t.div x2 y1) in
let x2y2p = (Ints_t.div x2 y2) in
(min4 x1y1n x1y2n x2y1n x2y2n, max4 x1y1p x1y2p x2y1p x2y2p)
let add (x1, x2) (y1, y2) = (Ints_t.add x1 y1, Ints_t.add x2 y2)
let sub (x1, x2) (y1, y2) = (Ints_t.sub x1 y2, Ints_t.sub x2 y1)
let neg (x1, x2) = (Ints_t.neg x2, Ints_t.neg x1)
let one = (Ints_t.one, Ints_t.one)
let zero = (Ints_t.zero, Ints_t.zero)
let top_bool = (Ints_t.zero, Ints_t.one)
let to_int (x1, x2) =
if Ints_t.equal x1 x2 then Some x1 else None
end
module IntervalFunctor(Ints_t : IntOps.IntOps): SOverflow with type int_t = Ints_t.t and type t = (Ints_t.t * Ints_t.t) option =
struct
let name () = "intervals"
type int_t = Ints_t.t
type t = (Ints_t.t * Ints_t.t) option [@@deriving eq, ord, hash]
module IArith = IntervalArith(Ints_t)
let range ik = BatTuple.Tuple2.mapn Ints_t.of_bigint (Size.range ik)
let top () = failwith @@ "top () not implemented for " ^ (name ())
let top_of ik = Some (range ik)
let bot () = None
let bot_of ik = bot () (* TODO: improve *)
let show = function None -> "bottom" | Some (x,y) -> "["^Ints_t.to_string x^","^Ints_t.to_string y^"]"
include Std (struct type nonrec t = t let name = name let top_of = top_of let bot_of = bot_of let show = show let equal = equal end)
let equal_to i = function
| None -> failwith "unsupported: equal_to with bottom"
| Some (a, b) ->
if a = b && b = i then `Eq else if Ints_t.compare a i <= 0 && Ints_t.compare i b <=0 then `Top else `Neq
let norm ?(suppress_ovwarn=false) ?(cast=false) ik : (t -> t * overflow_info) = function None -> (None, {underflow=false; overflow=false}) | Some (x,y) ->
if Ints_t.compare x y > 0 then
(None,{underflow=false; overflow=false})
else (
let (min_ik, max_ik) = range ik in
let underflow = Ints_t.compare min_ik x > 0 in
let overflow = Ints_t.compare max_ik y < 0 in
let ov_info = { underflow = underflow && not suppress_ovwarn; overflow = overflow && not suppress_ovwarn } in
let v =
if underflow || overflow then
if should_wrap ik then (* could add [|| cast], but that's GCC implementation-defined behavior: https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html#Integers-implementation *)
(* We can only soundly wrap if at most one overflow occurred, otherwise the minimal and maximal values of the interval *)
(* on Z will not safely contain the minimal and maximal elements after the cast *)
let diff = Ints_t.abs (Ints_t.sub max_ik min_ik) in
let resdiff = Ints_t.abs (Ints_t.sub y x) in
if Ints_t.compare resdiff diff > 0 then
top_of ik
else
let l = Ints_t.of_bigint @@ Size.cast ik (Ints_t.to_bigint x) in
let u = Ints_t.of_bigint @@ Size.cast ik (Ints_t.to_bigint y) in
if Ints_t.compare l u <= 0 then
Some (l, u)
else
(* Interval that wraps around (begins to the right of its end). We can not represent such intervals *)
top_of ik
else if not cast && should_ignore_overflow ik then
let tl, tu = BatOption.get @@ top_of ik in
Some (Ints_t.max tl x, Ints_t.min tu y)
else
top_of ik
else
Some (x,y)
in
(v, ov_info)
)
let leq (x:t) (y:t) =
match x, y with
| None, _ -> true
| Some _, None -> false
| Some (x1,x2), Some (y1,y2) -> Ints_t.compare x1 y1 >= 0 && Ints_t.compare x2 y2 <= 0
let join ik (x:t) y =
match x, y with
| None, z | z, None -> z
| Some (x1,x2), Some (y1,y2) -> norm ik @@ Some (Ints_t.min x1 y1, Ints_t.max x2 y2) |> fst
let meet ik (x:t) y =
match x, y with
| None, z | z, None -> None
| Some (x1,x2), Some (y1,y2) -> norm ik @@ Some (Ints_t.max x1 y1, Ints_t.min x2 y2) |> fst
(* TODO: change to_int signature so it returns a big_int *)
let to_int x = Option.bind x (IArith.to_int)
let of_interval ?(suppress_ovwarn=false) ik (x,y) = norm ~suppress_ovwarn ik @@ Some (x,y)
let of_int ik (x: int_t) = of_interval ik (x,x)
let zero = Some IArith.zero
let one = Some IArith.one
let top_bool = Some IArith.top_bool
let of_bool _ik = function true -> one | false -> zero
let to_bool (a: t) = match a with
| None -> None
| Some (l, u) when Ints_t.compare l Ints_t.zero = 0 && Ints_t.compare u Ints_t.zero = 0 -> Some false
| x -> if leq zero x then None else Some true
let starting ?(suppress_ovwarn=false) ik n =
norm ~suppress_ovwarn ik @@ Some (n, snd (range ik))
let ending ?(suppress_ovwarn=false) ik n =
norm ~suppress_ovwarn ik @@ Some (fst (range ik), n)
(* TODO: change signature of maximal, minimal to return big_int*)
let maximal = function None -> None | Some (x,y) -> Some y
let minimal = function None -> None | Some (x,y) -> Some x
let cast_to ?torg ?no_ov t = norm ~cast:true t (* norm does all overflow handling *)
let widen ik x y =
match x, y with
| None, z | z, None -> z
| Some (l0,u0), Some (l1,u1) ->
let (min_ik, max_ik) = range ik in
let threshold = get_interval_threshold_widening () in
let upper_threshold u =
let ts = if get_interval_threshold_widening_constants () = "comparisons" then WideningThresholds.upper_thresholds () else ResettableLazy.force widening_thresholds in
let u = Ints_t.to_bigint u in
let t = List.find_opt (fun x -> Z.compare u x <= 0) ts in
BatOption.map_default Ints_t.of_bigint max_ik t
in
let lower_threshold l =
let ts = if get_interval_threshold_widening_constants () = "comparisons" then WideningThresholds.lower_thresholds () else ResettableLazy.force widening_thresholds_desc in
let l = Ints_t.to_bigint l in
let t = List.find_opt (fun x -> Z.compare l x >= 0) ts in
BatOption.map_default Ints_t.of_bigint min_ik t
in
let lt = if threshold then lower_threshold l1 else min_ik in
let l2 = if Ints_t.compare l0 l1 = 0 then l0 else Ints_t.min l1 (Ints_t.max lt min_ik) in
let ut = if threshold then upper_threshold u1 else max_ik in
let u2 = if Ints_t.compare u0 u1 = 0 then u0 else Ints_t.max u1 (Ints_t.min ut max_ik) in
norm ik @@ Some (l2,u2) |> fst
let widen ik x y =
let r = widen ik x y in
if M.tracing && not (equal x y) then M.tracel "int" "interval widen %a %a -> %a\n" pretty x pretty y pretty r;
assert (leq x y); (* TODO: remove for performance reasons? *)
r
let narrow ik x y =
match x, y with
| _,None | None, _ -> None
| Some (x1,x2), Some (y1,y2) ->
let (min_ik, max_ik) = range ik in
let lr = if Ints_t.compare min_ik x1 = 0 then y1 else x1 in
let ur = if Ints_t.compare max_ik x2 = 0 then y2 else x2 in
norm ik @@ Some (lr,ur) |> fst
let narrow ik x y =
if get_interval_narrow_by_meet () then
meet ik x y
else
narrow ik x y
let log f ~annihilator ik i1 i2 =
match is_bot i1, is_bot i2 with
| true, true -> bot_of ik
| true, _
| _ , true -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show i1) (show i2)))
| _ ->
match to_bool i1, to_bool i2 with
| Some x, _ when x = annihilator -> of_bool ik annihilator
| _, Some y when y = annihilator -> of_bool ik annihilator
| Some x, Some y -> of_bool ik (f x y)
| _ -> top_of ik
let logor = log (||) ~annihilator:true
let logand = log (&&) ~annihilator:false
let log1 f ik i1 =
if is_bot i1 then
bot_of ik
else
match to_bool i1 with
| Some x -> of_bool ik (f ik x)
| _ -> top_of ik
let lognot = log1 (fun _ik -> not)
let bit f ik i1 i2 =
match is_bot i1, is_bot i2 with
| true, true -> bot_of ik
| true, _
| _ , true -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show i1) (show i2)))
| _ ->
match to_int i1, to_int i2 with
| Some x, Some y -> (try of_int ik (f ik x y) |> fst with Division_by_zero -> top_of ik)
| _ -> top_of ik
let bitcomp f ik i1 i2 =
match is_bot i1, is_bot i2 with
| true, true -> (bot_of ik,{underflow=false; overflow=false})
| true, _
| _ , true -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show i1) (show i2)))
| _ ->
match to_int i1, to_int i2 with
| Some x, Some y -> (try of_int ik (f ik x y) with Division_by_zero | Invalid_argument _ -> (top_of ik,{underflow=false; overflow=false}))
| _ -> (top_of ik,{underflow=true; overflow=true})
let bitxor = bit (fun _ik -> Ints_t.bitxor)
let bitand ik i1 i2 =
match is_bot i1, is_bot i2 with
| true, true -> bot_of ik
| true, _
| _ , true -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show i1) (show i2)))
| _ ->
match to_int i1, to_int i2 with
| Some x, Some y -> (try of_int ik (Ints_t.bitand x y) |> fst with Division_by_zero -> top_of ik)
| _, Some y when Ints_t.equal y Ints_t.zero -> of_int ik Ints_t.zero |> fst
| _, Some y when Ints_t.equal y Ints_t.one -> of_interval ik (Ints_t.zero, Ints_t.one) |> fst
| _ -> top_of ik
let bitor = bit (fun _ik -> Ints_t.bitor)
let bit1 f ik i1 =
if is_bot i1 then
bot_of ik
else
match to_int i1 with
| Some x -> of_int ik (f ik x) |> fst
| _ -> top_of ik
let bitnot = bit1 (fun _ik -> Ints_t.bitnot)
let shift_right = bitcomp (fun _ik x y -> Ints_t.shift_right x (Ints_t.to_int y))
let neg ?no_ov ik = function None -> (None,{underflow=false; overflow=false}) | Some x -> norm ik @@ Some (IArith.neg x)
let binary_op_with_norm ?no_ov op ik x y = match x, y with
| None, None -> (None, {overflow=false; underflow= false})
| None, _ | _, None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
| Some x, Some y -> norm ik @@ Some (op x y)
let add ?no_ov = binary_op_with_norm IArith.add
let mul ?no_ov = binary_op_with_norm IArith.mul
let sub ?no_ov = binary_op_with_norm IArith.sub
let shift_left ik a b =
match is_bot a, is_bot b with
| true, true -> (bot_of ik,{underflow=false; overflow=false})
| true, _
| _ , true -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show a) (show b)))
| _ ->
match a, minimal b, maximal b with
| Some a, Some bl, Some bu when (Ints_t.compare bl Ints_t.zero >= 0) ->
(try
let r = IArith.shift_left a (Ints_t.to_int bl, Ints_t.to_int bu) in
norm ik @@ Some r
with Z.Overflow -> (top_of ik,{underflow=false; overflow=true}))
| _ -> (top_of ik,{underflow=true; overflow=true})
let rem ik x y = match x, y with
| None, None -> None
| None, _ | _, None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
| Some (xl, xu), Some (yl, yu) ->
if is_top_of ik x && is_top_of ik y then
(* This is needed to preserve soundness also on things bigger than int32 e.g. *)
(* x: 3803957176L -> T in Interval32 *)
(* y: 4209861404L -> T in Interval32 *)
(* x % y: 3803957176L -> T in Interval32 *)
(* T in Interval32 is [-2147483648,2147483647] *)
(* the code below computes [-2147483647,2147483647] for this though which is unsound *)
top_of ik
else
(* If we have definite values, Ints_t.rem will give a definite result.
* Otherwise we meet with a [range] the result can be in.
* This range is [0, min xu b] if x is positive, and [max xl -b, min xu b] if x can be negative.
* The precise bound b is one smaller than the maximum bound. Negative y give the same result as positive. *)
let pos x = if Ints_t.compare x Ints_t.zero < 0 then Ints_t.neg x else x in
let b = Ints_t.sub (Ints_t.max (pos yl) (pos yu)) Ints_t.one in
let range = if Ints_t.compare xl Ints_t.zero>= 0 then Some (Ints_t.zero, Ints_t.min xu b) else Some (Ints_t.max xl (Ints_t.neg b), Ints_t.min (Ints_t.max (pos xl) (pos xu)) b) in
meet ik (bit (fun _ik -> Ints_t.rem) ik x y) range
let rec div ?no_ov ik x y =
match x, y with
| None, None -> (bot (),{underflow=false; overflow=false})
| None, _ | _, None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
| (Some (x1,x2) as x), (Some (y1,y2) as y) ->
begin
let is_zero v = Ints_t.compare v Ints_t.zero = 0 in
match y1, y2 with
| l, u when is_zero l && is_zero u -> (top_of ik,{underflow=false; overflow=false}) (* TODO warn about undefined behavior *)
| l, _ when is_zero l -> div ik (Some (x1,x2)) (Some (Ints_t.one,y2))
| _, u when is_zero u -> div ik (Some (x1,x2)) (Some (y1, Ints_t.(neg one)))
| _ when leq (of_int ik (Ints_t.zero) |> fst) (Some (y1,y2)) -> (top_of ik,{underflow=false; overflow=false})
| _ -> binary_op_with_norm IArith.div ik x y
end
let ne ik x y =
match x, y with
| None, None -> bot_of ik
| None, _ | _, None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
| Some (x1,x2), Some (y1,y2) ->
if Ints_t.compare y2 x1 < 0 || Ints_t.compare x2 y1 < 0 then
of_bool ik true
else if Ints_t.compare x2 y1 <= 0 && Ints_t.compare y2 x1 <= 0 then
of_bool ik false
else top_bool
let eq ik x y =
match x, y with
| None, None -> bot_of ik
| None, _ | _, None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
| Some (x1,x2), Some (y1,y2) ->
if Ints_t.compare y2 x1 <= 0 && Ints_t.compare x2 y1 <= 0 then
of_bool ik true
else if Ints_t.compare y2 x1 < 0 || Ints_t.compare x2 y1 < 0 then
of_bool ik false
else top_bool
let ge ik x y =
match x, y with
| None, None -> bot_of ik
| None, _ | _, None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
| Some (x1,x2), Some (y1,y2) ->
if Ints_t.compare y2 x1 <= 0 then of_bool ik true
else if Ints_t.compare x2 y1 < 0 then of_bool ik false
else top_bool
let le ik x y =
match x, y with
| None, None -> bot_of ik
| None, _ | _, None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
| Some (x1,x2), Some (y1,y2) ->
if Ints_t.compare x2 y1 <= 0 then of_bool ik true
else if Ints_t.compare y2 x1 < 0 then of_bool ik false
else top_bool
let gt ik x y =
match x, y with
| None, None -> bot_of ik
| None, _ | _, None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
| Some (x1,x2), Some (y1,y2) ->
if Ints_t.compare y2 x1 < 0 then of_bool ik true
else if Ints_t.compare x2 y1 <= 0 then of_bool ik false
else top_bool
let lt ik x y =
match x, y with
| None, None -> bot_of ik
| None, _ | _, None -> raise (ArithmeticOnIntegerBot (Printf.sprintf "%s op %s" (show x) (show y)))
| Some (x1,x2), Some (y1,y2) ->
if Ints_t.compare x2 y1 < 0 then of_bool ik true
else if Ints_t.compare y2 x1 <= 0 then of_bool ik false
else top_bool
let invariant_ikind e ik x =
match x with
| Some (x1, x2) when Ints_t.compare x1 x2 = 0 ->
if get_bool "witness.invariant.exact" then
let x1 = Ints_t.to_bigint x1 in
Invariant.of_exp Cil.(BinOp (Eq, e, kintegerCilint ik x1, intType))
else
Invariant.top ()
| Some (x1, x2) ->
let (min_ik, max_ik) = range ik in
let (x1', x2') = BatTuple.Tuple2.mapn (Ints_t.to_bigint) (x1, x2) in
let inexact_type_bounds = get_bool "witness.invariant.inexact-type-bounds" in
let i1 = if inexact_type_bounds || Ints_t.compare min_ik x1 <> 0 then Invariant.of_exp Cil.(BinOp (Le, kintegerCilint ik x1', e, intType)) else Invariant.none in