Another dfusion nuking (not much left :) )

develop
Warmist 2012-09-25 11:30:38 +03:00
parent 0bee8c360e
commit ddc83a0a72
11 changed files with 5 additions and 636 deletions

@ -14,10 +14,8 @@
#include "lua_Process.h"
#include "lua_Hexsearch.h"
#include "lua_Misc.h"
#include "lua_VersionInfo.h"
#include "functioncall.h"
#include "lua_FunctionCall.h"
#include "lua_Offsets.h"
#include "DataDefs.h"
#include "LuaTools.h"
@ -43,15 +41,12 @@ DFhackCExport command_result plugin_init (color_ostream &out, std::vector <Plugi
{
lua::state st=lua::glua::Get();
//maybe remake it to run automaticaly
//maybe remake it to run automatically
Lua::Open(out, st);
lua::RegisterProcess(st);
lua::RegisterHexsearch(st);
lua::RegisterMisc(st);
lua::RegisterVersionInfo(st);
lua::RegisterFunctionCall(st);
lua::RegisterEngine(st);
#ifdef LINUX_BUILD
st.push(1);

@ -1,26 +0,0 @@
#ifndef FUNCTIONCALL__H
#define FUNCTIONCALL__H
#include <vector>
using std::vector;
using std::size_t;
class FunctionCaller
{
public:
enum callconv
{
STD_CALL, //__stdcall - all in stack
FAST_CALL, //__fastcall - as much in registers as fits
THIS_CALL, //__thiscall - eax ptr to this, rest in stack
CDECL_CALL //__cdecl - same as stdcall but no stack realign
};
FunctionCaller(size_t base):base_(base){};
int CallFunction(size_t func_ptr,callconv conv,const vector<int> &arguments);
private:
int CallF(size_t count,callconv conv,void* f,const vector<int> &arguments);
size_t base_;
};
#endif //FUNCTIONCALL__H

@ -1,14 +0,0 @@
#ifndef LUA_FUNCTIONCALL__H
#define LUA_FUNCTIONCALL__H
#include "luamain.h"
#include "functioncall.h"
namespace lua
{
void RegisterFunctionCall(lua::state &st);
}
#endif

@ -7,7 +7,6 @@
#include <MemAccess.h>
#include "luamain.h"
#include "OutFile.h"
#include "functioncall.h"
#include "LuaTools.h"
namespace lua

@ -1,13 +0,0 @@
#ifndef LUA_OFFSETS_H
#define LUA_OFFSETS_H
#include "luamain.h"
namespace lua
{
void RegisterEngine(lua::state &st);
}
#endif

@ -1,12 +0,0 @@
#ifndef LUA_VERSIONINFO_H
#define LUA_VERSIONINFO_H
#include "Core.h"
#include <VersionInfo.h>
#include "luamain.h"
namespace lua
{
void RegisterVersionInfo(lua::state &st);
}
#endif

@ -1,121 +0,0 @@
#include "functioncall.h"
#ifdef LINUX_BUILD
#define __F_TDEF(CONV,CONV_,tag) typedef int(__attribute__ ( (CONV_) ) *F_TYPE##CONV##tag)
#else
#define __F_TDEF(CONV,CONV_,tag) typedef int(__ ## CONV_ *F_TYPE##CONV##tag)
#endif
#define __F_T(CONV,tag) F_TYPE##CONV##tag
#define __F_TYPEDEFS(CONV,CONV_) __F_TDEF(CONV,CONV_,1)(int);\
__F_TDEF(CONV,CONV_,2)(int,int);\
__F_TDEF(CONV,CONV_,3)(int,int,int);\
__F_TDEF(CONV,CONV_,4)(int,int,int,int);\
__F_TDEF(CONV,CONV_,5)(int,int,int,int,int);\
__F_TDEF(CONV,CONV_,6)(int,int,int,int,int,int);\
__F_TDEF(CONV,CONV_,7)(int,int,int,int,int,int,int)
#define __FCALL(CONV,CONV_) if(conv==CONV)\
{ \
if(count==1)\
ret= (reinterpret_cast<__F_T(CONV,1)>(f))\
(arguments[0]);\
else if(count==2)\
ret= (reinterpret_cast<__F_T(CONV,2)>(f))\
(arguments[0],arguments[1]);\
else if(count==3)\
ret= (reinterpret_cast<__F_T(CONV,3)>(f))\
(arguments[0],arguments[1],arguments[2]);\
else if(count==4)\
ret= (reinterpret_cast<__F_T(CONV,4)>(f))\
(arguments[0],arguments[1],arguments[2],arguments[3]);\
else if(count==5)\
ret= (reinterpret_cast<__F_T(CONV,5)>(f))\
(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);\
else if(count==6)\
ret= (reinterpret_cast<__F_T(CONV,6)>(f))\
(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4],arguments[5]);\
else if(count==7)\
ret= (reinterpret_cast<__F_T(CONV,7)>(f))\
(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4],arguments[5],arguments[6]);\
}
#define __FCALLEX(CONV,CONV_) if(conv==CONV)\
{ if(count==1) {__F_T(CONV,1) tmp_F=reinterpret_cast<__F_T(CONV,1)>(f); return tmp_F(arguments[0]);}\
else if(count==2){__F_T(CONV,2) tmp_F=reinterpret_cast<__F_T(CONV,2)>(f); return tmp_F(arguments[0],arguments[1]);}\
else if(count==3){__F_T(CONV,3) tmp_F=reinterpret_cast<__F_T(CONV,3)>(f); return tmp_F(arguments[0],arguments[1],arguments[2]);}\
else if(count==4){__F_T(CONV,4) tmp_F=reinterpret_cast<__F_T(CONV,4)>(f); return tmp_F(arguments[0],arguments[1],arguments[2],arguments[3]);}\
else if(count==5){__F_T(CONV,5) tmp_F=reinterpret_cast<__F_T(CONV,5)>(f); return tmp_F(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4]);}\
else if(count==6){__F_T(CONV,6) tmp_F=reinterpret_cast<__F_T(CONV,6)>(f); return tmp_F(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4],arguments[5]);}\
else if(count==7){__F_T(CONV,7) tmp_F=reinterpret_cast<__F_T(CONV,7)>(f); return tmp_F(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4],arguments[5],arguments[6]);}\
}
/*else if(count==8)\
ret= (reinterpret_cast<__F_T(CONV,8)>(f))\
(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4],arguments[5],arguments[6],arguments[7]);\
else if(count==9)\
ret= (reinterpret_cast<int (CONV_*)(int,int,int,int,int\
,int,int,int,int)>(f))\
(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4],arguments[5],arguments[6],arguments[7],arguments[8]);\
else if(count==10)\
ret= (reinterpret_cast<int (CONV_*)(int,int,int,int,int\
,int,int,int,int,int)>(f))\
(arguments[0],arguments[1],arguments[2],arguments[3],arguments[4],arguments[5],arguments[6],arguments[7],arguments[8],arguments[9]);}*/
/*int FunctionCaller::CallF(size_t count,callconv conv,void* f,const vector<int> &arguments)//more complex but not more error safe
{
__F_TYPEDEFS(STD_CALL,__stdcall);
__F_TYPEDEFS(FAST_CALL,__fastcall);
__F_TYPEDEFS(THIS_CALL,__thiscall);
__F_TYPEDEFS(CDECL_CALL,__cdecl);
{
__FCALLEX(STD_CALL,__stdcall);
__FCALLEX(FAST_CALL,__fastcall);
__FCALLEX(THIS_CALL,__thiscall);
__FCALLEX(CDECL_CALL,__cdecl);
}
}*/
int FunctionCaller::CallFunction(size_t func_ptr,callconv conv,const vector<int> &arguments)
{
//nasty nasty code...
#ifdef LINUX_BUILD //quick fix
if(conv==THIS_CALL)
conv=STD_CALL;
#endif
void* f= reinterpret_cast<void*>(func_ptr+base_);
size_t count=arguments.size();
if(count==0)
return (reinterpret_cast<int (*)()>(f))(); //does not matter how we call it...
int ret=0;
//typedefs
__F_TYPEDEFS(STD_CALL,stdcall);
__F_TYPEDEFS(FAST_CALL,fastcall);
__F_TYPEDEFS(THIS_CALL,thiscall);
__F_TYPEDEFS(CDECL_CALL,cdecl);
//calls
__FCALL(STD_CALL,stdcall);
__FCALL(FAST_CALL,fastcall);
__FCALL(THIS_CALL,thiscall);
__FCALL(CDECL_CALL,cdecl);
return -1; //incorect type. Should probably throw...
//return CallF(count,conv,f,arguments);
/*//testing part{ worked some time ago..., put where DFHack::Core is accesible
c->Suspend();
FunctionCaller caller(c->p->getBase());
std::vector <int> args;
args.push_back((size_t)"Hello world");
args.push_back(4);
args.push_back(4);
args.push_back(0);
dfprint mprint=(dfprint)(0x27F030+c->p->getBase());
mprint("Hello world",4,4,0);
//caller.CallFunction((0x27F030),FunctionCaller::THIS_CALL,args);
c->Resume();
return CR_OK;
//}end testing*/
}
#undef __FCALL
#undef __FCALLEX
#undef __F_TYPEDEFS
#undef __F_T

