about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFabrice Desclaux <fabrice.desclaux@cea.fr>2017-04-14 20:13:55 +0200
committerFabrice Desclaux <fabrice.desclaux@cea.fr>2017-04-21 11:05:06 +0200
commit102ad42976e7fcae3c67a21b61d0fe9294eb1fc4 (patch)
treea2059a53e0abed5ff8de66a28efc6dfcde9f1152
parent9b0ce9e798941e700b166da43e100f04f12df05f (diff)
downloadmiasm-102ad42976e7fcae3c67a21b61d0fe9294eb1fc4.tar.gz
miasm-102ad42976e7fcae3c67a21b61d0fe9294eb1fc4.zip
Example: add irblock simplification example
-rw-r--r--example/disasm/full.py17
-rw-r--r--example/samples/x86_32_dead.S15
-rwxr-xr-xtest/test_all.py9
3 files changed, 36 insertions, 5 deletions
diff --git a/example/disasm/full.py b/example/disasm/full.py
index b919310a..33903282 100644
--- a/example/disasm/full.py
+++ b/example/disasm/full.py
@@ -9,6 +9,7 @@ from miasm2.expression.expression import ExprId
 from miasm2.core.interval import interval
 from miasm2.analysis.machine import Machine
 from miasm2.analysis.data_flow import dead_simp, DiGraphDefUse, ReachingDefinitions
+from miasm2.expression.simplifications import expr_simp
 
 log = logging.getLogger("dis")
 console_handler = logging.StreamHandler()
@@ -43,7 +44,7 @@ parser.add_argument('-z', "--dis-nulstart-block", action="store_true",
 parser.add_argument('-l', "--dontdis-retcall", action="store_true",
                     help="If set, disassemble only call destinations")
 parser.add_argument('-s', "--simplify", action="store_true",
-                    help="Use the liveness analysis pass")
+                    help="Apply simplifications rules (liveness, graph simplification, ...)")
 parser.add_argument('-o', "--shiftoffset", default=None,
                     type=lambda x: int(x, 0),
                     help="Shift input binary by an offset")
@@ -210,7 +211,7 @@ if args.gen_ir:
     for label, block in ir_arch_a.blocks.iteritems():
         print block
 
-    if args.simplify:
+    if args.simplify > 0:
         dead_simp(ir_arch_a)
 
     if args.defuse:
@@ -221,3 +222,15 @@ if args.gen_ir:
     open('graph_irflow.dot', 'w').write(out)
     out = ir_arch.graph.dot()
     open('graph_irflow_raw.dot', 'w').write(out)
+
+    if args.simplify > 1:
+        ir_arch_a.simplify(expr_simp)
+        modified = True
+        while modified:
+            modified = False
+            modified |= dead_simp(ir_arch_a)
+            modified |= ir_arch_a.remove_empty_assignblks()
+            modified |= ir_arch_a.remove_jmp_blocks()
+            modified |= ir_arch_a.merge_blocks()
+
+        open('graph_irflow_reduced.dot', 'w').write(ir_arch_a.graph.dot())
diff --git a/example/samples/x86_32_dead.S b/example/samples/x86_32_dead.S
new file mode 100644
index 00000000..e1130842
--- /dev/null
+++ b/example/samples/x86_32_dead.S
@@ -0,0 +1,15 @@
+main:
+	MOV     ECX, ECX
+	INC     ECX
+	CMP     ECX, 0
+	JZ      lbl0
+	INC     EAX
+lbl0:
+	DEC     EAX
+	JMP     lbl1
+lbl1:
+	MOV     EAX, 3
+	JMP     lbl2
+lbl2:
+	MOV     EAX, 4
+	RET
diff --git a/test/test_all.py b/test/test_all.py
index d2c3e5e2..0cc50d03 100755
--- a/test/test_all.py
+++ b/test/test_all.py
@@ -432,6 +432,7 @@ test_x86_64 = ExampleShellcode(["x86_64", "x86_64.S", "demo_x86_64.bin",
 test_x86_32_if_reg = ExampleShellcode(['x86_32', 'x86_32_if_reg.S', "x86_32_if_reg.bin"])
 test_x86_32_seh = ExampleShellcode(["x86_32", "x86_32_seh.S", "x86_32_seh.bin",
                                     "--PE"])
+test_x86_32_dead = ExampleShellcode(['x86_32', 'x86_32_dead.S', "x86_32_dead.bin"])
 
 test_human = ExampleShellcode(["x86_64", "human.S", "human.bin"])
 
@@ -449,7 +450,7 @@ testset += test_mips32l
 testset += test_x86_64
 testset += test_x86_32_if_reg
 testset += test_x86_32_seh
-
+testset += test_x86_32_dead
 testset += test_human
 
 class ExampleDisassembler(Example):
@@ -480,9 +481,9 @@ class ExampleDisasmFull(ExampleDisassembler):
 
     def __init__(self, *args, **kwargs):
         super(ExampleDisasmFull, self).__init__(*args, **kwargs)
-        self.command_line = ["full.py", "-g", "-s", "-d", "-m"] + self.command_line
+        self.command_line = ["full.py", "-g", "-ss", "-d", "-m"] + self.command_line
         self.products += ["graph_defuse.dot", "graph_execflow.dot",
-                          "graph_irflow.dot", "graph_irflow_raw.dot", "lines.dot"]
+                          "graph_irflow.dot", "graph_irflow_raw.dot", "lines.dot", "graph_irflow_reduced.dot"]
 
 
 testset += ExampleDisasmFull(["arml", Example.get_sample("demo_arm_l.bin"),
@@ -519,6 +520,8 @@ testset += ExampleDisasmFull(["x86_32", os.path.join("..", "..", "test",
                                                      "arch", "x86", "qemu",
                                                      "test-i386"),
                               "func_iret"])
+testset += ExampleDisasmFull(["x86_32", Example.get_sample("x86_32_dead.bin"),
+                              "0"], depends=[test_x86_32_dead])
 
 
 ## Expression