Beispiel #1
0
    def test_dumped_logic(self):
        # Dumped logic matches the logic in the example
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            buf_out = cStringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf_out)

            buf_in = cStringIO(buf_out.getvalue())
            parser = SmtLibParser()
            script_in = parser.get_script(buf_in)
            for cmd in script_in:
                if cmd.name == "set-logic":
                    logic_in = cmd.args[0]
                    if logic == logics.QF_BOOL:
                        self.assertEqual(logic_in, logics.QF_UF)
                    elif logic == logics.BOOL:
                        self.assertEqual(logic_in, logics.LRA)
                    else:
                        self.assertEqual(logic_in, logic, script_in)
                    break
            else:  # Loops exited normally
                print("-" * 40)
                print(script_in)
Beispiel #2
0
def main(path):
    parser = SmtLibParser()
    script = parser.get_script_fname(path)
    formula = script.get_last_formula()
    h = SegmentsHeuristics(formula, 'z3')
    l = h.try_to_unsatisfy()
    print(l)
Beispiel #3
0
def configure(inpcode, options):
  global solver
  global solver_name
  if ("pysmt" in options):
    solver_name = options["pysmt"]
  elif ("smtpipe" in options):
    solver_name = "custom: " + options["smtpipe"].split("/")[-1]
    solver_cmd = options["smtpipe"]
    # solver_logics = [BOOL, LIA, LRA, NIA, NRA, QF_LRA, QF_NIA, QF_NRA, QF_UFLIA, QF_UFLRA, QF_UFNIA, QF_UFNRA, UFLIRA, UFLRA, UFNIA, AUFNIRA]
    # solver_logics = [AUFNIRA]
    solver_logic = eval(options["pipe_logic"])
    solver_logics = [solver_logic]
    env = get_env()
    env.factory.add_generic_solver(solver_name, solver_cmd, solver_logics)
  verbprint = utils.verbprint
  verbprint(2, "Parsing the input...", False)
  parser = SmtLibParser()
  script = parser.get_script(inpcode)
  formula = script.get_last_formula()
  # print "Got a formula: " + str(f)
  verbprint(2, "done.", True)
  verbprint(2, "Initializing the solver...", False)
  if ("smtpipe" in options):
    solver = Solver(name=solver_name, logic=solver_logic)
    #solver = SmtLibSolver(options["smtpipe"], env, LRA)
  else:
    solver = Solver(name=solver_name)
  verbprint(2, "done.", True)
  verbprint(2, "Loading the input file in the solver...", False)
  solver.add_assertion(formula)
  verbprint(2, "done.", True)
Beispiel #4
0
 def test_define_funs_arg_and_fun(self):
     smtlib_script = "\n".join(['(define-fun f ((n Int)) Int n)', '(declare-fun n () Real)'])
     stream = StringIO(smtlib_script)
     parser = SmtLibParser()
     _ = parser.get_script(stream)
     # No exceptions are thrown
     self.assertTrue(True)
Beispiel #5
0
    def __init__(self, env=None, interactive=False):
        self._parser = SmtLibParser()
        SmtLibParser.__init__(self, env, interactive)

        self.orig = System()
        self.curr = System()

        self.strat = StratificationOracle()
        self.syntax = SyntaxInference()

        self._sorts = set()
        self._sort2fin = dict()
        self._enumsorts = dict()
        self._enum2qvar = dict()
        self._fin2sort = dict()
        self._enum2inf = dict()

        self._ordered_sorts = dict()
        self._ordered_min = dict()
        self._ordered_max = dict()
        self._dependency_height = dict()
        self._enum_height = dict()

        self._idx = 0
        self.gen = common.gopts.gen
        self._child_sort = set()
        self._parent_sort = dict()
        self._definitionMap = dict()
