Exemple #1
0
    def test_walker_new_operators_complete(self):
        walkerA = IdentityDagWalker(env=self.env)
        idx = op.new_node_type(node_str="fancy_new_node")
        walkerB = IdentityDagWalker(env=self.env)
        with self.assertRaises(KeyError):
            walkerA.functions[idx]
        self.assertEqual(walkerB.functions[idx], walkerB.walk_error)

        # Use a mixin to handle the node type
        class FancyNewNodeWalkerMixin(object):
            def walk_fancy_new_node(self, args, **kwargs):
                raise UnsupportedOperatorError

        class IdentityDagWalker2(IdentityDagWalker, FancyNewNodeWalkerMixin):
            pass

        walkerC = IdentityDagWalker2(env=self.env)
        self.assertEqual(walkerC.functions[idx], walkerC.walk_fancy_new_node)
Exemple #2
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)
Exemple #3
0
    def test_identity_walker_simple(self):
        def walk_and_to_or(formula, args, **kwargs):
            return Or(args)

        def walk_or_to_and(formula, args, **kwargs):
            return And(args)

        walker = IdentityDagWalker(env=get_env())

        walker.set_function(walk_and_to_or, op.AND)
        walker.set_function(walk_or_to_and, op.OR)

        x, y, z = Symbol('x'), Symbol('y'), Symbol('z')

        cnf = And(Or(x, y, z), Or(z, Not(y)))
        fake_dnf = Or(And(x, y, z), And(z, Not(y)))
        result = walker.walk(cnf)
        self.assertEqual(result, fake_dnf)

        alternation = Or(cnf, Not(cnf))
        expected = And(fake_dnf, Not(fake_dnf))
        result = walker.walk(alternation)
        self.assertEqual(result, expected)
Exemple #4
0
 def test_identity_dag_walker(self):
     idw = IdentityDagWalker()
     for (f, _, _, _) in get_example_formulae():
         rebuilt = idw.walk(f)
         self.assertTrue(rebuilt == f, "Rebuilt formula is not identical")