Esempio n. 1
0
    def test_merge3(self):
        ctx = context.newctx()
        x = util.FreshBitVec('x', 32)
        ctx.globals['%4'] = x

        cond = util.FreshBool('cond')

        ctx.push(cond)
        ctx.globals['%4'] += 1
        g1 = ctx.pop()

        ctx.push(z3.Not(cond))
        ctx.globals['%4'] += 1
        g2 = ctx.pop()

        assert not util.path_condition_implies(g1, cond)
        assert not util.path_condition_implies(g2, z3.Not(cond))

        assert not util.path_condition_implies(ctx, cond)
        assert not util.path_condition_implies(ctx, z3.Not(cond))

        ctx.merge(cond, g1, g2)

        assert not util.path_condition_implies(ctx, cond)
        assert not util.path_condition_implies(ctx, z3.Not(cond))
Esempio n. 2
0
    def test_more_nest(self):
        ctx = context.newctx()
        x = util.FreshBitVec('x', 32)
        ctx.globals['%4'] = x

        ctx.push()
        self._inc(ctx)
        self.prove(ctx.globals['%4'] == x + 1)
        ctx.pop()

        ctx.push()
        self._inc(ctx)
        self.prove(ctx.globals['%4'] == x + 1)
        ctx.push()
        self._inc(ctx)
        self.prove(ctx.globals['%4'] == x + 2)
        ctx.push()
        self._inc(ctx)
        self.prove(ctx.globals['%4'] == x + 3)
        ctx.pop()
        self.prove(ctx.globals['%4'] == x + 2)
        ctx.pop()
        self.prove(ctx.globals['%4'] == x + 1)
        ctx.pop()

        self.prove(ctx.globals['%4'] == x)
Esempio n. 3
0
 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))
Esempio n. 4
0
 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())
Esempio n. 5
0
 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))
Esempio n. 6
0
 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)
Esempio n. 7
0
 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())
Esempio n. 8
0
    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)
Esempio n. 9
0
 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)
Esempio n. 10
0
    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)
Esempio n. 11
0
    def test_nested_restore(self):
        ctx = context.newctx()

        x = util.FreshBitVec('x', 32)

        ctx.globals['%4'] = x

        ctx.push()
        ctx.globals['%4'] += 1
        b1 = ctx.pop()

        self.prove(ctx.globals['%4'] == x)
        self.prove(b1.globals['%4'] == x + 1)

        ctx.restore(b1)

        self.prove(ctx.globals['%4'] == x + 1)
Esempio n. 12
0
    def test_nested_push_pop(self):
        ctx = context.newctx()

        x = util.FreshBitVec('x', 32)

        ctx.globals['%4'] = x

        ctx.push()

        ctx.push()
        ctx.globals['%4'] += 1
        self.prove(ctx.globals['%4'] == x + 1)
        b11 = ctx.pop()

        self.prove(ctx.globals['%4'] == x)

        ctx.push()
        ctx.globals['%4'] += 2
        self.prove(ctx.globals['%4'] == x + 2)
        b12 = ctx.pop()

        cond1 = util.FreshBool('cond')
        ctx.merge(cond1, b11, b12)

        self.prove(z3.Implies(cond1, ctx.globals['%4'] == x + 1))
        self.prove(z3.Implies(z3.Not(cond1), ctx.globals['%4'] == x + 2))

        b1 = ctx.pop()

        ctx.push()
        ctx.globals['%4'] += 10
        b2 = ctx.pop()

        cond2 = util.FreshBool('cond')
        ctx.merge(cond2, b1, b2)

        self.prove(z3.Implies(z3.And(cond1, cond2),
                              ctx.globals['%4'] == x + 1))
        self.prove(z3.Implies(z3.And(z3.Not(cond1), cond2),
                              ctx.globals['%4'] == x + 2))
        self.prove(z3.Implies(z3.And(cond1, z3.Not(cond2)),
                              ctx.globals['%4'] == x + 10))
        self.prove(z3.Implies(z3.And(z3.Not(cond1), z3.Not(cond2)),
                              ctx.globals['%4'] == x + 10))
Esempio n. 13
0
    def test_push_pop(self):
        ctx = context.newctx()

        x = util.FreshBitVec('x', 32)
        cond = util.FreshBool('cond')

        ctx.globals['%4'] = x

        ctx.push(cond)
        ctx.globals['%4'] += 1
        b1 = ctx.pop()

        ctx.push(z3.Not(cond))
        ctx.globals['%4'] += 2
        b2 = ctx.pop()

        ctx.merge(cond, b1, b2)

        self.prove(z3.Implies(cond, ctx.globals['%4'] == x + 1))
        self.prove(z3.Implies(z3.Not(cond), ctx.globals['%4'] == x + 2))
Esempio n. 14
0
    def test_pcond(self):
        ctx = context.newctx()

        x = util.FreshBitVec('x', 32)
        cond = util.FreshBool('cond')

        ctx.globals['%4'] = x

        ctx.push(cond)
        ctx.globals['%4'] += 1
        print 'cond', ctx.path_condition
        b1 = ctx.pop()

        ctx.push(z3.Not(cond))
        ctx.globals['%4'] += 1
        print 'ncond', ctx.path_condition
        b2 = ctx.pop()

        print 'cond', b1.path_condition
        print 'ncond', b2.path_condition
        print [], ctx.path_condition
        ctx.restore(b1)
        print [], ctx.path_condition
Esempio n. 15
0
 def test_nest(self):
     ctx = context.newctx()
     x = util.FreshBitVec('x', 32)
     ctx.globals['%4'] = x
     self._nest(ctx, 5)
Esempio n. 16
0
 def setUp(self):
     self._ctx = newctx()