def get_refsources(repo, branch): repopath = helper_zz.get_repopath(repo) global refsourcepath if not os.path.exists(refsourcepath): os.makedirs(refsourcepath) pickle_in = open("output/Patch_evolution_" + branch + "_pickle", 'rb') cve_commit_element_content = pickle.load(pickle_in) for cve in cve_commit_element_content: print 'get reference kernel source code for', cve cvepath = refsourcepath + '/' + cve if not os.path.exists(cvepath): os.mkdir(cvepath) for afterpatchcommit in cve_commit_element_content[cve][ 'aftercommits']: commitpath = cvepath + '/' + afterpatchcommit if not os.path.exists(commitpath): os.mkdir(commitpath) for (filename, funcname) in cve_commit_element_content[cve][ 'aftercommits'][afterpatchcommit]: directorypath = commitpath + '/' + '/'.join( filename.split('/')[:-1]) if not os.path.exists(directorypath): os.makedirs(directorypath) filepath = commitpath + '/' + filename string1 = 'cd ' + repopath + ';git show ' + afterpatchcommit + ':' + filename + ' > ' + filepath helper_zz.command(string1)
def get_patchfile(repopath, prevcommit, filename1, commit, filename2, funcnames): string1 = 'cd ' + repopath + ';git diff ' + prevcommit + ':' + filename1 + ' ' + commit + ':' + filename2 p_buf = helper_zz.command(string1) #revert may results in no difference if len(p_buf) == 0: return [] headstart = 0 head = p_buf[0] p_buf2 = [] local_pbuf = [] for i in range(len(p_buf)): line = p_buf[i] if '@@' in line: local_pbuf_str = ''.join(local_pbuf) if 'diff' in head or any(funcname in local_pbuf_str for funcname in funcnames): p_buf2 += local_pbuf local_pbuf = [] head = line local_pbuf += [line] local_pbuf_str = ''.join(local_pbuf) if 'diff' in head or any(funcname in local_pbuf_str for funcname in funcnames): p_buf2 += local_pbuf return p_buf2
def generate_matchcommands_target(branch, targetkernelpath, config): if not os.path.exists(targetkernelpath + '/boot'): print 'no binary image in', targetkernelpath return symbletable_path = targetkernelpath + "/" + "System.map" if not os.path.exists(symbletable_path): string1 = './helpers/ext_sym ' + targetkernelpath + '/boot > ' + targetkernelpath + '/System.map' result = helper_zz.command(string1) global refsourcepath, refsourcepath pickle_in = open("output/Patch_evolution_" + branch + "_pickle", 'rb') cve_commit_element_content = pickle.load(pickle_in) matchcommands = [] for cve in cve_commit_element_content: print 'generate matchcommands(target kernel) for', cve for afterpatchcommit in cve_commit_element_content[cve][ 'aftercommits']: sigspath = refsourcepath + '/' + cve + '/' + afterpatchcommit refkernel = sigspath + '/refkernel' unpatchkernel = sigspath + '/unpatchkernel' matchcommand = 'python match_sig.py ' + sigspath + ' 1 ' + refkernel + ' ' + unpatchkernel + ' ' + targetkernelpath matchcommands += [matchcommand] with open('output/Fiberinputs/matchcommands_target', 'a') as f: for line in matchcommands: f.write(line + '\n')
def get_mainfilecommits(repopath, branch, filename): string1 = 'cd ' + repopath + ';git log --pretty=oneline --first-parent ' + branch + ' -- -p ' + filename resultbuf = helper_zz.command(string1) mainfilecommits = [] resultbuf.reverse() for line in resultbuf: commit = line[:12] mainfilecommits += [commit] return mainfilecommits
def get_initcommit(kernel, patchfiles): patchfile = list(patchfiles)[0] string1 = 'cd ' + kernel + ';git log --first-parent --pretty=oneline -- -p ' + patchfile #print 'get_initcommit',string1 result = helper_zz.command(string1) if len(result) == 0: logresult([patchfile, 'not exist in', kernel]) return None return result[-1][:12]
def patchlocator(targetrepo, targetbranch, patchesinfo): #used in logresult global Targetrepo, Targetbranch Targetrepo = targetrepo Targetbranch = targetbranch outputdir = 'output/upstreamresults/' + targetrepo #with open(outputdir+'/'+targetbranch,'r') as f: # outputbuf = f.readlines() if not os.path.exists(outputdir): os.makedirs(outputdir) targetrepopath = helper_zz.get_repopath(targetrepo) string1 = 'cd ' + targetrepopath + ';git log --first-parent --pretty=oneline ' + targetbranch mainlog = helper_zz.command(string1) mainlogcommits = [line[:12] for line in mainlog] string1 = 'cd ' + targetrepopath + ';git log --pretty=oneline ' + targetbranch commitlog = helper_zz.command(string1) with open(patchesinfo, 'r') as f: s_buf = f.readlines() for line in s_buf: if '#' in line: continue (cve, repo, commit) = line[:-1].split(' ') if targetrepo == "android" or targetrepo == "linux": if "linux" not in repo and "common" not in repo: continue #if any(cve in line for line in outputbuf): # continue result = get_strict_patchcommits((cve, repo, commit), targetrepo, targetbranch, commitlog, mainlogcommits) if type(result) == str: if 'initcommit' in result: result = result.split(' ')[1] logresult([ cve, result, helper_zz.get_commitdate(targetrepopath, result) ]) else: logresult([cve, result])