Пример #1
0
    def test_fill_df_given_while(self):
        as_tree = ast.parse(
            ms("""\
             z = 2           # 0th block
             while a < 3:    # 1st block
                 if a < 2:   # 2nd block
                      z = 2  # 3rd block
                 b = 2       # Eth block
             c = 3           # Fth block
            """))
        cfg_real = Cfg(as_tree)
        dom_tree = DominatorTree(cfg_real)
        dom_tree.fill_dominates()
        dom_tree.build_tree()
        dom_tree.fill_df()

        self.assertDfEqual(
            dom_tree,
            [],
            [2, 2],
            [2, 2],
            [5, 5],
            [2, 2],
            [],
        )
Пример #2
0
    def test_fill_dominate_given_if_else(self):
        as_tree = ast.parse(
            ms("""\
            a = 3           # 1st
            if a > 3:       #  |
                a = E       # 2nd
            else:           # 3rd
                z = F       #  |
            y = F           # Eth
            """))
        cfg_real = Cfg(as_tree)
        dom_tree = DominatorTree(cfg_real)
        dom_tree.fill_dominates()
        expected_dominator = {'0': [1, 2, 3], '1': [], '2': [], '3': []}

        self.assertDominatorEqual(dom_tree, expected_dominator)
Пример #3
0
    def test_build_dominator_tree_given_2_lvl(self):
        as_tree = ast.parse(
            ms("""\
             z = 2           # 0th block
             while a < 3:    # 1st block
                 if a < 2:   # 2nd block
                      z = 2  # 3rd block
                 b = 2       # Eth block
             c = 3           # Fth block
            """))
        cfg_real = Cfg(as_tree)
        dom_tree = DominatorTree(cfg_real)
        dom_tree.fill_dominates()
        dom_tree.build_tree()

        expected_block_list = build_blocks([5, 5, None], [4, 4, None],
                                           [3, 3, None], [6, 6, None],
                                           [2, 2, None], [1, 1, None],
                                           block_links={
                                               '5': [4],
                                               '4': [2, 3],
                                               '2': [1, 0],
                                               '1': [],
                                               '0': [],
                                               '3': []
                                           })

        self.assertBasicBlockListEqual(dom_tree.dominator_nodes,
                                       expected_block_list)
        pass
Пример #4
0
    def test_build_dominator_tree_given_1_lvl(self):
        as_tree = ast.parse(
            ms("""\
            a = 3           # 1st
            if a > 3:       #  |
                a = E       # 2nd
            else:           # 3rd
                z = F       #  |
            y = F           # Eth
            """))
        cfg_real = Cfg(as_tree)
        dom_tree = DominatorTree(cfg_real)
        dom_tree.fill_dominates()
        dom_tree.build_tree()

        expected_block_list = build_blocks([6, 6, None], [3, 3, None],
                                           [5, 5, None], [1, 2, None],
                                           block_links={
                                               '3': [1, 2, 0],
                                               '2': [],
                                               '1': [],
                                               '0': []
                                           })

        self.assertBasicBlockListEqual(dom_tree.dominator_nodes,
                                       expected_block_list)
Пример #5
0
    def test_fill_dominate_given_while(self):
        as_tree = ast.parse(
            ms("""\
             z = 2           # 0th block
             while a < 3:    # 1st block
                 if a < 2:   # 2nd block
                      z = 2  # 3rd block
                 b = 2       # Eth block
             c = 3           # Fth block
            """))
        cfg_real = Cfg(as_tree)
        dom_tree = DominatorTree(cfg_real)
        dom_tree.fill_dominates()
        expected_dominator = {
            '0': [1, 2, 3, 4, 5],
            '1': [2, 3, 4, 5],
            '2': [3, 4],
            '3': [],
            '4': [],
            '5': []
        }

        self.assertDominatorEqual(dom_tree, expected_dominator)
Пример #6
0
    def test_dominator_tree_given_13_blocks(self):
        """
                |---------> R
                |      /    |            \
                |     A <-- B            C
                |     |    / \           | \
                |     D <--   E  <-      |  ----  G
                |     |       |   |      F      /  \
                |     L       |   |      |      |  J
                |     \       |   |       \     |  |
                |       ----> H --|         I <----|
                |             \             /
                ----------------------> K ---/
        """
        blocks = self.build_blocks_arb(
            block_links={
                'A': ['B', 'C', 'H'],
                'B': ['D'],
                'C': ['B', 'D', 'F'],
                'D': ['E'],
                'E': ['G'],
                'F': ['G'],
                'G': ['F', 'M'],
                'H': ['I', 'J'],
                'I': ['L'],
                'J': ['K', 'L'],
                'K': ['L'],
                'L': ['M'],
                'M': ['A', 'L']
            })

        cfg_real = Cfg()
        cfg_real.block_list = blocks
        cfg_real.root = blocks[0]
        dom_tree = DominatorTree(cfg_real)
        dom_tree.fill_dominates()
        dom_tree.build_tree()

        expected_blocks = self.build_blocks_arb(
            block_links={
                'A': ['B', 'C', 'D', 'F', 'G', 'H', 'L', 'M'],
                'B': [],
                'C': [],
                'D': ['E'],
                'E': [],
                'F': [],
                'G': [],
                'H': ['I', 'J'],
                'I': [],
                'J': ['K'],
                'K': [],
                'L': [],
                'M': []
            })

        self.assertBasicBlockListEqual(dom_tree.dominator_nodes,
                                       expected_blocks)
Пример #7
0
    def test_fill_df_given_if_else(self):
        as_tree = ast.parse(
            ms("""\
            a = 3           # 1st
            if a > 3:       #  |
                a = E       # 2nd
            else:           # 3rd
                z = F       #  |
            y = F           # Eth
            """))
        cfg_real = Cfg(as_tree)
        dom_tree = DominatorTree(cfg_real)
        dom_tree.fill_dominates()
        dom_tree.build_tree()
        dom_tree.fill_df()

        self.assertDfEqual(dom_tree, [], [6, 6], [6, 6], [])
Пример #8
0
    def test_dominator_tree_given_complex_block(self):
        """
                 A
                 |
                 B   <------|
              /    \        |
             C      F       |
             |    /  \      |
             |    G   I     |
             |    \   /     |
             |      H       |
              \    /        |
                D-----------|
                |
                E
        """
        blocks = self.build_blocks_arb(
            block_links={
                'A': ['B'],
                'B': ['C', 'F'],
                'C': ['D'],
                'D': ['E', 'B'],
                'E': [],
                'F': ['G', 'I'],
                'G': ['H'],
                'H': ['D'],
                'I': ['H']
            })

        cfg_real = Cfg()
        cfg_real.block_list = blocks
        cfg_real.root = blocks[0]
        dom_tree = DominatorTree(cfg_real)
        dom_tree.fill_dominates()
        dom_tree.build_tree()

        expected_blocks = self.build_blocks_arb(
            block_links={
                'A': ['B'],
                'B': ['C', 'D', 'F'],
                'C': [],
                'D': ['E'],
                'E': [],
                'F': ['G', 'H', 'I'],
                'G': [],
                'H': [],
                'I': []
            })

        self.assertBasicBlockListEqual(dom_tree.dominator_nodes,
                                       expected_blocks)