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