Skip to content

Commit 793cd68

Browse files
committed
test run
1 parent fe31c95 commit 793cd68

File tree

1 file changed

+41
-56
lines changed

1 file changed

+41
-56
lines changed

lab6/llvm-pass.so.cc

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,54 @@
11
#include "llvm/Passes/PassPlugin.h"
22
#include "llvm/Passes/PassBuilder.h"
33
#include "llvm/IR/IRBuilder.h"
4-
5-
namespace {
64
using namespace llvm;
7-
8-
class CustomModulePass : public PassInfoMixin<CustomModulePass> {
9-
public:
10-
PreservedAnalyses run(Module &Module, ModuleAnalysisManager &AnalysisManager);
5+
struct LLVMPass : public PassInfoMixin<LLVMPass> {
6+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
117
};
8+
PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
9+
LLVMContext &Ctx = M.getContext();
10+
IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx);
11+
12+
FunctionType *debugTy = FunctionType::get(Type::getVoidTy(Ctx), {Int32Ty}, false);
13+
FunctionCallee debug_func = M.getOrInsertFunction("debug", debugTy);
14+
ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763);
15+
16+
for (auto &F : M) {
17+
//errs() << "func: " << F.getName() << "\n";
18+
if(F.getName() != "main" || F.isDeclaration())
19+
continue;
20+
21+
errs() << "func: " << F.getName() << "\n";
22+
23+
auto ArgIter = F.arg_begin();
24+
Argument *argcArg = &*ArgIter++;
25+
Argument *argvArg = &*ArgIter;
1226

13-
PreservedAnalyses CustomModulePass::run(Module &Module, ModuleAnalysisManager &AnalysisManager) {
14-
LLVMContext &Context = Module.getContext();
15-
16-
// Create types
17-
auto *I32Type = Type::getInt32Ty(Context);
18-
19-
// Setup debug function
20-
FunctionType *DebugFunctionType = FunctionType::get(Type::getVoidTy(Context), {I32Type}, false);
21-
FunctionCallee DebugFunction = Module.getOrInsertFunction("debug", DebugFunctionType);
22-
23-
// Magic number constant
24-
ConstantInt *MagicNumber = ConstantInt::get(I32Type, 48763);
25-
26-
// Process functions in module
27-
for (Function &CurrentFunction : Module) {
28-
// Skip non-main functions and declarations
29-
if (CurrentFunction.getName() != "main" || CurrentFunction.isDeclaration())
30-
continue;
31-
32-
errs() << "Processing function: " << CurrentFunction.getName() << "\n";
33-
34-
// Get function arguments
35-
auto ArgIterator = CurrentFunction.arg_begin();
36-
Argument *ArgCount = &*ArgIterator++;
37-
Argument *ArgVector = &*ArgIterator;
38-
39-
// Insert at the beginning of function entry block
40-
IRBuilder<> IRB(&*CurrentFunction.getEntryBlock().begin());
41-
42-
// Insert call to debug function
43-
IRB.CreateCall(DebugFunction, {MagicNumber});
44-
45-
// Replace all uses of argc with magic number
46-
for (BasicBlock &Block : CurrentFunction) {
47-
for (Instruction &Inst : Block) {
48-
for (unsigned OpIdx = 0; OpIdx < Inst.getNumOperands(); ++OpIdx) {
49-
if (Inst.getOperand(OpIdx) == ArgCount) {
50-
Inst.setOperand(OpIdx, MagicNumber);
51-
}
52-
}
53-
}
27+
Instruction *InsertPt = &*F.getEntryBlock().getFirstInsertionPt();
28+
IRBuilder<> Builder(InsertPt);
29+
30+
Builder.CreateCall(debug_func, {debug_arg});
31+
32+
for(auto &BB : F){
33+
for(auto &I : BB){
34+
for(unsigned i = 0; i < I.getNumOperands(); ++i){
35+
if(I.getOperand(i) == argcArg){
36+
I.setOperand(i, debug_arg);
37+
}
38+
}
39+
}
5440
}
55-
56-
// Modify argv[1]
57-
Value *IndexOne = ConstantInt::get(I32Type, 1);
58-
Value *SecondArgPtr = IRB.CreateInBoundsGEP(ArgVector->getType()->getPointerElementType(),
59-
ArgVector, IndexOne);
60-
Value *MessagePtr = IRB.CreateGlobalStringPtr("hayaku... motohayaku!");
61-
IRB.CreateStore(MessagePtr, SecondArgPtr);
41+
42+
Value *idx1 = ConstantInt::get(Int32Ty, 1);
43+
Value *argv1Ptr = Builder.CreateInBoundsGEP(argvArg->getType()->getPointerElementType(), argvArg, idx1);
44+
Value *strPtr = Builder.CreateGlobalStringPtr("hayaku... motohayaku!");
45+
Builder.CreateStore(strPtr, argv1Ptr);
46+
47+
//Builder.CreateStore(debug_arg, argcArg);
48+
6249
}
63-
6450
return PreservedAnalyses::none();
6551
}
66-
6752
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
6853
llvmGetPassPluginInfo() {
6954
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",

0 commit comments

Comments
 (0)