forked from chaosite/MeGASampler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc_metric.py
More file actions
executable file
·560 lines (466 loc) · 16.8 KB
/
calc_metric.py
File metadata and controls
executable file
·560 lines (466 loc) · 16.8 KB
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
#!/home/mok/work_space/MeGASampler_Z3/venv/bin python3
from __future__ import annotations
import abc
import argparse
import itertools
import functools
import fractions
import operator
import sys
import typing as typ
import collections
import z3
import sys
sys.setrecursionlimit(20000000)
PARSER = argparse.ArgumentParser(
description="Calculate coverage from samples file and smt2 file"
)
PARSER.add_argument(
"-s",
"--samples",
metavar="FILE",
type=open,
required=True,
help="File to load samples from",
)
PARSER.add_argument(
"-f",
"--formula",
metavar="FILE",
type=open,
required=True,
help="Formula file (smt2)",
)
PARSER.add_argument(
"--use-c-api",
action="store_true",
help="Use Z3 C API calls to load samples, if applicable",
)
PARSER.add_argument(
"-m", "--metric", required=True, choices=["satisfies", "wire_coverage"]
)
PARSER.add_argument(
"-l",
"--limit",
metavar="LIMIT",
type=int,
default=0,
help="Limit number of samples proccessed (0 for no limit)",
)
PARSER.add_argument(
"-p", "--print-first", action="store_true", help="Print the first satisfying sample"
)
CTX = z3.Context()
def prod(*args):
if len(args) == 1:
args = args[0]
return functools.reduce(operator.mul, args, 1)
class Metric(abc.ABC):
def __init__(self, formula: str):
self._solver = z3.Solver(ctx=CTX)
self._total = 0
self._satisfies = 0
self._solver.from_string(formula)
@abc.abstractmethod
def count_sample(self, sample: list[tuple[str, int]]) -> bool:
pass
@property
def result(self) -> fractions.Fraction:
return fractions.Fraction(self._satisfies, self._total)
class SatisfiesMetric(Metric):
def __init__(self, formula: str, use_c_api: bool = False):
super().__init__(formula)
self._intsort = z3.IntSort(ctx=CTX)
self._use_c_api = use_c_api
# self._vars = {str(var): var for var in z3util.get_vars(g.as_expr())}
def _add_sample_via_c_api(self, sample: list[tuple[str, int]]):
for var, value in sample:
numeral = z3.Z3_mk_numeral(CTX.ref(), str(value), self._intsort.ast)
z3.Z3_inc_ref(CTX.ref(), numeral)
symbol = z3.Z3_mk_string_symbol(CTX.ref(), var)
const = z3.Z3_mk_const(CTX.ref(), symbol, self._intsort.ast)
# z3.Z3_inc_ref(CTX.ref(), const)
eq = z3.Z3_mk_eq(CTX.ref(), const, numeral)
# z3.Z3_inc_ref(CTX.ref(), const)
z3.Z3_solver_assert(CTX.ref(), self._solver.solver, eq)
# Now they are Z3's?
z3.Z3_dec_ref(CTX.ref(), numeral)
# z3.Z3_dec_ref(CTX.ref(), const)
# z3.Z3_dec_ref(CTX.ref(), eq)
def _add_sample_via_smtlib(self, sample: list[tuple[str, int]]):
self._solver.from_string(
"".join(
f"(declare-fun {var} () Int)\n(assert (= {var} {value}))"
for var, value in sample
)
)
def _add_sample(self, sample: list[tuple[str, int]]):
if self._use_c_api:
self._add_sample_via_c_api(sample)
else:
self._add_sample_via_smtlib(sample)
def _check_sample(self, sample: list[tuple[str, int]]) -> z3.CheckSatResult:
# Easiest way to do this seems to just ask Z3...
# Hope this isn't **too** costly.
self._solver.push()
self._add_sample(sample)
r = self._solver.check()
self._solver.pop()
return r
def count_sample(self, sample: list[tuple[str, int]]) -> bool:
result = self._check_sample(sample)
if result == z3.sat:
self._satisfies += 1
self._total += 1
return result == z3.sat
class ManualSatisfiesMetric(Metric):
def __init__(self, formula: str, statistics: typ.Optional[NodeStatistics] = None):
super().__init__(formula)
self._statistics = statistics
expr = z3.And(self._solver.assertions())
self._evaluator = self._build_evaluator(expr)
def count_sample(self, sample: list[tuple[str, int]]):
model = dict(sample)
res = self._evaluator(model)
if res:
self._satisfies += 1
self._total += 1
return res
@property
def result(self) -> fractions.Fraction:
if self._statistics:
return self._statistics.result
return super().result
def _build_evaluator(self, expr: z3.Expr):
return self._build_bool(expr)
def _build_bool(self, expr):
assert z3.is_bool(expr)
if self._statistics:
self._statistics.register_node(expr.get_id(), "bool")
if z3.is_and(expr):
return self._build_nary(expr, all, self._build_bool)
elif z3.is_or(expr):
return self._build_nary(expr, any, self._build_bool)
elif z3.is_not(expr):
return self._build_unary_not(expr)
elif z3.is_le(expr):
return self._build_binary(expr, operator.le, self._build_int)
elif z3.is_lt(expr):
return self._build_binary(expr, operator.lt, self._build_int)
elif z3.is_gt(expr):
return self._build_binary(expr, operator.gt, self._build_int)
elif z3.is_ge(expr):
return self._build_binary(expr, operator.ge, self._build_int)
elif z3.is_eq(expr):
if z3.is_bool(expr.children()[0]):
sub_op = self._build_bool
elif z3.is_int(expr.children()[0]):
sub_op = self._build_int
elif z3.is_array(expr.children()[0]):
sub_op = self._build_array
else:
raise NotImplementedError(f"What is this? {expr}")
return self._build_binary(expr, operator.eq, sub_op)
elif z3.is_true(expr) or z3.is_false(expr):
return self._build_leaf_literal(expr, bool(expr))
elif z3.is_const(expr):
return self._build_leaf_symbol(expr)
raise Exception(f"Unhandled: {expr}")
def _build_int(self, expr):
assert z3.is_int(expr)
if self._statistics:
self._statistics.register_node(expr.get_id(), "int")
if z3.is_add(expr):
return self._build_nary(expr, sum, self._build_int)
elif z3.is_sub(expr):
return self._build_binary(expr, operator.sub, self._build_int)
elif z3.is_mul(expr):
return self._build_nary(expr, prod, self._build_int)
elif z3.is_app_of(expr, z3.Z3_OP_UMINUS):
return self._build_unary_minus(expr)
elif z3.is_int_value(expr):
return self._build_leaf_literal(expr, expr.as_long())
elif z3.is_const(expr):
return self._build_leaf_symbol(expr)
elif z3.is_select(expr):
return self._build_array_select(expr)
elif z3.is_app_of(expr, z3.Z3_OP_ITE):
return self._build_ite(expr)
raise Exception(f"Unhandled: {expr}")
def _build_ite(self, expr):
node_id = expr.get_id()
predicate = self._build_bool(expr.arg(0))
if z3.is_int(expr):
op = self._build_int
elif z3.is_bool(expr):
op = self._build_bool
elif z3.is_array(expr):
op = self._build_array
else:
raise Exception(f"Unhandled ite: {expr}")
t_side = op(expr.arg(1))
f_side = op(expr.arg(2))
def e(model):
if predicate(model):
value = t_side(model)
else:
value = f_side(model)
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
def _build_array(self, expr):
assert z3.is_array(expr)
node_id = expr.get_id()
if self._statistics:
self._statistics.register_node(node_id, "array")
if z3.is_const(expr):
return self._build_array_leaf_symbol(expr)
elif z3.is_store(expr):
return self._build_array_store(expr)
elif z3.is_app_of(expr, z3.Z3_OP_ITE):
return self._build_ite(expr)
raise Exception(f"Unhandled array: {expr}")
def _build_array_leaf_symbol(self, expr):
name = expr.decl().name()
node_id = expr.get_id()
def e(model):
value = model.get(name, collections.defaultdict(int))
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
def _build_array_store(self, expr):
node_id = expr.get_id()
array = self._build_array(expr.arg(0))
index = self._build_int(expr.arg(1))
store = self._build_int(expr.arg(2))
def e(model):
old_value = array(model)
value = collections.defaultdict(old_value.default_factory, old_value)
value[index(model)] = store(model)
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
def _build_array_select(self, expr):
node_id = expr.get_id()
array = self._build_array(expr.arg(0))
index = self._build_int(expr.arg(1))
def e(model):
value = array(model)[index(model)]
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
def _build_nary(self, expr, op, subtype):
children = [subtype(subexpr) for subexpr in expr.children()]
node_id = expr.get_id()
def e(model):
value = op(child(model) for child in children)
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
def _build_binary(self, expr, op, subtype):
left, right = [subtype(subexpr) for subexpr in expr.children()]
node_id = expr.get_id()
def e(model):
value = op(left(model), right(model))
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
def _build_unary_not(self, expr):
child = self._build_bool(expr.arg(0))
node_id = expr.get_id()
def e(model):
value = not child(model)
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
def _build_unary_minus(self, expr):
child = self._build_int(expr.arg(0))
node_id = expr.get_id()
def e(model):
value = -child(model)
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
def _build_leaf_symbol(self, expr):
name = expr.decl().name()
node_id = expr.get_id()
def e(model):
value = model.get(name, 0)
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
def _build_leaf_literal(self, expr, value):
node_id = expr.get_id()
def e(model):
if self._statistics:
self._statistics.evaluate_node(node_id, value)
return value
return e
class NodeStatistics(abc.ABC):
def __init__(self):
self._storage: typ.MutableMapping[int, tuple[typ.Any, typ.Any, str]] = {}
self._totals: typ.Optional[typ.MutableMapping[int, int]] = None
@abc.abstractmethod
def register_node(self, node_id: int, sort: str):
pass
@abc.abstractmethod
def evaluate_node(self, node_id: int, value: typ.Any):
pass
def set_totals(self, totals: typ.MutableMapping[int, typ.Any]):
self._totals = totals
@property
@abc.abstractmethod
def result(self) -> fractions.Fraction:
pass
class WireCoverageStatistics(NodeStatistics):
MASK = 2**64 - 1
def register_node(self, node_id: int, sort: str):
if sort == "bool":
self._storage[node_id] = (False, False, sort)
elif sort == "int":
self._storage[node_id] = (0, 0, sort)
elif sort == "array":
self._storage[node_id] = (None, None, sort) # TODO: ???
else:
raise Exception(f"Unhandled: {sort}")
def evaluate_node(self, node_id: int, value: typ.Any):
old_true, old_false, sort = self._storage[node_id]
if sort == "bool":
self._storage[node_id] = (old_true or value, old_false or not value, sort)
elif sort == "int":
self._storage[node_id] = (
old_true | (value & self.MASK),
old_false | ((value & self.MASK) ^ self.MASK),
sort,
)
elif sort == "array":
pass # TODO: ???
else:
raise Exception(f"Unhandled: {sort}")
def node_total(self, node_id: int, sort: str) -> int:
if self._totals:
return self._totals[node_id]
if sort == "bool":
return 1
elif sort == "int":
return bin(self.MASK).count("1")
elif sort == "array":
return 0 # TODO: ???
else:
raise ValueError(f"Unhandled: {sort}")
def node_count(self, true_count, false_count, sort: str) -> int:
if sort == "bool":
return 1 if true_count and false_count else 0
elif sort == "int":
return bin(true_count & false_count).count("1")
elif sort == "array":
return 0
else:
raise ValueError(f"Unhandled: {sort}")
@property
def result(self) -> fractions.Fraction:
count = 0
total = 0
for key, (true_count, false_count, sort) in self._storage.items():
total += self.node_total(key, sort)
count += self.node_count(true_count, false_count, sort)
return fractions.Fraction(count, total)
@classmethod
def union_totals(
cls, *statistics: WireCoverageStatistics
) -> typ.MutableMapping[int, int]:
ret: typ.MutableMapping[int, int] = {}
if not statistics:
raise ValueError("Needs at least one statistics")
for key in statistics[0]._storage:
sort = statistics[0]._storage[key][2]
if sort == "bool":
ret[key] = (
1
if any(
s._storage[key][0] and s._storage[key][1] for s in statistics
)
else 0
)
elif sort == "int":
v = 0
for s in statistics:
t, f, _ = s._storage[key]
v |= t & f
ret[key] = bin(v).count("1")
elif sort == "array":
ret[key] = 0
else:
raise ValueError(f"Unhandled: {sort}")
return ret
def _load_formula(f: typ.TextIO) -> str:
return f.read()
def _apply_metric(
metric: Metric,
samples: typ.Iterator[list[tuple[str, int]]],
limit: int = 0,
print_first: bool = False,
):
if limit > 0:
samples = itertools.islice(samples, limit)
first = True
for sample in samples:
sat = metric.count_sample(sample)
if sat and print_first and first:
first = False
print(sample)
def parse_samples(f: typ.TextIO) -> typ.Iterator[list[tuple[str, int]]]:
def parse_int(value):
return int(value.strip("()").replace(" ", ""))
def parse_array(value):
splitted = value.strip("[],").split(",")
default = int(splitted[1])
assert len(splitted) - 2 == int(splitted[0])
return collections.defaultdict(
lambda: default, (x.split("->") for x in splitted[2:])
)
def to_tuple(sample):
var, value = sample.split(":")
return var, parse_array(value) if value[0] == "[" else parse_int(value)
for line in f:
p = line.split(" ", maxsplit=1)[1].strip("; \n").split(";")
yield [to_tuple(x) for x in p]
def calc_metric(
_formula: typ.TextIO,
_samples: typ.TextIO,
_metric: str,
_use_c_api=False,
limit=0,
print_first=False,
) -> fractions.Fraction:
formula = _load_formula(_formula)
samples = parse_samples(_samples)
if _metric == "satisfies":
metric: Metric = SatisfiesMetric(formula, use_c_api=_use_c_api)
elif _metric == "wire_coverage":
metric = ManualSatisfiesMetric(formula, statistics=WireCoverageStatistics())
_apply_metric(metric, samples, limit, print_first)
return metric.result
def main():
args = PARSER.parse_args(sys.argv[1:])
print(
calc_metric(
args.formula,
args.samples,
args.metric,
_use_c_api=args.use_c_api,
limit=args.limit,
print_first=args.print_first
)
)
if __name__ == "__main__":
main()