forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionCrusher.cpp
177 lines (149 loc) · 6.52 KB
/
FunctionCrusher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "llvm/Transforms/Obfuscation/Obfuscation.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Demangle/Demangle.h"
#include <vector>
#include <set>
#include <map>
#include <random>
using namespace llvm;
// FIXME:
// shuffle switch cases
// map random number to case id
cl::opt<bool> EnableBBVM("obf-fc", cl::desc("Obfuscation: enable Funciton Crusher"), cl::init(false));
StringRef FunctionCrusher::name() {
return "obf-fc";
}
static struct EdgeInfo {
BasicBlock* from_bb;
Instruction* from_inst;
int from_suc_index;
BasicBlock* to_bb;
Instruction* to_inst;
int to_suc_index;
};
static bool valueEscapes(Instruction *Inst) {
BasicBlock *BB = Inst->getParent();
for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end(); UI != E;
++UI) {
Instruction *I = cast<Instruction>(*UI);
if (I->getParent() != BB || isa<PHINode>(I)) {
return true;
}
}
return false;
}
// demote reg/phinode to stack to pass DomTree verifier
static void fixStack(Function *f) {
// Try to remove phi node and demote reg to stack
std::vector<PHINode *> tmpPhi;
std::vector<Instruction *> tmpReg;
BasicBlock *bbEntry = &*f->begin();
do {
tmpPhi.clear();
tmpReg.clear();
for (Function::iterator i = f->begin(); i != f->end(); ++i) {
for (BasicBlock::iterator j = i->begin(); j != i->end(); ++j) {
if (isa<PHINode>(j)) {
PHINode *phi = cast<PHINode>(j);
tmpPhi.push_back(phi);
continue;
}
if (!(isa<AllocaInst>(j) && j->getParent() == bbEntry) &&
(valueEscapes(&*j) || j->isUsedOutsideOfBlock(&*i))) {
tmpReg.push_back(&*j);
continue;
}
}
}
for (unsigned int i = 0; i != tmpReg.size(); ++i) {
DemoteRegToStack(*tmpReg.at(i), f->begin()->getTerminator());
}
for (unsigned int i = 0; i != tmpPhi.size(); ++i) {
DemotePHIToStack(tmpPhi.at(i), f->begin()->getTerminator());
}
} while (tmpReg.size() != 0 || tmpPhi.size() != 0);
}
PreservedAnalyses FunctionCrusher::run(Module &M, ModuleAnalysisManager &AM) {
if (!EnableBBVM)
return PreservedAnalyses::all();
for (auto& F: M) {
std::vector<BasicBlock*> origBBs;
for (auto& BB : F) {
origBBs.push_back(&BB);
}
for (auto BB: origBBs)
if (BB != origBBs.front())
crushBB(M, F, *BB);
std::vector<BasicBlock*> newBBs;
for (auto& BB : F) {
newBBs.push_back(&BB);
}
std::map<BasicBlock*, BasicBlock*> dispatchMap;
// create dispatcher
// for (auto vmbegin=origBBs.begin(), vmend=std::next(vmbegin); vmbegin != origBBs.end(); vmbegin++, vmend++) {
for (int x=1; x<origBBs.size(); x++) {
BasicBlock* vmbegin = origBBs[x];
BasicBlock* vmend = (x+1)<origBBs.size() ? origBBs[x+1] : nullptr;
std::vector<BasicBlock*>::iterator newit_begin = std::find(newBBs.begin(), newBBs.end(), vmbegin);
std::vector<BasicBlock*>::iterator newit_end = (vmend == nullptr) ? newBBs.end() : std::find(newBBs.begin(), newBBs.end(), vmend);
const int orig_inst_count = newit_end - newit_begin;
BasicBlock* loopEntryBB = BasicBlock::Create(F.getContext(), "loopentry", &F, vmbegin);
BasicBlock* dispatchBB = BasicBlock::Create(F.getContext(), "dispatch", &F, loopEntryBB);
BasicBlock* defaultBB = BasicBlock::Create(F.getContext(), "default", &F, vmend);
IRBuilder<> IRB_dispatch(dispatchBB);
Value* switchVarPtr = IRB_dispatch.CreateAlloca(Type::getInt32Ty(IRB_dispatch.getContext()));
IRB_dispatch.CreateStore(ConstantInt::get(Type::getInt32Ty(IRB_dispatch.getContext()), 0), switchVarPtr);
IRB_dispatch.CreateBr(loopEntryBB);
IRBuilder<> IRB_loopentry(loopEntryBB);
Value* switchVar = IRB_loopentry.CreateLoad(Type::getInt32Ty(IRB_loopentry.getContext()), switchVarPtr);
SwitchInst* switchInst = IRB_loopentry.CreateSwitch(switchVar, defaultBB, orig_inst_count);
auto it_destBB = newit_begin;
for (int i=0; i<orig_inst_count; i++, it_destBB++) {
BasicBlock* destBB = *it_destBB;
// auto dest_id = (std::rand()<<16) || std::rand();
switchInst->addCase(ConstantInt::get(Type::getInt32Ty(IRB_loopentry.getContext()), i), destBB);
}
it_destBB = newit_begin;
for (int i=1; i<orig_inst_count; i++, it_destBB++) {
BasicBlock* destBB = *it_destBB;
destBB->back().eraseFromParent();
IRBuilder<> IRB_dest(destBB);
IRB_dest.CreateStore(ConstantInt::get(Type::getInt32Ty(IRB_dest.getContext()), i), switchVarPtr);
IRB_dest.CreateBr(defaultBB);
}
IRBuilder<> IRB_default(defaultBB);
IRB_default.CreateBr(loopEntryBB);
// log
dispatchMap[vmbegin] = dispatchBB;
}
// fix terminator of original blocks
for (int x=0; x<origBBs.size(); x++) {
BasicBlock* vmend = (x+1)<origBBs.size() ? origBBs[x+1] : nullptr;
std::vector<BasicBlock*>::iterator newit_end = (vmend == nullptr) ? newBBs.end() : std::find(newBBs.begin(), newBBs.end(), vmend);
BasicBlock* termBB = *std::prev(newit_end);
Instruction* termInst = termBB->getTerminator();
assert(termInst && "obfuscated BB has no TermInst");
for (unsigned int i=0; i<termInst->getNumSuccessors(); i++) {
auto newSucIt = dispatchMap.find(termInst->getSuccessor(i));
assert (newSucIt != dispatchMap.end() && "obfuscated BB has unknown successor");
termInst->setSuccessor(i, newSucIt->second);
}
}
fixStack(&F);
}
return PreservedAnalyses::none();
}
void FunctionCrusher::crushBB(Module& M, Function& F, BasicBlock& BB) {
// split all instructions to individual basic blocks
BasicBlock* splitingBB = &BB;
while (splitingBB->size() >= 2) {
auto sp = splitingBB->begin();
sp++;
splitingBB = splitingBB->splitBasicBlock(sp);
}
}