rebuild iggy's as3vm garbage collector
This commit is contained in:
@@ -441,6 +441,22 @@ foreach(platform IN ITEMS durango orbis ps3 vita)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
foreach(platform IN ITEMS durango orbis ps3 vita)
|
||||
string(TOUPPER ${platform} platform_upper)
|
||||
set(target iggy_reconstructed_as3vm_gc_${platform})
|
||||
add_library(${target} STATIC as3vm_gc.c)
|
||||
target_compile_definitions(
|
||||
${target}
|
||||
PRIVATE IGGY_RECONSTRUCT_AS3VM_GC_${platform_upper}
|
||||
)
|
||||
configure_iggy_reconstructed_target(${target} c_std_11)
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
|
||||
target_compile_options(${target} PRIVATE -fno-builtin)
|
||||
elseif(MSVC)
|
||||
target_compile_options(${target} PRIVATE /Oi-)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
add_library(iggy_reconstructed_win32_rrThreads2_durango STATIC win32_rrThreads2.c)
|
||||
target_compile_definitions(
|
||||
iggy_reconstructed_win32_rrThreads2_durango
|
||||
|
||||
@@ -414,6 +414,26 @@ the exact import set were verified against the retail object disassembly on all
|
||||
four builds. The GC allocator, write barrier, and interning primitives remain
|
||||
imported.
|
||||
|
||||
`as3vm_gc.c` restores the AS3/AVM2 garbage collector — an incremental
|
||||
generational tri-color mark-sweep with a young nursery, a tenured old
|
||||
generation, a remembered set, and time-sliced collection. Its four builds
|
||||
(52/37/40/35 recovered functions, split into alloc/mark/sweep/tenure/validate
|
||||
includes) expose all 14 collector entry points and own the `iggy_debug_entity`
|
||||
and `updated_object` tracking pointers. It reconstructs the young bump allocator
|
||||
with its even-word 16-byte alignment and oversize fallback, the segregated
|
||||
old-generation free lists with boundary-tag coalescing, the tri-color marking
|
||||
core (white/grey/black via the low flag bits, the grey stack, and the full root
|
||||
scan over classes, the player class table, VM value slots, weak tables, and the
|
||||
five stage trees), the Dijkstra write barrier that logs old-to-young stores into
|
||||
the remembered set, the incremental step state machine with its `rrGetTime`
|
||||
budget, the collect/sweep path (freeing unmarked blocks, running finalizers,
|
||||
integrating display-object destruction and movie-dirty tracking), object tenuring
|
||||
with pointer forwarding, and the release-mode heap validators. The 64-bit and
|
||||
32-bit GC-state, node-header, and free-list layouts, the exact flag transitions
|
||||
and sweep predicate, and the import set were verified against the retail object
|
||||
disassembly on all four builds; the interpreter, finalizer, and allocator
|
||||
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.
|
||||
|
||||
435
iggy-decompiled/src/reconstructed/as3vm_gc.c
Normal file
435
iggy-decompiled/src/reconstructed/as3vm_gc.c
Normal file
@@ -0,0 +1,435 @@
|
||||
/* Reconstructed translation unit for RAD's Iggy AS3 / AVM2 garbage collector. */
|
||||
/* Source-equivalent recovery of c:\projects\g\src\flash\as3vm_gc.c */
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "as3vm_gc.h"
|
||||
|
||||
#if (defined(IGGY_RECONSTRUCT_AS3VM_GC_DURANGO) + \
|
||||
defined(IGGY_RECONSTRUCT_AS3VM_GC_ORBIS) + \
|
||||
defined(IGGY_RECONSTRUCT_AS3VM_GC_PS3) + \
|
||||
defined(IGGY_RECONSTRUCT_AS3VM_GC_VITA)) != 1
|
||||
#error "select exactly one as3vm_gc 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_GC_DURANGO) || \
|
||||
defined(IGGY_RECONSTRUCT_AS3VM_GC_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_gc_source_path[] =
|
||||
"c:\\projects\\g\\src\\flash\\as3vm_gc.c";
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Recovered external dependencies (the genuine nm ' U ' imports). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/* allocation core (iggy_allocation -- satisfied). */
|
||||
extern void *iggy_arena_malloc(void *player, void *owner, int size, int tag);
|
||||
extern void *iggy_malloc_CANFAIL_fileline(intptr_t player, int size,
|
||||
uint32_t tag, const char *file,
|
||||
int line);
|
||||
extern void iggy_free_alloc(intptr_t player, void *block);
|
||||
|
||||
/* interpreter / class collaborators (as3vm_interp / as3vm_class -- unbuilt). */
|
||||
extern void as3vm_finalize(void *vm, void *object);
|
||||
extern void as3vm_interpreter_notice_gc(int phase);
|
||||
extern void as3vm_interpreter_request_collection(void *vm);
|
||||
extern int as3vm_needs_cleaning(void *vm, void *object);
|
||||
|
||||
/* swf_play / display collaborators (unbuilt). */
|
||||
extern void iggy_mark_movie_dirty(void *player, void *object);
|
||||
extern void iggy_clear_movie_dirty(void *player, void *object);
|
||||
extern void iggy_destroy_sprite(void *player, void *sprite);
|
||||
|
||||
/* timing (win32_rrTime -- satisfied). */
|
||||
extern uint64_t rrGetTime(void);
|
||||
extern uint64_t rrTimeToMicros(int64_t ticks);
|
||||
|
||||
/* libc (referenced by every target). */
|
||||
extern void *memcpy(void *dst, const void *src, size_t n);
|
||||
extern void *memset(void *dst, int value, size_t n);
|
||||
extern void qsort(void *base, size_t nmemb, size_t size,
|
||||
int (*compar)(const void *, const void *));
|
||||
extern void longjmp(void *env, int value);
|
||||
|
||||
/* null/undefined sentinel DATA symbol (referenced only by address). */
|
||||
extern uint8_t as3vm_nullundef_storage[];
|
||||
|
||||
/* telemetry DATA pointers (referenced by address; NULL-guarded). */
|
||||
extern void *iggy_tm_internal;
|
||||
extern void *iggy_tm_normal;
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Owned globals (present on all four objects, start NULL). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/* set by update_old_object on the tenure/promotion path (raw:2970). */
|
||||
void *updated_object;
|
||||
/* set by the sweeper to the last detached / swept block (raw:1507). */
|
||||
void *iggy_debug_entity;
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Retail file-statics. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int num_gc_steps;
|
||||
|
||||
/* gc_warn / gc_notify event tags (indexed by phase / warning code). */
|
||||
static const char *const event_names[8] = {
|
||||
"(iggy/gc)tenure", "(iggy/gc)mark", "(iggy/gc)mark_roots",
|
||||
"(iggy/gc)pre_sweep", "(iggy/gc)sweep", "(iggy/gc)grey_list_overflow",
|
||||
"(iggy/gc)remembered", "(iggy/gc)pool",
|
||||
};
|
||||
|
||||
/* the tagged undefined / null sentinels (address of the nullundef storage). */
|
||||
#define GC_UNDEF ((void *)&as3vm_nullundef_storage[9])
|
||||
#define GC_NULLV ((void *)&as3vm_nullundef_storage[1])
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Machine widths + node header layout. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
#if IGGY_AS3_64BIT
|
||||
#define WORD 8
|
||||
#define MIN_BLOCK 0x20u
|
||||
#define N_NEXT 0x10 /* free-list forward link */
|
||||
#else
|
||||
#define WORD 4
|
||||
#define MIN_BLOCK 0x18u
|
||||
#define N_NEXT 0x0c
|
||||
#endif
|
||||
#define N_KIND 0 /* node[0] kind byte */
|
||||
#define N_FLAGS 1 /* node[1] flags byte */
|
||||
#define N_SIZE 4 /* node+4 block byte size (u32) */
|
||||
#define N_PREV 8 /* free-list backward link */
|
||||
|
||||
/* flags byte (node[1]). */
|
||||
#define F_MARKLO 0x01
|
||||
#define F_REMEMBERED 0x04
|
||||
#define F_GREY 0x08
|
||||
#define F_MARKING 0x18
|
||||
#define F_OLD 0x20
|
||||
#define F_FREE 0x40
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* VM-context byte offsets recovered from raw (64-bit durango / 32-bit */
|
||||
/* vita). The GC state is a contiguous region inside the VM context; */
|
||||
/* the 32-bit column is the ILP32 layout the same source lowers to. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
#if IGGY_AS3_64BIT
|
||||
/* general VM/player-context fields. */
|
||||
#define VM_MARK_WORK 0x58 /* pending incremental-mark work head (vm[0xb]) */
|
||||
#define VM_FINAL_A 0xd8 /* finalizable list A (vm[0x1b]) */
|
||||
#define VM_FINAL_B 0xe0 /* finalizable list B (vm[0x1c]) */
|
||||
#define VM_ROOTBLK 0xd0 /* misc root list (vm[0x1a]) */
|
||||
#define VM_CLEAR_A 0x40 /* zeroed by clear (vm[8]) */
|
||||
#define VM_CLEAR_C 0x70 /* zeroed by clear (vm[0xe]) */
|
||||
#define VM_OLD_POOL_CFG 0xbc /* old-pool size config (u32) */
|
||||
/* GC-state region. */
|
||||
#define G_OLD_FORCED 0x6978
|
||||
#define G_GREY_TOP 0x6980
|
||||
#define G_GREY_LIMIT 0x6988
|
||||
#define G_GREY_BASE 0x6990
|
||||
#define G_GREY_COUNT 0x6998
|
||||
#define G_GROW_FLAG 0x699c
|
||||
#define G_SCAN_WORDS 0x69a0
|
||||
#define G_OLD_BYTES 0x69a4
|
||||
#define G_POOL_HEAD 0x69a8
|
||||
#define G_POOL_COUNT 0x69b0
|
||||
#define G_POOL_BYTES 0x69b4
|
||||
#define G_FREELIST 0x69b8 /* freelist[16] */
|
||||
#define G_OLD_BLKS 0x6a38
|
||||
#define G_OLD_LIVE 0x6a3c
|
||||
#define G_OLD_SNAP 0x6a40
|
||||
#define G_POOL_FLAGS 0x6a44
|
||||
#define G_POOL_AVAIL 0x6a45
|
||||
#define G_IN_GC 0x6a46
|
||||
#define G_ALLOC_BYTES 0x6a48
|
||||
#define G_STAT_A 0x6a50
|
||||
#define G_STAT_B 0x6a58
|
||||
#define G_STEP_START 0x6a60
|
||||
#define G_STAT_MICROS 0x6a68
|
||||
#define G_NOTIFY_CB 0x6a70
|
||||
#define G_RATIO_SIGN 0x6a78
|
||||
#define G_RATIO_A 0x6a80
|
||||
#define G_RATIO_B 0x6a84
|
||||
#define G_RATIO_C 0x6a88
|
||||
#define G_STEP_BUDGET 0x6a7c
|
||||
#define G_SWEEP_BLOCK 0x6a90
|
||||
#define G_SWEEP_POOL 0x6a98
|
||||
#define G_BIGARR_CHAIN 0x6aa0
|
||||
#define G_REM_APPEND 0x6aa8
|
||||
#define G_REM_BASE 0x6ab0
|
||||
#define G_REM_LIMIT 0x6ab8
|
||||
#define G_REM_END 0x6ac0
|
||||
#define G_REM_OVF 0x6ac8
|
||||
#define G_YOUNG_BUMP 0x6ad0
|
||||
#define G_YOUNG_LIMIT 0x6ad8
|
||||
#define G_YOUNG_BASE 0x6ae0
|
||||
#define G_YOUNG_SIZE 0x6af0
|
||||
#define G_UPDATE_CHAIN 0x6af8
|
||||
#define G_CLASS_CHAIN 0x6b00
|
||||
#define G_STACK2_TOP 0x6b10
|
||||
#define G_STACK2_LIMIT 0x6b18
|
||||
/* player-relative offsets (added to *(vm)). */
|
||||
#define P_MOVIE_A 0x2660
|
||||
#define P_MOVIE_B 0x24c0
|
||||
#define P_SPRITES 0x24b8
|
||||
#define P_DIRTY 0x2958
|
||||
#define P_STAGE0 0x29a0
|
||||
#define P_STAGE1 0x29a8
|
||||
#define P_STAGE2 0x29b0
|
||||
#define P_STAGE3 0x29b8
|
||||
#define P_STAGE4 0x29f0
|
||||
#define P_ERRCODE 0x3538
|
||||
#define P_JMPIDX 0x3542
|
||||
#define P_JMPBASE 0x3550
|
||||
#define P_JMPSTRIDE 0x100
|
||||
/* value stack (interpreter operand stack scanned by mark_stack). */
|
||||
#define VM_OPSTACK_TOP 0x28f0
|
||||
#define VM_OPSTACK_LIMIT 0x2900
|
||||
#else
|
||||
/* general VM/player-context fields. */
|
||||
#define VM_MARK_WORK 0x34
|
||||
#define VM_FINAL_A 0x8c
|
||||
#define VM_FINAL_B 0x90
|
||||
#define VM_ROOTBLK 0x88
|
||||
#define VM_CLEAR_A 0x28
|
||||
#define VM_CLEAR_C 0x40
|
||||
#define VM_OLD_POOL_CFG 0x74
|
||||
/* GC-state region. */
|
||||
#define G_OLD_FORCED 0x4730
|
||||
#define G_GREY_TOP 0x4738
|
||||
#define G_GREY_LIMIT 0x473c
|
||||
#define G_GREY_BASE 0x4740
|
||||
#define G_GREY_COUNT 0x4744
|
||||
#define G_GROW_FLAG 0x4748
|
||||
#define G_SCAN_WORDS 0x474c
|
||||
#define G_OLD_BYTES 0x4750
|
||||
#define G_POOL_HEAD 0x4754
|
||||
#define G_POOL_COUNT 0x4758
|
||||
#define G_POOL_BYTES 0x475c
|
||||
#define G_FREELIST 0x4760 /* freelist[16] */
|
||||
#define G_OLD_BLKS 0x47a0
|
||||
#define G_OLD_LIVE 0x47a4
|
||||
#define G_OLD_SNAP 0x47a8
|
||||
#define G_POOL_FLAGS 0x47ac
|
||||
#define G_POOL_AVAIL 0x47ad
|
||||
#define G_IN_GC 0x47ae
|
||||
#define G_ALLOC_BYTES 0x47b0
|
||||
#define G_STAT_A 0x47b8
|
||||
#define G_STAT_B 0x47c0
|
||||
#define G_STEP_START 0x47c8
|
||||
#define G_STAT_MICROS 0x47d0
|
||||
#define G_NOTIFY_CB 0x47d8
|
||||
#define G_RATIO_SIGN 0x47dc
|
||||
#define G_STEP_BUDGET 0x47e0
|
||||
#define G_RATIO_A 0x47e4
|
||||
#define G_RATIO_B 0x47e8
|
||||
#define G_RATIO_C 0x47ec
|
||||
#define G_SWEEP_BLOCK 0x47f0
|
||||
#define G_SWEEP_POOL 0x47f4
|
||||
#define G_BIGARR_CHAIN 0x47f8
|
||||
#define G_REM_APPEND 0x47fc
|
||||
#define G_REM_BASE 0x4800
|
||||
#define G_REM_LIMIT 0x4804
|
||||
#define G_REM_END 0x4808
|
||||
#define G_REM_OVF 0x480c
|
||||
#define G_YOUNG_BUMP 0x4810
|
||||
#define G_YOUNG_LIMIT 0x4814
|
||||
#define G_YOUNG_BASE 0x4818
|
||||
#define G_YOUNG_SIZE 0x4820
|
||||
#define G_UPDATE_CHAIN 0x4824
|
||||
#define G_CLASS_CHAIN 0x4828
|
||||
#define G_STACK2_TOP 0x4830
|
||||
#define G_STACK2_LIMIT 0x4834
|
||||
/* player-relative offsets (added to *(vm)). */
|
||||
#define P_MOVIE_A 0x2024
|
||||
#define P_MOVIE_B 0x1efc
|
||||
#define P_SPRITES 0x1ef8
|
||||
#define P_DIRTY 0x2100
|
||||
#define P_STAGE0 0x2150
|
||||
#define P_STAGE1 0x2158
|
||||
#define P_STAGE2 0x2160
|
||||
#define P_STAGE3 0x2168
|
||||
#define P_STAGE4 0x2190
|
||||
#define P_ERRCODE 0x2bc4
|
||||
#define P_JMPIDX 0x2bce
|
||||
#define P_JMPBASE 0x2b18
|
||||
#define P_JMPSTRIDE 0xc0
|
||||
/* value stack. */
|
||||
#define VM_OPSTACK_TOP 0x1c60
|
||||
#define VM_OPSTACK_LIMIT 0x1c68
|
||||
#endif
|
||||
|
||||
/* telemetry vtable slot offsets (guarded, never entered in the harness). */
|
||||
#if IGGY_AS3_64BIT
|
||||
#define TM_BEGIN 0xa0
|
||||
#define TM_END 0xa8
|
||||
#define TM_LOG 0x148
|
||||
#else
|
||||
#define TM_BEGIN 0x50
|
||||
#define TM_END 0x54
|
||||
#define TM_LOG 0xa4
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Context field accessors. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
#define VMB(vm, off) (*(uint8_t *)((uint8_t *)(vm) + (off)))
|
||||
#define VMU16(vm, off) (*(uint16_t *)((uint8_t *)(vm) + (off)))
|
||||
#define VMU32(vm, off) (*(uint32_t *)((uint8_t *)(vm) + (off)))
|
||||
#define VMI32(vm, off) (*(int32_t *)((uint8_t *)(vm) + (off)))
|
||||
#define VMF(vm, off) (*(float *)((uint8_t *)(vm) + (off)))
|
||||
#define VMI64(vm, off) (*(int64_t *)((uint8_t *)(vm) + (off)))
|
||||
#define VMU64(vm, off) (*(uint64_t *)((uint8_t *)(vm) + (off)))
|
||||
#define VMP(vm, off) (*(uint8_t **)((uint8_t *)(vm) + (off)))
|
||||
#define VMU(vm, off) (*(uintptr_t *)((uint8_t *)(vm) + (off)))
|
||||
#define PLAYER(vm) (*(uint8_t **)(vm))
|
||||
|
||||
/* node byte / word accessors. */
|
||||
#define NB(node, i) (((uint8_t *)(node))[(i)])
|
||||
#define NU16(node, i) (*(uint16_t *)((uint8_t *)(node) + (i)))
|
||||
#define NU32(node, i) (*(uint32_t *)((uint8_t *)(node) + (i)))
|
||||
#define NI32(node, i) (*(int32_t *)((uint8_t *)(node) + (i)))
|
||||
#define NP(node, i) (*(uint8_t **)((uint8_t *)(node) + (i)))
|
||||
|
||||
/* value tag test: heap value == (node + 1) with low bit set. */
|
||||
#define IS_HEAP(v) (((uintptr_t)(v) & 3u) == 1u)
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Forward declarations for the file-static collector graph. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
typedef void (*gc_visit_fn)(void *vm, void *slot);
|
||||
|
||||
static uint32_t gc_size_class(uint32_t size);
|
||||
static void gc_grow_grey_stack(void *vm);
|
||||
static void gc_grey_push(void *vm, uint8_t *node);
|
||||
static void gc_warn(void *vm, uint32_t code);
|
||||
static void gc_notify(void *vm, int event, int amount, uint32_t a, uint32_t b);
|
||||
|
||||
static void old_alloc_unlink(void *vm, uint8_t *block);
|
||||
static void old_alloc_create_freeblock(void *vm, uint8_t *block, uint32_t size);
|
||||
static uint8_t *as3vm_old_block_alloc_raw(void *vm, uint32_t size);
|
||||
static uint8_t *as3vm_old_alloc(void *vm, uint32_t words);
|
||||
static uint8_t *as3vm_old_pool_alloc(void *vm, int words);
|
||||
static uint8_t *as3vm_old_pool_init(void *vm, uint8_t *heap);
|
||||
static void as3vm_old_pools_sort(void *vm);
|
||||
static int pointer_compare(const void *a, const void *b);
|
||||
static void add_to_remembered(void *vm, uint8_t *node);
|
||||
|
||||
static void maybe_greyv(void *vm, uint8_t *value);
|
||||
static void maybe_grey(void *vm, void *slot);
|
||||
static int mark_increment(void *vm, uint32_t budget);
|
||||
static void mark_stack(void *vm);
|
||||
static void mark_roots(void *vm);
|
||||
static void mark_grey_movies(void *vm, uint8_t *movie);
|
||||
static int movie_reachable(void *vm, uint8_t *movie);
|
||||
static void visit_class(void *vm, uint8_t *klass, gc_visit_fn fn, int full);
|
||||
static void visit_traits(void *vm, uint8_t *traits, gc_visit_fn fn,
|
||||
uint8_t *owner);
|
||||
static void visit_roots(void *vm, gc_visit_fn fn, int full);
|
||||
static void visit_movies(void *vm, gc_visit_fn fn, uint8_t *movie);
|
||||
|
||||
static uint8_t *as3vm_old_pool_sweep(void *vm, uint8_t *pool, uint8_t *cursor,
|
||||
int budget, int *consumed);
|
||||
static void as3vm_old_sweep_faux(void *vm);
|
||||
static void as3vm_sweep(void *vm);
|
||||
static int old_sweep_increment(void *vm, int budget);
|
||||
static void old_sweep_add_grey_items(void *vm);
|
||||
static void old_process_remembered_set_overflow(void *vm);
|
||||
static void pre_sweep(void *vm);
|
||||
static int count_movies_rec(uint8_t *movie, int count);
|
||||
static void clear_all_movies_post_sweep(void *vm);
|
||||
static void clear_movies_post_sweep(void *vm, uint8_t *movie);
|
||||
static void clear_movies_processed(void *vm, uint8_t *movie);
|
||||
static void gc_collect_raw(void *vm);
|
||||
static void gc_step(void *vm);
|
||||
|
||||
static void update_location(void *vm, void *slot);
|
||||
static void update_old_object(void *vm, uint8_t *node);
|
||||
static void clear_movies_post_tenure(void *vm, uint8_t *movie, int *count);
|
||||
static void tenure(void *vm);
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Telemetry helpers (NULL-guarded; never entered under the harness). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void gc_tm_enter(int line, const char *tag) {
|
||||
(void)line;
|
||||
(void)tag;
|
||||
if (iggy_tm_normal != NULL) {
|
||||
void (*fn)(void *) =
|
||||
*(void (**)(void *))((uint8_t *)iggy_tm_normal + TM_BEGIN);
|
||||
fn(iggy_tm_normal);
|
||||
}
|
||||
}
|
||||
|
||||
static void gc_tm_leave(int line) {
|
||||
(void)line;
|
||||
if (iggy_tm_normal != NULL) {
|
||||
void (*fn)(void *) =
|
||||
*(void (**)(void *))((uint8_t *)iggy_tm_normal + TM_END);
|
||||
fn(iggy_tm_normal);
|
||||
}
|
||||
}
|
||||
|
||||
static void gc_tm_log(int a, int b) {
|
||||
(void)a;
|
||||
(void)b;
|
||||
if (iggy_tm_internal != NULL) {
|
||||
void (*fn)(void *) =
|
||||
*(void (**)(void *))((uint8_t *)iggy_tm_internal + TM_LOG);
|
||||
fn(iggy_tm_internal);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Shared low-level helpers. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/* Segregated free-list size class for a byte size (0..15). */
|
||||
static uint32_t gc_size_class(uint32_t size) {
|
||||
uint32_t klass = (size - 0x18u) >> 3;
|
||||
if (klass > 7u) {
|
||||
klass = size >> 8;
|
||||
if (klass > 7u) {
|
||||
klass = 7u;
|
||||
}
|
||||
klass += 8u;
|
||||
}
|
||||
return klass;
|
||||
}
|
||||
|
||||
/* Push a value node onto the grey stack (grows on overflow). */
|
||||
static void gc_grey_push(void *vm, uint8_t *node) {
|
||||
if (VMU(vm, G_GREY_LIMIT) <= VMU(vm, G_GREY_TOP)) {
|
||||
gc_grow_grey_stack(vm);
|
||||
}
|
||||
if (NB(node, N_KIND) > 0x2a) {
|
||||
__builtin_trap();
|
||||
}
|
||||
*(uint8_t **)VMP(vm, G_GREY_TOP) = node;
|
||||
VMP(vm, G_GREY_TOP) += WORD;
|
||||
}
|
||||
|
||||
#include "as3vm_gc_alloc.inc"
|
||||
#include "as3vm_gc_mark.inc"
|
||||
#include "as3vm_gc_sweep.inc"
|
||||
#include "as3vm_gc_tenure.inc"
|
||||
#include "as3vm_gc_validate.inc"
|
||||
77
iggy-decompiled/src/reconstructed/as3vm_gc.h
Normal file
77
iggy-decompiled/src/reconstructed/as3vm_gc.h
Normal file
@@ -0,0 +1,77 @@
|
||||
#ifndef IGGY_RECONSTRUCTED_AS3VM_GC_H
|
||||
#define IGGY_RECONSTRUCTED_AS3VM_GC_H
|
||||
|
||||
/*
|
||||
* Reconstructed public ABI for RAD's as3vm_gc translation unit -- Iggy's
|
||||
* AS3 / AVM2 garbage collector.
|
||||
*
|
||||
* Source-equivalent recovery of c:\projects\g\src\flash\as3vm_gc.c
|
||||
*
|
||||
* as3vm_gc is an incremental, generational mark-sweep collector:
|
||||
*
|
||||
* - a young NURSERY served by a bump allocator (round-up-to-even-words = a
|
||||
* 16-byte-aligned minimum block), overflowing to an OLD generation of
|
||||
* segregated free-list pools;
|
||||
* - a tri-colour (white / grey / black) Dijkstra write barrier -- the flags
|
||||
* byte carries 0x01 mark-low, 0x08 grey/queued, 0x18 marking mask, 0x20
|
||||
* old-generation, 0x04 in-remembered-set, 0x40 free/detached; old->young
|
||||
* stores are logged into a remembered set;
|
||||
* - time-sliced incremental marking (gc_step) driven by rrGetTime, with a
|
||||
* grey stack that grows on overflow;
|
||||
* - a tenure/promotion pass that copies survivors into the old generation,
|
||||
* coalescing sweep of the old pools, and finalization of dead objects;
|
||||
* - a family of release-mode heap validators.
|
||||
*
|
||||
* The module exports exactly 14 functions and owns exactly two externally
|
||||
* named pointer globals -- updated_object (the node currently being promoted
|
||||
* by the tenure path) and iggy_debug_entity (the last block touched by the
|
||||
* sweeper). Both are file-scope void* and therefore zero-initialised.
|
||||
*
|
||||
* VALUE TAGGING (shared with as3vm_create): a small integer boxes to
|
||||
* (value << 1) with a clear low bit; a heap value is (node + 1) with a set low
|
||||
* bit -- nodes are >= 16-byte aligned so the tag is unambiguous. node[0] is
|
||||
* the kind byte, node[1] the flags byte.
|
||||
*
|
||||
* The 64-bit DURANGO / ORBIS ABI is LP64; PS3 / VITA are ILP32 (PS3 big
|
||||
* endian). The exported symbol names are invariant across all four targets.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Young bump / old-pool allocation. The real ABI returns the node pointer
|
||||
* (alloc_remembered consumes it); Ghidra typed these 'void'. */
|
||||
void *as3vm_gc_alloc(void *vm, uint32_t words);
|
||||
void *as3vm_gc_alloc_remembered(void *vm, uint32_t words);
|
||||
|
||||
/* Heap lifecycle. */
|
||||
uint64_t as3vm_gc_init(void *vm, void *config, void *heap);
|
||||
void as3vm_gc_clear(void *vm);
|
||||
|
||||
/* Incremental + full collection. */
|
||||
void as3vm_gc_step(void *vm);
|
||||
void as3vm_gc_collect(void *vm);
|
||||
void as3vm_gc_collect_exact(void *vm);
|
||||
|
||||
/* Old-generation free-list release + coalesce. */
|
||||
void *as3vm_old_block_free(void *vm, void *block, uint32_t size);
|
||||
|
||||
/* Generational write barrier (slow path). */
|
||||
void as3vm_write_barrier_slowpath(void *vm, void *container, void *value);
|
||||
|
||||
/* Release-mode heap validators. */
|
||||
void as3vm_validate_memory(void);
|
||||
void as3vm_validate_mid_sweep(void *vm);
|
||||
void as3vm_validate_old_heap(void *vm);
|
||||
void as3vm_validate_post_sweep(void *vm);
|
||||
void validate_onstage_movies(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* IGGY_RECONSTRUCTED_AS3VM_GC_H */
|
||||
511
iggy-decompiled/src/reconstructed/as3vm_gc_alloc.inc
Normal file
511
iggy-decompiled/src/reconstructed/as3vm_gc_alloc.inc
Normal file
@@ -0,0 +1,511 @@
|
||||
/* as3vm_gc: young bump allocator, old-generation segregated free-list pools,
|
||||
* heap init / clear, the generational write barrier, and the notify plumbing.
|
||||
*/
|
||||
|
||||
typedef void (*gc_notify_cb)(void *player, void *info);
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Notify / warn plumbing (the VM debug callback at G_NOTIFY_CB). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void gc_warn(void *vm, uint32_t code) {
|
||||
gc_notify_cb cb = *(gc_notify_cb *)((uint8_t *)vm + G_NOTIFY_CB);
|
||||
if (cb != NULL) {
|
||||
uint8_t info[0x50];
|
||||
memset(info, 0, sizeof(info));
|
||||
*(const char **)(info + 0x28) = event_names[code & 7u];
|
||||
*(uint32_t *)(info + 0x30) = code;
|
||||
cb(PLAYER(vm), info);
|
||||
}
|
||||
}
|
||||
|
||||
/* gc_notify (per-phase telemetry callback) is defined in the sweep .inc,
|
||||
* next to its gc_step / gc_collect_raw callers. */
|
||||
|
||||
/* Grow (reset) the grey stack after an overflow, warning once. */
|
||||
static void gc_grow_grey_stack(void *vm) {
|
||||
if (VMU32(vm, G_GROW_FLAG) == 0) {
|
||||
VMU32(vm, G_GROW_FLAG) = 1;
|
||||
gc_warn(vm, 5);
|
||||
}
|
||||
VMP(vm, G_GREY_TOP) =
|
||||
VMP(vm, G_GREY_BASE) + (uintptr_t)(VMU32(vm, G_GREY_COUNT) >> 1) * WORD;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Old-generation free-list primitives. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void old_alloc_unlink(void *vm, uint8_t *block) {
|
||||
if (NP(block, N_NEXT) != NULL) {
|
||||
NP(NP(block, N_NEXT), N_PREV) = NP(block, N_PREV);
|
||||
}
|
||||
if (NP(block, N_PREV) != NULL) {
|
||||
NP(NP(block, N_PREV), N_NEXT) = NP(block, N_NEXT);
|
||||
NP(block, N_PREV) = NULL;
|
||||
NP(block, N_NEXT) = NULL;
|
||||
return;
|
||||
}
|
||||
{
|
||||
uint32_t klass = gc_size_class(NU32(block, N_SIZE));
|
||||
VMP(vm, G_FREELIST + (uintptr_t)klass * WORD) = NP(block, N_NEXT);
|
||||
NP(block, N_PREV) = NULL;
|
||||
NP(block, N_NEXT) = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void old_alloc_create_freeblock(void *vm, uint8_t *block, uint32_t size) {
|
||||
uint8_t *old;
|
||||
uint32_t klass;
|
||||
memset(block, 0xf2, size);
|
||||
NB(block, N_FLAGS) |= F_FREE;
|
||||
NB(block, size + 1) |= 1;
|
||||
NU32(block, size - 4) = size;
|
||||
NU32(block, N_SIZE) = size;
|
||||
klass = gc_size_class(size);
|
||||
old = VMP(vm, G_FREELIST + (uintptr_t)klass * WORD);
|
||||
VMP(vm, G_FREELIST + (uintptr_t)klass * WORD) = block;
|
||||
NP(block, N_PREV) = NULL;
|
||||
NP(block, N_NEXT) = old;
|
||||
if (old != NULL) {
|
||||
NP(old, N_PREV) = block;
|
||||
}
|
||||
}
|
||||
|
||||
/* Best/exact-fit allocation out of the segregated old free lists. */
|
||||
static uint8_t *as3vm_old_block_alloc_raw(void *vm, uint32_t size) {
|
||||
uint32_t klass;
|
||||
uint8_t *blk;
|
||||
uint32_t search;
|
||||
|
||||
klass = (size - 0x18u) >> 3;
|
||||
if (klass > 7u) {
|
||||
klass = size >> 8;
|
||||
if (klass > 7u) {
|
||||
klass = 7u;
|
||||
}
|
||||
klass += 8u;
|
||||
}
|
||||
blk = VMP(vm, G_FREELIST + (uintptr_t)klass * WORD);
|
||||
if (blk != NULL) {
|
||||
if (klass < 8u) {
|
||||
old_alloc_unlink(vm, blk);
|
||||
NB(blk, N_FLAGS) &= (uint8_t)~F_FREE;
|
||||
NB(blk, NU32(blk, N_SIZE) + 1) &= (uint8_t)~1u;
|
||||
VMI32(vm, G_OLD_LIVE) += (int)size;
|
||||
VMI32(vm, G_OLD_BLKS) += 1;
|
||||
return blk;
|
||||
}
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
if (NU32(blk, N_SIZE) == size) {
|
||||
old_alloc_unlink(vm, blk);
|
||||
NB(blk, N_FLAGS) &= (uint8_t)~F_FREE;
|
||||
NB(blk, NU32(blk, N_SIZE) + 1) &= (uint8_t)~1u;
|
||||
VMI32(vm, G_OLD_LIVE) += (int)size;
|
||||
VMI32(vm, G_OLD_BLKS) += 1;
|
||||
return blk;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
search = (size + 8u) >> 3;
|
||||
if (search > 7u) {
|
||||
search = (size + 0x20u) >> 8;
|
||||
if (search > 7u) {
|
||||
search = 7u;
|
||||
}
|
||||
search += 8u;
|
||||
}
|
||||
if (search < 0x10u) {
|
||||
uint32_t k;
|
||||
for (k = search; k < 0x10u; ++k) {
|
||||
uint8_t *b;
|
||||
for (b = VMP(vm, G_FREELIST + (uintptr_t)k * WORD); b != NULL;
|
||||
b = NP(b, N_NEXT)) {
|
||||
uint32_t bsz = NU32(b, N_SIZE);
|
||||
if (size + 0x20u <= bsz) {
|
||||
old_alloc_unlink(vm, b);
|
||||
old_alloc_create_freeblock(vm, b + size, bsz - size);
|
||||
NB(b, N_FLAGS) &= (uint8_t)~F_FREE;
|
||||
NU32(b, N_SIZE) = size;
|
||||
NB(b + size, N_FLAGS) &= (uint8_t)~1u;
|
||||
VMI32(vm, G_OLD_LIVE) += (int)size;
|
||||
VMI32(vm, G_OLD_BLKS) += 1;
|
||||
return b;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Allocate a fresh on-demand old-generation pool. */
|
||||
static uint8_t *as3vm_old_pool_alloc(void *vm, int words) {
|
||||
uint32_t need = (uint32_t)words * 8u + 0x100u;
|
||||
uint32_t avail = VMU32(vm, G_POOL_BYTES) >> 2;
|
||||
uint32_t sz;
|
||||
uint32_t rounded;
|
||||
uint8_t *pool;
|
||||
uint8_t *data;
|
||||
|
||||
if (avail <= need) {
|
||||
avail = need;
|
||||
}
|
||||
sz = 0x2000u;
|
||||
if (0x2000u < avail) {
|
||||
sz = avail;
|
||||
}
|
||||
rounded = (sz + 7u) & 0xfffffff8u;
|
||||
pool = (uint8_t *)iggy_malloc_CANFAIL_fileline(
|
||||
(intptr_t)PLAYER(vm), (int)(rounded + 0x28u), 0xb, as3vm_gc_source_path,
|
||||
0x3cd);
|
||||
if (pool == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
*(uintptr_t *)(pool + WORD) = rounded;
|
||||
*(uint8_t **)pool = VMP(vm, G_POOL_HEAD);
|
||||
VMP(vm, G_POOL_HEAD) = pool;
|
||||
data = pool + 2 * WORD;
|
||||
old_alloc_create_freeblock(vm, data, rounded);
|
||||
NB(data, N_FLAGS) &= (uint8_t)~1u;
|
||||
NB(data + rounded, N_FLAGS) &= (uint8_t)~F_FREE;
|
||||
VMU32(vm, G_POOL_COUNT) += 1;
|
||||
VMU32(vm, G_POOL_BYTES) += rounded + 0x10u;
|
||||
as3vm_old_pools_sort(vm);
|
||||
return pool;
|
||||
}
|
||||
|
||||
/* Initialise the old generation from the optional preallocated heap. */
|
||||
static uint8_t *as3vm_old_pool_init(void *vm, uint8_t *heap) {
|
||||
uint32_t cfg = VMU32(vm, VM_OLD_POOL_CFG);
|
||||
if (cfg != 0) {
|
||||
uint8_t *pool;
|
||||
uint8_t *ret;
|
||||
uint32_t dsz;
|
||||
uint8_t *data;
|
||||
if (heap == NULL) {
|
||||
pool = (uint8_t *)iggy_arena_malloc(PLAYER(vm), NULL, (int)cfg, 0x4a);
|
||||
ret = NULL;
|
||||
} else {
|
||||
pool = heap;
|
||||
ret = heap + cfg;
|
||||
}
|
||||
*(uintptr_t *)(pool + WORD) = cfg;
|
||||
dsz = cfg - 0x28u;
|
||||
*(uint8_t **)pool = NULL;
|
||||
data = pool + 2 * WORD;
|
||||
old_alloc_create_freeblock(vm, data, dsz);
|
||||
NB(data, N_FLAGS) &= (uint8_t)~1u;
|
||||
NB(data + dsz, N_FLAGS) &= (uint8_t)~F_FREE;
|
||||
VMP(vm, G_POOL_HEAD) = pool;
|
||||
VMU32(vm, G_POOL_COUNT) = 1;
|
||||
VMU32(vm, G_POOL_BYTES) = dsz;
|
||||
VMU16(vm, G_POOL_FLAGS) = 0;
|
||||
return ret;
|
||||
}
|
||||
VMU16(vm, G_POOL_FLAGS) = 0x101;
|
||||
return heap;
|
||||
}
|
||||
|
||||
static int pointer_compare(const void *a, const void *b) {
|
||||
uintptr_t x = *(const uintptr_t *)a;
|
||||
uintptr_t y = *(const uintptr_t *)b;
|
||||
if (y > x) {
|
||||
return -1;
|
||||
}
|
||||
return (x > y) ? 1 : 0;
|
||||
}
|
||||
|
||||
/* Sort the old-pool list into ascending address order (for the sweeper). */
|
||||
static void as3vm_old_pools_sort(void *vm) {
|
||||
int n = VMI32(vm, G_POOL_COUNT);
|
||||
uint8_t **arr = (uint8_t **)__builtin_alloca((size_t)(n > 0 ? n : 1) *
|
||||
sizeof(uint8_t *));
|
||||
uint8_t *p = VMP(vm, G_POOL_HEAD);
|
||||
int i;
|
||||
for (i = 0; i < n; ++i) {
|
||||
arr[i] = p;
|
||||
p = *(uint8_t **)p;
|
||||
}
|
||||
qsort(arr, (size_t)(n > 0 ? n : 0), sizeof(uint8_t *), pointer_compare);
|
||||
for (i = n - 1; i >= 0; --i) {
|
||||
*(uint8_t **)arr[i] = p;
|
||||
p = arr[i];
|
||||
}
|
||||
VMP(vm, G_POOL_HEAD) = p;
|
||||
}
|
||||
|
||||
/* Old-gen object allocation: block from the free lists, else grow a pool,
|
||||
* else longjmp out on out-of-memory. */
|
||||
static uint8_t *as3vm_old_alloc(void *vm, uint32_t words) {
|
||||
uint32_t bytes;
|
||||
uint8_t *node;
|
||||
|
||||
VMU32(vm, G_SCAN_WORDS) += words;
|
||||
VMI64(vm, G_ALLOC_BYTES) += (int64_t)words;
|
||||
bytes = words * 8u;
|
||||
if (bytes < MIN_BLOCK) {
|
||||
bytes = MIN_BLOCK;
|
||||
}
|
||||
VMI32(vm, G_OLD_BYTES) += (int)bytes;
|
||||
node = as3vm_old_block_alloc_raw(vm, bytes);
|
||||
if (node == NULL) {
|
||||
if (VMB(vm, G_POOL_AVAIL) != 0) {
|
||||
as3vm_old_pool_alloc(vm, (int)words);
|
||||
}
|
||||
node = as3vm_old_block_alloc_raw(vm, bytes);
|
||||
if (node == NULL) {
|
||||
uint8_t *player = PLAYER(vm);
|
||||
uint32_t idx = (uint32_t)NB(player, P_JMPIDX);
|
||||
uint8_t *env = player + (uintptr_t)(idx - 1u) * P_JMPSTRIDE + P_JMPBASE;
|
||||
VMU32(player, P_ERRCODE) = 0x3e9;
|
||||
longjmp(env, 1);
|
||||
__builtin_trap();
|
||||
}
|
||||
}
|
||||
if (VMP(vm, G_SWEEP_BLOCK) != NULL &&
|
||||
(uintptr_t)VMP(vm, G_SWEEP_BLOCK) <= (uintptr_t)node) {
|
||||
NB(node, N_FLAGS) = 0x38;
|
||||
return node;
|
||||
}
|
||||
NB(node, N_FLAGS) = F_OLD;
|
||||
return node;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Remembered set append (old->young store logging). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void add_to_remembered(void *vm, uint8_t *node) {
|
||||
uintptr_t append;
|
||||
NB(node, N_FLAGS) |= F_REMEMBERED;
|
||||
*(uint8_t **)VMP(vm, G_REM_APPEND) = node;
|
||||
VMP(vm, G_REM_APPEND) += WORD;
|
||||
append = VMU(vm, G_REM_APPEND);
|
||||
if (VMU(vm, G_REM_LIMIT) <= append) {
|
||||
if (append == VMU(vm, G_REM_END)) {
|
||||
if (VMI32(vm, G_REM_OVF) == 0) {
|
||||
gc_warn(vm, 5);
|
||||
VMI32(vm, G_REM_OVF) = 1;
|
||||
}
|
||||
VMP(vm, G_REM_APPEND) = VMP(vm, G_REM_BASE);
|
||||
return;
|
||||
}
|
||||
if (append == VMU(vm, G_REM_LIMIT)) {
|
||||
as3vm_interpreter_request_collection(vm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Public allocation entry points. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void *as3vm_gc_alloc(void *vm, uint32_t words) {
|
||||
uint8_t *node;
|
||||
|
||||
if (VMI32(vm, G_OLD_FORCED) != 0) {
|
||||
node = as3vm_old_alloc(vm, words);
|
||||
NB(node, N_FLAGS) &= F_MARKLO;
|
||||
NB(node, N_FLAGS) |= F_OLD;
|
||||
return (void *)node;
|
||||
}
|
||||
|
||||
if (VMU(vm, G_YOUNG_LIMIT) <
|
||||
(uintptr_t)words * WORD + VMU(vm, G_YOUNG_BUMP)) {
|
||||
if (words > 0x80u) {
|
||||
return (void *)as3vm_old_alloc(vm, words);
|
||||
}
|
||||
} else if (VMU(vm, G_REM_APPEND) < VMU(vm, G_REM_LIMIT)) {
|
||||
goto bump;
|
||||
}
|
||||
if (VMP(vm, VM_MARK_WORK) != NULL) {
|
||||
tenure(vm);
|
||||
gc_step(vm);
|
||||
}
|
||||
bump:
|
||||
node = VMP(vm, G_YOUNG_BUMP);
|
||||
VMP(vm, G_YOUNG_BUMP) =
|
||||
node + (uintptr_t)(((words & 1u) + words) * (uint32_t)WORD);
|
||||
NB(node, N_FLAGS) = 0;
|
||||
return (void *)node;
|
||||
}
|
||||
|
||||
void *as3vm_gc_alloc_remembered(void *vm, uint32_t words) {
|
||||
uint8_t *node = (uint8_t *)as3vm_gc_alloc(vm, words);
|
||||
if (node != NULL && (NB(node, N_FLAGS) & F_OLD) != 0 &&
|
||||
VMI32(vm, G_OLD_FORCED) == 0) {
|
||||
NB(node, N_KIND) = 0x11;
|
||||
add_to_remembered(vm, node);
|
||||
}
|
||||
return (void *)node;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Old-block release (free-list merge / coalesce). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void *as3vm_old_block_free(void *vm, void *block_, uint32_t size) {
|
||||
uint8_t *block = (uint8_t *)block_;
|
||||
uint8_t *next;
|
||||
|
||||
VMI32(vm, G_OLD_BLKS) -= 1;
|
||||
VMI32(vm, G_OLD_LIVE) -= (int)size;
|
||||
VMI32(vm, G_OLD_BYTES) -= (int)size;
|
||||
|
||||
if ((NB(block, N_FLAGS) & 1) != 0) {
|
||||
block = block - NU32(block, (uint32_t)-4);
|
||||
old_alloc_unlink(vm, block);
|
||||
size += NU32(block, N_SIZE);
|
||||
}
|
||||
next = block + size;
|
||||
if ((NB(next, N_FLAGS) & F_FREE) != 0) {
|
||||
old_alloc_unlink(vm, next);
|
||||
size += NU32(next, N_SIZE);
|
||||
}
|
||||
old_alloc_create_freeblock(vm, block, size);
|
||||
return (void *)block;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Heap init / clear. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
uint64_t as3vm_gc_init(void *vm, void *config, void *heap) {
|
||||
uint8_t *cfg = (uint8_t *)config;
|
||||
uint8_t *pool;
|
||||
uint8_t *ybase;
|
||||
uint8_t *remset;
|
||||
uint8_t *grey;
|
||||
uint32_t ysize;
|
||||
int32_t mark_bytes;
|
||||
uint32_t rem_words;
|
||||
uint32_t soft;
|
||||
uint32_t lo;
|
||||
uint32_t half;
|
||||
int32_t grey_words;
|
||||
|
||||
pool = as3vm_old_pool_init(vm, (uint8_t *)heap);
|
||||
ysize = *(uint32_t *)(cfg + 8);
|
||||
VMU32(vm, G_YOUNG_SIZE) = ysize;
|
||||
|
||||
if (pool == NULL) {
|
||||
ybase = (uint8_t *)iggy_arena_malloc(PLAYER(vm), NULL, (int)ysize, 0x48);
|
||||
remset = NULL;
|
||||
if (ybase == NULL) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
ybase = pool;
|
||||
remset = pool + ysize;
|
||||
}
|
||||
VMP(vm, G_YOUNG_BASE) = ybase;
|
||||
VMP(vm, G_YOUNG_LIMIT) = ybase + ysize;
|
||||
VMP(vm, G_YOUNG_BUMP) = ybase;
|
||||
VMI32(vm, G_OLD_BYTES) = 0;
|
||||
|
||||
mark_bytes = *(int32_t *)(cfg + 0x10);
|
||||
rem_words = (uint32_t)(mark_bytes >> 3);
|
||||
if (remset == NULL) {
|
||||
remset = (uint8_t *)iggy_arena_malloc(PLAYER(vm), NULL,
|
||||
(int)(rem_words * 8u), 0x47);
|
||||
grey = NULL;
|
||||
if (remset == NULL) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
grey = remset + rem_words * 8u;
|
||||
}
|
||||
VMP(vm, G_REM_BASE) = remset;
|
||||
VMP(vm, G_REM_APPEND) = remset;
|
||||
remset = remset + rem_words * 8u;
|
||||
VMP(vm, G_REM_END) = remset;
|
||||
|
||||
soft = (uint32_t)(mark_bytes >> 8) & 0x7ffffffu;
|
||||
lo = (soft < 0x100u) ? 0x100u : soft;
|
||||
half = rem_words >> 1;
|
||||
soft = (lo < half) ? lo : half;
|
||||
|
||||
VMI32(vm, G_REM_OVF) = 0;
|
||||
VMU(vm, G_BIGARR_CHAIN) = 1;
|
||||
VMU32(vm, G_RATIO_B) = 0x40000000u;
|
||||
VMU32(vm, G_RATIO_A) = 0x3d800000u;
|
||||
VMU32(vm, G_RATIO_SIGN) = 0xffffffffu;
|
||||
VMU32(vm, G_RATIO_C) = 0x41000000u;
|
||||
VMP(vm, G_REM_LIMIT) = remset - (uintptr_t)soft * WORD;
|
||||
VMU32(vm, G_STEP_BUDGET) = 500;
|
||||
|
||||
grey_words = *(int32_t *)(cfg + 0x14) >> 3;
|
||||
VMI32(vm, G_GREY_COUNT) = grey_words;
|
||||
if (grey == NULL) {
|
||||
grey = (uint8_t *)iggy_arena_malloc(PLAYER(vm), NULL, grey_words * 8, 0x49);
|
||||
if (grey == NULL) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
VMP(vm, G_GREY_BASE) = grey;
|
||||
VMU32(vm, G_GROW_FLAG) = 0;
|
||||
VMP(vm, G_GREY_TOP) = grey;
|
||||
VMP(vm, G_UPDATE_CHAIN) = NULL;
|
||||
VMP(vm, G_GREY_LIMIT) = grey + (uintptr_t)VMU32(vm, G_GREY_COUNT) * WORD;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void as3vm_gc_clear(void *vm) {
|
||||
uint8_t *block;
|
||||
|
||||
for (block = VMP(vm, VM_FINAL_B); block != NULL;
|
||||
block = *(uint8_t **)block) {
|
||||
uint8_t *slot = block + 2 * WORD;
|
||||
int i;
|
||||
for (i = 0; i < 0x100; ++i) {
|
||||
uintptr_t value = (uintptr_t)*(uint8_t **)slot;
|
||||
if ((value & 3u) == 1u) {
|
||||
as3vm_finalize(vm, (void *)(value - 1u));
|
||||
*(uint8_t **)slot = NULL;
|
||||
}
|
||||
slot += WORD;
|
||||
}
|
||||
}
|
||||
|
||||
while (VMP(vm, G_POOL_HEAD) != NULL) {
|
||||
uint8_t *pool = VMP(vm, G_POOL_HEAD);
|
||||
VMP(vm, G_POOL_HEAD) = *(uint8_t **)pool;
|
||||
iggy_free_alloc((intptr_t)PLAYER(vm), pool);
|
||||
}
|
||||
VMP(vm, G_POOL_HEAD) = NULL;
|
||||
VMU32(vm, G_POOL_COUNT) = 0;
|
||||
VMI32(vm, G_OLD_BLKS) = 0;
|
||||
VMU32(vm, G_SCAN_WORDS) = 0;
|
||||
VMU(vm, VM_MARK_WORK) = 0;
|
||||
VMU(vm, VM_CLEAR_A) = 0;
|
||||
VMU(vm, VM_CLEAR_C) = 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Generational write barrier (slow path). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void as3vm_write_barrier_slowpath(void *vm, void *container_, void *value_) {
|
||||
uint8_t *container = (uint8_t *)container_;
|
||||
uint8_t *value = (uint8_t *)value_;
|
||||
uint8_t cflags = NB(container, N_FLAGS);
|
||||
uint8_t inv = (uint8_t)~NB(value, N_FLAGS);
|
||||
|
||||
if ((inv & cflags & F_OLD) == 0) {
|
||||
if ((inv & cflags & F_MARKING) == F_MARKING) {
|
||||
if (VMP(vm, G_SWEEP_BLOCK) == NULL) {
|
||||
NB(value, N_FLAGS) |= F_GREY;
|
||||
gc_grey_push(vm, value);
|
||||
} else if ((uintptr_t)VMP(vm, G_SWEEP_BLOCK) <= (uintptr_t)value) {
|
||||
__builtin_trap();
|
||||
}
|
||||
}
|
||||
} else if ((cflags & F_REMEMBERED) == 0) {
|
||||
add_to_remembered(vm, container);
|
||||
NB(container, N_FLAGS) |= F_REMEMBERED;
|
||||
}
|
||||
}
|
||||
693
iggy-decompiled/src/reconstructed/as3vm_gc_mark.inc
Normal file
693
iggy-decompiled/src/reconstructed/as3vm_gc_mark.inc
Normal file
@@ -0,0 +1,693 @@
|
||||
/* Reconstructed AS3/AVM2 tri-color marking core for Iggy's GC. */
|
||||
/* Translated from raw/durango (authoritative), cross-checked against */
|
||||
/* raw/orbis (LP64, for the decompiler-dropped call arguments) and raw/vita */
|
||||
/* (ILP32, for the 32-bit root word-indices). This file is #included at the */
|
||||
/* tail of as3vm_gc.c; every macro/accessor/import/forward-decl it uses is */
|
||||
/* defined there. */
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Root/container offsets that are true VM-context WORD indices and */
|
||||
/* therefore differ between the LP64 and ILP32 lowerings of the same */
|
||||
/* source. 64-bit values are index*8 (durango); 32-bit values are */
|
||||
/* index*4 (vita). Struct-internal field offsets (movie/class/traits) */
|
||||
/* stay as durango literals; that is the compiled/reviewed target. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
#if IGGY_AS3_64BIT
|
||||
#define MK_CLASSROOT(i) VMP(vm, (uint32_t)((uint32_t)(i) * 8u))
|
||||
#define MK_NCLASSES_OFF 0x2598 /* vm[0x4b3] class count */
|
||||
#define MK_CLASSARR_OFF 0x0e38 /* &vm[0x1c7] class array (stride 3w) */
|
||||
#define MK_REG_A_OFF 0x0d28 /* vm[0x1a5] class-registry list head */
|
||||
#define MK_REG_NEXT 0xc0 /* registry node -> next */
|
||||
#define MK_REG_COUNT 0x68 /* registry node -> member count (i32) */
|
||||
#define MK_REG_MEMBERS 0x98 /* registry node -> member array ptr */
|
||||
#define MK_REG_STRIDE 0x90 /* member stride */
|
||||
#define MK_REG_FLAG 0x1a /* member -> flags byte (bit 0x10) */
|
||||
#define MK_NS_ROOT 0x88 /* &vm[0x11] scope-chain root */
|
||||
#define MK_NS_FLAG 0x4f /* scope -> live flag (i32) */
|
||||
#define MK_NS_NEXT 0x57 /* scope -> next */
|
||||
#define MK_PSCAN_BASE 0x268 /* player class-table scan start */
|
||||
#define MK_PSCAN_END 0x368 /* player class-table scan end */
|
||||
#define MK_VMSLOT(i) ((uint8_t *)vm + (size_t)((uint32_t)(i) * 8u))
|
||||
#define MK_PROOT_OFF 0x2640 /* player-relative extra value root */
|
||||
#define MK_ROOT521_OFF 0x2908 /* &vm[0x521] */
|
||||
#define MK_ROOTS_BASE 0x08 /* vm[1] root array base */
|
||||
#define MK_ROOTS_CNT 0x10 /* vm[2] root array count (i32) */
|
||||
#define MK_WM_ROOT 0xf0 /* &vm[0x1e] weak-table array (6 slots)*/
|
||||
#define MK_WM_NEXT 0x28 /* weak-table -> next bucket */
|
||||
#define MK_WM_KEY 0x10 /* weak-table -> key holder */
|
||||
#define MK_WM_ENTS 0x20 /* weak-table -> entry list */
|
||||
#define MK_WM_WEAK 0x1e /* entry -> weak flag byte */
|
||||
#else
|
||||
#define MK_CLASSROOT(i) VMP(vm, (uint32_t)(((uint32_t)(i) + 0x99u) * 4u))
|
||||
#define MK_NCLASSES_OFF 0x1508 /* vm[0x542] */
|
||||
#define MK_CLASSARR_OFF 0x0958 /* &vm[0x256] */
|
||||
#define MK_REG_A_OFF 0x08b4 /* vm[0x22d] */
|
||||
#define MK_REG_NEXT 0x74
|
||||
#define MK_REG_COUNT 0x44
|
||||
#define MK_REG_MEMBERS 0x60
|
||||
#define MK_REG_STRIDE 0x4c
|
||||
#define MK_REG_FLAG 0x0e
|
||||
#define MK_NS_ROOT 0x4c /* &vm[0x13] */
|
||||
#define MK_NS_FLAG 0x37
|
||||
#define MK_NS_NEXT 0x3b
|
||||
#define MK_PSCAN_BASE 0x218
|
||||
#define MK_PSCAN_END 0x298
|
||||
#define MK_VMSLOT(i) ((uint8_t *)vm + (size_t)(((uint32_t)(i) + 2u) * 4u))
|
||||
#define MK_PROOT_OFF 0x200c
|
||||
#define MK_ROOT521_OFF 0x16e8 /* &vm[0x5ba] */
|
||||
#define MK_ROOTS_BASE 0x04 /* vm[1] */
|
||||
#define MK_ROOTS_CNT 0x08 /* vm[2] */
|
||||
#define MK_WM_ROOT 0x98 /* &vm[0x26] */
|
||||
#define MK_WM_NEXT 0x18
|
||||
#define MK_WM_KEY 0x0c
|
||||
#define MK_WM_ENTS 0x14
|
||||
#define MK_WM_WEAK 0x12
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Shade helpers. Both assume the caller already proved the value is */
|
||||
/* a heap pointer whose flag byte lacks the F_MARKING (0x18) bits. */
|
||||
/* MK_SHADE1: the young/leaf test used by the value scanners */
|
||||
/* (leaf when kind<=0x10 and kind not in {2,8}); the 0x1fefb mask is */
|
||||
/* durango's exact encoding of that set. */
|
||||
/* MK_SHADE2: the slot-scan test (leaf when kind<=0x12 and kind!=2). */
|
||||
/* Non-leaf values are greyed and pushed via the shared gc_grey_push. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
#define MK_SHADE1(v) \
|
||||
do { \
|
||||
uint8_t _k = (v)[-1]; \
|
||||
if ((_k == 1) || (((*(int16_t *)((v) + 5) == 0) && (_k < 0x11)) && \
|
||||
(((0x1fefbU >> (_k & 0x1f)) & 1u) != 0u))) { \
|
||||
(v)[0] = (uint8_t)((v)[0] | 0x18); \
|
||||
} else { \
|
||||
(v)[0] = (uint8_t)((v)[0] | 0x08); \
|
||||
gc_grey_push(vm, (v) - 1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define MK_SHADE2(v) \
|
||||
do { \
|
||||
uint8_t _f = (v)[0]; \
|
||||
uint8_t _k = (v)[-1]; \
|
||||
if ((_k == 1) || \
|
||||
(((*(int16_t *)((v) + 5) == 0) && (_k < 0x13)) && (_k != 2))) { \
|
||||
(v)[0] = (uint8_t)(_f | 0x18); \
|
||||
} else { \
|
||||
(v)[0] = (uint8_t)(_f | 0x08); \
|
||||
gc_grey_push(vm, (v) - 1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* maybe_greyv -- shade a tagged VALUE (the heap pointer, not a slot). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static void maybe_greyv(void *vm, uint8_t *value) {
|
||||
if (IS_HEAP(value) && (NB(value, 0) & 0x18) == 0) {
|
||||
MK_SHADE1(value);
|
||||
}
|
||||
}
|
||||
|
||||
/* maybe_grey -- shade the VALUE currently stored at *slot. */
|
||||
static void maybe_grey(void *vm, void *slot) {
|
||||
uint8_t *value = *(uint8_t **)slot;
|
||||
if (IS_HEAP(value) && (NB(value, 0) & 0x18) == 0) {
|
||||
MK_SHADE1(value);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* mark_stack -- scan the interpreter operand stack and the auxiliary */
|
||||
/* second stack, shading every heap value found. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static void mark_stack(void *vm) {
|
||||
uint8_t **p = (uint8_t **)VMP(vm, VM_OPSTACK_TOP);
|
||||
uint8_t **limit = (uint8_t **)VMP(vm, VM_OPSTACK_LIMIT);
|
||||
for (; (uintptr_t)p < (uintptr_t)limit; p++) {
|
||||
uint8_t *v = *p;
|
||||
if (IS_HEAP(v) && (NB(v, 0) & 0x18) == 0) {
|
||||
MK_SHADE1(v);
|
||||
}
|
||||
}
|
||||
p = (uint8_t **)VMP(vm, G_STACK2_TOP);
|
||||
limit = (uint8_t **)VMP(vm, G_STACK2_LIMIT);
|
||||
for (; (uintptr_t)p < (uintptr_t)limit; p++) {
|
||||
uint8_t *v = *p;
|
||||
if (IS_HEAP(v) && (NB(v, 0) & 0x18) == 0) {
|
||||
MK_SHADE1(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* mark_grey_movies -- greyscan a movie subtree (self + child chain). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static void mark_grey_movies(void *vm, uint8_t *movie) {
|
||||
uint8_t *child;
|
||||
if (NB(movie, 1) & 0x80) {
|
||||
visit_movies(vm, maybe_grey, movie);
|
||||
}
|
||||
for (child = NP(movie, 0x10); child != NULL; child = NP(child, 8)) {
|
||||
mark_grey_movies(vm, child);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* mark_increment -- pop nodes off the grey stack and darken them, */
|
||||
/* spending up to `budget` node-words of work. Returns the leftover */
|
||||
/* budget. This is the incremental heart of the collector. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static int mark_increment(void *vm, uint32_t budget) {
|
||||
int remaining = (int)budget;
|
||||
uint32_t nslots;
|
||||
uint32_t count;
|
||||
uint8_t *slotp;
|
||||
|
||||
for (;;) {
|
||||
if (remaining < 1) {
|
||||
return remaining;
|
||||
}
|
||||
if (VMU(vm, G_GREY_BASE) < VMU(vm, G_GREY_TOP)) {
|
||||
uint8_t *top = VMP(vm, G_GREY_TOP) - WORD;
|
||||
uint8_t *node;
|
||||
VMP(vm, G_GREY_TOP) = top;
|
||||
node = *(uint8_t **)top;
|
||||
nslots = NU16(node, 6);
|
||||
count = nslots;
|
||||
if (NB(node, 0) > 0x2a) {
|
||||
iggy_debug_entity = node;
|
||||
__builtin_trap();
|
||||
}
|
||||
NB(node, 1) = (uint8_t)(NB(node, 1) | 0x18);
|
||||
|
||||
if (NB(node, 0) == 2) {
|
||||
/* dense array node: element[0] is the shared value, the tail is a
|
||||
packed pointer array scanned back-to-front (half at a time). */
|
||||
maybe_greyv(vm, NP(node, 0x18));
|
||||
remaining -= NI32(node, 4);
|
||||
count = NU32(node, 8);
|
||||
if ((int32_t)NU32(node, 8) < 0) {
|
||||
count = NU32(node, 4);
|
||||
}
|
||||
slotp = node + (size_t)(int32_t)count * 8 + 0x18;
|
||||
if (count != 0) {
|
||||
if (NP(node, 0x10) == NULL) {
|
||||
goto generic_scan;
|
||||
}
|
||||
NP(node, 0x10) = VMP(vm, G_BIGARR_CHAIN);
|
||||
VMP(vm, G_BIGARR_CHAIN) = node;
|
||||
if ((int32_t)count > 1) {
|
||||
uint32_t half = count >> 1;
|
||||
do {
|
||||
uint8_t *v = *(uint8_t **)slotp;
|
||||
if (IS_HEAP(v) && (NB(v, 0) & 0x18) == 0) {
|
||||
MK_SHADE1(v);
|
||||
}
|
||||
slotp -= 0x10;
|
||||
half--;
|
||||
} while (half != 0);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
remaining -= (int)NU16(node, 4);
|
||||
if (NB(node, 0) > 0x12) {
|
||||
maybe_greyv(vm, NP(node, 0x20));
|
||||
if (NB(node, 0) == 0x24) {
|
||||
/* display object: greyscan the attached movie and both trait
|
||||
hash tables (each entry may need lazy cleaning). */
|
||||
if (NP(node, 0x28) != NULL && (NB(NP(node, 0x28), 0) & 8) == 0) {
|
||||
visit_movies(vm, maybe_grey, NP(node, 0x28));
|
||||
}
|
||||
for (uint8_t *set = NP(node, 0x30); set != NULL;
|
||||
set = NP(set, 0x28)) {
|
||||
for (uint8_t **ent = (uint8_t **)NP(set, 0x20); ent != NULL;
|
||||
ent = (uint8_t **)ent[2]) {
|
||||
uint8_t *slot = ent[0];
|
||||
uint8_t *v = (slot != NULL) ? *(uint8_t **)slot : NULL;
|
||||
if (slot == NULL || v == (uint8_t *)0x7) {
|
||||
if (NP(set, 0x40) == NULL) {
|
||||
as3vm_needs_cleaning(vm, set);
|
||||
}
|
||||
} else if (((uint8_t *)ent)[0x1e] == 0 && IS_HEAP(v) &&
|
||||
(NB(v, 0) & 0x18) == 0) {
|
||||
MK_SHADE1(v);
|
||||
}
|
||||
}
|
||||
for (uint8_t **ent = (uint8_t **)NP(set, 0x18); ent != NULL;
|
||||
ent = (uint8_t **)ent[2]) {
|
||||
uint8_t *slot = ent[0];
|
||||
uint8_t *v = (slot != NULL) ? *(uint8_t **)slot : NULL;
|
||||
if (slot == NULL || v == (uint8_t *)0x7) {
|
||||
if (NP(set, 0x40) == NULL) {
|
||||
as3vm_needs_cleaning(vm, set);
|
||||
}
|
||||
} else if (((uint8_t *)ent)[0x1e] == 0 && IS_HEAP(v) &&
|
||||
(NB(v, 0) & 0x18) == 0) {
|
||||
MK_SHADE1(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (nslots != 0) {
|
||||
slotp = node + (size_t)NU16(node, 4) * 8 - 8;
|
||||
generic_scan:
|
||||
for (;;) {
|
||||
uint8_t *v;
|
||||
uint8_t f;
|
||||
count = (uint32_t)(count - 1);
|
||||
if ((int32_t)count < 0) {
|
||||
break;
|
||||
}
|
||||
if (*(uintptr_t *)slotp == (uintptr_t)1) {
|
||||
iggy_debug_entity = node;
|
||||
__builtin_trap();
|
||||
}
|
||||
v = *(uint8_t **)slotp;
|
||||
if (IS_HEAP(v)) {
|
||||
f = NB(v, 0);
|
||||
iggy_debug_entity = node;
|
||||
if ((f & 0x18) == 0) {
|
||||
MK_SHADE2(v);
|
||||
}
|
||||
}
|
||||
slotp -= 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (VMI32(vm, G_GROW_FLAG) == 0) {
|
||||
return remaining;
|
||||
}
|
||||
VMI32(vm, G_GROW_FLAG) = 0;
|
||||
old_sweep_add_grey_items(vm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* mark_roots -- seed the grey set from the VM roots and the two */
|
||||
/* top-level movie trees plus the sprite list. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static void mark_roots(void *vm) {
|
||||
uint8_t *player = PLAYER(vm);
|
||||
uint8_t *movie_a = player + P_MOVIE_A;
|
||||
uint8_t *movie_b = player + P_MOVIE_B;
|
||||
uint8_t *child;
|
||||
uint8_t *sprite;
|
||||
|
||||
visit_roots(vm, maybe_grey, 0);
|
||||
NB(movie_a, 1) = (uint8_t)(NB(movie_a, 1) | 0x80);
|
||||
NB(movie_a, 0) = (uint8_t)(NB(movie_a, 0) & 0xef);
|
||||
NB(movie_b, 1) = (uint8_t)(NB(movie_b, 1) | 0x80);
|
||||
NB(movie_b, 0) = (uint8_t)(NB(movie_b, 0) & 0xef);
|
||||
|
||||
if (NB(movie_a, 1) & 0x80) {
|
||||
visit_movies(vm, maybe_grey, movie_a);
|
||||
}
|
||||
for (child = NP(movie_a, 0x10); child != NULL; child = NP(child, 8)) {
|
||||
mark_grey_movies(vm, child);
|
||||
}
|
||||
if (NB(movie_b, 1) & 0x80) {
|
||||
visit_movies(vm, maybe_grey, movie_b);
|
||||
}
|
||||
for (child = NP(movie_b, 0x10); child != NULL; child = NP(child, 8)) {
|
||||
mark_grey_movies(vm, child);
|
||||
}
|
||||
for (sprite = *(uint8_t **)(player + P_SPRITES); sprite != NULL;
|
||||
sprite = NP(sprite, 8)) {
|
||||
if ((NB(sprite, 0) & 8) == 0) {
|
||||
if (NB(sprite, 1) & 0x80) {
|
||||
visit_movies(vm, maybe_grey, sprite);
|
||||
}
|
||||
for (child = NP(sprite, 0x10); child != NULL; child = NP(child, 8)) {
|
||||
mark_grey_movies(vm, child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* movie_reachable -- true when a movie (or any descendant) still has */
|
||||
/* a live-marked backing object; used to decide lazy destruction. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static int movie_reachable(void *vm, uint8_t *movie) {
|
||||
uint8_t *obj = NP(movie, 0x20);
|
||||
uint8_t *ov = NULL;
|
||||
int quiescent = ((NB(movie, 1) & 1) == 0) && (NU16(movie, 6) == 0) &&
|
||||
((NB(movie, 0) & 0x10) == 0);
|
||||
|
||||
if (quiescent &&
|
||||
(obj == NULL || (ov = *(uint8_t **)obj) == (uint8_t *)0x7 ||
|
||||
ov == (uint8_t *)GC_NULLV || (NB(ov, 0) & 0x18) == 0)) {
|
||||
if (NB(movie, 3) == 7) {
|
||||
uint8_t *arr = NP(movie, 0x48);
|
||||
for (int off = 0; off < 0x20; off += 8) {
|
||||
uint8_t *slot = *(uint8_t **)(arr + off);
|
||||
if (slot != NULL && movie_reachable(vm, slot) != 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
uint8_t *child = NP(movie, 0x10);
|
||||
while (child != NULL) {
|
||||
if (movie_reachable(vm, child) != 0) {
|
||||
return 1;
|
||||
}
|
||||
child = NP(child, 8);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* visit_class -- apply `fn` to a class and its super-chain. When */
|
||||
/* `full` is zero the trait tables and prototype slots are visited too. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static void visit_class(void *vm, uint8_t *klass, gc_visit_fn fn, int full) {
|
||||
do {
|
||||
fn(vm, klass + 0x30);
|
||||
fn(vm, klass + 0x28);
|
||||
if (full == 0) {
|
||||
fn(vm, klass + 0x28);
|
||||
fn(vm, klass + 0x68);
|
||||
visit_traits(vm, NP(klass, 0x48), fn, klass);
|
||||
visit_traits(vm, NP(klass, 0x50), fn, klass);
|
||||
}
|
||||
klass = NP(klass, 0x80);
|
||||
} while (klass != NULL);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* visit_traits -- apply `fn` to the slot vector of a trait table. */
|
||||
/* update_location takes the relocation fast path (young-space slots */
|
||||
/* only); every other visitor gets each slot address in turn. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static void visit_traits(void *vm, uint8_t *traits, gc_visit_fn fn,
|
||||
uint8_t *owner) {
|
||||
(void)owner;
|
||||
if (traits == NULL) {
|
||||
return;
|
||||
}
|
||||
if (fn == update_location) {
|
||||
uintptr_t ybase = (uintptr_t)VMP(vm, G_YOUNG_BASE);
|
||||
uintptr_t ysize = (uintptr_t)VMU32(vm, G_YOUNG_SIZE);
|
||||
uint8_t *arr = NULL;
|
||||
uint32_t local = 0;
|
||||
if (NP(traits, 0x30) != NULL) {
|
||||
local = NU16(traits, 0x12);
|
||||
arr = NP(traits, 0x30) +
|
||||
(size_t)(int)((uint32_t)NU16(traits, 0x10) -
|
||||
(uint32_t)NU16(traits, 0x12)) *
|
||||
8;
|
||||
}
|
||||
if (IS_HEAP(*(uint8_t **)traits) &&
|
||||
((uintptr_t) * (uint8_t **)traits - ybase) < ysize) {
|
||||
update_location(vm, traits);
|
||||
}
|
||||
if (IS_HEAP(NP(traits, WORD)) &&
|
||||
((uintptr_t)NP(traits, WORD) - ybase) < ysize) {
|
||||
update_location(vm, traits + WORD);
|
||||
}
|
||||
if (NP(traits, 0x30) != NULL) {
|
||||
arr -= 8;
|
||||
if (local != 0) {
|
||||
uint32_t k = local;
|
||||
do {
|
||||
arr += 8;
|
||||
if (IS_HEAP(*(uint8_t **)arr) &&
|
||||
((uintptr_t) * (uint8_t **)arr - ybase) < ysize) {
|
||||
update_location(vm, arr);
|
||||
}
|
||||
k--;
|
||||
} while (k != 0);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fn(vm, traits);
|
||||
fn(vm, traits + WORD);
|
||||
if (NP(traits, 0x30) != NULL) {
|
||||
int i = (int)((uint32_t)NU16(traits, 0x10) - (uint32_t)NU16(traits, 0x12));
|
||||
int cnt = (int)(uint32_t)NU16(traits, 0x10);
|
||||
for (; i < cnt; i++) {
|
||||
fn(vm, NP(traits, 0x30) + (size_t)i * 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* visit_movies -- walk a movie tree, applying `fn` to every embedded */
|
||||
/* value slot. maybe_grey shades in place; update_location relocates */
|
||||
/* young-space referents. The tail sibling walk is iterative. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static void visit_movies(void *vm, gc_visit_fn fn, uint8_t *movie) {
|
||||
do {
|
||||
uint8_t stamped;
|
||||
uint8_t *obj;
|
||||
|
||||
if (fn == maybe_grey) {
|
||||
if (NB(movie, 0) & 0x10) {
|
||||
return;
|
||||
}
|
||||
stamped = (uint8_t)(NB(movie, 0) | 0x10);
|
||||
NB(movie, 1) = (uint8_t)(NB(movie, 1) & 0x7f);
|
||||
} else {
|
||||
if (fn != update_location) {
|
||||
return;
|
||||
}
|
||||
if (NB(movie, 0) & 8) {
|
||||
return;
|
||||
}
|
||||
stamped = (uint8_t)(NB(movie, 0) | 8);
|
||||
}
|
||||
|
||||
obj = NP(movie, 0x20);
|
||||
NB(movie, 0) = stamped;
|
||||
if (obj != NULL) {
|
||||
uint8_t *ov = *(uint8_t **)obj;
|
||||
if (ov != (uint8_t *)0x7 && ov != (uint8_t *)GC_NULLV) {
|
||||
if (fn == update_location) {
|
||||
uintptr_t ybase = (uintptr_t)VMP(vm, G_YOUNG_BASE);
|
||||
uintptr_t ysize = (uintptr_t)VMU32(vm, G_YOUNG_SIZE);
|
||||
if (IS_HEAP(ov) && ((uintptr_t)ov - ybase) < ysize) {
|
||||
update_location(vm, obj);
|
||||
}
|
||||
for (uint8_t *lnk = NP(movie, 0x50); lnk != NULL;
|
||||
lnk = NP(lnk, 0x10)) {
|
||||
uint8_t *lv = *(uint8_t **)NP(lnk, 8);
|
||||
if (IS_HEAP(lv) && ((uintptr_t)lv - ybase) < ysize) {
|
||||
update_location(vm, NP(lnk, 8));
|
||||
}
|
||||
}
|
||||
} else if (fn == maybe_grey) {
|
||||
if (IS_HEAP(ov) && (NB(ov, 0) & 0x18) == 0) {
|
||||
maybe_greyv(vm, ov);
|
||||
}
|
||||
for (uint8_t *lnk = NP(movie, 0x50); lnk != NULL;
|
||||
lnk = NP(lnk, 0x10)) {
|
||||
uint8_t *lv = *(uint8_t **)NP(lnk, 8);
|
||||
if (IS_HEAP(lv) && (NB(lv, 0) & 0x18) == 0) {
|
||||
maybe_greyv(vm, lv);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (uint8_t *set = *(uint8_t **)(ov + 0x2f); set != NULL;
|
||||
set = NP(set, 0x28)) {
|
||||
for (uint8_t **ent = (uint8_t **)NP(set, 0x20); ent != NULL;
|
||||
ent = (uint8_t **)ent[2]) {
|
||||
if (ent[0] != NULL && *(uintptr_t *)ent[0] != (uintptr_t)7) {
|
||||
fn(vm, ent[0]);
|
||||
}
|
||||
}
|
||||
for (uint8_t **ent = (uint8_t **)NP(set, 0x18); ent != NULL;
|
||||
ent = (uint8_t **)ent[2]) {
|
||||
if (ent[0] != NULL && *(uintptr_t *)ent[0] != (uintptr_t)7) {
|
||||
fn(vm, ent[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (NP(movie, 0x58) != NULL) {
|
||||
fn(vm, movie + 0x58);
|
||||
}
|
||||
if (NP(movie, 0x180) != NULL) {
|
||||
fn(vm, movie + 0x180);
|
||||
}
|
||||
if (NP(movie, 0x80) != NULL && (NB(NP(movie, 0x80), 0) & 8) == 0) {
|
||||
visit_movies(vm, fn, NP(movie, 0x80));
|
||||
}
|
||||
if (NP(movie, 0x68) != NULL) {
|
||||
visit_movies(vm, fn, NP(movie, 0x68));
|
||||
}
|
||||
if (NP(movie, 0x70) != NULL) {
|
||||
visit_movies(vm, fn, NP(movie, 0x70));
|
||||
}
|
||||
if (NP(movie, 0x78) != NULL) {
|
||||
visit_movies(vm, fn, NP(movie, 0x78));
|
||||
}
|
||||
if (NP(movie, 0x18) != NULL) {
|
||||
visit_movies(vm, fn, NP(movie, 0x18));
|
||||
}
|
||||
if (NB(movie, 3) == 7) {
|
||||
uint8_t *arr = NP(movie, 0x48);
|
||||
for (int off = 0; off < 0x20; off += 8) {
|
||||
if (*(uint8_t **)(arr + off) != NULL) {
|
||||
visit_movies(vm, fn, *(uint8_t **)(arr + off));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
movie = NP(movie, 0x10);
|
||||
if (movie == NULL) {
|
||||
return;
|
||||
}
|
||||
} while (NP(movie, 8) == NULL);
|
||||
|
||||
do {
|
||||
visit_movies(vm, fn, movie);
|
||||
movie = NP(movie, 8);
|
||||
} while (movie != NULL);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* visit_roots -- enumerate every GC root of the VM: the fixed class */
|
||||
/* table, the dynamic class registry, scope chains, the VM value slots, */
|
||||
/* the weak-reference tables and the five player stage trees. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
static void visit_roots(void *vm, gc_visit_fn fn, int full) {
|
||||
uint8_t *player = PLAYER(vm);
|
||||
|
||||
visit_class(vm, MK_CLASSROOT(0x506), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x507), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x508), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x509), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x50a), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x50b), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x50c), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x50d), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x512), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x516), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x510), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x511), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x517), fn, full);
|
||||
visit_class(vm, MK_CLASSROOT(0x519), fn, full);
|
||||
|
||||
{
|
||||
int n = VMI32(vm, MK_NCLASSES_OFF);
|
||||
if (n > 0) {
|
||||
uint8_t **arr = (uint8_t **)((uint8_t *)vm + MK_CLASSARR_OFF);
|
||||
for (int i = 0; i < n; i++) {
|
||||
visit_class(vm, arr[(size_t)i * 3], fn, full);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (uint8_t *node = VMP(vm, MK_REG_A_OFF); node != NULL;
|
||||
node = NP(node, MK_REG_NEXT)) {
|
||||
int n = NI32(node, MK_REG_COUNT);
|
||||
uint8_t *members = NP(node, MK_REG_MEMBERS);
|
||||
for (int i = 0; i < n; i++) {
|
||||
uint8_t *m = members + (size_t)i * MK_REG_STRIDE;
|
||||
if (NB(m, MK_REG_FLAG) & 0x10) {
|
||||
visit_class(vm, m, fn, full);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t *ns = VMP(vm, MK_NS_ROOT);
|
||||
if (ns != NULL) {
|
||||
if (NI32(ns, MK_NS_FLAG) != 0) {
|
||||
fn(vm, (uint8_t *)vm + MK_NS_ROOT);
|
||||
}
|
||||
for (uint8_t *nx = NP(ns, MK_NS_NEXT); nx != NULL;
|
||||
nx = NP(nx, MK_NS_NEXT)) {
|
||||
if (NI32(nx, MK_NS_FLAG) != 0) {
|
||||
fn(vm, ns + MK_NS_NEXT);
|
||||
}
|
||||
ns = nx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (uint32_t off = MK_PSCAN_BASE; off < MK_PSCAN_END; off += WORD) {
|
||||
for (uint8_t *node = *(uint8_t **)(player + off); node != NULL;
|
||||
node = NP(node, MK_REG_NEXT)) {
|
||||
int n = NI32(node, MK_REG_COUNT);
|
||||
uint8_t *members = NP(node, MK_REG_MEMBERS);
|
||||
for (int i = 0; i < n; i++) {
|
||||
uint8_t *m = members + (size_t)i * MK_REG_STRIDE;
|
||||
if (NB(m, MK_REG_FLAG) & 0x10) {
|
||||
visit_class(vm, m, fn, full);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn(vm, MK_VMSLOT(0xb));
|
||||
fn(vm, MK_VMSLOT(0x8));
|
||||
fn(vm, MK_VMSLOT(0xc));
|
||||
fn(vm, MK_VMSLOT(0x9));
|
||||
fn(vm, MK_VMSLOT(0xe));
|
||||
fn(vm, MK_VMSLOT(0x10));
|
||||
fn(vm, MK_VMSLOT(0xa));
|
||||
fn(vm, player + MK_PROOT_OFF);
|
||||
if (VMP(vm, MK_ROOT521_OFF) != NULL) {
|
||||
fn(vm, (uint8_t *)vm + MK_ROOT521_OFF);
|
||||
}
|
||||
|
||||
{
|
||||
int cnt = VMI32(vm, MK_ROOTS_CNT);
|
||||
uint8_t *base = VMP(vm, MK_ROOTS_BASE);
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
if (*(uint8_t **)(base + (size_t)i * WORD) != NULL) {
|
||||
fn(vm, base + (size_t)i * WORD);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int t = 0; t < 6; t++) {
|
||||
for (uint8_t *e = *(uint8_t **)((uint8_t *)vm + MK_WM_ROOT + (size_t)t * WORD);
|
||||
e != NULL; e = NP(e, MK_WM_NEXT)) {
|
||||
uint8_t *key = NP(e, MK_WM_KEY);
|
||||
if (key != NULL && *(uintptr_t *)key != (uintptr_t)7) {
|
||||
uint8_t *ent = NP(e, MK_WM_ENTS);
|
||||
int any = 0;
|
||||
if (ent != NULL) {
|
||||
do {
|
||||
uint8_t *slot = *(uint8_t **)ent;
|
||||
if (slot != NULL && *(uintptr_t *)slot != (uintptr_t)7 &&
|
||||
NB(ent, MK_WM_WEAK) == 0) {
|
||||
fn(vm, slot);
|
||||
any = 1;
|
||||
}
|
||||
ent = *(uint8_t **)(ent + 2 * WORD);
|
||||
} while (ent != NULL);
|
||||
if (any) {
|
||||
fn(vm, NP(e, MK_WM_KEY));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (*(uint8_t **)(player + P_STAGE2) != NULL) {
|
||||
visit_movies(vm, fn, *(uint8_t **)(player + P_STAGE2));
|
||||
}
|
||||
if (*(uint8_t **)(player + P_STAGE3) != NULL) {
|
||||
visit_movies(vm, fn, *(uint8_t **)(player + P_STAGE3));
|
||||
}
|
||||
if (*(uint8_t **)(player + P_STAGE0) != NULL) {
|
||||
visit_movies(vm, fn, *(uint8_t **)(player + P_STAGE0));
|
||||
}
|
||||
if (*(uint8_t **)(player + P_STAGE1) != NULL) {
|
||||
visit_movies(vm, fn, *(uint8_t **)(player + P_STAGE1));
|
||||
}
|
||||
if (*(uint8_t **)(player + P_STAGE4) != NULL) {
|
||||
visit_movies(vm, fn, *(uint8_t **)(player + P_STAGE4));
|
||||
}
|
||||
}
|
||||
736
iggy-decompiled/src/reconstructed/as3vm_gc_sweep.inc
Normal file
736
iggy-decompiled/src/reconstructed/as3vm_gc_sweep.inc
Normal file
@@ -0,0 +1,736 @@
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Sweep + collection-driver half of Iggy's AS3 GC. */
|
||||
/* Translated from durango raw (authoritative), cross-checked against */
|
||||
/* vita for the 32-bit layout. #included at the tail of as3vm_gc.c; */
|
||||
/* every macro / accessor / import / forward-declared static it uses */
|
||||
/* is defined there. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
/* --- deep-only offsets with no dedicated context macro ------------- */
|
||||
|
||||
/* old-pool node: [next][len][data...]. */
|
||||
#define POOL_LEN WORD
|
||||
#define POOL_DATA (2 * WORD)
|
||||
|
||||
/* fixed header the node-size derivation adds for a kind-2 dense array. */
|
||||
#if IGGY_AS3_64BIT
|
||||
#define N_ARR_BASE 0x20
|
||||
#else
|
||||
#define N_ARR_BASE 0x14
|
||||
#endif
|
||||
|
||||
/* byte-size -> GC-word shift (log2 WORD). */
|
||||
#if IGGY_AS3_64BIT
|
||||
#define WORD_SHIFT 3
|
||||
#else
|
||||
#define WORD_SHIFT 2
|
||||
#endif
|
||||
|
||||
/* big-array remembered container + remembered-set page layout. */
|
||||
#if IGGY_AS3_64BIT
|
||||
#define ARR_NEXT 0x10
|
||||
#define ARR_SLOTS 0x20
|
||||
#define REM_ENTRIES 0x10
|
||||
#else
|
||||
#define ARR_NEXT 0x0c
|
||||
#define ARR_SLOTS 0x14
|
||||
#define REM_ENTRIES 0x0c
|
||||
#endif
|
||||
#define ARR_COUNT 0x08
|
||||
#define ARR_ALT 0x04
|
||||
|
||||
/* movie / sprite node layout. */
|
||||
#if IGGY_AS3_64BIT
|
||||
#define MV_CHILD 0x10
|
||||
#define MV_CHILDARR 0x48
|
||||
#define MV_PARENT 0x80
|
||||
#else
|
||||
#define MV_CHILD 0x0c
|
||||
#define MV_CHILDARR 0x28
|
||||
#define MV_PARENT 0x44
|
||||
#endif
|
||||
#define MV_KIND 0x03
|
||||
#define MV_NEXT 0x08
|
||||
|
||||
/* pre_sweep incremental-marking list + dead-root table (no context macro). */
|
||||
#if IGGY_AS3_64BIT
|
||||
#define PS_MARKLIST 0x88
|
||||
#define PS_MARKLINK 0x57
|
||||
#define PS_ROOTTAB 0x3960
|
||||
#define PS_ROOTSTRIDE 0x18
|
||||
#else
|
||||
#define PS_MARKLIST 0x4c
|
||||
#define PS_MARKLINK 0x3b
|
||||
#define PS_ROOTTAB 0x2720
|
||||
#define PS_ROOTSTRIDE 0x10
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Byte size of a live/allocated node from its kind byte. Durango */
|
||||
/* inlines this at every sweep site; recovered here once. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static uint32_t gc_node_bytes(uint8_t *node) {
|
||||
uint8_t kind = NB(node, N_KIND);
|
||||
uint32_t bytes;
|
||||
if (kind < 3) {
|
||||
if (kind == 1) {
|
||||
bytes = NU32(node, N_SIZE) * WORD;
|
||||
} else if (kind == 2) {
|
||||
bytes = NU32(node, N_SIZE) * WORD + N_ARR_BASE;
|
||||
} else {
|
||||
bytes = 0;
|
||||
}
|
||||
} else {
|
||||
bytes = (uint32_t)NU16(node, N_SIZE) * WORD;
|
||||
}
|
||||
#if !IGGY_AS3_64BIT
|
||||
bytes = (bytes + 7u) & 0xfffffff8u;
|
||||
#endif
|
||||
if (bytes < MIN_BLOCK) {
|
||||
bytes = MIN_BLOCK;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* One incremental pass over a single old pool (raw:532). */
|
||||
/* Returns the resume cursor; *consumed reports the words retired. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static uint8_t *as3vm_old_pool_sweep(void *vm, uint8_t *pool, uint8_t *cursor,
|
||||
int budget, int *consumed) {
|
||||
int done = 0;
|
||||
uint8_t *end = pool + POOL_DATA + *(uintptr_t *)(pool + POOL_LEN);
|
||||
|
||||
if ((intptr_t)budget * WORD < (intptr_t)(end - cursor)) {
|
||||
end = cursor + (size_t)budget * WORD;
|
||||
}
|
||||
if ((uintptr_t)end <= (uintptr_t)cursor) {
|
||||
*consumed = 0;
|
||||
return cursor;
|
||||
}
|
||||
|
||||
do {
|
||||
uint8_t flags = NB(cursor, N_FLAGS);
|
||||
if ((flags & F_FREE) == 0) {
|
||||
uint32_t bytes = gc_node_bytes(cursor);
|
||||
if (budget < done) {
|
||||
break;
|
||||
}
|
||||
done += (int)(bytes >> WORD_SHIFT);
|
||||
if (flags & F_MARKING) {
|
||||
/* survived: strip the mark bits, keep the prev-free bit, tenure. */
|
||||
NB(cursor, N_FLAGS) = (uint8_t)((flags & 1) | F_OLD);
|
||||
cursor += bytes;
|
||||
} else {
|
||||
/* dead: return it to the segregated free lists (inline block_free). */
|
||||
VMI32(vm, G_OLD_BLKS) -= 1;
|
||||
VMI32(vm, G_OLD_LIVE) -= (int32_t)bytes;
|
||||
VMI32(vm, G_OLD_BYTES) -= (int32_t)bytes;
|
||||
if (NB(cursor, N_FLAGS) & 1) {
|
||||
/* coalesce with the free predecessor. */
|
||||
cursor -= NU32(cursor, -4);
|
||||
old_alloc_unlink(vm, cursor);
|
||||
bytes += NU32(cursor, N_SIZE);
|
||||
}
|
||||
uint8_t *next = cursor + bytes;
|
||||
if (NB(next, N_FLAGS) & F_FREE) {
|
||||
/* coalesce with the free successor. */
|
||||
old_alloc_unlink(vm, next);
|
||||
bytes += NU32(next, N_SIZE);
|
||||
}
|
||||
old_alloc_create_freeblock(vm, cursor, bytes);
|
||||
cursor += NU32(cursor, N_SIZE);
|
||||
}
|
||||
} else {
|
||||
cursor += NU32(cursor, N_SIZE);
|
||||
}
|
||||
} while ((uintptr_t)cursor < (uintptr_t)end);
|
||||
|
||||
*consumed = done;
|
||||
return cursor;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Drive as3vm_old_pool_sweep across every pool (raw:745). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void as3vm_old_sweep_faux(void *vm) {
|
||||
uint8_t *pool = VMP(vm, G_POOL_HEAD);
|
||||
VMP(vm, G_SWEEP_POOL) = pool;
|
||||
if (pool == NULL) {
|
||||
VMP(vm, G_SWEEP_BLOCK) = NULL;
|
||||
return;
|
||||
}
|
||||
VMP(vm, G_SWEEP_BLOCK) = pool + POOL_DATA;
|
||||
|
||||
uint8_t *p;
|
||||
uint8_t *cursor;
|
||||
do {
|
||||
p = VMP(vm, G_SWEEP_POOL);
|
||||
cursor = VMP(vm, G_SWEEP_BLOCK);
|
||||
int budget = 1000;
|
||||
do {
|
||||
int consumed = 0;
|
||||
cursor = as3vm_old_pool_sweep(vm, p, cursor, budget, &consumed);
|
||||
budget -= consumed;
|
||||
if ((uintptr_t)cursor ==
|
||||
(uintptr_t)p + POOL_DATA + *(uintptr_t *)(p + POOL_LEN)) {
|
||||
p = NP(p, 0);
|
||||
cursor = (p == NULL) ? NULL : p + POOL_DATA;
|
||||
}
|
||||
} while (budget > 0 && p != NULL);
|
||||
VMP(vm, G_SWEEP_POOL) = p;
|
||||
VMP(vm, G_SWEEP_BLOCK) = cursor;
|
||||
} while (p != NULL);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Full-collection sweep wrapper (raw:787). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void as3vm_sweep(void *vm) {
|
||||
gc_tm_enter(0x95e, "(iggy/gc) iggy_gc_sweep");
|
||||
as3vm_old_sweep_faux(vm);
|
||||
gc_tm_leave(0x98a);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Time-sliced incremental old-space sweep (raw:2072). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int old_sweep_increment(void *vm, int budget) {
|
||||
uint8_t *pool = VMP(vm, G_SWEEP_POOL);
|
||||
uint8_t *cursor = VMP(vm, G_SWEEP_BLOCK);
|
||||
|
||||
if (budget > 0x3ffffff) {
|
||||
budget = 0x4000000;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
uint8_t *end = pool + POOL_DATA + *(uintptr_t *)(pool + POOL_LEN);
|
||||
int consumed = 0;
|
||||
if ((intptr_t)budget * WORD < (intptr_t)(end - cursor)) {
|
||||
end = cursor + (size_t)budget * WORD;
|
||||
}
|
||||
|
||||
while ((uintptr_t)cursor < (uintptr_t)end) {
|
||||
uint8_t flags = NB(cursor, N_FLAGS);
|
||||
if ((flags & F_FREE) == 0) {
|
||||
uint32_t bytes = gc_node_bytes(cursor);
|
||||
if (budget < consumed) {
|
||||
break;
|
||||
}
|
||||
consumed += (int)(bytes >> WORD_SHIFT);
|
||||
if (flags & F_MARKING) {
|
||||
NB(cursor, N_FLAGS) = (uint8_t)((flags & 1) | F_OLD);
|
||||
cursor += bytes;
|
||||
} else {
|
||||
VMI32(vm, G_OLD_BLKS) -= 1;
|
||||
VMI32(vm, G_OLD_LIVE) -= (int32_t)bytes;
|
||||
VMI32(vm, G_OLD_BYTES) -= (int32_t)bytes;
|
||||
if (NB(cursor, N_FLAGS) & 1) {
|
||||
cursor -= NU32(cursor, -4);
|
||||
old_alloc_unlink(vm, cursor);
|
||||
bytes += NU32(cursor, N_SIZE);
|
||||
}
|
||||
uint8_t *next = cursor + bytes;
|
||||
if (NB(next, N_FLAGS) & F_FREE) {
|
||||
old_alloc_unlink(vm, next);
|
||||
bytes += NU32(next, N_SIZE);
|
||||
}
|
||||
/* inline old_alloc_create_freeblock (poisons with 0xf2). */
|
||||
memset(cursor, 0xf2, (size_t)bytes);
|
||||
NB(cursor, N_FLAGS) |= F_FREE;
|
||||
NB(cursor + bytes, N_FLAGS) |= 1;
|
||||
NU32(cursor, bytes - 4) = bytes;
|
||||
NU32(cursor, N_SIZE) = bytes;
|
||||
uint32_t cls = gc_size_class(bytes);
|
||||
uint8_t *oldhead = VMP(vm, G_FREELIST + cls * WORD);
|
||||
VMP(vm, G_FREELIST + cls * WORD) = cursor;
|
||||
NP(cursor, N_NEXT) = oldhead;
|
||||
NP(cursor, N_PREV) = NULL;
|
||||
if (oldhead != NULL) {
|
||||
NP(oldhead, N_PREV) = cursor;
|
||||
}
|
||||
cursor += NU32(cursor, N_SIZE);
|
||||
}
|
||||
} else {
|
||||
cursor += NU32(cursor, N_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
budget -= consumed;
|
||||
if ((uintptr_t)cursor ==
|
||||
(uintptr_t)pool + POOL_DATA + *(uintptr_t *)(pool + POOL_LEN)) {
|
||||
pool = NP(pool, 0);
|
||||
cursor = (pool == NULL) ? NULL : pool + POOL_DATA;
|
||||
}
|
||||
if (budget < 1 || pool == NULL) {
|
||||
VMP(vm, G_SWEEP_POOL) = pool;
|
||||
VMP(vm, G_SWEEP_BLOCK) = cursor;
|
||||
return budget;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Re-grey every marked old object across all pools (raw:2002). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void old_sweep_add_grey_items(void *vm) {
|
||||
uint8_t *pool = VMP(vm, G_POOL_HEAD);
|
||||
while (pool != NULL) {
|
||||
uint8_t *node = pool + POOL_DATA;
|
||||
uint8_t *end = node + *(uintptr_t *)(pool + POOL_LEN);
|
||||
uint32_t step = MIN_BLOCK;
|
||||
for (; (uintptr_t)node < (uintptr_t)end; node += step) {
|
||||
if ((NB(node, N_FLAGS) & F_FREE) == 0) {
|
||||
if ((NB(node, N_FLAGS) & F_MARKING) == F_GREY) {
|
||||
gc_grey_push(vm, node);
|
||||
}
|
||||
step = gc_node_bytes(node);
|
||||
} else {
|
||||
step = NU32(node, N_SIZE);
|
||||
}
|
||||
}
|
||||
if (VMI32(vm, G_GROW_FLAG) != 0) {
|
||||
return;
|
||||
}
|
||||
pool = NP(pool, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Re-scan remembered old objects after grey-stack overflow (raw:1942).*/
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void old_process_remembered_set_overflow(void *vm) {
|
||||
uint8_t *pool = VMP(vm, G_POOL_HEAD);
|
||||
while (pool != NULL) {
|
||||
uint8_t *node = pool + POOL_DATA;
|
||||
uint8_t *end = node + *(uintptr_t *)(pool + POOL_LEN);
|
||||
uint32_t step = MIN_BLOCK;
|
||||
for (; (uintptr_t)node < (uintptr_t)end; node += step) {
|
||||
if ((NB(node, N_FLAGS) & F_FREE) == 0) {
|
||||
if (NB(node, N_FLAGS) & F_REMEMBERED) {
|
||||
update_old_object(vm, node);
|
||||
}
|
||||
step = gc_node_bytes(node);
|
||||
} else {
|
||||
step = NU32(node, N_SIZE);
|
||||
}
|
||||
}
|
||||
if (VMI32(vm, G_GROW_FLAG) != 0) {
|
||||
return;
|
||||
}
|
||||
pool = NP(pool, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Count a movie and its descendants (raw:1156). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int count_movies_rec(uint8_t *movie, int count) {
|
||||
int total = count + 1;
|
||||
for (uint8_t *child = NP(movie, MV_CHILD); child != NULL;
|
||||
child = NP(child, MV_NEXT)) {
|
||||
total = count_movies_rec(child, total);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Post-sweep movie flag scrub (raw:1065 recursive; raw:1029 driver). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void clear_movies_post_sweep(void *vm, uint8_t *movie) {
|
||||
NB(movie, N_FLAGS) &= 0x7f;
|
||||
NB(movie, 0) &= 0xef;
|
||||
if (NB(movie, MV_KIND) == 7) {
|
||||
uint8_t **slots = (uint8_t **)NP(movie, MV_CHILDARR);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (slots[i] != NULL) {
|
||||
clear_movies_post_sweep(vm, slots[i]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (uint8_t *child = NP(movie, MV_CHILD); child != NULL;
|
||||
child = NP(child, MV_NEXT)) {
|
||||
clear_movies_post_sweep(vm, child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void clear_all_movies_post_sweep(void *vm) {
|
||||
clear_movies_post_sweep(vm, PLAYER(vm) + P_MOVIE_A);
|
||||
clear_movies_post_sweep(vm, PLAYER(vm) + P_MOVIE_B);
|
||||
for (uint8_t *movie = *(uint8_t **)(PLAYER(vm) + P_SPRITES); movie != NULL;
|
||||
movie = NP(movie, MV_NEXT)) {
|
||||
if (NB(movie, 0) & 0x10) {
|
||||
NB(movie, N_FLAGS) &= 0x7f;
|
||||
NB(movie, 0) &= 0xef;
|
||||
if (NB(movie, MV_KIND) == 7) {
|
||||
uint8_t **slots = (uint8_t **)NP(movie, MV_CHILDARR);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (slots[i] != NULL) {
|
||||
clear_movies_post_sweep(vm, slots[i]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (uint8_t *child = NP(movie, MV_CHILD); child != NULL;
|
||||
child = NP(child, MV_NEXT)) {
|
||||
clear_movies_post_sweep(vm, child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Clear the per-cycle "processed" flag on a movie subtree (raw:1130). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void clear_movies_processed(void *vm, uint8_t *movie) {
|
||||
NB(movie, 0) &= 0xf7;
|
||||
if (NB(movie, MV_KIND) == 7) {
|
||||
uint8_t **slots = (uint8_t **)NP(movie, MV_CHILDARR);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (slots[i] != NULL) {
|
||||
clear_movies_processed(vm, slots[i]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (uint8_t *child = NP(movie, MV_CHILD); child != NULL;
|
||||
child = NP(child, MV_NEXT)) {
|
||||
clear_movies_processed(vm, child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Finalize / reap unreachable objects before the block sweep */
|
||||
/* (raw:2270). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void pre_sweep(void *vm) {
|
||||
/* 1. prune fully-swept nodes from the incremental-marking work list. */
|
||||
uint8_t **link = (uint8_t **)((uint8_t *)vm + PS_MARKLIST);
|
||||
uint8_t *node = *link;
|
||||
while (node != NULL) {
|
||||
uint8_t *next = *(uint8_t **)(node + PS_MARKLINK);
|
||||
if ((NB(node, 0) & F_MARKING) == 0) {
|
||||
*link = next;
|
||||
} else {
|
||||
link = (uint8_t **)(node + PS_MARKLINK);
|
||||
}
|
||||
node = next;
|
||||
}
|
||||
|
||||
/* 2. compact the big-array remembered containers, dropping dead slots. */
|
||||
uint8_t *chain = (uint8_t *)VMU(vm, G_BIGARR_CHAIN);
|
||||
while ((uintptr_t)chain != 1) {
|
||||
uint8_t *arr = chain;
|
||||
int32_t count = NI32(arr, ARR_COUNT);
|
||||
chain = NP(arr, ARR_NEXT);
|
||||
int neg = count < 0;
|
||||
*(uintptr_t *)(arr + ARR_NEXT) = 1;
|
||||
if (neg) {
|
||||
count = NI32(arr, ARR_ALT);
|
||||
}
|
||||
uint32_t newcount = 0;
|
||||
if (count > 0) {
|
||||
uint8_t *wval = arr + ARR_SLOTS;
|
||||
uint8_t *wtag = arr + ARR_SLOTS + WORD;
|
||||
uint8_t *rtag = wtag;
|
||||
long iters = (((long)count - 1) >> 1) + 1;
|
||||
uint32_t kept = 0;
|
||||
do {
|
||||
uint8_t *val = *(uint8_t **)(rtag - WORD);
|
||||
if (IS_HEAP(val)) {
|
||||
if ((NB(val, 0) & F_MARKING) == 0) {
|
||||
if (!neg) {
|
||||
goto slot_done;
|
||||
}
|
||||
*(uintptr_t *)(rtag - WORD) = 3;
|
||||
*(void **)rtag = GC_UNDEF;
|
||||
goto keep_count;
|
||||
}
|
||||
goto keep_write;
|
||||
} else if ((uintptr_t)val == 3) {
|
||||
if (neg) {
|
||||
goto keep_count;
|
||||
}
|
||||
goto slot_done;
|
||||
} else if ((uintptr_t)val == 7) {
|
||||
goto slot_done;
|
||||
}
|
||||
keep_write:
|
||||
if (!neg) {
|
||||
*(uint8_t **)wval = val;
|
||||
*(uint8_t **)wtag = *(uint8_t **)rtag;
|
||||
}
|
||||
keep_count:
|
||||
kept += 2;
|
||||
wval += 2 * WORD;
|
||||
wtag += 2 * WORD;
|
||||
slot_done:
|
||||
newcount = kept;
|
||||
rtag += 2 * WORD;
|
||||
iters--;
|
||||
} while (iters != 0);
|
||||
}
|
||||
if (NI32(arr, ARR_COUNT) >= 0) {
|
||||
NU32(arr, ARR_COUNT) = newcount;
|
||||
}
|
||||
}
|
||||
|
||||
/* 3. sweep the finalizable remembered set down to the undef sentinel. */
|
||||
VMU(vm, G_BIGARR_CHAIN) = 1;
|
||||
for (uint8_t *page = VMP(vm, VM_FINAL_A); page != NULL; page = NP(page, 0)) {
|
||||
for (int j = 0; j < 0x100; j++) {
|
||||
uint8_t **slot = (uint8_t **)(page + REM_ENTRIES + (size_t)j * WORD);
|
||||
uint8_t *val = *slot;
|
||||
if (IS_HEAP(val) && (NB(val, 0) & F_MARKING) == 0 &&
|
||||
(VMU(vm, G_SWEEP_POOL) == 0 ||
|
||||
(uintptr_t)VMP(vm, G_SWEEP_BLOCK) <= (uintptr_t)(val - 1))) {
|
||||
*(uintptr_t *)slot = 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 4. recompute movie reachability, folding it into flag bit 0x08. */
|
||||
int total = 0;
|
||||
for (uint8_t *movie = *(uint8_t **)(PLAYER(vm) + P_SPRITES); movie != NULL;
|
||||
movie = NP(movie, MV_NEXT)) {
|
||||
int reach = movie_reachable(vm, movie);
|
||||
total++;
|
||||
NB(movie, 0) = (uint8_t)(NB(movie, 0) ^ (((reach << 3) ^ NB(movie, 0)) & 8));
|
||||
}
|
||||
clear_all_movies_post_sweep(vm);
|
||||
|
||||
/* 5. propagate reachability up each movie's parent chain. */
|
||||
for (uint8_t *movie = *(uint8_t **)(PLAYER(vm) + P_SPRITES); movie != NULL;
|
||||
movie = NP(movie, MV_NEXT)) {
|
||||
if ((NB(movie, N_FLAGS) & 8) != 0 && (NB(movie, 0) & 8) != 0) {
|
||||
uint8_t *p = movie;
|
||||
while (p != NULL) {
|
||||
uint8_t *parent = NP(p, MV_PARENT);
|
||||
if (parent == NULL) {
|
||||
NB(p, 0) |= 8;
|
||||
break;
|
||||
}
|
||||
if ((NB(p, N_FLAGS) & 8) != 0) {
|
||||
if ((NB(p, 0) & 8) != 0) {
|
||||
break;
|
||||
}
|
||||
NB(p, 0) |= 8;
|
||||
}
|
||||
p = parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 6. destroy the unreachable sprites; tally the survivors. */
|
||||
int reachable = 0;
|
||||
uint8_t *movie = *(uint8_t **)(PLAYER(vm) + P_SPRITES);
|
||||
while (movie != NULL) {
|
||||
uint8_t flags0 = NB(movie, 0);
|
||||
uint8_t *nextm = NP(movie, MV_NEXT);
|
||||
if ((flags0 & 8) == 0) {
|
||||
iggy_destroy_sprite(PLAYER(vm), movie);
|
||||
} else {
|
||||
reachable++;
|
||||
}
|
||||
movie = nextm;
|
||||
}
|
||||
gc_tm_log(total, reachable);
|
||||
|
||||
/* 7. clear the processed flag over the surviving movie subtrees. */
|
||||
for (uint8_t *m = *(uint8_t **)(PLAYER(vm) + P_SPRITES); m != NULL;
|
||||
m = NP(m, MV_NEXT)) {
|
||||
if ((NB(m, 0) & 8) != 0) {
|
||||
clear_movies_processed(vm, m);
|
||||
}
|
||||
}
|
||||
|
||||
/* 8. finalize the dead entries in the finalizable remembered set. */
|
||||
for (uint8_t *page = VMP(vm, VM_FINAL_B); page != NULL; page = NP(page, 0)) {
|
||||
for (int j = 0; j < 0x100; j++) {
|
||||
uint8_t **slot = (uint8_t **)(page + REM_ENTRIES + (size_t)j * WORD);
|
||||
uint8_t *val = *slot;
|
||||
if (IS_HEAP(val) && (NB(val, 0) & F_MARKING) == 0) {
|
||||
as3vm_finalize(vm, val - 1);
|
||||
*slot = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 9. null out dead heap values still pinned by the root table. */
|
||||
uint8_t *ent = (uint8_t *)vm + PS_ROOTTAB;
|
||||
for (int j = 0; j < 0x200; j++) {
|
||||
uint8_t *val = *(uint8_t **)ent;
|
||||
if (IS_HEAP(val) && (NB(val, 0) & F_MARKING) == 0) {
|
||||
*(uint8_t **)ent = NULL;
|
||||
}
|
||||
ent += PS_ROOTSTRIDE;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Fire the user's GC-event callback with per-phase telemetry */
|
||||
/* (raw:1215). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void gc_notify(void *vm, int event, int amount, uint32_t a, uint32_t b) {
|
||||
if (VMP(vm, G_NOTIFY_CB) == NULL || amount == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint64_t snapshot = rrGetTime();
|
||||
uint64_t ev[10];
|
||||
memset(ev, 0, sizeof(ev));
|
||||
uint8_t *p = (uint8_t *)ev;
|
||||
|
||||
int64_t micros =
|
||||
(int64_t)rrTimeToMicros((int64_t)(snapshot - VMU64(vm, G_STEP_START)));
|
||||
*(int64_t *)(p + 0x00) = micros;
|
||||
*(uint64_t *)(p + 0x08) = VMU64(vm, G_STAT_A);
|
||||
*(uint64_t *)(p + 0x10) = VMU64(vm, G_STAT_B);
|
||||
VMU64(vm, G_STAT_MICROS) += (uint64_t)micros;
|
||||
*(uint64_t *)(p + 0x18) = VMU64(vm, G_ALLOC_BYTES);
|
||||
*(uint32_t *)(p + 0x48) = a;
|
||||
*(uint32_t *)(p + 0x4c) = b;
|
||||
*(uint64_t *)(p + 0x20) = VMU64(vm, G_STAT_MICROS);
|
||||
*(const char **)(p + 0x28) = event_names[event];
|
||||
*(int32_t *)(p + 0x30) = event;
|
||||
*(int32_t *)(p + 0x34) = amount * 8;
|
||||
*(int32_t *)(p + 0x38) = (VMI32(vm, G_SCAN_WORDS) - VMI32(vm, G_OLD_SNAP)) * 8;
|
||||
*(int32_t *)(p + 0x3c) = VMI32(vm, G_SCAN_WORDS) * 8;
|
||||
*(int32_t *)(p + 0x40) = VMI32(vm, G_OLD_LIVE);
|
||||
*(int32_t *)(p + 0x44) = VMI32(vm, G_POOL_BYTES);
|
||||
|
||||
void (*cb)(void *, void *) = (void (*)(void *, void *))VMP(vm, G_NOTIFY_CB);
|
||||
cb(PLAYER(vm), ev);
|
||||
VMU64(vm, G_STEP_START) = rrGetTime();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* One time-sliced incremental GC step (raw:1266). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void gc_step(void *vm) {
|
||||
int mark_budget =
|
||||
(int)((float)(int)VMU32(vm, G_SCAN_WORDS) * VMF(vm, G_RATIO_B));
|
||||
if (mark_budget > 0x0ffffffe) {
|
||||
mark_budget = 0x0fffffff;
|
||||
}
|
||||
int old_budget =
|
||||
(int)((float)(int)(VMU32(vm, G_OLD_BYTES) >> 3) * VMF(vm, G_RATIO_A));
|
||||
if (old_budget > 0x0ffffffe) {
|
||||
old_budget = 0x0fffffff;
|
||||
}
|
||||
uint32_t budget =
|
||||
(uint32_t)(old_budget + mark_budget + VMI32(vm, G_STEP_BUDGET));
|
||||
|
||||
VMU64(vm, G_STEP_START) = rrGetTime();
|
||||
VMU16(vm, G_IN_GC) = 1;
|
||||
|
||||
uint32_t remaining = budget;
|
||||
if (VMP(vm, G_SWEEP_POOL) == NULL) {
|
||||
as3vm_interpreter_notice_gc(1);
|
||||
remaining = (uint32_t)mark_increment(vm, budget);
|
||||
gc_notify(vm, 1, (int)(budget - remaining), 0, 0);
|
||||
if ((int)remaining > 0) {
|
||||
gc_tm_enter(0xe4f, "(iggy/gc) iggy_mark_incremental");
|
||||
mark_roots(vm);
|
||||
gc_notify(vm, 2, 1, 0, 0);
|
||||
uint32_t r2 = (uint32_t)mark_increment(vm, remaining);
|
||||
gc_notify(vm, 1, (int)(remaining - r2), 0, 0);
|
||||
remaining = r2;
|
||||
if ((int)r2 > 0) {
|
||||
mark_stack(vm);
|
||||
remaining = (uint32_t)mark_increment(vm, r2);
|
||||
gc_notify(vm, 1, (int)(r2 - remaining), 0, 0);
|
||||
if (VMU(vm, G_GREY_TOP) == VMU(vm, G_GREY_BASE) &&
|
||||
VMI32(vm, G_GROW_FLAG) == 0) {
|
||||
as3vm_interpreter_notice_gc(2);
|
||||
pre_sweep(vm);
|
||||
gc_notify(vm, 3, 1, 0, 0);
|
||||
uint8_t *head = VMP(vm, G_POOL_HEAD);
|
||||
VMP(vm, G_SWEEP_POOL) = head;
|
||||
VMP(vm, G_SWEEP_BLOCK) = (head == NULL) ? NULL : head + POOL_DATA;
|
||||
}
|
||||
}
|
||||
gc_tm_leave(0xe82);
|
||||
}
|
||||
VMI64(vm, G_STAT_A) += (int64_t)(int)(budget - remaining);
|
||||
}
|
||||
|
||||
if (VMP(vm, G_SWEEP_POOL) != NULL && (int)remaining > 0) {
|
||||
gc_tm_enter(0xe8c, "(iggy/gc) iggy_sweep_incremental");
|
||||
uint32_t sweep_budget =
|
||||
(uint32_t)(int)((float)(int)remaining * VMF(vm, G_RATIO_C));
|
||||
uint32_t acc = sweep_budget;
|
||||
uint32_t last = 0;
|
||||
int consumed = 0;
|
||||
do {
|
||||
last = acc;
|
||||
old_sweep_increment(vm, (int)sweep_budget);
|
||||
sweep_budget = 50000;
|
||||
consumed = (int)last;
|
||||
acc = (uint32_t)(consumed + 50000);
|
||||
} while (VMP(vm, G_SWEEP_POOL) != NULL);
|
||||
maybe_greyv(vm, VMP(vm, VM_MARK_WORK));
|
||||
maybe_greyv(vm, VMP(vm, VM_CLEAR_A));
|
||||
VMI64(vm, G_STAT_B) += (int64_t)consumed;
|
||||
gc_notify(vm, 4, (int)last, 0, 0);
|
||||
gc_tm_leave(0xeb2);
|
||||
}
|
||||
|
||||
VMU32(vm, G_SCAN_WORDS) = 0;
|
||||
VMU16(vm, G_IN_GC) = 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Stop-the-world collection driver (raw:1180). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void gc_collect_raw(void *vm) {
|
||||
gc_tm_enter(0xec5, "(iggy/gc) iggy_gc_full_collection");
|
||||
mark_roots(vm);
|
||||
mark_stack(vm);
|
||||
int r;
|
||||
do {
|
||||
r = mark_increment(vm, 0x100000);
|
||||
} while (r < 1);
|
||||
pre_sweep(vm);
|
||||
as3vm_sweep(vm);
|
||||
mark_roots(vm);
|
||||
gc_tm_leave(0xecd);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Exported collection entry points (prototypes in as3vm_gc.h). */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
void as3vm_gc_collect(void *vm) {
|
||||
num_gc_steps++;
|
||||
tenure(vm);
|
||||
gc_collect_raw(vm);
|
||||
}
|
||||
|
||||
void as3vm_gc_collect_exact(void *vm) {
|
||||
tenure(vm);
|
||||
gc_collect_raw(vm);
|
||||
gc_collect_raw(vm);
|
||||
}
|
||||
|
||||
void as3vm_gc_step(void *vm) {
|
||||
if (VMP(vm, VM_MARK_WORK) != NULL && VMI32(vm, G_OLD_FORCED) == 0) {
|
||||
tenure(vm);
|
||||
gc_step(vm);
|
||||
}
|
||||
}
|
||||
464
iggy-decompiled/src/reconstructed/as3vm_gc_tenure.inc
Normal file
464
iggy-decompiled/src/reconstructed/as3vm_gc_tenure.inc
Normal file
@@ -0,0 +1,464 @@
|
||||
/* as3vm_gc: the tenure / promotion (young -> old) collector.
|
||||
*
|
||||
* The nursery is a bump-allocated semispace; tenure evacuates every live young
|
||||
* object into the old generation. update_location(vm, slot) is the copy/scan
|
||||
* primitive: it follows a value slot, and when it points at a live young node
|
||||
* it copies the object into the old gen (as3vm_old_alloc + memcpy), overwrites
|
||||
* the young node with a forward marker (byte==2) + forward pointer, greys the
|
||||
* copy, and rewrites the slot with the new tagged value. Big arrays defer to
|
||||
* the G_UPDATE_CHAIN worklist and class objects to the G_CLASS_CHAIN worklist,
|
||||
* both drained by tenure. update_old_object scans an already-old object's
|
||||
* slots. tenure walks the remembered set, all VM/player roots, the operand
|
||||
* stacks, the movie trees and dirty list, drains both worklists, finalizes the
|
||||
* dead young finalizables, clears movie mark bits, notifies, and resets the
|
||||
* nursery. Translated from durango raw:2472-3001, cross-checked vs vita.
|
||||
*
|
||||
* A tagged heap value `v` equals `node + 1` (low 2 bits == 01): v[-1] is the
|
||||
* kind byte (node[0]), v[0] is the flags byte (node[1]), v[7] holds the forward
|
||||
* pointer after evacuation. The offset macros below carry the two lowerings
|
||||
* (durango LP64 authoritative, vita ILP32 for the 32-bit column).
|
||||
*/
|
||||
|
||||
#if IGGY_AS3_64BIT
|
||||
#define TEN_FWD 7 /* forward pointer slot (v+7 == node+8) */
|
||||
#define TEN_CLASSPTR 0x2f /* embedded class-object pointer (v-relative) */
|
||||
#define TEN_CLASSLINK 0x38 /* class-object G_CLASS_CHAIN link field */
|
||||
#define TEN_BIGLINK 0xf /* big-array chain link (v+0xf == node+0x10) */
|
||||
#define TEN_BIGNEXT 0x10 /* big-array chain next (node-relative) */
|
||||
#define TEN_ARR2_DATA 0x18 /* kind-2 array element data start (node-rel) */
|
||||
#define TEN_KIND2_HDR 4 /* kind-2 header word count */
|
||||
#define TEN_TRAITROOT 0x88 /* vm trait-root list head */
|
||||
#define TEN_TRAITLINK 0x57 /* trait-root chain link */
|
||||
#define TEN_ROOTTAB 0x3960 /* const-pool root table base (vm-rel) */
|
||||
#define TEN_ROOTTAB_STRIDE 0x18
|
||||
#define TEN_ROOTTAB_FLAG 0x6960 /* guard: is the root table active? (i32) */
|
||||
#define TEN_FINAL_COUNT 8 /* finalizable-block active-count field */
|
||||
#define TEN_FINAL_SLOTS 0x10 /* finalizable-block slot array start */
|
||||
#define TEN_YOUNG_SHIFT 3 /* nursery byte->word shift */
|
||||
#define TEN_CLS_NEXT 0x38 /* class worklist next (cleared on pop) */
|
||||
#define TEN_CLS_SLOT 0x10 /* class single indirect slot */
|
||||
#define TEN_CLS_LISTA 0x20 /* class trait-slot list A */
|
||||
#define TEN_CLS_LISTB 0x18 /* class trait-slot list B */
|
||||
#define TEN_CLS_ITER 0x28 /* class inheritance-chain link */
|
||||
#define TEN_CLSNODE_FLAG 0x1e /* trait-slot node skip flag */
|
||||
#define TEN_MOV_DIRTYNEXT 0x38 /* movie dirty-list next */
|
||||
#define TEN_MOV_PARENT 0x80 /* movie parent link */
|
||||
#else
|
||||
#define TEN_FWD 7
|
||||
#define TEN_CLASSPTR 0x1b
|
||||
#define TEN_CLASSLINK 0x20
|
||||
#define TEN_BIGLINK 0xb
|
||||
#define TEN_BIGNEXT 0xc
|
||||
#define TEN_ARR2_DATA 0x10
|
||||
#define TEN_KIND2_HDR 5
|
||||
#define TEN_TRAITROOT 0x4c
|
||||
#define TEN_TRAITLINK 0x3b
|
||||
#define TEN_ROOTTAB 0x2720
|
||||
#define TEN_ROOTTAB_STRIDE 0x10
|
||||
#define TEN_ROOTTAB_FLAG 0x4720
|
||||
#define TEN_FINAL_COUNT 4
|
||||
#define TEN_FINAL_SLOTS 0xc
|
||||
#define TEN_YOUNG_SHIFT 2
|
||||
#define TEN_CLS_NEXT 0x20
|
||||
#define TEN_CLS_SLOT 0xc
|
||||
#define TEN_CLS_LISTA 0x14
|
||||
#define TEN_CLS_LISTB 0x10
|
||||
#define TEN_CLS_ITER 0x18
|
||||
#define TEN_CLSNODE_FLAG 0x12
|
||||
#define TEN_MOV_DIRTYNEXT 0x20
|
||||
#define TEN_MOV_PARENT 0x44
|
||||
#endif
|
||||
|
||||
/* is `v` a tagged heap value that falls inside the [base, base+size] nursery? */
|
||||
#define TEN_IN_YOUNG(v, base, size) \
|
||||
((((uintptr_t)(v)) & 3u) == 1u && \
|
||||
((uintptr_t)(v) - 1u - (uintptr_t)(base)) <= (uintptr_t)(size))
|
||||
|
||||
/* Evacuate the value at *slot into the old generation (if young), rewriting
|
||||
* the slot in place, then tail-follow the single interesting child slot.
|
||||
* Multi-slot objects and 0x24 classes are pushed to the worklists instead. */
|
||||
static void update_location(void *vm, void *slot) {
|
||||
uint8_t *val = *(uint8_t **)slot;
|
||||
|
||||
for (;;) {
|
||||
if (((uintptr_t)val & 3u) != 1u) {
|
||||
return;
|
||||
}
|
||||
if ((uintptr_t)val < (uintptr_t)VMP(vm, G_YOUNG_BASE)) {
|
||||
return;
|
||||
}
|
||||
if ((uintptr_t)VMP(vm, G_YOUNG_LIMIT) < (uintptr_t)val) {
|
||||
return;
|
||||
}
|
||||
if (val[0] == 2) {
|
||||
/* already evacuated -- copy the forward pointer into the slot. */
|
||||
*(uint8_t **)slot = *(uint8_t **)(val + TEN_FWD);
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t kind = val[-1];
|
||||
uint32_t follow;
|
||||
uint32_t words;
|
||||
if (kind < 3) {
|
||||
if (kind == 1) {
|
||||
words = NU32(val, 3);
|
||||
follow = 0;
|
||||
} else if (kind == 2) {
|
||||
int32_t n = NI32(val, TEN_FWD);
|
||||
if (n < 0) {
|
||||
n = NI32(val, 3);
|
||||
}
|
||||
follow = (uint32_t)(n + 1);
|
||||
words = (uint32_t)(NI32(val, 3) + TEN_KIND2_HDR);
|
||||
} else {
|
||||
follow = 0;
|
||||
words = 0;
|
||||
}
|
||||
} else {
|
||||
follow = NU16(val, 5);
|
||||
if (kind > 0x12) {
|
||||
follow = (uint32_t)(NU16(val, 5) + 2);
|
||||
if (kind == 0x24) {
|
||||
uint8_t *klass = NP(val, TEN_CLASSPTR);
|
||||
if (klass != NULL) {
|
||||
*(uint8_t **)(klass + TEN_CLASSLINK) = VMP(vm, G_CLASS_CHAIN);
|
||||
VMP(vm, G_CLASS_CHAIN) = klass;
|
||||
}
|
||||
}
|
||||
}
|
||||
words = NU16(val, 3);
|
||||
}
|
||||
|
||||
uint8_t *dst = as3vm_old_alloc(vm, words);
|
||||
uint8_t saved = dst[1];
|
||||
uint8_t *newval = dst + 1;
|
||||
memcpy(dst, val - 1, (size_t)words * (size_t)WORD);
|
||||
dst[1] = saved;
|
||||
val[0] = 2;
|
||||
*(uint8_t **)(val + TEN_FWD) = newval;
|
||||
|
||||
if (VMP(vm, G_SWEEP_BLOCK) == NULL && ((uintptr_t)newval & 3u) == 1u &&
|
||||
(dst[1] & F_MARKING) == 0) {
|
||||
uint8_t k = dst[0];
|
||||
if (k == 1 || (NU16(dst, 6) == 0 && k < 0x11 &&
|
||||
((0x1fefbu >> (k & 0x1fu)) & 1u) != 0)) {
|
||||
dst[1] |= F_MARKING;
|
||||
} else {
|
||||
dst[1] |= F_GREY;
|
||||
gc_grey_push(vm, dst);
|
||||
}
|
||||
}
|
||||
|
||||
*(uint8_t **)slot = newval;
|
||||
|
||||
if (follow > 1) {
|
||||
/* big array: defer element scan to update_old_object via the worklist. */
|
||||
*(uint8_t **)(val + TEN_BIGLINK) = VMP(vm, G_UPDATE_CHAIN);
|
||||
VMP(vm, G_UPDATE_CHAIN) = val - 1;
|
||||
return;
|
||||
}
|
||||
if (follow == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (dst[0] == 2) {
|
||||
slot = dst + TEN_ARR2_DATA;
|
||||
} else {
|
||||
slot = dst + ((uintptr_t)NU16(dst, 4) - 1u) * (uintptr_t)WORD;
|
||||
}
|
||||
val = *(uint8_t **)slot;
|
||||
}
|
||||
}
|
||||
|
||||
/* Scan every slot of an already-promoted (old) object, relocating any young
|
||||
* pointees. Handles kind-2 arrays, generic objects and 0x24 classes. */
|
||||
static void update_old_object(void *vm, uint8_t *node) {
|
||||
node[1] |= F_OLD;
|
||||
updated_object = (void *)(node + 1);
|
||||
|
||||
uint8_t *lo;
|
||||
uint8_t *hi;
|
||||
if (node[0] == 2) {
|
||||
int32_t n = NI32(node, 8);
|
||||
if (n < 0) {
|
||||
hi = node + ((intptr_t)NI32(node, 4) + TEN_KIND2_HDR) * WORD;
|
||||
} else {
|
||||
hi = node + (intptr_t)n * WORD + (intptr_t)TEN_KIND2_HDR * WORD;
|
||||
}
|
||||
lo = node + TEN_ARR2_DATA;
|
||||
} else {
|
||||
hi = node + (uintptr_t)NU16(node, 4) * (uintptr_t)WORD;
|
||||
lo = hi - (uintptr_t)NU16(node, 6) * (uintptr_t)WORD;
|
||||
}
|
||||
|
||||
for (hi -= WORD; (uintptr_t)lo <= (uintptr_t)hi; hi -= WORD) {
|
||||
update_location(vm, hi);
|
||||
}
|
||||
|
||||
if (node[0] > 0x12) {
|
||||
update_location(vm, node + 0x20);
|
||||
if (node[0] == 0x24) {
|
||||
uint8_t *movie = *(uint8_t **)(node + 0x28);
|
||||
if (movie != NULL && (movie[0] & F_GREY) == 0) {
|
||||
do {
|
||||
uint8_t *par = *(uint8_t **)(movie + TEN_MOV_PARENT);
|
||||
while (par != NULL && (movie[1] & F_GREY) == 0) {
|
||||
movie = par;
|
||||
par = *(uint8_t **)(movie + TEN_MOV_PARENT);
|
||||
}
|
||||
visit_movies(vm, update_location, movie);
|
||||
movie = *(uint8_t **)(movie + TEN_MOV_PARENT);
|
||||
} while (movie != NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Recursively clear the post-tenure mark/dirty bits on a movie subtree,
|
||||
* optionally counting the nodes visited. */
|
||||
static void clear_movies_post_tenure(void *vm, uint8_t *movie, int *count) {
|
||||
if ((movie[0] & F_REMEMBERED) != 0) {
|
||||
movie[1] |= 0x80;
|
||||
movie[0] &= (uint8_t)~0x10u;
|
||||
iggy_clear_movie_dirty(PLAYER(vm), movie);
|
||||
}
|
||||
if (count != NULL) {
|
||||
*count = *count + 1;
|
||||
}
|
||||
if ((movie[0] & F_GREY) != 0) {
|
||||
movie[0] &= (uint8_t)~F_GREY;
|
||||
}
|
||||
if (movie[3] == 7) {
|
||||
for (intptr_t i = 0; i < 0x20; i += WORD) {
|
||||
uint8_t *child = *(uint8_t **)(i + *(uint8_t **)(movie + 0x48));
|
||||
if (child != NULL) {
|
||||
clear_movies_post_tenure(vm, child, count);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (uint8_t *child = *(uint8_t **)(movie + 0x10); child != NULL;
|
||||
child = *(uint8_t **)(child + 8)) {
|
||||
clear_movies_post_tenure(vm, child, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The full young-generation promotion pass. */
|
||||
static void tenure(void *vm) {
|
||||
uint8_t *young_base = VMP(vm, G_YOUNG_BASE);
|
||||
uint32_t young_size = VMU32(vm, G_YOUNG_SIZE);
|
||||
int sprite_count = 0;
|
||||
|
||||
gc_tm_enter(0x1015, "(iggy/gc)iggy_gc_tenure");
|
||||
VMU64(vm, G_STEP_START) = rrGetTime();
|
||||
VMU16(vm, G_IN_GC) = 1;
|
||||
as3vm_interpreter_notice_gc(0);
|
||||
VMI32(vm, G_OLD_SNAP) = VMI32(vm, G_SCAN_WORDS);
|
||||
|
||||
/* Fold the remembered set into the old scan, or rebuild it on overflow. */
|
||||
if (VMI32(vm, G_REM_OVF) == 0) {
|
||||
for (uint8_t **e = (uint8_t **)VMP(vm, G_REM_BASE);
|
||||
(uintptr_t)e < (uintptr_t)VMP(vm, G_REM_APPEND); e++) {
|
||||
uint8_t *node = *e;
|
||||
node[1] &= (uint8_t)~F_REMEMBERED;
|
||||
}
|
||||
for (uint8_t **e = (uint8_t **)VMP(vm, G_REM_BASE);
|
||||
(uintptr_t)e < (uintptr_t)VMP(vm, G_REM_APPEND); e++) {
|
||||
update_old_object(vm, *e);
|
||||
}
|
||||
} else {
|
||||
VMU32(vm, G_REM_OVF) = 0;
|
||||
old_process_remembered_set_overflow(vm);
|
||||
}
|
||||
VMP(vm, G_REM_APPEND) = VMP(vm, G_REM_BASE);
|
||||
|
||||
/* Root scan. */
|
||||
visit_roots(vm, update_location, 0);
|
||||
|
||||
for (uintptr_t p = VMU(vm, VM_OPSTACK_TOP); p < VMU(vm, VM_OPSTACK_LIMIT);
|
||||
p += WORD) {
|
||||
update_location(vm, (void *)p);
|
||||
}
|
||||
for (uintptr_t p = VMU(vm, G_STACK2_TOP); p < VMU(vm, G_STACK2_LIMIT);
|
||||
p += WORD) {
|
||||
update_location(vm, (void *)p);
|
||||
}
|
||||
|
||||
visit_movies(vm, update_location, PLAYER(vm) + P_MOVIE_A);
|
||||
visit_movies(vm, update_location, PLAYER(vm) + P_MOVIE_B);
|
||||
|
||||
/* Walk the dirty movie list, propagating dirty up to the highest
|
||||
* unmarked/undirty ancestor, then relocating that subtree's roots. */
|
||||
for (uint8_t *d = *(uint8_t **)(PLAYER(vm) + P_DIRTY); d != NULL;) {
|
||||
uint8_t *next = *(uint8_t **)(d + TEN_MOV_DIRTYNEXT);
|
||||
uint8_t *top = d;
|
||||
uint8_t flags = d[1];
|
||||
for (;;) {
|
||||
if ((flags & F_GREY) != 0) {
|
||||
break;
|
||||
}
|
||||
uint8_t *par = *(uint8_t **)(top + TEN_MOV_PARENT);
|
||||
if (par == NULL || (par[0] & F_REMEMBERED) != 0) {
|
||||
break;
|
||||
}
|
||||
iggy_mark_movie_dirty(PLAYER(vm), par);
|
||||
flags = par[1];
|
||||
top = par;
|
||||
}
|
||||
if ((*(uint8_t **)(top + TEN_MOV_PARENT) == NULL || (top[1] & F_GREY) != 0) &&
|
||||
(top[0] & F_GREY) == 0) {
|
||||
visit_movies(vm, update_location, top);
|
||||
}
|
||||
d = next;
|
||||
}
|
||||
|
||||
/* The interpreter trait root and its class chain. */
|
||||
update_location(vm, (uint8_t *)vm + TEN_TRAITROOT);
|
||||
for (uint8_t *c = VMP(vm, TEN_TRAITROOT); c != NULL;
|
||||
c = *(uint8_t **)(c + TEN_TRAITLINK)) {
|
||||
update_location(vm, c + TEN_TRAITLINK);
|
||||
}
|
||||
|
||||
/* Drain the deferred worklists to fixpoint, then finalize and reset. */
|
||||
for (;;) {
|
||||
if (VMP(vm, G_UPDATE_CHAIN) == NULL) {
|
||||
if (VMP(vm, G_CLASS_CHAIN) == (uint8_t *)1) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
do {
|
||||
uint8_t *arr = VMP(vm, G_UPDATE_CHAIN);
|
||||
VMP(vm, G_UPDATE_CHAIN) = *(uint8_t **)(arr + TEN_BIGNEXT);
|
||||
update_old_object(vm, *(uint8_t **)(arr + 8) - 1);
|
||||
} while (VMP(vm, G_UPDATE_CHAIN) != NULL);
|
||||
}
|
||||
|
||||
while (VMP(vm, G_CLASS_CHAIN) != (uint8_t *)1) {
|
||||
uint8_t *cls = VMP(vm, G_CLASS_CHAIN);
|
||||
VMP(vm, G_CLASS_CHAIN) = *(uint8_t **)(cls + TEN_CLS_NEXT);
|
||||
*(uint8_t **)(cls + TEN_CLS_NEXT) = NULL;
|
||||
do {
|
||||
uint8_t *lista = *(uint8_t **)(cls + TEN_CLS_LISTA);
|
||||
if (*(uint8_t **)(cls + TEN_CLS_SLOT) != NULL) {
|
||||
update_location(vm, *(uint8_t **)(cls + TEN_CLS_SLOT));
|
||||
}
|
||||
for (uint8_t *n = lista; n != NULL;
|
||||
n = *(uint8_t **)(n + 2 * WORD)) {
|
||||
uint8_t *v = *(uint8_t **)n;
|
||||
if (v != NULL && n[TEN_CLSNODE_FLAG] == 0 && *(uintptr_t *)v != 7) {
|
||||
update_location(vm, v);
|
||||
}
|
||||
}
|
||||
for (uint8_t *n = *(uint8_t **)(cls + TEN_CLS_LISTB); n != NULL;
|
||||
n = *(uint8_t **)(n + 2 * WORD)) {
|
||||
uint8_t *v = *(uint8_t **)n;
|
||||
if (v != NULL && n[TEN_CLSNODE_FLAG] == 0 && *(uintptr_t *)v != 7) {
|
||||
update_location(vm, v);
|
||||
}
|
||||
}
|
||||
cls = *(uint8_t **)(cls + TEN_CLS_ITER);
|
||||
} while (cls != NULL);
|
||||
}
|
||||
}
|
||||
|
||||
/* Finalizable list A: replace dead references with the undefined sentinel. */
|
||||
for (uint8_t *blk = VMP(vm, VM_FINAL_A); blk != NULL;
|
||||
blk = *(uint8_t **)blk) {
|
||||
if (*(int32_t *)(blk + TEN_FINAL_COUNT) != 0) {
|
||||
uint8_t **s = (uint8_t **)(blk + TEN_FINAL_SLOTS);
|
||||
for (int i = 0; i < 0x100; i++, s++) {
|
||||
uint8_t *v = *s;
|
||||
if (TEN_IN_YOUNG(v, young_base, young_size)) {
|
||||
*s = (v[0] == 2) ? *(uint8_t **)(v + TEN_FWD) : (uint8_t *)7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Const-pool root table. */
|
||||
if (VMI32(vm, TEN_ROOTTAB_FLAG) != 0) {
|
||||
uint8_t *entry = (uint8_t *)vm + TEN_ROOTTAB;
|
||||
for (int i = 0; i < 0x200; i++, entry += TEN_ROOTTAB_STRIDE) {
|
||||
uint8_t *v = *(uint8_t **)entry;
|
||||
if (TEN_IN_YOUNG(v, young_base, young_size)) {
|
||||
*(uint8_t **)entry = (v[0] == 2) ? *(uint8_t **)(v + TEN_FWD) : NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Clear the movie-root mark bits, then the sprite list (counting when a
|
||||
* notify callback is installed, otherwise just clearing). */
|
||||
uint8_t *movie_a = PLAYER(vm) + P_MOVIE_A;
|
||||
if ((movie_a[0] & F_GREY) != 0) {
|
||||
clear_movies_post_tenure(vm, movie_a, NULL);
|
||||
}
|
||||
uint8_t *movie_b = PLAYER(vm) + P_MOVIE_B;
|
||||
if ((movie_b[0] & F_GREY) != 0) {
|
||||
clear_movies_post_tenure(vm, movie_b, NULL);
|
||||
}
|
||||
|
||||
if (VMU(vm, G_NOTIFY_CB) == 0) {
|
||||
for (uint8_t *sp = *(uint8_t **)(PLAYER(vm) + P_SPRITES); sp != NULL;
|
||||
sp = *(uint8_t **)(sp + 8)) {
|
||||
if ((sp[0] & F_GREY) != 0) {
|
||||
clear_movies_post_tenure(vm, sp, NULL);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (uint8_t *sp = *(uint8_t **)(PLAYER(vm) + P_SPRITES); sp != NULL;
|
||||
sp = *(uint8_t **)(sp + 8)) {
|
||||
if ((sp[0] & F_GREY) == 0) {
|
||||
sprite_count += count_movies_rec(sp, 0);
|
||||
} else {
|
||||
clear_movies_post_tenure(vm, sp, &sprite_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Finalizable list B: run finalizers on dead references, then null them. */
|
||||
for (uint8_t *blk = VMP(vm, VM_FINAL_B); blk != NULL;
|
||||
blk = *(uint8_t **)blk) {
|
||||
if (*(int32_t *)(blk + TEN_FINAL_COUNT) != 0) {
|
||||
uint8_t **s = (uint8_t **)(blk + TEN_FINAL_SLOTS);
|
||||
for (int i = 0; i < 0x100; i++, s++) {
|
||||
uint8_t *v = *s;
|
||||
if (TEN_IN_YOUNG(v, young_base, young_size)) {
|
||||
if (v[0] == 2) {
|
||||
*s = *(uint8_t **)(v + TEN_FWD);
|
||||
} else {
|
||||
as3vm_finalize(vm, (void *)(v - 1));
|
||||
*s = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Reset the finalizable active-counts. */
|
||||
for (uint8_t *blk = VMP(vm, VM_FINAL_A); blk != NULL; blk = *(uint8_t **)blk) {
|
||||
*(int32_t *)(blk + TEN_FINAL_COUNT) = 0;
|
||||
}
|
||||
for (uint8_t *blk = VMP(vm, VM_FINAL_B); blk != NULL; blk = *(uint8_t **)blk) {
|
||||
*(int32_t *)(blk + TEN_FINAL_COUNT) = 0;
|
||||
}
|
||||
|
||||
if (VMU(vm, G_NOTIFY_CB) != 0) {
|
||||
int movie_count = count_movies_rec(PLAYER(vm) + P_MOVIE_A, 0);
|
||||
int amount = (int)(((uintptr_t)VMP(vm, G_YOUNG_BUMP) -
|
||||
(uintptr_t)VMP(vm, G_YOUNG_BASE)) >>
|
||||
TEN_YOUNG_SHIFT);
|
||||
gc_notify(vm, 0, amount, (uint32_t)movie_count, (uint32_t)sprite_count);
|
||||
}
|
||||
|
||||
/* Reset the nursery to an empty semispace. */
|
||||
VMP(vm, G_YOUNG_BUMP) = VMP(vm, G_YOUNG_BASE);
|
||||
VMU16(vm, G_IN_GC) = 0;
|
||||
uint32_t limit_words = VMU32(vm, G_RATIO_SIGN) >> TEN_YOUNG_SHIFT;
|
||||
if (VMU32(vm, G_YOUNG_SIZE) < limit_words) {
|
||||
limit_words = VMU32(vm, G_YOUNG_SIZE);
|
||||
}
|
||||
VMP(vm, G_YOUNG_LIMIT) =
|
||||
(uint8_t *)((uintptr_t)limit_words + (uintptr_t)VMP(vm, G_YOUNG_BASE));
|
||||
|
||||
gc_tm_leave(0x11c8);
|
||||
}
|
||||
42
iggy-decompiled/src/reconstructed/as3vm_gc_validate.inc
Normal file
42
iggy-decompiled/src/reconstructed/as3vm_gc_validate.inc
Normal file
@@ -0,0 +1,42 @@
|
||||
/* as3vm_gc: release-mode heap validators.
|
||||
*
|
||||
* as3vm_validate_memory / validate_onstage_movies are compiled to a bare
|
||||
* `return;` in the retail build (the debug asserts are stripped, raw:816 /
|
||||
* raw:3007). The three old-heap walkers iterate every pool from the pool-list
|
||||
* head (G_POOL_HEAD); a NULL head returns immediately. In release they walk
|
||||
* the blocks and compute each block's size but take no action -- the assert
|
||||
* bodies that would have fired on corruption are gone.
|
||||
*/
|
||||
|
||||
#define VAL_POOL_NEXT 0
|
||||
#define VAL_POOL_SIZE WORD
|
||||
#define VAL_POOL_DATA (2 * WORD)
|
||||
|
||||
/* shared release-mode walk over every block of every old pool. */
|
||||
static void gc_walk_old_heap(void *vm) {
|
||||
uint8_t *pool = VMP(vm, G_POOL_HEAD);
|
||||
while (pool != NULL) {
|
||||
uint8_t *cursor = pool + VAL_POOL_DATA;
|
||||
uint8_t *end = cursor + *(uintptr_t *)(pool + VAL_POOL_SIZE);
|
||||
while ((uintptr_t)cursor < (uintptr_t)end) {
|
||||
uint32_t size;
|
||||
if ((NB(cursor, N_FLAGS) & F_FREE) == 0) {
|
||||
size = gc_node_bytes(cursor);
|
||||
} else {
|
||||
size = NU32(cursor, N_SIZE);
|
||||
}
|
||||
cursor += size;
|
||||
}
|
||||
pool = *(uint8_t **)(pool + VAL_POOL_NEXT);
|
||||
}
|
||||
}
|
||||
|
||||
void as3vm_validate_memory(void) { return; }
|
||||
|
||||
void validate_onstage_movies(void) { return; }
|
||||
|
||||
void as3vm_validate_mid_sweep(void *vm) { gc_walk_old_heap(vm); }
|
||||
|
||||
void as3vm_validate_old_heap(void *vm) { gc_walk_old_heap(vm); }
|
||||
|
||||
void as3vm_validate_post_sweep(void *vm) { gc_walk_old_heap(vm); }
|
||||
@@ -72,3 +72,7 @@ as3vm_names 13 11 vita complete
|
||||
rrCore 0 0 durango,orbis,ps3,vita complete
|
||||
as3vm_create 32 31 durango,orbis,ps3,vita complete
|
||||
radss_xaudio2 3 1 durango complete
|
||||
as3vm_gc 52 14 durango complete
|
||||
as3vm_gc 37 14 orbis complete
|
||||
as3vm_gc 40 14 ps3 complete
|
||||
as3vm_gc 35 14 vita complete
|
||||
|
||||
|
281
tests/support/as3vm_gc_test_support.c
Normal file
281
tests/support/as3vm_gc_test_support.c
Normal file
@@ -0,0 +1,281 @@
|
||||
/*
|
||||
* Host harness for the reconstructed as3vm_gc translation unit.
|
||||
*
|
||||
* as3vm_gc is Iggy's AS3 / AVM2 garbage collector: an incremental mark-sweep
|
||||
* collector with a young/old (nursery + remembered-set) generational split, a
|
||||
* Dijkstra grey-stack write barrier, finalizers, and a family of heap
|
||||
* validators. UNLIKE the sibling as3vm_create (which owned no data global),
|
||||
* as3vm_gc owns exactly TWO externally named pointer globals -- updated_object
|
||||
* (set by the tenure/promotion path to the node being promoted) and
|
||||
* iggy_debug_entity (set by the sweeper to the last detached block). Those two
|
||||
* globals are DEFINED BY THE MODULE, not by this harness; the runtime test
|
||||
* reads them back through ctypes to confirm they exist and start NULL.
|
||||
*
|
||||
* The GC node header (recovered from raw/durango + cross-checked against the
|
||||
* 32-bit vita layout) is: node[0] = kind byte (1 stringdata, 2 array, ... ,
|
||||
* 0x11 remembered-forward, higher kinds object/closure), node[1] = flags byte
|
||||
* with bit 0x01 = live/mark-low, 0x04 = in-remembered-set, 0x08 = grey/queued,
|
||||
* 0x18 = marking mask, 0x20 = old-generation, 0x40 = free/detached; the tagged
|
||||
* value is (node + 1) with the low bit set because nodes are >= 16-byte aligned
|
||||
* (minimum block size 0x20). Small integers box to (value << 1) with the low
|
||||
* bit clear -- the same tagging as as3vm_create.
|
||||
*
|
||||
* The symbols supplied here are exactly the genuine nm ' U ' imports of the
|
||||
* retail object (invariant real-Iggy/rr/libc set on all four platforms; there
|
||||
* is NO per-platform libc divergence for this module -- see the cross test).
|
||||
* This DURANGO harness models them as functional / recording stubs so the
|
||||
* durango dylib links and the collector actually runs:
|
||||
*
|
||||
* - allocation core (iggy_allocation, satisfied): iggy_arena_malloc,
|
||||
* iggy_malloc_CANFAIL_fileline -> functional bump arena handing out zeroed
|
||||
* aligned buffers, recording tag + count, with a controllable
|
||||
* fail-after-N so the gc_init OOM path is observable; iggy_free_alloc ->
|
||||
* records the freed block + count.
|
||||
* - interpreter / class collaborators (as3vm_interp / as3vm_class, unbuilt):
|
||||
* as3vm_finalize (records the finalized object),
|
||||
* as3vm_interpreter_notice_gc, as3vm_interpreter_request_collection,
|
||||
* as3vm_needs_cleaning -> record.
|
||||
* - swf_play / display collaborators (unbuilt): iggy_mark_movie_dirty,
|
||||
* iggy_clear_movie_dirty, iggy_destroy_sprite -> record.
|
||||
* - timing (win32_rrTime, satisfied): rrGetTime / rrTimeToMicros ->
|
||||
* controllable clock so the incremental step time-budget is drivable.
|
||||
* - null/undefined sentinel + telemetry DATA symbols (as3vm_nullundef_storage,
|
||||
* iggy_tm_internal, iggy_tm_normal) -> provided as data (referenced by
|
||||
* address only; NULL / zeroed).
|
||||
* - libc: memcpy / memset / qsort resolve against the host and are
|
||||
* deliberately NOT stubbed (sibling as3vm_create / iggy_sound convention);
|
||||
* longjmp is overridden with a recording escape to a harness setjmp anchor
|
||||
* (as3vm_gc_test_protect) so the OOM/abort longjmp is observable WITHOUT
|
||||
* crashing the test process.
|
||||
*/
|
||||
|
||||
#include <setjmp.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* ---- Recording counters + controllable collaborator state. ---- */
|
||||
static size_t finalize_count;
|
||||
static size_t interpreter_notice_gc_count;
|
||||
static size_t interpreter_request_collection_count;
|
||||
static size_t needs_cleaning_count;
|
||||
static size_t arena_malloc_count;
|
||||
static size_t malloc_canfail_count;
|
||||
static size_t free_alloc_count;
|
||||
static size_t mark_movie_dirty_count;
|
||||
static size_t clear_movie_dirty_count;
|
||||
static size_t destroy_sprite_count;
|
||||
static size_t get_time_count;
|
||||
static size_t longjmp_count;
|
||||
|
||||
static void *last_finalized_ptr;
|
||||
static void *last_freed_ptr;
|
||||
static void *last_alloc_ptr;
|
||||
static int32_t last_alloc_tag;
|
||||
static int32_t last_longjmp_value;
|
||||
static int32_t last_notice_phase;
|
||||
|
||||
/* Controllable failure injection + clock. */
|
||||
static int arena_fail_after = -1; /* -1 = never fail. */
|
||||
static int canfail_fail_after = -1; /* -1 = never fail. */
|
||||
static uint64_t fake_time;
|
||||
|
||||
/* Bump arena backing the allocator stubs. */
|
||||
static uint8_t arena_pool[8 << 20];
|
||||
static size_t arena_used;
|
||||
|
||||
/* setjmp anchor for the OOM/abort longjmp escape. */
|
||||
static jmp_buf gc_recovery;
|
||||
|
||||
static void *pool_alloc(size_t size, size_t align) {
|
||||
if (align < 16u) {
|
||||
align = 16u;
|
||||
}
|
||||
size_t base = (arena_used + (align - 1u)) & ~(align - 1u);
|
||||
size_t rounded = (size + 63u) & ~(size_t)63u;
|
||||
if (rounded == 0u) {
|
||||
rounded = 64u;
|
||||
}
|
||||
if (base + rounded > sizeof(arena_pool)) {
|
||||
return 0;
|
||||
}
|
||||
arena_used = base + rounded;
|
||||
return arena_pool + base;
|
||||
}
|
||||
|
||||
void as3vm_gc_test_reset(void) {
|
||||
finalize_count = 0;
|
||||
interpreter_notice_gc_count = 0;
|
||||
interpreter_request_collection_count = 0;
|
||||
needs_cleaning_count = 0;
|
||||
arena_malloc_count = 0;
|
||||
malloc_canfail_count = 0;
|
||||
free_alloc_count = 0;
|
||||
mark_movie_dirty_count = 0;
|
||||
clear_movie_dirty_count = 0;
|
||||
destroy_sprite_count = 0;
|
||||
get_time_count = 0;
|
||||
longjmp_count = 0;
|
||||
last_finalized_ptr = 0;
|
||||
last_freed_ptr = 0;
|
||||
last_alloc_ptr = 0;
|
||||
last_alloc_tag = 0;
|
||||
last_longjmp_value = 0;
|
||||
last_notice_phase = 0;
|
||||
arena_fail_after = -1;
|
||||
canfail_fail_after = -1;
|
||||
fake_time = 0;
|
||||
arena_used = 0;
|
||||
}
|
||||
|
||||
/* ---- Count / last-value accessors. ---- */
|
||||
size_t as3vm_gc_test_finalize_count(void) { return finalize_count; }
|
||||
size_t as3vm_gc_test_interpreter_notice_gc_count(void) {
|
||||
return interpreter_notice_gc_count;
|
||||
}
|
||||
size_t as3vm_gc_test_interpreter_request_collection_count(void) {
|
||||
return interpreter_request_collection_count;
|
||||
}
|
||||
size_t as3vm_gc_test_needs_cleaning_count(void) { return needs_cleaning_count; }
|
||||
size_t as3vm_gc_test_arena_malloc_count(void) { return arena_malloc_count; }
|
||||
size_t as3vm_gc_test_malloc_canfail_count(void) { return malloc_canfail_count; }
|
||||
size_t as3vm_gc_test_free_alloc_count(void) { return free_alloc_count; }
|
||||
size_t as3vm_gc_test_mark_movie_dirty_count(void) {
|
||||
return mark_movie_dirty_count;
|
||||
}
|
||||
size_t as3vm_gc_test_clear_movie_dirty_count(void) {
|
||||
return clear_movie_dirty_count;
|
||||
}
|
||||
size_t as3vm_gc_test_destroy_sprite_count(void) { return destroy_sprite_count; }
|
||||
size_t as3vm_gc_test_get_time_count(void) { return get_time_count; }
|
||||
size_t as3vm_gc_test_longjmp_count(void) { return longjmp_count; }
|
||||
|
||||
void *as3vm_gc_test_last_finalized_ptr(void) { return last_finalized_ptr; }
|
||||
void *as3vm_gc_test_last_freed_ptr(void) { return last_freed_ptr; }
|
||||
void *as3vm_gc_test_last_alloc_ptr(void) { return last_alloc_ptr; }
|
||||
int32_t as3vm_gc_test_last_alloc_tag(void) { return last_alloc_tag; }
|
||||
int32_t as3vm_gc_test_last_longjmp_value(void) { return last_longjmp_value; }
|
||||
int32_t as3vm_gc_test_last_notice_phase(void) { return last_notice_phase; }
|
||||
|
||||
/* ---- Controls. ---- */
|
||||
void as3vm_gc_test_set_arena_fail_after(int n) { arena_fail_after = n; }
|
||||
void as3vm_gc_test_set_canfail_fail_after(int n) { canfail_fail_after = n; }
|
||||
void as3vm_gc_test_set_time(uint64_t value) { fake_time = value; }
|
||||
void as3vm_gc_test_advance_time(uint64_t delta) { fake_time += delta; }
|
||||
size_t as3vm_gc_test_arena_used(void) { return arena_used; }
|
||||
|
||||
/* ---- Allocation-core imports (iggy_allocation). ---- */
|
||||
void *iggy_arena_malloc(void *player, void *owner, int size, int id) {
|
||||
(void)player;
|
||||
(void)owner;
|
||||
++arena_malloc_count;
|
||||
last_alloc_tag = id;
|
||||
if (arena_fail_after >= 0 && (int)arena_malloc_count > arena_fail_after) {
|
||||
last_alloc_ptr = 0;
|
||||
return 0;
|
||||
}
|
||||
last_alloc_ptr = pool_alloc(size > 0 ? (size_t)size : 16u, 16u);
|
||||
return last_alloc_ptr;
|
||||
}
|
||||
|
||||
void *iggy_malloc_CANFAIL_fileline(intptr_t player, int size, uint32_t tag) {
|
||||
(void)player;
|
||||
++malloc_canfail_count;
|
||||
last_alloc_tag = (int32_t)tag;
|
||||
if (canfail_fail_after >= 0 &&
|
||||
(int)malloc_canfail_count > canfail_fail_after) {
|
||||
last_alloc_ptr = 0;
|
||||
return 0;
|
||||
}
|
||||
last_alloc_ptr = pool_alloc(size > 0 ? (size_t)size : 16u, 16u);
|
||||
return last_alloc_ptr;
|
||||
}
|
||||
|
||||
void iggy_free_alloc(intptr_t player, void *block) {
|
||||
(void)player;
|
||||
last_freed_ptr = block;
|
||||
++free_alloc_count;
|
||||
}
|
||||
|
||||
/* ---- Interpreter / class collaborators (record only). ---- */
|
||||
void as3vm_finalize(void *vm, void *object) {
|
||||
(void)vm;
|
||||
last_finalized_ptr = object;
|
||||
++finalize_count;
|
||||
}
|
||||
|
||||
void as3vm_interpreter_notice_gc(int phase) {
|
||||
last_notice_phase = phase;
|
||||
++interpreter_notice_gc_count;
|
||||
}
|
||||
|
||||
void as3vm_interpreter_request_collection(void *vm) {
|
||||
(void)vm;
|
||||
++interpreter_request_collection_count;
|
||||
}
|
||||
|
||||
int as3vm_needs_cleaning(void *vm, void *object) {
|
||||
(void)vm;
|
||||
(void)object;
|
||||
++needs_cleaning_count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ---- swf_play / display collaborators (record only). ---- */
|
||||
void iggy_mark_movie_dirty(void *player, void *object) {
|
||||
(void)player;
|
||||
(void)object;
|
||||
++mark_movie_dirty_count;
|
||||
}
|
||||
|
||||
void iggy_clear_movie_dirty(void *player, void *object) {
|
||||
(void)player;
|
||||
(void)object;
|
||||
++clear_movie_dirty_count;
|
||||
}
|
||||
|
||||
void iggy_destroy_sprite(void *player, void *sprite) {
|
||||
(void)player;
|
||||
(void)sprite;
|
||||
++destroy_sprite_count;
|
||||
}
|
||||
|
||||
/* ---- Timing imports (controllable clock drives the step time-budget). ---- */
|
||||
uint64_t rrGetTime(void) {
|
||||
++get_time_count;
|
||||
return fake_time;
|
||||
}
|
||||
|
||||
uint64_t rrTimeToMicros(int64_t time) {
|
||||
/* The retail unit is already microsecond-scaled ticks in the harness. */
|
||||
return (uint64_t)(time < 0 ? -time : time);
|
||||
}
|
||||
|
||||
/* ---- null/undefined sentinel + telemetry DATA symbols. ---- */
|
||||
uint8_t as3vm_nullundef_storage[64];
|
||||
void *iggy_tm_internal;
|
||||
void *iggy_tm_normal;
|
||||
|
||||
/*
|
||||
* ---- OOM/abort longjmp escape ----
|
||||
* The GC longjmps to a buffer installed by the interpreter on an out-of-memory
|
||||
* or heap-corruption abort. as3vm_gc_test_protect installs a harness recovery
|
||||
* anchor with _setjmp, and the overridden longjmp records the value and escapes
|
||||
* to it (via _longjmp, an independent pair) so the abort path is observable
|
||||
* without unwinding into an un-anchored frame.
|
||||
*/
|
||||
int as3vm_gc_test_protect(void (*fn)(void *), void *argument) {
|
||||
int recovered = _setjmp(gc_recovery);
|
||||
if (recovered == 0) {
|
||||
fn(argument);
|
||||
return 0;
|
||||
}
|
||||
return recovered;
|
||||
}
|
||||
|
||||
void longjmp(jmp_buf env, int value) {
|
||||
(void)env;
|
||||
++longjmp_count;
|
||||
last_longjmp_value = value;
|
||||
_longjmp(gc_recovery, value == 0 ? 1 : value);
|
||||
}
|
||||
406
tests/test_reconstructed_as3vm_gc.py
Normal file
406
tests/test_reconstructed_as3vm_gc.py
Normal file
@@ -0,0 +1,406 @@
|
||||
"""Runtime tests for the reconstructed as3vm_gc translation unit.
|
||||
|
||||
RED STATE: as3vm_gc.c / as3vm_gc.h do not exist yet, so setUpClass raises
|
||||
"missing as3vm_gc runtime inputs". Every assertion below is grounded in a
|
||||
specific line of iggy-decompiled/raw/durango/as3vm_gc.c (cited inline) so a
|
||||
faithful DURANGO reconstruction turns these green.
|
||||
|
||||
as3vm_gc is Iggy's AS3 / AVM2 garbage collector: an incremental mark-sweep
|
||||
collector with a young/old (nursery + remembered-set) generational split, a
|
||||
Dijkstra grey-stack write barrier, finalizers, and a family of heap validators.
|
||||
It exports 14 entry points and owns EXACTLY TWO externally named pointer globals
|
||||
-- updated_object and iggy_debug_entity -- which this test reads back through
|
||||
ctypes to confirm the module defines them and zero-initializes them.
|
||||
|
||||
GC NODE HEADER (raw, cross-checked vita 32-bit):
|
||||
node[0] = kind byte (1 stringdata, 2 array, ..., 0x11 remembered-forward,
|
||||
higher kinds object/closure).
|
||||
node[1] = flags byte: 0x01 live/mark-low, 0x04 in-remembered-set,
|
||||
0x08 grey/queued, 0x18 marking mask, 0x20 old-generation,
|
||||
0x40 free/detached.
|
||||
tagged value = (node + 1); small integers box to (value << 1). Nodes are
|
||||
>= 16-byte aligned (minimum block 0x20) so the +1 tag is unambiguous.
|
||||
|
||||
VM-CONTEXT OFFSETS pinned here (64-bit DURANGO, cited per test):
|
||||
0x0058 pending incremental-mark work head (0 => step/alloc do no GC work)
|
||||
0x00e0 finalizable-block list head (vm[0x1c], raw:95)
|
||||
0x6978 old-forced / GC-disabled flag (27000, raw:41)
|
||||
0x69a8 old-pool / block list head (vm[0xd35], raw:108/836)
|
||||
0x6aa8 remembered-set append slot pointer (raw:12)
|
||||
0x6ab8 remembered-set soft limit (raw:15)
|
||||
0x6ad0 young bump pointer (vm[0xd5a], raw:60)
|
||||
0x6ad8 young limit (vm[0xd5b], raw:47)
|
||||
0x6ae0 young base (vm[0xd5c], raw:181)
|
||||
0x6af0 young size (u32) (vm+0xd5e, raw:170)
|
||||
0x00bc old-pool size config (u32) (raw:504)
|
||||
|
||||
What is pinned is real, deterministic DURANGO behavior: the two no-op release
|
||||
validators, the young bump allocator (round-up-to-even-words, flag-byte zero,
|
||||
returned node pointer), gc_alloc_remembered's old-generation gate (young nodes
|
||||
are NOT remembered), the generational write barrier's remembered-set append +
|
||||
remembered-bit set, gc_step / the validators short-circuiting on an empty heap,
|
||||
gc_init's three arena allocations + young-field setup + OOM (return 0) path,
|
||||
gc_clear's finalizer sweep + field reset, and the two owned globals starting
|
||||
NULL. The DEEP collector internals -- the exact mark-bit / grey-stack state
|
||||
machine of gc_step, tenure/promotion (which sets updated_object), the sweep
|
||||
coalescing (which sets iggy_debug_entity), and old_block_free's free-list
|
||||
merge -- are intentionally NOT exercised here and are flagged in the module
|
||||
report as structural-only pins for the impl / review phase.
|
||||
|
||||
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
|
||||
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_gc.c"
|
||||
HEADER = RECONSTRUCTED / "as3vm_gc.h"
|
||||
SUPPORT = ROOT / "tests/support/as3vm_gc_test_support.c"
|
||||
|
||||
VM_BYTES = 0x7000
|
||||
OFF_MARK_WORK = 0x58
|
||||
OFF_FINALIZE_LIST = 0xE0
|
||||
OFF_OLD_FORCED = 0x6978
|
||||
OFF_POOL_LIST = 0x69A8
|
||||
OFF_REM_APPEND = 0x6AA8
|
||||
OFF_REM_LIMIT = 0x6AB8
|
||||
OFF_YOUNG_BUMP = 0x6AD0
|
||||
OFF_YOUNG_LIMIT = 0x6AD8
|
||||
OFF_YOUNG_BASE = 0x6AE0
|
||||
OFF_YOUNG_SIZE = 0x6AF0
|
||||
OFF_OLD_POOL_CFG = 0xBC
|
||||
|
||||
|
||||
def _u8(base, offset):
|
||||
return ctypes.c_uint8.from_address(base + offset).value
|
||||
|
||||
|
||||
def _u32(base, offset):
|
||||
return ctypes.c_uint32.from_address(base + offset).value
|
||||
|
||||
|
||||
def _u64(base, offset):
|
||||
return ctypes.c_uint64.from_address(base + offset).value
|
||||
|
||||
|
||||
def _set64(base, offset, value):
|
||||
ctypes.c_uint64.from_address(base + offset).value = value
|
||||
|
||||
|
||||
def _set32(base, offset, value):
|
||||
ctypes.c_uint32.from_address(base + offset).value = value
|
||||
|
||||
|
||||
class ReconstructedAs3vmGcTests(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_gc runtime inputs: " + ", ".join(missing))
|
||||
cls.temp = tempfile.TemporaryDirectory()
|
||||
cls.base = Path(cls.temp.name) / "as3vm_gc.dylib"
|
||||
build = subprocess.run([
|
||||
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra",
|
||||
"-Werror", "-Wl,-undefined,dynamic_lookup",
|
||||
"-DIGGY_RECONSTRUCT_AS3VM_GC_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_gc_{self._testMethodName}.dylib")
|
||||
shutil.copyfile(self.base, unique)
|
||||
self.lib = ctypes.CDLL(str(unique))
|
||||
self._bind(self.lib)
|
||||
self.lib.as3vm_gc_test_reset()
|
||||
self._keep = []
|
||||
|
||||
def _bind(self, lib):
|
||||
void_p = ctypes.c_void_p
|
||||
u32 = ctypes.c_uint32
|
||||
u64 = ctypes.c_uint64
|
||||
|
||||
lib.as3vm_gc_test_reset.argtypes = []
|
||||
lib.as3vm_gc_test_reset.restype = None
|
||||
for name in ("finalize", "interpreter_notice_gc",
|
||||
"interpreter_request_collection", "needs_cleaning",
|
||||
"arena_malloc", "malloc_canfail", "free_alloc",
|
||||
"mark_movie_dirty", "clear_movie_dirty", "destroy_sprite",
|
||||
"get_time", "longjmp"):
|
||||
fn = getattr(lib, f"as3vm_gc_test_{name}_count")
|
||||
fn.restype = ctypes.c_size_t
|
||||
fn.argtypes = []
|
||||
for name in ("last_finalized_ptr", "last_freed_ptr", "last_alloc_ptr"):
|
||||
fn = getattr(lib, f"as3vm_gc_test_{name}")
|
||||
fn.restype = void_p
|
||||
fn.argtypes = []
|
||||
lib.as3vm_gc_test_last_alloc_tag.restype = ctypes.c_int32
|
||||
lib.as3vm_gc_test_last_alloc_tag.argtypes = []
|
||||
lib.as3vm_gc_test_set_arena_fail_after.restype = None
|
||||
lib.as3vm_gc_test_set_arena_fail_after.argtypes = [ctypes.c_int]
|
||||
|
||||
# Module exports under test. Ghidra typed the allocators 'void' but the
|
||||
# real ABI returns the node pointer (alloc_remembered consumes it,
|
||||
# raw:75); the reconstruction returns void*.
|
||||
lib.as3vm_gc_alloc.restype = void_p
|
||||
lib.as3vm_gc_alloc.argtypes = [void_p, u32]
|
||||
lib.as3vm_gc_alloc_remembered.restype = void_p
|
||||
lib.as3vm_gc_alloc_remembered.argtypes = [void_p, u32]
|
||||
lib.as3vm_gc_init.restype = u64
|
||||
lib.as3vm_gc_init.argtypes = [void_p, void_p, void_p]
|
||||
lib.as3vm_gc_clear.restype = None
|
||||
lib.as3vm_gc_clear.argtypes = [void_p]
|
||||
lib.as3vm_gc_step.restype = None
|
||||
lib.as3vm_gc_step.argtypes = [void_p]
|
||||
lib.as3vm_write_barrier_slowpath.restype = None
|
||||
lib.as3vm_write_barrier_slowpath.argtypes = [void_p, void_p, void_p]
|
||||
lib.as3vm_validate_memory.restype = None
|
||||
lib.as3vm_validate_memory.argtypes = []
|
||||
lib.as3vm_validate_mid_sweep.restype = None
|
||||
lib.as3vm_validate_mid_sweep.argtypes = [void_p]
|
||||
lib.as3vm_validate_old_heap.restype = None
|
||||
lib.as3vm_validate_old_heap.argtypes = [void_p]
|
||||
lib.as3vm_validate_post_sweep.restype = None
|
||||
lib.as3vm_validate_post_sweep.argtypes = [void_p]
|
||||
lib.validate_onstage_movies.restype = None
|
||||
lib.validate_onstage_movies.argtypes = []
|
||||
|
||||
def _count(self, name):
|
||||
return getattr(self.lib, f"as3vm_gc_test_{name}_count")()
|
||||
|
||||
def _no_collaborator_calls(self):
|
||||
for name in ("finalize", "interpreter_notice_gc",
|
||||
"interpreter_request_collection", "needs_cleaning",
|
||||
"arena_malloc", "free_alloc", "mark_movie_dirty",
|
||||
"clear_movie_dirty", "destroy_sprite", "get_time"):
|
||||
self.assertEqual(self._count(name), 0, name)
|
||||
|
||||
def _vm(self):
|
||||
vm = (ctypes.c_uint8 * VM_BYTES)()
|
||||
self._keep.append(vm)
|
||||
return ctypes.addressof(vm)
|
||||
|
||||
def _buffer(self, size):
|
||||
buf = (ctypes.c_uint8 * size)()
|
||||
self._keep.append(buf)
|
||||
return ctypes.addressof(buf)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# release-mode no-op validators
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_validate_memory_and_onstage_movies_are_release_noops(self):
|
||||
# raw:816-820 / raw:3007-3011 both bodies are a bare `return;` in the
|
||||
# release build -- no allocation, no collaborator call, no crash.
|
||||
self.lib.as3vm_validate_memory()
|
||||
self.lib.validate_onstage_movies()
|
||||
self._no_collaborator_calls()
|
||||
|
||||
def test_heap_validators_short_circuit_on_empty_old_list(self):
|
||||
# raw:836-873 / 890-927 / 944-981 each walker starts at *(vm+0x69a8);
|
||||
# a NULL head returns immediately, touching nothing.
|
||||
vm = self._vm()
|
||||
_set64(vm, OFF_POOL_LIST, 0)
|
||||
self.lib.as3vm_validate_mid_sweep(vm)
|
||||
self.lib.as3vm_validate_old_heap(vm)
|
||||
self.lib.as3vm_validate_post_sweep(vm)
|
||||
self._no_collaborator_calls()
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# young bump allocator
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def _arm_young(self, vm, span=0x1000):
|
||||
# Wire just the young nursery fields gc_alloc reads (raw:41-63): not
|
||||
# old-forced, room available, remembered append below its limit so the
|
||||
# allocator jumps straight to the bump (raw:54), no pending mark work.
|
||||
young = self._buffer(span)
|
||||
_set32(vm, OFF_OLD_FORCED, 0)
|
||||
_set64(vm, OFF_MARK_WORK, 0)
|
||||
_set64(vm, OFF_YOUNG_BUMP, young)
|
||||
_set64(vm, OFF_YOUNG_LIMIT, young + span)
|
||||
_set64(vm, OFF_YOUNG_BASE, young)
|
||||
_set64(vm, OFF_REM_APPEND, 0)
|
||||
_set64(vm, OFF_REM_LIMIT, 1)
|
||||
return young
|
||||
|
||||
def test_gc_alloc_bumps_even_words_and_zeroes_flag_byte(self):
|
||||
# raw:60-63 node = old bump ptr; bump by ((words&1)+words)*8 (odd word
|
||||
# counts round up to the next even count = 16-byte alignment); node[1]
|
||||
# (the flags byte) is cleared; the node pointer is returned.
|
||||
for words, expect in ((3, 32), (4, 32), (5, 48)):
|
||||
vm = self._vm()
|
||||
young = self._arm_young(vm)
|
||||
ctypes.c_uint8.from_address(young + 1).value = 0xFF
|
||||
node = self.lib.as3vm_gc_alloc(vm, words)
|
||||
self.assertEqual(node, young)
|
||||
self.assertEqual(_u64(vm, OFF_YOUNG_BUMP), young + expect)
|
||||
self.assertEqual(_u8(young, 1), 0)
|
||||
self.assertEqual(self._count("arena_malloc"), 0)
|
||||
|
||||
def test_gc_alloc_remembered_does_not_remember_young_nodes(self):
|
||||
# raw:70-82 alloc_remembered only tags (node[0]=0x11) + adds to the
|
||||
# remembered set when the returned node is OLD-generation (node[1]&0x20)
|
||||
# and the GC is not old-forced. A fresh young node has a zeroed flag
|
||||
# byte, so the gate is skipped: the node is returned as-is and the
|
||||
# remembered append pointer is untouched.
|
||||
vm = self._vm()
|
||||
young = self._arm_young(vm)
|
||||
node = self.lib.as3vm_gc_alloc_remembered(vm, 4)
|
||||
self.assertEqual(node, young)
|
||||
self.assertEqual(_u64(vm, OFF_YOUNG_BUMP), young + 32)
|
||||
self.assertNotEqual(_u8(young, 0), 0x11) # not tagged remembered
|
||||
self.assertEqual(_u64(vm, OFF_REM_APPEND), 0) # nothing appended
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# generational write barrier
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_write_barrier_appends_old_to_young_store_to_remembered_set(self):
|
||||
# raw:988-1023 + add_to_remembered raw:6-30 an old container (flag
|
||||
# 0x20) storing a young pointee (flag 0x20 clear), not yet remembered
|
||||
# (flag 0x04 clear), takes the generational branch: the container is
|
||||
# appended to *(vm+0x6aa8), the append pointer advances 8, and the
|
||||
# container's remembered bit (0x04) is set.
|
||||
vm = self._vm()
|
||||
src = self._buffer(0x40)
|
||||
dst = self._buffer(0x40)
|
||||
ctypes.c_uint8.from_address(src + 1).value = 0x20 # old, unremembered
|
||||
ctypes.c_uint8.from_address(dst + 1).value = 0x00 # young
|
||||
|
||||
rem = (ctypes.c_uint64 * 8)()
|
||||
self._keep.append(rem)
|
||||
rem_addr = ctypes.addressof(rem)
|
||||
_set64(vm, OFF_REM_APPEND, rem_addr)
|
||||
_set64(vm, OFF_REM_LIMIT, rem_addr + 8 * 8)
|
||||
|
||||
self.lib.as3vm_write_barrier_slowpath(vm, src, dst)
|
||||
self.assertEqual(rem[0], src) # container appended
|
||||
self.assertEqual(_u64(vm, OFF_REM_APPEND), rem_addr + 8)
|
||||
self.assertEqual(_u8(src, 1) & 0x04, 0x04) # remembered bit set
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# incremental step short-circuit
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_gc_step_is_noop_without_pending_mark_work(self):
|
||||
# raw:242-251 gc_step only runs tenure()+gc_step() when there is
|
||||
# pending mark work (*(vm+0x58) != 0) and the GC is not old-forced. An
|
||||
# empty mark head means an immediate return -- no clock read, no import.
|
||||
vm = self._vm()
|
||||
_set64(vm, OFF_MARK_WORK, 0)
|
||||
_set32(vm, OFF_OLD_FORCED, 0)
|
||||
self.lib.as3vm_gc_step(vm)
|
||||
self._no_collaborator_calls()
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# heap init: arena setup, young fields, OOM
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def _config(self, young_size, mark_bytes, rem_bytes):
|
||||
config = (ctypes.c_uint8 * 0x20)()
|
||||
self._keep.append(config)
|
||||
addr = ctypes.addressof(config)
|
||||
ctypes.c_uint32.from_address(addr + 0x08).value = young_size
|
||||
ctypes.c_int32.from_address(addr + 0x10).value = mark_bytes
|
||||
ctypes.c_int32.from_address(addr + 0x14).value = rem_bytes
|
||||
return addr
|
||||
|
||||
def test_gc_init_allocates_three_arenas_and_arms_young(self):
|
||||
# raw:156-235 with the old-pool config (vm+0xbc) zero, old_pool_init
|
||||
# returns NULL (raw:526) and gc_init allocates young (tag 0x48),
|
||||
# mark-stack (tag 0x47) and remembered-set (tag 0x49) arenas, then arms
|
||||
# the young bump/limit/base fields and records the young size.
|
||||
vm = self._vm()
|
||||
_set32(vm, OFF_OLD_POOL_CFG, 0)
|
||||
config = self._config(young_size=0x800, mark_bytes=0x400,
|
||||
rem_bytes=0x200)
|
||||
result = self.lib.as3vm_gc_init(vm, config, None)
|
||||
self.assertEqual(result, 1)
|
||||
self.assertEqual(self._count("arena_malloc"), 3)
|
||||
self.assertEqual(_u32(vm, OFF_YOUNG_SIZE), 0x800)
|
||||
young_base = _u64(vm, OFF_YOUNG_BASE)
|
||||
self.assertNotEqual(young_base, 0)
|
||||
self.assertEqual(_u64(vm, OFF_YOUNG_BUMP), young_base)
|
||||
self.assertEqual(_u64(vm, OFF_YOUNG_LIMIT), young_base + 0x800)
|
||||
|
||||
def test_gc_init_returns_zero_when_arena_malloc_fails(self):
|
||||
# raw:171-176 a NULL from the first arena allocation (the young space)
|
||||
# aborts init: gc_init returns 0.
|
||||
vm = self._vm()
|
||||
_set32(vm, OFF_OLD_POOL_CFG, 0)
|
||||
config = self._config(young_size=0x800, mark_bytes=0x400,
|
||||
rem_bytes=0x200)
|
||||
self.lib.as3vm_gc_test_set_arena_fail_after(0) # fail call #1
|
||||
result = self.lib.as3vm_gc_init(vm, config, None)
|
||||
self.assertEqual(result, 0)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# clear: finalizer sweep + field reset
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_gc_clear_on_empty_context_finalizes_nothing_and_resets_fields(self):
|
||||
# raw:88-121 with no finalizable blocks (vm[0x1c]==0) and no old pools
|
||||
# (vm[0xd35]==0), clear calls neither as3vm_finalize nor iggy_free_alloc
|
||||
# and zeroes its bookkeeping fields (vm[8], vm[0xb]=vm+0x58, vm[0xe]).
|
||||
vm = self._vm()
|
||||
_set64(vm, OFF_FINALIZE_LIST, 0)
|
||||
_set64(vm, OFF_POOL_LIST, 0)
|
||||
_set64(vm, 0x40, 0xDEAD) # vm[8]
|
||||
_set64(vm, 0x58, 0xBEEF) # vm[0xb]
|
||||
_set64(vm, 0x70, 0xCAFE) # vm[0xe]
|
||||
self.lib.as3vm_gc_clear(vm)
|
||||
self.assertEqual(self._count("finalize"), 0)
|
||||
self.assertEqual(self._count("free_alloc"), 0)
|
||||
self.assertEqual(_u64(vm, 0x40), 0)
|
||||
self.assertEqual(_u64(vm, 0x58), 0)
|
||||
self.assertEqual(_u64(vm, 0x70), 0)
|
||||
|
||||
def test_gc_clear_finalizes_live_tagged_slots(self):
|
||||
# raw:95-107 clear walks each finalizable block's 0x100 slots at
|
||||
# block+0x10; a slot whose low two bits are 0b01 (a live tagged heap
|
||||
# value) is finalized with (slot - 1) and then zeroed.
|
||||
vm = self._vm()
|
||||
block = self._buffer(0x10 + 0x100 * 8)
|
||||
_set64(block, 0, 0) # next = NULL (single block)
|
||||
_set64(block, 0x10, 0x1000001) # slot0: tagged, low2 == 0b01
|
||||
_set64(vm, OFF_FINALIZE_LIST, block)
|
||||
_set64(vm, OFF_POOL_LIST, 0)
|
||||
self.lib.as3vm_gc_clear(vm)
|
||||
self.assertEqual(self._count("finalize"), 1)
|
||||
self.assertEqual(self.lib.as3vm_gc_test_last_finalized_ptr(), 0x1000000)
|
||||
self.assertEqual(_u64(block, 0x10), 0) # slot cleared
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# owned globals
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_owned_globals_exist_and_start_null(self):
|
||||
# nm evidence: as3vm_gc owns exactly two pointer globals -- updated_object
|
||||
# (tenure/promotion, raw:2970) and iggy_debug_entity (sweep, raw:1507).
|
||||
# Both are file-scope void* and therefore zero-initialized.
|
||||
for name in ("updated_object", "iggy_debug_entity"):
|
||||
value = ctypes.c_void_p.in_dll(self.lib, name)
|
||||
self.assertIn(value.value, (None, 0), name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
344
tests/test_reconstructed_as3vm_gc_cross.py
Normal file
344
tests/test_reconstructed_as3vm_gc_cross.py
Normal file
@@ -0,0 +1,344 @@
|
||||
"""Cross-platform contract tests for the reconstructed as3vm_gc module.
|
||||
|
||||
Mirrors tests/test_reconstructed_as3vm_create_cross.py /
|
||||
tests/test_reconstructed_iggy_sound_cross.py (the freshest 4-platform templates
|
||||
in the same AS3/AVM2 VM cluster). RED until as3vm_gc.c / as3vm_gc.h (and their
|
||||
CMake / modules.tsv rows) exist; the retail-evidence assertions run now and
|
||||
prove the nm/strings parsing.
|
||||
|
||||
as3vm_gc is Iggy's AS3 / AVM2 garbage collector: an incremental mark-sweep
|
||||
collector with a young/old (nursery + remembered-set) generational split, a
|
||||
Dijkstra grey-stack write barrier, finalizers, and a family of heap validators.
|
||||
It is a 4-PLATFORM module (durango 3404 / orbis 3578 / ps3 4776 / vita 4514 raw
|
||||
lines; retail source_functions 52 / 37 / 40 / 35 -- these DIFFER per platform
|
||||
because each target inlines a different number of file-static collector helpers).
|
||||
|
||||
How EXPECTED_* were derived (all four .obj under
|
||||
work/iggy-native-decomp/<platform>-objects/as3vm_gc.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 14-name
|
||||
set on durango/orbis/ps3/vita -- the exported GC ABI is
|
||||
invariant even though the internal helper count varies.
|
||||
(The .opd descriptors on ps3 surface each export a
|
||||
second time as 'D'; those normalize to the exported
|
||||
function names and are NOT separate globals -- same
|
||||
handling as as3vm_create / iggy_sound.)
|
||||
|
||||
EXPECTED_GLOBALS = {iggy_debug_entity, updated_object} -- UNLIKE
|
||||
as3vm_create (which owned no data global), as3vm_gc DOES
|
||||
own exactly two externally named pointer globals, present
|
||||
on ALL FOUR objects:
|
||||
- updated_object -- set by update_old_object
|
||||
(tenure/promotion path, raw:2970) to the tagged node
|
||||
currently being promoted into the old generation.
|
||||
- iggy_debug_entity -- set by the sweeper (raw:1507,
|
||||
1635, 1643) to the last detached/swept block.
|
||||
Both are pointer-sized: nm reports them as Common ('C',
|
||||
size 8) on durango/orbis, Common ('C', size 4) on vita,
|
||||
and BSS ('B') on ps3 -- i.e. one machine pointer each
|
||||
(8 bytes on the 64-bit targets, 4 on the 32-bit ones).
|
||||
In the reconstruction they are plain file-scope
|
||||
`void *` definitions, which clang (-fno-common default)
|
||||
lowers to extern BSS ('B') on every target; the names
|
||||
filter below accepts T/D/B/C so both spellings match.
|
||||
|
||||
EXPECTED_IMPORTS = the genuine non-scaffolding references present on ALL
|
||||
FOUR retail objects (INVARIANT -- see the discrepancy
|
||||
note below):
|
||||
- interpreter / class collaborators (as3vm_interp /
|
||||
as3vm_class, not yet reconstructed -- expected
|
||||
unbuilt): as3vm_finalize,
|
||||
as3vm_interpreter_notice_gc,
|
||||
as3vm_interpreter_request_collection,
|
||||
as3vm_needs_cleaning
|
||||
- null/undefined sentinel DATA symbol (as3vm_gc's own
|
||||
forward-declared extern, unbuilt): as3vm_nullundef_storage
|
||||
- allocation core (iggy_allocation, satisfied):
|
||||
iggy_arena_malloc, iggy_free_alloc,
|
||||
iggy_malloc_CANFAIL_fileline
|
||||
- swf_play / display collaborators (unbuilt --
|
||||
expected): iggy_clear_movie_dirty, iggy_destroy_sprite,
|
||||
iggy_mark_movie_dirty
|
||||
- telemetry DATA pointers (referenced by address;
|
||||
dot-UNprefixed on ps3 like the other address-taken
|
||||
symbols): iggy_tm_internal, iggy_tm_normal
|
||||
- timing (win32_rrTime, satisfied): rrGetTime,
|
||||
rrTimeToMicros
|
||||
- libc: longjmp (OOM/abort escape), qsort (old-pool
|
||||
sort), memcpy, memset
|
||||
This real-Iggy/rr/libc set is INVARIANT across the four
|
||||
platforms.
|
||||
|
||||
PLATFORM_IMPORTS = EMPTY on every platform. DISCREPANCY-WITH-EXPECTATION,
|
||||
following the evidence: the brief anticipated per-platform
|
||||
libc differences (qsort / longjmp / memset inlining), but
|
||||
retail `llvm-nm --undefined-only` shows memcpy, memset,
|
||||
longjmp AND qsort present on ALL FOUR objects with no
|
||||
per-target divergence. Every remaining per-platform
|
||||
difference is pure toolchain scaffolding, captured in
|
||||
RUNTIME_ALLOWLISTS -- so the genuine import set is uniform
|
||||
and PLATFORM_IMPORTS carries no members. The structure is
|
||||
retained (empty sets) to match the sibling templates and
|
||||
to give the impl phase a place to record any lowering
|
||||
divergence it actually observes.
|
||||
|
||||
RUNTIME_ALLOWLISTS = pure toolchain scaffolding residual after subtracting
|
||||
the invariant import set, verified exact against retail
|
||||
`llvm-nm --undefined-only`:
|
||||
durango: __GSHandlerCheck, __chkstk,
|
||||
__security_check_cookie, __security_cookie,
|
||||
_fltused
|
||||
orbis: _GLOBAL_OFFSET_TABLE_
|
||||
ps3: {} (empty)
|
||||
vita: __aeabi_unwind_cpp_pr0, __stack_chk_fail,
|
||||
__stack_chk_guard
|
||||
(as3vm_gc pulls only __aeabi_unwind_cpp_pr0 on vita --
|
||||
NOT the pr1 personality iggy_sound also needed; follow
|
||||
the evidence.)
|
||||
|
||||
PROVENANCE = the canonical Windows source path
|
||||
c:\\projects\\g\\src\\flash\\as3vm_gc.c is embedded in
|
||||
EVERY one of the four retail objects (durango carries it
|
||||
alongside the build-temp .obj path; ps3/vita carry it
|
||||
alongside a bare 'as3vm_gc.c'). Because the canonical
|
||||
string is uniformly present, the retail-obj check asserts
|
||||
it uniformly, like iggy_sound -- not the per-platform
|
||||
split as3vm_create needed.
|
||||
|
||||
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_gc.c"
|
||||
HEADER = RECONSTRUCTED / "as3vm_gc.h"
|
||||
LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm")
|
||||
VARIANTS = {
|
||||
"durango": ("clang", "x86_64-pc-windows-msvc"),
|
||||
"orbis": ("clang", "x86_64-unknown-freebsd"),
|
||||
"vita": ("clang", "armv7a-unknown-linux-gnueabihf"),
|
||||
}
|
||||
PS3_CLANG = Path("/opt/homebrew/opt/llvm/bin/clang")
|
||||
if PS3_CLANG.exists():
|
||||
VARIANTS["ps3"] = (str(PS3_CLANG), "powerpc-unknown-linux-gnu")
|
||||
|
||||
# Invariant 14-name exported GC ABI (leading '.' stripped on ps3).
|
||||
EXPECTED_FUNCTIONS = {
|
||||
"as3vm_gc_alloc",
|
||||
"as3vm_gc_alloc_remembered",
|
||||
"as3vm_gc_clear",
|
||||
"as3vm_gc_collect",
|
||||
"as3vm_gc_collect_exact",
|
||||
"as3vm_gc_init",
|
||||
"as3vm_gc_step",
|
||||
"as3vm_old_block_free",
|
||||
"as3vm_validate_memory",
|
||||
"as3vm_validate_mid_sweep",
|
||||
"as3vm_validate_old_heap",
|
||||
"as3vm_validate_post_sweep",
|
||||
"as3vm_write_barrier_slowpath",
|
||||
"validate_onstage_movies",
|
||||
}
|
||||
# as3vm_gc owns exactly two externally named pointer globals (present on all
|
||||
# four objects); the debug/dirty tracking state of the collector.
|
||||
EXPECTED_GLOBALS = {
|
||||
"iggy_debug_entity",
|
||||
"updated_object",
|
||||
}
|
||||
|
||||
# Genuine non-scaffolding imports present on all four retail objects (invariant).
|
||||
EXPECTED_IMPORTS = {
|
||||
# interpreter / class collaborators (as3vm_interp / as3vm_class -- unbuilt).
|
||||
"as3vm_finalize",
|
||||
"as3vm_interpreter_notice_gc",
|
||||
"as3vm_interpreter_request_collection",
|
||||
"as3vm_needs_cleaning",
|
||||
# null/undefined sentinel DATA symbol (forward extern -- unbuilt).
|
||||
"as3vm_nullundef_storage",
|
||||
# allocation core (iggy_allocation -- satisfied).
|
||||
"iggy_arena_malloc",
|
||||
"iggy_free_alloc",
|
||||
"iggy_malloc_CANFAIL_fileline",
|
||||
# swf_play / display collaborators (unbuilt).
|
||||
"iggy_clear_movie_dirty",
|
||||
"iggy_destroy_sprite",
|
||||
"iggy_mark_movie_dirty",
|
||||
# telemetry DATA pointers (referenced by address).
|
||||
"iggy_tm_internal",
|
||||
"iggy_tm_normal",
|
||||
# timing (win32_rrTime -- satisfied).
|
||||
"rrGetTime",
|
||||
"rrTimeToMicros",
|
||||
# libc the reconstruction lowers on every target.
|
||||
"longjmp",
|
||||
"qsort",
|
||||
"memcpy",
|
||||
"memset",
|
||||
}
|
||||
# The genuine import set is UNIFORM across the four targets (memcpy/memset/
|
||||
# longjmp/qsort all appear on every object); no per-platform libc lowering
|
||||
# divergence survives. Empty sets retained to match the sibling templates.
|
||||
PLATFORM_IMPORTS = {
|
||||
"durango": set(),
|
||||
"orbis": set(),
|
||||
"ps3": set(),
|
||||
"vita": set(),
|
||||
}
|
||||
RUNTIME_ALLOWLISTS = {
|
||||
"durango": {"__GSHandlerCheck", "__chkstk", "__security_check_cookie",
|
||||
"__security_cookie", "_fltused"},
|
||||
"orbis": {"_GLOBAL_OFFSET_TABLE_"},
|
||||
"ps3": set(),
|
||||
"vita": {"__aeabi_unwind_cpp_pr0", "__stack_chk_fail", "__stack_chk_guard"},
|
||||
}
|
||||
# The canonical Windows source path is embedded in every one of the four retail
|
||||
# objects, so assert it uniformly (like iggy_sound).
|
||||
CANONICAL_PROVENANCE = r"c:\projects\g\src\flash\as3vm_gc.c"
|
||||
|
||||
|
||||
def normalized_symbol(name):
|
||||
name = name.removeprefix(".")
|
||||
name = name.removeprefix("__imp_")
|
||||
if name == "_setjmp":
|
||||
return "setjmp"
|
||||
return name
|
||||
|
||||
|
||||
class ReconstructedAs3vmGcCrossTests(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 (52/37/40/35) while the exports stay 14.
|
||||
expected = {
|
||||
"durango": (52, 3404), "orbis": (37, 3578),
|
||||
"ps3": (40, 4776), "vita": (35, 4514),
|
||||
}
|
||||
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_gc\t"))
|
||||
self.assertEqual(int(row.split("\t")[2]), functions, platform)
|
||||
raw = ROOT / f"iggy-decompiled/raw/{platform}/as3vm_gc.c"
|
||||
self.assertEqual(
|
||||
len(raw.read_text(encoding="utf-8").splitlines()),
|
||||
lines, platform)
|
||||
obj = ROOT / (
|
||||
f"work/iggy-native-decomp/{platform}-objects/as3vm_gc.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(CANONICAL_PROVENANCE, strings.stdout, platform)
|
||||
|
||||
if SOURCE.exists():
|
||||
cmake = (RECONSTRUCTED / "CMakeLists.txt").read_text(
|
||||
encoding="utf-8")
|
||||
self.assertIn(
|
||||
"iggy_reconstructed_as3vm_gc_${platform}", cmake)
|
||||
self.assertIn(
|
||||
"add_library(${target} STATIC as3vm_gc.c)", cmake)
|
||||
self.assertIn(
|
||||
"IGGY_RECONSTRUCT_AS3VM_GC_${platform_upper}", cmake)
|
||||
inventory = (RECONSTRUCTED / "modules.tsv").read_text(
|
||||
encoding="utf-8")
|
||||
# Exports are invariant (14) but retail source_functions vary
|
||||
# (52/37/40/35), so the inventory uses per-platform rows exactly
|
||||
# like iggy_sound (NOT a single combined as3vm_create-style row).
|
||||
for platform, functions in (
|
||||
("durango", 52), ("orbis", 37),
|
||||
("ps3", 40), ("vita", 35)):
|
||||
self.assertIn(
|
||||
f"as3vm_gc\t{functions}\t14\t{platform}\tcomplete",
|
||||
inventory)
|
||||
|
||||
def test_exact_exports_globals_normalized_imports_and_c_cpp_abi(self):
|
||||
self.assertTrue(SOURCE.exists(), f"missing {SOURCE}")
|
||||
self.assertTrue(HEADER.exists(), f"missing {HEADER}")
|
||||
self.assertIn(r"c:\\projects\\g\\src\\flash\\as3vm_gc.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_gc.h"\n'
|
||||
'typedef void (*GenericFn)(void);\n'
|
||||
'static GenericFn gc_abis[] = {\n' + function_pointers + '};\n'
|
||||
'int use_abis(void) {\n'
|
||||
' for (unsigned i = 0; '
|
||||
'i < sizeof(gc_abis) / sizeof(gc_abis[0]); ++i) {\n'
|
||||
' if (!gc_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_gc_{platform}.o"
|
||||
build = subprocess.run([
|
||||
compiler, "-target", target, "-ffreestanding",
|
||||
"-fno-builtin",
|
||||
"-c", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
|
||||
*extra,
|
||||
f"-DIGGY_RECONSTRUCT_AS3VM_GC_{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_gc_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_GC_{platform.upper()}=1",
|
||||
"-I", str(RECONSTRUCTED), str(probe),
|
||||
], capture_output=True, text=True)
|
||||
self.assertEqual(syntax.returncode, 0,
|
||||
(platform, language, syntax.stderr))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user