Beispiel #6
0
def remove_quant_and_define_funs(path):
    parser = SmtLibParser()
    script = parser.get_script_fname(path)
    formula = script.get_last_formula()
    prenex_normalizer = PrenexNormalizer()
    quantifications, matrix = prenex_normalizer.walk(formula)
    quantifier, variables = quantifications[0]
    assert (len(quantifications) == 1)
    env = get_env()
    mgr = env.formula_manager
    subs = dict((x, mgr.FreshSymbol(x.symbol_type())) for x in variables)
    substitued_matrix = matrix.substitute(subs)

    declarations = []
    declarations.extend(script.filter_by_command_name("declare-sort"))
    declarations.extend(script.filter_by_command_name("declare-fun"))
    declarations.extend(script.filter_by_command_name("declare-const"))
    new_script = SmtLibScript()
    new_script.add(SET_LOGIC, [QF_NRA])
    for key in subs.keys():
        new = subs[key]
        new_script.add("declare-fun", [new])
    for declaration in declarations:
        new_script.add_command(declaration)
    new_script.add("assert", [substitued_matrix])
    new_script.add("check-sat", [])
    buf = cStringIO()
    new_script.serialize(buf, False)
    smtlib = buf.getvalue()
    print(smtlib)
Beispiel #7
0
    def __init__(self, args, environment, logic, LOGICS=None, **options):
        Solver.__init__(self, environment, logic=logic, **options)
        self.to = self.environment.typeso
        if LOGICS is not None: self.LOGICS = LOGICS
        self.args = args
        self.declared_vars = [set()]
        self.declared_sorts = [set()]
        self.solver = Popen(args,
                            stdout=PIPE,
                            stderr=PIPE,
                            stdin=PIPE,
                            bufsize=-1)
        # Give time to the process to start-up
        time.sleep(0.01)
        self.parser = SmtLibParser(interactive=True)
        if PY2:
            self.solver_stdin = self.solver.stdin
            self.solver_stdout = self.solver.stdout
        else:
            self.solver_stdin = TextIOWrapper(self.solver.stdin)
            self.solver_stdout = TextIOWrapper(self.solver.stdout)

        # Initialize solver
        if options.get("produce_interpolants"):
            self.set_option(":produce-interpolants", "true")
        self.options(self)
        self.set_logic(logic)
Beispiel #8
0
    def test_basic2(self):
        model_source = """\
(model
  ;; universe for U:
  ;;   (as @val1 U) (as @val0 U)
  (define-fun b () U
    (as @val0 U))
  (define-fun a () U
    (as @val1 U))
  (define-fun f ((x!0 U)) U
    (ite (= x!0 (as @val1 U)) (as @val0 U)
      (as @val1 U)))
)
"""
        model_buf = StringIO(model_source)

        parser = SmtLibParser()
        simplifier = SmtLibModelValidationSimplifier(self.env)

        U = self.tm.Type('U', 0)

        # We construct the model even befor symbols are constructed in pysmt
        model, interpretations = parser.parse_model(model_buf)

        a = self.fm.Symbol('a', U)
        b = self.fm.Symbol('b', U)
        f = self.fm.Symbol('f', FunctionType(U, [U]))
        formula = self.fm.And(self.fm.Not(self.fm.Equals(a, b)),
                              self.fm.Equals(self.fm.Function(f, [a]), b))

        simp = simplifier.simplify(formula.substitute(model, interpretations))
        self.assertEqual(simp, self.fm.TRUE())
Beispiel #9
0
    def __init__(self, args, environment, logic, LOGICS=None, **options):
        Solver.__init__(self, environment, logic=logic)

        # Flag used to debug interaction with the solver
        self.dbg = False

        if LOGICS is not None: self.LOGICS = LOGICS
        self.args = args
        self.declared_vars = set()
        self.solver = Popen(args,
                            stdout=PIPE,
                            stderr=PIPE,
                            stdin=PIPE,
                            bufsize=-1)
        # Give time to the process to start-up
        time.sleep(0.01)
        self.parser = SmtLibParser(interactive=True)
        if PY2:
            self.solver_stdin = self.solver.stdin
            self.solver_stdout = self.solver.stdout
        else:
            self.solver_stdin = TextIOWrapper(self.solver.stdin)
            self.solver_stdout = TextIOWrapper(self.solver.stdout)

        # Initialize solver
        self.set_option(":print-success", "true")
        if self.options.generate_models:
            self.set_option(":produce-models", "true")
        # Redirect diagnostic output to stdout
        self.set_option(":diagnostic-output-channel", '"stdout"')
        self.set_logic(logic)
