-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTargetPass.cpp
72 lines (56 loc) · 2.34 KB
/
TargetPass.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
/**
* Copyright (C) 2014 - 2020 Map2Check tool
* This file is part of the Map2Check tool, and is made available under
* the terms of the GNU General Public License version 2.
*
* LLVM -> NCSA
*
* SPDX-License-Identifier: (GPL-2.0 AND NCSA)
**/
#include "TargetPass.hpp"
bool TargetPass::runOnFunction(Function& F) {
llvm::errs() << "Running TargetPass with: " << this->targetFunctionName;
this->targetFunctionMap2Check = F.getParent()->getOrInsertFunction(
"map2check_target_function", Type::getVoidTy(F.getContext()),
Type::getInt8PtrTy(F.getContext()), Type::getInt32Ty(F.getContext()),
Type::getInt32Ty(F.getContext()));
Function::iterator functionIterator = F.begin();
BasicBlock::iterator instructionIterator = functionIterator->begin();
IRBuilder<> builder(reinterpret_cast<Instruction*>(&*instructionIterator));
this->functionName = builder.CreateGlobalStringPtr(F.getName());
for (Function::iterator bb = F.begin(), e = F.end(); bb != e; ++bb) {
for (BasicBlock::iterator i = bb->begin(), e = bb->end(); i != e; ++i) {
if (CallInst* callInst = dyn_cast<CallInst>(&*i)) {
currentInstruction = i;
this->runOnCallInstruction(callInst, &F.getContext());
}
}
}
return true;
}
void TargetPass::runOnCallInstruction(CallInst* callInst, LLVMContext* Ctx) {
Function* calleeFunction = callInst->getCalledFunction();
if (calleeFunction == NULL) {
Value* v = callInst->getCalledValue();
calleeFunction = dyn_cast<Function>(v->stripPointerCasts());
if (calleeFunction == NULL) {
return;
}
}
if (calleeFunction->getName() == targetFunctionName) {
this->instrumentErrorInstruction(callInst, Ctx);
}
}
void TargetPass::instrumentErrorInstruction(CallInst* callInst,
LLVMContext* Ctx) {
IRBuilder<> builder(reinterpret_cast<Instruction*>(&*currentInstruction));
Value* name_llvm = functionName;
DebugInfo debugInfo(Ctx, callInst);
// errs() << *debugInfo.getLineNumberValue() << "----\n";
Value* args[] = {name_llvm, debugInfo.getScopeNumberValue(),
debugInfo.getLineNumberValue()};
builder.CreateCall(targetFunctionMap2Check, args);
}
char TargetPass::ID = 4;
static RegisterPass<TargetPass> X("target_function",
"Adds map2check calls to check reachability");