def test_build_simpler_code(self):
     cfg, value_dep_graph = self._build_ssa('data/simple.armasm')
     print(write(cfg))
     print(write(value_dep_graph))
     self.assertTrue(
         self._is_connected_graph(value_dep_graph,
                                  value_dep_graph.nodes()[0]))
 def test_build_complex(self):
     instructions = TextDisassembleReader(
         self.ASM_2_PATH).read_instructions()
     cfg = ARMControlFlowGraph(instructions)
     cfg.build()
     print(write(cfg))
     self._assert_instructions_are_not_repeated(cfg, instructions)
     self.assertTrue(10 < len(cfg.edges()))
     self.assertTrue(9 < len(cfg.nodes()))
     self.assertTrue(3 < self._count_conditional_nodes(cfg))
 def test_build(self):
     """
     Build the dominator tree
     """
     instructions = TextDisassembleReader(self.ASM_PATH).read_instructions()
     graph_builder = ARMControlFlowGraph(instructions)
     graph_builder.build()
     dom_tree = build_dominator_tree(graph_builder.cfg,
                                     graph_builder.root_node)
     self.assertTrue(7 < len(dom_tree.edges()))
     self.assertTrue(8 < len(dom_tree.nodes()))
     print(write(dom_tree))
    def test_build_simple(self):
        instructions = TextDisassembleReader(self.ASM_PATH).read_instructions()
        cfg = ARMControlFlowGraph(instructions)
        cfg.build()

        print(write(cfg))
        self.assertTrue(18 < len(cfg.edges()))
        self.assertTrue(15 < len(cfg.nodes()))
        self.assertEqual(2, self._count_conditional_nodes(cfg))
        # This one is to catch a bug
        self.assertEqual(0, len(cfg.root_node.instructions))
        self._assert_instructions_are_not_repeated(cfg, instructions)
 def test_remove_conditionals(self):
     instructions = TextDisassembleReader(self.ASM_PATH).read_instructions()
     cfg = ARMControlFlowGraph(instructions)
     cfg.build()
     d = cfg.get_dict_nodes()
     cfg.remove_conditionals()
     print(write(cfg))
     for n in cfg:
         self.assertNotEqual(CFGBlock.COND, n.kind)
     self.assertTrue(cfg.has_edge((d[6], d[7])))
     self.assertTrue(cfg.has_edge((d[6], d[16])))
     self.assertTrue(cfg.has_edge((d[13], d[14])))
     self.assertTrue(cfg.has_edge((d[13], d[17])))
 def test_dominance_frontier(self):
     graph, instructions = self._build_cfg_with_dominators(
         'data/dissasembly.armasm')
     build_dominance_frontier(graph)
     print(write(graph))
     d = graph.get_dict_nodes()
     for n in graph:
         print('<< {} >> - {}'.format(n, n.dom_frontier))
     # The root as no frontier
     self.assertEqual(0, len(d[1].dom_frontier))
     # A random node's frontier pick to evaluate
     self.assertTrue(d[2] in d[15].dom_frontier)
     self.assertTrue(d[10] in d[15].dom_frontier)
     # A node who's frontier contains himself
     self.assertTrue(d[10] in d[10].dom_frontier)
    def test_phi_node_placement(self):
        graph, instructions = self._build_cfg_with_dominators(
            'data/dissasembly.armasm', self, remove_conditionals=True)
        build_dominance_frontier(graph)
        place_phi_nodes(graph)

        d = graph.get_dict_nodes()
        # Nodes with phi functions
        self.assertTrue(len(d[10].phi_functions) > 0)
        #        self.assertTrue(len(d[2].phi_functions) > 0)
        # Nodes without phi functions
        self.assertTrue(len(d[4].phi_functions) == 0)
        self.assertTrue(len(d[6].phi_functions) == 0)
        self.assertTrue(len(d[12].phi_functions) == 0)

        print(write(graph))
 def test_build(self):
     cfg, value_dep_graph = self._build_ssa('data/dissasembly.armasm')
     print(write(cfg))
     print(write(value_dep_graph))
     self.assertTrue(value_dep_graph is not None)