예제 #1
0
    def test_visitors(self):
        solver = Z3Solver.instance()
        cs = ConstraintSet()
        arr = cs.new_array(name="MEM")
        a = cs.new_bitvec(32, name="VAR")
        self.assertEqual(get_depth(a), 1)
        cond = Operators.AND(a < 200, a > 100)
        arr[0] = ord("a")
        arr[1] = ord("b")

        self.assertEqual(get_depth(cond), 3)
        self.assertEqual(get_depth(arr[a + 1]), 4)
        self.assertEqual(
            translate_to_smtlib(arr[a + 1]),
            "(select (store (store MEM #x00000000 #x61) #x00000001 #x62) (bvadd VAR #x00000001))",
        )

        arr[3] = arr[a + 1]
        aux = arr[a + Operators.ZEXTEND(arr[a], 32)]

        self.assertEqual(get_depth(aux), 9)
        self.maxDiff = 1500
        self.assertEqual(
            translate_to_smtlib(aux),
            "(select (store (store (store MEM #x00000000 #x61) #x00000001 #x62) #x00000003 (select (store (store MEM #x00000000 #x61) #x00000001 #x62) (bvadd VAR #x00000001))) (bvadd VAR ((_ zero_extend 24) (select (store (store (store MEM #x00000000 #x61) #x00000001 #x62) #x00000003 (select (store (store MEM #x00000000 #x61) #x00000001 #x62) (bvadd VAR #x00000001))) VAR))))",
        )

        values = arr[0:2]
        self.assertEqual(len(values), 2)
        self.assertItemsEqual(solver.get_all_values(cs, values[0]), [ord("a")])
        self.assertItemsEqual(solver.get_all_values(cs, values[1]), [ord("b")])
        arr[1:3] = "cd"

        values = arr[0:3]
        self.assertEqual(len(values), 3)
        self.assertItemsEqual(solver.get_all_values(cs, values[0]), [ord("a")])
        self.assertItemsEqual(solver.get_all_values(cs, values[1]), [ord("c")])
        self.assertItemsEqual(solver.get_all_values(cs, values[2]), [ord("d")])
        self.assertEqual(
            pretty_print(aux, depth=2),
            "ArraySelect\n  ArrayStore\n    ...\n  BitVecAdd\n    ...\n")
        self.assertEqual(pretty_print(Operators.EXTRACT(a, 0, 8), depth=1),
                         "BitVecExtract{0:7}\n  ...\n")
        self.assertEqual(pretty_print(a, depth=2), "VAR\n")

        x = BitVecConstant(32, 100, taint=("important", ))
        y = BitVecConstant(32, 200, taint=("stuff", ))
        z = constant_folder(x + y)
        self.assertItemsEqual(z.taint, ("important", "stuff"))
        self.assertEqual(z.value, 300)

        self.assertRaises(Exception, translate_to_smtlib, 1)

        self.assertEqual(
            translate_to_smtlib(simplify(Operators.ZEXTEND(a, 32))), "VAR")
        self.assertEqual(
            translate_to_smtlib(
                simplify(Operators.EXTRACT(Operators.EXTRACT(a, 0, 8), 0, 8))),
            "((_ extract 7 0) VAR)",
        )
예제 #2
0
 def test_constant_folding_udiv(self):
     cs = ConstraintSet()
     x = BitVecConstant(size=32, value=0xFFFFFFFF, taint=("important",))
     y = BitVecConstant(size=32, value=2, taint=("stuff",))
     z = constant_folder(x.udiv(y))
     self.assertItemsEqual(z.taint, ("important", "stuff"))
     self.assertEqual(z.value, 0x7FFFFFFF)
예제 #3
0
 def testBasicTaint(self):
     a = BitVecConstant(size=32, value=100, taint=("SOURCE1",))
     b = BitVecConstant(size=32, value=200, taint=("SOURCE2",))
     c = a + b
     self.assertIsInstance(c, BitVecAdd)
     self.assertIsInstance(c, Operation)
     self.assertIsInstance(c, Expression)
     self.assertTrue("SOURCE1" in c.taint)
     self.assertTrue("SOURCE2" in c.taint)
예제 #4
0
    def test_arithmetic_simplify_udiv(self):
        cs = ConstraintSet()
        a = cs.new_bitvec(32, name="VARA")
        b = a + Operators.UDIV(BitVecConstant(size=32, value=0), BitVecConstant(size=32, value=2))
        self.assertEqual(translate_to_smtlib(b), "(bvadd VARA (bvudiv #x00000000 #x00000002))")
        self.assertEqual(translate_to_smtlib(simplify(b)), "VARA")

        c = a + Operators.UDIV(BitVecConstant(size=32, value=2), BitVecConstant(size=32, value=2))
        self.assertEqual(translate_to_smtlib(c), "(bvadd VARA (bvudiv #x00000002 #x00000002))")
        self.assertEqual(translate_to_smtlib(simplify(c)), "(bvadd VARA #x00000001)")
