Source file l_partition.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
open Cil_types
open Ast_const
(**
Partitions the domain of some l-value (as a function) given its type
The l-value is specified through a function that must returns a fresh copy
each time (with unique expression ids).
[depth] is the maximal depth to go into the l-value and [width] is the maximal
width for array and structures explorations.
*)
let rec partition_lval ~depth ~width ~(emit: exp -> unit) typ lval =
Options.debug "partitioning l-value @[%a@] : @[%a@] (max_depth=%d)" Printer.pp_lval (lval ()) Printer.pp_typ typ depth;
if depth >= 0 then
match typ.tnode with
| TVoid | TFun _ ->
()
| TInt _ | TFloat _ | TPtr _ | TEnum _ ->
let exp () = Exp_builder.lval (lval ()) in
partition_exp ~depth ~width ~emit typ exp
| TNamed typ ->
partition_lval ~depth ~width ~emit typ.ttype lval
| TComp comp ->
let i = ref (-1) in
let onfield field =
incr i;
if !i < width then
let lval' () = Cil.addOffsetLval (Field (field, NoOffset)) (lval ()) in
partition_lval ~depth:(depth-1) ~width ~emit field.ftype lval'
in
begin match comp.cfields with
| Some cfields -> List.iter onfield cfields
| None -> ()
end
| TArray (typ, Some length) ->
begin match Cil.isInteger length with
| Some l ->
let l = if Integer.lt l (Integer.of_int width) then Integer.to_int_exn l else width in
for i = 0 to l-1 do
let lval' () = Cil.addOffsetLval (Index (Exp_builder.integer i, NoOffset)) (lval ()) in
partition_lval ~depth:(depth-1) ~width ~emit typ lval'
done
| _ ->
emit (Exp_builder.binop Eq (Cil.copy_exp length) (Exp_builder.zero ()));
for i = 0 to width-1 do
let emit' cond = emit (Exp_builder.binop LAnd (Exp_builder.binop Gt (Cil.copy_exp length)
(Exp_builder.integer i)) cond) in
let lval' () = Cil.addOffsetLval (Index (Exp_builder.integer i, NoOffset))
(lval ()) in
partition_lval ~depth:(depth-1) ~width ~emit:emit' typ lval'
done
end
| TArray (typ, None) ->
let lval' () = Cil.addOffsetLval (Index (Exp_builder.zero (), NoOffset)) (lval ()) in
partition_lval ~depth:(depth-1) ~width ~emit typ lval'
| TBuiltin_va_list ->
failwith "Builtin va list not supported by partition_lval"
(**
Partitions the domain of some expression (as a function) given its type.
See {!partition_lval} for details.
*)
and partition_exp ~depth ~width ~(emit : exp -> unit) typ exp =
Options.debug "partitioning expression @[%a@] : @[%a@] (max_depth=%d)" Printer.pp_exp (exp ()) Printer.pp_typ typ depth;
if depth >= 0 then
match typ.tnode with
| TInt (IUChar|IUShort|IUInt|IULong|IULongLong|IBool) ->
List.iter emit [
Exp_builder.binop Eq (exp ()) (Exp_builder.zero ());
Exp_builder.binop Ne (exp ()) (Exp_builder.zero ())
];
| TInt _ | TFloat _ ->
List.iter emit [
Exp_builder.binop Eq (exp ()) (Exp_builder.zero ());
Exp_builder.binop Gt (exp ()) (Exp_builder.zero ());
Exp_builder.binop Lt (exp ()) (Exp_builder.zero ())
]
| TPtr typ ->
emit (Exp_builder.binop Eq (exp ()) (Exp_builder.zero ()));
emit (Exp_builder.binop Ne (exp ()) (Exp_builder.zero ()));
let emit' cond = emit (Exp_builder.binop LAnd (Exp_builder.binop Ne (exp ()) (Exp_builder.zero ())) cond) in
let lval' () = Cil.mkMem ~addr:(exp ()) ~off:NoOffset in
partition_lval ~depth:(depth-1) ~width ~emit:emit' typ lval'
| TNamed ti ->
partition_exp ~depth ~width ~emit ti.ttype exp
| TEnum enum ->
let onitem item =
emit (Exp_builder.binop Eq (exp ()) (Cil.copy_exp item.eival));
in
List.iter onitem enum.eitems
| TVoid | TFun _ | TComp _ | TArray _ ->
assert false
| TBuiltin_va_list ->
failwith "Builtin va list not supported by partition_exp"
class inputDomainVisitor max_depth max_width all_funs globals mk_label = object (self)
inherit Visitor.frama_c_inplace
method gformals vars loc =
let acc = ref [] in
let emit exp =
if Cil.isIntegerConstant exp then
()
else
acc := mk_label exp [] loc :: !acc
in
let gen_for_var var =
partition_lval
~depth:max_depth ~width:max_width ~emit var.vtype (fun ()-> Cil.var var)
in
List.iter gen_for_var vars;
List.rev !acc
method! vfunc f =
if Annotators.shouldInstrumentFun f.svar &&
(all_funs || f.svar.vname = Kernel.MainFunction.get ()) then begin
let lbls = self#gformals (globals @ f.sformals) f.svar.vdecl in
f.sbody.bstmts <- lbls @ f.sbody.bstmts
end;
Cil.SkipChildren
end
module Partition = Annotators.Register (struct
let name = "IDP"
let help = "Input Domain Partition"
let apply mk_label ast =
let max_depth = Options.MaxDepth.get () in
let max_width = Options.MaxWidth.get () in
let all_funs = Options.AllFuns.get () in
let globals_as_input = Options.GlobalsAsInput.get () in
Options.debug "input domain partition (max depth %d, max width %d, all funs? %b, globals as input? %b)" max_depth max_width all_funs globals_as_input;
let globals = if globals_as_input then Utils.extract_global_vars ast else [] in
Visitor.visitFramacFileSameGlobals
(new inputDomainVisitor max_depth max_width all_funs globals mk_label :> Visitor.frama_c_inplace)
ast
end)