-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathreentrancy.cpp
145 lines (120 loc) · 7.01 KB
/
reentrancy.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
/**
* @file reentrancy.cpp
* @brief find possible reentrancy
*
*/
#include "near_core.h"
#include <fstream>
#include <set>
#include <string>
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Pass.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
namespace {
struct Reentrancy : public llvm::FunctionPass {
static char ID;
private:
llvm::raw_fd_ostream *os = nullptr;
public:
Reentrancy() : FunctionPass(ID) {
std::error_code EC;
os = new llvm::raw_fd_ostream(std::string(getenv("TMP_DIR")) + std::string("/.reentrancy.tmp"), EC, llvm::sys::fs::OpenFlags::OF_Append);
}
~Reentrancy() { os->close(); }
bool runOnFunction(llvm::Function &F) override {
using namespace llvm;
StringRef const funcFileName;
if (!Rustle::debug_check_all_func && Rustle::regexForLibFunc.match(F.getName()))
return false;
if (Rustle::debug_print_function)
Rustle::Logger().Debug("Checking function ", F.getName());
for (BasicBlock &BB : F)
for (Instruction &I : BB) {
if (!I.getDebugLoc().get() || Rustle::regexForLibLoc.match(I.getDebugLoc().get()->getFilename()))
continue;
if (CallBase *callInst = dyn_cast<CallBase>(&I)) {
if (!callInst->getCalledFunction())
continue;
if (Rustle::regexPromiseResult.match(callInst->getCalledFunction()->getName())) {
BasicBlock *successfulBlock = nullptr;
std::set<Value *> pmRsUsers;
Rustle::simpleFindUsers(callInst->getArgOperand(0), pmRsUsers, false, true); // callInst->getArgOperand(0) is the return value of `promise_result`
for (auto *i : pmRsUsers) {
if (auto *switchInst = dyn_cast<SwitchInst>(i)) {
for (auto caseIt : switchInst->cases()) {
if (caseIt.getCaseValue()->equalsInt(1)) { // PromiseResult::Successful == 1
successfulBlock = caseIt.getCaseSuccessor();
break;
}
}
}
if (successfulBlock)
break;
}
if (successfulBlock) {
bool hasNewBlock = true;
auto *currentBlock = successfulBlock;
while (hasNewBlock) {
hasNewBlock = false;
// Rustle::Logger().Debug(currentBlock->getName());
for (Instruction &I : *currentBlock) {
// branch inst, branch between basic block
if (auto *branchInst = dyn_cast<BranchInst>(&I)) {
for (unsigned i = 0; i < branchInst->getNumSuccessors(); i++) {
if (branchInst->getSuccessor(i)->getName().startswith("bb")) { // ignore panic or so
currentBlock = branchInst->getSuccessor(i);
hasNewBlock = true;
}
}
}
// store inst, check store
if (auto *storeInst = dyn_cast<StoreInst>(&I)) {
bool usedInReturn = false;
bool useSelf = false;
std::set<Value *> stLocUsers;
Rustle::simpleFindUsers(storeInst->getPointerOperand(), stLocUsers); // find users of store location
for (auto *i : stLocUsers) {
if (auto *retInst = dyn_cast<ReturnInst>(i)) { // find if store location is used in return value of `F`
// [!] below is code to strictly check whether return type equals stored value's type, but it's unnecessary in most cases
// if (retInst->getReturnValue() && storeInst->getValueOperand()->getType() == retInst->getReturnValue()->getType()) {
usedInReturn = true;
break;
// }
}
}
std::set<Value *> selfUsers;
Rustle::simpleFindUsers(F.getArg(0), selfUsers); // find users of `&self`
for (auto *i : selfUsers) {
if (i == storeInst->getPointerOperand()) {
useSelf = true;
break;
}
}
if (!usedInReturn && useSelf) {
Rustle::Logger().Warning("Changing state upon PromiseResult::Successful at ", I.getDebugLoc(), " may lead to reentrancy.");
*os << F.getName() << "@" << I.getDebugLoc()->getFilename() << "@" << I.getDebugLoc().getLine() << "\n";
return false;
}
}
}
}
return false;
}
}
}
}
return false;
}
};
} // namespace
char Reentrancy::ID = 0;
static llvm::RegisterPass<Reentrancy> X("reentrancy", "", false /* Only looks at CFG */, false /* Analysis Pass */);
static llvm::RegisterStandardPasses Y(llvm::PassManagerBuilder::EP_EarlyAsPossible, [](const llvm::PassManagerBuilder &builder, llvm::legacy::PassManagerBase &PM) { PM.add(new Reentrancy()); });