def buildFlowGraph(self, f):
     fg = CGraph()
     func = self.pyew.functions[f]
     bbs = {}
     nodes = {}
     for bb in func.basic_blocks:
         instructions = set()
         bb_start = bb.instructions[0].offset
         end_offset = bb.instructions[-1].offset
         bb_end = end_offset + bb.instructions[-1].size
         
         buf = self.pyew.getBytes(bb_start, bb_end-bb_start)
         instructions = self.pyew.disassemble(buf=buf, baseoffset=bb_start, marker=False)
         instructions = instructions.split("\n")
         if instructions[-1] == "":
             del instructions[len(instructions)-1]
         
         bbs[bb_start] = instructions
         bbs[end_offset] = instructions
         
         n = CNode(str(bb.offset), data=instructions)
         fg.addNode(n)
         nodes[bb.offset] = n
     
     for bb in func.basic_blocks:
         next_head = self.pyew.NextHead(bb.instructions[-1].offset)
         for conn in bb.connections:
             a, b = conn
             if nodes.has_key(b) and nodes.has_key(bb.offset):
                 if len(bb.connections) == 1:
                     color = 0 # edge always
                 elif next_head == b:
                     color = 2 # edge false or unknown
                 else:
                     color = 1 # edge true
                 
                 fg.addEdge(nodes[bb.offset], nodes[b], value=color)
     
     return fg
Exemple #2
0
 def buildFlowGraph(self, f):
     fg = CGraph()
     func = self.pyew.functions[f]
     bbs = {}
     nodes = {}
     for bb in func.basic_blocks:
         instructions = set()
         bb_start = bb.instructions[0].offset
         end_offset = bb.instructions[-1].offset
         bb_end = end_offset + bb.instructions[-1].size
         
         buf = self.pyew.getBytes(bb_start, bb_end-bb_start)
         instructions = self.pyew.disassemble(buf=buf, baseoffset=bb_start, marker=False)
         instructions = instructions.split("\n")
         if instructions[-1] == "":
             del instructions[len(instructions)-1]
         
         bbs[bb_start] = instructions
         bbs[end_offset] = instructions
         
         n = CNode(str(bb.offset), data=instructions)
         fg.addNode(n)
         nodes[bb.offset] = n
     
     for bb in func.basic_blocks:
         next_head = self.pyew.NextHead(bb.instructions[-1].offset)
         for conn in bb.connections:
             a, b = conn
             if nodes.has_key(b) and nodes.has_key(bb.offset):
                 if len(bb.connections) == 1:
                     color = 0 # edge always
                 elif next_head == b:
                     color = 2 # edge false or unknown
                 else:
                     color = 1 # edge true
                 
                 fg.addEdge(nodes[bb.offset], nodes[b], value=color)
     
     return fg
Exemple #3
0
    def make_tree_for(self, group):
        """ Make a phylogenetic tree for the given group. """

        l = list(group)
        l.sort()
        s = str(l)
        if s in self.groups_done:
            if self.debug:
                print "Already clustered group found, skipping..."
            return None

        g = CGraph()
        g.letter = self.ascii_letters[self.last_letter]
        self.last_letter += 1

        group = list(group)
        dm = self.diff_matrix
        parent = None
        name = "New node %d"
        node_num = 0
        nodes = {}
        previous_node_name = node_name = None

        total_groups = len(group) - 1
        groups = 0
        for it in xrange(len(group) - 1):
            groups += 1
            node_num += 1
            # start by building a smaller difference matrix only for the samples
            # in this specific group
            dm = self.get_diff_matrix_for(group, dm)
            qm, lowest = self.get_q_matrix(dm)
            group = dm.keys()

            new_group = list(group)
            # Build a new node with the highest value found so far
            if node_name is not None:
                previous_node_name = node_name
            node_name = name % node_num
            n = CNode(node_name, shape="point")
            g.addNode(n)
            nodes[node_name] = n

            if parent is None:
                parent = n

            # Create the edges between the new node 'n' and the samples wih the
            # lowest Q matrix value
            if it != 0:
                lowest = None

            n1, n2 = self.get_neighbors(qm, group, lowest, previous_node_name)
            if not nodes.has_key(n1):
                nodes[n1] = CNode(n1)
            if not nodes.has_key(n2):
                nodes[n2] = CNode(n2)

            dfu = dgu = 0  #self.get_dfg_distances(n1, n2, dm)
            if not g.edgeExists(nodes[n1], n):
                g.addEdge(n, nodes[n1], check_dups=True, label=str(dfu))
            if not g.edgeExists(nodes[n2], n):
                g.addEdge(n, nodes[n2], check_dups=True, label=str(dgu))
            if n1 in new_group:
                new_group.remove(n1)
            if n2 in new_group:
                new_group.remove(n2)
            new_group.append(node_name)

            dm = self.update_diff_matrix(node_name, n1, n2, new_group, dm)
            group = new_group

        self.groups_done.add(s)
        return g