diff --git a/runtime/host.h b/runtime/host.h index f5528f18..065256a2 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -167,6 +167,9 @@ void mem_thaw(uint64_t a, uint64_t n); // makes the frozen r void mem_freeze_stats(uint64_t *frozen, uint64_t *freezes, uint64_t *fault_thaws, uint64_t *other_thaws); // regions frozen now; freezes and thaws since the last call void mem_kernel_free(void *p, uint64_t size); // TLR/TLS reuse after thread exit +int mem_guest_readable(uint64_t a, uint64_t n); // [a, a+n) can be read without a fault: inside a loaded + // module or the committed heap answered from the layout, + // anything else by plat_mem_readable (an OS query) // ---- modules (loader.c) typedef struct { diff --git a/runtime/memory.c b/runtime/memory.c index f57117ab..4dcc064c 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -288,6 +288,35 @@ uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr) { return R_OK; } +// The runtime's readers of game objects (menu_cursor.c, mouse_direct.c) check every pointer before they follow it. An +// OS query (plat_mem_readable: VirtualQuery) costs tens of microseconds in this process, and a reader that checks a +// few dozen pointers at a time stalls the game thread for milliseconds. The game's objects live in the committed heap +// and its globals in the modules' images, which are readable for as long as they are mapped: those answer from the +// layout. Any other range asks the OS. The heap only shrinks when the game asks for it (mem_set_heap_size), not while +// it runs. +int mem_guest_readable(uint64_t a, uint64_t n) { + static uint64_t fast, slow; + if (a + n < a) return 0; + uint64_t hs = __atomic_load_n(&heap_size, __ATOMIC_ACQUIRE); + int ok = a >= L.heap.base && a + n <= L.heap.base + hs; + for (int i = 0; !ok && i < ldr_count(); i++) { + const RtLoaded *m = ldr_module(i); + ok = a >= m->base && a + n <= m->base + m->size; + } + if (ok) { + if (__atomic_add_fetch(&fast, 1, __ATOMIC_RELAXED) == 100000) + rt_log("memory: 100000 guest reads checked by the layout, %llu by the OS", + (unsigned long long)__atomic_load_n(&slow, __ATOMIC_RELAXED)); + } else { + ok = plat_mem_readable(a, n); + uint64_t s = __atomic_add_fetch(&slow, 1, __ATOMIC_RELAXED); + if (!(s & (s - 1)) && s >= 1024) // 1024, 2048, 4096...: how often the layout could not answer + rt_log("memory: guest reads checked by the layout %llu, by the OS %llu", + (unsigned long long)__atomic_load_n(&fast, __ATOMIC_RELAXED), (unsigned long long)s); + } + return ok; +} + // ---- module data permissions (0x02, 0x03) and module pages for QueryMemory static int module_index(uint64_t a) { for (int i = 0; i < ldr_count(); i++) { diff --git a/runtime/mouse_direct.c b/runtime/mouse_direct.c index f28e3154..271523ed 100644 --- a/runtime/mouse_direct.c +++ b/runtime/mouse_direct.c @@ -173,7 +173,7 @@ enum { VERB_USE_BOW = 59, VERB_USE_SPECIAL_POWER = 61, VERB_DRAWN_BOW = 205, VER enum { PLAYER_FOUND = 1, PLAYER_ABILITY = 2, PLAYER_BOW = 4 }; static int readable(uint64_t p, uint64_t n) { - return p >= 0x10000 && p < (1ull << 47) && !(p & 7) && plat_mem_readable(p, n); + return p >= 0x10000 && p < (1ull << 47) && !(p & 7) && mem_guest_readable(p, n); } static int32_t guest_s32(uint64_t at) { int32_t v; memcpy(&v, (void *)(uintptr_t)at, sizeof v); return v; } static int game_object(uint64_t p, uint64_t n) { // readable, its vtable in main's image diff --git a/tests/mouse_direct_check.py b/tests/mouse_direct_check.py index 796aae29..c827212e 100644 --- a/tests/mouse_direct_check.py +++ b/tests/mouse_direct_check.py @@ -45,7 +45,7 @@ void plat_mutex_unlock(PlatMutex *m) { (void)m; } void rt_log(const char *fmt, ...) { (void)fmt; } float cam_speed_applied(void) { return 1; } uint64_t input_buttons_last(void) { return 0; } -int plat_mem_readable(uint64_t a, uint64_t n) { (void)a; (void)n; return 0; } // no player: on-foot gains +int mem_guest_readable(uint64_t a, uint64_t n) { (void)a; (void)n; return 0; } // no player: on-foot gains static float scalar(__uint128_t v) { float f; memcpy(&f,&v,4); return f; } int main(void) { assert(hook && activity_hook);