Exemple #1
0
    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,onefile=True,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)
Exemple #2
0
    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,onefile=True,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)
Exemple #3
0
    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()
Exemple #4
0
    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,onefile=True,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()
Exemple #5
0
def parse():
    parser = argparse.ArgumentParser()
    parser.add_argument('cfilename',
                        help='name of kendra c file (e.g., id115.c)')
    parser.add_argument('--show_invariants',
                        help='show invariants in addition to diagnostic',
                        action='store_true')
    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()

    print(RP.file_code_tostring(cfile, showinvs=args.show_invariants))
    print(RP.file_proofobligation_stats_tostring(cfile))
        pdir = config.projects[args.path]
        cpath = os.path.join(config.testdir, pdir)
    else:
        cpath = os.path.abspath(args.path)

    if not os.path.isdir(cpath):
        print(UP.cpath_not_found_err_msg(cpath))
        exit(1)

    sempath = os.path.join(cpath, 'semantics')
    if not os.path.isdir(sempath):
        print(UP.semantics_not_found_err_msg(cpath))
        exit(1)

    try:
        cfapp = CApplication(sempath, args.cfile)
        cfile = cfapp.get_cfile()
    except CFileNotFoundException as e:
        print(e)
        exit(0)

    try:
        if args.showcode:
            if args.open:
                print(RP.file_code_open_tostring(cfile,
                                                 showinvs=args.showinvs))
            else:
                print(RP.file_code_tostring(cfile))

        print(RP.file_proofobligation_stats_tostring(cfile))
    except IndexedTableError as e:
        cpath = os.path.join(config.testdir, pdir)
    else:
        cpath = os.path.abspath(args.path)

    if not os.path.isdir(cpath):
        print(UP.cpath_not_found_err_msg(cpath))
        exit(1)

    sempath = os.path.join(cpath, 'semantics')
    if not os.path.isdir(sempath):
        success = UF.unpack_tar_file(cpath)
        if not success:
            print(UP.semantics_tar_not_found_err_msg(cpath))
            exit(1)

    capp = CApplication(sempath)
    if args.contractpath is None:
        contractpath = os.path.join(cpath, 'ktacontracts')
    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
        exit(0)

    # check linkinfo
    globaldefs = os.path.join(
        sempath, os.path.join('ktadvance', 'globaldefinitions.xml'))
    if not os.path.isfile(globaldefs):
        print(UP.global_definitions_not_found_err_msg(cpath))
        exit(0)

    if args.contractpath is None:
        contractpath = os.path.join(cpath, 'ktacontracts')
    else:
        contractpath = args.contractpath

    excludefiles = ['io.c', 'main_linux.c', 'std_thread.c']
    capp = CApplication(sempath,
                        contractpath=contractpath,
                        excludefiles=excludefiles)

    # assume wordsize of 64
    # use unreachability as a means of proof obligation discharge

    with timing('analysis of ' + args.path):

        am = AnalysisManager(capp,
                             wordsize=64,
                             unreachability=True,
                             thirdpartysummaries=[UF.get_juliet_summaries()])

        am.generate_and_check_app('llrvisp')
if __name__ == '__main__':

    header = ['open', 'api', 'rv', 'global', 'invariants', 'check-valid']

    title = 'testcase     ppos     %open     %api     %rv   %global   %invs  %checkvalid'

    missing = []

    print('\n' + title)
    print('-' * 80)
    for t in sorted(testcases):
        testdir = os.path.join(zitser, t)
        semdir = os.path.join(testdir, 'semantics')
        if os.path.isdir(semdir):
            results = {}
            capp = CApplication(semdir)
            ppomethods = capp.get_ppo_methods()
            total = sum(ppomethods[m] for m in ppomethods)
            line = [t, str(total).rjust(8)]
            for m in header:
                if m in ppomethods:
                    p = float(ppomethods[m]) / float(total) * 100.0
                    mp = '{:> 6.1f}'.format(p)
                    line.append(mp)
                else:
                    line.append('0.0'.rjust(6))
            print('   '.join(line))
        else:
            missing.append(t)

    if len(missing) > 0:
