about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--miasm2/arch/x86/sem.py11
-rw-r--r--miasm2/jitter/jitload.py4
-rw-r--r--test/arch/x86/unit/asm_test.py78
-rw-r--r--test/arch/x86/unit/mn_float.py22
-rw-r--r--test/arch/x86/unit/mn_strings.py48
-rw-r--r--test/test_all.py2
6 files changed, 156 insertions, 9 deletions
diff --git a/miasm2/arch/x86/sem.py b/miasm2/arch/x86/sem.py
index 46302de3..781b3321 100644
--- a/miasm2/arch/x86/sem.py
+++ b/miasm2/arch/x86/sem.py
@@ -3377,17 +3377,14 @@ class ir_x86_16(ir):
             if e.dst == zf:
                 zf_val = e.src
 
+        cond_dec = ExprCond(c_reg - ExprInt_from(c_reg, 1), ExprInt1(0), ExprInt1(1))
         # end condition
         if zf_val is None:
-            c_cond = ExprCond(c_reg, ExprInt1(0), ExprInt1(1))
+            c_cond = cond_dec
         elif instr.additional_info.g1.value & 2:  # REPNE
-            # c_cond = ExprCond(c_reg, ExprInt1(0), ExprInt1(1)) | (zf_val)
-            c_cond = ExprCond(c_reg, ExprInt1(0), ExprInt1(1)) | (zf)
+            c_cond = cond_dec | zf
         elif instr.additional_info.g1.value & 4:  # REP
-            # c_cond = ExprCond(c_reg, ExprInt1(0), ExprInt1(1)) |
-            # (zf_val^ExprInt32(1))
-            c_cond = ExprCond(
-                c_reg, ExprInt1(0), ExprInt1(1)) | (zf ^ ExprInt1(1))
+            c_cond = cond_dec | (zf ^ ExprInt1(1))
 
         # gen while
         lbl_do = ExprId(self.gen_label(), instr.mode)
