예제 #1
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)
예제 #2
0
def fetchop_func_pbe(specification, grammar, expr):
    if (exprs.is_constant_expression(expr)):
        if specification.theory == 'SLIA':
            eval_context = evaluation.EvaluationContext()
            value = evaluation.evaluate_expression_raw(expr, eval_context)
            if isinstance(value, int):
                result = fetchop(expr)
            else:
                const_category = get_constant_category(
                    value, specification.valuations)
                result = aconst_to_string(const_category)
        else:
            result = fetchop(expr)
    elif (exprs.is_formal_parameter_expression(expr)):
        # if isinstance(specification, specifications.PBESpec):
        #     n_params = len(specification.synth_fun.domain_types)
        # else: # formula spec
        #     n_params = len(specification.synth_funs[0].domain_types)
        # param_category = get_parameter_category(expr.parameter_position, n_params)
        # # print(exprs.expression_to_string(expr), ' ', param_category)

        # only a single parameter
        result = aparam_to_string(0)
    else:
        result = fetchop(expr)
    # print(exprs.expression_to_string(expr), ' ', result)
    return result
    def term_signature(self, term, points):
        eval_ctx = self.eval_ctx
        if len(self.synth_funs) > 1:
            assert exprs.is_application_of(term, ',')
            interpretations = term.children
            for func, interpretation in zip(self.synth_funs, interpretations):
                eval_ctx.set_interpretation(func, interpretation)
        else:
            eval_ctx.set_interpretation(self.synth_funs[0], term)

        retval = []
        for point in points:
            eval_ctx.set_valuation_map(point)
            try:
                r = evaluation.evaluate_expression_raw(self.canon_spec,
                                                       eval_ctx)
                # print(exprs.expression_to_string(term), "is", r, "on", [ p.value_object for p in point ])
                # print(eval_ctx.eval_stack_top)
                retval.append(r)
            except (basetypes.PartialFunctionError,
                    basetypes.UnboundLetVariableError):
                # Exceptions may be raised when applying partial functions like div, mod, etc
                retval.append(False)

        return retval
예제 #4
0
 def add_point(point, pred_sig_list, term_sig_list):
     points.append(point)
     eval_ctx.set_valuation_map(point)
     solv = evaluation.evaluate_expression_raw(sol, eval_ctx)
     new_pred_sig_list = [
         utils.bitset_extend(
             sig, evaluation.evaluate_expression_raw(pred, eval_ctx))
         for (sig, pred) in zip(pred_sig_list, preds)
     ]
     new_term_sig_list = [
         utils.bitset_extend(
             sig,
             solv == evaluation.evaluate_expression_raw(term, eval_ctx))
         for (sig, term) in zip(term_sig_list, terms)
     ]
     return (new_pred_sig_list, new_term_sig_list)
예제 #5
0
 def _verify_expr(self, term):
     eval_ctx = self.eval_ctx
     for point, value in self.valuations.items():
         eval_ctx.set_valuation_map(point)
         result = evaluation.evaluate_expression_raw(term, eval_ctx)
         if result != value:
             return [point]
     return term
예제 #6
0
 def _initialize_valuations(self, expr_valuations):
     eval_ctx = self.eval_ctx
     def evaluate(t):
         return 
     self.valuations = {}
     for args, value in expr_valuations:
         raw_args = tuple([ evaluation.evaluate_expression(a, eval_ctx) for a in args ])
         raw_value = evaluation.evaluate_expression_raw(value, eval_ctx)
         self.valuations[raw_args] = raw_value
예제 #7
0
    def _verify_guard_term_list(self, guard_term_list, dt_tuple):
        eval_ctx = self.eval_ctx
        cex_points = []
        selected_leaf_terms = []

        at_least_one_branch_failed = False

        for (pred, term_list) in guard_term_list:
            good_terms = term_list.copy()

            for point, value in self.spec.valuations.items():
                eval_ctx.set_valuation_map(point)
                try:
                    if not evaluation.evaluate_expression_raw(pred, eval_ctx):
                        continue

                    next_good_terms = []
                    for term in good_terms:
                        curr_value = evaluation.evaluate_expression_raw(
                            term, eval_ctx)
                        if curr_value == value:
                            next_good_terms.append(term)
                    good_terms = next_good_terms

                    if len(good_terms) == 0:
                        at_least_one_branch_failed = True
                        cex_points.append(point)
                        break
                except:
                    continue

            if len(good_terms) > 0:
                selected_leaf_terms.append(good_terms[0])

        if at_least_one_branch_failed:
            retval = list(set(cex_points))
            retval.sort()
            return retval
        else:
            (term_list, term_sig_list, pred_list, pred_sig_list, dt) = dt_tuple
            e = decision_tree_to_expr(dt, pred_list, self.syn_ctx,
                                      selected_leaf_terms)
            return e
