From 48a093e6f0e908d9a17f1ebce6eec16185e30742 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Fri, 23 Jun 2017 06:41:04 +0430 Subject: added a new command to extract mutagens --- bruiser/bruiser.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index 42de9e0..7030377 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -31,6 +31,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* #include #include #include +#include +#include +#include +#include /*LLVM headers*/ #include "clang/AST/AST.h" #include "clang/AST/ASTConsumer.h" @@ -80,7 +84,14 @@ namespace std::vector SOURCE_FILES; }; + struct ShellCache + { + std::string LastFileUsed; + std::string LastFileUsedShort; + }; + ShellGlobal ShellGlobalInstance; + ShellCache ShellCacheInstance; } /**********************************************************************************************************************/ cl::opt Intrusive("intrusive", cl::desc("If set true. bruiser will mutate the source."), cl::init(true), cl::cat(BruiserCategory), cl::ZeroOrMore); @@ -1136,6 +1147,9 @@ class LuaWrapper { if (iter.rfind(filename) == iter.size() - filename.size()) { + ShellCacheInstance.LastFileUsed = iter; + ShellCacheInstance.LastFileUsedShort = filename; + targetfile.open(iter); if(targetfile.rdstate() != std::ios_base::goodbit) @@ -1155,6 +1169,57 @@ class LuaWrapper return lineend - linebegin + 1U; } + int BruiserLuaMutagenExtraction(lua_State* __ls) + { + int numargs = lua_gettop(__ls); + std::string extractiontarget; + + if (numargs == 1) + { + extractiontarget = lua_tostring(__ls, 1); + } + + pid_t pid = fork(); + + if (pid < 0) + { + /*bruiser could not spawn a child*/ + PRINT_WITH_COLOR_LB(RED, "could not fork a child process(m0)."); + lua_pushnumber(__ls, EXIT_FAILURE); + } + + /*only the child process runs this*/ + if (pid == 0) + { + for(auto &iter : ShellGlobalInstance.SOURCE_FILES) + { + if (iter.rfind(extractiontarget) == iter.size() - extractiontarget.size()) + { + ShellCacheInstance.LastFileUsedShort = extractiontarget; + ShellCacheInstance.LastFileUsed = iter; + std::cout << BLUE << "running: " << CYAN << "../mutator-lvl0 " << iter.c_str() << NORMAL << "\n"; + //int retval = execl("../", "mutator-lvl0", iter.c_str(), NULL); + int retval = execl("../mutator-lvl0", "mutator-lvl0", iter.c_str(), NULL); + std::cout << BLUE << "child process retuned " << retval << NORMAL << "\n"; + lua_pushnumber(__ls, retval); + exit(EXIT_SUCCESS); + } + } + } + + /*only the parent process runs this*/ + if (pid > 0) + { + /*the parent-bruiser- will need to wait on the child. the child will run m0.*/ + int status; + pid_t returned; + returned = waitpid(pid, &status, 0); + lua_pushnumber(__ls, returned); + } + + return 1; + } + #define LIST_GENERATOR(__x1) \ int List##__x1(lua_State* __ls)\ {\ @@ -1269,6 +1334,7 @@ int main(int argc, const char **argv) lua_register(LE.GetLuaState(), "make", &LuaDispatch<&LuaWrapper::BruiserLuaRunMake>); lua_register(LE.GetLuaState(), "historysize", &LuaDispatch<&LuaWrapper::BruiserLuaChangeHistorySize>); lua_register(LE.GetLuaState(), "showsource", &LuaDispatch<&LuaWrapper::BruiserLuaShowSourcecode>); + lua_register(LE.GetLuaState(), "extractmutagen", &LuaDispatch<&LuaWrapper::BruiserLuaMutagenExtraction>); /*its just regisering the List function from LuaWrapper with X-macros.*/ #define X(__x1, __x2) lua_register(LE.GetLuaState(), #__x1, &LuaDispatch<&LuaWrapper::List##__x1>); -- cgit v1.2.3