diff options
| -rw-r--r-- | miasm2/arch/mips32/arch.py | 8 | ||||
| -rw-r--r-- | test/arch/mips32/unit/asm_test.py | 66 | ||||
| -rw-r--r-- | test/arch/mips32/unit/mn_bcc.py | 32 | ||||
| -rw-r--r-- | test/test_all.py | 1 |
4 files changed, 100 insertions, 7 deletions
diff --git a/miasm2/arch/mips32/arch.py b/miasm2/arch/mips32/arch.py index 12f4ff8e..a5463b6f 100644 --- a/miasm2/arch/mips32/arch.py +++ b/miasm2/arch/mips32/arch.py @@ -126,7 +126,7 @@ class instruction_mips32(cpu.instruction): if not isinstance(e, ExprInt): return - ad = e.arg + self.offset + 4 + ad = e.arg + self.offset l = symbol_pool.getby_offset_create(ad) s = ExprId(l, e.size) self.args[ndx] = s @@ -174,18 +174,14 @@ class instruction_mips32(cpu.instruction): def fixDstOffset(self): ndx = self.get_dst_num() e = self.args[ndx] - print 'FIX', ndx, e, self.offset, self.l if self.offset is None: raise ValueError('symbol not resolved %s' % self.l) if not isinstance(e, ExprInt): return off = e.arg - self.offset - print "diff", e, hex(self.offset) - print hex(off) if int(off % 4): raise ValueError('strange offset! %r' % off) self.args[ndx] = ExprInt32(off) - print 'final', self def get_args_expr(self): args = [a for a in self.args] @@ -444,8 +440,6 @@ class mips32_eposh(mips32_imm, cpu.m_arg): return True -class mips32_imm(mips32_imm): - pass class mips32_cpr(cpu.m_arg): diff --git a/test/arch/mips32/unit/asm_test.py b/test/arch/mips32/unit/asm_test.py new file mode 100644 index 00000000..b6cb7b2d --- /dev/null +++ b/test/arch/mips32/unit/asm_test.py @@ -0,0 +1,66 @@ +#! /usr/bin/env python +import sys +import os + +from miasm2.core.cpu import parse_ast +from miasm2.arch.mips32.arch import mn_mips32, 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_mips32.regs.all_regs_ids_byname) + + +class Asm_Test(object): + + def __init__(self): + self.myjit = Machine("mips32l").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_mips32, 'l', 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() + patches = asmbloc.asm_resolve_final(mn_mips32, 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.add_memory_page( + run_addr, PAGE_READ | PAGE_WRITE, self.assembly) + + self.myjit.cpu.RA = 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/mips32/unit/mn_bcc.py b/test/arch/mips32/unit/mn_bcc.py new file mode 100644 index 00000000..729f3679 --- /dev/null +++ b/test/arch/mips32/unit/mn_bcc.py @@ -0,0 +1,32 @@ +#! /usr/bin/env python +from asm_test import Asm_Test + + +class Test_BCC(Asm_Test): + MYSTRING = "test string" + TXT = ''' + main: + ADDIU A0, V0, mystr +strlen: + LBU V0, 0(A0) + BEQ V0, ZERO, SKIP + ADDU V1, ZERO, ZERO +loop: + ADDIU A0, A0, 1 + LBU V0, 0(A0) + BNE V0, ZERO, loop + ADDIU V1, V1, 1 +SKIP: + JR RA + ADDU V0, V1, ZERO + + mystr: + .string "%s" + ''' % MYSTRING + + def check(self): + assert(self.myjit.cpu.V0 == len(self.MYSTRING)) + + +if __name__ == "__main__": + [test()() for test in [Test_BCC]] diff --git a/test/test_all.py b/test/test_all.py index 54537bdf..8d5d16fe 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -46,6 +46,7 @@ for script in ["x86/sem.py", "msp430/sem.py", "sh4/arch.py", "mips32/arch.py", + "mips32/unit/mn_bcc.py", ]: testset += RegressionTest([script], base_dir="arch") |