diff options
author | bloodstalker <thabogre@gmail.com> | 2018-01-12 15:06:46 +0000 |
---|---|---|
committer | bloodstalker <thabogre@gmail.com> | 2018-01-12 15:06:46 +0000 |
commit | 575615c5a41592f3701853f90e1762c6dce4abe4 (patch) | |
tree | fca52890bb597c587441db1a99a1d16c63f98694 | |
parent | update (diff) | |
download | mutator-575615c5a41592f3701853f90e1762c6dce4abe4.tar.gz mutator-575615c5a41592f3701853f90e1762c6dce4abe4.zip |
you can now run objects that are loaded into bruiser from a shared library from lua. right now very simple examples work. i have to figure out how to handles runtime pointer recasts.
-rw-r--r-- | bfd/test/test.c | 7 | ||||
-rw-r--r-- | bruiser/bruiser.cpp | 109 | ||||
-rw-r--r-- | bruiser/executioner.h | 9 | ||||
-rw-r--r-- | bruiser/lua-scripts/demo1.lua | 36 |
4 files changed, 94 insertions, 67 deletions
diff --git a/bfd/test/test.c b/bfd/test/test.c index 7b1e01c..fa9f74d 100644 --- a/bfd/test/test.c +++ b/bfd/test/test.c @@ -6,7 +6,8 @@ int myfunc3(void) {return 3;} int myfunc4(void) {return 4;} int myfunc5(void) {return 5;} int myfunc6(void) {return 6;} -int myfunc7(int a, int b) {return a + b;} +int add2(int a, int b) {return a + b;} +int sub2(int a, int b) {return a - b;} int myvar1 = 1; int myvar2 = 2; @@ -15,7 +16,7 @@ int myvar4 = 4; int main(int argc, char** argv) { int sum; - sum = myfunc7(10, 20); + sum = add2(10, 20); printf("i live!\n"); - return myfunc7(10, 20); + return sub2(20, 10); } diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index 5e55d3b..75f9ace 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -72,16 +72,14 @@ using namespace clang::tooling; #endif /**********************************************************************************************************************/ /*global vars*/ -namespace -{ +namespace { // start of anonymous namespace static llvm::cl::OptionCategory BruiserCategory("Empty"); std::vector<std::string> PushToLua; bruiser::M0_ERR m0_err [[maybe_unused]]; bruiser::BruiserReport BruiseRep; - struct ShellGlobal - { + struct ShellGlobal { ShellGlobal() = default; std::vector<std::string> PATH; @@ -91,15 +89,14 @@ namespace unsigned int HISTORY_SIZE = SHELL_HISTORY_SIZE; }; - struct ShellCache - { + struct ShellCache { std::string LastFileUsed; std::string LastFileUsedShort; }; ShellGlobal ShellGlobalInstance; ShellCache ShellCacheInstance; -} +} // end of anonymous naemspace /**********************************************************************************************************************/ cl::opt<bool> Intrusive("intrusive", cl::desc("If set true. bruiser will mutate the source."), cl::init(true), cl::cat(BruiserCategory), cl::ZeroOrMore); cl::opt<bool> CheckSystemHeader("SysHeader", cl::desc("bruiser will run through System Headers"), cl::init(false), cl::cat(BruiserCategory), cl::ZeroOrMore); @@ -325,18 +322,17 @@ class PyExec { std::vector<std::vector<uint8_t>> hexobj; }; /**********************************************************************************************************************/ -class CompilationDatabaseProcessor -{ +class XObjReliquary {}; +/**********************************************************************************************************************/ +class CompilationDatabaseProcessor { public: CompilationDatabaseProcessor(CompilationDatabase &__cdb) : CDB(__cdb) {} - void CalcMakePath(void) - { + void CalcMakePath(void) { std::vector<std::string> Paths; std::vector<CompileCommand> CCV = CDB.getAllCompileCommands(); - for(auto &iter : CCV) - { + for(auto &iter : CCV) { SourceFiles.push_back(iter.Filename); //PRINT_WITH_COLOR_LB(RED, SourceFiles.back().c_str()); } @@ -345,37 +341,22 @@ class CompilationDatabaseProcessor //PRINT_WITH_COLOR_LB(RED, MakePath.c_str()); } - bool CompilationDatabseIsEmpty(void) - { + bool CompilationDatabseIsEmpty(void) { std::vector<CompileCommand> CCV = CDB.getAllCompileCommands(); - - if(CCV.empty()) - { - return true; - } - + if(CCV.empty()) {return true;} return false; } - std::string GetMakePath(void) - { - return this->MakePath; - } + std::string GetMakePath(void) {return this->MakePath;} - std::vector<std::string> GetSourceFiles(void) - { - return this->SourceFiles; - } + std::vector<std::string> GetSourceFiles(void) {return this->SourceFiles;} - void PopulateGPATH(void) - { + void PopulateGPATH(void) { ShellGlobalInstance.PATH.push_back(MakePath); } - void PopulateGSOURCEFILES(void) - { - for (auto &iter : SourceFiles) - { + void PopulateGSOURCEFILES(void) { + for (auto &iter : SourceFiles) { ShellGlobalInstance.SOURCE_FILES.push_back(iter); } } @@ -1247,9 +1228,33 @@ class LuaWrapper if (numargs != 2) { PRINT_WITH_COLOR_LB(RED, "arg number should be 2."); } - + std::vector<uint8_t> xobj_code_; + std::string xobj_name; + int table_length = lua_rawlen(__ls, 1); + if (lua_type(__ls, 1) != LUA_TTABLE) { + PRINT_WITH_COLOR_LB(RED, "the stack value is not a table but is being accessed as such."); + } else { + PRINT_WITH_COLOR_LB(GREEN, "stack index 1 is a table."); + } + std::cout << CYAN << "table_length: " << table_length << NORMAL << "\n"; + for (int i = 1; i <= table_length; ++i) { + lua_rawgeti(__ls, 1, i); + xobj_code_.push_back(int(lua_tonumber(__ls, i + 2))); + } + std::cout << RED << "function code: "; + for (auto& iter : xobj_code_) {std::cout << RED << int(iter) << " ";} + std::cout << NORMAL <<"\n"; + xobj_name = lua_tostring(__ls, 2); Executioner executioner; - return 1; + 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); + ptr(); + xobj_2int ptr2; + ptr2 = (xobj_2int)ptr; + std::cout << MAGENTA << "result: " << NORMAL << ptr2(30,20) << "\n"; + return 0; } /*read the m0 report*/ @@ -1802,13 +1807,10 @@ int main(int argc, const char **argv) { CompilationDatabaseProcessor CDBP(CDB); /*checking whether the compilation database is found and not empty*/ - if (CDBP.CompilationDatabseIsEmpty()) - { + if (CDBP.CompilationDatabseIsEmpty()) { PRINT_WITH_COLOR_LB(RED, "bruiser could not find the compilation database."); return 1; - } - else - { + } else { CDBP.CalcMakePath(); CDBP.PopulateGPATH(); CDBP.PopulateGSOURCEFILES(); @@ -1870,41 +1872,30 @@ int main(int argc, const char **argv) { #undef LIST_LIST_GENERATORS /*The non-cli execution loop*/ - if (NonCLILuaScript != "") - { + if (NonCLILuaScript != "") { std::ifstream lua_script_noncli; lua_script_noncli.open(NonCLILuaScript); std::string line; - - while(std::getline(lua_script_noncli, line)) - { + while(std::getline(lua_script_noncli, line)) { BruiserLog.PrintToLog("running in non-cli mode..."); BruiserLog.PrintToLog(line + "\n"); LE.RunChunk((char*)line.c_str()); } - dostring(LE.GetLuaState(), "os.exit()", "test"); return 0; } /*cli execution loop*/ - while((command = linenoise(">>>")) != NULL) - { + while((command = linenoise(">>>")) != NULL) { linenoiseHistoryAdd(command); linenoiseHistorySave(SHELL_HISTORY_FILE); - if (std::string(command).find("!", 0) == 0) - { + if (std::string(command).find("!", 0) == 0) { std::string histnumber_str = std::string(command).substr(1, std::string::npos); unsigned int history_num = std::stoi(histnumber_str, 0, 10); - if (history_num >= ShellGlobalInstance.HISTORY_SIZE) - { + if (history_num >= ShellGlobalInstance.HISTORY_SIZE) { PRINT_WITH_COLOR_LB(RED, "invalid history number passed."); continue; - } - else - { - - } + } else {} } LE.RunChunk(command); linenoiseFree(command); diff --git a/bruiser/executioner.h b/bruiser/executioner.h index 0113d6c..26bd042 100644 --- a/bruiser/executioner.h +++ b/bruiser/executioner.h @@ -36,6 +36,8 @@ 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 LuaRegFunc = int(*)(lua_State*); constexpr int MEMORY_SIZE = 32768; std::vector<uint8_t> memory(MEMORY_SIZE, 0); @@ -178,10 +180,7 @@ class Executioner { std::vector<uint64_t> getAllArgs(lua_State* __ls) { int numargs = lua_gettop(__ls); std::vector<uint64_t> args; - - for (int i = 0; i < numargs; ++i) { - } - + for (int i = 0; i < numargs; ++i) {} return args; } @@ -203,6 +202,8 @@ class Executioner { lua_register(_lua_State, "xobjwrapper", LuaXobjWrapper); } + XObject getXobject(void* _ptr) {return (XObject)_ptr;} + void xobjsGetPtrs(void) { for (auto& iter : obj_mem_ptrs) { XObject dummy = (XObject)iter.first; diff --git a/bruiser/lua-scripts/demo1.lua b/bruiser/lua-scripts/demo1.lua index de83431..eb55acb 100644 --- a/bruiser/lua-scripts/demo1.lua +++ b/bruiser/lua-scripts/demo1.lua @@ -81,8 +81,34 @@ function codeTableByName(name) return nil end +function codeTableByName_number(name) + local return_table = {} + local func_name_table = objload("elf_get_func_names", "../bfd/test/test.so", "symbol_list") + local code_table = objload("elf_get_func_code", "../bfd/test/test.so", "code_list") + for k,v in ipairs(func_name_table) do + if v == name then + for k1, v1 in ipairs(code_table[k]) do + table.insert(return_table, v1) + end + return return_table + end + end + return nil +end + +function printFuncSizes() + local func_name_table = objload("elf_get_func_names", "../bfd/test/test.so", "symbol_list") + local code_table = objload("elf_get_func_code", "../bfd/test/test.so", "code_list") + local counter = 1 + print("function sizes:") + for k, v in ipairs(code_table) do + print("code size for "..func_name_table[counter].." is".." "..#v) + counter = counter + 1 + end +end + -- start of @placeholder -function XobjRegister(code_table, registration_name) +function xobjRegister(code_table, registration_name) -- lightuserdata local Xobjpointer return Xobjpointer @@ -110,6 +136,14 @@ function main() io.write(v, " ") end io.write("\n") + + local add2_code = codeTableByName_number("'add2'") + local sub2_code = codeTableByName_number("'sub2'") + + printFuncSizes() + + xobjregister(add2_code, "add2") + xobjregister(sub2_code, "sub2") end main() |