예제 #8
0
 def _compute_signature(self, expr):
     if self.applications is None:
         # Single invocation (not multifunction)
         points = self.points
         res = [ None ] * len(points)
         for i in range(len(points)):
             # Assumes introvars are at the beginning of the point
             self.eval_ctx.set_valuation_map(points[i])
             res[i] = evaluation.evaluate_expression_raw(expr, self.eval_ctx)
         return res
     else:
         points = self.points
         res = [ None ] * len(points)
         for i in range(len(points)):
             sig = []
             for profile in self.point_profiles[i]:
                 self.eval_ctx.set_valuation_map(profile)
                 sig.append(evaluation.evaluate_expression_raw(expr, self.eval_ctx))
             res[i] = sig
         return res
예제 #9
0
 def verify_term_solve(self, terms):
     eval_ctx = self.eval_ctx
     for point, value in self.valuations.items():
         eval_ctx.set_valuation_map(point)
         found_one = False
         for term in terms:
             result = evaluation.evaluate_expression_raw(term, eval_ctx)
             if result == value:
                 found_one = True
         if not found_one:
             return [point]
     return None
예제 #10
0
def get_pbespec_kind_bv(specification, grammar):
    generator_factory = enumerators.RecursiveGeneratorFactory()
    term_generator = grammar.to_generator(generator_factory)
    max_size = 3
    term_generator.set_size(max_size)
    smallexprs = [exp for exp in term_generator.generate()]
    inputs2values = {}

    valuations = specification.valuations
    # value : str | int | utils.BitVector | bool
    # valuations: exprs.Value tuple -> str | int | utils.BitVector | bool
    # we make inputs be of primitive type
    flags = set([])
    eval_context = evaluation.EvaluationContext()
    for inputs, output in valuations.items():
        eval_context.set_valuation_map(inputs)
        values = []
        for se in smallexprs:
            try:
                values.append(
                    evaluation.evaluate_expression_raw(se, eval_context))
            except:
                continue
        inputs2values[inputs] = values
        if (any(is_sub(v, output) for v in values)):
            flags.add(PBESpecKind.SOME_INPUT_BELONG_TO_OUTPUT)
        if (any(is_sub(output, v) for v in values)):
            flags.add(PBESpecKind.SOME_OUTPUT_BELONG_TO_INPUT)
        if (any(is_intersection_nonempty(output, v) for v in values)):
            flags.add(PBESpecKind.SOME_INPUT_INTERSECT_OUTPUT)
        # if (any(i1 != i2 and is_sub(i1, i2) for (i1, i2) in itertools.product(inputs, inputs))):
        #     flags.add(PBESpecKind.SOME_INPUT_BELONG_TO_SOME_INPUT)

    if (all(
            any(is_sub(value, output) for value in inputs2values[inputs])
            for inputs, output in valuations.items())):
        flags.add(PBESpecKind.ALL_INPUT_BELONG_TO_OUTPUT)
    if (all(
            any(is_sub(output, value) for value in inputs2values[inputs])
            for inputs, output in valuations.items())):
        flags.add(PBESpecKind.ALL_OUTPUT_BELONG_TO_INPUT)
    if (all(
            any(
                is_intersection_nonempty(output, value)
                for value in inputs2values[inputs])
            for inputs, output in valuations.items())):
        flags.add(PBESpecKind.ALL_INPUT_INTERSECT_OUTPUT)

    if len(flags) == 0:
        flags.add(PBESpecKind.NO_INTERSECTION)
    return tuple(flags)
예제 #11
0
        def compute_indicator(term, points):
            eval_ctx.set_interpretation(func, term)

            retval = []
            for point in points:
                eval_ctx.set_valuation_map(point)
                try:
                    retval.append(
                        evaluation.evaluate_expression_raw(
                            indicator_expr, eval_ctx))
                except (basetypes.UnboundLetVariableError,
                        basetypes.PartialFunctionError):
                    # Can't mess up on predicates
                    return [False] * len(points)
            return retval
예제 #12
0
    def _verify_expr(self, term):
        eval_ctx = self.eval_ctx
        for point, value in self.valuations.items():
            eval_ctx.set_valuation_map(point)
            try:
                result = evaluation.evaluate_expression_raw(term, eval_ctx)
                if result != value:
                    if options.allex:
                        return [
                            point for point, value in self.valuations.items()
                        ]
                    else:
                        return [point]
            except:
                continue

        return term