예제 #5
0
 def testBasicITETaint(self):
     a = BitVecConstant(size=32, value=100, taint=("SOURCE1",))
     b = BitVecConstant(size=32, value=200, taint=("SOURCE2",))
     c = BitVecConstant(size=32, value=300, taint=("SOURCE3",))
     d = BitVecConstant(size=32, value=400, taint=("SOURCE4",))
     x = Operators.ITEBV(32, a > b, c, d)
     self.assertTrue("SOURCE1" in x.taint)
     self.assertTrue("SOURCE2" in x.taint)
     self.assertTrue("SOURCE3" in x.taint)
     self.assertTrue("SOURCE4" in x.taint)
예제 #6
0
 def testBasicOperation(self):
     """ Add """
     a = BitVecConstant(size=32, value=100)
     b = BitVecVariable(size=32, name="VAR")
     c = a + b
     self.assertIsInstance(c, BitVecAdd)
     self.assertIsInstance(c, Operation)
     self.assertIsInstance(c, Expression)
예제 #7
0
    def testBasicAST_001(self):
        """ Can't build abstract classes """
        a = BitVecConstant(size=32, value=100)

        self.assertRaises(TypeError, Expression, ())
        self.assertRaises(TypeError, Constant, 123)
        self.assertRaises(TypeError, Variable, "NAME")
        self.assertRaises(TypeError, Operation, a)
예제 #8
0
 def test_no_variable_expression_can_be_true(self):
     """
     Tests if solver.can_be_true is correct when the expression has no nodes that subclass
     from Variable (e.g. BitVecConstant)
     """
     x = BitVecConstant(size=32, value=10)
     cs = ConstraintSet()
     self.assertFalse(self.solver.can_be_true(cs, x == False))
예제 #9
0
 def testBasicOperation(self):
     """ Add """
     a = BitVecConstant(32, 100)
     b = BitVecVariable(32, "VAR")
     c = a + b
     self.assertIsInstance(c, BitVecAdd)
     self.assertIsInstance(c, Operation)
     self.assertIsInstance(c, Expression)
예제 #10
0
    def testBasicReplace(self):
        """ Add """
        a = BitVecConstant(size=32, value=100)
        b1 = BitVecVariable(size=32, name="VAR1")
        b2 = BitVecVariable(size=32, name="VAR2")

        c = a + b1

        x = replace(c, {b1: b2})
        self.assertEqual(translate_to_smtlib(x), "(bvadd #x00000064 VAR2)")
예제 #11
0
    def test_related_to(self):
        import gzip
        import pickle, sys

        filename = os.path.abspath(
            os.path.join(DIRPATH, "data", "ErrRelated.pkl.gz"))

        # A constraint set and a contraint caught in the act of making related_to fail
        constraints, constraint = pickle.loads(
            gzip.open(filename, "rb").read())

        Z3Solver.instance().can_be_true.cache_clear()
        ground_truth = Z3Solver.instance().can_be_true(constraints, constraint)
        self.assertEqual(ground_truth, False)

        Z3Solver.instance().can_be_true.cache_clear()
        self.assertEqual(
            ground_truth,
            Z3Solver.instance().can_be_true(
                constraints.related_to(constraints), constraint),
        )

        # Replace
        new_constraint = Operators.UGE(
            Operators.SEXTEND(BitVecConstant(256, 0x1A), 256, 512) *
            BitVecConstant(512, 1),
            0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000,
        )
        self.assertEqual(translate_to_smtlib(constraint),
                         translate_to_smtlib(new_constraint))

        Z3Solver.instance().can_be_true.cache_clear()
        self.assertEqual(
            ground_truth,
            Z3Solver.instance().can_be_true(constraints, new_constraint))

        Z3Solver.instance().can_be_true.cache_clear()
        self.assertEqual(
            ground_truth,
            Z3Solver.instance().can_be_true(
                constraints.related_to(new_constraint), new_constraint),
        )
예제 #12
0
 def test_bitvec(self):
     r = Register(32)
     b = BitVecConstant(32, 0)
     r.write(b)
     self.assertIs(r.read(), b)
예제 #13
0
 def test_bitvec_flag(self):
     r = Register(1)
     b = BitVecConstant(32, 0)
     r.write(b)
     # __nonzero__ (==) currently unimplemented for Bool
     self.assertTrue(isinstance(r.read(), Bool))
예제 #14
0
 def test_constant_bitvec(self):
     """
     Tests if higher bits are masked out
     """
     x = BitVecConstant(size=32, value=0xFF00000000)
     self.assertTrue(x.value == 0)