-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdemo-pass.so.cc
More file actions
77 lines (46 loc) · 1.59 KB
/
demo-pass.so.cc
File metadata and controls
77 lines (46 loc) · 1.59 KB
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
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/IR/IRBuilder.h"
using namespace llvm;
struct DEMOPass : public PassInfoMixin<DEMOPass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
LLVMContext & C = M.getContext();
Type * VoidTy = Type::getVoidTy(C);
FunctionCallee demo_func = M.getOrInsertFunction("__demo_func", VoidTy);
printf("Demo Pass running!\n");
for (auto &F : M) {
for (auto &BB : F) {
for (auto &I : BB) {
if (CallInst *call = dyn_cast<CallInst>(&I)) {
if (GetCallInsFunctionName(call).equals("puts")) {
IRBuilder<> IRB(&I);
IRB.CreateCall(demo_func)->setMetadata(M.getMDKindID("nosanitize"),
MDNode::get(C, None));
}
}
}
}
}
return PreservedAnalyses::none();
}
static void registerCallbacks(PassBuilder &PB) {
PB.registerOptimizerLastEPCallback(
[](ModulePassManager &MPM, OptimizationLevel OL) {
MPM.addPass(DEMOPass());
});
}
llvm::StringRef GetCallInsFunctionName(CallInst *call) {
if (Function *func = call->getCalledFunction()) {
return func->getName();
} else {
// Indirect call
return dyn_cast<Function>(call->getCalledOperand()->stripPointerCasts())
->getName();
}
}
};
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "DEMOPass", "1.0",
DEMOPass::registerCallbacks};
}