-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaultInjector.cpp
734 lines (631 loc) · 26.7 KB
/
FaultInjector.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//===----------------------------------------------------------------------===//
//
// LLVM Fault Injection Tool
//
//===----------------------------------------------------------------------===//
//
// Copyright (C) 2019. rollrat. All Rights Reserved.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Demangle/Demangle.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Dependency.h"
#include <map>
#include <random>
#include <set>
#include <sstream>
#include <string>
#define DEBUG_TYPE "fault-injection"
#define MARK_FUNCTION_NAME "__marking_faultinject"
#define IGNORE_ZERO_SIZE 1
#define USE_RAW_INJECT 1
using namespace llvm;
static std::map<const Type *, std::string> fi_rettype_funcname_map;
namespace {
void genFullNameOpcodeMap(std::map<std::string, unsigned> &opcodenamemap) {
#define HANDLE_INST(N, OPC, CLASS) \
opcodenamemap[std::string(Instruction::getOpcodeName(N))] = N;
#include "llvm/IR/Instruction.def"
}
static int a = 1;
///---------------------------------------------------------
///
/// Insert Machine
///
///---------------------------------------------------------
class FaultInjectionInsertMachine {
public:
//
// Insert code that does not depend on Instruction or Register.
//
static void insertMetaFunc(Module &M) {
//
// Find main function
//
Function *mainFunc = M.getFunction("main");
BasicBlock *entryBB = &mainFunc->front();
//
// Insert 'fault_inject_init' function.
//
Constant *initFunc = getInitFunction(M);
CallInst::Create(initFunc, "", entryBB->getFirstNonPHI());
//
// Insert 'fault_inject_finish' function.
//
Constant *finishFunc = getFinishFunction(M);
std::set<Instruction *> exitInsts;
getProgramExitInsts(M, exitInsts);
assert(exitInsts.size() != 0 &&
"Program does not have explicit exit point");
for (std::set<Instruction *>::iterator it = exitInsts.begin();
it != exitInsts.end(); ++it) {
Instruction *term = *it;
CallInst::Create(finishFunc, "", term);
}
//
// Insert 'inject_fault#num' functions.
//
createInjectionFunctions(M);
}
static void insertFaultInjection(Module &M, Function &F, Instruction *I,
int index, int f_index,
std::vector<int> ®s) {
for (auto reg : regs) insertFaultInjection(M, F, I, index, f_index, reg);
}
//
// Inject 'inject_fault#num' functions for test fault-injection.
//
static bool insertFaultInjection(Module &M, Function &F, Instruction *I,
int index, int f_index, int reg_num) {
Constant *inject_func = getInjectFunc(M);
Constant *trace_func = getTraceFunc(M);
Instruction *alloca_insertPoint =
I->getParent()->getParent()->begin()->getFirstNonPHIOrDbgOrLifetime();
Value *target = I;
if (reg_num != -1) {
target = I->getOperand(reg_num);
}
//
// Get 'inject_fault#num' parameter types.
//
Type *return_type = target->getType();
#if IGNORE_ZERO_SIZE
if (return_type->getScalarSizeInBits() == 0) return false;
#endif
std::vector<Type *> inject_func_types(6);
inject_func_types[0] = Type::getInt32Ty(M.getContext()); // f_index
inject_func_types[1] = Type::getInt32Ty(M.getContext()); // index
inject_func_types[2] = Type::getInt32Ty(M.getContext()); // reg_num
inject_func_types[3] = Type::getInt32Ty(M.getContext()); // dependecy
inject_func_types[4] = return_type; // data
inject_func_types[5] =
PointerType::get(Type::getInt8Ty(M.getContext()), 0); // name
ArrayRef<Type *> paramtypes_array_ref(inject_func_types);
FunctionType *injectfunctype =
FunctionType::get(return_type, paramtypes_array_ref, false);
std::string funcname = getFIFuncNameforType(return_type);
Constant *injectfunc = M.getOrInsertFunction(funcname, injectfunctype);
//
// Set 'inject_fault#num' parameters.
//
std::vector<Value *> args(6);
args[0] = ConstantInt::get(Type::getInt32Ty(M.getContext()), f_index);
args[1] = ConstantInt::get(Type::getInt32Ty(M.getContext()), index);
args[2] = ConstantInt::get(Type::getInt32Ty(M.getContext()), reg_num);
args[3] = ConstantInt::get(Type::getInt32Ty(M.getContext()),
getDepedencyLevel(I));
args[4] = target;
std::string opcode_str = I->getOpcodeName();
GlobalVariable *opcode_str_gv = findOrCreateGlobalNameString(M, opcode_str);
std::vector<Constant *> indices_for_gep(2);
indices_for_gep[0] = ConstantInt::get(Type::getInt32Ty(M.getContext()), 0);
indices_for_gep[1] = ConstantInt::get(Type::getInt32Ty(M.getContext()), 0);
ArrayRef<Constant *> indices_for_gep_array_ref(indices_for_gep);
Constant *gep_expr = ConstantExpr::getGetElementPtr(
NULL, opcode_str_gv, indices_for_gep_array_ref, true);
args[5] = gep_expr; // opcode in
ArrayRef<Value *> args_array_ref(args);
Instruction *insertptr = getInsertPtrforRegsofInst(target, I);
Instruction *ficall =
CallInst::Create(injectfunc, args_array_ref, "fi", insertptr);
//
// Concatenates the results of an existing register with the
// 'inject_fault#num' function.
//
std::list<User *> inst_uses;
for (Value::user_iterator user_it = target->user_begin();
user_it != target->user_end(); ++user_it) {
User *user = *user_it;
if (user != ficall) inst_uses.push_back(user);
}
for (std::list<User *>::iterator use_it = inst_uses.begin();
use_it != inst_uses.end(); ++use_it) {
User *user = *use_it;
user->replaceUsesOfWith(target, ficall);
if (isa<TerminatorInst>(user)) continue;
//
// Insert 'fault_inject_trace' function.
//
AllocaInst *tmploc = new AllocaInst(
return_type, M.getDataLayout().getProgramAddressSpace(),
"tmploc_" + intToString(a), &*F.getEntryBlock().begin());
StoreInst *store_inst =
new StoreInst(ficall, tmploc, cast<Instruction>(user)->getNextNode());
std::vector<Value *> trace_args(7);
trace_args[0] =
ConstantInt::get(Type::getInt32Ty(M.getContext()), f_index);
trace_args[1] = ConstantInt::get(Type::getInt32Ty(M.getContext()), index);
trace_args[2] =
ConstantInt::get(Type::getInt32Ty(M.getContext()), reg_num);
trace_args[3] = ConstantInt::get(Type::getInt32Ty(M.getContext()),
getDepedencyLevel(I));
trace_args[4] =
ConstantInt::get(Type::getInt32Ty(M.getContext()),
target->getType()->getScalarSizeInBits()); // size
trace_args[5] = new BitCastInst(
tmploc, PointerType::get(Type::getInt8Ty(M.getContext()), 0),
"tmploc_cast_" + intToString(a++),
cast<Instruction>(user)->getNextNode()->getNextNode());
trace_args[6] = gep_expr;
ArrayRef<Value *> trace_args_array_ref(trace_args);
CallInst::Create(
getTraceFunc(M), trace_args_array_ref, "",
cast<Instruction>(user)->getNextNode()->getNextNode()->getNextNode());
}
return true;
}
static void insertRawFaultInjection(Module &M, Function &F, Instruction *I, std::mt19937& R) {
std::uniform_int_distribution<std::mt19937::result_type> dist(0, 31);
int loc = dist(R);
auto xor_op = BinaryOperator::CreateXor(I, ConstantInt::get(Type::getInt32Ty(M.getContext()), ~(1 << loc)), "rfi", I->getNextNode());
std::list<User *> inst_uses;
for (Value::user_iterator user_it = I->user_begin();
user_it != I->user_end(); ++user_it) {
User *user = *user_it;
if (user != I && user != xor_op) inst_uses.push_back(user);
}
for (std::list<User *>::iterator use_it = inst_uses.begin();
use_it != inst_uses.end(); ++use_it) {
User *user = *use_it;
user->replaceUsesOfWith(I, xor_op);
}
F.print(errs());
}
static void insertDetermineLogicForRawFaultInjection(Module &M, Function &F, Instruction *I, Instruction *T, std::mt19937& R) {
// Create XOR setter and xor operand resetter
// ENTRY:
// %xor_marker = alloca i32, align 4
// store i32 (0~31 random number), %xor_marker, align 4
// CORE:
// %t = xor %target, %xor_marker
// store i32 0, %xor_marker
// pick random bits
std::uniform_int_distribution<std::mt19937::result_type> dist(0, 31);
int loc = dist(R);
IntegerType *type = Type::getInt32Ty(M.getContext());
AllocaInst *xor_marker =
new AllocaInst(type, M.getDataLayout().getProgramAddressSpace(),
"xor_marker", &*F.getEntryBlock().begin());
Value *num = ConstantInt::get(type, loc, true);
new StoreInst(num, xor_marker, &*++F.getEntryBlock().begin());
// Create xor and resetter
Value *num_zero = ConstantInt::get(type, 0, true);
StoreInst *resetter = new StoreInst(num_zero, xor_marker, I->getNextNode());
LoadInst *val = new LoadInst(xor_marker, "xor_val", resetter);
BinaryOperator *fi = BinaryOperator::CreateXor(I, val, "rfi", val->getNextNode());
/*for (int i = 0; i < I->getNumOperands(); i++)
if (I->getOperand(i) == T)
I->setOperand(i, fi);*/
std::list<User *> inst_uses;
for (Value::user_iterator user_it = I->user_begin();
user_it != I->user_end(); ++user_it) {
User *user = *user_it;
if (user != I && user != fi) inst_uses.push_back(user);
}
for (std::list<User *>::iterator use_it = inst_uses.begin();
use_it != inst_uses.end(); ++use_it) {
User *user = *use_it;
user->replaceUsesOfWith(I, fi);
}
F.print(errs());
}
private:
#pragma region Meta Functions
static Constant *getInitFunction(Module &M) {
LLVMContext &context = M.getContext();
FunctionType *fi_init_func_type =
FunctionType::get(Type::getVoidTy(context), false);
return M.getOrInsertFunction("fault_inject_init", fi_init_func_type);
}
static Constant *getFinishFunction(Module &M) {
LLVMContext &context = M.getContext();
FunctionType *fi_init_func_type =
FunctionType::get(Type::getVoidTy(context), false);
return M.getOrInsertFunction("fault_inject_finish", fi_init_func_type);
}
static Instruction *getTermInstofFunction(Function *func) {
BasicBlock &termbb = func->back();
Instruction *ret = termbb.getTerminator();
assert(isa<ReturnInst>(ret) ||
isa<UnreachableInst>(ret) &&
"Last instruction is not return or exit() instruction");
return ret;
}
static void getProgramExitInsts(Module &M,
std::set<Instruction *> &exitinsts) {
for (Module::iterator m_it = M.begin(); m_it != M.end(); ++m_it) {
if (!m_it->isDeclaration()) {
for (inst_iterator f_it = inst_begin(*m_it); f_it != inst_end(*m_it);
++f_it) {
Instruction *inst = &(*f_it);
if (CallInst *ci = dyn_cast<CallInst>(inst)) {
Function *calledFunc = ci->getCalledFunction();
if (calledFunc && calledFunc->hasName() &&
calledFunc->getName().str() == "exit") {
exitinsts.insert(inst);
}
}
}
}
}
Function *mainfunc = M.getFunction("main");
exitinsts.insert(getTermInstofFunction(mainfunc));
}
#pragma endregion
#pragma region Fault Injection Functions
static Instruction *getInsertPtrforRegsofInst(Value *reg, Instruction *inst) {
if (reg == inst) {
if (inst->isTerminator()) {
errs() << "ERROR: LLFI not able to inject into destination register of "
<< *inst << ", change isRegofInstInjectable() to fix it\n";
exit(2);
} else {
Instruction *nn = inst->getNextNode();
while (isa<PHINode>(nn)) nn = nn->getNextNode();
return &*nn;
}
} else {
if (isa<PHINode>(inst)) {
errs() << "ERROR: LLFI not able to inject into source register of "
<< *inst << ", change isRegofInstInjectable to fix it\n";
exit(2);
}
return inst->getNextNode();
}
}
static std::string intToString(int i) {
std::stringstream s;
s << i;
return s.str();
}
static std::string getFIFuncNameforType(const Type *type) {
std::string funcname;
if (fi_rettype_funcname_map.find(type) != fi_rettype_funcname_map.end()) {
funcname = fi_rettype_funcname_map[type];
} else {
funcname = "inject_fault";
int ficount = fi_rettype_funcname_map.size();
funcname += intToString(ficount);
fi_rettype_funcname_map[type] = funcname;
}
return funcname;
}
static GlobalVariable *findOrCreateGlobalNameString(Module &M,
std::string name) {
LLVMContext &context = M.getContext();
std::string str_prefix = std::string("fault_inject_");
std::string str_suffix = std::string("_namestr");
GlobalVariable *nameStr =
M.getGlobalVariable(str_prefix + name + str_suffix, true);
if (nameStr != NULL) return nameStr;
std::string gv_nameStr = str_prefix + name + str_suffix;
Constant *name_c = ConstantDataArray::getString(context, name);
nameStr = new GlobalVariable(name_c->getType(), true,
GlobalVariable::InternalLinkage, name_c,
gv_nameStr.c_str());
M.getGlobalList().push_back(nameStr);
return nameStr;
}
static Constant *getTraceFunc(Module &M) {
std::vector<Type *> profile_func_param_types(7);
LLVMContext &context = M.getContext();
profile_func_param_types[0] = Type::getInt32Ty(context); // function index
profile_func_param_types[1] = Type::getInt32Ty(context); // index
profile_func_param_types[2] = Type::getInt32Ty(context); // register number
profile_func_param_types[3] = Type::getInt32Ty(context); // dependency
profile_func_param_types[4] = Type::getInt32Ty(context); // size
profile_func_param_types[5] =
PointerType::get(Type::getInt8Ty(context), 0); // value
profile_func_param_types[6] =
PointerType::get(Type::getInt8Ty(context), 0); // opcode name
ArrayRef<Type *> profile_func_param_types_array_ref(
profile_func_param_types);
FunctionType *fi_init_func_type = FunctionType::get(
Type::getVoidTy(context), profile_func_param_types_array_ref, false);
return M.getOrInsertFunction("fault_inject_trace", fi_init_func_type);
}
static Constant *getInjectFunc(Module &M) {
std::vector<Type *> profile_func_param_types(7);
LLVMContext &context = M.getContext();
profile_func_param_types[0] = Type::getInt32Ty(context); // function index
profile_func_param_types[1] = Type::getInt32Ty(context); // index
profile_func_param_types[2] = Type::getInt32Ty(context); // register number
profile_func_param_types[3] = Type::getInt32Ty(context); // dependency
profile_func_param_types[4] = Type::getInt32Ty(context); // size
profile_func_param_types[5] =
PointerType::get(Type::getInt8Ty(context), 0); // value
profile_func_param_types[6] =
PointerType::get(Type::getInt8Ty(context), 0); // opcode name
ArrayRef<Type *> profile_func_param_types_array_ref(
profile_func_param_types);
FunctionType *fi_init_func_type = FunctionType::get(
Type::getVoidTy(context), profile_func_param_types_array_ref, false);
return M.getOrInsertFunction("fault_inject", fi_init_func_type);
}
static Constant *getDetermineFunc(Module &M) {
std::vector<Type *> profile_func_param_types(4);
LLVMContext &context = M.getContext();
profile_func_param_types[0] = Type::getInt32Ty(context); // function index
profile_func_param_types[1] = Type::getInt32Ty(context); // index
profile_func_param_types[2] = Type::getInt32Ty(context); // register number
profile_func_param_types[3] = Type::getInt32Ty(context); // dependency
ArrayRef<Type *> profile_func_param_types_array_ref(
profile_func_param_types);
FunctionType *fi_init_func_type = FunctionType::get(
Type::getInt1Ty(context), profile_func_param_types_array_ref, false);
return M.getOrInsertFunction("fault_inject_determine", fi_init_func_type);
}
static void createInjectionFunctions(Module &M) {
Constant *pre_fi_func = getDetermineFunc(M);
Constant *injectfunc = getInjectFunc(M);
Constant *tracefunc = getTraceFunc(M);
for (std::map<const Type *, std::string>::const_iterator fi =
fi_rettype_funcname_map.begin();
fi != fi_rettype_funcname_map.end(); ++fi) {
const Type *fi_type = fi->first;
Type *fi_type_unconst = const_cast<Type *>(fi_type);
std::string fi_name = fi->second;
createInjectionFuncforType(M, fi_type_unconst, fi_name, injectfunc,
pre_fi_func);
}
}
static void createInjectionFuncforType(Module &M, Type *fitype,
std::string &fi_name,
Constant *injectfunc,
Constant *pre_fi_func) {
LLVMContext &context = M.getContext();
Function *f = M.getFunction(fi_name);
std::vector<Value *> args;
for (Function::arg_iterator ai = f->arg_begin(); ai != f->arg_end(); ++ai)
args.push_back(&*ai);
BasicBlock *entryblock = BasicBlock::Create(context, "entry", f);
// store the value of target instruction to memory
AllocaInst *tmploc =
new AllocaInst(fitype, M.getDataLayout().getProgramAddressSpace(),
"tmploc", entryblock);
new StoreInst(args[4], tmploc, entryblock);
std::vector<Value *> pre_fi_args(4);
pre_fi_args[0] = args[0]; // f_index
pre_fi_args[1] = args[1]; // index
pre_fi_args[2] = args[2]; // reg_num
pre_fi_args[3] = args[3]; // dependency
ArrayRef<Value *> pre_fi_args_array_ref(pre_fi_args);
Value *prefuncval = CallInst::Create(pre_fi_func, pre_fi_args_array_ref,
"pre_cond", entryblock);
BasicBlock *fiblock = BasicBlock::Create(context, "inject", f);
BasicBlock *exitblock = BasicBlock::Create(context, "exit", f);
// if prefuncval is true, goto inject function
BranchInst::Create(fiblock, exitblock, prefuncval, entryblock);
BranchInst *fi2exit_branch = BranchInst::Create(exitblock, fiblock);
std::vector<Value *> fi_args(7);
fi_args[0] = args[0]; // f_index
int size = fitype->getScalarSizeInBits();
fi_args[1] = args[1]; // index
fi_args[2] = args[2]; // reg_num
fi_args[3] = args[3]; // dependency
fi_args[4] = ConstantInt::get(Type::getInt32Ty(context), size); // size
fi_args[5] = new BitCastInst(
tmploc, PointerType::get(Type::getInt8Ty(context), 0), "tmploc_cast",
fi2exit_branch); // pointer to target memory
fi_args[6] = args[5]; // opcode name
ArrayRef<Value *> fi_args_array_ref(fi_args);
CallInst::Create(injectfunc, fi_args_array_ref, "", fi2exit_branch);
LoadInst *updateval = new LoadInst(tmploc, "updateval", exitblock);
ReturnInst::Create(context, updateval, exitblock);
}
static int getDepedencyLevel(Instruction *I) {
// auto mgr = getInfoManager(I->getDebugLoc()->getLine());
// if (mgr == nullptr)
return -1;
// mgr->doFolding();
// return (*mgr->begin())->getType();
}
#pragma endregion
};
///---------------------------------------------------------
///
/// Profile Collector
///
///---------------------------------------------------------
class FaultInjectionTargetSelector {
Function *target_function;
std::vector<std::pair<Instruction *, int>> selected;
public:
FaultInjectionTargetSelector(Function *TargetFunction)
: target_function(TargetFunction) {}
void selectInstructions() {
std::vector<CallInst *> marker;
for (auto &bb : *target_function) {
for (auto &inst : bb) {
#if !USE_RAW_INJECT
// [===== Store Instruction =====]
// if (isa<StoreInst> (inst)) {
// if (inst.getOperand(0)->getType()->isIntegerTy())
// continue;
// selected.push_back({ &inst, 0 });
// inst.print(errs(), true);
// errs() << "\ninstrcution selected\n";
//}
//// [===== Load Instruction =====]
// else if (isa<LoadInst>(inst)) {
// if (!isa<Instruction>(inst.getOperand(0)))
// continue;
//
// selected.push_back({ &inst, -1 });
// inst.print(errs(), true);
// errs() << "\ninstrcution selected\n";
//}
// destination register
if (isRegofInstFITarget(&inst, &inst))
if (isRegofInstInjectable(&inst, &inst))
selected.push_back({&inst, -1});
// source register
int pos = 0;
for (User::op_iterator op_it = inst.op_begin(); op_it != inst.op_end();
++op_it, ++pos) {
Value *src = *op_it;
if (isRegofInstFITarget(src, &inst, pos)) {
if (isRegofInstInjectable(src, &inst)) {
selected.push_back({&inst, pos});
} else {
errs() << "LLFI cannot inject faults in source reg ";
if (isa<BasicBlock>(src))
errs() << src->getName();
else
errs() << *src;
errs() << " of instruction " << inst << "\n";
}
}
}
/*if (isa<StoreInst>(inst))
selected.push_back({ &inst, 1 });*/
#else
if (CallInst *call = dyn_cast<CallInst>(&inst)) {
if (call->getCalledFunction()->getName().startswith(MARK_FUNCTION_NAME)) {
auto target = cast<Instruction>(call->getOperand(0));
selected.push_back({ target, 0 });
std::list<User *> inst_uses;
for (Value::user_iterator user_it = target->user_begin();
user_it != target->user_end(); ++user_it) {
User *user = *user_it;
if (user != call) inst_uses.push_back(user);
}
for (std::list<User *>::iterator use_it = inst_uses.begin();
use_it != inst_uses.end(); ++use_it) {
if (!isa<PHINode>(*use_it)) {
if (isa<StoreInst>(*use_it))
if (cast<StoreInst>(*use_it)->getPointerOperand() == target)
continue;
if (!isa<LoadInst>(*use_it))
continue;
selected.push_back({ cast<Instruction>(*use_it),-1 });
errs() << "SELECT : " << *cast<Instruction>(*use_it) << '\n';
}
}
marker.push_back(call);
}
}
#endif
}
}
for (auto ci : marker)
ci->eraseFromParent();
}
std::vector<std::pair<Instruction *, int>> getSelectedInsts() {
return selected;
}
private:
bool isRegofInstFITarget(Value *reg, Instruction *inst) { return true; }
bool isRegofInstFITarget(Value *reg, Instruction *inst, int pos) {
return false;
}
bool isRegofInstInjectable(Value *reg, Instruction *inst) {
// TODO: keep updating
// if we find anything that can be covered, remove them from the checks
// if we find new cases that we cannot handle, add them to the checks
if (reg == inst) {
if (inst->getType()->isVoidTy() || isa<TerminatorInst>(inst)) {
return false;
}
} else {
if (isa<BasicBlock>(reg) || isa<PHINode>(inst)) return false;
}
return true;
}
};
class ProfileCollector {
std::set<std::string> function_name;
public:
static ProfileCollector *Instance;
ProfileCollector(Module &M) { getModuleFuncs(M); }
static ProfileCollector *getInstance(Module &M) {
if (Instance == nullptr) Instance = new ProfileCollector(M);
return Instance;
}
void getModuleFuncs(Module &M) {
for (Module::iterator it = M.begin(); it != M.end(); ++it) {
std::string func_name = it->getName().str();
// std::string final_name = demangleFuncName(func_name);
function_name.insert(func_name);
}
}
};
///---------------------------------------------------------
///
/// LLVM Fault Injection Pass
///
///---------------------------------------------------------
struct LLVMFaultInjectionPass : public ModulePass {
static char ID;
LLVMFaultInjectionPass() : ModulePass(ID) {}
~LLVMFaultInjectionPass() {}
bool runOnModule(Module &M) override {
if (!M.getFunction("main")) {
errs() << "Error: main function not found.";
exit(1);
}
int count_of_selection = 0;
int f_index = 0;
std::mt19937 rng;
rng.seed(std::random_device()());
for (Module::iterator m_it = M.begin(); m_it != M.end();
++m_it, ++f_index) {
//if (m_it->getName() != "main") continue;
FaultInjectionTargetSelector selector(&*m_it);
selector.selectInstructions();
#if !USE_RAW_INJECT
for (auto &inst : selector.getSelectedInsts()) {
if (FaultInjectionInsertMachine::insertFaultInjection(
M, *m_it, inst.first, count_of_selection, f_index, inst.second))
count_of_selection++;
}
#else
if (selector.getSelectedInsts().size() == 0) continue;
std::uniform_int_distribution<std::mt19937::result_type> dist(1, selector.getSelectedInsts().size() - 1);
auto selected = selector.getSelectedInsts()[dist(rng)].first;
FaultInjectionInsertMachine::insertDetermineLogicForRawFaultInjection(M, *m_it, selected, selector.getSelectedInsts()[0].first, rng);
break;
#endif
}
#if !USE_RAW_INJECT
FaultInjectionInsertMachine::insertMetaFunc(M);
#endif
return true;
}
};
} // namespace
char LLVMFaultInjectionPass::ID = 0;
static RegisterPass<LLVMFaultInjectionPass> X("faultinject",
"Fault Injection Pass",
false /* Only looks at CFG */,
false /* Analysis Pass */);