diff options
author | bloodstalker <thabogre@gmail.com> | 2017-07-16 12:22:24 +0000 |
---|---|---|
committer | bloodstalker <thabogre@gmail.com> | 2017-07-16 12:22:24 +0000 |
commit | 150b4996185e5fad33b98df7cb6d7ee327f756c3 (patch) | |
tree | 1a16f4358d2bab9070bb4b521c9d035fd579940a | |
parent | added a pp check due to api changes (diff) | |
download | mutator-150b4996185e5fad33b98df7cb6d7ee327f756c3.tar.gz mutator-150b4996185e5fad33b98df7cb6d7ee327f756c3.zip |
wip
-rw-r--r-- | bruiser/ORCmutation.h | 109 |
1 files changed, 103 insertions, 6 deletions
diff --git a/bruiser/ORCmutation.h b/bruiser/ORCmutation.h index 5ca243e..c026a1b 100644 --- a/bruiser/ORCmutation.h +++ b/bruiser/ORCmutation.h @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* /*inclusion guard*/ #ifndef ORC_MUTATION_H #define ORC_MUTATION_H -#if 0 +#if __clang_major__ == 5 /**********************************************************************************************************************/ /*included modules*/ /*project headers*/ @@ -33,38 +33,135 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* /*llvm headers*/ #include "llvm/ExecutionEngine/SectionMemoryManager.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" +#include "llvm/ExecutionEngine/JITSymbol.h" #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" +#include "llvm/ExecutionEngine/SectionMemoryManager.h" #include "llvm/ExecutionEngine/Orc/CompileUtils.h" #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h" #include "llvm/ExecutionEngine/Orc/LambdaResolver.h" #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h" #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h" +#include "llvm/IR/DataLayout.h" #include "llvm/IR/Mangler.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/Support/DynamicLibrary.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Scalar/GVN.h" /**********************************************************************************************************************/ /*using*/ using namespace llvm; /**********************************************************************************************************************/ /*globals*/ /**********************************************************************************************************************/ +namespace mutatorjit{ class MutatorJIT { private: std::unique_ptr<TargetMachine> TM; const DataLayout DL; std::unique_ptr<orc::JITCompileCallbackManager> CompileCallbackManager; - orc::RTDyldObjectLinkingLayer<> ObjectLinkingLayer; - orc::IRCompileLayer<decltype(ObjectLinkingLayer)> CompileLayer; + orc::RTDyldObjectLinkingLayer ObjectLayer; + orc::IRCompileLayer<decltype(ObjectLayer), orc::SimpleCompiler> CompileLayer; - typedef std::function<std::unique_ptr<llvm::Module>(std::unique_ptr<llvm::Module>)> OptimizeFunction; + using OptimizeFunction = std::function<std::shared_ptr<llvm::Module>(std::shared_ptr<llvm::Module>)>; orc::IRTransformLayer<decltype(CompileLayer), OptimizeFunction> OptimizeLayer; - orc::CompileOnDemandLayer<decltype(OptimizeLayer)> CODL; + //orc::CompileOnDemandLayer<decltype(OptimizeLayer)> CODL; public: - typedef decltype(CODL)::ModuleSetHandleT ModuleHandle; + using ModuleHandle = decltype(OptimizeLayer)::ModuleHandleT; + + MutatorJIT() : TM(EngineBuilder().selectTarget()), DL(TM->createDataLayout()), \ + ObjectLayer([]() {return std::make_shared<SectionMemoryManager>();}),\ + CompileLayer(ObjectLayer, orc::SimpleCompiler(*TM)), \ + OptimizeLayer(CompileLayer, [this](std::shared_ptr<llvm::Module> M){return optimizeModule(std::move(M));}) + { + llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr); + } + + TargetMachine &getTargetMachine() + { + return *TM; + } + + ModuleHandle addModule(std::unique_ptr<llvm::Module> M) + { + auto Resolver = orc::createLambdaResolver + ( + [&](const std::string &Name) + { + if (auto Sym = OptimizeLayer.findSymbol(Name, false)) + { + return Sym; + } + else + { + return JITSymbol(nullptr); + } + }, + [](const std::string &Name) + { + if (auto SymAddr = RTDyldMemoryManager::getSymbolAddressInProcess(Name)) + { + return JITSymbol(SymAddr, JITSymbolFlags::Exported); + } + else + { + return JITSymbol(nullptr); + } + } + ); + + return cantFail(OptimizeLayer.addModule(std::move(M), std::move(Resolver))); + } + + JITSymbol findSymbol(const std::string Name) + { + std::string MangledName; + raw_string_ostream MangledNameStream(MangledName); + Mangler::getNameWithPrefix(MangledNameStream, Name, DL); + return OptimizeLayer.findSymbol(MangledNameStream.str(), true); + } + + void removeModule(ModuleHandle H) + { + cantFail(OptimizeLayer.removeModule(H)); + } + + private: + std::shared_ptr<llvm::Module> optimizeModule(std::shared_ptr<llvm::Module> M) + { + auto FPM = llvm::make_unique<legacy::FunctionPassManager>(M.get()); + + FPM->add(createInstructionCombiningPass()); + FPM->add(createReassociatePass()); + FPM->add(createGVNPass()); + FPM->add(createCFGSimplificationPass()); + FPM->doInitialization(); + + for (auto &F : *M) + { + FPM->run(F); + } + + return M; + } +}; + +class runMainOnJit +{ +#if 0 + std::unique_ptr<llvm::Module> M = buildModule(); + MutatorJIT jit; + Handle H = jit.addModule(*M); + int (*Main)(int, char*[]) = + (int(*)(int*, char*[]))jit.findSymbol("main").getSymbolAddressInProcess(); + int result = Main(); + jit.removeModule(H); +#endif }; +} //end of namespace mutatorjit /**********************************************************************************************************************/ /**********************************************************************************************************************/ /**********************************************************************************************************************/ |