-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticFP.cpp
More file actions
129 lines (113 loc) · 4.91 KB
/
StaticFP.cpp
File metadata and controls
129 lines (113 loc) · 4.91 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
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
//=============================================================================
// FILE:
// StaticFP.cpp
//
// DESCRIPTION:
// Visits all functions in a module, prints their names and the number of
// arguments via stderr. Strictly speaking, this is an analysis pass (i.e.
// the functions are not modified). However, in order to keep things simple
// there's no 'print' method here (every analysis pass should implement it).
//
// USAGE:
// New PM
// opt -load-pass-plugin=libHelloWorld.dylib -passes="hello-world" `\`
// -disable-output <input-llvm-file>
//
//
// License: MIT
//=============================================================================
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include <fstream>
#include <sstream>
using namespace llvm;
//-----------------------------------------------------------------------------
// HelloWorld implementation
//-----------------------------------------------------------------------------
// No need to expose the internals of the pass to the outside world - keep
// everything in an anonymous namespace.
namespace {
// Function to write header to the CSV file
void writeCSVHeader(std::ofstream &csvFile) {
csvFile << "Function Name, Function Pointer Address, Instruction Address\n";
}
// Function to write a line to the CSV file
void writeCSVLine(std::ofstream &csvFile, const std::string &functionName, const std::string &address, const std::string &instruction) {
csvFile << functionName << "," << address << "," << instruction << "\n";
}
// Function to print the address of the function pointer
void printFunctionPointerAddress(Value *CalledValue, LLVMContext &Context, std::ofstream &csvFile, const Function &F, const Instruction &I) {
std::string addrStr;
raw_string_ostream addrStream(addrStr);
addrStream << CalledValue;
std::string insStr;
raw_string_ostream insStream(insStr);
insStream << &I;
// Write the function name, instruction, and pointer address to the CSV
writeCSVLine(csvFile, F.getName().str(), addrStream.str(), insStream.str());
}
// FunctionPass that analyzes function pointers
struct StaticFP : public PassInfoMixin<StaticFP> {
// Entry point for the pass
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM) {
// Open CSV file
std::ofstream csvFile;
if (F.getName() == "main") { // Open file for the first function (or adjust as needed)
csvFile.open("function_pointers.csv", std::ios::out | std::ios::trunc);
writeCSVHeader(csvFile);
} else {
csvFile.open("function_pointers.csv", std::ios::out | std::ios::app);
}
for (auto &BB : F) {
for (auto &I : BB) {
if (auto *Call = dyn_cast<CallBase>(&I)) {
if (auto *CalledFunction = Call->getCalledFunction()) {
// Direct function call, ignore.
continue;
} else {
Value *CalledValue = Call->getCalledOperand();
if (CalledValue->getType()->isPointerTy()) {
// Indirect function call
errs() << "Function pointer used in function: " << F.getName() << "\n";
errs() << "Instruction: " << I << "\n";
errs() << "Function pointed to: " << CalledValue << "\n";
printFunctionPointerAddress(CalledValue, F.getContext(), csvFile, F, I);
}
}
}
}
}
csvFile.close(); // Close the file after each function (or manage it as needed)
return PreservedAnalyses::all();
}
static bool isRequired() { return true; }
};
}
//-----------------------------------------------------------------------------
// New PM Registration
//-----------------------------------------------------------------------------
llvm::PassPluginLibraryInfo getStaticFPPluginInfo() {
const auto callback = [](PassBuilder &PB) {
PB.registerPipelineEarlySimplificationEPCallback(
[&](ModulePassManager &MPM, auto) {
MPM.addPass(createModuleToFunctionPassAdaptor(StaticFP()));HelloWorld
return true;
});
};
return {LLVM_PLUGIN_API_VERSION, "static-fp", "0.0.1", callback};
}
// This is the core interface for pass plugins. It guarantees that 'opt' will
// be able to recognize HelloWorld when added to the pass pipeline on the
// command line, i.e. via '-passes=hello-world'
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return getStaticFPPluginInfo();
}