From bc23cc9eca8a0f6053f0ef7a6087dfbb7a764df8 Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 28 Jul 2011 01:00:12 +0300 Subject: [PATCH] Made Process::setPermisions functions, to set memory page's permisions --- library/Process-linux.cpp | 13 +++++++++++++ library/Process-windows.cpp | 15 +++++++++++++++ library/include/dfhack/Process.h | 3 +++ 3 files changed, 31 insertions(+) diff --git a/library/Process-linux.cpp b/library/Process-linux.cpp index 23d8baf12..937bd155b 100644 --- a/library/Process-linux.cpp +++ b/library/Process-linux.cpp @@ -25,6 +25,7 @@ distribution. #include "Internal.h" #include #include +#include #include #include @@ -176,4 +177,16 @@ string Process::getPath() int Process::getPID() { return getpid(); +} + +bool Process::setPermisions(const t_memrange & range,const t_memrange &trgrange) +{ + int result; + int protect=0; + if(trgrange.read)protect|=PROT_READ; + if(trgrange.write)protect|=PROT_WRITE; + if(trgrange.execute)protect|=PROT_EXECUTE; + result=mprotect((void *)range.start, range.end-range.start,protect); + + return result==0; } \ No newline at end of file diff --git a/library/Process-windows.cpp b/library/Process-windows.cpp index 2edfc6325..88b77891e 100644 --- a/library/Process-windows.cpp +++ b/library/Process-windows.cpp @@ -356,3 +356,18 @@ string Process::getPath() string out(String); return(out.substr(0,out.find_last_of("\\"))); } + +bool Process::setPermisions(const t_memrange & range,const t_memrange &trgrange) +{ + 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,range.end-range.start,newprotect,&oldprotect); + + return result; +} diff --git a/library/include/dfhack/Process.h b/library/include/dfhack/Process.h index d2ca7e355..df681c475 100644 --- a/library/include/dfhack/Process.h +++ b/library/include/dfhack/Process.h @@ -269,6 +269,9 @@ namespace DFHack int getPID(); /// get the DF Process FilePath std::string getPath(); + + /// modify permisions of memory range + bool setPermisions(const t_memrange & range,const t_memrange &trgrange); private: VersionInfo * my_descriptor; PlatformSpecific *d;