1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core-legacy/sourcehook/sh_memory.h Fri Nov 14 05:02:00 2008 -0600
1.3 @@ -0,0 +1,186 @@
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))
1.45 +# else
1.46 +# error Unsupported OS/Compiler
1.47 +# endif
1.48 +
1.49 +
1.50 +namespace SourceHook
1.51 +{
1.52 + inline bool SetMemAccess(void *addr, size_t len, int access)
1.53 + {
1.54 +# ifdef __linux__
1.55 + return mprotect(SH_LALIGN(addr), len + SH_LALDIF(addr), access)==0 ? true : false;
1.56 +# else
1.57 + DWORD tmp;
1.58 + DWORD prot;
1.59 + switch (access)
1.60 + {
1.61 + case SH_MEM_READ:
1.62 + prot = PAGE_READONLY; break;
1.63 + case SH_MEM_READ | SH_MEM_WRITE:
1.64 + prot = PAGE_READWRITE; break;
1.65 + case SH_MEM_READ | SH_MEM_EXEC:
1.66 + prot = PAGE_EXECUTE_READ; break;
1.67 + default:
1.68 + case SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC:
1.69 + prot = PAGE_EXECUTE_READWRITE; break;
1.70 + }
1.71 + return VirtualProtect(addr, len, prot, &tmp) ? true : false;
1.72 +# endif
1.73 + }
1.74 +
1.75 +#ifdef __linux__
1.76 + namespace
1.77 + {
1.78 + bool g_BadReadCalled;
1.79 + jmp_buf g_BadReadJmpBuf;
1.80 +
1.81 + static void BadReadHandler(int sig)
1.82 + {
1.83 + if (g_BadReadCalled)
1.84 + longjmp(g_BadReadJmpBuf, 1);
1.85 + }
1.86 + }
1.87 +#endif
1.88 +
1.89 + /**
1.90 + * @brief Checks whether the specified memory region is (still) accessible
1.91 + *
1.92 + * @param addr The lower boundary
1.93 + * @param len Length of the region to be checked
1.94 + */
1.95 + namespace
1.96 + {
1.97 + bool ModuleInMemory(char *addr, size_t len)
1.98 + {
1.99 +#ifdef __linux__
1.100 + // On linux, first check /proc/self/maps
1.101 + long lower = reinterpret_cast<long>(addr);
1.102 + long upper = lower + len;
1.103 +
1.104 + FILE *pF = fopen("/proc/self/maps", "r");
1.105 + if (pF)
1.106 + {
1.107 + // Linux /proc/self/maps -> parse
1.108 + // Format:
1.109 + // lower upper prot stuff path
1.110 + // 08048000-0804c000 r-xp 00000000 03:03 1010107 /bin/cat
1.111 + long rlower, rupper;
1.112 + while (fscanf(pF, "%lx-%lx", &rlower, &rupper) != EOF)
1.113 + {
1.114 + // Check whether we're IN THERE!
1.115 + if (lower >= rlower && upper <= rupper)
1.116 + {
1.117 + fclose(pF);
1.118 + return true;
1.119 + }
1.120 + // Read to end of line
1.121 + int c;
1.122 + while ((c = fgetc(pF)) != '\n')
1.123 + {
1.124 + if (c == EOF)
1.125 + break;
1.126 + }
1.127 + if (c == EOF)
1.128 + break;
1.129 + }
1.130 + fclose(pF);
1.131 + return false;
1.132 + }
1.133 + pF = fopen("/proc/curproc/map", "r");
1.134 + if (pF)
1.135 + {
1.136 + // FreeBSD /proc/curproc/map -> parse
1.137 + // 0x804800 0x805500 13 15 0xc6e18960 r-x 21 0x0 COW NC vnode
1.138 + long rlower, rupper;
1.139 + while (fscanf(pF, "0x%lx 0x%lx", &rlower, &rupper) != EOF)
1.140 + {
1.141 + // Check whether we're IN THERE!
1.142 + if (lower >= rlower && upper <= rupper)
1.143 + {
1.144 + fclose(pF);
1.145 + return true;
1.146 + }
1.147 + // Read to end of line
1.148 + int c;
1.149 + while ((c = fgetc(pF)) != '\n')
1.150 + {
1.151 + if (c == EOF)
1.152 + break;
1.153 + }
1.154 + if (c == EOF)
1.155 + break;
1.156 + }
1.157 + fclose(pF);
1.158 + return false;
1.159 + }
1.160 +
1.161 + // Both of the above failed, try to actually read and trap sigsegv (implemented by Damaged Soul)
1.162 + void(*prevHandler)(int sig);
1.163 + g_BadReadCalled = true;
1.164 +
1.165 + if (setjmp(g_BadReadJmpBuf))
1.166 + return true;
1.167 +
1.168 + prevHandler = signal(SIGSEGV, BadReadHandler);
1.169 +
1.170 + volatile const char *p = reinterpret_cast<const char*>(addr);
1.171 + char dummy;
1.172 +
1.173 + for (size_t i = 0; i < len; i++)
1.174 + dummy = p[i];
1.175 +
1.176 + g_BadReadCalled = false;
1.177 +
1.178 + signal(SIGSEGV, prevHandler);
1.179 +
1.180 + return false;
1.181 +#else
1.182 + // On Win32, simply use IsBadReadPtr
1.183 + return !IsBadReadPtr(addr, len);
1.184 +#endif
1.185 + }
1.186 + }
1.187 +}
1.188 +
1.189 +#endif