about summary refs log tree commit diff stats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/arch/x86/unit/mn_seh.py66
-rw-r--r--test/test_all.py1
2 files changed, 67 insertions, 0 deletions
diff --git a/test/arch/x86/unit/mn_seh.py b/test/arch/x86/unit/mn_seh.py
new file mode 100644
index 00000000..12750b05
--- /dev/null
+++ b/test/arch/x86/unit/mn_seh.py
@@ -0,0 +1,66 @@
+#! /usr/bin/env python
+import sys
+
+from miasm2.os_dep.win_api_x86_32_seh import fake_seh_handler, build_teb, \
+    set_win_fs_0, return_from_exception, EXCEPTION_PRIV_INSTRUCTION, \
+    return_from_seh, FS_0_AD, DEFAULT_SEH
+from miasm2.os_dep.win_32_structs import ContextException
+
+from asm_test import Asm_Test_32
+
+from pdb import pm
+
+class Test_SEH(Asm_Test_32):
+    """SEH Handling"""
+
+    @staticmethod
+    def deal_exception_priv(jitter):
+        print 'Exception Priv', hex(jitter.cpu.ESP)
+        pc = fake_seh_handler(jitter, EXCEPTION_PRIV_INSTRUCTION)
+        jitter.pc = pc
+        jitter.cpu.EIP = pc
+        return True
+
+    def init_machine(self):
+        super(Test_SEH, self).init_machine()
+        build_teb(self.myjit, FS_0_AD)
+        set_win_fs_0(self.myjit)
+        self.myjit.add_exception_handler((1 << 17),
+                                         Test_SEH.deal_exception_priv)
+        self.myjit.add_breakpoint(return_from_exception, return_from_seh)
+
+
+class Test_SEH_simple(Test_SEH):
+    TXT = '''
+    main:
+       XOR EAX, EAX
+       XOR EDX, EDX
+
+       PUSH handler
+       PUSH DWORD PTR FS:[EDX]
+       MOV DWORD PTR FS:[EDX], ESP
+
+       STI
+
+       MOV EBX, DWORD PTR [ESP]
+       MOV DWORD PTR FS:[EDX], EBX
+       ADD ESP, 0x8
+
+       RET
+
+    handler:
+       MOV ECX, DWORD PTR [ESP+0xC]
+       INC DWORD PTR [ECX+0x%08x]
+       MOV DWORD PTR [ECX+0x%08x], 0xcafebabe
+       XOR EAX, EAX
+       RET
+    ''' % (ContextException.get_offset("eip"),
+           ContextException.get_offset("eax"))
+
+    def check(self):
+        assert(self.myjit.cpu.EAX == 0xcafebabe)
+        assert(self.myjit.cpu.EBX == DEFAULT_SEH)
+
+
+if __name__ == "__main__":
+    [test(*sys.argv[1:])() for test in [Test_SEH_simple]]
diff --git a/test/test_all.py b/test/test_all.py
index c3e3c1fb..72975801 100644
--- a/test/test_all.py
+++ b/test/test_all.py
@@ -71,6 +71,7 @@ for script in ["x86/sem.py",
                "x86/unit/mn_pextr.py",
                "x86/unit/mn_pmovmskb.py",
                "x86/unit/mn_pushpop.py",
+               "x86/unit/mn_seh.py",
                "arm/arch.py",
                "arm/sem.py",
                "aarch64/unit/mn_ubfm.py",