diff options
Diffstat (limited to '')
-rw-r--r-- | bruiser/bruiser.cpp | 18 | ||||
-rw-r--r-- | bruiser/bruiser.h | 3 | ||||
-rw-r--r-- | 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<void*, size_t> 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<mem_func func> 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 <iostream> +#include <functional> #include <tuple> #include <vector> #include <cstdint> #include <cstdarg> #include <cstring> +#include <stdarg.h> #include <sys/mman.h> #include <unistd.h> /**********************************************************************************************************************/ @@ -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<typename T> + T xobjcaster(void* ptr, T v) {return v;} + template<typename T, typename... Args> + T xobjcaster(void* ptr, T first, Args... args) {/*return (first(*)(args...))xobjcaster(ptr);*/} + constexpr int MEMORY_SIZE = 32768; std::vector<uint8_t> memory(MEMORY_SIZE, 0); @@ -58,7 +69,7 @@ namespace { // start of anonymous namespace return 0; } - inline void argInjector(lua_State* __ls) { + inline std::vector<uint8_t> 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<uint8_t> arg_emitter(std::vector<uint8_t> _args) {} + std::vector<uint8_t> arg_emitter(std::vector<uint8_t> _args) { + std::vector<uint8_t> 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<int(lua_State*)> __eph) {ephs.push_back(__eph);} + private: std::vector<std::pair<void*, size_t>> obj_mem_ptrs; std::vector<std::vector<uint8_t>> objs; std::vector<std::string> names; std::vector<XObject> xobjs; + std::vector<void*> xvoidptrs; + std::vector<std::function<int(lua_State*)>> 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<m_func func> +int LuaDispatch2(lua_State* __ls) +{ + EphemeralFunc* LWPtr = *static_cast<EphemeralFunc**>(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 /**********************************************************************************************************************/ |