示例#1
0
    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
示例#2
0
def gen_block_data_flow_graph(ir_arch, ad, block_flow_cb):
    for irblock in ir_arch.blocks.values():
        print irblock

    ir_arch.dead_simp()

    irblock_0 = None
    for irblock in ir_arch.blocks.values():
        if irblock.label.offset == ad:
            irblock_0 = irblock
            break
    assert (irblock_0 is not None)
    flow_graph = DiGraph()
    flow_graph.node2str = lambda n: node2str(flow_graph, n)

    for irblock in ir_arch.blocks.values():
        block_flow_cb(ir_arch, flow_graph, irblock)

    for irblock in ir_arch.blocks.values():
        print irblock
        print 'IN', [str(x) for x in irblock.in_nodes]
        print 'OUT', [str(x) for x in irblock.out_nodes]

    print '*' * 20, 'interblock', '*' * 20
    inter_bloc_flow(ir_arch, flow_graph, irblock_0.label)

    # from graph_qt import graph_qt
    # graph_qt(flow_graph)
    open('data.dot', 'w').write(flow_graph.dot())
示例#3
0
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
示例#4
0
 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
示例#5
0
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
示例#6
0
    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
示例#7
0
    def __init__(self, segments, abicls, machine):
        self.segments = segments
        self.abicls = abicls

        self.input_reg = {}
        self.output_reg = {}

        self._previous_addr = 0
        self._current_addr = 0
        self.paths = DiGraph()

        self.in_memory = {}
        self.out_memory = {}

        self.refs = {}

        self._ira = Machine(machine).ira()
        self._ptr_size = self._ira.sizeof_pointer() / 8
        self.sp = self._ira.sp.name
示例#8
0
 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)
示例#9
0
    def __init__(self, abicls, machine):
        self.abicls = abicls

        self.input_reg = {}
        self.output_reg = {}

        self._previous_addr = 0
        self._current_addr = 0
        self._instr_count = 0
        self._pending_call = []
        # Function addr -> list of information on calls
        self.function_calls = {}
        self.paths = DiGraph()

        self.in_memory = {}
        self.out_memory = {}

        self._ira = Machine(machine).ira()
        self._ptr_size = self._ira.sizeof_pointer() / 8
        self.sp = self._ira.sp.name
示例#10
0
def gen_bloc_data_flow_graph(
        ir_arch, in_str, ad):  # arch, attrib, pool_bin, bloc, symbol_pool):
    out_str = ""

    # ir_arch = ir_x86_32(symbol_pool)

    for irbloc in ir_arch.blocs.values():
        print irbloc

    ir_arch.gen_graph()
    ir_arch.dead_simp()

    irbloc_0 = None
    for irbloc in ir_arch.blocs.values():
        if irbloc.label.offset == ad:
            irbloc_0 = irbloc
            break
    assert (irbloc_0 is not None)
    flow_graph = DiGraph()
    flow_graph.node2str = lambda n: node2str(flow_graph, n)
    done = set()
    todo = set([irbloc_0.label])

    bloc2w = {}

    for irbloc in ir_arch.blocs.values():
        intra_bloc_flow_raw(ir_arch, flow_graph, irbloc)
        # intra_bloc_flow_symb(ir_arch, flow_graph, irbloc)

    for irbloc in ir_arch.blocs.values():
        print irbloc
        print 'IN', [str(x) for x in irbloc.in_nodes]
        print 'OUT', [str(x) for x in irbloc.out_nodes]

    print '*' * 20, 'interbloc', '*' * 20
    inter_bloc_flow(ir_arch, flow_graph, irbloc_0.label)

    # sys.path.append('/home/serpilliere/projet/m2_devel/miasm2/core')
    # from graph_qt import graph_qt
    # graph_qt(flow_graph)
    open('data.txt', 'w').write(flow_graph.dot())
示例#11
0
def gen_block_data_flow_graph(ir_arch, ad, block_flow_cb):
    for irblock in ir_arch.blocks.values():
        print irblock

    dead_simp(ir_arch)

    irblock_0 = None
    for irblock in ir_arch.blocks.values():
        loc_key = irblock.loc_key
        offset = ir_arch.loc_db.get_location_offset(loc_key)
        if offset == ad:
            irblock_0 = irblock
            break
    assert (irblock_0 is not None)
    flow_graph = DiGraph()
    flow_graph.node2str = lambda n: node2str(flow_graph, n)

    irb_in_nodes = {}
    irb_out_nodes = {}
    for label in ir_arch.blocks:
        irb_in_nodes[label] = {}
        irb_out_nodes[label] = {}

    for label, irblock in ir_arch.blocks.iteritems():
        block_flow_cb(ir_arch, flow_graph, irblock, irb_in_nodes[label],
                      irb_out_nodes[label])

    for label in ir_arch.blocks:
        print label
        print 'IN', [str(x) for x in irb_in_nodes[label]]
        print 'OUT', [str(x) for x in irb_out_nodes[label]]

    print '*' * 20, 'interblock', '*' * 20
    inter_block_flow(ir_arch, flow_graph, irblock_0.loc_key, irb_in_nodes,
                     irb_out_nodes)

    # from graph_qt import graph_qt
    # graph_qt(flow_graph)
    open('data.dot', 'w').write(flow_graph.dot())
示例#12
0
 def __init__(self, ab=[]):
     self.blocs = {}
     self.g = DiGraph()
     self.add_blocs(ab)
示例#13
0
 def as_graph(self):
     """Generates a Digraph of dependencies"""
     graph = DiGraph()
     for node_a, node_b in self.links:
         graph.add_edge(node_b, node_a)
     return graph