コード例 #1
0
ファイル: jskparser.py プロジェクト: zjzh/java-sketch
def parse(path, **kwargs):
    # print 'field_types:', utils.get_fld_descriptors('jskparser/J')
    # ftypes = utils.get_mtd_types('jskparser/J', 'm2', 2)
    # print 'ftypes:', ftypes
    # exit()
    ## logging configuration
    create_logger(log_lvls.get(kwargs.get('log_lvl', '10')))

    lib = kwargs.get('lib', True)
    with open(util.toAST(path, 'java', lib), 'r') as fd:
        d = json.load(fd)
        d.update({u'GSYMTAB': 'RESET'})
        logging.info('parsing file...')
        program = CompilationUnit(d)

        s = SymtabGen(lib=lib)
        logging.info('generating symbol table...')
        s.visit(program)
        logging.info('building class heirarchy...')
        utils.build_subs(program)

    return program
コード例 #2
0
ファイル: jskparser.py プロジェクト: plum-umd/java-sketch
def parse(path, **kwargs):
    # print 'field_types:', utils.get_fld_descriptors('jskparser/J')
    # ftypes = utils.get_mtd_types('jskparser/J', 'm2', 2)
    # print 'ftypes:', ftypes
    # exit()
    ## logging configuration
    create_logger(log_lvls.get(kwargs.get('log_lvl', '10')))

    lib = kwargs.get('lib', True)
    with open(util.toAST(path, 'java', lib), 'r') as fd:
        d = json.load(fd)
        d.update({u'GSYMTAB':'RESET'})
        logging.info('parsing file...')
        program = CompilationUnit(d)
        
        s = SymtabGen(lib=lib)
        logging.info('generating symbol table...')
        s.visit(program)
        logging.info('building class heirarchy...')
        utils.build_subs(program)

    return program