@ -1,39 +0,0 @@
#include "lua_FunctionCall.h"
using namespace lua;
int lua_FunctionCall(lua_State *L)
{
lua::state st(L);
FunctionCaller cl(0);
size_t ptr=st.as<size_t>(1);
int callconv=st.as<size_t>(2);
vector<int> arguments;
for(int i=3;i<st.gettop();i++)
arguments.push_back(st.as<int>(i));
int ret=cl.CallFunction(ptr,(FunctionCaller::callconv)callconv,arguments);
st.push(ret);
return 1;// dunno if len is needed...
}
const luaL_Reg lua_functioncall_func[]=
{
{"call",lua_FunctionCall},
{NULL,NULL}
};
#define __ADDCONST(name) st.push(::FunctionCaller:: name); st.setglobal(#name)
void lua::RegisterFunctionCall(lua::state &st)
{
st.getglobal("FunctionCall");
if(st.is<lua::nil>())
{
st.pop();
st.newtable();
}
__ADDCONST(STD_CALL);
__ADDCONST(FAST_CALL);
__ADDCONST(THIS_CALL);
__ADDCONST(CDECL_CALL);
lua::RegFunctionsLocal(st, lua_functioncall_func);
st.setglobal("FunctionCall");
}
#undef __ADDCONST

