|
1 | 1 | #include "llvm/Passes/PassPlugin.h" |
2 | 2 | #include "llvm/Passes/PassBuilder.h" |
3 | 3 | #include "llvm/IR/IRBuilder.h" |
| 4 | +#include "llvm/IR/Constants.h" |
| 5 | +#include "llvm/IR/GlobalVariable.h" |
4 | 6 |
|
5 | 7 | using namespace llvm; |
6 | 8 |
|
7 | | -struct LLVMPass : public PassInfoMixin<LLVMPass> { |
8 | | - PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); |
9 | | -}; |
| 9 | +struct LLVMPass : PassInfoMixin<LLVMPass> { |
| 10 | + PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) { |
| 11 | + LLVMContext &Ctx = M.getContext(); |
| 12 | + |
| 13 | + // 1) Declare debug prototype: void debug(i32) |
| 14 | + FunctionCallee debugFunc = M.getOrInsertFunction( |
| 15 | + "debug", |
| 16 | + FunctionType::get(Type::getVoidTy(Ctx), {Type::getInt32Ty(Ctx)}, false) |
| 17 | + ); |
| 18 | + ConstantInt *const48763 = ConstantInt::get(Type::getInt32Ty(Ctx), 48763); |
| 19 | + |
| 20 | + // 2) Locate main |
| 21 | + if (Function *F = M.getFunction("main")) { |
| 22 | + BasicBlock &entryBB = F->getEntryBlock(); |
| 23 | + // Insert right after any allocas/PHIs in entry |
| 24 | + IRBuilder<> builder(&*entryBB.getFirstInsertionPt()); |
| 25 | + |
| 26 | + // --- (40%) Call debug(48763) --- |
| 27 | + builder.CreateCall(debugFunc, {const48763}); |
10 | 28 |
|
11 | | -PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) { |
12 | | - LLVMContext &Ctx = M.getContext(); |
13 | | - IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx); |
14 | | - FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty); |
15 | | - ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763); |
| 29 | + // --- (30%) Overwrite argc → 48763 --- |
| 30 | + // main signature is: i32 @main(i32 %argc, i8** %argv) |
| 31 | + Argument *argcArg = &*F->arg_begin(); |
| 32 | + argcArg->replaceAllUsesWith(const48763); |
16 | 33 |
|
17 | | - for (auto &F : M) { |
18 | | - errs() << "func: " << F.getName() << "\n"; |
| 34 | + // --- (30%) Overwrite argv[1] → "hayaku... motohayaku!" --- |
| 35 | + Argument *argvArg = &*(std::next(F->arg_begin())); |
| 36 | + // Create a global constant string |
| 37 | + Value *strPtr = builder.CreateGlobalStringPtr("hayaku... motohayaku!"); |
| 38 | + // Compute pointer to argv[1]: getelementptr i8*, i8** %argv, i64 1 |
| 39 | + Value *idx1 = ConstantInt::get(Type::getInt64Ty(Ctx), 1); |
| 40 | + Value *ptrToArg1 = builder.CreateInBoundsGEP( |
| 41 | + argvArg->getType()->getPointerElementType(), // element type = i8* |
| 42 | + argvArg, // base pointer i8** |
| 43 | + idx1 |
| 44 | + ); |
| 45 | + // Store the new string into argv[1] |
| 46 | + builder.CreateStore(strPtr, ptrToArg1); |
| 47 | + } |
19 | 48 |
|
| 49 | + return PreservedAnalyses::none(); |
20 | 50 | } |
21 | | - return PreservedAnalyses::none(); |
22 | | -} |
| 51 | +}; |
23 | 52 |
|
24 | 53 | extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK |
25 | 54 | llvmGetPassPluginInfo() { |
26 | 55 | return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0", |
27 | 56 | [](PassBuilder &PB) { |
28 | 57 | PB.registerOptimizerLastEPCallback( |
29 | | - [](ModulePassManager &MPM, OptimizationLevel OL) { |
| 58 | + [](ModulePassManager &MPM, OptimizationLevel) { |
30 | 59 | MPM.addPass(LLVMPass()); |
31 | 60 | }); |
32 | 61 | }}; |
33 | 62 | } |
34 | | - |
|
0 commit comments