コード例 #3
0
def main(cmd, smpl_paths, tmpl_paths, patterns, out_dir, log_lv=logging.DEBUG):
    ## logging configuration
    logging.config.fileConfig(os.path.join(pwd, "logging.conf"))
    logging.getLogger().setLevel(log_lv)

    ## check custom codegen was built
    codegen_jar = os.path.join("codegen", "lib", "codegen.jar")
    if not os.path.isfile(codegen_jar):
        raise Exception("can't find " + codegen_jar)

    if cmd == "android":
        tmpl_paths.append(adr_tmpl)
    elif cmd == "gui":
        tmpl_paths.append(gui_tmpl)

    if cmd == "pattern":
        _patterns = patterns[:]
    else:  ## android or gui
        _patterns = [C.P.ACCA, C.P.ACCU, C.P.ACCM, C.P.ADP, \
            C.P.BLD, C.P.FAC, C.P.SNG, C.P.PRX, C.P.OBS, C.P.STA]

    opts = []  ## sketch options
    if conf["verbose"]:
        opts.extend(["-V", "10"])
    if conf["timeout"]:
        opts.extend(["--fe-timeout", str(conf["timeout"])])
        opts.extend(["--slv-timeout", str(conf["timeout"])])
    # place to keep sketch's temporary files
    opts.extend(["--fe-tempdir", out_dir])
    opts.append("--fe-keep-tmp")

    tmpls = []
    output_paths = []
    for p in patterns:  ## for each pattern or demo
        logging.info("demo: " + p)
        _smpl_paths = smpl_paths[:]
        _tmpl_paths = tmpl_paths[:]

        _smpl_paths.append(os.path.join(smpl_dir, cmd, p))
        if cmd == "pattern":
            client_path = os.path.join(tmpl_dir, cmd, p)
        else:  ## android or gui
            client_path = os.path.join(tmpl_dir, app, cmd, p)
        _tmpl_paths.append(client_path)

        ## (smpl|tmpl)_path is either a single file or a folder containing files

        ## read and parse templates
        tmpl_files = []
        for tmpl_path in _tmpl_paths:
            tmpl_files.extend(util.get_files_from_path(tmpl_path, "java"))

        ast = util.toAST(tmpl_files)

        ## convert AST to meta data
        tmpl = Template(ast)

        ## mark client-side classes
        client_files = util.get_files_from_path(client_path, "java")
        for client in client_files:
            base = os.path.basename(client)
            cname = os.path.splitext(base)[0]
            cls = class_lookup(cname)
            cls.client = True

        ## read and parse samples
        smpl_files = []
        for smpl_path in _smpl_paths:
            smpl_files.extend(util.get_files_from_path(smpl_path, "txt"))

        sample.reset()
        smpls = []
        for fname in smpl_files:
            smpl = Sample(fname, tmpl.is_event)
            smpls.append(smpl)

        ## make harness
        harness.mk_harnesses(cmd, tmpl, smpls)

        ## pattern rewriting
        rewrite.visit(cmd, smpls, tmpl, _patterns)
        java_sk_dir = os.path.join(out_dir, '_'.join(["java_sk", p]))
        decode.dump(cmd, java_sk_dir, tmpl)

        ## clean up templates
        #reducer.reduce_anno(smpls, tmpl)
        #reducer.remove_cls(smpls, tmpl)

        tmpl.freeze()
        tmpls.append(tmpl)

        ## encode (rewritten) templates into sketch files
        sk_dir = os.path.join(out_dir, '_'.join(["sk", p]))
        if conf["encoding"]:
            encoder.to_sk(cmd, smpls, tmpl, sk_dir)
        else:  # not encoding
            logging.info("pass the encoding phase; rather use previous files")

        ## run sketch
        output_path = os.path.join(out_dir, "output", "{}.txt".format(p))
        output_paths.append(output_path)
        if conf["sketch"]:
            if os.path.exists(output_path): os.remove(output_path)

            # custom codegen
            _opts = opts[:]
            _opts.extend(["--fe-custom-codegen", codegen_jar])

            if conf["randassign"] or conf["parallel"]:
                _opts.append("--slv-randassign")
                _opts.extend(["--bnd-dag-size",
                              "16000000"])  # 16M ~> 8G memory

            sketch.set_default_option(_opts)

            if conf["parallel"]:
                ## Python implementation as a CEGIS (sketch-backend) wrapper
                #_, r = sketch.be_p_run(sk_dir, output_path)
                # Java implementation inside sketch-frontend
                _opts.append("--slv-parallel")
                if conf["p_cpus"]:
                    _opts.extend(["--slv-p-cpus", str(conf["p_cpus"])])
                if conf["ntimes"]:
                    _opts.extend(["--slv-ntimes", str(conf["ntimes"])])
                if conf["randdegree"]:  # assume FIXED strategy
                    _opts.extend(["--slv-randdegree", str(conf["randdegree"])])
                else:  # adaptive concretization
                    _opts.extend(["--slv-strategy", "WILCOXON"])
                _, r = sketch.run(sk_dir, output_path)
            else:
                _, r = sketch.run(sk_dir, output_path)
            # if sketch fails, halt the process here
            if not r: sys.exit(1)

            ## run sketch again to obtain control-flows
            sketch.set_default_option(opts)
            r = sketch.ctrl_flow_run(sk_dir, output_path, out_dir)
            if not r: sys.exit(1)

        else:  # not running sketch
            logging.info("pass sketch; rather read: {}".format(output_path))

        ## end of loop (per pattern/demo)

    ## generate compilable model
    java_dir = os.path.join(out_dir, "java")
    decode.to_java(cmd, java_dir, tmpls, output_paths, _patterns)
    logging.info("synthesis done")

    return 0
