-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSubstitution.cpp
More file actions
422 lines (362 loc) · 15.9 KB
/
Copy pathSubstitution.cpp
File metadata and controls
422 lines (362 loc) · 15.9 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//===- Substitution.cpp - Instruction substitution obfuscation -----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE for details.
//
// Modifications Copyright (c) 2026 Danny Mundy
//
//===----------------------------------------------------------------------===//
//
// Implements instruction substitution patterns and the pass driver.
//
//===----------------------------------------------------------------------===//
#include "Substitution.h"
#include "CryptoUtils.h"
#include "Utils.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO.h"
using namespace llvm;
#define DEBUG_TYPE "substitution"
STATISTIC(Add, "Add substituted");
STATISTIC(Sub, "Sub substituted");
STATISTIC(And, "And substituted");
STATISTIC(Or, "Or substituted");
STATISTIC(Xor, "Xor substituted");
namespace {
cl::opt<bool> Substitution("sub", cl::init(false),
cl::desc("Enable instruction substitutions"));
cl::opt<int>
ObfTimes("sub_loop",
cl::desc("Choose how many time the -sub pass loops on a function"),
cl::value_desc("number of times"), cl::init(1), cl::Optional);
bool substitute(Function *f, std::size_t origBBs, std::size_t origInsts);
void addNeg(BinaryOperator *bo);
void addDoubleNeg(BinaryOperator *bo);
void addRand(BinaryOperator *bo);
void addRand2(BinaryOperator *bo);
void subNeg(BinaryOperator *bo);
void subRand(BinaryOperator *bo);
void subRand2(BinaryOperator *bo);
void andSubstitution(BinaryOperator *bo);
void andSubstitutionRand(BinaryOperator *bo);
void orSubstitution(BinaryOperator *bo);
void orSubstitutionRand(BinaryOperator *bo);
void xorSubstitution(BinaryOperator *bo);
void xorSubstitutionRand(BinaryOperator *bo);
// Each opcode has a table of algebraically equivalent rewrites; the pass
// picks one at random per instruction to diversify output across builds.
#define NUMBER_ADD_SUBST 4
#define NUMBER_SUB_SUBST 3
#define NUMBER_AND_SUBST 2
#define NUMBER_OR_SUBST 2
#define NUMBER_XOR_SUBST 2
void (*funcAdd[NUMBER_ADD_SUBST])(BinaryOperator *bo) = {&addNeg, &addDoubleNeg,
&addRand, &addRand2};
void (*funcSub[NUMBER_SUB_SUBST])(BinaryOperator *bo) = {&subNeg, &subRand,
&subRand2};
void (*funcAnd[NUMBER_AND_SUBST])(BinaryOperator *bo) = {&andSubstitution,
&andSubstitutionRand};
void (*funcOr[NUMBER_OR_SUBST])(BinaryOperator *bo) = {&orSubstitution,
&orSubstitutionRand};
void (*funcXor[NUMBER_XOR_SUBST])(BinaryOperator *bo) = {&xorSubstitution,
&xorSubstitutionRand};
bool substitute(Function *f, std::size_t origBBs, std::size_t origInsts) {
Function *tmp = f;
bool Changed = false;
int times = ObfTimes;
do {
for (Function::iterator bb = tmp->begin(); bb != tmp->end(); ++bb) {
if (shouldSkipBlock(&*bb)) {
continue;
}
for (BasicBlock::iterator inst = bb->begin(); inst != bb->end();) {
Instruction *Cur = &*inst++;
if (shouldSkipInstruction(Cur)) {
continue;
}
if (isArithObf(*Cur)) {
continue;
}
if (auto *BO = dyn_cast<BinaryOperator>(Cur)) {
Type *Ty = BO->getType();
if (!Ty->isIntegerTy() || Ty->isVectorTy()) {
continue;
}
// nsw/nuw/exact flags can't be preserved through substitution.
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO)) {
if (OBO->hasNoSignedWrap() || OBO->hasNoUnsignedWrap())
continue;
}
if (auto *PE = dyn_cast<PossiblyExactOperator>(BO)) {
if (PE->isExact())
continue;
}
Instruction *Prev = BO->getPrevNode();
bool Replaced = false;
switch (BO->getOpcode()) {
case BinaryOperator::Add:
(funcAdd[llvm::cryptoutils->get_range(NUMBER_ADD_SUBST)])(BO);
++Add;
Replaced = true;
break;
case BinaryOperator::Sub:
(funcSub[llvm::cryptoutils->get_range(NUMBER_SUB_SUBST)])(BO);
++Sub;
Replaced = true;
break;
// No substitution patterns for mul/div/rem/shift yet.
case Instruction::And:
(funcAnd[llvm::cryptoutils->get_range(2)])(BO);
++And;
Replaced = true;
break;
case Instruction::Or:
(funcOr[llvm::cryptoutils->get_range(2)])(BO);
++Or;
Replaced = true;
break;
case Instruction::Xor:
(funcXor[llvm::cryptoutils->get_range(2)])(BO);
++Xor;
Replaced = true;
break;
default:
break;
}
if (Replaced) {
// Tag the inserted range to avoid re-obfuscating our own ops.
markInsertedRange(*bb, Prev, BO, "sub");
Changed = true;
}
if (Replaced && BO->use_empty())
BO->eraseFromParent();
}
}
}
} while (--times > 0 && checkObfuscationBudget(*tmp, origBBs, origInsts, "sub"));
return Changed;
}
// Implementation of a = b - (-c)
void addNeg(BinaryOperator *bo) {
BinaryOperator *op = nullptr;
if (bo->getOpcode() == Instruction::Add) {
op = BinaryOperator::CreateNeg(bo->getOperand(1), "", bo->getIterator());
op =
BinaryOperator::Create(Instruction::Sub, bo->getOperand(0), op, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
}
// Implementation of a = -(-b + (-c))
void addDoubleNeg(BinaryOperator *bo) {
Instruction *op, *op2 = nullptr;
if (bo->getOpcode() == Instruction::Add) {
op = BinaryOperator::CreateNeg(bo->getOperand(0), "", bo->getIterator());
op2 = BinaryOperator::CreateNeg(bo->getOperand(1), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Add, op, op2, "", bo->getIterator());
op = BinaryOperator::CreateNeg(op, "", bo->getIterator());
// Check signed wrap
// op->setHasNoSignedWrap(bo->hasNoSignedWrap());
// op->setHasNoUnsignedWrap(bo->hasNoUnsignedWrap());
} else {
op = UnaryOperator::CreateFNeg(bo->getOperand(0), "", bo->getIterator());
op2 = UnaryOperator::CreateFNeg(bo->getOperand(1), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::FAdd, op, op2, "", bo->getIterator());
op = UnaryOperator::CreateFNeg(op, "", bo->getIterator());
}
bo->replaceAllUsesWith(op);
}
// Implementation of r = rand (); a = b + r; a = a + c; a = a - r
void addRand(BinaryOperator *bo) {
BinaryOperator *op = nullptr;
if (bo->getOpcode() == Instruction::Add) {
Type *ty = bo->getType();
ConstantInt *co =
(ConstantInt *)ConstantInt::get(ty, llvm::cryptoutils->get_uint64_t());
op =
BinaryOperator::Create(Instruction::Add, bo->getOperand(0), co, "", bo->getIterator());
op =
BinaryOperator::Create(Instruction::Add, op, bo->getOperand(1), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Sub, op, co, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
}
// Implementation of r = rand (); a = b - r; a = a + b; a = a + r
void addRand2(BinaryOperator *bo) {
BinaryOperator *op = nullptr;
if (bo->getOpcode() == Instruction::Add) {
Type *ty = bo->getType();
ConstantInt *co =
(ConstantInt *)ConstantInt::get(ty, llvm::cryptoutils->get_uint64_t());
op =
BinaryOperator::Create(Instruction::Sub, bo->getOperand(0), co, "", bo->getIterator());
op =
BinaryOperator::Create(Instruction::Add, op, bo->getOperand(1), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Add, op, co, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
}
// Implementation of a = b + (-c)
void subNeg(BinaryOperator *bo) {
Instruction *op = nullptr;
if (bo->getOpcode() == Instruction::Sub) {
op = BinaryOperator::CreateNeg(bo->getOperand(1), "", bo->getIterator());
op =
BinaryOperator::Create(Instruction::Add, bo->getOperand(0), op, "", bo->getIterator());
// Check signed wrap
// op->setHasNoSignedWrap(bo->hasNoSignedWrap());
// op->setHasNoUnsignedWrap(bo->hasNoUnsignedWrap());
} else {
op = UnaryOperator::CreateFNeg(bo->getOperand(1), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::FAdd, bo->getOperand(0), op, "", bo->getIterator());
}
bo->replaceAllUsesWith(op);
}
// Implementation of r = rand (); a = b + r; a = a - c; a = a - r
void subRand(BinaryOperator *bo) {
BinaryOperator *op = nullptr;
if (bo->getOpcode() == Instruction::Sub) {
Type *ty = bo->getType();
ConstantInt *co =
(ConstantInt *)ConstantInt::get(ty, llvm::cryptoutils->get_uint64_t());
op =
BinaryOperator::Create(Instruction::Add, bo->getOperand(0), co, "", bo->getIterator());
op =
BinaryOperator::Create(Instruction::Sub, op, bo->getOperand(1), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Sub, op, co, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
}
// Implementation of r = rand (); a = b - r; a = a - c; a = a + r
void subRand2(BinaryOperator *bo) {
BinaryOperator *op = nullptr;
if (bo->getOpcode() == Instruction::Sub) {
Type *ty = bo->getType();
ConstantInt *co =
(ConstantInt *)ConstantInt::get(ty, llvm::cryptoutils->get_uint64_t());
op =
BinaryOperator::Create(Instruction::Sub, bo->getOperand(0), co, "", bo->getIterator());
op =
BinaryOperator::Create(Instruction::Sub, op, bo->getOperand(1), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Add, op, co, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
}
// a = b & c => a = (b ^ ~c) & b
void andSubstitution(BinaryOperator *bo) {
BinaryOperator *op = nullptr;
op = BinaryOperator::CreateNot(bo->getOperand(1), "", bo->getIterator());
BinaryOperator *op1 =
BinaryOperator::Create(Instruction::Xor, bo->getOperand(0), op, "", bo->getIterator());
op = BinaryOperator::Create(Instruction::And, op1, bo->getOperand(0), "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
// a = a & b <=> !(!a | !b) & (r | !r)
// The (r | !r) tautology adds junk ops without changing the result.
void andSubstitutionRand(BinaryOperator *bo) {
Type *ty = bo->getType();
ConstantInt *co =
(ConstantInt *)ConstantInt::get(ty, llvm::cryptoutils->get_uint64_t());
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(0), "", bo->getIterator());
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo->getIterator());
BinaryOperator *opr = BinaryOperator::CreateNot(co, "", bo->getIterator());
BinaryOperator *opa =
BinaryOperator::Create(Instruction::Or, op, op1, "", bo->getIterator());
opr = BinaryOperator::Create(Instruction::Or, co, opr, "", bo->getIterator());
op = BinaryOperator::CreateNot(opa, "", bo->getIterator());
op = BinaryOperator::Create(Instruction::And, op, opr, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
// a = b | c via (a ^ r) ^ (b ^ r) | (a & b), randomized with tautology terms.
void orSubstitutionRand(BinaryOperator *bo) {
Type *ty = bo->getType();
ConstantInt *co =
(ConstantInt *)ConstantInt::get(ty, llvm::cryptoutils->get_uint64_t());
BinaryOperator *op = BinaryOperator::CreateNot(bo->getOperand(0), "", bo->getIterator());
BinaryOperator *op1 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo->getIterator());
BinaryOperator *op2 = BinaryOperator::CreateNot(co, "", bo->getIterator());
BinaryOperator *op3 =
BinaryOperator::Create(Instruction::And, op, co, "", bo->getIterator());
BinaryOperator *op4 =
BinaryOperator::Create(Instruction::And, bo->getOperand(0), op2, "", bo->getIterator());
BinaryOperator *op5 =
BinaryOperator::Create(Instruction::And, op1, co, "", bo->getIterator());
BinaryOperator *op6 =
BinaryOperator::Create(Instruction::And, bo->getOperand(1), op2, "", bo->getIterator());
op3 = BinaryOperator::Create(Instruction::Or, op3, op4, "", bo->getIterator());
op4 = BinaryOperator::Create(Instruction::Or, op5, op6, "", bo->getIterator());
op5 = BinaryOperator::Create(Instruction::Xor, op3, op4, "", bo->getIterator());
op3 = BinaryOperator::Create(Instruction::Or, op, op1, "", bo->getIterator());
op3 = BinaryOperator::CreateNot(op3, "", bo->getIterator());
op4 = BinaryOperator::Create(Instruction::Or, co, op2, "", bo->getIterator());
op4 = BinaryOperator::Create(Instruction::And, op3, op4, "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Or, op5, op4, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
// a = b | c => (b & c) | (b ^ c)
void orSubstitution(BinaryOperator *bo) {
BinaryOperator *op = nullptr;
op = BinaryOperator::Create(Instruction::And, bo->getOperand(0),
bo->getOperand(1), "", bo->getIterator());
BinaryOperator *op1 = BinaryOperator::Create(
Instruction::Xor, bo->getOperand(0), bo->getOperand(1), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Or, op, op1, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
// a = a ^ b => (!a & b) | (a & !b)
void xorSubstitution(BinaryOperator *bo) {
BinaryOperator *op = nullptr;
op = BinaryOperator::CreateNot(bo->getOperand(0), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::And, bo->getOperand(1), op, "", bo->getIterator());
BinaryOperator *op1 =
BinaryOperator::CreateNot(bo->getOperand(1), "", bo->getIterator());
op1 = BinaryOperator::Create(Instruction::And, bo->getOperand(0), op1, "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Or, op, op1, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
// a = a ^ b <=> (a ^ r) ^ (b ^ r), expanded through boolean ops with random r.
void xorSubstitutionRand(BinaryOperator *bo) {
BinaryOperator *op = nullptr;
Type *ty = bo->getType();
ConstantInt *co =
(ConstantInt *)ConstantInt::get(ty, llvm::cryptoutils->get_uint64_t());
op = BinaryOperator::CreateNot(bo->getOperand(0), "", bo->getIterator());
op = BinaryOperator::Create(Instruction::And, co, op, "", bo->getIterator());
BinaryOperator *opr = BinaryOperator::CreateNot(co, "", bo->getIterator());
BinaryOperator *op1 =
BinaryOperator::Create(Instruction::And, bo->getOperand(0), opr, "", bo->getIterator());
BinaryOperator *op2 = BinaryOperator::CreateNot(bo->getOperand(1), "", bo->getIterator());
op2 = BinaryOperator::Create(Instruction::And, op2, co, "", bo->getIterator());
BinaryOperator *op3 =
BinaryOperator::Create(Instruction::And, bo->getOperand(1), opr, "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Or, op, op1, "", bo->getIterator());
op1 = BinaryOperator::Create(Instruction::Or, op2, op3, "", bo->getIterator());
op = BinaryOperator::Create(Instruction::Xor, op, op1, "", bo->getIterator());
bo->replaceAllUsesWith(op);
}
} // namespace
PreservedAnalyses SubstitutionPass::run(Function &F,
FunctionAnalysisManager &AM) {
if (ObfTimes <= 0) {
errs() << "Substitution application number -sub_loop=x must be x > 0";
return PreservedAnalyses::all();
}
if (toObfuscate(Substitution, &F, "sub")) {
ObfPassContext Ctx = beginFunctionObfuscation(F, "sub");
bool Changed = substitute(&F, Ctx.OrigBBs, Ctx.OrigInsts);
finishFunctionObfuscation(F, "sub", Ctx, Changed, false);
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
}
return PreservedAnalyses::all();
}