| 1 |
|
| 2 |
|
| 3 |
""" The purpose of this test file is to check that the optimizeopt *test* cases |
| 4 |
are correct. It uses the z3 SMT solver to check the before/after optimization |
| 5 |
traces for equivalence. Only supports very few operations for now, but would |
| 6 |
have found the buggy tests in d9616aacbd02/issue #3832. |
| 7 |
|
| 8 |
It can also be used to do bounded model checking on the optimizer, by |
| 9 |
generating random traces.""" |
| 10 |
import sys |
| 11 |
import time, io |
| 12 |
import pytest |
| 13 |
import struct |
| 14 |
|
| 15 |
from rpython.rlib.rarithmetic import LONG_BIT, r_uint, intmask |
| 16 |
from rpython.rlib.longlong2float import float2longlong |
| 17 |
from rpython.jit.metainterp.optimizeopt.test.test_util import ( |
| 18 |
BaseTest, convert_old_style_to_targets, FakeJitDriverStaticData) |
| 19 |
from rpython.jit.metainterp.optimizeopt.test.test_optimizeintbound import ( |
| 20 |
TestOptimizeIntBounds as TOptimizeIntBounds) |
| 21 |
from rpython.jit.metainterp.optimizeopt.test.test_optimizeheap import ( |
| 22 |
TestOptimizeHeap as TOptimizeHeap) |
| 23 |
from rpython.rtyper import rclass |
| 24 |
from rpython.jit.metainterp import compile |
| 25 |
from rpython.jit.metainterp.resoperation import ( |
| 26 |
rop, ResOperation, InputArgInt, OpHelpers, InputArgRef, |
| 27 |
AbstractResOp) |
| 28 |
from rpython.jit.metainterp.history import ( |
| 29 |
JitCellToken, Const, ConstInt, ConstPtr, ConstFloat, |
| 30 |
get_const_ptr_for_string) |
| 31 |
from rpython.jit.tool.oparser import parse, convert_loop_to_trace |
| 32 |
from rpython.jit.backend.test.test_random import RandomLoop, Random, OperationBuilder, AbstractOperation |
| 33 |
from rpython.jit.backend.test.test_random import GuardPtrOperation |
| 34 |
from rpython.jit.backend.test import test_ll_random |
| 35 |
from rpython.jit.backend.llgraph.runner import LLGraphCPU |
| 36 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 37 |
from rpython.jit.metainterp.optimizeopt.intutils import MININT, MAXINT |
| 38 |
from rpython.jit.metainterp.history import new_ref_dict |
| 39 |
from rpython.rtyper.lltypesystem import lltype |
| 40 |
|
| 41 |
try: |
| 42 |
import z3 |
| 43 |
from hypothesis import given, strategies |
| 44 |
except ImportError: |
| 45 |
pytest.skip("please install z3 (z3-solver on pypi) and hypothesis") |
| 46 |
|
| 47 |
TRUEBV = z3.BitVecVal(1, LONG_BIT) |
| 48 |
FALSEBV = z3.BitVecVal(0, LONG_BIT) |
| 49 |
|
| 50 |
class CheckError(Exception): |
| 51 |
pass |
| 52 |
|
| 53 |
|
| 54 |
def check_z3(beforeinputargs, beforeops, afterinputargs, afterops): |
| 55 |
c = Checker(beforeinputargs, beforeops, afterinputargs, afterops) |
| 56 |
c.check() |
| 57 |
return c.unsat_count, c.unknown_count |
| 58 |
|
| 59 |
def z3_pydiv(x, y): |
| 60 |
r = x / y |
| 61 |
psubx = r * y - x |
| 62 |
return z3.If( |
| 63 |
y == z3.BitVecVal(0, LONG_BIT), |
| 64 |
z3.BitVecVal(0xdeadbeef, LONG_BIT), |
| 65 |
r + (z3.If(y < 0, psubx, -psubx) >> (LONG_BIT - 1))) |
| 66 |
|
| 67 |
def z3_pymod(x, y): |
| 68 |
r = x % y |
| 69 |
return z3.If( |
| 70 |
y == z3.BitVecVal(0, LONG_BIT), |
| 71 |
z3.BitVecVal(0xdeadbeef, LONG_BIT), |
| 72 |
r + (y & z3.If(y < 0, -r, r) >> (LONG_BIT - 1))) |
| 73 |
|
| 74 |
class Checker(object): |
| 75 |
def __init__(self, beforeinputargs, beforeops, afterinputargs, afterops): |
| 76 |
self.solver = z3.Solver() |
| 77 |
if pytest.config.option.z3timeout: |
| 78 |
self.solver.set("timeout", pytest.config.option.z3timeout) |
| 79 |
self.box_to_z3 = {} |
| 80 |
self.seen_names = {} |
| 81 |
self.constptr_to_z3 = new_ref_dict() |
| 82 |
self.beforeinputargs = beforeinputargs |
| 83 |
self.beforeops = beforeops |
| 84 |
self.afterinputargs = afterinputargs |
| 85 |
self.afterops = afterops |
| 86 |
self.unsat_count = self.unknown_count = 0 |
| 87 |
|
| 88 |
self.result_ovf = None |
| 89 |
self.heapindex = 0 |
| 90 |
self.arraycopyindex = 0 |
| 91 |
self.true_for_all_heaps = [] |
| 92 |
self.fresh_pointers = [] |
| 93 |
self._init_heap_types() |
| 94 |
|
| 95 |
def _init_heap_types(self): |
| 96 |
self.fielddescr_to_z3indexvar = {} |
| 97 |
|
| 98 |
self.nullpointer = z3.BitVec('NULL', LONG_BIT) |
| 99 |
self.solver_add(self.nullpointer == z3.BitVecVal(0, LONG_BIT)) |
| 100 |
|
| 101 |
self._lltype_to_index = {} |
| 102 |
|
| 103 |
def solver_add(self, cond): |
| 104 |
if z3.simplify(cond).eq(z3.BoolVal(True)): |
| 105 |
return |
| 106 |
res = self.solver.check(cond) |
| 107 |
|
| 108 |
if res == z3.unsat: |
| 109 |
assert 0, "programming error, trying to add something to solver that is equivalent to False: " + str(cond) |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
self.solver.add(cond) |
| 115 |
|
| 116 |
def fielddescr_indexvar(self, descr): |
| 117 |
if descr in self.fielddescr_to_z3indexvar: |
| 118 |
return self.fielddescr_to_z3indexvar[descr] |
| 119 |
repr = "%s_%s" % (descr.S._name, descr.fieldname) |
| 120 |
var = z3.BitVec(repr, LONG_BIT) |
| 121 |
self.solver_add(var == len(self.fielddescr_to_z3indexvar)) |
| 122 |
self.fielddescr_to_z3indexvar[descr] = var |
| 123 |
return var |
| 124 |
|
| 125 |
def _lltype_heaptypes_index(self, T): |
| 126 |
if T in self._lltype_to_index: |
| 127 |
return self._lltype_to_index[T] |
| 128 |
|
| 129 |
res = ~len(self._lltype_to_index) |
| 130 |
if isinstance(T, lltype.GcArray): |
| 131 |
varname = 'typeindex_array_%s' % (~res) |
| 132 |
else: |
| 133 |
varname = 'typeindex_struct_%s' % T._name |
| 134 |
z3heaptypeindex = z3.BitVec(self._unique_name(varname), LONG_BIT) |
| 135 |
self.solver_add(z3heaptypeindex == res) |
| 136 |
self._lltype_to_index[T] = z3heaptypeindex |
| 137 |
return z3heaptypeindex |
| 138 |
|
| 139 |
def convert(self, box): |
| 140 |
if isinstance(box, ConstInt): |
| 141 |
return z3.BitVecVal(box.getint(), LONG_BIT) |
| 142 |
if isinstance(box, ConstPtr): |
| 143 |
if not box.value: |
| 144 |
return self.nullpointer |
| 145 |
if box.value in self.constptr_to_z3: |
| 146 |
return self.constptr_to_z3[box.value] |
| 147 |
res = z3.BitVec('constPTR_%s' % len(self.constptr_to_z3), LONG_BIT) |
| 148 |
self.constptr_to_z3[box.value] = res |
| 149 |
is_array = False |
| 150 |
try: |
| 151 |
typeptr = lltype.cast_opaque_ptr(rclass.OBJECTPTR, box.value).typeptr |
| 152 |
except lltype.InvalidCast: |
| 153 |
T = lltype.typeOf(box.value._obj.container) |
| 154 |
typ = self._lltype_heaptypes_index(T) |
| 155 |
if isinstance(T, lltype.GcArray): |
| 156 |
is_array = True |
| 157 |
else: |
| 158 |
typ = typeptr.subclassrange_min |
| 159 |
self.solver_add(self.state.heaptypes[res] == typ) |
| 160 |
for freshptr in self.fresh_pointers: |
| 161 |
self.solver_add(freshptr != res) |
| 162 |
if is_array: |
| 163 |
self.solver_add(self.state.arraylength[res] == box.value._obj.container.getlength()) |
| 164 |
else: |
| 165 |
self.solver_add(self.state.arraylength[res] == -1) |
| 166 |
return res |
| 167 |
if isinstance(box, ConstFloat): |
| 168 |
if LONG_BIT != 64: |
| 169 |
pytest.skip("float checking not supported on 32-bit") |
| 170 |
return z3.BitVecVal(float2longlong(box.getfloat()), LONG_BIT) |
| 171 |
|
| 172 |
assert not isinstance(box, Const) |
| 173 |
return self.box_to_z3[box] |
| 174 |
|
| 175 |
def convertarg(self, box, arg): |
| 176 |
return self.convert(box.getarg(arg)) |
| 177 |
|
| 178 |
def newvar(self, box, repr=None): |
| 179 |
if repr is None: |
| 180 |
repr = box.repr_short(box._repr_memo) |
| 181 |
repr = self._unique_name(repr) |
| 182 |
|
| 183 |
result = z3.BitVec(repr, LONG_BIT) |
| 184 |
self.box_to_z3[box] = result |
| 185 |
return result |
| 186 |
|
| 187 |
def _unique_name(self, repr): |
| 188 |
while repr in self.seen_names: |
| 189 |
repr += "_" |
| 190 |
self.seen_names[repr] = None |
| 191 |
return repr |
| 192 |
|
| 193 |
def newheaptypes(self): |
| 194 |
pointersort = typesort = z3.BitVecSort(LONG_BIT) |
| 195 |
heaptypes = z3.Array('types', pointersort, typesort) |
| 196 |
self.solver_add(heaptypes[self.nullpointer] == -1) |
| 197 |
return heaptypes |
| 198 |
|
| 199 |
def newarraylength(self): |
| 200 |
pointersort = arraylengthsort = z3.BitVecSort(LONG_BIT) |
| 201 |
return z3.Array('arraylength', pointersort, arraylengthsort) |
| 202 |
|
| 203 |
def newheap(self): |
| 204 |
pointersort = z3.BitVecSort(LONG_BIT) |
| 205 |
heapobjectsort = z3.ArraySort(pointersort, pointersort) |
| 206 |
self.heapindex += 1 |
| 207 |
heap = z3.Array('heap%s'% self.heapindex, pointersort, heapobjectsort) |
| 208 |
for ptr, index, res in self.true_for_all_heaps: |
| 209 |
self.solver_add(heap[ptr][index] == res) |
| 210 |
return heap |
| 211 |
|
| 212 |
def print_chunk(self, chunk, label, model): |
| 213 |
print |
| 214 |
print "=============", label, "==================" |
| 215 |
for op in chunk: |
| 216 |
if op in self.box_to_z3: |
| 217 |
text = "-----> " + hex(intmask(r_uint(int(str(model[self.box_to_z3[op]]))))) |
| 218 |
else: |
| 219 |
text = "" |
| 220 |
print op, text |
| 221 |
|
| 222 |
def prove(self, cond, *ops): |
| 223 |
z3res = self.solver.check(z3.Not(cond)) |
| 224 |
if z3res == z3.unsat: |
| 225 |
self.unsat_count += 1 |
| 226 |
elif z3res == z3.unknown: |
| 227 |
self.unknown_count += 1 |
| 228 |
elif z3res == z3.sat: |
| 229 |
|
| 230 |
|
| 231 |
model = self.solver.model() |
| 232 |
print "ERROR counterexample:" |
| 233 |
print "inputs:" |
| 234 |
for beforeinput, afterinput in zip(self.beforeinputargs, self.afterinputargs): |
| 235 |
if model[self.box_to_z3[beforeinput]] is not None: |
| 236 |
print beforeinput, afterinput, hex(intmask(r_uint(int(str(model[self.box_to_z3[beforeinput]]))))) |
| 237 |
else: |
| 238 |
print beforeinput, afterinput, "unassigned in the model" |
| 239 |
print "chunks:" |
| 240 |
for i, chunk in enumerate(self.chunks): |
| 241 |
beforechunk, beforelast, afterchunk, afterlast = chunk |
| 242 |
if i == self.chunkindex: |
| 243 |
print "vvvvvvvvvvvvvvv Problem vvvvvvvvvvvvvvv" |
| 244 |
self.print_chunk(beforechunk + [beforelast], "before", model) |
| 245 |
self.print_chunk(afterchunk + [afterlast], "after", model) |
| 246 |
print |
| 247 |
if i == self.chunkindex: |
| 248 |
break |
| 249 |
print "END counterexample" |
| 250 |
|
| 251 |
|
| 252 |
l = [] |
| 253 |
if ops: |
| 254 |
l.append("in the following ops:") |
| 255 |
for op in ops: |
| 256 |
l.append(str(op)) |
| 257 |
l.append("the following SMT condition was not provable:") |
| 258 |
l.append(str(cond)) |
| 259 |
l.append("_________________") |
| 260 |
l.append("smt model:") |
| 261 |
l.append(str(self.solver)) |
| 262 |
l.append("_________________") |
| 263 |
l.append("counterexample:") |
| 264 |
l.append(str(model)) |
| 265 |
raise CheckError("\n".join(l)) |
| 266 |
|
| 267 |
def cond(self, z3expr): |
| 268 |
return z3.If(z3expr, TRUEBV, FALSEBV) |
| 269 |
|
| 270 |
def add_to_solver(self, ops, state): |
| 271 |
self.state = state |
| 272 |
for op in ops: |
| 273 |
if op.type != 'v': |
| 274 |
res = self.newvar(op) |
| 275 |
else: |
| 276 |
res = None |
| 277 |
|
| 278 |
opname = op.getopname() |
| 279 |
descr = op.getdescr() |
| 280 |
|
| 281 |
arg0 = arg1 = arg2 = None |
| 282 |
if not op.is_guard(): |
| 283 |
state.no_ovf = None |
| 284 |
else: |
| 285 |
assert state.before |
| 286 |
cond = self.guard_to_condition(op, state) |
| 287 |
self.prove(cond, op) |
| 288 |
continue |
| 289 |
|
| 290 |
|
| 291 |
if op.numargs() == 1: |
| 292 |
arg0 = self.convert(op.getarg(0)) |
| 293 |
elif op.numargs() == 2: |
| 294 |
arg0 = self.convert(op.getarg(0)) |
| 295 |
arg1 = self.convert(op.getarg(1)) |
| 296 |
elif op.numargs() == 3: |
| 297 |
arg0 = self.convert(op.getarg(0)) |
| 298 |
arg1 = self.convert(op.getarg(1)) |
| 299 |
arg2 = self.convert(op.getarg(2)) |
| 300 |
|
| 301 |
|
| 302 |
if opname == "int_add": |
| 303 |
expr = arg0 + arg1 |
| 304 |
elif opname == "int_sub": |
| 305 |
expr = arg0 - arg1 |
| 306 |
elif opname == "int_mul": |
| 307 |
expr = arg0 * arg1 |
| 308 |
elif opname == "int_and": |
| 309 |
expr = arg0 & arg1 |
| 310 |
elif opname == "int_or": |
| 311 |
expr = arg0 | arg1 |
| 312 |
elif opname == "int_xor": |
| 313 |
expr = arg0 ^ arg1 |
| 314 |
elif opname == "int_eq": |
| 315 |
expr = self.cond(arg0 == arg1) |
| 316 |
elif opname == "int_ne": |
| 317 |
expr = self.cond(arg0 != arg1) |
| 318 |
elif opname == "int_lt": |
| 319 |
expr = self.cond(arg0 < arg1) |
| 320 |
elif opname == "int_le": |
| 321 |
expr = self.cond(arg0 <= arg1) |
| 322 |
elif opname == "int_gt": |
| 323 |
expr = self.cond(arg0 > arg1) |
| 324 |
elif opname == "int_ge": |
| 325 |
expr = self.cond(arg0 >= arg1) |
| 326 |
elif opname == "int_is_true": |
| 327 |
expr = self.cond(arg0 != FALSEBV) |
| 328 |
elif opname == "uint_lt": |
| 329 |
expr = self.cond(z3.ULT(arg0, arg1)) |
| 330 |
elif opname == "uint_le": |
| 331 |
expr = self.cond(z3.ULE(arg0, arg1)) |
| 332 |
elif opname == "uint_gt": |
| 333 |
expr = self.cond(z3.UGT(arg0, arg1)) |
| 334 |
elif opname == "uint_ge": |
| 335 |
expr = self.cond(z3.UGE(arg0, arg1)) |
| 336 |
elif opname == "int_is_zero": |
| 337 |
expr = self.cond(arg0 == FALSEBV) |
| 338 |
elif opname == "int_neg": |
| 339 |
expr = -arg0 |
| 340 |
elif opname == "int_invert": |
| 341 |
expr = ~arg0 |
| 342 |
elif opname == "int_lshift": |
| 343 |
expr = arg0 << arg1 |
| 344 |
elif opname == "int_rshift": |
| 345 |
expr = arg0 >> arg1 |
| 346 |
elif opname == "uint_rshift": |
| 347 |
expr = z3.LShR(arg0, arg1) |
| 348 |
elif opname == "int_add_ovf": |
| 349 |
expr = arg0 + arg1 |
| 350 |
m = z3.SignExt(LONG_BIT, arg0) + z3.SignExt(LONG_BIT, arg1) |
| 351 |
state.no_ovf = m == z3.SignExt(LONG_BIT, expr) |
| 352 |
elif opname == "int_sub_ovf": |
| 353 |
expr = arg0 - arg1 |
| 354 |
m = z3.SignExt(LONG_BIT, arg0) - z3.SignExt(LONG_BIT, arg1) |
| 355 |
state.no_ovf = m == z3.SignExt(LONG_BIT, expr) |
| 356 |
elif opname == "int_mul_ovf": |
| 357 |
expr = arg0 * arg1 |
| 358 |
m = z3.SignExt(LONG_BIT, arg0) * z3.SignExt(LONG_BIT, arg1) |
| 359 |
state.no_ovf = m == z3.SignExt(LONG_BIT, expr) |
| 360 |
elif opname == "int_signext": |
| 361 |
numbits = op.getarg(1).getint() * 8 |
| 362 |
expr = z3.SignExt(64 - numbits, z3.Extract(numbits - 1, 0, arg0)) |
| 363 |
elif opname == "int_force_ge_zero": |
| 364 |
expr = z3.If(arg0 < 0, 0, arg0) |
| 365 |
elif opname == "uint_mul_high": |
| 366 |
|
| 367 |
|
| 368 |
zarg0 = z3.ZeroExt(LONG_BIT, arg0) |
| 369 |
zarg1 = z3.ZeroExt(LONG_BIT, arg1) |
| 370 |
expr = z3.Extract(LONG_BIT * 2 - 1, LONG_BIT, zarg0 * zarg1) |
| 371 |
elif opname == "same_as_i" or opname == "same_as_r": |
| 372 |
expr = arg0 |
| 373 |
|
| 374 |
elif opname == "ptr_eq" or opname == "instance_ptr_eq": |
| 375 |
expr = self.cond(arg0 == arg1) |
| 376 |
elif opname == "ptr_ne" or opname == "instance_ptr_ne": |
| 377 |
expr = self.cond(arg0 != arg1) |
| 378 |
elif opname == "new_with_vtable": |
| 379 |
expr = res |
| 380 |
self.fresh_pointer(res) |
| 381 |
vtable = descr.get_vtable().adr.ptr |
| 382 |
self.solver_add(state.heaptypes[expr] == vtable.subclassrange_min) |
| 383 |
self.solver_add(state.heap[expr] == z3.K(z3.BitVecSort(LONG_BIT), z3.BitVecVal(0, LONG_BIT))) |
| 384 |
elif opname == "getfield_gc_i" or opname == "getfield_gc_r": |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
index = self.fielddescr_indexvar(descr) |
| 389 |
parentdescr = descr.get_parent_descr() |
| 390 |
if parentdescr.is_object(): |
| 391 |
vtable = parentdescr.get_vtable().adr.ptr |
| 392 |
self.solver_add(state.heaptypes[arg0] >= vtable.subclassrange_min) |
| 393 |
self.solver_add(state.heaptypes[arg0] <= vtable.subclassrange_max) |
| 394 |
expr = state.heap[arg0][index] |
| 395 |
if descr.is_always_pure(): |
| 396 |
self.true_for_all_heaps.append((arg0, index, expr)) |
| 397 |
if isinstance(op.getarg(0), ConstPtr) and descr.is_always_pure(): |
| 398 |
ptr = lltype.cast_opaque_ptr(lltype.Ptr(descr.S), op.getarg(0).value) |
| 399 |
const_res = getattr(ptr, descr.fieldname) |
| 400 |
assert opname == "getfield_gc_i" |
| 401 |
self.solver_add(expr == const_res) |
| 402 |
if descr.is_integer_bounded(): |
| 403 |
self.solver_add(expr >= descr.get_integer_min()) |
| 404 |
self.solver_add(expr <= descr.get_integer_max()) |
| 405 |
elif opname == "setfield_gc": |
| 406 |
index = self.fielddescr_indexvar(descr) |
| 407 |
|
| 408 |
heapexpr = z3.Store(state.heap, arg0, z3.Store(state.heap[arg0], index, arg1)) |
| 409 |
parentdescr = descr.get_parent_descr() |
| 410 |
if parentdescr.is_object(): |
| 411 |
vtable = parentdescr.get_vtable().adr.ptr |
| 412 |
self.solver_add(state.heaptypes[arg0] >= vtable.subclassrange_min) |
| 413 |
self.solver_add(state.heaptypes[arg0] <= vtable.subclassrange_max) |
| 414 |
|
| 415 |
state.heap = self.newheap() |
| 416 |
|
| 417 |
self.solver_add(state.heap == heapexpr) |
| 418 |
|
| 419 |
if self.is_const(op.getarg(1)): |
| 420 |
self.solver_add(arg0 != self.nullpointer) |
| 421 |
elif opname == "getarrayitem_gc_r" or opname == "getarrayitem_gc_i" or opname == "getarrayitem_gc_f": |
| 422 |
|
| 423 |
z3typ = self._lltype_heaptypes_index(descr.A) |
| 424 |
self.solver_add(state.heaptypes[arg0] == z3typ) |
| 425 |
self.solver_add(arg1 >= 0) |
| 426 |
self.solver_add(arg1 < state.arraylength[arg0]) |
| 427 |
expr = state.heap[arg0][arg1] |
| 428 |
if descr.is_item_integer_bounded(): |
| 429 |
self.solver_add(expr >= descr.get_item_integer_min()) |
| 430 |
self.solver_add(expr <= descr.get_item_integer_max()) |
| 431 |
elif opname == "setarrayitem_gc": |
| 432 |
heapexpr = z3.Store(state.heap, arg0, z3.Store(state.heap[arg0], arg1, arg2)) |
| 433 |
z3typ = self._lltype_heaptypes_index(descr.A) |
| 434 |
self.solver_add(state.heaptypes[arg0] == z3typ) |
| 435 |
self.solver_add(arg1 >= 0) |
| 436 |
self.solver_add(arg1 < state.arraylength[arg0]) |
| 437 |
state.heap = self.newheap() |
| 438 |
self.solver_add(state.heap == heapexpr) |
| 439 |
if self.is_const(op.getarg(2)): |
| 440 |
self.solver_add(arg0 != self.nullpointer) |
| 441 |
elif opname == "arraylen_gc": |
| 442 |
expr = state.arraylength[arg0] |
| 443 |
self.solver_add(expr >= 0) |
| 444 |
elif opname == "new_array" or opname == "new_array_clear": |
| 445 |
expr = res |
| 446 |
self.fresh_pointer(res) |
| 447 |
z3typ = self._lltype_heaptypes_index(descr.A) |
| 448 |
self.solver_add(state.heaptypes[res] == z3typ) |
| 449 |
|
| 450 |
self.solver_add(res != self.nullpointer) |
| 451 |
|
| 452 |
self.solver_add(state.arraylength[res] == arg0) |
| 453 |
self.solver_add(state.heap[expr] == z3.K(z3.BitVecSort(LONG_BIT), z3.BitVecVal(0, LONG_BIT))) |
| 454 |
elif opname == "escape_n": |
| 455 |
|
| 456 |
|
| 457 |
state.heap = state.nextheap(self) |
| 458 |
|
| 459 |
elif opname in ["label", "escape_i", "debug_merge_point", "force_token"]: |
| 460 |
|
| 461 |
continue |
| 462 |
elif opname == "call_n": |
| 463 |
effectinfo = op.getdescr().get_extra_info() |
| 464 |
oopspecindex = effectinfo.oopspecindex |
| 465 |
if oopspecindex == EffectInfo.OS_ARRAYCOPY or oopspecindex == EffectInfo.OS_ARRAYMOVE: |
| 466 |
array_from = self.convert(op.getarg(1)) |
| 467 |
if oopspecindex == EffectInfo.OS_ARRAYCOPY: |
| 468 |
array_to = self.convert(op.getarg(2)) |
| 469 |
index_from = self.convert(op.getarg(3)) |
| 470 |
index_to = self.convert(op.getarg(4)) |
| 471 |
len_arg = op.getarg(5) |
| 472 |
copy_len = self.convert(len_arg) |
| 473 |
else: |
| 474 |
array_to = array_from |
| 475 |
index_from = self.convert(op.getarg(2)) |
| 476 |
index_to = self.convert(op.getarg(3)) |
| 477 |
len_arg = op.getarg(4) |
| 478 |
copy_len = self.convert(len_arg) |
| 479 |
|
| 480 |
z3typ = self._lltype_heaptypes_index(effectinfo.single_write_descr_array.A) |
| 481 |
|
| 482 |
self.solver_add(state.heaptypes[array_from] == z3typ) |
| 483 |
if oopspecindex == EffectInfo.OS_ARRAYCOPY: |
| 484 |
self.solver_add(state.heaptypes[array_to] == z3typ) |
| 485 |
|
| 486 |
if self.is_const(len_arg): |
| 487 |
len_arg_const = len_arg.getint() |
| 488 |
|
| 489 |
if len_arg_const == 0: |
| 490 |
continue |
| 491 |
|
| 492 |
curr_heap = state.heap |
| 493 |
for i in range(len_arg_const): |
| 494 |
heapexpr = z3.Store(curr_heap, array_to, z3.Store(curr_heap[array_to], index_to + i, curr_heap[array_from][index_from + i])) |
| 495 |
new_heap = self.newheap() |
| 496 |
self.solver.add(new_heap == heapexpr) |
| 497 |
curr_heap = new_heap |
| 498 |
state.heap = curr_heap |
| 499 |
continue |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
arr_range_from = z3.BitVec('__arr_index_from_%d' % self.arraycopyindex, LONG_BIT) |
| 508 |
arr_range_to = z3.BitVec('__arr_index_to_%d' % self.arraycopyindex, LONG_BIT) |
| 509 |
other_range = z3.BitVec('__arr_other_index_%d' % self.arraycopyindex, LONG_BIT) |
| 510 |
p = z3.BitVec('__arr_pointer_%d' % self.arraycopyindex, LONG_BIT) |
| 511 |
|
| 512 |
|
| 513 |
new_heap = self.newheap() |
| 514 |
|
| 515 |
|
| 516 |
self.solver.add(z3.ForAll([arr_range_from, arr_range_to], x: |
| 517 |
z3.Implies(z3.And( |
| 518 |
z3.And(arr_range_from >= index_from, arr_range_from < index_from + copy_len), |
| 519 |
z3.And(arr_range_to >= index_to, arr_range_to < arr_range_to + copy_len)), |
| 520 |
new_heap[array_to][arr_range_to] == state.heap[array_from][arr_range_from]))) |
| 521 |
|
| 522 |
|
| 523 |
self.solver.add(z3.ForAll([other_range], |
| 524 |
z3.Implies(z3.And(other_range < index_to, other_range >= index_to + copy_len), |
| 525 |
new_heap[array_to][other_range] == state.heap[array_to][other_range]))) |
| 526 |
|
| 527 |
|
| 528 |
self.solver.add(z3.ForAll([p], |
| 529 |
z3.Implies(p != array_to, new_heap[p] == state.heap[p]))) |
| 530 |
state.heap = new_heap |
| 531 |
self.arraycopyindex += 1 |
| 532 |
else: |
| 533 |
assert 0, "unsupported" |
| 534 |
elif opname == "call_pure_i" or opname == "call_i": |
| 535 |
|
| 536 |
effectinfo = op.getdescr().get_extra_info() |
| 537 |
oopspecindex = effectinfo.oopspecindex |
| 538 |
if oopspecindex == EffectInfo.OS_INT_PY_DIV: |
| 539 |
arg0 = self.convert(op.getarg(1)) |
| 540 |
arg1 = self.convert(op.getarg(2)) |
| 541 |
expr = z3_pydiv(arg0, arg1) |
| 542 |
elif oopspecindex == EffectInfo.OS_INT_PY_MOD: |
| 543 |
arg0 = self.convert(op.getarg(1)) |
| 544 |
arg1 = self.convert(op.getarg(2)) |
| 545 |
expr = z3_pymod(arg0, arg1) |
| 546 |
else: |
| 547 |
assert 0, "unsupported" |
| 548 |
else: |
| 549 |
assert 0, "unsupported" |
| 550 |
if res is not None: |
| 551 |
self.solver_add(res == expr) |
| 552 |
|
| 553 |
def is_const(self, arg): |
| 554 |
return isinstance(arg, Const) or isinstance(arg, ConstPtr) |
| 555 |
|
| 556 |
def fresh_pointer(self, res): |
| 557 |
for box, var in self.box_to_z3.iteritems(): |
| 558 |
if box.type == "r" and res is not var: |
| 559 |
self.solver_add(res != var) |
| 560 |
for const, z3var in self.constptr_to_z3.iteritems(): |
| 561 |
self.solver_add(res != z3var) |
| 562 |
self.fresh_pointers.append(res) |
| 563 |
|
| 564 |
def guard_to_condition(self, guard, state): |
| 565 |
opname = guard.getopname() |
| 566 |
if opname == "guard_true": |
| 567 |
return self.convertarg(guard, 0) == TRUEBV |
| 568 |
elif opname == "guard_false": |
| 569 |
return self.convertarg(guard, 0) == FALSEBV |
| 570 |
elif opname == "guard_value": |
| 571 |
return self.convertarg(guard, 0) == self.convertarg(guard, 1) |
| 572 |
elif opname == "guard_no_overflow": |
| 573 |
assert state.no_ovf is not None |
| 574 |
return state.no_ovf |
| 575 |
elif opname == "guard_overflow": |
| 576 |
assert state.no_ovf is not None |
| 577 |
return z3.Not(state.no_ovf) |
| 578 |
elif opname == "guard_class": |
| 579 |
arg0 = self.convertarg(guard, 0) |
| 580 |
cls = guard.getarg(1) |
| 581 |
vtable = cls.value.adr.ptr |
| 582 |
return state.heaptypes[arg0] == vtable.subclassrange_min |
| 583 |
elif opname == "guard_nonnull": |
| 584 |
|
| 585 |
return self.convertarg(guard, 0) != self.nullpointer |
| 586 |
elif opname == "guard_isnull": |
| 587 |
return self.convertarg(guard, 0) == self.nullpointer |
| 588 |
else: |
| 589 |
assert 0, "unsupported " + opname |
| 590 |
|
| 591 |
def check_last(self, beforelast, state_before, afterlast, state_after): |
| 592 |
if beforelast.getopname() in ("jump", "finish"): |
| 593 |
assert beforelast.numargs() == afterlast.numargs() |
| 594 |
for i in range(beforelast.numargs()): |
| 595 |
beforearg = beforelast.getarg(i) |
| 596 |
if beforearg.type == 'r' and isinstance(beforearg, AbstractResOp): |
| 597 |
if beforearg.opnum in (rop.NEW_WITH_VTABLE, rop.NEW, rop.NEW_ARRAY, rop.NEW_ARRAY_CLEAR): |
| 598 |
continue |
| 599 |
before = self.convertarg(beforelast, i) |
| 600 |
after = self.convertarg(afterlast, i) |
| 601 |
self.prove(before == after, beforelast, afterlast) |
| 602 |
return |
| 603 |
assert beforelast.is_guard() |
| 604 |
|
| 605 |
cond_before = self.guard_to_condition(beforelast, state_before) |
| 606 |
if afterlast is None: |
| 607 |
|
| 608 |
self.prove(cond_before, beforelast) |
| 609 |
else: |
| 610 |
cond_after = self.guard_to_condition(afterlast, state_after) |
| 611 |
equivalent = cond_before == cond_after |
| 612 |
self.prove(equivalent, beforelast, afterlast) |
| 613 |
|
| 614 |
self.solver_add(cond_before) |
| 615 |
if afterlast: |
| 616 |
self.solver_add(cond_after) |
| 617 |
|
| 618 |
def check(self): |
| 619 |
for beforeinput, afterinput in zip(self.beforeinputargs, self.afterinputargs): |
| 620 |
self.box_to_z3[beforeinput] = self.newvar(afterinput, "input_%s_%s" % (beforeinput, afterinput)) |
| 621 |
|
| 622 |
state_before = State() |
| 623 |
state_after = State(state_before) |
| 624 |
state_before.heap = state_after.heap = self.newheap() |
| 625 |
state_before.heaptypes = state_after.heaptypes = self.newheaptypes() |
| 626 |
state_before.arraylength = state_after.arraylength = self.newarraylength() |
| 627 |
self.chunks = list(chunk_ops(self.beforeops, self.afterops)) |
| 628 |
for chunkindex, (beforechunk, beforelast, afterchunk, afterlast) in enumerate(self.chunks): |
| 629 |
self.chunkindex = chunkindex |
| 630 |
self.add_to_solver(beforechunk, state_before) |
| 631 |
self.add_to_solver(afterchunk, state_after) |
| 632 |
self.check_last(beforelast, state_before, afterlast, state_after) |
| 633 |
|
| 634 |
class State(object): |
| 635 |
def __init__(self, state_before=None): |
| 636 |
self.before = state_before is None |
| 637 |
self.no_ovf = None |
| 638 |
if self.before: |
| 639 |
self.heap_sequence = [] |
| 640 |
else: |
| 641 |
self.heap_sequence = state_before.heap_sequence |
| 642 |
self.heap_index = 0 |
| 643 |
|
| 644 |
def nextheap(self, checker): |
| 645 |
if self.before: |
| 646 |
res = checker.newheap() |
| 647 |
self.heap_sequence.append(res) |
| 648 |
else: |
| 649 |
res = self.heap_sequence[self.heap_index] |
| 650 |
self.heap_index += 1 |
| 651 |
return res |
| 652 |
|
| 653 |
def chunk_ops(beforeops, afterops): |
| 654 |
beforeops = list(reversed(beforeops)) |
| 655 |
afterops = list(reversed(afterops)) |
| 656 |
while 1: |
| 657 |
if not beforeops: |
| 658 |
assert not afterops |
| 659 |
return |
| 660 |
beforechunk, beforelast = up_to_guard(beforeops) |
| 661 |
afterchunk, afterlast = up_to_guard(afterops) |
| 662 |
while (beforelast is not None and |
| 663 |
(afterlast is None or |
| 664 |
beforelast.rd_resume_position < afterlast.rd_resume_position)): |
| 665 |
beforechunk.append(beforelast) |
| 666 |
bc, beforelast = up_to_guard(beforeops) |
| 667 |
beforechunk.extend(bc) |
| 668 |
if beforelast is None: |
| 669 |
beforelast = beforechunk.pop() |
| 670 |
if afterlast is None and afterchunk: |
| 671 |
afterlast = afterchunk.pop() |
| 672 |
yield beforechunk, beforelast, afterchunk, afterlast |
| 673 |
|
| 674 |
|
| 675 |
def up_to_guard(oplist): |
| 676 |
res = [] |
| 677 |
while oplist: |
| 678 |
op = oplist.pop() |
| 679 |
if op.is_guard(): |
| 680 |
return res, op |
| 681 |
res.append(op) |
| 682 |
return res, None |
| 683 |
|
| 684 |
def test_solver_add_false_protection(): |
| 685 |
c = Checker(None, None, None, None) |
| 686 |
x = z3.BitVec('x', 64) |
| 687 |
c.solver_add(x == 1) |
| 688 |
with pytest.raises(AssertionError): |
| 689 |
c.solver_add(x != 1) |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
class BaseCheckZ3(BaseTest): |
| 695 |
|
| 696 |
enable_opts = "intbounds:rewrite:virtualize:string:earlyforce:pure:heap" |
| 697 |
|
| 698 |
def optimize_loop(self, ops, optops, call_pure_results=None): |
| 699 |
from rpython.jit.metainterp.opencoder import Trace, TraceIterator |
| 700 |
loop = self.parse(ops) |
| 701 |
token = JitCellToken() |
| 702 |
if loop.operations[-1].getopnum() == rop.JUMP: |
| 703 |
loop.operations[-1].setdescr(token) |
| 704 |
exp = parse(optops, namespace=self.namespace.copy()) |
| 705 |
expected = convert_old_style_to_targets(exp, jump=True) |
| 706 |
call_pure_results = self._convert_call_pure_results(call_pure_results) |
| 707 |
trace = convert_loop_to_trace(loop, self.metainterp_sd) |
| 708 |
compile_data = compile.SimpleCompileData( |
| 709 |
trace, call_pure_results=call_pure_results, |
| 710 |
enable_opts=self.enable_opts) |
| 711 |
compile_data.forget_optimization_info = lambda *args, **kwargs: None |
| 712 |
jitdriver_sd = FakeJitDriverStaticData() |
| 713 |
info, ops = compile_data.optimize_trace(self.metainterp_sd, jitdriver_sd, {}) |
| 714 |
beforeinputargs, beforeops = trace.unpack() |
| 715 |
|
| 716 |
correct, timeout = check_z3(beforeinputargs, beforeops, info.inputargs, ops) |
| 717 |
print 'correct conditions:', correct, 'timed out conditions:', timeout |
| 718 |
|
| 719 |
|
| 720 |
class TestBuggyTestsFail(BaseCheckZ3): |
| 721 |
def check_z3_throws_error(self, ops, optops, call_pure_results=None): |
| 722 |
from rpython.jit.metainterp.opencoder import Trace, TraceIterator |
| 723 |
loop = self.parse(ops) |
| 724 |
token = JitCellToken() |
| 725 |
if loop.operations[-1].getopnum() == rop.JUMP: |
| 726 |
loop.operations[-1].setdescr(token) |
| 727 |
trace = convert_loop_to_trace(loop, self.metainterp_sd) |
| 728 |
beforeinputargs, beforeops = trace.unpack() |
| 729 |
loopopt = self.parse(optops) |
| 730 |
if loopopt.operations[-1].getopnum() == rop.JUMP: |
| 731 |
loopopt.operations[-1].setdescr(token) |
| 732 |
opttrace = convert_loop_to_trace(loopopt, self.metainterp_sd) |
| 733 |
afterinputargs, afterops = opttrace.unpack() |
| 734 |
for afterop in afterops: |
| 735 |
assert not afterop.is_guard() |
| 736 |
with pytest.raises(CheckError): |
| 737 |
check_z3(beforeinputargs, beforeops, afterinputargs, afterops) |
| 738 |
|
| 739 |
def test_duplicate_getarrayitem_after_setarrayitem_3(self): |
| 740 |
ops = """ |
| 741 |
[p1, p2, p3, i1] |
| 742 |
setarrayitem_gc(p1, i1, p2, descr=arraydescr2) |
| 743 |
setarrayitem_gc(p1, 0, p3, descr=arraydescr2) |
| 744 |
p5 = getarrayitem_gc_r(p1, i1, descr=arraydescr2) |
| 745 |
jump(p5) |
| 746 |
""" |
| 747 |
expected = """ |
| 748 |
[p1, p2, p3, i1] |
| 749 |
setarrayitem_gc(p1, i1, p2, descr=arraydescr2) |
| 750 |
setarrayitem_gc(p1, 0, p3, descr=arraydescr2) |
| 751 |
jump(p2) |
| 752 |
""" |
| 753 |
self.check_z3_throws_error(ops, expected) |
| 754 |
|
| 755 |
|
| 756 |
class CallIntPyModPyDiv(AbstractOperation): |
| 757 |
def produce_into(self, builder, r): |
| 758 |
from rpython.jit.backend.test.test_random import getint |
| 759 |
k = r.random() |
| 760 |
if k < 0.20: |
| 761 |
v_second = ConstInt(r.random_integer()) |
| 762 |
else: |
| 763 |
v_second = r.choice(builder.intvars) |
| 764 |
|
| 765 |
if k > 0.80 and type(v_second) is not ConstInt: |
| 766 |
v_first = ConstInt(r.random_integer()) |
| 767 |
else: |
| 768 |
v_first = r.choice(builder.intvars) |
| 769 |
|
| 770 |
while (( |
| 771 |
getint(v_second) == -1 and getint(v_first) == MININT) |
| 772 |
or getint(v_second) == 0 |
| 773 |
): |
| 774 |
v_second = ConstInt(r.random_integer()) |
| 775 |
|
| 776 |
if r.random() > 0.5: |
| 777 |
descr = BaseTest.int_py_div_descr |
| 778 |
res = getint(v_first) // getint(v_second) |
| 779 |
func = 12 |
| 780 |
else: |
| 781 |
descr = BaseTest.int_py_mod_descr |
| 782 |
res = getint(v_first) % getint(v_second) |
| 783 |
func = 14 |
| 784 |
ops = builder.loop.operations |
| 785 |
op = ResOperation(rop.INT_EQ, [v_second, ConstInt(0)]) |
| 786 |
op._example_int = 0 |
| 787 |
ops.append(op) |
| 788 |
|
| 789 |
op = ResOperation(rop.GUARD_FALSE, [op]) |
| 790 |
op.setdescr(builder.getfaildescr()) |
| 791 |
op.setfailargs(builder.subset_of_intvars(r)) |
| 792 |
ops.append(op) |
| 793 |
|
| 794 |
op1 = ResOperation(rop.INT_EQ, [v_first, ConstInt(MININT)]) |
| 795 |
op1._example_int = int(getint(v_first) == MININT) |
| 796 |
ops.append(op1) |
| 797 |
|
| 798 |
op2 = ResOperation(rop.INT_EQ, [v_second, ConstInt(-1)]) |
| 799 |
op2._example_int = int(getint(v_second) == -1) |
| 800 |
ops.append(op2) |
| 801 |
|
| 802 |
op = ResOperation(rop.INT_AND, [op1, op2]) |
| 803 |
op._example_int = 0 |
| 804 |
ops.append(op) |
| 805 |
|
| 806 |
op = ResOperation(rop.GUARD_FALSE, [op]) |
| 807 |
op.setdescr(builder.getfaildescr()) |
| 808 |
op.setfailargs(builder.subset_of_intvars(r)) |
| 809 |
ops.append(op) |
| 810 |
|
| 811 |
op = ResOperation(rop.CALL_PURE_I, [ConstInt(func), v_first, v_second], |
| 812 |
descr=descr) |
| 813 |
if not hasattr(builder, "call_pure_results"): |
| 814 |
builder.call_pure_results = {} |
| 815 |
builder.call_pure_results[(ConstInt(func), ConstInt(getint(v_first)), ConstInt(getint(v_second)))] = ConstInt(res) |
| 816 |
op._example_int = res |
| 817 |
ops.append(op) |
| 818 |
builder.intvars.append(op) |
| 819 |
|
| 820 |
class RangeCheck(AbstractOperation): |
| 821 |
def produce_into(self, builder, r): |
| 822 |
from rpython.jit.backend.test.test_random import getint |
| 823 |
v_int = r.choice(list(set(builder.intvars) - set(builder.boolvars))) |
| 824 |
val = getint(v_int) |
| 825 |
ops = builder.loop.operations |
| 826 |
bound = r.random_integer() |
| 827 |
if bound > val: |
| 828 |
op = ResOperation(rop.INT_LE, [v_int, ConstInt(bound)]) |
| 829 |
else: |
| 830 |
op = ResOperation(rop.INT_GE, [v_int, ConstInt(bound)]) |
| 831 |
op._example_int = 1 |
| 832 |
ops.append(op) |
| 833 |
|
| 834 |
op = ResOperation(rop.GUARD_TRUE, [op]) |
| 835 |
op.setdescr(builder.getfaildescr()) |
| 836 |
op.setfailargs(builder.subset_of_intvars(r)) |
| 837 |
ops.append(op) |
| 838 |
|
| 839 |
class KnownBitsCheck(AbstractOperation): |
| 840 |
def produce_into(self, builder, r): |
| 841 |
|
| 842 |
|
| 843 |
from rpython.jit.backend.test.test_random import getint |
| 844 |
v_int = r.choice(list(set(builder.intvars) - set(builder.boolvars))) |
| 845 |
val = getint(v_int) |
| 846 |
ops = builder.loop.operations |
| 847 |
mask = r.random_integer() |
| 848 |
res = val & mask |
| 849 |
op = ResOperation(rop.INT_AND, [v_int, ConstInt(mask)]) |
| 850 |
op._example_int = res |
| 851 |
ops.append(op) |
| 852 |
|
| 853 |
op = ResOperation(rop.GUARD_VALUE, [op, ConstInt(res)]) |
| 854 |
op.setdescr(builder.getfaildescr()) |
| 855 |
op.setfailargs(builder.subset_of_intvars(r)) |
| 856 |
ops.append(op) |
| 857 |
|
| 858 |
|
| 859 |
OPERATIONS = [op for op in OperationBuilder.OPERATIONS if not isinstance(op, GuardPtrOperation)] + [ |
| 860 |
|
| 861 |
RangeCheck(None), KnownBitsCheck(None)] * 10 |
| 862 |
|
| 863 |
for i in range(4): |
| 864 |
OPERATIONS.append(test_ll_random.GetFieldOperation(rop.GETFIELD_GC_I)) |
| 865 |
OPERATIONS.append(test_ll_random.GetFieldOperation(rop.GETFIELD_GC_I)) |
| 866 |
OPERATIONS.append(test_ll_random.SetFieldOperation(rop.SETFIELD_GC)) |
| 867 |
|
| 868 |
OPERATIONS.append(test_ll_random.NewOperation(rop.NEW_WITH_VTABLE)) |
| 869 |
|
| 870 |
OPERATIONS.append(test_ll_random.GetArrayItemOperation(rop.GETARRAYITEM_GC_I)) |
| 871 |
OPERATIONS.append(test_ll_random.GetArrayItemOperation(rop.GETARRAYITEM_GC_I)) |
| 872 |
OPERATIONS.append(test_ll_random.SetArrayItemOperation(rop.SETARRAYITEM_GC)) |
| 873 |
OPERATIONS.append(test_ll_random.NewArrayOperation(rop.NEW_ARRAY_CLEAR)) |
| 874 |
OPERATIONS.append(test_ll_random.ArrayLenOperation(rop.ARRAYLEN_GC)) |
| 875 |
|
| 876 |
for i in range(2): |
| 877 |
OPERATIONS.append(test_ll_random.GuardClassOperation(rop.GUARD_CLASS)) |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
class Z3OperationBuilder(test_ll_random.LLtypeOperationBuilder): |
| 882 |
produce_failing_guards = False |
| 883 |
OPERATIONS = OPERATIONS |
| 884 |
floatvars = [] |
| 885 |
|
| 886 |
class TestOptimizeIntBoundsZ3(BaseCheckZ3, TOptimizeIntBounds): |
| 887 |
def check_random_function_z3(self, cpu, r, num=None, max=None): |
| 888 |
t1 = time.time() |
| 889 |
output = io.BytesIO() |
| 890 |
loop = RandomLoop(cpu, Z3OperationBuilder, r, output=output) |
| 891 |
trace = convert_loop_to_trace(loop.loop, self.metainterp_sd) |
| 892 |
compile_data = compile.SimpleCompileData( |
| 893 |
trace, call_pure_results=self._convert_call_pure_results(getattr(loop.builder, 'call_pure_results', None)), |
| 894 |
enable_opts=self.enable_opts) |
| 895 |
jitdriver_sd = FakeJitDriverStaticData() |
| 896 |
info, ops = compile_data.optimize_trace(self.metainterp_sd, jitdriver_sd, {}) |
| 897 |
print info.inputargs |
| 898 |
for op in ops: |
| 899 |
print op |
| 900 |
beforeinputargs, beforeops = trace.unpack() |
| 901 |
|
| 902 |
t2 = time.time() |
| 903 |
try: |
| 904 |
correct, timeout = check_z3(beforeinputargs, beforeops, info.inputargs, ops) |
| 905 |
except CheckError: |
| 906 |
print "to reproduce:" |
| 907 |
print "_" * 60 |
| 908 |
print make_reproducer(output.getvalue()) |
| 909 |
print "_" * 60 |
| 910 |
raise |
| 911 |
t3 = time.time() |
| 912 |
print 'generation/optimization [s]:', t2 - t1, 'z3:', t3 - t2, "total:", t3 - t1 |
| 913 |
print 'correct conditions:', correct, 'timed out conditions:', timeout |
| 914 |
if num is not None: |
| 915 |
print ' # passed (%d/%s).' % (num + 1, max) |
| 916 |
else: |
| 917 |
print ' # passed.' |
| 918 |
print |
| 919 |
|
| 920 |
def test_random_z3(self): |
| 921 |
cpu = LLGraphCPU(None) |
| 922 |
cpu.supports_floats = False |
| 923 |
cpu.setup_once() |
| 924 |
r = Random() |
| 925 |
seed = pytest.config.option.randomseed |
| 926 |
try: |
| 927 |
if pytest.config.option.repeat == -1: |
| 928 |
i = 0 |
| 929 |
while 1: |
| 930 |
self.check_random_function_z3(cpu, r, i) |
| 931 |
i += 1 |
| 932 |
seed = r.randrange(sys.maxint) |
| 933 |
r.seed(seed) |
| 934 |
else: |
| 935 |
for i in range(pytest.config.option.repeat): |
| 936 |
r.seed(seed) |
| 937 |
self.check_random_function_z3(cpu, r, i, |
| 938 |
pytest.config.option.repeat) |
| 939 |
seed = r.randrange(sys.maxint) |
| 940 |
except Exception as e: |
| 941 |
print "_" * 60 |
| 942 |
print "got exception", e |
| 943 |
print "seed was", seed |
| 944 |
raise |
| 945 |
|
| 946 |
@given(strategies.randoms()) |
| 947 |
def DISABLED_test_random_loop_parses(r): |
| 948 |
cpu = LLGraphCPU(None) |
| 949 |
cpu.supports_floats = False |
| 950 |
cpu.setup_once() |
| 951 |
output = io.BytesIO() |
| 952 |
try: |
| 953 |
loop = RandomLoop(cpu, Z3OperationBuilder, Random(r), output=output) |
| 954 |
except Exception: |
| 955 |
raise |
| 956 |
s = output.getvalue() |
| 957 |
r = make_reproducer(s) |
| 958 |
try: |
| 959 |
check_via_reproducer_string(r) |
| 960 |
except Exception as e: |
| 961 |
print "error", e |
| 962 |
print "to reproduce" |
| 963 |
print "_" * 60 |
| 964 |
print r |
| 965 |
print "_" * 60 |
| 966 |
raise |
| 967 |
|
| 968 |
def check_via_reproducer_string(r): |
| 969 |
d = {} |
| 970 |
exec r in d |
| 971 |
loop = d['loop'] |
| 972 |
cpu = LLGraphCPU(None) |
| 973 |
cpu.supports_floats = False |
| 974 |
cpu.setup_once() |
| 975 |
t = TestOptimizeIntBoundsZ3() |
| 976 |
t.cls_attributes() |
| 977 |
trace = convert_loop_to_trace(loop, t.metainterp_sd) |
| 978 |
compile_data = compile.SimpleCompileData( |
| 979 |
trace, call_pure_results=None, |
| 980 |
enable_opts=t.enable_opts) |
| 981 |
compile_data.forget_optimization_info = lambda *args, **kwargs: None |
| 982 |
jitdriver_sd = FakeJitDriverStaticData() |
| 983 |
info, ops = compile_data.optimize_trace(t.metainterp_sd, jitdriver_sd, {}) |
| 984 |
beforeinputargs, beforeops = trace.unpack() |
| 985 |
|
| 986 |
correct, timeout = check_z3(beforeinputargs, beforeops, info.inputargs, ops) |
| 987 |
|
| 988 |
@given(strategies.randoms()) |
| 989 |
def test_random_hypothesis(r): |
| 990 |
cpu = LLGraphCPU(None) |
| 991 |
cpu.supports_floats = False |
| 992 |
cpu.setup_once() |
| 993 |
t = TestOptimizeIntBoundsZ3() |
| 994 |
t.cls_attributes() |
| 995 |
t.check_random_function_z3(cpu, Random(r), 0) |
| 996 |
|
| 997 |
def make_reproducer(s): |
| 998 |
lines = s.splitlines() |
| 999 |
assert "cpu.execute_token" in lines[-1] |
| 1000 |
lines.pop() |
| 1001 |
assert "loop_args =" in lines[-1] |
| 1002 |
lines.pop() |
| 1003 |
assert "cpu.compile_loop" in lines[-1] |
| 1004 |
lines.pop() |
| 1005 |
assert "looptoken =" in lines[-1] |
| 1006 |
lines.pop() |
| 1007 |
preamble = """\ |
| 1008 |
from rpython.rtyper.lltypesystem import lltype, rffi, llmemory |
| 1009 |
from rpython.rtyper import rclass |
| 1010 |
from rpython.jit.metainterp.resoperation import rop, ResOperation, \ |
| 1011 |
InputArgInt, InputArgRef, InputArgFloat |
| 1012 |
from rpython.jit.metainterp.history import TargetToken, TreeLoop |
| 1013 |
from rpython.jit.metainterp.history import BasicFailDescr, BasicFinalDescr |
| 1014 |
from rpython.jit.metainterp.history import ConstInt, ConstPtr |
| 1015 |
from rpython.jit.backend.llgraph.runner import LLGraphCPU as CPU |
| 1016 |
from rpython.jit.codewriter import heaptracker |
| 1017 |
if 1: |
| 1018 |
""" |
| 1019 |
post = """ |
| 1020 |
loop = TreeLoop('reproduce') |
| 1021 |
loop.inputargs = inputargs |
| 1022 |
loop.operations = operations |
| 1023 |
""" |
| 1024 |
return preamble + "\n".join(lines) + post |
| 1025 |
|
| 1026 |
class TestOptimizeHeapZ3(BaseCheckZ3, TOptimizeHeap): |
| 1027 |
def dont_execute(self): |
| 1028 |
pass |
| 1029 |
test_nonvirtual_later = dont_execute |
| 1030 |
test_nonvirtual_write_null_fields_on_force = dont_execute |
| 1031 |
test_nonvirtual_array_write_null_fields_on_force = dont_execute |
| 1032 |
test_arraycopy_1 = dont_execute |
| 1033 |
test_arraycopy_not_virtual = dont_execute |
| 1034 |
test_arraycopy_invalidate_3 = dont_execute |
| 1035 |
test_varray_negative_items_from_invalid_loop = dont_execute |
| 1036 |
test_virtual_array_of_struct = dont_execute |
| 1037 |
test_virtual_array_of_struct_forced = dont_execute |
| 1038 |
test_virtual_array_of_struct_arraycopy = dont_execute |
| 1039 |
test_nonvirtual_array_of_struct_arraycopy = dont_execute |
| 1040 |
test_varray_struct_negative_items_from_invalid_loop = dont_execute |
| 1041 |
test_varray_struct_too_large_items = dont_execute |
| 1042 |
|
| 1043 |
if __name__ == '__main__': |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
with open(sys.argv[1], "r") as f: |
| 1054 |
ops = f.read() |
| 1055 |
import pytest, os |
| 1056 |
class config: |
| 1057 |
class option: |
| 1058 |
z3timeout = 100000 |
| 1059 |
pytest.config = config |
| 1060 |
try: |
| 1061 |
check_via_reproducer_string(ops) |
| 1062 |
except CheckError as e: |
| 1063 |
print e |
| 1064 |
os._exit(0) |
| 1065 |
except Exception as e: |
| 1066 |
print e |
| 1067 |
os._exit(-1) |
| 1068 |
os._exit(-1) |
| 1069 |
|
| 1070 |
|