Showing
2 changed files
with
55 additions
and
0 deletions
| 1 | +#include "llvm/Pass.h" | ||
| 2 | +#include "llvm/IR/Function.h" | ||
| 3 | +#include "llvm/IR/Module.h" | ||
| 4 | +#include "llvm/IR/Instructions.h" | ||
| 5 | +#include "llvm/Support/Alignment.h" | ||
| 6 | +#include "llvm/Support/raw_ostream.h" | ||
| 7 | +#include "llvm/IR/CFG.h" | ||
| 8 | +#include <fstream> | ||
| 9 | +#include <iostream> | ||
| 10 | +using namespace llvm; | ||
| 11 | + | ||
| 12 | +namespace { | ||
| 13 | + struct PreProcess : public FunctionPass { | ||
| 14 | + static char ID; | ||
| 15 | + | ||
| 16 | + PreProcess() : FunctionPass(ID) { } | ||
| 17 | + bool runOnFunction(Function &F) override { | ||
| 18 | + Module* mod = F.getParent(); | ||
| 19 | + std::vector<Instruction *> instructions; | ||
| 20 | + std::vector<BasicBlock *> RetBlocks; | ||
| 21 | + bool inserted = false; | ||
| 22 | + std::ofstream functionFile("functions.txt", std::ios_base::app); | ||
| 23 | + if (functionFile.is_open()) { | ||
| 24 | + functionFile << F.getName().str() << "\n"; | ||
| 25 | + functionFile.close(); | ||
| 26 | + } | ||
| 27 | + for (auto &BB : F) { | ||
| 28 | + for (auto &I : BB) { | ||
| 29 | + if (I.getOpcode() == Instruction::Ret) { | ||
| 30 | + instructions.push_back(&I); | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + for (auto &I : instructions) { | ||
| 35 | + BasicBlock *BB = I->getParent(); | ||
| 36 | + // One Instruction Basic Block has only one ret instructions | ||
| 37 | + if (!BB->size() < 2) | ||
| 38 | + { | ||
| 39 | + BasicBlock *retblock = BB->splitBasicBlock(I->getIterator(), "obfuscatedreturn"); | ||
| 40 | + } else { | ||
| 41 | + BB->setName("obfuscatedreturn"); | ||
| 42 | + } | ||
| 43 | + } | ||
| 44 | + return true; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + }; // end of struct Hello | ||
| 48 | +} // end of anonymous namespace | ||
| 49 | + | ||
| 50 | +char PreProcess::ID = 0; | ||
| 51 | + | ||
| 52 | +static RegisterPass<PreProcess> X("preprocess", "Hello World Pass", | ||
| 53 | + false /* Only looks at CFG */, | ||
| 54 | + false /* Analysis Pass */); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment