diff -r b507295429f9 -r 8015336442cb core/sourcehook/sh_memory.h --- a/core/sourcehook/sh_memory.h Sun Nov 22 23:54:55 2009 -0600 +++ b/core/sourcehook/sh_memory.h Tue Nov 24 07:19:57 2009 -0600 @@ -16,12 +16,12 @@ // Unprotect now sets to readwrite // The vtable doesn't need to be executable anyway -# if /********/ defined _WIN32 +# if SH_XP == SH_XP_WINAPI # include # define SH_MEM_READ 1 # define SH_MEM_WRITE 2 # define SH_MEM_EXEC 4 -# elif /******/ defined __linux__ +# elif SH_XP == SH_XP_POSIX # include # include # include @@ -50,9 +50,9 @@ { inline bool SetMemAccess(void *addr, size_t len, int access) { -# ifdef __linux__ +# if SH_XP == SH_XP_POSIX return mprotect(SH_LALIGN(addr), len + SH_LALDIF(addr), access)==0 ? true : false; -# else +# elif SH_XP == SH_XP_WINAPI DWORD tmp; DWORD prot; switch (access) @@ -71,17 +71,25 @@ # endif } -#ifdef __linux__ +#if SH_XP == SH_XP_POSIX namespace { bool g_BadReadCalled; jmp_buf g_BadReadJmpBuf; +# if SH_SYS == SH_SYS_LINUX static void BadReadHandler(int sig) { if (g_BadReadCalled) longjmp(g_BadReadJmpBuf, 1); } +# elif SH_SYS == SH_SYS_APPLE + static void BadReadHandler(int signal, siginfo_t* my_siginfo, void* my_context) + { + if (g_BadReadCalled) + longjmp(g_BadReadJmpBuf, 1); + } +# endif } #endif @@ -95,7 +103,7 @@ { bool ModuleInMemory(char *addr, size_t len) { -#ifdef __linux__ +#if SH_SYS == SH_SYS_LINUX // On linux, first check /proc/self/maps long lower = reinterpret_cast(addr); long upper = lower + len; @@ -177,7 +185,31 @@ signal(SIGSEGV, prevHandler); return false; -#else +#elif SH_SYS == SH_SYS_APPLE + struct sigaction sa, osa; + sa.sa_sigaction = BadReadHandler; + sa.sa_flags = SA_SIGINFO | SA_RESTART; + + g_BadReadCalled = true; + + if (setjmp(g_BadReadJmpBuf)) + return false; + + if (sigaction(SIGBUS, &sa, &osa) == -1) + return false; + + volatile const char *p = reinterpret_cast(addr); + char dummy; + + for (size_t i = 0; i < len; i++) + dummy = p[i]; + + g_BadReadCalled = false; + + sigaction(SIGBUS, &osa, NULL); + + return true; +#elif SH_XP == SH_XP_WINAPI // On Win32, simply use IsBadReadPtr return !IsBadReadPtr(addr, len); #endif