def getCoveragePaths(fgraph, maxpath=None): ''' Get a set of paths which will cover every block, but will *end* on branches which re-merge with previously traversed paths. This allows a full coverage of the graph with as little work as possible, but *will* omit possible states. Returns: yield based path generator ( where path is list if (nid,edge) tuples ) ''' pathcnt = 0 nodedone = {} for root in fgraph.getHierRootNodes(): proot = vg_pathcore.newPathNode(nid=root[0], eid=None) todo = [(root,proot), ] while todo: node,cpath = todo.pop() refsfrom = fgraph.getRefsFrom(node) # Record that we have visited this node... nodedone[node[0]] = True # This is a leaf node! if not refsfrom: path = vg_pathcore.getPathToNode(cpath) yield [ _nodeedge(n) for n in path ] pathcnt += 1 if maxpath != None and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsfrom: # If we're branching to a visited node, return the path as is if nodedone.get(toid): path = vg_pathcore.getPathToNode(cpath) yield [ _nodeedge(n) for n in path ] # Check if that was the last path we should yield pathcnt += 1 if maxpath != None and pathcnt >= maxpath: return # If we're at a completed node, take no further branches continue npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) tonode = fgraph.getNode(toid) todo.append((tonode,npath))
def getLoopPaths(fgraph): ''' Similar to getCodePaths(), however, getLoopPaths() will return path lists which loop. The last element in the (node,edge) list will be the first "looped" block. ''' for root in fgraph.getHierRootNodes(): proot = vg_pathcore.newPathNode(nid=root[0], eid=None) todo = [ (root[0],proot,0), ] while todo: node,cpath,loopcnt = todo.pop() count = 0 free = [] if loopcnt == 1: yield [ _nodeedge(n) for n in vg_pathcore.getPathToNode(npath) ] else: for eid, fromid, toid, einfo in fgraph.getRefsFromByNid(node): loopcnt = vg_pathcore.getPathLoopCount(cpath, 'nid', toid) if loopcnt > 1: continue count += 1 npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) todo.append((toid,npath,loopcnt)) if not count: vg_pathcore.trimPath(cpath)
def _getCodePathsThru2(fgraph, tgtcbva, path, firstpath, loopcnt=0, pathcnt=0, maxpath=None): tgtnode = fgraph.getNode(tgtcbva) todo = [ (tgtnode,firstpath), ] while todo: node,cpath = todo.pop() refsfrom = fgraph.getRefsFrom(node) # This is a leaf node! if not refsfrom: path = vg_pathcore.getPathToNode(cpath) yield path, pathcnt vg_pathcore.trimPath(cpath) pathcnt += 1 if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsfrom: # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', toid) if loops > loopcnt: continue npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) tonode = fgraph.getNode(toid) todo.append((tonode,npath))
def getLoopPaths(fgraph): ''' Similar to getCodePaths(), however, getLoopPaths() will return path lists which loop. The last element in the (node,edge) list will be the first "looped" block. ''' loops = [] for root in fgraph.getRootNodes(): proot = vg_pathcore.newPathNode(nid=root, eid=None) todo = [ (root,proot), ] while todo: nodeid,cpath = todo.pop() for eid, fromid, toid, einfo in fgraph.getRefsFrom(nodeid): loopcnt = vg_pathcore.getPathLoopCount(cpath, 'nid', toid) if loopcnt > 1: continue npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) if loopcnt == 1: loops.append(npath) else: todo.append((toid,npath)) for lnode in loops: yield [ _nodeedge(n) for n in vg_pathcore.getPathToNode(lnode) ]
def getCodePathsThru(fgraph, tgtcbva, loopcnt=0, maxpath=None): ''' Yields all the paths through the hierarchical graph which pass through the target codeblock "tgtcb". Each "root" node is traced to the target, and all paths are traversed from there to the end. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePathsThru(fgraph, tgtcb): for node,edge in path: ...etc... ''' # this starts with the "To" side, finding a path back from tgtcbva to root pathcnt = 0 looptrack = [] pnode = vg_pathcore.newPathNode(nid=tgtcbva, eid=None) rootnodes = fgraph.getHierRootNodes() tgtnode = fgraph.getNode(tgtcbva) todo = [(tgtnode,pnode), ] while todo: node,cpath = todo.pop() refsto = fgraph.getRefsTo(node) # This is the root node! if node in rootnodes: path = vg_pathcore.getPathToNode(cpath) path.reverse() # build the path in the right direction newcpath = None lastnk = {'eid':None} for np,nc,nk in path: newcpath = vg_pathcore.newPathNode(parent=newcpath, nid=nk['nid'], eid=lastnk['eid']) lastnk = nk for fullpath, count in _getCodePathsThru2(fgraph, tgtcbva, path, newcpath, loopcnt=loopcnt, pathcnt=pathcnt, maxpath=maxpath): yield [ _nodeedge(n) for n in fullpath ] vg_pathcore.trimPath(cpath) pathcnt += count if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsto: # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', fromid) if loops > loopcnt: continue #vg_pathcore.setNodeProp(cpath, 'eid', eid) #print "-e: %d %x %x %s" % (eid, fromid, toid, repr(einfo)) npath = vg_pathcore.newPathNode(parent=cpath, nid=fromid, eid=eid) fromnode = fgraph.getNode(fromid) todo.append((fromnode,npath))
def do_argtrack(self, line): """ Track input arguments to the given function by name or address. Usage: argtrack <func_addr_expr> <arg_idx> """ if not line: return self.do_help("argtrack") argv = e_cli.splitargs(line) if len(argv) != 2: return self.do_help("argtrack") try: fva = self.parseExpression(argv[0]) except Exception as e: self.vprint("Invalid Address Expression: %s" % argv[0]) return try: idx = self.parseExpression(argv[1]) except Exception as e: self.vprint("Invalid Index Expression: %s" % argv[1]) return if self.getFunction(fva) != fva: self.vprint("Invalid Function Address: (0x%.8x) %s" % (fva, line)) for pleaf in viv_vector.trackArgOrigin(self, fva, idx): self.vprint('='*80) path = vg_path.getPathToNode(pleaf) path.reverse() for pnode in path: fva = vg_path.getNodeProp(pnode, 'fva') argv = vg_path.getNodeProp(pnode, 'argv') callva = vg_path.getNodeProp(pnode, 'cva') argidx = vg_path.getNodeProp(pnode, 'argidx') if callva != None: aval, amagic = argv[argidx] arepr = '0x%.8x' % aval if amagic != None: arepr = repr(amagic) frepr = 'UNKNOWN' if fva != None: frepr = '0x%.8x' % fva self.vprint('func: %s calls at: 0x%.8x with his own: %s' % (frepr, callva, arepr)) self.vprint("="*80)
def getCodePathsTo(fgraph, tocbva, loopcnt=0, maxpath=None): ''' Yields all the paths through the hierarchical graph starting at the "root nodes" and ending at tocbva. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePathsTo(fgraph, tocbva): for node,edge in path: ...etc... ''' pathcnt = 0 looptrack = [] pnode = vg_pathcore.newPathNode(nid=tocbva, eid=None) #rootnodes = fgraph.getHierRootNodes() cbnode = fgraph.getNode(tocbva) todo = [(cbnode,pnode), ] while todo: node,cpath = todo.pop() refsto = fgraph.getRefsTo(node) # Is this is the root node? if node[1].get('rootnode'): path = vg_pathcore.getPathToNode(cpath) path.reverse() yield [ _nodeedge(n) for n in path ] vg_pathcore.trimPath(cpath) pathcnt += 1 if maxpath and pathcnt >= maxpath: return for eid, n1, n2, einfo in refsto: # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', n1) if loops > loopcnt: continue vg_pathcore.setNodeProp(cpath, 'eid', eid) npath = vg_pathcore.newPathNode(parent=cpath, nid=n1, eid=None) node1 = fgraph.getNode(n1) todo.append((node1,npath))
def getCodePathsFrom(fgraph, fromcbva, loopcnt=0, maxpath=None): ''' Yields all the paths through the hierarchical graph beginning with "fromcbva", which is traced to all terminating points. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePathsFrom(fgraph, fromcbva): for node,edge in path: ...etc... ''' pathcnt = 0 proot = vg_pathcore.newPathNode(nid=fromcbva, eid=None) cbnid,cbnode = fgraph.getNode(fromcbva) todo = [(cbnid,proot), ] while todo: nid,cpath = todo.pop() refsfrom = fgraph.getRefsFromByNid(nid) # This is a leaf node! if not refsfrom: path = vg_pathcore.getPathToNode(cpath) yield [ _nodeedge(n) for n in path ] vg_pathcore.trimPath(cpath) pathcnt += 1 if maxpath and pathcnt >= maxpath: return for eid, fromid, n2, einfo in refsfrom: # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', n2) if loops > loopcnt: continue npath = vg_pathcore.newPathNode(parent=cpath, nid=n2, eid=eid) todo.append((n2,npath))
def getCodePathsFrom(fgraph, fromcbva, loopcnt=0, maxpath=None): ''' Yields all the paths through the hierarchical graph beginning with "fromcbva", which is traced to all terminating points. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePathsFrom(fgraph, fromcbva): for node,edge in path: ...etc... ''' pathcnt = 0 proot = vg_pathcore.newPathNode(nid=fromcbva, eid=None) cbnid, cbnode = fgraph.getNode(fromcbva) todo = [(cbnid, proot), ] while todo: nid, cpath = todo.pop() refsfrom = fgraph.getRefsFromByNid(nid) # This is a leaf node! if not refsfrom: path = vg_pathcore.getPathToNode(cpath) yield [_nodeedge(n) for n in path] vg_pathcore.trimPath(cpath) pathcnt += 1 if maxpath and pathcnt >= maxpath: return for eid, fromid, n2, einfo in refsfrom: # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', n2) if loops > loopcnt: continue npath = vg_pathcore.newPathNode(parent=cpath, nid=n2, eid=eid) todo.append((n2, npath))
def getCodePaths(fgraph, loopcnt=0, maxpath=None): ''' Yields all the paths through the hierarchical graph. Each "root" node is traced to all terminating points. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePaths(fgraph): for node,edge in path: ...etc... ''' pathcnt = 0 for root in fgraph.getHierRootNodes(): proot = vg_pathcore.newPathNode(nid=root[0], eid=None) todo = [(root,proot), ] while todo: node,cpath = todo.pop() refsfrom = fgraph.getRefsFrom(node) # This is a leaf node! if not refsfrom: path = vg_pathcore.getPathToNode(cpath) yield [ _nodeedge(n) for n in path ] vg_pathcore.trimPath(cpath) pathcnt += 1 if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsfrom: # Skip loops if they are "deeper" than we are allowed if vg_pathcore.getPathLoopCount(cpath, 'nid', toid) > loopcnt: continue npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) tonode = fgraph.getNode(toid) todo.append((tonode,npath))
def _getCodePathsThru2(fgraph, tgtcbva, path, firstpath, loopcnt=0, pathcnt=0, maxpath=None): tgtnode = fgraph.getNode(tgtcbva) todo = [ (tgtnode, firstpath), ] while todo: node, cpath = todo.pop() refsfrom = fgraph.getRefsFrom(node) # This is a leaf node! if not refsfrom: path = vg_pathcore.getPathToNode(cpath) yield path, pathcnt vg_pathcore.trimPath(cpath) pathcnt += 1 if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsfrom: # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', toid) if loops > loopcnt: continue npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) tonode = fgraph.getNode(toid) todo.append((tonode, npath))
def getCodePaths(vw, fromva, tova, trim=True): """ Return a list of paths, where each path is a list of code blocks from fromva to tova. Usage: getCodePaths(vw, <fromva>, <tova>) -> [ [frblock, ..., toblock], ...] NOTE: "trim" causes an optimization which may not reveal *all* the paths, but is much faster to run. It will never return no paths when there are some, but may not return all of them... (based on path overlap) """ done = {} res = [] frcb = vw.getCodeBlock(fromva) tocb = vw.getCodeBlock(tova) if frcb == None: raise viv_exc.InvalidLocation(fromva) if tocb == None: raise viv_exc.InvalidLocation(tova) frva = frcb[0] # For compare speed root = vg_path.newPathNode(cb=tocb, cbva=tocb[0]) todo = [root, ] done[tova] = tocb cbcache = {} while len(todo): path = todo.pop() cbva = vg_path.getNodeProp(path, 'cbva') codeblocks = cbcache.get(cbva) if codeblocks == None: codeblocks = getCodeFlow(vw, cbva) cbcache[cbva] = codeblocks for cblock in codeblocks: bva,bsize,bfva = cblock # Don't follow loops... if vg_path.isPathLoop(path, 'cbva', bva): continue # If we have been here before and it's *not* the answer, # skip out... if trim and done.get(bva) != None: continue done[bva] = cblock newpath = vg_path.newPathNode(parent=path, cb=cblock, cbva=bva) # If this one is a match, we don't need to # track past it. Also, put it in the results list # so we don't have to do it later.... if bva == frva: res.append(newpath) else: todo.append(newpath) # Now... if we have some results, lets build the block list. ret = [] for cpath in res: fullpath = vg_path.getPathToNode(cpath) # We actually do it by inbound references, so reverse the result! fullpath.reverse() ret.append([vg_path.getNodeProp(path, 'cb') for path in fullpath]) return ret
def getCodePaths(vw, fromva, tova, trim=True): """ Return a list of paths, where each path is a list of code blocks from fromva to tova. Usage: getCodePaths(vw, <fromva>, <tova>) -> [ [frblock, ..., toblock], ...] NOTE: "trim" causes an optimization which may not reveal *all* the paths, but is much faster to run. It will never return no paths when there are some, but may not return all of them... (based on path overlap) """ done = {} res = [] frcb = vw.getCodeBlock(fromva) tocb = vw.getCodeBlock(tova) if frcb == None: raise viv_exc.InvalidLocation(fromva) if tocb == None: raise viv_exc.InvalidLocation(tova) frva = frcb[0] # For compare speed root = vg_path.newPathNode(cb=tocb, cbva=tocb[0]) todo = [ root, ] done[tova] = tocb cbcache = {} while len(todo): path = todo.pop() cbva = vg_path.getNodeProp(path, 'cbva') codeblocks = cbcache.get(cbva) if codeblocks == None: codeblocks = getCodeFlow(vw, cbva) cbcache[cbva] = codeblocks for cblock in codeblocks: bva, bsize, bfva = cblock # Don't follow loops... if vg_path.isPathLoop(path, 'cbva', bva): continue # If we have been here before and it's *not* the answer, # skip out... if trim and done.get(bva) != None: continue done[bva] = cblock newpath = vg_path.newPathNode(parent=path, cb=cblock, cbva=bva) # If this one is a match, we don't need to # track past it. Also, put it in the results list # so we don't have to do it later.... if bva == frva: res.append(newpath) else: todo.append(newpath) # Now... if we have some results, lets build the block list. ret = [] for cpath in res: fullpath = vg_path.getPathToNode(cpath) # We actually do it by inbound references, so reverse the result! fullpath.reverse() ret.append([vg_path.getNodeProp(path, 'cb') for path in fullpath]) return ret
def pathSearch(self, n1, n2=None, edgecb=None, tocb=None): ''' Search for the shortest path from one node to another with the option to filter based on edges using edgecb. edgecb should be a function: def myedgecb(graph, eid, n1, n2, depth) which returns True if it's OK to traverse this node in the search. Additionally, n2 may be None and the caller may specify tocb with a function such as: def mytocb(graph, nid) which must return True on finding the target node Returns a list of edge ids... ''' if n2 is None and tocb is None: raise Exception('You must use either n2 or tocb!') root = vg_pathcore.newPathNode(nid=n1, eid=None) todo = [ (root, 0), ] # FIXME make this a deque so it can be FIFO while len(todo): pnode, depth = todo.pop() # popleft() ppnode, pkids, pprops = pnode nid = pprops.get('nid') for edge in self.getRefsFromByNid(nid): eid, srcid, dstid, eprops = edge if vg_pathcore.isPathLoop(pnode, 'nid', dstid): continue # Check if the callback is present and likes us... if edgecb is not None: if not edgecb(self, edge, depth): continue # Are we the match? match = False if dstid == n2: match = True if tocb and tocb(self, dstid): match = True if match: m = vg_pathcore.newPathNode(pnode, nid=dstid, eid=eid) path = vg_pathcore.getPathToNode(m) ret = [] for ppnode, pkids, pprops in path: eid = pprops.get('eid') if eid is not None: ret.append(eid) yield ret # Add the next set of choices to evaluate. branch = vg_pathcore.newPathNode(pnode, nid=dstid, eid=eid) todo.append((branch, depth + 1))
def pathSearch(self, n1, n2=None, edgecb=None, tocb=None): ''' Search for the shortest path from one node to another with the option to filter based on edges using edgecb. edgecb should be a function: def myedgecb(graph, eid, n1, n2, depth) which returns True if it's OK to traverse this node in the search. Additionally, n2 may be None and the caller may specify tocb with a function such as: def mytocb(graph, nid) which must return True on finding the target node Returns a list of edge ids... ''' if n2 == None and tocb == None: raise Exception('You must use either n2 or tocb!') root = vg_pathcore.newPathNode(nid=n1, eid=None) todo = [(root, 0),] # FIXME make this a deque so it can be FIFO while len(todo): pnode,depth = todo.pop() # popleft() ppnode, pkids, pprops = pnode nid = pprops.get('nid') for edge in self.getRefsFromByNid(nid): eid, srcid, dstid, eprops = edge if vg_pathcore.isPathLoop(pnode, 'nid', dstid): continue # Check if the callback is present and likes us... if edgecb != None: if not edgecb(self, edge, depth): continue # Are we the match? match = False if dstid == n2: match = True if tocb and tocb(self, dstid): match = True if match: m = vg_pathcore.newPathNode(pnode, nid=dstid, eid=eid) path = vg_pathcore.getPathToNode(m) ret = [] for ppnode, pkids, pprops in path: eid = pprops.get('eid') if eid != None: ret.append(eid) yield ret # Add the next set of choices to evaluate. branch = vg_pathcore.newPathNode(pnode, nid=dstid, eid=eid) todo.append((branch, depth+1))
def getCodePathsThru(fgraph, tgtcbva, loopcnt=0, maxpath=None): ''' Yields all the paths through the hierarchical graph which pass through the target codeblock "tgtcb". Each "root" node is traced to the target, and all paths are traversed from there to the end. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePathsThru(fgraph, tgtcb): for node,edge in path: ...etc... ''' # this starts with the "To" side, finding a path back from tgtcbva to root pathcnt = 0 looptrack = [] pnode = vg_pathcore.newPathNode(nid=tgtcbva, eid=None) rootnodes = fgraph.getHierRootNodes() tgtnode = fgraph.getNode(tgtcbva) todo = [ (tgtnode, pnode), ] while todo: node, cpath = todo.pop() refsto = fgraph.getRefsTo(node) # This is the root node! if node in rootnodes: path = vg_pathcore.getPathToNode(cpath) path.reverse() # build the path in the right direction newcpath = None lastnk = {'eid': None} for np, nc, nk in path: newcpath = vg_pathcore.newPathNode(parent=newcpath, nid=nk['nid'], eid=lastnk['eid']) lastnk = nk for fullpath, count in _getCodePathsThru2(fgraph, tgtcbva, path, newcpath, loopcnt=loopcnt, pathcnt=pathcnt, maxpath=maxpath): yield [_nodeedge(n) for n in fullpath] vg_pathcore.trimPath(cpath) pathcnt += count if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsto: # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', fromid) if loops > loopcnt: continue #vg_pathcore.setNodeProp(cpath, 'eid', eid) #print "-e: %d %x %x %s" % (eid, fromid, toid, repr(einfo)) npath = vg_pathcore.newPathNode(parent=cpath, nid=fromid, eid=eid) fromnode = fgraph.getNode(fromid) todo.append((fromnode, npath))
def getFuncCbRoutedPaths(self, fromva, tova, loopcnt=0, maxpath=None, maxsec=None): ''' Yields all the paths through the hierarchical graph starting at the "root nodes" and ending at tocbva. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePathsTo(fgraph, tocbva): for node,edge in path: ...etc... ''' fgraph = self.graph self.__update = 0 self.__go__ = True pathcnt = 0 tocbva = getGraphNodeByVa(fgraph, tova) frcbva = getGraphNodeByVa(fgraph, fromva) preRouteGraph(fgraph, fromva, tova) pnode = vg_pathcore.newPathNode(nid=frcbva, eid=None) todo = [(frcbva, pnode), ] if maxsec: self.watchdog(maxsec) while todo: if not self.__go__: raise PathForceQuitException() nodeid,cpath = todo.pop() refsfrom = fgraph.getRefsFrom((nodeid, None)) # This is the root node! if nodeid == tocbva: path = vg_pathcore.getPathToNode(cpath) yield [ _nodeedge(n) for n in path ] vg_pathcore.trimPath(cpath) pathcnt += 1 self.__update = 1 if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsfrom: if fgraph.getNodeProps(fromid).get('down') != True: #sys.stderr.write('.') # TODO: drop the bad edges from graph in preprocessing? instead of "if" here continue # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', fromid) if loops > loopcnt: vg_pathcore.trimPath(cpath) #sys.stderr.write('o') # as long as we have at least one path, we count loops as paths, lest we die. if pathcnt: pathcnt += 1 continue npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) todo.append((toid,npath)) vg_pathcore.trimPath(cpath)
def getFuncCbRoutedPaths_genback(self, fromva, tova, loopcnt=0, maxpath=None, maxsec=None): ''' Yields all the paths through the hierarchical graph starting at the "root nodes" and ending at tocbva. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePathsTo(fgraph, tocbva): for node,edge in path: ...etc... ''' fgraph = self.graph self.__update = 0 self.__go__ = True pathcnt = 0 tocbva = getGraphNodeByVa(fgraph, tova) frcbva = getGraphNodeByVa(fgraph, fromva) preRouteGraph(fgraph, fromva, tova) pnode = vg_pathcore.newPathNode(nid=tocbva, eid=None) todo = [(tocbva,pnode), ] if maxsec: self.watchdog(maxsec) while todo: if not self.__go__: raise PathForceQuitException() nodeid,cpath = todo.pop() refsto = fgraph.getRefsTo((nodeid, None)) # This is the root node! if nodeid == frcbva: path = vg_pathcore.getPathToNode(cpath) path.reverse() self.__steplock.acquire() yield [ viv_graph._nodeedge(n) for n in path ] vg_pathcore.trimPath(cpath) pathcnt += 1 self.__update = 1 self.__steplock.release() if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsto: if fgraph.getNodeProps(fromid).get('up') != True: # TODO: drop the bad edges from graph in preprocessing? instead of "if" here vg_pathcore.trimPath(cpath) continue # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', fromid) if loops > loopcnt: continue vg_pathcore.setNodeProp(cpath, 'eid', eid) npath = vg_pathcore.newPathNode(parent=cpath, nid=fromid, eid=None) todo.append((fromid,npath))
def getFuncCbRoutedPaths_genback(self, fromva, tova, loopcnt=0, maxpath=None, timeout=None): ''' Yields all the paths through the hierarchical graph starting at the "root nodes" and ending at tocbva. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePathsTo(fgraph, tocbva): for node,edge in path: ...etc... ''' fgraph = self.graph self.__update = 0 self.__go__ = True pathcnt = 0 tocbva = getGraphNodeByVa(fgraph, tova) frcbva = getGraphNodeByVa(fgraph, fromva) preRouteGraph(fgraph, fromva, tova) pnode = vg_pathcore.newPathNode(nid=tocbva, eid=None) todo = [ (tocbva, pnode), ] maxtime = None if timeout: maxtime = time.time() + timeout while todo: if maxtime and time.time() > maxtime: raise PathForceQuitException() if not self.__go__: raise PathForceQuitException() nodeid, cpath = todo.pop() refsto = fgraph.getRefsTo((nodeid, None)) # This is the root node! if nodeid == frcbva: path = vg_pathcore.getPathToNode(cpath) path.reverse() self.__steplock.acquire() yield [viv_graph._nodeedge(n) for n in path] vg_pathcore.trimPath(cpath) pathcnt += 1 self.__update = 1 self.__steplock.release() if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsto: if fgraph.getNodeProps(fromid).get('up') != True: # TODO: drop the bad edges from graph in preprocessing? instead of "if" here vg_pathcore.trimPath(cpath) continue # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', fromid) if loops > loopcnt: continue vg_pathcore.setNodeProp(cpath, 'eid', eid) npath = vg_pathcore.newPathNode(parent=cpath, nid=fromid, eid=None) todo.append((fromid, npath)) self.__go__ = False
return try: idx = self.parseExpression(argv[1]) except Exception, e: self.vprint("Invalid Index Expression: %s" % argv[1]) return if self.getFunction(fva) != fva: self.vprint("Invalid Function Address: (0x%.8x) %s" % (fva, line)) for pleaf in viv_vector.trackArgOrigin(self, fva, idx): self.vprint('='*80) path = vg_path.getPathToNode(pleaf) path.reverse() for pnode in path: fva = vg_path.getNodeProp(pnode, 'fva') argv = vg_path.getNodeProp(pnode, 'argv') callva = vg_path.getNodeProp(pnode, 'cva') argidx = vg_path.getNodeProp(pnode, 'argidx') if callva != None: aval, amagic = argv[argidx] arepr = '0x%.8x' % aval if amagic != None: arepr = repr(amagic) frepr = 'UNKNOWN' if fva != None: frepr = '0x%.8x' % fva
def getFuncCbRoutedPaths(self, fromva, tova, loopcnt=0, maxpath=None, timeout=None): ''' Yields all the paths through the hierarchical graph starting at the "root nodes" and ending at tocbva. Specify a loopcnt to allow loop paths to be generated with the given "loop iteration count" Example: for path in getCodePathsTo(fgraph, tocbva): for node,edge in path: ...etc... ''' fgraph = self.graph self.__update = 0 self.__go__ = True pathcnt = 0 tocbva = getGraphNodeByVa(fgraph, tova) frcbva = getGraphNodeByVa(fgraph, fromva) preRouteGraph(fgraph, fromva, tova) pnode = vg_pathcore.newPathNode(nid=frcbva, eid=None) todo = [ (frcbva, pnode), ] maxtime = None if timeout: maxtime = time.time() + timeout while todo: if maxtime and time.time() > maxtime: raise PathForceQuitException() if not self.__go__: raise PathForceQuitException() nodeid, cpath = todo.pop() refsfrom = fgraph.getRefsFrom((nodeid, None)) # This is the root node! if nodeid == tocbva: path = vg_pathcore.getPathToNode(cpath) yield [_nodeedge(n) for n in path] vg_pathcore.trimPath(cpath) pathcnt += 1 self.__update = 1 if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsfrom: if fgraph.getNodeProps(fromid).get('down') != True: #sys.stderr.write('.') # TODO: drop the bad edges from graph in preprocessing? instead of "if" here continue # Skip loops if they are "deeper" than we are allowed loops = vg_pathcore.getPathLoopCount(cpath, 'nid', fromid) if loops > loopcnt: vg_pathcore.trimPath(cpath) #sys.stderr.write('o') # as long as we have at least one path, we count loops as paths, lest we die. if pathcnt: pathcnt += 1 continue npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) todo.append((toid, npath)) vg_pathcore.trimPath(cpath) self.__go__ = False