Beispiel #10
0
def extract_var_relationship(var_expr_map):
    # preserve user-input : program variable relationship
    # include program variable names for program specification
    parser = SmtLibParser()
    relationship = None
    for expr_map in var_expr_map:
        prog_var_name, prog_var_expr = expr_map[0]
        angelic_var_name, angelic_var_expr = expr_map[1]
        prog_dependent_var_list = set(
            re.findall("\(select (.+?) \(_ ", prog_var_expr))
        angelic_dependent_var_list = set(
            re.findall("\(select (.+?) \(_ ", angelic_var_expr))
        dependent_var_list = set(
            list(prog_dependent_var_list) + list(angelic_dependent_var_list))

        str_script = "(set-logic QF_AUFBV )\n"
        str_script += "(declare-fun " + prog_var_name + " () (Array (_ BitVec 32) (_ BitVec 8) ) )\n"
        for var_d in dependent_var_list:
            str_script += "(declare-fun " + var_d + " () (Array (_ BitVec 32) (_ BitVec 8) ) )\n"
        str_script += "(assert (= " + prog_var_expr + " " + angelic_var_expr + " ))\n"
        str_script += "(assert (= " + prog_var_name + " " + angelic_var_name + " ))\n"
        str_script += "(exit)\n"
        script = parser.get_script(cStringIO(str_script))
        formula = script.get_last_formula()
        if not relationship:
            relationship = formula
        else:
            relationship = And(relationship, formula)
    return relationship
Beispiel #11
0
def execute_script_fname(smtfile, logic, expected_result):
    """Read and call a Solver to solve the instance"""

    reset_env()
    Solver = get_env().factory.Solver
    parser = SmtLibParser()
    script = parser.get_script_fname(smtfile)
    try:
        solver = Solver(logic=logic, incremental=False, generate_models=False)
        if logic == QF_UF and type(solver).__name__ == 'CVC4Solver':
            warnings.warn(
                "Test (%s, %s) skipped because CVC4 can't handle QF_UF." %
                (logic, smtfile))
            return
        if logic == QF_UF and type(solver).__name__ == 'BoolectorSolver':
            warnings.warn(
                "Test (%s, %s) skipped because Boolector can't handle QF_UF." %
                (logic, smtfile))
            return
        log = script.evaluate(solver)
    except NoSolverAvailableError:
        raise SkipTest("No solver for logic %s." % logic)
    except SolverReturnedUnknownResultError:
        if not logic.quantifier_free:
            warnings.warn(
                "Test (%s, %s) could not be solved due to quantifiers." %
                (logic, smtfile))
            return
        raise

    res = check_sat_filter(log)
    assert expected_result == res
Beispiel #12
0
def validateModel(smtFile, modelFile):
    try:
        if (not path.exists(smtFile)):
            raise Exception("File not found: {}".format(smtFile))

        if (not path.exists(modelFile)):
            raise Exception("File not found: {}".format(modelFile))

        parser = SmtLibParser()

        formula = readSmtFile(parser, smtFile)
        symbols = getSymbols(parser)
        model = readModel(parser, modelFile)

        checkFullModel(model, symbols)

        result = simplify(formula.substitute(model))

        if (not result.is_constant()):
            print("INVALID: Did not provide a full model.")
        elif (not result.is_true()):
            print("INVALID: Model does not evaluate to true.")
        else:
            print("VALID")
    except Exception as e:
        print(e)
        sys.exit(1)
Beispiel #13
0
def execute_script_fname(smtfile, logic, expected_result):
    """Read and call a Solver to solve the instance"""

    reset_env()
    Solver = get_env().factory.Solver
    parser = SmtLibParser()
    script = parser.get_script_fname(smtfile)
    try:
        log = script.evaluate(
            Solver(logic=logic, incremental=False, generate_models=False))
    except NoSolverAvailableError:
        raise SkipTest("No solver for logic %s." % logic)
    except SolverReturnedUnknownResultError:
        if not logic.quantifier_free:
            warnings.warn(
                "Test (%s, %s) could not be solver due to quantifiers." %
                (logic, smtfile))
            return
        raise

    res = check_sat_filter(log)
    if res:
        assert expected_result == "sat"
    else:
        assert expected_result == "unsat"
