Ejemplo n.º 1
0
 def __init__(self, bv_size):
     super().__init__('bvnor', 2, (exprtypes.BitVectorType(bv_size),
                                   exprtypes.BitVectorType(bv_size)),
                      exprtypes.BitVectorType(bv_size))
     self.smt_function = lambda a, b: ~(a | b)
     self.eval_children = lambda a, b: ~(a | b)
     self.commutative = True
     self.associative = True
Ejemplo n.º 2
0
 def __init__(self, bv_size):
     super().__init__('bvcomp', 2, (exprtypes.BitVectorType(bv_size),
                                    exprtypes.BitVectorType(bv_size)),
                      exprtypes.BitVectorType(1))
     self.smt_function = lambda a, b: z3.If(a == b, z3.BitVecVal(1, 1),
                                            z3.BitVecVal(0, 1))
     self.eval_children = lambda a, b: a.bvcomp(b)
     self.commutative = True
     self.associative = True
Ejemplo n.º 3
0
def preprocess_operators(term_exprs, pred_exprs):
    eval_context = evaluation.EvaluationContext()
    bitsize = 64
    bvlshr = semantics_bv.BVLShR(bitsize)

    new_term_exprs = set([])
    new_pred_exprs = set([])
    for term_expr, f in term_exprs:
        subst_pairs = set([])
        all_exprs = exprs.get_all_exprs(term_expr)
        for e in all_exprs:
            if exprs.is_function_expression(e):
                if e.function_info.function_name == 'bvudiv':
                    if exprs.is_constant_expression(e.children[1]):
                        value = evaluation.evaluate_expression_raw(
                            e.children[1], eval_context)
                        new_right_child = exprs.ConstantExpression(
                            exprs.Value(
                                BitVector(int(math.log2(value.value)),
                                          bitsize),
                                exprtypes.BitVectorType(bitsize)))
                        subst_pairs.add(
                            (e,
                             exprs.FunctionExpression(
                                 bvlshr, (e.children[0], new_right_child))))

        new_term_expr = term_expr
        for (old_term, new_term) in subst_pairs:
            new_term_expr = exprs.substitute(new_term_expr, old_term, new_term)
        new_term_exprs.add((new_term_expr, f))

    for pred_expr, f in pred_exprs:
        subst_pairs = set([])
        all_exprs = exprs.get_all_exprs(pred_expr)
        for e in all_exprs:
            if exprs.is_function_expression(e):
                if e.function_info.function_name == 'bvudiv':
                    if exprs.is_constant_expression(e.children[1]):
                        value = evaluation.evaluate_expression_raw(
                            e.children[1], eval_context)
                        new_right_child = exprs.ConstantExpression(
                            exprs.Value(
                                BitVector(int(math.log2(value.value)),
                                          bitsize),
                                exprtypes.BitVectorType(bitsize)))
                        subst_pairs.add(
                            (e,
                             exprs.FunctionExpression(
                                 bvlshr, (e.children[0], new_right_child))))

        new_pred_expr = pred_expr
        for (old_term, new_term) in subst_pairs:
            new_pred_expr = exprs.substitute(new_pred_expr, old_term, new_term)
        new_pred_exprs.add((new_pred_expr, f))

    return (new_term_exprs, new_pred_exprs)
Ejemplo n.º 4
0
    def __init__(self, bv_size):
        super().__init__('bvurem', 2, (exprtypes.BitVectorType(bv_size),
                                       exprtypes.BitVectorType(bv_size)),
                         exprtypes.BitVectorType(bv_size))
        self.smt_function = z3.URem

        def eval_c(a, b):
            if b.value == 0:
                raise basetypes.PartialFunctionError()
            return a.urem(b)

        self.eval_children = eval_c
Ejemplo n.º 5
0
    def __init__(self, bv_size):
        super().__init__('bvsdiv', 2, (exprtypes.BitVectorType(bv_size),
                                       exprtypes.BitVectorType(bv_size)),
                         exprtypes.BitVectorType(bv_size))
        self.smt_function = lambda a, b: a / b

        def eval_c(a, b):
            if b.value == 0:
                raise basetypes.PartialFunctionError()
            return a.sdiv(b)

        self.eval_children = eval_c
Ejemplo n.º 6
0
def sexp_to_type(sexp):
    if type(sexp) == list and sexp[0] == 'BitVec':
        assert type(sexp[1]) == tuple and sexp[1][0] == 'Int'
        length = sexp[1][1]
        return exprtypes.BitVectorType(length)
    elif sexp == 'Int':
        return exprtypes.IntType()
    elif sexp == 'Bool':
        return exprtypes.BoolType()
    elif sexp == 'String':
        return exprtypes.StringType()
    else:
        raise Exception("Unknown type: %s" % str(sexp))
