-
Notifications
You must be signed in to change notification settings - Fork 15
/
unclaimed_storage_fee.cpp
146 lines (126 loc) · 6.42 KB
/
unclaimed_storage_fee.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
/**
* @file unclaimed_storage_fee.cpp
* @brief check if there is a check for storage fee balance in `storage_unregister`, the balance should be 0 before withdrawing
*
*/
#include "near_core.h"
#include <fstream>
#include <set>
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.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 UnclaimedStorageFee : public llvm::ModulePass {
static char ID;
private:
llvm::raw_fd_ostream *os = nullptr;
llvm::Regex const regex_storage_unregister = llvm::Regex("[0-9]+storage_unregister[0-9]+");
/**
* @brief find if there is `icmp eq/ne/ge/le balance, 0` in *F
*
* @param F pointer to the target function
*/
bool hasBalanceCmp(llvm::Function *F, int depth = 3) {
using namespace llvm;
if (depth <= 0)
return false;
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 (auto &BB : *F) {
for (auto &I : BB) {
if (isa<CallBase>(I) && hasBalanceCmp(dyn_cast<CallBase>(&I)->getCalledFunction(), depth - 1)) {
return true;
}
if (auto *icmpInst = dyn_cast<ICmpInst>(&I)) {
if (icmpInst->getOperand(0)->getType()->isIntegerTy(128)) { // balance is a 128-bit integer (unsigned or signed)
// balance == 0, balance != 0
if (icmpInst->isEquality() && isa<ConstantInt>(icmpInst->getOperand(1)) && dyn_cast<ConstantInt>(icmpInst->getOperand(1))->getZExtValue() == 0) {
// Rustle::Logger().Debug(icmpInst);
return true;
}
// 0 == balance, 0 != balance
if (icmpInst->isEquality() && isa<ConstantInt>(icmpInst->getOperand(0)) && dyn_cast<ConstantInt>(icmpInst->getOperand(0))->getZExtValue() == 0) {
// Rustle::Logger().Debug(icmpInst);
return true;
}
switch (icmpInst->getPredicate()) {
// balance <= 0
case ICmpInst::Predicate::ICMP_ULE:
if (isa<ConstantInt>(icmpInst->getOperand(1)) && dyn_cast<ConstantInt>(icmpInst->getOperand(1))->getZExtValue() == 0) {
// Rustle::Logger().Debug(icmpInst);
return true;
}
break;
// balance > 0
case ICmpInst::Predicate::ICMP_UGT:
if (isa<ConstantInt>(icmpInst->getOperand(1)) && dyn_cast<ConstantInt>(icmpInst->getOperand(1))->getZExtValue() == 0) {
// Rustle::Logger().Debug(icmpInst);
return true;
}
break;
// 0 >= balance
case ICmpInst::Predicate::ICMP_UGE:
if (isa<ConstantInt>(icmpInst->getOperand(0)) && dyn_cast<ConstantInt>(icmpInst->getOperand(0))->getZExtValue() == 0) {
// Rustle::Logger().Debug(icmpInst);
return true;
}
break;
// 0 < balance
case ICmpInst::Predicate::ICMP_ULT:
if (isa<ConstantInt>(icmpInst->getOperand(0)) && dyn_cast<ConstantInt>(icmpInst->getOperand(0))->getZExtValue() == 0) {
// Rustle::Logger().Debug(icmpInst);
return true;
}
break;
default: break;
}
}
}
}
}
return false;
}
public:
UnclaimedStorageFee() : ModulePass(ID) {
std::error_code EC;
os = new llvm::raw_fd_ostream(std::string(getenv("TMP_DIR")) + std::string("/.unclaimed-storage-fee.tmp"), EC, llvm::sys::fs::OpenFlags::OF_Append);
}
~UnclaimedStorageFee() { os->close(); }
bool runOnModule(llvm::Module &M) override {
using namespace llvm;
for (auto &F : M.functions()) {
StringRef const funcFileName;
if (!Rustle::debug_check_all_func && Rustle::regexForLibFunc.match(F.getName()))
continue;
if (Rustle::debug_print_function)
Rustle::Logger().Debug("Checking function ", F.getName());
if (!regex_storage_unregister.match(F.getName()))
continue;
*os << F.getName();
if (hasBalanceCmp(&F)) {
Rustle::Logger().Info("Find storage fee balance check for\e[34m storage_unregister");
*os << "@True\n";
} else {
Rustle::Logger().Warning("Lack storage fee balance check for\e[34m storage_unregister");
*os << "@False\n";
}
}
return false;
}
};
} // namespace
char UnclaimedStorageFee::ID = 0;
static llvm::RegisterPass<UnclaimedStorageFee> X("unclaimed-storage-fee", "", 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 UnclaimedStorageFee()); });