core/MemoryUtils.cpp
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/MemoryUtils.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,220 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include "MemoryUtils.h"
    1.36 +#include "ShareSys.h"
    1.37 +#ifdef PLATFORM_LINUX
    1.38 +#include <string.h>
    1.39 +#include <elf.h>
    1.40 +#endif
    1.41 +
    1.42 +MemoryUtils g_MemUtils;
    1.43 +
    1.44 +#if 0
    1.45 +MemoryUtils::MemoryUtils()
    1.46 +{
    1.47 +#ifdef PLATFORM_WINDOWS
    1.48 +
    1.49 +	SYSTEM_INFO info;
    1.50 +	GetSystemInfo(&info);
    1.51 +
    1.52 +	m_PageSize = info.dwPageSize;
    1.53 +
    1.54 +#elif defined PLATFORM_POSIX
    1.55 +
    1.56 +	m_PageSize = sysconf(_SC_PAGE_SIZE);
    1.57 +
    1.58 +#endif
    1.59 +}
    1.60 +#endif
    1.61 +
    1.62 +void MemoryUtils::OnSourceModAllInitialized()
    1.63 +{
    1.64 +	g_ShareSys.AddInterface(NULL, this);
    1.65 +}
    1.66 +
    1.67 +void *MemoryUtils::FindPattern(const void *libPtr, const char *pattern, size_t len)
    1.68 +{
    1.69 +	DynLibInfo lib;
    1.70 +	bool found;
    1.71 +	char *ptr, *end;
    1.72 +
    1.73 +	memset(&lib, 0, sizeof(DynLibInfo));
    1.74 +
    1.75 +	if (!GetLibraryInfo(libPtr, lib))
    1.76 +	{
    1.77 +		return NULL;
    1.78 +	}
    1.79 +
    1.80 +	ptr = reinterpret_cast<char *>(lib.baseAddress);
    1.81 +	end = ptr + lib.memorySize;
    1.82 +
    1.83 +	while (ptr < end)
    1.84 +	{
    1.85 +		found = true;
    1.86 +		for (register size_t i = 0; i < len; i++)
    1.87 +		{
    1.88 +			if (pattern[i] != '\x2A' && pattern[i] != ptr[i])
    1.89 +			{
    1.90 +				found = false;
    1.91 +				break;
    1.92 +			}
    1.93 +		}
    1.94 +
    1.95 +		if (found)
    1.96 +			return ptr;
    1.97 +
    1.98 +		ptr++;
    1.99 +	}
   1.100 +
   1.101 +	return NULL;
   1.102 +}
   1.103 +
   1.104 +bool MemoryUtils::GetLibraryInfo(const void *libPtr, DynLibInfo &lib)
   1.105 +{
   1.106 +	unsigned long baseAddr;
   1.107 +
   1.108 +	if (libPtr == NULL)
   1.109 +	{
   1.110 +		return false;
   1.111 +	}
   1.112 +
   1.113 +#ifdef PLATFORM_WINDOWS
   1.114 +
   1.115 +	MEMORY_BASIC_INFORMATION info;
   1.116 +	IMAGE_DOS_HEADER *dos;
   1.117 +	IMAGE_NT_HEADERS *pe;
   1.118 +	IMAGE_FILE_HEADER *file;
   1.119 +	IMAGE_OPTIONAL_HEADER *opt;
   1.120 +
   1.121 +	if (!VirtualQuery(libPtr, &info, sizeof(MEMORY_BASIC_INFORMATION)))
   1.122 +	{
   1.123 +		return false;
   1.124 +	}
   1.125 +
   1.126 +	baseAddr = reinterpret_cast<unsigned long>(info.AllocationBase);
   1.127 +
   1.128 +	/* All this is for our insane sanity checks :o */
   1.129 +	dos = reinterpret_cast<IMAGE_DOS_HEADER *>(baseAddr);
   1.130 +	pe = reinterpret_cast<IMAGE_NT_HEADERS *>(baseAddr + dos->e_lfanew);
   1.131 +	file = &pe->FileHeader;
   1.132 +	opt = &pe->OptionalHeader;
   1.133 +
   1.134 +	/* Check PE magic and signature */
   1.135 +	if (dos->e_magic != IMAGE_DOS_SIGNATURE || pe->Signature != IMAGE_NT_SIGNATURE || opt->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)
   1.136 +	{
   1.137 +		return false;
   1.138 +	}
   1.139 +
   1.140 +	/* Check architecture, which is 32-bit/x86 right now
   1.141 +	 * Should change this for 64-bit if Valve gets their act together
   1.142 +	 */
   1.143 +	if (file->Machine != IMAGE_FILE_MACHINE_I386)
   1.144 +	{
   1.145 +		return false;
   1.146 +	}
   1.147 +
   1.148 +	/* For our purposes, this must be a dynamic library */
   1.149 +	if ((file->Characteristics & IMAGE_FILE_DLL) == 0)
   1.150 +	{
   1.151 +		return false;
   1.152 +	}
   1.153 +
   1.154 +	/* Finally, we can do this */
   1.155 +	lib.memorySize = opt->SizeOfImage;
   1.156 +
   1.157 +#elif defined PLATFORM_LINUX
   1.158 +
   1.159 +	Dl_info info;
   1.160 +	Elf32_Ehdr *file;
   1.161 +	Elf32_Phdr *phdr;
   1.162 +	uint16_t phdrCount;
   1.163 +
   1.164 +	if (!dladdr(libPtr, &info))
   1.165 +	{
   1.166 +		return false;
   1.167 +	}
   1.168 +
   1.169 +	if (!info.dli_fbase || !info.dli_fname)
   1.170 +	{
   1.171 +		return false;
   1.172 +	}
   1.173 +
   1.174 +	/* This is for our insane sanity checks :o */
   1.175 +	baseAddr = reinterpret_cast<unsigned long>(info.dli_fbase);
   1.176 +	file = reinterpret_cast<Elf32_Ehdr *>(baseAddr);
   1.177 +
   1.178 +	/* Check ELF magic */
   1.179 +	if (memcmp(ELFMAG, file->e_ident, SELFMAG) != 0)
   1.180 +	{
   1.181 +		return false;
   1.182 +	}
   1.183 +
   1.184 +	/* Check ELF version */
   1.185 +	if (file->e_ident[EI_VERSION] != EV_CURRENT)
   1.186 +	{
   1.187 +		return false;
   1.188 +	}
   1.189 +
   1.190 +	/* Check ELF architecture, which is 32-bit/x86 right now
   1.191 +	 * Should change this for 64-bit if Valve gets their act together
   1.192 +	 */
   1.193 +	if (file->e_ident[EI_CLASS] != ELFCLASS32 || file->e_machine != EM_386 || file->e_ident[EI_DATA] != ELFDATA2LSB)
   1.194 +	{
   1.195 +		return false;
   1.196 +	}
   1.197 +
   1.198 +	/* For our purposes, this must be a dynamic library/shared object */
   1.199 +	if (file->e_type != ET_DYN)
   1.200 +	{
   1.201 +		return false;
   1.202 +	}
   1.203 +
   1.204 +	phdrCount = file->e_phnum;
   1.205 +	phdr = reinterpret_cast<Elf32_Phdr *>(baseAddr + file->e_phoff);
   1.206 +	
   1.207 +	/* Add up the memory sizes of segments marked as PT_LOAD as those are the only ones that should be in memory */
   1.208 +	for (uint16_t i = 0; i < phdrCount; i++)
   1.209 +	{
   1.210 +		Elf32_Phdr &hdr = phdr[i];
   1.211 +
   1.212 +		if (hdr.p_type == PT_LOAD)
   1.213 +		{
   1.214 +			lib.memorySize += hdr.p_memsz;
   1.215 +		}
   1.216 +	}
   1.217 +
   1.218 +#endif
   1.219 +
   1.220 +	lib.baseAddress = reinterpret_cast<void *>(baseAddr);
   1.221 +
   1.222 +	return true;
   1.223 +}