sourcehook/sh_memory.h
author Pavol Marko <pavolmarko@gmail.com>
Thu Nov 01 01:58:44 2007 +0000 (2007-11-01)
branchhookman_autogen
changeset 435 35296642e147
parent 294 0d87bc8606f8
child 442 07575ad47137
permissions -rw-r--r--
sh_memory now has CPageAlloc, testhookmangen.cpp contains a small test for it, GenBuffer uses CPageAlloc
(with this simple change, thesthookmangen uses 124K less memory!)
     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 #include "sh_list.h"
    47 
    48 namespace SourceHook
    49 {
    50 	inline bool SetMemAccess(void *addr, size_t len, int access)
    51 	{
    52 # ifdef __linux__
    53 		return mprotect(SH_LALIGN(addr), len + SH_LALDIF(addr), access)==0 ? true : false;
    54 # else
    55 		DWORD tmp;
    56 		DWORD prot;
    57 		switch (access)
    58 		{
    59 		case SH_MEM_READ:
    60 			prot = PAGE_READONLY; break;
    61 		case SH_MEM_READ | SH_MEM_WRITE:
    62 			prot = PAGE_READWRITE; break;
    63 		case SH_MEM_READ | SH_MEM_EXEC:
    64 			prot = PAGE_EXECUTE_READ; break;
    65 		default:
    66 		case SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC:
    67 			prot = PAGE_EXECUTE_READWRITE; break;
    68 		}
    69 		return VirtualProtect(addr, len, prot, &tmp) ? true : false;
    70 # endif
    71 	}
    72 
    73 #ifdef __linux__
    74 	namespace
    75 	{
    76 		bool g_BadReadCalled;
    77 		jmp_buf g_BadReadJmpBuf;
    78 
    79 		static void BadReadHandler(int sig)
    80 		{
    81 			if (g_BadReadCalled)
    82 				longjmp(g_BadReadJmpBuf, 1);
    83 		}
    84 	}
    85 #endif
    86 
    87 	/**
    88 	*	@brief Checks whether the specified memory region is (still) accessible
    89 	*
    90 	*	@param addr The lower boundary
    91 	*	@param len Length of the region to be checked
    92 	*/
    93 	namespace
    94 	{
    95 		bool ModuleInMemory(char *addr, size_t len)
    96 		{
    97 #ifdef __linux__
    98 			// On linux, first check /proc/self/maps
    99 			long lower = reinterpret_cast<long>(addr);
   100 			long upper = lower + len;
   101 
   102 			FILE *pF = fopen("/proc/self/maps", "r");
   103 			if (pF)
   104 			{
   105 				// Linux /proc/self/maps -> parse
   106 				// Format:
   107 				// lower    upper    prot     stuff                 path
   108 				// 08048000-0804c000 r-xp 00000000 03:03 1010107    /bin/cat
   109 				long rlower, rupper;
   110 				while (fscanf(pF, "%lx-%lx", &rlower, &rupper) != EOF)
   111 				{
   112 					// Check whether we're IN THERE!
   113 					if (lower >= rlower && upper <= rupper)
   114 					{
   115 						fclose(pF);
   116 						return true;
   117 					}
   118 					// Read to end of line
   119 					int c;
   120 					while ((c = fgetc(pF)) != '\n')
   121 					{
   122 						if (c == EOF)
   123 							break;
   124 					}
   125 					if (c == EOF)
   126 						break;
   127 				}
   128 				fclose(pF);
   129 				return false;
   130 			}
   131 			pF = fopen("/proc/curproc/map", "r");
   132 			if (pF)
   133 			{
   134 				// FreeBSD /proc/curproc/map -> parse
   135 				// 0x804800 0x805500 13 15 0xc6e18960 r-x 21 0x0 COW NC vnode
   136 				long rlower, rupper;
   137 				while (fscanf(pF, "0x%lx 0x%lx", &rlower, &rupper) != EOF)
   138 				{
   139 					// Check whether we're IN THERE!
   140 					if (lower >= rlower && upper <= rupper)
   141 					{
   142 						fclose(pF);
   143 						return true;
   144 					}
   145 					// Read to end of line
   146 					int c;
   147 					while ((c = fgetc(pF)) != '\n')
   148 					{
   149 						if (c == EOF)
   150 							break;
   151 					}
   152 					if (c == EOF)
   153 						break;
   154 				}
   155 				fclose(pF);
   156 				return false;
   157 			}
   158 
   159 			// Both of the above failed, try to actually read and trap sigsegv (implemented by Damaged Soul)
   160 			void(*prevHandler)(int sig);
   161 			g_BadReadCalled = true;
   162 
   163 			if (setjmp(g_BadReadJmpBuf))
   164 				return true;
   165 
   166 			prevHandler = signal(SIGSEGV, BadReadHandler);
   167 
   168 			volatile const char *p = reinterpret_cast<const char*>(addr);
   169 			char dummy;
   170 
   171 			for (size_t i = 0; i < len; i++)
   172 				dummy = p[i];
   173 
   174 			g_BadReadCalled = false;
   175 
   176 			signal(SIGSEGV, prevHandler);
   177 
   178 			return false;
   179 #else
   180 			// On Win32, simply use IsBadReadPtr
   181 			return !IsBadReadPtr(addr, len);
   182 #endif
   183 		}
   184 	}
   185 
   186 	/*
   187 		Class which lets us allocate memory regions in special pages only meant for on the fly code generation.
   188 
   189 		If we alloc with malloc and then set the page access type to read/exec only, other regions returned by
   190 		malloc that are in the same page would lose their write access as well and the process could crash.
   191 
   192 		Allocating one page per code generation session is usually a waste of memory and on some plattforms also
   193 		a waste of virtual address space (Windows� VirtualAlloc has a granularity of 64K).
   194 
   195 
   196 		IMPORTANT: the memory that Alloc() returns is not a in a defined state!
   197 		It could be in read+exec OR read+write mode.
   198 		-> call SetRE() or SetRW() before using allocated memory!
   199 	*/
   200 	class CPageAlloc
   201 	{
   202 		struct AllocationUnit
   203 		{
   204 			size_t begin_offset;
   205 			size_t size;
   206 
   207 			AllocationUnit(size_t p_offs, size_t p_size) : begin_offset(p_offs), size(p_size)
   208 			{
   209 			}
   210 
   211 			bool operator < (const AllocationUnit &other) const
   212 			{
   213 				return begin_offset < other.begin_offset;
   214 			}
   215 		};
   216 		
   217 		typedef List<AllocationUnit> AUList;
   218 
   219 		struct AllocatedRegion
   220 		{
   221 			void *startPtr;
   222 			size_t size;
   223 			bool isolated;					// may contain only one AU
   224 			AUList allocUnits;
   225 
   226 			bool TryAlloc(size_t reqsize, void * &outAddr)
   227 			{
   228 				// Check for isolated
   229 				if (isolated && !allocUnits.empty())
   230 					return false;
   231 
   232 				// Find the smallest gap where req fits
   233 				size_t lastend = 0;
   234 				size_t smallestgap_pos = size + 1;
   235 				size_t smallestgap_size = size + 1;
   236 
   237 				for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
   238 				{
   239 					if (iter->begin_offset - lastend >= reqsize)
   240 					{
   241 						if (iter->begin_offset - lastend < smallestgap_size)
   242 						{
   243 							smallestgap_size = iter->begin_offset - lastend;
   244 							smallestgap_pos = lastend;
   245 						}
   246 					}
   247 					lastend = iter->begin_offset + iter->size;
   248 				}
   249 
   250 				if (size - lastend >= reqsize)
   251 				{
   252 					if (size - lastend < smallestgap_size)
   253 					{
   254 						smallestgap_size = size - lastend;
   255 						smallestgap_pos = lastend;
   256 					}
   257 				}
   258 
   259 				if (smallestgap_pos < size)
   260 				{
   261 					outAddr = reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + smallestgap_pos);
   262 					allocUnits.push_sorted( AllocationUnit(smallestgap_pos, reqsize) );
   263 					return true;
   264 				}
   265 				else
   266 				{
   267 					return false;
   268 				}
   269 			}
   270 
   271 			bool TryFree(void *addr)
   272 			{
   273 				if (addr < startPtr || addr >= reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size))
   274 					return false;
   275 
   276 				size_t offs = reinterpret_cast<char*>(addr) - reinterpret_cast<char*>(startPtr);
   277 
   278 				for (AUList::iterator iter = allocUnits.begin(); iter != allocUnits.end(); ++iter)
   279 				{
   280 					if (iter->begin_offset == offs)
   281 					{
   282 						allocUnits.erase(iter);
   283 						return true;
   284 					}
   285 				}
   286 
   287 				return false;
   288 			}
   289 
   290 			bool Contains(void *addr)
   291 			{
   292 				return addr >= startPtr && addr < reinterpret_cast<void*>(reinterpret_cast<char*>(startPtr) + size);
   293 			}
   294 
   295 			void FreeRegion()
   296 			{
   297 #ifdef __linux__
   298 				munmap(startPtr, size);
   299 #else
   300 				VirtualFree(startPtr, 0, MEM_RELEASE);
   301 #endif
   302 			}
   303 		};
   304 
   305 		typedef List<AllocatedRegion> ARList;
   306 
   307 		size_t m_PageSize;
   308 		ARList m_Regions;
   309 
   310 		bool AddRegion(size_t minSize, bool isolated)
   311 		{
   312 			AllocatedRegion newRegion;
   313 			newRegion.startPtr = 0;
   314 			newRegion.isolated = isolated;
   315 
   316 			// Compute real size -> align up to m_PageSize boundary
   317 
   318 			newRegion.size = minSize - (minSize % m_PageSize);
   319 			if (newRegion.size < minSize)
   320 				newRegion.size += m_PageSize;
   321 
   322 #ifdef __linux__
   323 			newRegion.startPtr = mmap(0, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAN_ANONYMOUS, -1);
   324 #else
   325 			newRegion.startPtr = VirtualAlloc(NULL, newRegion.size, MEM_COMMIT, PAGE_READWRITE);
   326 #endif
   327 			
   328 			if (newRegion.startPtr)
   329 			{
   330 				m_Regions.push_back(newRegion);
   331 				return true;
   332 			}
   333 			else
   334 			{
   335 				return false;
   336 			}
   337 			
   338 		}
   339 
   340 		void *AllocPriv(size_t size, bool isolated)
   341 		{
   342 			void *addr;
   343 
   344 			if (!isolated)
   345 			{
   346 				for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
   347 				{
   348 					if (iter->TryAlloc(size, addr))
   349 						return addr;
   350 				}
   351 			}
   352 
   353 			if (!AddRegion(size, isolated))
   354 				return NULL;
   355 			
   356 			bool tmp = m_Regions.back().TryAlloc(size, addr);
   357 			SH_ASSERT(tmp, ("TryAlloc fails after AddRegion"));
   358 			return addr;
   359 		}
   360 
   361 	public:
   362 		CPageAlloc()
   363 		{
   364 #ifdef __linux__
   365 			m_PageSize = sysconf(_SC_PAGESIZE);
   366 #else
   367 			SYSTEM_INFO sysInfo;
   368 			GetSystemInfo(&sysInfo);
   369 			m_PageSize = sysInfo.dwPageSize;
   370 #endif
   371 		}
   372 
   373 		~CPageAlloc()
   374 		{
   375 			// Free all regions
   376 			for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
   377 			{
   378 				iter->FreeRegion();
   379 			}
   380 		}
   381 
   382 		void *Alloc(size_t size)
   383 		{
   384 			return AllocPriv(size, false);
   385 		}
   386 
   387 		void *AllocIsolated(size_t size)
   388 		{
   389 			return AllocPriv(size, true);
   390 		}
   391 
   392 		void Free(void *ptr)
   393 		{
   394 			for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
   395 			{
   396 				if (iter->TryFree(ptr))
   397 				{
   398 					if (iter->allocUnits.empty())
   399 					{
   400 						iter->FreeRegion();
   401 						m_Regions.erase(iter);
   402 					}
   403 					break;
   404 				}
   405 			}
   406 		}
   407 
   408 		void SetRE(void *ptr)
   409 		{
   410 			for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
   411 			{
   412 				if (iter->Contains(ptr))
   413 				{
   414 					SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_EXEC);
   415 					break;
   416 				}
   417 			}
   418 		}
   419 
   420 		void SetRW(void *ptr)
   421 		{
   422 			for (ARList::iterator iter = m_Regions.begin(); iter != m_Regions.end(); ++iter)
   423 			{
   424 				if (iter->Contains(ptr))
   425 				{
   426 					SetMemAccess(iter->startPtr, iter->size, SH_MEM_READ | SH_MEM_WRITE);
   427 					break;
   428 				}
   429 			}
   430 		}
   431 
   432 		size_t GetPageSize()
   433 		{
   434 			return m_PageSize;
   435 		}
   436 	};
   437 }
   438 
   439 #endif