From 5c5c6d1dd048d062f90155c8415ebd778631f5fe Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 25 Nov 2026 21:06:27 +0300 Subject: [PATCH] With a mod plugin on, read-only module data stays writable on the host, so Zonai Fieldworks can swap main's RELRO entries when a member is switched on in play (#1451) QueryMemory still reports the permission the game asked for. Material Columns swaps 11 dispatch slots in main's RELRO from its own code at switch time; on the platforms the mod is made for that write goes through, here it faulted. Co-Authored-By: Claude Opus 5.5 --- runtime/host.h | 2 ++ runtime/memory.c | 14 +++++++++++--- runtime/plugin_init.c | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/runtime/host.h b/runtime/host.h index ef721106..7c7b4ceb 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -148,6 +148,8 @@ void mem_init(void); // reserve every regi const RtGuestLayout *mem_layout(void); uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr); // 0x01 uint32_t mem_set_permission(uint64_t a, uint64_t n, uint32_t perm); // 0x02 +extern int mem_plugin_writes; // plugin_init.c plugins_start: a mod plugin is on; read-only module data stays writable + // on the host (mem_set_permission) uint32_t mem_set_attribute(uint64_t a, uint64_t n, uint32_t mask, uint32_t at); // 0x03 uint32_t mem_map(uint64_t dst, uint64_t src, uint64_t n); // 0x04 uint32_t mem_unmap(uint64_t dst, uint64_t src, uint64_t n); // 0x05 diff --git a/runtime/memory.c b/runtime/memory.c index 12cc2c3d..82562a34 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -500,6 +500,8 @@ static int module_data(uint64_t a, uint64_t n) { return off >= m->data_off && n <= m->size - off ? i : -1; } +int mem_plugin_writes; + uint32_t mem_set_permission(uint64_t a, uint64_t n, uint32_t perm) { if (a & (PAGE - 1)) return R_INVALID_ADDRESS; if (!n || (n & (PAGE - 1))) return R_INVALID_SIZE; @@ -515,10 +517,16 @@ uint32_t mem_set_permission(uint64_t a, uint64_t n, uint32_t perm) { return R_INVALID_STATE; } memset(t, (int)perm, n / PAGE); - protect(a, n, perm == PERM_RW ? PLAT_PROT_RW : perm == PERM_R ? PLAT_PROT_READ : PLAT_PROT_NONE, "module data"); + // With a mod plugin on, a read-only run (the RELRO the SDK seals in each module's DT_INIT, after the plugins' + // inits) stays writable on the host while QueryMemory reports what the game asked. Zonai Fieldworks swaps entries + // of main's RELRO tables from its own code when a member is switched on in play (#1451), writing main's address + // directly; on the platforms the mod is made for that write goes through, here it faulted. + int keep_rw = perm == PERM_R && mem_plugin_writes; + protect(a, n, perm == PERM_RW || keep_rw ? PLAT_PROT_RW : perm == PERM_R ? PLAT_PROT_READ : PLAT_PROT_NONE, + "module data"); plat_mutex_unlock(&lock); - rt_log("%s data permission +%#llx size %#llx perm %u", m->name, (unsigned long long)(a - m->base), - (unsigned long long)n, perm); + rt_log("%s data permission +%#llx size %#llx perm %u%s", m->name, (unsigned long long)(a - m->base), + (unsigned long long)n, perm, keep_rw ? " (kept writable on the host: a mod plugin is on)" : ""); return R_OK; } diff --git a/runtime/plugin_init.c b/runtime/plugin_init.c index 7554da83..af7e8778 100644 --- a/runtime/plugin_init.c +++ b/runtime/plugin_init.c @@ -708,6 +708,7 @@ int plugins_start(Cpu *c, const RtLoaded *sdk) { for (int i = 0; i < naliases; i++) live += aliases[i].live; rt_log("plugins: %d of %d plugins on in %llu ms (%d process-memory maps, %d kept)", on, nrun, (unsigned long long)(plat_now_ms() - t0), naliases, live); + mem_plugin_writes = on > 0; // before main's DT_INIT seals its RELRO (memory.c mem_set_permission) if (on && cfg_bool("TOTK_PLUGIN_TRACE")) trace_entries(); return 1; #else