コード例 #4
0
ファイル: __init__.py プロジェクト: plum-umd/pasket
def main(cmd, smpl_paths, tmpl_paths, patterns, out_dir, log_lv=logging.DEBUG):
    ## logging configuration
    logging.config.fileConfig(os.path.join(pwd, "logging.conf"))
    logging.getLogger().setLevel(log_lv)

    ## check custom codegen was built
    codegen_jar = os.path.join("codegen", "lib", "codegen.jar")
    if not os.path.isfile(codegen_jar):
        raise Exception("can't find " + codegen_jar)

    if cmd == "android":
        tmpl_paths.append(adr_tmpl)
    elif cmd == "gui":
        tmpl_paths.append(gui_tmpl)

    if cmd == "pattern":
        _patterns = patterns[:]
    else:  ## android or gui
        _patterns = [C.P.ACCA, C.P.ACCU, C.P.ACCM, C.P.ADP, C.P.BLD, C.P.FAC, C.P.SNG, C.P.PRX, C.P.OBS, C.P.STA]

    opts = []  ## sketch options
    if conf["verbose"]:
        opts.extend(["-V", "10"])
    if conf["timeout"]:
        opts.extend(["--fe-timeout", str(conf["timeout"])])
        opts.extend(["--slv-timeout", str(conf["timeout"])])
    # place to keep sketch's temporary files
    opts.extend(["--fe-tempdir", out_dir])
    opts.append("--fe-keep-tmp")

    tmpls = []
    output_paths = []
    for p in patterns:  ## for each pattern or demo
        logging.info("demo: " + p)
        _smpl_paths = smpl_paths[:]
        _tmpl_paths = tmpl_paths[:]

        _smpl_paths.append(os.path.join(smpl_dir, cmd, p))
        if cmd == "pattern":
            client_path = os.path.join(tmpl_dir, cmd, p)
        else:  ## android or gui
            client_path = os.path.join(tmpl_dir, app, cmd, p)
        _tmpl_paths.append(client_path)

        ## (smpl|tmpl)_path is either a single file or a folder containing files

        ## read and parse templates
        tmpl_files = []
        for tmpl_path in _tmpl_paths:
            tmpl_files.extend(util.get_files_from_path(tmpl_path, "java"))

        ast = util.toAST(tmpl_files)

        ## convert AST to meta data
        tmpl = Template(ast)

        ## mark client-side classes
        client_files = util.get_files_from_path(client_path, "java")
        for client in client_files:
            base = os.path.basename(client)
            cname = os.path.splitext(base)[0]
            cls = class_lookup(cname)
            cls.client = True

        ## read and parse samples
        smpl_files = []
        for smpl_path in _smpl_paths:
            smpl_files.extend(util.get_files_from_path(smpl_path, "txt"))

        sample.reset()
        smpls = []
        for fname in smpl_files:
            smpl = Sample(fname, tmpl.is_event)
            smpls.append(smpl)

        ## make harness
        harness.mk_harnesses(cmd, tmpl, smpls)

        ## pattern rewriting
        rewrite.visit(cmd, smpls, tmpl, _patterns)
        java_sk_dir = os.path.join(out_dir, "_".join(["java_sk", p]))
        decode.dump(cmd, java_sk_dir, tmpl)

        ## clean up templates
        # reducer.reduce_anno(smpls, tmpl)
        # reducer.remove_cls(smpls, tmpl)

        tmpl.freeze()
        tmpls.append(tmpl)

        ## encode (rewritten) templates into sketch files
        sk_dir = os.path.join(out_dir, "_".join(["sk", p]))
        if conf["encoding"]:
            encoder.to_sk(cmd, smpls, tmpl, sk_dir)
        else:  # not encoding
            logging.info("pass the encoding phase; rather use previous files")

        ## run sketch
        output_path = os.path.join(out_dir, "output", "{}.txt".format(p))
        output_paths.append(output_path)
        if conf["sketch"]:
            if os.path.exists(output_path):
                os.remove(output_path)

            # custom codegen
            _opts = opts[:]
            _opts.extend(["--fe-custom-codegen", codegen_jar])

            if conf["randassign"] or conf["parallel"]:
                _opts.append("--slv-randassign")
                _opts.extend(["--bnd-dag-size", "16000000"])  # 16M ~> 8G memory

            sketch.set_default_option(_opts)

            if conf["parallel"]:
                ## Python implementation as a CEGIS (sketch-backend) wrapper
                # _, r = sketch.be_p_run(sk_dir, output_path)
                # Java implementation inside sketch-frontend
                _opts.append("--slv-parallel")
                if conf["p_cpus"]:
                    _opts.extend(["--slv-p-cpus", str(conf["p_cpus"])])
                if conf["ntimes"]:
                    _opts.extend(["--slv-ntimes", str(conf["ntimes"])])
                if conf["randdegree"]:  # assume FIXED strategy
                    _opts.extend(["--slv-randdegree", str(conf["randdegree"])])
                else:  # adaptive concretization
                    _opts.extend(["--slv-strategy", "WILCOXON"])
                _, r = sketch.run(sk_dir, output_path)
            else:
                _, r = sketch.run(sk_dir, output_path)
            # if sketch fails, halt the process here
            if not r:
                sys.exit(1)

            ## run sketch again to obtain control-flows
            sketch.set_default_option(opts)
            r = sketch.ctrl_flow_run(sk_dir, output_path, out_dir)
            if not r:
                sys.exit(1)

        else:  # not running sketch
            logging.info("pass sketch; rather read: {}".format(output_path))

        ## end of loop (per pattern/demo)

    ## generate compilable model
    java_dir = os.path.join(out_dir, "java")
    decode.to_java(cmd, java_dir, tmpls, output_paths, _patterns)
    logging.info("synthesis done")

    return 0
