core/MemoryUtils.cpp
author David Anderson <dvander@alliedmods.net>
Wed Feb 27 13:49:57 2008 +0000 (2008-02-27)
changeset 1900 457329ae65bd
parent 1330 ab94316bb52b
child 1947 e73a1e51833b
permissions -rw-r--r--
fixed amb1483 - GetEntPropString() rejected FIELD_MODELNAME/FIELD_SOUNDNAME
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "MemoryUtils.h"
    33 #include "ShareSys.h"
    34 #ifdef PLATFORM_LINUX
    35 #include <string.h>
    36 #include <elf.h>
    37 #endif
    38 
    39 MemoryUtils g_MemUtils;
    40 
    41 #if 0
    42 MemoryUtils::MemoryUtils()
    43 {
    44 #ifdef PLATFORM_WINDOWS
    45 
    46 	SYSTEM_INFO info;
    47 	GetSystemInfo(&info);
    48 
    49 	m_PageSize = info.dwPageSize;
    50 
    51 #elif defined PLATFORM_POSIX
    52 
    53 	m_PageSize = sysconf(_SC_PAGE_SIZE);
    54 
    55 #endif
    56 }
    57 #endif
    58 
    59 void MemoryUtils::OnSourceModAllInitialized()
    60 {
    61 	g_ShareSys.AddInterface(NULL, this);
    62 }
    63 
    64 void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len)
    65 {
    66 	DynLibInfo lib;
    67 	bool found;
    68 	char *ptr, *end;
    69 
    70 	memset(&lib, 0, sizeof(DynLibInfo));
    71 
    72 	if (!GetLibraryInfo(libPtr, lib))
    73 	{
    74 		return NULL;
    75 	}
    76 
    77 	ptr = reinterpret_cast<char *>(lib.baseAddress);
    78 	end = ptr + lib.memorySize;
    79 
    80 	while (ptr < end)
    81 	{
    82 		found = true;
    83 		for (register size_t i = 0; i < len; i++)
    84 		{
    85 			if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
    86 			{
    87 				found = false;
    88 				break;
    89 			}
    90 		}
    91 
    92 		if (found)
    93 			return ptr;
    94 
    95 		ptr++;
    96 	}
    97 
    98 	return NULL;
    99 }
   100 
   101 bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib)
   102 {
   103 	unsigned long baseAddr;
   104 
   105 	if (libPtr == NULL)
   106 	{
   107 		return false;
   108 	}
   109 
   110 #ifdef PLATFORM_WINDOWS
   111 
   112 	MEMORY_BASIC_INFORMATION info;
   113 	IMAGE_DOS_HEADER *dos;
   114 	IMAGE_NT_HEADERS *pe;
   115 	IMAGE_FILE_HEADER *file;
   116 	IMAGE_OPTIONAL_HEADER *opt;
   117 
   118 	if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION)))
   119 	{
   120 		return false;
   121 	}
   122 
   123 	baseAddr = reinterpret_cast<unsigned long>(info.AllocationBase);
   124 
   125 	/* All this is for our insane sanity checks :o */
   126 	dos = reinterpret_cast<IMAGE_DOS_HEADER *>(baseAddr);
   127 	pe = reinterpret_cast<IMAGE_NT_HEADERS *>(baseAddr + dos->e_lfanew);
   128 	file = &pe->FileHeader;
   129 	opt = &pe->OptionalHeader;
   130 
   131 	/* Check PE magic and signature */
   132 	if (dos->e_magic != IMAGE_DOS_SIGNATURE || pe->Signature != IMAGE_NT_SIGNATURE || opt->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)
   133 	{
   134 		return false;
   135 	}
   136 
   137 	/* Check architecture, which is 32-bit/x86 right now
   138 	 * Should change this for 64-bit if Valve gets their act together
   139 	 */
   140 	if (file->Machine != IMAGE_FILE_MACHINE_I386)
   141 	{
   142 		return false;
   143 	}
   144 
   145 	/* For our purposes, this must be a dynamic library */
   146 	if ((file->Characteristics & IMAGE_FILE_DLL) == 0)
   147 	{
   148 		return false;
   149 	}
   150 
   151 	/* Finally, we can do this */
   152 	lib.memorySize = opt->SizeOfImage;
   153 
   154 #elif defined PLATFORM_LINUX
   155 
   156 	Dl_info info;
   157 	Elf32_Ehdr *file;
   158 	Elf32_Phdr *phdr;
   159 	uint16_t phdrCount;
   160 
   161 	if (!dladdr(libPtr, &info))
   162 	{
   163 		return false;
   164 	}
   165 
   166 	if (!info.dli_fbase || !info.dli_fname)
   167 	{
   168 		return false;
   169 	}
   170 
   171 	/* This is for our insane sanity checks :o */
   172 	baseAddr = reinterpret_cast<unsigned long>(info.dli_fbase);
   173 	file = reinterpret_cast<Elf32_Ehdr *>(baseAddr);
   174 
   175 	/* Check ELF magic */
   176 	if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
   177 	{
   178 		return false;
   179 	}
   180 
   181 	/* Check ELF version */
   182 	if (file->e_ident[EI_VERSION] != EV_CURRENT)
   183 	{
   184 		return false;
   185 	}
   186 
   187 	/* Check ELF architecture, which is 32-bit/x86 right now
   188 	 * Should change this for 64-bit if Valve gets their act together
   189 	 */
   190 	if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB)
   191 	{
   192 		return false;
   193 	}
   194 
   195 	/* For our purposes, this must be a dynamic library/shared object */
   196 	if (file->e_type != ET_DYN)
   197 	{
   198 		return false;
   199 	}
   200 
   201 	phdrCount = file->e_phnum;
   202 	phdr = reinterpret_cast<Elf32_Phdr *>(baseAddr + file->e_phoff);
   203 	
   204 	/* Add up the memory sizes of segments marked as PT_LOAD as those are the only ones that should be in memory */
   205 	for (uint16_t i = 0; i < phdrCount; i++)
   206 	{
   207 		Elf32_Phdr &hdr = phdr[i];
   208 
   209 		if (hdr.p_type == PT_LOAD)
   210 		{
   211 			lib.memorySize += hdr.p_memsz;
   212 		}
   213 	}
   214 
   215 #endif
   216 
   217 	lib.baseAddress = reinterpret_cast<void *>(baseAddr);
   218 
   219 	return true;
   220 }