Source file datalog_code.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
open Common
type var = string
type func = string
type fld = string
type heap = string
type callsite = string
type fact =
| PointTo of var * heap
| Assign of var * var
| AssignContent of var * var
| AssignAddress of var * var
| AssignDeref of var * var
| AssignLoadField of var * var * fld
| AssignStoreField of var * fld * var
| AssignFieldAddress of var * var * fld
| AssignArrayElt of var * var
| AssignArrayDeref of var * var
| AssignArrayElementAddress of var * var
| Parameter of func * int * var
| Return of func * var
| Argument of callsite * int * var
| ReturnValue of callsite * var
| CallDirect of callsite * func
| CallIndirect of callsite * var
type value =
| V of var
| F of fld
| N of func
| I of callsite
| Z of int
let string_of_value = function
| V x | F x | N x | I x -> x
| Z _ -> raise Impossible
type _rule = string
type _meta_fact =
string * value list
let meta_fact = function
| PointTo (a, b) -> "point_to", [ V a; V b; ]
| Assign (a, b) -> "assign", [ V a; V b; ]
| AssignContent (a, b) -> "assign_content", [ V a; V b; ]
| AssignAddress (a, b) -> "assign_address", [ V a; V b; ]
| AssignDeref (a, b) -> "assign_deref", [ V a; V b; ]
| AssignLoadField (a, b, c) -> "assign_load_field", [ V a; V b; F c ]
| AssignStoreField (a, b, c) -> "assign_store_field", [ V a; F b; V c ]
| AssignFieldAddress (a, b, c) -> "assign_field_address", [ V a; V b; F c ]
| AssignArrayElt (a, b) -> "assign_array_elt", [ V a; V b; ]
| AssignArrayDeref (a, b) -> "assign_array_deref", [ V a; V b; ]
| AssignArrayElementAddress (a, b) -> "assign_array_element_address", [ V a; V b; ]
| Parameter (a, b, c) -> "parameter", [ N a; Z b; V c ]
| Return (a, b) -> "return", [ N a; V b; ]
| Argument (a, b, c) -> "argument", [ I a; Z b; V c ]
| ReturnValue (a, b) -> "call_ret", [ I a; V b; ]
| CallDirect (a, b) -> "call_direct", [ I a; N b; ]
| CallIndirect (a, b) -> "call_indirect", [ I a; V b; ]
let string_of_fact fact =
let str, xs = meta_fact fact in
spf "%s(%s)" str
(xs +> List.map (function
| V x | F x | N x | I x -> spf "'%s'" x
| Z i -> spf "%d" i
) +> Common.join ", "
)
type _domain = string
let domain_of_value = function
| V _ -> "V"
| F _ -> "F"
| N _ -> "N"
| I _ -> "I"
| Z _ -> "Z"
type _idx = (string , value Common.hashset) Hashtbl.t
let bddbddb_of_facts facts dir =
let metas = facts +> List.map meta_fact in
let hvalues = Hashtbl.create 6 in
let hrules = Hashtbl.create 30 in
metas +> List.iter (fun (arule, xs) ->
let listref =
try Hashtbl.find hrules arule
with Not_found ->
let aref = ref [] in
Hashtbl.add hrules arule aref;
aref
in
listref := xs :: !listref;
xs +> List.iter (fun v ->
let add_v v =
let domain = domain_of_value v in
let hdomain =
try Hashtbl.find hvalues domain
with Not_found ->
let h = Hashtbl.create 10001 in
Hashtbl.add hvalues domain h;
h
in
Hashtbl.replace hdomain v true;
in
add_v v;
(match v with
| F s -> add_v (V s)
| N s -> add_v (V s)
| _ -> ()
)
)
);
let domains_idx =
hvalues +> Common.hash_to_list +> List.map (fun (domain, hdomain) ->
let conv = hdomain +> Common.hashset_to_list +> Common.index_list_0 in
domain, (
conv, conv +> Common.hash_of_list
)
)
in
Common.command2 (spf "rm -f %s/*" dir);
domains_idx +> List.iter (fun (domain, (map, _idx)) ->
if domain <> "Z"
then begin
let file = Filename.concat dir (domain ^ ".map") in
Common.with_open_outfile file (fun (pr_no_nl, _chan) ->
let pr s = pr_no_nl (s ^ "\n") in
map +> List.iter (fun (v, _int) ->
pr (string_of_value v)
)
)
end
);
hrules +> Common.hash_to_list +> List.iter (fun (arule, xxs) ->
let arule =
match arule with
| "point_to" -> "point_to0"
| "assign" -> "assign0"
| s -> s
in
let file = Filename.concat dir (arule ^ ".tuples") in
Common.with_open_outfile file (fun (pr_no_nl, _chan) ->
let pr s = pr_no_nl (s ^ "\n") in
(match !xxs with
| [] -> ()
| xs::_xxs ->
let hcnt = Hashtbl.create 6 in
pr (spf "# %s"
(xs +> List.map (fun v ->
let domain = domain_of_value v in
let cnt =
try Hashtbl.find hcnt domain
with Not_found ->
let cnt = ref 0 in
Hashtbl.add hcnt domain cnt;
cnt
in
let i = !cnt in
incr cnt;
spf "%s%d:18" domain i
) +> Common.join " "))
);
!xxs +> List.iter (fun xs ->
let ints =
xs +> List.map (fun v ->
let i =
match v with
| Z i -> i
| _ ->
let domain = domain_of_value v in
let (_, hdomainconv) = List.assoc domain domains_idx in
Hashtbl.find hdomainconv v
in
i
)
in
pr (ints +> List.map i_to_s +> Common.join " ")
);
)
);
let fvals = try List.assoc "F" domains_idx +> fst with Not_found -> [] in
let nvals = try List.assoc "N" domains_idx +> fst with Not_found -> [] in
let (_vvals, vconv) = List.assoc "V" domains_idx in
let arule = "field_to_var" in
let file = Filename.concat dir (arule ^ ".tuples") in
Common.with_open_outfile file (fun (pr_no_nl, _chan) ->
let pr s = pr_no_nl (s ^ "\n") in
pr "# F0:18 V0:18";
fvals +> List.iter (fun (fld, idx) ->
match fld with
| F s ->
let v = V s in
let idx2 = Hashtbl.find vconv v in
pr (spf "%d %d" idx idx2)
| _ ->
pr2_gen (fld, idx);
raise Impossible
)
);
let arule = "var_to_func" in
let file = Filename.concat dir (arule ^ ".tuples") in
Common.with_open_outfile file (fun (pr_no_nl, _chan) ->
let pr s = pr_no_nl (s ^ "\n") in
pr "# V0:18 N0:18";
nvals +> List.iter (fun (n, idx) ->
match n with
| N s ->
let v = V s in
let idx2 = Hashtbl.find vconv v in
pr (spf "%d %d" idx2 idx)
| _ ->
pr2_gen (n, idx);
raise Impossible
)
);
()
let bddbddb_explain_tuples file =
let (d,b,_e) = Common2.dbe_of_filename file in
let dst = Common2.filename_of_dbe (d,b,"explain") in
Common.with_open_outfile dst (fun (pr_no_nl, _chan) ->
let pr s = pr_no_nl (s ^ "\n") in
let xs = Common.cat file in
(match xs with
| header::xs ->
if header =~ "# \\(.*\\)"
then
let s = Common.matched1 header in
let flds = Common.split "[ \t]" s in
let fld_domains =
flds +> List.map (fun s ->
if s =~ "\\([A-Z]\\)[0-9]?:"
then Common.matched1 s
else failwith (spf "could not find header in %s" file)
)
in
let fld_translates =
fld_domains +> List.map (fun s ->
let mapfile = Common2.filename_of_dbe (d,s,"map") in
Common.cat mapfile +> Array.of_list
)
in
xs +> List.iter (fun s ->
let vs = Common.split "[ \t]" s +> List.map s_to_i in
let args =
Common2.zip vs fld_translates +> List.map (fun (i, arr) ->
arr.(i)
)
in
pr (spf "%s(%s)" b (Common.join ", " args))
)
else failwith (spf "could not find header in %s" file)
| [] -> pr2 (spf "empty file %s" file)
)
);
dst