-
Notifications
You must be signed in to change notification settings - Fork 15
/
ext_call_trait.cpp
69 lines (57 loc) · 2.48 KB
/
ext_call_trait.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
/**
* @file ext_call_trait.cpp
* @brief find all trait functions used as external calls' traits
*
*/
#include "near_core.h"
#include <vector>
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Function.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 ExtCallTrait : public llvm::ModulePass {
static char ID;
private:
llvm::raw_fd_ostream *os = nullptr;
public:
ExtCallTrait() : ModulePass(ID) {
std::error_code EC;
os = new llvm::raw_fd_ostream(Rustle::ext_call_trait_file, EC, llvm::sys::fs::OpenFlags::OF_Append);
}
~ExtCallTrait() { os->close(); }
bool runOnModule(llvm::Module &M) override {
using namespace llvm;
CallGraph const CG(M);
for (auto &F : M.functions()) {
if (!Rustle::debug_check_all_func && Rustle::regexForLibFunc.match(F.getName()))
continue;
if (Rustle::debug_print_function)
Rustle::Logger().Debug("Checking function ", F.getName());
for (llvm::BasicBlock &BB : F)
for (llvm::Instruction &I : BB) {
if (!I.getDebugLoc().get() || Rustle::regexForLibLoc.match(I.getDebugLoc().get()->getFilename()))
continue;
if (llvm::CallBase *callInst = llvm::dyn_cast<llvm::CallBase>(&I)) {
if (!callInst->getCalledFunction())
continue;
if (Rustle::regexExtCall.match(callInst->getCalledFunction()->getName())) {
*os << F.getName() << "\n";
return false;
}
}
}
}
return false;
}
};
} // namespace
char ExtCallTrait::ID = 0;
static llvm::RegisterPass<ExtCallTrait> X("ext-call-trait", "function tagged with #[ext_contract()]", 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 ExtCallTrait()); });