diff options
| author | Camille Mougey <commial@gmail.com> | 2016-04-19 17:11:20 +0200 |
|---|---|---|
| committer | Camille Mougey <commial@gmail.com> | 2016-04-19 17:11:20 +0200 |
| commit | 79e03f015430386c3bae2f1ce56fbb9b12831536 (patch) | |
| tree | 84fa57aa0cc589dd7eb579a2d9927e611671e361 | |
| parent | 8fff094b010db48e661e926d19536457ca5f86c1 (diff) | |
| parent | ef5631fd7449f547011f9239870f69fcdd0d1f95 (diff) | |
| download | miasm-79e03f015430386c3bae2f1ce56fbb9b12831536.tar.gz miasm-79e03f015430386c3bae2f1ce56fbb9b12831536.zip | |
Merge pull request #353 from serpilliere/fix_pusha16
Fix pusha16
| -rw-r--r-- | miasm2/arch/x86/sem.py | 66 | ||||
| -rw-r--r-- | test/arch/x86/unit/asm_test.py | 59 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_daa.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_das.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_float.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_int.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_pcmpeq.py | 8 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_pextr.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_pinsr.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_pmaxu.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_pminu.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_pmovmskb.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_pshufb.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_psrl_psll.py | 6 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_punpck.py | 14 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_pushpop.py | 125 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_stack.py | 4 | ||||
| -rw-r--r-- | test/arch/x86/unit/mn_strings.py | 6 | ||||
| -rw-r--r-- | test/test_all.py | 1 |
19 files changed, 242 insertions, 87 deletions
diff --git a/miasm2/arch/x86/sem.py b/miasm2/arch/x86/sem.py index cc227819..f66570a7 100644 --- a/miasm2/arch/x86/sem.py +++ b/miasm2/arch/x86/sem.py @@ -1064,51 +1064,45 @@ def popfw(ir, instr): m2_expr.ExprAff(mRSP[instr.mode], mRSP[instr.mode] + m2_expr.ExprInt(2, mRSP[instr.mode].size))) return e, [] +pa_regs = [ + mRAX, mRCX, + mRDX, mRBX, + mRSP, mRBP, + mRSI, mRDI +] -def pushad(ir, instr): +def pusha_gen(ir, instr, size): e = [] - s = instr.v_opmode() - opmode, admode = s, instr.v_admode() - if not s in [16, 32, 64]: - raise ValueError('bad size stacker!') - - regs = [ - mRAX[instr.mode][:s], mRCX[instr.mode][ - :s], mRDX[instr.mode][:s], mRBX[instr.mode][:s], - mRSP[instr.mode][:s], mRBP[instr.mode][:s], - mRSI[instr.mode][:s], mRDI[instr.mode][:s]] - - for i in xrange(len(regs)): - c = mRSP[instr.mode][:s] + m2_expr.ExprInt(-(s / 8) * (i + 1), s) - e.append(m2_expr.ExprAff(m2_expr.ExprMem(c, s), regs[i])) - e.append(m2_expr.ExprAff(mRSP[instr.mode][:s], c)) + for i, reg in enumerate(pa_regs): + stk_ptr = mRSP[instr.mode] + m2_expr.ExprInt(-(reg[size].size / 8) * (i + 1), instr.mode) + e.append(m2_expr.ExprAff(m2_expr.ExprMem(stk_ptr, reg[size].size), reg[size])) + e.append(m2_expr.ExprAff(mRSP[instr.mode], stk_ptr)) return e, [] +def pusha(ir, instr): + return pusha_gen(ir, instr, 16) -def popad(ir, instr): +def pushad(ir, instr): + return pusha_gen(ir, instr, 32) + +def popa_gen(ir, instr, size): e = [] - s = instr.v_opmode() - opmode, admode = s, instr.v_admode() - if not s in [16, 32, 64]: - raise ValueError('bad size stacker!') - regs = [ - mRAX[instr.mode][:s], mRCX[instr.mode][ - :s], mRDX[instr.mode][:s], mRBX[instr.mode][:s], - mRSP[instr.mode][:s], mRBP[instr.mode][:s], - mRSI[instr.mode][:s], mRDI[instr.mode][:s]] - myesp = mRSP[instr.mode][:s] - regs.reverse() - for i in xrange(len(regs)): - if regs[i] == myesp: + for i, reg in enumerate(reversed(pa_regs)): + if reg == mRSP: continue - c = myesp + m2_expr.ExprInt_from(myesp, ((s / 8) * i)) - e.append(m2_expr.ExprAff(regs[i], m2_expr.ExprMem(c, s))) + stk_ptr = mRSP[instr.mode] + m2_expr.ExprInt((reg[size].size / 8) * i, instr.mode) + e.append(m2_expr.ExprAff(reg[size], m2_expr.ExprMem(stk_ptr, instr.mode))) - c = myesp + m2_expr.ExprInt_from(myesp, ((s / 8) * (i + 1))) - e.append(m2_expr.ExprAff(myesp, c)) + stk_ptr = mRSP[instr.mode] + m2_expr.ExprInt((instr.mode / 8) * (i + 1), instr.mode) + e.append(m2_expr.ExprAff(mRSP[instr.mode], stk_ptr)) return e, [] +def popa(ir, instr): + return popa_gen(ir, instr, 16) + +def popad(ir, instr): + return popa_gen(ir, instr, 32) def call(ir, instr, dst): e = [] @@ -4043,10 +4037,10 @@ mnemo_func = {'mov': mov, 'popfd': popfd, 'popfq': popfd, 'popfw': popfw, + 'pusha': pusha, 'pushad': pushad, - 'pusha': pushad, 'popad': popad, - 'popa': popad, + 'popa': popa, 'call': call, 'ret': ret, 'retf': retf, diff --git a/test/arch/x86/unit/asm_test.py b/test/arch/x86/unit/asm_test.py index bf609aa5..118a57b4 100644 --- a/test/arch/x86/unit/asm_test.py +++ b/test/arch/x86/unit/asm_test.py @@ -21,22 +21,38 @@ if filename and os.path.isfile(filename): reg_and_id = dict(mn_x86.regs.all_regs_ids_byname) class Asm_Test(object): + run_addr = 0x0 + def __init__(self): - self.myjit = Machine("x86_32").jitter() + self.myjit = Machine(self.arch_name).jitter() self.myjit.init_stack() self.myjit.jit.log_regs = False self.myjit.jit.log_mn = False + def test_init(self): + pass + + def prepare(self): + pass def __call__(self): + self.prepare() self.asm() + self.init_machine() + self.test_init() self.run() self.check() + def run(self): + + self.myjit.init_run(self.run_addr) + self.myjit.continue_run() + + assert(self.myjit.pc == self.ret_addr) def asm(self): - blocs, symbol_pool = parse_asm.parse_txt(mn_x86, 32, self.TXT, + blocs, symbol_pool = parse_asm.parse_txt(mn_x86, self.arch_attrib, self.TXT, symbol_pool = self.myjit.ir_arch.symbol_pool) # fix shellcode addr symbol_pool.set_offset(symbol_pool.getby_name("main"), 0x0) @@ -48,18 +64,37 @@ class Asm_Test(object): s = str(s) self.assembly = s - def run(self): - run_addr = 0 - self.myjit.vm.add_memory_page(run_addr, PAGE_READ | PAGE_WRITE, self.assembly) + def check(self): + raise NotImplementedError('abstract method') - self.myjit.push_uint32_t(0x1337beef) - self.myjit.add_breakpoint(0x1337beef, lambda x:False) +class Asm_Test_32(Asm_Test): + arch_name = "x86_32" + arch_attrib = 32 + ret_addr = 0x1337beef - self.myjit.init_run(run_addr) - self.myjit.continue_run() + def init_machine(self): + self.myjit.vm.add_memory_page(self.run_addr, PAGE_READ | PAGE_WRITE, self.assembly) + self.myjit.push_uint32_t(self.ret_addr) + self.myjit.add_breakpoint(self.ret_addr, lambda x:False) - assert(self.myjit.pc == 0x1337beef) - def check(self): - raise NotImplementedError('abstract method') +class Asm_Test_16(Asm_Test): + arch_name = "x86_16" + arch_attrib = 16 + ret_addr = 0x1337 + + def __init__(self): + self.myjit = Machine(self.arch_name).jitter() + self.myjit.stack_base = 0x1000 + self.myjit.stack_size = 0x1000 + self.myjit.init_stack() + + self.myjit.jit.log_regs = False + self.myjit.jit.log_mn = False + + + def init_machine(self): + self.myjit.vm.add_memory_page(self.run_addr, PAGE_READ | PAGE_WRITE, self.assembly) + self.myjit.push_uint16_t(self.ret_addr) + self.myjit.add_breakpoint(self.ret_addr, lambda x:False) diff --git a/test/arch/x86/unit/mn_daa.py b/test/arch/x86/unit/mn_daa.py index cb96a22b..7aadf582 100644 --- a/test/arch/x86/unit/mn_daa.py +++ b/test/arch/x86/unit/mn_daa.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 -class Test_DAA(Asm_Test): +class Test_DAA(Asm_Test_32): TXT = ''' main: MOV EBP, ESP diff --git a/test/arch/x86/unit/mn_das.py b/test/arch/x86/unit/mn_das.py index ba84abdd..0828cafe 100644 --- a/test/arch/x86/unit/mn_das.py +++ b/test/arch/x86/unit/mn_das.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 -class Test_DAS(Asm_Test): +class Test_DAS(Asm_Test_32): TXT = ''' main: MOV EBP, ESP diff --git a/test/arch/x86/unit/mn_float.py b/test/arch/x86/unit/mn_float.py index 863e86c3..81eb518b 100644 --- a/test/arch/x86/unit/mn_float.py +++ b/test/arch/x86/unit/mn_float.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 -class Test_FADD(Asm_Test): +class Test_FADD(Asm_Test_32): TXT = ''' main: ; test float diff --git a/test/arch/x86/unit/mn_int.py b/test/arch/x86/unit/mn_int.py index 119e5b08..0f4a5717 100644 --- a/test/arch/x86/unit/mn_int.py +++ b/test/arch/x86/unit/mn_int.py @@ -1,9 +1,9 @@ #! /usr/bin/env python from miasm2.jitter.csts import EXCEPT_INT_XX -from asm_test import Asm_Test +from asm_test import Asm_Test_32 -class Test_INT(Asm_Test): +class Test_INT(Asm_Test_32): TXT = ''' main: INT 0x42 diff --git a/test/arch/x86/unit/mn_pcmpeq.py b/test/arch/x86/unit/mn_pcmpeq.py index a8774cbc..06815e76 100644 --- a/test/arch/x86/unit/mn_pcmpeq.py +++ b/test/arch/x86/unit/mn_pcmpeq.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 import sys -class Test_PCMPEQB(Asm_Test): +class Test_PCMPEQB(Asm_Test_32): TXT = ''' main: CALL next @@ -21,7 +21,7 @@ class Test_PCMPEQB(Asm_Test): assert self.myjit.cpu.MM1 == 0xFF00000000FF0000 -class Test_PCMPEQW(Asm_Test): +class Test_PCMPEQW(Asm_Test_32): TXT = ''' main: CALL next @@ -41,7 +41,7 @@ class Test_PCMPEQW(Asm_Test): -class Test_PCMPEQD(Asm_Test): +class Test_PCMPEQD(Asm_Test_32): TXT = ''' main: CALL next diff --git a/test/arch/x86/unit/mn_pextr.py b/test/arch/x86/unit/mn_pextr.py index eb724cf9..0469eed7 100644 --- a/test/arch/x86/unit/mn_pextr.py +++ b/test/arch/x86/unit/mn_pextr.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 import sys -class Test_PEXTRB(Asm_Test): +class Test_PEXTRB(Asm_Test_32): TXT = ''' main: CALL next diff --git a/test/arch/x86/unit/mn_pinsr.py b/test/arch/x86/unit/mn_pinsr.py index b7a86d2d..a10cd286 100644 --- a/test/arch/x86/unit/mn_pinsr.py +++ b/test/arch/x86/unit/mn_pinsr.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 import sys -class Test_PINSRB(Asm_Test): +class Test_PINSRB(Asm_Test_32): TXT = ''' main: CALL next diff --git a/test/arch/x86/unit/mn_pmaxu.py b/test/arch/x86/unit/mn_pmaxu.py index 08e54c03..50cbff94 100644 --- a/test/arch/x86/unit/mn_pmaxu.py +++ b/test/arch/x86/unit/mn_pmaxu.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 import sys -class Test_PMAXU(Asm_Test): +class Test_PMAXU(Asm_Test_32): TXT = ''' main: CALL next diff --git a/test/arch/x86/unit/mn_pminu.py b/test/arch/x86/unit/mn_pminu.py index 38a29787..27c9ad1e 100644 --- a/test/arch/x86/unit/mn_pminu.py +++ b/test/arch/x86/unit/mn_pminu.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 import sys -class Test_PMINU(Asm_Test): +class Test_PMINU(Asm_Test_32): TXT = ''' main: CALL next diff --git a/test/arch/x86/unit/mn_pmovmskb.py b/test/arch/x86/unit/mn_pmovmskb.py index 97435794..796e977c 100644 --- a/test/arch/x86/unit/mn_pmovmskb.py +++ b/test/arch/x86/unit/mn_pmovmskb.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 import sys -class Test_PMOVMSKB(Asm_Test): +class Test_PMOVMSKB(Asm_Test_32): TXT = ''' main: CALL next diff --git a/test/arch/x86/unit/mn_pshufb.py b/test/arch/x86/unit/mn_pshufb.py index 187b2f72..594b0870 100644 --- a/test/arch/x86/unit/mn_pshufb.py +++ b/test/arch/x86/unit/mn_pshufb.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 import sys -class Test_PSHUFB(Asm_Test): +class Test_PSHUFB(Asm_Test_32): TXT = ''' main: CALL next diff --git a/test/arch/x86/unit/mn_psrl_psll.py b/test/arch/x86/unit/mn_psrl_psll.py index 93a356f7..79125612 100644 --- a/test/arch/x86/unit/mn_psrl_psll.py +++ b/test/arch/x86/unit/mn_psrl_psll.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 import sys -class Test_PSRL(Asm_Test): +class Test_PSRL(Asm_Test_32): TXT = ''' main: CALL next @@ -26,7 +26,7 @@ class Test_PSRL(Asm_Test): assert self.myjit.cpu.MM2 == 0x0112233405566778L assert self.myjit.cpu.MM3 == 0x0112233445566778L -class Test_PSLL(Asm_Test): +class Test_PSLL(Asm_Test_32): TXT = ''' main: CALL next diff --git a/test/arch/x86/unit/mn_punpck.py b/test/arch/x86/unit/mn_punpck.py index 84d86c32..8b655aa0 100644 --- a/test/arch/x86/unit/mn_punpck.py +++ b/test/arch/x86/unit/mn_punpck.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 import sys -class Test_PUNPCKHBW(Asm_Test): +class Test_PUNPCKHBW(Asm_Test_32): TXT = ''' main: CALL next @@ -21,7 +21,7 @@ class Test_PUNPCKHBW(Asm_Test): assert self.myjit.cpu.MM1 == 0xAA11BB22CC33DD44 -class Test_PUNPCKHWD(Asm_Test): +class Test_PUNPCKHWD(Asm_Test_32): TXT = ''' main: CALL next @@ -41,7 +41,7 @@ class Test_PUNPCKHWD(Asm_Test): -class Test_PUNPCKHDQ(Asm_Test): +class Test_PUNPCKHDQ(Asm_Test_32): TXT = ''' main: CALL next @@ -62,7 +62,7 @@ class Test_PUNPCKHDQ(Asm_Test): -class Test_PUNPCKLBW(Asm_Test): +class Test_PUNPCKLBW(Asm_Test_32): TXT = ''' main: CALL next @@ -81,7 +81,7 @@ class Test_PUNPCKLBW(Asm_Test): assert self.myjit.cpu.MM1 == 0xEE55FF6602770188 -class Test_PUNPCKLWD(Asm_Test): +class Test_PUNPCKLWD(Asm_Test_32): TXT = ''' main: CALL next @@ -101,7 +101,7 @@ class Test_PUNPCKLWD(Asm_Test): -class Test_PUNPCKLDQ(Asm_Test): +class Test_PUNPCKLDQ(Asm_Test_32): TXT = ''' main: CALL next diff --git a/test/arch/x86/unit/mn_pushpop.py b/test/arch/x86/unit/mn_pushpop.py new file mode 100644 index 00000000..d230a088 --- /dev/null +++ b/test/arch/x86/unit/mn_pushpop.py @@ -0,0 +1,125 @@ +#! /usr/bin/env python +from asm_test import Asm_Test_16, Asm_Test_32 +from miasm2.core.utils import pck16, pck32 + + +def init_regs(test): + test.myjit.cpu.EAX = 0x11111111 + test.myjit.cpu.EBX = 0x22222222 + test.myjit.cpu.ECX = 0x33333333 + test.myjit.cpu.EDX = 0x44444444 + test.myjit.cpu.ESI = 0x55555555 + test.myjit.cpu.EDI = 0x66666666 + test.myjit.cpu.EBP = 0x77777777 + test.stk_origin = test.myjit.cpu.ESP + + +class Test_PUSHAD_32(Asm_Test_32): + MYSTRING = "test pushad 32" + + def prepare(self): + self.myjit.ir_arch.symbol_pool.add_label("lbl_ret", self.ret_addr) + + def test_init(self): + init_regs(self) + self.buf = "" + for reg_name in reversed(["EAX", "ECX", + "EDX", "EBX", + "ESP", "EBP", + "ESI", "EDI"]): + self.buf += pck32(getattr(self.myjit.cpu, reg_name)) + + TXT = ''' + main: + PUSHAD + JMP lbl_ret + ''' + + def check(self): + buf = self.myjit.vm.get_mem(self.myjit.cpu.ESP, 0x4 * 8) + assert(buf == self.buf) + + +class Test_PUSHA_32(Asm_Test_32): + MYSTRING = "test pusha 32" + + def prepare(self): + self.myjit.ir_arch.symbol_pool.add_label("lbl_ret", self.ret_addr) + + def test_init(self): + init_regs(self) + self.buf = "" + for reg_name in reversed(["AX", "CX", + "DX", "BX", + "SP", "BP", + "SI", "DI"]): + self.buf += pck16(getattr(self.myjit.cpu, reg_name)) + + TXT = ''' + main: + PUSHA + JMP lbl_ret + ''' + + def check(self): + buf = self.myjit.vm.get_mem(self.myjit.cpu.ESP, 0x2 * 8) + assert(buf == self.buf) + + +class Test_PUSHA_16(Asm_Test_16): + MYSTRING = "test pusha 16" + + def prepare(self): + self.myjit.ir_arch.symbol_pool.add_label("lbl_ret", self.ret_addr) + + def test_init(self): + init_regs(self) + self.buf = "" + for reg_name in reversed(["AX", "CX", + "DX", "BX", + "SP", "BP", + "SI", "DI"]): + self.buf += pck16(getattr(self.myjit.cpu, reg_name)) + + TXT = ''' + main: + PUSHA + JMP lbl_ret + ''' + + def check(self): + buf = self.myjit.vm.get_mem(self.myjit.cpu.SP, 0x2 * 8) + assert(buf == self.buf) + + +class Test_PUSHAD_16(Asm_Test_16): + MYSTRING = "test pushad 16" + + def prepare(self): + self.myjit.ir_arch.symbol_pool.add_label("lbl_ret", self.ret_addr) + + def test_init(self): + init_regs(self) + self.buf = "" + for reg_name in reversed(["EAX", "ECX", + "EDX", "EBX", + "ESP", "EBP", + "ESI", "EDI"]): + self.buf += pck32(getattr(self.myjit.cpu, reg_name)) + + TXT = ''' + main: + PUSHAD + JMP lbl_ret + ''' + + def check(self): + buf = self.myjit.vm.get_mem(self.myjit.cpu.SP, 0x4 * 8) + assert(buf == self.buf) + + +if __name__ == "__main__": + [test()() for test in [Test_PUSHA_16, Test_PUSHA_32, + Test_PUSHAD_16, Test_PUSHAD_32 + ] + ] diff --git a/test/arch/x86/unit/mn_stack.py b/test/arch/x86/unit/mn_stack.py index dd349d54..6ae26d67 100644 --- a/test/arch/x86/unit/mn_stack.py +++ b/test/arch/x86/unit/mn_stack.py @@ -1,8 +1,8 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 -class Test_PUSHPOP(Asm_Test): +class Test_PUSHPOP(Asm_Test_32): TXT = ''' main: MOV EBP, ESP diff --git a/test/arch/x86/unit/mn_strings.py b/test/arch/x86/unit/mn_strings.py index db52fa74..f8055665 100644 --- a/test/arch/x86/unit/mn_strings.py +++ b/test/arch/x86/unit/mn_strings.py @@ -1,7 +1,7 @@ #! /usr/bin/env python -from asm_test import Asm_Test +from asm_test import Asm_Test_32 -class Test_SCAS(Asm_Test): +class Test_SCAS(Asm_Test_32): MYSTRING = "test string" TXT = ''' main: @@ -22,7 +22,7 @@ class Test_SCAS(Asm_Test): assert(self.myjit.cpu.EDI == self.myjit.ir_arch.symbol_pool.getby_name('mystr').offset + len(self.MYSTRING)+1) -class Test_MOVS(Asm_Test): +class Test_MOVS(Asm_Test_32): MYSTRING = "test string" TXT = ''' main: diff --git a/test/test_all.py b/test/test_all.py index 34bb0a55..53e8d513 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -58,6 +58,7 @@ for script in ["x86/sem.py", "x86/unit/mn_pinsr.py", "x86/unit/mn_pextr.py", "x86/unit/mn_pmovmskb.py", + "x86/unit/mn_pushpop.py", "arm/arch.py", "arm/sem.py", "aarch64/unit/mn_ubfm.py", |