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
(** Various simple/old solvers and solver utilities. *)
open Batteries
open GobConfig
open Goblint_constraint.ConstrSys
open Goblint_constraint.SolverTypes
module LoadRunSolver: GenericEqSolver =
functor (S: EqConstrSys) (VH: Hashtbl.S with type key = S.v) ->
struct
let solve xs vs =
let solver_file = "solver.marshalled" in
let load_run = Fpath.v (get_string "load_run") in
let solver = Fpath.(load_run / solver_file) in
Logs.debug "Loading the solver result of a saved run from %s" (Fpath.to_string solver);
let vh: S.d VH.t = Serialize.unmarshal solver in
if get_bool "ana.opt.hashcons" then (
let vh' = VH.create (VH.length vh) in
VH.iter (fun x d ->
let x' = S.Var.relift x in
let d' = S.Dom.relift d in
VH.replace vh' x' d'
) vh;
vh'
)
else
vh
end
module LoadRunIncrSolver: DemandEqIncrSolver =
PostSolver.DemandEqIncrSolverFromEqSolver (LoadRunSolver)
module SolverStats (S:EqConstrSys) (HM:Hashtbl.S with type key = S.v) =
struct
open S
open Messages
let stack_d = ref 0
let full_trace = false
let start_c = 0
let max_c : int ref = ref (-1)
let max_var : Var.t option ref = ref None
let histo = HM.create 1024
let increase (v:Var.t) =
let set v c =
if not full_trace && (c > start_c && c > !max_c && not (GobOption.exists (Var.equal v) !max_var)) then begin
if tracing then trace "sol" "Switched tracing to %a" Var.pretty_trace v;
max_c := c;
max_var := Some v
end
in
try let c = HM.find histo v in
set v (c+1);
HM.replace histo v (c+1)
with Not_found -> begin
set v 1;
HM.add histo v 1
end
let start_event () = ()
let stop_event () = ()
let new_var_event x =
incr SolverStats.vars;
if tracing then trace "sol" "New %a" Var.pretty_trace x
let get_var_event x =
if tracing && full_trace then trace "sol" "Querying %a" Var.pretty_trace x
let eval_rhs_event x =
if tracing && full_trace then trace "sol" "(Re-)evaluating %a" Var.pretty_trace x;
incr SolverStats.evals;
if (get_bool "dbg.solver-progress") then (incr stack_d; Logs.debug "%d" !stack_d)
let update_var_event x o n =
if tracing then increase x;
if full_trace || (not (Dom.is_bot o) && GobOption.exists (Var.equal x) !max_var) then begin
if tracing then tracei "sol_max" "(%d) Update to %a" !max_c Var.pretty_trace x;
if tracing then traceu "sol_max" "%a" Dom.pretty_diff (n, o)
end
let print_solver_stats = ref (fun () -> ())
let ncontexts = ref 0
let print_context_stats rho =
let histo = Hashtbl.create 13 in
let str k = GobPretty.sprint S.Var.pretty_trace k in
let is_fun k = match S.Var.node k with FunctionEntry _ -> true | _ -> false in
HM.iter (fun k _ -> if is_fun k then Hashtbl.modify_def 0 (str k) ((+)1) histo) rho;
ncontexts := Hashtbl.fold (fun _ -> (+)) histo 0;
let topn = 5 in
Logs.debug "Found %d contexts for %d functions. Top %d functions:" !ncontexts (Hashtbl.length histo) topn;
Hashtbl.to_list histo
|> List.sort (fun (_,n1) (_,n2) -> compare n2 n1)
|> List.take topn
|> List.iter @@ fun (k,n) -> Logs.debug "%d\tcontexts for %s" n k
let stats_csv =
let save_run_str = GobConfig.get_string "save_run" in
if save_run_str <> "" then (
let save_run = Fpath.v save_run_str in
GobSys.mkdir_or_exists save_run;
Fpath.(to_string (save_run / "solver_stats.csv")) |> open_out |> Option.some
) else None
let write_csv xs oc = output_string oc @@ String.concat ",\t" xs ^ "\n"
let print_stats _ =
Logs.newline ();
Logs.info "runtime: %s" (GobSys.string_of_time ());
Logs.info "vars: %d, evals: %d" !SolverStats.vars !SolverStats.evals;
Option.may (fun v -> ignore @@ Logs.info "max updates: %d for var %a" !max_c Var.pretty_trace v) !max_var;
Logs.newline ();
!print_solver_stats ();
Logs.newline ();
let gc = GobGc.print_quick_stat Legacy.stderr in
Logs.newline ();
Option.may (write_csv [GobSys.string_of_time (); string_of_int !SolverStats.vars; string_of_int !SolverStats.evals; string_of_int !ncontexts; string_of_int gc.Gc.top_heap_words]) stats_csv
let () =
let = write_csv ["runtime"; "vars"; "evals"; "contexts"; "max_heap"] in
Option.may write_header stats_csv;
Sys.set_signal (GobSys.signal_of_string (get_string "dbg.solver-signal")) (Signal_handle print_stats);
Sys.set_signal Sys.sigvtalrm (Signal_handle print_stats);
let ssi = get_int "dbg.solver-stats-interval" in
if ssi > 0 then
let it = float_of_int ssi in
ignore Unix.(setitimer ITIMER_VIRTUAL { it_interval = it; it_value = it });
end
(** use this if your [box] is [join] --- the simple solver *)
module DirtyBoxSolver : GenericEqSolver =
functor (S:EqConstrSys) ->
functor (H:Hashtbl.S with type key = S.v) ->
struct
open SolverBox.Warrow (S.Dom)
include SolverStats (S) (H)
let h_find_default h x d =
try H.find h x
with Not_found -> d
let solve xs vs =
let stbl = H.create 1024 in
let infl = H.create 1024 in
let sol = H.create 1024 in
let rec solve_one x =
if not (H.mem stbl x) then begin
if not (H.mem sol x) then (new_var_event x; H.add sol x (S.Dom.bot ()));
H.replace stbl x ();
eval_rhs_event x;
Option.may (fun f -> set x (f (eval x) set)) (S.system x)
end
and eval x y =
get_var_event y;
solve_one y;
H.replace infl y (x :: h_find_default infl y []);
H.find sol y
and set x d =
if not (H.mem sol x) then solve_one x;
let oldd = H.find sol x in
let newd = box oldd d in
update_var_event x oldd newd;
if not (S.Dom.equal oldd newd) then begin
H.replace sol x newd;
let deps = h_find_default infl x [] in
List.iter (H.remove stbl) deps;
H.remove infl x;
solve_all deps
end
and solve_all xs =
List.iter solve_one xs
in
start_event ();
List.iter (fun (k,v) -> set k v) xs;
solve_all vs;
stop_event ();
sol
end
module SoundBoxSolverImpl =
functor (S:EqConstrSys) ->
functor (H:Hashtbl.S with type key = S.v) ->
struct
open SolverBox.Warrow (S.Dom)
include SolverStats (S) (H)
let h_find_default h x d =
try H.find h x
with Not_found -> d
let solveWithStart (ht,hts) xs vs =
let stbl = H.create 1024 in
let infl = H.create 1024 in
let sol = ht in
let sols = hts in
let called = H.create 1024 in
let rec solve_one x =
if not (H.mem stbl x) then begin
if not (H.mem sol x) then (new_var_event x; H.add sol x (S.Dom.bot ()));
H.replace stbl x ();
H.replace called x ();
eval_rhs_event x;
let set_x d = if H.mem called x then set x d in
Option.may (fun f -> set_x (f (eval x) side)) (S.system x);
H.remove called x
end
and eval x y =
get_var_event y;
solve_one y;
H.replace infl y (x :: h_find_default infl y []);
H.find sol y
and side x d =
let nd = S.Dom.join d (h_find_default sols x (S.Dom.bot ())) in
H.replace sols x nd;
set x nd
and set x d =
if not (H.mem sol x) then solve_one x;
let oldd = H.find sol x in
let newd = box oldd (S.Dom.join d (h_find_default sols x (S.Dom.bot ()))) in
if not (S.Dom.equal oldd newd) then begin
update_var_event x oldd newd;
H.replace sol x newd;
let deps = h_find_default infl x [] in
List.iter (H.remove stbl) deps;
H.remove infl x;
H.replace infl x [x];
if Messages.tracing && full_trace
then Messages.trace "sol" "Need to review %d deps." (List.length deps);
solve_all deps
end
and solve_all xs =
List.iter solve_one xs
in
start_event ();
List.iter (fun (k,v) -> side k v) xs;
solve_all vs; stop_event ();
sol, sols
(** the solve function *)
let solve xs ys = solveWithStart (H.create 1024, H.create 1024) xs ys |> fst
end
module SoundBoxSolver : GenericEqSolver = SoundBoxSolverImpl
module PreciseSideEffectBoxSolver : GenericEqSolver =
functor (S:EqConstrSys) ->
functor (H:Hashtbl.S with type key = S.v) ->
struct
open SolverBox.Warrow (S.Dom)
include SolverStats (S) (H)
let h_find_default h x d =
try H.find h x
with Not_found -> d
module VM = Map.Make (S.Var)
module VS = Set.Make (S.Var)
let solve xs vs =
let stbl = H.create 1024 in
let infl = H.create 1024 in
let sol = H.create 1024 in
let sols = H.create 1024 in
let sdeps = H.create 1024 in
let called = H.create 1024 in
let rec solve_one x =
if not (H.mem stbl x) then begin
if not (H.mem sol x) then (new_var_event x; H.add sol x (S.Dom.bot ()));
H.replace stbl x ();
H.replace called x ();
H.remove sols x;
eval_rhs_event x;
Option.may (fun f -> set x (f (eval x) (side x))) (S.system x);
H.remove called x
end
and eval x y =
get_var_event y;
solve_one y;
H.replace infl y (x :: h_find_default infl y []);
H.find sol y
and side y x d =
H.replace sdeps x (VS.add y (h_find_default sdeps x VS.empty));
let om = h_find_default sols y VM.empty in
let nm = VM.modify_def (S.Dom.bot ()) x (S.Dom.join d) om in
H.replace sols y nm;
set x d
and set x d =
if not (H.mem sol x) then solve_one x;
let oldd = H.find sol x in
let find_join_sides z d =
try S.Dom.join d (VM.find x (H.find sols z))
with Not_found -> d
in
let newd = box oldd (VS.fold find_join_sides (h_find_default sdeps x VS.empty) d) in
update_var_event x oldd newd;
if not (S.Dom.equal oldd newd) then begin
H.replace sol x newd;
let deps = h_find_default infl x [] in
List.iter (H.remove stbl) deps;
H.remove infl x;
solve_all deps
end
and solve_all xs =
List.iter solve_one xs
in
start_event ();
List.iter (fun (k,v) -> side k k v) xs;
solve_all vs;
stop_event ();
sol
end