Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lab5/antiasan.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <string.h>


extern char gS[];
extern char gBadBuf[];
extern void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
Expand Down
56 changes: 42 additions & 14 deletions lab6/llvm-pass.so.cc
Original file line number Diff line number Diff line change
@@ -1,34 +1,62 @@
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/GlobalVariable.h"

using namespace llvm;

struct LLVMPass : public PassInfoMixin<LLVMPass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
};
struct LLVMPass : PassInfoMixin<LLVMPass> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
LLVMContext &Ctx = M.getContext();

// 1) Declare debug prototype: void debug(i32)
FunctionCallee debugFunc = M.getOrInsertFunction(
"debug",
FunctionType::get(Type::getVoidTy(Ctx), {Type::getInt32Ty(Ctx)}, false)
);
ConstantInt *const48763 = ConstantInt::get(Type::getInt32Ty(Ctx), 48763);

// 2) Locate main
if (Function *F = M.getFunction("main")) {
BasicBlock &entryBB = F->getEntryBlock();
// Insert right after any allocas/PHIs in entry
IRBuilder<> builder(&*entryBB.getFirstInsertionPt());

// --- (40%) Call debug(48763) ---
builder.CreateCall(debugFunc, {const48763});

PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
LLVMContext &Ctx = M.getContext();
IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx);
FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty);
ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763);
// --- (30%) Overwrite argc → 48763 ---
// main signature is: i32 @main(i32 %argc, i8** %argv)
Argument *argcArg = &*F->arg_begin();
argcArg->replaceAllUsesWith(const48763);

for (auto &F : M) {
errs() << "func: " << F.getName() << "\n";
// --- (30%) Overwrite argv[1] → "hayaku... motohayaku!" ---
Argument *argvArg = &*(std::next(F->arg_begin()));
// Create a global constant string
Value *strPtr = builder.CreateGlobalStringPtr("hayaku... motohayaku!");
// Compute pointer to argv[1]: getelementptr i8*, i8** %argv, i64 1
Value *idx1 = ConstantInt::get(Type::getInt64Ty(Ctx), 1);
Value *ptrToArg1 = builder.CreateInBoundsGEP(
argvArg->getType()->getPointerElementType(), // element type = i8*
argvArg, // base pointer i8**
idx1
);
// Store the new string into argv[1]
builder.CreateStore(strPtr, ptrToArg1);
}

return PreservedAnalyses::none();
}
return PreservedAnalyses::none();
}
};

extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
[](PassBuilder &PB) {
PB.registerOptimizerLastEPCallback(
[](ModulePassManager &MPM, OptimizationLevel OL) {
[](ModulePassManager &MPM, OptimizationLevel) {
MPM.addPass(LLVMPass());
});
}};
}

Loading