def test_read_read_same(self): # "Helgi's problem" ctx = newctx() p = dt.fresh_ptr(ctx, util.fresh_name( 'p'), it.PointerType(it.IntType(64))) self.assertEquals(p.getelementptr(ctx, util.i64(0)).read( ctx), p.getelementptr(ctx, util.i64(0)).read(ctx))
def test_struct_of_ints(self): ctx = newctx() point = it.StructType('Point', [it.IntType(64), it.IntType(64)]) q = dt.fresh_ptr(ctx, util.fresh_name('q'), it.PointerType(point)) print ctx['references'][q._ref._name] x = util.FreshBitVec('x', 64) q.getelementptr(ctx, util.i64(0), util.i64(0)).write(ctx, x) s = z3.Solver() s.add(z3.Not(x == q.getelementptr(ctx, util.i64(0), util.i64(0)).read(ctx))) self.assertEquals(s.check(), z3.unsat)
def declare_global_constant(self, ctx, name, global_type, value=None, value_type=None): ctx['globals'][name] = dt.fresh_ptr(ctx, name, global_type) if value_type.is_array() and value_type.deref().is_int(): inner_size = value_type.deref().size() for n, i in enumerate(util.parse_constant_array(value)): ctx['globals'][name].getelementptr( ctx, util.i64(0), util.i64(n)).write(ctx, z3.BitVecVal(i, inner_size))
def test_array_of_stucts(self): ctx = newctx() points = it.ArrayType(10, it.StructType( 'Point', [it.IntType(64), it.IntType(64)])) p = dt.fresh_ptr(ctx, util.fresh_name('p'), points) y = util.FreshBitVec('y', 64) x = util.FreshBitVec('x', 64) # x and y are within bounds ctx['solver'].add(z3.ULT(x, 10)) ctx['solver'].add(z3.ULT(y, 10)) p.getelementptr(ctx, x, util.i64(0)).write(ctx, x) s = z3.Solver() s.add(z3.Not(z3.Implies(x == y, p.getelementptr( ctx, y, util.i64(0)).read(ctx) == y))) self.assertEquals(s.check(), z3.unsat)
def test_pointer_to_int(self): ctx = newctx() p = dt.fresh_ptr(ctx, util.fresh_name( 'p'), it.PointerType(it.IntType(64))) print ctx['references'][p._ref._name] p.write(ctx, util.i64(4)) self.assertEquals(4, p.read(ctx).as_long())
def test_pointer_ite(self): ctx = newctx() p1 = dt.fresh_ptr(ctx, util.fresh_name( 'p'), it.PointerType(it.IntType(64))) p2 = dt.fresh_ptr(ctx, util.fresh_name( 'p'), it.PointerType(it.IntType(64))) cond = z3.Bool('cond') p3 = util.If(cond, p1, p2) p3.write(ctx, util.i64(4)) s = z3.Solver() s.add(z3.Not(z3.Implies(cond, p1.read(ctx) == util.i64(4)))) self.assertEquals(s.check(), z3.unsat) s = z3.Solver() s.add(z3.Not(z3.Implies(z3.Not(cond), p2.read(ctx) == util.i64(4)))) self.assertEquals(s.check(), z3.unsat)
def test_array_bounds(self): ctx = newctx() points = it.ArrayType(10, it.IntType(64)) p = dt.fresh_ptr(ctx, util.fresh_name('p'), points) p = p.getelementptr(ctx, 50) with self.assertRaises(IndexError): p.write(ctx, util.i64(10))
def canonical_path(self): path = [util.i64(0)] for gep in self._geps: assert len(gep) > 0, "Cannot have empty gep" if hasattr(gep[0], 'sexpr') and gep[0].size() != 64: path[-1] = path[-1] + z3.BitVecVal(util.as_long(gep[0]), 64) else: path[-1] = path[-1] + gep[0] path += gep[1:] return map(util.simplify, path)
def test_pointer_pointer(self): ctx = newctx() p1 = dt.fresh_ptr(ctx, util.fresh_name( 'p'), it.PointerType(it.PointerType(it.IntType(64)))) p2 = dt.fresh_ptr(ctx, util.fresh_name( 'p'), it.PointerType(it.IntType(64))) p1.write(ctx, p2) p2.write(ctx, util.i64(10)) self.assertEquals(p2.read(ctx).as_long(), p1.read(ctx).read(ctx).as_long())