From f2e4de4c7d7cc7c7ccda2459114be7c6bf7dd586 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Wed, 17 Jan 2018 21:10:06 +0330 Subject: update --- bruiser/bruiser.cpp | 18 +++++--------- bruiser/bruiser.h | 3 +++ bruiser/executioner.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 14 deletions(-) diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index 75f9ace..114b0ec 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -1065,7 +1065,7 @@ class LiveActionListArrays : public ASTFrontendAction class LuaWrapper { public: - LuaWrapper(ClangTool &__CT) : CT(__CT) {} + LuaWrapper(ClangTool &__CT, Executioner& __EX) : CT(__CT), executioner(__EX) {} /*print out the history*/ int BruiserLuaHistory(lua_State* __ls) @@ -1245,15 +1245,16 @@ class LuaWrapper for (auto& iter : xobj_code_) {std::cout << RED << int(iter) << " ";} std::cout << NORMAL <<"\n"; xobj_name = lua_tostring(__ls, 2); - Executioner executioner; + //Executioner executioner; std::pair xobj = executioner.loadObjsInXMem(xobj_code_); std::cout << "xobj will be registered as " << YELLOW << xobj_name << NORMAL << ". " << "it is recommended to use a post- or pre-fix for the xobj names to avoid namespace pollution." "\n"; std::cout << GREEN << "pointer: " << BLUE << xobj.first << " " << GREEN << "size: " << BLUE << xobj.second << NORMAL << "\n"; - XObject ptr = executioner.getXobject(xobj.first); + XObject ptr = (XObject)xobj.first; ptr(); xobj_2int ptr2; ptr2 = (xobj_2int)ptr; std::cout << MAGENTA << "result: " << NORMAL << ptr2(30,20) << "\n"; + //devi_luareg(__ls, ptr2, xobj_name, executioner); return 0; } @@ -1766,18 +1767,11 @@ class LuaWrapper private: ClangTool CT; + Executioner executioner; }; /**********************************************************************************************************************/ /**********************************************************************************************************************/ typedef int (LuaWrapper::*mem_func)(lua_State* L); - -/** - * @brief A template function to wrap LuaWrapper members into somehting that lua accepts. - * - * @param __ls lua state - * - * @return returns a pointer to the member function wrapped the way lua accepts it. - */ template int LuaDispatch(lua_State* __ls) { @@ -1817,7 +1811,7 @@ int main(int argc, const char **argv) { } /*initialize the LuaWrapper class so we can register and run them from lua.*/ - LuaWrapper LW(Tool); + LuaWrapper LW(Tool, executioner); /*linenoise init*/ linenoiseSetCompletionCallback(bruiser::ShellCompletion); diff --git a/bruiser/bruiser.h b/bruiser/bruiser.h index f8d6095..0dce545 100644 --- a/bruiser/bruiser.h +++ b/bruiser/bruiser.h @@ -50,6 +50,9 @@ namespace bruiser #define JOIN2(x1, x2) x1##x2 #define JOIN3(x1, x2, x3) x1##x2##x3 +#define PTR_NVA(x1, x2) x1(*)(x2) +#define PTR_VA(x1, x2) x1(*)(x2, ...) + #define RED "\033[1;31m" #define CYAN "\033[1;36m" #define GREEN "\033[1;32m" diff --git a/bruiser/executioner.h b/bruiser/executioner.h index 26bd042..98bd08b 100644 --- a/bruiser/executioner.h +++ b/bruiser/executioner.h @@ -23,11 +23,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* #include "lua-5.3.4/src/lua.hpp" #include +#include #include #include #include #include #include +#include #include #include /**********************************************************************************************************************/ @@ -37,7 +39,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* namespace { // start of anonymous namespace using XObject = void(*)(void); using xobj_2int = int(*)(int, int); + using xobj_int = int(*)(int, ...); + using xobj_float = float(*)(float, ...); + using xobj_double = double(*)(double, ...); using LuaRegFunc = int(*)(lua_State*); + + template + T xobjcaster(void* ptr, T v) {return v;} + template + T xobjcaster(void* ptr, T first, Args... args) {/*return (first(*)(args...))xobjcaster(ptr);*/} + constexpr int MEMORY_SIZE = 32768; std::vector memory(MEMORY_SIZE, 0); @@ -58,7 +69,7 @@ namespace { // start of anonymous namespace return 0; } - inline void argInjector(lua_State* __ls) { + inline std::vector codegen(lua_State* __ls) { int numargs = lua_gettop(__ls); for (int i = 2; i <= numargs; ++i) { if (lua_type(__ls, i) == LUA_TBOOLEAN) { @@ -86,7 +97,10 @@ namespace { // start of anonymous namespace } } - std::vector arg_emitter(std::vector _args) {} + std::vector arg_emitter(std::vector _args) { + std::vector ret; + return ret; + } int LuaXobjWrapper(lua_State* __ls) { int numargs = lua_gettop(__ls); @@ -174,6 +188,7 @@ class Executioner { std::cout << "could not make vmemory executable.\n"; return std::make_pair(nullptr, 0); } + xvoidptrs.push_back(program_memory); return std::make_pair(program_memory, code_size); } @@ -211,12 +226,60 @@ class Executioner { } } + void pusheph(std::function __eph) {ephs.push_back(__eph);} + private: std::vector> obj_mem_ptrs; std::vector> objs; std::vector names; std::vector xobjs; + std::vector xvoidptrs; + std::vector> ephs; +}; +/**********************************************************************************************************************/ +/**********************************************************************************************************************/ +#if 1 +class EphemeralFunc { + public: + EphemeralFunc(xobj_2int _ptr, std::string _name) : ptr(_ptr), name(_name) {} + virtual ~EphemeralFunc() {} + + int lua_func(lua_State* __ls) { + int numargs = lua_gettop(__ls); + if (numargs != 2) { + PRINT_WITH_COLOR(RED, "expected 2 arguments..."); + lua_tonumber(__ls, 0); + return 1; + } + int arg1 = lua_tonumber(__ls, 1); + int arg2 = lua_tonumber(__ls, 1); + std::cout << RED << "right before execution..." << NORMAL << "\n"; + int result = ptr(arg1, arg2); + lua_pushnumber(__ls, result); + return 1; + } + + private: + xobj_2int ptr; + std::string name; }; + +typedef int (EphemeralFunc::*m_func)(lua_State* L); +template +int LuaDispatch2(lua_State* __ls) +{ + EphemeralFunc* LWPtr = *static_cast(lua_getextraspace(__ls)); + return ((*LWPtr).*func)(__ls); +} + +int devi_luareg(lua_State* __ls, xobj_2int __xobj, std::string __name, Executioner& __EX) { + EphemeralFunc eph(__xobj, __name); + //__EX.pusheph(eph.lua_func); + lua_register(__ls, __name.c_str(), &LuaDispatch2<&EphemeralFunc::lua_func>); + return 0; +} +#endif +/**********************************************************************************************************************/ /**********************************************************************************************************************/ #endif /**********************************************************************************************************************/ -- cgit v1.2.3