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 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-25 21:06:27 +03:00
1 parent d4794238d4
commit 5c5c6d1dd0
3 files changed
+14 -3

No files matched your search

+2
View File
@@ -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
+11 -3
View File
@@ -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;
}
+1
View File
@@ -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