예제 #1
0
 def test_invalid_args(self):
     with self.assertRaises(AssertionError):
         bitvectify(1, -1)
     with self.assertRaises(AssertionError):
         bitvectify(Constant(0, 8), 8 + 1)
     with self.assertRaises(AssertionError):
         bitvectify(Variable("x", 8), 8 + 1)
     with self.assertRaises(AssertionError):
         bitvectify(Term(width=8), 8 + 1)
예제 #2
0
    def __new__(cls, *args, **options):
        assert len(cls.input_widths) == len(args)
        newargs = []
        for arg, width in zip(args, cls.input_widths):
            newargs.append(core.bitvectify(arg, width))
        args = newargs

        if all(isinstance(arg, core.Constant) for arg in args) or \
                options.pop("symbolic_inputs", False):
            result = cls.eval(*args)
        else:
            raise TypeError("expected bit-vector constant arguments")

        output = list(core.tuplify(result))
        assert len(cls.output_widths) == len(output)
        for i in range(len(output)):
            output[i] = core.bitvectify(output[i], cls.output_widths[i])

        if isinstance(result, collections.Sequence):
            return tuple(output)
        else:
            return output[0]
예제 #3
0
    def __new__(cls, *args, **options):
        if len(cls.input_widths) != len(args):
            raise ValueError(
                "{} requires {} inputs but {} were given: {}".format(
                    cls.__name__, len(cls.input_widths), len(args), args))
        newargs = []
        for arg, width in zip(args, cls.input_widths):
            newargs.append(core.bitvectify(arg, width))
        args = newargs

        if all(isinstance(arg, core.Constant) for arg in args) or \
                options.pop("symbolic_inputs", False):
            result = cls.eval(*args)
        else:
            raise TypeError("expected bit-vector constant arguments")

        assert isinstance(result, collections.abc.Sequence)
        assert len(cls.output_widths) == len(result)

        output = []
        for r, width in zip(result, cls.output_widths):
            output.append(core.bitvectify(r, width))

        return tuple(output)
예제 #4
0
    def __init__(self, input_diff, cte):  # noqa: 102
        assert cte != 0
        input_diff = _tuplify(input_diff)
        n = input_diff[0].val.width
        cte = core.bitvectify(cte, width=n)
        self.op = extraop.make_partial_operation(operation.BvAdd, tuple([None, cte]))
        self.op.constant = cte  # temporary patch
        super().__init__(input_diff)

        index_first_one = 0
        for i in range(0, cte.width):
            if cte[i] == 1:
                index_first_one = i
                break

        self._index_first_one = index_first_one
        self._effective_width = n - 1 - index_first_one
        self._effective_precision = max(min(type(self).precision, self._effective_width - 2), 0)  # 2 found empirically
예제 #5
0
    def test_initialization(self):
        x = Variable("x", 8)
        y = Variable("y", 8)
        b = Variable("b", 1)

        for op in simple_op:
            expr = op(x, y)
            self.assertEqual(expr, bitvectify(expr, op.output_width(x, y)))
            self.assertFalse(expr.is_Atom)
            self.assertEqual(expr.atoms(), {x, y})

        for op in unary_op:
            expr = op(x)
            self.assertEqual(expr, bitvectify(expr, op.output_width(x)))
            self.assertFalse(expr.is_Atom)
            self.assertEqual(expr.atoms(), {x})

        for op in [RotateLeft, RotateRight, ZeroExtend, Repeat]:
            expr = op(x, 2)
            self.assertEqual(expr, bitvectify(expr, op.output_width(x, 2)))
            self.assertFalse(expr.is_Atom)

        expr = Concat(x, y)
        self.assertEqual(expr, bitvectify(expr, Concat.output_width(x, y)))
        self.assertFalse(expr.is_Atom)
        self.assertEqual(expr.atoms(), {x, y})

        expr = Extract(x, 4, 2)
        self.assertEqual(expr, bitvectify(expr, Extract.output_width(x, 4, 2)))
        self.assertFalse(expr.is_Atom)
        self.assertEqual(expr.atoms(), {x})

        expr = Ite(b, x, y)
        self.assertEqual(expr, bitvectify(expr, Ite.output_width(b, x, y)))
        self.assertFalse(expr.is_Atom)
        self.assertEqual(expr.atoms(), {b, x, y})
예제 #6
0
 def test_initialization(self):
     self.assertEqual(bitvectify(2, 8), Constant(2, 8))
     self.assertEqual(bitvectify("x", 8), Variable("x", 8))
     self.assertEqual(bitvectify(Term(width=8), 8), Term(width=8))