commit 83c2ff90151e15e7eec91c389143a96077e8ede3
parent d0dc00f6948548ac83a407b47ccdac5d490c51e8
Author: anon <anon@example.com>
Date: Wed, 11 Nov 2026 01:21:24 +0000
Cache immutable browser assets for direct guest reads
Diffstat:
6 files changed, 263 insertions(+), 3 deletions(-)
diff --git a/client/Makefile.linux b/client/Makefile.linux
@@ -22,6 +22,12 @@ build/linux/game: $(OBJECTS)
objects: $(OBJECTS)
+build/linux/browser-asset-cache: tests/browser_asset_cache.c runtime/browser_asset_cache.c runtime/browser_asset_cache.h runtime/browser_profile.h
+ @mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -Wl,--wrap=pread -o $@
+
+test: build/linux/browser-asset-cache
+
build/linux/browser-resolution: tests/browser_resolution.c runtime/browser_resolution.c runtime/guest_memory.c runtime/recomp_state.c
@mkdir -p $(@D)
$(CC) $(CPPFLAGS) $(CFLAGS) $^ -lm -o $@
@@ -206,6 +212,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/guest-scanf
./build/linux/network-startup
./build/linux/files
+ ./build/linux/browser-asset-cache
./build/linux/os-version
./build/linux/guest-format
./build/linux/event
diff --git a/client/Makefile.wasm b/client/Makefile.wasm
@@ -3,7 +3,7 @@ CPPFLAGS := -Iruntime -Igame -Idata -DRECOMP_PORTABLE
# Guest exception landing pads use Wasm longjmp support.
CFLAGS := -Oz -flto=thin -fno-strict-aliasing -pthread -sSUPPORT_LONGJMP=wasm
PORT_FLAGS := -sUSE_SDL=2 -sUSE_FREETYPE=1 -sUSE_ZLIB=1
-RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_audio.c runtime/linux_music.c runtime/browser_audio.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/browser_network.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c runtime/linux_path.c runtime/browser_host.c runtime/browser_loop.c runtime/browser_resolution.c runtime/browser_profile.c
+RUNTIME_SOURCES := runtime/linux_host.c runtime/linux_imports.c runtime/linux_gdi.c runtime/linux_audio.c runtime/linux_music.c runtime/browser_audio.c runtime/linux_window.c runtime/linux_graphics.c runtime/gl_renderer.c runtime/linux_platform.c runtime/linux_diagnostics.c runtime/linux_sync.c runtime/linux_file.c runtime/linux_tls.c runtime/linux_thread.c runtime/linux_snapshot.c runtime/browser_network.c runtime/browser_asset_cache.c runtime/guest_memory.c runtime/guest_heap.c runtime/guest_crt.c runtime/guest_stdio.c runtime/guest_exception.c runtime/guest_rtti.c runtime/guest_format.c runtime/guest_msvcp.c runtime/recomp_state.c runtime/eh32.c runtime/linux_path.c runtime/browser_host.c runtime/browser_loop.c runtime/browser_resolution.c runtime/browser_profile.c
GAME_SOURCES := $(wildcard game/*/*.c) game/dispatch.c
RUNTIME_SOURCES += runtime/browser_testing.c
DATA_SOURCES := $(wildcard data/*.c)
diff --git a/client/runtime/browser_asset_cache.c b/client/runtime/browser_asset_cache.c
@@ -0,0 +1,85 @@
+#include "browser_asset_cache.h"
+#include "browser_profile.h"
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static struct asset_chunk {
+ ino_t inode;
+ off_t offset;
+ size_t size;
+ unsigned char *bytes;
+ struct asset_chunk *next;
+} *chunks;
+static size_t cached_bytes;
+
+ssize_t browser_asset_read_locked(int descriptor, ino_t inode, off_t file_size,
+ void *buffer, size_t length)
+{
+ off_t position = lseek(descriptor, 0, SEEK_CUR);
+ if (position < 0) return -1;
+ if (position >= file_size || !length) return 0;
+ if (length > (uint64_t)(file_size - position)) length = file_size - position;
+ size_t copied = 0;
+
+ while (copied < length) {
+ off_t offset = (position + copied) / 1048576 * 1048576;
+ struct asset_chunk **entry = &chunks;
+ while (*entry && ((*entry)->inode != inode || (*entry)->offset != offset))
+ entry = &(*entry)->next;
+ struct asset_chunk *chunk = *entry;
+ double hit = chunk ? ao_profile_begin("asset cache hit") : 0;
+
+ if (chunk) {
+ /* Move resident chunks to the front without contacting the fetch worker. */
+ *entry = chunk->next;
+ } else {
+ size_t size = file_size - offset < 1048576 ? file_size - offset : 1048576;
+ while (cached_bytes + size > 256 * 1024 * 1024) {
+ struct asset_chunk **oldest = &chunks;
+ while ((*oldest)->next) oldest = &(*oldest)->next;
+ cached_bytes -= (*oldest)->size;
+ free((*oldest)->bytes);
+ free(*oldest);
+ *oldest = NULL;
+ }
+
+ /* One worker request fills a complete chunk for subsequent small reads. */
+ chunk = malloc(sizeof(*chunk));
+ if (!chunk) abort();
+ *chunk = (struct asset_chunk){.inode = inode, .offset = offset, .size = size,
+ .bytes = malloc(size)};
+ if (!chunk->bytes) abort();
+ double start = ao_profile_begin("asset cache fill");
+ ssize_t result;
+ do {
+ result = pread(descriptor, chunk->bytes, size, offset);
+ } while (result < 0 && errno == EINTR);
+ char detail[64] = "";
+ if (start) snprintf(detail, sizeof(detail), "resident_bytes=%zu", cached_bytes + (result == (ssize_t)size ? size : 0));
+ ao_profile_end("asset cache fill", start, detail);
+ if (result != (ssize_t)size) {
+ free(chunk->bytes);
+ free(chunk);
+ if (result >= 0) errno = EIO;
+ if (!copied) return -1;
+ break;
+ }
+ cached_bytes += size;
+ }
+ chunk->next = chunks;
+ chunks = chunk;
+
+ size_t within = position + copied - offset;
+ size_t size = chunk->size - within;
+ if (size > length - copied) size = length - copied;
+ memcpy((unsigned char *)buffer + copied, chunk->bytes + within, size);
+ copied += size;
+ if (hit) ao_profile_end("asset cache hit", hit, NULL);
+ }
+ if (lseek(descriptor, position + copied, SEEK_SET) < 0) return -1;
+ return copied;
+}
diff --git a/client/runtime/browser_asset_cache.h b/client/runtime/browser_asset_cache.h
@@ -0,0 +1,11 @@
+#ifndef BROWSER_ASSET_CACHE_H
+#define BROWSER_ASSET_CACHE_H
+
+#include <sys/types.h>
+
+/* Immutable FetchFS files only; callers hold the guest file lock. Inodes remain
+ * alive for the browser session, including when their descriptors are closed. */
+ssize_t browser_asset_read_locked(int descriptor, ino_t inode, off_t file_size,
+ void *buffer, size_t length);
+
+#endif
diff --git a/client/runtime/linux_file.c b/client/runtime/linux_file.c
@@ -3,6 +3,10 @@
#include "linux_path.h"
#include <pthread.h>
#include "linux_file.h"
+#ifdef __EMSCRIPTEN__
+#include "browser_asset_cache.h"
+#include <emscripten/wasmfs.h>
+#endif
#include <errno.h>
#include <fcntl.h>
#include <sys/file.h>
@@ -17,6 +21,7 @@ struct guest_file {
dev_t device;
ino_t inode;
int exclusive;
+ off_t asset_size;
#endif
struct guest_file *next;
};
@@ -120,6 +125,8 @@ static void create_file_locked(void)
handle->device = info.st_dev;
handle->inode = info.st_ino;
handle->exclusive = exclusive;
+ handle->asset_size = wasmfs_get_backend_by_fd(descriptor) ==
+ wasmfs_get_backend_by_path("/remote") ? info.st_size : -1;
#endif
handle->next = handles;
handles = handle;
@@ -161,25 +168,34 @@ static void read_file_locked(void)
if (!handle) return;
if (!output && size) { MEM32(g_fs_base + 0x34) = 998; return; }
- /* Preserve the guest's 512-byte sector alignment despite relocated host memory. */
+ /* Enforce guest sector alignment; only native O_DIRECT needs an aligned buffer. */
void *buffer = output ? (void *)ADDR(output) : NULL;
if (handle->unbuffered && size) {
off_t position = lseek(handle->descriptor, 0, SEEK_CUR);
if (position < 0) { MEM32(g_fs_base + 0x34) = file_error(errno); return; }
if ((output | size | position) & 511) { MEM32(g_fs_base + 0x34) = 87; return; }
+#ifndef __EMSCRIPTEN__
if (posix_memalign(&buffer, 4096, size)) { MEM32(g_fs_base + 0x34) = 8; return; }
+#endif
}
double profile_start = ao_profile_begin("file read");
ssize_t result;
do {
- result = read(handle->descriptor, buffer, size);
+#ifdef __EMSCRIPTEN__
+ if (handle->asset_size >= 0)
+ result = browser_asset_read_locked(handle->descriptor, handle->inode, handle->asset_size, buffer, size);
+ else
+#endif
+ result = read(handle->descriptor, buffer, size);
} while (result < 0 && errno == EINTR);
ao_profile_end("file read", profile_start, NULL);
if (result < 0) MEM32(g_fs_base + 0x34) = file_error(errno);
+#ifndef __EMSCRIPTEN__
if (handle->unbuffered && size) {
if (result > 0) memcpy((void *)ADDR(output), buffer, result);
free(buffer);
}
+#endif
if (result >= 0) { MEM32(count) = result; g_eax = 1; }
}
diff --git a/client/tests/browser_asset_cache.c b/client/tests/browser_asset_cache.c
@@ -0,0 +1,141 @@
+#define _GNU_SOURCE
+#include "browser_asset_cache.h"
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+static unsigned reads;
+static off_t failed_offset = -1;
+ssize_t __real_pread(int descriptor, void *buffer, size_t size, off_t offset);
+ssize_t __wrap_pread(int descriptor, void *buffer, size_t size, off_t offset)
+{
+ ++reads;
+ if (offset == failed_offset) { errno = EIO; return -1; }
+ return __real_pread(descriptor, buffer, size, offset);
+}
+
+static void boundaries_and_positions(int first, int second, ino_t inode, off_t size)
+{
+ unsigned char bytes[16] = {0};
+ assert(lseek(first, 1048574, SEEK_SET) == 1048574);
+ unsigned before = reads;
+
+ assert(browser_asset_read_locked(first, inode, size, bytes, 6) == 6);
+
+ assert(!memcmp(bytes, "ABCDEF", 6));
+ assert(reads == before + 2);
+ assert(lseek(first, 0, SEEK_CUR) == 1048580);
+ assert(lseek(second, 0, SEEK_CUR) == 0);
+
+ // Independent handles share bytes, while retaining their own file positions.
+ before = reads;
+ assert(browser_asset_read_locked(second, inode, size, bytes, 5) == 5);
+ assert(!memcmp(bytes, "hello", 5));
+ assert(reads == before);
+ assert(lseek(second, 0, SEEK_CUR) == 5);
+ assert(lseek(first, 0, SEEK_CUR) == 1048580);
+
+ assert(lseek(first, size - 2, SEEK_SET) == size - 2);
+ assert(browser_asset_read_locked(first, inode, size, bytes, sizeof(bytes)) == 2);
+ assert(!memcmp(bytes, "yz", 2));
+ before = reads;
+ assert(browser_asset_read_locked(first, inode, size, bytes, 1) == 0);
+ assert(browser_asset_read_locked(first, inode, size, NULL, 0) == 0);
+ assert(lseek(first, size + 10, SEEK_SET) == size + 10);
+ assert(browser_asset_read_locked(first, inode, size, bytes, 1) == 0);
+ assert(reads == before);
+}
+
+static void failed_fill_can_be_retried(int descriptor, ino_t inode, off_t size)
+{
+ unsigned char bytes[4] = {9, 9, 9, 9};
+ assert(lseek(descriptor, 2097150, SEEK_SET) == 2097150);
+ failed_offset = 2097152;
+
+ assert(browser_asset_read_locked(descriptor, inode, size, bytes, sizeof(bytes)) == 2);
+
+ assert(bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 9);
+ assert(lseek(descriptor, 0, SEEK_CUR) == 2097152);
+ assert(browser_asset_read_locked(descriptor, inode, size, bytes, 2) == -1);
+ assert(errno == EIO && lseek(descriptor, 0, SEEK_CUR) == 2097152);
+ failed_offset = -1;
+ assert(browser_asset_read_locked(descriptor, inode, size, bytes, 2) == 2);
+ assert(bytes[0] == 0 && bytes[1] == 0);
+}
+
+static void different_files_do_not_share_chunks(int descriptor, ino_t inode, off_t size)
+{
+ FILE *other = tmpfile();
+ assert(other && fwrite("world", 1, 5, other) == 5 && !fflush(other));
+ struct stat info;
+ assert(!fstat(fileno(other), &info) && info.st_ino != inode);
+ assert(lseek(fileno(other), 0, SEEK_SET) == 0);
+ unsigned char bytes[5];
+
+ assert(browser_asset_read_locked(fileno(other), info.st_ino, 5, bytes, 5) == 5);
+ assert(!memcmp(bytes, "world", 5));
+ assert(lseek(descriptor, 0, SEEK_SET) == 0);
+ assert(browser_asset_read_locked(descriptor, inode, size, bytes, 5) == 5);
+
+ assert(!memcmp(bytes, "hello", 5));
+ assert(!fclose(other));
+}
+
+static void startup_chunks_survive_until_budget_eviction(int descriptor, ino_t inode, off_t size)
+{
+ unsigned char byte;
+ for (unsigned index = 0; index < 185; ++index) {
+ assert(lseek(descriptor, index * 1048576, SEEK_SET) == index * 1048576);
+ assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1);
+ }
+ unsigned before = reads;
+
+ // The whole startup set remains resident, well beyond the old JS cache budget.
+ for (unsigned index = 0; index < 185; ++index) {
+ assert(lseek(descriptor, index * 1048576, SEEK_SET) == index * 1048576);
+ assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1);
+ }
+ assert(reads == before);
+
+ // Keep chunk zero recent, then force eviction of the oldest chunks.
+ assert(lseek(descriptor, 0, SEEK_SET) == 0);
+ assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1);
+ for (unsigned index = 185; index <= 256; ++index) {
+ assert(lseek(descriptor, index * 1048576, SEEK_SET) == index * 1048576);
+ assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1);
+ }
+ before = reads;
+ assert(lseek(descriptor, 0, SEEK_SET) == 0);
+ assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1);
+ assert(byte == 'h' && reads == before);
+ assert(lseek(descriptor, 1048576, SEEK_SET) == 1048576);
+ assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1);
+ assert(byte == 'C' && reads == before + 1);
+}
+
+int main(void)
+{
+ char path[] = "/tmp/ao-asset-cache-XXXXXX";
+ int first = mkstemp(path), second = open(path, O_RDONLY);
+ assert(first >= 0 && second >= 0 && !unlink(path));
+ off_t size = 257 * 1048576 + 3;
+ assert(!ftruncate(first, size));
+ assert(pwrite(first, "hello", 5, 0) == 5);
+ assert(pwrite(first, "ABCDEF", 6, 1048574) == 6);
+ assert(pwrite(first, "xyz", 3, size - 3) == 3);
+ struct stat info;
+ assert(!fstat(first, &info));
+
+ boundaries_and_positions(first, second, info.st_ino, size);
+ failed_fill_can_be_retried(first, info.st_ino, size);
+ different_files_do_not_share_chunks(first, info.st_ino, size);
+ startup_chunks_survive_until_budget_eviction(first, info.st_ino, size);
+
+ assert(!close(first) && !close(second));
+ puts("asset cache: direct hits, independent handles, boundaries, EOF, retry and bounded LRU passed");
+}