From ee509f6350a25e42aca3c705bd35ec8ac4e962dd Mon Sep 17 00:00:00 2001 From: ptitSeb Date: Mon, 14 Nov 2022 21:06:33 +0100 Subject: Small fixes and improvement to memory protection handling --- src/custommem.c | 62 +++++++++++++++++++++++++++++-------------------- src/include/custommem.h | 1 + src/libtools/signals.c | 11 +++++++-- 3 files changed, 47 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/custommem.c b/src/custommem.c index 0b1b2e98..3aae1b90 100644 --- a/src/custommem.c +++ b/src/custommem.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "box64context.h" #include "elfloader.h" @@ -1111,7 +1112,7 @@ void setProtection(uintptr_t addr, size_t size, uint32_t prot) #endif } if(prot || memprot[i].prot!=memprot_default) { - uintptr_t bstart = ((i<<16)end)?(end&0xffff):0xffff; for (uintptr_t j=bstart; i<=bend; ++i) memprot[i].prot[j] = prot; @@ -1120,6 +1121,18 @@ void setProtection(uintptr_t addr, size_t size, uint32_t prot) pthread_mutex_unlock(&mutex_prot); } +void refreshProtection(uintptr_t addr) +{ + pthread_mutex_lock(&mutex_prot); + uintptr_t idx = (addr>>MEMPROT_SHIFT); + if(memprot[idx>>16].prot!=memprot_default) { + int prot = memprot[idx>>16].prot[idx&0xffff]; + int ret = mprotect((void*)(idx<>16); + const uintptr_t start = i&(MEMPROT_SIZE-1); + const uintptr_t finish = (((i|(MEMPROT_SIZE-1))si_code==2 && ((prot&~PROT_CUSTOM)==5 || (prot&~PROT_CUSTOM)==7)) { - relockMutex(Locks); - return; // that's probably just a multi-task glitch, like seen in terraria + static uintptr_t old_addr = 0; + printf_log(/*LOG_DEBUG*/LOG_INFO, "Strange SIGSEGV with Access error on %p for %p, db=%p, prot=0x%x (old_addr=%p)\n", pc, addr, db, prot, (void*)old_addr); + if(old_addr!=(uintptr_t)addr) { + old_addr = (uintptr_t)addr; + refreshProtection(old_addr); + relockMutex(Locks); + return; // that's probably just a multi-task glitch, like seen in terraria + } + old_addr = 0; } old_code = info->si_code; old_pc = pc; -- cgit 1.4.1