def repair(self, old_stmt, new_stmt, uks): """ Transforms from solutions for unknowns to fixes """ # /var/tmp/CETI2_xkj_Bm/MedianBad1.c.s2.t3_z1_c0.ceti.c: m = y; ===> m = uk_0 + uk_1 * x; ===> uk_0 0, uk_1 1 assert isinstance(old_stmt, str) and old_stmt, old_stmt assert isinstance(new_stmt, str) and new_stmt, new_stmt assert isinstance(uks, str) and uks, uks uksd = dict(ukv.split() for ukv in uks.split(',')) label = "repair_stmt{}:".format(self.sid) contents = [] for l in self.label_src.read_text().splitlines(): l = l.strip() if contents and label in contents[-1]: assert old_stmt in l l = new_stmt for uk in uksd: l = l.replace(uk, uksd[uk]) l = l + "// was: {}".format(old_stmt) contents.append(l) contents = '\n'.join(contents) repair_src = pathlib.Path("{}_repair_{}_{}_{}_{}.c".format( self.label_src, self.wid, self.cid, self.myid, "_".join(map(str, self.idxs)))) repair_src.write_text(contents) CM.vcmd("astyle -Y {}".format(repair_src)) return repair_src
def kl_compile(cls, src): assert src.is_file(), src # compile file with llvm include = settings.KLEE_SRC / "include" assert include.is_dir(), include opts = "-emit-llvm -c" obj = src.with_suffix('.o') cmd = "clang -I {} {} {} -o {}".format(include, opts, src, obj) mlog.debug("$ {}".format(cmd)) out_msg, err_msg = CM.vcmd(cmd) assert not out_msg, out_msg assert "clang" not in err_msg and \ "error" not in err_msg, (cmd, err_msg) if err_msg: mlog.debug(err_msg) assert obj.is_file(), obj return obj
def spy(cls, src, ast_file, sids): assert src.is_file(), src assert ast_file.is_file(), ast_file assert sids, sids ssids = '"{}"'.format(" ".join(map(str, sids))) label_src = src.with_suffix('.label.c') cmd = "./spy.exe {} {} {} {} {}".format( ast_file, label_src, ssids, settings.tplLevel, settings.maxV) mlog.debug(cmd) out_msg, err_msg = CM.vcmd(cmd) assert label_src.is_file(), label_src # mlog.debug(out_msg) # mlog.debug(err_msg) # compute tasks clist = out_msg.splitlines()[-1] clist = [c.strip() for c in clist.split(";")] clist = [c[1:][:-1] for c in clist] # remove ( ) clist = [map(int, c.split(',')) for c in clist] tasks = [] for sid, cid, n in clist: rs = cls.get_data(cid, n) rs = [(sid, cid) + r for r in rs] tasks.extend(rs) from random import shuffle shuffle(tasks) mlog.debug("tasks {}".format(len(tasks))) return label_src, tasks
def run_dk(pname,execute_cmd = False): """ Takes in the name of a program and (1) run daikon on its trace directory (pname/). The result will be output as 'pname.dk.bin' This assumes the file pname.dk.sh created by 'create_dk_run_script' function has been executed. (2) read the Daikon result binary file to text format This assumes the Daikon result file 'pname.dk.bin' has been generated from (1) EXAMPLES: 1) java -Xmx1024m daikon.Daikon cohendiv/*.dtrace -o cohendiv.dk_ --no_text_output --no_show_progress --noversion 2) EXAMPLES: java daikon.PrintInvariants --output_num_samples cohen.dk_ """ dk_cmd = "java -Xmx2048m "\ "daikon.Daikon {fr} -o {pn}/{pn}.dk_ "\ "--no_text_output --no_show_progress --noversion "\ "> /dev/null" dk_cmd = dk_cmd.format(pn=pname,fr=os.path.join(pname,'*.dtrace')) if execute_cmd: CM.vcmd(dk_cmd,verbose=1) else: print dk_cmd dk_read = "java daikon.PrintInvariants "\ "--output_num_samples "\ "{pn}/{pn}.dk_ > {pn}/{pn}.dk".format(pn=pname) if execute_cmd: assert os.path.isfile("{pn}/{pn}.dk_".format(pn=pname)) CM.vcmd(dk_read,verbose=1) else: print dk_read
def ccompile(src): assert src.is_file(), src exe_file = src.with_suffix('.exe') cmd = ("clang {} -o {}".format(src, exe_file)) out_msg, err_msg = CM.vcmd(cmd) assert "Error: " not in err_msg, err_msg assert not out_msg, out_msg assert exe_file.is_file() return exe_file
def run_dk(pname, execute_cmd=False): """ Takes in the name of a program and (1) run daikon on its trace directory (pname/). The result will be output as 'pname.dk.bin' This assumes the file pname.dk.sh created by 'create_dk_run_script' function has been executed. (2) read the Daikon result binary file to text format This assumes the Daikon result file 'pname.dk.bin' has been generated from (1) EXAMPLES: 1) java -Xmx1024m daikon.Daikon cohendiv/*.dtrace -o cohendiv.dk_ --no_text_output --no_show_progress --noversion 2) EXAMPLES: java daikon.PrintInvariants --output_num_samples cohen.dk_ """ dk_cmd = "java -Xmx2048m "\ "daikon.Daikon {fr} -o {pn}/{pn}.dk_ "\ "--no_text_output --no_show_progress --noversion "\ "> /dev/null" dk_cmd = dk_cmd.format(pn=pname, fr=os.path.join(pname, '*.dtrace')) if execute_cmd: CM.vcmd(dk_cmd, verbose=1) else: print dk_cmd dk_read = "java daikon.PrintInvariants "\ "--output_num_samples "\ "{pn}/{pn}.dk_ > {pn}/{pn}.dk".format(pn=pname) if execute_cmd: assert os.path.isfile("{pn}/{pn}.dk_".format(pn=pname)) CM.vcmd(dk_read, verbose=1) else: print dk_read
def run(inps): if path.is_file(): path.unlink() # remove inps_str = [" ".join(map(str, inp)) for inp in inps] cmds = ["{} {}".format(exe, inp_str) for inp_str in inps_str] for cmd in cmds: out_msg, err_msg = CM.vcmd(cmd) assert not err_msg assert path.is_file(), path sids = [int(sid) for sid in path.read_text().splitlines() if sid] return sids
def kl_exec(cls, obj, timeout, outdir, opts=[]): assert obj.is_file(), obj assert timeout >= 1, timeout assert isinstance(outdir, pathlib.Path), outdir kl_opts = settings.KLEE_OPTS.format(timeout, timeout, outdir) if opts: kl_opts += ' '.join(map(str, opts)) cmd = "{} {} {}".format(settings.KLEE_EXE, kl_opts, obj).strip() mlog.debug("$ {}".format(cmd)) proc = CM.vcmd(cmd, stderr=sp.STDOUT, do_communicate=False) return proc
def instr(self, src, ast_file): assert src.is_file(), src assert ast_file.is_file(), src fl_src = src.with_suffix('.fl.c') mlog.info("get good/bad inps from {}".format(fl_src)) cmd = "./instr.exe {} {} {}".format(fl_src, ast_file, settings.maxV) mlog.debug(cmd) out_msg, err_msg = CM.vcmd(cmd) assert not err_msg, err_msg mlog.debug(out_msg) assert fl_src.is_file() return fl_src
def cov(cls, src, ast_file): """ Create file with cov info (i.e., printf stmts) """ assert src.is_file(), src assert ast_file.is_file(), src cov_src = src.with_suffix('.cov.c') mlog.info("fault localization: {}".format(cov_src)) cmd = "./coverage.exe {} {}".format(cov_src, ast_file) mlog.debug(cmd) out_msg, err_msg = CM.vcmd(cmd) assert not err_msg, err_msg mlog.debug(out_msg) assert cov_src.is_file(), cov_src return cov_src
def start(self): # compile exe = self.cov_src.with_suffix('.exe') cmd = "clang {} -o {}".format(self.cov_src, exe) mlog.debug(cmd) _, err_msg = CM.vcmd(cmd) assert "error" not in err_msg, err_msg assert exe.is_file(), exe good_sids, bad_sids = self.collect_cov(exe) good_nruns, good_freqs = self.analyze_covs(good_sids) bad_nruns, bad_freqs = self.analyze_covs(bad_sids) assert bad_nruns == len(self.bad_inps) sscores = self.get_scores(good_nruns, good_freqs, bad_nruns, bad_freqs) return sscores
def transform(cls, src, inps_file, wid, sid, cid, myid, idxs): assert src.is_file(), src assert inps_file.is_file(), src assert wid >= 0, wid assert cid >= 0, cid assert myid >= 0, myid # call ocaml prog to transform file xinfo = "t{}_z{}_c{}".format(cid, len(idxs), myid) mlog.debug('worker {}: transform {} sid {} tpl {} xinfo {} idxs {} ***' .format(wid, src, sid, cid, xinfo, idxs)) cmd = ('./modify.exe {} {} {} {} "{}" {} {}' .format(src, inps_file, sid, cid, " ".join(map(str, idxs)), xinfo, settings.maxV)) mlog.debug("$ {}".format(cmd)) rs, rs_err = CM.vcmd(cmd) # assert not rs, rs mlog.debug(rs_err[:-1] if rs_err.endswith("\n") else rs_err) if "error" in rs_err or "success" not in rs_err: mlog.error("worker {}: transform failed '{}' !".format(wid, cmd)) mlog.error(rs_err) raise AssertionError # obtained the created result # Alert: Transform success: ## '/tmp/cece_4b2065/q.bug2.c.s1.t5_z3_c1.ceti.c' ## __cil_tmp4 = (x || y) || z; ## __cil_tmp4 = (x || y) && z; rs_file, old_stmt, new_stmt = "", "", "" for line in rs_err.splitlines(): if "success:" in line: line = line.split("##") assert len(line) == 4, len(line) # must has 4 parts line = [l.strip() for l in line[1:]] rs_file = line[0][1:][:-1] # remove ' ' from filename old_stmt, new_stmt = line[1], line[2] break rs_file = pathlib.Path(rs_file) assert rs_file.is_file(), rs_file return rs_file, old_stmt, new_stmt
def preproc(cls, src): assert src.is_file(), src mlog.info("preprocess and get AST from '{}'".format(src)) preproc_src = src.with_suffix('.preproc.c') # /a.preproc.c ast_file = src.with_suffix('.ast') # /a.ast cmd = "./preproc.exe {} {} {} {} {}" cmd = cmd.format(src, settings.mainQ, settings.correctQ, preproc_src, ast_file) mlog.debug(cmd) out_msg, err_msg = CM.vcmd(cmd) assert not err_msg, err_msg mlog.debug(out_msg) assert preproc_src.is_file(), preproc_src assert ast_file.is_file(), ast_file return preproc_src, ast_file
def check(inp): cmd = "{} {}".format(self.exe_file, ' '.join(map(str, inp))) out_msg, err_msg = CM.vcmd(cmd) assert not err_msg return "PASS" in out_msg