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 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 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 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 walkCodePaths(fgraph, callback, loopcnt=0, maxpath=None): ''' walkCodePaths is a path generator which uses a callback function to determine the viability of each particular path. This approach allows the calling function (eg. walkSymbolikPaths) to do in-generator checks/processing and trim paths which are simply not possible/desireable. Callbacks will receive the current path, the current edge, and the new path node. For root nodes, the current path and edge will be None types. ''' pathcnt = 0 for root in fgraph.getHierRootNodes(): proot = vg_pathcore.newPathNode(nid=root[0], eid=None) # Fire callback once to init the dest "path node" callback(None, None, proot) 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 ] # let the callback know we've reached one... #if callback(cpath, None, None): yield cpath 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 edge = (eid, fromid, toid, einfo) npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) if not callback(cpath, edge, npath): vg_pathcore.trimPath(npath) continue todo.append((fgraph.getNode(toid), npath))
def walkCodePaths(fgraph, callback, loopcnt=0, maxpath=None): ''' walkCodePaths is a path generator which uses a callback function to determine the viability of each particular path. This approach allows the calling function (eg. walkSymbolikPaths) to do in-generator checks/processing and trim paths which are simply not possible/desireable. Callbacks will receive the current path, the current edge, and the new path node. For root nodes, the current path and edge will be None types. ''' pathcnt = 0 routed = fgraph.getMeta('Routed', False) for root in fgraph.getHierRootNodes(): proot = vg_pathcore.newPathNode(nid=root[0], eid=None) # Fire callback once to init the dest "path node" callback(None, None, proot) 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 ] # let the callback know we've reached one... #if callback(cpath, None, None): yield cpath vg_pathcore.trimPath(cpath) pathcnt += 1 if maxpath and pathcnt >= maxpath: return for eid, fromid, toid, einfo in refsfrom: # skip edges which are not marked "follow" if routed and not einfo.get('follow', False): continue # Skip loops if they are "deeper" than we are allowed if vg_pathcore.getPathLoopCount(cpath, 'nid', toid) > loopcnt: continue edge = (eid,fromid,toid,einfo) npath = vg_pathcore.newPathNode(parent=cpath, nid=toid, eid=eid) if not callback(cpath, edge, npath): vg_pathcore.trimPath(npath) continue todo.append((fgraph.getNode(toid),npath))
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 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 is not 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 is not 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 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) node = fgraph.getNode(tgtcbva) todo = [(node,pnode), ] while todo: node,cpath = todo.pop() refsto = fgraph.getRefsTo(node) # This is the root node! if node[1].get('rootnode'): 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 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 trackArgOrigin(vw, fva, argidx): """ Return an input tree (visgraph path tree) of the trackable inputs to the specified function. Each node in the list will be a leaf node for a path leading down toward a call to the target function. Each node will have the following path node properties: fva - The function argidx - The index of the argument input with this call cva - The address of the call (to our next) (None on root node) argv - A list of (<val>,<magic>) tuples for the call args (None on root node) """ rootpath = vg_path.newPathNode(fva=fva, cva=None, trackidx=argidx, argidx=None, argv=None) todo = [ rootpath, ] while len(todo): path = todo.pop() fva = vg_path.getNodeProp(path, 'fva') trackidx = vg_path.getNodeProp(path, 'trackidx') # Get all of our callers and their arguments to us for callva, argv in trackFunctionInputs(vw, fva): newfva = vw.getFunction(callva) pargs = dict(parent=path, fva=newfva, cva=callva, argidx=trackidx, argv=argv) newpath = vg_path.newPathNode(**pargs) aval, amagic = argv[trackidx] if isinstance(amagic, viv_magic.StackArg) and newfva: vg_path.setNodeProp(newpath, 'trackidx', amagic.index) todo.append(newpath) return vg_path.getLeafNodes(rootpath)
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 _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 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 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 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 trackArgOrigin(vw, fva, argidx): """ Return an input tree (visgraph path tree) of the trackable inputs to the specified function. Each node in the list will be a leaf node for a path leading down toward a call to the target function. Each node will have the following path node properties: fva - The function argidx - The index of the argument input with this call cva - The address of the call (to our next) (None on root node) argv - A list of (<val>,<magic>) tuples for the call args (None on root node) """ rootpath = vg_path.newPathNode(fva=fva, cva=None, trackidx=argidx, argidx=None, argv=None) todo = [rootpath, ] while len(todo): path = todo.pop() fva = vg_path.getNodeProp(path, 'fva') trackidx = vg_path.getNodeProp(path, 'trackidx') # Get all of our callers and their arguments to us for callva, argv in trackFunctionInputs(vw, fva): newfva = vw.getFunction(callva) pargs = dict(parent=path, fva=newfva, cva=callva, argidx=trackidx, argv=argv) newpath = vg_path.newPathNode(**pargs) aval, amagic = argv[trackidx] if isinstance(amagic, viv_magic.StackArg) and newfva: vg_path.setNodeProp(newpath, 'trackidx', amagic.index) todo.append(newpath) return vg_path.getLeafNodes(rootpath)
def newCodePathNode(self, parent=None, bva=None): ''' NOTE: Right now, this is only called from the actual branch state which needs it. it must stay that way for now (register context is being copied for symbolic emulator...) ''' props = { 'bva':bva, # the entry virtual address for this branch 'valist':[], # the virtual addresses in this node in order 'calllog':[], # FIXME is this even used? 'readlog':[], # a log of all memory reads from this block 'writelog':[],# a log of all memory writes from this block } return vg_path.newPathNode(parent=parent, **props)
def newCodePathNode(self, parent=None, bva=None): ''' NOTE: Right now, this is only called from the actual branch state which needs it. it must stay that way for now (register context is being copied for symbolic emulator...) ''' props = { 'bva': bva, # the entry virtual address for this branch 'valist': [], # the virtual addresses in this node in order 'readlog': [], # a log of all memory reads from this block 'writelog': [], # a log of all memory writes from this block } ret = vg_path.newPathNode(parent=parent, **props) return ret
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 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 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 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
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