From 9244662e10c43e403c97be00d78b2c320d98d05c Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Fri, 8 Dec 2017 22:08:03 +0330 Subject: wip --- bruiser/bruiser.cpp | 1 + bruiser/executioner.cpp | 47 -------------------- bruiser/executioner.h | 116 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 47 deletions(-) delete mode 100644 bruiser/executioner.cpp create mode 100644 bruiser/executioner.h (limited to 'bruiser') diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index 3197a5e..46f8323 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -26,6 +26,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* #include "../mutator_aux.h" #include "mutagen.h" #include "ORCmutation.h" +#include "executioner.h" /*standard headers*/ #include #include diff --git a/bruiser/executioner.cpp b/bruiser/executioner.cpp deleted file mode 100644 index 5aad56b..0000000 --- a/bruiser/executioner.cpp +++ /dev/null @@ -1,47 +0,0 @@ - -/***************************************************Project Mutator****************************************************/ -//-*-c++-*- -/*first line intentionally left blank.*/ -/*loads the objects into executable memory and registers them with lua.*/ -/*Copyright (C) 2017 Farzad Sadeghi - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ -/**********************************************************************************************************************/ -#include -#include -#include "lua-5.3.4/src/lua.hpp" -/**********************************************************************************************************************/ -namespace { - constexpr int MEMORY_SIZE = 30000; - std::vector memory(MEMORY_SIZE, 0); -} - -class Executioner { - public: - Executioner() {} - ~Executioner() {} - - void getObjs(std::vector> _objs) { - objs = _objs; - } - - void registerWithLua(lua_State* _lua_State) {} - - private: - std::vector> objs; -}; -/**********************************************************************************************************************/ -/*last line intentionally left blank.*/ - diff --git a/bruiser/executioner.h b/bruiser/executioner.h new file mode 100644 index 0000000..d1c0b32 --- /dev/null +++ b/bruiser/executioner.h @@ -0,0 +1,116 @@ + +/***************************************************Project Mutator****************************************************/ +//-*-c++-*- +/*first line intentionally left blank.*/ +/*loads the objects into executable memory and registers them with lua.*/ +/*Copyright (C) 2017 Farzad Sadeghi + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ +/**********************************************************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include "lua-5.3.4/src/lua.hpp" +/**********************************************************************************************************************/ +#ifndef EXECUTIONER_H +#define EXECUTIONER_H +/**********************************************************************************************************************/ +namespace { + using XObject = void(*)(void); + constexpr int MEMORY_SIZE = 32768; + std::vector memory(MEMORY_SIZE, 0); + + void* alloc_writeable_memory(size_t _size) { + void* ptr = mmap(0, _size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (ptr == (void*)-1) { + perror("could not allocate virtual memory."); + return nullptr; + } + return ptr; + } + + int make_mem_executable(void* _mem, size_t _size) { + if (mprotect(_mem, _size, PROT_READ | PROT_EXEC) == -1) { + perror("could not make virtual memory executable."); + return -1; + } + return 0; + } +} + +int getMemorySize(void) {return MEMORY_SIZE;} + +class Executioner { + public: + Executioner() {} + ~Executioner() { + for (auto &iter : obj_mem_ptrs) { + if (iter.first != nullptr) { + if (munmap(iter.first, iter.second) < 0) { + perror("could not unmap vmemory."); + } + } + } + } + + void getObjs(std::vector>& _objs) {objs = _objs;} + + void getNames(std::vector& _names) {names = _names;} + + std::pair loadObjsInXMem(std::vector& _obj_code) { + size_t code_size = _obj_code.size(); + void* program_memory = alloc_writeable_memory(code_size); + if (program_memory == nullptr) { + std::cout << "could not allocate virtual memory\n"; + return std::make_pair(nullptr, 0); + } + memcpy(program_memory, _obj_code.data(), code_size); + if (make_mem_executable(program_memory, code_size) < 0) { + std::cout << "could not make vmemory executable.\n"; + return std::make_pair(nullptr, 0); + } + return std::make_pair(program_memory, code_size); + } + + void loadAll(void) { + for (auto &iter : objs) { + this->loadObjsInXMem(iter); + } + } + + void emitByte(uint8_t _byte, std::vector& _code) { + _code.push_back(_byte); + } + + void emitBytes(std::vector& _bytes, std::vector& _code) { + for (auto &iter : _bytes) {this->emitByte(iter, _code);} + } + + void registerWithLua(lua_State* _lua_State) {} + + private: + std::vector> obj_mem_ptrs; + std::vector> objs; + std::vector names; +}; +/**********************************************************************************************************************/ +#endif +/**********************************************************************************************************************/ +/*last line intentionally left blank.*/ + -- cgit v1.2.3