279 lines
14 KiB
Python
279 lines
14 KiB
Python
"""Runtime tests for the reconstructed as3vm_abc_decode translation unit.
|
|
|
|
RED STATE: as3vm_abc_decode.c / as3vm_abc_decode.h do not exist yet, so
|
|
setUpClass raises "missing as3vm_abc_decode runtime inputs". Every assertion
|
|
below is grounded in a specific line of
|
|
iggy-decompiled/raw/durango/as3vm_abc_decode.c (cited inline) so a faithful
|
|
DURANGO reconstruction turns these green.
|
|
|
|
as3vm_abc_decode is Iggy's ABC (AVM2 bytecode) file parser. It exports EXACTLY
|
|
ONE entry point on all four platforms -- iggy_abc_file -- which parses a DoABC
|
|
block: the minor/major version header, the constant pool (ints / uints /
|
|
doubles / strings / namespaces / ns_sets / multinames), method_info,
|
|
metadata_info, instance_info + class_info, script_info and method_body_info,
|
|
building the VM's class / method / constant structures (via as3vm_classmap_add,
|
|
as3vm_multiname_make, as3vm_build_trait_array, as3vm_link_class, ...).
|
|
|
|
It OWNS NO externally named data global: the twenty-nine durango ' R ' symbols
|
|
nm surfaces are MSVC rodata COMDATs (the twenty-seven "Verification error: ..."
|
|
/ "Problem class was %n" diagnostics, the embedded __FILE__ source-path string,
|
|
and the wide-UTF16 "Vector" literal) -- read-only artifacts, local on the other
|
|
three platforms, NOT owned data (see the cross test's EXPECTED_GLOBALS ==
|
|
set()). The constant-kind / trait-kind dispatch tables are anonymous
|
|
&__ImageBase-relative .rdata jump tables, not named globals either.
|
|
|
|
VALUE / STRUCT MODEL (raw/durango, 64-bit offsets; the runtime test builds ONLY
|
|
the durango dylib so the layout is correct):
|
|
- iggy_abc_file(param_1, read_byte_fn, reader_state, ...): param_1 is a
|
|
two-level VM pointer -- param_1[0] == P (the "vm object"), *(P) == C (the
|
|
parse context). read_byte_fn is invoked as (*read_byte_fn)(reader_state)
|
|
to pull one input byte at a time.
|
|
- the FIRST action (raw:1493) is iggy_arena_alloc_create(0x2000, 0x2000,
|
|
*(C + 0x34e0)) -- the per-file arena, created BEFORE any byte is read.
|
|
- the header (raw:1509-1516) reads two little-endian u16s: minor =
|
|
b1*0x100 + b0, then major = b3*0x100 + b2. The version gate is
|
|
major == 0x2e (46) AND minor in {0x10 (16), 0x12 (18)}; durango writes it
|
|
`(major != 0x2e) || ((minor - 0x10) & 0xfffd) != 0`.
|
|
- on a version mismatch (raw:1516-1519): iggy_send_error(C, 0x196,
|
|
"Verification error: AS3 version number not supported: %d.%d", major,
|
|
minor) and the function returns 0 -- BEFORE abc_cpool and the deep parse.
|
|
|
|
WHAT IS PINNED (real, deterministic DURANGO behavior):
|
|
- the arena is created (iggy_arena_alloc_create, args 0x2000 / 0x2000) before
|
|
the header is read.
|
|
- a bad MAJOR version (!= 46) trips the gate: iggy_send_error(C, 0x196, "AS3
|
|
version..." fmt), return 0. The diagnostic CONTENT is load-bearing.
|
|
- a bad MINOR version (major 46 but minor 17 / 19, i.e. NOT in {16,18}) trips
|
|
the same gate -- pinning the (minor-0x10)&0xfffd bitmask via its reject side.
|
|
- the version error forwards code 0x196 with the module's own rodata fmt.
|
|
|
|
DEFERRED (flagged structural, NOT exercised here): everything past the version
|
|
gate -- abc_cpool + the seven pool decoders, the constant-kind dispatch
|
|
(convert_kind's &__ImageBase jump table), abc_method / abc_method_body /
|
|
abc_metadata, abc_instance + abc_class + abc_trait, the trait-kind dispatch,
|
|
abc_script, and the class-linking / verify-and-recompile call-outs. The
|
|
version-header ACCEPT set (46 / 16 and 46 / 18) is documented but not
|
|
runtime-exercised: acceptance falls straight into the deep constant-pool parse,
|
|
which is structural. The file-static abc_u32 u30/u32 varint is likewise not
|
|
directly callable (single export), so it is reachable only transitively.
|
|
|
|
Full-suite runs use python3.12 (the machine python3 is 3.14, which SEGFAULTs on
|
|
the full suite via an unrelated CPython ctypes-GC bug -- run it as
|
|
`python3.12 -u -m unittest discover -s tests -p 'test_*.py' -v` so the
|
|
"Ran N tests ... OK" summary flushes before any post-summary teardown segfault);
|
|
single-module runs of this file are fine on either interpreter.
|
|
"""
|
|
|
|
import ctypes
|
|
import shutil
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
|
|
SOURCE = RECONSTRUCTED / "as3vm_abc_decode.c"
|
|
HEADER = RECONSTRUCTED / "as3vm_abc_decode.h"
|
|
SUPPORT = ROOT / "tests/support/as3vm_abc_decode_test_support.c"
|
|
|
|
# 64-bit DURANGO value / struct constants (cited per test).
|
|
OFF_P_CONTEXT = 0 # *(P) == C, the parse context (raw:1495).
|
|
OFF_C_ARENA_PARENT = 0x34E0 # C -> arena parent, arena_alloc_create arg (raw:1493).
|
|
VERSION_MAJOR = 0x2E # required major version 46 (raw:1516).
|
|
VERSION_MINOR_A = 0x10 # accepted minor 16 (raw:1516).
|
|
VERSION_MINOR_B = 0x12 # accepted minor 18 (raw:1516).
|
|
SEND_ERROR_CODE = 0x196 # iggy_send_error category (raw:1517).
|
|
ARENA_CREATE_SIZE = 0x2000 # arena_alloc_create block size (raw:1493).
|
|
|
|
AS3_VERSION_MESSAGE = (
|
|
b"Verification error: AS3 version number not supported: %d.%d")
|
|
|
|
|
|
def _u16_bytes(value):
|
|
return bytes([value & 0xFF, (value >> 8) & 0xFF])
|
|
|
|
|
|
def _header(minor, major):
|
|
# The 4-byte ABC version header: minor u16 (LE) then major u16 (LE).
|
|
return _u16_bytes(minor) + _u16_bytes(major)
|
|
|
|
|
|
class ReconstructedAs3vmAbcDecodeTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
missing = [str(path) for path in (SOURCE, HEADER, SUPPORT)
|
|
if not path.exists()]
|
|
if missing:
|
|
raise AssertionError(
|
|
"missing as3vm_abc_decode runtime inputs: " + ", ".join(missing))
|
|
cls.temp = tempfile.TemporaryDirectory()
|
|
cls.base = Path(cls.temp.name) / "as3vm_abc_decode.dylib"
|
|
build = subprocess.run([
|
|
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra",
|
|
"-Werror", "-Wl,-undefined,dynamic_lookup",
|
|
"-DIGGY_RECONSTRUCT_AS3VM_ABC_DECODE_DURANGO=1",
|
|
"-I", str(RECONSTRUCTED),
|
|
str(SOURCE), str(SUPPORT), "-o", str(cls.base),
|
|
], capture_output=True, text=True)
|
|
if build.returncode:
|
|
raise AssertionError(build.stdout + build.stderr)
|
|
|
|
@classmethod
|
|
def tearDownClass(cls):
|
|
cls.temp.cleanup()
|
|
|
|
def setUp(self):
|
|
unique = (Path(self.temp.name) /
|
|
f"as3vm_abc_decode_{self._testMethodName}.dylib")
|
|
shutil.copyfile(self.base, unique)
|
|
self.lib = ctypes.CDLL(str(unique))
|
|
self._bind(self.lib)
|
|
self.lib.as3vm_abc_decode_test_reset()
|
|
self._keep = []
|
|
|
|
def _bind(self, lib):
|
|
void_p = ctypes.c_void_p
|
|
|
|
lib.as3vm_abc_decode_test_reset.restype = None
|
|
lib.as3vm_abc_decode_test_reset.argtypes = []
|
|
lib.as3vm_abc_decode_test_set_bytes.restype = None
|
|
lib.as3vm_abc_decode_test_set_bytes.argtypes = [void_p, ctypes.c_size_t]
|
|
lib.as3vm_abc_decode_test_reader_fn.restype = void_p
|
|
lib.as3vm_abc_decode_test_reader_fn.argtypes = []
|
|
for name in ("send_error", "arena_create", "arena_malloc", "reader_pos"):
|
|
fn = getattr(lib, f"as3vm_abc_decode_test_{name}_count"
|
|
if name != "reader_pos"
|
|
else "as3vm_abc_decode_test_reader_pos")
|
|
fn.restype = ctypes.c_size_t
|
|
fn.argtypes = []
|
|
lib.as3vm_abc_decode_test_send_last_ctx.restype = void_p
|
|
lib.as3vm_abc_decode_test_send_last_ctx.argtypes = []
|
|
lib.as3vm_abc_decode_test_send_last_code.restype = ctypes.c_int
|
|
lib.as3vm_abc_decode_test_send_last_code.argtypes = []
|
|
lib.as3vm_abc_decode_test_send_last_fmt.restype = void_p
|
|
lib.as3vm_abc_decode_test_send_last_fmt.argtypes = []
|
|
lib.as3vm_abc_decode_test_arena_create_a0.restype = ctypes.c_ssize_t
|
|
lib.as3vm_abc_decode_test_arena_create_a0.argtypes = []
|
|
lib.as3vm_abc_decode_test_arena_create_a1.restype = ctypes.c_ssize_t
|
|
lib.as3vm_abc_decode_test_arena_create_a1.argtypes = []
|
|
lib.as3vm_abc_decode_test_arena_create_parent.restype = void_p
|
|
lib.as3vm_abc_decode_test_arena_create_parent.argtypes = []
|
|
|
|
# Module export under test (durango 64-bit ABI): the sole entry point.
|
|
lib.iggy_abc_file.restype = ctypes.c_uint64
|
|
lib.iggy_abc_file.argtypes = [
|
|
void_p, void_p, void_p, void_p, void_p, ctypes.c_uint8]
|
|
|
|
def _buffer(self, size):
|
|
buf = (ctypes.c_uint8 * size)()
|
|
self._keep.append(buf)
|
|
return ctypes.addressof(buf)
|
|
|
|
def _u64(self, address, value):
|
|
ctypes.c_uint64.from_address(address).value = value
|
|
|
|
def _vm(self):
|
|
# param_1[0] == P; *(P) == C. P needs slot 0x527 (raw:1497) and C needs
|
|
# +0x34e0 (raw:1493); size generously so the shallow path is deref-safe.
|
|
context = self._buffer(0x3600) # C: the parse context.
|
|
vm_object = self._buffer(0x3000) # P: the vm object (P[0] := C).
|
|
self._u64(vm_object + OFF_P_CONTEXT, context)
|
|
param_1 = self._buffer(0x200)
|
|
self._u64(param_1, vm_object)
|
|
return param_1, context
|
|
|
|
def _set_bytes(self, data):
|
|
buf = (ctypes.c_uint8 * len(data)).from_buffer_copy(data)
|
|
self._keep.append(buf)
|
|
self.lib.as3vm_abc_decode_test_set_bytes(
|
|
ctypes.addressof(buf), len(data))
|
|
|
|
def _call(self, param_1):
|
|
reader = self.lib.as3vm_abc_decode_test_reader_fn()
|
|
return self.lib.iggy_abc_file(
|
|
param_1, reader, ctypes.c_void_p(0),
|
|
ctypes.c_void_p(0), ctypes.c_void_p(0), 0)
|
|
|
|
# ------------------------------------------------------------------ #
|
|
# arena setup precedes the header read.
|
|
# ------------------------------------------------------------------ #
|
|
|
|
def test_arena_created_before_header_with_expected_block_sizes(self):
|
|
# raw:1493 iggy_arena_alloc_create(0x2000, 0x2000, *(C+0x34e0)) is the
|
|
# first statement -- the per-file arena is built before any input byte.
|
|
param_1, _ = self._vm()
|
|
self._set_bytes(_header(VERSION_MINOR_A, 0)) # bad major -> early bail.
|
|
self._call(param_1)
|
|
self.assertEqual(self.lib.as3vm_abc_decode_test_arena_create_count(), 1)
|
|
self.assertEqual(
|
|
self.lib.as3vm_abc_decode_test_arena_create_a0(), ARENA_CREATE_SIZE)
|
|
self.assertEqual(
|
|
self.lib.as3vm_abc_decode_test_arena_create_a1(), ARENA_CREATE_SIZE)
|
|
|
|
# ------------------------------------------------------------------ #
|
|
# version-header gate -- MAJOR.
|
|
# ------------------------------------------------------------------ #
|
|
|
|
def test_bad_major_version_emits_diagnostic_and_returns_zero(self):
|
|
# raw:1516-1519 major != 0x2e trips the version gate: error(C, 0x196,
|
|
# "...AS3 version number not supported: %d.%d", major, minor) -> 0.
|
|
param_1, context = self._vm()
|
|
self._set_bytes(_header(VERSION_MINOR_A, 0x0000)) # major 0 != 46.
|
|
result = self._call(param_1)
|
|
self.assertEqual(result, 0)
|
|
self.assertEqual(self.lib.as3vm_abc_decode_test_send_error_count(), 1)
|
|
self.assertEqual(
|
|
self.lib.as3vm_abc_decode_test_send_last_code(), SEND_ERROR_CODE)
|
|
# error() is dispatched on C == *(P) (the parse context).
|
|
self.assertEqual(
|
|
self.lib.as3vm_abc_decode_test_send_last_ctx(), context)
|
|
|
|
def test_version_error_forwards_exact_diagnostic_string(self):
|
|
# raw:1517 the fmt pointer is the module's own rodata "Verification
|
|
# error: AS3 version number not supported: %d.%d" -- exact CONTENT is
|
|
# load-bearing for source-equivalence.
|
|
param_1, _ = self._vm()
|
|
self._set_bytes(_header(VERSION_MINOR_A, 0x00FF)) # major 255 != 46.
|
|
self._call(param_1)
|
|
fmt = self.lib.as3vm_abc_decode_test_send_last_fmt()
|
|
self.assertTrue(fmt, "iggy_send_error received a null format string")
|
|
self.assertEqual(ctypes.string_at(fmt), AS3_VERSION_MESSAGE)
|
|
|
|
# ------------------------------------------------------------------ #
|
|
# version-header gate -- MINOR bitmask reject side.
|
|
# ------------------------------------------------------------------ #
|
|
|
|
def test_minor_seventeen_with_good_major_is_rejected(self):
|
|
# raw:1516 (minor-0x10)&0xfffd != 0 for minor 17 (0x11): 17-16 == 1,
|
|
# 1 & 0xfffd == 1 -> reject even though major is the accepted 46.
|
|
param_1, _ = self._vm()
|
|
self._set_bytes(_header(0x0011, VERSION_MAJOR)) # minor 17, major 46.
|
|
result = self._call(param_1)
|
|
self.assertEqual(result, 0)
|
|
self.assertEqual(self.lib.as3vm_abc_decode_test_send_error_count(), 1)
|
|
self.assertEqual(
|
|
self.lib.as3vm_abc_decode_test_send_last_code(), SEND_ERROR_CODE)
|
|
|
|
def test_minor_nineteen_with_good_major_is_rejected(self):
|
|
# raw:1516 minor 19 (0x13): 19-16 == 3, 3 & 0xfffd == 1 -> reject. The
|
|
# accept set is exactly {16, 18}; 19 is outside it.
|
|
param_1, _ = self._vm()
|
|
self._set_bytes(_header(0x0013, VERSION_MAJOR)) # minor 19, major 46.
|
|
result = self._call(param_1)
|
|
self.assertEqual(result, 0)
|
|
self.assertEqual(self.lib.as3vm_abc_decode_test_send_error_count(), 1)
|
|
|
|
def test_version_gate_reads_exactly_four_header_bytes(self):
|
|
# raw:1509-1515 the gate consumes 4 bytes (two u16s) before deciding;
|
|
# on the bad-major reject it stops there (no deep parse consumes more).
|
|
param_1, _ = self._vm()
|
|
self._set_bytes(_header(VERSION_MINOR_A, 0x0000)) # bad major.
|
|
self._call(param_1)
|
|
self.assertEqual(self.lib.as3vm_abc_decode_test_reader_pos(), 4)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|