Source file senv.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
let solvers =
let open Formula_options in
[ Bitwuzla; Boolector; Z3; CVC4; Yices ]
let map =
let open Formula_options in
let open Smt_options in
function
| Auto | Bitwuzla_builtin | Bitwuzla_legacy | Z3_builtin -> assert false
| Bitwuzla_smtlib -> Bitwuzla
| Boolector_smtlib -> Boolector
| Z3_smtlib -> Z3
| CVC4_smtlib -> CVC4
| Yices_smtlib -> Yices
let get_solver : ?solver:Smt.Smt_options.solver -> unit -> (module Solver.OPEN)
=
fun ?(solver = Smt_options.SMTSolver.get ()) () ->
let module Logger = Smt_options.Logger in
match solver with
| (Auto | Bitwuzla_builtin) when Option.is_some Libsolver.bitwuzla_cxx ->
Smt_options.Logger.debug "Use native Bitwuzla binding (cxx).";
let module Api = Api_solver.Make ((val Option.get Libsolver.bitwuzla_cxx)) in
(module Api.Open)
| (Auto | Bitwuzla_builtin | Bitwuzla_legacy)
when Option.is_some Libsolver.bitwuzla_c ->
Smt_options.Logger.debug "Use native Bitwuzla binding (c).";
let module Api = Api_solver.Make ((val Option.get Libsolver.bitwuzla_c)) in
(module Api.Open)
| (Auto | Z3_builtin) when Option.is_some Libsolver.z3 ->
Smt_options.Logger.debug "Use native z3 binding.";
let module Api = Api_solver.SafeArray ((val Option.get Libsolver.z3)) in
(module Api.Open)
| Auto -> (
try
let solver = List.find Prover.ping solvers in
Smt_options.Logger.info "Found %a in the path." Prover.pp solver;
Formula_options.Solver.set solver;
(module Smt2_solver.Solver)
with Not_found -> Logger.fatal "No SMT solver found.")
| Bitwuzla_builtin | Bitwuzla_legacy ->
Smt_options.Logger.fatal
"Native bitwuzla binding is required but not available."
| Z3_builtin ->
Smt_options.Logger.fatal
"Native z3 binding is required but not available."
| solver when Prover.ping (map solver) ->
Smt_options.Logger.debug "Found %a in the path." Prover.pp (map solver);
Formula_options.Solver.set (map solver);
(module Smt2_solver.Solver)
| solver ->
Smt_options.Logger.fatal "%a is required but not available in path."
Prover.pp (map solver)
exception Undef = Types.Undef
exception Uninterp = Types.Uninterp
exception Unknown = Types.Unknown
exception Non_unique = Types.Non_unique
exception Non_mergeable = Types.Non_mergeable
type 'a test = 'a Types.test =
| True of 'a
| False of 'a
| Both of { t : 'a; f : 'a }
module BiMap = Basic_types.BigInt.Map
module NiTbl = Basic_types.Int.Htbl
open Sexpr
module BiItM = Imap
module S = Basic_types.String.Map
module I = Basic_types.Int.Map
module R = Basic_types.Int.Htbl
module K = Basic_types.Int.Set
type _ Types.value += Term : Sexpr.Expr.t Types.value
type lazy_memory = Solver.lazy_memory
type 'a Types.feature +=
| VisibleSymbols : Expr.t Dba_types.Var.Map.t Types.feature
| VisibleMemory : Memory.t Types.feature
module State
(D : Domains.S)
(Solver : Solver.GET_MODEL_WITH_STATS)
(QS : Types.QUERY_STATISTICS) =
struct
module Uid = struct
type t = Suid.t
let zero = Suid.incr Suid.zero
let succ = Suid.incr
let compare = Suid.compare
end
module Solver = Solver (QS)
let timeout =
match Formula_options.Solver.Timeout.get () with
| 0 -> None
| n -> Some (float_of_int n)
type t = {
constraints : Expr.t list;
clauses : int;
mutable deps : BvSet.t BvMap.t;
mutable domains : D.t BvMap.t;
mutable anchors : K.t;
vsymbols : Expr.t I.t;
varrays : Memory.t S.t;
vmemory : Memory.t;
lmem : lazy_memory;
model : Model.t;
}
module C : Ai.CONTEXT with type t = t and type v := D.t = struct
type nonrec t = t
let add_dependency t ~parent e =
t.deps <-
BvMap.add e
(BvSet.add parent
(try BvMap.find e t.deps with Not_found -> BvSet.empty))
t.deps
let find_dependency t e = BvMap.find e t.deps
let add t e v = t.domains <- BvMap.add e v t.domains
let find t e = BvMap.find e t.domains
end
module Overapprox : Memory_manager.CONTEXT with type t = t and type v := D.t =
struct
include Ai.Make (D) (C)
let anchor t (m : Memory.t) =
match m with
| Root | Symbol _ -> ()
| Layer { id; _ } -> t.anchors <- K.add id t.anchors
let anchored t (m : Memory.t) =
match m with
| Root | Symbol _ -> true
| Layer { id; _ } -> K.mem id t.anchors
end
module MMU = Memory_manager.Make (D) (Overapprox)
let pp ppf state = Model.pp ppf state.model
let empty () =
let addr_space = Kernel_options.Machine.word_size () in
{
constraints = [];
clauses = 0;
deps = BvMap.empty;
domains = BvMap.empty;
anchors = K.empty;
vsymbols = I.empty;
varrays = S.empty;
vmemory = Memory.root;
lmem = { content = BiItM.empty; lemmas = []; addr_space };
model = Model.empty addr_space;
}
let alloc ~array state =
let symbol = Memory.fresh array in
{ state with varrays = S.add array symbol state.varrays }
let assign ({ id; _ } : Types.Var.t) value state =
{ state with vsymbols = I.add id value state.vsymbols }
let write ~addr value dir state =
let vmemory = MMU.write state ~addr value dir state.vmemory in
{ state with vmemory }
let store name ~addr value dir state =
try
let ar = S.find name state.varrays in
let varrays =
S.add name (MMU.write state ~addr value dir ar) state.varrays
in
{ state with varrays }
with Not_found -> raise_notrace (Uninterp name)
let lookup ({ id; _ } as var : Types.Var.t) t =
try I.find id t.vsymbols with Not_found -> raise_notrace (Undef var)
let read ~addr bytes dir state =
let bytes = MMU.read state ~addr bytes dir state.vmemory in
(bytes, state)
let select name ~addr bytes dir state =
try
let array = S.find name state.varrays in
let bytes = MMU.read state ~addr bytes dir array in
(bytes, state)
with Not_found -> raise_notrace (Uninterp name)
let memcpy ~addr len orig state =
let base = Bv.value_of addr in
let lmem =
{
state.lmem with
content =
BiItM.add ~base len (Bv.value_of addr, orig) state.lmem.content;
}
in
let vmemory =
MMU.source state ~addr:(Expr.constant addr) ~len orig state.vmemory
in
{ state with lmem; vmemory }
let assume cond state =
if Expr.is_equal cond Expr.one then (
QS.Preprocess.incr_true ();
Some state)
else if Expr.is_equal cond Expr.zero then (
QS.Preprocess.incr_false ();
None)
else
match D.is_zero (Overapprox.eval state cond) with
| True ->
QS.Preprocess.incr_false ();
None
| False ->
QS.Preprocess.incr_true ();
Some state
| Unknown ->
let constraints = cond :: state.constraints
and clauses = state.clauses + 1 in
if Bitvector.zero = Model.eval state.model cond then (
QS.Solver.start_timer ();
let r = Solver.check_sat ?timeout state.lmem constraints in
QS.Solver.stop_timer ();
match r with
| Unknown -> raise Unknown
| Unsat -> None
| Sat model ->
let state = { state with constraints; clauses; model } in
Overapprox.refine state cond D.one;
Some state)
else (
QS.Preprocess.incr_true ();
let state = { state with constraints; clauses } in
Overapprox.refine state cond D.one;
Some state)
let test cond state =
if Expr.is_equal cond Expr.one then (
QS.Preprocess.incr_true ();
True state)
else if Expr.is_equal cond Expr.zero then (
QS.Preprocess.incr_false ();
False state)
else
match D.is_zero (Overapprox.eval state cond) with
| True ->
QS.Preprocess.incr_false ();
False state
| False ->
QS.Preprocess.incr_true ();
True state
| Unknown -> (
let tcons = cond :: state.constraints
and fcons = Expr.lognot cond :: state.constraints
and clauses = state.clauses + 1 in
let e = Model.eval state.model cond in
let to_check, constraints =
if Bv.is_zero e then (tcons, fcons) else (fcons, tcons)
in
QS.Solver.start_timer ();
let r = Solver.check_sat ?timeout state.lmem to_check in
QS.Solver.stop_timer ();
match r with
| Unknown -> raise Unknown
| Unsat ->
if Bv.is_zero e then (
let state = { state with constraints; clauses } in
Overapprox.refine state cond D.zero;
False state)
else
let state = { state with constraints; clauses } in
Overapprox.refine state cond D.one;
True state
| Sat model ->
let t, f =
if Bv.is_zero e then
( { state with constraints = to_check; clauses; model },
{ state with constraints; clauses } )
else
( { state with constraints; clauses },
{ state with constraints = to_check; clauses; model } )
in
Overapprox.refine t cond D.one;
Overapprox.refine f cond D.zero;
Both { t; f })
let enumerate =
let with_solver state e n enum except =
QS.Solver.start_timer ();
match
Solver.fold_values ?timeout state.lmem state.constraints e ~n ~except
(fun bv model enum ->
let cond = Expr.equal e (Expr.constant bv) in
let state =
{
state with
constraints = cond :: state.constraints;
clauses = state.clauses + 1;
model;
}
in
ignore (Overapprox.eval state cond);
Overapprox.refine state cond D.one;
(bv, state) :: enum)
enum
with
| exception Unknown ->
QS.Solver.stop_timer ();
raise Unknown
| enum ->
QS.Solver.stop_timer ();
enum
in
fun e ?(n = 1) ?(except = []) state ->
match e with
| Expr.Cst bv when List.mem bv except = false ->
QS.Preprocess.incr_const ();
[ (bv, state) ]
| Expr.Cst _ ->
QS.Preprocess.incr_const ();
[]
| _ -> (
let bv = Model.eval state.model e in
let n, enum, except =
if List.mem bv except then (n, [], except)
else (
QS.Preprocess.incr_const ();
let cond = Expr.equal e (Expr.constant bv) in
let state =
{
state with
constraints = cond :: state.constraints;
clauses = state.clauses + 1;
}
in
ignore (Overapprox.eval state cond);
Overapprox.refine state cond D.one;
(n - 1, [ (bv, state) ], bv :: except))
in
if n = 0 then enum
else
let size = Expr.sizeof e and d = Overapprox.eval state e in
match D.project ~size d with
| Point _ -> enum
| Top | Seq _ -> with_solver state e n enum except)
let rec zip c0 n0 c1 n1 c0' c1' =
if n0 < n1 then
zip c0 n0 (List.tl c1) (n1 - 1) c0' (Expr.logand c1' (List.hd c1))
else if n1 < n0 then
zip (List.tl c0) (n0 - 1) c1 n1 (Expr.logand c0' (List.hd c0)) c1'
else if c0 == c1 then (c0', c1', c0, n0)
else
zip (List.tl c0) (n0 - 1) (List.tl c1) (n1 - 1)
(Expr.logand c0' (List.hd c0))
(Expr.logand c1' (List.hd c1))
let zip c0 n0 c1 n1 =
let c0', c1', common, n = zip c0 n0 c1 n1 Expr.one Expr.one in
(c0', c1', common, n)
let merge ~parent:_ t t' =
if t == t' then t
else if t.lmem == t'.lmem then (
let c, c', common, n =
zip t.constraints t.clauses t'.constraints t'.clauses
in
let cu = Expr.logor c c' in
let constraints = cu :: common
and clauses = n + 1
and domains =
BvMap.merge
(fun e o0 o1 ->
match (o0, o1) with
| None, None -> assert false
| Some d0, Some d1 -> Some (D.union ~size:(Expr.sizeof e) d0 d1)
| (Some _ | None), (Some _ | None) -> Some (D.top (Expr.sizeof e)))
t.domains t'.domains
and anchors = K.union t.anchors t'.anchors
and deps =
BvMap.merge
(fun _ o o' ->
match (o, o') with
| None, None -> assert false
| None, Some _ -> o'
| Some _, None -> o
| Some d, Some d' -> Some (BvSet.union d d'))
t.deps t'.deps
and vsymbols =
if t.vsymbols == t'.vsymbols then t.vsymbols
else
I.merge
(fun _ o0 o1 ->
match (o0, o1) with
| None, None -> assert false
| Some e0, Some e1 ->
if Expr.is_equal e0 e1 then o0 else Some (Expr.ite c' e1 e0)
| Some _, None -> o0
| None, Some _ -> o1)
t.vsymbols t'.vsymbols
and lmem = t.lmem
and model = t.model in
let t'' =
{
t with
constraints;
clauses;
deps;
domains;
anchors;
vsymbols;
lmem;
model;
}
in
ignore (Overapprox.eval t'' cu);
let varrays =
if t.varrays == t'.varrays then t.varrays
else
S.merge
(fun _ o0 o1 ->
match (o0, o1) with
| Some a0, Some a1 -> Some (MMU.merge t'' c' a1 a0)
| (Some _ | None), (Some _ | None) -> raise_notrace Non_mergeable)
t.varrays t'.varrays
and vmemory = MMU.merge t'' c' t'.vmemory t.vmemory in
Overapprox.refine t'' cu D.one;
{ t'' with varrays; vmemory })
else raise_notrace Non_mergeable
module Value = struct
type t = Expr.t
let kind = Term
let constant = Expr.constant
let var id name size = Expr.var (name ^ Suid.to_string id) size name
let unary = Expr.unary
let binary = Expr.binary
let ite = Expr.ite
let is_symbolic : t -> bool = function Cst _ -> true | _ -> false
let is_zero : t -> Types.trilean = function
| Cst bv -> if Bv.is_zeros bv then True else False
| _ -> Unknown
end
let assertions t = t.constraints
let get_value (e : Expr.t) _ =
match e with Cst bv -> bv | _ -> raise_notrace Non_unique
let get_a_value (e : Expr.t) t = Model.eval t.model e
let pp_smt (target : Expr.t Types.target) ppf t =
let module P = Smt2_solver.Printer in
let ctx = P.create ~next_id:Uid.zero () in
List.iter (P.visit_bl ctx) t.constraints;
let defs =
match target with
| Some defs ->
List.iter (fun (e, _) -> P.visit_bv ctx e) defs;
defs
| None ->
P.visit_ax ctx t.vmemory;
List.rev
(I.fold
(fun id expr defs ->
match Dba.Var.from_id id with
| exception Not_found -> defs
| { name; _ } ->
P.visit_bv ctx expr;
(expr, name) :: defs)
t.vsymbols [])
in
Format.pp_open_vbox ppf 0;
P.pp_print_defs ppf ctx;
List.iter
(fun (bv, name) ->
Format.fprintf ppf "@[<h>(define-fun %s () (_ BitVec %d)@ " name
(Expr.sizeof bv);
P.pp_print_bv ctx ppf bv;
Format.fprintf ppf ")@]@ ")
defs;
if target = None then
Format.fprintf ppf
"@[<h>(define-fun memory () (Array (_ BitVec %d) (_ BitVec 8))@ %a)@]"
(Kernel_options.Machine.word_size ())
(P.pp_print_ax ctx) t.vmemory;
List.iter
(fun bl ->
Format.pp_open_hbox ppf ();
Format.pp_print_string ppf "(assert ";
P.pp_print_bl ctx ppf bl;
Format.pp_print_char ppf ')';
Format.pp_close_box ppf ();
Format.pp_print_space ppf ())
t.constraints;
Format.pp_close_box ppf ()
let to_formula t =
let module C = Smt2_solver.Cross in
let ctx = C.create ~next_id:Uid.zero () in
List.iter (C.assert_bl ctx) t.constraints;
C.define_ax ctx "memory" t.vmemory;
I.iter
(fun id expr ->
match Dba.Var.from_id id with
| exception Not_found -> ()
| { name; _ } -> C.define_bv ctx name expr)
t.vsymbols;
C.to_formula ctx
let getter : type a. a Types.feature -> (t -> a) option = function
| VisibleSymbols ->
Some
(fun state ->
I.fold
(fun id expr map ->
match Dba.Var.from_id id with
| exception Not_found -> map
| var -> Dba_types.Var.Map.add var expr map)
state.vsymbols Dba_types.Var.Map.empty)
| VisibleMemory -> Some (fun state -> state.vmemory)
| _ -> None
let setter _ = None
end
type Options.Engine.t += Vanilla | Multi_checks
let () =
Options.Engine.register "vanilla" Vanilla (fun () ->
let module Solver = Solver.Once ((val get_solver ())) in
(module State (Domains.Interval) (Solver)))
let () =
Options.Engine.register "multi-checks" Multi_checks (fun () ->
let module Solver = Solver.MultiChecks ((val get_solver ())) in
(module State (Domains.Interval) (Solver)))