forked from HikariObfuscator/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
245 lines (230 loc) · 8.55 KB
/
Utils.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// For open-source license, please refer to [License](https://github.com/HikariObfuscator/Hikari/wiki/License).
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Obfuscation/Utils.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/NoFolder.h"
using namespace llvm;
using namespace std;
// Shamefully borrowed from ../Scalar/RegToMem.cpp :(
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;
}
void appendToAnnotations(Module &M, ConstantStruct *Data) {
// Type for the annotation array
// { i8*, i8*, i8*, i32 }
GlobalVariable *AnnotationGV = M.getGlobalVariable("llvm.global.annotations");
Type *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
Type *Int32Ty = Type::getInt32Ty(M.getContext());
if (AnnotationGV == nullptr) {
ArrayType *AT = ArrayType::get(Data->getType(), 1);
ConstantArray *CA = cast<ConstantArray>(ConstantArray::get(AT, {Data}));
GlobalVariable *newGV = new GlobalVariable(M, CA->getType(), false,
GlobalValue::AppendingLinkage,
CA, "llvm.global.annotations");
newGV->setSection("llvm.metadata");
return;
} else {
StructType *ST = StructType::get(
M.getContext(), {Int8PtrTy, Int8PtrTy, Int8PtrTy, Int32Ty});
vector<Constant *> exists;
for (unsigned i = 0;
i < cast<ArrayType>(AnnotationGV->getInitializer()->getType())
->getNumElements();
i++) {
exists.push_back(AnnotationGV->getInitializer()->getAggregateElement(i));
}
exists.push_back(Data);
ArrayType *AT = ArrayType::get(ST, exists.size());
ConstantArray *CA = cast<ConstantArray>(
ConstantArray::get(AT, ArrayRef<Constant *>(exists)));
GlobalVariable *newGV = new GlobalVariable(M, CA->getType(), false,
GlobalValue::AppendingLinkage,
CA, "llvm.global.annotations");
newGV->setSection("llvm.metadata");
return;
}
}
void FixFunctionConstantExpr(Function *Func) {
// Replace ConstantExpr with equal instructions
// Otherwise replacing on Constant will crash the compiler
for (BasicBlock &BB : *Func) {
FixBasicBlockConstantExpr(&BB);
}
}
void FixBasicBlockConstantExpr(BasicBlock *BB) {
// Replace ConstantExpr with equal instructions
// Otherwise replacing on Constant will crash the compiler
// Things to note:
// - Phis must be placed at BB start so CEs must be placed prior to current BB
assert(!BB->empty()&&"BasicBlock is empty!");
assert((BB->getParent()!=NULL)&&"BasicBlock must be in a Function!");
Instruction* FunctionInsertPt=&*(BB->getParent()->getEntryBlock().getFirstInsertionPt());
//Instruction* LocalBBInsertPt=&*(BB.getFirstInsertionPt());
for (Instruction &I : *BB) {
if(isa<LandingPadInst>(I)||isa<FuncletPadInst>(I)){
continue;
}
for(unsigned i=0;i<I.getNumOperands();i++){
if(ConstantExpr* C=dyn_cast<ConstantExpr>(I.getOperand(i))){
Instruction* InsertPt=&I;
IRBuilder<NoFolder> IRB(InsertPt);
if(isa<PHINode>(I)){
IRB.SetInsertPoint(FunctionInsertPt);
}
Instruction *Inst = IRB.Insert(C->getAsInstruction());
I.setOperand(i,Inst);
}
}
}
}
map<GlobalValue *, StringRef> BuildAnnotateMap(Module &M) {
map<GlobalValue *, StringRef> VAMap;
GlobalVariable *glob = M.getGlobalVariable("llvm.global.annotations");
if (glob != nullptr && glob->hasInitializer()) {
ConstantArray *CDA = cast<ConstantArray>(glob->getInitializer());
for (Value *op : CDA->operands()) {
ConstantStruct *anStruct = cast<ConstantStruct>(op);
/*
Structure: [Value,Annotation,SourceFilePath,LineNumber]
Usually wrapped inside GEP/BitCast
We only care about Value and Annotation Here
*/
GlobalValue *Value =
cast<GlobalValue>(anStruct->getOperand(0)->getOperand(0));
GlobalVariable *Annotation =
cast<GlobalVariable>(anStruct->getOperand(1)->getOperand(0));
if (Annotation->hasInitializer()) {
VAMap[Value] =
cast<ConstantDataSequential>(Annotation->getInitializer())
->getAsCString();
}
}
}
return VAMap;
}
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);
}
std::string readAnnotate(Function *f) {
std::string annotation = "";
// Get annotation variable
GlobalVariable *glob =
f->getParent()->getGlobalVariable("llvm.global.annotations");
if (glob != NULL) {
// Get the array
if (ConstantArray *ca = dyn_cast<ConstantArray>(glob->getInitializer())) {
for (unsigned i = 0; i < ca->getNumOperands(); ++i) {
// Get the struct
if (ConstantStruct *structAn =
dyn_cast<ConstantStruct>(ca->getOperand(i))) {
if (ConstantExpr *expr =
dyn_cast<ConstantExpr>(structAn->getOperand(0))) {
// If it's a bitcast we can check if the annotation is concerning
// the current function
if (expr->getOpcode() == Instruction::BitCast &&
expr->getOperand(0) == f) {
ConstantExpr *note = cast<ConstantExpr>(structAn->getOperand(1));
// If it's a GetElementPtr, that means we found the variable
// containing the annotations
if (note->getOpcode() == Instruction::GetElementPtr) {
if (GlobalVariable *annoteStr =
dyn_cast<GlobalVariable>(note->getOperand(0))) {
if (ConstantDataSequential *data =
dyn_cast<ConstantDataSequential>(
annoteStr->getInitializer())) {
if (data->isString()) {
annotation += data->getAsString().lower() + " ";
}
}
}
}
}
}
}
}
}
}
return annotation;
}
// Unlike O-LLVM which uses __attribute__ that is not supported by the ObjC CFE.
// We use a dummy call here and remove the call later
// Very dumb and definitely slower than the function attribute method
// Merely a hack
bool readFlag(Function *f, std::string attribute) {
for (inst_iterator I = inst_begin(f); I != inst_end(f); I++) {
Instruction *Inst = &*I;
if (CallInst *CI = dyn_cast<CallInst>(Inst)) {
if (CI->getCalledFunction() != nullptr &&
CI->getCalledFunction()->getName().contains("hikari_" + attribute)) {
CI->eraseFromParent();
return true;
}
}
}
return false;
}
bool toObfuscate(bool flag, Function *f, std::string attribute) {
// Check if declaration
if (f->isDeclaration()) {
return false;
}
// Check external linkage
if (f->hasAvailableExternallyLinkage() != 0) {
return false;
}
std::string attr = attribute;
std::string attrNo = "no" + attr;
// We have to check the nofla flag first
// Because .find("fla") is true for a string like "fla" or
// "nofla"
if (readAnnotate(f).find(attrNo) != std::string::npos ||
readFlag(f, attrNo)) {
return false;
}
if (readAnnotate(f).find(attr) != std::string::npos || readFlag(f, attr)) {
return true;
}
if (flag == true) {
return true;
}
return false;
}