Print OS errors if mprotect fails

This helped catch a bug in DHooks before, so it's worth adding it in here as well.
This commit is contained in:
Peace-Maker 2021-11-02 16:46:12 +01:00 committed by Your Name
parent e280d33a28
commit d16f45c4b6

View File

@ -39,6 +39,8 @@
#include <amtl/am-bits.h> #include <amtl/am-bits.h>
#include <jit/x86/x86_macros.h> #include <jit/x86/x86_macros.h>
#include <amtl/os/am-system-errors.h>
#include <cstdio>
struct patch_t struct patch_t
{ {
@ -53,14 +55,21 @@ struct patch_t
inline void ProtectMemory(void *addr, int length, int prot) inline void ProtectMemory(void *addr, int length, int prot)
{ {
char error[256];
#if defined PLATFORM_POSIX #if defined PLATFORM_POSIX
long pageSize = sysconf(_SC_PAGESIZE); long pageSize = sysconf(_SC_PAGESIZE);
void *startPage = ke::AlignedBase(addr, pageSize); void *startPage = ke::AlignedBase(addr, pageSize);
void *endPage = ke::AlignedBase((void *)((intptr_t)addr + length), pageSize); void *endPage = ke::AlignedBase((void *)((intptr_t)addr + length), pageSize);
mprotect(startPage, ((intptr_t)endPage - (intptr_t)startPage) + pageSize, prot); if (mprotect(startPage, ((intptr_t)endPage - (intptr_t)startPage) + pageSize, prot) == -1) {
ke::FormatSystemError(error, sizeof(error));
fprintf(stderr, "mprotect: %s\n", error);
}
#elif defined PLATFORM_WINDOWS #elif defined PLATFORM_WINDOWS
DWORD old_prot; DWORD old_prot;
VirtualProtect(addr, length, prot, &old_prot); if (!VirtualProtect(addr, length, prot, &old_prot)) {
ke::FormatSystemError(error, sizeof(error));
fprintf(stderr, "VirtualProtect: %s\n", error);
}
#endif #endif
} }