diff --git a/libdisasm/test/static-generate.py b/libdisasm/test/static-generate.py new file mode 100755 index 0000000..1eb95c8 --- /dev/null +++ b/libdisasm/test/static-generate.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python + +# Copyright (c) 2009 Diego Ongaro +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +import sys + +DEFINITIONS = 'static.txt' +MODES = ['intel-32', 'att-32', 'intel-16', 'att-16'] + +if len(sys.argv) >= 2: + target = sys.argv[1] +else: + target = 'static-tests' + +# Read lines from definitions file +data = [] +linenum = 1 +for line in open(DEFINITIONS).readlines(): + # strip out comments + line = line.split('#', 1)[0] + + # strip out whitespace + line = line.strip() + + # strip out blank lines + if len(line) != 0: + data.append((linenum, line)) + linenum += 1 + +class SError(SyntaxError): + def __init__(self, linenum, line, msg): + self.linenum = linenum + self.line = line + self.msg = msg + + def __str__(self): + return "%s on line %d ('%s')" % (self.msg, + self.linenum, + self.line) + + +# Parse lines into a list of tests +tests = [] +test = None +for (linenum, line) in data: + + # split by ':' into 'label: value' + if ':' not in line: + raise SError(linenum, line, "Missing ':'") + label, value = map(str.strip, line.split(':', 1)) + + if not label: + raise SError(linenum, line, "Missing label") + if not value: + raise SError(linenum, line, "Missing value") + + if label == 'input': + + # save previous test + if test is not None: + if len(test) < len(MODES) + 1: + raise SError(linenum, line, "Test missing output line(s)") + tests.append(test) + + # start new test + try: + [chr(int(byte, 16)) for byte in value.split()] + except ValueError: + raise SError(linenum, line, "Expected space-separated hex bytes") + test = {label: value} + + elif label in MODES: + if test is None: + raise SError(linenum, line, "Tests must start with an 'input' line") + if label in test: + raise SError(linenum, line, "Duplicate output line") + test[label] = value + +# save last test +if test is not None: + if len(test) < len(MODES) + 1: + raise SError(linenum, line, "Test missing output line(s)") + tests.append(test) + +# write files out for run-static.sh +files = {'input': open('%s.bin' % (target), 'w')} +for mode in MODES: + files[mode] = open('%s.%s' % (target, mode), 'w') + +for test in tests: + for byte in test['input'].split(): + files['input'].write(chr(int(byte, 16))) + for mode in MODES: + files[mode].write('%-30s %s\n' % (test['input'], test[mode])) diff --git a/libdisasm/test/static-generate.sh b/libdisasm/test/static-generate.sh deleted file mode 100755 index 286398b..0000000 --- a/libdisasm/test/static-generate.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh -if [ -z "$1" ]; then - echo "Usage: $0 TARGET" >&2 - exit 1 -fi - - -# Thanks to Diego Ongaro and Brent Stephens -/bin/echo -ne '\x81\x3E\x00\x00\x55\xAA' > "$1".bin -echo '81 3E 00 00 55 AA cmp [esi], 0xAA550000' > "$1".intel-32 -echo '81 3E 00 00 55 AA cmpl $0xAA550000, (%esi)' > "$1".att-32 -echo '81 3E 00 00 55 AA cmp [], 0xAA55' > "$1".intel-16 -echo '81 3E 00 00 55 AA cmpw $0xAA55, 0x0' > "$1".att-16 diff --git a/libdisasm/test/static.txt b/libdisasm/test/static.txt new file mode 100644 index 0000000..af5b49d --- /dev/null +++ b/libdisasm/test/static.txt @@ -0,0 +1,13 @@ +# Thanks to Diego Ongaro and Brent Stephens +input: 81 3E 00 00 55 AA +intel-32: cmp [esi], 0xAA550000 +att-32: cmpl $0xAA550000, (%esi) +intel-16: cmp [], 0xAA55 +att-16: cmpw $0xAA55, 0x0 + +# Thanks to Diego Ongaro +input: 26 F3 6F +intel-32: repz outs edx, es:[esi] +att-32: repz outsl %es:(%esi), %edx +intel-16: repz outs dx, es:[esi] +att-16: repz outsw %es:(%esi), %dx