Files
neoLegacy-mac/tests/test_reconstructed_as3vm_create.py
2026-07-18 02:10:53 -05:00

496 lines
23 KiB
Python

"""Runtime tests for the reconstructed as3vm_create translation unit.
RED STATE: as3vm_create.c / as3vm_create.h do not exist yet, so setUpClass
raises "missing as3vm_create runtime inputs". Every assertion below is grounded
in a specific line of iggy-decompiled/raw/durango/as3vm_create.c (cited inline)
so a faithful DURANGO reconstruction turns these green.
as3vm_create is Iggy's AS3 / AVM2 GC-managed value construction layer: it boxes
int / uint / number / namespace / string / array / object / closure values,
builds arraydata + stringdata backing buffers, mints interned + permanent
strings, and lays down object headers with traits / proto / constructor slots.
It exports 31 entry points and owns NO externally named data global -- every
table it touches (built-in class prototypes at vm+0x2830 / 0x2850 / 0x2868, the
GC root stack at vm+0x6b18) lives INSIDE the caller's VM context, allocated
fresh per test here, so each test is naturally isolated; a pristine dylib copy
is loaded per method as well, so the harness bump arena also starts clean.
VALUE TAGGING (raw:239-247, 260-268, 825-832; cross-checked vita 32-bit):
small integer -> (value << 1), low bit CLEAR, no heap allocation
heap value -> (node_pointer + 1), low bit SET (node is 16-aligned)
The heap node header is node[0]=kind byte, node[1]=GC flag byte, node[4..]=a
size/type word, node[8]=first payload slot (double value / stringdata pointer /
traits vtable).
What is pinned is real, deterministic DURANGO behavior: the int / uint / number
boxing + tagging and its heap fallback, the number special cases (0, +/-1,
integral shift), the stringdata char copy, the namespace header, the lazy
object two-phase (make_object_lazy_GC + finish_object_lazy filling the proto
slots), the traits-object finalizer gate, the function-closure impl-pointer
capture, the string-object wrap through the GC root stack, the interned-string
path through rrUTF8toUTF16 + as3vm_find_intern_string, and the non-GC permanent
string builder. The deepest paths -- make_class_closure_GC's qname property
naming, make_static_object / make_static_array's player-table walk,
make_array_with_traits_GC's full write-barrier slot loop, and
make_string_utf8_GC's alloca fast path -- are intentionally NOT exercised here
and are flagged in the module report.
"""
import ctypes
import shutil
from pathlib import Path
import struct
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
SOURCE = RECONSTRUCTED / "as3vm_create.c"
HEADER = RECONSTRUCTED / "as3vm_create.h"
SUPPORT = ROOT / "tests/support/as3vm_create_test_support.c"
# 64-bit DURANGO VM-context offsets (raw, cited per test). The makers reach
# through the caller-owned VM for the built-in class prototypes and the GC root
# stack; the tests wire just the slots each maker touches.
VM_OBJECT_CLASS = 0x2830 # raw:11,422,481 Object class ptr
VM_FUNCTION_CLASS = 0x2850 # raw:199 Function class ptr
VM_ARRAY_CLASS = 0x2868 # raw:54,222 Array class ptr
VM_ROOT_STACK = 0x6B18 # raw:48,312,477,605 GC root-stack top pointer
VM_BYTES = 0x7000 # cover the highest touched offset (0x6b18)
CLASS_PROTO_SLOT = 0x48 # raw:11 class + 0x48 -> proto/vtable array pointer
def _u8(base, offset):
return ctypes.c_uint8.from_address(base + offset).value
def _u16(base, offset):
return ctypes.c_uint16.from_address(base + offset).value
def _u32(base, offset):
return ctypes.c_uint32.from_address(base + offset).value
def _u64(base, offset):
return ctypes.c_uint64.from_address(base + offset).value
class ReconstructedAs3vmCreateTests(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_create runtime inputs: " + ", ".join(missing))
cls.temp = tempfile.TemporaryDirectory()
cls.base = Path(cls.temp.name) / "as3vm_create.dylib"
build = subprocess.run([
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra",
"-Werror", "-Wl,-undefined,dynamic_lookup",
"-DIGGY_RECONSTRUCT_AS3VM_CREATE_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):
# Load a PRISTINE copy so each test gets a fresh harness bump arena.
unique = (Path(self.temp.name) /
f"as3vm_create_{self._testMethodName}.dylib")
shutil.copyfile(self.base, unique)
self.lib = ctypes.CDLL(str(unique))
self._bind(self.lib)
self.lib.as3vm_create_test_reset()
self._keep = []
def _bind(self, lib):
lib.as3vm_create_test_reset.argtypes = []
lib.as3vm_create_test_reset.restype = None
for name in (
"gc_alloc", "gc_alloc_remembered", "write_barrier",
"register_finalizer", "set_property_name",
"qname_set_dont_enum", "find_intern_string",
"arena_malloc_aligned", "last_alloc_words"):
fn = getattr(lib, f"as3vm_create_test_{name}_count"
if name != "last_alloc_words"
else "as3vm_create_test_last_alloc_words")
fn.restype = ctypes.c_size_t
fn.argtypes = []
for name in ("last_alloc_ptr", "last_finalizer_ptr",
"functionclosure_impl_addr", "methodclosure_impl_addr"):
fn = getattr(lib, f"as3vm_create_test_{name}")
fn.restype = ctypes.c_void_p
fn.argtypes = []
lib.as3vm_create_test_last_intern_create.restype = ctypes.c_int32
lib.as3vm_create_test_last_intern_create.argtypes = []
lib.as3vm_create_test_set_intern_result.restype = None
lib.as3vm_create_test_set_intern_result.argtypes = [ctypes.c_void_p]
void_p = ctypes.c_void_p
u64 = ctypes.c_uint64
# Makers Ghidra typed with a return value -> read the tagged result.
lib.as3vm_make_int_GC.restype = u64
lib.as3vm_make_int_GC.argtypes = [void_p, ctypes.c_uint32]
lib.as3vm_make_uint_GC.restype = u64
lib.as3vm_make_uint_GC.argtypes = [void_p, ctypes.c_uint32]
lib.as3vm_make_integer_GC.restype = u64
lib.as3vm_make_integer_GC.argtypes = [void_p, ctypes.c_double]
lib.as3vm_make_number_GC.restype = u64
lib.as3vm_make_number_GC.argtypes = [void_p, ctypes.c_double]
lib.as3vm_make_namespace.restype = u64
lib.as3vm_make_namespace.argtypes = [void_p]
lib.as3vm_make_stringdata_GC.restype = u64
lib.as3vm_make_stringdata_GC.argtypes = [void_p, void_p, ctypes.c_int32]
lib.as3vm_make_object_lazy_GC.restype = u64
lib.as3vm_make_object_lazy_GC.argtypes = [void_p, ctypes.c_uint8]
lib.as3vm_make_object_with_traits_GC.restype = u64
lib.as3vm_make_object_with_traits_GC.argtypes = [
void_p, void_p, ctypes.c_int32]
lib.as3vm_make_string_GC.restype = u64
lib.as3vm_make_string_GC.argtypes = [void_p, void_p]
lib.as3vm_make_string_permanent.restype = u64
lib.as3vm_make_string_permanent.argtypes = [void_p, void_p]
lib.as3vm_make_interned_string_c.restype = u64
lib.as3vm_make_interned_string_c.argtypes = [void_p, ctypes.c_char_p]
# Makers Ghidra typed void -> inspect the recorded gc_alloc buffer.
lib.as3vm_make_number_not_int_GC.restype = None
lib.as3vm_make_number_not_int_GC.argtypes = [void_p, ctypes.c_uint64]
lib.as3vm_make_function_closure_GC.restype = None
lib.as3vm_make_function_closure_GC.argtypes = [void_p, void_p]
lib.as3vm_finish_object_lazy.restype = None
lib.as3vm_finish_object_lazy.argtypes = [void_p, void_p]
def _count(self, name):
return getattr(self.lib, f"as3vm_create_test_{name}_count")()
def _vm(self):
vm = (ctypes.c_uint8 * VM_BYTES)()
self._keep.append(vm)
return ctypes.addressof(vm)
def _wire_root_stack(self, vm):
# raw:48 *(vm+0x6b18) is a pointer to the operand-stack top; the makers
# push their argument, gc_alloc, then reload it. A 16-slot scratch
# stack is ample for the single push each maker performs.
stack = (ctypes.c_uint64 * 16)()
self._keep.append(stack)
ctypes.c_uint64.from_address(vm + VM_ROOT_STACK).value = \
ctypes.addressof(stack)
return stack
def _wire_class(self, vm, class_offset, proto0=0xCAFEF00D):
# raw:11 *(vm+class_offset) -> class; class+0x48 -> proto array;
# proto[0] is the value copied into the new node's proto slot.
proto = (ctypes.c_uint64 * 4)(proto0, 0, 0, 0)
klass = (ctypes.c_uint8 * 0x80)()
self._keep += [proto, klass]
klass_addr = ctypes.addressof(klass)
ctypes.c_uint64.from_address(klass_addr + CLASS_PROTO_SLOT).value = \
ctypes.addressof(proto)
ctypes.c_uint64.from_address(vm + class_offset).value = klass_addr
return ctypes.addressof(proto), proto0
# ------------------------------------------------------------------ #
# int / uint boxing + tagging
# ------------------------------------------------------------------ #
def test_make_int_small_returns_shifted_tagged_int(self):
# raw:239-240 a value in [-2^30, 2^30) returns (value*2) with a clear
# low bit and never touches the GC arena.
vm = self._vm()
self.assertEqual(self.lib.as3vm_make_int_GC(vm, 5), 10)
self.assertEqual(self.lib.as3vm_make_int_GC(vm, 0), 0)
# (uint)-1 is out of range; use the largest in-range magnitude instead.
self.assertEqual(self.lib.as3vm_make_int_GC(vm, 0x3FFFFFFF),
0x3FFFFFFF * 2)
self.assertEqual(self._count("gc_alloc"), 0)
def test_make_int_large_boxes_double_on_heap(self):
# raw:242-247 out of range -> gc_alloc, node[0]=7, node[4]=2,
# node[8]=double(value), return node+1 (tagged pointer).
vm = self._vm()
result = self.lib.as3vm_make_int_GC(vm, 0x40000000)
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(result & 1, 1) # heap tag
node = result - 1
self.assertEqual(_u8(node, 0x00), 7)
self.assertEqual(_u32(node, 0x04), 2)
self.assertEqual(struct.unpack("<d", struct.pack(
"<Q", _u64(node, 0x08)))[0], float(0x40000000))
def test_make_uint_small_and_large(self):
# raw:825-831 < 2^30 -> (value*2); otherwise a heap double node.
vm = self._vm()
self.assertEqual(self.lib.as3vm_make_uint_GC(vm, 3), 6)
self.assertEqual(self._count("gc_alloc"), 0)
result = self.lib.as3vm_make_uint_GC(vm, 0x40000000)
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(result & 1, 1)
self.assertEqual(_u8(result - 1, 0x00), 7)
def test_make_integer_floors_in_range_and_boxes_outside(self):
# raw:260-268 in [-2^30, 2^30) -> floor(d) then (int*2); a fractional
# value truncates toward -inf; out of range -> heap double node.
vm = self._vm()
self.assertEqual(self.lib.as3vm_make_integer_GC(vm, 5.0), 10)
self.assertEqual(self.lib.as3vm_make_integer_GC(vm, 5.9), 10) # floor
self.assertEqual(self._count("gc_alloc"), 0)
big = self.lib.as3vm_make_integer_GC(vm, 5.0e9)
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(big & 1, 1)
self.assertEqual(_u8(big - 1, 0x00), 7)
# ------------------------------------------------------------------ #
# number boxing -- specials + integral shift + heap fallback
# ------------------------------------------------------------------ #
def test_make_number_special_cases_and_boxing(self):
# raw:370-391 in range: 0.0 -> 0; |d|==1.0 -> +/-2; an integral value
# -> (int*2); a fractional in-range value -> heap node storing the raw
# double bits; out of range -> heap.
vm = self._vm()
self.assertEqual(self.lib.as3vm_make_number_GC(vm, 0.0), 0)
self.assertEqual(self.lib.as3vm_make_number_GC(vm, 1.0), 2)
self.assertEqual(self.lib.as3vm_make_number_GC(vm, -1.0),
0xFFFFFFFFFFFFFFFE) # (-1)*2
self.assertEqual(self.lib.as3vm_make_number_GC(vm, 3.0), 6)
self.assertEqual(self._count("gc_alloc"), 0)
boxed = self.lib.as3vm_make_number_GC(vm, 2.5)
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(boxed & 1, 1)
node = boxed - 1
self.assertEqual(_u8(node, 0x00), 7)
self.assertEqual(struct.unpack("<d", struct.pack(
"<Q", _u64(node, 0x08)))[0], 2.5)
def test_make_number_not_int_always_boxes(self):
# raw:403-406 unconditional heap node: gc_alloc(2), node[4]=2,
# node[0]=7, node[8]=the passed 64-bit double payload (verbatim).
vm = self._vm()
bits = struct.unpack("<Q", struct.pack("<d", 6.25))[0]
self.lib.as3vm_make_number_not_int_GC(vm, bits)
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(self.lib.as3vm_create_test_last_alloc_words(), 2)
node = self.lib.as3vm_create_test_last_alloc_ptr()
self.assertEqual(_u8(node, 0x00), 7)
self.assertEqual(_u32(node, 0x04), 2)
self.assertEqual(_u64(node, 0x08), bits)
# ------------------------------------------------------------------ #
# namespace -- flat header, no proto deref
# ------------------------------------------------------------------ #
def test_make_namespace_header(self):
# raw:345-350 gc_alloc(3): node[0]=0xc, node[4]=3, node[2]=7,
# node[0x10]=0, return node+1.
vm = self._vm()
result = self.lib.as3vm_make_namespace(vm)
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(self.lib.as3vm_create_test_last_alloc_words(), 3)
self.assertEqual(result & 1, 1)
node = result - 1
self.assertEqual(_u8(node, 0x00), 0xC)
self.assertEqual(_u32(node, 0x04), 3)
self.assertEqual(_u8(node, 0x02), 7)
self.assertEqual(_u64(node, 0x10), 0)
# ------------------------------------------------------------------ #
# stringdata -- utf16 char copy + min size
# ------------------------------------------------------------------ #
def test_make_stringdata_copies_utf16_chars(self):
# raw:762-775 size = max(len+1,4) rounded to words; node[0]=1,
# node[8]=len, chars memcpy'd at node+0xc, return node+1.
vm = self._vm()
text = "abcdef"
chars = (ctypes.c_uint16 * len(text))(*[ord(c) for c in text])
self._keep.append(chars)
result = self.lib.as3vm_make_stringdata_GC(
vm, ctypes.addressof(chars), len(text))
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(result & 1, 1)
node = result - 1
self.assertEqual(_u8(node, 0x00), 1)
self.assertEqual(_u32(node, 0x08), len(text))
for i, ch in enumerate(text):
self.assertEqual(_u16(node, 0x0C + i * 2), ord(ch))
def test_make_stringdata_null_source_skips_copy(self):
# raw:762-774 a NULL source still allocates a node and records the
# length, but performs no memcpy.
vm = self._vm()
result = self.lib.as3vm_make_stringdata_GC(vm, None, 0)
self.assertEqual(self._count("gc_alloc"), 1)
node = result - 1
self.assertEqual(_u8(node, 0x00), 1)
self.assertEqual(_u32(node, 0x08), 0)
# ------------------------------------------------------------------ #
# lazy object two-phase -- make_object_lazy_GC + finish_object_lazy
# ------------------------------------------------------------------ #
def test_object_lazy_then_finish_fills_proto(self):
# raw:440-445 lazy alloc: gc_alloc_remembered(5), node[0]=kind,
# node[4]=5, node[2]=0, node[0x10]=0, return node+1.
# raw:11-13 finish: proto = *(*(vm+0x2830)+0x48); tagged+7 (=node+8) =
# proto array; tagged+0x1f (=node+0x20) = proto[0].
vm = self._vm()
proto_addr, proto0 = self._wire_class(vm, VM_OBJECT_CLASS)
tagged = self.lib.as3vm_make_object_lazy_GC(vm, 0x23)
self.assertEqual(self._count("gc_alloc_remembered"), 1)
self.assertEqual(self.lib.as3vm_create_test_last_alloc_words(), 5)
self.assertEqual(tagged & 1, 1)
node = tagged - 1
self.assertEqual(_u8(node, 0x00), 0x23)
self.assertEqual(_u32(node, 0x04), 5)
self.assertEqual(_u8(node, 0x02), 0)
self.lib.as3vm_finish_object_lazy(vm, ctypes.c_void_p(tagged))
self.assertEqual(_u64(node, 0x08), proto_addr)
self.assertEqual(_u64(node, 0x20), proto0)
# ------------------------------------------------------------------ #
# traits object -- finalizer gate
# ------------------------------------------------------------------ #
def _traits(self, slot_count=0):
# raw:503-513 traits struct: +0x10 (u16) = slot count, +0x12 (u16) =
# a copied flag word, +0x30 (ptr) = the slot template memcpy'd in.
traits = (ctypes.c_uint8 * 0x40)()
template = (ctypes.c_uint64 * max(slot_count, 1))()
self._keep += [traits, template]
addr = ctypes.addressof(traits)
ctypes.c_uint16.from_address(addr + 0x10).value = slot_count
ctypes.c_uint16.from_address(addr + 0x12).value = 0xABCD
ctypes.c_uint64.from_address(addr + 0x30).value = \
ctypes.addressof(template)
return addr
def test_make_object_with_traits_finalizer_gate(self):
# raw:494-529 gc_alloc(count+5), node[0]=0x23, node[6]=flag word,
# node[4]=count+5; a non-zero param_3 registers a finalizer, zero does
# not. With a zero slot count no write barrier fires (flags byte 0).
vm = self._vm()
traits = self._traits(slot_count=0)
tagged = self.lib.as3vm_make_object_with_traits_GC(vm, traits, 0)
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(self.lib.as3vm_create_test_last_alloc_words(), 5)
self.assertEqual(self._count("register_finalizer"), 0)
node = tagged - 1
self.assertEqual(_u8(node, 0x00), 0x23)
self.assertEqual(_u16(node, 0x06), 0xABCD)
self.assertEqual(_u16(node, 0x04), 5)
again = self.lib.as3vm_make_object_with_traits_GC(vm, traits, 1)
self.assertEqual(self._count("register_finalizer"), 1)
self.assertEqual(self.lib.as3vm_create_test_last_finalizer_ptr(),
again)
# ------------------------------------------------------------------ #
# function closure -- captures the implementation pointer
# ------------------------------------------------------------------ #
def test_make_function_closure_captures_impl_pointer(self):
# raw:195-204 gc_alloc(8), node[0]=0x14, node[4]=0x20008,
# node+0x10 = &as3vm_functionclosure_implementation, node+0x28 = param.
vm = self._vm()
proto_addr, proto0 = self._wire_class(vm, VM_FUNCTION_CLASS)
method = 0x1234560
self.lib.as3vm_make_function_closure_GC(vm, ctypes.c_void_p(method))
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(self.lib.as3vm_create_test_last_alloc_words(), 8)
node = self.lib.as3vm_create_test_last_alloc_ptr()
self.assertEqual(_u8(node, 0x00), 0x14)
self.assertEqual(_u32(node, 0x04), 0x20008)
self.assertEqual(
_u64(node, 0x10),
self.lib.as3vm_create_test_functionclosure_impl_addr())
self.assertEqual(_u64(node, 0x28), method)
self.assertEqual(_u64(node, 0x08), proto_addr)
self.assertEqual(_u64(node, 0x20), proto0)
# ------------------------------------------------------------------ #
# string object -- wraps a stringdata through the GC root stack
# ------------------------------------------------------------------ #
def test_make_string_GC_wraps_stringdata(self):
# raw:599-617 push param onto the root stack, gc_alloc(2), reload it,
# node[0]=8, node[4]=0x10002, node[8]=the stringdata pointer, node+1.
# An even (untagged) stringdata pointer keeps the low-bit write barrier
# dormant (raw:613).
vm = self._vm()
self._wire_root_stack(vm)
stringdata = (ctypes.c_uint8 * 32)()
self._keep.append(stringdata)
sd_addr = ctypes.addressof(stringdata)
self.assertEqual(sd_addr & 1, 0)
tagged = self.lib.as3vm_make_string_GC(vm, ctypes.c_void_p(sd_addr))
self.assertEqual(self._count("gc_alloc"), 1)
self.assertEqual(self.lib.as3vm_create_test_last_alloc_words(), 2)
self.assertEqual(self._count("write_barrier"), 0)
node = tagged - 1
self.assertEqual(_u8(node, 0x00), 8)
self.assertEqual(_u32(node, 0x04), 0x10002)
self.assertEqual(_u64(node, 0x08), sd_addr)
# ------------------------------------------------------------------ #
# interned string -- rrUTF8toUTF16 + as3vm_find_intern_string
# ------------------------------------------------------------------ #
def test_make_interned_string_c_interns_and_tags(self):
# raw:296-298 convert the C string to UTF-16, look it up with create=1,
# and return the interned node + 1 (low-bit tagged).
vm = self._vm()
intern = (ctypes.c_uint8 * 16)()
self._keep.append(intern)
intern_addr = ctypes.addressof(intern)
self.lib.as3vm_create_test_set_intern_result(
ctypes.c_void_p(intern_addr))
result = self.lib.as3vm_make_interned_string_c(vm, b"hello")
self.assertEqual(self._count("find_intern_string"), 1)
self.assertEqual(self.lib.as3vm_create_test_last_intern_create(), 1)
self.assertEqual(result, intern_addr + 1)
# ------------------------------------------------------------------ #
# permanent string -- non-GC arena builder
# ------------------------------------------------------------------ #
def test_make_string_permanent_builds_via_arena(self):
# raw:694-717 rrwcslen the UTF-16 source, iggy_arena_malloc_aligned the
# char buffer + a 0x10-byte wrapper (2 arena allocs, 0 gc_alloc); the
# wrapper node[0]=8, node[1]=0x30, node+8 -> the char buffer (+1).
vm = self._vm()
text = "world"
chars = (ctypes.c_uint16 * (len(text) + 1))(
*([ord(c) for c in text] + [0]))
player = (ctypes.c_uint64 * 2)()
self._keep += [chars, player]
wrapped = self.lib.as3vm_make_string_permanent(
ctypes.addressof(player), ctypes.addressof(chars))
self.assertEqual(self._count("arena_malloc_aligned"), 2)
self.assertEqual(self._count("gc_alloc"), 0)
self.assertEqual(wrapped & 1, 1)
node = wrapped - 1
self.assertEqual(_u8(node, 0x00), 8)
self.assertEqual(_u8(node, 0x01), 0x30)
self.assertNotEqual(_u64(node, 0x08), 0)
if __name__ == "__main__":
unittest.main()