def test_ppo_proofs(self, delaytest=False): """Run analysis and check if analysis results match expected results. Skip checking results if delaytest is true. """ if not os.path.isfile(self.config.canalyzer): raise AnalyzerMissingError(self.config.canalyzer) self.testresults.set_pevs() for creffile in self.get_cref_files(): cfilename = creffile.name cfilefilename = UF.get_cfile_filename(self.tgtxpath, cfilename) if not os.path.isfile(cfilefilename): raise XmlFileNotFoundError(cfilefilename) capp = CApplication( self.sempath, cfilename=cfilename, contractpath=self.contractpath ) cfile = capp.get_single_file() # only generate invariants if required if creffile.has_domains(): for d in creffile.get_domains(): am = AnalysisManager(capp, verbose=self.verbose) am.generate_and_check_file(cfilename, d) cfile.reinitialize_tables() ppos = cfile.get_ppos() if delaytest: continue for cfun in creffile.get_functions(): fname = cfun.name funppos = [ppo for ppo in ppos if ppo.cfun.name == fname] refppos = cfun.get_ppos() self.check_ppo_proofs(cfilename, cfun, funppos, refppos)
def test_spo_proofs(self, delaytest=False): """Run analysis and check if the analysis results match the expected results. Skip the checking if delaytest is True. """ self.testresults.set_sevs() for creffile in self.get_cref_files(): creffilename = creffile.name cfilefilename = UF.get_cfile_filename(self.tgtxpath, creffilename) if not os.path.isfile(cfilefilename): raise XmlFileNotFoundError(cfilefilename) capp = CApplication( self.sempath, cfilename=creffilename, contractpath=self.contractpath ) cappfile = capp.get_single_file() if creffile.has_domains(): for d in creffile.get_domains(): am = AnalysisManager(capp, verbose=self.verbose) am.generate_and_check_file(creffilename, d) cappfile.reinitialize_tables() spos = cappfile.get_spos() if delaytest: continue for cfun in creffile.get_functions(): fname = cfun.name funspos = [spo for spo in spos if spo.cfun.name == fname] refspos = cfun.get_spos() self.check_spo_proofs(creffilename, cfun, funspos, refspos)
def test_ppos(self): """Create primary proof obligations and check if created as expected.""" if not os.path.isfile(self.config.canalyzer): raise AnalyzerMissingError(self.config.canalyzer) self.testresults.set_ppos() saved = False try: for creffile in self.get_cref_files(): creffilename = creffile.name creffilefilename = UF.get_cfile_filename(self.tgtxpath, creffilename) if not os.path.isfile(creffilefilename): raise XmlFileNotFoundError(creffilefilename) capp = CApplication( self.sempath, cfilename=creffilename, contractpath=self.contractpath ) am = AnalysisManager(capp, verbose=self.verbose) am.create_file_primary_proofobligations(creffilename) cfile = capp.get_single_file() capp.collect_post_assumes() ppos = cfile.get_ppos() for creffun in creffile.get_functions(): fname = creffun.name cfun = cfile.get_function_by_name(fname) if self.saveref: if creffun.has_ppos(): print("Ppos not created for " + fname + " (delete first)") else: self.create_reference_ppos( creffilename, fname, cfun.get_ppos() ) saved = True else: refppos = creffun.get_ppos() funppos = [ppo for ppo in ppos if ppo.cfun.name == fname] if len(refppos) == len(funppos): self.testresults.add_ppo_count_success(creffilename, fname) self.check_ppos(creffilename, fname, funppos, refppos) else: self.testresults.add_ppo_count_error( creffilename, fname, len(funppos), len(refppos) ) raise FunctionPPOError(creffilename + ":" + fname) except FunctionPPOError as detail: self.print_test_results() print("Function PPO error: " + str(detail)) exit() if self.saveref and saved: self.testsetref.save() exit()
def test_spos(self, delaytest=False): """Run analysis and check if all expected spos are created.""" try: for creffile in self.get_cref_files(): self.testresults.set_spos() cfilename = creffile.name cfilefilename = UF.get_cfile_filename(self.tgtxpath, cfilename) if not os.path.isfile(cfilefilename): raise XmlFileNotFoundError(xfilefilename) capp = CApplication( self.sempath, cfilename=cfilename, contractpath=self.contractpath ) cappfile = capp.get_single_file() capp.update_spos() capp.collect_post_assumes() spos = cappfile.get_spos() if delaytest: continue for cfun in creffile.get_functions(): fname = cfun.name if self.saveref: if cfun.has_spos(): print( "Spos not created for " + fname + " in " + cfilename + " (delete first)" ) else: self.create_reference_spos(cfilename, fname, spos[fname]) else: refspos = cfun.get_spos() funspos = [spo for spo in spos if spo.cfun.name == fname] if funspos is None and len(refspos) == 0: self.testresults.add_spo_count_success(cfilename, fname) elif len(refspos) == len(funspos): self.testresults.add_spo_count_success(cfilename, fname) self.check_spos(cfilename, fname, funspos, refspos) else: self.testresults.add_spo_count_error( cfilename, fname, len(funspos), len(refspos) ) raise FunctionSPOError( cfilename + ":" + fname + " (" + str(len(funspos)) + ")" ) except FunctionSPOError as detail: self.print_test_results() print("") print("*" * 80) print("Function SPO error: " + str(detail)) print("*" * 80) exit() if self.saveref: self.testsetref.save() exit()
import chc.util.xmlutil as UX from chc.app.CApplication import CApplication def parse(): parser = argparse.ArgumentParser() parser.add_argument("cfilename", help="name of kendra c file (e.g. id115.c)") args = parser.parse_args() return args if __name__ == "__main__": args = parse() cfilename = args.cfilename cpath = UF.get_kendra_cpath(cfilename) if cpath is None: print("*" * 80) print("Unable to find the test set for file " + cfilename) print("*" * 80) exit(1) sempath = os.path.join(cpath, "semantics") cfapp = CApplication(sempath, cfilename) cfile = cfapp.get_cfile() cfile.create_contract(os.path.join(cpath, "ktacontracts"))
if __name__ == "__main__": args = parse() cfilename = args.cfilename cpath = UF.get_kendra_cpath(cfilename) if cpath is None: print("*" * 80) print("Unable to find the test set for file " + cfilename) print("*" * 80) exit(1) sempath = os.path.join(cpath, "semantics") cfapp = CApplication(sempath, cfilename) if cfapp.has_single_file(): cfile = cfapp.get_cfile() if (args.table is None) or args.list: print(str(DT.list_file_tables())) else: print(str(DT.get_file_table(cfile, args.table))) else: print("*" * 80) print("File not found. Please make sure the test has been analyzed.") print("*" * 80) exit(1)
return args if __name__ == "__main__": args = parse() try: cpath = UF.get_project_path(args.path) UF.check_analysis_results(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, "semantics") capp = CApplication(sempath) result = {} def collect_fn_callees(fn): callees = [str(i.get_callee()) for i in fn.get_call_instrs()] fnresult = result[fn.name] = {} fncallees = fnresult["callees"] = {} for c in callees: fncallees.setdefault(c, 0) fncallees[c] += 1 def collect_fi_callees(fi): fi.iter_functions(collect_fn_callees) capp.iter_files(collect_fi_callees)
if __name__ == "__main__": args = parse() try: cpath = UF.get_juliet_testpath(args.cwe, args.test) UF.check_analysis_results(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, "semantics") excludefiles = ["io.c", "main_linux.c", "std_thread.c"] capp = CApplication(sempath, excludefiles=excludefiles) def pofilter(p): return True if args.predicates: def pofilter(p): return p.get_predicate_tag() in args.predicates openppos = capp.get_open_ppos() violations = capp.get_violations() delegated = capp.get_delegated() if len(openppos) > 0: print("Open proof obligations:\n" + ("=" * 80))
parser.add_argument('tablename',help='name of table to be shown') args = parser.parse_args() return args if __name__ == '__main__': args = parse() cpath = os.path.abspath(args.path) try: UF.check_semantics(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath,'semantics') try: cfapp = CApplication(sempath,args.cfile) cfile = cfapp.get_cfile() except CFileNotFoundException as e: print(str(e)) exit(1) try: cfun = cfile.get_function_by_name(args.cfunction) except UF.CHError as e: print(str(e.wrap())) exit(1) print(str(DT.get_function_table(cfile,args.cfunction,args.tablename)))
return args if __name__ == "__main__": args = parse() try: cpath = UF.get_project_path(args.path) UF.check_analysis_results(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, "semantics") capp = CApplication(sempath) def pofilter(p): return True if args.predicates: def pofilter(p): return p.get_predicate_tag() in args.predicates openppos = capp.get_open_ppos() violations = capp.get_violations() delegated = capp.get_delegated() if len(openppos) > 0: print("Open proof obligations:\n" + ("=" * 80))
return args if __name__ == '__main__': args = parse() try: cpath = UF.get_project_path(args.path) UF.check_analysis_results(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, 'semantics') capp = CApplication(sempath) resultheaders = {} result = {} def get_fn_missing_summaries(fn): missingsummaries = fn.api.get_missing_summaries() for s in missingsummaries: names = s.split('/') if len(names) == 2: resultheaders.setdefault(names[0], {}) resultheaders[names[0]].setdefault(names[1], 0) resultheaders[names[0]][names[1]] += 1 else: result.setdefault(s, 0) result[s] += 1
ifilename = parsemanager.preprocess_file_with_gcc(basename) result = parsemanager.parse_ifile(ifilename) if result != 0: print('*' * 80) print('Error in parsing ' + cfilename) print('*' * 80) exit(1) except OSError as e: print('OSError while parsing file: ' + str(e)) exit(1) # ------------------------------------------------------------- analysis --- sempath = os.path.join(cpath, 'semantics') capp = CApplication(sempath, cfilename) am = AnalysisManager(capp, verbose=args.verbose) am.create_file_primary_proofobligations(cfilename) am.reset_tables(cfilename) capp.collect_post_assumes() am.generate_and_check_file(cfilename, 'llrvisp') am.reset_tables(cfilename) capp.collect_post_assumes() for k in range(0, 5): capp.update_spos() am.generate_and_check_file(cfilename, 'llrvisp') am.reset_tables(cfilename)
try: cpath = UF.get_project_path(args.path) UF.check_semantics(cpath, deletesemantics=False) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, 'semantics') if args.contractpath is None: contractpath = os.path.join(cpath, 'chc_contracts') else: contractpath = args.contractpath capp = CApplication(sempath, contractpath=contractpath) lines = [] result = {} result['pre'] = 0 result['post'] = 0 def f(fi): if fi.has_file_contracts(): if (not args.xpost) and fi.contracts.has_postconditions(): lines.append(str(fi.contracts.report_postconditions())) result['post'] += fi.contracts.count_postconditions() if (not args.xpre) and fi.contracts.has_preconditions(): lines.append(str(fi.contracts.report_preconditions())) result['pre'] += fi.contracts.count_preconditions()
return args if __name__ == "__main__": args = parse() try: cpath = UF.get_project_path(args.path) UF.check_semantics(cpath, deletesemantics=False) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, "semantics") capp = CApplication(sempath) result: Dict[Any, Any] = {} asminstrs: List[Any] = [] def f(fi): if fi.name not in result: result[fi.name] = {} def g(fn): if fn.name not in result[fi.name]: result[fi.name][fn.name] = {} result[fi.name][fn.name]["stmts"] = 0 result[fi.name][fn.name]["calls"] = 0 result[fi.name][fn.name]["assigns"] = 0 result[fi.name][fn.name]["asms"] = 0
"--contractpath", help="path to save the contracts file", default=None ) parser.add_argument("--outputpath", help="path to save system displays") args = parser.parse_args() return args if __name__ == "__main__": args = parse() try: UF.check_gui() cpath = UF.get_juliet_testpath(args.cwe, args.test) UF.check_analysis_results(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, "semantics") if args.contractpath is None: contractpath = os.path.join(cpath, "chc_contracts") else: contractpath = args.contractpath capp = CApplication(sempath, contractpath=contractpath) am = AnalysisManager(capp) am.rungui(cpath, outputpath=args.outputpath)
args = parse() try: cpath = UF.get_juliet_testpath(args.cwe, args.test) UF.check_semantics(cpath, deletesemantics=True) except UF.CHError as e: print(str(e.wrap())) exit(1) logfilename = os.path.join(cpath, "chc_logfile.txt") logging.basicConfig(filename=logfilename, level=logging.WARNING) sempath = os.path.join(cpath, "semantics") excludefiles = ["io.c", "main_linux.c", "std_thread.c"] capp = CApplication(sempath, excludefiles=excludefiles) linker = CLinker(capp) linker.link_compinfos() linker.link_varinfos() capp.iter_files(save_xrefs) linker.save_global_compinfos() if args.contractpath is None: contractpath = os.path.join(cpath, "chc_contracts") else: contractpath = args.contractpath # have to reinitialize capp to get linking info properly initialized capp = CApplication(sempath, contractpath=contractpath,
exit(1) logfilename = os.path.join(cpath, 'chclogfile.txt') logging.basicConfig(filename=logfilename, level=loglevel) if args.contractpath is None: contractpath = os.path.join(cpath, 'chc_contracts') else: contractpath = args.contractpath # check linkinfo globaldefs = os.path.join( sempath, os.path.join('ktadvance', 'globaldefinitions.xml')) if not os.path.isfile(globaldefs): capp = CApplication(sempath, contractpath=contractpath) linker = CLinker(capp) linker.link_compinfos() linker.link_varinfos() capp.iter_files(save_xrefs) linker.save_global_compinfos() # have to reinitialize capp to get linking info properly initialized capp = CApplication(sempath, contractpath=contractpath, candidate_contractpath=args.candidate_contractpath) am = AnalysisManager(capp, verbose=args.verbose, wordsize=args.wordsize) with timing('analysis'):
try: cpath = UF.get_project_path(args.path) UF.check_analysis_results(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, "semantics") stats = {} stats["openppos"] = 0 stats["openspos"] = 0 stats["ppoviolations"] = 0 stats["spoviolations"] = 0 capp = CApplication(sempath) fns = [] def v(f): ppoviolations = f.get_violations() spoviolations = f.get_spo_violations() openppos = f.get_open_ppos() openspos = f.get_open_spos() if len(ppoviolations) > 0 or len(spoviolations) > 0: fns.append(f) stats["ppoviolations"] += len(ppoviolations) stats["spoviolations"] += len(spoviolations) stats["openppos"] += len(openppos) stats["openspos"] += len(openspos) capp.iter_functions(v)
try: cpath = UF.get_project_path(args.path) UF.check_cfile(args.path,args.cfile) UF.check_semantics(cpath,deletesemantics=args.deletesemantics) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath,'semantics') if args.contractpath is None: contractpath = os.path.join(cpath,'chc_contracts') else: contractpath = args.contractpath capp = CApplication(sempath,args.cfile,contractpath=contractpath) chcpath = capp.path try: xfilename = UF.get_cfile_filename(chcpath,args.cfile) UF.check_cfile(chcpath,xfilename) except UF.CHError as e: print(str(e.wrap())) exit(1) am = AnalysisManager(capp,wordsize=int(args.wordsize),verbose=args.verbose, unreachability=args.unreachability, thirdpartysummaries=args.thirdpartysummaries) cfilename = args.cfile
return args if __name__ == '__main__': args = parse() try: cpath = UF.get_project_path(args.path) UF.check_semantics(cpath, deletesemantics=False) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, 'semantics') capp = CApplication(sempath) if args.contractpath is None: contractpath = os.path.join(cpath, 'chc_contracts') else: contractpath = args.contractpath ignorefns = {} if not args.ignorefile is None: if os.path.isfile(args.ignorefile): with open(args.ignorefile, 'r') as fp: headers = json.load(fp) for h in headers: for fn in headers[h]['functions']: ignorefns[fn] = h
args = parse() try: cpath = UF.get_juliet_testpath(args.cwe, args.test) UF.check_analysis_results(cpath) d = UF.get_juliet_scorekey(args.cwe, args.test) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, "semantics") excludefiles = ["io.c", "main_linux.c", "std_thread.c"] capp = CApplication(sempath, excludefiles=excludefiles) testset = JulietTestSetRef(d) try: julietppos = JTS.get_julietppos(testset) ppopairs = JTS.get_ppo_pairs(julietppos, capp) print(JTS.testppo_results_tostring(ppopairs, capp)) except IT.IndexedTableError as e: print("\n" + ("*" * 80) + "\nThe format of the analysis results has changed" + "\nPlease re-run the analysis first" + "\n" + ("*" * 80)) exit(1) testsummary: Dict[Any, Any] = {}
sempath = os.path.join(cpath, 'semantics') def indexfile(cfile): if args.verbose: print('- ' + cfile.name) def indexfn(cfun): if not cfun.name in functionindex: functionindex[cfun.name] = [] cfunrecord = {} cfunrecord['f'] = cfile.name cfunrecord['s'] = cfun.svar.get_vstorage() functionindex[cfun.name].append(cfunrecord) cfile.iter_functions(indexfn) capp = CApplication(sempath) with timing('indexing functions'): if args.verbose: print('-' * 80) print('Indexing files ...') capp.iter_files(indexfile) if args.verbose: print('-' * 80) print('Index:') for fn in sorted(functionindex): print(' - ' + fn) for fnrec in functionindex[fn]: pstatic = ' (static)' if fnrec['s'] == 's' else ''
UF.check_analysis_results(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) if args.history: summarieslist = UF.read_project_summary_results_history(cpath) else: summarieslist = [ UF.read_project_summary_results(cpath) ] for summaries in summarieslist: # summaries = UF.read_project_summary_results(cpath) try: if summaries == None: sempath = os.path.join(cpath,'semantics') capp = CApplication(sempath) timestamp = os.stat(capp.path).st_ctime result = RP.project_proofobligation_stats_to_dict(capp) result['timestamp'] = timestamp result['project'] = cpath UF.save_project_summary_results(cpath, result) UF.save_project_summary_results_as_xml(cpath, result) summaries = UF.read_project_summary_results(cpath) print(RP.project_proofobligation_stats_dict_to_string(summaries)) except IndexedTableError as e: print( '\n' + ('*' * 80) + '\nThe analysis results format has changed' + '\nYou may have to re-run the analysis first: ' + '\n' + e.msg + '\n' + ('*' * 80))
except UF.CHError as e: print(str(e.wrap())) exit(1) callgraph = UF.load_callgraph(cpath) if len(callgraph) == 0: print('Please create a callgraph first with chc_make_callgraph.py') exit(0) sempath = os.path.join(cpath, 'semantics') if not os.path.isdir(sempath): print(UP.semantics_not_found_err_msg(cpath)) exit(1) capp = CApplication(sempath) lines = [] stats = {} stats['npost'] = 0 stats['nglobal'] = 0 stats['ndepppo'] = 0 stats['ndepspo'] = 0 calleetable = {} # file -> function -> postcondition request list def record_callee(callerfile, p): calleefn = callerfile.capp.resolve_vid_function( callerfile.index, p.callee.get_vid()) if calleefn is None: return
capp.indexmanager.savexrefs(capp.path, f.name, f.index) if __name__ == "__main__": args = parse() try: cpath = UF.get_project_path(args.path) UF.check_semantics(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, "semantics") capp = CApplication(sempath) print("Global entities") if capp.declarations: print(str(capp.declarations.get_stats())) print("\nFile line and function counts\n") print(capp.get_line_counts()) request = { "mem": lambda v: v.is_mem(), "tmp": lambda v: v.is_tmpvar(), "var": lambda v: v.is_var and not v.is_tmpvar(), } lhosts = {} def f(cfile):
cpath = UF.get_juliet_testpath(args.cwe, args.test) UF.check_analysis_results(cpath) except UF.CHError as e: print(str(e.wrap())) exit(1) sempath = os.path.join(cpath, "semantics") excludefiles = ["io.c", "main_linux.c", "std_thread.c"] summary = UF.read_project_summary_results(cpath) if summary is not None: print(RP.project_proofobligation_stats_dict_to_string(summary)) exit(0) capp = CApplication(sempath, excludefiles=excludefiles) filterout = ["io", "main_linux", "std_thread"] dc = ["deadcode"] def filefilter(f): return f not in filterout print( RP.project_proofobligation_stats_tostring(capp, extradsmethods=dc, filefilter=filefilter)) contract_condition_violations = capp.get_contract_condition_violations() if len(contract_condition_violations) > 0: