rebuild iggy's native avm2 interpreter core
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
|
||||
450
iggy-decompiled/src/reconstructed/as3vm_interpreter.c
Normal file
450
iggy-decompiled/src/reconstructed/as3vm_interpreter.c
Normal file
@@ -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 <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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"
|
||||
118
iggy-decompiled/src/reconstructed/as3vm_interpreter.h
Normal file
118
iggy-decompiled/src/reconstructed/as3vm_interpreter.h
Normal file
@@ -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 <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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 */
|
||||
479
iggy-decompiled/src/reconstructed/as3vm_interpreter_call.inc
Normal file
479
iggy-decompiled/src/reconstructed/as3vm_interpreter_call.inc
Normal file
@@ -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);
|
||||
}
|
||||
@@ -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.<T> 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.<T> (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;
|
||||
}
|
||||
2373
iggy-decompiled/src/reconstructed/as3vm_interpreter_dispatch.inc
Normal file
2373
iggy-decompiled/src/reconstructed/as3vm_interpreter_dispatch.inc
Normal file
File diff suppressed because it is too large
Load Diff
261
iggy-decompiled/src/reconstructed/as3vm_interpreter_equality.inc
Normal file
261
iggy-decompiled/src/reconstructed/as3vm_interpreter_equality.inc
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
545
iggy-decompiled/src/reconstructed/as3vm_interpreter_globals.inc
Normal file
545
iggy-decompiled/src/reconstructed/as3vm_interpreter_globals.inc
Normal file
@@ -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<T> 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<T> 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<d) * (int)(longlong)d`. */
|
||||
#if IGGY_AS3_64BIT
|
||||
return (d == (double)((int64_t)d & 0xffffffff)) ? 1 : 0;
|
||||
#else
|
||||
return (d == (double)((uint32_t)(0.0 < d) * (int)d)) ? 1 : 0;
|
||||
#endif
|
||||
}
|
||||
return 0;
|
||||
case 0x0c:
|
||||
return (TAG(v) == 1 && TYPEB(v) == '\f') ? 1 : 0;
|
||||
case 0x12:
|
||||
return (TAG(v) == 1 && TYPEB(v) == 0x12) ? 1 : 0;
|
||||
case 0x14:
|
||||
return (TAG(v) == 1 && (TYPEB(v) & 0x10) != 0) ? 1 : 0;
|
||||
case 0x20:
|
||||
if (TAG(v) == 1 && TYPEB(v) == ' ') {
|
||||
/* Vector<T> 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
|
||||
}
|
||||
}
|
||||
404
iggy-decompiled/src/reconstructed/as3vm_interpreter_support.inc
Normal file
404
iggy-decompiled/src/reconstructed/as3vm_interpreter_support.inc
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
140
tests/support/as3vm_interpreter_test_support.c
Normal file
140
tests/support/as3vm_interpreter_test_support.c
Normal file
@@ -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 <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* ---- 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;
|
||||
}
|
||||
459
tests/test_reconstructed_as3vm_interpreter.py
Normal file
459
tests/test_reconstructed_as3vm_interpreter.py
Normal file
@@ -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("<d", (ctypes.c_uint8 * 8).from_address(box + 8), 0,
|
||||
value)
|
||||
return box
|
||||
|
||||
def _vm(self, size=0x6C00):
|
||||
return self._buffer(size)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Trivial deterministic ops
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_do_validate_returns_one(self):
|
||||
# raw:941 as3vm_do_validate is a hard `return 1`.
|
||||
self.assertEqual(self.lib.as3vm_do_validate(), 1)
|
||||
|
||||
def test_notice_gc_and_request_collection_are_noops(self):
|
||||
# raw:1281-1295 both are empty `return;` stubs; calling must not crash.
|
||||
self.assertIsNone(self.lib.as3vm_interpreter_notice_gc())
|
||||
self.assertIsNone(self.lib.as3vm_interpreter_request_collection())
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# as3vm_is_nan / as3vm_is_inf -- boxed-double classification
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_is_nan_classifies_the_boxed_double(self):
|
||||
# raw:1320-1323 NAN(*(box+8)) -> 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()
|
||||
492
tests/test_reconstructed_as3vm_interpreter_cross.py
Normal file
492
tests/test_reconstructed_as3vm_interpreter_cross.py
Normal file
@@ -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/<platform>-objects/as3vm_interpreter.obj, verified with
|
||||
/opt/homebrew/opt/llvm/bin/llvm-nm and `nm -g`):
|
||||
|
||||
EXPECTED_FUNCTIONS = `llvm-nm --extern-only --defined-only <obj>` 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_<platform>.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 <stdint.h>\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 ` <offset> <grp> <grp> <grp> <grp> <ascii-gutter>`; 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()
|
||||
Reference in New Issue
Block a user