diff --git a/iggy-decompiled/src/reconstructed/CMakeLists.txt b/iggy-decompiled/src/reconstructed/CMakeLists.txt index e3bf4ea..073f7ec 100644 --- a/iggy-decompiled/src/reconstructed/CMakeLists.txt +++ b/iggy-decompiled/src/reconstructed/CMakeLists.txt @@ -571,6 +571,27 @@ target_compile_definitions( ) configure_iggy_reconstructed_target(iggy_reconstructed_radss_xaudio2_durango c_std_11) +# as3vm_interpreter -- AS3 / AVM2 bytecode interpreter + core VM op layer +# (4-platform; the VM cluster keystone). Imports fmod / memcpy (the AS3 '%' +# operator + value/scope moves), so guard against libcall re-synthesis with +# -fno-builtin (clang/gcc) or /Oi- (MSVC), matching the sibling as3vm_gc / +# as3vm_class / as3vm_convert / as3vm_property / as3vm_imp_array. +foreach(platform IN ITEMS durango orbis ps3 vita) + string(TOUPPER ${platform} platform_upper) + set(target iggy_reconstructed_as3vm_interpreter_${platform}) + add_library(${target} STATIC as3vm_interpreter.c) + target_compile_definitions( + ${target} + PRIVATE IGGY_RECONSTRUCT_AS3VM_INTERPRETER_${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() + # rrCore: 4-platform, data-only TU (one owned global, zero functions). foreach(platform IN ITEMS durango orbis ps3 vita) string(TOUPPER ${platform} platform_upper) diff --git a/iggy-decompiled/src/reconstructed/README.md b/iggy-decompiled/src/reconstructed/README.md index 3d03ab5..7c0db37 100644 --- a/iggy-decompiled/src/reconstructed/README.md +++ b/iggy-decompiled/src/reconstructed/README.md @@ -513,6 +513,39 @@ disassembly on all four builds — the runtime harness exercises only the 64-bit path, so every 32-bit offset was recovered directly from the ps3/vita raw. The value construction, conversion, GC, and interpreter primitives remain imported. +`as3vm_interpreter.c` restores the AVM2 bytecode interpreter and the core VM +operations — the keystone of the VM cluster. Its four builds (per-platform +function counts 58/46/45/57, split across globals, equality, support, construct, +call, and the single large dispatch include) expose 37 entry points and, more +importantly, *own* the ten VM sentinel globals every other VM unit imports: the +`nullundef_storage`/`truefalse_storage` value cells (null at +1 and undefined at ++9, false at +1 and true at +9, exact type bytes 0x05/0x04 and 0x06), the +`global_nan`/`global_inf` number boxes, `must_init`, the four infinity/NaN +constants, and the profiling counter — all byte-verified against the retail +`.data` on every build. It reconstructs `interpreter_initialize` and the sentinel +setup, the NaN/infinity classification, `is_ofclass`/`coerce`/`cast` with the +type-byte dispatch that subsumes the retail `&__ImageBase` jump table, the slot +get/set primitives with number-boxing and the write barrier, the string concat +and interning entry points, the call machinery (function, method, getter, setter, +apply, and the closure trampolines with their argument spill and this-binding), +the construction sequence with its display-object factory and pool paths, and the +whole `interpret_method_GC` opcode-dispatch loop — stack, local, and scope +manipulation, arithmetic and modulo, comparison and the abstract/strict equality +engine, branches and lookupswitch, the call/construct/callproperty/callsuper +family, the getproperty/setproperty fast paths with their Dictionary and Array +specialisations, slot access, the is/as/instanceof and new-object/array/class/ +activation/catch/applytype opcodes, findproperty, and the `in` operator. Because +the retail `interpret_method_GC` is an un-recovered jump table in *both* 64-bit +decompiles (Ghidra bails with "too many branches"), the 32-bit offsets came from +the fully-expanded vita build and every 64-bit opcode-body offset — method +tables, slot counts, signature return types, activation and class-info layouts, +the object prototype field, the constructsuper native-ctor slot, and the typeof +string cache — was recovered directly from the durango and orbis object +disassembly, which also exposed two latent lift errors (a `newactivation` +prototype misread and a phantom `typeof` number-class substitution) that the +object machine code disproved. The value pool, name resolution, property, GC, +conversion, and display-object 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. diff --git a/iggy-decompiled/src/reconstructed/as3vm_interpreter.c b/iggy-decompiled/src/reconstructed/as3vm_interpreter.c new file mode 100644 index 0000000..1a4f0f7 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/as3vm_interpreter.c @@ -0,0 +1,450 @@ +/* Reconstructed translation unit for RAD's Iggy AS3 / AVM2 bytecode interpreter + * and core VM operation layer -- the keystone of the AS3/AVM2 VM cluster. */ +/* Source-equivalent recovery of c:\projects\g\src\flash\as3vm_interpreter.c */ + +#include +#include + +#include "as3vm_interpreter.h" + +#if (defined(IGGY_RECONSTRUCT_AS3VM_INTERPRETER_DURANGO) + \ + defined(IGGY_RECONSTRUCT_AS3VM_INTERPRETER_ORBIS) + \ + defined(IGGY_RECONSTRUCT_AS3VM_INTERPRETER_PS3) + \ + defined(IGGY_RECONSTRUCT_AS3VM_INTERPRETER_VITA)) != 1 +#error "select exactly one as3vm_interpreter 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 (ps3 big endian). */ +#if defined(IGGY_RECONSTRUCT_AS3VM_INTERPRETER_DURANGO) || \ + defined(IGGY_RECONSTRUCT_AS3VM_INTERPRETER_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_interpreter_source_path[] = + "c:\\projects\\g\\src\\flash\\as3vm_interpreter.c"; + +/* An as3value is a machine word carrying a 2-bit tag in its low bits. */ +typedef uintptr_t as3v; + +/* ================================================================== */ +/* Value-model tag helpers (identical on all four targets). */ +/* ================================================================== */ + +#define TAG(v) ((as3v)(v) & 3u) +#define IS_HEAP(v) (TAG(v) == 1u) +#define TYPEB(v) (*(uint8_t *)((as3v)(v) - 1)) /* heap cell type byte */ +#define MARKB(v) (*(uint8_t *)(as3v)(v)) /* heap cell GC mark byte */ +#define HOLE ((as3v)7) /* empty-slot sentinel */ + +/* ================================================================== */ +/* Owned VM sentinel-value globals (the keystone data every sibling */ +/* module imports by address). Exact retail-recovered initialized */ +/* bytes: truefalse false@+1 / true@+9 (type 0x06), nullundef null@+1 */ +/* (0x05) / undefined@+9 (0x04), global_nan / global_inf boxed number */ +/* header 0x3007 with a zero double payload, must_init == 1. */ +/* ================================================================== */ + +uint8_t as3vm_truefalse_storage[16] = { + 0x06, 0x30, 0, 0, 0, 0, 0, 0, + 0x06, 0x30, 0, 0, 0, 0, 0, 0}; +uint8_t as3vm_nullundef_storage[16] = { + 0x05, 0x30, 0, 0, 0, 0, 0, 0, + 0x04, 0x30, 0, 0, 0, 0, 0, 0}; +uint8_t as3vm_global_nan[16] = { + 0x07, 0x30, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0}; +uint8_t as3vm_global_inf[16] = { + 0x07, 0x30, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0}; +uint32_t as3vm_must_init = 1; + +/* Bss counter (zero) + the tentative common NaN / Infinity doubles the + * whole VM cluster reads by address. */ +uint32_t as3vm_profiling; +float as3vm_NaNf; +double as3vm_NaNd; +double as3vm_N_Infinity; +double as3vm_P_Infinity; + +/* Value-cell + boxed-number sentinels carved out of the storage blobs. */ +#define VFALSE ((as3v)&as3vm_truefalse_storage[1]) /* boolean false */ +#define VTRUE ((as3v)&as3vm_truefalse_storage[9]) /* boolean true */ +#define VNULL ((as3v)&as3vm_nullundef_storage[1]) /* null */ +#define VUNDEF ((as3v)&as3vm_nullundef_storage[9]) /* undefined */ +#define VNAN ((as3v)&as3vm_global_nan[1]) /* boxed NaN number */ + +/* ================================================================== */ +/* Machine-width VM-context field offsets. 64-bit column recovered */ +/* from the durango object (llvm-objdump -d); 32-bit column from the */ +/* vita raw lift. All are byte offsets from the vm base pointer. */ +/* ================================================================== */ + +#if IGGY_AS3_64BIT +#define WORD 8 + +#define VM_FRAME_CUR 0x28f0 /* current locals base pointer */ +#define VM_FRAME_FLOOR 0x28f8 /* locals underflow floor */ +#define VM_EXC_SRC 0x2900 /* saved-into-frame at initialize */ +#define VM_PENDING_EXC 0x2908 /* pending AS3 error value */ +#define VM_ERR_LINE 0x2914 /* pending error source line (int32) */ +#define VM_ERR_LOC 0x2918 /* pending error source location ptr */ +#define VM_ERR_CLR 0x2918 /* cleared at throw */ +#define VM_ERR_D2 0x2920 /* previous error info value */ +#define VM_ERR_D1 0x2928 /* previous error info extra */ +#define VM_ERR_S1 0x2938 /* current error info extra */ +#define VM_ERR_S2 0x2940 /* current error info value */ +#define VM_CLEAR_A 0x28d0 +#define VM_CLEAR_B 0x28d8 +#define VM_GC_TOP 0x6b18 +#define VM_GC_BASE 0x6b10 +#define VM_GC_LIMIT 0x6b20 +#define VM_NUMBER_CLASS 0x2840 +#define VM_LATENAME 0x2938 +#define VM_LCACHE_ENABLE 0x6960 +#define VM_LCACHE_BASE 0x3960 +#define VM_LCACHE_DICT 0x3978 +#define VM_LCACHE_STRIDE 0x180 +#define VM_SCRIPT_TABLE 0x70 +#define VM_SCRIPT_CLASS 0x28c0 +#define VM_OBJECT_CLASS 0x2830 +#define VM_CLASS_CLASS 0x2648 +#define VM_FUNC_CLASS 0x2860 +#define VM_ARGUMENTS_QN 0xe20 +#define VM_DEFAULT_OBJ 0x2828 +#define VM_APPLY_THIS 0x58 +#define VM_CTOR_QN 0xda8 +#define VM_METHOD_CTX 0xd28 + +/* Object / class / arraydata layout (64-bit). */ +#define O_CLASS 8 /* object -> holder/class ptr */ +#define CLS_SLOTBASE 0x68 /* class + 0x68 + slot*0x10 = field ushort */ +#define CLS_SLOTCLASS 0x60 /* class + (slot+6)*0x10 = slot class ptr */ +#define CLS_SLOTSTRIDE 0x10 +#define CLS_SLOTTYPE 0x6a /* class + 0x6a + slot*0x10 = slot type byte */ +#define TYPE_TAG_OFF 0x1b /* class + 0x1b = the type byte */ +#define O_INVOKE 0x10 /* callable object cell + 0x10 = invoke fn ptr */ +#define V_INVOKE 0xf /* callable as3value + 0xf = invoke fn ptr */ +#define O_STARTCLASS 0x20 /* holder + 0x20 = object start class */ +#define CLS_SUPER 0x40 /* class + 0x40 = superclass link */ +#define CLS_PARAM_TYPE 0x48 /* parameterized(Vector) class + 0x48 = elem-type id (word) */ +#define CLS_IFACE_CNT 0x1e /* class + 0x1e = interface count (byte) */ +#define CLS_IFACE_PTR 0x58 /* class + 0x58 = interface ptr array */ +#define STR_LEN 7 /* stringdata + 7 = length (int32) */ +#define STR_BYTES 0xb /* stringdata + 0xb = utf16 payload */ +#define INTERN_ADJ 0x29 /* interned-stringdata back-adjust */ +#define INTERN_ADJ2 0x28 + +#else /* 32-bit (ps3 / vita) */ +#define WORD 4 + +#define VM_FRAME_CUR 0x16dc +#define VM_FRAME_FLOOR 0x16e0 +#define VM_EXC_SRC 0x16e4 +#define VM_PENDING_EXC 0x16e8 +#define VM_ERR_LINE 0x16f0 +#define VM_ERR_LOC 0x16f4 +#define VM_ERR_CLR 0x16f4 +#define VM_ERR_D2 0x16f8 +#define VM_ERR_D1 0x16fc +#define VM_ERR_S1 0x1704 +#define VM_ERR_S2 0x1708 +#define VM_CLEAR_A 0x16cc +#define VM_CLEAR_B 0x16d0 +#define VM_GC_TOP 0x4834 +#define VM_GC_BASE 0x4830 +#define VM_GC_LIMIT 0x4838 +#define VM_NUMBER_CLASS 0x1684 +#define VM_LATENAME 0x1704 +#define VM_LCACHE_ENABLE 0x4720 +#define VM_LCACHE_BASE 0x2720 +#define VM_LCACHE_DICT 0x2720 +#define VM_LCACHE_STRIDE 0x100 +#define VM_SCRIPT_TABLE 0x40 +#define VM_SCRIPT_CLASS 0x16c4 +#define VM_OBJECT_CLASS 0x1564 +#define VM_CLASS_CLASS 0x1564 +#define VM_FUNC_CLASS 0x167c +#define VM_ARGUMENTS_QN 0x710 +#define VM_DEFAULT_OBJ 0x1414 +#define VM_APPLY_THIS 0x2c +#define VM_CTOR_QN 0x244 +#define VM_METHOD_CTX 0x694 + +/* Object / class / arraydata layout (32-bit). */ +#define O_CLASS 8 +#define CLS_SLOTBASE 0x3c /* class + 0x3c + slot*8 = field ushort */ +#define CLS_SLOTCLASS 0x38 /* class + 0x38 + slot*8 = slot class ptr */ +#define CLS_SLOTSTRIDE 8 +#define CLS_SLOTTYPE 0x3e +#define TYPE_TAG_OFF 0xf +#define O_INVOKE 0xc +#define V_INVOKE 0xb +#define O_STARTCLASS 0x18 +#define CLS_SUPER 0x24 +#define CLS_PARAM_TYPE 0x28 /* parameterized(Vector) class + 0x28 = elem-type id (word) */ +#define CLS_IFACE_CNT 0x12 +#define CLS_IFACE_PTR 0x30 +#define STR_LEN 7 +#define STR_BYTES 0xb +#define INTERN_ADJ 0x19 +#define INTERN_ADJ2 0x18 +#endif + +/* GC operand-stack push / pop helpers (spill a value across a re-entrant + * call so the collector can see it). */ +#define GC_TOP(vm) (*(as3v *)((as3v)(vm) + VM_GC_TOP)) +#define GC_PUSH(vm, val) \ + do { \ + *(as3v *)GC_TOP(vm) = (as3v)(val); \ + GC_TOP(vm) += WORD; \ + } while (0) +#define GC_POP(vm) (GC_TOP(vm) -= WORD) + +/* Read a machine-word VM-context field by byte offset. */ +#define VMF(vm, off) (*(as3v *)((as3v)(vm) + (off))) + +/* ================================================================== */ +/* Recovered external dependencies (the genuine nm ' U ' imports). */ +/* Invariant 69-symbol Iggy set + {fmod, memcpy, rr_isnan} on all four. */ +/* ================================================================== */ + +/* type conversion (as3vm_convert). */ +extern as3v as3vm_DefaultValue_None_GC(void *vm, as3v value); +extern as3v as3vm_DefaultValue_Number_GC(void *vm, as3v value); +extern int as3vm_NumberToInt32(double value); +extern int as3vm_StringToArrayIndex(void *stringdata, uint32_t *out); +extern void *as3vm_ToCharArray_GC(void *vm, as3v value, void *scratch, + int *out_len); +extern int as3vm_ToInt32_GC(void *vm, as3v value, int32_t *out); +extern as3v as3vm_ToNumberDirect_GC(void *vm, as3v value, double *out); +extern as3v as3vm_ToNumber_GC(void *vm, as3v value); +extern as3v as3vm_ToString_GC(void *vm, as3v value); +extern int as3vm_ToUInt32_GC(void *vm, as3v value, uint32_t *out); +extern as3v as3vm_to_boolean(as3v value); +extern int as3vm_to_boolean_direct(as3v value); + +/* array backing (as3vm_imp_array). */ +extern int64_t as3vm_array_delete_value_GC(void *vm, void *object, int index); +extern void as3vm_array_set_value_GC(void *vm, void *object, int index, + void *value); + +/* property resolution (as3vm_property + unbuilt variants). */ +extern as3v as3vm_call_property_name_super(void *vm, as3v object, as3v name); +extern as3v as3vm_find_property_scoped_GC(void *vm, as3v scope, as3v name); +extern as3v as3vm_get_property_name_GC(void *vm, as3v object, as3v name); +extern as3v as3vm_get_property_name_closure_GC(void *vm, as3v object, as3v name); +extern as3v as3vm_get_property_name_closure_super_GC(void *vm, as3v object, + as3v name); +extern as3v as3vm_get_property_name_flexible(void *vm, as3v object, as3v name); +extern as3v as3vm_get_property_qname_GC(void *vm, as3v object, as3v qname); +extern as3v as3vm_get_property_scoped_closure_GC(void *vm, as3v scope, + as3v name); +extern as3v as3vm_get_prototype(void *vm, as3v value); +extern int64_t as3vm_object_delete_property_GC(void *vm, as3v object, as3v name); +extern int64_t as3vm_proplist_delete_GC(void *vm, as3v object, as3v name); +extern as3v as3vm_proplist_get(void *vm, as3v object, as3v name); +extern void as3vm_proplist_set_GC(void *vm, as3v object, as3v name, as3v value); +extern intptr_t as3vm_qmap_find_name(void *vm, void *table, as3v name); +extern void as3vm_set_property_name_GC(void *vm, as3v object, as3v name, + as3v value, int flags); +extern as3v as3vm_set_property_name_super_GC(void *vm, as3v object, as3v name, + as3v value); + +/* naming / interning (as3vm_names). */ +extern intptr_t as3vm_find_intern_string(void *vm, intptr_t length, void *bytes, + uint32_t flags); +extern as3v as3vm_make_namespace(void *vm); +extern void as3vm_multiname_free(void *vm, as3v multiname); +extern intptr_t as3vm_multiname_make_optimized(void *vm, intptr_t name, + intptr_t count, uint32_t flags); +extern intptr_t as3vm_qname_add_init(void *vm, void *name, void *table, + int flags); +extern intptr_t as3vm_qname_add_init_safe(void *vm, void *name, void *table, + int flags); +extern intptr_t as3vm_qname_find(void *vm, void *name, void *table, int flags); + +/* value construction (as3vm_create + unbuilt). */ +extern as3v as3vm_make_array_GC(void *vm, int count, int flags); +extern as3v as3vm_make_array_with_traits_GC(void *vm, void *traits, int flag); +extern void *as3vm_make_arraydata_GC(void *vm, int count, int flags); +extern as3v as3vm_make_error_GC(void *vm, int code); +extern as3v as3vm_make_function_closure_GC(void *vm, as3v scope, as3v method); +extern as3v as3vm_make_int_GC(void *vm, int32_t value); +extern as3v as3vm_make_integer_GC(void *vm, int64_t value); +extern as3v as3vm_make_method_closure_GC(void *vm, as3v this_value, as3v method); +extern as3v as3vm_make_number_GC(void *vm, as3v raw_value); +extern as3v as3vm_make_number_not_int_GC(void *vm, double value); +extern as3v as3vm_make_object_GC(void *vm, int type); +extern as3v as3vm_make_object_no_constructor_GC(void *vm); +extern as3v as3vm_make_object_with_proto_GC(void *vm, as3v proto); +extern as3v as3vm_make_object_with_traits_GC(void *vm, void *traits, int flag); +extern as3v as3vm_make_string_GC(void *vm, void *stringdata); +extern void *as3vm_make_stringdata_GC(void *vm, void *bytes, int length); +extern as3v as3vm_make_uint_GC(void *vm, uint32_t value); +extern as3v as3vm_make_vector_class(void *vm, as3v element_class); + +/* GC (as3vm_gc). */ +extern void as3vm_write_barrier_slowpath(void *vm, void *target, void *value); + +/* component / pool (genuinely-unbuilt). */ +extern void as3vmcomp_set_value(void *vm, void *target, as3v value, int flags); +extern as3v as3vm_pool_alloc(void *vm, as3v value, int flags); +extern void as3vm_pool_free(void *vm); + +/* flash display factories (swf_play / flash_display -- unbuilt). */ +extern intptr_t iggy_create_empty_bitmap(void *handle); +extern intptr_t iggy_create_movie_complete_GC(void *handle, void *object, + void *args, int argc); +extern intptr_t iggy_make_empty_button(void *handle); +extern intptr_t iggy_make_empty_shape(void *handle); +extern intptr_t iggy_make_empty_sprite(void *handle); +extern intptr_t iggy_make_empty_textfield(void *handle); +extern void iggy_mark_movie_dirty(void *handle, intptr_t object); + +/* iggy_main diagnostics + rr time. */ +extern int iggy_random(void); +extern void iggy_uncaught_exception_warning(void *handle); +extern uint64_t rrGetTicks(void); + +/* libc / libm the reconstruction lowers. */ +extern double fmod(double x, double y); +extern void *memcpy(void *dst, const void *src, size_t n); +extern int rr_isnan(double value); + +/* On macOS the runtime durango dylib links with -undefined dynamic_lookup and + * the host support object supplies only the shallow-path imports; these weak + * references let the un-exercised remainder resolve lazily. Harmless on the + * cross-compile-only (freestanding) targets. */ +#if defined(__APPLE__) +#pragma weak as3vm_DefaultValue_None_GC +#pragma weak as3vm_DefaultValue_Number_GC +#pragma weak as3vm_NumberToInt32 +#pragma weak as3vm_StringToArrayIndex +#pragma weak as3vm_ToCharArray_GC +#pragma weak as3vm_ToInt32_GC +#pragma weak as3vm_ToNumber_GC +#pragma weak as3vm_ToString_GC +#pragma weak as3vm_ToUInt32_GC +#pragma weak as3vm_to_boolean +#pragma weak as3vm_to_boolean_direct +#pragma weak as3vm_array_delete_value_GC +#pragma weak as3vm_array_set_value_GC +#pragma weak as3vm_call_property_name_super +#pragma weak as3vm_find_property_scoped_GC +#pragma weak as3vm_get_property_name_GC +#pragma weak as3vm_get_property_name_closure_GC +#pragma weak as3vm_get_property_name_closure_super_GC +#pragma weak as3vm_get_property_name_flexible +#pragma weak as3vm_get_property_qname_GC +#pragma weak as3vm_get_property_scoped_closure_GC +#pragma weak as3vm_get_prototype +#pragma weak as3vm_object_delete_property_GC +#pragma weak as3vm_proplist_delete_GC +#pragma weak as3vm_proplist_get +#pragma weak as3vm_proplist_set_GC +#pragma weak as3vm_qmap_find_name +#pragma weak as3vm_set_property_name_GC +#pragma weak as3vm_set_property_name_super_GC +#pragma weak as3vm_make_namespace +#pragma weak as3vm_multiname_free +#pragma weak as3vm_multiname_make_optimized +#pragma weak as3vm_qname_add_init +#pragma weak as3vm_qname_add_init_safe +#pragma weak as3vm_qname_find +#pragma weak as3vm_make_array_GC +#pragma weak as3vm_make_array_with_traits_GC +#pragma weak as3vm_make_arraydata_GC +#pragma weak as3vm_make_error_GC +#pragma weak as3vm_make_function_closure_GC +#pragma weak as3vm_make_int_GC +#pragma weak as3vm_make_integer_GC +#pragma weak as3vm_make_method_closure_GC +#pragma weak as3vm_make_number_not_int_GC +#pragma weak as3vm_make_object_GC +#pragma weak as3vm_make_object_no_constructor_GC +#pragma weak as3vm_make_object_with_proto_GC +#pragma weak as3vm_make_object_with_traits_GC +#pragma weak as3vm_make_string_GC +#pragma weak as3vm_make_stringdata_GC +#pragma weak as3vm_make_uint_GC +#pragma weak as3vm_make_vector_class +#pragma weak as3vmcomp_set_value +#pragma weak as3vm_pool_alloc +#pragma weak as3vm_pool_free +#pragma weak iggy_create_empty_bitmap +#pragma weak iggy_create_movie_complete_GC +#pragma weak iggy_make_empty_button +#pragma weak iggy_make_empty_shape +#pragma weak iggy_make_empty_sprite +#pragma weak iggy_make_empty_textfield +#pragma weak iggy_mark_movie_dirty +#pragma weak iggy_random +#pragma weak iggy_uncaught_exception_warning +#pragma weak rrGetTicks +#pragma weak as3vm_ToNumber_GC +#pragma weak fmod +#endif + +/* ================================================================== */ +/* Forward declarations of the file-static helpers shared across the */ +/* reconstruction's split translation-unit fragments. */ +/* ================================================================== */ + +static as3v abstract_equality(void *vm, as3v a, as3v b); +static as3v abstract_relational(void *vm, as3v a, as3v b); +static as3v strict_equality(as3v a, as3v b); +static as3v string_data_compare(as3v a, as3v b); +static int string_eq(as3v a, as3v b); +static uint64_t is_instance_of_user(void *vm, as3v value, as3v klass); +static uint64_t is_oftype(void *vm, as3v value, as3v type); +static void lazy_init_class_GC(void *vm, as3v klass); +static as3v as3vm_initialize_script_GC(void *vm, as3v script); +static void prep_method_exec_GC(void *vm, void *frame, as3v result, as3v fn, + as3v method, void *args, unsigned argc, + void *call_frame); +static void interpret_method_GC(as3v *frame); +static void interpret_method_profiled_GC(as3v *frame); +static void record_error_location(void *vm, as3v *record); +static void record_error_function(void *vm, as3v value); +static void record_error_info(void *vm, as3v value, as3v extra); +static void *make_scope_stack_closure_GC(void *vm, void *scope); +static void *make_scope_stack_closure_extended_GC(void *vm, void *scope, + as3v extra); +static as3v resolve_latename_qname(void *vm, as3v name, as3v value, + uint32_t flags); +static as3v *name_resolve_runtime_GC(void *vm, as3v *name_slot, as3v *value_slot, + uint32_t flags); +static void *clone_arrdata_GC(void *vm, void *arraydata); +static void do_func_call_mv_GC(void *vm, as3v result, as3v *method, as3v this_v, + void *args, unsigned flags); + +/* Record the pending AS3 error into the VM error slots (the inlined + * make_error + slot-shuffle sequence the raw repeats at every throw site). */ +static void raise_error(void *vm, int code) { + as3v base = (as3v)vm; + as3v err = as3vm_make_error_GC(vm, code); + *(as3v *)(base + VM_PENDING_EXC) = err; + *(as3v *)(base + VM_ERR_CLR) = 0; + *(as3v *)(base + VM_ERR_D1) = *(as3v *)(base + VM_ERR_S1); + *(as3v *)(base + VM_ERR_D2) = *(as3v *)(base + VM_ERR_S2); + *(as3v *)(base + VM_ERR_S1) = 0; + *(as3v *)(base + VM_ERR_S2) = 0; +} + +/* The reconstruction is split into focused fragments to keep each file small + * (high cohesion, low coupling) exactly like the sibling VM modules. */ +#include "as3vm_interpreter_globals.inc" +#include "as3vm_interpreter_equality.inc" +#include "as3vm_interpreter_support.inc" +#include "as3vm_interpreter_construct.inc" +#include "as3vm_interpreter_call.inc" +#include "as3vm_interpreter_dispatch.inc" diff --git a/iggy-decompiled/src/reconstructed/as3vm_interpreter.h b/iggy-decompiled/src/reconstructed/as3vm_interpreter.h new file mode 100644 index 0000000..1d8a388 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/as3vm_interpreter.h @@ -0,0 +1,118 @@ +#ifndef IGGY_RECONSTRUCTED_AS3VM_INTERPRETER_H +#define IGGY_RECONSTRUCTED_AS3VM_INTERPRETER_H + +/* + * Reconstructed public ABI for RAD's as3vm_interpreter translation unit -- Iggy's + * AS3 / AVM2 bytecode interpreter and the core VM operation layer, the keystone + * of the AS3/AVM2 VM cluster. + * + * Source-equivalent recovery of c:\projects\g\src\flash\as3vm_interpreter.c + * + * The module exports EXACTLY 37 entry points on all four platforms (durango / + * orbis / ps3 / vita) and -- UNIQUELY among the VM modules -- OWNS the ten VM + * sentinel-value data globals every other VM module imports by address + * (as3vm_truefalse_storage / as3vm_nullundef_storage / as3vm_global_nan / + * as3vm_global_inf / as3vm_must_init / as3vm_profiling / as3vm_NaNf / + * as3vm_NaNd / as3vm_N_Infinity / as3vm_P_Infinity). The internal file-static + * helper count DIFFERS per platform (58 / 46 / 45 / 57 source_functions; vita + * fully expands the opcode-dispatch switch inline) while the exported ABI stays + * a fixed 37 on all four. + * + * VALUE / SENTINEL MODEL (64-bit offsets; the 32-bit column is macro-gated in + * the .c): an as3value is a tagged machine word -- (v & 3) is the tag. Tag 1 == + * heap cell with a 1-byte type at v[-1]; an odd non-1 word (tag 3) is an inline + * int; 7 is the empty-slot / "hole" sentinel. The value-cell sentinels every + * sibling module imports live INSIDE this module's two owned storage blobs: + * null = &nullundef_storage[1], undefined = &nullundef_storage[9], + * false = &truefalse_storage[1], true = &truefalse_storage[9]. A boxed number + * is a cell whose double payload lives at +8. + * + * 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 LP64, ps3/vita ILP32, ps3 big-endian) but the exported + * symbol names are invariant across all four. + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* ---- Debug / no-op trap hooks. ---- */ +void IggyDebugBreakOnAS3Exception(void); +void iggy_monitor_variable(void); + +/* ---- Trivial deterministic VM state ops. ---- */ +uint64_t as3vm_do_validate(void); +void as3vm_interpreter_notice_gc(void); +void as3vm_interpreter_request_collection(void); +void as3vm_interpreter_initialize(void *vm); +void as3vm_finalize(void *vm, void *object); + +/* ---- Boxed-number classification (reads *(box+8)). ---- */ +uint64_t as3vm_is_nan(void *box); +uint64_t as3vm_is_inf(void *box); + +/* ---- Class-chain membership. ---- */ +uint64_t as3vm_is_ofclass(void *object, void *klass); + +/* ---- Slot read / write (with number boxing + write barrier). ---- */ +uint64_t as3vm_slot_get_GC(void *vm, void *object, int slot); +uint64_t as3vm_slot_set_GC(void *vm, void *object, int slot, void *value, + int init); +uint64_t as3vm_avm2_slot_get_GC(void *vm, void *object, int slot); +uint64_t as3vm_avm2_slot_set_GC(void *vm, void *object, int slot, void *value, + int init); + +/* ---- Type coercion / cast (type-byte jump-table dispatch). ---- */ +int64_t as3vm_coerce_GC(void *vm, int64_t value, int64_t type); +int64_t as3vm_cast_GC(void *vm, int64_t value, int64_t type); + +/* ---- String interning. ---- */ +int64_t as3vm_intern_string_value(void *vm, int64_t value, uint32_t flags); +int64_t as3vm_string_interned(void *vm, int64_t value); +int64_t as3vm_string_interned_nocreate(void *vm, int64_t value); +uint64_t as3vm_string_concatenate_GC(void *vm, uint64_t a, uint64_t b); + +/* ---- Script / class-script lookup. ---- */ +void *as3vm_find_class_script_GC(void *vm, void *name); +int64_t as3vm_get_class_script_GC(void *vm, void *name); + +/* ---- Lookup-cache flush. ---- */ +void as3vm_flush_lcache(void *vm, int64_t value); +void as3vm_flush_lcache_dict(void *vm, int64_t value); + +/* ---- Exception raise (records the pending AS3 error into the VM). ---- */ +void as3vm_exception_GC(void *vm, int code); + +/* ---- Object construction sequence. ---- */ +void *as3vm_construct_pre_GC(void *vm, int64_t klass_value); +void as3vm_construct_GC(void *vm, void *result, int64_t klass_value, + void *this_slot, uint32_t argc); +void as3vm_construct_post_GC(void *frame, void *result, int64_t klass_value, + void *this_slot, uint32_t argc); + +/* ---- Call machinery (arg spill + this-binding + frame setup). ---- */ +uint64_t as3vm_do_func_call_vmc_GC(void *vm, void *result, void *fn, + int64_t frame, void *args, uint32_t flags); +uint64_t as3vm_do_func_call_vv_GC(void *vm, int64_t *result, int64_t fn, + int64_t args, int argc, uint32_t flags); +void as3vm_do_method_call_GC(void *vm, void *result, void *name, void *this_slot, + uint32_t argc); +void as3vm_do_apply_GC(int64_t *frame, void *fn, int argc, int64_t args); +uint64_t as3vm_do_getter_GC(void *vm, uint64_t this_value, int64_t getter); +uint64_t as3vm_do_setter_GC(void *vm, uint64_t this_value, int64_t setter, + uint64_t value); + +/* ---- Closure / method trampolines (call-frame in param). ---- */ +void as3vm_functioncall_implementation(int64_t *frame); +void as3vm_functionclosure_implementation(int64_t *frame); +void as3vm_methodclosure_implementation(int64_t frame); + +#ifdef __cplusplus +} +#endif + +#endif /* IGGY_RECONSTRUCTED_AS3VM_INTERPRETER_H */ diff --git a/iggy-decompiled/src/reconstructed/as3vm_interpreter_call.inc b/iggy-decompiled/src/reconstructed/as3vm_interpreter_call.inc new file mode 100644 index 0000000..600dab4 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/as3vm_interpreter_call.inc @@ -0,0 +1,479 @@ +/* as3vm_interpreter_call.inc -- the AS3 call machinery: value/value + value/ + * method-closure invocation, apply, getter/setter thunks, method-name calls, + * the function/method/closure trampolines, method-frame preparation, and the + * profiled interpret entry. Included by as3vm_interpreter.c. */ + +/* AS3 call frame produced by prep_method_exec_GC and consumed by + * interpret_method_GC (12 machine words). */ +enum { + MF_VM = 0, /* [0] vm */ + MF_SAVED = 1, /* [1] saved locals base */ + MF_LOCALS = 2, /* [2] locals base */ + MF_THIS = 3, /* [3] this slot */ + MF_ARGS = 4, /* [4] arg base */ + MF_ARGS2 = 5, /* [5] arg base (working) */ + MF_PC = 6, /* [6] program counter (0 at entry) */ + MF_CODE = 7, /* [7] bytecode ptr */ + MF_RESULT = 8, /* [8] result slot */ + MF_CTX = 9, /* [9] method context */ + MF_METHOD = 10, /* [10] method info */ + MF_SCOPE = 11 /* [11] scope base */ +}; + +/* Method-info / method-body struct field offsets the frame-prep path reads. + * 64-bit column from orbis (001009b0 prep_method_exec_GC), 32-bit from vita. + * `minfo` fields are byte offsets from the method-info ptr; `*minfo` (MB_*) + * fields are byte offsets from its first (method-body) word. MI_*_IDX are + * machine-word indices into minfo (the struct packs differently per width, so + * the index -- not just the scaled offset -- differs). */ +#if IGGY_AS3_64BIT +#define MI_MAXSTACK 0x12 /* minfo + 0x12 = ushort max operand stack */ +#define MI_SCOPE_A 0x16 /* minfo + 0x16 = byte scope count A */ +#define MI_SCOPE_B 0x17 /* minfo + 0x17 = byte scope count B */ +#define MI_CODE_IDX 4 /* minfo[4] = bytecode ptr */ +#define MI_SCOPE_IDX 5 /* minfo[5] = scope base */ +#define MB_COERCE 0x10 /* *minfo + 0x10 = param-type coerce table */ +#define MB_PARAMCNT 0x2c /* *minfo + 0x2c = declared param count (byte) */ +#define MB_FLAGS 0x2d /* *minfo + 0x2d = method flags (byte) */ +#define MB_OPTCNT 0x2f /* *minfo + 0x2f = optional-arg count (byte) */ +#define MB_DEFAULTS 0x38 /* *minfo + 0x38 = default-arg value table */ +#define VM_METHOD_CTX_FIELD 0xd28 /* vm field copied into MF_CTX */ +#else +#define MI_MAXSTACK 0xa +#define MI_SCOPE_A 0xe +#define MI_SCOPE_B 0xf +#define MI_CODE_IDX 6 +#define MI_SCOPE_IDX 7 +#define MB_COERCE 0x8 +#define MB_PARAMCNT 0x18 +#define MB_FLAGS 0x19 +#define MB_OPTCNT 0x1b +#define MB_DEFAULTS 0x20 +#define VM_METHOD_CTX_FIELD 0x8b4 +#endif + +/* ------------------------------------------------------------------ */ +/* value/method-closure + value/value invocation. */ +/* ------------------------------------------------------------------ */ + +uint64_t as3vm_do_func_call_vmc_GC(void *vm, void *result, void *fn, + int64_t frame, void *args, uint32_t flags) { + as3v callee[6]; + as3v *argp = (as3v *)args; + *argp = (as3v)fn; + callee[0] = (as3v)vm; + callee[1] = (as3v)result; + callee[2] = (as3v)args; + callee[3] = flags; + callee[4] = (as3v)frame; + callee[5] = 0; + (*(void (**)(void *))((as3v)frame + O_INVOKE))(callee); + return 1; +} + +uint64_t as3vm_do_func_call_vv_GC(void *vm, int64_t *result, int64_t fn, + int64_t args, int argc, uint32_t flags) { + as3v f = (as3v)fn; + if (TAG(f) == 1) { + if ((TYPEB(f) & 0x10) != 0) { + as3v callee[6]; + callee[0] = (as3v)vm; + callee[1] = (as3v)result; + callee[2] = (as3v)args; + callee[3] = (as3v)argc; + callee[4] = f - 1; + callee[5] = flags; + (*(void (**)(void *))(f + V_INVOKE))(callee); + return 1; + } + if (TYPEB(f) == '&') { + as3v klass = *(as3v *)(*(as3v *)(f + STR_LEN) + O_STARTCLASS); + if (argc != 1) { + if (klass != VMF(vm, VM_CLASS_CLASS)) { + raise_error(vm, 0x72); + return 0; + } + GC_PUSH(vm, 0); + as3vm_construct_GC(vm, result, fn, (void *)(GC_TOP(vm) - WORD), 0); + GC_POP(vm); + return 1; + } + { + as3v cast_v; + as3v arg1 = *(as3v *)(args + WORD); + if (klass == VMF(vm, VM_OBJECT_CLASS) && + (arg1 == VNULL || arg1 == VUNDEF)) { + cast_v = as3vm_make_object_no_constructor_GC(vm); + } else { + cast_v = as3vm_cast_GC(vm, arg1, (int64_t)klass); + } + if (cast_v != HOLE) { + *result = cast_v; + return 1; + } + raise_error(vm, 0x78); + return 0; + } + } + } + raise_error(vm, 0x78); + return 0; +} + +/* Function.apply -- spill this + args onto the GC stack and forward. */ +void as3vm_do_apply_GC(int64_t *frame, void *fn, int argc, int64_t args) { + void *vm = (void *)frame[0]; + as3v callee = *(as3v *)frame[3]; + if (TAG(callee) == 1 && (*(uint8_t *)(callee - 1) & 0x10) != 0) { + as3v this_v = (as3v)fn; + as3v spill; + if (this_v == VNULL) { + this_v = VMF(vm, VM_APPLY_THIS); + } + GC_PUSH(vm, this_v); + spill = GC_TOP(vm); + for (int i = 0; i < argc; ++i) { + GC_PUSH(vm, *(as3v *)(args + (as3v)i * WORD)); + } + as3vm_do_func_call_vv_GC(vm, (int64_t *)frame[1], (int64_t)callee, + (int64_t)(spill - WORD), argc, 0xffffffff); + GC_TOP(vm) -= (as3v)(argc + 1) * WORD; + return; + } + raise_error(vm, 0x78); + (void)args; +} + +uint64_t as3vm_do_getter_GC(void *vm, uint64_t this_value, int64_t getter) { + as3v callee[6]; + as3v out[4]; + GC_PUSH(vm, this_value); + callee[0] = (as3v)vm; + callee[1] = (as3v)out; + callee[2] = 0; + callee[3] = GC_TOP(vm) - WORD; + callee[4] = (as3v)getter; + (*(void (**)(void *))((as3v)getter + O_INVOKE))(callee); + GC_POP(vm); + return out[0]; +} + +uint64_t as3vm_do_setter_GC(void *vm, uint64_t this_value, int64_t setter, + uint64_t value) { + if (setter != 0) { + as3v callee[6]; + as3v out[2]; + GC_PUSH(vm, this_value); + GC_PUSH(vm, value); + callee[0] = (as3v)vm; + callee[1] = (as3v)out; + callee[2] = 1; + callee[3] = GC_TOP(vm) - 2 * WORD; + callee[4] = (as3v)setter; + (*(void (**)(void *))((as3v)setter + O_INVOKE))(callee); + GC_TOP(vm) -= 2 * WORD; + } + return 1; +} + +void as3vm_do_method_call_GC(void *vm, void *result, void *name, void *this_slot, + uint32_t argc) { + as3v base = (as3v)vm; + as3v recv = *(as3v *)this_slot; + if (recv == VUNDEF || recv == VNULL) { + raise_error(vm, 0x78); + return; + } + { + as3v member = as3vm_get_property_name_GC(vm, recv, (as3v)name); + if (member != HOLE) { + if (TAG(member) == 1 && *(char *)(member - 1) == '&' && + *(as3v *)(*(as3v *)(member + STR_LEN) + WORD) == 0) { + GC_PUSH(vm, member); + lazy_init_class_GC(vm, *(as3v *)(*(as3v *)(member + STR_LEN) + + O_STARTCLASS)); + GC_POP(vm); + member = *(as3v *)GC_TOP(vm); + if (*(as3v *)(base + VM_PENDING_EXC) != 0) { + return; + } + } + as3vm_do_func_call_vv_GC(vm, (int64_t *)result, (int64_t)member, + (int64_t)this_slot, (int)argc, 0); + return; + } + if (*(as3v *)(base + VM_PENDING_EXC) == 0) { + raise_error(vm, 0x78); + } + } +} + +/* ------------------------------------------------------------------ */ +/* Method-frame preparation + the function/closure trampolines. */ +/* ------------------------------------------------------------------ */ + +static void prep_method_exec_GC(void *vm, void *frame, as3v result, as3v receiver, + as3v method, void *args, unsigned argc, + void *call_frame) { + as3v base = (as3v)vm; + as3v *fr = (as3v *)frame; + as3v locals_top = *(as3v *)(base + VM_FRAME_CUR); + as3v *minfo = (as3v *)method; + as3v *argv = (as3v *)((as3v)args + WORD); + as3v *locals; + as3v *scope; + unsigned i; + unsigned fixed; + + if (*(as3v *)(base + VM_PENDING_EXC) != 0) { + return; + } + fr[MF_VM] = base; + fr[MF_SAVED] = locals_top; + GC_PUSH(vm, (as3v)call_frame + 1); + locals_top -= (as3v)(*(uint16_t *)((as3v)minfo + 2 * WORD)) * WORD; + fr[MF_LOCALS] = locals_top; + for (i = 0; i < *(uint16_t *)((as3v)minfo + 2 * WORD); ++i) { + ((as3v *)fr[MF_LOCALS])[i] = 0; + } + locals = (as3v *)(locals_top - + (as3v)(*(uint16_t *)((as3v)minfo + MI_MAXSTACK)) * WORD); + fr[MF_THIS] = (as3v)locals; + fixed = (unsigned)(*(uint8_t *)((as3v)minfo + MI_SCOPE_B)) - + (unsigned)(*(uint8_t *)((as3v)minfo + MI_SCOPE_A)); + fr[MF_PC] = 0; + fr[MF_RESULT] = result; + scope = locals - (int)fixed; + fr[MF_ARGS] = (as3v)scope; + fr[MF_ARGS2] = (as3v)scope; + + if ((as3v)(scope - 1) < *(as3v *)(base + VM_FRAME_FLOOR) || + *(as3v *)(base + VM_GC_LIMIT) - 0x80 <= GC_TOP(vm)) { + raise_error(vm, 0x77); + return; + } + *(as3v *)(base + VM_FRAME_CUR) = (as3v)(scope - 1); + if (TAG(receiver) == 1 && 0x1f < TYPEB(receiver)) { + as3v holder = *(as3v *)(receiver + STR_LEN); + if (*(as3v *)(holder + WORD) == 0) { + *(as3v *)(holder + WORD) = + *(as3v *)(*(as3v *)(*(as3v *)(holder + O_STARTCLASS) + CLS_TRAITS) + + WORD); + } + scope[-1] = *(as3v *)(*(as3v *)(receiver + STR_LEN) + WORD); + } else { + scope[-1] = *(as3v *)(base + VM_DEFAULT_OBJ); + } + locals[0] = receiver; + + { + unsigned declared = *(uint8_t *)(*minfo + MB_PARAMCNT); + unsigned n; + if (argc < declared) { + for (i = 0; i < argc; ++i) { + locals[1 + i] = argv[i]; + } + n = argc; + { + unsigned optional = declared - (unsigned)*(uint8_t *)(*minfo + MB_OPTCNT); + for (; n < optional; ++n) { + locals[1 + n] = VUNDEF; + } + for (; n < declared; ++n) { + locals[1 + n] = *(as3v *)(*(as3v *)(*minfo + MB_DEFAULTS) + + (as3v)(n - optional) * WORD); + } + } + } else { + for (i = 0; i < declared; ++i) { + locals[1 + i] = argv[i]; + } + n = declared; + } + for (i = n + 1; i < *(uint16_t *)((as3v)minfo + MI_MAXSTACK); ++i) { + locals[i] = VUNDEF; + } + for (i = 0; i < fixed; ++i) { + ((as3v *)fr[MF_ARGS])[i] = VUNDEF; + } + { + unsigned coerce_n = declared; + if (argc < declared) coerce_n = argc; + for (i = 0; i < coerce_n; ++i) { + if (*(as3v *)(*(as3v *)(*minfo + MB_COERCE) + (as3v)i * WORD) != 0) { + as3v c = as3vm_coerce_GC(vm, locals[1 + i], 0); + if (c == HOLE) { + raise_error(vm, 0x78); + locals[1 + i] = VUNDEF; + *(as3v *)(base + VM_FRAME_CUR) = fr[MF_SAVED]; + return; + } + locals[1 + i] = c; + } + } + } + if ((*(uint8_t *)(*minfo + MB_FLAGS) & 4) != 0) { + int rest = (int)argc - (int)*(uint8_t *)(*minfo + MB_PARAMCNT); + as3v arr; + if (rest < 0) rest = 0; + arr = as3vm_make_array_GC(vm, rest, rest); + locals[1 + *(uint8_t *)(*minfo + MB_PARAMCNT)] = arr; + for (i = 0; i < (unsigned)rest; ++i) { + *(as3v *)(*(as3v *)(arr + O_ARRDATA) + O_CLASSWORD + (as3v)i * WORD) = + argv[*(uint8_t *)(*minfo + MB_PARAMCNT) + i]; + } + } + if ((*(uint8_t *)(*minfo + MB_FLAGS) & 1) != 0) { + int n2 = (int)argc; + as3v arr; + as3v cl; + if (n2 < 0) n2 = 0; + arr = as3vm_make_array_GC(vm, n2, n2); + locals[1 + *(uint8_t *)(*minfo + MB_PARAMCNT)] = arr; + for (i = 0; i < (unsigned)n2; ++i) { + *(as3v *)(*(as3v *)(arr + O_ARRDATA) + O_CLASSWORD + (as3v)i * WORD) = argv[i]; + } + cl = *(as3v *)(GC_TOP(vm) - WORD); + if (TAG(cl) == 1 && *(char *)(cl - 1) == '\x10') { + cl = as3vm_make_method_closure_GC(vm, locals[0], cl); + } + as3vm_set_property_name_GC(vm, locals[1 + *(uint8_t *)(*minfo + MB_PARAMCNT)], + VMF(vm, VM_ARGUMENTS_QN), cl, 1); + } + GC_POP(vm); + fr[MF_METHOD] = (as3v)minfo; + fr[MF_CODE] = minfo[MI_CODE_IDX]; + fr[MF_CTX] = *(as3v *)(base + VM_METHOD_CTX_FIELD); + fr[MF_SCOPE] = minfo[MI_SCOPE_IDX]; + } +} + +void as3vm_functioncall_implementation(int64_t *frame) { + as3v base = (as3v)frame[0]; + as3v recv = *(as3v *)frame[3]; + as3v mframe[12]; + if (TAG(recv) != 1 || TYPEB(recv) < 0x20) { + recv = *(as3v *)(base + VM_APPLY_THIS); + } + prep_method_exec_GC((void *)base, mframe, frame[1], recv, + *(as3v *)(frame[4] + WORD), (void *)frame[3], + (unsigned)frame[2], (void *)frame[4]); + if (*(as3v *)(base + VM_PENDING_EXC) == 0) { + if (as3vm_profiling != 0) { + interpret_method_profiled_GC(mframe); + return; + } + interpret_method_GC(mframe); + } +} + +void as3vm_functionclosure_implementation(int64_t *frame) { + as3v base = (as3v)frame[0]; + as3v closure = (as3v)frame[4]; + as3v method = *(as3v *)(closure + 0x28); + as3v mframe[12]; + + if (*(void **)(method + V_INVOKE) == (void *)as3vm_functioncall_implementation) { + as3v recv; + if ((int)frame[5] == 1) { + recv = *(as3v *)(closure + 0x30); + if (recv == 0) { + recv = *(as3v *)(base + VM_APPLY_THIS); + } + } else { + recv = *(as3v *)frame[3]; + } + GC_PUSH((void *)base, *(as3v *)(closure + 0x38)); + prep_method_exec_GC((void *)base, mframe, frame[1], recv, + *(as3v *)(method + STR_LEN), (void *)frame[3], + (unsigned)frame[2], (void *)frame[4]); + GC_POP((void *)base); + if (*(as3v *)(base + VM_PENDING_EXC) == 0) { + if (as3vm_profiling != 0) { + interpret_method_profiled_GC(mframe); + return; + } + interpret_method_GC(mframe); + } + } else if (*(void **)(method + V_INVOKE) != + (void *)as3vm_methodclosure_implementation) { + frame[4] = (int64_t)(method - 1); + (*(void (**)(int64_t *))(method + V_INVOKE))(frame); + } +} + +void as3vm_methodclosure_implementation(int64_t frame) { + as3v fr = (as3v)frame; + as3v closure = *(as3v *)(fr + 0x20); + as3v method = *(as3v *)(closure + WORD); + *(as3v *)(*(as3v *)(fr + 0x18)) = *(as3v *)(closure + 0x18); + *(as3v *)(fr + 0x20) = method; + (*(void (**)(void))(method + O_INVOKE))(); +} + +/* Method-value call variant used by lazy class initialisation. */ +static void do_func_call_mv_GC(void *vm, as3v result, as3v *method, as3v this_v, + void *args, unsigned flags) { + as3v base = (as3v)vm; + as3v mframe[12]; + if (TAG(this_v) != 1 || TYPEB(this_v) < 0x20) { + this_v = *(as3v *)(base + VM_APPLY_THIS); + } + prep_method_exec_GC(vm, mframe, result, this_v, method[1], args, flags, + (void *)method[0]); + if (*(as3v *)(base + VM_PENDING_EXC) == 0) { + if (as3vm_profiling != 0) { + interpret_method_profiled_GC(mframe); + return; + } + interpret_method_GC(mframe); + } +} + +/* ------------------------------------------------------------------ */ +/* Profiled interpret entry (records per-method tick spans). */ +/* ------------------------------------------------------------------ */ + +/* Profiling ring-buffer inside the vm. This is a distinct structure from + * VM_METHOD_CTX (the per-call method-context field, vm+0xd28/0x694): the + * profiler keeps a depth counter followed by an array of {method-key, tick} + * slots (0x10-byte stride on both widths). The base and internal layout + * differ per width -- 64-bit from orbis as3vm_functioncall_implementation + * (0x1008a0: depth vm+0x295c, mkey vm+0x2960+i*0x10, tick vm+0x2968+i*0x10); + * 32-bit from vita (0x00010...: depth vm+0x1718, mkey vm+0x1720+i*0x10, tick + * vm+0x1728+i*0x10). The prior lift reused VM_METHOD_CTX+0x14 as the base, + * which is wrong on both. */ +#if IGGY_AS3_64BIT +#define PROF_DEPTH 0x295c /* int depth counter */ +#define PROF_SLOT_KEY 0x2960 /* slot i: method key @ base + i*0x10 */ +#define PROF_SLOT_TICK 0x2968 /* slot i: tick value @ base + i*0x10 */ +#else +#define PROF_DEPTH 0x1718 +#define PROF_SLOT_KEY 0x1720 +#define PROF_SLOT_TICK 0x1728 +#endif + +static void interpret_method_profiled_GC(as3v *frame) { + as3v base = frame[MF_VM]; + int depth = *(int *)(base + PROF_DEPTH); + if (depth < 0x100) { + as3v mkey = *(as3v *)frame[MF_METHOD]; + int next = depth + 1; + uint64_t start = rrGetTicks(); + *(int *)(base + PROF_DEPTH) = next; + if (next < 0x100) { + *(as3v *)(base + PROF_SLOT_KEY + (as3v)next * 0x10) = mkey; + *(uint64_t *)(base + PROF_SLOT_TICK + (as3v)next * 0x10) = start; + } + interpret_method_GC(frame); + { + uint64_t end = rrGetTicks(); + if (depth >= 0) { + *(uint64_t *)(base + PROF_SLOT_TICK + (as3v)depth * 0x10) = end; + } + } + *(int *)(base + PROF_DEPTH) = depth; + return; + } + interpret_method_GC(frame); +} diff --git a/iggy-decompiled/src/reconstructed/as3vm_interpreter_construct.inc b/iggy-decompiled/src/reconstructed/as3vm_interpreter_construct.inc new file mode 100644 index 0000000..74e8a0a --- /dev/null +++ b/iggy-decompiled/src/reconstructed/as3vm_interpreter_construct.inc @@ -0,0 +1,499 @@ +/* as3vm_interpreter_construct.inc -- the AS3 `new` object-construction sequence + * (construct_pre / construct / construct_post): allocate the instance, run the + * intrinsic-class fast paths (Boolean/Number/String/int/uint/Namespace/Array/ + * Vector), spin up the flash-display factory objects, and invoke the AS3 + * constructor. Reconstructed from the vita build (which fully inlines the + * class-kind switch) reconciled with the durango compact form. Included by + * as3vm_interpreter.c. */ + +/* Class-registry + scratch VM fields the construct path touches, plus the + * class/object struct offsets the intrinsic + user-class paths dereference. + * 64-bit column recovered from the orbis raw (which fully expands the + * construct switch; identical LP64 layout to the tested durango build); + * 32-bit column from the vita raw (word index * 4). The display-class + * registry indices are the flash-factory class slots the '$' sprite walk + * compares against (orbis param_1[0x4b4..0x4dd], vita param_1[0x544..0x56d]). */ +#if IGGY_AS3_64BIT +#define VMC_CLASS_SCRATCH 0x28d0 /* orbis param_1[0x51a] -- factory scratch */ +#define VMC_CLASS_ACTIVE 0x28d8 /* orbis param_1[0x51b] -- active-class save slot */ +#define VMC_PROTO_HINT 0x28e0 /* orbis param_1[0x51c] */ +#define VMC_ARRAY_CLASS 0x2648 +#define VMC_ARRAY_PROTO 0x2650 +#define VMC_VECTOR_MARK 0x2658 +#define VMC_MOVIE_ROOT 0x2610 /* orbis param_1[0x4c2] -- class-walk terminator */ +#define VMC_BUTTON_CLASS 0x25a8 /* orbis param_1[0x4b5] */ +#define VMC_SHAPE_CLASS 0x25c8 /* orbis param_1[0x4b9] */ +#define VMC_MC_CLASS 0x26c8 /* orbis param_1[0x4d9] -- no-factory class */ +#define VMC_BITMAP_CLASS 0x26e8 /* orbis param_1[0x4dd] */ +#define VMC_TEXT_CLASS 0x25a0 /* orbis param_1[0x4b4] */ +#define VMC_SPRITE_CLASS 0x2658 /* orbis param_1[0x4cb] -- no-factory class */ + +/* Class-struct field offsets (base = the start class). */ +#define CLS_FLAG 0x10 /* plVar2[2] -- traits/flag word */ +#define CLS_MOVIE_SRC 0x20 /* plVar2[4] -- movie-complete source obj */ +#define CLS_NATIVE_CTOR 0x38 /* plVar2[7] -- native constructor fn ptr */ +#define CLS_TRAITS 0x48 /* plVar2[9] -- traits pointer */ +#define CLS_ABSTRACT 0x1a /* class + 0x1a = abstract flag byte */ + +/* Object-struct field offsets (base = the instance value). */ +#define O_LHASH 0xf /* obj + 0xf = lookup-hash int (zeroed) */ +#define O_CLASSWORD 0x1f /* obj + 0x1f = first traits word / arr payload */ +#define O_DISPLAY_OBJ 0x27 /* obj + 0x27 = '$' display-object slot */ +#define O_ARRDATA 0x2f /* obj + 0x2f = arraydata pointer */ +#else +#define VMC_CLASS_SCRATCH 0x16cc /* vita param_1[0x5b3] / local_60[0x33] */ +#define VMC_CLASS_ACTIVE 0x16d0 /* vita local_60[0x34] */ +#define VMC_PROTO_HINT 0x16d4 /* vita local_60[0x35] */ +#define VMC_ARRAY_CLASS 0x1698 +#define VMC_ARRAY_PROTO 0x16c8 +#define VMC_VECTOR_MARK 0x16cc +#define VMC_MOVIE_ROOT 0x1548 /* vita param_1[0x552] */ +#define VMC_BUTTON_CLASS 0x1514 /* vita param_1[0x545] */ +#define VMC_SHAPE_CLASS 0x1524 /* vita param_1[0x549] */ +#define VMC_MC_CLASS 0x15a4 /* vita param_1[0x569] -- no-factory class */ +#define VMC_BITMAP_CLASS 0x15b4 /* vita param_1[0x56d] */ +#define VMC_TEXT_CLASS 0x1510 /* vita param_1[0x544] */ +#define VMC_SPRITE_CLASS 0x156c /* vita param_1[0x55b] -- no-factory class */ + +/* Class-struct field offsets (base = the start class). */ +#define CLS_FLAG 0x8 /* piVar5[2] */ +#define CLS_MOVIE_SRC 0x14 /* piVar5[5] */ +#define CLS_NATIVE_CTOR 0x20 /* piVar5[8] */ +#define CLS_TRAITS 0x28 /* piVar5[10] */ +#define CLS_ABSTRACT 0xe /* class + 0xe = abstract flag byte */ + +/* Object-struct field offsets (base = the instance value). */ +#define O_LHASH 0xb +#define O_CLASSWORD 0x13 +#define O_DISPLAY_OBJ 0x17 +#define O_ARRDATA 0x1b +#endif + +/* The display-object's pooled-child slot + the movie-complete result field + * share the class movie-source offset (orbis 0x20 / vita 0x14). */ +#define O_MOVIE_POOL CLS_MOVIE_SRC + +/* construct_pre: allocate + shallow-init an instance of a concrete class value + * (used by the interpreter's newobject/newclass paths before the ctor runs). */ +void *as3vm_construct_pre_GC(void *vm, int64_t klass_value) { + as3v kv = (as3v)klass_value; + as3v klass; + as3v obj; + + if (TAG(kv) != 1 || TYPEB(kv) != '&') { + return (void *)VNULL; + } + klass = *(as3v *)(*(as3v *)(kv + STR_LEN) + O_STARTCLASS); + if (*(as3v *)(*(as3v *)(klass + CLS_TRAITS) + WORD) == 0) { + lazy_init_class_GC(vm, klass); + if (*(as3v *)(*(as3v *)(klass + CLS_TRAITS) + WORD) == 0) { + return (void *)VNULL; + } + } + if ((uint8_t)(*(char *)(klass + TYPE_TAG_OFF) - 0x23) > 1) { + return (void *)VNULL; + } + VMF(vm, VM_CLEAR_B) = klass; + obj = as3vm_make_object_with_traits_GC(vm, *(void **)(klass + CLS_TRAITS), + *(as3v *)(klass + CLS_FLAG) != 0); + *(uint8_t *)(obj - 1) = *(uint8_t *)(klass + TYPE_TAG_OFF); + *(as3v *)(obj + STR_LEN) = *(as3v *)(klass + CLS_TRAITS); + *(int *)(obj + O_LHASH) = 0; + *(as3v *)(obj + O_CLASSWORD) = **(as3v **)(klass + CLS_TRAITS); + if (*(char *)(klass + TYPE_TAG_OFF) == '$') { + *(as3v *)(obj + O_ARRDATA) = 0; + } + return (void *)obj; +} + +/* construct: the full class-kind dispatch (intrinsic fast paths + flash-display + * factories + user constructor). */ +void as3vm_construct_GC(void *vm, void *result, int64_t klass_value, + void *this_slot, uint32_t argc) { + as3v kv = (as3v)klass_value; + as3v *res = (as3v *)result; + as3v *thisp = (as3v *)this_slot; + as3v klass; + int nargs = (int)argc; + + if (TAG(kv) != 1) { + *res = HOLE; + return; + } + if ((TYPEB(kv) & 0x10) != 0) { + /* Callable class value: resolve its qname constructor, make the receiver, + * then call it. */ + as3v ctor = as3vm_get_property_qname_GC(vm, kv, VMF(vm, VM_CTOR_QN)); + as3v recv; + as3v thisv = VUNDEF; + if (TAG(ctor) == 1 && 0x1f < TYPEB(ctor)) { + recv = as3vm_make_object_with_proto_GC(vm, ctor); + } else { + recv = as3vm_make_object_GC(vm, 0x23); + } + *res = recv; + GC_PUSH(vm, recv); + *thisp = recv; + as3vm_do_func_call_vv_GC(vm, (int64_t *)&thisv, (int64_t)kv, + (int64_t)this_slot, nargs, 1); + GC_POP(vm); + if (TAG(thisv) == 1 && 0x1f < TYPEB(thisv)) { + *res = thisv; + } else { + *res = *(as3v *)GC_TOP(vm); + } + return; + } + if (TYPEB(kv) != 0x26) { + *res = HOLE; + return; + } + klass = *(as3v *)(*(as3v *)(kv + STR_LEN) + O_STARTCLASS); + /* Lazy class init: durango calls lazy_init_class_GC directly (the orbis/vita + * expansions inline the same class+0x68 init-descriptor sequence the helper + * lifts); the guard reads the traits-init word (*(traits + WORD)). */ + if (*(as3v *)(*(as3v *)(klass + CLS_TRAITS) + WORD) == 0) { + lazy_init_class_GC(vm, klass); + if (*(as3v *)(*(as3v *)(klass + CLS_TRAITS) + WORD) == 0) { + *res = HOLE; + return; + } + } + if ((*(uint8_t *)(klass + CLS_ABSTRACT) & 1) != 0) { + *res = VNULL; + raise_error(vm, 0x72); + return; + } + + switch (*(uint8_t *)(klass + TYPE_TAG_OFF)) { + case 4: + *res = VUNDEF; + break; + case 5: + *res = VNULL; + break; + case 6: + if (nargs < 1) { + *res = VFALSE; + } else { + *res = as3vm_to_boolean(thisp[1]); + } + break; + case 7: + if (nargs == 0) { + *res = 0; + } else if (nargs == 1) { + *res = as3vm_ToNumber_GC(vm, thisp[1]); + } else { + *res = HOLE; + } + break; + case 8: + if (nargs == 0) { + *res = (as3v)as3vm_find_intern_string(vm, 0, 0, 1) + 1; + } else if (nargs == 1) { + *res = as3vm_ToString_GC(vm, thisp[1]); + } else { + *res = HOLE; + } + break; + case 9: + if (nargs == 0) { + *res = 0; + } else if (nargs == 1) { + int32_t i32; + if (as3vm_ToInt32_GC(vm, thisp[1], &i32) == 0) { + *res = HOLE; + } else { + *res = as3vm_make_int_GC(vm, i32); + } + } else { + *res = HOLE; + } + break; + case 10: + if (nargs == 0) { + *res = 0; + } else if (nargs == 1) { + uint32_t u32; + if (as3vm_ToUInt32_GC(vm, thisp[1], &u32) == 0) { + *res = HOLE; + } else { + *res = as3vm_make_uint_GC(vm, u32); + } + } else { + *res = HOLE; + } + break; + case 0x0c: + *res = as3vm_make_namespace(vm); + break; + case 0x12: + case 0x14: + case 0x26: + raise_error(vm, 0x78); + *res = HOLE; + break; + case 0x20: { + /* Array / Vector. construction. */ + as3v arr; + int single_num = (nargs == 1) && (thisp[1] & 1) == 0; + if (single_num || nargs != 1) { + int len = (int)((int64_t)thisp[1] >> 1); + if (len < 0 || (nargs > 0 && (thisp[1] & 1) != 0)) { + raise_error(vm, 0x75); + break; + } + if (nargs == 0) len = 0; + arr = as3vm_make_array_GC(vm, len, len); + if (nargs > 1 && as3vm_to_boolean_direct(thisp[2]) != 0) { + *(uint8_t *)(arr + 1) |= 2; + } + (void)as3vm_make_array_with_traits_GC(vm, (void *)klass, 0); + *res = arr; + } else { + arr = as3vm_make_array_GC(vm, nargs, nargs); + { + as3v ad = *(as3v *)(arr + 0x1b); + for (int i = 0; i < nargs; ++i) { + as3v e = thisp[1 + i]; + if (TAG(e) == 1 && e != HOLE && + 0x17u < (*(uint8_t *)ad & ~*(uint8_t *)e)) { + as3vm_write_barrier_slowpath(vm, (void *)(ad - 1), + (void *)(e - 1)); + } + *(as3v *)(ad - 1 + 0x14 + (as3v)i * WORD) = e; + } + } + (void)as3vm_coerce_GC(vm, (int64_t)VNULL, (int64_t)klass); + *res = arr; + } + break; + } + case 0x21: + case 0x23: + case 0x24: + case 0x27: + case 0x28: + case 0x29: { + /* Generic user-class instance (+ flash-display factory objects). + * Re-lifted from orbis (00107810, the fully-expanded case at ~1440-1624) + * reconciled with the vita build; all struct offsets width-gated. */ + as3v src = *(as3v *)(klass + CLS_MOVIE_SRC); + as3v obj; + as3v saved_active; + as3v factory = 0; + uint8_t tb; + + /* Fast path: a small non-Object movie source with no active display + * realises a complete movie directly. */ + if (src != 0 && VMF(vm, VMC_CLASS_SCRATCH) == 0 && + *(uint8_t *)src < 8 && *(uint8_t *)src != 4) { + as3v mc = iggy_create_movie_complete_GC(*(void **)vm, (void *)src, + this_slot, (uint32_t)nargs); + *res = **(as3v **)(mc + O_MOVIE_POOL); + return; + } + + saved_active = VMF(vm, VMC_CLASS_ACTIVE); + VMF(vm, VMC_CLASS_ACTIVE) = klass; + obj = as3vm_make_object_with_traits_GC(vm, *(void **)(klass + CLS_TRAITS), + *(as3v *)(klass + CLS_FLAG) != 0); + tb = *(uint8_t *)(klass + TYPE_TAG_OFF); + if (tb == 0x21 || tb == 0x24) { + *(uint8_t *)(obj - 1) = tb; + if (tb == 0x24) { + *(as3v *)(obj + O_ARRDATA) = 0; + } + } + *(as3v *)(obj + STR_LEN) = *(as3v *)(klass + CLS_TRAITS); + *(as3v *)(obj + O_CLASSWORD) = **(as3v **)(klass + CLS_TRAITS); + *(int *)(obj + O_LHASH) = 0; + *res = obj; + + /* Vector. (type 0x21) constructed with a single `undefined` argument + * clones the traits template arraydata into the instance. */ + if (*(char *)(klass + TYPE_TAG_OFF) == '!' && nargs == 1 && + thisp[1] == VUNDEF) { + as3v tmpl = **(as3v **)(klass + CLS_TRAITS); + int count = *(int *)(tmpl + 3); + as3v ad; + as3v tsrc; + GC_PUSH(vm, obj); + GC_PUSH(vm, tmpl); + ad = (as3v)as3vm_make_arraydata_GC(vm, count, *(int *)(tmpl + 7)); + GC_POP(vm); + tsrc = *(as3v *)GC_TOP(vm); + if ((*(uint8_t *)ad & 0x20) == 0) { + for (int i = 0; i < count; ++i) { + *(as3v *)(ad + O_CLASSWORD + (as3v)i * WORD) = + *(as3v *)(tsrc + O_CLASSWORD + (as3v)i * WORD); + } + } else { + for (int i = 0; i < count; ++i) { + as3v e = *(as3v *)(tsrc + O_CLASSWORD + (as3v)i * WORD); + if (TAG(e) == 1 && e != HOLE && + 0x17u < (*(uint8_t *)ad & ~*(uint8_t *)e)) { + as3vm_write_barrier_slowpath(vm, (void *)(ad - 1), + (void *)(e - 1)); + e = *(as3v *)(tsrc + O_CLASSWORD + (as3v)i * WORD); + } + *(as3v *)(ad + O_CLASSWORD + (as3v)i * WORD) = e; + } + } + GC_POP(vm); + obj = *(as3v *)GC_TOP(vm); + if (TAG(ad) == 1 && ad != HOLE && + 0x17u < (*(uint8_t *)obj & ~*(uint8_t *)ad)) { + as3vm_write_barrier_slowpath(vm, (void *)(obj - 1), (void *)(ad - 1)); + } + *(as3v *)(obj + O_CLASSWORD) = ad; + *(as3v *)(ad + O_LHASH) = 1; /* word write (orbis zeroes 0xf..0x16) */ + *res = obj; + } + + *thisp = obj; + + /* Display-object factory selection. */ + src = *(as3v *)(klass + CLS_MOVIE_SRC); + if (src != 0 && *(uint8_t *)src != 4) { + uint8_t st = *(uint8_t *)src; + if (st == 0x14 || (uint8_t)(st - 0x15) < 2) { + VMF(vm, VMC_PROTO_HINT) = src; + } + } else if (*(char *)(klass + TYPE_TAG_OFF) == '$' && + VMF(vm, VMC_CLASS_SCRATCH) == 0) { + as3v c = klass; + int made = 0; + if (c != VMF(vm, VMC_MOVIE_ROOT)) { + do { + if (c == VMF(vm, VMC_BUTTON_CLASS)) { + factory = (as3v)iggy_make_empty_button(*(void **)vm); + made = 1; + break; + } + if (c == VMF(vm, VMC_SHAPE_CLASS)) { + factory = (as3v)iggy_make_empty_shape(*(void **)vm); + made = 1; + break; + } + if (c == VMF(vm, VMC_MC_CLASS)) { + made = 1; /* no factory */ + break; + } + if (c == VMF(vm, VMC_BITMAP_CLASS)) { + factory = (as3v)iggy_create_empty_bitmap(*(void **)vm); + made = 1; + break; + } + if (c == VMF(vm, VMC_TEXT_CLASS)) { + factory = (as3v)iggy_make_empty_textfield(*(void **)vm); + made = 1; + break; + } + if (c == VMF(vm, VMC_SPRITE_CLASS)) { + made = 1; /* no factory */ + break; + } + c = *(as3v *)(c + CLS_SUPER); + } while (c != VMF(vm, VMC_MOVIE_ROOT)); + } + if (made == 0) { + factory = (as3v)iggy_make_empty_sprite(*(void **)vm); + } + VMF(vm, VMC_CLASS_SCRATCH) = factory; + if (factory != 0) { + *(uint8_t *)(factory + 1) |= 1; + } + } + + /* '$' display objects record the active display child. */ + if (*(char *)(klass + TYPE_TAG_OFF) == '$') { + *(as3v *)(obj + O_DISPLAY_OBJ) = VMF(vm, VMC_CLASS_SCRATCH); + } + + /* Run the constructor: an AS3 ctor object via its invoke slot, or a + * native constructor fn pointer when the class has no ctor object. */ + { + as3v ctor_obj = *(as3v *)klass; + as3v scratch[2]; + as3v cf[6]; + cf[0] = (as3v)vm; + cf[1] = (as3v)scratch; + cf[2] = (as3v)nargs; + cf[3] = (as3v)this_slot; + if (ctor_obj == 0) { + as3v nctor = *(as3v *)(klass + CLS_NATIVE_CTOR); + if (nctor != 0) { + cf[4] = 0; + (*(void (**)(void *))nctor)(cf); + } + } else { + cf[4] = ctor_obj; + (*(void (**)(void *))(ctor_obj + O_INVOKE))(cf); + } + } + + if (factory != 0) { + as3v pooled; + *(uint8_t *)(factory + 1) &= 0xfe; + pooled = **(as3v **)(factory + O_MOVIE_POOL); + as3vm_pool_free(vm); + pooled = as3vm_pool_alloc(vm, pooled, 1); + *(as3v *)(factory + O_MOVIE_POOL) = pooled; + iggy_mark_movie_dirty(*(void **)vm, (intptr_t)factory); + } + VMF(vm, VMC_CLASS_ACTIVE) = saved_active; + break; + } + default: + *res = HOLE; + break; + } +} + +/* construct_post: after a native ctor returns, box/finalise the instance for a + * concrete class. */ +void as3vm_construct_post_GC(void *frame, void *result, int64_t klass_value, + void *this_slot, uint32_t argc) { + as3v *fr = (as3v *)frame; + as3v *res = (as3v *)result; + as3v kv = (as3v)klass_value; + as3v klass; + as3v recv; + + if (TAG(kv) != 1 || TYPEB(kv) != '&' || + (uint8_t)(*(char *)((klass = *(as3v *)(*(as3v *)(kv + STR_LEN) + + O_STARTCLASS)) + + TYPE_TAG_OFF) - + 0x23) > 1) { + *res = HOLE; + return; + } + recv = *res; + *(as3v *)fr[VM_GC_TOP / WORD] = recv; + fr[VM_GC_TOP / WORD] += WORD; + { + as3v cf[8]; + cf[0] = (as3v)frame; + if (*(as3v *)klass == 0) { + void (*ctor)(void *) = *(void (**)(void *))(klass + 7 * WORD); + if (ctor != 0) { + cf[1] = recv; + ctor(cf); + } + } else { + if (this_slot != 0) { + *(as3v *)this_slot = recv; + } + cf[1] = (as3v)argc; + (*(void (**)(void *))(*(as3v *)klass + O_INVOKE))(cf); + if (fr[VM_PENDING_EXC / WORD] != 0) { + iggy_uncaught_exception_warning((void *)fr[0]); + fr[VM_GC_TOP / WORD] -= WORD; + return; + } + } + } + fr[VM_GC_TOP / WORD] -= WORD; +} diff --git a/iggy-decompiled/src/reconstructed/as3vm_interpreter_dispatch.inc b/iggy-decompiled/src/reconstructed/as3vm_interpreter_dispatch.inc new file mode 100644 index 0000000..6c251b4 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/as3vm_interpreter_dispatch.inc @@ -0,0 +1,2373 @@ +/* as3vm_interpreter_dispatch.inc -- the file-static AVM2 bytecode dispatch loop + * interpret_method_GC, fully expanded from the vita raw (32-bit; authoritative + * for opcode semantics -- raw/vita/as3vm_interpreter.c:3449-6568) and cross + * checked against the durango compact dispatcher (raw/durango:1732-1758) for + * 64-bit field-offset sanity. Included last by as3vm_interpreter.c so every + * shared helper (globals/equality/support/construct/call fragments) is already + * visible. + * + * Frame-relative reading notes recovered while reconstructing this loop + * (frame layout per call.inc's MF_* enum): + * - frame[MF_LOCALS] - WORD is the initial operand-stack pointer (points at + * the current top-of-stack slot; empty stack sits one slot below the + * locals floor). + * - frame[MF_THIS] is the base of the register file (register 0 = this). + * - frame[MF_SCOPE] is the per-method constant-pool base (every pool-index + * operand in the raw is `local_6c[0xb] + idx*4`); despite the enum + * comment this is NOT the scope chain. + * - frame[MF_ARGS] / frame[MF_ARGS2] are the scope-stack base / current-top + * pointers (pushwith/popscope grow and shrink frame[MF_ARGS2]). + * - frame[MF_PC] doubles as the outermost active `with`-scope saved base + * (0 == none); it is not an instruction pointer. + * - frame[MF_METHOD] is the raw method-info pointer; interpret_method_GC's + * own exception-table walk reads fields directly off it (handler count, + * handler array, from/to/target/type). These, and every object/array/ + * dict layout offset below, were originally transcribed as bare vita + * (32-bit) literals because interpret_method_GC could not be expanded on + * any 64-bit target: BOTH raw/durango AND raw/orbis lower this loop as an + * un-recovered jump table (Ghidra: "Could not recover jumptable at + * 0x001010e2 ... Too many branches" in raw/orbis:774). The 64-bit column + * is therefore recovered from orbis SIBLING translation units that touch + * the same structures (as3vm_property.c, as3vm_create.c, as3vm_abc_decode.c, + * as3vm_interpreter.c's own recovered slot/apply/prep helpers) and is now + * width-gated: see the macro block below plus O_CLASSWORD / O_ARRDATA / + * O_INVOKE / CLS_PARAM_TYPE / INTERN_ADJ (as3vm_interpreter_construct.inc) + * and the exception-handler (EXC_) and dict (DENT_, ENUM_) macros. + * + * The final group that no sibling exposed was recovered DIRECTLY from the + * retail object machine code -- the jump table and its opcode case bodies + * ARE present in the compiled binary even though Ghidra rebuilt no switch. + * Read from work/iggy-native-decomp/durango-objects/as3vm_interpreter.obj + * (primary, tested platform) and cross-checked in the orbis object; the + * recovered offsets are width-gated in the MBI_/SLOT_COUNT/SIG_RETTYPE/ + * ACT_TRAITS/CI_/NEWOBJ_/TYPEOF_STR macro block below, each citing the + * durango instruction it came from: + * * get_method_by_index: holder+count 0xc->0x14, holder+table 0x24->0x38 + * * getslot/setslot family: class+slot-count 0x14->0x1c + * * returnvalue/returnvoid: method-signature+rettype 0xc->0x18 + * * newactivation: minfo+activation-traits 0x28->0x40 (and the stored + * "proto" is that traits value itself, not *(traits+0x28) -- a + * reconstruction misread corrected here per vita raw + both objects) + * * newclass/applytype: classinfo class-value 0x1c->0x30, sub-holders + * 0x28->0x48 / 0x2c->0x50, cinit + inner slots +4->WORD + * * newobject: proto/object-class vm field 0x167c->0x2830 (== sibling + * VM_OBJECT_CLASS, NOT VM_FUNC_CLASS 0x2860), +proto ptr 0x28->0x48 + * * constructsuper: native-ctor slot is superclass+CLS_NATIVE_CTOR + * (0x20->0x38), NOT 8*WORD -- the "index 8" was wrong on 64-bit (0x40) + * * typeof: string cache 0x928..0x940 -> 0xdd8..0xe08 (stride WORD); the + * supposed "VM_NUMBER_CLASS substitution" for int/case-4/case-7 was a + * reconstruction misread -- those arms read cache slots 3/1/3, proven + * by vita raw (vm+0x934/+0x92c) and both objects (vm+0xdf0/+0xde0) + */ + +/* ------------------------------------------------------------------ */ +/* Best-effort local constants with cross-file 64-bit provenance. */ +/* ------------------------------------------------------------------ */ + +#if IGGY_AS3_64BIT +#define CLS_VECREF 0x80 /* cached Vector. class; as3vm_class.c:175 */ +#define VM_ROOT_QNAMES 0xd98 /* qname root table; as3vm_class.c/names.c/property.c */ +#else +#define CLS_VECREF 0x44 /* as3vm_class.c:269 */ +#define VM_ROOT_QNAMES 0x908 /* as3vm_class.c:206,210; as3vm_names.c:105; as3vm_property.c:229 */ +#endif + +/* ------------------------------------------------------------------ */ +/* Width-gated struct offsets used by the enumeration / array / dict */ +/* fast paths and the exception-unwind walk. The 32-bit column is the */ +/* vita raw the loop was lifted from; the 64-bit column is recovered */ +/* from orbis SIBLING translation units, because orbis's own */ +/* interpret_method_GC is an un-recovered jump table (Ghidra: "Could */ +/* not recover jumptable at 0x001010e2") exactly like durango -- so the */ +/* 64-bit opcode bodies are NOT expandable from raw/orbis directly. */ +/* Ground truth per macro is cited inline. */ +/* */ +/* Per-object qname hashtable ("dict"): the table pointer lives at the */ +/* object's O_CLASSWORD; a key/value entry pair is two machine words. */ +/* 64-bit from orbis as3vm_property.c: qname_add_init (0x101020) stores */ +/* key @ table+0x1f+slot*8 and value at the next slot; the enumeration */ +/* is 1-based (nextname bails on idx<1) so nextname/nextvalue read the */ +/* key/value of pair (idx-1) at table+idx*STRIDE+{KEY,VAL}, and */ +/* is_enumerable reads pair idx at table+idx*STRIDE+{KIND,FLAG}. */ +/* has_enumerable_property (0x101a30) confirms the dont-enum bitmap ptr */ +/* header slot (DICT_NAMES) and the >>4 (0x10) pair stride. */ +#if IGGY_AS3_64BIT +#define DICT_ENT_STRIDE 0x10 /* key+value pair stride (2*WORD) */ +#define DENT_KEY 0xf /* nextname key @ table + idx*STRIDE + KEY */ +#define DENT_VAL 0x17 /* nextvalue val @ table + idx*STRIDE + VAL */ +#define ENUM_KIND 0x1f /* is_enumerable kind (== O_CLASSWORD) */ +#define ENUM_FLAG 0x27 /* is_enumerable flags (== O_CLASSWORD+WORD) */ +#define DICT_NAMES 0x17 /* table header -> dont-enum bitmap ptr */ +#else +#define DICT_ENT_STRIDE 8 /* vita is_enumerable 0x1278e / nextname 0x1e */ +#define DENT_KEY 0xb +#define DENT_VAL 0xf +#define ENUM_KIND 0x13 +#define ENUM_FLAG 0x17 +#define DICT_NAMES 0xf +#endif + +/* Method exception-handler table (read only by this loop's unwind path). + * 32-bit from vita as3vm_abc_decode.c:2019+; 64-bit from orbis + * as3vm_abc_decode.c:1789+ -- the same ABC decoder writes the handler + * count, the handler array pointer, and each handler's from/to/target/ + * type (per-handler from@+0 / to@+4 / target@+8 are int32 on both). */ +#if IGGY_AS3_64BIT +#define EXC_COUNT 0x18 /* minfo + EXC_COUNT = ushort handler count */ +#define EXC_ARRAY 0x30 /* minfo + EXC_ARRAY = handler array ptr */ +#define EXC_STRIDE 0x20 /* per-handler stride */ +#define EXC_TYPE 0x18 /* handler + EXC_TYPE = catch class ptr */ +#else +#define EXC_COUNT 0x10 +#define EXC_ARRAY 0x20 +#define EXC_STRIDE 0x14 +#define EXC_TYPE 0x10 +#endif + +/* Function-closure writable slots the newfunction opcode fills in. + * 32-bit from vita make_function_closure_GC (0x10c56, gc_alloc'd slots at + * cell+0x1c / cell+0x20); 64-bit from orbis make_function_closure_GC + * (0x1011f0, the matching cell+0x30 / cell+0x38 slots). Value-relative. */ +#if IGGY_AS3_64BIT +#define CLOS_SCOPE 0x37 /* newfunction: scope-vector slot */ +#define CLOS_RECV 0x2f /* newfunction: bound-receiver slot */ +#else +#define CLOS_SCOPE 0x1f +#define CLOS_RECV 0x1b +#endif + +/* ------------------------------------------------------------------ */ +/* 64-bit struct offsets recovered DIRECTLY from the retail OBJECT */ +/* machine code (work/iggy-native-decomp/durango-objects/ */ +/* as3vm_interpreter.obj, cross-checked in the orbis object) for the */ +/* opcode bodies that live ONLY in the un-recovered jump table. Ghidra */ +/* could not lift these on any 64-bit target, but the compiled jump- */ +/* table + case bodies carry them. The 32-bit column is the faithful */ +/* vita raw literal; each 64-bit value is cited to the durango */ +/* interpret_method_GC-relative instruction it was read from (both */ +/* durango and orbis objects agree unless noted). */ +#if IGGY_AS3_64BIT +#define MBI_COUNT 0x14 /* get_method_by_index holder+count (ushort); dur get_method_by_index+0x16 `movzwl 0x14(%r9)`, orbis interp+0x3f0f */ +#define MBI_TABLE 0x38 /* get_method_by_index holder+table; dur get_method_by_index+0x20 `movq 0x38(%r9)`, orbis interp+0x3f19 */ +#define SLOT_COUNT 0x1c /* getslot/setslot/g-slot class+count (ushort); dur interp+0x1e23 `movzwl 0x1c(%rax)`, orbis interp+0x17bf `cmpw 0x1c` */ +#define SIG_RETTYPE 0x18 /* return*: method-signature+rettype; dur interp+0x41f1/+0x448d `movq 0x18(%rcx)` */ +#define ACT_TRAITS 0x40 /* newactivation minfo+activation-traits; dur interp+0x2f2a/+0x2f43 `movq 0x40`, orbis interp+0x2c96 */ +#define CI_CLASSVAL 0x30 /* newclass/applytype classinfo+class-value; dur interp+0x309c `movq 0x30`, orbis interp+0x11f6; applytype dur+0x3f00 */ +#define CI_SUBA 0x48 /* newclass classinfo+cinit2-holder; dur interp+0x30af/+0x30e0 `movq 0x48`, orbis interp+0x1215 */ +#define CI_SUBB 0x50 /* newclass classinfo+cinit1-holder; dur interp+0x30c9 `movq 0x50`, orbis interp+0x124a */ +#define NEWOBJ_CLASS 0x2830 /* newobject vm+proto/object-class field; dur interp+0x31ea `movq 0x2830(%r14)`, orbis interp+0xedd (== sibling as3vm_create.c/as3vm_class.c VM_OBJECT_CLASS 64-bit) */ +#define NEWOBJ_PROTO 0x48 /* newobject class-object+prototype-class ptr; dur interp+0x31fa `movq 0x48(%rax)`, orbis interp+0xee5 */ +#define TYPEOF_STR_BASE 0xdd8 /* typeof result-string cache slot 0; dur interp+0x3d9d..+0x3e1c, orbis interp+0x1c38.., slots at BASE + k*WORD */ +#else +#define MBI_COUNT 0xc /* vita get_method_by_index (raw:2740) */ +#define MBI_TABLE 0x24 +#define SLOT_COUNT 0x14 /* vita raw:6117 getslot etc. */ +#define SIG_RETTYPE 0xc /* vita raw:5297 returnvalue */ +#define ACT_TRAITS 0x28 /* vita raw:4798 newactivation */ +#define CI_CLASSVAL 0x1c /* vita raw:4817 newclass */ +#define CI_SUBA 0x28 +#define CI_SUBB 0x2c +#define NEWOBJ_CLASS 0x167c /* vita raw:4687 (local_40+0x1600+0x7c) == sibling VM_OBJECT_CLASS 32-bit */ +#define NEWOBJ_PROTO 0x28 +#define TYPEOF_STR_BASE 0x928 /* vita raw:5940-5975 typeof cache, slots at BASE + k*4 */ +#endif +/* One typeof-result cache slot (k = word index from TYPEOF_STR_BASE). */ +#define TYPEOF_STR(k) VMF(vm, TYPEOF_STR_BASE + (as3v)(k) * WORD) + +/* clone_arrdata_GC's only call site in the shipped binary lives in a sibling + * translation unit outside this reconstruction's scope (neither raw/vita nor + * raw/durango as3vm_interpreter.c ever calls it themselves); the shared + * as3vm_make_arraydata_GC import it exists to reach is already reached + * transitively below via make_scope_stack_closure_GC / + * make_scope_stack_closure_extended_GC. Keep the file-static forward + * declaration referenced here so it is not flagged as dead code. */ +static void *(*const keep_clone_arrdata_GC)(void *, void *) = clone_arrdata_GC; + +/* The vita raw literally calls __modsi3 directly for the integer-modulo + * opcode's fast path (raw/vita:5860: `__modsi3((int)a >> 1,(int)b >> 1)`) + * rather than relying on the compiler's own lowering of `%`, which on this + * EABI target would otherwise pull the unrelated (and unexpected) + * __aeabi_idivmod combined div/mod helper. durango/orbis/ps3 keep the plain + * `%` operator (a native hardware divide on those targets, no libcall). */ +#if defined(IGGY_RECONSTRUCT_AS3VM_INTERPRETER_VITA) +extern int __modsi3(int, int); +#define IGGY_IMOD(a, b) __modsi3((a), (b)) +#else +#define IGGY_IMOD(a, b) ((a) % (b)) +#endif + +/* ------------------------------------------------------------------ */ +/* Opcode-only static helpers (raw/vita:2733-3443). */ +/* ------------------------------------------------------------------ */ + +static int is_enumerable(as3v object, int index) { + as3v entry = *(as3v *)(object + O_CLASSWORD) + (as3v)index * DICT_ENT_STRIDE; + as3v kind = *(as3v *)(entry + ENUM_KIND); + if (kind == 7 || kind == 3) { + return 0; + } + if ((*(uint32_t *)(entry + ENUM_FLAG) & 7) == 3) { + return 0; + } + { + as3v names = *(as3v *)(*(as3v *)(object + O_CLASSWORD) + DICT_NAMES); + if (names == 0) { + return 1; + } + return *(int16_t *)(names + (as3v)index * 2 + 0xb) == 0; + } +} + +static as3v get_method_by_index(void *vm, as3v object, unsigned index) { + (void)vm; + if (TAG(object) == 1 && object != HOLE && TYPEB(object) > 0x13) { + as3v holder = *(as3v *)(object + 7); + if (index + 1 < (unsigned)*(uint16_t *)(holder + MBI_COUNT)) { + return *(as3v *)(*(as3v *)(holder + MBI_TABLE) + (as3v)(index + 1) * WORD); + } + } + return 0; +} + +static int number_is_array_index(as3v numberValue) { + double d = *(double *)(numberValue + 7); + uint32_t nonneg = (0.0 < d) ? 1u : 0u; + uint32_t trunc = (uint32_t)(int32_t)d; + return (double)(nonneg * trunc) == d; +} + +static as3v get_property_name_cacheable(void *vm, as3v name) { + if (TAG(name) == 1 && name != HOLE && TYPEB(name) > 0x13) { + void *table = (void *)(*(as3v *)(name + O_CLASSWORD) - 1); + return (as3v)as3vm_qmap_find_name(vm, table, name); + } + return 0; +} + +/* Shared "coerce a lazily-initialized class value" fixup: getsuper / + * callproperty-family opcodes re-run the class's static initializer the first + * time a still-uninitialized class value flows through a property lookup. */ +static as3v lazy_init_if_class(void *vm, as3v value) { + if (TAG(value) == 1 && value != HOLE && TYPEB(value) == '&') { + as3v holder = *(as3v *)(value + STR_LEN); + if (*(as3v *)(holder + WORD) == 0) { + GC_PUSH(vm, value); + lazy_init_class_GC(vm, *(as3v *)(holder + O_STARTCLASS)); + GC_POP(vm); + value = *(as3v *)GC_TOP(vm); + } + } + return value; +} + +/* ------------------------------------------------------------------ */ +/* interpret_method_GC -- the fully-expanded opcode switch. */ +/* ------------------------------------------------------------------ */ + +static void interpret_method_GC(as3v *frame) { + void *vm = (void *)frame[MF_VM]; + (void)keep_clone_arrdata_GC; + + if (VMF(vm, VM_PENDING_EXC) != 0) { + VMF(vm, VM_FRAME_CUR) = frame[MF_SAVED]; + return; + } + + { + uint8_t *pc = (uint8_t *)frame[MF_CODE]; + as3v *stack = (as3v *)(frame[MF_LOCALS] - WORD); + as3v *locals = (as3v *)frame[MF_THIS]; + as3v *pool = (as3v *)frame[MF_SCOPE]; + as3v minfo = frame[MF_METHOD]; + as3v declaring_class = ((as3v *)minfo)[1]; + uint8_t opcode; + +#define OPB(off) (pc[(off)]) +#define OPU16(off) (*(uint16_t *)(pc + (off))) +#define OPS16(off) (*(int16_t *)(pc + (off))) +#define ERRMSG(msg) \ + do { \ + VMF(vm, VM_ERR_S1) = (as3v)(msg); \ + VMF(vm, VM_ERR_S2) = 0; \ + goto err78; \ + } while (0) + + dispatch: + opcode = *pc; + switch (opcode) { + case 0x00: + case 0x01: + case 0x0a: + case 0x0b: + case 0x22: + case 0x33: case 0x34: case 0x35: case 0x36: + case 0x37: case 0x38: case 0x39: case 0x3a: + case 0x3b: case 0x3c: case 0x3d: case 0x3e: case 0x3f: + case 0x4b: + case 0x4d: + case 0x50: case 0x51: case 0x52: + case 0x54: + case 0x5b: case 0x5c: + case 0x5f: + case 0x67: + case 0x69: + case 0x6b: + goto invalid_op; + + case 0x79: case 0x7a: case 0x7b: case 0x7c: case 0x7d: case 0x7e: + case 0x7f: case 0x81: case 0x83: case 0x84: + case 0x88: case 0x89: case 0x8a: case 0x8b: case 0x8c: case 0x8d: + case 0x8e: case 0x8f: + case 0x98: case 0x99: case 0x9a: case 0x9b: case 0x9c: case 0x9d: + case 0x9e: case 0x9f: + case 0xb5: case 0xb6: case 0xb7: + case 0xb8: + pc += 8; + goto invalid_op; + + case 0xb9: case 0xba: case 0xbb: case 0xbc: case 0xbd: case 0xbe: + case 0xbf: + case 0xc8: case 0xc9: case 0xca: case 0xcb: case 0xcc: case 0xcd: + case 0xce: case 0xcf: + case 0xd8: case 0xd9: case 0xda: case 0xdb: case 0xdc: case 0xdd: + case 0xde: case 0xdf: + case 0xe0: case 0xe1: case 0xe2: case 0xe3: case 0xe4: case 0xe5: + case 0xe6: case 0xe7: + pc += 6; + goto invalid_op; + + case 0xe8: case 0xe9: case 0xea: case 0xeb: case 0xec: case 0xed: + case 0xee: + case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7: + case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: + case 0xfe: case 0xff: + pc += 4; + goto invalid_op; + + case 0x02: /* nop */ + pc += 2; + break; + + case 0x03: { /* throw */ + VMF(vm, VM_PENDING_EXC) = *stack; + VMF(vm, VM_ERR_LOC) = 0; + goto already_raised; + } + + case 0x04: { /* getsuper */ + as3v name = pool[OPU16(2)]; + pc += 4; + if (TAG(name) == 2) { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, stack, 0); + if (sp2 == NULL) goto err78; + stack = sp2; + } + { + as3v recv = *stack; + if (recv == VUNDEF || recv == VNULL) { + record_error_info(vm, name, (as3v)"had no super when trying to get super variable $"); + goto err76; + } + { + as3v result = as3vm_get_property_name_closure_super_GC(vm, recv, name); + if (result == HOLE) { + record_error_info(vm, name, (as3v)"get from super found no definition for $"); + goto err76; + } + result = lazy_init_if_class(vm, result); + *stack = result; + } + } + break; + } + + case 0x05: { /* setsuper */ + as3v name = pool[OPU16(2)]; + as3v value = *stack; + as3v recv = stack[-1]; + pc += 4; + stack -= 1; + GC_PUSH(vm, value); + if (TAG(name) == 2) { + as3v *rp = (as3v *)&recv; + as3v *sp2 = name_resolve_runtime_GC(vm, &name, rp, 1); + if (sp2 == NULL) goto err78; + recv = *rp; + } + GC_POP(vm); + value = *(as3v *)GC_TOP(vm); + as3vm_set_property_name_super_GC(vm, recv, name, value); + if (VMF(vm, VM_PENDING_EXC) != 0) goto already_raised; + stack -= 1; + break; + } + + case 0x06: /* dxns */ + pc += 4; + break; + case 0x07: /* dxnslate */ + stack -= 1; + pc += 2; + break; + case 0x08: /* kill */ + locals[OPU16(2)] = VUNDEF; + pc += 4; + break; + case 0x09: /* label */ + pc += 2; + break; + + case 0x0c: { /* ifnlt */ + int16_t disp = OPS16(2); + as3v r = abstract_relational(vm, stack[-1], stack[0]); + stack -= 2; + pc = (r != VTRUE) ? pc + 4 + disp : pc + 4; + break; + } + case 0x0d: { /* ifnle */ + int16_t disp = OPS16(2); + as3v r = abstract_relational(vm, stack[0], stack[-1]); + stack -= 2; + pc = (r != VFALSE) ? pc + 4 + disp : pc + 4; + break; + } + case 0x0e: { /* ifngt */ + int16_t disp = OPS16(2); + as3v r = abstract_relational(vm, stack[0], stack[-1]); + stack -= 2; + pc = (r != VTRUE) ? pc + 4 + disp : pc + 4; + break; + } + case 0x0f: { /* ifnge */ + int16_t disp = OPS16(2); + as3v r = abstract_relational(vm, stack[-1], stack[0]); + stack -= 2; + pc = (r != VFALSE) ? pc + 4 + disp : pc + 4; + break; + } + case 0x10: /* jump */ + pc = pc + 4 + OPS16(2); + break; + case 0x11: { /* iftrue */ + as3v cond = as3vm_to_boolean(*stack); + stack -= 1; + pc = (cond == VTRUE) ? pc + 4 + OPS16(2) : pc + 4; + break; + } + case 0x12: { /* iffalse */ + as3v cond = as3vm_to_boolean(*stack); + stack -= 1; + pc = (cond == VFALSE) ? pc + 4 + OPS16(2) : pc + 4; + break; + } + case 0x13: { /* ifeq */ + as3v r = abstract_equality(vm, stack[-1], *stack); + if (r == HOLE) ERRMSG("in '==' (equality)"); + stack -= 2; + pc = (r == VTRUE) ? pc + 4 + OPS16(2) : pc + 4; + break; + } + case 0x14: { /* ifne */ + as3v r = abstract_equality(vm, stack[-1], *stack); + if (r == HOLE) ERRMSG("in '!=' (inequality)"); + stack -= 2; + pc = (r == VFALSE) ? pc + 4 + OPS16(2) : pc + 4; + break; + } + case 0x15: { /* iflt */ + as3v r = abstract_relational(vm, stack[-1], *stack); + stack -= 2; + pc = (r == VTRUE) ? pc + 4 + OPS16(2) : pc + 4; + break; + } + case 0x16: { /* ifle */ + as3v r = abstract_relational(vm, *stack, stack[-1]); + stack -= 2; + pc = (r == VFALSE) ? pc + 4 + OPS16(2) : pc + 4; + break; + } + case 0x17: { /* ifgt */ + as3v r = abstract_relational(vm, *stack, stack[-1]); + stack -= 2; + pc = (r == VTRUE) ? pc + 4 + OPS16(2) : pc + 4; + break; + } + case 0x18: { /* ifge */ + as3v r = abstract_relational(vm, stack[-1], *stack); + stack -= 2; + pc = (r == VFALSE) ? pc + 4 + OPS16(2) : pc + 4; + break; + } + case 0x19: { /* ifstricteq */ + as3v b = *stack, a = stack[-1]; + int taken; + if (TAG(a) == 1 && a != HOLE && TAG(b) == 1 && b != HOLE && + TYPEB(a) == 8 && TYPEB(b) == 8) { + taken = string_eq(*(as3v *)(a + STR_LEN) - 1, *(as3v *)(b + STR_LEN) - 1) != 0; + } else { + taken = strict_equality(a, b) == VTRUE; + } + stack -= 2; + pc = taken ? pc + 4 + OPS16(2) : pc + 4; + break; + } + case 0x1a: { /* ifstrictne */ + as3v b = *stack, a = stack[-1]; + int equal; + if (TAG(a) == 1 && a != HOLE && TAG(b) == 1 && b != HOLE && + TYPEB(a) == 8 && TYPEB(b) == 8) { + equal = string_eq(*(as3v *)(a + STR_LEN) - 1, *(as3v *)(b + STR_LEN) - 1) != 0; + } else { + equal = strict_equality(a, b) == VTRUE; + } + stack -= 2; + pc = equal ? pc + 4 : pc + 4 + OPS16(2); + break; + } + case 0x1b: { /* lookupswitch */ + uint16_t count = OPU16(2); + int32_t sel; + if (!as3vm_ToInt32_GC(vm, *stack, &sel)) goto err78; + if (sel < 0 || (int32_t)(uint32_t)count < sel) sel = -1; + stack -= 1; + pc = pc + OPS16(4 + sel * 2); + break; + } + case 0x1c: { /* pushwith */ + as3v scope = *stack; + if (scope == VNULL || scope == VUNDEF) ERRMSG("in 'with'"); + if (frame[MF_PC] == 0) frame[MF_PC] = frame[MF_ARGS2]; + *(as3v *)frame[MF_ARGS2] = scope; + frame[MF_ARGS2] += WORD; + stack -= 1; + pc += 2; + break; + } + case 0x1d: { /* popscope */ + frame[MF_ARGS2] -= WORD; + if (frame[MF_ARGS2] == frame[MF_PC]) frame[MF_PC] = 0; + pc += 2; + break; + } + case 0x1e: { /* nextname */ + as3v idxv = *stack; + as3v objv; + int idx; + pc += 2; + if ((idxv & 1) != 0) { + if (TAG(idxv) == 1 && TYPEB(idxv) == 7) { + double d = *(double *)(idxv + STR_LEN); + idx = (int)d; + if ((double)idx != d) goto err78; + } else { + goto err78; + } + } else { + idx = (int)((intptr_t)idxv >> 1); + } + objv = stack[-1]; + if (TAG(objv) == 1 && TYPEB(objv) > 0x13) { + as3v arrdata = *(as3v *)(objv + O_CLASSWORD); + int32_t len = *(int32_t *)(arrdata + 7); + if (len < 0) len = *(int32_t *)(arrdata + 3); + if (idx < 1 || (idx * 2 - len != 0 && len <= idx * 2)) { + if (TYPEB(objv) == 0x20) { + int eidx = idx - (len >> 1) - 1; + if (eidx < *(int32_t *)(*(as3v *)(objv + O_ARRDATA) + 7)) { + as3v name = *(as3v *)(*(as3v *)(objv + O_ARRDATA) + (as3v)eidx * WORD + O_CLASSWORD); + stack[-1] = (name == 7) ? VNULL : (as3v)(eidx * 2); + } else { + stack[-1] = VNULL; + } + } else { + stack[-1] = VNULL; + } + } else { + as3v entry = arrdata + (as3v)idx * DICT_ENT_STRIDE; + as3v key = *(as3v *)(entry + DENT_KEY); + if (key == 3 || key == 7) { + stack[-1] = VNULL; + } else if ((*(uint32_t *)(entry + DENT_VAL) & 7) == 3) { + stack[-1] = VNULL; + } else if ((key & 3) == 3) { + stack[-1] = *(as3v *)(key + 1) + 1; + } else { + stack[-1] = key; + } + } + } else { + stack[-1] = VNULL; + } + stack -= 1; + break; + } + case 0x1f: { /* hasnext */ + as3v idxv = *stack; + int idx; + pc += 2; + if ((idxv & 1) == 0) { + idx = (int)((intptr_t)idxv >> 1); + } else if (TAG(idxv) == 1 && TYPEB(idxv) == 7 && + (double)(int)*(double *)(idxv + STR_LEN) == + *(double *)(idxv + STR_LEN)) { + idx = (int)*(double *)(idxv + STR_LEN); + } else { + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + { + as3v objv = stack[-1]; + if (TAG(objv) == 1 && TYPEB(objv) > 0x13) { + as3v arrdata = *(as3v *)(objv + O_CLASSWORD); + int32_t len = *(int32_t *)(arrdata + 7); + int cur = idx * 2, next = idx * 2 - 2; + if (len < 0) len = *(int32_t *)(arrdata + 3); + for (;;) { + cur = next; + if (len <= cur) break; + next = cur + 2; + if (is_enumerable(objv, idx)) break; + idx += 1; + } + stack[-1] = (cur < len) ? (as3v)next : 0; + } else { + stack[-1] = 0; + } + } + stack -= 1; + break; + } + case 0x20: /* pushnull */ + *++stack = VNULL; + pc += 2; + break; + case 0x21: /* pushundefined */ + *++stack = VUNDEF; + pc += 2; + break; + case 0x23: { /* nextvalue */ + as3v idxv = *stack; + as3v objv; + int idx; + pc += 2; + if ((idxv & 1) != 0) { + if (TAG(idxv) == 1 && TYPEB(idxv) == 7) { + double d = *(double *)(idxv + STR_LEN); + idx = (int)d; + if ((double)idx != d) goto err78; + } else { + goto err78; + } + } else { + idx = (int)((intptr_t)idxv >> 1); + } + objv = stack[-1]; + if (TAG(objv) == 1 && TYPEB(objv) > 0x13) { + as3v arrdata = *(as3v *)(objv + O_CLASSWORD); + int32_t len = *(int32_t *)(arrdata + 7); + if (len < 0) len = *(int32_t *)(arrdata + 3); + if (idx < 1 || (idx * 2 - len != 0 && len <= idx * 2)) { + if (TYPEB(objv) == 0x20) { + int eidx = idx - (len >> 1) - 1; + if (eidx < *(int32_t *)(*(as3v *)(objv + O_ARRDATA) + 7)) { + as3v val = *(as3v *)(*(as3v *)(objv + O_ARRDATA) + (as3v)eidx * WORD + O_CLASSWORD); + stack[-1] = (val == 7) ? VUNDEF : val; + } else { + stack[-1] = VUNDEF; + } + } else { + stack[-1] = VUNDEF; + } + } else { + as3v entry = arrdata + (as3v)idx * DICT_ENT_STRIDE; + as3v val = *(as3v *)(entry + DENT_VAL); + stack[-1] = ((val & 7) == 3) ? VUNDEF : val; + } + } else { + stack[-1] = VUNDEF; + } + stack -= 1; + break; + } + case 0x24: /* pushbyte */ + *++stack = (as3v)((int64_t)(int8_t)OPB(2) << 1); + pc += 4; + break; + case 0x25: /* pushshort */ + *++stack = (as3v)((int64_t)OPS16(2) << 1); + pc += 4; + break; + case 0x26: /* pushtrue */ + *++stack = VTRUE; + pc += 2; + break; + case 0x27: /* pushfalse */ + *++stack = VFALSE; + pc += 2; + break; + case 0x28: /* pushnan */ + *++stack = VNAN; + pc += 2; + break; + case 0x29: /* pop */ + stack -= 1; + pc += 2; + break; + case 0x2a: { /* dup */ + as3v top = stack[0]; + *++stack = top; + pc += 2; + break; + } + case 0x2b: { /* swap */ + as3v t = stack[-1]; + stack[-1] = stack[0]; + stack[0] = t; + pc += 2; + break; + } + case 0x2c: /* pushstring */ + case 0x2d: /* pushint */ + case 0x2e: /* pushuint */ + case 0x2f: /* pushdouble */ + case 0x31: /* pushnamespace */ + *++stack = pool[OPU16(2)]; + pc += 4; + break; + case 0x30: { /* pushscope */ + as3v scope = *stack; + if (scope == VNULL || scope == VUNDEF) goto err78; + *(as3v *)frame[MF_ARGS2] = scope; + frame[MF_ARGS2] += WORD; + stack -= 1; + pc += 2; + break; + } + case 0x32: { /* hasnext2 */ + unsigned objreg = OPU16(4); + unsigned idxreg = OPU16(2); + as3v objv = locals[objreg]; + as3v idxv = locals[idxreg]; + int idx; + pc += 6; + if ((idxv & 1) != 0) { + if (TAG(idxv) == 1 && TYPEB(idxv) == 7 && + (double)(int)*(double *)(idxv + STR_LEN) == + *(double *)(idxv + STR_LEN)) { + idx = (int)*(double *)(idxv + STR_LEN); + } else { + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + } else { + idx = (int)((intptr_t)idxv >> 1); + } + { + as3v cur = (objv == VNULL) ? VUNDEF : objv; + int found = 0; + idx += 1; + if (cur != VNULL) { + for (;;) { + if (TAG(cur) == 1 && TYPEB(cur) > 0x13) { + as3v arrdata = *(as3v *)(cur + O_CLASSWORD); + int32_t len = *(int32_t *)(arrdata + 7); + int p = idx * 2; + if (len < 0) len = *(int32_t *)(arrdata + 3); + if (p == len || p < len) { + for (; p <= len; p += 2, idx += 1) { + if (is_enumerable(cur, idx - 1)) { + *++stack = VTRUE; + locals[objreg] = cur; + locals[idxreg] = (as3v)p; + found = 1; + break; + } + } + if (found) break; + } + if (TYPEB(cur) == 0x20) { + int32_t alen = *(int32_t *)(*(as3v *)(cur + O_ARRDATA) + 7); + int eidx = idx - (len >> 1) - 1; + if (eidx < alen) { + as3v elems = *(as3v *)(cur + O_ARRDATA) - 1; + for (; eidx < alen; eidx += 1, idx += 1) { + if (*(as3v *)(elems + (O_CLASSWORD + 1) + (as3v)eidx * WORD) != 7) { + *++stack = VTRUE; + locals[objreg] = cur; + locals[idxreg] = (as3v)(idx << 1); + found = 1; + break; + } + } + if (found) break; + } + } + } + { + as3v next = as3vm_get_prototype(vm, cur); + if (cur == VNULL || next == 0 || next == cur) { + cur = VNULL; + break; + } + cur = next; + idx = 1; + } + } + } + if (!found) { + *++stack = VFALSE; + locals[objreg] = VNULL; + locals[idxreg] = 0; + } + } + break; + } + + case 0x40: { /* newfunction */ + as3v method = *(as3v *)pool[OPU16(2)] + 1; + as3v closure = as3vm_make_function_closure_GC(vm, 0, method); + void *scopevec = make_scope_stack_closure_GC(vm, frame); + as3v receiver; + pc += 4; + *++stack = closure; + if (TAG((as3v)scopevec) == 1 && (as3v)scopevec != HOLE && + 0x17u < (MARKB(closure) & ~*(uint8_t *)scopevec)) { + as3vm_write_barrier_slowpath(vm, (void *)(closure - 1), (void *)((as3v)scopevec - 1)); + } + *(as3v *)(closure + CLOS_SCOPE) = (as3v)scopevec; + { + as3v mfield = ((as3v *)frame[MF_CTX])[-1]; + if (*(as3v *)(mfield + 7) == 0) { + receiver = *(as3v *)frame[MF_CTX]; + } else { + receiver = *(as3v *)(mfield + O_CLASSWORD); + } + } + if (TAG(receiver) == 1 && receiver != HOLE && + 0x17u < (MARKB(closure) & ~*(uint8_t *)receiver)) { + as3vm_write_barrier_slowpath(vm, (void *)(closure - 1), (void *)(receiver - 1)); + } + *(as3v *)(closure + CLOS_RECV) = receiver; + break; + } + case 0x41: { /* call */ + unsigned argc = OPU16(2); + as3v *base = stack - argc; + pc += 4; + stack = base - 1; + as3vm_do_func_call_vv_GC(vm, (int64_t *)stack, (int64_t)base[-1], (int64_t)(as3v)base, (int)argc, 1); + if (VMF(vm, VM_PENDING_EXC) != 0) { + if (VMF(vm, VM_ERR_LOC) != 0) goto unwind_no_raise; + record_error_function(vm, base[-1]); + goto already_raised; + } + break; + } + case 0x42: { /* construct */ + unsigned argc = OPU16(2); + as3v *base = stack - argc; + as3v saved = VMF(vm, VM_CLEAR_A); + pc += 4; + VMF(vm, VM_CLEAR_A) = 0; + as3vm_construct_GC(vm, base, base[0], base, argc); + VMF(vm, VM_CLEAR_A) = saved; + if (VMF(vm, VM_PENDING_EXC) != 0) goto already_raised; + stack = base; + if (*base == 7) ERRMSG("constructing $"); + break; + } + case 0x43: { /* callmethod */ + unsigned argc = OPU16(4); + as3v *base = stack - argc; + as3v fn; + pc += 6; + if (*stack == VUNDEF || *stack == VNULL) ERRMSG("method call on non-Object"); + fn = get_method_by_index(vm, *base, OPU16(2)); + as3vm_do_func_call_vv_GC(vm, (int64_t *)base, (int64_t)fn, (int64_t)(as3v)base, (int)argc, 0); + stack = base; + if (VMF(vm, VM_PENDING_EXC) != 0) { + if (VMF(vm, VM_ERR_LOC) == 0) { + record_error_function(vm, 0); + goto already_raised; + } + goto unwind_no_raise; + } + break; + } + case 0x44: { /* callstatic */ + unsigned argc = OPU16(4); + as3v method = pool[OPU16(2)]; + as3v *base = stack - argc; + pc += 6; + do_func_call_mv_GC(vm, (as3v)base, (as3v *)method, base[0], base + 1, argc); + stack = base; + if (VMF(vm, VM_PENDING_EXC) != 0) { + if (VMF(vm, VM_ERR_LOC) == 0) { + record_error_location(vm, (as3v *)method); + goto already_raised; + } + goto unwind_no_raise; + } + break; + } + case 0x45: { /* callsuper */ + as3v name = pool[OPU16(4)]; + unsigned argc = OPU16(2); + as3v *base = stack - argc; + pc += 6; + if (TAG(name) == 2) { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, base, 0); + if (sp2 == NULL) goto err78; + base = sp2; + } + { + as3v fn = as3vm_call_property_name_super(vm, *locals, name); + if (fn == HOLE) { + record_error_info(vm, name, (as3v)"bad super call of $"); + goto err76; + } + if (TAG(fn) == 1 && TYPEB(fn) == 0x10 && base[0] == *locals) { + as3vm_do_func_call_vv_GC(vm, (int64_t *)base, (int64_t)fn, (int64_t)(as3v)base, (int)argc, 0); + stack = base; + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + break; + } + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + } + case 0x46: { /* callproperty */ + as3v name = pool[OPU16(4)]; + unsigned argc = OPU16(2); + as3v *base = stack - argc; + pc += 6; + if (TAG(name) == 2) { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, base, 0); + if (sp2 == NULL) goto err78; + base = sp2; + } + { + as3v recv = *base; + if (recv == VUNDEF || recv == VNULL) { + record_error_info(vm, name, (as3v)"non-callable object was called with $"); + goto err78; + } + { + as3v fn = as3vm_get_property_name_GC(vm, recv, name); + fn = lazy_init_if_class(vm, fn); + if (fn != VUNDEF && fn != VNULL) { + as3vm_do_func_call_vv_GC(vm, (int64_t *)base, (int64_t)fn, (int64_t)(as3v)base, (int)argc, 0); + stack = base; + if (VMF(vm, VM_PENDING_EXC) != 0) { + if (VMF(vm, VM_ERR_LOC) == 0) { + record_error_function(vm, fn); + goto already_raised; + } + goto unwind_no_raise; + } + break; + } + record_error_info(vm, name, (as3v)"got null or undef when looking for method named $"); + goto err78; + } + } + } + case 0x47: /* returnvoid */ + goto do_return_void; + case 0x48: { /* returnvalue */ + as3v value = *stack; + as3v sig = *(as3v *)minfo; + as3v rettype = *(as3v *)(sig + SIG_RETTYPE); + if (rettype != 0) { + value = as3vm_coerce_GC(vm, value, rettype); + *stack = value; + if (value == 7) ERRMSG("coercing return value"); + } + *(as3v *)frame[MF_RESULT] = value; + VMF(vm, VM_FRAME_CUR) = frame[MF_SAVED]; + return; + } + case 0x49: { /* constructsuper */ + unsigned argc = OPU16(2); + as3v *base = stack - argc; + as3v superclass; + pc += 4; + if (declaring_class == 0 || + (superclass = *(as3v *)(declaring_class + CLS_SUPER)) == 0) { + ERRMSG("invoking super-constructor"); + } + { + as3v ctorobj = *(as3v *)superclass; + as3v cf[8]; + cf[0] = (as3v)vm; + if (ctorobj == 0) { + void (*nativector)(void *) = *(void (**)(void *))(superclass + CLS_NATIVE_CTOR); + if (nativector != NULL) { + cf[1] = (as3v)base; + cf[2] = (as3v)vm; + cf[3] = 1; + cf[4] = argc; + nativector(cf); + } + } else { + cf[1] = (as3v)vm; + cf[2] = argc; + cf[3] = (as3v)base; + cf[4] = (as3v)base; + (*(void (**)(void *))(ctorobj + O_INVOKE))(cf); + } + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + } + stack = base - 1; + break; + } + case 0x4a: { /* constructprop */ + as3v name = pool[OPU16(4)]; + unsigned argc = OPU16(2); + as3v *base = stack - argc; + pc += 6; + if (TAG(name) == 2) { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, base, 0); + if (sp2 == NULL) goto err78; + base = sp2; + } + { + as3v klass = as3vm_get_property_name_GC(vm, *base, name); + as3v saved = VMF(vm, VM_CLEAR_A); + VMF(vm, VM_CLEAR_A) = 0; + as3vm_construct_GC(vm, base, klass, base, argc); + VMF(vm, VM_CLEAR_A) = saved; + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + stack = base; + if (*base != 7) break; + record_error_info(vm, name, (as3v)"constructing $"); + goto err78; + } + } + case 0x4c: { /* callproplex */ + as3v name = pool[OPU16(4)]; + unsigned argc = OPU16(2); + as3v *base = stack - argc; + pc += 6; + if (TAG(name) == 2) { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, base, 0); + if (sp2 == NULL) goto err78; + base = sp2; + } + { + as3v recv = *base; + if (recv != VUNDEF && recv != VNULL) { + as3v fn = as3vm_get_property_name_GC(vm, recv, name); + fn = lazy_init_if_class(vm, fn); + *base = VNULL; + as3vm_do_func_call_vv_GC(vm, (int64_t *)base, (int64_t)fn, (int64_t)(as3v)base, (int)argc, 0); + stack = base; + if (VMF(vm, VM_PENDING_EXC) != 0) { + if (VMF(vm, VM_ERR_LOC) == 0) { + record_error_function(vm, fn); + goto already_raised; + } + goto unwind_no_raise; + } + break; + } + record_error_info(vm, name, (as3v)"non-callable object was called with $"); + goto err78; + } + } + case 0x4e: { /* callsupervoid */ + as3v name = pool[OPU16(4)]; + unsigned argc = OPU16(2); + as3v *base = stack - argc; + pc += 6; + if (TAG(name) == 2) { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, base, 0); + if (sp2 == NULL) goto err78; + base = sp2; + } + { + as3v fn = as3vm_call_property_name_super(vm, *locals, name); + if (fn == HOLE) { + record_error_info(vm, name, (as3v)"bad super call of $"); + goto err76; + } + if (TAG(fn) == 1 && TYPEB(fn) == 0x10 && base[0] == *locals) { + as3vm_do_func_call_vv_GC(vm, (int64_t *)base, (int64_t)fn, (int64_t)(as3v)base, (int)argc, 0); + stack = base - 1; + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + break; + } + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + } + case 0x4f: { /* callpropvoid */ + as3v name = pool[OPU16(4)]; + unsigned argc = OPU16(2); + as3v *base = stack - argc; + pc += 6; + if (TAG(name) == 2) { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, base, 0); + if (sp2 == NULL) goto err78; + base = sp2; + } + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + { + as3v recv = *base; + if (recv == VUNDEF || recv == VNULL) { + record_error_info(vm, name, (as3v)"non-callable object was called with $"); + goto err78; + } + { + as3v fn = as3vm_get_property_name_GC(vm, recv, name); + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + if (fn != VUNDEF && fn != VNULL) { + fn = lazy_init_if_class(vm, fn); + as3vm_do_func_call_vv_GC(vm, (int64_t *)base, (int64_t)fn, (int64_t)(as3v)base, (int)argc, 0); + stack = base - 1; + if (VMF(vm, VM_PENDING_EXC) != 0) { + if (VMF(vm, VM_ERR_LOC) == 0) { + record_error_function(vm, fn); + goto already_raised; + } + goto unwind_no_raise; + } + break; + } + record_error_info(vm, name, (as3v)"got null or undef when looking for method named $"); + goto err78; + } + } + } + + case 0x53: { /* applytype */ + as3v arg; + as3v klass; + unsigned pop = 1; + if (OPS16(2) != 1 || stack[-1] != VMF(vm, VMC_ARRAY_PROTO)) { + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + arg = *stack; + if (TAG(arg) == 1 && TYPEB(arg) > 0x1f) { + if (TYPEB(arg) == '&') { + as3v elemClass = *(as3v *)(*(as3v *)(arg + STR_LEN) + O_STARTCLASS); + as3v vecClass = *(as3v *)(elemClass + CLS_VECREF); + if (vecClass == 0) { + vecClass = as3vm_make_vector_class(vm, elemClass); + pop = OPU16(2); + } + klass = *(as3v *)(vecClass + CI_CLASSVAL); + stack -= pop; + *stack = klass; + pc += 4; + break; + } + } else if (arg == VNULL) { + klass = *(as3v *)(0 + CI_CLASSVAL); + (void)klass; + } + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + + case 0x55: { /* newobject */ + unsigned n = OPU16(2); + as3v *base = stack - n * 2; + as3v protoclass = **(as3v **)(VMF(vm, NEWOBJ_CLASS) + NEWOBJ_PROTO); + as3v arrdata; + as3v holderobj; + unsigned cap; + pc += 4; + cap = (unsigned)(((int)n + (-*(int32_t *)(protoclass + 7) >> 1)) * 5) >> 2; + { + unsigned p = cap - 1 & cap; + while (p != 0) { + p = cap + 1 & cap; + cap = cap + 1; + } + } + if ((int)cap < 4) cap = 4; + arrdata = (as3v)as3vm_make_arraydata_GC(vm, cap << 1, 0); + { + unsigned i; + as3v ent = arrdata; + for (i = 0; i < cap; ++i, ent += DICT_ENT_STRIDE) { + *(as3v *)(ent + ENUM_FLAG) = VUNDEF; + *(as3v *)(ent + ENUM_KIND) = 7; + } + } + GC_PUSH(vm, arrdata); + holderobj = as3vm_make_object_GC(vm, 0x23); + GC_POP(vm); + arrdata = *(as3v *)GC_TOP(vm); + if (0x17u < (MARKB(holderobj) & ~*(uint8_t *)arrdata)) { + as3vm_write_barrier_slowpath(vm, (void *)(holderobj - 1), (void *)(arrdata - 1)); + } + *(as3v *)(holderobj + O_CLASSWORD) = arrdata; + { + int32_t alen = *(int32_t *)(protoclass + 3); + if (alen > 0) { + int32_t i; + as3v ent = protoclass - 1; + for (i = 0; i < alen; i += 2, ent += DICT_ENT_STRIDE) { + if (*(as3v *)(ent + (ENUM_KIND + 1)) != 7) { + as3vm_qname_add_init((void *)arrdata, (void *)(*(as3v *)(ent + (ENUM_KIND + 1)) - 3), + (void *)*(as3v *)(ent + (ENUM_FLAG + 1)), 0); + alen = *(int32_t *)(protoclass + 3); + } + } + } + } + { + unsigned i; + as3v *pair = base; + for (i = 0; i < n; ++i) { + as3v nameval = pair[1]; + as3v sd; + as3v qtable; + if (TAG(nameval) != 1 || TYPEB(nameval) != '\b') { + ERRMSG("name in bare-Object-constructor is non-String"); + } + sd = *(as3v *)(nameval + STR_LEN); + if (*(char *)(sd + 1) != 'c') { + intptr_t interned = as3vm_find_intern_string(vm, *(int32_t *)(sd + 7), + (void *)(sd + 0xb), 1); + *(as3v *)(nameval + STR_LEN) = (as3v)interned + 1; + } + qtable = (as3v)as3vm_qname_find(vm, (void *)(*(as3v *)(nameval + STR_LEN) - INTERN_ADJ), + (void *)VMF(vm, VM_ROOT_QNAMES), 1); + pair += 2; + as3vm_qname_add_init_safe(vm, (void *)holderobj, (void *)qtable, (int)pair[0]); + } + } + stack = base + 1; + *stack = holderobj; + break; + } + case 0x56: { /* newarray */ + unsigned n = OPU16(2); + as3v arrval = as3vm_make_array_GC(vm, (int)n, (int)n); + as3v elems = *(as3v *)(arrval + O_ARRDATA); + as3v *base = stack - n; + pc += 4; + if (*(uint8_t *)elems == 0) { + memcpy((void *)(elems + O_CLASSWORD), (void *)(base + 1), (size_t)n * WORD); + } else if (n != 0) { + unsigned i; + as3v ent = elems - 1 + (O_CLASSWORD + 1); + for (i = 0; i < n; ++i, ent += WORD) { + as3v e = base[1 + i]; + if (TAG(e) == 1 && e != HOLE && 0x17u < (*(uint8_t *)elems & ~*(uint8_t *)e)) { + as3vm_write_barrier_slowpath(vm, (void *)(elems - 1), (void *)(e - 1)); + } + *(as3v *)ent = e; + } + } + stack = base + 1; + *stack = arrval; + break; + } + case 0x57: { /* newactivation */ + as3v traits = *(as3v *)(minfo + ACT_TRAITS); + as3v obj; + if (traits == 0) { + obj = as3vm_make_object_GC(vm, 0x23); + } else { + /* The activation "proto" written into the object is the traits + * value itself (a re-read of minfo+ACT_TRAITS), NOT *(traits+0x28): + * vita raw (raw/vita:4802-4809, `puVar9 = *(minfo+0x28)`) and the + * durango object (interp+0x2f43 `movq 0x40(minfo)` -> obj+7) agree. + * The earlier `*(traits+0x28)` transcription was a reconstruction + * error, wrong on both widths. */ + as3v proto = *(as3v *)(minfo + ACT_TRAITS); + obj = as3vm_make_object_with_traits_GC(vm, (void *)traits, 0); + *(as3v *)(obj + STR_LEN) = proto; + *(as3v *)(obj + O_CLASSWORD) = *(as3v *)proto; + *(as3v *)(obj + O_LHASH) = 0; + } + *++stack = obj; + pc += 2; + break; + } + case 0x58: { /* newclass */ + as3v classinfo = pool[OPU16(2)]; + as3v classval = *(as3v *)(classinfo + CI_CLASSVAL); + pc += 4; + *stack = classval; + if (*(as3v *)(classinfo + WORD) != 0 && *(as3v *)(*(as3v *)(classinfo + CI_SUBA) + WORD) == 0) { + as3v cinit1 = (as3v)make_scope_stack_closure_GC(vm, frame); + as3v cinit2; + *(as3v *)(*(as3v *)(classinfo + CI_SUBB) + WORD) = cinit1; + cinit2 = (as3v)make_scope_stack_closure_extended_GC(vm, frame, *(as3v *)(classinfo + CI_CLASSVAL)); + *(as3v *)(*(as3v *)(classinfo + CI_SUBA) + WORD) = cinit2; + { + as3v cf[8]; + as3v cinitobj = *(as3v *)(classinfo + WORD); + cf[0] = (as3v)vm; + cf[1] = 0; + cf[2] = (as3v)stack; + cf[3] = cinitobj; + (*(void (**)(void *))(cinitobj + O_INVOKE))(cf); + } + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + } + break; + } + case 0x59: { /* getdescendants (unsupported, E4X) */ + as3v name = pool[OPU16(2)]; + pc += 4; + if (TAG(name) == 2) { + if (name_resolve_runtime_GC(vm, &name, stack, 0) == NULL) goto err78; + } + goto invalid_op; + } + case 0x5a: /* newcatch */ + *++stack = as3vm_make_object_no_constructor_GC(vm); + pc += 4; + break; + + case 0x5d: { /* findpropstrict */ + as3v name = pool[OPU16(2)]; + pc += 4; + if (TAG(name) == 2) { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, stack, 0); + if (sp2 == NULL) goto err78; + stack = sp2; + } + { + as3v scope = (as3v)&frame[MF_ARGS]; + as3v result = as3vm_find_property_scoped_GC(vm, scope, name); + if (result == HOLE) { + record_error_info(vm, name, (as3v)"looking for $"); + goto err76; + } + *++stack = result; + } + break; + } + case 0x5e: { /* findproperty */ + as3v name = pool[OPU16(2)]; + pc += 4; + if (TAG(name) == 2) { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, stack, 0); + if (sp2 == NULL) goto err78; + stack = sp2; + } + { + as3v scope = (as3v)&frame[MF_ARGS]; + as3v result = as3vm_find_property_scoped_GC(vm, scope, name); + if (result == HOLE) result = VMF(vm, VM_FRAME_FLOOR); + *++stack = result; + } + break; + } + case 0x60: { /* getlex */ + as3v name = pool[OPU16(2)]; + as3v scope = (as3v)&frame[MF_ARGS]; + as3v result = as3vm_get_property_scoped_closure_GC(vm, scope, name); + pc += 4; + if (result == HOLE) { + record_error_info(vm, name, (as3v)"looking for $"); + goto err76; + } + result = lazy_init_if_class(vm, result); + *++stack = result; + break; + } + case 0x61: { /* setproperty */ + as3v name = pool[OPU16(2)]; + as3v value = *stack; + as3v recv = stack[-1]; + pc += 4; + stack -= 1; + if (TAG(name) == 2 && *(as3v *)(name - 2) == 0 && *(as3v *)(name + 2) != 2 && + TAG(recv) == 1) { + if (TYPEB(recv) == '!') { + as3v idx = *stack; + if (TAG(idx) == 1 && idx != HOLE && *(uint8_t *)(idx - 1) > 0xd) { + as3vm_proplist_set_GC(vm, recv, idx, value); + stack -= 2; + break; + } + } else if (TYPEB(recv) == ' ') { + as3v idx = *stack; + int32_t i = 0; + int have_index = 0; + if ((*(uint8_t *)(recv + 1) & 1) != 0) { + value = as3vm_coerce_GC(vm, value, *(as3v *)(*(as3v *)(*(as3v *)(recv + STR_LEN) + O_STARTCLASS) + CLS_PARAM_TYPE)); + if (value == 7) goto err78; + idx = *stack; + } + if ((idx & 1) == 0) { + i = (int32_t)((intptr_t)idx >> 1); + have_index = 1; + } else if (TAG(idx) == 1 && idx != HOLE && (idx & 3) != 3 && TYPEB(idx) == 7 && + number_is_array_index(idx)) { + i = (int32_t)*(double *)(idx + STR_LEN); + have_index = 1; + } else if (TAG(idx) == 1 && idx != HOLE && TYPEB(idx) == 8) { + uint32_t out = 0; + if (as3vm_StringToArrayIndex((void *)(*(as3v *)(idx + STR_LEN)), &out)) { + i = (int32_t)out; + have_index = 1; + } + } + if (have_index) { + as3vm_array_set_value_GC(vm, (void *)recv, i, (void *)value); + stack -= 2; + break; + } + } + } + GC_PUSH(vm, value); + { + as3v *sp2 = stack; + if (TAG(name) == 2) { + sp2 = name_resolve_runtime_GC(vm, &name, stack, 1); + if (sp2 == NULL) goto err78; + } + stack = sp2; + GC_POP(vm); + value = *(as3v *)GC_TOP(vm); + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + { + as3v tgt = *stack; + if (tgt == VUNDEF || tgt == VNULL) { + record_error_info(vm, name, + tgt == VNULL + ? (as3v) "can't set field on null, setting $" + : (as3v) "setting field on type that doesn't allow setting fields, setting $"); + goto err78; + } + as3vm_set_property_name_GC(vm, tgt, name, value, 0); + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + stack -= 1; + } + } + break; + } + case 0x62: /* getlocal */ + *++stack = locals[OPU16(2)]; + pc += 4; + break; + case 0x63: /* setlocal */ + locals[OPU16(2)] = *stack; + stack -= 1; + pc += 4; + break; + case 0x64: { /* getglobalscope */ + as3v base0 = ((as3v *)frame[MF_ARGS])[-1]; + as3v result = (*(as3v *)(base0 + 7) == 0) ? *(as3v *)frame[MF_ARGS] : *(as3v *)(base0 + O_CLASSWORD); + *++stack = result; + pc += 2; + break; + } + case 0x65: /* getscopeobject */ + *++stack = *(as3v *)(frame[MF_ARGS] + (as3v)OPU16(2) * WORD); + pc += 4; + break; + case 0x66: { /* getproperty */ + as3v name = pool[OPU16(2)]; + pc += 4; + if (TAG(name) == 2) { + if (*(as3v *)(name - 2) == 0 && *(as3v *)(name + 2) != 2 && TAG(stack[-1]) == 1) { + as3v recv = stack[-1]; + if (TYPEB(recv) == '!') { + as3v idx = *stack; + if (TAG(idx) == 1 && idx != HOLE && *(uint8_t *)(idx - 1) > 0xd) { + as3v r = as3vm_proplist_get(vm, recv, idx); + stack[-1] = (r == 7) ? VNULL : r; + stack -= 1; + break; + } + } else if (TYPEB(recv) == ' ') { + as3v idx = *stack; + int32_t i = 0; + int have_index = 0; + if ((idx & 1) == 0) { + i = (int32_t)((intptr_t)idx >> 1); + have_index = 1; + } else if (TAG(idx) == 1 && idx != HOLE && (idx & 3) != 3 && TYPEB(idx) == 7 && + number_is_array_index(idx)) { + i = (int32_t)*(double *)(idx + STR_LEN); + have_index = 1; + } else if (TAG(idx) == 1 && idx != HOLE && TYPEB(idx) == 8) { + uint32_t out = 0; + if (as3vm_StringToArrayIndex((void *)(*(as3v *)(idx + STR_LEN)), &out)) { + i = (int32_t)out; + have_index = 1; + } + } + if (have_index && i >= 0 && i < *(int32_t *)(*(as3v *)(stack[-1] + O_ARRDATA) + 7)) { + as3v r = *(as3v *)(*(as3v *)(stack[-1] + O_ARRDATA) + (as3v)i * WORD + O_CLASSWORD); + stack[-1] = (r == 7) ? VNULL : r; + stack -= 1; + break; + } + if (have_index) { + stack[-1] = VNULL; + stack -= 1; + break; + } + /* not a valid numeric-index shortcut; fall through to the + * general resolved-name lookup below. */ + } + } + { + as3v *sp2 = name_resolve_runtime_GC(vm, &name, stack, 0); + if (sp2 == NULL) goto err78; + stack = sp2; + } + } else { + /* compile-time qname (raw/vita:5086-5223): the real interpreter + * probes a per-vm lookup-cache keyed by a lazily-assigned identity + * hash on the receiver before falling back to the general lookup. + * The persistent cache table is a pure speed optimization + * consumed only by this opcode's cache-hit path in the original + * binary, so this reconstruction preserves the (observable) + * hash-priming side effect and the cacheable-name probe, then + * always continues through the always-correct general lookup + * below rather than reproduce the cache-slot bookkeeping. */ + as3v recv0 = *stack; + if (TAG(recv0) == 1 && recv0 != HOLE && TYPEB(recv0) > 0x1f) { + if (*(uint32_t *)(recv0 + LCACHE_HASH_OFF) == 0) { + uint32_t h = (uint32_t)iggy_random(); + if (h == 0) h = (uint32_t)recv0; + *(uint32_t *)(recv0 + LCACHE_HASH_OFF) = h; + } + (void)get_property_name_cacheable(vm, name); + } + } + { + as3v recv = *stack; + if (recv == VUNDEF || recv == VNULL) { + record_error_info(vm, name, (as3v)"getting field $"); + goto err78; + } + { + as3v result = as3vm_get_property_name_closure_GC(vm, recv, name); + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + *stack = (result == HOLE) ? VNULL : lazy_init_if_class(vm, result); + } + } + break; + } + case 0x68: { /* initproperty */ + as3v name = pool[OPU16(2)]; + as3v value = *stack; + pc += 4; + GC_PUSH(vm, value); + stack -= 1; + { + as3v *sp2 = stack; + if (TAG(name) == 2) { + sp2 = name_resolve_runtime_GC(vm, &name, stack, 1); + if (sp2 == NULL) goto err78; + } + stack = sp2; + GC_POP(vm); + value = *(as3v *)GC_TOP(vm); + as3vm_set_property_name_GC(vm, *stack, name, value, 1); + if (VMF(vm, VM_PENDING_EXC) != 0) goto unwind_no_raise; + stack -= 1; + } + break; + } + case 0x6a: { /* deleteproperty */ + as3v name = pool[OPU16(2)]; + int handled = 0; + pc += 4; + if (TAG(name) == 2 && *(as3v *)(name - 2) == 0 && *(as3v *)(name + 2) != 2) { + as3v recv = stack[-1]; + as3v idx = *stack; + if (TAG(recv) == 1 && TYPEB(recv) == '!') { + if (TAG(idx) == 1 && idx != HOLE && *(uint8_t *)(idx - 1) >= 0xe) { + int deleted = (int)as3vm_proplist_delete_GC(vm, recv, idx); + stack[-1] = deleted ? VTRUE : VFALSE; + stack -= 1; + handled = 1; + } + } else { + int ok = 1; + if ((idx & 1) != 0) { + if ((idx & 3) == 3 || TAG(idx) != 1 || TYPEB(idx) != 7 || !number_is_array_index(idx)) { + ok = 0; + } + } + if (ok && TAG(recv) == 1 && TYPEB(recv) == ' ') { + int32_t i = ((idx & 1) == 0) ? (int32_t)((intptr_t)idx >> 1) + : (int32_t)*(double *)(idx + STR_LEN); + int deleted = (int)as3vm_array_delete_value_GC(vm, (void *)recv, i); + stack[-1] = deleted ? VTRUE : VFALSE; + stack -= 1; + handled = 1; + } + } + } + if (!handled) { + as3v *sp2 = stack; + if (TAG(name) == 2) { + sp2 = name_resolve_runtime_GC(vm, &name, stack, 1); + if (sp2 == NULL) goto err78; + } + stack = sp2; + { + as3v recv = *stack; + if (recv == VUNDEF || recv == VNULL) { + record_error_info(vm, name, (as3v)"non-object had attempted deletion of $"); + goto err76; + } + if (TAG(recv) == 1 && TYPEB(recv) > 0x13) { + *stack = as3vm_object_delete_property_GC(vm, recv, name) ? VTRUE : VFALSE; + } else { + *stack = VFALSE; + } + } + } + break; + } + case 0x6c: { /* getslot */ + as3v obj = *stack; + pc += 4; + if (obj == VUNDEF || obj == VNULL) goto err78; + if (TAG(obj) != 1 || TYPEB(obj) < 0x20 || + *(uint16_t *)(*(as3v *)(obj + STR_LEN) + SLOT_COUNT) <= OPU16(2)) { + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + *stack = as3vm_avm2_slot_get_GC(vm, (void *)(obj - 1), OPU16(2)); + break; + } + case 0x6d: { /* setslot */ + as3v obj = stack[-1]; + pc += 4; + if (TAG(obj) != 1 || TYPEB(obj) < 0x20) goto err78; + if (*(uint16_t *)(*(as3v *)(obj + STR_LEN) + SLOT_COUNT) <= OPU16(2)) { + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + as3vm_slot_set_GC(vm, (void *)obj, OPU16(2), (void *)*stack, 0); + stack -= 2; + break; + } + case 0x6e: { /* getglobalslot */ + as3v base0 = ((as3v *)frame[MF_ARGS])[-1]; + as3v holder = (*(as3v *)(base0 + 7) == 0) ? *(as3v *)frame[MF_ARGS] : *(as3v *)(base0 + O_CLASSWORD); + pc += 4; + if (*(uint16_t *)(*(as3v *)(holder + STR_LEN) + SLOT_COUNT) <= OPU16(2)) { + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + *++stack = as3vm_avm2_slot_get_GC(vm, (void *)(holder - 1), OPU16(2)); + break; + } + case 0x6f: { /* setglobalslot */ + as3v holder = *(as3v *)frame[MF_ARGS]; + pc += 4; + if (*(uint16_t *)(*(as3v *)(holder + STR_LEN) + SLOT_COUNT) <= OPU16(2)) { + as3vm_exception_GC(vm, 0x79); + goto already_raised; + } + as3vm_slot_set_GC(vm, (void *)holder, OPU16(2), (void *)*stack, 0); + stack -= 1; + break; + } + case 0x70: { /* convert_s */ + as3v r = as3vm_ToString_GC(vm, *stack); + pc += 2; + *stack = r; + if (r == 7) ERRMSG("converting to String"); + break; + } + case 0x71: /* esc_xelem (unsupported, pass through) */ + case 0x72: /* esc_xattr (unsupported, pass through) */ + pc += 2; + break; + case 0x73: { /* convert_i */ + int32_t i32; + pc += 2; + if (!as3vm_ToInt32_GC(vm, *stack, &i32)) ERRMSG("converting to Int"); + *stack = as3vm_make_int_GC(vm, i32); + break; + } + case 0x74: { /* convert_u */ + uint32_t u32; + pc += 2; + if (!as3vm_ToUInt32_GC(vm, *stack, &u32)) ERRMSG("converting to UInt"); + *stack = as3vm_make_uint_GC(vm, u32); + break; + } + case 0x75: { /* convert_d */ + as3v r = as3vm_ToNumber_GC(vm, *stack); + pc += 2; + *stack = r; + if (r == 7) ERRMSG("converting to Number"); + break; + } + case 0x76: /* convert_b */ + *stack = as3vm_to_boolean(*stack); + pc += 2; + break; + case 0x77: /* convert_o */ + pc += 2; + if (*stack == VUNDEF || *stack == VNULL) ERRMSG("converting to Object"); + break; + case 0x78: /* checkfilter */ + pc += 2; + if (TAG(*stack) != 1 || *(uint8_t *)(*stack - 1) < 0x28) goto err78; + break; + case 0x82: /* coerce_a */ + pc += 2; + break; + case 0x85: { /* coerce_s */ + as3v v = *stack; + pc += 2; + if (v == VNULL || v == VUNDEF) { + *stack = VNULL; + } else { + as3v r = as3vm_ToString_GC(vm, v); + *stack = r; + if (r == 7) ERRMSG("converting to String"); + } + break; + } + case 0x86: { /* astype */ + as3v classval; + as3v result; + if (OPS16(2) != 6) { + as3v name = pool[OPU16(4)]; + as3v scope = (as3v)&frame[MF_ARGS]; + classval = as3vm_get_property_scoped_closure_GC(vm, scope, name); + if (classval == HOLE) { + record_error_info(vm, name, (as3v)"couldn't find classname $"); + goto err76; + } + classval = lazy_init_if_class(vm, classval); + if (TAG(classval) != 1 || TYPEB(classval) != '&') { + record_error_info(vm, name, (as3v)"got a non-class for $"); + goto err76; + } + } else { + classval = pool[OPU16(4)]; + } + pc += 6; + result = is_oftype(vm, *stack, classval) ? *stack : VNULL; + *stack = result; + break; + } + case 0x87: { /* astypelate */ + as3v classval = *stack; + int ok; + if (TAG(classval) != 1 || TYPEB(classval) != '&') ERRMSG("'as' value was not a class"); + ok = (int)is_oftype(vm, stack[-1], *(as3v *)(*(as3v *)(classval + STR_LEN) + O_STARTCLASS)); + stack[-1] = ok ? stack[-1] : VNULL; + stack -= 1; + pc += 2; + break; + } + case 0x90: { /* negate */ + as3v v = *stack; + pc += 2; + if ((v & 1) == 0) { + if ((as3v)(-(intptr_t)v) == v) { + *stack = as3vm_make_number_not_int_GC(vm, -(double)((intptr_t)v >> 1)); + } else { + *stack = (as3v)(-(intptr_t)v); + } + } else { + double d; + if (!as3vm_ToNumberDirect_GC(vm, v, &d)) ERRMSG("in '-' (negation)"); + *stack = as3vm_make_number_not_int_GC(vm, -d); + } + break; + } + case 0x91: { /* increment */ + double d; + pc += 2; + if (!as3vm_ToNumberDirect_GC(vm, *stack, &d)) ERRMSG("increment"); + *stack = as3vm_make_number_GC(vm, (as3v)(d + 1.0)); + break; + } + case 0x92: { /* inclocal */ + unsigned reg = OPU16(2); + double d; + pc += 4; + if (!as3vm_ToNumberDirect_GC(vm, locals[reg], &d)) ERRMSG("increment"); + locals[reg] = as3vm_make_number_GC(vm, (as3v)(d + 1.0)); + break; + } + case 0x93: { /* decrement */ + double d; + pc += 2; + if (!as3vm_ToNumberDirect_GC(vm, *stack, &d)) ERRMSG("decrement"); + *stack = as3vm_make_number_GC(vm, (as3v)(d - 1.0)); + break; + } + case 0x94: { /* declocal */ + unsigned reg = OPU16(2); + double d; + pc += 4; + if (!as3vm_ToNumberDirect_GC(vm, locals[reg], &d)) ERRMSG("decrement"); + locals[reg] = as3vm_make_number_GC(vm, (as3v)(d - 1.0)); + break; + } + case 0x95: { /* typeof */ + /* Each result reads a per-vm typeof-string cache slot; slot k lives + * at TYPEOF_STR_BASE + k*WORD (vita 0x928+k*4 raw:5940-5975; durango + * 0xdd8+k*8, both objects). The int / case-4 / case-7 arms read slots + * 3 / 1 / 3 -- NOT VM_NUMBER_CLASS: the earlier "retail quirk + * substituting VM_NUMBER_CLASS" was a reconstruction misread (vita + * raw reads vm+0x934 and vm+0x92c, durango reads vm+0xdf0 and vm+0xde0, + * never the number class at vm+0x2840). */ + as3v v = *stack; + pc += 2; + if ((v & 1) == 0) { + *stack = TYPEOF_STR(3); + } else if ((v & 3) == 3) { + *stack = TYPEOF_STR(0); + } else { + switch (*(uint8_t *)(v - 1)) { + case 4: *stack = TYPEOF_STR(1); break; + case 5: *stack = TYPEOF_STR(6); break; + case 6: *stack = TYPEOF_STR(2); break; + case 7: *stack = TYPEOF_STR(3); break; + case 8: *stack = TYPEOF_STR(4); break; + case 0x10: case 0x12: case 0x14: *stack = TYPEOF_STR(5); break; + default: *stack = TYPEOF_STR(6); break; + } + } + break; + } + case 0x96: { /* not */ + as3v b = as3vm_to_boolean(*stack); + *stack = (b == VTRUE) ? VFALSE : VTRUE; + pc += 2; + break; + } + case 0x97: { /* bitnot */ + int32_t i32; + pc += 2; + if (!as3vm_ToInt32_GC(vm, *stack, &i32)) ERRMSG("in '~' (bitwise not)"); + *stack = as3vm_make_int_GC(vm, ~i32); + break; + } + case 0xa0: { /* add */ + as3v b = *stack, a = stack[-1]; + pc += 2; + if (((a | b) & 1) == 0) { + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, (int32_t)((intptr_t)a >> 1) + (int32_t)((intptr_t)b >> 1)); + } else if ((((a & 1) == 0) || (TAG(a) == 1 && TYPEB(a) == 7)) && + (((b & 1) == 0) || (TAG(b) == 1 && TYPEB(b) == 7))) { + double da = ((a & 1) == 0) ? (double)((intptr_t)a >> 1) : *(double *)(a + STR_LEN); + double db = ((b & 1) == 0) ? (double)((intptr_t)b >> 1) : *(double *)(b + STR_LEN); + stack -= 1; + stack[0] = as3vm_make_number_not_int_GC(vm, da + db); + } else { + int isSpecial = + (TAG(a) == 1 && TYPEB(a) > 0x22 && + *(as3v *)(*(as3v *)(a + STR_LEN) + O_STARTCLASS) == VMF(vm, VM_CLASS_CLASS)) || + (TAG(b) == 1 && TYPEB(b) > 0x22 && + *(as3v *)(*(as3v *)(b + STR_LEN) + O_STARTCLASS) == VMF(vm, VM_CLASS_CLASS)) || + (TAG(a) == 1 && TYPEB(a) == 8) || (TAG(b) == 1 && TYPEB(b) == 8); + if (isSpecial) { + as3v r = as3vm_string_concatenate_GC(vm, b, a); + stack -= 1; + if (r == 7) ERRMSG("in '+' as string concatenation"); + stack[0] = r; + } else if ((a & b & 1) == 0 || (*(uint8_t *)(a - 1) < 0x28 || *(uint8_t *)(b - 1) < 0x28)) { + if (TAG(a) == 1 && TYPEB(a) > 0x1f) { + a = as3vm_DefaultValue_None_GC(vm, a); + } + if (TAG(b) == 1 && TYPEB(b) > 0x1f) { + b = as3vm_DefaultValue_None_GC(vm, b); + } + if ((TAG(a) == 1 && TYPEB(a) == 8) || (TAG(b) == 1 && TYPEB(b) == 8)) { + as3v r = as3vm_string_concatenate_GC(vm, b, a); + stack -= 1; + if (r == 7) ERRMSG("in '+' as string concatenation"); + stack[0] = r; + } else { + double da, db; + if (!as3vm_ToNumberDirect_GC(vm, a, &da) || !as3vm_ToNumberDirect_GC(vm, b, &db)) { + ERRMSG("in '+' as addition on numbers"); + } + stack -= 1; + stack[0] = as3vm_make_number_GC(vm, (as3v)(da + db)); + } + } else { + stack -= 1; + } + } + break; + } + case 0xa1: { /* subtract */ + as3v b = *stack, a = stack[-1]; + pc += 2; + if (((a | b) & 1) == 0) { + stack -= 1; + stack[0] = as3vm_make_number_GC(vm, (as3v)(double)((int32_t)((intptr_t)a >> 1) - (int32_t)((intptr_t)b >> 1))); + } else { + double da, db; + if (!as3vm_ToNumberDirect_GC(vm, b, &da) || !as3vm_ToNumberDirect_GC(vm, a, &db)) { + ERRMSG("in '-' (subtraction)"); + } + stack -= 1; + stack[0] = as3vm_make_number_not_int_GC(vm, db - da); + } + break; + } + case 0xa2: { /* multiply */ + as3v b = *stack, a = stack[-1]; + pc += 2; + if (((a | b) & 1) == 0) { + stack -= 1; + stack[0] = as3vm_make_number_GC(vm, (as3v)(double)((int32_t)((intptr_t)b >> 1) * (int32_t)((intptr_t)a >> 1))); + } else { + double da, db; + if (!as3vm_ToNumberDirect_GC(vm, b, &da) || !as3vm_ToNumberDirect_GC(vm, a, &db)) { + ERRMSG("in '*' (multiplication)"); + } + stack -= 1; + stack[0] = as3vm_make_number_not_int_GC(vm, db * da); + } + break; + } + case 0xa3: { /* divide */ + double da, db; + pc += 2; + if (!as3vm_ToNumberDirect_GC(vm, stack[-1], &da) || !as3vm_ToNumberDirect_GC(vm, *stack, &db)) { + ERRMSG("in '/' (divide)"); + } + stack -= 1; + stack[0] = as3vm_make_number_GC(vm, (as3v)(da / db)); + break; + } + case 0xa4: { /* modulo */ + as3v b = *stack, a = stack[-1]; + pc += 2; + if (((a | b) & 1) == 0) { + int32_t ia = (int32_t)((intptr_t)a >> 1), ib = (int32_t)((intptr_t)b >> 1); + if (ib == 0) { + stack[-1] = VNAN; + } else { + stack[-1] = (as3v)(IGGY_IMOD(ia, ib) << 1); + } + } else { + double da, db; + if (!as3vm_ToNumberDirect_GC(vm, b, &da) || !as3vm_ToNumberDirect_GC(vm, a, &db)) { + ERRMSG("in '%' (modulus)"); + } + if (da == 0.0) { + stack[-1] = VNAN; + } else { + double r = fmod(db, da); + stack[-1] = as3vm_make_number_not_int_GC(vm, r); + } + } + stack -= 1; + break; + } + case 0xa5: { /* lshift */ + as3v b = *stack, a = stack[-1]; + pc += 2; + if (((b | a) & 1) == 0) { + int32_t ia = (int32_t)((intptr_t)a >> 1); + int32_t shift = ((int32_t)b << 0x1a) >> 0x1b; + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ia << (shift & 0x1f)); + } else { + int32_t ia, ib; + if (!as3vm_ToInt32_GC(vm, a, &ia) || !as3vm_ToInt32_GC(vm, b, &ib)) { + ERRMSG("in '<<' (left-shift)"); + } + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ia << (ib & 0x1f)); + } + break; + } + case 0xa6: { /* rshift */ + int32_t ia, ib; + pc += 2; + if (!as3vm_ToInt32_GC(vm, stack[-1], &ia) || !as3vm_ToInt32_GC(vm, *stack, &ib)) { + ERRMSG("in '>>' (right shift)"); + } + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ia >> (ib & 0x1f)); + break; + } + case 0xa7: { /* urshift */ + int32_t ia; + uint32_t ib; + pc += 2; + if (!as3vm_ToInt32_GC(vm, stack[-1], &ia) || !as3vm_ToUInt32_GC(vm, *stack, &ib)) { + ERRMSG("in '>>' (right shift)"); + } + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, (int32_t)((uint32_t)ia >> (ib & 0x1f))); + break; + } + case 0xa8: { /* bitand */ + int32_t ia, ib; + pc += 2; + if (!as3vm_ToInt32_GC(vm, *stack, &ia) || !as3vm_ToInt32_GC(vm, stack[-1], &ib)) { + ERRMSG("in '&' (bitwise and)"); + } + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ia & ib); + break; + } + case 0xa9: { /* bitor */ + int32_t ia, ib; + pc += 2; + if (!as3vm_ToInt32_GC(vm, *stack, &ia) || !as3vm_ToInt32_GC(vm, stack[-1], &ib)) { + ERRMSG("in '|' (bitwise or)"); + } + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ia | ib); + break; + } + case 0xaa: { /* bitxor */ + int32_t ia, ib; + pc += 2; + if (!as3vm_ToInt32_GC(vm, *stack, &ia) || !as3vm_ToInt32_GC(vm, stack[-1], &ib)) { + ERRMSG("in '^' (bitwise xor)"); + } + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ia ^ ib); + break; + } + case 0xab: { /* equals */ + as3v r = abstract_equality(vm, stack[-1], *stack); + pc += 2; + stack -= 1; + if (r == HOLE) ERRMSG("in '=='"); + stack[0] = r; + break; + } + case 0xac: /* strictequals */ + stack[-1] = strict_equality(*stack, stack[-1]); + stack -= 1; + pc += 2; + break; + case 0xad: { /* lessthan */ + as3v r = abstract_relational(vm, stack[-1], *stack); + stack -= 1; + stack[0] = (r == VTRUE) ? VTRUE : VFALSE; + pc += 2; + break; + } + case 0xae: { /* lessequals */ + as3v r = abstract_relational(vm, *stack, stack[-1]); + stack -= 1; + stack[0] = (r == VFALSE) ? VTRUE : VFALSE; + pc += 2; + break; + } + case 0xaf: { /* greaterthan */ + as3v r = abstract_relational(vm, *stack, stack[-1]); + stack -= 1; + stack[0] = (r == VTRUE) ? VTRUE : VFALSE; + pc += 2; + break; + } + case 0xb0: { /* greaterequals */ + as3v r = abstract_relational(vm, stack[-1], *stack); + stack -= 1; + stack[0] = (r == VFALSE) ? VTRUE : VFALSE; + pc += 2; + break; + } + case 0xb1: { /* instanceof */ + as3v classval = *stack; + pc += 2; + if (TAG(classval) != 1 || TYPEB(classval) != '&') ERRMSG("not class in 'instanceof'"); + { + as3v startclass = *(as3v *)(*(as3v *)(classval + STR_LEN) + O_STARTCLASS); + char tag = *(char *)(startclass + TYPE_TAG_OFF); + stack -= 1; + if (tag == 9 || tag == 0xa) { + stack[0] = VFALSE; + } else { + stack[0] = is_oftype(vm, stack[0], startclass) ? VTRUE : VFALSE; + } + } + break; + } + case 0xb2: { /* istype */ + as3v classval; + if (OPS16(2) != 6) { + as3v name = pool[OPU16(4)]; + as3v scope = (as3v)&frame[MF_ARGS]; + classval = as3vm_get_property_scoped_closure_GC(vm, scope, name); + if (classval == HOLE) { + record_error_info(vm, name, (as3v)"istype couldn't find classname $"); + goto err76; + } + classval = lazy_init_if_class(vm, classval); + if (TAG(classval) != 1 || TYPEB(classval) != '&') { + record_error_info(vm, name, (as3v)"istype got non-class for $"); + goto err76; + } + classval = *(as3v *)(*(as3v *)(classval + STR_LEN) + O_STARTCLASS); + } else { + classval = pool[OPU16(4)]; + } + pc += 6; + *stack = is_oftype(vm, *stack, classval) ? VTRUE : VFALSE; + break; + } + case 0xb3: { /* istypelate */ + as3v classval = *stack; + pc += 2; + if (TAG(classval) != 1 || TYPEB(classval) != '&') ERRMSG("non-class in 'is' of dynamic type"); + { + int ok = (int)is_oftype(vm, stack[-1], *(as3v *)(*(as3v *)(classval + STR_LEN) + O_STARTCLASS)); + stack -= 1; + stack[0] = ok ? VTRUE : VFALSE; + } + break; + } + case 0xb4: { /* in */ + uint8_t scratch[128]; + as3v strv = stack[-1]; + int len = 0; + void *chars; + pc += 2; + chars = as3vm_ToCharArray_GC(vm, strv, scratch, &len); + if (chars == NULL) ERRMSG("non-String used with 'in'"); + { + intptr_t interned = as3vm_find_intern_string(vm, len, chars, 0); + if (interned == 0) { + stack[-1] = VFALSE; + } else { + as3v qtable = (as3v)as3vm_qname_find(vm, (void *)interned, (void *)VMF(vm, VM_ROOT_QNAMES), 0); + if (qtable == 0) { + stack[-1] = VFALSE; + } else { + as3v found = as3vm_get_property_name_flexible(vm, *stack, qtable); + stack[-1] = (found != 0) ? VTRUE : VFALSE; + } + } + } + stack -= 1; + break; + } + + case 0xc0: { /* increment_i */ + int32_t i32; + pc += 2; + if (!as3vm_ToInt32_GC(vm, *stack, &i32)) ERRMSG("increment"); + *stack = as3vm_make_integer_GC(vm, (int64_t)(i32 + 1)); + break; + } + case 0xc1: { /* decrement_i */ + int32_t i32; + pc += 2; + if (!as3vm_ToInt32_GC(vm, *stack, &i32)) ERRMSG("decrement"); + *stack = as3vm_make_integer_GC(vm, (int64_t)(i32 - 1)); + break; + } + case 0xc2: { /* inclocal_i */ + unsigned reg = OPU16(2); + int32_t i32; + pc += 4; + if (!as3vm_ToInt32_GC(vm, locals[reg], &i32)) ERRMSG("increment"); + locals[reg] = as3vm_make_integer_GC(vm, (int64_t)(i32 + 1)); + break; + } + case 0xc3: { /* declocal_i */ + unsigned reg = OPU16(2); + int32_t i32; + pc += 4; + if (!as3vm_ToInt32_GC(vm, locals[reg], &i32)) ERRMSG("decrement"); + locals[reg] = as3vm_make_integer_GC(vm, (int64_t)(i32 - 1)); + break; + } + case 0xc4: { /* negate_i */ + as3v v = *stack; + pc += 2; + if ((v & 1) == 0) { + if ((as3v)(-(intptr_t)v) == v) { + *stack = as3vm_make_int_GC(vm, -(int32_t)((intptr_t)v >> 1)); + } else { + *stack = (as3v)(-(intptr_t)v); + } + } else { + int32_t i32; + if (!as3vm_ToInt32_GC(vm, v, &i32)) ERRMSG("in '-' (negation)"); + if (i32 < -0x3fffffff || 0x3fffffff < i32) { + *stack = as3vm_make_int_GC(vm, -i32); + } else { + *stack = (as3v)(i32 * -2); + } + } + break; + } + case 0xc5: { /* add_i */ + as3v b = *stack, a = stack[-1]; + pc += 2; + if (((b | a) & 1) == 0) { + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, (int32_t)((intptr_t)b >> 1) + (int32_t)((intptr_t)a >> 1)); + } else if ((((b & 1) == 0) || (TAG(b) == 1 && TYPEB(b) == 7)) && + (((a & 1) == 0) || (TAG(a) == 1 && TYPEB(a) == 7))) { + double da = ((a & 1) == 0) ? (double)((intptr_t)a >> 1) : *(double *)(a + STR_LEN); + double db = ((b & 1) == 0) ? (double)((intptr_t)b >> 1) : *(double *)(b + STR_LEN); + int32_t ia = as3vm_NumberToInt32(da); + int32_t ib = as3vm_NumberToInt32(db); + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ib + ia); + } else { + int32_t ia, ib; + if (!as3vm_ToInt32_GC(vm, b, &ia) || !as3vm_ToInt32_GC(vm, a, &ib)) goto err78; + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ia + ib); + } + break; + } + case 0xc6: { /* subtract_i */ + as3v b = *stack, a = stack[-1]; + pc += 2; + if (((b | a) & 1) == 0) { + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, (int32_t)((intptr_t)a >> 1) - (int32_t)((intptr_t)b >> 1)); + } else if ((((b & 1) == 0) || (TAG(b) == 1 && TYPEB(b) == 7)) && + (((a & 1) == 0) || (TAG(a) == 1 && TYPEB(a) == 7))) { + double da = ((a & 1) == 0) ? (double)((intptr_t)a >> 1) : *(double *)(a + STR_LEN); + double db = ((b & 1) == 0) ? (double)((intptr_t)b >> 1) : *(double *)(b + STR_LEN); + int32_t ia = as3vm_NumberToInt32(da); + int32_t ib = as3vm_NumberToInt32(db); + stack -= 1; + stack[0] = as3vm_make_number_not_int_GC(vm, (double)(ia - ib)); + } else { + int32_t ia, ib; + if (!as3vm_ToInt32_GC(vm, b, &ia) || !as3vm_ToInt32_GC(vm, a, &ib)) ERRMSG("in '-' (subtraction)"); + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ib - ia); + } + break; + } + case 0xc7: { /* multiply_i */ + as3v b = *stack, a = stack[-1]; + pc += 2; + if (((b | a) & 1) == 0) { + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, (int32_t)((intptr_t)b >> 1) * (int32_t)((intptr_t)a >> 1)); + } else { + int32_t ia, ib; + if (!as3vm_ToInt32_GC(vm, b, &ia) || !as3vm_ToInt32_GC(vm, a, &ib)) ERRMSG("in '*' (multiplication)"); + stack -= 1; + stack[0] = as3vm_make_int_GC(vm, ib * ia); + } + break; + } + + case 0xd0: *++stack = locals[0]; pc += 2; break; /* getlocal0 */ + case 0xd1: *++stack = locals[1]; pc += 2; break; /* getlocal1 */ + case 0xd2: *++stack = locals[2]; pc += 2; break; /* getlocal2 */ + case 0xd3: *++stack = locals[3]; pc += 2; break; /* getlocal3 */ + case 0xd4: locals[0] = *stack; stack -= 1; pc += 2; break; /* setlocal0 */ + case 0xd5: locals[1] = *stack; stack -= 1; pc += 2; break; /* setlocal1 */ + case 0xd6: locals[2] = *stack; stack -= 1; pc += 2; break; /* setlocal2 */ + case 0xd7: locals[3] = *stack; stack -= 1; pc += 2; break; /* setlocal3 */ + + case 0xef: /* debug */ + pc += 2; + break; + case 0xf0: /* debugline */ + VMF(vm, VM_ERR_LOC) = pool[OPU16(2)]; + pc += 4; + break; + case 0xf1: /* debugfile */ + VMF(vm, VM_ERR_LINE) = (int32_t)pool[OPU16(2)]; + pc += 4; + break; + + default: + goto invalid_op; + } + goto dispatch; + + invalid_op: + raise_error(vm, 0x74); + goto after_raise; + err76: + raise_error(vm, 0x76); + goto after_raise; + err78: + raise_error(vm, 0x78); + goto after_raise; + + do_return_void: { + as3v sig = *(as3v *)minfo; + as3v rettype = *(as3v *)(sig + SIG_RETTYPE); + as3v result = VUNDEF; + if (rettype != 0) { + result = as3vm_coerce_GC(vm, VUNDEF, rettype); + if (result == 7) result = VUNDEF; + } + *(as3v *)frame[MF_RESULT] = result; + VMF(vm, VM_FRAME_CUR) = frame[MF_SAVED]; + return; + } + + already_raised: + unwind_no_raise: + if (VMF(vm, VM_ERR_LOC) == 0) { + as3v mctx = frame[MF_CTX]; + if (mctx != 0 && *(as3v *)mctx != 0) record_error_location(vm, (as3v *)mctx); + } + after_raise: { + as3v *savedp = (as3v *)frame[MF_SAVED]; + as3v *clr; + for (clr = (as3v *)frame[MF_LOCALS]; clr < savedp; ++clr) *clr = VNULL; + { + unsigned pcunits = (unsigned)(((as3v)pc - frame[MF_CODE]) >> 1); + unsigned count = *(uint16_t *)(minfo + EXC_COUNT); + unsigned i; + as3v target = 0; + int found = 0; + for (i = 0; i < count; ++i) { + as3v entry = *(as3v *)(minfo + EXC_ARRAY) + (as3v)i * EXC_STRIDE; + uint32_t efrom = *(uint32_t *)(entry + 0); + uint32_t eto = *(uint32_t *)(entry + 4); + if (pcunits >= efrom && pcunits <= eto) { + as3v etype = *(as3v *)(entry + EXC_TYPE); + if (etype == 0) { + found = 1; + target = *(as3v *)(entry + 8); + break; + } + { + as3v pending = VMF(vm, VM_PENDING_EXC); + if (TAG(pending) == 1 && TYPEB(pending) > 0x1f) { + if (as3vm_is_ofclass((void *)(pending - 1), (void *)etype)) { + found = 1; + target = *(as3v *)(entry + 8); + break; + } + } + } + } + } + if (!found) { + VMF(vm, VM_FRAME_CUR) = frame[MF_SAVED]; + return; + } + pc = (uint8_t *)(frame[MF_CODE] + (as3v)target * 2); + stack = (as3v *)frame[MF_LOCALS]; + frame[MF_ARGS2] = frame[MF_ARGS]; + *stack = VMF(vm, VM_PENDING_EXC); + VMF(vm, VM_PENDING_EXC) = 0; + goto dispatch; + } + } + +#undef OPB +#undef OPU16 +#undef OPS16 +#undef ERRMSG + } +} diff --git a/iggy-decompiled/src/reconstructed/as3vm_interpreter_equality.inc b/iggy-decompiled/src/reconstructed/as3vm_interpreter_equality.inc new file mode 100644 index 0000000..8288920 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/as3vm_interpreter_equality.inc @@ -0,0 +1,261 @@ +/* as3vm_interpreter_equality.inc -- ECMAScript abstract / strict equality and + * relational comparison, plus the UTF-16 string-data comparators they share. + * Internal file-statics driven by the opcode-dispatch loop. Included by + * as3vm_interpreter.c. */ + +/* Boolean-result sentinels (this module's own storage blobs). */ +#define VBOOL(x) ((x) ? VTRUE : VFALSE) + +/* UTF-16 stringdata comparison: 0x2129 (true) if equal, 0x2121 (false) + * otherwise. Length at stringdata+7, payload at stringdata+0xb. */ +static as3v string_data_compare(as3v a, as3v b) { + int len = *(int *)(a + STR_LEN); + if (len != *(int *)(b + STR_LEN)) { + return VFALSE; + } + { + const uint16_t *pa = (const uint16_t *)(a + STR_BYTES); + const uint16_t *pb = (const uint16_t *)(b + STR_BYTES); + for (int i = 0; i < len; ++i) { + if (pa[i] != pb[i]) { + return VFALSE; + } + } + } + return VTRUE; +} + +/* Raw stringdata equality (length at +8, payload at +0xc): 1 / 0. */ +static int string_eq(as3v a, as3v b) { + int len = *(int *)(a + 8); + if (len != *(int *)(b + 8)) { + return 0; + } + if (a != b && len > 0) { + const uint16_t *pa = (const uint16_t *)(a + 0xc); + const uint16_t *pb = (const uint16_t *)(b + 0xc); + for (int i = 0; i < len; ++i) { + if (pa[i] != pb[i]) { + return 0; + } + } + } + return 1; +} + +/* ECMAScript strict (===) equality. */ +static as3v strict_equality(as3v a, as3v b) { + int isnan_a; + double da, db; + uint8_t ta, tb; + + if (a == b && (TAG(a) != 1 || TYPEB(a) != '\a')) { + return VTRUE; + } + if ((a & 1) == 0 || (b & 1) == 0) { + if (((a | b) & 1) == 0) { + return VFALSE; + } + if ((a & 1) == 0) { + if (TAG(b) != 1 || TYPEB(b) != '\a' || + (isnan_a = rr_isnan(*(double *)(b + STR_LEN)), isnan_a != 0)) { + return VFALSE; + } + db = *(double *)(b + STR_LEN); + da = (double)((int64_t)a >> 1); + } else { + if (TAG(a) != 1 || TYPEB(a) != '\a' || + (isnan_a = rr_isnan(*(double *)(a + STR_LEN)), isnan_a != 0)) { + return VFALSE; + } + db = *(double *)(a + STR_LEN); + da = (double)((int64_t)b >> 1); + } + return VBOOL(!(rr_isnan(da) || rr_isnan(db)) && da == db); + } + if ((a & b & 1) == 0 || (ta = TYPEB(a), tb = TYPEB(b), ta != tb) || ta < 4) { + return VFALSE; + } + if (ta < 6) { + return VTRUE; + } + if (ta != 7) { + if (ta == 8) { + return string_data_compare(*(as3v *)(a + STR_LEN), *(as3v *)(b + STR_LEN)); + } + return VFALSE; + } + da = *(double *)(a + STR_LEN); + db = *(double *)(b + STR_LEN); + return VBOOL(!(rr_isnan(da) || rr_isnan(db)) && da == db); +} + +/* ECMAScript abstract (==) equality with ToNumber / DefaultValue coercions. */ +static as3v abstract_equality(void *vm, as3v a, as3v b) { + as3v base = (as3v)vm; + as3v zero = 0, two = 2; + double left = 0.0; + + if (a == b && (TAG(a) != 1 || TYPEB(a) != '\a')) { + return VTRUE; + } + for (;;) { + while ((a & 1) == 0 || (b & 1) == 0) { + if ((a & 1) != 0) { + unsigned t = a & 3; + double other = (double)((int64_t)b >> 1); + if (t == 1 && TYPEB(a) == '\a') { + if (rr_isnan(*(double *)(a + STR_LEN))) return VFALSE; + left = *(double *)(a + STR_LEN); + return VBOOL(!(rr_isnan(left) || rr_isnan(other)) && left == other); + } + if ((as3v)&as3vm_truefalse_storage[0] < a && + a < (as3v)&as3vm_truefalse_storage[10]) { + if (a == VTRUE) zero = two; + return (b != zero) ? VFALSE : VTRUE; + } + if (t != 1) return VFALSE; + if (TYPEB(a) != '\b') { + if (TYPEB(a) < 0x20) return VFALSE; + a = as3vm_DefaultValue_None_GC(vm, a); + if (a == HOLE) return HOLE; + continue; + } + if (as3vm_ToNumberDirect_GC(vm, a, &left) == 0) return HOLE; + if (rr_isnan(left)) return VFALSE; + return VBOOL(!(rr_isnan(left) || rr_isnan(other)) && left == other); + } + if ((b & 1) == 0) return VFALSE; + { + unsigned t = b & 3; + double other = (double)((int64_t)a >> 1); + if (t == 1 && TYPEB(b) == '\a') { + if (rr_isnan(*(double *)(b + STR_LEN))) return VFALSE; + left = *(double *)(b + STR_LEN); + return VBOOL(!(rr_isnan(left) || rr_isnan(other)) && left == other); + } + if ((as3v)&as3vm_truefalse_storage[0] < b && + b < (as3v)&as3vm_truefalse_storage[10]) { + if (b == VTRUE) zero = two; + return (a != zero) ? VFALSE : VTRUE; + } + if (t != 1) return VFALSE; + if (TYPEB(b) == '\b') { + if (as3vm_ToNumberDirect_GC(vm, b, &left) == 0) return HOLE; + if (rr_isnan(left)) return VFALSE; + return VBOOL(!(rr_isnan(left) || rr_isnan(other)) && left == other); + } + if (TYPEB(b) < 0x20) return VFALSE; + b = as3vm_DefaultValue_None_GC(vm, b); + if (b == HOLE) return HOLE; + } + } + if ((a & 7) == 7 || (b & 7) == 7) return VFALSE; + { + uint8_t ta = TYPEB(a), tb = TYPEB(b); + if (ta == tb) { + char t = (char)ta; + if (t == '\a') { + double x = *(double *)(a + STR_LEN), y = *(double *)(b + STR_LEN); + return VBOOL(!(rr_isnan(x) || rr_isnan(y)) && x == y); + } + if (a == b) return VTRUE; + if (t == '\b') { + return string_data_compare(*(as3v *)(a + STR_LEN), + *(as3v *)(b + STR_LEN)); + } + if (t == '\x06') return VFALSE; + if (t != '\x12') { + if (a == VUNDEF || a == VNULL) return VTRUE; + return VFALSE; + } + if (*(int64_t *)(a + STR_LEN) != *(int64_t *)(b + STR_LEN)) return VFALSE; + return VBOOL(*(int64_t *)(a + 0x17) == *(int64_t *)(b + 0x17)); + } + if ((a == VNULL && b == VUNDEF) || (b == VNULL && a == VUNDEF)) { + return VTRUE; + } + if (ta == 7 && tb == 8) { + left = *(double *)(a + STR_LEN); + } else if (tb == 7 && ta == 8) { + left = *(double *)(b + STR_LEN); + b = a; + } else if (ta == 6) { + a = (a == VTRUE) ? two : zero; + continue; + } else if (tb == 6) { + b = (b == VTRUE) ? two : zero; + continue; + } else if (TAG(b) == 1 && tb >= 0x20 && ta == 8) { + GC_PUSH(vm, a); + b = as3vm_DefaultValue_None_GC(vm, b); + GC_POP(vm); + a = *(as3v *)GC_TOP(vm); + if (b == HOLE) return HOLE; + continue; + } else if (TAG(a) == 1 && ta >= 0x20 && tb == 8) { + GC_PUSH(vm, b); + a = as3vm_DefaultValue_None_GC(vm, a); + GC_POP(vm); + b = *(as3v *)GC_TOP(vm); + if (a == HOLE) return HOLE; + continue; + } else { + return VFALSE; + } + { + double rhs; + if (as3vm_ToNumberDirect_GC(vm, b, &rhs) == 0) return HOLE; + return VBOOL(!(rr_isnan(left) || rr_isnan(rhs)) && left == rhs); + } + } + } + (void)base; +} + +/* ECMAScript abstract relational comparison (a < b): true / false / undefined. */ +static as3v abstract_relational(void *vm, as3v a, as3v b) { + double la, lb; + int i; + + if (TAG(a) == 1 && 0x1f < TYPEB(a)) { + GC_PUSH(vm, b); + a = as3vm_DefaultValue_Number_GC(vm, a); + GC_POP(vm); + b = *(as3v *)GC_TOP(vm); + } + if (TAG(b) == 1 && 0x1f < TYPEB(b)) { + GC_PUSH(vm, a); + b = as3vm_DefaultValue_Number_GC(vm, b); + GC_POP(vm); + a = *(as3v *)GC_TOP(vm); + } + if (TAG(a) != 1 || TYPEB(a) != '\b' || TAG(b) != 1 || TYPEB(b) != '\b') { + GC_PUSH(vm, b); + i = (int)as3vm_ToNumberDirect_GC(vm, a, &la); + GC_POP(vm); + if (i != 0) { + if (as3vm_ToNumberDirect_GC(vm, *(as3v *)GC_TOP(vm), &lb) != 0) { + if (rr_isnan(la) == 0 && rr_isnan(lb) == 0) { + return (lb <= la) ? VFALSE : VTRUE; + } + return VUNDEF; + } + } + return HOLE; + } + { + as3v sa = *(as3v *)(a + STR_LEN); + as3v sb = *(as3v *)(b + STR_LEN); + int la_len = *(int *)(sa + STR_LEN); + int lb_len = *(int *)(sb + STR_LEN); + const uint16_t *pa = (const uint16_t *)(sa + STR_BYTES); + const uint16_t *pb = (const uint16_t *)(sb + STR_BYTES); + for (i = 0; i < la_len && i < lb_len; ++i) { + if (pa[i] != pb[i]) { + return (pb[i] <= pa[i]) ? VFALSE : VTRUE; + } + } + return (lb_len <= la_len) ? VFALSE : VTRUE; + } +} diff --git a/iggy-decompiled/src/reconstructed/as3vm_interpreter_globals.inc b/iggy-decompiled/src/reconstructed/as3vm_interpreter_globals.inc new file mode 100644 index 0000000..9981047 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/as3vm_interpreter_globals.inc @@ -0,0 +1,545 @@ +/* as3vm_interpreter_globals.inc -- trivial VM state ops, boxed-number + * classification, slot read/write, string interning, coercion/cast dispatch, + * and the lookup-cache flush. Included by as3vm_interpreter.c. */ + +/* ------------------------------------------------------------------ */ +/* Debug / no-op trap hooks. */ +/* ------------------------------------------------------------------ */ + +void IggyDebugBreakOnAS3Exception(void) { return; } + +void iggy_monitor_variable(void) { return; } + +/* ------------------------------------------------------------------ */ +/* Trivial deterministic VM state ops. */ +/* ------------------------------------------------------------------ */ + +uint64_t as3vm_do_validate(void) { return 1; } + +void as3vm_interpreter_notice_gc(void) { return; } + +void as3vm_interpreter_request_collection(void) { return; } + +void as3vm_interpreter_initialize(void *vm) { + as3v base = (as3v)vm; + *(as3v *)(base + VM_FRAME_CUR) = *(as3v *)(base + VM_EXC_SRC); + *(as3v *)(base + VM_PENDING_EXC) = 0; + *(as3v *)(base + VM_CLEAR_A) = 0; + *(as3v *)(base + VM_CLEAR_B) = 0; + *(as3v *)(base + VM_GC_TOP) = *(as3v *)(base + VM_GC_BASE); +} + +void as3vm_finalize(void *vm, void *object) { + /* Dispatch through the object's class destructor slot. */ + as3v holder = *(as3v *)((as3v)object + O_CLASS); + as3v klass = *(as3v *)(holder + O_STARTCLASS); + void (*dtor)(void) = *(void (**)(void))(klass + WORD * 2); + (void)vm; + dtor(); +} + +/* ------------------------------------------------------------------ */ +/* Boxed-number classification (reads the double at box+8). */ +/* ------------------------------------------------------------------ */ + +uint64_t as3vm_is_nan(void *box) { + return rr_isnan(*(double *)((as3v)box + 8)) ? 1 : 0; +} + +uint64_t as3vm_is_inf(void *box) { + double d = *(double *)((as3v)box + 8); + return (d == as3vm_P_Infinity || d == as3vm_N_Infinity) ? 1 : 0; +} + +/* ------------------------------------------------------------------ */ +/* Class-chain membership. */ +/* ------------------------------------------------------------------ */ + +uint64_t as3vm_is_ofclass(void *object, void *klass) { + as3v holder = *(as3v *)((as3v)object + O_CLASS); + as3v start = *(as3v *)(holder + O_STARTCLASS); + as3v query = (as3v)klass; + + if (*(char *)(query + TYPE_TAG_OFF) == '*') { + /* '*' == any-type: walk the class chain testing the interface sets. */ + for (; start != 0; start = *(as3v *)(start + CLS_SUPER)) { + unsigned count = *(uint8_t *)(start + CLS_IFACE_CNT); + if (count != 0) { + as3v *ifaces = *(as3v **)(start + CLS_IFACE_PTR); + for (unsigned i = 0; i < count; ++i) { + if (ifaces[i] == query) { + return 1; + } + } + } + } + return 0; + } + for (; start != query; start = *(as3v *)(start + CLS_SUPER)) { + if (start == 0) { + return 0; + } + } + return 1; +} + +/* ------------------------------------------------------------------ */ +/* Slot read / write (with number boxing + old->young write barrier). */ +/* ------------------------------------------------------------------ */ + +uint64_t as3vm_slot_get_GC(void *vm, void *object, int slot) { + as3v klass = *(as3v *)((as3v)object + O_CLASS); + as3v foff = *(uint16_t *)(klass + CLS_SLOTBASE + (as3v)slot * CLS_SLOTSTRIDE); + if (*(as3v *)(klass + CLS_SLOTCLASS + (as3v)slot * CLS_SLOTSTRIDE) == + *(as3v *)((as3v)vm + VM_NUMBER_CLASS)) { + return as3vm_make_number_GC(vm, *(as3v *)((as3v)object + foff)); + } + return *(as3v *)((as3v)object + foff); +} + +uint64_t as3vm_avm2_slot_get_GC(void *vm, void *object, int slot) { + return as3vm_slot_get_GC(vm, object, slot); +} + +uint64_t as3vm_slot_set_GC(void *vm, void *object, int slot, void *value, + int init) { + as3v obj = (as3v)object; + as3v val = (as3v)value; + as3v klass = *(as3v *)(obj + STR_LEN); /* object header class ptr */ + as3v scls; + + if (*(char *)(klass + CLS_SLOTTYPE + (as3v)slot * CLS_SLOTSTRIDE) == '\x04' && + init == 0) { + return 0; + } + scls = *(as3v *)(klass + CLS_SLOTCLASS + (as3v)slot * CLS_SLOTSTRIDE); + if (scls == 0) { + if (TAG(val) == 1 && val != HOLE && + 0x17u < (MARKB(obj) & ~*(uint8_t *)val)) { + as3vm_write_barrier_slowpath(vm, (void *)(obj - 1), (void *)(val - 1)); + } + *(as3v *)(obj - 1 + + *(uint16_t *)(klass + CLS_SLOTBASE + (as3v)slot * CLS_SLOTSTRIDE)) = + val; + return 1; + } + if (scls == *(as3v *)((as3v)vm + VM_NUMBER_CLASS)) { + double num; + int ok; + GC_PUSH(vm, obj); + ok = (int)as3vm_ToNumberDirect_GC(vm, val, &num); + GC_POP(vm); + if (ok != 0) { + as3v self = *(as3v *)GC_TOP(vm); + *(double *)(self + + (*(uint16_t *)(*(as3v *)(self + STR_LEN) + CLS_SLOTBASE + + (as3v)slot * CLS_SLOTSTRIDE) - + 1)) = num; + return 1; + } + return 0; + } else { + as3v coerced; + as3v self; + GC_PUSH(vm, obj); + coerced = as3vm_coerce_GC( + vm, val, + *(as3v *)(klass + CLS_SLOTCLASS + (as3v)slot * CLS_SLOTSTRIDE)); + GC_POP(vm); + self = *(as3v *)GC_TOP(vm); + if (coerced != HOLE) { + if (TAG(coerced) == 1 && + 0x17u < (MARKB(self) & ~*(uint8_t *)coerced)) { + as3vm_write_barrier_slowpath(vm, (void *)(self - 1), + (void *)(coerced - 1)); + } + *(as3v *)(self + + (*(uint16_t *)(*(as3v *)(self + STR_LEN) + CLS_SLOTBASE + + (as3v)slot * CLS_SLOTSTRIDE) - + 1)) = coerced; + return 1; + } + return 0; + } +} + +uint64_t as3vm_avm2_slot_set_GC(void *vm, void *object, int slot, void *value, + int init) { + return as3vm_slot_set_GC(vm, object, slot, value, init); +} + +/* ------------------------------------------------------------------ */ +/* String interning. */ +/* ------------------------------------------------------------------ */ + +int64_t as3vm_intern_string_value(void *vm, int64_t value, uint32_t flags) { + as3v stringdata = *(as3v *)((as3v)value + STR_LEN); + if (*(char *)(stringdata + 1) == 'c') { + return (int64_t)(stringdata - INTERN_ADJ); + } else { + intptr_t interned = as3vm_find_intern_string( + vm, *(int *)(stringdata + STR_LEN), (void *)(stringdata + STR_BYTES), + flags); + if (interned != 0) { + *(as3v *)((as3v)value + STR_LEN) = (as3v)interned + INTERN_ADJ; + return interned; + } + return 0; + } +} + +int64_t as3vm_string_interned(void *vm, int64_t value) { + as3v stringdata; + intptr_t interned; + if (*(char *)((as3v)value + 1) == 'X') { + return value; + } + stringdata = *(as3v *)((as3v)value + STR_LEN); + if (*(char *)(stringdata + 1) == 'c') { + return (int64_t)(stringdata - INTERN_ADJ2); + } + interned = as3vm_find_intern_string(vm, *(int *)(stringdata + STR_LEN), + (void *)(stringdata + STR_BYTES), 1); + if (interned != 0) { + *(as3v *)((as3v)value + STR_LEN) = (as3v)interned + INTERN_ADJ; + return interned + 1; + } + return 7; +} + +int64_t as3vm_string_interned_nocreate(void *vm, int64_t value) { + as3v stringdata; + intptr_t interned; + if (*(char *)((as3v)value + 1) == 'X') { + return value; + } + stringdata = *(as3v *)((as3v)value + STR_LEN); + if (*(char *)(stringdata + 1) == 'c') { + return (int64_t)(stringdata - INTERN_ADJ2); + } + interned = as3vm_find_intern_string(vm, *(int *)(stringdata + STR_LEN), + (void *)(stringdata + STR_BYTES), 0); + if (interned != 0) { + *(as3v *)((as3v)value + STR_LEN) = (as3v)interned + INTERN_ADJ; + return interned + 1; + } + return 7; +} + +/* ------------------------------------------------------------------ */ +/* Type coercion / cast -- the type-byte dispatch (retail emits an */ +/* &__ImageBase-indexed jump table on durango; the vita build fully */ +/* expands it to the switch reconstructed here). */ +/* ------------------------------------------------------------------ */ + +int64_t as3vm_coerce_GC(void *vm, int64_t value, int64_t type) { + as3v v = (as3v)value; + int32_t i32; + uint32_t u32; + switch (*(char *)((as3v)type + TYPE_TAG_OFF)) { + case 0x06: + return (int64_t)as3vm_to_boolean(v); + case 0x07: + if (v == VUNDEF) return (int64_t)VNAN; + if (v == VNULL) return 0; + return (int64_t)as3vm_ToNumber_GC(vm, v); + case 0x08: + if (v == VUNDEF || v == VNULL) return (int64_t)VNULL; + return (int64_t)as3vm_ToString_GC(vm, v); + case 0x09: + if (v == VUNDEF || v == VNULL) return 0; + if ((v & 1) != 0) { + if (as3vm_ToInt32_GC(vm, v, &i32) == 0) return 7; + return (int64_t)as3vm_make_int_GC(vm, i32); + } + return value; + case 0x0a: + if (v == VUNDEF || v == VNULL) return 0; + /* Retail masks bit0 | sign-bit in one word-width test (orbis + * `param_2 & 0x8000000000000001`, vita `(param_2 & 1) | (int)param_2 < 0`). + * intptr_t picks bit 63 on 64-bit, bit 31 on 32-bit; a plain int64_t + * cast would zero-extend the 32-bit atom and never see the sign. */ + if ((v & 1) != 0 || (intptr_t)v < 0) { + if (as3vm_ToUInt32_GC(vm, v, &u32) == 0) return 7; + return (int64_t)as3vm_make_uint_GC(vm, u32); + } + return value; + case 0x0c: + if (TAG(v) != 1 || TYPEB(v) != 0x0c) { + return (int64_t)as3vm_ToString_GC(vm, v); + } + return value; + case 0x12: + case 0x14: + if (v != VNULL && v != VUNDEF && + (TAG(v) != 1 || (TYPEB(v) & 0x10) == 0)) { + return 7; + } + return (v == VNULL || v == VUNDEF) ? (int64_t)VNULL : value; + case 0x20: + if (TAG(v) == 1 && TYPEB(v) == 0x20) { + /* Vector element-type identity: word-width compare of the class's + * elem-type id (orbis `*(long*)(type+0x48)==*(long*)(v+7)`; vita + * `*(int*)(type+0x28)==*(int*)(v+7)`). */ + if (*(intptr_t *)((as3v)type + CLS_PARAM_TYPE) != + *(intptr_t *)(v + STR_LEN)) + return 7; + return value; + } + if (v == VUNDEF) return (int64_t)VNULL; + if (v == VNULL) return value; + return 7; + case 0x21: + case 0x23: + case 0x24: + case 0x26: + case 0x2a: + if (v == VUNDEF || v == VNULL) return (int64_t)VNULL; + if ((as3v)type != *(as3v *)((as3v)vm + VM_FUNC_CLASS) && + is_instance_of_user(vm, v, (as3v)type) == 0) { + return 7; + } + return value; + case 0x25: + return 7; + default: + if (TAG(v) != 1 || TYPEB(v) != *(uint8_t *)((as3v)type + TYPE_TAG_OFF)) { + return 7; + } + return value; + } +} + +int64_t as3vm_cast_GC(void *vm, int64_t value, int64_t type) { + as3v v = (as3v)value; + int32_t i32; + uint32_t u32; + switch (*(char *)((as3v)type + TYPE_TAG_OFF)) { + case 0x06: + return (int64_t)as3vm_to_boolean(v); + case 0x07: + if (v == VUNDEF) return (int64_t)VNAN; + if (v == VNULL) return 0; + return (int64_t)as3vm_ToNumber_GC(vm, v); + case 0x08: + if (v != VUNDEF && v != VNULL) { + return (int64_t)as3vm_ToString_GC(vm, v); + } + return value; + case 0x09: + if (v == VUNDEF || v == VNULL) return 0; + if ((v & 1) != 0) { + if (as3vm_ToInt32_GC(vm, v, &i32) == 0) return 7; + return (int64_t)as3vm_make_int_GC(vm, i32); + } + return value; + case 0x0a: + if (v == VUNDEF || v == VNULL) return 0; + /* Retail masks bit0 | sign-bit in one word-width test (orbis + * `param_2 & 0x8000000000000001`, vita `(param_2 & 1) | (int)param_2 < 0`). + * intptr_t picks bit 63 on 64-bit, bit 31 on 32-bit; a plain int64_t + * cast would zero-extend the 32-bit atom and never see the sign. */ + if ((v & 1) != 0 || (intptr_t)v < 0) { + if (as3vm_ToUInt32_GC(vm, v, &u32) == 0) return 7; + return (int64_t)as3vm_make_uint_GC(vm, u32); + } + return value; + case 0x0c: + if (TAG(v) != 1 || TYPEB(v) != 0x0c) { + return (int64_t)as3vm_ToString_GC(vm, v); + } + return value; + case 0x12: + case 0x14: + if (v != VNULL && v != VUNDEF && + (TAG(v) != 1 || (TYPEB(v) & 0x10) == 0)) { + return 7; + } + return (v == VNULL || v == VUNDEF) ? (int64_t)VNULL : value; + case 0x20: + if (TAG(v) == 1 && TYPEB(v) == 0x20) { + /* Vector element-type identity: word-width compare of the class's + * elem-type id (orbis `*(long*)(type+0x48)==*(long*)(v+7)`; vita + * `*(int*)(type+0x28)==*(int*)(v+7)`). */ + if (*(intptr_t *)((as3v)type + CLS_PARAM_TYPE) != + *(intptr_t *)(v + STR_LEN)) + return 7; + return value; + } + if (v == VUNDEF) return (int64_t)VNULL; + if (v == VNULL) return value; + return 7; + case 0x21: + case 0x23: + case 0x24: + case 0x26: + case 0x2a: + if (v == VUNDEF || v == VNULL) return (int64_t)VNULL; + if ((as3v)type != *(as3v *)((as3v)vm + VM_FUNC_CLASS) && + is_instance_of_user(vm, v, (as3v)type) == 0) { + return 7; + } + return value; + case 0x25: + return 7; + default: + if (TAG(v) != 1 || TYPEB(v) != *(uint8_t *)((as3v)type + TYPE_TAG_OFF)) { + return 7; + } + return value; + } +} + +/* is_oftype: the AS3 `is` predicate -- a type-byte switch (parallel to + * coerce/cast) yielding 1 / 0 without coercing. */ +static uint64_t is_oftype(void *vm, as3v value, as3v type) { + as3v v = value; + (void)vm; + switch (*(char *)(type + TYPE_TAG_OFF)) { + case 0x06: + return ((as3v)&as3vm_truefalse_storage[0] < v && + v < (as3v)&as3vm_truefalse_storage[10]) + ? 1 + : 0; + case 0x07: + return ((v & 1) == 0 || (TAG(v) == 1 && TYPEB(v) == '\a')) ? 1 : 0; + case 0x08: + return (TAG(v) == 1 && TYPEB(v) == '\b') ? 1 : 0; + case 0x09: + if ((v & 1) == 0) return 1; + if (TAG(v) == 1 && TYPEB(v) == '\a') { + double d = *(double *)(v + STR_LEN); + return (d == (double)(int)d) ? 1 : 0; + } + return 0; + case 0x0a: + /* even (non-inline-int) atom is a uint iff its sign bit is clear; + * word-width sign bit (orbis `param_1>>0x3f`, vita `(uint)v>>0x1f`). */ +#if IGGY_AS3_64BIT + if ((v & 1) == 0) return (v >> 63) ^ 1; +#else + if ((v & 1) == 0) return ((uint32_t)v >> 31) ^ 1; +#endif + if (TAG(v) == 1 && TYPEB(v) == '\a') { + double d = *(double *)(v + STR_LEN); + /* d is a uint iff it survives a round trip through its low 32 bits. + * orbis `(double)((long)d & 0xffffffff)`; vita lowers the [0,2^32) + * clamp as `(uint)(0.0 element-type identity (word-width compare; see coerce). */ + return (*(intptr_t *)(type + CLS_PARAM_TYPE) == + *(intptr_t *)(v + STR_LEN)) + ? 1 + : 0; + } + return 0; + case 0x26: + return (TAG(v) == 1 && TYPEB(v) == '&') ? 1 : 0; + case 0x21: + case 0x23: + case 0x24: + case 0x2a: + if (TAG(v) == 1 && 0x1f < TYPEB(v)) { + return as3vm_is_ofclass((void *)(v - 1), (void *)type); + } + return 0; + default: + return 0; + } +} + +/* is_instance_of_user: a class instance whose start class walks to the query + * class (via as3vm_is_ofclass) is an instance; other tagged values are not. */ +static uint64_t is_instance_of_user(void *vm, as3v value, as3v klass) { + if (TAG(value) == 1) { + uint8_t tb = TYPEB(value); + if (tb == 0x26 && klass == *(as3v *)((as3v)vm + VM_FUNC_CLASS)) { + return 1; + } + if (tb != 0x22 && tb >= 0x20 && tb < 0x25) { + return as3vm_is_ofclass((void *)(value - 1), (void *)klass); + } + } + return 0; +} + +/* ------------------------------------------------------------------ */ +/* Lookup-cache flush (single + dict form). */ +/* ------------------------------------------------------------------ */ + +#if IGGY_AS3_64BIT +#define LCACHE_HASH_OFF 0xf +#else +#define LCACHE_HASH_OFF 0xb +#endif + +void as3vm_flush_lcache(void *vm, int64_t value) { + as3v base = (as3v)vm; + as3v v = (as3v)value; + if (*(int *)(base + VM_LCACHE_ENABLE) != 0 && TAG(v) == 1 && + 0x13 < TYPEB(v) && *(uint32_t *)(v + LCACHE_HASH_OFF) != 0) { + as3v *slot = (as3v *)(base + VM_LCACHE_BASE + + (*(uint32_t *)(v + LCACHE_HASH_OFF) & 0x1f) * + VM_LCACHE_STRIDE); + int n = 0x10; + do { + if (*slot == v) { + *slot = 0; + } +#if IGGY_AS3_64BIT + slot += 3; +#else + slot += 4; +#endif + n -= 1; + } while (n != 0); + } +} + +void as3vm_flush_lcache_dict(void *vm, int64_t value) { + as3v base = (as3v)vm; + as3v v = (as3v)value; + if (*(int *)(base + VM_LCACHE_ENABLE) != 0 && TAG(v) == 1 && + 0x13 < TYPEB(v) && *(uint32_t *)(v + LCACHE_HASH_OFF) != 0) { +#if IGGY_AS3_64BIT + as3v *slot = (as3v *)(base + VM_LCACHE_DICT + + (*(uint32_t *)(v + LCACHE_HASH_OFF) & 0x1f) * + VM_LCACHE_STRIDE); + int n = 4; + do { + if (slot[-3] == v && *(int *)((as3v)slot - 4) != 0) slot[-3] = 0; + if (slot[0] == v && *(int *)((as3v)slot + 0x14) != 0) slot[0] = 0; + if (slot[3] == v && *(int *)((as3v)slot + 0x2c) != 0) slot[3] = 0; + if (slot[6] == v && *(int *)((as3v)slot + 0x44) != 0) slot[6] = 0; + slot += 0xc; + n -= 1; + } while (n != 0); +#else + uint32_t *slot = (uint32_t *)(base + VM_LCACHE_DICT + + (*(uint32_t *)(v + LCACHE_HASH_OFF) & 0x1f) * + VM_LCACHE_STRIDE); + int n = 0x10; + do { + if (*slot == (uint32_t)v && slot[3] != 0) { + *slot = 0; + } + slot += 4; + n -= 1; + } while (n != 0); +#endif + } +} diff --git a/iggy-decompiled/src/reconstructed/as3vm_interpreter_support.inc b/iggy-decompiled/src/reconstructed/as3vm_interpreter_support.inc new file mode 100644 index 0000000..4e7237a --- /dev/null +++ b/iggy-decompiled/src/reconstructed/as3vm_interpreter_support.inc @@ -0,0 +1,404 @@ +/* as3vm_interpreter_support.inc -- interpreter support helpers: exception raise, + * pending-error record population, class-script lookup + lazy init, scope-stack + * closure capture, runtime name resolution, string concatenation, and arraydata + * cloning. Included by as3vm_interpreter.c. */ + +/* ------------------------------------------------------------------ */ +/* Exception raise (public export) + error-record population helpers. */ +/* ------------------------------------------------------------------ */ + +void as3vm_exception_GC(void *vm, int code) { raise_error(vm, code); } + +static void record_error_location(void *vm, as3v *record) { + as3v base = (as3v)vm; + if (record != NULL) { + if (record[4] != 0) { + *(as3v *)(base + VM_ERR_LOC) = *(as3v *)(record[4] + WORD) + 0x34; + *(int32_t *)(base + VM_ERR_LINE) = + *(int32_t *)(*(as3v *)(record[4] + WORD) + 0x30); + return; + } + if (record[1] != 0) { + as3v *info = *(as3v **)(record[1] + WORD); + if (info != NULL && (info[0] == record[0] || info[1] == record[0])) { + *(as3v *)(base + VM_ERR_LOC) = *(as3v *)(info[0xc] + WORD) + 0x34; + *(int32_t *)(base + VM_ERR_LINE) = + *(int32_t *)(*(as3v *)(info[0xc] + WORD) + 0x30); + return; + } + } + } + *(int32_t *)(base + VM_ERR_LINE) = 3; + *(as3v *)(base + VM_ERR_LOC) = 0; +} + +static void record_error_function(void *vm, as3v value) { + as3v base = (as3v)vm; + if (TAG(value) != 1) { + return; + } + for (;;) { + uint8_t tb = TYPEB(value); + if ((tb & 0x10) == 0) { + return; + } + if (tb == 0x10) { + if (*(void **)(value + 0xf) == (void *)as3vm_functioncall_implementation) { + record_error_location(vm, *(as3v **)(value + STR_LEN)); + return; + } + if (*(as3v *)(value + STR_LEN) == 0) { + return; + } + *(as3v *)(base + VM_ERR_LOC) = *(as3v *)(value + STR_LEN) + 0x34; + *(int32_t *)(base + VM_ERR_LINE) = + *(int32_t *)(*(as3v *)(value + STR_LEN) + 0x30); + return; + } + if (tb != 0x12) { + if (tb == 0x14) { + *(int32_t *)(base + VM_ERR_LINE) = 0x0b; + *(as3v *)(base + VM_ERR_LOC) = 0; + } + return; + } + value = *(as3v *)(value + STR_LEN) + 1; + if (TAG(value) != 1) { + return; + } + } +} + +static void record_error_info(void *vm, as3v value, as3v extra) { + as3v base = (as3v)vm; + as3v payload = 0; + if (value != 0) { + unsigned t = value & 3; + if (t == 0) { + payload = *(as3v *)(value + WORD); + } else if (t == 1) { + if (value != 1) { + payload = *(as3v *)(*(as3v *)(value + STR_LEN) + WORD); + *(as3v *)(base + VM_ERR_S1) = extra; + *(as3v *)(base + VM_ERR_S2) = payload; + return; + } + } else if (t == 3) { + *(as3v *)(base + VM_ERR_S1) = extra; + *(as3v *)(base + VM_ERR_S2) = value - 3; + return; + } + *(as3v *)(base + VM_ERR_S2) = payload; + } + *(as3v *)(base + VM_ERR_S1) = extra; +} + +/* ------------------------------------------------------------------ */ +/* Class-script lookup + lazy class initialization. */ +/* ------------------------------------------------------------------ */ + +static void lazy_init_class_GC(void *vm, as3v klass) { + as3v init_obj = *(as3v *)(klass + CLS_SLOTBASE); + as3v initv = + *(as3v *)(init_obj + + (**(uint16_t **)(*(as3v *)(init_obj + STR_LEN) + CLS_IFACE_PTR) - + 1)); + GC_PUSH(vm, init_obj); + as3vm_do_func_call_vv_GC(vm, (int64_t *)&initv, (int64_t)initv, (int64_t)GC_TOP(vm) - WORD, + 0, 0); + GC_POP(vm); +} + +static as3v as3vm_initialize_script_GC(void *vm, as3v script) { + as3v base = (as3v)vm; + as3v callee = *(as3v *)(((**(uint16_t **)(*(as3v *)(script + STR_LEN) + + CLS_IFACE_PTR) - + 1)) + + script); + if (*(as3v *)(base + VM_PENDING_EXC) == 0) { + as3vmcomp_set_value(vm, (void *)(script - 1), 0, 0); + GC_PUSH(vm, script); + as3vm_do_func_call_vv_GC(vm, (int64_t *)&callee, (int64_t)callee, + (int64_t)GC_TOP(vm) - WORD, 0, 0); + { + as3v pending = *(as3v *)(base + VM_PENDING_EXC); + GC_POP(vm); + if (pending == 0) { + return *(as3v *)GC_TOP(vm); + } + } + } + return HOLE; +} + +void *as3vm_find_class_script_GC(void *vm, void *name) { + as3v base = (as3v)vm; + as3v s = as3vm_get_property_name_GC(vm, VMF(vm, VM_SCRIPT_TABLE), (as3v)name); + if (s == VUNDEF || s == VNULL) { + return (void *)HOLE; + } + if (*(as3v *)(*(as3v *)(s + STR_LEN) + O_STARTCLASS) == + VMF(vm, VM_SCRIPT_CLASS) && + *(as3v *)(s + (**(uint16_t **)(*(as3v *)(s + STR_LEN) + CLS_IFACE_PTR) - + 1)) != 0) { + s = as3vm_initialize_script_GC(vm, s); + } + (void)base; + return (void *)s; +} + +int64_t as3vm_get_class_script_GC(void *vm, void *name) { + as3v s = as3vm_get_property_name_GC(vm, VMF(vm, VM_SCRIPT_TABLE), (as3v)name); + as3v member; + if (s == VUNDEF) { + return HOLE; + } + if (*(as3v *)(*(as3v *)(s + STR_LEN) + O_STARTCLASS) == + VMF(vm, VM_SCRIPT_CLASS) && + *(as3v *)(s + (**(uint16_t **)(*(as3v *)(s + STR_LEN) + CLS_IFACE_PTR) - + 1)) != 0) { + s = as3vm_initialize_script_GC(vm, s); + } + if (s == HOLE) { + return HOLE; + } + GC_PUSH(vm, s); + member = as3vm_get_property_name_GC(vm, s, (as3v)name); + { + as3v self = *(as3v *)(GC_TOP(vm) - WORD); + GC_POP(vm); + if (TAG(member) == 1 && *(char *)(member - 1) == '\x10') { + member = as3vm_make_method_closure_GC(vm, self, member); + } + } + return (int64_t)member; +} + +/* ------------------------------------------------------------------ */ +/* Scope-stack closure capture (arraydata snapshot of the scope stack). */ +/* ------------------------------------------------------------------ */ + +static void *make_scope_stack_closure_GC(void *vm, void *scope) { + as3v sc = (as3v)scope; + int extra = (int)((*(as3v *)(sc + 0x28) - *(as3v *)(sc + 0x20)) / WORD); + as3v basev = *(as3v *)(*(as3v *)(sc + 0x20) - WORD); + int total = *(int *)(basev + STR_LEN) + extra; + as3v ad = (as3v)as3vm_make_arraydata_GC(vm, total, total); + int baselen = *(int *)(basev + STR_LEN); + + if ((*(uint8_t *)ad & 0x20) != 0) { + for (int i = 0; i < baselen; ++i) { + as3v e = *(as3v *)(basev + 0x1f + (as3v)i * WORD); + if (TAG(e) == 1 && e != HOLE && 0x17u < (~*(uint8_t *)e & *(uint8_t *)ad)) { + as3vm_write_barrier_slowpath(vm, (void *)(ad - 1), (void *)(e - 1)); + } + *(as3v *)(ad + 0x1f + (as3v)i * WORD) = e; + } + for (int i = 0; i < extra; ++i) { + as3v e = *(as3v *)(*(as3v *)(sc + 0x20) + (as3v)i * WORD); + if (TAG(e) == 1 && e != HOLE && 0x17u < (~*(uint8_t *)e & *(uint8_t *)ad)) { + as3vm_write_barrier_slowpath(vm, (void *)(ad - 1), (void *)(e - 1)); + } + *(as3v *)(ad + (as3v)(baselen + i) * WORD + 0x1f) = e; + } + return (void *)ad; + } + memcpy((void *)(ad + 0x1f), (void *)(basev + 0x1f), + (size_t)baselen * WORD); + memcpy((void *)(ad + (as3v)baselen * WORD + 0x1f), *(void **)(sc + 0x20), + (size_t)extra * WORD); + return (void *)ad; +} + +static void *make_scope_stack_closure_extended_GC(void *vm, void *scope, + as3v extra_value) { + as3v sc = (as3v)scope; + int extra = (int)((*(as3v *)(sc + 0x28) - *(as3v *)(sc + 0x20)) / WORD); + as3v basev = *(as3v *)(*(as3v *)(sc + 0x20) - WORD); + int total = extra + 1 + *(int *)(basev + STR_LEN); + as3v ad; + int baselen = *(int *)(basev + STR_LEN); + + GC_PUSH(vm, extra_value); + ad = (as3v)as3vm_make_arraydata_GC(vm, total, total); + if ((*(uint8_t *)ad & 0x20) != 0) { + as3v last; + for (int i = 0; i < baselen; ++i) { + as3v e = *(as3v *)(basev + 0x1f + (as3v)i * WORD); + if (TAG(e) == 1 && e != HOLE && 0x17u < (~*(uint8_t *)e & *(uint8_t *)ad)) { + as3vm_write_barrier_slowpath(vm, (void *)(ad - 1), (void *)(e - 1)); + } + *(as3v *)(ad + 0x1f + (as3v)i * WORD) = e; + } + for (int i = 0; i < extra; ++i) { + as3v e = *(as3v *)(*(as3v *)(sc + 0x20) + (as3v)i * WORD); + if (TAG(e) == 1 && e != HOLE && 0x17u < (~*(uint8_t *)e & *(uint8_t *)ad)) { + as3vm_write_barrier_slowpath(vm, (void *)(ad - 1), (void *)(e - 1)); + } + *(as3v *)(ad + (as3v)(baselen + i) * WORD + 0x1f) = e; + } + GC_POP(vm); + last = *(as3v *)GC_TOP(vm); + if (TAG(last) == 1 && last != HOLE && 0x17u < (~*(uint8_t *)last & *(uint8_t *)ad)) { + as3vm_write_barrier_slowpath(vm, (void *)(ad - 1), (void *)(last - 1)); + } + *(as3v *)(ad + (as3v)total * WORD + 0x17) = last; + return (void *)ad; + } + memcpy((void *)(ad + 0x1f), (void *)(basev + 0x1f), (size_t)baselen * WORD); + memcpy((void *)(ad + (as3v)baselen * WORD + 0x1f), *(void **)(sc + 0x20), + (size_t)extra * WORD); + GC_POP(vm); + *(as3v *)(ad + (as3v)total * WORD + 0x17) = *(as3v *)GC_TOP(vm); + return (void *)ad; +} + +/* ------------------------------------------------------------------ */ +/* Runtime multiname resolution (late-name / qname). */ +/* ------------------------------------------------------------------ */ + +static as3v resolve_latename_qname(void *vm, as3v name, as3v value, + uint32_t flags) { + as3v base = (as3v)vm; + if (name == 0) { + return 1; + } + if ((value & 3) == 0) { + as3v found = as3vm_qname_find(vm, (void *)name, (void *)value, (int)flags); + return found != 0 ? found : 1; + } + if (TAG(*(as3v *)(base + VM_LATENAME)) == 1) { + as3vm_multiname_free(vm, *(as3v *)(base + VM_LATENAME) - 1); + } + { + as3v optimized = + as3vm_multiname_make_optimized(vm, name, value - 1, flags); + *(as3v *)(base + VM_LATENAME) = optimized; + return optimized; + } +} + +static as3v *name_resolve_runtime_GC(void *vm, as3v *name_slot, as3v *value_slot, + uint32_t flags) { + as3v entry = *name_slot; + as3v kind = *(as3v *)(entry - 2); + + if (kind == 0) { + as3v str = as3vm_ToString_GC(vm, *value_slot); + *value_slot = str; + if (str == HOLE) { + return NULL; + } + { + as3v sd = *(as3v *)(str + STR_LEN); + if (*(char *)(sd + 1) == 'c') { + kind = sd - INTERN_ADJ; + } else { + intptr_t interned = as3vm_find_intern_string( + vm, *(int *)(sd + STR_LEN), (void *)(sd + STR_BYTES), flags); + kind = (as3v)interned; + if (interned != 0) { + *(as3v *)(str + STR_LEN) = (as3v)interned + INTERN_ADJ; + } + } + } + value_slot -= 1; + } + { + as3v selector = *(as3v *)(entry + 6); + as3v resolved; + if (selector == 2) { + as3v runtime = *value_slot; + value_slot -= 1; + if (TAG(runtime) != 1 || *(char *)(runtime - 1) != '\f') { + return NULL; + } + if (kind == 0) { + resolved = 1; + } else { + as3v found = + as3vm_qname_find(vm, (void *)kind, (void *)(runtime - 1), (int)flags); + resolved = found != 0 ? found : 1; + } + } else { + resolved = resolve_latename_qname(vm, kind, entry, flags); + } + *name_slot = resolved; + } + return value_slot; +} + +/* ------------------------------------------------------------------ */ +/* String concatenation (UTF-16, GC-visible temporaries on the stack). */ +/* ------------------------------------------------------------------ */ + +uint64_t as3vm_string_concatenate_GC(void *vm, uint64_t a, uint64_t b) { + as3v base = (as3v)vm; + uint8_t scratch_a[128]; + uint8_t scratch_b[128]; + int len_a = 0, len_b = 0; + void *src_a, *src_b; + uint16_t *buf_a = NULL; + uint64_t out; + + GC_PUSH(vm, a); + GC_PUSH(vm, b); + src_a = as3vm_ToCharArray_GC(vm, *(as3v *)(GC_TOP(vm) - 2 * WORD), scratch_a, + &len_a); + if (src_a != NULL) { + buf_a = (uint16_t *)__builtin_alloca((size_t)len_a * 2 + 16); + memcpy(buf_a, src_a, (size_t)len_a * 2); + } + src_b = as3vm_ToCharArray_GC(vm, *(as3v *)(GC_TOP(vm) - WORD), scratch_b, + &len_b); + if (src_a == NULL || src_b == NULL) { + GC_TOP(vm) -= 2 * WORD; + return 0; + } + { + uint16_t *buf_b = (uint16_t *)__builtin_alloca((size_t)len_b * 2 + 16); + as3v sd; + memcpy(buf_b, src_b, (size_t)len_b * 2); + sd = (as3v)as3vm_make_stringdata_GC(vm, 0, len_a + len_b); + memcpy((void *)(sd + STR_BYTES), buf_a, (size_t)len_a * 2); + memcpy((void *)(sd + (as3v)len_a * 2 + STR_BYTES), buf_b, + (size_t)len_b * 2); + GC_TOP(vm) -= 2 * WORD; + out = as3vm_make_string_GC(vm, (void *)sd); + } + (void)base; + return out; +} + +/* ------------------------------------------------------------------ */ +/* Arraydata clone (dense backing-store copy with barriers). */ +/* ------------------------------------------------------------------ */ + +static void *clone_arrdata_GC(void *vm, void *arraydata) { + as3v src = (as3v)arraydata; + int count = *(int *)(src + 3); + as3v dst; + + GC_PUSH(vm, src); + dst = (as3v)as3vm_make_arraydata_GC(vm, count, *(uint32_t *)(src + STR_LEN)); + GC_POP(vm); + { + as3v self = *(as3v *)GC_TOP(vm); + if ((*(uint8_t *)dst & 0x20) == 0) { + for (int i = 0; i < count; ++i) { + *(as3v *)(dst + 0x1f + (as3v)i * WORD) = + *(as3v *)(self + 0x1f + (as3v)i * WORD); + } + } else { + for (int i = 0; i < count; ++i) { + as3v e = *(as3v *)(self + 0x1f + (as3v)i * WORD); + if (TAG(e) == 1 && e != HOLE && + 0x17u < (~*(uint8_t *)e & *(uint8_t *)dst)) { + as3vm_write_barrier_slowpath(vm, (void *)(dst - 1), (void *)(e - 1)); + } + *(as3v *)(dst + 0x1f + (as3v)i * WORD) = e; + } + } + } + return (void *)dst; +} diff --git a/iggy-decompiled/src/reconstructed/modules.tsv b/iggy-decompiled/src/reconstructed/modules.tsv index aaaf6d5..bb0c5dd 100644 --- a/iggy-decompiled/src/reconstructed/modules.tsv +++ b/iggy-decompiled/src/reconstructed/modules.tsv @@ -92,3 +92,7 @@ as3vm_imp_array 57 52 durango complete as3vm_imp_array 57 52 orbis complete as3vm_imp_array 56 52 ps3 complete as3vm_imp_array 56 52 vita complete +as3vm_interpreter 58 37 durango complete +as3vm_interpreter 46 37 orbis complete +as3vm_interpreter 45 37 ps3 complete +as3vm_interpreter 57 37 vita complete diff --git a/tests/support/as3vm_interpreter_test_support.c b/tests/support/as3vm_interpreter_test_support.c new file mode 100644 index 0000000..b78d08f --- /dev/null +++ b/tests/support/as3vm_interpreter_test_support.c @@ -0,0 +1,140 @@ +/* + * Host harness for the reconstructed as3vm_interpreter translation unit. + * + * as3vm_interpreter is Iggy's AS3 / AVM2 bytecode interpreter and the core VM + * operation layer -- the keystone of the AS3/AVM2 VM cluster. It exports + * EXACTLY 37 entry points on all four platforms (durango / orbis / ps3 / vita), + * and -- UNIQUELY among the VM modules -- it OWNS the ten VM sentinel-value data + * globals every other VM module imports by address: + * + * Common / tentative (zero-initialized, C on durango/orbis/vita, B/bss on ps3): + * as3vm_NaNf (4) as3vm_NaNd (8) as3vm_N_Infinity (8) as3vm_P_Infinity (8) + * Initialized .data (D): + * as3vm_truefalse_storage (16) as3vm_nullundef_storage (16) + * as3vm_global_nan (16) as3vm_global_inf (16) as3vm_must_init (4) + * Bss (B): + * as3vm_profiling (4) + * + * VALUE / SENTINEL MODEL (recovered from raw/durango, 64-bit offsets; the runtime + * test builds ONLY the durango dylib so 64-bit layout is correct): + * - an as3value is a tagged word: (v & 3) is the tag. Tag 1 == heap cell with a + * 1-byte type at v[-1]; 7 is the empty-slot / "hole" sentinel. + * - the value-cell sentinels every sibling module imports live INSIDE this + * module's two owned storage blobs: null = &nullundef+1, undefined = + * &nullundef+9, false = &truefalse+1, true = &truefalse+9. The initialized + * bytes encode the type bytes at those offsets (nullundef[0]=0x05 "null", + * nullundef[8]=0x04 "undefined", truefalse[0]=truefalse[8]=0x06 "boolean"). + * - as3vm_global_nan / as3vm_global_inf are boxed number values: a 16-byte cell + * whose header word is 0x3007 (bytes 07 30) and whose double payload (+8) is + * zero in the static image, populated at runtime. + * - the VM's GC-visible operand-stack top pointer lives at vm+0x6b18 + * (&DAT_00006b18 + vm) and its base at vm+0x6b10; as3vm_interpreter_initialize + * resets top = base (raw:1273). The number class pointer lives at vm+0x2840 + * (slot_get boxes a slot whose class matches it, raw:1400-1402). + * + * The symbols supplied below are the genuine nm ' U ' imports the SHALLOW pinned + * paths actually touch -- a subset of the invariant 69-symbol real-Iggy import + * set (see the cross test). This DURANGO harness models them as functional / + * recording stubs so the durango dylib links (-Wl,-undefined,dynamic_lookup lets + * the un-exercised remainder resolve lazily) and the shallow VM ops run. The 37 + * module exports and the 10 owned globals are DEFINED BY THE MODULE, never here. + * + * DEFERRED (needs the deep interpreter, flagged structural in the runtime + * docstring, NOT exercised): the file-static interpret_method_GC opcode-dispatch + * loop, the construct_pre/GC/post sequence, the do_func_call / do_method_call / + * do_apply / do_getter / do_setter call machinery, string_concatenate, the + * functioncall / methodclosure / functionclosure trampolines, exception_GC, the + * type-byte jump-table path of coerce_GC / cast_GC, and finalize. + */ + +#include +#include + +/* ---- Recording counters. ---- */ +static size_t make_number_count; +static size_t find_intern_count; +static size_t write_barrier_count; +static size_t make_error_count; + +/* ---- Controllable returns. ---- */ +static void *make_number_result; /* value as3vm_make_number_GC returns. */ +static void *make_number_last_arg; /* raw slot value it last received. */ +static intptr_t find_intern_result; /* value as3vm_find_intern_string returns. */ +static void *last_barrier_target; + +void as3vm_interpreter_test_reset(void) { + make_number_count = 0; + find_intern_count = 0; + write_barrier_count = 0; + make_error_count = 0; + make_number_result = 0; + make_number_last_arg = 0; + find_intern_result = 0; + last_barrier_target = 0; +} + +/* ---- Count / last-value accessors. ---- */ +size_t as3vm_interpreter_test_make_number_count(void) { return make_number_count; } +size_t as3vm_interpreter_test_find_intern_count(void) { return find_intern_count; } +size_t as3vm_interpreter_test_write_barrier_count(void) { + return write_barrier_count; +} +void *as3vm_interpreter_test_make_number_last_arg(void) { + return make_number_last_arg; +} +void *as3vm_interpreter_test_last_barrier_target(void) { + return last_barrier_target; +} + +/* ---- Controls. ---- */ +void as3vm_interpreter_test_set_make_number_result(void *v) { + make_number_result = v; +} +void as3vm_interpreter_test_set_find_intern_result(intptr_t v) { + find_intern_result = v; +} + +/* ---- Value construction (as3vm_create, unbuilt) -- recording stubs. ---- */ +void *as3vm_make_number_GC(void *vm, void *raw_value) { + (void)vm; + ++make_number_count; + make_number_last_arg = raw_value; + return make_number_result; +} + +void *as3vm_make_error_GC(void) { + ++make_error_count; + return 0; +} + +/* ---- Interning (as3vm_names, unbuilt) -- controllable stub. ---- */ +intptr_t as3vm_find_intern_string(void *vm, intptr_t length, void *bytes, + uint32_t flags) { + (void)vm; + (void)length; + (void)bytes; + (void)flags; + ++find_intern_count; + return find_intern_result; +} + +/* ---- GC write barrier (as3vm_gc, satisfied elsewhere) -- recording. ---- */ +void as3vm_write_barrier_slowpath(void *vm, void *target, void *value) { + (void)vm; + (void)value; + last_barrier_target = target; + ++write_barrier_count; +} + +/* ---- Number coercion (as3vm_convert, unbuilt) -- benign stub. ---- */ +int as3vm_ToNumberDirect_GC(void *vm, void *value, void *out) { + (void)vm; + (void)value; + (void)out; + return 0; +} + +/* ---- Float classification (rrMath) -- in case is_nan lowers to it. ---- */ +int rr_isnan(double value) { + return value != value; +} diff --git a/tests/test_reconstructed_as3vm_interpreter.py b/tests/test_reconstructed_as3vm_interpreter.py new file mode 100644 index 0000000..51fe04c --- /dev/null +++ b/tests/test_reconstructed_as3vm_interpreter.py @@ -0,0 +1,459 @@ +"""Runtime tests for the reconstructed as3vm_interpreter translation unit. + +RED STATE: as3vm_interpreter.c / as3vm_interpreter.h do not exist yet, so +setUpClass raises "missing as3vm_interpreter runtime inputs". Every assertion +below is grounded in a specific line of +iggy-decompiled/raw/durango/as3vm_interpreter.c (cited inline) so a faithful +DURANGO reconstruction turns these green. + +as3vm_interpreter is Iggy's AS3 / AVM2 bytecode interpreter and the core VM +operation layer -- the keystone of the VM cluster. It exports EXACTLY 37 entry +points on all four platforms and -- UNIQUELY among the VM modules -- it OWNS the +ten VM sentinel-value data globals every other VM module imports by address: +NaNf / NaNd / N_Infinity / P_Infinity (C common), truefalse_storage / +nullundef_storage / global_nan / global_inf / must_init (D initialized) and +profiling (B bss). + +VALUE / SENTINEL MODEL (raw/durango, offsets 64-bit; the runtime test builds ONLY +the durango dylib so 64-bit layout is correct): + - an as3value is a tagged word: (v & 3) is the tag. Tag 1 == heap cell with a + 1-byte type at v[-1]; 7 is the empty-slot / "hole" sentinel. + - the value-cell sentinels the sibling modules import live INSIDE this module's + two owned storage blobs: null = &nullundef+1, undefined = &nullundef+9, + false = &truefalse+1, true = &truefalse+9. The initialized bytes encode the + type bytes at those offsets (nullundef[0]=0x05, nullundef[8]=0x04, + truefalse[0]=truefalse[8]=0x06). + - a boxed number is a cell whose double payload lives at +8; as3vm_is_nan / + as3vm_is_inf classify *(box+8) (raw:1306-1323). + - the VM's GC operand-stack top lives at vm+0x6b18 and its base at vm+0x6b10; + as3vm_interpreter_initialize resets top = base (raw:1273). The Number class + pointer lives at vm+0x2840 (raw:1400). + +WHAT IS PINNED (real, deterministic DURANGO behavior): + - as3vm_do_validate returns 1 unconditionally (raw:938-942). + - as3vm_interpreter_notice_gc / as3vm_interpreter_request_collection are no-ops + (raw:1281-1295). + - as3vm_is_nan: *(box+8) NaN -> 1, finite -> 0 (raw:1317-1324). + - as3vm_is_inf: *(box+8) == as3vm_P_Infinity or as3vm_N_Infinity (the module's + OWN globals) -> 1, else 0 (raw:1303-1311). + - as3vm_interpreter_initialize: resets vm+0x6b18 (GC stack top) to vm+0x6b10 + (base) and clears vm+0x28d0 / vm+0x28d8 (raw:1271-1273). + - as3vm_slot_get_GC / as3vm_avm2_slot_get_GC: a slot whose class != the Number + class (vm+0x2840) returns the raw stored as3value; a slot whose class == the + Number class boxes it through as3vm_make_number_GC (raw:1399-1405 / 316-322). + - as3vm_coerce_GC / as3vm_cast_GC: on the NON-jump-table path (target type byte + at target+0x1b, minus 6, >= 0x25) a tag-1 value whose own type v[-1] equals + the target type is returned unchanged; otherwise the 7 sentinel is returned + (raw:404-416 / 429-441). + - as3vm_intern_string_value: an already-interned stringdata (byte[1]=='c') + returns stringdata-0x29 without calling out; otherwise it delegates to + as3vm_find_intern_string and returns 0 on a miss (raw:1237-1249). + - as3vm_is_ofclass: a direct class-chain hit returns 1; a chain that terminates + without a match returns 0 (raw:1361-1368). + - the ten owned sentinel globals exist with their exact retail-recovered + initialized bytes / sizes (llvm-objdump -s -j .data on the durango object). + +DEFERRED (flagged for the impl / review phase, NOT exercised here -- structural +only): + - the file-static interpret_method_GC opcode-dispatch loop (the big vita switch + that fully expands to 6621 raw lines) and interpret_method_profiled_GC. + - the object-construction sequence as3vm_construct_pre_GC / as3vm_construct_GC / + as3vm_construct_post_GC (raw:448-937) and the scope-stack closures. + - the call machinery as3vm_do_func_call_vmc_GC / _vv_GC / as3vm_do_method_call_GC + / as3vm_do_apply_GC / as3vm_do_getter_GC / as3vm_do_setter_GC and the + functioncall / methodclosure / functionclosure trampolines (arg spill + + this-binding). + - as3vm_string_concatenate_GC, as3vm_exception_GC (Ghidra recovers a garbled + 1-param signature though callers pass (vm,code)), as3vm_finalize (indirect + jump), and the type-byte &__ImageBase jump-table path of coerce_GC / cast_GC. + +Full-suite runs use python3.12 (the machine python3 is 3.14, which SEGFAULTs on +the full suite via an unrelated CPython ctypes-GC bug); single-module runs of +this file are fine on either interpreter. +""" + +import ctypes +import shutil +import struct +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_interpreter.c" +HEADER = RECONSTRUCTED / "as3vm_interpreter.h" +SUPPORT = ROOT / "tests/support/as3vm_interpreter_test_support.c" + +# 64-bit DURANGO value / struct constants (cited per test). +TAG_MASK = 3 # (v & 3) is the as3value tag. +HOLE_SENTINEL = 7 # empty-slot / "no value" as3value (raw:416). +OFF_BOX_DOUBLE = 8 # boxed number: double payload at box+8 (raw:1306). +OFF_TARGET_TYPE = 0x1B # coerce/cast target class type byte (raw:404/429). +OFF_VM_NUMBER_CLASS = 0x2840 # vm -> Number class pointer (raw:1400). +OFF_VM_GC_BASE = 0x6B10 # vm -> GC operand-stack base (raw:1273). +OFF_VM_GC_TOP = 0x6B18 # vm -> GC operand-stack top (raw:1273). +OFF_VM_CLEAR_A = 0x28D0 # vm field cleared by initialize (raw:1271). +OFF_VM_CLEAR_B = 0x28D8 # vm field cleared by initialize (raw:1272). + +# The ten owned globals: (ctype-size, retail-recovered initialized bytes). D +# globals carry exact .data bytes; C/B globals are zero-initialized. Recovered +# via llvm-objdump -s -j .data on the durango object. +TRUEFALSE_BYTES = bytes([0x06, 0x30, 0, 0, 0, 0, 0, 0, + 0x06, 0x30, 0, 0, 0, 0, 0, 0]) +NULLUNDEF_BYTES = bytes([0x05, 0x30, 0, 0, 0, 0, 0, 0, + 0x04, 0x30, 0, 0, 0, 0, 0, 0]) +GLOBAL_NAN_BYTES = bytes([0x07, 0x30, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0]) +GLOBAL_INF_BYTES = bytes([0x07, 0x30, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0]) + + +class ReconstructedAs3vmInterpreterTests(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_interpreter runtime inputs: " + ", ".join(missing)) + cls.temp = tempfile.TemporaryDirectory() + cls.base = Path(cls.temp.name) / "as3vm_interpreter.dylib" + build = subprocess.run([ + "clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra", + "-Werror", "-Wl,-undefined,dynamic_lookup", + "-DIGGY_RECONSTRUCT_AS3VM_INTERPRETER_DURANGO=1", + "-I", str(RECONSTRUCTED), + str(SOURCE), str(SUPPORT), "-o", str(cls.base), + ], capture_output=True, text=True) + if build.returncode: + raise AssertionError(build.stdout + build.stderr) + + @classmethod + def tearDownClass(cls): + cls.temp.cleanup() + + def setUp(self): + unique = (Path(self.temp.name) / + f"as3vm_interpreter_{self._testMethodName}.dylib") + shutil.copyfile(self.base, unique) + self.lib = ctypes.CDLL(str(unique)) + self._bind(self.lib) + self.lib.as3vm_interpreter_test_reset() + self._keep = [] + + def _bind(self, lib): + void_p = ctypes.c_void_p + c_int = ctypes.c_int + + lib.as3vm_interpreter_test_reset.argtypes = [] + lib.as3vm_interpreter_test_reset.restype = None + for name in ("make_number", "find_intern", "write_barrier"): + fn = getattr(lib, f"as3vm_interpreter_test_{name}_count") + fn.restype = ctypes.c_size_t + fn.argtypes = [] + lib.as3vm_interpreter_test_make_number_last_arg.restype = void_p + lib.as3vm_interpreter_test_make_number_last_arg.argtypes = [] + lib.as3vm_interpreter_test_set_make_number_result.restype = None + lib.as3vm_interpreter_test_set_make_number_result.argtypes = [void_p] + lib.as3vm_interpreter_test_set_find_intern_result.restype = None + lib.as3vm_interpreter_test_set_find_intern_result.argtypes = [ + ctypes.c_ssize_t] + + # Module exports under test (durango 64-bit ABI). + lib.as3vm_do_validate.restype = ctypes.c_uint64 + lib.as3vm_do_validate.argtypes = [] + lib.as3vm_interpreter_notice_gc.restype = None + lib.as3vm_interpreter_notice_gc.argtypes = [] + lib.as3vm_interpreter_request_collection.restype = None + lib.as3vm_interpreter_request_collection.argtypes = [] + lib.as3vm_is_nan.restype = ctypes.c_uint64 + lib.as3vm_is_nan.argtypes = [void_p] + lib.as3vm_is_inf.restype = ctypes.c_uint64 + lib.as3vm_is_inf.argtypes = [void_p] + lib.as3vm_interpreter_initialize.restype = None + lib.as3vm_interpreter_initialize.argtypes = [void_p] + lib.as3vm_slot_get_GC.restype = ctypes.c_uint64 + lib.as3vm_slot_get_GC.argtypes = [void_p, void_p, c_int] + lib.as3vm_avm2_slot_get_GC.restype = ctypes.c_uint64 + lib.as3vm_avm2_slot_get_GC.argtypes = [void_p, void_p, c_int] + lib.as3vm_coerce_GC.restype = ctypes.c_int64 + lib.as3vm_coerce_GC.argtypes = [void_p, ctypes.c_int64, ctypes.c_int64] + lib.as3vm_cast_GC.restype = ctypes.c_int64 + lib.as3vm_cast_GC.argtypes = [void_p, ctypes.c_int64, ctypes.c_int64] + lib.as3vm_intern_string_value.restype = ctypes.c_int64 + lib.as3vm_intern_string_value.argtypes = [ + void_p, ctypes.c_int64, ctypes.c_uint32] + lib.as3vm_is_ofclass.restype = ctypes.c_uint64 + lib.as3vm_is_ofclass.argtypes = [void_p, void_p] + + def _count(self, name): + return getattr(self.lib, f"as3vm_interpreter_test_{name}_count")() + + def _buffer(self, size): + buf = (ctypes.c_uint8 * size)() + self._keep.append(buf) + return ctypes.addressof(buf) + + def _u64(self, address, value): + ctypes.c_uint64.from_address(address).value = value + + def _u16(self, address, value): + ctypes.c_uint16.from_address(address).value = value + + def _byte(self, address, value): + ctypes.c_uint8.from_address(address).value = value + + def _double_box(self, value): + # A boxed number: 16-byte cell whose double payload lives at box+8. + box = self._buffer(0x20) + struct.pack_into(" 1, a finite value -> 0. + self.assertEqual(self.lib.as3vm_is_nan(self._double_box(float("nan"))), 1) + self.assertEqual(self.lib.as3vm_is_nan(self._double_box(1.5)), 0) + + def test_is_inf_compares_against_owned_infinity_globals(self): + # raw:1306-1310 is_inf returns 1 iff *(box+8) equals the module's OWN + # as3vm_P_Infinity / as3vm_N_Infinity globals. Pin the comparison itself + # against known sentinels rather than assuming how the reconstruction + # initializes these (tentative) common globals. + p_inf = ctypes.c_double.in_dll(self.lib, "as3vm_P_Infinity") + n_inf = ctypes.c_double.in_dll(self.lib, "as3vm_N_Infinity") + p_inf.value = 424242.0 + n_inf.value = -999.0 + self.assertEqual(self.lib.as3vm_is_inf(self._double_box(424242.0)), 1) + self.assertEqual(self.lib.as3vm_is_inf(self._double_box(-999.0)), 1) + self.assertEqual(self.lib.as3vm_is_inf(self._double_box(0.0)), 0) + + # ------------------------------------------------------------------ # + # as3vm_interpreter_initialize -- GC-stack reset + field clear + # ------------------------------------------------------------------ # + + def test_initialize_resets_gc_stack_top_and_clears_fields(self): + # raw:1271-1273 vm+0x6b18 (GC stack top) := vm+0x6b10 (base); the two + # scratch fields vm+0x28d0 / vm+0x28d8 are zeroed. + vm = self._vm() + base = self._buffer(0x400) + self._u64(vm + OFF_VM_GC_BASE, base) + self._u64(vm + OFF_VM_GC_TOP, 0xDEAD) + self._u64(vm + OFF_VM_CLEAR_A, 0x1111) + self._u64(vm + OFF_VM_CLEAR_B, 0x2222) + self.lib.as3vm_interpreter_initialize(vm) + self.assertEqual(ctypes.c_uint64.from_address(vm + OFF_VM_GC_TOP).value, + base) + self.assertEqual( + ctypes.c_uint64.from_address(vm + OFF_VM_CLEAR_A).value, 0) + self.assertEqual( + ctypes.c_uint64.from_address(vm + OFF_VM_CLEAR_B).value, 0) + + # ------------------------------------------------------------------ # + # as3vm_slot_get_GC / as3vm_avm2_slot_get_GC -- slot read + number boxing + # ------------------------------------------------------------------ # + + def _slot_object(self, slot, field_offset, slot_class): + # obj[8] -> class. class carries the ushort field offset at + # class+0x68+slot*0x10 and the slot's class pointer at class+(slot+6)*0x10. + klass = self._buffer(0x200) + self._u16(klass + 0x68 + slot * 0x10, field_offset) + self._u64(klass + (slot + 6) * 0x10, slot_class) + obj = self._buffer(0x200) + self._u64(obj + 8, klass) + return obj + + def test_slot_get_non_number_returns_raw_value(self): + # raw:1400-1405 a slot whose class != vm+0x2840 (Number) returns the raw + # as3value stored at obj+field_offset, unboxed. + vm = self._vm(0x3000) + self._u64(vm + OFF_VM_NUMBER_CLASS, 0xABCDEF) + other_class = self._buffer(0x40) + obj = self._slot_object(slot=0, field_offset=0x40, slot_class=other_class) + self._u64(obj + 0x40, 0x1234567) + self.assertEqual(self.lib.as3vm_slot_get_GC(vm, obj, 0), 0x1234567) + self.assertEqual(self._count("make_number"), 0) + + def test_slot_get_number_class_boxes_through_make_number(self): + # raw:1401-1402 a slot whose class == vm+0x2840 (Number) is boxed via + # as3vm_make_number_GC, which receives the raw stored value. + vm = self._vm(0x3000) + number_class = self._buffer(0x40) + self._u64(vm + OFF_VM_NUMBER_CLASS, number_class) + obj = self._slot_object(slot=0, field_offset=0x40, + slot_class=number_class) + self._u64(obj + 0x40, 0x77770000) + boxed = self._buffer(0x10) + self.lib.as3vm_interpreter_test_set_make_number_result( + ctypes.c_void_p(boxed)) + self.assertEqual(self.lib.as3vm_slot_get_GC(vm, obj, 0), boxed) + self.assertEqual(self._count("make_number"), 1) + self.assertEqual( + self.lib.as3vm_interpreter_test_make_number_last_arg(), 0x77770000) + + def test_avm2_slot_get_matches_slot_get(self): + # raw:316-322 as3vm_avm2_slot_get_GC is byte-identical to slot_get for + # the non-number path. + vm = self._vm(0x3000) + self._u64(vm + OFF_VM_NUMBER_CLASS, 0xABCDEF) + other_class = self._buffer(0x40) + obj = self._slot_object(slot=0, field_offset=0x40, slot_class=other_class) + self._u64(obj + 0x40, 0x55aa55aa) + self.assertEqual(self.lib.as3vm_avm2_slot_get_GC(vm, obj, 0), 0x55aa55aa) + + # ------------------------------------------------------------------ # + # as3vm_coerce_GC / as3vm_cast_GC -- non-jump-table identity path + # ------------------------------------------------------------------ # + + def _typed_value(self, type_byte): + # A tag-1 heap cell: value = cell+1, so (value & 3) == 1 and the type + # byte lives at value[-1] == cell[0]. + cell = self._buffer(0x20) + self._byte(cell, type_byte) + return cell + 1 + + def _target_class(self, type_byte): + target = self._buffer(0x40) + self._byte(target + OFF_TARGET_TYPE, type_byte) + return target + + def test_coerce_matching_type_returns_value_unchanged(self): + # raw:438-440 target type byte 0x40 (0x40-6=0x3a >= 0x25 -> the direct + # path); a tag-1 value whose own type equals it is returned unchanged. + vm = self._vm(0x40) + value = self._typed_value(0x40) + target = self._target_class(0x40) + self.assertEqual(self.lib.as3vm_coerce_GC(vm, value, target), value) + + def test_coerce_mismatched_type_returns_hole_sentinel(self): + # raw:441 a tag-1 value whose type differs from the target yields 7. + vm = self._vm(0x40) + value = self._typed_value(0x41) + target = self._target_class(0x40) + self.assertEqual( + self.lib.as3vm_coerce_GC(vm, value, target), HOLE_SENTINEL) + + def test_cast_matching_type_returns_value_unchanged(self): + # raw:413-414 as3vm_cast_GC shares the direct-path identity check. + vm = self._vm(0x40) + value = self._typed_value(0x40) + target = self._target_class(0x40) + self.assertEqual(self.lib.as3vm_cast_GC(vm, value, target), value) + + # ------------------------------------------------------------------ # + # as3vm_intern_string_value -- already-interned fast path + miss + # ------------------------------------------------------------------ # + + def test_intern_already_interned_returns_stringdata_minus_0x29(self): + # raw:1238-1240 stringdata byte[1]=='c' marks it already interned; the + # function returns stringdata-0x29 without calling find_intern_string. + vm = self._vm(0x40) + stringdata = self._buffer(0x40) + self._byte(stringdata + 1, ord("c")) + value = self._buffer(0x40) + self._u64(value + 7, stringdata) + self.assertEqual( + self.lib.as3vm_intern_string_value(vm, value, 0), stringdata - 0x29) + self.assertEqual(self._count("find_intern"), 0) + + def test_intern_miss_delegates_and_returns_zero(self): + # raw:1242-1249 a non-interned stringdata delegates to + # as3vm_find_intern_string; a 0 result is a miss and the function + # returns 0. + vm = self._vm(0x40) + stringdata = self._buffer(0x40) + self._byte(stringdata + 1, ord("x")) + value = self._buffer(0x40) + self._u64(value + 7, stringdata) + self.lib.as3vm_interpreter_test_set_find_intern_result(0) + self.assertEqual(self.lib.as3vm_intern_string_value(vm, value, 0), 0) + self.assertEqual(self._count("find_intern"), 1) + + # ------------------------------------------------------------------ # + # as3vm_is_ofclass -- class-chain walk (non-'*' path) + # ------------------------------------------------------------------ # + + def _instance(self, start_class): + # obj[8] -> holder; holder[0x20] -> the object's start class. + holder = self._buffer(0x40) + self._u64(holder + 0x20, start_class) + obj = self._buffer(0x40) + self._u64(obj + 8, holder) + return obj + + def test_is_ofclass_direct_hit_returns_one(self): + # raw:1362-1363 when the object's start class IS the query class (and the + # query is not the '*' any-type), the walk matches immediately. + klass = self._buffer(0x40) + self._byte(klass + OFF_TARGET_TYPE, 0x10) # non-'*'. + obj = self._instance(start_class=klass) + self.assertEqual(self.lib.as3vm_is_ofclass(obj, klass), 1) + + def test_is_ofclass_chain_terminates_returns_zero(self): + # raw:1365-1368 a start class that mismatches and whose superclass link + # (+0x40) is null ends the walk with 0. + query = self._buffer(0x40) + self._byte(query + OFF_TARGET_TYPE, 0x10) # non-'*'. + start = self._buffer(0x40) + self._u64(start + 0x40, 0) # no superclass. + obj = self._instance(start_class=start) + self.assertEqual(self.lib.as3vm_is_ofclass(obj, query), 0) + + # ------------------------------------------------------------------ # + # The ten owned VM sentinel globals -- exact retail-recovered image + # ------------------------------------------------------------------ # + + def _global_bytes(self, name, size): + return bytes((ctypes.c_uint8 * size).in_dll(self.lib, name)) + + def test_owned_initialized_globals_match_retail_bytes(self): + # llvm-objdump -s -j .data (durango). The value-cell + boxed-number + # sentinels every sibling VM module imports are defined HERE with these + # exact initialized bytes. + self.assertEqual( + self._global_bytes("as3vm_truefalse_storage", 16), TRUEFALSE_BYTES) + self.assertEqual( + self._global_bytes("as3vm_nullundef_storage", 16), NULLUNDEF_BYTES) + self.assertEqual( + self._global_bytes("as3vm_global_nan", 16), GLOBAL_NAN_BYTES) + self.assertEqual( + self._global_bytes("as3vm_global_inf", 16), GLOBAL_INF_BYTES) + self.assertEqual( + ctypes.c_uint32.in_dll(self.lib, "as3vm_must_init").value, 1) + + def test_owned_zero_initialized_globals_exist(self): + # The common (tentative) NaN / Infinity globals and the bss profiling + # counter default to zero. Their presence proves this module OWNS them. + self.assertEqual( + ctypes.c_uint32.in_dll(self.lib, "as3vm_profiling").value, 0) + self.assertEqual( + ctypes.c_float.in_dll(self.lib, "as3vm_NaNf").value, 0.0) + self.assertEqual( + ctypes.c_double.in_dll(self.lib, "as3vm_NaNd").value, 0.0) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_reconstructed_as3vm_interpreter_cross.py b/tests/test_reconstructed_as3vm_interpreter_cross.py new file mode 100644 index 0000000..79fc465 --- /dev/null +++ b/tests/test_reconstructed_as3vm_interpreter_cross.py @@ -0,0 +1,492 @@ +"""Cross-platform contract tests for the reconstructed as3vm_interpreter module. + +Mirrors tests/test_reconstructed_as3vm_property_cross.py / +tests/test_reconstructed_as3vm_imp_array_cross.py (the freshest 4-platform +templates in the same AS3/AVM2 VM cluster). RED until as3vm_interpreter.c / +as3vm_interpreter.h (and their CMake / modules.tsv rows) exist; the +retail-evidence assertions run now and prove the nm/strings parsing. + +as3vm_interpreter is Iggy's AS3 / AVM2 bytecode interpreter and the core VM +operation layer -- the keystone of the VM cluster. It is a 4-PLATFORM module +(durango 2613 / orbis 3234 / ps3 2475 / vita 6621 raw lines; retail +source_functions 58 / 46 / 45 / 57 -- the counts vary widely, and vita fully +expands the opcode-dispatch switch inline, driving its 6621-line body -- while +the exported ABI stays a fixed 37 on all four). + +How EXPECTED_* were derived (all four .obj under +work/iggy-native-decomp/-objects/as3vm_interpreter.obj, verified with +/opt/homebrew/opt/llvm/bin/llvm-nm and `nm -g`): + + EXPECTED_FUNCTIONS = `llvm-nm --extern-only --defined-only ` T-tagged + names, leading '.' stripped on ps3. IDENTICAL 37-name + set on durango/orbis/ps3/vita -- the exported VM ABI is + invariant even though the internal helper count varies + (58/46/45/57). The .opd descriptors on ps3 surface each + export a second time as 'D'; those normalize to the + exported names and are NOT separate globals (same handling + as as3vm_property / as3vm_imp_array). + + EXPECTED_GLOBALS = the TEN VM sentinel-value globals as3vm_interpreter OWNS + -- the VM cluster's keystone data. Every other VM module + (as3vm_property / as3vm_imp_array / ...) imports + as3vm_nullundef_storage / as3vm_truefalse_storage by + address; they are DEFINED HERE, alongside the boxed NaN / + Infinity sentinels and the init/profiling state: + - as3vm_NaNf / as3vm_NaNd / as3vm_N_Infinity / + as3vm_P_Infinity (C common on durango/orbis/vita, + B/bss on ps3; zero-initialized tentative defs) + - as3vm_truefalse_storage / as3vm_nullundef_storage / + as3vm_global_nan / as3vm_global_inf / as3vm_must_init + (D initialized data, sizes 16/16/16/16/4) + - as3vm_profiling (B bss, 4) + The D globals carry exact retail-recovered initialized + bytes (llvm-objdump -s -j .data): truefalse = 06 30..(x2) + [null/undefined type bytes 0x05/0x04, boolean 0x06], + global_nan / global_inf = 07 30.. + zero double payload, + must_init = 1. The NAME set is pinned on all four; the + exact bytes are additionally asserted below (uniform + modulo ps3 big-endian must_init byte order). + + EXPECTED_IMPORTS = the genuine non-scaffolding references present on ALL + FOUR retail objects (INVARIANT -- verified identical, 69 + symbols). Most are satisfied by completed VM modules + (as3vm_convert: ToNumber/ToString/ToInt32/ToUInt32/ + ToCharArray/NumberToInt32/StringToArrayIndex/DefaultValue/ + to_boolean; as3vm_property: get/set/find_property, + qmap_find_name, proplist_get/set/delete, + object_delete_property, get_prototype; as3vm_create: + make_*_GC; as3vm_names: qname_find / + multiname_make_optimized/free, find_intern_string; + as3vm_gc: write_barrier_slowpath; as3vm_imp_array: + array_set/delete_value_GC). The genuinely-UNBUILT ones + (as3vm_pool_alloc/free, as3vm_qname_add_init/_safe, + as3vm_make_vector_class, as3vm_call_property_name_super, + as3vm_get_property_name_closure*/flexible/scoped_closure, + as3vm_set_property_name_super_GC, as3vmcomp_set_value, + the iggy_* flash-display factories, + iggy_random/iggy_uncaught_exception_warning, rrGetTicks) + are still expected here. This real-Iggy set is INVARIANT + across the four platforms. + + PLATFORM_IMPORTS = the libc / libm the reconstruction lowers. UNIFORM here + ({fmod, memcpy, rr_isnan} on every object -- no per-target + divergence), routed through PLATFORM_IMPORTS (not + EXPECTED_IMPORTS) because they are libc/libm rather than + Iggy symbols. fmod backs the AS3 '%' operator; memcpy + backs value/scope moves; rr_isnan the numeric compares. + + RUNTIME_ALLOWLISTS = pure toolchain scaffolding residual after subtracting the + genuine import set, verified exact against retail + `llvm-nm --undefined-only`: + durango: __GSHandlerCheck, __ImageBase, __chkstk, + __security_check_cookie, __security_cookie, + _fltused + orbis: _GLOBAL_OFFSET_TABLE_ + ps3: {} (empty) + vita: __aeabi_unwind_cpp_pr0, __aeabi_unwind_cpp_pr1, + __modsi3, __stack_chk_fail, __stack_chk_guard + (durango pulls __ImageBase -- the base of the + type-byte-indexed coerce_GC / cast_GC jump tables; vita + pulls __modsi3 for the integer-modulo opcode. Both are + allowlisted so they are subtracted whether or not the + reconstruction's portable form re-emits them.) + + PROVENANCE = PER-PLATFORM here, UNLIKE as3vm_property / as3vm_imp_array + (where the canonical src path was embedded uniformly). + strings on the retail objects shows the durango object + carries ONLY its build-temp .obj path + (c:\\projects\\g\\build\\release\\temps\\libdu\\ + iggy_durango\\as3vm_interpreter.obj) with NO src\\flash + path; orbis carries the canonical + c:\\projects\\g\\src\\flash\\as3vm_interpreter.c; ps3 / + vita carry a bare 'as3vm_interpreter.c'. The retail check + asserts each platform's ACTUAL embedded string; the + reconstructed source embeds the canonical path. + +Run the full suite with python3.12 (`python3.12 -m unittest discover -s tests`): +the machine python3 is 3.14, which SEGFAULTs on the full suite via an unrelated +CPython ctypes-GC bug. Single-module runs are fine on either interpreter; this +cross test's cross-compile/nm logic is python-version-independent. +""" + +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_interpreter.c" +HEADER = RECONSTRUCTED / "as3vm_interpreter.h" +LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm") +LLVM_OBJDUMP = Path("/opt/homebrew/opt/llvm/bin/llvm-objdump") +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") + +# Invariant 37-name exported VM ABI (leading '.' stripped on ps3). +EXPECTED_FUNCTIONS = { + "IggyDebugBreakOnAS3Exception", + "as3vm_avm2_slot_get_GC", + "as3vm_avm2_slot_set_GC", + "as3vm_cast_GC", + "as3vm_coerce_GC", + "as3vm_construct_GC", + "as3vm_construct_post_GC", + "as3vm_construct_pre_GC", + "as3vm_do_apply_GC", + "as3vm_do_func_call_vmc_GC", + "as3vm_do_func_call_vv_GC", + "as3vm_do_getter_GC", + "as3vm_do_method_call_GC", + "as3vm_do_setter_GC", + "as3vm_do_validate", + "as3vm_exception_GC", + "as3vm_finalize", + "as3vm_find_class_script_GC", + "as3vm_flush_lcache", + "as3vm_flush_lcache_dict", + "as3vm_functioncall_implementation", + "as3vm_functionclosure_implementation", + "as3vm_get_class_script_GC", + "as3vm_intern_string_value", + "as3vm_interpreter_initialize", + "as3vm_interpreter_notice_gc", + "as3vm_interpreter_request_collection", + "as3vm_is_inf", + "as3vm_is_nan", + "as3vm_is_ofclass", + "as3vm_methodclosure_implementation", + "as3vm_slot_get_GC", + "as3vm_slot_set_GC", + "as3vm_string_concatenate_GC", + "as3vm_string_interned", + "as3vm_string_interned_nocreate", + "iggy_monitor_variable", +} +# The ten VM sentinel-value globals as3vm_interpreter OWNS (the keystone data). +EXPECTED_GLOBALS = { + "as3vm_NaNf", + "as3vm_NaNd", + "as3vm_N_Infinity", + "as3vm_P_Infinity", + "as3vm_truefalse_storage", + "as3vm_nullundef_storage", + "as3vm_global_nan", + "as3vm_global_inf", + "as3vm_must_init", + "as3vm_profiling", +} +# Exact retail-recovered initialized bytes of the D (initialized) globals +# (llvm-objdump -s -j .data; uniform across all four objects modulo ps3 +# big-endian must_init byte order). +OWNED_DATA_BYTES = { + "as3vm_truefalse_storage": bytes([0x06, 0x30, 0, 0, 0, 0, 0, 0, + 0x06, 0x30, 0, 0, 0, 0, 0, 0]), + "as3vm_nullundef_storage": bytes([0x05, 0x30, 0, 0, 0, 0, 0, 0, + 0x04, 0x30, 0, 0, 0, 0, 0, 0]), + "as3vm_global_nan": bytes([0x07, 0x30, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0]), + "as3vm_global_inf": bytes([0x07, 0x30, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0]), +} + +# Genuine non-scaffolding imports present on all four retail objects (invariant, +# 69 symbols). Most satisfied by completed VM modules; the interpreter/flash +# factories + pools remain genuinely-unbuilt. +EXPECTED_IMPORTS = { + # type conversion (as3vm_convert -- satisfied). + "as3vm_DefaultValue_None_GC", + "as3vm_DefaultValue_Number_GC", + "as3vm_NumberToInt32", + "as3vm_StringToArrayIndex", + "as3vm_ToCharArray_GC", + "as3vm_ToInt32_GC", + "as3vm_ToNumberDirect_GC", + "as3vm_ToNumber_GC", + "as3vm_ToString_GC", + "as3vm_ToUInt32_GC", + "as3vm_to_boolean", + "as3vm_to_boolean_direct", + # array backing (as3vm_imp_array -- satisfied). + "as3vm_array_delete_value_GC", + "as3vm_array_set_value_GC", + # property resolution (as3vm_property -- satisfied + unbuilt variants). + "as3vm_call_property_name_super", + "as3vm_find_property_scoped_GC", + "as3vm_get_property_name_GC", + "as3vm_get_property_name_closure_GC", + "as3vm_get_property_name_closure_super_GC", + "as3vm_get_property_name_flexible", + "as3vm_get_property_qname_GC", + "as3vm_get_property_scoped_closure_GC", + "as3vm_get_prototype", + "as3vm_object_delete_property_GC", + "as3vm_proplist_delete_GC", + "as3vm_proplist_get", + "as3vm_proplist_set_GC", + "as3vm_qmap_find_name", + "as3vm_set_property_name_GC", + "as3vm_set_property_name_super_GC", + # naming / interning (as3vm_names -- satisfied + unbuilt). + "as3vm_find_intern_string", + "as3vm_make_namespace", + "as3vm_multiname_free", + "as3vm_multiname_make_optimized", + "as3vm_qname_add_init", + "as3vm_qname_add_init_safe", + "as3vm_qname_find", + # value construction (as3vm_create -- satisfied + unbuilt). + "as3vm_make_array_GC", + "as3vm_make_array_with_traits_GC", + "as3vm_make_arraydata_GC", + "as3vm_make_error_GC", + "as3vm_make_function_closure_GC", + "as3vm_make_int_GC", + "as3vm_make_integer_GC", + "as3vm_make_method_closure_GC", + "as3vm_make_number_GC", + "as3vm_make_number_not_int_GC", + "as3vm_make_object_GC", + "as3vm_make_object_no_constructor_GC", + "as3vm_make_object_with_proto_GC", + "as3vm_make_object_with_traits_GC", + "as3vm_make_string_GC", + "as3vm_make_stringdata_GC", + "as3vm_make_uint_GC", + "as3vm_make_vector_class", + # GC (as3vm_gc -- satisfied). + "as3vm_write_barrier_slowpath", + # component / pool (genuinely-unbuilt). + "as3vmcomp_set_value", + "as3vm_pool_alloc", + "as3vm_pool_free", + # flash display factories (swf_play / flash_display -- genuinely-unbuilt). + "iggy_create_empty_bitmap", + "iggy_create_movie_complete_GC", + "iggy_make_empty_button", + "iggy_make_empty_shape", + "iggy_make_empty_sprite", + "iggy_make_empty_textfield", + "iggy_mark_movie_dirty", + # iggy_main diagnostics + rr time (genuinely-unbuilt / satisfied). + "iggy_random", + "iggy_uncaught_exception_warning", + "rrGetTicks", +} +# The libc/libm lowering is UNIFORM here ({fmod, memcpy, rr_isnan} on all four), +# but routed through PLATFORM_IMPORTS because it is libc/libm rather than Iggy. +PLATFORM_IMPORTS = { + "durango": {"fmod", "memcpy", "rr_isnan"}, + "orbis": {"fmod", "memcpy", "rr_isnan"}, + "ps3": {"fmod", "memcpy", "rr_isnan"}, + "vita": {"fmod", "memcpy", "rr_isnan"}, +} +RUNTIME_ALLOWLISTS = { + "durango": {"__GSHandlerCheck", "__ImageBase", "__chkstk", + "__security_check_cookie", "__security_cookie", "_fltused"}, + "orbis": {"_GLOBAL_OFFSET_TABLE_"}, + "ps3": set(), + "vita": {"__aeabi_unwind_cpp_pr0", "__aeabi_unwind_cpp_pr1", "__modsi3", + "__stack_chk_fail", "__stack_chk_guard"}, +} +# Provenance is PER-PLATFORM (unlike as3vm_property / as3vm_imp_array): durango +# embeds only its build-temp .obj path, orbis the canonical src path, ps3 / vita +# a bare basename. +PROVENANCE_STRINGS = { + "durango": (r"c:\projects\g\build\release\temps\libdu\iggy_durango" + r"\as3vm_interpreter.obj"), + "orbis": r"c:\projects\g\src\flash\as3vm_interpreter.c", + "ps3": "as3vm_interpreter.c", + "vita": "as3vm_interpreter.c", +} +# The reconstructed source embeds the canonical Windows source path. +CANONICAL_PROVENANCE = r"c:\projects\g\src\flash\as3vm_interpreter.c" + + +def normalized_symbol(name): + name = name.removeprefix(".") + name = name.removeprefix("__imp_") + if name == "_setjmp": + return "setjmp" + return name + + +class ReconstructedAs3vmInterpreterCrossTests(unittest.TestCase): + def test_native_provenance_counts_source_path_and_inventory(self): + # (function count, raw line count) per platform, from + # iggy_.modules.tsv column 3 and `wc -l` on the raw .c. The + # source_functions DIFFER (58/46/45/57) while the exports stay 37; vita + # fully expands the opcode switch (6621 lines). + expected = { + "durango": (58, 2613), "orbis": (46, 3234), + "ps3": (45, 2475), "vita": (57, 6621), + } + 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_interpreter\t")) + self.assertEqual(int(row.split("\t")[2]), functions, platform) + raw = ROOT / f"iggy-decompiled/raw/{platform}/as3vm_interpreter.c" + self.assertEqual( + len(raw.read_text(encoding="utf-8").splitlines()), + lines, platform) + obj = ROOT / ( + f"work/iggy-native-decomp/{platform}-objects/" + "as3vm_interpreter.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(PROVENANCE_STRINGS[platform], strings.stdout, platform) + + if SOURCE.exists(): + cmake = (RECONSTRUCTED / "CMakeLists.txt").read_text( + encoding="utf-8") + self.assertIn( + "iggy_reconstructed_as3vm_interpreter_${platform}", cmake) + self.assertIn( + "add_library(${target} STATIC as3vm_interpreter.c)", cmake) + self.assertIn( + "IGGY_RECONSTRUCT_AS3VM_INTERPRETER_${platform_upper}", cmake) + inventory = (RECONSTRUCTED / "modules.tsv").read_text( + encoding="utf-8") + # Exports are invariant (37) but retail source_functions vary + # (58/46/45/57), so the inventory uses per-platform rows exactly like + # as3vm_property / as3vm_imp_array (NOT a single combined row). + for platform, functions in ( + ("durango", 58), ("orbis", 46), + ("ps3", 45), ("vita", 57)): + self.assertIn( + f"as3vm_interpreter\t{functions}\t37\t{platform}\tcomplete", + inventory) + + def test_owned_data_globals_match_retail_initialized_bytes(self): + # The exact retail-recovered initialized bytes of the D sentinel globals + # (llvm-objdump -s -j .data) -- the VM value-cell + boxed-number + # sentinels every sibling module imports. Asserted on the durango object + # (little-endian, source of the model); uniform across the four. + obj = ROOT / ( + "work/iggy-native-decomp/durango-objects/as3vm_interpreter.obj") + dump = subprocess.run( + [str(LLVM_OBJDUMP), "-s", "-j", ".data", str(obj)], + capture_output=True, text=True) + self.assertEqual(dump.returncode, 0, dump.stderr) + blob = _parse_objdump_data(dump.stdout) + # Each D global's bytes appear contiguously in the .data image. + for name, want in OWNED_DATA_BYTES.items(): + self.assertIn(want, blob, name) + # must_init is the int32 1 sentinel. + self.assertIn(bytes([0x01, 0x00, 0x00, 0x00]), blob) + + 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_interpreter.c", + SOURCE.read_text(encoding="utf-8")) + + function_pointers = "".join( + f" (GenericFn)&{name},\n" for name in sorted(EXPECTED_FUNCTIONS)) + probe_text = ( + '#include \n#include "as3vm_interpreter.h"\n' + 'typedef void (*GenericFn)(void);\n' + 'static GenericFn interp_abis[] = {\n' + function_pointers + '};\n' + 'int use_abis(void) {\n' + ' for (unsigned i = 0; ' + 'i < sizeof(interp_abis) / sizeof(interp_abis[0]); ++i) {\n' + ' if (!interp_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_interpreter_{platform}.o" + build = subprocess.run([ + compiler, "-target", target, "-ffreestanding", + "-fno-builtin", + "-c", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", + *extra, + f"-DIGGY_RECONSTRUCT_AS3VM_INTERPRETER_{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_interpreter_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_INTERPRETER_" + f"{platform.upper()}=1", + "-I", str(RECONSTRUCTED), str(probe), + ], capture_output=True, text=True) + self.assertEqual(syntax.returncode, 0, + (platform, language, syntax.stderr)) + + +def _parse_objdump_data(text): + # Reassemble the raw bytes of a `llvm-objdump -s -j .data` hex dump. Each + # data line is ` `; split off + # the ascii gutter (two-space separator), drop the offset, keep the hex. + out = bytearray() + for line in text.splitlines(): + if " " not in line: + continue + hex_region = line.split(" ", 1)[0] + tokens = hex_region.split() + if len(tokens) < 2: + continue + try: + int(tokens[0], 16) + except ValueError: + continue + for token in tokens[1:]: + if (token and len(token) % 2 == 0 and + all(c in "0123456789abcdefABCDEF" for c in token)): + out.extend(bytes.fromhex(token)) + return bytes(out) + + +if __name__ == "__main__": + unittest.main()