@ -129,111 +129,16 @@ static int GetMod(lua_State *L)
st.push(pos);
return 1;
}
static int lua_malloc(lua_State *L)
{
lua::state st(L);
size_t size=st.as<size_t>(1);
size_t pos=reinterpret_cast<size_t>(malloc(size));
st.push(pos);
return 1;
}
static int lua_malloc_free(lua_State *L)
{
lua::state st(L);
size_t ptr=st.as<size_t>(1);
free(reinterpret_cast<void*>(ptr));
return 0;
}
#ifdef LINUX_BUILD
static size_t __attribute__((stdcall)) PushValue(size_t ret,uint32_t eax,uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t edi,uint32_t esi,uint32_t esp,uint32_t ebp)
#else
static size_t __stdcall PushValue(size_t ret,uint32_t eax,uint32_t ebx,uint32_t ecx,uint32_t edx,uint32_t edi,uint32_t esi,uint32_t esp,uint32_t ebp)
#endif
{
lua::state st=lua::glua::Get();
st.getglobal("OnFunction");
if(st.is<lua::nil>())
return 0;
st.newtable();
st.push(eax);
st.setfield("eax");
st.push(ebx);
st.setfield("ebx");
st.push(ecx);
st.setfield("ecx");
st.push(edx);
st.setfield("edx");
st.push(edi);
st.setfield("edi");
st.push(esi);
st.setfield("esi");
st.push(esp+12);
st.setfield("esp");
st.push(ebp);
st.setfield("ebp");
st.push(ret);
st.setfield("ret");
DFHack::Lua::SafeCall(DFHack::Core::getInstance().getConsole(),st,1,1);
return st.as<uint32_t>();
}
static int Get_PushValue(lua_State *L)
{
lua::state st(L);
st.push((uint32_t)&PushValue);
return 1;
}
static int Call_Df(lua_State *L)
{
lua::state st(L);
FunctionCaller f(0);
std::vector<int> args;
size_t ptr;
FunctionCaller::callconv conv;
ptr=st.as<size_t>(1);
conv=(FunctionCaller::callconv)st.as<size_t>(2);
for(size_t j=3;j<=st.gettop();j++)
args.push_back(st.as<int>(j));
st.push(f.CallFunction(ptr,conv,args));
return 1;
}
static int Suspend_Df(lua_State *L)
{
lua::state st(L);
DFHack::Core::getInstance().Suspend();
return 0;
}
static int Resume_Df(lua_State *L)
{
lua::state st(L);
DFHack::Core::getInstance().Resume();
return 0;
}
static int Cast(lua_State *L)
{
lua::state st(L);
if(DFHack::Lua::IsDFObject(st,1)!=DFHack::Lua::OBJ_TYPE)
st.error("First argument must be df type!");
if(!st.is<lua::number>(2)) //todo maybe lightuserdata?
st.error("Second argument must be pointer as a number!");
st.getfield("_identity",1);
DFHack::Lua::PushDFObject(st,(DFHack::type_identity*)lua_touserdata(st,-1),(void*)st.as<int>(2));
return 1;
}
const luaL_Reg lua_misc_func[]=
{
{"alloc",lua_malloc},
{"free",lua_malloc_free},
{"loadmod",LoadMod},
{"getmod",GetMod},
{"loadobj",LoadObj},
{"loadobjsymbols",LoadObjSymbols},
{"findmarker",FindMarker},
{"newmod",NewMod},
{"getpushvalue",Get_PushValue},
{"calldf",Call_Df},
{"suspend",Suspend_Df},
{"resume",Resume_Df},
{"cast",Cast},
{NULL,NULL}
};
void lua::RegisterMisc(lua::state &st)

