Files
neoLegacy-mac/tests/test_reconstructed_swf_decode.py

406 lines
18 KiB
Python

"""Runtime tests for the reconstructed swf_decode translation unit.
RED STATE: swf_decode.c / swf_decode.h do not exist yet, so setUpClass raises
"missing swf_decode runtime inputs". Every assertion below is grounded in a
specific line of iggy-decompiled/raw/durango/swf_decode.c (cited inline) so a
faithful DURANGO reconstruction turns these green.
swf_decode is Iggy's SWF/.iggy tag decoder + library/export bookkeeping: the
whole-file entry (iggy_decode_swf_from_memory), the late image-decoder dispatch
(iggy_decode_image), the library-table accessors (iggy_add_library /
iggy_get_library / iggy_get_library_id / iggy_get_libnum / iggy_get_export /
iggy_free_library_id) and the per-tag PlaceObject size classifier
(iggy_get_placeobject_size). It exports EXACTLY 9 entry points on all four
platforms and OWNS four externally named data globals (iggy_libraries,
iggy_load_imports, iggy_load_processing_num_libraries, last_abc).
THE LIBRARY TABLE (durango 64-bit -- the runtime test builds ONLY the durango
dylib so the 64-bit layout is authoritative):
iggy_libraries is a 0x20-slot array of 8-byte library pointers (0x100 bytes;
raw:5744/5918/5933). add_library indexes it by the signed byte at lib+0x234
and stores the pointer; get_library reads slot i back. The two round-trip
through the shared module-owned global, so the test drives both without a
direct .in_dll poke. Each fresh per-test dylib load starts the array (and the
file-static iggy_library_id_allocation bitmask) at zeroed BSS.
WHAT IS PINNED (real, deterministic DURANGO behaviour, raw line cites inline):
add_library stores lib at iggy_libraries[lib->id] and returns the id
(raw:5744-5745); get_library reads the slot (raw:5918); the two round-trip and
an untouched slot reads 0. get_library_id scans the 0x20 slots and returns the
index whose non-null library's name (lib+0x210) rrwcscmp-matches, else -1
(raw:5932-5944). get_export walks the lib+0x220 export table (count @ +0x21c,
stride 0x18, name @ +8) and returns entry[i].value @ +0 on a name match, else 0
(raw:5866-5878). get_libnum allocates the first clear bit of the id bitmask and
free_library_id clears a bit, so alloc/free/realloc round-trips (raw:5892-5908 /
5848-5851). get_placeobject_size is a pure flag-bit classifier returning
{0x40,0x48,0x50,0x58,0x60} (raw:5957-5971). decode_image dispatches on the tag
byte @ tag+0x25 and no-ops on an unhandled tag (raw:5757-5779).
decode_swf_from_memory with comp=='C' and a NULL iggy_zlib_decoder takes the
compressed-header reject path: iggy_send_error(ctx,0x3e9,0) then return 0
(raw:5806-5812).
DEFERRED (flagged structural, NOT exercised): decode_swf_from_memory's success
path (swf_decode_contents -- the whole tag-parse loop, raw:5831+); decode_image's
handled tags (late_definebitsjpeg / late_definebitslossless drive the jpeg / zlib
/ image_storage chain, raw:5976+); read_abc_file / the last_abc + iggy_load_imports
population. The 32-bit ps3 / vita layout (globals 0x80 / 0x300 / 0x78 -- half the
64-bit width) is noted for the implementer but NOT exercised by the durango-only
runtime test.
Full-suite runs use python3.12 (the machine python3 is 3.14, which SEGFAULTs at
teardown on the full suite via an unrelated CPython ctypes-GC bug -- exit 139 AFTER
the last "ok" with 0 fail/err is a teardown artifact, not a failure).
"""
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 / "swf_decode.c"
HEADER = RECONSTRUCTED / "swf_decode.h"
SUPPORT = ROOT / "tests/support/swf_decode_test_support.c"
MASK64 = (1 << 64) - 1
# 64-bit DURANGO library struct / table offsets (raw:5744/5918/5932-5944).
LIB_ID = 0x234 # signed byte: library slot index (raw:5744-5745).
LIB_NAME = 0x210 # ptr: library name (non-null gate for get_library_id).
LIB_EXPORT_COUNT = 0x21C # int: export entry count (raw:5867).
LIB_EXPORT_TABLE = 0x220 # ptr: export table base (raw:5870).
EXPORT_STRIDE = 0x18 # per-entry bytes: [+0]=value, [+8]=name ptr (raw:5870-5872).
EXPORT_VALUE = 0x00 # returned on a name match (raw:5872).
EXPORT_NAME = 0x08 # rrwcscmp'd against the query (raw:5870).
LIBRARY_SLOTS = 0x20 # get_library_id scans 32 slots (raw:5943).
# 64-bit DURANGO PlaceObject size classifier fields (raw:5957-5971).
PO_PTR30 = 0x30 # non-zero -> 0x60 (raw:5957).
PO_BYTE0A = 0x0A # != 0x7f -> 0x60 (raw:5957).
PO_FLAGS = 0x09 # flag byte driving 0x40/0x48/0x50/0x58 (raw:5960).
class ReconstructedSwfDecodeTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
missing = [str(path) for path in (SOURCE, HEADER, SUPPORT)
if not path.exists()]
if missing:
raise AssertionError(
"missing swf_decode runtime inputs: " + ", ".join(missing))
cls.temp = tempfile.TemporaryDirectory()
cls.base = Path(cls.temp.name) / "swf_decode.dylib"
build = subprocess.run([
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra",
"-Werror", "-Wl,-undefined,dynamic_lookup",
"-DIGGY_RECONSTRUCT_SWF_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"swf_decode_{self._testMethodName}.dylib")
shutil.copyfile(self.base, unique)
self.lib = ctypes.CDLL(str(unique))
self.lib.swf_decode_test_reset()
self._keep = []
# --- harness accessors -------------------------------------------- #
def _count(self, name):
fn = getattr(self.lib, f"swf_decode_test_{name}_count")
fn.restype = ctypes.c_size_t
fn.argtypes = []
return fn()
def _last_error(self):
self.lib.swf_decode_test_last_error_code.restype = ctypes.c_int32
return self.lib.swf_decode_test_last_error_code()
def _set_zlib_result(self, value):
fn = self.lib.swf_decode_test_set_zlib_decoder_result
fn.restype = None
fn.argtypes = [ctypes.c_void_p]
fn(ctypes.c_void_p(value))
# --- buffer helpers ----------------------------------------------- #
def _buf(self, size):
buf = (ctypes.c_uint64 * max((size + 7) // 8, 1))()
self._keep.append(buf)
return buf
def _addr(self, buf):
return ctypes.addressof(buf)
def _poke_u64(self, buf, offset, value):
ctypes.c_uint64.from_buffer(buf, offset).value = value & MASK64
def _poke_i32(self, buf, offset, value):
ctypes.c_int32.from_buffer(buf, offset).value = value
def _poke_u8(self, buf, offset, value):
ctypes.c_uint8.from_buffer(buf, offset).value = value & 0xFF
# --- export wrappers ---------------------------------------------- #
def _add_library(self, lib):
fn = self.lib.iggy_add_library
fn.restype = ctypes.c_int
fn.argtypes = [ctypes.c_void_p]
return fn(ctypes.c_void_p(self._addr(lib)))
def _get_library(self, index):
fn = self.lib.iggy_get_library
fn.restype = ctypes.c_uint64
fn.argtypes = [ctypes.c_int]
return fn(index)
def _get_library_id(self, name_ptr):
fn = self.lib.iggy_get_library_id
fn.restype = ctypes.c_int
fn.argtypes = [ctypes.c_void_p]
return fn(ctypes.c_void_p(name_ptr))
def _get_export(self, lib, name_ptr):
fn = self.lib.iggy_get_export
fn.restype = ctypes.c_uint64
fn.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
return fn(ctypes.c_void_p(self._addr(lib)), ctypes.c_void_p(name_ptr))
def _get_libnum(self):
fn = self.lib.iggy_get_libnum
fn.restype = ctypes.c_uint32
fn.argtypes = []
return fn()
def _free_library_id(self, index):
fn = self.lib.iggy_free_library_id
fn.restype = None
fn.argtypes = [ctypes.c_uint8]
fn(index)
def _placeobject_size(self, obj):
fn = self.lib.iggy_get_placeobject_size
fn.restype = ctypes.c_uint64
fn.argtypes = [ctypes.c_void_p]
return fn(ctypes.c_void_p(self._addr(obj)))
def _library(self, slot):
lib = self._buf(0x260)
self._poke_u8(lib, LIB_ID, slot)
return lib
# ------------------------------------------------------------------ #
# iggy_add_library / iggy_get_library -- round-trip through the owned
# iggy_libraries[0x20] pointer array.
# ------------------------------------------------------------------ #
def test_add_library_stores_at_id_slot_and_returns_id(self):
# raw:5744-5745 store lib at iggy_libraries[lib->id]; return the id.
lib = self._library(5)
self.assertEqual(self._add_library(lib), 5)
# raw:5918 get_library reads the same slot back.
self.assertEqual(self._get_library(5), self._addr(lib) & MASK64)
def test_get_library_untouched_slot_is_zero(self):
# raw:5918 a slot never written by add_library reads 0 (fresh BSS).
self.assertEqual(self._get_library(7), 0)
def test_add_library_round_trips_two_distinct_slots(self):
# raw:5744/5918 two libraries at different ids land in different slots.
lib_a = self._library(1)
lib_b = self._library(2)
self.assertEqual(self._add_library(lib_a), 1)
self.assertEqual(self._add_library(lib_b), 2)
self.assertEqual(self._get_library(1), self._addr(lib_a) & MASK64)
self.assertEqual(self._get_library(2), self._addr(lib_b) & MASK64)
# ------------------------------------------------------------------ #
# iggy_get_library_id -- name scan across the 0x20-slot table.
# ------------------------------------------------------------------ #
def test_get_library_id_matches_by_name(self):
# raw:5932-5944 scan the table; the slot whose non-null library name
# rrwcscmp-matches wins. The rrwcscmp stub matches by pointer identity,
# so a shared name buffer is the hit.
name_a = self._buf(0x10)
name_b = self._buf(0x10)
lib_a = self._library(0)
lib_b = self._library(1)
self._poke_u64(lib_a, LIB_NAME, self._addr(name_a))
self._poke_u64(lib_b, LIB_NAME, self._addr(name_b))
self._add_library(lib_a)
self._add_library(lib_b)
self.assertEqual(self._get_library_id(self._addr(name_b)), 1)
self.assertEqual(self._get_library_id(self._addr(name_a)), 0)
def test_get_library_id_miss_returns_minus_one(self):
# raw:5944 an exhausted scan returns -1.
name_a = self._buf(0x10)
lib_a = self._library(0)
self._poke_u64(lib_a, LIB_NAME, self._addr(name_a))
self._add_library(lib_a)
other = self._buf(0x10) # distinct pointer -> no match.
self.assertEqual(self._get_library_id(self._addr(other)), -1)
def test_get_library_id_skips_library_without_name(self):
# raw:5935 a library whose name field (lib+0x210) is 0 is skipped.
lib_a = self._library(0) # name left 0 -> never compared.
self._add_library(lib_a)
other = self._buf(0x10)
self.assertEqual(self._get_library_id(self._addr(other)), -1)
# ------------------------------------------------------------------ #
# iggy_get_export -- walk the per-library export table.
# ------------------------------------------------------------------ #
def _lib_with_exports(self, entries):
# entries: list of (value, name_buf). Builds the lib+0x220 table.
lib = self._buf(0x260)
table = self._buf(len(entries) * EXPORT_STRIDE)
self._poke_i32(lib, LIB_EXPORT_COUNT, len(entries))
self._poke_u64(lib, LIB_EXPORT_TABLE, self._addr(table))
for i, (value, name_buf) in enumerate(entries):
base = i * EXPORT_STRIDE
self._poke_u64(table, base + EXPORT_VALUE, value)
self._poke_u64(table, base + EXPORT_NAME, self._addr(name_buf))
return lib
def test_get_export_returns_value_on_name_match(self):
# raw:5870-5872 a name match returns entry[i].value at table+i*0x18.
name0 = self._buf(0x10)
name1 = self._buf(0x10)
lib = self._lib_with_exports([(0xA11CE, name0), (0xB0B, name1)])
self.assertEqual(self._get_export(lib, self._addr(name1)), 0xB0B)
self.assertEqual(self._get_export(lib, self._addr(name0)), 0xA11CE)
self.assertGreater(self._count("wcscmp"), 0)
def test_get_export_miss_returns_zero(self):
# raw:5878 no matching export returns 0.
name0 = self._buf(0x10)
lib = self._lib_with_exports([(0xA11CE, name0)])
other = self._buf(0x10)
self.assertEqual(self._get_export(lib, self._addr(other)), 0)
def test_get_export_empty_table_returns_zero(self):
# raw:5867 a zero count never enters the loop -> 0.
lib = self._buf(0x260)
self._poke_i32(lib, LIB_EXPORT_COUNT, 0)
other = self._buf(0x10)
self.assertEqual(self._get_export(lib, self._addr(other)), 0)
# ------------------------------------------------------------------ #
# iggy_get_libnum / iggy_free_library_id -- id bitmask alloc/free.
# ------------------------------------------------------------------ #
def test_get_libnum_allocates_sequential_ids(self):
# raw:5892-5900 fresh bitmask (zeroed BSS) hands out 0, 1, 2 in order.
self.assertEqual(self._get_libnum(), 0)
self.assertEqual(self._get_libnum(), 1)
self.assertEqual(self._get_libnum(), 2)
def test_free_library_id_makes_slot_reusable(self):
# raw:5848-5851 freeing an id clears its bit so the next alloc reuses it.
self.assertEqual(self._get_libnum(), 0)
self.assertEqual(self._get_libnum(), 1)
self.assertEqual(self._get_libnum(), 2)
self._free_library_id(1)
self.assertEqual(self._get_libnum(), 1)
# ------------------------------------------------------------------ #
# iggy_get_placeobject_size -- flag-bit size classifier (pure).
# ------------------------------------------------------------------ #
def _place_obj(self, flags=0, byte0a=0x7F, ptr30=0):
obj = self._buf(0x40)
self._poke_u64(obj, PO_PTR30, ptr30)
self._poke_u8(obj, PO_BYTE0A, byte0a)
self._poke_u8(obj, PO_FLAGS, flags)
return obj
def test_placeobject_size_ptr30_or_wrong_marker_is_0x60(self):
# raw:5957-5958 a non-zero ptr@+0x30 forces 0x60.
self.assertEqual(self._placeobject_size(self._place_obj(ptr30=1)), 0x60)
# a byte@+0xa other than 0x7f also forces 0x60.
self.assertEqual(
self._placeobject_size(self._place_obj(byte0a=0x10)), 0x60)
def test_placeobject_size_flag_bit_0x20_is_0x58(self):
# raw:5961-5962 flag bit 0x20 set -> 0x58.
self.assertEqual(self._placeobject_size(self._place_obj(flags=0x20)),
0x58)
def test_placeobject_size_flag_bit_2_is_0x50(self):
# raw:5970 flag bit 0x02 set (and bit 0x20 clear) -> 0x50.
self.assertEqual(self._placeobject_size(self._place_obj(flags=0x02)),
0x50)
def test_placeobject_size_flag_bit_0x40_is_0x48(self):
# raw:5964-5965 bit 0x02 clear, bit 0x40 set -> 0x48.
self.assertEqual(self._placeobject_size(self._place_obj(flags=0x40)),
0x48)
def test_placeobject_size_no_flags_is_0x40(self):
# raw:5966-5967 bits 0x02/0x40 clear -> 0x40 (the base size).
self.assertEqual(self._placeobject_size(self._place_obj(flags=0x00)),
0x40)
# ------------------------------------------------------------------ #
# iggy_decode_image -- tag dispatch (unhandled tag no-ops).
# ------------------------------------------------------------------ #
def test_decode_image_unhandled_tag_is_noop(self):
# raw:5757-5779 a tag byte outside {0x06,0x14,0x15,0x23,0x24} hits no
# branch -> late_definebits* never runs -> no import is reached.
ctx = self._buf(0x40)
tag = self._buf(0x40)
self._poke_u8(tag, 0x25, 0x01) # unhandled tag code.
fn = self.lib.iggy_decode_image
fn.restype = None
fn.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_uint32]
fn(ctypes.c_void_p(self._addr(ctx)), ctypes.c_void_p(self._addr(tag)),
None, 0)
self.assertEqual(self._count("zlib_decoder"), 0)
self.assertEqual(self._count("send_error"), 0)
# ------------------------------------------------------------------ #
# iggy_decode_swf_from_memory -- compressed-header reject path.
# ------------------------------------------------------------------ #
def test_decode_swf_compressed_null_decoder_rejects(self):
# raw:5806-5812 comp=='C' opens iggy_zlib_decoder; a NULL result ->
# iggy_send_error(ctx,0x3e9,0) and return 0. The zlib stub defaults to
# NULL, so this path is taken.
ctx = self._buf(0x10000) # generous: covers all header field reads.
data = self._buf(0x40)
self._set_zlib_result(0)
fn = self.lib.iggy_decode_swf_from_memory
fn.restype = ctypes.c_uint32
fn.argtypes = [ctypes.c_void_p, ctypes.c_char, ctypes.c_void_p,
ctypes.c_int64, ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_uint32]
result = fn(ctypes.c_void_p(self._addr(ctx)), b"C",
ctypes.c_void_p(self._addr(data)), 0x20,
None, None, None, 0)
self.assertEqual(result, 0)
self.assertEqual(self._count("zlib_decoder"), 1)
self.assertEqual(self._count("send_error"), 1)
self.assertEqual(self._last_error(), 0x3E9)
if __name__ == "__main__":
unittest.main()