diff --git a/miasm2/jitter/jitload.py b/miasm2/jitter/jitload.py
index 285c41dd..6ff9f0f8 100644
--- a/miasm2/jitter/jitload.py
+++ b/miasm2/jitter/jitload.py
@@ -348,8 +348,8 @@ def vm_load_pe(vm, fname, align_s=True, load_hdr=True,
 
     if aligned:
         if load_hdr:
-            hdr_len = max(0x200, e.NThdr.sectionalignment)
-            min_len = min(e.SHList[0].addr, hdr_len)
+            hdr_len = max(0x200, e.NThdr.sizeofheaders)
+            min_len = min(e.SHList[0].addr, 0x1000)#e.NThdr.sizeofheaders)
             pe_hdr = e.content[:hdr_len]
             pe_hdr = pe_hdr + min_len * "\x00"
             pe_hdr = pe_hdr[:min_len]
diff --git a/test/arch/x86/unit/asm_test.py b/test/arch/x86/unit/asm_test.py
new file mode 100644
index 00000000..401b344a
--- /dev/null
+++ b/test/arch/x86/unit/asm_test.py
@@ -0,0 +1,78 @@
+#! /usr/bin/env python
+import sys
+import os
+
+from miasm2.core.cpu import parse_ast
+from miasm2.arch.x86.arch import mn_x86, base_expr, variable
+from miasm2.core import parse_asm
+from miasm2.expression.expression import *
+from miasm2.core import asmbloc
+from elfesteem.strpatchwork import StrPatchwork
+from miasm2.analysis.machine import Machine
+from miasm2.jitter.csts import *
+from pdb import pm
+
+
+filename = os.environ.get('PYTHONSTARTUP')
+if filename and os.path.isfile(filename):
+    execfile(filename)
+
+
+reg_and_id = dict(mn_x86.regs.all_regs_ids_byname)
+
+
+def my_ast_int2expr(a):
+    return ExprInt32(a)
+
+
+def my_ast_id2expr(t):
+    return reg_and_id.get(t, ExprId(t, size=32))
+
+my_var_parser = parse_ast(my_ast_id2expr, my_ast_int2expr)
+base_expr.setParseAction(my_var_parser)
+
+
+class Asm_Test(object):
+    def __init__(self):
+        self.myjit = Machine("x86_32").jitter()
+        self.myjit.init_stack()
+
+        self.myjit.jit.log_regs = False
+        self.myjit.jit.log_mn = False
+
+
+    def __call__(self):
+        self.asm()
+        self.run()
+        self.check()
+
+
+    def asm(self):
+        blocs, symbol_pool = parse_asm.parse_txt(mn_x86, 32, self.TXT,
+                                                 symbol_pool = self.myjit.ir_arch.symbol_pool)
+        # fix shellcode addr
+        symbol_pool.set_offset(symbol_pool.getby_name("main"), 0x0)
+        s = StrPatchwork()
+        resolved_b, patches = asmbloc.asm_resolve_final(
+            mn_x86, '32', blocs[0], symbol_pool)
+        for offset, raw in patches.items():
+            s[offset] = raw
+
+        s = str(s)
+        self.assembly = s
+
+    def run(self):
+        run_addr = 0
+        self.myjit.vm.vm_add_memory_page(run_addr, PAGE_READ | PAGE_WRITE, self.assembly)
+
+        self.myjit.vm_push_uint32_t(0x1337beef)
+
+        self.myjit.add_breakpoint(0x1337beef, lambda x:False)
+
+        self.myjit.init_run(run_addr)
+        self.myjit.continue_run()
+
+        assert(self.myjit.pc == 0x1337beef)
+
+    def check(self):
+        raise NotImplementedError('abstract method')
diff --git a/test/arch/x86/unit/mn_float.py b/test/arch/x86/unit/mn_float.py
new file mode 100644
index 00000000..863e86c3
--- /dev/null
+++ b/test/arch/x86/unit/mn_float.py
@@ -0,0 +1,22 @@
+#! /usr/bin/env python
+from asm_test import Asm_Test
+
+
+class Test_FADD(Asm_Test):
+    TXT = '''
+    main:
+       ; test float
+       PUSH 0
+       FLD1
+       FLD1
+       FADD ST, ST(1)
+       FIST  DWORD PTR [ESP]
+       POP  EAX
+       RET
+    '''
+    def check(self):
+        assert(self.myjit.cpu.EAX == 2)
+
+
+if __name__ == "__main__":
+    [test()() for test in [Test_FADD]]
diff --git a/test/arch/x86/unit/mn_strings.py b/test/arch/x86/unit/mn_strings.py
new file mode 100644
index 00000000..db52fa74
--- /dev/null
+++ b/test/arch/x86/unit/mn_strings.py
@@ -0,0 +1,48 @@
+#! /usr/bin/env python
+from asm_test import Asm_Test
+
+class Test_SCAS(Asm_Test):
+    MYSTRING = "test string"
+    TXT = '''
+    main:
+       LEA EDI, DWORD PTR [mystr]
+       XOR  ECX, ECX
+       DEC  ECX
+       REPNE SCASB
+       NOT ECX
+       DEC ECX
+       RET
+
+    mystr:
+    .string "%s"
+    ''' % MYSTRING
+
+    def check(self):
+        assert(self.myjit.cpu.ECX == len(self.MYSTRING))
+        assert(self.myjit.cpu.EDI == self.myjit.ir_arch.symbol_pool.getby_name('mystr').offset + len(self.MYSTRING)+1)
+
+
+class Test_MOVS(Asm_Test):
+    MYSTRING = "test string"
+    TXT = '''
+    main:
+       LEA ESI, DWORD PTR [mystr]
+       LEA EDI, DWORD PTR [buffer]
+       MOV ECX, %d
+       REPE  MOVSB
+       RET
+
+    mystr:
+    .string "%s"
+    buffer:
+    .string "%s"
+    ''' % (len(MYSTRING), MYSTRING, " "*len(MYSTRING))
+
+    def check(self):
+        assert(self.myjit.cpu.ECX == 0)
+        assert(self.myjit.cpu.EDI == self.myjit.ir_arch.symbol_pool.getby_name('buffer').offset + len(self.MYSTRING))
+        assert(self.myjit.cpu.ESI == self.myjit.ir_arch.symbol_pool.getby_name('mystr').offset + len(self.MYSTRING))
+
+
+if __name__ == "__main__":
+    [test()() for test in [Test_SCAS, Test_MOVS]]
diff --git a/test/test_all.py b/test/test_all.py
index 784196f6..f931b776 100644
--- a/test/test_all.py
+++ b/test/test_all.py
@@ -24,6 +24,8 @@ all_tests = {
         "architecture": [
             ["arch/x86/arch.py"],
             ["arch/x86/sem.py"],
+            ["arch/x86/unit/mn_strings.py"],
+            ["arch/x86/unit/mn_float.py"],
             ["arch/arm/arch.py"],
             ["arch/arm/sem.py"],
             ["arch/msp430/arch.py"],