Beispiel #14
0
def collect_symbolic_path(log_path, project_path):
    """
       This function will read the output log of a klee concolic execution and
       extract the partial path conditions
    """
    emitter.normal("\textracting path conditions")
    ppc_list = list()
    last_sym_path = ""
    if os.path.exists(log_path):
        source_path = ""
        path_condition = ""
        with open(log_path, 'r') as trace_file:
            for line in trace_file:
                if '[path:ppc]' in line:
                    if project_path in line or definitions.DIRECTORY_LIB in line:
                        source_path = str(line.replace("[path:ppc]", '')).split(" : ")[0]
                        source_path = source_path.strip()
                        source_path = os.path.abspath(source_path)
                        path_condition = str(line.replace("[path:ppc]", '')).split(" : ")[1]
                        continue
                if source_path:
                    if "(exit)" not in line:
                        path_condition = path_condition + line
                    else:
                        ppc_list.append((source_path, path_condition))
                        last_sym_path = path_condition
                        source_path = ""
                        path_condition = ""
    # constraints['last-sym-path'] = last_sym_path
    # print(constraints.keys())
    parser = SmtLibParser()
    script = parser.get_script(cStringIO(last_sym_path))
    formula = script.get_last_formula()
    return ppc_list, formula
Beispiel #15
0
    def __init__(self,
                 args,
                 environment,
                 logic,
                 user_options=None,
                 LOGICS=None):
        Solver.__init__(self,
                        environment,
                        logic=logic,
                        user_options=user_options)

        if LOGICS is not None: self.LOGICS = LOGICS
        self.args = args
        self.declared_vars = set()
        self.solver = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE)
        self.parser = SmtLibParser()
        if PY2:
            self.solver_stdin = self.solver.stdin
            self.solver_stdout = self.solver.stdout
        else:
            self.solver_stdin = TextIOWrapper(self.solver.stdin)
            self.solver_stdout = TextIOWrapper(self.solver.stdout)

        self.dbg = False

        # Initialize solver
        self._send_command(
            SmtLibCommand(smtcmd.SET_OPTION, [":print-success", "false"]))
        self._send_command(
            SmtLibCommand(smtcmd.SET_OPTION, [":produce-models", "true"]))

        if self.options is not None:
            for o, v in iteritems(self.options):
                self._send_command(SmtLibCommand(smtcmd.SET_OPTION, [o, v]))
        self._send_command(SmtLibCommand(smtcmd.SET_LOGIC, [logic]))
Beispiel #16
0
    def test_dumped_logic(self):
        # Dumped logic matches the logic in the example.
        #
        # There are a few cases where we use a logic
        # that does not exist in SMT-LIB, and the SMT-LIB
        # serialization logic will find a logic that
        # is more expressive. We need to adjust the test
        # for those cases (see rewrite dict below).
        rewrite = {
            logics.QF_BOOL: logics.QF_UF,
            logics.BOOL: logics.LRA,
            logics.QF_NIRA: logics.AUFNIRA,
        }
        fs = get_example_formulae()

        for (f_out, _, _, logic) in fs:
            buf_out = StringIO()
            script_out = smtlibscript_from_formula(f_out)
            script_out.serialize(outstream=buf_out)
            buf_in = StringIO(buf_out.getvalue())
            parser = SmtLibParser()
            script_in = parser.get_script(buf_in)
            for cmd in script_in:
                if cmd.name == "set-logic":
                    logic_in = cmd.args[0]
                    self.assertEqual(logic_in, rewrite.get(logic, logic))
                    break
            else:  # Loops exited normally
                print("-"*40)
                print(script_in)
