rebuild iggy's as3vm value construction

This commit is contained in:
coah
2026-07-18 02:10:53 -05:00
parent c23cd9041f
commit 107f290135
9 changed files with 2033 additions and 0 deletions

View File

@@ -425,6 +425,22 @@ foreach(platform IN ITEMS durango orbis ps3 vita)
endif()
endforeach()
foreach(platform IN ITEMS durango orbis ps3 vita)
string(TOUPPER ${platform} platform_upper)
set(target iggy_reconstructed_as3vm_create_${platform})
add_library(${target} STATIC as3vm_create.c)
target_compile_definitions(
${target}
PRIVATE IGGY_RECONSTRUCT_AS3VM_CREATE_${platform_upper}
)
configure_iggy_reconstructed_target(${target} c_std_11)
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
target_compile_options(${target} PRIVATE -fno-builtin)
elseif(MSVC)
target_compile_options(${target} PRIVATE /Oi-)
endif()
endforeach()
add_library(iggy_reconstructed_win32_rrThreads2_durango STATIC win32_rrThreads2.c)
target_compile_definitions(
iggy_reconstructed_win32_rrThreads2_durango

View File

@@ -398,6 +398,22 @@ The 64-bit and 32-bit table and node layouts, per-platform toolchain differences
and the exact import set match all four builds; the hash, probe, and rehash math
were verified against the retail object disassembly.
`as3vm_create.c` restores the AS3/AVM2 GC-managed value construction layer — the
makers for boxed numbers, strings, objects, arrays, closures, and namespaces. Its
four builds (32 functions each) expose all 31 make entry points with no owned
globals. It reconstructs the value-tagging scheme (small integers as `value<<1`
with the low bit clear, heap values as `node+1`), the exact integer-range and
integral-versus-fractional boxing decisions, the GC allocation discipline
(root-stack push before allocation, pointer reload after, and the gated write
barrier), the two-phase lazy object build, the traits/proto/constructor object
and arraydata-backed array makers, the function/method/class closure capture with
qualified-name property naming, and the interned/permanent/UTF-8/wide string
builders. The 64-bit and 32-bit node layouts and header fields, the per-platform
`floor`/`strlen` inline-versus-call split (with no `memset` on any target), and
the exact import set were verified against the retail object disassembly on all
four builds. The GC allocator, write barrier, and interning primitives remain
imported.
Configure this directory directly with CMake to build the reconstructed units.
Every added module must compile independently and have behavioral tests before
it is listed in `modules.tsv` as complete.

View File

@@ -0,0 +1,682 @@
/* Reconstructed translation unit for RAD's Iggy AS3 / AVM2 value construction. */
/* Source-equivalent recovery of c:\projects\g\src\flash\as3vm_create.c */
#include <stddef.h>
#include <stdint.h>
#include "as3vm_create.h"
#if (defined(IGGY_RECONSTRUCT_AS3VM_CREATE_DURANGO) + \
defined(IGGY_RECONSTRUCT_AS3VM_CREATE_ORBIS) + \
defined(IGGY_RECONSTRUCT_AS3VM_CREATE_PS3) + \
defined(IGGY_RECONSTRUCT_AS3VM_CREATE_VITA)) != 1
#error "select exactly one as3vm_create target"
#endif
#if defined(__clang__) || defined(__GNUC__)
#define IGGY_USED __attribute__((used))
#else
#define IGGY_USED
#endif
/* durango/orbis are LP64; ps3/vita are ILP32. */
#if defined(IGGY_RECONSTRUCT_AS3VM_CREATE_DURANGO) || \
defined(IGGY_RECONSTRUCT_AS3VM_CREATE_ORBIS)
#define IGGY_AS3_64BIT 1
#else
#define IGGY_AS3_64BIT 0
#endif
/* Provenance recorded exactly as the upstream Windows toolchain emitted it. */
static const char IGGY_USED as3vm_create_source_path[] =
"c:\\projects\\g\\src\\flash\\as3vm_create.c";
/* ------------------------------------------------------------------ */
/* Recovered external dependencies (the genuine nm ' U ' imports). */
/* ------------------------------------------------------------------ */
extern void *as3vm_gc_alloc(void *vm, uint32_t words);
extern void *as3vm_gc_alloc_remembered(void *vm, uint32_t words);
extern void as3vm_write_barrier_slowpath(void *vm, void *node, void *target);
extern void as3vm_register_finalizer(void *vm, void *object);
extern void as3vm_set_property_name_GC(void *vm, void *name, void *qname,
void *value, int32_t flag);
extern void as3vm_qname_set_dont_enum_GC(void *vm, void *name, void *qname);
extern void *as3vm_find_intern_string(void *vm, size_t units, const void *chars,
int32_t create);
extern void as3vm_functionclosure_implementation(void);
extern void as3vm_methodclosure_implementation(void);
/* null/undefined sentinel DATA symbol (referenced only by address). */
extern uint8_t as3vm_nullundef_storage[];
extern void *iggy_arena_malloc_aligned(void *player, void *heap, uint32_t size,
int32_t align, int32_t tag);
extern int32_t rrUTF8toUTF16(uint16_t *destination, int32_t destination_units,
const void *source, int32_t source_bytes,
int32_t replacement, int32_t truncate);
extern int32_t rrwcslen(const void *text);
extern void *memcpy(void *destination, const void *source, size_t size);
/* Per-platform libc/libm: floor is a call on durango/ps3/vita and inlined on
* orbis; strlen is a call on orbis/ps3/vita and inlined on durango. */
#if !defined(IGGY_RECONSTRUCT_AS3VM_CREATE_ORBIS)
extern double floor(double value);
#endif
#if !defined(IGGY_RECONSTRUCT_AS3VM_CREATE_DURANGO)
extern size_t strlen(const char *string);
#endif
#if defined(__APPLE__)
#pragma weak as3vm_gc_alloc
#pragma weak as3vm_gc_alloc_remembered
#pragma weak as3vm_write_barrier_slowpath
#pragma weak as3vm_register_finalizer
#pragma weak as3vm_set_property_name_GC
#pragma weak as3vm_qname_set_dont_enum_GC
#pragma weak as3vm_find_intern_string
#pragma weak as3vm_functionclosure_implementation
#pragma weak as3vm_methodclosure_implementation
#pragma weak as3vm_nullundef_storage
#pragma weak iggy_arena_malloc_aligned
#pragma weak rrUTF8toUTF16
#pragma weak rrwcslen
#endif
/* ------------------------------------------------------------------ */
/* VM-context byte offsets + node field offsets recovered from raw. */
/* ------------------------------------------------------------------ */
#if IGGY_AS3_64BIT
#define VM_OBJECT_CLASS 0x2830 /* raw/durango:11,422,481 */
#define VM_FUNCTION_CLASS 0x2850 /* raw/durango:199 */
#define VM_ARRAY_CLASS 0x2868 /* raw/durango:54,222 */
#define VM_ARRAY_MARK 0x28c8 /* raw/durango:90 */
#define VM_ROOT_STACK 0x6b18 /* raw/durango:48,312,477,605 */
#define VM_QNAME_CTOR 0xdc0 /* raw/durango:170 */
#define VM_QNAME_NAME 0xda8 /* raw/durango:175 */
#define CLASS_PROTO 0x48 /* class + 0x48 -> proto/vtable array */
#define STATIC_ARR_CLASS_IDX 0x50d /* player[0x50d] -> array class */
#define WORD 8
#define N_ID 0x10
#define N_PROTO0 0x20
#define N_TRAITS 0x28
#define N_ARRPTR 0x30
#define N_FC_IMPL 0x10
#define N_FC_PARAM 0x28
#define N_MC_IMPL 0x10
#define N_MC_RCV 0x18
#define TAG_PROTO0 0x1f
#define TAG_ID 0x0f
#define NUM_WORDS 2
#define NS_WORDS 3
#define OBJ_WORDS 5
#define PROTO_WORDS 6
#define FC_WORDS 8
#define MC_WORDS 4
#define STR_WORDS 2
#define ARR_WORDS 7
#define ARRDATA_EXTRA 4
#define TRAITS_EXTRA 5
#define ARRDATA_Z1 0x10
#define ARRDATA_Z2 0x18
#define K_FLAG 0x1a
#define K_TRAITS 0x50
#define K_NAMESTORE 0x30
#define K_STAR 0x1b
#define K_QNAME 0x28
#define NC_TRAITS 0x48
#define NC_FIN 0x10
#define T_SLOTCOUNT 0x10
#define T_FLAG 0x12
#define T_TEMPLATE 0x30
#define AT_SLOTCOUNT 0x10
#define AT_FLAG 0x12
#define AT_TYPEOBJ 0x20
#define AT_TYPEMARK 0x40
#define AT_TEMPLATE 0x30
#define RCV_HDR 7
#define RCV_T1 0x20
#define RCV_T2 0x1a
#define SO_HDR_BYTES 0x28
#define SA_DATA_BYTES 0x20
#define SA_WRAP_BYTES 0x38
#else
#define VM_OBJECT_CLASS 0x167c
#define VM_FUNCTION_CLASS 0x168c
#define VM_ARRAY_CLASS 0x1698
#define VM_ARRAY_MARK 0x16c8
#define VM_ROOT_STACK 0x4834
#define VM_QNAME_CTOR 0x91c
#define VM_QNAME_NAME 0x910
#define CLASS_PROTO 0x28
#define STATIC_ARR_CLASS_IDX 0x5a6
#define WORD 4
#define N_ID 0x0c
#define N_PROTO0 0x14
#define N_TRAITS 0x18
#define N_ARRPTR 0x1c
#define N_FC_IMPL 0x0c
#define N_FC_PARAM 0x18
#define N_MC_IMPL 0x0c
#define N_MC_RCV 0x10
#define TAG_PROTO0 0x13
#define TAG_ID 0x0b
#define NUM_WORDS 4
#define NS_WORDS 4
#define OBJ_WORDS 6
#define PROTO_WORDS 7
#define FC_WORDS 9
#define MC_WORDS 5
#define STR_WORDS 3
#define ARR_WORDS 8
#define ARRDATA_EXTRA 5
#define TRAITS_EXTRA 6
#define ARRDATA_Z1 0x0c
#define ARRDATA_Z2 0x10
#define K_FLAG 0x0e
#define K_TRAITS 0x2c
#define K_NAMESTORE 0x1c
#define K_STAR 0x0f
#define K_QNAME 0x18
#define NC_TRAITS 0x28
#define NC_FIN 0x08
#define T_SLOTCOUNT 0x08
#define T_FLAG 0x0a
#define T_TEMPLATE 0x20
#define AT_SLOTCOUNT 0x08
#define AT_FLAG 0x0a
#define AT_TYPEOBJ 0x18
#define AT_TYPEMARK 0x24
#define AT_TEMPLATE 0x20
#define RCV_HDR 7
#define RCV_T1 0x18
#define RCV_T2 0x0e
#define SO_HDR_BYTES 0x18
#define SA_DATA_BYTES 0x14
#define SA_WRAP_BYTES 0x20
#endif
#define TAG_PROTO 7 /* tagged+7 == node+8 (proto array pointer slot) */
#define N_PROTO 8 /* proto array pointer (right after the 8-byte header) */
#define N_PAYLOAD 8 /* double / stringdata pointer / string node pointer */
#define N_CHARS 0x0c /* stringdata inline UTF-16 buffer */
#define N_LEN 8 /* stringdata character length */
/* The GC flag byte at node[1]; orbis uses ^0xf8, the others use ~. */
#if defined(IGGY_RECONSTRUCT_AS3VM_CREATE_ORBIS)
#define GC_BITS(x) ((uint8_t)((x) ^ 0xf8))
#else
#define GC_BITS(x) ((uint8_t)(~(uint8_t)(x)))
#endif
/* proto array pointer for the built-in class at vm+class_off. */
#define CLASS_PROTO_ARR(vm, class_off) \
(*(void ***)(*(uint8_t **)((char *)(vm) + (class_off)) + CLASS_PROTO))
/* Tagged sentinel used by make_class_closure to detect the "any" name. */
#define NULLUNDEF_SENTINEL ((uintptr_t)&as3vm_nullundef_storage + 1)
/* ------------------------------------------------------------------ */
/* GC root-stack push / reload (raw pushes the argument, allocs, then */
/* reloads it because the collector may relocate through gc_alloc). */
/* ------------------------------------------------------------------ */
static void root_push(void *vm, void *value) {
void **top = *(void ***)((char *)vm + VM_ROOT_STACK);
*top = value;
*(char **)((char *)vm + VM_ROOT_STACK) = (char *)top + WORD;
}
static void *root_reload(void *vm) {
char *top = *(char **)((char *)vm + VM_ROOT_STACK) - WORD;
*(char **)((char *)vm + VM_ROOT_STACK) = top;
return *(void **)top;
}
/* Conditional write barrier for a pointer store into a GC node. */
static void maybe_barrier(void *vm, uint8_t *node, uint8_t *slot) {
if (((uintptr_t)slot & 3) == 1 && slot != (uint8_t *)0x7 &&
(GC_BITS(*slot) & node[1]) > 0x17) {
as3vm_write_barrier_slowpath(vm, node, slot - 1);
}
}
/* Copy the built-in class proto array pointer + its head into the node. */
static void set_proto(void *vm, uint8_t *node, size_t class_off) {
void **proto = CLASS_PROTO_ARR(vm, class_off);
*(void **)(node + N_PROTO) = proto;
*(void **)(node + N_PROTO0) = *proto;
}
/* C-string length: durango inlines the scan, the others call strlen. */
static int32_t cstr_len(const char *string) {
#if defined(IGGY_RECONSTRUCT_AS3VM_CREATE_DURANGO)
int32_t index = -1;
do {
index += 1;
} while (string[index] != '\0');
return index;
#else
return (int32_t)strlen(string);
#endif
}
/* floor for a value already known to lie in [-2^30, 2^30): durango/ps3/vita
* lower to a libm call; orbis inlines a truncation (matching its roundsd). */
static int32_t floor_to_int(double value) {
#if defined(IGGY_RECONSTRUCT_AS3VM_CREATE_ORBIS)
int32_t truncated = (int32_t)value;
return truncated - (value < (double)truncated);
#else
return (int32_t)floor(value);
#endif
}
/* ------------------------------------------------------------------ */
/* Number boxing -- specials, integral shift, heap double fallback. */
/* ------------------------------------------------------------------ */
uint64_t as3vm_make_int_GC(void *vm, uint32_t value) {
uint8_t *node;
if (value + 0x40000000u < 0x80000000u) {
return (uint64_t)(int64_t)(int32_t)(value * 2u);
}
node = (uint8_t *)as3vm_gc_alloc(vm, NUM_WORDS);
*(uint32_t *)(node + 4) = NUM_WORDS;
node[0] = 7;
*(double *)(node + N_PAYLOAD) = (double)(int32_t)value;
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_uint_GC(void *vm, uint32_t value) {
uint8_t *node;
if (value < 0x40000000u) {
return (uint64_t)(value * 2u);
}
node = (uint8_t *)as3vm_gc_alloc(vm, NUM_WORDS);
*(uint32_t *)(node + 4) = NUM_WORDS;
node[0] = 7;
*(double *)(node + N_PAYLOAD) = (double)value;
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_integer_GC(void *vm, double value) {
uint8_t *node;
if (value >= -1073741824.0 && value < 1073741824.0) {
return (uint64_t)(int64_t)(floor_to_int(value) * 2);
}
node = (uint8_t *)as3vm_gc_alloc(vm, NUM_WORDS);
*(uint32_t *)(node + 4) = NUM_WORDS;
node[0] = 7;
*(double *)(node + N_PAYLOAD) = value;
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_number_GC(void *vm, double value) {
uint8_t *node;
if (value >= -1073741824.0 && value < 1073741824.0) {
if (value == 0.0) {
return 0;
}
if ((value < 0.0 ? -value : value) == 1.0) {
return value > 0.0 ? 2u : 0xFFFFFFFFFFFFFFFEull;
}
if ((double)(int32_t)value == value) {
return (uint64_t)(int64_t)((int32_t)value * 2);
}
}
node = (uint8_t *)as3vm_gc_alloc(vm, NUM_WORDS);
*(uint32_t *)(node + 4) = NUM_WORDS;
node[0] = 7;
*(double *)(node + N_PAYLOAD) = value;
return (uint64_t)((uintptr_t)node + 1);
}
void as3vm_make_number_not_int_GC(void *vm, uint64_t bits) {
uint8_t *node = (uint8_t *)as3vm_gc_alloc(vm, NUM_WORDS);
*(uint32_t *)(node + 4) = NUM_WORDS;
node[0] = 7;
*(uint64_t *)(node + N_PAYLOAD) = bits;
}
/* ------------------------------------------------------------------ */
/* Namespace -- flat header, no proto deref. */
/* ------------------------------------------------------------------ */
uint64_t as3vm_make_namespace(void *vm) {
uint8_t *node = (uint8_t *)as3vm_gc_alloc(vm, NS_WORDS);
node[0] = 0x0c;
*(uint32_t *)(node + 4) = NS_WORDS;
node[2] = 7;
*(void **)(node + N_ID) = 0;
return (uint64_t)((uintptr_t)node + 1);
}
/* ------------------------------------------------------------------ */
/* stringdata backing buffers. */
/* ------------------------------------------------------------------ */
uint64_t as3vm_make_stringdata_GC(void *vm, const void *chars, int32_t length) {
uint8_t *node;
int32_t rounded = length + 1;
#if IGGY_AS3_64BIT
uint32_t words;
if (rounded < 4) {
rounded = 4;
}
words = (uint32_t)(((int64_t)rounded * 2 + 0x13) >> 3);
#else
uint32_t words;
if (rounded < 2) {
rounded = 2;
}
words = (uint32_t)(rounded * 2 + 0xf) >> 2;
#endif
node = (uint8_t *)as3vm_gc_alloc(vm, words);
*(uint32_t *)(node + 4) = words;
*(uint16_t *)(node + 2) = 0;
node[0] = 1;
if (chars != NULL) {
memcpy(node + N_CHARS, chars, (size_t)length * 2);
}
*(int32_t *)(node + N_LEN) = length;
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_stringdata_c_GC(void *vm, const char *utf8) {
uint16_t buffer[0x40];
int32_t length = rrUTF8toUTF16(buffer, 0x40, utf8, -1, 0x3f, 1);
return as3vm_make_stringdata_GC(vm, buffer, length);
}
/* make_stringdata_utf8_GC -- alloca fast path with a growing retry (file
* static; raw/durango:843, raw/orbis:346). */
static uint64_t make_stringdata_utf8_GC(void *vm, const void *utf8,
uint32_t nbytes) {
int32_t capacity = nbytes < 0x100u ? (int32_t)nbytes + 1 : 0x100;
uint16_t *buffer = (uint16_t *)__builtin_alloca((size_t)capacity * 2);
int32_t length = rrUTF8toUTF16(buffer, capacity, utf8, (int32_t)nbytes, 0x3f,
nbytes < 0x100u ? 0 : 0);
if (length < 0) {
length = rrUTF8toUTF16(NULL, 0, utf8, (int32_t)nbytes, 0x3f, 0);
buffer = (uint16_t *)__builtin_alloca((size_t)length * 2);
length = rrUTF8toUTF16(buffer, length, utf8, (int32_t)nbytes, 0x3f, 1);
}
return as3vm_make_stringdata_GC(vm, buffer, length);
}
/* ------------------------------------------------------------------ */
/* Object construction -- lazy two-phase, traits, proto. */
/* ------------------------------------------------------------------ */
uint64_t as3vm_make_object_lazy_GC(void *vm, uint8_t kind) {
uint8_t *node = (uint8_t *)as3vm_gc_alloc_remembered(vm, OBJ_WORDS);
node[0] = kind;
*(uint32_t *)(node + N_ID) = 0;
node[2] = 0;
*(uint32_t *)(node + 4) = OBJ_WORDS;
return (uint64_t)((uintptr_t)node + 1);
}
void as3vm_finish_object_lazy(void *vm, void *tagged) {
void **proto = CLASS_PROTO_ARR(vm, VM_OBJECT_CLASS);
*(void **)((char *)tagged + TAG_PROTO) = proto;
*(void **)((char *)tagged + TAG_PROTO0) = *proto;
}
uint64_t as3vm_make_object_GC(void *vm, uint8_t kind) {
uint8_t *node = (uint8_t *)as3vm_gc_alloc_remembered(vm, OBJ_WORDS);
*(uint32_t *)(node + N_ID) = 0;
node[0] = kind;
node[2] = 0;
*(uint32_t *)(node + 4) = OBJ_WORDS;
set_proto(vm, node, VM_OBJECT_CLASS);
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_object_with_proto_GC(void *vm, void *proto) {
uint8_t *node;
root_push(vm, proto);
node = (uint8_t *)as3vm_gc_alloc_remembered(vm, PROTO_WORDS);
*(uint32_t *)(node + N_ID) = 0;
node[0] = 0x25;
node[2] = 0;
*(uint16_t *)(node + 4) = PROTO_WORDS;
*(uint16_t *)(node + 6) = 1;
set_proto(vm, node, VM_OBJECT_CLASS);
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_object_with_traits_GC(void *vm, void *traits,
int32_t finalize) {
uint8_t *t = (uint8_t *)traits;
uint16_t slots = *(uint16_t *)(t + T_SLOTCOUNT);
uint16_t flag = *(uint16_t *)(t + T_FLAG);
uint8_t *node = (uint8_t *)as3vm_gc_alloc(vm, (uint32_t)(slots + TRAITS_EXTRA));
*(uint16_t *)(node + 4) = (uint16_t)(slots + TRAITS_EXTRA);
*(uint32_t *)(node + N_ID) = 0;
*(void **)(node + N_PROTO) = 0;
*(void **)(node + N_PROTO0) = 0;
node[0] = 0x23;
node[2] = 0;
*(uint16_t *)(node + 6) = flag;
memcpy(node + N_TRAITS, *(void **)(t + T_TEMPLATE), (size_t)slots * WORD);
if (node[1] & 0x20) {
uint8_t *end = node + (size_t)(slots + TRAITS_EXTRA) * WORD;
uint8_t *slot = end - (size_t)(*(uint16_t *)(node + 6)) * WORD;
for (; slot < end; slot += WORD) {
maybe_barrier(vm, node, *(uint8_t **)slot);
}
}
if (finalize != 0) {
as3vm_register_finalizer(vm, node + 1);
}
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_object_no_constructor_GC(void *vm, void *klass) {
uint8_t *k = (uint8_t *)klass;
void *traits = *(void **)(k + NC_TRAITS);
uint64_t tagged = as3vm_make_object_with_traits_GC(
vm, traits, *(void **)(k + NC_FIN) != 0);
char *node = (char *)(uintptr_t)tagged;
*(void **)(node + TAG_PROTO) = traits;
*(void **)(node + TAG_PROTO0) = *(void **)traits;
*(uint32_t *)(node + TAG_ID) = 0;
return tagged;
}
/* ------------------------------------------------------------------ */
/* String object -- wraps a stringdata through the GC root stack. */
/* ------------------------------------------------------------------ */
uint64_t as3vm_make_string_GC(void *vm, void *stringdata) {
uint8_t *node;
uint8_t *slot;
root_push(vm, stringdata);
node = (uint8_t *)as3vm_gc_alloc(vm, STR_WORDS);
slot = (uint8_t *)root_reload(vm);
*(uint16_t *)(node + 4) = STR_WORDS;
*(uint16_t *)(node + 6) = 1;
node[0] = 8;
node[2] = 0;
maybe_barrier(vm, node, slot);
*(void **)(node + N_PAYLOAD) = slot;
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_string_c_GC(void *vm, const char *utf8) {
uint64_t stringdata = as3vm_make_stringdata_c_GC(vm, utf8);
return as3vm_make_string_GC(vm, (void *)(uintptr_t)stringdata);
}
uint64_t as3vm_make_string_wc_GC(void *vm, const void *chars, int32_t length) {
uint64_t stringdata;
if (length == -1 && chars != NULL) {
length = rrwcslen(chars);
}
stringdata = as3vm_make_stringdata_GC(vm, chars, length);
return as3vm_make_string_GC(vm, (void *)(uintptr_t)stringdata);
}
uint64_t as3vm_make_string_utf8_GC(void *vm, const void *utf8, uint32_t nbytes) {
uint64_t stringdata = make_stringdata_utf8_GC(vm, utf8, nbytes);
return as3vm_make_string_GC(vm, (void *)(uintptr_t)stringdata);
}
/* ------------------------------------------------------------------ */
/* Interned string via rrUTF8toUTF16 + as3vm_find_intern_string. */
/* ------------------------------------------------------------------ */
uint64_t as3vm_make_interned_string_c(void *vm, const char *utf8) {
uint16_t buffer[0x100];
int32_t length = cstr_len(utf8);
int32_t units = rrUTF8toUTF16(buffer, 0x100, utf8, length, 0x3f, 0);
void *node = as3vm_find_intern_string(vm, (size_t)units, buffer, 1);
return (uint64_t)((uintptr_t)node + 1);
}
/* ------------------------------------------------------------------ */
/* Non-GC permanent string builders (arena allocated). */
/* ------------------------------------------------------------------ */
uint64_t as3vm_make_string_permanent(void *player, const void *chars) {
int32_t length = rrwcslen(chars);
uint8_t *data = (uint8_t *)iggy_arena_malloc_aligned(
*(void **)player, NULL, (uint32_t)((length + 1) * 2 + 2), 8, 0x79);
uint8_t *wrap = (uint8_t *)iggy_arena_malloc_aligned(
*(void **)player, NULL, WORD * 4u, 8, 0x7a);
const uint16_t *src = (const uint16_t *)chars;
uint16_t *dst = (uint16_t *)(data + N_CHARS);
int32_t i;
wrap[0] = 8;
wrap[1] = 0x30;
*(uint16_t *)(wrap + 4) = STR_WORDS + 1;
*(uint16_t *)(wrap + 6) = 1;
wrap[2] = 0;
wrap[3] = 0;
*(void **)(wrap + N_PAYLOAD) = data + 1;
data[0] = 1;
data[1] = 0x30;
data[2] = 0;
data[3] = 0;
*(uint32_t *)(data + 4) = (uint32_t)((length + 1) * 2 + 0x15) >> 2;
*(int32_t *)(data + N_LEN) = length;
for (i = 0; i <= length; ++i) {
dst[i] = src[i];
}
return (uint64_t)((uintptr_t)wrap + 1);
}
uint64_t as3vm_make_string_c_permanent(void *player, const char *utf8) {
int32_t length = cstr_len(utf8);
uint8_t *data = (uint8_t *)iggy_arena_malloc_aligned(
*(void **)player, NULL, (uint32_t)(length * 2 + 0x12), 8, 0x79);
uint8_t *wrap = (uint8_t *)iggy_arena_malloc_aligned(
*(void **)player, NULL, WORD * 4u, 8, 0x7a);
uint16_t *dst = (uint16_t *)(data + N_CHARS);
int32_t i;
wrap[0] = 8;
wrap[1] = 0x30;
*(uint16_t *)(wrap + 4) = STR_WORDS + 1;
*(uint16_t *)(wrap + 6) = 1;
wrap[2] = 0;
wrap[3] = 0;
*(void **)(wrap + N_PAYLOAD) = data + 1;
data[0] = 1;
data[1] = 0x30;
data[2] = 0;
data[3] = 0;
*(uint32_t *)(data + 4) = (uint32_t)(length * 2 + 0x15) >> 2;
*(int32_t *)(data + N_LEN) = length;
for (i = 0; i <= length; ++i) {
dst[i] = (uint16_t)(uint8_t)utf8[i];
}
return (uint64_t)((uintptr_t)wrap + 1);
}
/* ------------------------------------------------------------------ */
/* Closures -- function / method / class. */
/* ------------------------------------------------------------------ */
void as3vm_make_function_closure_GC(void *vm, void *method) {
uint8_t *node = (uint8_t *)as3vm_gc_alloc(vm, FC_WORDS);
*(void **)(node + N_FC_PARAM) = method;
node[0] = 0x14;
*(uint16_t *)(node + 4) = FC_WORDS;
*(uint16_t *)(node + 6) = 2;
set_proto(vm, node, VM_FUNCTION_CLASS);
*(void (**)(void))(node + N_FC_IMPL) = &as3vm_functionclosure_implementation;
*(void **)(node + N_FC_PARAM + WORD) = 0;
*(void **)(node + N_FC_PARAM + WORD * 2) = 0;
}
uint64_t as3vm_make_method_closure_GC(void *vm, void *receiver, void *method) {
uint8_t *node;
uint8_t *slot;
root_push(vm, receiver);
node = (uint8_t *)as3vm_gc_alloc(vm, MC_WORDS);
slot = (uint8_t *)root_reload(vm);
*(void **)(node + N_PAYLOAD) = (char *)method - 1;
node[0] = 0x12;
maybe_barrier(vm, node, slot);
*(void **)(node + N_MC_RCV) = slot;
if (((uintptr_t)slot & 3) == 1 && slot[-1] > 0x1f &&
(*(uint8_t *)(*(char **)(*(char **)(slot + RCV_HDR) + RCV_T1) + RCV_T2) &
4)) {
node[3] = 1;
} else {
node[3] = 0;
}
*(uint16_t *)(node + 4) = MC_WORDS;
*(uint16_t *)(node + 6) = 1;
*(void (**)(void))(node + N_MC_IMPL) = &as3vm_methodclosure_implementation;
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_class_closure_GC(void *vm, void *klass) {
uint8_t *k = (uint8_t *)klass;
uint8_t *tagged;
if ((*(uint8_t *)(k + K_FLAG) & 2) == 0) {
tagged = (uint8_t *)(uintptr_t)as3vm_make_object_with_traits_GC(
vm, *(void **)(k + K_TRAITS), 0);
} else {
uint8_t *node = (uint8_t *)as3vm_gc_alloc_remembered(vm, OBJ_WORDS);
*(uint32_t *)(node + N_ID) = 0;
node[0] = 0x26;
node[2] = 0;
*(uint32_t *)(node + 4) = OBJ_WORDS;
set_proto(vm, node, VM_OBJECT_CLASS);
tagged = node + 1;
}
*(uint8_t **)(k + K_NAMESTORE) = tagged;
tagged[-1] = 0x26;
*(void **)(tagged + TAG_PROTO) = *(void **)(k + K_TRAITS);
*(void **)(tagged + TAG_PROTO0) = **(void ***)(k + K_TRAITS);
*(uint32_t *)(tagged + TAG_ID) = 0;
if (*(char *)(k + K_STAR) != '*' &&
*(uintptr_t *)(k + K_QNAME) != NULLUNDEF_SENTINEL) {
root_push(vm, tagged);
as3vm_set_property_name_GC(vm, *(void **)(k + K_QNAME),
*(void **)((char *)vm + VM_QNAME_CTOR),
*(void **)(k + K_NAMESTORE), 1);
as3vm_qname_set_dont_enum_GC(vm, *(void **)(k + K_QNAME),
*(void **)((char *)vm + VM_QNAME_CTOR));
as3vm_set_property_name_GC(vm, *(void **)(k + K_NAMESTORE),
*(void **)((char *)vm + VM_QNAME_NAME),
*(void **)(k + K_QNAME), 1);
as3vm_qname_set_dont_enum_GC(vm, *(void **)(k + K_NAMESTORE),
*(void **)((char *)vm + VM_QNAME_NAME));
return (uint64_t)(uintptr_t)root_reload(vm);
}
return (uint64_t)(uintptr_t)tagged;
}
#include "as3vm_create_arrays.inc"

View File

@@ -0,0 +1,96 @@
#ifndef IGGY_RECONSTRUCTED_AS3VM_CREATE_H
#define IGGY_RECONSTRUCTED_AS3VM_CREATE_H
/*
* Reconstructed public ABI for RAD's as3vm_create translation unit -- 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.
*
* Source-equivalent recovery of c:\projects\g\src\flash\as3vm_create.c
*
* The module exports exactly 31 functions (plus one file-static helper,
* make_stringdata_utf8_GC) and owns NO externally named data global: every table
* it touches (the built-in class prototypes at vm+0x2830 / 0x2850 / 0x2868 /
* 0x28c8, the GC root stack at vm+0x6b18, the class-name qnames at vm+0xda8 /
* 0xdc0 on the 64-bit targets) lives INSIDE the caller's VM context and is
* passed in by pointer. The signatures below follow the 64-bit DURANGO ABI
* recovered from the raw decompilation; the retail calling convention and struct
* offsets differ per platform (durango/orbis are LP64, ps3/vita ILP32, ps3
* big-endian), but the exported symbol names are invariant across all four.
*
* VALUE TAGGING: a small integer boxes to (value << 1) with a clear low bit; a
* heap value is (node_pointer + 1) with a set low bit (the node is 16-aligned so
* the +1 tag is unambiguous). node[0] is the kind byte (7 number, 8 string, 1
* stringdata, 0xc namespace, 0x12 method closure, 0x14 function closure, 0x20
* array, 0x23 object-with-traits, 0x25 object-with-proto, 0x26 class closure).
*/
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* int / uint / number boxing -- small values tag inline, heap values box a
* double node and return the tagged pointer. */
uint64_t as3vm_make_int_GC(void *vm, uint32_t value);
uint64_t as3vm_make_uint_GC(void *vm, uint32_t value);
uint64_t as3vm_make_integer_GC(void *vm, double value);
uint64_t as3vm_make_number_GC(void *vm, double value);
void as3vm_make_number_not_int_GC(void *vm, uint64_t bits);
/* Flat namespace header node. */
uint64_t as3vm_make_namespace(void *vm);
/* stringdata backing buffers (UTF-16). */
uint64_t as3vm_make_stringdata_GC(void *vm, const void *chars, int32_t length);
uint64_t as3vm_make_stringdata_c_GC(void *vm, const char *utf8);
/* Object construction -- lazy two-phase, traits, proto, no-constructor. */
uint64_t as3vm_make_object_lazy_GC(void *vm, uint8_t kind);
void as3vm_finish_object_lazy(void *vm, void *tagged);
uint64_t as3vm_make_object_GC(void *vm, uint8_t kind);
uint64_t as3vm_make_object_no_constructor_GC(void *vm, void *klass);
uint64_t as3vm_make_object_with_proto_GC(void *vm, void *proto);
uint64_t as3vm_make_object_with_traits_GC(void *vm, void *traits,
int32_t finalize);
/* String wrappers -- GC-rooted stringdata wrap + C / wide / utf8 helpers. */
uint64_t as3vm_make_string_GC(void *vm, void *stringdata);
uint64_t as3vm_make_string_c_GC(void *vm, const char *utf8);
uint64_t as3vm_make_string_wc_GC(void *vm, const void *chars, int32_t length);
uint64_t as3vm_make_string_utf8_GC(void *vm, const void *utf8, uint32_t nbytes);
/* Non-GC permanent string builders (arena-allocated). */
uint64_t as3vm_make_string_permanent(void *player, const void *chars);
uint64_t as3vm_make_string_c_permanent(void *player, const char *utf8);
/* Interned string via rrUTF8toUTF16 + as3vm_find_intern_string. */
uint64_t as3vm_make_interned_string_c(void *vm, const char *utf8);
/* Closures -- function / method / class. */
void as3vm_make_function_closure_GC(void *vm, void *method);
uint64_t as3vm_make_method_closure_GC(void *vm, void *receiver, void *method);
uint64_t as3vm_make_class_closure_GC(void *vm, void *klass);
/* Array construction + arraydata backing. */
uint64_t as3vm_make_arraydata_GC(void *vm, int32_t count, uint32_t initial);
uint64_t as3vm_make_array_GC(void *vm, int32_t count, uint32_t initial);
uint64_t as3vm_make_array_from_arraydata_GC(void *vm, void *arraydata,
uint32_t initial);
uint64_t as3vm_make_array_with_traits_GC(void *vm, void *traits,
int32_t finalize);
uint64_t as3vm_make_incomplete_array_GC(void *vm);
/* Permanent (arena) static object / array builders. */
uint64_t as3vm_make_static_array(void *player, int32_t count, void *heap);
uint64_t as3vm_make_static_object(void *player, void *klass, void *heap);
#ifdef __cplusplus
}
#endif
#endif /* IGGY_RECONSTRUCTED_AS3VM_CREATE_H */

View File

@@ -0,0 +1,143 @@
/* Array + permanent static object/array makers for as3vm_create.
* Included at the end of as3vm_create.c (uses its macros + helpers). */
/* ------------------------------------------------------------------ */
/* Array construction + arraydata backing. */
/* ------------------------------------------------------------------ */
uint64_t as3vm_make_arraydata_GC(void *vm, int32_t count, uint32_t initial) {
uint8_t *node = (uint8_t *)as3vm_gc_alloc(vm, (uint32_t)(count + ARRDATA_EXTRA));
node[0] = 2;
*(uint32_t *)(node + 8) = initial;
*(int32_t *)(node + 4) = count;
node[2] = 0;
*(void **)(node + ARRDATA_Z1) = 0;
*(void **)(node + ARRDATA_Z2) = 0;
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_incomplete_array_GC(void *vm) {
uint8_t *node = (uint8_t *)as3vm_gc_alloc(vm, ARR_WORDS);
node[0] = 0x20;
node[2] = 0;
node[3] = 0;
*(uint16_t *)(node + 4) = ARR_WORDS;
*(uint16_t *)(node + 6) = 1;
set_proto(vm, node, VM_ARRAY_CLASS);
*(uint32_t *)(node + N_ID) = 0;
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_array_from_arraydata_GC(void *vm, void *arraydata,
uint32_t initial) {
uint8_t *node;
uint8_t *slot;
root_push(vm, arraydata);
node = (uint8_t *)as3vm_gc_alloc(vm, ARR_WORDS);
node[0] = 0x20;
node[2] = 0;
node[3] = 0;
*(uint16_t *)(node + 4) = ARR_WORDS;
*(uint16_t *)(node + 6) = 1;
set_proto(vm, node, VM_ARRAY_CLASS);
*(uint32_t *)(node + N_ID) = 0;
slot = (uint8_t *)root_reload(vm);
*(uint32_t *)(slot + 7) = initial;
maybe_barrier(vm, node, slot);
*(void **)(node + N_ARRPTR) = slot;
return (uint64_t)((uintptr_t)node + 1);
}
uint64_t as3vm_make_array_GC(void *vm, int32_t count, uint32_t initial) {
uint8_t *node = (uint8_t *)as3vm_gc_alloc(vm, (uint32_t)(count + ARRDATA_EXTRA));
node[0] = 2;
*(uint32_t *)(node + 8) = 0;
*(int32_t *)(node + 4) = count;
node[2] = 0;
*(void **)(node + ARRDATA_Z1) = 0;
*(void **)(node + ARRDATA_Z2) = 0;
return as3vm_make_array_from_arraydata_GC(vm, node + 1, initial);
}
uint64_t as3vm_make_array_with_traits_GC(void *vm, void *traits,
int32_t finalize) {
uint8_t *t = (uint8_t *)traits;
uint16_t slots = *(uint16_t *)(t + AT_SLOTCOUNT);
uint16_t flag = *(uint16_t *)(t + AT_FLAG);
uint8_t *node = (uint8_t *)as3vm_gc_alloc(vm, (uint32_t)(slots + TRAITS_EXTRA));
void *type_obj = *(void **)(t + AT_TYPEOBJ);
*(uint32_t *)(node + N_ID) = 0;
*(void **)(node + N_PROTO) = t;
*(void **)(node + N_PROTO0) = *(void **)t;
node[0] = 0x20;
node[2] = (*(void **)((char *)type_obj + AT_TYPEMARK) ==
*(void **)((char *)vm + VM_ARRAY_MARK));
*(uint16_t *)(node + 6) = flag;
*(uint16_t *)(node + 4) = (uint16_t)(slots + TRAITS_EXTRA);
memcpy(node + N_TRAITS, *(void **)(t + AT_TEMPLATE), (size_t)slots * WORD);
if (node[1] & 0x20) {
uint8_t *end = node + (size_t)(slots + TRAITS_EXTRA) * WORD;
uint8_t *slot = end - (size_t)(*(uint16_t *)(node + 6)) * WORD;
for (; slot < end; slot += WORD) {
maybe_barrier(vm, node, *(uint8_t **)slot);
}
}
if (finalize != 0) {
as3vm_register_finalizer(vm, node + 1);
}
return (uint64_t)((uintptr_t)node + 1);
}
/* ------------------------------------------------------------------ */
/* Permanent (arena) static object / array builders. */
/* ------------------------------------------------------------------ */
uint64_t as3vm_make_static_array(void *player, int32_t count, void *heap) {
void **pl = (void **)player;
uint8_t *data = (uint8_t *)iggy_arena_malloc_aligned(
heap, pl[0], (uint32_t)(count * WORD + SA_DATA_BYTES), 8, 0x7e);
uint8_t *wrap;
void **proto;
data[1] = 0x30;
data[0] = 2;
*(void **)(data + ARRDATA_Z2) = 0;
data[2] = 0;
*(int32_t *)(data + 8) = count;
*(int32_t *)(data + 4) = count;
*(void **)(data + ARRDATA_Z1) = 0;
wrap = (uint8_t *)iggy_arena_malloc_aligned(heap, pl[0], SA_WRAP_BYTES, 8,
0x7e);
wrap[0] = 0x20;
wrap[1] = 0x30;
*(uint16_t *)(wrap + 4) = ARR_WORDS;
*(uint16_t *)(wrap + 6) = 1;
proto = (void **)(*(uint8_t **)(pl[STATIC_ARR_CLASS_IDX]) + CLASS_PROTO);
*(uint32_t *)(wrap + N_ID) = 0;
*(void **)(wrap + N_PROTO) = proto;
*(void **)(wrap + N_PROTO0) = *proto;
*(void **)(wrap + N_ARRPTR) = data + 1;
return (uint64_t)((uintptr_t)wrap + 1);
}
uint64_t as3vm_make_static_object(void *player, void *klass, void *heap) {
void **pl = (void **)player;
uint8_t *k = (uint8_t *)klass;
uint8_t *traits = *(uint8_t **)(k + NC_TRAITS);
uint16_t slots = *(uint16_t *)(traits + T_SLOTCOUNT);
uint16_t flag = *(uint16_t *)(traits + T_FLAG);
uint8_t *node = (uint8_t *)iggy_arena_malloc_aligned(
heap, pl[0], (uint32_t)(slots * WORD + SO_HDR_BYTES), 8, 0x7d);
node[0] = 0x23;
node[1] = 0x30;
node[2] = 0;
*(uint16_t *)(node + 4) = (uint16_t)(slots + TRAITS_EXTRA);
*(uint16_t *)(node + 6) = flag;
*(void **)(node + N_PROTO) = 0;
*(uint32_t *)(node + N_ID) = 0;
*(void **)(node + N_PROTO0) = 0;
memcpy(node + N_TRAITS, *(void **)(traits + T_TEMPLATE), (size_t)slots * WORD);
*(void **)(node + N_PROTO) = *(void **)(k + NC_TRAITS);
*(void **)(node + N_PROTO0) = **(void ***)(k + NC_TRAITS);
*(uint32_t *)(node + N_ID) = 0;
return (uint64_t)((uintptr_t)node + 1);
}

View File

@@ -70,4 +70,5 @@ as3vm_names 12 11 orbis complete
as3vm_names 13 11 ps3 complete
as3vm_names 13 11 vita complete
rrCore 0 0 durango,orbis,ps3,vita complete
as3vm_create 32 31 durango,orbis,ps3,vita complete
radss_xaudio2 3 1 durango complete
1 module source_functions external_functions platforms status
70 as3vm_names 13 11 ps3 complete
71 as3vm_names 13 11 vita complete
72 rrCore 0 0 durango,orbis,ps3,vita complete
73 as3vm_create 32 31 durango,orbis,ps3,vita complete
74 radss_xaudio2 3 1 durango complete

View File

@@ -0,0 +1,245 @@
/*
* Host harness for the reconstructed as3vm_create translation unit.
*
* 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 (the built-in class prototypes at
* vm+0x2830 / 0x2850 / 0x2868 / 0x28c8, the GC root stack at vm+0x6b18, the
* class-name qnames at vm+0xda8 / 0xdc0) lives INSIDE the caller's VM context,
* not in this TU (verified: nm shows no B/D/C data symbols on any of the four
* retail objects; the durango R constants are read-only float literals).
*
* The value-tagging scheme (invariant across all four platforms, cross-checked
* durango 64-bit vs vita 32-bit): a small integer boxes to (value << 1) with a
* clear low bit; a heap value is (node_pointer + 1) with a set low bit, where
* node is 16-byte aligned so the +1 tag is unambiguous. node[0] is the kind
* byte (7 number, 8 string, 1 stringdata, 0xc namespace, 0x12 method closure,
* 0x14 function closure, 0x20 array, 0x23 object-with-traits, 0x25
* object-with-proto, 0x26 class closure).
*
* The only symbols that have to be supplied from outside are the genuine nm
* ' U ' imports. This DURANGO harness provides the full genuine import set
* (identical real-Iggy set on all four platforms; per-platform libc/libm
* extras -- floor, strlen -- differ, see the cross test):
*
* - GC allocator (as3vm_gc / as3vm_interp, NOT yet reconstructed -- expected
* unbuilt imports): as3vm_gc_alloc, as3vm_gc_alloc_remembered
* -> functional bump arena returning zeroed 16-aligned buffers so the
* makers actually run; the requested word count is recorded.
* - write barrier (as3vm_gc, unbuilt): as3vm_write_barrier_slowpath -> record.
* - finalizer / property naming (as3vm_gc / as3vm_class, unbuilt):
* as3vm_register_finalizer, as3vm_set_property_name_GC,
* as3vm_qname_set_dont_enum_GC -> record.
* - interning (as3vm_names, NOW reconstructed): as3vm_find_intern_string
* -> controllable return; records the create flag.
* - closure implementations (as3vm_interp, unbuilt): their ADDRESS is captured
* into the closure node, never called -- provided as real functions whose
* address the tests read back through an accessor.
* - null/undefined sentinel (as3vm_gc, unbuilt): as3vm_nullundef_storage is a
* DATA symbol (referenced by address, dot-unprefixed on ps3 like the other
* address-taken symbols) -- provided as a data array.
* - arena (iggy_arena_allocator, satisfied): iggy_arena_malloc_aligned
* -> functional bump arena (the non-GC permanent + static makers).
* - rrstring (satisfied): rrUTF8toUTF16 (6-arg canonical), rrwcslen -> real
* ASCII-range conversions so string makers copy real chars.
* - libc: memcpy resolves against the host and is deliberately NOT stubbed,
* matching the sibling as3vm_names / iggy_sound harness convention.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/* ---- Counters + controllable collaborator state. ---- */
static size_t gc_alloc_count;
static size_t gc_alloc_remembered_count;
static size_t write_barrier_count;
static size_t register_finalizer_count;
static size_t set_property_name_count;
static size_t qname_set_dont_enum_count;
static size_t find_intern_string_count;
static size_t arena_malloc_aligned_count;
static size_t last_alloc_words;
static void *last_alloc_ptr;
static void *last_finalizer_ptr;
static int32_t last_intern_create;
static intptr_t intern_result;
/* Bump arena backing the alloc stubs. */
static uint8_t arena_pool[4 << 20];
static size_t arena_used;
static void *pool_alloc(size_t size, size_t align) {
if (align < 16u) {
align = 16u;
}
size_t base = (arena_used + (align - 1u)) & ~(align - 1u);
/* Pad generously: the widest maker writes node+0x38 plus a trait tail. */
size_t rounded = (size + 63u) & ~(size_t)63u;
if (rounded == 0u) {
rounded = 64u;
}
if (base + rounded > sizeof(arena_pool)) {
return 0;
}
arena_used = base + rounded;
return arena_pool + base;
}
void as3vm_create_test_reset(void) {
gc_alloc_count = 0;
gc_alloc_remembered_count = 0;
write_barrier_count = 0;
register_finalizer_count = 0;
set_property_name_count = 0;
qname_set_dont_enum_count = 0;
find_intern_string_count = 0;
arena_malloc_aligned_count = 0;
last_alloc_words = 0;
last_alloc_ptr = 0;
last_finalizer_ptr = 0;
last_intern_create = 0;
intern_result = 0;
arena_used = 0;
}
size_t as3vm_create_test_gc_alloc_count(void) { return gc_alloc_count; }
size_t as3vm_create_test_gc_alloc_remembered_count(void) {
return gc_alloc_remembered_count;
}
size_t as3vm_create_test_write_barrier_count(void) { return write_barrier_count; }
size_t as3vm_create_test_register_finalizer_count(void) {
return register_finalizer_count;
}
size_t as3vm_create_test_set_property_name_count(void) {
return set_property_name_count;
}
size_t as3vm_create_test_qname_set_dont_enum_count(void) {
return qname_set_dont_enum_count;
}
size_t as3vm_create_test_find_intern_string_count(void) {
return find_intern_string_count;
}
size_t as3vm_create_test_arena_malloc_aligned_count(void) {
return arena_malloc_aligned_count;
}
size_t as3vm_create_test_last_alloc_words(void) { return last_alloc_words; }
void *as3vm_create_test_last_alloc_ptr(void) { return last_alloc_ptr; }
void *as3vm_create_test_last_finalizer_ptr(void) { return last_finalizer_ptr; }
int32_t as3vm_create_test_last_intern_create(void) { return last_intern_create; }
void as3vm_create_test_set_intern_result(void *value) {
intern_result = (intptr_t)value;
}
/* ---- GC allocator imports (functional bump arena; size is in 8-byte words). */
void *as3vm_gc_alloc(void *vm, uint32_t words) {
(void)vm;
++gc_alloc_count;
last_alloc_words = words;
last_alloc_ptr = pool_alloc((size_t)words * 8u, 16u);
return last_alloc_ptr;
}
void *as3vm_gc_alloc_remembered(void *vm, uint32_t words) {
(void)vm;
++gc_alloc_remembered_count;
last_alloc_words = words;
last_alloc_ptr = pool_alloc((size_t)words * 8u, 16u);
return last_alloc_ptr;
}
/* ---- Write barrier (record only). ---- */
void as3vm_write_barrier_slowpath(void *vm, void *node, void *target) {
(void)vm;
(void)node;
(void)target;
++write_barrier_count;
}
/* ---- Finalizer + property-naming collaborators (record only). ---- */
void as3vm_register_finalizer(void *vm, void *object) {
(void)vm;
last_finalizer_ptr = object;
++register_finalizer_count;
}
void as3vm_set_property_name_GC(void *vm, void *name, void *qname, void *value,
int32_t flag) {
(void)vm;
(void)name;
(void)qname;
(void)value;
(void)flag;
++set_property_name_count;
}
void as3vm_qname_set_dont_enum_GC(void *vm, void *name, void *qname) {
(void)vm;
(void)name;
(void)qname;
++qname_set_dont_enum_count;
}
/* ---- Interning import (controllable; drives make_interned_string_c). ---- */
void *as3vm_find_intern_string(void *vm, size_t units, void *chars,
int32_t create) {
(void)vm;
(void)units;
(void)chars;
last_intern_create = create;
++find_intern_string_count;
return (void *)intern_result;
}
/* ---- Closure implementations: address captured, never called. ---- */
void as3vm_functionclosure_implementation(void) {}
void as3vm_methodclosure_implementation(void) {}
void *as3vm_create_test_functionclosure_impl_addr(void) {
return (void *)&as3vm_functionclosure_implementation;
}
void *as3vm_create_test_methodclosure_impl_addr(void) {
return (void *)&as3vm_methodclosure_implementation;
}
/* ---- null/undefined sentinel: DATA symbol referenced by address. ---- */
uint8_t as3vm_nullundef_storage[64];
/* ---- Arena allocator import (non-GC permanent + static makers). ---- */
void *iggy_arena_malloc_aligned(void *player, void *heap, uint32_t size,
int32_t align, int32_t tag) {
(void)player;
(void)heap;
(void)tag;
++arena_malloc_aligned_count;
last_alloc_ptr = pool_alloc(size, (size_t)(align > 0 ? align : 16));
return last_alloc_ptr;
}
/* ---- rrstring imports (satisfied; real ASCII-range conversions). ---- */
int32_t rrUTF8toUTF16(uint16_t *destination, int32_t destination_units,
const uint8_t *source, int32_t source_bytes,
int32_t replacement, int32_t truncate) {
(void)replacement;
(void)truncate;
int32_t count = 0;
for (int32_t i = 0;
(source_bytes < 0 ? source[i] != 0 : i < source_bytes) &&
count < destination_units;
++i) {
destination[count++] = (uint16_t)source[i];
}
return count;
}
int32_t rrwcslen(const uint16_t *text) {
int32_t length = 0;
while (text[length] != 0) {
++length;
}
return length;
}

View File

@@ -0,0 +1,495 @@
"""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()

View File

@@ -0,0 +1,339 @@
"""Cross-platform contract tests for the reconstructed as3vm_create module.
Mirrors tests/test_reconstructed_as3vm_names_cross.py /
tests/test_reconstructed_iggy_sound_cross.py (the freshest 4-platform templates
whose imports GENUINELY differ per platform). RED until as3vm_create.c /
as3vm_create.h (and their CMake / modules.tsv rows) exist; the retail-evidence
assertions run now and prove the nm/strings parsing.
How EXPECTED_* were derived (all four .obj under
work/iggy-native-decomp/<platform>-objects/as3vm_create.obj, verified with
/opt/homebrew/opt/llvm/bin/llvm-nm):
EXPECTED_FUNCTIONS = `llvm-nm --extern-only --defined-only <obj>` T-tagged
names, leading '.' stripped on ps3. IDENTICAL 31-name
set on durango/orbis/ps3/vita -- the exported AS3 value
construction ABI is invariant. Retail *function* counts
are 32 on ALL FOUR (31 exports + 1 file-static helper,
make_stringdata_utf8_GC), so -- unlike as3vm_names /
iggy_sound -- the inventory uses a SINGLE combined row,
the iggy_allocation convention.
EXPECTED_GLOBALS = {} -- as3vm_create owns NO externally named data global.
Every table it touches (the built-in class prototypes at
vm+0x2830 / 0x2850 / 0x2868 / 0x28c8, the GC root stack
at vm+0x6b18, the class-name qnames at vm+0xda8 / 0xdc0)
lives INSIDE the caller's VM context, not this TU: nm
shows no B/D/C data symbols on any object. On ps3 the
.opd descriptors surface each export a second time as
'D'; those normalize to the exported function names and
are NOT separate globals (same handling as as3vm_names /
iggy_sound). The durango object carries three read-only
float literals ('R' constants) which are neither
T/D/B/C-extern nor globals in the reconstruction.
EXPECTED_IMPORTS = the genuine non-scaffolding references present on ALL
FOUR retail objects (invariant):
- GC allocator + barrier (as3vm_gc, unbuilt):
as3vm_gc_alloc, as3vm_gc_alloc_remembered,
as3vm_write_barrier_slowpath, as3vm_register_finalizer
- property naming (as3vm_class, unbuilt):
as3vm_set_property_name_GC, as3vm_qname_set_dont_enum_GC
- interning (as3vm_names, NOW reconstructed):
as3vm_find_intern_string
- closure impls, captured by ADDRESS (as3vm_interp,
unbuilt): as3vm_functionclosure_implementation,
as3vm_methodclosure_implementation
- null/undefined sentinel DATA symbol (as3vm_gc,
unbuilt): as3vm_nullundef_storage (referenced by
address; dot-UNprefixed on ps3 like the other
address-taken symbols -- it is DATA, not a call)
- arena (iggy_arena_allocator, satisfied):
iggy_arena_malloc_aligned
- rrstring (satisfied): rrUTF8toUTF16, rrwcslen
- libc the reconstruction lowers on every target: memcpy
This real-Iggy set is INVARIANT across the four platforms.
PLATFORM_IMPORTS = the per-platform libc/libm lowerings, from retail
`llvm-nm --undefined-only` (NOT forced uniform):
- floor (libm): the make_integer_GC / make_number_GC
in-range integral check emits a floor call on
durango/ps3/vita; orbis (-O2, x86_64) lowers it to
an inline SSE roundsd and has NO floor import.
- strlen (libc): the C-string makers emit strlen on
orbis/ps3/vita; durango inlines the do/while scan and
has NO strlen import.
durango: {floor}
orbis: {strlen}
ps3: {floor, strlen}
vita: {floor, strlen}
(memset appears on NONE of the four -- do not assume it.)
RUNTIME_ALLOWLISTS = pure toolchain scaffolding residual after subtracting
core + platform extras, verified exact against retail
`llvm-nm --undefined-only`:
durango: __GSHandlerCheck, __chkstk,
__security_check_cookie, __security_cookie,
_fltused
orbis: _GLOBAL_OFFSET_TABLE_
ps3: {} (empty)
vita: __aeabi_unwind_cpp_pr0, __stack_chk_fail,
__stack_chk_guard
(durango pulls __chkstk for make_stringdata_utf8_GC's
alloca and _fltused for the double math -- follow the
evidence.)
PROVENANCE = the canonical Windows source path
c:\\projects\\g\\src\\flash\\as3vm_create.c is the true
upstream path (orbis preserves it verbatim), so the
reconstruction embeds it and the SOURCE assertion checks
it. In the retail objects, though, the provenance string
is NOT uniform -- follow the evidence per platform:
durango: only the build path
c:\\projects\\g\\build\\...\\as3vm_create.obj
(no .c string at all)
orbis: c:\\projects\\g\\src\\flash\\as3vm_create.c
ps3: bare 'as3vm_create.c'
vita: bare 'as3vm_create.c'
so the retail-obj check asserts each platform's actual
embedded string, NOT a single uniform path.
"""
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_create.c"
HEADER = RECONSTRUCTED / "as3vm_create.h"
LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm")
VARIANTS = {
"durango": ("clang", "x86_64-pc-windows-msvc"),
"orbis": ("clang", "x86_64-unknown-freebsd"),
"vita": ("clang", "armv7a-unknown-linux-gnueabihf"),
}
PS3_CLANG = Path("/opt/homebrew/opt/llvm/bin/clang")
if PS3_CLANG.exists():
VARIANTS["ps3"] = (str(PS3_CLANG), "powerpc-unknown-linux-gnu")
EXPECTED_FUNCTIONS = {
"as3vm_finish_object_lazy",
"as3vm_make_array_GC",
"as3vm_make_array_from_arraydata_GC",
"as3vm_make_array_with_traits_GC",
"as3vm_make_arraydata_GC",
"as3vm_make_class_closure_GC",
"as3vm_make_function_closure_GC",
"as3vm_make_incomplete_array_GC",
"as3vm_make_int_GC",
"as3vm_make_integer_GC",
"as3vm_make_interned_string_c",
"as3vm_make_method_closure_GC",
"as3vm_make_namespace",
"as3vm_make_number_GC",
"as3vm_make_number_not_int_GC",
"as3vm_make_object_GC",
"as3vm_make_object_lazy_GC",
"as3vm_make_object_no_constructor_GC",
"as3vm_make_object_with_proto_GC",
"as3vm_make_object_with_traits_GC",
"as3vm_make_static_array",
"as3vm_make_static_object",
"as3vm_make_string_GC",
"as3vm_make_string_c_GC",
"as3vm_make_string_c_permanent",
"as3vm_make_string_permanent",
"as3vm_make_string_utf8_GC",
"as3vm_make_string_wc_GC",
"as3vm_make_stringdata_GC",
"as3vm_make_stringdata_c_GC",
"as3vm_make_uint_GC",
}
# as3vm_create owns no externally named data global (all VM state is caller
# owned; the durango R float literals are read-only constants, not globals).
EXPECTED_GLOBALS = set()
# Genuine non-scaffolding imports present on all four retail objects (invariant).
EXPECTED_IMPORTS = {
# GC allocator + barrier + finalizer (as3vm_gc -- not yet reconstructed).
"as3vm_gc_alloc",
"as3vm_gc_alloc_remembered",
"as3vm_write_barrier_slowpath",
"as3vm_register_finalizer",
# property naming (as3vm_class -- not yet reconstructed).
"as3vm_set_property_name_GC",
"as3vm_qname_set_dont_enum_GC",
# interning (as3vm_names -- now reconstructed).
"as3vm_find_intern_string",
# closure implementations, captured by ADDRESS (as3vm_interp -- unbuilt).
"as3vm_functionclosure_implementation",
"as3vm_methodclosure_implementation",
# null/undefined sentinel DATA symbol (as3vm_gc -- unbuilt).
"as3vm_nullundef_storage",
# arena (iggy_arena_allocator -- satisfied).
"iggy_arena_malloc_aligned",
# rrstring (satisfied).
"rrUTF8toUTF16",
"rrwcslen",
# libc the reconstruction lowers on every target.
"memcpy",
}
# Per-platform libc/libm lowerings (NOT forced into a single uniform set).
PLATFORM_IMPORTS = {
"durango": {"floor"},
"orbis": {"strlen"},
"ps3": {"floor", "strlen"},
"vita": {"floor", "strlen"},
}
RUNTIME_ALLOWLISTS = {
"durango": {"__GSHandlerCheck", "__chkstk", "__security_check_cookie",
"__security_cookie", "_fltused"},
"orbis": {"_GLOBAL_OFFSET_TABLE_"},
"ps3": set(),
"vita": {"__aeabi_unwind_cpp_pr0", "__stack_chk_fail", "__stack_chk_guard"},
}
# The provenance string actually embedded in each retail object differs (see the
# module docstring); assert per-platform evidence rather than a uniform path.
RETAIL_PROVENANCE = {
"durango": r"c:\projects\g\build",
"orbis": r"c:\projects\g\src\flash\as3vm_create.c",
"ps3": "as3vm_create.c",
"vita": "as3vm_create.c",
}
def normalized_symbol(name):
name = name.removeprefix(".")
name = name.removeprefix("__imp_")
if name == "_setjmp":
return "setjmp"
return name
class ReconstructedAs3vmCreateCrossTests(unittest.TestCase):
def test_native_provenance_counts_source_path_and_inventory(self):
# (function count, raw line count) per platform, from
# iggy_<platform>.modules.tsv column 3 and `wc -l` on the raw .c. All
# four are 32 functions / 31 exports -- identical, so a combined row.
expected = {
"durango": (32, 901), "orbis": (32, 986),
"ps3": (32, 1086), "vita": (32, 1096),
}
for platform, (functions, lines) in expected.items():
modules = ROOT / (
f"iggy-decompiled/symbols/iggy_{platform}.modules.tsv")
row = next(
line for line in modules.read_text(encoding="utf-8").splitlines()
if line.startswith("as3vm_create\t"))
self.assertEqual(int(row.split("\t")[2]), functions, platform)
raw = ROOT / f"iggy-decompiled/raw/{platform}/as3vm_create.c"
self.assertEqual(
len(raw.read_text(encoding="utf-8").splitlines()),
lines, platform)
obj = ROOT / (
f"work/iggy-native-decomp/{platform}-objects/as3vm_create.obj")
symbols = subprocess.run(
["nm", "-g", str(obj)], capture_output=True, text=True)
self.assertEqual(symbols.returncode, 0, (platform, symbols.stderr))
normalized = symbols.stdout.replace(" .", " ")
for symbol in EXPECTED_FUNCTIONS | EXPECTED_GLOBALS:
self.assertIn(symbol, normalized, (platform, symbol))
strings = subprocess.run(
["strings", str(obj)], capture_output=True, text=True)
self.assertIn(RETAIL_PROVENANCE[platform], strings.stdout, platform)
if SOURCE.exists():
cmake = (RECONSTRUCTED / "CMakeLists.txt").read_text(
encoding="utf-8")
self.assertIn(
"iggy_reconstructed_as3vm_create_${platform}", cmake)
self.assertIn(
"add_library(${target} STATIC as3vm_create.c)", cmake)
self.assertIn(
"IGGY_RECONSTRUCT_AS3VM_CREATE_${platform_upper}", cmake)
inventory = (RECONSTRUCTED / "modules.tsv").read_text(
encoding="utf-8")
# Counts are identical (32 functions / 31 exports) on all four, so
# the inventory uses a SINGLE combined row -- the iggy_allocation
# convention -- not per-platform rows.
self.assertIn(
"as3vm_create\t32\t31\tdurango,orbis,ps3,vita\tcomplete",
inventory)
def test_exact_exports_globals_normalized_imports_and_c_cpp_abi(self):
self.assertTrue(SOURCE.exists(), f"missing {SOURCE}")
self.assertTrue(HEADER.exists(), f"missing {HEADER}")
self.assertIn(r"c:\\projects\\g\\src\\flash\\as3vm_create.c",
SOURCE.read_text(encoding="utf-8"))
function_pointers = "".join(
f" (GenericFn)&{name},\n" for name in sorted(EXPECTED_FUNCTIONS))
probe_text = (
'#include <stdint.h>\n#include "as3vm_create.h"\n'
'typedef void (*GenericFn)(void);\n'
'static GenericFn create_abis[] = {\n' + function_pointers + '};\n'
'int use_abis(void) {\n'
' for (unsigned i = 0; '
'i < sizeof(create_abis) / sizeof(create_abis[0]); ++i) {\n'
' if (!create_abis[i]) return 0;\n'
' }\n'
' return 1;\n'
'}\n')
with tempfile.TemporaryDirectory() as directory:
temporary = Path(directory)
for platform, (compiler, target) in VARIANTS.items():
extra = ["-mthumb"] if platform == "vita" else []
output = temporary / f"as3vm_create_{platform}.o"
build = subprocess.run([
compiler, "-target", target, "-ffreestanding",
"-fno-builtin",
"-c", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
*extra,
f"-DIGGY_RECONSTRUCT_AS3VM_CREATE_{platform.upper()}=1",
"-I", str(RECONSTRUCTED), str(SOURCE), "-o", str(output),
], capture_output=True, text=True)
self.assertEqual(build.returncode, 0, (platform, build.stderr))
if LLVM_NM.exists():
defined = subprocess.run([
str(LLVM_NM), "--extern-only", "--defined-only",
str(output),
], capture_output=True, text=True)
undefined = subprocess.run([
str(LLVM_NM), "--undefined-only", str(output),
], capture_output=True, text=True)
names = {normalized_symbol(parts[-1])
for line in defined.stdout.splitlines()
if len(parts := line.split()) >= 2 and
parts[-2].upper() in {"T", "D", "B", "C"}}
imports = {normalized_symbol(line.split()[-1])
for line in undefined.stdout.splitlines()
if line.split()}
self.assertEqual(
names, EXPECTED_FUNCTIONS | EXPECTED_GLOBALS, platform)
expected_imports = (
EXPECTED_IMPORTS | PLATFORM_IMPORTS[platform])
self.assertEqual(
imports - RUNTIME_ALLOWLISTS[platform],
expected_imports, (platform, imports))
for language, standard in (("c", "c11"), ("c++", "c++17")):
suffix = "c" if language == "c" else "cpp"
probe = temporary / f"as3vm_create_abi_{platform}.{suffix}"
probe.write_text(probe_text, encoding="utf-8")
syntax = subprocess.run([
compiler, "-target", target, "-ffreestanding",
"-fsyntax-only", f"-std={standard}",
"-Wall", "-Wextra", "-Werror", *extra,
f"-DIGGY_RECONSTRUCT_AS3VM_CREATE_{platform.upper()}=1",
"-I", str(RECONSTRUCTED), str(probe),
], capture_output=True, text=True)
self.assertEqual(syntax.returncode, 0,
(platform, language, syntax.stderr))
if __name__ == "__main__":
unittest.main()