Exemplo n.º 1
0
def load_data():
    import argparse

    a = argparse.ArgumentParser()
    a.add_argument('filename', type=str, nargs='?', default=argparse.SUPPRESS)
    a.add_argument('-u',
                   action='store_true',
                   help="universal invariant inference")
    a.add_argument('-a',
                   action='store_true',
                   help="(experimental) use alpha-from-below")
    a.add_argument('-p',
                   action='store_true',
                   help="(experimental) use partial models, not sound")
    a.add_argument('-e',
                   action='store_true',
                   help="(experimental) stuff, broken")
    a.add_argument('--gen-enums',
                   action='store_true',
                   help="generalize enum values")
    a.add_argument('--no-preds-dll',
                   action='store_true',
                   help="do not generate abstraction predicates for dll")
    a.add_argument('--no-preds-sorted',
                   action='store_true',
                   help="do not generate 'sorted' abstraction predicates")
    a.add_argument('--domain', type=str, default=None)
    a.add_argument('--disable-opt',
                   dest="o",
                   action='store_false',
                   default=True,
                   help="(development) disable IC3 optimizations")
    a.add_argument(
        '--out-inv',
        action='store_true',
        help=
        "print the discovered invariant as a first-order formula (requires unicode support)"
    )
    a.add_argument('--latex',
                   action='store_true',
                   help="typeset discovered invariant using LaTeX")
    a.add_argument('-v', '--verbose', action='store_true')
    args = a.parse_args()

    if not hasattr(args, 'filename'):
        import os.path
        from filesystem.paths import find_closest
        here = os.path.dirname(__file__)
        args.filename = os.path.join(find_closest('benchmarks', start_at=here),
                                     'sll-last.imp')

    program = open(args.filename).read().decode('utf-8')
    print "* BENCHMARK"
    # print os.path.basename(args.filename),
    print args.filename

    # Initiate proof synopsis and load required modules
    syn = ProofSynopsis()

    # annot = list(ProofSynopsis.get_annotations(program))
    annot = list(syn.get_annotations(program))

    # Get more flags from @flags annotation in input file
    # (note: there is currently no way to override @flags)
    for (key, val) in annot:
        if key == "flags":
            args = a.parse_args(a.convert_arg_line_to_args(val), args)

    ms = module_system_default()
    uses = [
        t.strip() for (key, val) in annot for t in val.split() if key == "uses"
    ]
    if uses == []: uses = ['dtca_ea']  # backward compat
    for t in ['base', 'dtca']:
        if t not in uses: uses.append(t)

    syn.libs += [
        open(fn).read().decode('utf-8') for module_name in uses
        for fn in ms.find_module_source(module_name)
    ]
    syn.libs += [EXTRA_PROP_MACROS]

    # Construct vocabulary
    t = syn.type_declarations

    vocab = TwoVocabulary()
    vocab.type_declarations = t

    axioms = list(vocab << syn.first_pass(program))

    print "* PREDICATES"
    print vocab.preds

    print "* CONSTANTS"
    print vocab.consts

    # Extract loop from program (use cond := ... and loopBody := ... definitions as fallback)
    loop = MainLoop.locate_loop(syn.first_pass)

    if loop.prologue:
        raise NotImplementedError("loop prologue is currently not supported")
    loop.redefine(syn)

    trans = transition_system(syn, loop, vocab, t)
    cond = loop.cond
    cond0 = vocab.to_past(cond)

    ##############################################################
    use_extra_properties = not args.u

    if use_extra_properties:
        extra = generate_n_properties(syn, vocab.consts, vocab)

        extra.props += list(
            generate_unary_pred_properties(t, vocab.preds, vocab.consts))
        extra.props += list(
            generate_binary_pred_properties(t, vocab.preds, vocab.consts))

        extra.axioms += axioms

        # Stability (absence of dangling pointers)
        if FolSorts.FunctionType.parse(u'V→') in t.sorts.ary('alloc', 1):
            extra += generate_stability_properties(syn, vocab)

        # Properties for order and sorting
        if not args.no_preds_sorted:
            if FolSorts.FunctionType.parse(u'V×V→') in t.sorts.ary('R', 2):
                if args.domain is None or "S" in args.domain:
                    extra += generate_order_and_sorting_properties(syn, vocab)

        # Properties for reversal and doubly-linked lists
        if not args.no_preds_dll:
            if FolSorts.FunctionType.parse(u'V×V→') in t.sorts.ary('p*', 2):
                if args.domain is None or "R" in args.domain:
                    extra += generate_rev_properties(
                        syn,
                        vocab,
                        binary_too=(args.domain is None
                                    or "-" not in args.domain))
    else:
        extra = AbstractionProperties()
        extra.axioms += axioms

    # --- Now send everything to PDR + Z3

    z3g = Z3Gate()
    z3g.z3_decls = decls = z3g.define_symbols(t)
    z3g.expansion = syn.first_pass

    preds = [decls[x] for x in vocab.preds]

    extra_props = [z3g.formula(FolFormula.promote(p)) for p in extra.props]
    extra_axioms = [fol_formula_to_z3(phi, decls) for phi in extra.axioms]
    extra_axioms0 = [fol_formula_to_z3(phi, decls) for phi in extra.axioms0]

    init = z3g.formula("init")
    rho = And(z3g.formula(cond0), z3g.formula(trans),
              *(extra_axioms + extra_axioms0))

    bad = z3g.formula("bad")
    background = And(generate_gamma(syn, vocab.preds_flat, decls),
                     *extra_axioms)

    globals = [decls[x] for x in vocab.globals
               if t.sorts.ary(x, 0)]  # @ReservedAssignment
    locals = [(decls[x0], decls[x])
              for x0, x in vocab.locals]  # @ReservedAssignment

    if args.u:
        print "*** Using universal invariant inference"
        from mini_pdr import PDR
        if args.p:
            print "*** Use partial models (experimental)"
        if args.gen_enums:
            print "*** Generalize enum values"
        args.o = False  # universals currently not supported in opt version
    if args.o:
        from mini_pdr_opt import PDR  # @Reimport
    elif args.a:
        print "*** Using alpha from below"
        from mini_pdr_opt import PDR  # @Reimport
    else:
        from mini_pdr import PDR  # @Reimport

    print "* GLOBALS"
    print globals

    print "* LOCALS"
    print locals
    """
   if args.u:
        pdr = PDR(init, rho, bad, background, globals, locals, preds,
                  universal=args.u, partial=args.p, gen_enums=args.gen_enums,
                  experiment=args.e)
    else:
        pdr = PDR(init, rho, bad, background, globals, locals, [n])
    """
    return init, rho, bad, background, globals, locals, [n], args.n
Exemplo n.º 2
0
                        vocab,
                        binary_too=(args.domain is None
                                    or "-" not in args.domain))
    else:
        extra = AbstractionProperties()
        extra.axioms += axioms

    # --- Now send everything to PDR + Z3

    z3g = Z3Gate()
    z3g.z3_decls = decls = z3g.define_symbols(t)
    z3g.expansion = syn.first_pass

    preds = [decls[x] for x in vocab.preds]

    extra_props = [z3g.formula(FolFormula.promote(p)) for p in extra.props]
    extra_axioms = [fol_formula_to_z3(phi, decls) for phi in extra.axioms]
    extra_axioms0 = [fol_formula_to_z3(phi, decls) for phi in extra.axioms0]

    init = z3g.formula("init")
    rho = And(z3g.formula(cond0), z3g.formula(trans),
              *(extra_axioms + extra_axioms0))

    bad = z3g.formula("bad")
    background = And(generate_gamma(syn, vocab.preds_flat, decls),
                     *extra_axioms)

    globals = [decls[x] for x in vocab.globals
               if t.sorts.ary(x, 0)]  #   @ReservedAssignment
    locals = [(decls[x0], decls[x])
              for x0, x in vocab.locals]  # @ReservedAssignment