Beispiel #17
0
 def parse(self, file_id):
     fname = SMTLIB_FILE_PATTERN % file_id
     reset_env()
     parser = SmtLibParser()
     script = parser.get_script_fname(fname)
     self.assertIsNotNone(script)
     return script
Beispiel #18
0
 def test_parse_bvconst_width(self):
     smtlib_input = "(assert (> #x10 #x10))"
     parser = SmtLibParser()
     buffer_ = cStringIO(smtlib_input)
     expr = parser.get_script(buffer_).get_last_formula()
     const = expr.args()[0]
     self.assertEqual(const.bv_width(), 8, const.bv_width())
Beispiel #19
0
 def test_define_fun_serialize_complex_type(self):
     smtlib_script = '(define-fun f ((var (_ BitVec 32))) (_ BitVec 32) var)'
     stream = StringIO(smtlib_script)
     parser = SmtLibParser()
     script = parser.get_script(stream)
     # No exceptions are thrown
     self.assertEqual(smtlib_script.replace('var', '__var0'), script.commands[0].serialize_to_string())
Beispiel #20
0
 def __init__(self, string):
     self.script_string = string
     parser = SmtLibParser()
     self.script = parser.get_script(cStringIO(string))
     self.symbols = self.get_all_symbols()
     self.int, self.real = self.get_all_number_symbols()
     self.node_num = 0
Beispiel #21
0
    def read_properties(self, path: str) -> dict:
        """
        This method reads the SMT property file and
        creates a property for each node.

        Parameters
        ----------
        path : str
            The SMT-LIB file path.

        Returns
        -------
        dict
            The dictionary of properties

        """

        parser = SmtLibParser()
        try:
            script = parser.get_script_fname(path)
        except PysmtException:
            dialog = MessageDialog("Failed to parse SMT property.",
                                   MessageType.ERROR)
            dialog.exec()
            return dict()

        declarations = script.filter_by_command_name(
            ['declare-fun', 'declare-const'])
        assertions = script.filter_by_command_name('assert')
        var_set = []
        var_list = []
        properties = dict()

        for d in declarations:
            var_list.append(str(d.args[0]).replace('\'', ''))
            varname = str(d.args[0]).split('_')[0].replace(
                '\'', '')  # Variable format is <v_name>_<idx>
            if varname not in var_set:
                var_set.append(varname)

        counter = 0

        for a in assertions:
            line = str(a.args[0]).replace('\'', '')
            for v in var_set:
                if f" {v}" in line or f"({v}" in line:  # Either '(v ...' or '... v)'
                    if v not in properties.keys():
                        properties[v] = PropertyBlock(f"{counter}Pr",
                                                      "Generic SMT")
                        properties[v].smt_string = ''
                        properties[v].variables = list(
                            filter(lambda x: v in x, var_list))
                        counter += 1
                    conv = ExpressionTreeConverter()
                    wrap = conv.build_from_infix(line).as_prefix()
                    properties[v].smt_string += f"(assert {wrap})\n"
                    break

        return properties
Beispiel #22
0
 def test_typing_define_fun(self):
     script = """
     (define-fun x () Int 8.2)
     """
     p = SmtLibParser()
     buffer = StringIO(script)
     with self.assertRaises(PysmtSyntaxError):
         p.get_script(buffer)
Beispiel #23
0
 def test_parse_declare_const(self):
     smtlib_input = """
     (declare-const s Int)
     (check-sat)"""
     parser = SmtLibParser()
     buffer_ = cStringIO(smtlib_input)
     script = parser.get_script(buffer_)
     self.assertIsNotNone(script)
Beispiel #24
0
 def test_define_funs_same_args(self):
     # n is defined once as an Int and once as a Real
     smtlib_script = "\n".join(['(define-fun f ((n Int)) Int n)', '(define-fun f ((n Real)) Real n)'])
     stream = StringIO(smtlib_script)
     parser = SmtLibParser()
     _ = parser.get_script(stream)
     # No exceptions are thrown
     self.assertTrue(True)
Beispiel #25
0
def main(path, solver_name):
    solver = Solver(solver_name)
    parser = SmtLibParser()
    script = parser.get_script_fname(path)
    formula = script.get_last_formula()
    solver.add_assertion(formula)
    result = solver.solve()
    print(result)
