2011-02-24 13:34:51 -07:00
|
|
|
/*
|
2011-06-16 15:53:39 -06:00
|
|
|
https://github.com/peterix/dfhack
|
2012-09-29 20:03:37 -06:00
|
|
|
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
|
2011-02-24 13:34:51 -07:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
2011-06-16 15:53:39 -06:00
|
|
|
|
2011-02-24 13:34:51 -07:00
|
|
|
#include "Internal.h"
|
2011-06-19 17:12:07 -06:00
|
|
|
|
2014-02-27 14:58:15 -07:00
|
|
|
#define _WIN32_WINNT 0x0501
|
|
|
|
#define WINVER 0x0501
|
|
|
|
|
2011-06-19 17:12:07 -06:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
2014-02-27 14:58:15 -07:00
|
|
|
#include <psapi.h>
|
2011-06-19 17:12:07 -06:00
|
|
|
|
2011-04-10 03:03:24 -06:00
|
|
|
#include <cstring>
|
2011-04-16 17:48:58 -06:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2011-04-10 03:03:24 -06:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
using namespace std;
|
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "VersionInfo.h"
|
|
|
|
#include "VersionInfoFactory.h"
|
|
|
|
#include "Error.h"
|
|
|
|
#include "MemAccess.h"
|
2016-07-27 18:10:03 -06:00
|
|
|
#include "Memory.h"
|
2011-02-24 13:34:51 -07:00
|
|
|
using namespace DFHack;
|
2011-06-14 08:13:28 -06:00
|
|
|
namespace DFHack
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
2011-06-14 08:13:28 -06:00
|
|
|
class PlatformSpecific
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
2011-06-14 08:13:28 -06:00
|
|
|
public:
|
|
|
|
PlatformSpecific()
|
|
|
|
{
|
|
|
|
base = 0;
|
|
|
|
sections = 0;
|
|
|
|
};
|
|
|
|
HANDLE my_handle;
|
|
|
|
uint32_t my_pid;
|
|
|
|
IMAGE_NT_HEADERS pe_header;
|
|
|
|
IMAGE_SECTION_HEADER * sections;
|
2012-01-05 15:39:14 -07:00
|
|
|
char * base;
|
2011-02-24 13:34:51 -07:00
|
|
|
};
|
|
|
|
}
|
2018-07-08 16:50:14 -06:00
|
|
|
Process::Process(const VersionInfoFactory& factory) : identified(false)
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
|
|
|
HMODULE hmod = NULL;
|
|
|
|
DWORD needed;
|
|
|
|
bool found = false;
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
d = new PlatformSpecific();
|
|
|
|
// open process
|
|
|
|
d->my_pid = GetCurrentProcessId();
|
|
|
|
d->my_handle = GetCurrentProcess();
|
2011-02-24 13:34:51 -07:00
|
|
|
// try getting the first module of the process
|
2011-06-14 08:13:28 -06:00
|
|
|
if(EnumProcessModules(d->my_handle, &hmod, sizeof(hmod), &needed) == 0)
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
|
|
|
return; //if enumprocessModules fails, give up
|
|
|
|
}
|
|
|
|
|
|
|
|
// got base ;)
|
2012-01-05 15:39:14 -07:00
|
|
|
d->base = (char *)hmod;
|
2011-02-24 13:34:51 -07:00
|
|
|
|
|
|
|
// read from this process
|
|
|
|
try
|
|
|
|
{
|
2014-02-26 04:44:10 -07:00
|
|
|
uint32_t pe_offset = readDWord(d->base+0x3C);
|
2011-06-14 08:13:28 -06:00
|
|
|
read(d->base + pe_offset, sizeof(d->pe_header), (uint8_t *)&(d->pe_header));
|
|
|
|
const size_t sectionsSize = sizeof(IMAGE_SECTION_HEADER) * d->pe_header.FileHeader.NumberOfSections;
|
|
|
|
d->sections = (IMAGE_SECTION_HEADER *) malloc(sectionsSize);
|
|
|
|
read(d->base + pe_offset + sizeof(d->pe_header), sectionsSize, (uint8_t *)(d->sections));
|
2011-02-24 13:34:51 -07:00
|
|
|
}
|
|
|
|
catch (exception &)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-12-27 12:59:18 -07:00
|
|
|
my_pe = d->pe_header.FileHeader.TimeDateStamp;
|
2018-07-08 16:50:14 -06:00
|
|
|
auto vinfo = factory.getVersionInfoByPETimestamp(my_pe);
|
2011-02-24 13:34:51 -07:00
|
|
|
if(vinfo)
|
|
|
|
{
|
|
|
|
identified = true;
|
|
|
|
// give the process a data model and memory layout fixed for the base of first module
|
2018-07-08 16:50:14 -06:00
|
|
|
my_descriptor = std::make_shared<VersionInfo>(*vinfo);
|
2012-11-11 04:49:01 -07:00
|
|
|
my_descriptor->rebaseTo(getBase());
|
2011-02-24 13:34:51 -07:00
|
|
|
}
|
2015-09-22 18:01:05 -06:00
|
|
|
else
|
|
|
|
{
|
2015-12-27 12:59:18 -07:00
|
|
|
fprintf(stderr, "Unable to retrieve version information.\nPE timestamp: 0x%x\n", my_pe);
|
2015-09-22 18:01:05 -06:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
2011-02-24 13:34:51 -07:00
|
|
|
}
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
Process::~Process()
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
|
|
|
// destroy our rebased copy of the memory descriptor
|
2011-06-14 08:13:28 -06:00
|
|
|
if(d->sections != NULL)
|
|
|
|
free(d->sections);
|
2011-02-24 13:34:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
typedef struct _MEMORY_BASIC_INFORMATION
|
|
|
|
{
|
|
|
|
void * BaseAddress;
|
|
|
|
void * AllocationBase;
|
|
|
|
uint32_t AllocationProtect;
|
|
|
|
size_t RegionSize;
|
|
|
|
uint32_t State;
|
|
|
|
uint32_t Protect;
|
|
|
|
uint32_t Type;
|
|
|
|
} MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
//Internal structure used to store heap block information.
|
|
|
|
struct HeapBlock
|
|
|
|
{
|
|
|
|
PVOID dwAddress;
|
|
|
|
DWORD dwSize;
|
|
|
|
DWORD dwFlags;
|
|
|
|
ULONG reserved;
|
|
|
|
};
|
|
|
|
*/
|
2012-06-19 08:41:18 -06:00
|
|
|
|
|
|
|
static void GetDosNames(std::map<string, string> &table)
|
|
|
|
{
|
|
|
|
// Partially based on example from msdn:
|
|
|
|
// Translate path with device name to drive letters.
|
|
|
|
TCHAR szTemp[512];
|
|
|
|
szTemp[0] = '\0';
|
|
|
|
|
|
|
|
if (GetLogicalDriveStrings(sizeof(szTemp)-1, szTemp))
|
|
|
|
{
|
|
|
|
TCHAR szName[MAX_PATH];
|
|
|
|
TCHAR szDrive[3] = " :";
|
|
|
|
BOOL bFound = FALSE;
|
|
|
|
TCHAR* p = szTemp;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Copy the drive letter to the template string
|
|
|
|
*szDrive = *p;
|
|
|
|
|
|
|
|
// Look up each device name
|
|
|
|
if (QueryDosDevice(szDrive, szName, MAX_PATH))
|
|
|
|
table[szName] = szDrive;
|
|
|
|
|
|
|
|
// Go to the next NULL character.
|
|
|
|
while (*p++);
|
|
|
|
} while (*p); // end of string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
void Process::getMemRanges( vector<t_memrange> & ranges )
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION MBI;
|
2012-02-25 14:59:32 -07:00
|
|
|
//map<char *, unsigned int> heaps;
|
2011-02-24 13:34:51 -07:00
|
|
|
uint64_t movingStart = 0;
|
2012-06-19 08:41:18 -06:00
|
|
|
PVOID LastAllocationBase = 0;
|
2012-01-05 15:39:14 -07:00
|
|
|
map <char *, string> nameMap;
|
2012-06-19 08:41:18 -06:00
|
|
|
map <string,string> dosDrives;
|
2011-02-24 13:34:51 -07:00
|
|
|
|
|
|
|
// get page size
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo(&si);
|
|
|
|
uint64_t PageSize = si.dwPageSize;
|
2012-06-19 08:41:18 -06:00
|
|
|
|
|
|
|
// get dos drive names
|
|
|
|
GetDosNames(dosDrives);
|
|
|
|
|
|
|
|
ranges.clear();
|
|
|
|
|
2011-02-24 13:34:51 -07:00
|
|
|
// enumerate heaps
|
2012-02-25 14:59:32 -07:00
|
|
|
// HeapNodes(d->my_pid, heaps);
|
2011-02-24 13:34:51 -07:00
|
|
|
// go through all the VM regions, convert them to our internal format
|
2011-06-14 08:13:28 -06:00
|
|
|
while (VirtualQueryEx(d->my_handle, (const void*) (movingStart), &MBI, sizeof(MBI)) == sizeof(MBI))
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
|
|
|
movingStart = ((uint64_t)MBI.BaseAddress + MBI.RegionSize);
|
|
|
|
if(movingStart % PageSize != 0)
|
|
|
|
movingStart = (movingStart / PageSize + 1) * PageSize;
|
2012-06-19 08:41:18 -06:00
|
|
|
|
|
|
|
// Skip unallocated address space
|
|
|
|
if (MBI.State & MEM_FREE)
|
2011-02-24 13:34:51 -07:00
|
|
|
continue;
|
2012-06-19 08:41:18 -06:00
|
|
|
|
|
|
|
// Find range and permissions
|
2011-02-24 13:34:51 -07:00
|
|
|
t_memrange temp;
|
2012-06-19 08:41:18 -06:00
|
|
|
memset(&temp, 0, sizeof(temp));
|
|
|
|
|
2012-01-05 15:39:14 -07:00
|
|
|
temp.start = (char *) MBI.BaseAddress;
|
|
|
|
temp.end = ((char *)MBI.BaseAddress + (uint64_t)MBI.RegionSize);
|
2012-06-19 08:41:18 -06:00
|
|
|
temp.valid = true;
|
|
|
|
|
|
|
|
if (!(MBI.State & MEM_COMMIT))
|
|
|
|
temp.valid = false; // reserved address space
|
|
|
|
else if (MBI.Protect & PAGE_EXECUTE)
|
|
|
|
temp.execute = true;
|
|
|
|
else if (MBI.Protect & PAGE_EXECUTE_READ)
|
|
|
|
temp.execute = temp.read = true;
|
|
|
|
else if (MBI.Protect & PAGE_EXECUTE_READWRITE)
|
|
|
|
temp.execute = temp.read = temp.write = true;
|
|
|
|
else if (MBI.Protect & PAGE_EXECUTE_WRITECOPY)
|
|
|
|
temp.execute = temp.read = temp.write = true;
|
|
|
|
else if (MBI.Protect & PAGE_READONLY)
|
|
|
|
temp.read = true;
|
|
|
|
else if (MBI.Protect & PAGE_READWRITE)
|
|
|
|
temp.read = temp.write = true;
|
|
|
|
else if (MBI.Protect & PAGE_WRITECOPY)
|
|
|
|
temp.read = temp.write = true;
|
|
|
|
|
|
|
|
// Merge areas with the same properties
|
|
|
|
if (!ranges.empty() && LastAllocationBase == MBI.AllocationBase)
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
2012-06-19 08:41:18 -06:00
|
|
|
auto &last = ranges.back();
|
|
|
|
|
|
|
|
if (last.end == temp.start &&
|
|
|
|
last.valid == temp.valid && last.execute == temp.execute &&
|
|
|
|
last.read == temp.read && last.write == temp.write)
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
2012-06-19 08:41:18 -06:00
|
|
|
last.end = temp.end;
|
|
|
|
continue;
|
2011-02-24 13:34:51 -07:00
|
|
|
}
|
2012-06-19 08:41:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
// Find the mapped file name
|
|
|
|
if (GetMappedFileName(d->my_handle, temp.start, temp.name, 1024))
|
|
|
|
{
|
|
|
|
int vsize = strlen(temp.name);
|
|
|
|
|
|
|
|
// Translate NT name to DOS name
|
|
|
|
for (auto it = dosDrives.begin(); it != dosDrives.end(); ++it)
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
2012-06-19 08:41:18 -06:00
|
|
|
int ksize = it->first.size();
|
|
|
|
if (strncmp(temp.name, it->first.data(), ksize) != 0)
|
2011-02-24 13:34:51 -07:00
|
|
|
continue;
|
2012-06-19 08:41:18 -06:00
|
|
|
|
|
|
|
memcpy(temp.name, it->second.data(), it->second.size());
|
|
|
|
memmove(temp.name + it->second.size(), temp.name + ksize, vsize + 1 - ksize);
|
|
|
|
break;
|
2011-02-24 13:34:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2012-06-19 08:41:18 -06:00
|
|
|
temp.name[0] = 0;
|
|
|
|
#else
|
|
|
|
// Find the executable name
|
|
|
|
char *base = (char*)MBI.AllocationBase;
|
|
|
|
|
|
|
|
if(nameMap.count(base))
|
|
|
|
{
|
|
|
|
strncpy(temp.name, nameMap[base].c_str(), 1023);
|
|
|
|
}
|
|
|
|
else if(GetModuleBaseName(d->my_handle, (HMODULE)base, temp.name, 1024))
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
2012-06-19 08:41:18 -06:00
|
|
|
std::string nm(temp.name);
|
|
|
|
|
|
|
|
nameMap[base] = nm;
|
|
|
|
|
2011-02-24 13:34:51 -07:00
|
|
|
// this is our executable! (could be generalized to pull segments from libs, but whatever)
|
2012-06-19 08:41:18 -06:00
|
|
|
if(d->base == base)
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
2011-06-14 08:13:28 -06:00
|
|
|
for(int i = 0; i < d->pe_header.FileHeader.NumberOfSections; i++)
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
2012-06-19 08:41:18 -06:00
|
|
|
/*char sectionName[9];
|
2011-06-14 08:13:28 -06:00
|
|
|
memcpy(sectionName,d->sections[i].Name,8);
|
2011-02-24 13:34:51 -07:00
|
|
|
sectionName[8] = 0;
|
|
|
|
string nm;
|
|
|
|
nm.append(temp.name);
|
|
|
|
nm.append(" : ");
|
2012-06-19 08:41:18 -06:00
|
|
|
nm.append(sectionName);*/
|
|
|
|
nameMap[base + d->sections[i].VirtualAddress] = nm;
|
2011-02-24 13:34:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-19 08:41:18 -06:00
|
|
|
else
|
|
|
|
temp.name[0] = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Push the entry
|
|
|
|
LastAllocationBase = MBI.AllocationBase;
|
2011-02-24 13:34:51 -07:00
|
|
|
ranges.push_back(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-11 04:49:01 -07:00
|
|
|
uintptr_t Process::getBase()
|
2011-07-16 15:00:28 -06:00
|
|
|
{
|
|
|
|
if(d)
|
2012-11-11 04:49:01 -07:00
|
|
|
return (uintptr_t) d->base;
|
2016-07-27 18:10:03 -06:00
|
|
|
return DEFAULT_BASE_ADDR; // Memory.h
|
2011-07-16 15:00:28 -06:00
|
|
|
}
|
|
|
|
|
2012-11-11 04:49:01 -07:00
|
|
|
int Process::adjustOffset(int offset, bool to_file)
|
|
|
|
{
|
|
|
|
if (!d)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for(int i = 0; i < d->pe_header.FileHeader.NumberOfSections; i++)
|
|
|
|
{
|
|
|
|
auto §ion = d->sections[i];
|
|
|
|
|
|
|
|
if (to_file)
|
|
|
|
{
|
|
|
|
unsigned delta = offset - section.VirtualAddress;
|
|
|
|
if (delta >= section.Misc.VirtualSize)
|
|
|
|
continue;
|
|
|
|
if (!section.PointerToRawData || delta >= section.SizeOfRawData)
|
|
|
|
return -1;
|
|
|
|
return (int)(section.PointerToRawData + delta);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned delta = offset - section.PointerToRawData;
|
|
|
|
if (!section.PointerToRawData || delta >= section.SizeOfRawData)
|
|
|
|
continue;
|
|
|
|
if (delta >= section.Misc.VirtualSize)
|
|
|
|
return -1;
|
|
|
|
return (int)(section.VirtualAddress + delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-09 08:05:40 -06:00
|
|
|
string Process::doReadClassName (void * vptr)
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
2016-07-27 18:21:57 -06:00
|
|
|
char * rtti = readPtr((char *)vptr - sizeof(void*));
|
|
|
|
#ifdef DFHACK64
|
2017-01-21 14:16:06 -07:00
|
|
|
void *base;
|
|
|
|
if (!RtlPcToFileHeader(rtti, &base))
|
|
|
|
return "dummy";
|
|
|
|
char * typeinfo = (char *)base + readDWord(rtti + 0xC);
|
2016-07-27 18:21:57 -06:00
|
|
|
string raw = readCString(typeinfo + 0x10+4); // skips the .?AV
|
|
|
|
#else
|
2012-01-05 15:39:14 -07:00
|
|
|
char * typeinfo = readPtr(rtti + 0xC);
|
2011-06-14 08:13:28 -06:00
|
|
|
string raw = readCString(typeinfo + 0xC); // skips the .?AV
|
2016-07-27 18:21:57 -06:00
|
|
|
#endif
|
|
|
|
if (!raw.length())
|
|
|
|
return "dummy";
|
2011-06-14 08:13:28 -06:00
|
|
|
raw.resize(raw.length() - 2);// trim @@ from end
|
|
|
|
return raw;
|
2011-02-24 13:34:51 -07:00
|
|
|
}
|
|
|
|
|
2012-09-05 09:45:45 -06:00
|
|
|
uint32_t Process::getTickCount()
|
|
|
|
{
|
|
|
|
return GetTickCount();
|
|
|
|
}
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
string Process::getPath()
|
2011-02-24 13:34:51 -07:00
|
|
|
{
|
|
|
|
HMODULE hmod;
|
|
|
|
DWORD junk;
|
|
|
|
char String[255];
|
2011-06-14 08:13:28 -06:00
|
|
|
EnumProcessModules(d->my_handle, &hmod, 1 * sizeof(HMODULE), &junk); //get the module from the handle
|
|
|
|
GetModuleFileNameEx(d->my_handle,hmod,String,sizeof(String)); //get the filename from the module
|
2011-02-24 13:34:51 -07:00
|
|
|
string out(String);
|
|
|
|
return(out.substr(0,out.find_last_of("\\")));
|
|
|
|
}
|
2011-07-27 16:00:12 -06:00
|
|
|
|
|
|
|
bool Process::setPermisions(const t_memrange & range,const t_memrange &trgrange)
|
|
|
|
{
|
2012-02-25 14:59:32 -07:00
|
|
|
DWORD newprotect=0;
|
|
|
|
if(trgrange.read && !trgrange.write && !trgrange.execute)newprotect=PAGE_READONLY;
|
|
|
|
if(trgrange.read && trgrange.write && !trgrange.execute)newprotect=PAGE_READWRITE;
|
|
|
|
if(!trgrange.read && !trgrange.write && trgrange.execute)newprotect=PAGE_EXECUTE;
|
|
|
|
if(trgrange.read && !trgrange.write && trgrange.execute)newprotect=PAGE_EXECUTE_READ;
|
|
|
|
if(trgrange.read && trgrange.write && trgrange.execute)newprotect=PAGE_EXECUTE_READWRITE;
|
|
|
|
DWORD oldprotect=0;
|
|
|
|
bool result;
|
|
|
|
result=VirtualProtect((LPVOID)range.start,(char *)range.end-(char *)range.start,newprotect,&oldprotect);
|
|
|
|
|
|
|
|
return result;
|
2011-07-27 16:00:12 -06:00
|
|
|
}
|
2012-11-12 11:18:03 -07:00
|
|
|
|
|
|
|
void* Process::memAlloc(const int length)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
// returns 0 on error
|
|
|
|
ret = VirtualAlloc(0, length, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
if (!ret)
|
|
|
|
ret = (void*)-1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-16 12:49:30 -07:00
|
|
|
int Process::memDealloc(void *ptr, const int length)
|
2012-11-12 11:18:03 -07:00
|
|
|
{
|
|
|
|
// can only free the whole region at once
|
|
|
|
// vfree returns 0 on error
|
2012-11-16 12:49:30 -07:00
|
|
|
return !VirtualFree(ptr, 0, MEM_RELEASE);
|
2012-11-12 11:18:03 -07:00
|
|
|
}
|
|
|
|
|
2012-11-16 12:49:30 -07:00
|
|
|
int Process::memProtect(void *ptr, const int length, const int prot)
|
2012-11-12 11:18:03 -07:00
|
|
|
{
|
|
|
|
int prot_native = 0;
|
2012-11-16 12:49:30 -07:00
|
|
|
DWORD old_prot = 0;
|
2012-11-12 11:18:03 -07:00
|
|
|
|
|
|
|
// only support a few constant combinations
|
|
|
|
if (prot == 0)
|
|
|
|
prot_native = PAGE_NOACCESS;
|
|
|
|
else if (prot == Process::MemProt::READ)
|
|
|
|
prot_native = PAGE_READONLY;
|
2012-11-16 12:49:30 -07:00
|
|
|
else if (prot == (Process::MemProt::READ | Process::MemProt::WRITE))
|
2012-11-12 11:18:03 -07:00
|
|
|
prot_native = PAGE_READWRITE;
|
2012-11-16 12:49:30 -07:00
|
|
|
else if (prot == (Process::MemProt::READ | Process::MemProt::WRITE | Process::MemProt::EXEC))
|
2012-11-12 11:18:03 -07:00
|
|
|
prot_native = PAGE_EXECUTE_READWRITE;
|
2012-11-16 12:49:30 -07:00
|
|
|
else if (prot == (Process::MemProt::READ | Process::MemProt::EXEC))
|
2012-11-12 11:18:03 -07:00
|
|
|
prot_native = PAGE_EXECUTE_READ;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return !VirtualProtect(ptr, length, prot_native, &old_prot);
|
|
|
|
}
|