1 /* ======== SourceHook ========
2 * Copyright (C) 2004-2007 Metamod:Source Development Team
3 * No warranties of any kind
7 * Author(s): Pavol "PM OnoTo" Marko, Scott "Damaged Soul" Ehlert
8 * Contributors: lancevorgin, XAD, theqizmo
9 * ============================
12 #ifndef __SHINT_MEMORY_H__
13 #define __SHINT_MEMORY_H__
16 // Unprotect now sets to readwrite
17 // The vtable doesn't need to be executable anyway
19 # if /********/ defined _WIN32
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>
32 // http://www.die.net/doc/linux/man/man2/mprotect.2.html
35 # define PAGESIZE 4096
37 # define SH_MEM_READ PROT_READ
38 # define SH_MEM_WRITE PROT_WRITE
39 # define SH_MEM_EXEC PROT_EXEC
41 // We need to align addr down to pagesize on linux
42 // We assume PAGESIZE is a power of two
43 # define SH_LALIGN(x) (void*)((intptr_t)(x) & ~(PAGESIZE-1))
44 # define SH_LALDIF(x) ((intptr_t)(x) & (PAGESIZE-1))
46 # error Unsupported OS/Compiler
53 inline bool SetMemAccess(void *addr, size_t len, int access)
56 return mprotect(SH_LALIGN(addr), len + SH_LALDIF(addr), access)==0 ? true : false;
63 prot = PAGE_READONLY; break;
64 case SH_MEM_READ | SH_MEM_WRITE:
65 prot = PAGE_READWRITE; break;
66 case SH_MEM_READ | SH_MEM_EXEC:
67 prot = PAGE_EXECUTE_READ; break;
69 case SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC:
70 prot = PAGE_EXECUTE_READWRITE; break;
72 return VirtualProtect(addr, len, prot, &tmp) ? true : false;
80 jmp_buf g_BadReadJmpBuf;
82 static void BadReadHandler(int sig)
85 longjmp(g_BadReadJmpBuf, 1);
91 * @brief Checks whether the specified memory region is (still) accessible
93 * @param addr The lower boundary
94 * @param len Length of the region to be checked
98 bool ModuleInMemory(char *addr, size_t len)
101 // On linux, first check /proc/self/maps
102 long lower = reinterpret_cast<long>(addr);
103 long upper = lower + len;
105 FILE *pF = fopen("/proc/self/maps", "r");
108 // Linux /proc/self/maps -> parse
110 // lower upper prot stuff path
111 // 08048000-0804c000 r-xp 00000000 03:03 1010107 /bin/cat
113 while (fscanf(pF, "%lx-%lx", &rlower, &rupper) != EOF)
115 // Check whether we're IN THERE!
116 if (lower >= rlower && upper <= rupper)
121 // Read to end of line
123 while ((c = fgetc(pF)) != '\n')
134 pF = fopen("/proc/curproc/map", "r");
137 // FreeBSD /proc/curproc/map -> parse
138 // 0x804800 0x805500 13 15 0xc6e18960 r-x 21 0x0 COW NC vnode
140 while (fscanf(pF, "0x%lx 0x%lx", &rlower, &rupper) != EOF)
142 // Check whether we're IN THERE!
143 if (lower >= rlower && upper <= rupper)
148 // Read to end of line
150 while ((c = fgetc(pF)) != '\n')
162 // Both of the above failed, try to actually read and trap sigsegv (implemented by Damaged Soul)
163 void(*prevHandler)(int sig);
164 g_BadReadCalled = true;
166 if (setjmp(g_BadReadJmpBuf))
169 prevHandler = LinuxSignal::signal(SIGSEGV, BadReadHandler);
171 volatile const char *p = reinterpret_cast<const char*>(addr);
174 for (size_t i = 0; i < len; i++)
177 g_BadReadCalled = false;
179 LinuxSignal::signal(SIGSEGV, prevHandler);
183 // On Win32, simply use IsBadReadPtr
184 return !IsBadReadPtr(addr, len);