本文整理匯總了Python中miasm2.core.graph.DiGraph.add_node方法的典型用法代碼示例。如果您正苦於以下問題:Python DiGraph.add_node方法的具體用法?Python DiGraph.add_node怎麽用?Python DiGraph.add_node使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類miasm2.core.graph.DiGraph
的用法示例。
在下文中一共展示了DiGraph.add_node方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from miasm2.core.graph import DiGraph [as 別名]
# 或者: from miasm2.core.graph.DiGraph import add_node [as 別名]
class basicblocs:
def __init__(self, ab=[]):
self.blocs = {}
self.g = DiGraph()
self.add_blocs(ab)
def add(self, b):
self.blocs[b.label] = b
self.g.add_node(b.label)
for dst in b.bto:
if isinstance(dst.label, asm_label):
self.g.add_edge(b.label, dst.label)
def add_blocs(self, ab):
for b in ab:
self.add(b)
def get_bad_dst(self):
o = set()
for b in self.blocs.values():
for c in b.bto:
if c.c_t == asm_constraint.c_bad:
o.add(b)
return o
示例2: unflatGraph
# 需要導入模塊: from miasm2.core.graph import DiGraph [as 別名]
# 或者: from miasm2.core.graph.DiGraph import add_node [as 別名]
def unflatGraph(flat_graph):
graph = DiGraph()
nodes, edges = flat_graph
for node in nodes:
graph.add_node(node)
for nodeA, nodeB in edges:
graph.add_edge(nodeA, nodeB)
return graph
示例3: as_graph
# 需要導入模塊: from miasm2.core.graph import DiGraph [as 別名]
# 或者: from miasm2.core.graph.DiGraph import add_node [as 別名]
def as_graph(self):
"""Generates a Digraph of dependencies"""
graph = DiGraph()
for node_a, node_b in self.links:
if not node_b:
graph.add_node(node_a)
else:
graph.add_edge(node_a, node_b)
for parent, sons in self.pending.iteritems():
for son in sons:
graph.add_edge(parent, son)
return graph
示例4: blist2graph
# 需要導入模塊: from miasm2.core.graph import DiGraph [as 別名]
# 或者: from miasm2.core.graph.DiGraph import add_node [as 別名]
def blist2graph(ab):
"""
ab: list of asmbloc
return: graph of asmbloc
"""
g = DiGraph()
g.lbl2bloc = {}
for b in ab:
g.lbl2bloc[b.label] = b
g.add_node(b.label)
for x in b.bto:
g.add_edge(b.label, x.label)
return g
示例5: as_graph
# 需要導入模塊: from miasm2.core.graph import DiGraph [as 別名]
# 或者: from miasm2.core.graph.DiGraph import add_node [as 別名]
def as_graph(self, starting_nodes):
"""Return a DiGraph corresponding to computed dependencies, with
@starting_nodes as leafs
@starting_nodes: set of DependencyNode instance
"""
# Build subgraph for each starting_node
subgraphs = []
for starting_node in starting_nodes:
subgraphs.append(self._build_depGraph(starting_node))
# Merge subgraphs into a final DiGraph
graph = DiGraph()
for sourcegraph in subgraphs:
for node in sourcegraph.nodes():
graph.add_node(node)
for edge in sourcegraph.edges():
graph.add_uniq_edge(*edge)
return graph
示例6: _build_depGraph
# 需要導入模塊: from miasm2.core.graph import DiGraph [as 別名]
# 或者: from miasm2.core.graph.DiGraph import add_node [as 別名]
def _build_depGraph(self, depnode):
"""Recursively build the final list of DiGraph, and clean up unmodifier
nodes
@depnode: starting node
"""
if depnode not in self._cache or \
not self._cache[depnode]:
## There is no dependency
graph = DiGraph()
graph.add_node(depnode)
return graph
# Recursion
dependencies = list(self._cache[depnode])
graphs = []
for sub_depnode in dependencies:
graphs.append(self._build_depGraph(sub_depnode))
# head(graphs[i]) == dependencies[i]
graph = DiGraph()
graph.add_node(depnode)
for head in dependencies:
graph.add_uniq_edge(head, depnode)
for subgraphs in itertools.product(graphs):
for sourcegraph in subgraphs:
for node in sourcegraph.nodes():
graph.add_node(node)
for edge in sourcegraph.edges():
graph.add_uniq_edge(*edge)
# Update the running queue
return graph
示例7: sort_dst
# 需要導入模塊: from miasm2.core.graph import DiGraph [as 別名]
# 或者: from miasm2.core.graph.DiGraph import add_node [as 別名]
class ira:
def sort_dst(self, todo, done):
out = set()
while todo:
dst = todo.pop()
if self.ExprIsLabel(dst):
done.add(dst)
elif isinstance(dst, ExprMem) or isinstance(dst, ExprInt):
done.add(dst)
elif isinstance(dst, ExprCond):
todo.add(dst.src1)
todo.add(dst.src2)
elif isinstance(dst, ExprId):
out.add(dst)
else:
done.add(dst)
return out
def dst_trackback(self, b):
dst = b.dst
todo = set([dst])
out = set()
done = set()
for irs in reversed(b.irs):
if len(todo) == 0:
break
out = self.sort_dst(todo, done)
found = set()
follow = set()
for i in irs:
if not out:
break
for o in out:
if i.dst == o:
follow.add(i.src)
found.add(o)
for o in found:
out.remove(o)
for o in out:
if not o in found:
follow.add(o)
todo = follow
out = self.sort_dst(todo, done)
return done
def gen_graph(self, link_all = False):
"""
Gen irbloc digraph
@link_all: also gen edges to non present irblocs
"""
self.g = DiGraph()
for lbl, b in self.blocs.items():
# print 'add', lbl
self.g.add_node(lbl)
# dst = self.get_bloc_dst(b)
dst = self.dst_trackback(b)
# print "\tdst", dst
for d in dst:
if isinstance(d, ExprInt):
d = ExprId(
self.symbol_pool.getby_offset_create(int(d.arg)))
if self.ExprIsLabel(d):
if d.name in self.blocs or link_all is True:
self.g.add_edge(lbl, d.name)
def graph(self):
out = """
digraph asm_graph {
size="80,50";
node [
fontsize = "16",
shape = "box"
];
"""
all_lbls = {}
for lbl in self.g.nodes():
if not lbl in self.blocs:
continue
b = self.blocs[lbl]
ir_txt = [str(lbl)]
for irs in b.irs:
for l in irs:
ir_txt.append(str(l))
ir_txt.append("")
ir_txt.append("")
all_lbls[id(lbl)] = "\l\\\n".join(ir_txt)
for l, v in all_lbls.items():
out += '%s [label="%s"];\n' % (l, v)
for a, b in self.g.edges():
out += '%s -> %s;\n' % (id(a), id(b))
out += '}'
return out
def remove_dead(self, b):
for ir, _, c_out in zip(b.irs, b.c_in, b.c_out):
#.........這裏部分代碼省略.........
示例8: ira_regs_ids
# 需要導入模塊: from miasm2.core.graph import DiGraph [as 別名]
# 或者: from miasm2.core.graph.DiGraph import add_node [as 別名]
class ira:
def ira_regs_ids(self):
"""Returns ids of all registers used in the IR"""
return self.arch.regs.all_regs_ids + [self.IRDst]
def sort_dst(self, todo, done):
out = set()
while todo:
dst = todo.pop()
if self.ExprIsLabel(dst):
done.add(dst)
elif isinstance(dst, ExprMem) or isinstance(dst, ExprInt):
done.add(dst)
elif isinstance(dst, ExprCond):
todo.add(dst.src1)
todo.add(dst.src2)
elif isinstance(dst, ExprId):
out.add(dst)
else:
done.add(dst)
return out
def dst_trackback(self, b):
dst = b.dst
todo = set([dst])
done = set()
for irs in reversed(b.irs):
if len(todo) == 0:
break
out = self.sort_dst(todo, done)
found = set()
follow = set()
for i in irs:
if not out:
break
for o in out:
if i.dst == o:
follow.add(i.src)
found.add(o)
for o in found:
out.remove(o)
for o in out:
if o not in found:
follow.add(o)
todo = follow
return done
def gen_graph(self, link_all=True):
"""
Gen irbloc digraph
@link_all: also gen edges to non present irblocs
"""
self.g = DiGraph()
for lbl, b in self.blocs.items():
# print 'add', lbl
self.g.add_node(lbl)
# dst = self.get_bloc_dst(b)
dst = self.dst_trackback(b)
# print "\tdst", dst
for d in dst:
if isinstance(d, ExprInt):
d = ExprId(self.symbol_pool.getby_offset_create(int(d.arg)))
if self.ExprIsLabel(d):
if d.name in self.blocs or link_all is True:
self.g.add_edge(lbl, d.name)
def graph(self):
"""Output the graphviz script"""
out = """
digraph asm_graph {
size="80,50";
node [
fontsize = "16",
shape = "box"
];
"""
all_lbls = {}
for lbl in self.g.nodes():
if lbl not in self.blocs:
continue
irb = self.blocs[lbl]
ir_txt = [str(lbl)]
for irs in irb.irs:
for l in irs:
ir_txt.append(str(l))
ir_txt.append("")
ir_txt.append("")
all_lbls[hash(lbl)] = "\l\\\n".join(ir_txt)
for l, v in all_lbls.items():
# print l, v
out += '%s [label="%s"];\n' % (l, v)
for a, b in self.g.edges():
# print 'edge', a, b, hash(a), hash(b)
out += "%s -> %s;\n" % (hash(a), hash(b))
out += "}"
return out
#.........這裏部分代碼省略.........
示例9: sort_dst
# 需要導入模塊: from miasm2.core.graph import DiGraph [as 別名]
# 或者: from miasm2.core.graph.DiGraph import add_node [as 別名]
class ira:
def sort_dst(self, todo, done):
out = set()
while todo:
dst = todo.pop()
if self.ExprIsLabel(dst):
done.add(dst)
elif isinstance(dst, ExprMem) or isinstance(dst, ExprInt):
done.add(dst)
elif isinstance(dst, ExprCond):
todo.add(dst.src1)
todo.add(dst.src2)
elif isinstance(dst, ExprId):
out.add(dst)
else:
done.add(dst)
return out
def dst_trackback(self, b):
dst = b.dst
todo = set([dst])
out = set()
done = set()
for irs in reversed(b.irs):
if len(todo) == 0:
break
out = self.sort_dst(todo, done)
found = set()
follow = set()
for i in irs:
if not out:
break
for o in out:
if i.dst == o:
follow.add(i.src)
found.add(o)
for o in found:
out.remove(o)
for o in out:
if o not in found:
follow.add(o)
todo = follow
out = self.sort_dst(todo, done)
return done
def gen_graph(self, link_all = True):
"""
Gen irbloc digraph
@link_all: also gen edges to non present irblocs
"""
self.g = DiGraph()
for lbl, b in self.blocs.items():
# print 'add', lbl
self.g.add_node(lbl)
# dst = self.get_bloc_dst(b)
dst = self.dst_trackback(b)
# print "\tdst", dst
for d in dst:
if isinstance(d, ExprInt):
d = ExprId(
self.symbol_pool.getby_offset_create(int(d.arg)))
if self.ExprIsLabel(d):
if d.name in self.blocs or link_all is True:
self.g.add_edge(lbl, d.name)
def graph(self):
"""Output the graphviz script"""
out = """
digraph asm_graph {
size="80,50";
node [
fontsize = "16",
shape = "box"
];
"""
all_lbls = {}
for lbl in self.g.nodes():
if lbl not in self.blocs:
continue
irb = self.blocs[lbl]
ir_txt = [str(lbl)]
for irs in irb.irs:
for l in irs:
ir_txt.append(str(l))
ir_txt.append("")
ir_txt.append("")
all_lbls[hash(lbl)] = "\l\\\n".join(ir_txt)
for l, v in all_lbls.items():
# print l, v
out += '%s [label="%s"];\n' % (l, v)
for a, b in self.g.edges():
# print 'edge', a, b, hash(a), hash(b)
out += '%s -> %s;\n' % (hash(a), hash(b))
out += '}'
return out
#.........這裏部分代碼省略.........