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
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
parser.add_option("--p_cpus", action="store", dest="p_cpus", default=None, type="int", help="the number of cores to use for parallel running") parser.add_option("--ntimes", action="store", dest="ntimes", default=None, type="int", help="number of rounds on a single sketch-backend invocation") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="print intermediate messages verbosely") parser.add_option("-m", "--model", action="store_true", dest="model", default=False, help="use models of Java libraries") (opt, argv) = parser.parse_args() if len(argv) < 1: parser.error("incorrect number of arguments") configure(opt) pgr_files = [] if opt.model: model_dir = os.path.join(root_dir, "model") pgr_files.extend(util.get_files_from_path(model_dir, "java")) for arg in argv: pgr_files.extend(util.get_files_from_path(arg, "java")) sys.exit(main(pgr_files, opt.output))
(opt, argv) = parser.parse_args() if len(argv) < 1: parser.error("incorrect number of arguments") pwd = os.path.dirname(__file__) root_dir = os.path.join(pwd, "..") sys.path.append(root_dir) ## logging configuration logging.config.fileConfig(os.path.join(pwd, "logging.conf")) logging.getLogger().setLevel(logging.DEBUG) smpl_files = [] for arg in argv: smpl_files.extend(util.get_files_from_path(arg, "txt")) reset() smpls = [] for fname in smpl_files: smpl = Sample(fname, lambda mname: mname.endswith("Event")) smpls.append(smpl) if opt.method: _decls = decls(smpls) for cname in _decls.keys(): mnames = ", ".join(list(_decls[cname])) print "{}: {}".format(cname, mnames) if opt.event: _evts = util.flatten(map(op.attrgetter("evts"), smpls))