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())
def test_pointer_pointer_pointer(self): ctx = newctx() a = dt.fresh_ptr(ctx, util.fresh_name( 'a'), it.PointerType(it.PointerType(it.IntType(64)))) b = dt.fresh_ptr(ctx, util.fresh_name( 'b'), it.PointerType(it.IntType(64))) c = dt.fresh_ptr(ctx, util.fresh_name( 'c'), it.PointerType(it.IntType(64))) cond = z3.Bool('cond') p = util.If(cond, b, c) a.write(ctx, p) print a.read(ctx).read(ctx)
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_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 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 declare_global_variable(self, ctx, name, global_type, value=None, value_type=None): def unknown_function(*args, **kwargs): assert False, "Calling unknown function {}".format(name) unknown_function.read = lambda *args, **kwargs: unknown_function if global_type.deref(True).is_function(): ctx['globals'][name] = unknown_function else: ctx['globals'][name] = dt.fresh_ptr(ctx, name, global_type)
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 alloca(self, ctx, return_type, size, size_type, **kwargs): assert return_type.is_pointer() return dt.fresh_ptr(ctx, util.fresh_name('alloca'), return_type)