Source file ast.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
(** AST of the C language. *)
open Mopsa
open Mopsa_c_parser
open Universal.Ast
(** {2 Types} *)
type c_typedef = {
c_typedef_org_name: string; (** name as in source *)
c_typedef_unique_name: string; (** unique name *)
mutable c_typedef_def: typ; (** declaration *)
c_typedef_range: Location.range; (** declaration location *)
}
(** Type definition. *)
and c_record_kind = C_struct | C_union
(** Whether a record is struct or union. *)
and c_record_type = {
c_record_kind: c_record_kind;
c_record_org_name: string; (** name as in source, may be empty *)
c_record_unique_name: string; (** unique, non-empty name *)
c_record_defined: bool; (** false if only declared *)
c_record_sizeof: Z.t; (** size of record, in bytes *)
c_record_alignof: Z.t; (** alignment, in bytes *)
mutable c_record_fields: c_record_field list;
c_record_range: Location.range; (** declaration location *)
}
(** Struct or union type. *)
and c_record_field = {
c_field_org_name: string; (** may be empty for anonymous or padding fields *)
c_field_name: string; (** non-empty name *)
c_field_offset: int;
c_field_bit_offset: int;
c_field_type: typ;
c_field_range: Location.range; (** declaration location *)
c_field_index: int;
}
(** Struct or union field. *)
and c_enum_type = {
c_enum_org_name: string; (** name as in source, may be empty *)
c_enum_unique_name: string; (** unique, non-empty name *)
c_enum_defined: bool; (** false if only declared *)
c_enum_values: c_enum_value list;
c_enum_integer_type: c_integer_type;
c_enum_range: Location.range; (** declaration location *)
}
(** Enumerated type. *)
and c_enum_value = {
c_enum_val_org_name: string; (** name as in source *)
c_enum_val_unique_name: string; (** unique name *)
c_enum_val_value: Z.t;
c_enum_val_range: range;
}
(** A possible value in an enumerated type. *)
and c_integer_type =
| C_signed_char
| C_unsigned_char
| C_signed_short
| C_unsigned_short
| C_signed_int
| C_unsigned_int
| C_signed_long
| C_unsigned_long
| C_signed_long_long
| C_unsigned_long_long
| C_signed_int128
| C_unsigned_int128
(** Integer types. *)
and c_float_type = C_float | C_double | C_long_double | C_float128
(** Floating-point types. *)
and c_array_length =
| C_array_no_length
| C_array_length_cst of Z.t
| C_array_length_expr of expr
(** Cases of arrays length. *)
and c_qual = {
c_qual_is_const: bool;
c_qual_is_volatile: bool;
c_qual_is_restrict: bool;
}
(** Type qualifiers. *)
and c_function_type = {
c_ftype_return: typ;
c_ftype_params: typ list;
c_ftype_variadic: bool;
}
(** Function type. *)
type typ +=
| T_c_void
(** Void type. *)
| T_c_bool
| T_c_integer of c_integer_type
| T_c_float of c_float_type
| T_c_pointer of typ
(** Scalar types. *)
| T_c_array of typ * c_array_length
(** Arrays. *)
| T_c_bitfield of
typ *
int
(** Bitfields, with bit-width, only used in struct. *)
| T_c_function of c_function_type option
(** Function, with or without a prototype *)
| T_c_builtin_fn
(** Built-in functions *)
| T_c_typedef of c_typedef
(** Typedefs *)
| T_c_record of c_record_type
(** struct and union *)
| T_c_enum of c_enum_type
(** enums *)
| T_c_qualified of c_qual * typ
(** Qualified type. *)
| T_c_block_object of typ
(** Type of block objects. *)
| T_c_unknown_builtin of string
(** Unknown builtin type. *)
(** {2 Function descriptor} *)
(** *********************** *)
type c_fundec = {
mutable c_func_uid: int; (** unique identifier *)
mutable c_func_org_name: string; (** original name *)
mutable c_func_unique_name: string; (** unique name for globals and statics *)
c_func_is_static: bool;
mutable c_func_return: typ; (** type of returned value *)
mutable c_func_parameters: var list; (** function parameters *)
mutable c_func_body: stmt option; (** function body *)
mutable c_func_static_vars: var list; (** static variables declared in the function *)
mutable c_func_local_vars: var list; (** local variables declared in the function (excluding parameters) *)
mutable c_func_variadic: bool; (** whether the has a variable number of arguments *)
mutable c_func_range: range; (** location range of the declaration *)
mutable c_func_name_range: range; (** location range of the name in the declaration *)
mutable c_func_stub: Stubs.Ast.stub_func option; (** stub comment *)
}
(** Function descriptor. *)
(** {2 C variables} *)
type c_var_scope =
| Variable_global (** global shared among translation units *)
| Variable_extern (** declared but not defined *)
| Variable_local of c_fundec (** local to a function *)
| Variable_parameter of c_fundec (** formal argument *)
| Variable_file_static of string (** restricted to a translation unit *)
| Variable_func_static of c_fundec (** restricted to a function *)
let pp_scope fmt s =
Format.fprintf fmt "%s" (match s with
| Variable_global -> "variable_global"
| Variable_extern -> "extern" (** declared but not defined *)
| Variable_local _ -> "local"
| Variable_parameter _ -> "parameter"
| Variable_file_static _ -> "file static"
| Variable_func_static _ -> "func static")
(** Variable initialization. *)
type c_var_init =
| C_init_expr of expr
| C_init_list of c_var_init list (** specified elements *) * c_var_init option (** filler *)
| C_init_implicit of typ
type cvar = {
cvar_scope: c_var_scope; (** life-time scope of the variable *)
cvar_range: range; (** declaration range *)
cvar_uid: int;
cvar_orig_name : string;
cvar_uniq_name : string;
cvar_before_stmts: stmt list; (** list of statements to execute before the declaration of a variable *)
cvar_after_stmts: stmt list; (** list of statements to execute after the declaration of a variable *)
}
type var_kind +=
| V_cvar of cvar
(** C variable *)
let () =
register_var {
print = (fun next fmt v ->
match vkind v with
| V_cvar cvar ->
if !Framework.Core.Ast.Var.print_uniq_with_uid then
Format.fprintf fmt "%s:%a" cvar.cvar_orig_name pp_relative_range cvar.cvar_range
else
Format.fprintf fmt "%s" cvar.cvar_orig_name
| _ -> next fmt v
);
compare = (fun next v1 v2 ->
match vkind v1, vkind v2 with
| V_cvar cvar1, V_cvar cvar2 ->
Compare.compose [
(fun () -> Stdlib.compare cvar1.cvar_uid cvar2.cvar_uid);
(fun () -> Stdlib.compare cvar1.cvar_uniq_name cvar2.cvar_uniq_name)
]
| _ -> next v1 v2
);
}
(** {2 C expressions} *)
type operator +=
| O_c_and
| O_c_or
type c_inc_location =
| PRE
| POST
(** Whether an incrementation is performed before (PRE) or after (POST) the expression value is used *)
type c_inc_direction =
| INC
| DEC
(** Whether an incrementation is ++ or -- *)
type c_character_kind =
| C_char_ascii
| C_char_wide
| C_char_utf8
| C_char_utf16
| C_char_utf32
| C_char_unevaluated
type constant +=
| C_c_character of Z.t * c_character_kind
(** Constant character *)
| C_c_string of string * c_character_kind
(** Constant string literal *)
| C_c_invalid
(** Invalid pointer value *)
type expr_kind +=
| E_c_conditional of expr (** condition *) * expr (** then *) * expr (** else *)
(** ?: ternary operator *)
| E_c_array_subscript of expr (** array *) * expr (** index *)
(** Array access. *)
| E_c_member_access of expr (** record *) * int (** field index *) * string (** field *)
(** record.field access *)
| E_c_function of c_fundec
| E_c_builtin_function of string
| E_c_builtin_call of string * expr list
| E_c_arrow_access of expr (** pointer *) * int (** field index *) * string (** field *)
(** pointer->field access *)
| E_c_assign of expr (** lvalue *) * expr (** rvalue*)
(** Assignment as an expression *)
| E_c_compound_assign of
expr (** lvalue *) * typ (** promoted type of lvalue before operation *) *
operator (** operator *) *
expr (** rvalue *) *
typ (** type of the result, before converting back to lvalue type *)
(** Assignment with an operation: e1 += e2, etc. *)
| E_c_comma of expr * expr (** , operator *)
| E_c_increment of c_inc_direction * c_inc_location * expr
| E_c_address_of of expr
(** & operator (address of lvalue) *)
| E_c_deref of expr
(** * operator (pointer dereference) *)
| E_c_cast of expr * bool (** explicitness *)
(** casted expression *)
| E_c_statement of stmt
| E_c_predefined of string (** predefined identifier *)
| E_c_var_args of expr (** __builtin_va_arg *)
| E_c_atomic of int (** operation *) * expr * expr
| E_c_block_object of expr
(** Block objects are useful to distinguish between operations on
the block itself and its content. For, expanding the contents of
a block will duplicate every cell in the block, while expanding
the block object will update the pointers that point to the
block. *)
(** {2 Scope update} *)
type c_scope_update = {
c_scope_var_added: var list;
c_scope_var_removed: var list;
}
(** Scope update information for jump statements *)
(** {2 Statements} *)
type stmt_kind +=
| S_c_goto_stab of stmt
(** stabilization point for goto statement *)
| S_c_declaration of var * c_var_init option * c_var_scope
(** declaration of a variable *)
| S_c_do_while of
stmt (** body *) *
expr (** condition *)
(** do-while loop *)
| S_c_for of
stmt (** init *) *
expr option (** condition *) *
expr option (** increment *) *
stmt (** body *)
(** for loop; the scope of the locals declared in the init block
is the while for loop *)
| S_c_return of expr option * c_scope_update
(** return statement *)
| S_c_break of c_scope_update
(** break statement *)
| S_c_continue of c_scope_update
(** continue statement *)
| S_c_goto of string * c_scope_update
(** goto statements. *)
| S_c_switch of expr * stmt
(** switch statement. *)
| S_c_label of string
(** statement label. *)
| S_c_switch_case of expr list * c_scope_update
(** case of a switch statement.
case a:
case b:
stmt;
is represented through S_c_switch_case [a; b] to factor in some cases
For integer cases, we use the interval [a, b] to simplify expressions, similar to the GCC C extension for ranges
*)
| S_c_switch_default of c_scope_update
(** default case of switch statements. *)
| S_c_asm of string
(** inline assembly
for now, we keep only a string representation to display warnings;
see C_AST.asm_kind for a more usable representation when support is added
*)
type c_program = {
c_globals : (var * c_var_init option) list; (** global variables of the program *)
c_functions : c_fundec list; (** functions of the program *)
c_stub_directives : Stubs.Ast.stub_directive list; (** list of stub directives *)
}
type prog_kind +=
| C_program of c_program
module CProgramKey = GenContextKey(struct
type 'a t = c_program
let print pp fmt prog = Format.fprintf fmt "C program"
end)
(** Flow-insensitive context to keep the analyzed C program *)
let c_program_ctx = CProgramKey.key
(** Set the C program in the flow *)
let set_c_program prog flow =
Flow.set_ctx (Flow.get_ctx flow |> add_ctx c_program_ctx prog) flow
(** Get the C program from the flow *)
let get_c_program flow =
Flow.get_ctx flow |> find_ctx c_program_ctx
(** {2 Conversion to/from Clang parser types} *)
let to_clang_int_type : c_integer_type -> C_AST.integer_type = function
| C_signed_char -> C_AST.SIGNED_CHAR
| C_unsigned_char -> C_AST.UNSIGNED_CHAR
| C_signed_short -> C_AST.SIGNED_SHORT
| C_unsigned_short -> C_AST.UNSIGNED_SHORT
| C_signed_int -> C_AST.SIGNED_INT
| C_unsigned_int -> C_AST.UNSIGNED_INT
| C_signed_long -> C_AST.SIGNED_LONG
| C_unsigned_long -> C_AST.UNSIGNED_LONG
| C_signed_long_long -> C_AST.SIGNED_LONG_LONG
| C_unsigned_long_long -> C_AST.UNSIGNED_LONG_LONG
| C_signed_int128 -> C_AST.SIGNED_INT128
| C_unsigned_int128 -> C_AST.UNSIGNED_INT128
let to_clang_float_type : c_float_type -> C_AST.float_type = function
| C_float -> C_AST.FLOAT
| C_double -> C_AST.DOUBLE
| C_long_double -> C_AST.LONG_DOUBLE
| C_float128 -> C_AST.FLOAT128
let from_clang_int_type : C_AST.integer_type -> c_integer_type = function
| C_AST.(Char SIGNED) -> C_signed_char
| C_AST.(Char UNSIGNED) -> C_unsigned_char
| C_AST.SIGNED_CHAR -> C_signed_char
| C_AST.UNSIGNED_CHAR -> C_unsigned_char
| C_AST.SIGNED_SHORT -> C_signed_short
| C_AST.UNSIGNED_SHORT -> C_unsigned_short
| C_AST.SIGNED_INT -> C_signed_int
| C_AST.UNSIGNED_INT -> C_unsigned_int
| C_AST.SIGNED_LONG -> C_signed_long
| C_AST.UNSIGNED_LONG -> C_unsigned_long
| C_AST.SIGNED_LONG_LONG -> C_signed_long_long
| C_AST.UNSIGNED_LONG_LONG -> C_unsigned_long_long
| C_AST.SIGNED_INT128 -> C_signed_int128
| C_AST.UNSIGNED_INT128 -> C_unsigned_int128
let from_clang_float_type : C_AST.float_type -> c_float_type = function
| C_AST.FLOAT -> C_float
| C_AST.DOUBLE -> C_double
| C_AST.LONG_DOUBLE -> C_long_double
| C_AST.FLOAT128 -> C_float128
(** Target information *)
module TargetCtx = GenContextKey
(struct
type 'a t = Clang_AST.target_info
let print _ fmt _ =
Format.pp_print_string fmt "target information"
end)
let get_c_target_info flow =
let ctx = Flow.get_ctx flow in
find_ctx TargetCtx.key ctx
let set_c_target_info target flow =
let ctx = Flow.get_ctx flow in
let ctx' = add_ctx TargetCtx.key target ctx in
Flow.set_ctx ctx' flow
let remove_c_target_info flow =
let ctx = Flow.get_ctx flow in
let ctx' = remove_ctx TargetCtx.key ctx in
Flow.set_ctx ctx' flow
(** {2 Sizes and alignments} *)
(** [sizeof t] computes the size (in bytes) of a C type [t] *)
let rec sizeof_type_in_target (t : typ) target : Z.t =
match t with
| T_c_void -> C_utils.sizeof_type target C_AST.T_void
| T_c_bool -> C_utils.sizeof_type target C_AST.T_bool
| T_c_integer i -> to_clang_int_type i |> C_utils.sizeof_int target |> Z.of_int
| T_c_float f -> to_clang_float_type f |> C_utils.sizeof_float target |> Z.of_int
| T_c_pointer _ -> fst C_AST.void_ptr_type |> C_utils.sizeof_type target
| T_c_array (t, C_array_length_cst x) -> Z.mul x (sizeof_type_in_target t target)
| T_c_array (_, (C_array_no_length | C_array_length_expr _)) -> panic ~loc:__LOC__ "%a has no length information" pp_typ t
| T_c_bitfield(t, size) -> Z.of_int size
| T_c_function _ | T_c_builtin_fn -> panic ~loc:__LOC__ "%a is a function" pp_typ t
| T_c_typedef td -> sizeof_type_in_target td.c_typedef_def target
| T_c_record r ->
if not r.c_record_defined then Z.zero
else r.c_record_sizeof
| T_c_enum e ->
if not e.c_enum_defined then panic ~loc:__LOC__ "%a is undefined" pp_typ t;
sizeof_type_in_target (T_c_integer e.c_enum_integer_type) target
| T_c_qualified (_,t) -> sizeof_type_in_target t target
| t -> panic ~loc:__LOC__ "%a not a C type" pp_typ t
let sizeof_type (t : typ) flow : Z.t =
let target = get_c_target_info flow in
sizeof_type_in_target t target
let host_target_info = Clang_parser.get_target_info (Clang_parser.get_default_target_options ())
let sizeof_type_in_host (t : typ) : Z.t =
sizeof_type_in_target t host_target_info
let sizeof_expr (t:typ) flow range : expr =
let rec doit t =
match t with
| T_c_void | T_c_bool | T_c_integer _ | T_c_float _ | T_c_pointer _ | T_c_record _ | T_c_enum _ ->
mk_z (sizeof_type t flow) range
| T_c_array (t,l) ->
let len = match l with
| C_array_length_cst len -> mk_z len range
| C_array_length_expr e -> e
| C_array_no_length ->
mk_zero range
in
mk_binop (doit t) (O_mult) len range
| T_c_bitfield (t,_) -> invalid_arg "sizeof_expr: size of bitfield"
| T_c_function _ | T_c_builtin_fn -> invalid_arg "sizeof_expr: size of function"
| T_c_typedef t -> doit (t.c_typedef_def)
| _ -> assert false
in
doit t
(** Size (in bytes) of a type, as an expression. Handles variable-length ararys. *)
let rec remove_typedef = function
| T_c_typedef(td) -> remove_typedef (td.c_typedef_def)
| t -> t
let rec remove_qual = function
| T_c_qualified(_, t) -> remove_qual t
| T_c_pointer t -> T_c_pointer (remove_qual t)
| t -> t
let rec remove_typedef_qual = function
| T_c_qualified(_, t) -> remove_typedef_qual t
| T_c_typedef(td) -> remove_typedef_qual (td.c_typedef_def)
| t -> t
(** [is_signed t] whether [t] is signed *)
let rec is_signed (t : typ) : bool=
match remove_typedef_qual t with
| T_c_bool -> true
| T_c_integer it ->
begin
match it with
| C_signed_char | C_signed_short | C_signed_int
| C_signed_long | C_signed_long_long | C_signed_int128 -> true
| _ -> false
end
| T_c_enum e -> is_signed (T_c_integer e.c_enum_integer_type)
| _ -> panic ~loc:__LOC__ "%a is not an integer type" pp_typ t
(** [range t] computes the interval range of type [t] *)
let rangeof (t : typ) flow =
let part = 8*Z.to_int (sizeof_type t flow) in
if is_signed t then
let part' = Z.pow (Z.of_int (2)) (part -1) in
( Z.neg part', Z.sub part' (Z.of_int 1))
else
let part' = Z.pow (Z.of_int 2) part in
( Z.of_int 0 , Z.sub part' (Z.of_int 1))
(** [range t] computes the interval range of type [t] as integers *)
let int_rangeof t flow =
let a,b = rangeof t flow in
(Z.to_int a, Z.to_int b)
(** [wrap_expr e (l,h)] expression needed to bring back [e] in range ([l],[h]) *)
let wrap_expr (e: expr) ((l,h) : Z.t * Z.t) range : expr =
mk_unop (O_wrap(l,h)) e ~etyp:e.etyp range
let is_c_char_type (t:typ) =
match remove_typedef_qual t with
| T_c_integer (C_signed_char | C_unsigned_char) -> true
| _ -> false
let is_c_string_type (t:typ) =
match remove_typedef_qual t with
| T_c_array (t,_) -> is_c_char_type t
| _ -> false
(** [is_c_int_type t] tests whether [t] is an integer type *)
let is_c_int_type ( t : typ) =
match remove_typedef_qual t with
| T_c_bool -> true
| T_c_enum _ -> true
| T_c_integer _ -> true
| _ -> false
let is_c_int_array_type (t:typ) =
match remove_typedef_qual t with
| T_c_array (t,_) -> is_c_int_type t
| _ -> false
let is_c_signed_int_type (t:typ) =
match remove_typedef_qual t with
| T_c_bool -> false
| T_c_enum _ -> false
| T_c_integer (C_signed_char | C_signed_short | C_signed_int | C_signed_int128 | C_signed_long | C_signed_long_long) -> true
| T_c_integer (C_unsigned_char | C_unsigned_short | C_unsigned_int | C_unsigned_int128 | C_unsigned_long | C_unsigned_long_long) -> false
| _ -> false
let is_c_bool_type (t:typ) =
match remove_typedef_qual t with
| T_c_bool -> true
| _ -> false
(** [is_c_int_type t] tests whether [t] is a floating point type *)
let is_c_float_type ( t : typ) =
match remove_typedef_qual t with
| T_c_float _ -> true
| _ -> false
let get_c_float_type ( t : typ) =
match remove_typedef_qual t with
| T_c_float t -> t
| _ -> panic ~loc:__LOC__ "get_c_float_type called on a non-float type %a" pp_typ t
(** Get the float precision from a C type *)
let get_c_float_precision t =
match get_c_float_type t with
| C_float -> F_SINGLE
| C_double -> F_DOUBLE
| C_long_double -> F_LONG_DOUBLE
| C_float128 -> F_FLOAT128
let is_c_bitfield typ = match typ with
| T_c_bitfield _ -> true
| _ -> false
(** [is_c_int_type t] tests whether [t] is a numeric type *)
let is_c_num_type (t:typ) =
is_c_int_type t || is_c_float_type t || is_c_bitfield t
(** [is_c_scalar_type t] tests whether [t] is a scalar type *)
let is_c_scalar_type ( t : typ) =
match remove_typedef_qual t with
| T_c_bool | T_c_integer _ | T_c_float _ | T_c_pointer _ -> true
| T_c_bitfield _ -> true
| T_c_enum _ -> true
| _ -> false
(** [is_c_pointer t] tests whether [t] is a pointer *)
let is_c_pointer_type ( t : typ) =
match remove_typedef_qual t with
| T_c_pointer _ -> true
| T_c_array _ -> true
| _ -> false
let is_c_void_type (t:typ) =
match remove_typedef_qual t with
| T_c_void -> true
| _ -> false
let is_c_record_type ( t : typ) =
match remove_typedef_qual t with
| T_c_record _ -> true
| _ -> false
let is_c_struct_type (t : typ) =
match remove_typedef_qual t with
| T_c_record({c_record_kind = C_struct}) -> true
| _ -> false
let is_c_union_type (t : typ) =
match remove_typedef_qual t with
| T_c_record({c_record_kind = C_union}) -> true
| _ -> false
let rec is_c_array_type (t: typ) =
match remove_typedef_qual t with
| T_c_array _ -> true
| _ -> false
let rec is_c_function_type (t: typ) =
match remove_typedef_qual t with
| T_c_function _ -> true
| _ -> false
(** [is_scalartype t] lifts [t] to a pointer to [t] *)
let pointer_type (t : typ) =
(T_c_pointer t)
let rec under_pointer_type (t : typ) : typ =
match remove_typedef_qual t with
| T_c_pointer t' -> t'
| _ -> failwith "[under_pointer_type] called with a non pointer argument"
let rec under_array_type (t : typ) : typ =
match remove_typedef_qual t with
| T_c_array (t', _) -> t'
| _ -> failwith "[under_array_type] called with a non array argument"
let under_type (t: typ) : typ =
match remove_typedef_qual t with
| T_c_array _ -> under_array_type t
| T_c_pointer _ -> under_pointer_type t
| _ -> failwith "[under_type] called with a non array/pointer argument"
let void_to_char t =
match remove_typedef_qual t with
| T_c_void -> T_c_integer C_unsigned_char
| _ -> t
let get_array_constant_length t =
match remove_typedef_qual t with
| T_c_array(_, C_array_length_cst n) -> n
| _ -> assert false
let align_byte t i =
match remove_typedef_qual t with
| T_c_record crt -> (List.nth crt.c_record_fields i).c_field_offset
| _ -> assert false
let is_c_type = function
| T_c_void
| T_c_bool
| T_c_integer _
| T_c_float _
| T_c_pointer _
| T_c_array _
| T_c_bitfield _
| T_c_function _
| T_c_builtin_fn
| T_c_typedef _
| T_c_record _
| T_c_enum _
| T_c_qualified _ -> true
| T_addr -> true
| _ -> false
let is_c_function_parameter v =
match v.vkind with
| V_cvar { cvar_scope = Variable_parameter _ } -> true
| _ -> false
let mk_c_address_of e range =
mk_expr (E_c_address_of e) ~etyp:(T_c_pointer e.etyp) range
let mk_c_deref e range =
mk_expr (E_c_deref e) ~etyp:(under_pointer_type e.etyp) range
let mk_c_member_access r f range =
mk_expr (E_c_member_access (r, f.c_field_index, f.c_field_org_name)) ~etyp:f.c_field_type range
let mk_c_arrow_access r f range =
mk_expr (E_c_arrow_access (r, f.c_field_index, f.c_field_org_name)) ~etyp:f.c_field_type range
let mk_c_member_access_by_name r fname range =
let fields = match remove_typedef_qual r.etyp with
| T_c_record r -> r.c_record_fields
| _ -> assert false in
let field = List.find (fun f -> f.c_field_org_name = fname) fields in
mk_c_member_access r field range
let mk_c_arrow_access_by_name r fname range =
let t = under_type r.etyp in
let fields = match remove_typedef_qual t with
| T_c_record r -> r.c_record_fields
| _ -> assert false in
let field = List.find (fun f -> f.c_field_org_name = fname) fields in
mk_c_arrow_access r field range
let mk_c_subscript_access a i range =
mk_expr (E_c_array_subscript (a, i)) ~etyp:(under_type a.etyp) range
let mk_c_character c range t =
let x = int_of_char c in
let x = if is_signed t && x >= 128 then x - 256 else x in
mk_constant (C_c_character (Z.of_int x, C_char_ascii)) range ~etyp:t
let (s:string) (off:int) t flow =
let n = Z.to_int (sizeof_type t flow) in
let target = get_c_target_info flow in
let rec doit acc i =
if i >= n then acc else
let off' = if target.target_big_endian then off+i else off+n-i-1 in
doit (Z.add (Z.mul (Z.of_int 256) acc) (Z.of_int (int_of_char s.[off']))) (i+1)
in
let v = doit Z.zero 0 in
if is_signed t && v >= Z.shift_left Z.one (n*8-1)
then Z.sub v (Z.shift_left Z.one (n*8))
else v
let mk_c_multibyte_integer (s:string) (off:int) t flow range =
mk_z (extract_multibyte_integer s off t flow) ~typ:t range
let mk_c_invalid_pointer range =
mk_constant C_c_invalid ~etyp:(T_c_pointer T_c_void) range
let void = T_c_void
let u8 = T_c_integer(C_unsigned_char)
let s8 = T_c_integer(C_signed_char)
let s16 = T_c_integer(C_signed_short)
let u16 = T_c_integer(C_unsigned_short)
let s32 = T_c_integer(C_signed_int)
let u32 = T_c_integer(C_unsigned_int)
let s64 = T_c_integer(C_signed_long)
let u64 = T_c_integer(C_unsigned_long)
let ul = T_c_integer(C_unsigned_long)
let sl = T_c_integer(C_signed_long)
let ull = T_c_integer(C_unsigned_long_long)
let sll = T_c_integer(C_signed_long_long)
let array_type typ size = T_c_array(typ,C_array_length_cst size)
let size_type flow =
let t = C_utils.size_type (get_c_target_info flow) |>
from_clang_int_type in
T_c_integer t
let type_of_string s = T_c_array(s8, C_array_length_cst (Z.of_int (1 + String.length s)))
let is_c_block_object_type = function T_c_block_object _ -> true | _ -> false
let to_c_block_object e = mk_expr (E_c_block_object e) e.erange ~etyp:(T_c_block_object e.etyp)
let of_c_block_object e =
match ekind e with
| E_c_block_object ee -> ee
| _ -> assert false
let mk_c_string ?(kind=C_char_ascii) s range =
mk_constant (C_c_string (s, kind)) range ~etyp:(type_of_string s)
let mk_c_fun_typ f =
let ftype = {
c_ftype_return = f.c_func_return;
c_ftype_params = List.map (fun p -> p.vtyp) f.c_func_parameters;
c_ftype_variadic = f.c_func_variadic;
}
in
T_c_function (Some ftype)
let mk_c_call f args range =
mk_expr (E_call (mk_expr (E_c_function f) range ~etyp:(mk_c_fun_typ f), args)) range ~etyp:(f.c_func_return)
let mk_c_builtin_call builtin args typ range =
mk_expr (E_c_builtin_call (builtin,args)) range ~etyp:typ
let mk_c_call_stmt f args range =
let exp = mk_c_call f args range in
mk_stmt (S_expression exp) range
let mk_c_cast e t range =
mk_expr (E_c_cast(e, true)) ~etyp:t range
let mk_c_null range =
mk_c_cast (mk_zero ~typ:u8 range) (pointer_type void) range
let mk_c_declaration v init scope range =
mk_stmt (S_c_declaration (v, init, scope)) range
let is_c_global_scope = function
| Variable_global | Variable_extern | Variable_file_static _ -> true
| Variable_func_static _ | Variable_local _ | Variable_parameter _ -> false
let () =
register_typ_compare (fun next t1 t2 ->
match remove_typedef t1, remove_typedef t2 with
| T_c_void, T_c_void -> 0
| T_c_bool, T_c_bool -> 0
| T_c_integer i1, T_c_integer i2 -> compare i1 i2
| T_c_float f1, T_c_float f2 -> compare f1 f2
| T_c_pointer t1, T_c_pointer t2 -> compare_typ t1 t2
| T_c_array(t1, l1), T_c_array(t2, l2) ->
Compare.compose [
(fun () -> compare_typ t1 t2);
(fun () -> match l1, l2 with
| C_array_length_cst n1, C_array_length_cst n2 -> Z.compare n1 n2
| C_array_length_expr e1, C_array_length_expr e2 -> panic ~loc:__LOC__ "type compare on arrays with expr length not supported"
| C_array_no_length, C_array_no_length -> 0
| _ -> compare l1 l2
)
]
| T_c_bitfield(t1, n1), T_c_bitfield(t2, n2) ->
Compare.compose [
(fun () -> compare_typ t1 t2);
(fun () -> compare n1 n2)
]
| T_c_function f1, T_c_function f2 ->
begin
match f1, f2 with
| Some ff1, Some ff2 ->
if List.length ff1.c_ftype_params = List.length ff2.c_ftype_params then
let l = [
(fun () -> compare_typ ff1.c_ftype_return ff2.c_ftype_return);
(fun () -> compare ff1.c_ftype_variadic ff2.c_ftype_variadic)
] @ (List.map2 (fun t t' -> fun () -> compare_typ t t') ff1.c_ftype_params ff2.c_ftype_params)
in
Compare.compose l
else 1
| None, None -> 0
| _ -> 1
end
| T_c_builtin_fn, T_c_builtin_fn -> 0
| T_c_typedef td1, T_c_typedef td2 -> compare_typ td1.c_typedef_def td2.c_typedef_def
| T_c_record r1, T_c_record r2 ->
if r1 == r2 then 0
else
let compare_c_record_field f1 f2 =
if f1 == f2 then 0
else
Compare.compose [
(fun () -> Stdlib.compare f1.c_field_org_name f2.c_field_org_name);
(fun () -> Stdlib.compare f1.c_field_offset f2.c_field_offset);
(fun () -> Stdlib.compare f1.c_field_bit_offset f2.c_field_bit_offset);
(fun () -> compare_typ f1.c_field_type f2.c_field_type);
(fun () -> Stdlib.compare f1.c_field_index f2.c_field_index)
]
in
Compare.compose [
(fun () -> String.compare r1.c_record_unique_name r2.c_record_unique_name);
(fun () -> Stdlib.compare r1.c_record_kind r2.c_record_kind);
(fun () -> Stdlib.compare r1.c_record_defined r2.c_record_defined);
(fun () -> Z.compare r1.c_record_sizeof r2.c_record_sizeof);
(fun () -> Z.compare r1.c_record_alignof r2.c_record_alignof);
(fun () -> Compare.list compare_c_record_field r1.c_record_fields r2.c_record_fields)
]
| T_c_enum e1, T_c_enum e2 ->
let compare_c_enum_value v1 v2 =
Z.compare v1.c_enum_val_value v2.c_enum_val_value
in
Compare.compose [
(fun () -> Stdlib.compare e1.c_enum_defined e2.c_enum_defined);
(fun () -> Compare.list compare_c_enum_value e1.c_enum_values e2.c_enum_values);
(fun () -> Stdlib.compare e1.c_enum_integer_type e2.c_enum_integer_type)
]
| T_c_qualified (q1, t1), T_c_qualified (q2, t2) ->
Compare.compose [
(fun () -> compare q1.c_qual_is_const q2.c_qual_is_const);
(fun () -> compare q1.c_qual_is_volatile q2.c_qual_is_volatile);
(fun () -> compare q1.c_qual_is_restrict q2.c_qual_is_restrict);
(fun () -> compare_typ t1 t2)
]
| T_c_block_object tt1, T_c_block_object tt2 -> compare_typ tt1 tt2
| _ -> next t1 t2
)
let compare_c_fundec f1 f2 =
Compare.compose [
(fun () -> Stdlib.compare f1.c_func_org_name f2.c_func_org_name);
(fun () -> Stdlib.compare f1.c_func_is_static f2.c_func_is_static);
(fun () -> compare_typ f1.c_func_return f2.c_func_return);
(fun () -> Compare.list compare_var f1.c_func_parameters f2.c_func_parameters);
(fun () -> Compare.option compare_stmt f1.c_func_body f2.c_func_body);
(fun () -> Compare.list compare_var f1.c_func_static_vars f2.c_func_static_vars);
(fun () -> Compare.list compare_var f1.c_func_local_vars f2.c_func_local_vars);
(fun () -> Stdlib.compare f1.c_func_variadic f2.c_func_variadic)
]
let () =
register_expr_compare
(fun next e1 e2 ->
match ekind e1, ekind e2 with
| E_c_conditional(cond1,then1,else1), E_c_conditional(cond2,then2,else2) ->
Compare.triple compare_expr compare_expr compare_expr
(cond1,then1,else1)
(cond2,then2,else2)
| E_c_array_subscript(a1,i1), E_c_array_subscript(a2,i2) ->
Compare.pair compare_expr compare_expr
(a1,i1)
(a2,i2)
| E_c_member_access(s1,i1,f1), E_c_member_access(s2,i2,f2) ->
Compare.triple compare_expr compare compare
(s1,i1,f1)
(s2,i2,f2)
| E_c_function(f1), E_c_function(f2) ->
compare_c_fundec f1 f2
| E_c_builtin_function(f1), E_c_builtin_function(f2) ->
compare f1 f2
| E_c_builtin_call(f1,args1), E_c_builtin_call(f2,args2) ->
Compare.pair compare (Compare.list compare_expr)
(f1,args1)
(f2,args2)
| E_c_arrow_access(p1,i1,f1), E_c_arrow_access(p2,i2,f2) ->
Compare.triple compare_expr compare compare
(p1,i1,f1)
(p2,i2,f2)
| E_c_assign(x1,e1), E_c_assign(x2,e2) ->
Compare.pair compare_expr compare_expr
(x1,e1)
(x2,e2)
| E_c_compound_assign(lval1,t1,op1,rval1,tt1), E_c_compound_assign(lval2,t2,op2,rval2,tt2) ->
Compare.compose [
(fun () -> compare_expr lval1 lval2);
(fun () -> compare_typ t1 t2);
(fun () -> compare_operator op1 op2);
(fun () -> compare_expr rval1 rval2);
(fun () -> compare_typ tt1 tt2);
]
| E_c_comma(e1,ee1), E_c_comma(e2,ee2) ->
Compare.pair compare_expr compare_expr
(e1,ee1)
(e2,ee2)
| E_c_increment(dir1,loc1,e1), E_c_increment(dir2,loc2,e2) ->
Compare.triple compare compare compare_expr
(dir1,loc1,e1)
(dir2,loc2,e2)
| E_c_address_of(e1), E_c_address_of(e2) ->
compare_expr e1 e2
| E_c_deref(e1), E_c_deref(e2) ->
compare_expr e1 e2
| E_c_cast(e1,b1), E_c_cast(e2,b2) ->
Compare.pair compare_expr compare
(e1,b1)
(e2,b2)
| E_c_statement(s1), E_c_statement(s2) ->
compare_stmt s1 s2
| E_c_predefined(s1), E_c_predefined(s2) ->
compare s1 s2
| E_c_var_args(e1), E_c_var_args(e2) ->
compare_expr e1 e2
| E_c_atomic(op1,e1,ee1), E_c_atomic(op2,e2,ee2) ->
Compare.triple compare compare_expr compare_expr
(op1,e1,ee1)
(op2,e2,ee2)
| E_c_block_object(e1), E_c_block_object(e2) ->
compare_expr e1 e2
| _ -> next e1 e2
)
(** Statement comparison **)
let rec compare_c_var_init i1 i2 =
match i1, i2 with
| C_init_expr e1, C_init_expr e2 ->
compare_expr e1 e2
| C_init_list(l1,o1), C_init_list(l2,o2) ->
Compare.compose [
(fun () -> Compare.list compare_c_var_init l1 l2);
(fun () -> Compare.option compare_c_var_init o1 o2)
]
| C_init_implicit t1, C_init_implicit t2 ->
compare_typ t1 t2
| _ ->
Stdlib.compare i1 i2
let compare_c_fundec f1 f2 =
Compare.compose [
(fun () -> Stdlib.compare f1.c_func_uid f2.c_func_uid);
(fun () -> Stdlib.compare f1.c_func_unique_name f2.c_func_unique_name)
]
let compare_c_var_scope s1 s2 =
match s1, s2 with
| Variable_local f1, Variable_local f2
| Variable_parameter f1, Variable_parameter f2
| Variable_func_static f1,Variable_func_static f2 ->
compare_c_fundec f1 f2
| _ ->
Stdlib.compare s1 s2
let compare_c_var_scope_update s1 s2 =
Compare.compose [
(fun () -> Compare.list compare_var s1.c_scope_var_added s2.c_scope_var_added);
(fun () -> Compare.list compare_var s1.c_scope_var_removed s2.c_scope_var_removed)
]
let () =
register_stmt_compare
(fun next s1 s2 ->
match skind s1, skind s2 with
| S_c_goto_stab(s1), S_c_goto_stab(s2) ->
compare_stmt s1 s2
| S_c_declaration(v1,i1,s1), S_c_declaration(v2,i2,s2) ->
Compare.compose [
(fun () -> compare_var v1 v2);
(fun () -> Compare.option compare_c_var_init i1 i2);
(fun () -> compare_c_var_scope s1 s2)
]
| S_c_do_while(s1,e1), S_c_do_while(s2,e2) ->
Compare.compose [
(fun () -> compare_stmt s1 s2);
(fun () -> compare_expr e1 e2)
]
| S_c_for(init1,cond1,incr1,body1), S_c_for(init2,cond2,incr2,body2) ->
Compare.compose [
(fun () -> compare_stmt init1 init2);
(fun () -> Compare.option compare_expr cond1 cond2);
(fun () -> Compare.option compare_expr incr1 incr2);
(fun () -> compare_stmt body1 body2)
]
| S_c_return(e1,s1), S_c_return(e2,s2) ->
Compare.compose [
(fun () -> Compare.option compare_expr e1 e2);
(fun () -> compare_c_var_scope_update s1 s2)
]
| S_c_break(s1), S_c_break(s2)
| S_c_continue(s1), S_c_continue(s2) ->
compare_c_var_scope_update s1 s2
| S_c_goto(l1,s1), S_c_goto(l2,s2) ->
Compare.compose [
(fun () -> compare l1 l2);
(fun () -> compare_c_var_scope_update s1 s2)
]
| S_c_switch(e1,s1), S_c_switch(e2,s2) ->
Compare.compose [
(fun () -> compare_expr e1 e2);
(fun () -> compare_stmt s1 s2)
]
| S_c_label(l1), S_c_label(l2) ->
Stdlib.compare l1 l2
| S_c_switch_case(e1,s1), S_c_switch_case(e2,s2) ->
Compare.compose [
(fun () -> Compare.list compare_expr e1 e2);
(fun () -> compare_c_var_scope_update s1 s2)
]
| S_c_switch_default(s1), S_c_switch_default(s2) ->
compare_c_var_scope_update s1 s2
| _ -> next s1 s2
)
let range_cond e_mint rmin rmax range =
let condle = mk_binop e_mint O_le (mk_z rmax range) ~etyp:T_bool range in
let condge = mk_binop e_mint O_ge (mk_z rmin range) ~etyp:T_bool range in
mk_binop condle O_log_and condge ~etyp:T_bool range
let rec remove_casts e =
match ekind e with
| E_c_cast (e', _) -> remove_casts e'
| _ -> e
(** Simplify C constant expressions to constants *)
let rec c_expr_to_z (e:expr) flow : Z.t option =
match ekind e with
| E_constant (C_int n) -> Some n
| E_constant (C_c_character (ch,_)) -> Some ch
| E_unop (O_minus, e') ->
c_expr_to_z e' flow |> OptionExt.bind @@ fun n ->
Some (Z.neg n)
| E_unop (O_bit_invert, e') ->
c_expr_to_z e' flow |> OptionExt.bind @@ fun n ->
Some (Z.lognot n)
| E_unop (O_log_not, e') ->
c_expr_to_z e' flow |> OptionExt.bind @@ fun n ->
if Z.equal n Z.zero then Some Z.one else Some Z.zero
| E_binop(O_c_and, e1, e2) ->
c_expr_to_z e1 flow |> OptionExt.bind @@ fun n1 ->
if Z.equal n1 Z.zero then Some Z.zero else c_expr_to_z e2 flow
| E_binop(O_c_or, e1, e2) ->
c_expr_to_z e1 flow |> OptionExt.bind @@ fun n1 ->
if Z.equal n1 Z.zero then c_expr_to_z e2 flow else Some Z.one
| E_binop(op, e1, e2) ->
c_expr_to_z e1 flow |> OptionExt.bind @@ fun n1 ->
c_expr_to_z e2 flow |> OptionExt.bind @@ fun n2 ->
begin
match op with
| O_plus -> Some (Z.add n1 n2)
| O_minus -> Some (Z.sub n1 n2)
| O_mult -> Some (Z.mul n1 n2)
| O_div -> if Z.equal n2 Z.zero then None else Some (Z.div n1 n2)
| O_bit_lshift -> begin try Some (Z.shift_left n1 (Z.to_int n2)) with _ -> None end
| O_bit_rshift -> begin try Some (Z.shift_right n1 (Z.to_int n2)) with _ -> None end
| O_bit_and -> Some (Z.logand n1 n2)
| O_bit_or -> Some (Z.logor n1 n2)
| O_eq -> Some (if Z.equal n1 n2 then Z.one else Z.zero)
| O_ne -> Some (if Z.equal n1 n2 then Z.zero else Z.one)
| O_gt -> Some (if Z.gt n1 n2 then Z.one else Z.zero)
| O_ge -> Some (if Z.geq n1 n2 then Z.one else Z.zero)
| O_lt -> Some (if Z.lt n1 n2 then Z.one else Z.zero)
| O_le -> Some (if Z.leq n1 n2 then Z.one else Z.zero)
| _ -> None
end
| E_c_conditional(cond,e1,e2) ->
c_expr_to_z cond flow |> OptionExt.bind @@ fun c ->
if not (Z.equal c Z.zero)
then c_expr_to_z e1 flow
else c_expr_to_z e2 flow
| E_c_cast(ee,_) when is_c_int_type e.etyp ->
c_expr_to_z ee flow |> OptionExt.bind @@ fun n ->
let a,b = rangeof e.etyp flow in
if Z.leq a n && Z.leq n b then Some n else None
| _ -> None
let is_c_expr_equals_z e z flow =
match c_expr_to_z e flow with
| None -> false
| Some n -> Z.equal n z
let is_c_constant e flow =
match c_expr_to_z e flow with
| None -> false
| Some _ -> true
let rec is_c_lval e =
match ekind e with
| E_var _ | E_c_deref _ | E_c_array_subscript _ | E_c_member_access _ | E_c_arrow_access _ -> true
| Stubs.Ast.E_stub_primed ee -> is_c_lval ee
| _ -> false
let is_c_deref e =
match remove_casts e |> ekind with
| E_c_deref _ -> true
| _ -> false
let get_c_deref_type e =
match remove_casts e |> ekind with
| E_c_deref p -> under_type p.etyp
| _ -> assert false
(** Check if v is declared as a variable length array *)
let is_c_variable_length_array_type t =
match remove_typedef_qual t with
| T_c_array(_, C_array_length_expr _) -> true
| _ -> false
(** Check if v is declared as an array without length (as for many auxiliary variables) *)
let is_c_no_length_array_type t =
match remove_typedef_qual t with
| T_c_array(_, C_array_no_length) -> true
| _ -> false
(** Find the definition of a C function *)
let find_c_fundec_by_name name flow =
let prog = get_c_program flow in
List.find (fun f -> f.c_func_org_name = name) prog.c_functions
let find_c_fundec_by_uid uid flow =
let prog = get_c_program flow in
List.find (fun f -> f.c_func_uid = uid) prog.c_functions
(** Check if a pointer points to a nul-terminated array *)
let assert_valid_string (p:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_assert_valid_string" flow in
let stmt = mk_c_call_stmt f [p] range in
man.exec stmt flow
(** Check if a pointer points to a nul-terminated wide char array *)
let assert_valid_wide_string (p:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_assert_valid_wide_string" flow in
let stmt = mk_c_call_stmt f [p] range in
man.exec stmt flow
(** Check if a pointer points to a valid stream *)
let assert_valid_stream (p:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_assert_valid_stream" flow in
let stmt = mk_c_call_stmt f [p] range in
man.exec stmt flow
(** Check if a pointer points to a valid file descriptor *)
let assert_valid_file_descriptor (p:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_assert_valid_file_descriptor" flow in
let stmt = mk_c_call_stmt f [p] range in
man.exec stmt flow
(** Check if a pointer is valid *)
let assert_valid_ptr (p:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_assert_valid_ptr" flow in
let stmt = mk_c_call_stmt f [p] range in
man.exec stmt flow
(** Randomize an entire array *)
let memrand (p:expr) (i:expr) (j:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_memrand" flow in
let stmt = mk_c_call_stmt f [p; i; j] range in
man.exec stmt flow
(** Randomize a string *)
let strrand (p:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_strrand" flow in
let stmt = mk_c_call_stmt f [p] range in
man.exec stmt flow
(** Randomize a substring *)
let strnrand (p:expr) (n:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_strnrand" flow in
let stmt = mk_c_call_stmt f [p; n] range in
man.exec stmt flow
(** Randomize a wide substring *)
let wcsnrand (p:expr) (n:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_wcsnrand" flow in
let stmt = mk_c_call_stmt f [p; n] range in
man.exec stmt flow
(** Set elements of an array with the same value [c] *)
let memset (p:expr) (c:expr) (i:expr) (j:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_memset" flow in
let stmt = mk_c_call_stmt f [p; c; i; j] range in
man.exec stmt flow
(** Copy elements of an array *)
let memcpy (dst:expr) (src:expr) (i:expr) (j:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_memcpy" flow in
let stmt = mk_c_call_stmt f [dst; src; i; j] range in
man.exec stmt flow
(** Exit if status is non-zero *)
let error_error (p:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_error" flow in
let stmt = mk_c_call_stmt f [p] range in
man.exec stmt flow
(** Exit if status is non-zero *)
let error_error_at_line (p:expr) (n:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_error_at_line" flow in
let stmt = mk_c_call_stmt f [p; n] range in
man.exec stmt flow
let asprintf_stub (dst:expr) range man flow =
let f = find_c_fundec_by_name "_mopsa_asprintf" flow in
let exp = mk_c_call f [dst] range in
man.eval exp flow
let vasprintf_stub is_constant_string format (dst:expr) range man flow =
let f =
if is_constant_string then "_mopsa_constant_vasprintf"
else "_mopsa_general_vasprintf" in
let f = find_c_fundec_by_name f flow in
let exp = mk_c_call f (dst::format::[]) range in
man.eval exp flow
(** Stack variables *)
(** This vkind is used to attach the callstack to local variables *)
type var_kind += V_c_stack_var of callstack * var
(** Create a stack variable *)
let mk_stack_var cs v =
match vkind v with
| V_c_stack_var _ ->
v
| _ ->
let uniq_name = Format.asprintf "stack(%a, %s)" pp_callstack_short cs v.vname in
mkv uniq_name (V_c_stack_var (cs, v)) v.vtyp
let () = register_var {
print = (fun next fmt v ->
match vkind v with
| V_c_stack_var (cs, vv) -> pp_var fmt vv
| _ -> next fmt v
);
compare = (fun next v1 v2 ->
match vkind v1, vkind v2 with
| V_c_stack_var (cs1, vv1), V_c_stack_var (cs2, vv2) ->
Compare.pair compare_callstack compare_var
(cs1, vv1) (cs2, vv2)
| _ ->
next v1 v2
);
}
let rec var_scope v =
match v.vkind with
| V_cvar { cvar_scope } -> cvar_scope
| V_c_stack_var(_, vv) -> var_scope vv
| _ -> assert false