about summary refs log tree commit diff stats
path: root/test/arch/x86/qemu/testqemu64.py
diff options
context:
space:
mode:
authorAjax <commial@gmail.com>2018-12-03 15:56:32 +0100
committerAjax <commial@gmail.com>2018-12-04 12:27:25 +0100
commit7b69ac4c84c1cdbf3d54f431ae0f948ea25f679e (patch)
treea66bf69aeb024cc3ae8edf3956e5f75417a543f7 /test/arch/x86/qemu/testqemu64.py
parentb3104648122b721f00a3e7fc88a26c6212f1e17c (diff)
downloadmiasm-7b69ac4c84c1cdbf3d54f431ae0f948ea25f679e.tar.gz
miasm-7b69ac4c84c1cdbf3d54f431ae0f948ea25f679e.zip
Tests/QEMU-x86_64: add sample, script and expected outputs
The script is basically copied from testqemu.py
Diffstat (limited to 'test/arch/x86/qemu/testqemu64.py')
-rw-r--r--test/arch/x86/qemu/testqemu64.py132
1 files changed, 132 insertions, 0 deletions
diff --git a/test/arch/x86/qemu/testqemu64.py b/test/arch/x86/qemu/testqemu64.py
new file mode 100644
index 00000000..bd82d414
--- /dev/null
+++ b/test/arch/x86/qemu/testqemu64.py
@@ -0,0 +1,132 @@
+import os
+import sys
+import struct
+import logging
+from pdb import pm
+
+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"
+    out = []
+    i = 0
+    while i < len(fmt):
+        c = fmt[i]
+        if c != "%":
+            i+=1
+            continue
+        if fmt[i+1] == "%":
+            i+=2
+            continue
+        j = 0
+        i+=1
+        while fmt[i+j] in "0123456789$.-":
+            j+=1
+        if fmt[i+j] in ['l']:
+            j +=1
+        if fmt[i+j] == "h":
+            x = fmt[i+j:i+j+2]
+        else:
+            x = fmt[i+j]
+        i+=j
+        out.append(x)
+    return out
+
+nb_tests = 1
+def xxx___printf_chk(jitter):
+    """Tiny implementation of printf_chk"""
+    global nb_tests
+    ret_ad, args = jitter.func_args_systemv(["out", "format"])
+    if args.out != 1:
+        raise RuntimeError("Not implemented")
+    fmt = jitter.get_str_ansi(args.format)
+    # Manage llx
+    fmt = fmt.replace("llx", "lx")
+    fmt = fmt.replace("%016lx", "%016z")
+
+    fmt_a = parse_fmt(fmt)
+    args = []
+    i = 0
+
+    for x in fmt_a:
+        a = jitter.get_arg_n_systemv(2 + i)
+        if x == "s":
+            a = jitter.get_str_ansi(a)
+        elif x.lower() in ("x", 'd', 'z'):
+            pass
+        elif x.lower() in ("f", "l"):
+            a = struct.unpack("d", struct.pack("Q", a))[0]
+            i += 1
+        else:
+            raise RuntimeError("Not implemented format")
+        args.append(a)
+        i += 1
+
+    fmt = fmt.replace("%016z", "%016lx")
+    output = fmt%(tuple(args))
+    # NaN bad repr in Python
+    output = output.replace("nan", "-nan")
+
+    if "\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
+        raise RuntimeError("Bad semantic")
+
+    sys.stdout.write("[%d] %s" % (nb_tests, output))
+    nb_tests += 1
+    jitter.func_ret_systemv(ret_ad, 0)
+
+def xxx_puts(jitter):
+    '''
+    #include <stdio.h>
+    int puts(const char *s);
+
+    writes the string s and a trailing newline to stdout.
+    '''
+    ret_addr, args = jitter.func_args_systemv(['target'])
+    output = jitter.get_str_ansi(args.target)
+    # Check with expected result
+    line = expected.next()
+    if output != line.rstrip():
+        print "Expected:", line
+        print "Obtained:", output
+        raise RuntimeError("Bad semantic")
+    return jitter.func_ret_systemv(ret_addr, 1)
+
+# Parse arguments
+parser = Sandbox_Linux_x86_64.parser(description="ELF sandboxer")
+parser.add_argument("filename", help="ELF Filename")
+parser.add_argument("funcname", help="Targeted function's name")
+parser.add_argument("expected", help="Expected output")
+options = parser.parse_args()
+
+# Expected output
+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
+except AttributeError:
+    raise RuntimeError("The target binary must have a symtab section")
+
+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")
+
+
+# Run
+sb.run(addr)
+
+assert(sb.jitter.run is False)