Skip to content

Commit bd0a443

Browse files
committed
test run
1 parent 793cd68 commit bd0a443

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

lab6/llvm-pass.so.cc

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
11
#include "llvm/Passes/PassPlugin.h"
22
#include "llvm/Passes/PassBuilder.h"
33
#include "llvm/IR/IRBuilder.h"
4+
#include "llvm/IR/Constants.h"
5+
#include "llvm/IR/GlobalVariable.h"
6+
47
using namespace llvm;
8+
59
struct LLVMPass : public PassInfoMixin<LLVMPass> {
610
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
711
};
812
PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
913
LLVMContext &Ctx = M.getContext();
1014
IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx);
15+
PointerType *Int8PtrTy = Type::getInt8PtrTy(Ctx);
1116

12-
FunctionType *debugTy = FunctionType::get(Type::getVoidTy(Ctx), {Int32Ty}, false);
13-
FunctionCallee debug_func = M.getOrInsertFunction("debug", debugTy);
17+
FunctionType *DebugTy = FunctionType::get(Type::getVoidTy(Ctx), {Int32Ty}, false);
18+
FunctionCallee debug_func = M.getOrInsertFunction("debug", DebugTy);
1419
ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763);
1520

16-
for (auto &F : M) {
17-
//errs() << "func: " << F.getName() << "\n";
18-
if(F.getName() != "main" || F.isDeclaration())
19-
continue;
21+
Constant *StrConstant = ConstantDataArray::getString(Ctx, "hayaku... motohayaku!", true);
22+
GlobalVariable *StrVar = new GlobalVariable(M, StrConstant->getType(), true,
23+
GlobalValue::PrivateLinkage, StrConstant, ".str.hayaku");
24+
Constant *Zero = ConstantInt::get(Int32Ty, 0);
25+
Constant *Indices[] = {Zero, Zero};
26+
Constant *StrPtr = ConstantExpr::getGetElementPtr(StrConstant->getType(), StrVar, Indices);
2027

21-
errs() << "func: " << F.getName() << "\n";
22-
23-
auto ArgIter = F.arg_begin();
24-
Argument *argcArg = &*ArgIter++;
25-
Argument *argvArg = &*ArgIter;
28+
for (auto &F : M) {
29+
if (F.getName() != "main")
30+
continue;
2631

27-
Instruction *InsertPt = &*F.getEntryBlock().getFirstInsertionPt();
28-
IRBuilder<> Builder(InsertPt);
32+
errs() << "Instrumenting function: " << F.getName() << "\n";
33+
IRBuilder<> Builder(&*F.getEntryBlock().getFirstInsertionPt());
2934

3035
Builder.CreateCall(debug_func, {debug_arg});
3136

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-
}
40-
}
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);
37+
auto ArgIter = F.arg_begin();
38+
Argument *argcArg = ArgIter++;
39+
Argument *argvArg = ArgIter;
4640

47-
//Builder.CreateStore(debug_arg, argcArg);
41+
Value *Argv1Ptr = Builder.CreateGEP(Int8PtrTy, argvArg, ConstantInt::get(Int32Ty, 1));
42+
Builder.CreateStore(StrPtr, Argv1Ptr);
4843

44+
argcArg->replaceAllUsesWith(debug_arg);
4945
}
46+
5047
return PreservedAnalyses::none();
5148
}
49+
5250
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
5351
llvmGetPassPluginInfo() {
5452
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",

0 commit comments

Comments
 (0)