@ -1,190 +0,0 @@
#include "lua_Offsets.h"
#include <string.h>
#include <stdint.h>
//TODO make a seperate module with peeks/pokes and page permisions (linux/windows spec)
//TODO maybe remove alltogether- use DFHack::Process instead?
template <typename T>
T engine_peek(size_t offset)
{
return *(reinterpret_cast<T*>(offset));
}
template <typename T>
void engine_poke(size_t offset,T val)
{
*(reinterpret_cast<T*>(offset))=val;
}
void peekarb(size_t offset, void *mem,size_t size)
{
memcpy(mem,(void*)offset,size);
}
void peekstr(size_t offset, char* buf, size_t maxsize)
{
strncpy(buf,(char*)offset,maxsize);
}
void pokearb(size_t offset, void *mem,size_t size)
{
memcpy((void*)offset,mem,size);
}
void pokestr(size_t offset, char* buf, size_t maxsize)
{
strncpy((char*)offset,buf,maxsize);
}
//// lua stuff here
static int lua_peekb(lua_State *L)
{
lua::state st(L);
st.push(engine_peek<uint8_t>(st.as<size_t>(1)));
return 1;
}
static int lua_peekw(lua_State *L)
{
lua::state st(L);
st.push(engine_peek<uint16_t>(st.as<size_t>(1)));
return 1;
}
static int lua_peekd(lua_State *L)
{
lua::state st(L);
st.push(engine_peek<uint32_t>(st.as<size_t>(1)));
return 1;
}
static int lua_peekq(lua_State *L)
{
lua::state st(L);
st.push(engine_peek<uint64_t>(st.as<size_t>(1)));
return 1;
}
static int lua_peekfloat(lua_State *L)
{
lua::state st(L);
st.push(engine_peek<float>(st.as<size_t>(1)));
return 1;
}
static int lua_peekdouble(lua_State *L)
{
lua::state st(L);
st.push(engine_peek<double>(st.as<size_t>(1)));
return 1;
}
static int lua_peekarb(lua_State *L)
{
lua::state st(L);
size_t size=st.as<size_t>(2);
void *p=st.newuserdata(size);
peekarb(st.as<size_t>(1),p,size);
return 1;
}
static int lua_peekstr(lua_State *L)
{
lua::state st(L);
char *buf;
buf=new char[256];
peekstr(st.as<size_t>(1),buf,256);
std::string tstr(buf);
st.push(tstr);
delete [] buf;
return 1;
}
static int lua_peekstr2(lua_State *L)
{
lua::state st(L);
st.push(engine_peek<std::string>(st.as<size_t>(1)));
return 1;
}
static int lua_pokeb(lua_State *L)
{
lua::state st(L);
engine_poke<uint8_t>(st.as<size_t>(1),st.as<uint8_t>(2));
return 0;
}
static int lua_pokew(lua_State *L)
{
lua::state st(L);
engine_poke<uint16_t>(st.as<size_t>(1),st.as<uint16_t>(2));
return 0;
}
static int lua_poked(lua_State *L)
{
lua::state st(L);
engine_poke<uint32_t>(st.as<size_t>(1),st.as<uint32_t>(2));
return 0;
}
static int lua_pokeq(lua_State *L)
{
lua::state st(L);
engine_poke<uint64_t>(st.as<size_t>(1),st.as<uint64_t>(2));
return 0;
}
static int lua_pokefloat(lua_State *L)
{
lua::state st(L);
engine_poke<float>(st.as<size_t>(1),st.as<float>(2));
return 0;
}
static int lua_pokedouble(lua_State *L)
{
lua::state st(L);
engine_poke<double>(st.as<size_t>(1),st.as<double>(2));
return 0;
}
static int lua_pokearb(lua_State *L)
{
lua::state st(L);
void *p=(void *)lua_touserdata(L, 2);//st.as<lua::userdata>(2);
size_t size=st.as<size_t>(3);
pokearb(st.as<size_t>(1),p,size);
return 0;
}
static int lua_pokestr(lua_State *L)
{
lua::state st(L);
std::string trg=st.as<std::string>(2);
pokestr(st.as<size_t>(1),(char*)trg.c_str(),trg.size());
return 0;
}
static int lua_pokestr2(lua_State *L)
{
lua::state st(L);
std::string trg=st.as<std::string>(2);
engine_poke<std::string>(st.as<size_t>(1),trg);
return 0;
}
const luaL_Reg lua_engine_func[]=
{
{"peekb",lua_peekb},
{"peekw",lua_peekw},
{"peekd",lua_peekd},
{"peekq",lua_peekq},
{"peekfloat",lua_peekfloat},
{"peekdouble",lua_peekdouble},
{"peekarb",lua_peekarb},
{"peekstr",lua_peekstr},
{"peekstr2",lua_peekstr2},
{"pokeb",lua_pokeb},
{"pokew",lua_pokew},
{"poked",lua_poked},
{"pokeq",lua_pokeq},
{"pokefloat",lua_pokefloat},
{"pokedouble",lua_pokedouble},
{"pokearb",lua_pokearb},
{"pokestr",lua_pokestr},
{"pokestr2",lua_pokestr2},
{NULL,NULL}
};
void lua::RegisterEngine(lua::state &st)
{
st.getglobal("engine");
if(st.is<lua::nil>())
{
st.pop();
st.newtable();
}
lua::RegFunctionsLocal(st,lua_engine_func);
st.setglobal("engine");
}

