From 42a6aea4c06b2e03f489ee1d9419da5e8386792f Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Sun, 5 Nov 2017 21:41:02 +0330 Subject: now you can run load.py from bruiser. --- bruiser/bruiser-extra.h | 1 + bruiser/bruiser.cpp | 127 +++++++++++++++++++++++++++++++++++++++++- bruiser/bruiser.h | 3 +- bruiser/compile_commands.json | 8 +-- bruiser/makefile | 6 +- 5 files changed, 137 insertions(+), 8 deletions(-) (limited to 'bruiser') diff --git a/bruiser/bruiser-extra.h b/bruiser/bruiser-extra.h index b6f7158..40af99f 100644 --- a/bruiser/bruiser-extra.h +++ b/bruiser/bruiser-extra.h @@ -121,6 +121,7 @@ std::vector LUA_FUNCS = "changedirectory", "yolo", "pwd()", + "objload", "_G", "_VERSION", "assert", diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index 4549940..309e41d 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -54,6 +54,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* /*other*/ #include "linenoise/linenoise.h" #include "lua-5.3.4/src/lua.hpp" +#include #include "luadummy.h" /**********************************************************************************************************************/ @@ -201,6 +202,80 @@ class LuaEngine lua_State* LS; }; /**********************************************************************************************************************/ +class PyExec { + public: + PyExec(std::string __py_script_name, std::string __py_func_name, std::string __obj_path ) : + py_script_name(__py_script_name), py_func_name(__py_func_name), obj_path(__obj_path) {} + + int run(void) { + //std::wstring py_sys_path = L"../bfd"; + Py_Initialize(); + int argc = 2; + //std::wstring argv[2]; + wchar_t* argv[2]; + argv[0] = Py_DecodeLocale((char*)py_script_name.c_str(), 0); + argv[1] = Py_DecodeLocale((char*)obj_path.c_str(), 0); + PySys_SetArgv(argc, argv); + pName = PyUnicode_DecodeFSDefault(py_script_name.c_str()); + PyRun_SimpleString("import sys\nsys.path.append(\"../bfd\")\n"); + pModule = PyImport_Import(pName); + Py_DECREF(pName); + + if (pModule != nullptr) { + pFunc = PyObject_GetAttrString(pModule, py_func_name.c_str()); + if (pFunc && PyCallable_Check(pFunc)) { + std::cout << GREEN << "function is callable." << NORMAL << "\n"; + pArgs = PyTuple_New(1); + pValue = PyUnicode_FromString(obj_path.c_str()); + PyTuple_SetItem(pArgs, 0, pValue); + pArgs = nullptr; + std::cout << BLUE << "calling python function..." << NORMAL << "\n"; + pValue = PyObject_CallObject(pFunc, pArgs); + std::cout << BLUE << "i made it here" << NORMAL << "\n"; + //Py_DECREF(pArgs); + if (pValue != nullptr) { + std::cout << GREEN << "call finished successfully." << NORMAL << "\n"; + printf("Result of call: %ld\n", PyLong_AsLong(pValue)); + Py_DECREF(pValue); + } else { + Py_DECREF(pFunc); + Py_DECREF(pModule); + PyErr_Print(); + std::cout << RED << "call failed." << NORMAL << "\n"; + fprintf(stderr, "Call failed\n"); + return EXIT_FAILURE; + } + } + else { + if (PyErr_Occurred()) PyErr_Print(); + fprintf(stderr, "Cannot find function\"%s\"\n", py_func_name.c_str()); + } + Py_XDECREF(pFunc); + Py_DECREF(pModule); + } + else { + PyErr_Print(); + fprintf(stderr, "Failed to load \"%ls\"\n", argv[0]); + return 1; + } + Py_Finalize(); + return 0; + } + + private: + std::string py_script_name; + std::string py_func_name; + std::string obj_path; + PyObject *pName; + PyObject *pModule; + PyObject *pDict; + PyObject *pFunc; + PyObject *pArgs; + PyObject *pValue; + int argc; + char** argv; +}; +/**********************************************************************************************************************/ class CompilationDatabaseProcessor { public: @@ -1040,12 +1115,61 @@ class LuaWrapper } /*clear the screen*/ - int BruiserLuaClear(lua_State* _ls) + int BruiserLuaClear(lua_State* __ls) { linenoiseClearScreen(); return 0; } + int BruiserPyLoader(lua_State* __ls ) { + int numargs = lua_gettop(__ls); + //std::string filename = "../bfd/load.py"; + std::string filename = "load"; + std::string funcname; + std::string objjpath; + + if (numargs == 2) { + std::cout << CYAN << "got args." << NORMAL << "\n"; + funcname = lua_tostring(__ls, 1); + objjpath = lua_tostring(__ls, 2); + } + else { + std::cout << RED << "wrong number of arguments provided. should give the python script name, python func name and its args.\n" << NORMAL; + return EXIT_FAILURE; + } + + std::cout << CYAN << "initing the py embed class...\n" << NORMAL; + PyExec py(filename.c_str(), funcname.c_str(), objjpath.c_str()); + + std::cout << CYAN << "forking python script...\n" << NORMAL; + pid_t pid = fork(); + + if (pid < 0) + { + PRINT_WITH_COLOR_LB(RED, "could not fork..."); + lua_pushnumber(__ls, EXIT_FAILURE); + } + + if (pid == 0) + { + std::cout << BLUE << "running load.py: " << NORMAL << "\n"; + py.run(); + lua_pushnumber(__ls, 0); + exit(EXIT_SUCCESS); + } + + if (pid > 0) + { + int status; + pid_t returned; + returned = waitpid(pid, &status, 0); + lua_pushnumber(__ls, returned); + } + + lua_pushnumber(__ls, 0); + return 1; + } + /*read the m0 report*/ int BruiserLuaM0(lua_State* __ls) { @@ -1631,6 +1755,7 @@ int main(int argc, const char **argv) lua_register(LE.GetLuaState(), "changedirectory", &LuaDispatch<&LuaWrapper::BruiserLuaChangeDirectory>); lua_register(LE.GetLuaState(), "yolo", &LuaDispatch<&LuaWrapper::BruiserLuaYolo>); lua_register(LE.GetLuaState(), "pwd", &LuaDispatch<&LuaWrapper::BruiserLuaPWD>); + lua_register(LE.GetLuaState(), "objload", &LuaDispatch<&LuaWrapper::BruiserPyLoader>); /*its just regisering the List function from LuaWrapper with X-macros.*/ #define X(__x1, __x2) lua_register(LE.GetLuaState(), #__x1, &LuaDispatch<&LuaWrapper::List##__x1>); diff --git a/bruiser/bruiser.h b/bruiser/bruiser.h index b4d4b4a..e311c7e 100644 --- a/bruiser/bruiser.h +++ b/bruiser/bruiser.h @@ -143,7 +143,8 @@ help CMDHelp[] = { {"getpaths()", "getpaths()", "gets the currently loaded paths that bruiser looks through", "none", "array of strings"}, {"getsourcefiles()", "getsourcefiles()", "gets the currently loaded source files that bruiser will look through", "none", "array of strings"}, {"changedirectory()", "changedirectory()", "changes bruiser's working directory. only use it when you know what you are doing.", "destination directory, [string]", "return value"}, - {"pwd()", "pwd()", "pwd", "", ""} + {"pwd()", "pwd()", "pwd", "", ""}, + {"objload()", "objload(\"main\", \"../bfd/test/test.so\")", "load the compiled functions into bruiser", "string", "success or failure"} }; /**********************************************************************************************************************/ /** diff --git a/bruiser/compile_commands.json b/bruiser/compile_commands.json index 6c247d1..ef1b5c5 100644 --- a/bruiser/compile_commands.json +++ b/bruiser/compile_commands.json @@ -1,6 +1,6 @@ [ { - "command": "c++ -c -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -std=c++11 -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -ffunction-sections -fdata-sections -O2 -fno-exceptions -DLLVM_BUILD_GLOBAL_ISEL -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++1z -stdlib=libstdc++ -UNDEBUG -fexceptions -I/usr/include -o bruiser.o bruiser.cpp", + "command": "c++ -c -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -std=c++11 -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -ffunction-sections -fdata-sections -O2 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++1z -stdlib=libstdc++ -UNDEBUG -fexceptions -I/usr/include -I/usr/include/python3.5m -o bruiser.o bruiser.cpp", "directory": "/home/bloodstalker/devi/hell2/bruiser", "file": "/home/bloodstalker/devi/hell2/bruiser/bruiser.cpp" }, @@ -10,17 +10,17 @@ "file": "/home/bloodstalker/devi/hell2/bruiser/linenoise/linenoise.c" }, { - "command": "c++ -c -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -std=c++11 -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -ffunction-sections -fdata-sections -O2 -fno-exceptions -DLLVM_BUILD_GLOBAL_ISEL -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++1z -stdlib=libstdc++ -UNDEBUG -fexceptions -I/usr/include -o CompletionHints.o CompletionHints.cpp", + "command": "c++ -c -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -std=c++11 -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -ffunction-sections -fdata-sections -O2 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++1z -stdlib=libstdc++ -UNDEBUG -fexceptions -I/usr/include -I/usr/include/python3.5m -o CompletionHints.o CompletionHints.cpp", "directory": "/home/bloodstalker/devi/hell2/bruiser", "file": "/home/bloodstalker/devi/hell2/bruiser/CompletionHints.cpp" }, { - "command": "c++ -c -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -std=c++11 -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -ffunction-sections -fdata-sections -O2 -fno-exceptions -DLLVM_BUILD_GLOBAL_ISEL -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++1z -stdlib=libstdc++ -UNDEBUG -fexceptions -I/usr/include -o mutagen.o mutagen.cpp", + "command": "c++ -c -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -std=c++11 -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -ffunction-sections -fdata-sections -O2 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++1z -stdlib=libstdc++ -UNDEBUG -fexceptions -I/usr/include -I/usr/include/python3.5m -o mutagen.o mutagen.cpp", "directory": "/home/bloodstalker/devi/hell2/bruiser", "file": "/home/bloodstalker/devi/hell2/bruiser/mutagen.cpp" }, { - "command": "c++ -c -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -std=c++11 -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -ffunction-sections -fdata-sections -O2 -fno-exceptions -DLLVM_BUILD_GLOBAL_ISEL -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++1z -stdlib=libstdc++ -UNDEBUG -fexceptions -I/usr/include -o ORCmutation.o ORCmutation.cpp", + "command": "c++ -c -I/home/bloodstalker/extra/llvm-clang-4/llvm/include -I/home/bloodstalker/extra/llvm-clang-4/build/include -fPIC -fvisibility-inlines-hidden -Werror=date-time -std=c++11 -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -ffunction-sections -fdata-sections -O2 -fno-exceptions -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/bloodstalker/extra/llvm-clang-4/llvm/tools/clang/include -I/home/bloodstalker/extra/llvm-clang-4/build/tools/clang/include -std=c++1z -stdlib=libstdc++ -UNDEBUG -fexceptions -I/usr/include -I/usr/include/python3.5m -o ORCmutation.o ORCmutation.cpp", "directory": "/home/bloodstalker/devi/hell2/bruiser", "file": "/home/bloodstalker/devi/hell2/bruiser/ORCmutation.cpp" }, diff --git a/bruiser/makefile b/bruiser/makefile index 278da47..6fe4c60 100644 --- a/bruiser/makefile +++ b/bruiser/makefile @@ -3,7 +3,7 @@ include ../macros.mk #######################################VARS#################################### -CXX_FLAGS+=-I/usr/include +CXX_FLAGS+=-I/usr/include -I/usr/include/python3.5m BRUISER=bruiser LUA?=JIT LIB_LUA=./lua-5.3.4/src/liblua.a @@ -11,7 +11,8 @@ LIB_LUA_JIT=./LuaJIT/src/libluajit.a HEADER_LIST=bruiser.h bruiser-extra.h CompletionHints.h SRCS=bruiser.cpp, CompletionHints.cpp, ORCmutation.cpp, mutagen.cpp #for some reason without ld the build fails on ubuntu trusty on travis -EXTRA_LD_FLAGS+=-ldl +#EXTRA_LD_FLAGS+=-lpthread -ldl -lutil -lm -Xlinker -lpython3 +EXTRA_LD_FLAGS+=$(shell python3-config --ldflags) ######################################RULES#################################### .DEFAULT: all @@ -56,4 +57,5 @@ help: @echo 'there is help.' @echo 'all is the defualt target.' @echo 'clean runs clean.' + @echo 'deepclean will also clean the lua build' @echo 'for a more complete and detaild list of BUILD_MODE and other things look at the main makefiles help under project root.' -- cgit v1.2.3