core/sourcehook/sh_memory.h
changeset 539 db914129ae01
parent 523 6f1630519b81
child 633 153e2d943cc3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/sourcehook/sh_memory.h	Fri Nov 14 04:04:02 2008 -0600
     1.3 @@ -0,0 +1,187 @@
     1.4 +/* ======== SourceHook ========
     1.5 +* Copyright (C) 2004-2008 Metamod:Source Development Team
     1.6 +* No warranties of any kind
     1.7 +*
     1.8 +* License: zlib/libpng
     1.9 +*
    1.10 +* Author(s): Pavol "PM OnoTo" Marko, Scott "Damaged Soul" Ehlert
    1.11 +* Contributors: lancevorgin, XAD, theqizmo
    1.12 +* ============================
    1.13 +*/
    1.14 +
    1.15 +#ifndef __SHINT_MEMORY_H__
    1.16 +#define __SHINT_MEMORY_H__
    1.17 +
    1.18 +// Feb 17 / 2005:
    1.19 +//  Unprotect now sets to readwrite
    1.20 +//  The vtable doesn't need to be executable anyway
    1.21 +
    1.22 +# if	/********/ defined _WIN32
    1.23 +#		include <windows.h>
    1.24 +#		define SH_MEM_READ 1
    1.25 +#		define SH_MEM_WRITE 2
    1.26 +#		define SH_MEM_EXEC 4
    1.27 +# elif /******/ defined __linux__
    1.28 +#		include <sys/mman.h>
    1.29 +#		include <stdio.h>
    1.30 +#		include <signal.h>
    1.31 +#		include <setjmp.h>
    1.32 +// http://www.die.net/doc/linux/man/man2/mprotect.2.html
    1.33 +#		include <limits.h>
    1.34 +#		ifndef PAGESIZE
    1.35 +#			define PAGESIZE 4096
    1.36 +#		endif
    1.37 +#		define SH_MEM_READ PROT_READ
    1.38 +#		define SH_MEM_WRITE PROT_WRITE
    1.39 +#		define SH_MEM_EXEC PROT_EXEC
    1.40 +
    1.41 +// We need to align addr down to pagesize on linux
    1.42 +// We assume PAGESIZE is a power of two
    1.43 +#		define SH_LALIGN(x) (void*)((intptr_t)(x) & ~(PAGESIZE-1))
    1.44 +#		define SH_LALDIF(x) ((intptr_t)(x) % PAGESIZE)
    1.45 +# else
    1.46 +#		error Unsupported OS/Compiler
    1.47 +# endif
    1.48 +
    1.49 +#include "sh_list.h"
    1.50 +
    1.51 +namespace SourceHook
    1.52 +{
    1.53 +	inline bool SetMemAccess(void *addr, size_t len, int access)
    1.54 +	{
    1.55 +# ifdef __linux__
    1.56 +		return mprotect(SH_LALIGN(addr), len + SH_LALDIF(addr), access)==0 ? true : false;
    1.57 +# else
    1.58 +		DWORD tmp;
    1.59 +		DWORD prot;
    1.60 +		switch (access)
    1.61 +		{
    1.62 +		case SH_MEM_READ:
    1.63 +			prot = PAGE_READONLY; break;
    1.64 +		case SH_MEM_READ | SH_MEM_WRITE:
    1.65 +			prot = PAGE_READWRITE; break;
    1.66 +		case SH_MEM_READ | SH_MEM_EXEC:
    1.67 +			prot = PAGE_EXECUTE_READ; break;
    1.68 +		default:
    1.69 +		case SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC:
    1.70 +			prot = PAGE_EXECUTE_READWRITE; break;
    1.71 +		}
    1.72 +		return VirtualProtect(addr, len, prot, &tmp) ? true : false;
    1.73 +# endif
    1.74 +	}
    1.75 +
    1.76 +#ifdef __linux__
    1.77 +	namespace
    1.78 +	{
    1.79 +		bool g_BadReadCalled;
    1.80 +		jmp_buf g_BadReadJmpBuf;
    1.81 +
    1.82 +		static void BadReadHandler(int sig)
    1.83 +		{
    1.84 +			if (g_BadReadCalled)
    1.85 +				longjmp(g_BadReadJmpBuf, 1);
    1.86 +		}
    1.87 +	}
    1.88 +#endif
    1.89 +
    1.90 +	/**
    1.91 +	*	@brief Checks whether the specified memory region is (still) accessible
    1.92 +	*
    1.93 +	*	@param addr The lower boundary
    1.94 +	*	@param len Length of the region to be checked
    1.95 +	*/
    1.96 +	namespace
    1.97 +	{
    1.98 +		bool ModuleInMemory(char *addr, size_t len)
    1.99 +		{
   1.100 +#ifdef __linux__
   1.101 +			// On linux, first check /proc/self/maps
   1.102 +			long lower = reinterpret_cast<long>(addr);
   1.103 +			long upper = lower + len;
   1.104 +
   1.105 +			FILE *pF = fopen("/proc/self/maps", "r");
   1.106 +			if (pF)
   1.107 +			{
   1.108 +				// Linux /proc/self/maps -> parse
   1.109 +				// Format:
   1.110 +				// lower    upper    prot     stuff                 path
   1.111 +				// 08048000-0804c000 r-xp 00000000 03:03 1010107    /bin/cat
   1.112 +				long rlower, rupper;
   1.113 +				while (fscanf(pF, "%lx-%lx", &rlower, &rupper) != EOF)
   1.114 +				{
   1.115 +					// Check whether we're IN THERE!
   1.116 +					if (lower >= rlower && upper <= rupper)
   1.117 +					{
   1.118 +						fclose(pF);
   1.119 +						return true;
   1.120 +					}
   1.121 +					// Read to end of line
   1.122 +					int c;
   1.123 +					while ((c = fgetc(pF)) != '\n')
   1.124 +					{
   1.125 +						if (c == EOF)
   1.126 +							break;
   1.127 +					}
   1.128 +					if (c == EOF)
   1.129 +						break;
   1.130 +				}
   1.131 +				fclose(pF);
   1.132 +				return false;
   1.133 +			}
   1.134 +			pF = fopen("/proc/curproc/map", "r");
   1.135 +			if (pF)
   1.136 +			{
   1.137 +				// FreeBSD /proc/curproc/map -> parse
   1.138 +				// 0x804800 0x805500 13 15 0xc6e18960 r-x 21 0x0 COW NC vnode
   1.139 +				long rlower, rupper;
   1.140 +				while (fscanf(pF, "0x%lx 0x%lx", &rlower, &rupper) != EOF)
   1.141 +				{
   1.142 +					// Check whether we're IN THERE!
   1.143 +					if (lower >= rlower && upper <= rupper)
   1.144 +					{
   1.145 +						fclose(pF);
   1.146 +						return true;
   1.147 +					}
   1.148 +					// Read to end of line
   1.149 +					int c;
   1.150 +					while ((c = fgetc(pF)) != '\n')
   1.151 +					{
   1.152 +						if (c == EOF)
   1.153 +							break;
   1.154 +					}
   1.155 +					if (c == EOF)
   1.156 +						break;
   1.157 +				}
   1.158 +				fclose(pF);
   1.159 +				return false;
   1.160 +			}
   1.161 +
   1.162 +			// Both of the above failed, try to actually read and trap sigsegv (implemented by Damaged Soul)
   1.163 +			void(*prevHandler)(int sig);
   1.164 +			g_BadReadCalled = true;
   1.165 +
   1.166 +			if (setjmp(g_BadReadJmpBuf))
   1.167 +				return true;
   1.168 +
   1.169 +			prevHandler = signal(SIGSEGV, BadReadHandler);
   1.170 +
   1.171 +			volatile const char *p = reinterpret_cast<const char*>(addr);
   1.172 +			char dummy;
   1.173 +
   1.174 +			for (size_t i = 0; i < len; i++)
   1.175 +				dummy = p[i];
   1.176 +
   1.177 +			g_BadReadCalled = false;
   1.178 +
   1.179 +			signal(SIGSEGV, prevHandler);
   1.180 +
   1.181 +			return false;
   1.182 +#else
   1.183 +			// On Win32, simply use IsBadReadPtr
   1.184 +			return !IsBadReadPtr(addr, len);
   1.185 +#endif
   1.186 +		}
   1.187 +	}
   1.188 +}
   1.189 +
   1.190 +#endif