コード例 #5
0
ファイル: main.py プロジェクト: chubbymaggie/java-sketch
def main(jsk_paths, out_dir=res_dir, log_lv=logging.DEBUG):
  ## check custom codegen was built
  codegen_jar = os.path.join(root_dir, "codegen", "lib", "codegen.jar")
  if not os.path.isfile(codegen_jar):
    raise Exception("can't find " + codegen_jar)

  ## logging configuration
  logging.config.fileConfig(os.path.join(pwd, "logging.conf"))
  logging.getLogger().setLevel(log_lv)

  ast = util.toAST(jsk_paths)
  pgr = Program(ast)
  main_cls = pgr.main_cls
  demo_name = str(main_cls.name)

  ## rewrite field/method holes
  rewrite.visit(pgr)

  ## encode the program into sketch files
  sk_dir = os.path.join(out_dir, '_'.join(["sk", demo_name]))
  if conf["encoding"]:
    encoder.to_sk(pgr, sk_dir)
  else: # not encoding
    logging.info("pass the encoding phase; rather use previous files")

  opts = [] ## sketch options
  if conf["verbose"]:
    opts.extend(["-V", "10"])
  if conf["timeout"]:
    opts.extend(["--fe-timeout", str(conf["timeout"])])
    opts.extend(["--slv-timeout", str(conf["timeout"])])
  # place to keep sketch's temporary files
  opts.extend(["--fe-tempdir", out_dir])
  opts.append("--fe-keep-tmp")
  # custom codegen
  opts.extend(["--fe-custom-codegen", codegen_jar])

  ## run Sketch
  output_path = os.path.join(out_dir, "output", "{}.txt".format(demo_name))
  if conf["sketch"]:
    if os.path.exists(output_path): os.remove(output_path)

    if conf["randassign"] or conf["parallel"]:
      opts.append("--slv-randassign")
      opts.extend(["--bnd-dag-size", "16000000"]) # 16M ~> 8G memory

    sketch.set_default_option(opts)

    if conf["parallel"]:
      ## Python implementation as a CEGIS (sketch-backend) wrapper
      #_, r = sketch.be_p_run(sk_dir, output_path)
      # Java implementation inside sketch-frontend
      opts.append("--slv-parallel")
      if conf["p_cpus"]:
        opts.extend(["--slv-p-cpus", str(conf["p_cpus"])])
      if conf["ntimes"]:
        opts.extend(["--slv-ntimes", str(conf["ntimes"])])
      if conf["randdegree"]: # assume FIXED strategy
        opts.extend(["--slv-randdegree", str(conf["randdegree"])])
      else: # adaptive concretization
        opts.extend(["--slv-strategy", "WILCOXON"])
      _, r = sketch.run(sk_dir, output_path)
    else:
      _, r = sketch.run(sk_dir, output_path)
    # if sketch fails, halt the process here
    if not r: return 1

  else: # not running Sketch
    logging.info("pass sketch; rather read: {}".format(output_path))

  ## generate Java code
  java_dir = os.path.join(out_dir, "java")
  decode.to_java(java_dir, pgr, output_path)
  logging.info("synthesis done")

  return 0