commit 403bc5da6843759e503f31bde4a9801d77461102
parent be4f2c69feab82deb816d2384761e8b06008cc73
Author: anon <anon@example.com>
Date: Mon, 19 Oct 2026 22:40:28 +0000
Bound retained resources and browser asset chunks
Diffstat:
6 files changed, 104 insertions(+), 10 deletions(-)
diff --git a/src/Makefile.linux b/src/Makefile.linux
@@ -193,6 +193,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/tls
./build/linux/threads
./build/linux/resource-atomics
+ ./build/linux/resource-budget
./build/linux/memory-status
./build/linux/system-time
./build/linux/rtti
@@ -312,6 +313,12 @@ build/linux/resource-atomics: tests/resource_atomics.c game/hhr.dll/lifted-0000.
test: build/linux/resource-atomics
+build/linux/resource-budget: tests/resource_budget.c game/ppr.dll/lifted-0000.c runtime/guest_crt.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h runtime/guest_crt.h
+ mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@
+
+test: build/linux/resource-budget
+
build/linux/memory-status: tests/memory_status.c runtime/linux_platform.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h runtime/linux_platform.h runtime/linux_path.c runtime/linux_path.h
mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@
diff --git a/src/Makefile.wasm b/src/Makefile.wasm
@@ -36,6 +36,10 @@ build/wasm/guest-heap.js: tests/guest_heap.c runtime/guest_heap.c runtime/guest_
@mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -o $@
+build/wasm/resource-budget.js: tests/resource_budget.c game/ppr.dll/lifted-0000.c runtime/guest_crt.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h runtime/guest_crt.h
+ @mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@
+
build/wasm/terrain-occlusion.js: tests/terrain_occlusion.c game/bbr.dll/lifted-0064.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h
@mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) -DRECOMP_YIELD_EVERY=16 $(filter %.c,$^) -o $@
diff --git a/src/game/ppr.dll/lifted-0000.c b/src/game/ppr.dll/lifted-0000.c
@@ -4061,7 +4061,8 @@ L_40002705:
L_40002708:
eax = MEM32(eax); /* 0x40002708: mov eax, dword ptr [eax] */
PUSH32(esp, 0); /* 0x4000270A: push 0 */
- MEM32(esi + 0x174) = eax; /* 0x4000270C: mov dword ptr [esi + 0x174], eax */
+ /* Bound inactive resource retention independently of reported host RAM. */
+ MEM32(esi + 0x174) = eax > 64u * 1024 * 1024 ? 64u * 1024 * 1024 : eax;
MEM8(esi + 0x8) = (uint8_t)(1); /* 0x40002712: mov byte ptr [esi + 8], 1 */
RECOMP_ICALL(MEM32(0x400160DC)); /* 0x40002716: call dword ptr [0x400160dc] */
RECOMP_FLAGS_IN();
diff --git a/src/runtime/browser_fetch.js b/src/runtime/browser_fetch.js
@@ -14,7 +14,7 @@ addToLibrary({
// Active reads retain their bytes until copying into Wasm has finished.
function trimChunks() {
for (const [bytes, entry] of cachedChunks) {
- if (cachedBytes <= 64 * 1024 * 1024) break;
+ if (cachedBytes <= 16 * 1024 * 1024) break;
if (entry.file.readers) continue;
delete entry.file.chunks[entry.index];
cachedChunks.delete(bytes);
diff --git a/src/tests/browser_fetch.mjs b/src/tests/browser_fetch.mjs
@@ -250,15 +250,15 @@ async function cachedSmallFileAfterReload() {
await cachedSmallFileAfterReload();
async function leastRecentlyUsedChunksAreFetchedAgain() {
- const bytes = new Uint8Array(65 * 1024 * 1024);
+ const bytes = new Uint8Array(17 * 1024 * 1024);
bytes[0] = 11;
bytes[1048576] = 22;
const {backend, requests, heap} = await fixture(bytes, false, undefined, undefined, 1048576);
- for (let index = 0; index < 64; ++index)
+ for (let index = 0; index < 16; ++index)
assert.equal(await backend.read(1, 0, 1, index * 1048576), 1);
await backend.read(1, 0, 1, 0);
- await backend.read(1, 0, 1, 67108864);
+ await backend.read(1, 0, 1, 16777216);
await backend.read(1, 0, 1, 0);
assert.equal(heap[0], 11);
await backend.read(1, 0, 1, 1048576);
@@ -269,19 +269,19 @@ async function leastRecentlyUsedChunksAreFetchedAgain() {
}
async function readsLargerThanCacheKeepAllBytesUntilCopied() {
- const bytes = new Uint8Array(66 * 1024 * 1024);
+ const bytes = new Uint8Array(18 * 1024 * 1024);
bytes[0] = 11;
- bytes[69206015] = 33;
+ bytes[18874367] = 33;
const {backend, requests, heap} = await fixture(bytes, false, undefined, undefined, 1048576);
const length = await backend.read(1, 0, bytes.length, 0);
assert.equal(heap[0], 11);
- assert.equal(heap[69206015], 33);
+ assert.equal(heap[18874367], 33);
await backend.read(1, 0, 1, 0);
- assert.equal(length, 69206016);
+ assert.equal(length, 18874368);
assert.equal(heap[0], 11);
- assert.deepEqual(requests, ['bytes=0-1048575', 'bytes=1048576-69206015', 'bytes=0-1048575']);
+ assert.deepEqual(requests, ['bytes=0-1048575', 'bytes=1048576-18874367', 'bytes=0-1048575']);
}
await leastRecentlyUsedChunksAreFetchedAgain();
diff --git a/src/tests/resource_budget.c b/src/tests/resource_budget.c
@@ -0,0 +1,82 @@
+#include "recomp_types.h"
+#include "guest_memory.h"
+#include "guest_crt.h"
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+void sub_400026B0(void);
+static uint32_t physical_memory;
+
+static void open_database(void)
+{
+ assert(g_ecx == 0x50000500 && MEM32(g_esp + 4) == 0);
+ g_eax = 17;
+ g_esp += 8;
+}
+
+static void memory_status(void)
+{
+ uint32_t output = MEM32(g_esp + 4);
+ memset((void *)ADDR(output), 0, 32);
+ MEM32(output + 8) = physical_memory;
+ g_esp += 8;
+}
+
+static void current_time(void)
+{
+ assert(!MEM32(g_esp + 4));
+ g_eax = 12345;
+ g_esp += 4;
+}
+
+recomp_func_t recomp_lookup(uint32_t address)
+{
+ switch (address) {
+ case 0x70000100: return open_database;
+ case 0x70000200: return memory_status;
+ case 0x70000300: return current_time;
+ case 0x40014864: return guest_crt_lookup("_ftol");
+ default: assert(0); return NULL;
+ }
+}
+recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; }
+recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
+
+static void open_with_memory(uint32_t total, uint32_t expected)
+{
+ memset((void *)ADDR(0x50000100), 0, 0x300);
+ physical_memory = total;
+ MEM32(0x5000010c) = 0x50000500;
+ MEM32(0x50000500) = 0x50000600;
+ MEM32(0x50000648) = 0x70000100;
+ g_esp = 0x5000ff00;
+ MEM32(g_esp) = RECOMP_RETADDR;
+ g_ecx = 0x50000100;
+ g_ebp = 0x11223344; g_ebx = 0x22334455;
+ g_esi = 0x33445566; g_edi = 0x44556677;
+
+ sub_400026B0();
+
+ assert(MEM32(0x50000274) == expected);
+ assert(g_eax == 17 && g_esp == 0x5000ff04 && g_fp_top == 0);
+ assert(g_ebp == 0x11223344 && g_ebx == 0x22334455);
+ assert(g_esi == 0x33445566 && g_edi == 0x44556677);
+ assert(MEM8(0x50000108) == 1 && MEM32(0x4001d06c) == 12345);
+}
+
+int main(void)
+{
+ assert(guest_map("resource manager data", 0x40000000, 0x20000));
+ assert(guest_map("resource manager fixture", 0x50000000, 0x10000));
+ const double factor = 0.7;
+ memcpy((void *)ADDR(0x40016300), &factor, sizeof(factor));
+ MEM32(0x40016008) = 0x70000200;
+ MEM32(0x400160dc) = 0x70000300;
+ open_with_memory(0x08000000, 0);
+ open_with_memory(0x09600000, 0);
+ open_with_memory(0x09700000, 734003);
+ open_with_memory(0x10000000, 67108864);
+ open_with_memory(0x7fffffff, 67108864);
+ puts("resource budget: low-memory limits, bounded retention, database result and guest ABI passed");
+}