sourcehook/sh_memory.h
author Pavol Marko <pavolmarko@gmail.com>
Fri Nov 02 15:33:51 2007 +0000 (2007-11-02)
changeset 441 b27d2b9167b0
parent 294 0d87bc8606f8
child 435 35296642e147
child 480 d096210677d4
permissions -rw-r--r--
Fixes for GCC compat
     1 /* ======== SourceHook ========
     2 * Copyright (C) 2004-2007 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): Pavol "PM OnoTo" Marko, Scott "Damaged Soul" Ehlert
     8 * Contributors: lancevorgin, XAD, theqizmo
     9 * ============================
    10 */
    11 
    12 #ifndef __SHINT_MEMORY_H__
    13 #define __SHINT_MEMORY_H__
    14 
    15 // Feb 17 / 2005:
    16 //  Unprotect now sets to readwrite
    17 //  The vtable doesn't need to be executable anyway
    18 
    19 # if	/********/ defined _WIN32
    20 #		include <windows.h>
    21 #		define SH_MEM_READ 1
    22 #		define SH_MEM_WRITE 2
    23 #		define SH_MEM_EXEC 4
    24 # elif /******/ defined __linux__
    25 #		include <sys/mman.h>
    26 #		include <stdio.h>
    27 #		include <signal.h>
    28 #		include <setjmp.h>
    29 // http://www.die.net/doc/linux/man/man2/mprotect.2.html
    30 #		include <limits.h>
    31 #		ifndef PAGESIZE
    32 #			define PAGESIZE 4096
    33 #		endif
    34 #		define SH_MEM_READ PROT_READ
    35 #		define SH_MEM_WRITE PROT_WRITE
    36 #		define SH_MEM_EXEC PROT_EXEC
    37 
    38 // We need to align addr down to pagesize on linux
    39 // We assume PAGESIZE is a power of two
    40 #		define SH_LALIGN(x) (void*)((intptr_t)(x) & ~(PAGESIZE-1))
    41 #		define SH_LALDIF(x) ((intptr_t)(x) & (PAGESIZE-1))
    42 # else
    43 #		error Unsupported OS/Compiler
    44 # endif
    45 
    46 
    47 namespace SourceHook
    48 {
    49 	inline bool SetMemAccess(void *addr, size_t len, int access)
    50 	{
    51 # ifdef __linux__
    52 		return mprotect(SH_LALIGN(addr), len + SH_LALDIF(addr), access)==0 ? true : false;
    53 # else
    54 		DWORD tmp;
    55 		DWORD prot;
    56 		switch (access)
    57 		{
    58 		case SH_MEM_READ:
    59 			prot = PAGE_READONLY; break;
    60 		case SH_MEM_READ | SH_MEM_WRITE:
    61 			prot = PAGE_READWRITE; break;
    62 		case SH_MEM_READ | SH_MEM_EXEC:
    63 			prot = PAGE_EXECUTE_READ; break;
    64 		default:
    65 		case SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC:
    66 			prot = PAGE_EXECUTE_READWRITE; break;
    67 		}
    68 		return VirtualProtect(addr, len, prot, &tmp) ? true : false;
    69 # endif
    70 	}
    71 
    72 #ifdef __linux__
    73 	namespace
    74 	{
    75 		bool g_BadReadCalled;
    76 		jmp_buf g_BadReadJmpBuf;
    77 
    78 		static void BadReadHandler(int sig)
    79 		{
    80 			if (g_BadReadCalled)
    81 				longjmp(g_BadReadJmpBuf, 1);
    82 		}
    83 	}
    84 #endif
    85 
    86 	/**
    87 	*	@brief Checks whether the specified memory region is (still) accessible
    88 	*
    89 	*	@param addr The lower boundary
    90 	*	@param len Length of the region to be checked
    91 	*/
    92 	namespace
    93 	{
    94 		bool ModuleInMemory(char *addr, size_t len)
    95 		{
    96 #ifdef __linux__
    97 			// On linux, first check /proc/self/maps
    98 			long lower = reinterpret_cast<long>(addr);
    99 			long upper = lower + len;
   100 
   101 			FILE *pF = fopen("/proc/self/maps", "r");
   102 			if (pF)
   103 			{
   104 				// Linux /proc/self/maps -> parse
   105 				// Format:
   106 				// lower    upper    prot     stuff                 path
   107 				// 08048000-0804c000 r-xp 00000000 03:03 1010107    /bin/cat
   108 				long rlower, rupper;
   109 				while (fscanf(pF, "%lx-%lx", &rlower, &rupper) != EOF)
   110 				{
   111 					// Check whether we're IN THERE!
   112 					if (lower >= rlower && upper <= rupper)
   113 					{
   114 						fclose(pF);
   115 						return true;
   116 					}
   117 					// Read to end of line
   118 					int c;
   119 					while ((c = fgetc(pF)) != '\n')
   120 					{
   121 						if (c == EOF)
   122 							break;
   123 					}
   124 					if (c == EOF)
   125 						break;
   126 				}
   127 				fclose(pF);
   128 				return false;
   129 			}
   130 			pF = fopen("/proc/curproc/map", "r");
   131 			if (pF)
   132 			{
   133 				// FreeBSD /proc/curproc/map -> parse
   134 				// 0x804800 0x805500 13 15 0xc6e18960 r-x 21 0x0 COW NC vnode
   135 				long rlower, rupper;
   136 				while (fscanf(pF, "0x%lx 0x%lx", &rlower, &rupper) != EOF)
   137 				{
   138 					// Check whether we're IN THERE!
   139 					if (lower >= rlower && upper <= rupper)
   140 					{
   141 						fclose(pF);
   142 						return true;
   143 					}
   144 					// Read to end of line
   145 					int c;
   146 					while ((c = fgetc(pF)) != '\n')
   147 					{
   148 						if (c == EOF)
   149 							break;
   150 					}
   151 					if (c == EOF)
   152 						break;
   153 				}
   154 				fclose(pF);
   155 				return false;
   156 			}
   157 
   158 			// Both of the above failed, try to actually read and trap sigsegv (implemented by Damaged Soul)
   159 			void(*prevHandler)(int sig);
   160 			g_BadReadCalled = true;
   161 
   162 			if (setjmp(g_BadReadJmpBuf))
   163 				return true;
   164 
   165 			prevHandler = signal(SIGSEGV, BadReadHandler);
   166 
   167 			volatile const char *p = reinterpret_cast<const char*>(addr);
   168 			char dummy;
   169 
   170 			for (size_t i = 0; i < len; i++)
   171 				dummy = p[i];
   172 
   173 			g_BadReadCalled = false;
   174 
   175 			signal(SIGSEGV, prevHandler);
   176 
   177 			return false;
   178 #else
   179 			// On Win32, simply use IsBadReadPtr
   180 			return !IsBadReadPtr(addr, len);
   181 #endif
   182 		}
   183 	}
   184 }
   185 
   186 #endif