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