Skip to content

Commit c690afe

Browse files
committed
opt: Add faultinjection pass
1 parent b67166d commit c690afe

File tree

4 files changed

+1622
-0
lines changed

4 files changed

+1622
-0
lines changed

tools/opt/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ add_llvm_tool(opt
3131
NewPMDriver.cpp
3232
PassPrinters.cpp
3333
PrintSCC.cpp
34+
FaultInjector.cpp
35+
InjectFaultProc.cpp
36+
DeleteInjectFunc.cpp
3437
opt.cpp
3538

3639
DEPENDS

tools/opt/DeleteInjectFunc.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// LLVM Fault Injection Tool
4+
//
5+
//===----------------------------------------------------------------------===//
6+
//
7+
// Copyright (C) 2019. rollrat. All Rights Reserved.
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
#include "llvm/ADT/SmallVector.h"
12+
#include "llvm/Analysis/LoopInfo.h"
13+
#include "llvm/Demangle/Demangle.h"
14+
#include "llvm/IR/CFG.h"
15+
#include "llvm/IR/Constants.h"
16+
#include "llvm/IR/DebugInfoMetadata.h"
17+
#include "llvm/IR/Function.h"
18+
#include "llvm/IR/InstIterator.h"
19+
#include "llvm/IR/Instructions.h"
20+
#include "llvm/Pass.h"
21+
#include "llvm/Support/Casting.h"
22+
#include "llvm/Support/raw_ostream.h"
23+
#include <map>
24+
#include <random>
25+
#include <set>
26+
#include <sstream>
27+
#include <string>
28+
29+
#define DEBUG_TYPE "fault-injection"
30+
31+
#define MARK_FUNCTION_NAME "__faultinject_selected_target"
32+
#define IGNORE_ZERO_SIZE 1
33+
#define USE_RAW_INJECT 1
34+
35+
using namespace llvm;
36+
37+
namespace {
38+
39+
struct LLVMDeleteInjectionPass : public ModulePass {
40+
static char ID;
41+
42+
LLVMDeleteInjectionPass() : ModulePass(ID) {}
43+
44+
~LLVMDeleteInjectionPass() {}
45+
46+
bool runOnModule(Module &M) override {
47+
for (Module::iterator m_it = M.begin(); m_it != M.end();
48+
++m_it) {
49+
std::vector<CallInst *> mm;
50+
for (auto &bb : *m_it)
51+
for (auto &inst : bb)
52+
if (CallInst *call = dyn_cast<CallInst>(&inst))
53+
if (call->getCalledFunction() && call->getCalledFunction()->getName().contains("__faultinject_selected_target"))
54+
mm.push_back(call);
55+
56+
for (auto &ci : mm) {
57+
std::list<User *> inst_uses;
58+
for (Value::user_iterator user_it = ci->user_begin();
59+
user_it != ci->user_end(); ++user_it) {
60+
User *user = *user_it;
61+
if (user != ci)
62+
inst_uses.push_back(user);
63+
}
64+
auto uu = ci->getOperand(0);
65+
for (std::list<User *>::iterator use_it = inst_uses.begin();
66+
use_it != inst_uses.end(); ++use_it) {
67+
User *user = *use_it;
68+
user->replaceUsesOfWith(ci, uu);
69+
}
70+
ci->eraseFromParent();
71+
}
72+
}
73+
74+
return true;
75+
}
76+
};
77+
78+
}
79+
80+
char LLVMDeleteInjectionPass::ID = 0;
81+
82+
static RegisterPass<LLVMDeleteInjectionPass> X("deleteinjectfunc",
83+
"Delete Fault injection target marking function.",
84+
false /* Only looks at CFG */,
85+
false /* Analysis Pass */);

0 commit comments

Comments
 (0)