Exemple #10
0
if __name__ == '__main__':

    args = parse()
    cpath = UF.get_juliet_testpath(args.path)

    if not os.path.isdir(cpath):
        print(UP.cpath_not_found_err_msg(cpath))
        exit(1)

    sempath = os.path.join(cpath, 'semantics')
    if not os.path.isdir(sempath):
        print(UP.semantics_not_found_err_msg(cpath))
        exit(1)

    excludefiles = ['io.c', 'main_linux.c', 'std_thread.c']
    capp = CApplication(sempath, excludefiles=excludefiles)

    filterout = ['io', 'main_linux', 'std_thread']
    dc = ['deadcode']

    def filefilter(f):
        return (not f 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:

def parse():
    parser = argparse.ArgumentParser()
    parser.add_argument('path',
                        help='directory that holds the analysis results')
    parser.add_argument('--cfile', help='relative name of c file')
    parser.add_argument('--cfunction', help='name of function')
    args = parser.parse_args()
    return args


if __name__ == '__main__':

    args = parse()
    capp = CApplication(args.path)

    def p(a):
        print(a)

    def q(fn):
        print('Function ' + fn.getname())
        fn.getapi().apiassumptioniter(p)

    def r(f):
        print('File ' + f.getfilename())
        f.fniter(q)

    if not args.cfile is None:
        cfile = capp.getfile(args.cfile)
        if not args.cfunction is None:
    parser = argparse.ArgumentParser()
    parser.add_argument('path', help='sequence number of testcase, e.g., 231')
    args = parser.parse_args()
    return args


if __name__ == '__main__':

    args = parse()
    testpath = UF.get_itc_testpath(args.path)
    cpath = os.path.abspath(testpath)

    if not os.path.isdir(cpath):
        print(UP.cpath_not_found_err_msg(cpath))
        exit(1)

    sempath = os.path.join(cpath, 'semantics')
    if not os.path.isdir(sempath):
        success = UF.unpack_tar_file(cpath)
        if not success:
            print(UP.semantics_tar_not_found_err_msg(cpath))
            exit(1)

    contractpath = os.path.join(cpath, 'ktacontracts')
    capp = CApplication(sempath, contractpath=contractpath)

    def create(cfile):
        cfile.create_contract(contractpath)

    capp.iter_files(create)
Exemple #13
0
    if (not os.path.isdir(sempath)) or args.deletesemantics:
        success = UF.unpack_tar_file(cpath, args.deletesemantics)
        if not success:
            print(UP.semantics_tar_not_found_err_msg(cpath))
            exit(1)

    if args.contractpath is None:
        contractpath = os.path.join(cpath, 'ktacontracts')
    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'):
Exemple #14
0
    try:
        ifilename = parsemanager.preprocess_file_with_gcc(cfilename)
        result = parsemanager.parse_ifile(ifilename)
        if result != 0:
            print('*' * 80)
            print('Error in parsing ' + cfilename)
            print('*' * 80)
            exit(1)
    except OSError as e:
        print('Error in parsing file: ' + str(e))
        exit(1)

    sempath = os.path.join(cpath, 'semantics')
    contractpath = os.path.join(cpath, 'ktacontracts')

    capp = CApplication(sempath, cfilename, contractpath=contractpath)
    cfile = capp.get_cfile()
    cfile.create_contract(contractpath)

    ktadvpath = capp.path
    xfilename = UF.get_cfile_filename(ktadvpath, cfilename)

    if not os.path.isfile(xfilename):
        print(
            UP.err_msg([
                'No semantics files found for ' + args.cfile,
                '  Expected to find the file ' + xfilename,
                '  Please parse the file first with chc_parse_file.py or ' +
                'check the directory name'
            ]))
        exit(1)
Exemple #15
0
                gvinfo = self.cfile.declarations.get_global_varinfo_by_name(name)
                gconst = 'const' in gnode.attrib and gnode.get('const') == 'yes'
                gvalue = int(gnode.get('value')) if 'value' in gnode.attrib else None
                self.globalvariables[name] = CFileContractGlobalVar(gvinfo,gvalue,gconst)
        for fnode in xnode.find('functions').findall('function'):
            fn = CFunctionContract(self,fnode)
            self.functions[fn.name] = fn

if __name__ == '__main__':

    import os
    import xml.etree.ElementTree as ET
    from advance.app.CApplication import CApplication

    path = '/Users/henny/gitrepo/ktadvance/tests/sard/kendra/id263Q'
    contractpath = os.path.join(path,'ktacontracts')

    cfapp = CApplication(os.path.join(path,'semantics'),cfilename='id263.c',contractpath=contractpath)
    ccfile = os.path.join(path,'ktacontracts/id263_ktacc.xml')
    cfile = cfapp.get_cfile()


    tree = ET.parse(ccfile)
    root = tree.getroot()
    cnode = root.find('c-file')
    fnode = cnode[0]
    cfilecontracts = CFileContracts(cfile,contractpath)
    print(str(cfilecontracts.get_function_contract('function1')))
        
        
    sempath = os.path.join(cpath,'semantics')
    if (not os.path.isdir(sempath)) or args.deletesemantics:
        success = UF.unpack_tar_file(cpath,args.deletesemantics)
        if not success:
            print(UP.err_msg(['No file or directory found with semantics',
                                  '   Expected to find a directory ' + sempath,
                                  '   or a file semantics_linux.tar.gz in ' + cpath]))
            exit(1)

    if args.contractpath is None:
        contractpath = os.path.join(cpath,'ktacontracts')
    else:
        contractpath = args.contractpath    

    capp = CApplication(sempath,args.cfile,contractpath=contractpath)
    ktadvpath = capp.path
    xfilename = UF.get_cfile_filename(ktadvpath,args.cfile)

    if not os.path.isfile(xfilename):
        print(UP.err_msg(['No semantics files found for ' + args.cfile,
                              '  Expected to find the file ' + xfilename,
                              '  Please parse the file first with chc_parse_file.py or ' +
                              'check the directory name' ]))
        exit(1)
    
    am = AnalysisManager(capp,onefile=True,wordsize=int(args.wordsize),verbose=args.verbose,
                             unreachability=args.unreachability,
                             thirdpartysummaries=args.thirdpartysummaries)

    cfilename = args.cfile