@ -1,115 +0,0 @@
#include "lua_VersionInfo.h"
#define VI_FUNC(name) int lua_VI_ ## name (lua_State *L){\
lua::state s(L);\
DFHack::VersionInfo* vif=DFHack::Core::getInstance().vinfo;
#define END_VI_FUNC }
VI_FUNC(getBase)
//int lua_VI_getBase(lua_State *L)
s.push(vif->getBase());
return 1;
END_VI_FUNC
VI_FUNC(setBase)
uint32_t val=s.as<uint32_t>(1);
vif->setBase(val);
return 0;
END_VI_FUNC
VI_FUNC(rebaseTo)
uint32_t val=s.as<uint32_t>(1);
vif->rebaseTo(val);
return 0;
END_VI_FUNC
VI_FUNC(addMD5)
std::string val=s.as<std::string>(1);
vif->addMD5(val);
return 0;
END_VI_FUNC
VI_FUNC(hasMD5)
std::string val=s.as<std::string>(1);
s.push(vif->hasMD5(val));
return 1;
END_VI_FUNC
VI_FUNC(addPE)
uint32_t val=s.as<uint32_t>(1);
vif->addPE(val);
return 0;
END_VI_FUNC
VI_FUNC(hasPE)
uint32_t val=s.as<uint32_t>(1);
s.push(vif->hasPE(val));
return 1;
END_VI_FUNC
VI_FUNC(setVersion)
std::string val=s.as<std::string>(1);
vif->setVersion(val);
return 0;
END_VI_FUNC
VI_FUNC(getVersion)
s.push(vif->getVersion());
return 1;
END_VI_FUNC
VI_FUNC(setAddress)
std::string key=s.as<std::string>(1);
uint32_t val=s.as<uint32_t>(2);
vif->setAddress(key,val);
return 0;
END_VI_FUNC
VI_FUNC(getAddress)
std::string key=s.as<std::string>(1);
s.push(vif->getAddress(key));
return 1;
END_VI_FUNC
VI_FUNC(setOS)
unsigned os=s.as<unsigned>(1);
vif->setOS((DFHack::OSType)os);
return 0;
END_VI_FUNC
VI_FUNC(getOS)
s.push(vif->getOS());
return 1;
END_VI_FUNC
#undef VI_FUNC
#undef END_VI_FUNC
#define VI_FUNC(name) {#name,lua_VI_ ## name}
const luaL_Reg lua_vinfo_func[]=
{
VI_FUNC(getBase),
VI_FUNC(setBase),
VI_FUNC(rebaseTo),
VI_FUNC(addMD5),
VI_FUNC(hasMD5),
VI_FUNC(addPE),
VI_FUNC(hasPE),
VI_FUNC(setVersion),
VI_FUNC(getVersion),
VI_FUNC(setAddress),
VI_FUNC(getAddress),
VI_FUNC(setOS),
VI_FUNC(getOS),
{NULL,NULL}
};
#undef VI_FUNC
void lua::RegisterVersionInfo(lua::state &st)
{
st.getglobal("VersionInfo");
if(st.is<lua::nil>())
{
st.pop();
st.newtable();
}
lua::RegFunctionsLocal(st, lua_vinfo_func);
st.setglobal("VersionInfo");
}