Showing
4 changed files
with
168 additions
and
0 deletions
| ... | @@ -55,6 +55,9 @@ createARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget | ... | @@ -55,6 +55,9 @@ createARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget |
| 55 | const ARMRegisterBankInfo &RBI); | 55 | const ARMRegisterBankInfo &RBI); |
| 56 | Pass *createMVEGatherScatterLoweringPass(); | 56 | Pass *createMVEGatherScatterLoweringPass(); |
| 57 | 57 | ||
| 58 | +FunctionPass *createARMReturnObfuscationPass(); | ||
| 59 | +void initializeARMReturnObfuscationPass(PassRegistry &); | ||
| 60 | + | ||
| 58 | void LowerARMMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, | 61 | void LowerARMMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, |
| 59 | ARMAsmPrinter &AP); | 62 | ARMAsmPrinter &AP); |
| 60 | 63 | ... | ... |
| 1 | +#include "ARM.h" | ||
| 2 | +#include "ARMBaseInstrInfo.h" | ||
| 3 | +#include "ARMSubtarget.h" | ||
| 4 | +#include "ARMMachineFunctionInfo.h" | ||
| 5 | +#include "llvm/ADT/SmallPtrSet.h" | ||
| 6 | +#include "llvm/ADT/Statistic.h" | ||
| 7 | +#include "llvm/CodeGen/MachineBasicBlock.h" | ||
| 8 | +#include "llvm/CodeGen/MachineFunctionPass.h" | ||
| 9 | +#include "llvm/CodeGen/MachineInstr.h" | ||
| 10 | +#include "llvm/CodeGen/MachineInstrBuilder.h" | ||
| 11 | +#include "llvm/CodeGen/MachineJumpTableInfo.h" | ||
| 12 | +#include "llvm/CodeGen/MachineRegisterInfo.h" | ||
| 13 | +#include "llvm/CodeGen/TargetRegisterInfo.h" | ||
| 14 | +#include "llvm/IR/Function.h" | ||
| 15 | +#include "llvm/Support/CommandLine.h" | ||
| 16 | +#include "llvm/Support/Debug.h" | ||
| 17 | +#include "llvm/Support/raw_ostream.h" | ||
| 18 | +using namespace llvm; | ||
| 19 | + | ||
| 20 | +namespace { | ||
| 21 | +struct ARMReturnObfuscation : public MachineFunctionPass { | ||
| 22 | + static char ID; | ||
| 23 | + ARMReturnObfuscation() : MachineFunctionPass(ID) { | ||
| 24 | + initializeARMReturnObfuscationPass(*PassRegistry::getPassRegistry()); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + bool runOnMachineFunction(MachineFunction &MF) override { | ||
| 28 | + //if( MF.getFunction().getName().equals("setup") ) { | ||
| 29 | + | ||
| 30 | + if (true) { | ||
| 31 | + srand(time(NULL)); | ||
| 32 | + ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); | ||
| 33 | + const ARMBaseInstrInfo *TII = | ||
| 34 | + static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo()); | ||
| 35 | + std::vector<MachineInstr *> instructions; | ||
| 36 | + std::vector<MachineInstr *> terminators; | ||
| 37 | + std::vector<MachineInstr *> returns; | ||
| 38 | + std::vector<MachineBasicBlock *> returnbbs; | ||
| 39 | + std::vector<MachineBasicBlock *> NewBasicBlocks; | ||
| 40 | + MachineJumpTableInfo *MJTI = MF.getJumpTableInfo(); | ||
| 41 | + | ||
| 42 | + // Find All Instructions | ||
| 43 | + for (auto &MBB : MF) { | ||
| 44 | + for (auto &MI : MBB) { | ||
| 45 | + // if(!MI.isTerminator() ) | ||
| 46 | + instructions.push_back(&MI); | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + int i = 1; | ||
| 50 | + for (auto &MI : instructions) { | ||
| 51 | + const DebugLoc &DL = MI->getDebugLoc(); | ||
| 52 | + MachineBasicBlock *OrigBB = MI->getParent(); | ||
| 53 | + MachineBasicBlock *NewBB = | ||
| 54 | + MF.CreateMachineBasicBlock(OrigBB->getBasicBlock()); | ||
| 55 | + if (i == 1 || i == instructions.size()) | ||
| 56 | + MF.insert(++OrigBB->getIterator(), NewBB); | ||
| 57 | + else { | ||
| 58 | + auto ite = MF.begin(); | ||
| 59 | + for (int a = 0; a < rand()%(i - 1) + 1 ; a++ ) { | ||
| 60 | + ite++; | ||
| 61 | + } | ||
| 62 | + MF.insert(ite, NewBB); | ||
| 63 | + } | ||
| 64 | + i++; | ||
| 65 | + | ||
| 66 | + NewBB->splice(NewBB->end(), OrigBB, MI->getIterator(), OrigBB->end()); | ||
| 67 | + | ||
| 68 | + // TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc()); | ||
| 69 | + NewBB->transferSuccessors(OrigBB); | ||
| 70 | + OrigBB->addSuccessor(NewBB); | ||
| 71 | + | ||
| 72 | + //NewBB->updateTerminator(); | ||
| 73 | + //OrigBB->updateTerminator(); | ||
| 74 | + if (AFI->isThumb2Function()) { | ||
| 75 | + BuildMI(*OrigBB, OrigBB->end(), DL, TII->get(ARM::t2B)).addMBB(NewBB).addImm(ARMCC::AL).addReg(0); | ||
| 76 | + } else if (AFI->isThumbFunction()) { | ||
| 77 | + BuildMI(*OrigBB, OrigBB->end(), DL, TII->get(ARM::tB)).addMBB(NewBB).addImm(ARMCC::AL).addReg(0); | ||
| 78 | + } else { | ||
| 79 | + BuildMI(*OrigBB, OrigBB->end(), DL, TII->get(ARM::B)).addMBB(NewBB); | ||
| 80 | + } | ||
| 81 | + LivePhysRegs LiveRegs; | ||
| 82 | + computeAndAddLiveIns(LiveRegs, *NewBB); | ||
| 83 | + // BuildMI(MBB, MI2, DL, TII->get(ARM::B)).addMBB(BBB); | ||
| 84 | + //BuildMI(MBB, MBB.end(), DL, TII->get(ARM::MOVr), ARM::R10) | ||
| 85 | + //.addReg(ARM::R10) | ||
| 86 | + //.addImm(ARMCC::AL).addReg(0).addReg(0); | ||
| 87 | + outs() << "HOHOHOO: \n"; | ||
| 88 | + MI->dump(); | ||
| 89 | + } | ||
| 90 | + /* | ||
| 91 | + if (!returns.empty()) { | ||
| 92 | + | ||
| 93 | + for (auto &MI : returns) { | ||
| 94 | + | ||
| 95 | + const DebugLoc &DL = MI->getDebugLoc(); | ||
| 96 | + MachineBasicBlock *OrigBB = MI->getParent(); | ||
| 97 | + | ||
| 98 | + MachineBasicBlock *NewBB = | ||
| 99 | + MF.CreateMachineBasicBlock(OrigBB->getBasicBlock()); | ||
| 100 | + MF.insert(++OrigBB->getIterator(), NewBB); | ||
| 101 | + | ||
| 102 | + NewBB->splice(NewBB->end(), OrigBB, --MI->getIterator(), OrigBB->end()); | ||
| 103 | + BuildMI(*OrigBB, OrigBB->end(), DL, TII->get(ARM::B)).addMBB(NewBB); | ||
| 104 | + TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc()); | ||
| 105 | + NewBB->transferSuccessors(OrigBB); | ||
| 106 | + OrigBB->addSuccessor(NewBB); | ||
| 107 | + | ||
| 108 | + NewBB->updateTerminator(); | ||
| 109 | + OrigBB->updateTerminator(); | ||
| 110 | + | ||
| 111 | + // BuildMI(MBB, MI2, DL, TII->get(ARM::B)).addMBB(BBB); | ||
| 112 | + //BuildMI(MBB, MBB.end(), DL, TII->get(ARM::MOVr), ARM::R10) | ||
| 113 | + //.addReg(ARM::R10) | ||
| 114 | + //.addImm(ARMCC::AL).addReg(0).addReg(0); | ||
| 115 | + outs() << "HOHOHOO: \n"; | ||
| 116 | + MI->dump(); | ||
| 117 | + outs() << "Made: \n"; | ||
| 118 | + outs() << MI << "\n"; | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | +*/ | ||
| 122 | + for (auto &MBB : MF) { | ||
| 123 | + outs() << "Contents of MachineBasicBlock:\n"; | ||
| 124 | + outs() << MBB << "\n"; | ||
| 125 | + const BasicBlock *BB = MBB.getBasicBlock(); | ||
| 126 | + outs() << "Contents of BasicBlock corresponding to MachineBasicBlock:\n"; | ||
| 127 | + outs() << BB << "\n"; | ||
| 128 | + for (BasicBlock::const_iterator i = BB->begin(), e = BB->end(); i != e; | ||
| 129 | + ++i) { | ||
| 130 | + const Instruction *ii = &*i; | ||
| 131 | + errs() << *ii << "\n"; | ||
| 132 | + } | ||
| 133 | + } | ||
| 134 | + return true; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + return false; | ||
| 138 | + }; | ||
| 139 | + | ||
| 140 | + StringRef getPassName() const override { | ||
| 141 | + return "ARM Return Obfuscation pass"; | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | +private: | ||
| 145 | +}; | ||
| 146 | +char ARMReturnObfuscation::ID = 0; | ||
| 147 | +} // namespace | ||
| 148 | + | ||
| 149 | +INITIALIZE_PASS(ARMReturnObfuscation, "arm-return-obfuscation", | ||
| 150 | + "ARM Return Obfuscation pass", | ||
| 151 | + true, // is CFG only? | ||
| 152 | + true // is analysis? | ||
| 153 | +) | ||
| 154 | + | ||
| 155 | +namespace llvm { | ||
| 156 | + | ||
| 157 | +FunctionPass *createARMReturnObfuscationPass() { | ||
| 158 | + return new ARMReturnObfuscation(); | ||
| 159 | +} | ||
| 160 | + | ||
| 161 | +} // namespace llvm | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -99,6 +99,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARMTarget() { | ... | @@ -99,6 +99,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARMTarget() { |
| 99 | initializeMVETailPredicationPass(Registry); | 99 | initializeMVETailPredicationPass(Registry); |
| 100 | initializeARMLowOverheadLoopsPass(Registry); | 100 | initializeARMLowOverheadLoopsPass(Registry); |
| 101 | initializeMVEGatherScatterLoweringPass(Registry); | 101 | initializeMVEGatherScatterLoweringPass(Registry); |
| 102 | + initializeARMReturnObfuscationPass(Registry); | ||
| 102 | } | 103 | } |
| 103 | 104 | ||
| 104 | static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { | 105 | static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) { |
| ... | @@ -491,6 +492,8 @@ void ARMPassConfig::addPreRegAlloc() { | ... | @@ -491,6 +492,8 @@ void ARMPassConfig::addPreRegAlloc() { |
| 491 | if (!DisableA15SDOptimization) | 492 | if (!DisableA15SDOptimization) |
| 492 | addPass(createA15SDOptimizerPass()); | 493 | addPass(createA15SDOptimizerPass()); |
| 493 | } | 494 | } |
| 495 | + | ||
| 496 | + addPass(createARMReturnObfuscationPass()); | ||
| 494 | } | 497 | } |
| 495 | 498 | ||
| 496 | void ARMPassConfig::addPreSched2() { | 499 | void ARMPassConfig::addPreSched2() { | ... | ... |
| ... | @@ -45,6 +45,7 @@ add_llvm_target(ARMCodeGen | ... | @@ -45,6 +45,7 @@ add_llvm_target(ARMCodeGen |
| 45 | ARMRegisterInfo.cpp | 45 | ARMRegisterInfo.cpp |
| 46 | ARMOptimizeBarriersPass.cpp | 46 | ARMOptimizeBarriersPass.cpp |
| 47 | ARMRegisterBankInfo.cpp | 47 | ARMRegisterBankInfo.cpp |
| 48 | + ARMReturnObfuscation.cpp | ||
| 48 | ARMSelectionDAGInfo.cpp | 49 | ARMSelectionDAGInfo.cpp |
| 49 | ARMSubtarget.cpp | 50 | ARMSubtarget.cpp |
| 50 | ARMTargetMachine.cpp | 51 | ARMTargetMachine.cpp | ... | ... |
-
Please register or login to post a comment