core-legacy/sourcehook/sh_memory.h
changeset 627 607f6a134737
parent 545 07e5df11f9fc
child 629 1a819a61c3d1
     1.1 --- a/core-legacy/sourcehook/sh_memory.h	Sun Dec 07 23:50:23 2008 -0600
     1.2 +++ b/core-legacy/sourcehook/sh_memory.h	Mon Dec 22 23:38:06 2008 -0500
     1.3 @@ -16,12 +16,12 @@
     1.4  //  Unprotect now sets to readwrite
     1.5  //  The vtable doesn't need to be executable anyway
     1.6  
     1.7 -# if	/********/ defined _WIN32
     1.8 +# if SH_XP == SH_XP_WINAPI
     1.9  #		include <windows.h>
    1.10  #		define SH_MEM_READ 1
    1.11  #		define SH_MEM_WRITE 2
    1.12  #		define SH_MEM_EXEC 4
    1.13 -# elif /******/ defined __linux__
    1.14 +# elif SH_XP == SH_XP_POSIX
    1.15  #		include <sys/mman.h>
    1.16  #		include <stdio.h>
    1.17  #		include <signal.h>
    1.18 @@ -48,7 +48,7 @@
    1.19  {
    1.20  	inline bool SetMemAccess(void *addr, size_t len, int access)
    1.21  	{
    1.22 -# ifdef __linux__
    1.23 +# if SH_XP == SH_XP_POSIX
    1.24  		return mprotect(SH_LALIGN(addr), len + SH_LALDIF(addr), access)==0 ? true : false;
    1.25  # else
    1.26  		DWORD tmp;
    1.27 @@ -69,17 +69,25 @@
    1.28  # endif
    1.29  	}
    1.30  
    1.31 -#ifdef __linux__
    1.32 +#if SH_XP == SH_XP_POSIX
    1.33  	namespace
    1.34  	{
    1.35  		bool g_BadReadCalled;
    1.36  		jmp_buf g_BadReadJmpBuf;
    1.37  
    1.38 +# if SH_SYS == SH_SYS_LINUX
    1.39  		static void BadReadHandler(int sig)
    1.40  		{
    1.41  			if (g_BadReadCalled)
    1.42  				longjmp(g_BadReadJmpBuf, 1);
    1.43  		}
    1.44 +# elif SH_SYS == SH_SYS_APPLE
    1.45 +		static void BadReadHandler(int signal, siginfo_t* my_siginfo, void* my_context)	
    1.46 +		{
    1.47 +			if (g_BadReadCalled)
    1.48 +				longjmp(g_BadReadJmpBuf, 1);
    1.49 +		}
    1.50 +# endif
    1.51  	}
    1.52  #endif
    1.53  
    1.54 @@ -93,7 +101,7 @@
    1.55  	{
    1.56  		bool ModuleInMemory(char *addr, size_t len)
    1.57  		{
    1.58 -#ifdef __linux__
    1.59 +#if SH_SYS == SH_SYS_LINUX
    1.60  			// On linux, first check /proc/self/maps
    1.61  			long lower = reinterpret_cast<long>(addr);
    1.62  			long upper = lower + len;
    1.63 @@ -175,7 +183,29 @@
    1.64  			signal(SIGSEGV, prevHandler);
    1.65  
    1.66  			return false;
    1.67 -#else
    1.68 +#elif SH_SYS == SH_SYS_APPLE
    1.69 +			struct sigaction sa, osa;
    1.70 +			sa.sa_sigaction = BadReadHandler;
    1.71 +			sa.sa_flags = SA_SIGINFO | SA_RESTART;
    1.72 +
    1.73 +			g_BadReadCalled = true;
    1.74 +
    1.75 +			if (setjmp(g_BadReadJmpBuf))
    1.76 +				return false;
    1.77 +
    1.78 +			if (sigaction(SIGBUS, &sa, &osa) == -1)
    1.79 +				return false;
    1.80 +
    1.81 +			volatile const char *p = reinterpret_cast<const char *>(addr);
    1.82 +			char dummy;
    1.83 +
    1.84 +			for (size_t i = 0; i < len; i++)
    1.85 +				dummy = p[i];
    1.86 +
    1.87 +			g_BadReadCalled = false;
    1.88 +
    1.89 +            return true;
    1.90 +#elif SH_SYS == SH_SYS_WINAPI
    1.91  			// On Win32, simply use IsBadReadPtr
    1.92  			return !IsBadReadPtr(addr, len);
    1.93  #endif