diff options
Diffstat (limited to 'test/arch/x86/qemu/testqemu64.py')
| -rw-r--r-- | test/arch/x86/qemu/testqemu64.py | 60 |
1 files changed, 33 insertions, 27 deletions
diff --git a/test/arch/x86/qemu/testqemu64.py b/test/arch/x86/qemu/testqemu64.py index bd82d414..4fe51992 100644 --- a/test/arch/x86/qemu/testqemu64.py +++ b/test/arch/x86/qemu/testqemu64.py @@ -1,36 +1,42 @@ +from __future__ import print_function import os -import sys import struct import logging +from sys import stdout from pdb import pm +try: + stdout = stdout.buffer +except AttributeError: + pass + from miasm2.analysis.sandbox import Sandbox_Linux_x86_64 from miasm2.jitter.jitload import log_func from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE # Utils def parse_fmt(s): - fmt = s[:]+"\x00" + fmt = s[:]+b"\x00" out = [] i = 0 while i < len(fmt): - c = fmt[i] - if c != "%": + c = fmt[i:i+1] + if c != b"%": i+=1 continue - if fmt[i+1] == "%": + if fmt[i+1:i+2] == b"%": i+=2 continue j = 0 i+=1 - while fmt[i+j] in "0123456789$.-": + while fmt[i+j:i+j+1] in b"0123456789$.-": j+=1 - if fmt[i+j] in ['l']: + if fmt[i+j:i+j+1] in [b'l']: j +=1 - if fmt[i+j] == "h": + if fmt[i+j:i+j+1] == b"h": x = fmt[i+j:i+j+2] else: - x = fmt[i+j] + x = fmt[i+j:i+j+1] i+=j out.append(x) return out @@ -44,8 +50,8 @@ def xxx___printf_chk(jitter): raise RuntimeError("Not implemented") fmt = jitter.get_str_ansi(args.format) # Manage llx - fmt = fmt.replace("llx", "lx") - fmt = fmt.replace("%016lx", "%016z") + fmt = fmt.replace(b"llx", b"lx") + fmt = fmt.replace(b"%016lx", b"%016z") fmt_a = parse_fmt(fmt) args = [] @@ -53,11 +59,11 @@ def xxx___printf_chk(jitter): for x in fmt_a: a = jitter.get_arg_n_systemv(2 + i) - if x == "s": + if x == b"s": a = jitter.get_str_ansi(a) - elif x.lower() in ("x", 'd', 'z'): + elif x in (b"x", b'X', b'd', b'z', b'Z'): pass - elif x.lower() in ("f", "l"): + elif x.lower() in (b"f","l"): a = struct.unpack("d", struct.pack("Q", a))[0] i += 1 else: @@ -65,22 +71,22 @@ def xxx___printf_chk(jitter): args.append(a) i += 1 - fmt = fmt.replace("%016z", "%016lx") + fmt = fmt.replace(b"%016z", b"%016lx") output = fmt%(tuple(args)) # NaN bad repr in Python - output = output.replace("nan", "-nan") + output = output.replace(b"nan", b"-nan") - if "\n" not in output: + if b"\n" not in output: raise RuntimeError("Format must end with a \\n") # Check with expected result - line = expected.next() - if output != line: - print "Expected:", line - print "Obtained:", output + line = next(expected) + if output != line.encode(): + print("Expected:", line) + print("Obtained:", output) raise RuntimeError("Bad semantic") - sys.stdout.write("[%d] %s" % (nb_tests, output)) + stdout.write(b"[%d] %s" % (nb_tests, output)) nb_tests += 1 jitter.func_ret_systemv(ret_ad, 0) @@ -94,10 +100,10 @@ def xxx_puts(jitter): ret_addr, args = jitter.func_args_systemv(['target']) output = jitter.get_str_ansi(args.target) # Check with expected result - line = expected.next() + line = next(expected) if output != line.rstrip(): - print "Expected:", line - print "Obtained:", output + print("Expected:", line) + print("Obtained:", output) raise RuntimeError("Bad semantic") return jitter.func_ret_systemv(ret_addr, 1) @@ -114,7 +120,7 @@ expected = open(options.expected) # Create sandbox sb = Sandbox_Linux_x86_64(options.filename, options, globals()) try: - addr = sb.elf.getsectionbyname(".symtab").symbols[options.funcname].value + addr = sb.elf.getsectionbyname(".symtab")[options.funcname].value except AttributeError: raise RuntimeError("The target binary must have a symtab section") @@ -123,7 +129,7 @@ log_func.setLevel(logging.ERROR) # Segmentation sb.jitter.cpu.set_segm_base(8, 0x7fff0000) sb.jitter.cpu.FS = 8 -sb.jitter.vm.add_memory_page(0x7fff0000 + 0x28, PAGE_READ | PAGE_WRITE, "AAAAAAAA") +sb.jitter.vm.add_memory_page(0x7fff0000 + 0x28, PAGE_READ | PAGE_WRITE, b"AAAAAAAA") # Run |