Debian Codegraph @

rpython/jit/metainterp/optimizeopt/test/test_z3intbound.py

raw symbols SCIP
1 """ The purpose of this test file is to do bounded model checking of the
2 IntBound methods with Z3.
3
4 The approach is to generate random bounds, then perform operations on them, and
5 ask Z3 whether the resulting bound is a sound approximation of the result.
6 """
7
8 import pytest
9 import sys
10 import gc
11
12 from rpython.rlib.rarithmetic import LONG_BIT, r_uint, intmask, ovfcheck
13 from rpython.jit.metainterp.optimizeopt.intutils import (
14 IntBound,
15 unmask_one,
16 unmask_zero,
17 next_pow2_m1,
18 lowest_set_bit_only,
19 leading_zeros_mask,
20 flip_msb,
21 msbonly,
22 same_sign,
23 )
24 from rpython.jit.metainterp.optimize import InvalidLoop
25
26 from rpython.jit.metainterp.optimizeopt.test.test_intbound import knownbits_and_bound_with_contained_number
27
28 from rpython.jit.metainterp.optimizeopt.test.test_z3checktests import z3_pymod, z3_pydiv
29
30 try:
31 import z3
32 from hypothesis import given, strategies, assume, example
33 except ImportError:
34 pytest.skip("please install z3 (z3-solver on pypi) and hypothesis")
35
36 def BitVecVal(value):
37 return z3.BitVecVal(value, LONG_BIT)
38
39 def BitVec(name):
40 return z3.BitVec(name, LONG_BIT)
41
42 def z3_with_reduced_bitwidth(width):
43 def dec(test):
44 assert test.func_name.endswith("logic") # doesn't work for code in intutils.py
45 def newtest(*args, **kwargs):
46 global LONG_BIT, MAXINT, MININT
47 old_value = LONG_BIT
48 LONG_BIT = width
49 MAXINT = 2 ** (LONG_BIT - 1) - 1
50 MININT = -2 ** (LONG_BIT - 1)
51 try:
52 return test(*args, **kwargs)
53 finally:
54 LONG_BIT = old_value
55 MAXINT = sys.maxint
56 MININT = -sys.maxint - 1
57 newtest.func_name = test.func_name + "_wrapped"
58 return newtest
59 return dec
60
61 MAXINT = sys.maxint
62 MININT = -sys.maxint - 1
63
64 uints = strategies.builds(
65 r_uint,
66 strategies.integers(min_value=0, max_value=2**LONG_BIT - 1)
67 )
68
69 ints = strategies.builds(
70 lambda x: intmask(r_uint(x)),
71 strategies.integers(min_value=0, max_value=2**LONG_BIT - 1)
72 )
73
74 bounds = strategies.builds(
75 lambda tup: tup[0],
76 knownbits_and_bound_with_contained_number
77 )
78
79 varname_counter = 0
80
81 def z3_tnum_condition(variable, tvalue, tmask):
82 if isinstance(tvalue, r_uint):
83 tvalue = BitVecVal(tvalue)
84 if isinstance(tmask, r_uint):
85 tmask = BitVecVal(tmask)
86 return variable & ~tmask == tvalue
87
88 def z3_tvalue_tmask_are_valid(tvalue, tmask):
89 return tvalue & ~tmask == tvalue
90
91 def to_z3(bound, variable=None):
92 global varname_counter
93 if variable is None:
94 variable = BitVec("bv%s" % (varname_counter, ))
95 varname_counter += 1
96 components = []
97 if bound.upper < MAXINT:
98 components.append(variable <= BitVecVal(bound.upper))
99 if bound.lower > MININT:
100 components.append(variable >= BitVecVal(bound.lower))
101 if bound.tmask != r_uint(-1): # all unknown:
102 components.append(z3_tnum_condition(variable, bound.tvalue, bound.tmask))
103 if len(components) == 1:
104 return variable, components[0]
105 if len(components) == 0:
106 return variable, z3.BoolVal(True)
107 return variable, z3.And(*components)
108
109 class CheckError(Exception):
110 pass
111
112
113 def prove_implies(*args, **kwargs):
114 last = args[-1]
115 prev = args[:-1]
116 return prove(z3.Implies(z3.And(*prev), last), **kwargs)
117
118 def teardown_function(function):
119 # z3 doesn't add enough memory pressure, just collect after every function
120 # to counteract
121 gc.collect()
122
123 def prove(cond, use_timeout=True):
124 solver = z3.Solver()
125 if use_timeout and pytest.config.option.z3timeout:
126 solver.set("timeout", pytest.config.option.z3timeout)
127 z3res = solver.check(z3.Not(cond))
128 if z3res == z3.unsat:
129 pass
130 elif z3res == z3.unknown:
131 print "timeout", cond
132 assert use_timeout
133 elif z3res == z3.sat:
134 # not possible to prove!
135 global model
136 model = solver.model()
137 raise CheckError(cond, model)
138
139 @given(bounds, bounds)
140 def test_add(b1, b2):
141 b3 = b1.add_bound(b2)
142 var1, formula1 = to_z3(b1)
143 var2, formula2 = to_z3(b2)
144 var3, formula3 = to_z3(b3, var1 + var2)
145 prove_implies(formula1, formula2, formula3)
146
147 @given(bounds, bounds)
148 def test_add_bound_cannot_overflow(b1, b2):
149 bound = b1.add_bound_cannot_overflow(b2)
150 assume(bound)
151 var1, formula1 = to_z3(b1)
152 var2, formula2 = to_z3(b2)
153 m = z3.SignExt(LONG_BIT, var1) + z3.SignExt(LONG_BIT, var2)
154 no_ovf = m == z3.SignExt(LONG_BIT, var1 + var2)
155 prove_implies(formula1, formula2, no_ovf)
156
157 @given(bounds, bounds)
158 def test_add_bound_no_overflow(b1, b2):
159 b3 = b1.add_bound_no_overflow(b2)
160 var1, formula1 = to_z3(b1)
161 var2, formula2 = to_z3(b2)
162 var3, formula3 = to_z3(b3, var1 + var2)
163 m = z3.SignExt(LONG_BIT, var1) + z3.SignExt(LONG_BIT, var2)
164 no_ovf = m == z3.SignExt(LONG_BIT, var1 + var2)
165 prove_implies(formula1, formula2, no_ovf, formula3)
166
167 @given(bounds, bounds)
168 def test_sub(b1, b2):
169 b3 = b1.sub_bound(b2)
170 var1, formula1 = to_z3(b1)
171 var2, formula2 = to_z3(b2)
172 var3, formula3 = to_z3(b3, var1 - var2)
173 prove_implies(formula1, formula2, formula3)
174
175 @given(bounds, bounds)
176 def test_sub_bound_cannot_overflow(b1, b2):
177 bound = b1.sub_bound_cannot_overflow(b2)
178 assume(bound)
179 var1, formula1 = to_z3(b1)
180 var2, formula2 = to_z3(b2)
181 m = z3.SignExt(LONG_BIT, var1) - z3.SignExt(LONG_BIT, var2)
182 no_ovf = m == z3.SignExt(LONG_BIT, var1 - var2)
183 prove_implies(formula1, formula2, no_ovf)
184
185 @given(bounds, bounds)
186 def test_sub_bound_no_overflow(b1, b2):
187 b3 = b1.sub_bound_no_overflow(b2)
188 var1, formula1 = to_z3(b1)
189 var2, formula2 = to_z3(b2)
190 var3, formula3 = to_z3(b3, var1 - var2)
191 m = z3.SignExt(LONG_BIT, var1) - z3.SignExt(LONG_BIT, var2)
192 no_ovf = m == z3.SignExt(LONG_BIT, var1 - var2)
193 prove_implies(formula1, formula2, no_ovf, formula3)
194
195 @given(bounds, bounds)
196 def test_mul(b1, b2):
197 b3 = b1.mul_bound(b2)
198 var1, formula1 = to_z3(b1)
199 var2, formula2 = to_z3(b2)
200 var3, formula3 = to_z3(b3, var1 * var2)
201 prove_implies(formula1, formula2, formula3)
202
203 @given(bounds, bounds)
204 def test_mul_bound_cannot_overflow(b1, b2):
205 bound = b1.mul_bound_cannot_overflow(b2)
206 if bound:
207 var1, formula1 = to_z3(b1)
208 var2, formula2 = to_z3(b2)
209 m = z3.SignExt(LONG_BIT, var1) * z3.SignExt(LONG_BIT, var2)
210 no_ovf = m == z3.SignExt(LONG_BIT, var1 * var2)
211 prove_implies(formula1, formula2, no_ovf)
212
213 @given(bounds, bounds)
214 def test_mul_bound_no_overflow(b1, b2):
215 b3 = b1.mul_bound_no_overflow(b2)
216 var1, formula1 = to_z3(b1)
217 var2, formula2 = to_z3(b2)
218 var3, formula3 = to_z3(b3, var1 * var2)
219 m = z3.SignExt(LONG_BIT, var1) * z3.SignExt(LONG_BIT, var2)
220 no_ovf = m == z3.SignExt(LONG_BIT, var1 * var2)
221 prove_implies(formula1, formula2, no_ovf, formula3)
222
223 @given(bounds)
224 def test_neg(b1):
225 b2 = b1.neg_bound()
226 var1, formula1 = to_z3(b1)
227 var2, formula2 = to_z3(b2, -var1)
228 prove_implies(formula1, formula2)
229
230 @given(bounds, bounds)
231 def test_known(b1, b2):
232 var1, formula1 = to_z3(b1)
233 var2, formula2 = to_z3(b2)
234 if b1.known_lt(b2):
235 prove_implies(formula1, formula2, var1 < var2)
236 if b1.known_gt(b2):
237 prove_implies(formula1, formula2, var1 > var2)
238 if b1.known_le(b2):
239 prove_implies(formula1, formula2, var1 <= var2)
240 if b1.known_ge(b2):
241 prove_implies(formula1, formula2, var1 >= var2)
242 if b1.known_ne(b2):
243 prove_implies(formula1, formula2, var1 != var2)
244
245 @given(bounds, bounds)
246 def test_mod(b1, b2):
247 b3 = b1.mod_bound(b2)
248 print b1, b2, b3
249 var1, formula1 = to_z3(b1)
250 var2, formula2 = to_z3(b2)
251 var3, nonzero = z3_pymod_nonzero(var1, var2)
252 _, formula3 = to_z3(b3, var3)
253 prove_implies(formula1, formula2, nonzero, formula3)
254
255
256 # ____________________________________________________________
257 # boolean operations
258
259 @given(bounds, bounds)
260 def test_and(b1, b2):
261 b3 = b1.and_bound(b2)
262 var1, formula1 = to_z3(b1)
263 var2, formula2 = to_z3(b2)
264 var3, formula3 = to_z3(b3, var1 & var2)
265 prove_implies(formula1, formula2, formula3)
266
267 @given(bounds, bounds)
268 def test_or(b1, b2):
269 b3 = b1.or_bound(b2)
270 var1, formula1 = to_z3(b1)
271 var2, formula2 = to_z3(b2)
272 var3, formula3 = to_z3(b3, var1 | var2)
273 prove_implies(formula1, formula2, formula3)
274
275 @given(bounds, bounds)
276 def test_xor(b1, b2):
277 b3 = b1.xor_bound(b2)
278 var1, formula1 = to_z3(b1)
279 var2, formula2 = to_z3(b2)
280 var3, formula3 = to_z3(b3, var1 ^ var2)
281 prove_implies(formula1, formula2, formula3)
282
283 @given(bounds)
284 def test_invert(b1):
285 b2 = b1.invert_bound()
286 var1, formula1 = to_z3(b1)
287 var2, formula2 = to_z3(b2, ~var1)
288 prove_implies(formula1, formula2)
289
290
291 # ____________________________________________________________
292 # shrinking
293
294 @given(ints, ints)
295 def test_shrink_bounds_to_knownbits(x, y):
296 x, y = sorted([x, y])
297 b = IntBound(x, y, do_shrinking=False)
298 var1, formula1 = to_z3(b)
299 b.shrink()
300 var1, formula2 = to_z3(b, var1)
301 prove_implies(formula1, formula2)
302
303 @given(uints, uints)
304 def test_shrink_knownbits_to_bounds(x, y):
305 b = IntBound(tvalue=x & ~y, tmask=y, do_shrinking=False)
306 var1, formula1 = to_z3(b)
307 b.shrink()
308 var1, formula2 = to_z3(b, var1)
309 prove_implies(formula1, formula2)
310
311 @given(ints, ints, uints, uints)
312 def test_shrink_mixed(x, y, value, tmask):
313 print x, y, value, tmask
314 x, y = sorted([x, y])
315 b = IntBound(x, y, value & ~tmask, tmask, do_shrinking=False)
316 var1, formula1 = to_z3(b)
317 # check that b contains values before we shrink
318 solver = z3.Solver()
319 assume(solver.check(formula1) == z3.sat)
320 b.shrink()
321 var1, formula2 = to_z3(b, var1)
322 prove_implies(formula1, formula2)
323
324 # ____________________________________________________________
325 # explicit proofs of some of the helpers
326
327 def test_prove_next_pow2_m1():
328 x = BitVec('x')
329 res = next_pow2_m1(x)
330 # it's a power of 2 - 1
331 prove(res & (res + 1) == 0)
332 # it's bigger than x
333 prove(
334 z3.BV2Int(res) + 1 >= z3.BV2Int(x)
335 )
336
337 def test_prove_lowest_set_bit_only():
338 x = BitVec('x')
339 res = lowest_set_bit_only(x)
340 prove_implies(
341 x != 0,
342 popcount64(res) == 1
343 )
344 prove_implies(
345 x == 0,
346 res == 0,
347 )
348 # do it the pedestrian way: if a bit is set, then the bits with lower
349 # indexes must be 0
350 conditions = []
351 for i in range(LONG_BIT):
352 for j in range(i):
353 cond = z3.Implies(z3.Extract(i, i, res) == 1, z3.Extract(j, j, res) == 0)
354 conditions.append(cond)
355
356 prove(
357 z3.And(*conditions)
358 )
359
360 # ____________________________________________________________
361 # explicit proofs of IntBound logic/code
362
363 def make_z3_bound_and_tnum(name):
364 """ make a z3 knownbits number and bounds.
365 return values are:
366 - variable, corresponding to the concrete value
367 - lower, a variable corresponding to the lower bound
368 - upper, a variable corresponding to the upper bound
369 - tvalue and tmask, corresponding to the abstract value
370 - formula, which is the precondition that tvalue and tmask are well-formed,
371 that lower <= upper, and that the four variables are a valid abstraction
372 of the concrete value
373 """
374 variable = BitVec(name)
375 tvalue = BitVec(name + "_tvalue")
376 tmask = BitVec(name + "_tmask")
377 upper = BitVec(name + "_upper")
378 lower = BitVec(name + "_lower")
379 formula = z3.And(
380 z3_tnum_condition(variable, tvalue, tmask),
381 lower <= variable,
382 variable <= upper
383 )
384 return variable, lower, upper, tvalue, tmask, formula
385
386 def popcount64(w):
387 w -= (w >> 1) & 0x5555555555555555
388 w = (w & 0x3333333333333333) + ((w >> 2) & 0x3333333333333333)
389 w = (w + (w >> 4)) & 0x0f0f0f0f0f0f0f0f
390 return ((w * 0x0101010101010101) >> 56) & 0xff
391
392 def test_popcount64():
393 assert popcount64(1 << 60) == 1
394 assert popcount64((1 << 60) + 5) == 3
395 assert popcount64((1 << 63) + 0b11010110111) == 9
396
397 def z3_add_overflow(a, b):
398 result = a + b
399 result_wide = z3.SignExt(LONG_BIT, a) + z3.SignExt(LONG_BIT, b)
400 no_ovf = result_wide == z3.SignExt(LONG_BIT, result)
401 return result, no_ovf
402
403 def z3_sub_overflow(a, b):
404 result = a - b
405 result_wide = z3.SignExt(LONG_BIT, a) - z3.SignExt(LONG_BIT, b)
406 no_ovf = result_wide == z3.SignExt(LONG_BIT, result)
407 return result, no_ovf
408
409 def z3_mul_overflow(a, b):
410 result = a * b
411 result_wide = z3.SignExt(LONG_BIT, a) * z3.SignExt(LONG_BIT, b)
412 no_ovf = result_wide == z3.SignExt(LONG_BIT, result)
413 return result, no_ovf
414
415 # debugging functions to understand the counterexamples
416
417 def s(p):
418 if p.sort() == z3.BoolSort():
419 print model.evaluate(p)
420 else:
421 print hex(model.evaluate(p).as_signed_long())
422
423 def u(p):
424 print "r_uint(%s)" % bin(model.evaluate(p).as_long())
425
426 class Z3IntBound(IntBound):
427 def __init__(self, lower, upper, tvalue, tmask, concrete_variable=None):
428 self.lower = lower
429 self.upper = upper
430 self.tvalue = tvalue
431 self.tmask = tmask
432
433 self.concrete_variable = concrete_variable
434
435 @staticmethod
436 def new(lower, upper, tvalue, tmask):
437 return Z3IntBound(lower, upper, tvalue, tmask)
438
439 @staticmethod
440 def from_knownbits(tvalue, tmask):
441 # compute lower and upper bound like shrinking does it
442 res = Z3IntBound(None, None, tvalue, tmask)
443 res.lower = res._get_minimum_signed_by_knownbits()
444 res.upper = res._get_maximum_signed_by_knownbits()
445 return res
446
447 @staticmethod
448 def from_constant(const):
449 if isinstance(const, int):
450 const = BitVecVal(const)
451 res = Z3IntBound(const, const, const, 0)
452 return res
453
454 @staticmethod
455 def intmask(x):
456 # casts from unsigned to signed don't actually matter to Z3
457 return x
458
459 @staticmethod
460 def r_uint(x):
461 # casts from unsigned to signed don't actually matter to Z3
462 return x
463
464 @staticmethod
465 def _add_check_overflow(a, b, default):
466 result, no_ovf = z3_add_overflow(a, b)
467 return z3.If(no_ovf, result, default)
468
469 @staticmethod
470 def _sub_check_overflow(a, b, default):
471 result, no_ovf = z3_sub_overflow(a, b)
472 return z3.If(no_ovf, result, default)
473
474 @staticmethod
475 def _urshift(a, b):
476 return z3.LShR(a, b)
477
478 def __repr__(self):
479 more = ''
480 if self.concrete_variable is not None:
481 more = ', concrete_variable=%s' % (self.concrete_variable, )
482 return "<Z3IntBound lower=%s, upper=%s, tvalue=%s, tmask=%s%s>" % (
483 self.lower, self.upper, self.tvalue, self.tmask, more)
484 __str__ = __repr__
485
486 # ____________________________________________________________
487 # reimplementations of methods with control flow that Z3 doesn't support
488
489 def add_bound(self, other):
490 lower, no_ovf_lower = z3_add_overflow(self.lower, other.lower)
491 upper, no_ovf_upper = z3_add_overflow(self.upper, other.upper)
492 result_lower = z3.If(z3.And(no_ovf_lower, no_ovf_upper),
493 lower, MININT)
494 result_upper = z3.If(z3.And(no_ovf_lower, no_ovf_upper),
495 upper, MAXINT)
496 tvalue, tmask = self._tnum_add(other)
497 return Z3IntBound(result_lower, result_upper, tvalue, tmask)
498
499 def sub_bound(self, other):
500 result = self.concrete_variable - other.concrete_variable
501 lower, no_ovf_lower = z3_sub_overflow(self.lower, other.upper)
502 upper, no_ovf_upper = z3_sub_overflow(self.upper, other.lower)
503 result_lower = z3.If(z3.And(no_ovf_lower, no_ovf_upper),
504 lower, MININT)
505 result_upper = z3.If(z3.And(no_ovf_lower, no_ovf_upper),
506 upper, MAXINT)
507 tvalue, tmask = self._tnum_sub(other)
508 return Z3IntBound(result_lower, result_upper, tvalue, tmask)
509
510 def add_bound_cannot_overflow(self, other):
511 lower, no_ovf_lower = z3_add_overflow(self.lower, other.lower)
512 upper, no_ovf_upper = z3_add_overflow(self.upper, other.upper)
513 return z3.And(no_ovf_lower, no_ovf_upper)
514
515 def sub_bound_cannot_overflow(self, other):
516 lower, no_ovf_lower = z3_sub_overflow(self.lower, other.upper)
517 upper, no_ovf_upper = z3_sub_overflow(self.upper, other.lower)
518 return z3.And(no_ovf_lower, no_ovf_upper)
519
520 def and_bound(self, other):
521 pos1 = self.known_nonnegative()
522 pos2 = other.known_nonnegative()
523 # the next three if-conditions are proven by test_prove_and_bound_logic
524 lower = z3.If(z3.Or(pos1, pos2), z3.BitVecVal(0, LONG_BIT), MININT)
525 upper = z3.If(
526 pos1,
527 z3.If(
528 pos2,
529 z3_min(self.upper, other.upper),
530 self.upper),
531 z3.If(
532 pos2,
533 other.upper,
534 MAXINT))
535 res_tvalue, res_tmask = self._tnum_and(other)
536 return self.new(lower, upper, res_tvalue, res_tmask)
537
538 def lshift_bound_cannot_overflow(self, other):
539 _, no_ovf1 = z3_lshift_overflow(self.lower, other.lower)
540 _, no_ovf2 = z3_lshift_overflow(self.lower, other.upper)
541 _, no_ovf3 = z3_lshift_overflow(self.upper, other.lower)
542 _, no_ovf4 = z3_lshift_overflow(self.upper, other.upper)
543 return z3.And(no_ovf1, no_ovf2, no_ovf3, no_ovf4)
544
545 def rshift_bound(self, other):
546 result = self.concrete_variable >> other.concrete_variable
547 result1 = self.lower >> other.lower
548 result2 = self.lower >> other.upper
549 result3 = self.upper >> other.lower
550 result4 = self.upper >> other.upper
551 min1 = z3_min(result1, result2, result3, result4)
552 max1 = z3_max(result1, result2, result3, result4)
553 cond = z3.And(other.lower >= 0, other.upper < LONG_BIT)
554 min1 = z3.If(cond, min1, MININT)
555 max1 = z3.If(cond, max1, MAXINT)
556 tvalue, tmask = self._tnum_rshift(other.lower)
557 cond2 = z3.And(other.is_constant(), cond)
558 tvalue = z3.If(cond2,
559 tvalue, 0)
560 tmask = z3.If(cond2,
561 tmask, -1)
562 return Z3IntBound(min1, max1, tvalue, tmask)
563
564 def urshift_bound(self, other):
565 tvalue_if_const, tmask_if_const = self._tnum_urshift(other.lower)
566 tvalue = z3.If(other.is_constant(), tvalue_if_const, 0)
567 tmask = z3.If(other.is_constant(), tmask_if_const, -1)
568 lower = z3.If(z3.And(other.is_constant(), self.lower >= 0), self.lower >> other.lower, MININT)
569 upper = z3.If(z3.And(other.is_constant(), self.lower >= 0), self.upper >> other.lower, MAXINT)
570 return Z3IntBound.from_knownbits(tvalue, tmask)
571
572 def is_constant(self):
573 return z3.And(self.lower == self.upper, self.tmask == 0)
574
575 def is_bool(self):
576 return z3.And(0 <= self.lower, self.upper <= 1)
577
578 def known_eq_const(self, value):
579 return z3.If(self.is_constant(), self.lower == value, False)
580
581 def get_constant_int(self):
582 return z3.If(self.is_constant(), self.lower, 0xdeadbeef)
583 # ____________________________________________________________
584
585 def z3_formula(self, variable=None, must_be_minimal=False):
586 """ return the Z3 condition that:
587 - self is well-formed
588 - variable (or self.concrete_variable) is an element of the set
589 described by self
590 """
591 if variable is None:
592 variable = self.concrete_variable
593 assert variable is not None
594 result = z3.And(
595 # is the tnum well-formed? ie are the unknown bits in tvalue set to 0?
596 self.tvalue & ~self.tmask == self.tvalue,
597 # does variable fulfill the conditions imposed by tvalue and tmask?
598 z3_tnum_condition(variable, self.tvalue, self.tmask),
599 # does variable fulfill the conditions of the bounds?
600 self.lower <= variable,
601 variable <= self.upper,
602 )
603 if must_be_minimal:
604 tvalue, tmask, valid = self._tnum_improve_knownbits_by_bounds()
605 result = z3.And(
606 valid,
607 self.tvalue == tvalue,
608 self.tmask == tmask,
609 result
610 )
611 return result
612
613 def convert_to_concrete(self, model):
614 """ A helper function that can be used to turn a Z3 counterexample into an
615 IntBound instance to understand it better. """
616 v = r_uint(model.evaluate(self.tvalue).as_long())
617 m = r_uint(model.evaluate(self.tmask).as_long())
618 l = model.evaluate(self.lower).as_signed_long()
619 u = model.evaluate(self.upper).as_signed_long()
620 return IntBound(l, u, v, m, do_shrinking=False)
621
622 def prove_implies(self, *args):
623 formula_args = [(arg.z3_formula() if isinstance(arg, Z3IntBound) else arg)
624 for arg in (self, ) + args]
625 try:
626 prove_implies(
627 *formula_args,
628 use_timeout=False
629 )
630 except CheckError as e:
631 model = e.args[1]
632 example_self = self.convert_to_concrete(model)
633 print "ERROR", args
634 print "COUNTEREXAMPLE", example_self
635 assert 0
636
637 def make_z3_intbounds_instance(name, concrete_variable=None):
638 if concrete_variable is None:
639 variable = BitVec(name + "_concrete")
640 else:
641 variable = concrete_variable
642 tvalue = BitVec(name + "_tvalue")
643 tmask = BitVec(name + "_tmask")
644 upper = BitVec(name + "_upper")
645 lower = BitVec(name + "_lower")
646 return Z3IntBound(lower, upper, tvalue, tmask, variable)
647
648 def test_prove_invert():
649 bound = make_z3_intbounds_instance('self')
650 b2 = bound.invert_bound()
651 bound.prove_implies(
652 b2.z3_formula(~bound.concrete_variable),
653 )
654
655 def test_prove_min_max_unsigned_by_knownbits():
656 bound = make_z3_intbounds_instance('self')
657 minimum = bound.get_minimum_unsigned_by_knownbits()
658 bound.prove_implies(
659 z3.ULE(minimum, bound.concrete_variable),
660 )
661 maximum = bound.get_maximum_unsigned_by_knownbits()
662 bound.prove_implies(
663 z3.ULE(bound.concrete_variable, maximum),
664 )
665
666 def test_prove_min_max_signed_by_knownbits():
667 bound = make_z3_intbounds_instance('self')
668 minimum = bound._get_minimum_signed_by_knownbits()
669 bound.prove_implies(
670 minimum <= bound.concrete_variable
671 )
672 bound.prove_implies(
673 z3_tnum_condition(minimum, bound.tvalue, bound.tmask),
674 )
675
676 maximum = bound._get_maximum_signed_by_knownbits()
677 bound.prove_implies(
678 bound.concrete_variable <= maximum,
679 )
680 bound.prove_implies(
681 z3_tnum_condition(maximum, bound.tvalue, bound.tmask),
682 )
683
684 def test_prove_or():
685 b1 = make_z3_intbounds_instance('self')
686 b2 = make_z3_intbounds_instance('other')
687 b3 = b1.or_bound(b2)
688 b3.concrete_variable = b1.concrete_variable | b2.concrete_variable
689 b1.prove_implies(
690 b2,
691 b3
692 )
693
694 def test_prove_or_bounds_implied_by_knownbits():
695 b1 = make_z3_intbounds_instance('self')
696 b2 = make_z3_intbounds_instance('other')
697 b3 = b1.or_bound(b2)
698 # similar to test_prove_xor_bounds_implied_by_knownbits, the following
699 # logic was used to give explicit bounds for or of two nonnegative numbers.
700 mostsignificant = b1.upper | b2.upper
701 upper = next_pow2_m1(mostsignificant)
702 result = b1.concrete_variable | b2.concrete_variable
703 b1.prove_implies(
704 b1.z3_formula(must_be_minimal=True),
705 b2.z3_formula(must_be_minimal=True),
706 b1.lower >= 0,
707 b2.lower >= 0,
708 z3.And(b3.upper <= upper, b3.lower >= 0),
709 )
710
711 def test_prove_xor():
712 b1 = make_z3_intbounds_instance('self')
713 b2 = make_z3_intbounds_instance('other')
714 b3 = b1.xor_bound(b2)
715 b3.concrete_variable = b1.concrete_variable ^ b2.concrete_variable
716 b1.prove_implies(
717 b2,
718 b3,
719 )
720
721 def test_prove_xor_bounds_implied_by_knownbits():
722 b1 = make_z3_intbounds_instance('self')
723 b2 = make_z3_intbounds_instance('other')
724
725 # for non-negative b1 and b2, xor_bound used to contain code that set the
726 # lower bound to 0, and an upper bound to:
727 mostsignificant = b1.upper | b2.upper
728 upper = next_pow2_m1(mostsignificant)
729 # this is unnecessary, because the information is implied by the knownbits,
730 # which we prove here
731
732 result = b1.concrete_variable ^ b2.concrete_variable
733
734 b3 = b1.xor_bound(b2)
735 b3.concrete_variable = result
736 b1.prove_implies(
737 b1.z3_formula(must_be_minimal=True),
738 b2.z3_formula(must_be_minimal=True),
739 b1.lower >= 0,
740 b2.lower >= 0,
741 # check that the maximum implied by the known bits is not worse than
742 # the maximum computed by the expression above
743 z3.And(b3.upper <= upper, b3.lower >= 0),
744 )
745
746 def test_prove_and():
747 b1 = make_z3_intbounds_instance('self')
748 b2 = make_z3_intbounds_instance('other')
749 tvalue, tmask = b1._tnum_and(b2)
750 b1.prove_implies(
751 b2,
752 z3_tnum_condition(b1.concrete_variable & b2.concrete_variable, tvalue, tmask),
753 )
754
755 def test_prove_and_bound_logic():
756 b1 = make_z3_intbounds_instance('self')
757 b2 = make_z3_intbounds_instance('other')
758 res = b1.concrete_variable & b2.concrete_variable
759 b3 = b1.and_bound(b2)
760 b3.concrete_variable = res
761 b1.prove_implies(
762 b2,
763 b3
764 )
765
766 def test_prove_add_knownbits():
767 b1 = make_z3_intbounds_instance('self')
768 b2 = make_z3_intbounds_instance('other')
769 result = b1.concrete_variable + b2.concrete_variable
770 res_tvalue, res_tmask = b1._tnum_add(b2)
771 b1.prove_implies(
772 b2,
773 z3_tnum_condition(result, res_tvalue, res_tmask),
774 )
775
776 def test_prove_add_bound_logic():
777 b1 = make_z3_intbounds_instance('self')
778 b2 = make_z3_intbounds_instance('other')
779 result = b1.concrete_variable + b2.concrete_variable
780 b3 = b1.add_bound(b2)
781 b3.concrete_variable = result
782 b1.prove_implies(
783 b2,
784 b3
785 )
786
787 def test_prove_add_bound_cannot_overflow_logic():
788 b1 = make_z3_intbounds_instance('self')
789 b2 = make_z3_intbounds_instance('other')
790 result, no_ovf_result = z3_add_overflow(b1.concrete_variable, b2.concrete_variable)
791 b1.prove_implies(
792 b2,
793 b1.add_bound_cannot_overflow(b2),
794 no_ovf_result,
795 )
796
797 def test_prove_add_bound_no_overflow():
798 b1 = make_z3_intbounds_instance('self')
799 b2 = make_z3_intbounds_instance('other')
800 result, no_ovf = z3_add_overflow(b1.concrete_variable, b2.concrete_variable)
801 b3 = b1.add_bound_no_overflow(b2)
802 b3.concrete_variable = result
803 b1.prove_implies(
804 b2,
805 no_ovf,
806 b3
807 )
808
809 def test_prove_mul2_bound_no_overflow():
810 b1 = make_z3_intbounds_instance('self')
811 result, no_ovf = z3_add_overflow(b1.concrete_variable, b1.concrete_variable)
812 b2 = b1.mul2_bound_no_overflow()
813 b2.concrete_variable = result
814 b1.prove_implies(
815 no_ovf,
816 b2
817 )
818 # check that the result is always even
819 b1.prove_implies(
820 no_ovf,
821 z3.And(b2.tvalue & 1 == 0, b2.tmask & 1 == 0)
822 )
823
824 def test_prove_neg_logic():
825 b1 = make_z3_intbounds_instance('self')
826 one = Z3IntBound(BitVecVal(1), BitVecVal(1), BitVecVal(1), BitVecVal(0))
827 b2 = b1.neg_bound()
828 b2.concrete_variable = -b1.concrete_variable
829 b1.prove_implies(b2)
830
831 def test_prove_sub_knownbits():
832 b1 = make_z3_intbounds_instance('self')
833 b2 = make_z3_intbounds_instance('other')
834 result = b1.concrete_variable - b2.concrete_variable
835 res_tvalue, res_tmask = b1._tnum_sub(b2)
836 b1.prove_implies(
837 b2,
838 z3_tnum_condition(result, res_tvalue, res_tmask),
839 )
840
841 def test_prove_sub_bound_logic():
842 b1 = make_z3_intbounds_instance('self')
843 b2 = make_z3_intbounds_instance('other')
844 b3 = b1.sub_bound(b2)
845 b3.concrete_variable = b1.concrete_variable - b2.concrete_variable
846 b1.prove_implies(
847 b2,
848 b3
849 )
850
851 def test_prove_sub_bound_cannot_overflow_logic():
852 b1 = make_z3_intbounds_instance('self')
853 b2 = make_z3_intbounds_instance('other')
854 result, no_ovf_result = z3_sub_overflow(b1.concrete_variable, b2.concrete_variable)
855 b1.prove_implies(
856 b2,
857 b1.sub_bound_cannot_overflow(b2),
858 no_ovf_result,
859 )
860
861 def test_prove_sub_bound_no_overflow():
862 b1 = make_z3_intbounds_instance('self')
863 b2 = make_z3_intbounds_instance('other')
864 result, no_ovf = z3_sub_overflow(b1.concrete_variable, b2.concrete_variable)
865 b3 = b1.sub_bound_no_overflow(b2)
866 b1.prove_implies(
867 b2,
868 no_ovf,
869 b3.z3_formula(result)
870 )
871
872 def test_prove_and_backwards():
873 b1 = make_z3_intbounds_instance('self')
874 b2 = make_z3_intbounds_instance('other')
875 b3 = make_z3_intbounds_instance('result')
876 res = b1.concrete_variable & b2.concrete_variable
877 b3.concrete_variable = res
878 better_tvalue, better_tmask, valid = b2._tnum_and_backwards(b3)
879 b1.prove_implies(
880 b2,
881 b3,
882 z3.And(
883 valid,
884 z3_tnum_condition(b1.concrete_variable, better_tvalue, better_tmask)
885 ),
886 )
887
888 def test_prove_and_backwards_inconsistent():
889 b1 = make_z3_intbounds_instance('self')
890 b2 = make_z3_intbounds_instance('other')
891 b3 = make_z3_intbounds_instance('result')
892 res = b1.concrete_variable & b2.concrete_variable
893 better_tvalue, better_tmask, valid = b2._tnum_and_backwards(b3)
894 # hm, this is just the contraposition of test_prove_and_backwards, so
895 # trivially true?
896 b1.prove_implies(
897 b2,
898 b3,
899 # if we aren't consistent
900 z3.Not(valid),
901 # then the result must be different than the result of the &
902 b3.concrete_variable != res
903 )
904
905 def test_prove_or_backwards():
906 b1 = make_z3_intbounds_instance('self')
907 b2 = make_z3_intbounds_instance('other')
908 b3 = make_z3_intbounds_instance('result')
909 res = b1.concrete_variable | b2.concrete_variable
910 b3.concrete_variable = res
911 better_tvalue, better_tmask, valid = b2._tnum_or_backwards(b3)
912 b1.prove_implies(
913 b2,
914 b3,
915 z3.And(
916 valid,
917 z3_tnum_condition(b1.concrete_variable, better_tvalue, better_tmask)
918 ),
919 )
920
921 def test_prove_known_unsigned_lt_logic():
922 b1 = make_z3_intbounds_instance('self')
923 b2 = make_z3_intbounds_instance('other')
924 max_self = b1.get_maximum_unsigned_by_knownbits()
925 min_other = b2.get_minimum_unsigned_by_knownbits()
926 b1.prove_implies(
927 b2,
928 z3.ULT(max_self, min_other),
929 z3.ULT(b1.concrete_variable, b2.concrete_variable),
930 )
931
932 def test_prove_known_unsigned_lt_from_signed_lt_logic():
933 b1 = make_z3_intbounds_instance('self')
934 b2 = make_z3_intbounds_instance('other')
935 b1.prove_implies(
936 b2,
937 b1.lower >= 0,
938 b2.lower < b2.lower,
939 z3.ULT(b1.concrete_variable, b2.concrete_variable),
940 )
941 b1 = make_z3_intbounds_instance('self')
942 b2 = make_z3_intbounds_instance('other')
943 b1.prove_implies(
944 b2,
945 b1.lower < 0,
946 b2.lower < b2.lower,
947 z3.ULT(b1.concrete_variable, b2.concrete_variable),
948 )
949
950
951 def test_prove_known_eq_const_logic():
952 b1 = make_z3_intbounds_instance('self')
953 x = BitVec('x')
954 b1.prove_implies(
955 b1.known_eq_const(x),
956 b1.concrete_variable == x
957 )
958
959 def test_prove_uint_less_nonnegative_implies_nonnegative():
960 index = BitVec('index')
961 length = BitVec('length')
962 prove_implies(
963 length >= 0,
964 z3.ULT(index, length),
965 z3.And(index >= 0, index < length),
966 )
967 prove_implies(
968 length >= 0,
969 z3.ULT(index, length),
970 z3.And(index >= 0, index <= length - 1),
971 )
972 prove_implies(
973 length >= 0,
974 z3.ULE(index, length),
975 z3.And(index >= 0, index <= length),
976 )
977
978 def test_prove_uint_greater_negative_implies_negative():
979 a = BitVec('a')
980 b = BitVec('b')
981 prove_implies(
982 b < 0,
983 z3.UGE(a, b),
984 z3.And(a < 0, a >= b),
985 )
986 prove_implies(
987 b < 0,
988 z3.UGT(a, b),
989 z3.And(a < 0, a > b),
990 )
991 # an example
992 prove_implies(
993 b <= -3,
994 b >= -4,
995 z3.UGT(a, b),
996 z3.And(a <= -1, a >= -3),
997 )
998
999
1000 def test_prove_known_cmp():
1001 b1 = make_z3_intbounds_instance('self')
1002 b2 = make_z3_intbounds_instance('other')
1003 b1.prove_implies(
1004 b2,
1005 b1.known_lt(b2),
1006 b1.concrete_variable < b2.concrete_variable,
1007 )
1008 b1.prove_implies(
1009 b2,
1010 b1.known_le(b2),
1011 b1.concrete_variable <= b2.concrete_variable,
1012 )
1013 b1.prove_implies(
1014 b2,
1015 b1.known_gt(b2),
1016 b1.concrete_variable > b2.concrete_variable,
1017 )
1018 b1.prove_implies(
1019 b2,
1020 b1.known_ge(b2),
1021 b1.concrete_variable >= b2.concrete_variable,
1022 )
1023
1024 def test_prove_tnum_intersect():
1025 b1 = make_z3_intbounds_instance('self')
1026 b2 = make_z3_intbounds_instance('other')
1027 tvalue, tmask, valid = b1._tnum_intersect(b2.tvalue, b2.tmask)
1028 b1.prove_implies(
1029 b2.z3_formula(b1.concrete_variable),
1030 z3.And(
1031 valid,
1032 z3_tnum_condition(b1.concrete_variable, tvalue, tmask)
1033 )
1034 )
1035 # check that if valid if false, there are no values in the intersection
1036 b1.prove_implies(
1037 z3.Not(valid),
1038 z3.Not(b2.z3_formula(b1.concrete_variable)),
1039 )
1040 # and also that we only gain information
1041 b1.prove_implies(
1042 b2.z3_formula(b1.concrete_variable),
1043 valid,
1044 popcount64(~tmask) >= popcount64(~b1.tmask),
1045 )
1046
1047 def test_prove_tnum_intersect_idempotent():
1048 b1 = make_z3_intbounds_instance('self')
1049 b2 = make_z3_intbounds_instance('other')
1050 b2.concrete_variable = b1.concrete_variable
1051 tvalue1, tmask1, valid1 = b1._tnum_intersect(b2.tvalue, b2.tmask)
1052 tvalue2, tmask2, valid2 = b1._tnum_intersect(tvalue1, tmask1)
1053 b1.prove_implies(
1054 b2,
1055 z3.And(
1056 tvalue1 == tvalue2,
1057 tmask1 == tmask2,
1058 valid1,
1059 valid2,
1060 )
1061 )
1062
1063
1064 # ____________________________________________________________
1065 # prove things about _shrink_knownbits_by_bounds
1066
1067 def test_prove_tnum_implied_by_bounds():
1068 self = make_z3_intbounds_instance('self')
1069 bounds_tvalue, bounds_tmask = self._tnum_implied_by_bounds()
1070 val = BitVec('val')
1071 self.prove_implies(
1072 val >= self.lower,
1073 val <= self.upper,
1074 z3_tnum_condition(val, bounds_tvalue, bounds_tmask)
1075 )
1076
1077 def test_prove_shrink_knownbits_by_bounds():
1078 self = make_z3_intbounds_instance('self')
1079 tvalue, tmask, valid = self._tnum_improve_knownbits_by_bounds()
1080 self.prove_implies(
1081 z3_tnum_condition(self.concrete_variable, self.tvalue, self.tmask),
1082 z3.And(valid, z3_tnum_condition(self.concrete_variable, tvalue, tmask)),
1083 )
1084
1085 # ____________________________________________________________
1086 # prove things about _shrink_bounds_by_knownbits
1087
1088 def test_prove_shrink_bounds_by_knownbits_min_case1():
1089 # case 1, cl2set > set2cl
1090 b1 = make_z3_intbounds_instance('self')
1091
1092 threshold = b1.lower
1093 working_min, cl2set, set2cl = b1._helper_min_max_prepare(threshold)
1094 new_threshold = b1._helper_min_case1(working_min, cl2set)
1095
1096 # show that the new_threshold is larger than threshold
1097 b1.prove_implies(
1098 b1._get_minimum_signed_by_knownbits() < threshold,
1099 working_min != threshold,
1100 z3.UGT(cl2set, set2cl),
1101 new_threshold > threshold,
1102 )
1103 # correctness: show that there are no elements x in b1 with
1104 # threshold <= x < new_threshold
1105 b1.prove_implies(
1106 b1._get_minimum_signed_by_knownbits() < threshold,
1107 working_min != threshold,
1108 z3.UGT(cl2set, set2cl),
1109 z3.Not(b1.concrete_variable < new_threshold),
1110 )
1111 # precision: new_threshold is an element in the set, ie we
1112 # couldn't have increased the bound further
1113 b1.prove_implies(
1114 b1._get_minimum_signed_by_knownbits() < threshold,
1115 working_min != threshold,
1116 z3.UGT(cl2set, set2cl),
1117 z3_tnum_condition(new_threshold, b1.tvalue, b1.tmask),
1118 )
1119
1120
1121 def test_prove_shrink_bounds_by_knownbits_correctness_min_case2():
1122 # case 2) cl2set <= set2cl
1123 b1 = make_z3_intbounds_instance('self')
1124
1125 threshold = b1.lower
1126 working_min, cl2set, set2cl = b1._helper_min_max_prepare(threshold)
1127 working_min_ne_threshold = working_min != threshold
1128
1129 new_threshold = b1._helper_min_case2(working_min, set2cl)
1130
1131 # check that the bound is not getting worse
1132 b1.prove_implies(
1133 b1._get_minimum_signed_by_knownbits() < threshold,
1134 working_min_ne_threshold,
1135 z3.ULE(cl2set, set2cl),
1136 new_threshold > threshold
1137 )
1138
1139 # correctness: show that there are no elements x in b1 with
1140 # threshold <= x < new_threshold
1141 b1.prove_implies(
1142 b1._get_minimum_signed_by_knownbits() < threshold,
1143 working_min_ne_threshold,
1144 z3.ULE(cl2set, set2cl),
1145 z3.Not(b1.concrete_variable < new_threshold),
1146 )
1147
1148 # precision: new_threshold is an element in the set, ie we
1149 # couldn't have increased the bound further
1150 b1.prove_implies(
1151 b1._get_minimum_signed_by_knownbits() < threshold,
1152 working_min_ne_threshold,
1153 z3.ULE(cl2set, set2cl),
1154 z3_tnum_condition(new_threshold, b1.tvalue, b1.tmask),
1155 )
1156
1157 def test_prove_shrink_bounds_by_knownbits_max_case1():
1158 # case 1, cl2set < set2cl
1159 b1 = make_z3_intbounds_instance('self')
1160
1161 threshold = b1.upper
1162 working_min, cl2set, set2cl = b1._helper_min_max_prepare(threshold)
1163 new_threshold = b1._helper_max_case1(working_min, set2cl)
1164
1165 # show that the new_threshold is smaller than threshold
1166 b1.prove_implies(
1167 b1._get_maximum_signed_by_knownbits() > threshold,
1168 working_min != threshold,
1169 z3.ULT(cl2set, set2cl),
1170 new_threshold < threshold,
1171 )
1172 # correctness: show that there are no elements x in b1 with
1173 # new_threshold <= x < threshold
1174 b1.prove_implies(
1175 b1._get_maximum_signed_by_knownbits() > threshold,
1176 working_min != threshold,
1177 z3.ULT(cl2set, set2cl),
1178 z3.Not(b1.concrete_variable > new_threshold),
1179 )
1180 # precision: new_threshold is an element in the set, ie we
1181 # couldn't have increased the bound further
1182 b1.prove_implies(
1183 b1._get_maximum_signed_by_knownbits() > threshold,
1184 working_min != threshold,
1185 z3.ULT(cl2set, set2cl),
1186 z3_tnum_condition(new_threshold, b1.tvalue, b1.tmask),
1187 )
1188
1189
1190 def test_prove_shrink_bounds_by_knownbits_correctness_max_case2():
1191 # case 2) cl2set >= set2cl
1192 b1 = make_z3_intbounds_instance('self')
1193
1194 threshold = b1.upper
1195 working_max, cl2set, set2cl = b1._helper_min_max_prepare(threshold)
1196 working_max_ne_threshold = working_max != threshold
1197
1198 new_threshold = b1._helper_max_case2(working_max, cl2set)
1199
1200 # check that the bound is not getting worse
1201 b1.prove_implies(
1202 b1._get_maximum_signed_by_knownbits() > threshold,
1203 working_max_ne_threshold,
1204 z3.UGE(cl2set, set2cl),
1205 new_threshold < threshold
1206 )
1207
1208 # correctness: show that there are no elements x in b1 with
1209 # threshold <= x < new_threshold
1210 b1.prove_implies(
1211 b1._get_maximum_signed_by_knownbits() > threshold,
1212 working_max_ne_threshold,
1213 z3.UGE(cl2set, set2cl),
1214 z3.Not(b1.concrete_variable > new_threshold),
1215 )
1216
1217 # precision: new_threshold is an element in the set, ie we
1218 # couldn't have increased the bound further
1219 b1.prove_implies(
1220 b1._get_maximum_signed_by_knownbits() > threshold,
1221 working_max_ne_threshold,
1222 z3.UGE(cl2set, set2cl),
1223 z3_tnum_condition(new_threshold, b1.tvalue, b1.tmask),
1224 )
1225
1226 # ____________________________________________________________
1227
1228 def z3_lshift_overflow(a, b):
1229 res = a << b
1230 return res, (res >> b) == a
1231
1232 def z3_min(*args):
1233 res = args[0]
1234 for x in args[1:]:
1235 res = z3.If(res < x, res, x)
1236 return res
1237
1238 def z3_max(*args):
1239 res = args[0]
1240 for x in args[1:]:
1241 res = z3.If(res > x, res, x)
1242 return res
1243
1244 def z3_saturating_mul(a, b):
1245 result, no_ovf = z3_mul_overflow(a, b)
1246 default = z3.If(same_sign(a, b), BitVecVal(MAXINT), BitVecVal(MININT))
1247 return z3.If(no_ovf, result, default)
1248
1249 @z3_with_reduced_bitwidth(16)
1250 def test_prove_lshift_bound_logic():
1251 b1 = make_z3_intbounds_instance('self')
1252 b2 = make_z3_intbounds_instance('other')
1253 # bounds logic
1254 result, no_ovf = z3_lshift_overflow(b1.concrete_variable, b2.concrete_variable)
1255 result1, no_ovf1 = z3_lshift_overflow(b1.lower, b2.lower)
1256 result2, no_ovf2 = z3_lshift_overflow(b1.lower, b2.upper)
1257 result3, no_ovf3 = z3_lshift_overflow(b1.upper, b2.lower)
1258 result4, no_ovf4 = z3_lshift_overflow(b1.upper, b2.upper)
1259 min1 = z3_min(result1, result2, result3, result4)
1260 max1 = z3_max(result1, result2, result3, result4)
1261 b1.prove_implies(
1262 b2,
1263 z3.And(no_ovf1, no_ovf2, no_ovf3, no_ovf4),
1264 z3.And(min1 <= result, result <= max1, no_ovf),
1265 )
1266
1267 def test_prove_lshift_knownbits():
1268 b1 = make_z3_intbounds_instance('self')
1269 c = BitVec('const')
1270 result = b1.concrete_variable << c
1271 tvalue, tmask = b1._tnum_lshift(c)
1272 b1.prove_implies(
1273 c >= 0,
1274 c < LONG_BIT,
1275 z3_tnum_condition(result, tvalue, tmask)
1276 )
1277
1278 def test_prove_lshift_bound_cannot_overflow_logic():
1279 b1 = make_z3_intbounds_instance('self')
1280 b2 = make_z3_intbounds_instance('other')
1281 res = b1.lshift_bound_cannot_overflow(b2)
1282 result, no_ovf = z3_lshift_overflow(b1.concrete_variable, b2.concrete_variable)
1283 b1.prove_implies(
1284 b2,
1285 res,
1286 no_ovf
1287 )
1288
1289 def test_prove_rshift_knownbits():
1290 b1 = make_z3_intbounds_instance('self')
1291 c = BitVec('const')
1292 result = b1.concrete_variable >> c
1293 tvalue, tmask = b1._tnum_rshift(c)
1294 b1.prove_implies(
1295 c >= 0,
1296 z3_tnum_condition(result, tvalue, tmask),
1297 )
1298
1299 def test_prove_rshift_bound_logic():
1300 b1 = make_z3_intbounds_instance('self')
1301 b2 = make_z3_intbounds_instance('other')
1302 # bounds logic
1303 result = b1.concrete_variable >> b2.concrete_variable
1304 result1 = b1.lower >> b2.lower
1305 result2 = b1.lower >> b2.upper
1306 result3 = b1.upper >> b2.lower
1307 result4 = b1.upper >> b2.upper
1308 min1 = z3_min(result1, result2, result3, result4)
1309 max1 = z3_max(result1, result2, result3, result4)
1310 b1.prove_implies(
1311 b2,
1312 b2.lower >= 0,
1313 min1 <= result,
1314 result <= max1,
1315 )
1316
1317 @z3_with_reduced_bitwidth(32)
1318 def test_2_prove_rshift_bound_logic():
1319 b1 = make_z3_intbounds_instance('self')
1320 b2 = make_z3_intbounds_instance('other')
1321 b3 = b1.rshift_bound(b2)
1322 b3.concrete_variable = result = b1.concrete_variable >> b2.concrete_variable
1323 b1.prove_implies(
1324 b2,
1325 b2.lower >= 0,
1326 b2.upper < LONG_BIT,
1327 b3,
1328 )
1329
1330 def test_prove_urshift_knownbits():
1331 b1 = make_z3_intbounds_instance('self')
1332 c = BitVec('const')
1333 result = z3.LShR(b1.concrete_variable, c)
1334 tvalue, tmask = b1._tnum_urshift(c)
1335 b1.prove_implies(
1336 c >= 0,
1337 z3_tnum_condition(result, tvalue, tmask),
1338 )
1339
1340 def test_prove_urshift_knownbits_logic():
1341 b1 = make_z3_intbounds_instance('self')
1342 b2 = make_z3_intbounds_instance('other')
1343 result = z3.LShR(b1.concrete_variable, b2.concrete_variable)
1344 b3 = b1.urshift_bound(b2)
1345 b3.concrete_variable = result
1346 b1.prove_implies(
1347 b2,
1348 b3,
1349 )
1350
1351 def test_prove_lshift_bound_backwards_logic():
1352 b1 = make_z3_intbounds_instance('self')
1353 c_other = BitVec('const')
1354 res = b1.concrete_variable << c_other
1355 bresult = make_z3_intbounds_instance('result', res)
1356 tvalue = z3.LShR(bresult.tvalue, c_other)
1357 tmask = z3.LShR(bresult.tmask, c_other)
1358 s_tmask = ~z3.LShR(-1, c_other)
1359 valid = (bresult.tvalue & ((1 << c_other) - 1)) == 0
1360 tmask |= s_tmask
1361 b1.prove_implies(
1362 bresult,
1363 0 <= c_other,
1364 c_other <= LONG_BIT,
1365 z3.And(
1366 valid,
1367 z3_tnum_condition(b1.concrete_variable, tvalue, tmask)
1368 ),
1369 )
1370
1371 def test_prove_rshift_bound_backwards_logic():
1372 b1 = make_z3_intbounds_instance('self')
1373 c_other = BitVec('const')
1374 res = b1.concrete_variable >> c_other
1375 bresult = make_z3_intbounds_instance('result', res)
1376 tvalue = bresult.tvalue << c_other
1377 tmask = bresult.tmask << c_other
1378 tmask |= (1 << c_other) - 1
1379 b1.prove_implies(
1380 bresult,
1381 0 <= c_other,
1382 c_other < LONG_BIT,
1383 z3_tnum_condition(b1.concrete_variable, tvalue, tmask)
1384 )
1385
1386 def test_prove_rshift_bound_backwards_logic2():
1387 b1 = make_z3_intbounds_instance('self')
1388 c_other = BitVec('const')
1389 res = b1.concrete_variable >> c_other
1390 bresult = make_z3_intbounds_instance('result', res)
1391 power = 1 << c_other
1392 lower_shifted, no_ovf1 = z3_mul_overflow(bresult.lower, power)
1393 upper_shifted, no_ovf2 = z3_mul_overflow(bresult.upper, power)
1394 lower = z3_min(lower_shifted, lower_shifted | (power - 1), upper_shifted, upper_shifted | (power - 1))
1395 upper = z3_max(lower_shifted, lower_shifted | (power - 1), upper_shifted, upper_shifted | (power - 1))
1396 b1.prove_implies(
1397 bresult,
1398 0 <= c_other,
1399 c_other < LONG_BIT,
1400 no_ovf1,
1401 no_ovf2,
1402 z3.And(lower <= b1.concrete_variable, b1.concrete_variable <= upper),
1403 )
1404
1405
1406 @z3_with_reduced_bitwidth(8)
1407 def test_prove_mul_bound_logic():
1408 b1 = make_z3_intbounds_instance('self')
1409 b2 = make_z3_intbounds_instance('other')
1410 result, no_ovf = z3_mul_overflow(b1.concrete_variable, b2.concrete_variable)
1411 result1, no_ovf1 = z3_mul_overflow(b1.lower, b2.lower)
1412 result2, no_ovf2 = z3_mul_overflow(b1.lower, b2.upper)
1413 result3, no_ovf3 = z3_mul_overflow(b1.upper, b2.lower)
1414 result4, no_ovf4 = z3_mul_overflow(b1.upper, b2.upper)
1415 min1 = z3_min(result1, result2, result3, result4)
1416 max1 = z3_max(result1, result2, result3, result4)
1417 b1.prove_implies(
1418 b2,
1419 z3.And(no_ovf1, no_ovf2, no_ovf3, no_ovf4),
1420 z3.And(min1 <= result, result <= max1, no_ovf),
1421 )
1422
1423 @z3_with_reduced_bitwidth(8)
1424 def test_prove_mul_bound_cannot_overflow_logic():
1425 b1 = make_z3_intbounds_instance('self')
1426 b2 = make_z3_intbounds_instance('other')
1427 result, no_ovf_result = z3_mul_overflow(b1.concrete_variable, b2.concrete_variable)
1428 result1, no_ovf1 = z3_mul_overflow(b1.lower, b2.lower)
1429 result2, no_ovf2 = z3_mul_overflow(b1.lower, b2.upper)
1430 result3, no_ovf3 = z3_mul_overflow(b1.upper, b2.lower)
1431 result4, no_ovf4 = z3_mul_overflow(b1.upper, b2.upper)
1432 b1.prove_implies(
1433 b2,
1434 z3.And(no_ovf1, no_ovf2, no_ovf3, no_ovf4),
1435 no_ovf_result,
1436 )
1437
1438 @z3_with_reduced_bitwidth(12)
1439 def test_prove_square_bound_no_overflow_logic():
1440 b1 = make_z3_intbounds_instance('self')
1441 result, no_ovf_result = z3_mul_overflow(b1.concrete_variable, b1.concrete_variable)
1442 val0 = z3_saturating_mul(b1.lower, b1.lower)
1443 val1 = z3_saturating_mul(b1.upper, b1.upper)
1444 lower = z3_min(val0, val1)
1445 upper = z3_max(val0, val1)
1446 lower = z3.If(same_sign(b1.lower, b1.upper), lower, BitVecVal(0))
1447 b1.prove_implies(
1448 no_ovf_result,
1449 z3.And(
1450 lower <= result,
1451 result <= upper))
1452
1453 def z3_pymod_nonzero(x, y):
1454 r = x % y
1455 res = r + (y & z3.If(y < 0, -r, r) >> (LONG_BIT - 1))
1456 return res, y != 0
1457
1458 @z3_with_reduced_bitwidth(16)
1459 def test_prove_mod_bound_logic():
1460 b1 = make_z3_intbounds_instance('self')
1461 b2 = make_z3_intbounds_instance('other')
1462 result, nonzero = z3_pymod_nonzero(b1.concrete_variable, b2.concrete_variable)
1463 b1.prove_implies(
1464 b2,
1465 nonzero,
1466 z3.And(result <= z3_max(b2.upper - 1, 0), z3_min(b2.lower + 1, 0) <= result)
1467 )
1468
1469
1470 # ____________________________________________________________
1471 # proofs for rewrite rules
1472
1473 def test_int_xor_neg_one_is_invert():
1474 x = BitVec('x')
1475 prove(x ^ -1 == ~x)
1476
1477 def test_uint_cmp_equivalent_int_cmp_if_same_sign():
1478 x = BitVec('x')
1479 y = BitVec('y')
1480 prove_implies(
1481 (x >= 0) == (y >= 0),
1482 z3.ULT(x, y) == (x < y),
1483 )
1484 prove_implies(
1485 (x >= 0) == (y >= 0),
1486 z3.ULE(x, y) == (x <= y),
1487 )
1488 prove_implies(
1489 (x >= 0) == (y >= 0),
1490 z3.UGT(x, y) == (x > y),
1491 )
1492 prove_implies(
1493 (x >= 0) == (y >= 0),
1494 z3.UGE(x, y) == (x >= y),
1495 )
1496
1497 def test_prove_int_mul_1_lshift_rewrite():
1498 x = BitVec('x')
1499 y = BitVec('y')
1500 prove_implies(
1501 0 <= y,
1502 y < LONG_BIT,
1503 x * (1 << y) == x << y
1504 )