Beispiel #26
0
 def test_define_funs_same_args(self):
     smtlib_script = "\n".join([
         '(define-fun f ((n Int)) Int n)',
         '(define-fun f ((n Real)) Real n)'
     ])
     stream = cStringIO(smtlib_script)
     parser = SmtLibParser()
     script = parser.get_script(stream)
Beispiel #27
0
 def test_smtlib_define_fun_serialization(self):
     smtlib_input = "(define-fun init ((x Bool)) Bool (and x (and x (and x (and x (and x (and x x)))))))"
     parser = SmtLibParser()
     buffer_ = cStringIO(smtlib_input)
     s = parser.get_script(buffer_)
     for c in s:
         res = c.serialize_to_string(daggify=False)
     self.assertEqual(res.replace('__x0', 'x'), smtlib_input)
Beispiel #28
0
 def test_incomplete_stream(self):
     txt = """
     (declare-fun A () Bool)
     (declare-fun B () Bool)
     (assert (and A
     """
     parser = SmtLibParser()
     with self.assertRaises(PysmtSyntaxError):
         parser.get_script(StringIO(txt))
Beispiel #29
0
def bmc_summarize(smtin_filename="UNNAMED_in.smt2",
                  smtout_filename="UNNAMED_out.smt2"):

    parser = SmtLibParser()
    with open(smtin_filename, 'r+') as f:
        smtlib = parser.get_script(f)

    asserts = list(smtlib.filter_by_command_name([smtcmd.ASSERT]))
    defs = list(smtlib.filter_by_command_name([smtcmd.DEFINE_FUN]))
    decls = list(smtlib.filter_by_command_name([smtcmd.DECLARE_FUN]))

    #print(smtlib.get_last_formula())
    func_summary = None
    for stmt in asserts:
        #print(stmt)
        if stmt.args[0].is_iff() or stmt.args[0].is_equals():
            assert stmt.args[0].arg(0).is_symbol() and stmt.args[0].arg(1)
            #print("Assertion on a symbolic equation.")
            symbol_table[stmt.args[0].arg(0).symbol_name()] = stmt.args[0].arg(
                1)
        #pattern match for assertion on summary (PROPERTY: !(... = 0) )
        if stmt.args[0].is_not():
            safety_prop = stmt.args[0].arg(0)
            if (safety_prop.is_equals() or safety_prop.is_iff()
                ) and safety_prop.arg(1).is_bv_constant(0):
                func_summary = safety_prop.arg(0)

    summarizer = IdentityDagWalker()
    try:
        summary = summarizer.walk(func_summary)
    except:
        print("Could not summarize the summary.")
        import pdb
        pdb.set_trace()  #Exception raised.
    #import pdb; pdb.set_trace() #PLAY WITH REPRESENTATION in pdb if desired

    #Print to stdout in SMTLib format:
    print(";Summary looks like:\n")
    print(summary.to_smtlib(False) + "\n")

    #Rewrite back into SMTLibScript, then print simplification back to file
    newscript = SmtLibScript()
    newscript.add(smtcmd.SET_OPTION, [':produce-models', 'true'])
    newscript.add(smtcmd.SET_LOGIC, ["QF_AUFBV"])
    for decl in decls:
        newscript.add_command(decl)
    newscript.add(
        smtcmd.ASSERT,
        [Not(Equals(summary, BVZero(width=32)))
         ])  #NOTE: need the !(...=0) structure again, for the assertion
    newscript.add(smtcmd.CHECK_SAT, [])
    newscript.add(smtcmd.EXIT, [])

    with open(smtout_filename, 'w+') as f:
        newscript.serialize(f, daggify=False)
Beispiel #30
0
    def smtlib_solver(self, stream):
        smt_parser = SmtLibParser()
        name = self.args.solver
        if name == "auto":
            solver = Solver()
        else:
            solver = Solver(name=name)

        for cmd in smt_parser.get_command_generator(stream):
            r = evaluate_command(cmd, solver)
            self.print_result(cmd, r)