Ejemplo n.º 7
0
 def string_to_constant_rewrite(topsymb):
     if topsymb.isdigit() or (topsymb.startswith('-') and len(topsymb) >= 2
                              and topsymb[1:].isdigit):
         num = -1 * int(topsymb[1:]) if topsymb.startswith('-') else int(
             topsymb)
         return grammars.ExpressionRewrite(
             exprs.ConstantExpression(exprs.Value(num,
                                                  exprtypes.IntType())))
     elif topsymb.startswith('#x'):  # bitvector
         num = int('0' + topsymb[1:], 16)
         bitsize = (len(topsymb) - 2) * 4
         return grammars.ExpressionRewrite(
             exprs.ConstantExpression(
                 exprs.Value(num, exprtypes.BitVectorType(bitsize))))
     else:  # boolean
         return grammars.ExpressionRewrite(
             exprs.ConstantExpression(
                 exprs.Value(bool(topsymb), exprtypes.BoolType())))
Ejemplo n.º 8
0
def make_default_grammar(syn_ctx, theory, return_type, args):
    int_type = exprtypes.IntType()
    bool_type = exprtypes.BoolType()
    if theory == 'LIA':
        [start, start_bool,
         const] = ['Start', 'StartBool', 'ConstantIntegerType']
        nts = [start, start_bool, const]
        nt_type = {start: int_type, start_bool: bool_type, const: int_type}
        rules = {start: [], start_bool: [], const: []}

        ntr_start = NTRewrite(start, int_type)
        ntr_startbool = NTRewrite(start_bool, bool_type)
        ntr_const = NTRewrite(const, int_type)

        [ add_func, sub_func, mul_func, div_func, mod_func ] = \
                [ syn_ctx.make_function(name, int_type, int_type)
                        for name in [ 'add', 'sub', 'mul', 'div', 'mod' ] ]
        ite_func = syn_ctx.make_function('ite', bool_type, int_type, int_type)

        [ and_func, or_func ] = \
                [ syn_ctx.make_function(name, bool_type, bool_type)
                        for name in [ 'and', 'or' ] ]
        not_func = syn_ctx.make_function('not', bool_type)

        [ eq_func, ne_func, le_func, lt_func, ge_func, gt_func ] = \
                [ syn_ctx.make_function(name, int_type, int_type)
                        for name in [ '=', 'ne', '<=', '<', '>=', '>' ] ]

        # Start rules:
        # Args
        for arg in args:
            if exprs.get_expression_type(arg) == int_type:
                rules[start].append(ExpressionRewrite(arg))
            elif exprs.get_expression_type(arg) == bool_type:
                rules[start_bool].append(ExpressionRewrite(arg))

        # Constants
        rules[start].append(
            ExpressionRewrite(
                exprs.ConstantExpression(exprs.Value(1, int_type))))
        rules[start].append(
            ExpressionRewrite(
                exprs.ConstantExpression(exprs.Value(0, int_type))))
        rules[start].append(ntr_const)
        # Start + Start, Start - Start,
        rules[start].append(FunctionRewrite(add_func, ntr_start, ntr_start))
        rules[start].append(FunctionRewrite(sub_func, ntr_start, ntr_start))
        # Start * Constant, Start / Constant, Start mod Constant
        rules[start].append(FunctionRewrite(mul_func, ntr_start, ntr_start))
        rules[start].append(FunctionRewrite(div_func, ntr_start, ntr_start))
        rules[start].append(FunctionRewrite(mod_func, ntr_start, ntr_start))
        # ITE
        rules[start].append(
            FunctionRewrite(ite_func, ntr_startbool, ntr_start, ntr_start))

        # Start bool rules
        # And, or, not
        rules[start_bool].append(
            ExpressionRewrite(
                exprs.ConstantExpression(exprs.Value(False, bool_type))))
        rules[start_bool].append(
            ExpressionRewrite(
                exprs.ConstantExpression(exprs.Value(True, bool_type))))
        rules[start_bool].append(
            FunctionRewrite(and_func, ntr_startbool, ntr_startbool))
        rules[start_bool].append(
            FunctionRewrite(or_func, ntr_startbool, ntr_startbool))
        rules[start_bool].append(FunctionRewrite(not_func, ntr_startbool))
        # comparison ops
        rules[start_bool].append(FunctionRewrite(eq_func, ntr_start,
                                                 ntr_start))
        rules[start_bool].append(FunctionRewrite(ne_func, ntr_start,
                                                 ntr_start))
        rules[start_bool].append(FunctionRewrite(le_func, ntr_start,
                                                 ntr_start))
        rules[start_bool].append(FunctionRewrite(lt_func, ntr_start,
                                                 ntr_start))
        rules[start_bool].append(FunctionRewrite(ge_func, ntr_start,
                                                 ntr_start))
        rules[start_bool].append(FunctionRewrite(gt_func, ntr_start,
                                                 ntr_start))

        # Constant rules
        rules[const].append(
            ExpressionRewrite(
                exprs.ConstantExpression(exprs.Value(1, int_type))))
        rules[const].append(
            ExpressionRewrite(
                exprs.ConstantExpression(exprs.Value(0, int_type))))
        rules[const].append(FunctionRewrite(add_func, ntr_const, ntr_const))
        rules[const].append(FunctionRewrite(add_func, ntr_const, ntr_const))
        rules[const].append(FunctionRewrite(sub_func, ntr_const, ntr_const))

        if return_type == int_type:
            ret = Grammar(nts, nt_type, rules, start)
        elif return_type == bool_type:
            ret = Grammar(nts, nt_type, rules, start_bool)
        else:
            raise NotImplementedError
        ret.from_default = True
        return ret
    elif theory == 'BV':
        from semantics import semantics_bv
        from semantics import semantics_core
        print("ARSAYS: Default bit-vec grammar shouldn't be used!")
        (start, start_bool) = ('Start', 'StartBool')
        bv_size = 64
        nts = [start, start_bool]
        (bv_type, bool_type) = (exprtypes.BitVectorType(bv_size),
                                exprtypes.BoolType())
        nt_type = {start: bv_type, start_bool: bool_type}
        rules = {start: [], start_bool: []}

        ntr_start = NTRewrite(start, bv_type)
        ntr_start_bool = NTRewrite(start_bool, bool_type)

        rules[start].extend(
            map(
                lambda x: ExpressionRewrite(
                    exprs.ConstantExpression(exprs.Value(x, bv_type))),
                [0, 1]))

        for func in [
                semantics_bv.BVNot(bv_size),
                semantics_bv.BVAdd(bv_size),
                semantics_bv.BVAnd(bv_size),
                semantics_bv.BVOr(bv_size),
                semantics_bv.BVNeg(bv_size),
                semantics_bv.BVAdd(bv_size),
                semantics_bv.BVMul(bv_size),
                semantics_bv.BVSub(bv_size),
                semantics_bv.BVUDiv(bv_size),
                semantics_bv.BVSDiv(bv_size),
                semantics_bv.BVSRem(bv_size),
                semantics_bv.BVURem(bv_size),
                semantics_bv.BVShl(bv_size),
                semantics_bv.BVLShR(bv_size),
                semantics_bv.BVAShR(bv_size),
                semantics_bv.BVUlt(bv_size),
                semantics_bv.BVUle(bv_size),
                semantics_bv.BVUge(bv_size),
                semantics_bv.BVUgt(bv_size),
                semantics_bv.BVSle(bv_size),
                semantics_bv.BVSlt(bv_size),
                semantics_bv.BVSge(bv_size),
                semantics_bv.BVSgt(bv_size),
                semantics_bv.BVXor(bv_size),
                semantics_bv.BVXNor(bv_size),
                semantics_bv.BVNand(bv_size),
                semantics_bv.BVNor(bv_size),
                # semantics_bv.BVComp(bv_size)
                semantics_core.EqFunction(bv_type)
        ]:
            assert all([t == bv_type for t in func.domain_types])
            args = [ntr_start] * len(func.domain_types)

            if func.range_type == bv_type:
                rules[start].append(FunctionRewrite(func, *args))
            elif func.range_type == bool_type:
                rules[start_bool].append(FunctionRewrite(func, *args))
            else:
                assert False
        ite_func = semantics_core.IteFunction(bv_type)
        rules[start].append(
            FunctionRewrite(ite_func, ntr_start_bool, ntr_start, ntr_start))

        if return_type == bv_type:
            ret = Grammar(nts, nt_type, rules, start)
        elif return_type == bool_type:
            ret = Grammar(nts, nt_type, rules, start_bool)
        else:
            raise NotImplementedError
        ret.from_default = True
        return ret
    elif theory == 'SLIA':
        raise NotImplementedError
    else:
        raise NotImplementedError
Ejemplo n.º 9
0
 def __init__(self, bv_size):
     super().__init__('bvnot', 1, (exprtypes.BitVectorType(bv_size), ),
                      exprtypes.BitVectorType(bv_size))
     self.smt_function = lambda a: ~a
     self.eval_children = lambda a: ~a
Ejemplo n.º 10
0
 def __init__(self, bv_size):
     super().__init__('bvsgt', 2, (exprtypes.BitVectorType(bv_size),
                                   exprtypes.BitVectorType(bv_size)),
                      exprtypes.BoolType())
     self.smt_function = lambda a, b: a > b
     self.eval_children = lambda a, b: a.sgt(b)
Ejemplo n.º 11
0
 def __init__(self, bv_size):
     super().__init__('bvlshr', 2, (exprtypes.BitVectorType(bv_size),
                                    exprtypes.BitVectorType(bv_size)),
                      exprtypes.BitVectorType(bv_size))
     self.smt_function = z3.LShR
     self.eval_children = lambda a, b: a.lshr(b)