about summary refs log tree commit diff stats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/ir/reduce_graph.py531
-rwxr-xr-xtest/test_all.py1
2 files changed, 532 insertions, 0 deletions
diff --git a/test/ir/reduce_graph.py b/test/ir/reduce_graph.py
new file mode 100644
index 00000000..d8b78c90
--- /dev/null
+++ b/test/ir/reduce_graph.py
@@ -0,0 +1,531 @@
+"""Regression test module for DependencyGraph"""
+from pdb import pm
+
+from miasm2.expression.expression import ExprId, ExprInt, ExprAff, ExprCond, \
+    ExprLoc, LocKey
+
+from miasm2.core.locationdb import LocationDB
+from miasm2.ir.analysis import ira
+from miasm2.ir.ir import IRBlock, AssignBlock, IRCFG
+from miasm2.analysis.data_flow import merge_blocks
+
+loc_db = LocationDB()
+
+
+A = ExprId("a", 32)
+B = ExprId("b", 32)
+C = ExprId("c", 32)
+D = ExprId("d", 32)
+R = ExprId("r", 32)
+
+A_INIT = ExprId("a_init", 32)
+B_INIT = ExprId("b_init", 32)
+C_INIT = ExprId("c_init", 32)
+D_INIT = ExprId("d_init", 32)
+
+IRDst = ExprId("IRDst", 32)
+PC = ExprId("pc", 32)
+SP = ExprId("sp", 32)
+
+CST0 = ExprInt(0x0, 32)
+CST1 = ExprInt(0x1, 32)
+CST2 = ExprInt(0x2, 32)
+CST3 = ExprInt(0x3, 32)
+CST22 = ExprInt(0x22, 32)
+CST23 = ExprInt(0x23, 32)
+CST24 = ExprInt(0x24, 32)
+CST33 = ExprInt(0x33, 32)
+CST35 = ExprInt(0x35, 32)
+CST37 = ExprInt(0x37, 32)
+
+LBL0 = loc_db.add_location("lbl0", 0)
+LBL1 = loc_db.add_location("lbl1", 1)
+LBL2 = loc_db.add_location("lbl2", 2)
+LBL3 = loc_db.add_location("lbl3", 3)
+LBL4 = loc_db.add_location("lbl4", 4)
+LBL5 = loc_db.add_location("lbl5", 5)
+LBL6 = loc_db.add_location("lbl6", 6)
+
+
+class Regs(object):
+
+    """Fake registers for tests """
+    regs_init = {A: A_INIT, B: B_INIT, C: C_INIT, D: D_INIT}
+    all_regs_ids = [A, B, C, D, SP, PC, R]
+
+
+class Arch(object):
+
+    """Fake architecture for tests """
+    regs = Regs()
+
+    def getpc(self, attrib):
+        return PC
+
+    def getsp(self, attrib):
+        return SP
+
+
+class IRATest(ira):
+
+    """Fake IRA class for tests"""
+
+    def __init__(self, loc_db=None):
+        arch = Arch()
+        super(IRATest, self).__init__(arch, 32, loc_db)
+        self.IRDst = IRDst
+        self.ret_reg = R
+
+    def get_out_regs(self, _):
+        return set([self.ret_reg, self.sp])
+
+
+def gen_irblock(label, exprs_list):
+    """ Returns an IRBlock.
+    Used only for tests purpose
+    """
+    irs = []
+    for exprs in exprs_list:
+        if isinstance(exprs, AssignBlock):
+            irs.append(exprs)
+        else:
+            irs.append(AssignBlock(exprs))
+
+    irbl = IRBlock(label, irs)
+    return irbl
+
+
+
+
+############# Tests #############
+IRA = IRATest(loc_db)
+
+
+########## G1 ##########
+# Input
+G1 = IRA.new_ircfg()
+
+G1_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(B, C),
+            ExprAff(IRDst, ExprLoc(LBL1, 32)),
+        ]
+    ]
+)
+
+G1_IRB1 = gen_irblock(
+    LBL1,
+    [
+        [
+            ExprAff(IRDst, ExprLoc(LBL2, 32)),
+        ]
+    ]
+)
+
+G1_IRB2 = gen_irblock(
+    LBL2,
+    [
+        [
+            ExprAff(A, B),
+            ExprAff(IRDst, C),
+        ]
+    ]
+)
+
+for irb in [G1_IRB0, G1_IRB1, G1_IRB2]:
+    G1.add_irblock(irb)
+
+# Result
+G1_RES = IRA.new_ircfg()
+
+G1_RES_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(B, C),
+        ],
+        [
+            ExprAff(A, B),
+            ExprAff(IRDst, C),
+        ]
+
+    ]
+)
+
+
+
+for irb in [G1_RES_IRB0]:
+    G1_RES.add_irblock(irb)
+
+
+
+def cmp_ir_graph(g1, g2):
+    assert g1.blocks.items() == g2.blocks.items()
+    assert set(g1.edges()) == set(g2.edges())
+
+
+
+########## G2 ##########
+# Input
+
+G2 = IRA.new_ircfg()
+
+G2_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(IRDst, ExprLoc(LBL1, 32)),
+        ]
+    ]
+)
+
+G2_IRB1 = gen_irblock(
+    LBL1,
+    [
+        [
+            ExprAff(A, C),
+            ExprAff(IRDst, C),
+        ]
+    ]
+)
+
+for irb in [G2_IRB0, G2_IRB1]:
+    G2.add_irblock(irb)
+
+
+# Result
+G2_RES = IRA.new_ircfg()
+
+G2_RES_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(A, C),
+            ExprAff(IRDst, C),
+        ]
+    ]
+)
+
+
+for irb in [G2_RES_IRB0]:
+    G2_RES.add_irblock(irb)
+
+
+########## G3 ##########
+# Input
+
+G3 = IRA.new_ircfg()
+
+G3_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(IRDst, ExprLoc(LBL1, 32)),
+        ]
+    ]
+)
+
+G3_IRB1 = gen_irblock(
+    LBL1,
+    [
+        [
+            ExprAff(A, C),
+            ExprAff(IRDst, ExprLoc(LBL2, 32)),
+        ]
+    ]
+)
+
+G3_IRB2 = gen_irblock(
+    LBL2,
+    [
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, C),
+        ]
+    ]
+)
+
+
+for irb in [G3_IRB0, G3_IRB1, G3_IRB2]:
+    G3.add_irblock(irb)
+
+
+# Result
+G3_RES = IRA.new_ircfg()
+
+G3_RES_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(A, C),
+        ],
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, C),
+        ]
+    ]
+)
+
+
+for irb in [G3_RES_IRB0]:
+    G3_RES.add_irblock(irb)
+
+
+
+
+########## G4 ##########
+# Input
+
+G4 = IRA.new_ircfg()
+
+G4_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(IRDst, ExprLoc(LBL1, 32)),
+        ]
+    ]
+)
+
+G4_IRB1 = gen_irblock(
+    LBL1,
+    [
+        [
+            ExprAff(A, C),
+            ExprAff(IRDst, ExprLoc(LBL2, 32)),
+        ]
+    ]
+)
+
+G4_IRB2 = gen_irblock(
+    LBL2,
+    [
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, ExprLoc(LBL1, 32)),
+        ]
+    ]
+)
+
+
+for irb in [G4_IRB0, G4_IRB1, G4_IRB2]:
+    G4.add_irblock(irb)
+
+
+# Result
+G4_RES = IRA.new_ircfg()
+
+G4_RES_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(A, C),
+        ],
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, ExprLoc(LBL0, 32)),
+        ]
+    ]
+)
+
+
+for irb in [G4_RES_IRB0 ]:
+    G4_RES.add_irblock(irb)
+
+
+
+########## G5 ##########
+# Input
+
+G5 = IRA.new_ircfg()
+
+G5_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(IRDst, ExprLoc(LBL1, 32)),
+        ]
+    ]
+)
+
+G5_IRB1 = gen_irblock(
+    LBL1,
+    [
+        [
+            ExprAff(A, C),
+            ExprAff(IRDst, ExprLoc(LBL2, 32)),
+        ]
+    ]
+)
+
+G5_IRB2 = gen_irblock(
+    LBL2,
+    [
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, ExprCond(C, ExprLoc(LBL1, 32), ExprLoc(LBL3, 32))),
+        ]
+    ]
+)
+
+
+G5_IRB3 = gen_irblock(
+    LBL3,
+    [
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, C),
+        ]
+    ]
+)
+
+
+for irb in [G5_IRB0, G5_IRB1, G5_IRB2, G5_IRB3]:
+    G5.add_irblock(irb)
+
+
+# Result
+G5_RES = IRA.new_ircfg()
+
+G5_RES_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(A, C),
+        ],
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, ExprCond(C, ExprLoc(LBL0, 32), ExprLoc(LBL3, 32))),
+        ]
+    ]
+)
+
+
+G5_RES_IRB3 = gen_irblock(
+    LBL3,
+    [
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, C),
+        ]
+    ]
+)
+
+for irb in [G5_RES_IRB0, G5_RES_IRB3 ]:
+    G5_RES.add_irblock(irb)
+
+
+
+########## G6 ##########
+# Input
+
+G6 = IRA.new_ircfg()
+
+G6_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(IRDst, ExprCond(C, ExprLoc(LBL1, 32), ExprLoc(LBL2, 32))),
+        ]
+    ]
+)
+
+G6_IRB1 = gen_irblock(
+    LBL1,
+    [
+        [
+            ExprAff(IRDst, ExprLoc(LBL3, 32)),
+        ]
+    ]
+)
+
+G6_IRB2 = gen_irblock(
+    LBL2,
+    [
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, D),
+        ]
+    ]
+)
+
+
+G6_IRB3 = gen_irblock(
+    LBL3,
+    [
+        [
+            ExprAff(A, D),
+            ExprAff(IRDst, ExprLoc(LBL3, 32)),
+        ]
+    ]
+)
+
+
+for irb in [G6_IRB0, G6_IRB1, G6_IRB2, G6_IRB3]:
+    G6.add_irblock(irb)
+
+
+# Result
+G6_RES = IRA.new_ircfg()
+
+G6_RES_IRB0 = gen_irblock(
+    LBL0,
+    [
+        [
+            ExprAff(IRDst, ExprCond(C, ExprLoc(LBL3, 32), ExprLoc(LBL2, 32))),
+        ]
+    ]
+)
+
+
+G6_RES_IRB2 = gen_irblock(
+    LBL2,
+    [
+        [
+            ExprAff(D, A),
+            ExprAff(IRDst, D),
+        ]
+    ]
+)
+
+
+G6_RES_IRB3 = gen_irblock(
+    LBL3,
+    [
+        [
+            ExprAff(A, D),
+            ExprAff(IRDst, ExprLoc(LBL3, 32)),
+        ]
+    ]
+)
+
+
+for irb in [G6_RES_IRB0, G6_RES_IRB2, G6_RES_IRB3  ]:
+    G6_RES.add_irblock(irb)
+
+
+
+################# Tests
+
+
+for i, (g_test, g_ref) in enumerate(
+        [
+            (G1, G1_RES),
+            (G2, G2_RES),
+            (G3, G3_RES),
+            (G4, G4_RES),
+            (G5, G5_RES),
+            (G6, G6_RES),
+        ], 1):
+
+    heads = g_test.heads()
+    print '*'*10, 'Test', i, "*"*10
+    open('test_in_%d.dot' % i, 'w').write(g_test.dot())
+    merge_blocks(g_test, heads)
+    open('test_out_%d.dot' % i, 'w').write(g_test.dot())
+    open('test_ref_%d.dot' % i, 'w').write(g_ref.dot())
+
+    cmp_ir_graph(g_test, g_ref)
+    print '\t', 'OK'
diff --git a/test/test_all.py b/test/test_all.py
index 1c521ab0..665fc3a5 100755
--- a/test/test_all.py
+++ b/test/test_all.py
@@ -266,6 +266,7 @@ testset += RegressionTest(["test_chandler.py"], base_dir="expr_type",
 ## IR
 for script in ["symbexec.py",
                "ir.py",
+               "reduce_graph.py"
                ]:
     testset += RegressionTest([script], base_dir="ir")