예제 #13
0
def evaluate(expr, input, synth_file='./euphony/benchmarks/string/test/phone-5-test.sl'):
    '''Evaluate an expression for a given input

    Arguments:
        - expr(FunctionExpression): The expression to evaluate
        - input(str): the input to the given expression.
        - synth_file(str): path to a synthesis file defining the available
          available grammar. This file doesn't need to correspond to the same
          file that the expression was generated from. It just needs to have the
          same grammar definition. Constraints are not used in any way. It's
          required in order to load the proper logic into the expression
          evaluator. This is set to a file containing string logic by default.

    Returns:
        - (str): output from evaluating expression against given input
    '''
    eval_ctx = EvaluationContext()
    val_map = (Value(input, value_type=StringType()),)
    eval_ctx.set_valuation_map(val_map)
    # print('attempting to eval: {}'.format(expr))
    # print('eval ctx: {} -- {}'.format(val_map, type(val_map)))

    # This is pretty flow since it needs to re-read the synthesis file to know
    # the grammar that it needs to parse. Personally, I am not so concerned
    # about performance, but rather just getting it to work in the first place.
    file_sexp = parser.sexpFromFile(synth_file)
    benchmark_tuple = parser.extract_benchmark(file_sexp)
    (
            theories,
            syn_ctx,
            synth_instantiator,
            macro_instantiator,
            uf_instantiator,
            constraints,
            grammar_map,
            forall_vars_map,
            default_grammar_sfs
            ) = benchmark_tuple
    # We should only have one grammar, so just grab the only key to this map.
    # The type of the object is a SynthFun
    #print(constraints)
    synth_fun = list(grammar_map.keys())[0]
    # Pass this into the interpretation along with the expression we're
    # attempting to evaluate, or else we'll get an UnboundLetException
    eval_ctx.set_interpretation(synth_fun, expr)
    return evaluate_expression_raw(expr, eval_ctx)
예제 #14
0
    def term_signature(self, term, points):
        # try:
            eval_ctx = self.eval_ctx
            eval_ctx.set_interpretation(self.synth_fun, term)
            synth_fun_expr = self.synth_fun_expr

            retval = []
            for point in points:
                if point not in self.valuations:
                    # print("Something is almost certainly wrong!")
                    retval.append(True)
                    continue
                eval_ctx.set_valuation_map(point)
                try:
                    retval.append(self.valuations[point] == evaluation.evaluate_expression_raw(synth_fun_expr, eval_ctx))
                except (basetypes.PartialFunctionError, basetypes.UnboundLetVariableError):
                    retval.append(False)

            return retval
예제 #15
0
    def unify(self):
        term_solver = self.term_solver
        sig_to_term = term_solver.get_signature_to_term()

        # print([ f.function_name for f in self.synth_funs])
        # for point in self.points:
        # print([ c.value_object for c in point])
        # for (sig, term) in sig_to_term.items():
        # print(str(sig), exprs.expression_to_string(term))
        eval_ctx = self.eval_ctx
        self.last_dt_size = 0

        triv = self._try_trivial_unification()
        if triv is not None:
            yield ("TERM", triv)
            return

        # print([ [ pi.value_object for pi in p ] for p in self.points])

        pred_terms = []

        # Pick terms which cover maximum number of points
        sigs = [(s, s) for s in sig_to_term.keys()]
        while True:
            full_sig, curr_sig = max(sigs, key=lambda fc: len(fc[1]))

            # We have covered all points
            if len(curr_sig) == 0:
                break

            term = sig_to_term[full_sig]
            pred = self._compute_pre_condition(full_sig, curr_sig, term)
            pred_terms.append((pred, term))

            pred_sig = BitSet(len(self.points))
            for i in curr_sig:
                eval_ctx.set_valuation_map(self.points[i])
                if evaluation.evaluate_expression_raw(pred, eval_ctx):
                    pred_sig.add(i)
            assert not pred_sig.is_empty()

            # Remove newly covered points from all signatures
            sigs = [(f, c.difference(pred_sig)) for (f, c) in sigs]

        # for pred, term in pred_terms:
        #     print(_expr_to_str(pred), ' ====> ', _expr_to_str(term))
        e = self._pred_term_list_to_expr(pred_terms)
        if len(self.synth_funs) == 1:
            act_params = self.spec.canon_application[
                self.synth_funs[0]].children
            form_params = self.spec.formal_params[self.synth_funs[0]]
            e = exprs.substitute_all(e, list(zip(act_params, form_params)))
        else:
            es = []
            for ep, sf in zip(e, self.synth_funs):
                act_params = self.spec.canon_application[sf].children
                form_params = self.spec.formal_params[sf]
                es.append(
                    exprs.substitute_all(ep, list(zip(act_params,
                                                      form_params))))
            domain_types = tuple([exprtypes.IntType()] * len(self.synth_funs))
            e = exprs.FunctionExpression(
                semantics_core.CommaFunction(domain_types), tuple(es))
        yield ('TERM', e)
예제 #16
0
 def eval_on_relevent_points(pred):
     ret = []
     for p in relevent_points:
         eval_ctx.set_valuation_map(p)
         ret.append(evaluation.evaluate_expression_raw(pred, eval_ctx))
     return ret