Skip to content

Commit 9868a0c

Browse files
committed
fix: lab8 no angr
1 parent 5edd4b4 commit 9868a0c

File tree

2 files changed

+24
-75
lines changed

2 files changed

+24
-75
lines changed

.github/workflows/lab-autograding.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@ jobs:
5656
if [ ${{ steps.lab.outputs.result }} -eq 6 ]; then
5757
sudo apt install -y llvm-14
5858
fi
59+
if [ ${{ steps.lab.outputs.result }} -eq 8 ]; then
60+
python3 -m pip install angr
61+
fi
5962
./validate.sh

lab6/llvm-pass.so.cc

Lines changed: 21 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,34 @@
11
#include "llvm/Passes/PassPlugin.h"
22
#include "llvm/Passes/PassBuilder.h"
33
#include "llvm/IR/IRBuilder.h"
4-
#include "llvm/IR/Module.h"
5-
#include "llvm/IR/Function.h"
6-
#include "llvm/IR/Instructions.h"
7-
#include "llvm/IR/Constants.h"
8-
using namespace llvm;
9-
10-
namespace {
11-
12-
struct Lab6FinalPass : PassInfoMixin<Lab6FinalPass> {
13-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
14-
LLVMContext &C = M.getContext();
15-
auto *i32 = Type::getInt32Ty(C);
16-
auto *i8ptr = Type::getInt8PtrTy(C);
17-
18-
// 宣告 debug 函數與 48763 常數
19-
auto debugTy = FunctionType::get(Type::getVoidTy(C), {i32}, false);
20-
auto debugFn = M.getOrInsertFunction("debug", debugTy);
21-
auto const48763 = ConstantInt::get(i32, 48763);
22-
23-
// 建立常數字串 "hayaku... motohayaku!"
24-
auto strConst = ConstantDataArray::getString(C, "hayaku... motohayaku!", true);
25-
auto *gstr = new GlobalVariable(M, strConst->getType(), true,
26-
GlobalValue::PrivateLinkage, strConst, "haya_str");
27-
auto strPtr = ConstantExpr::getBitCast(gstr, i8ptr);
28-
29-
// 找到 main 函式
30-
Function *main = M.getFunction("main");
31-
if (!main) return PreservedAnalyses::all();
32-
33-
// IRBuilder 插入點設在 entry block 開頭
34-
IRBuilder<> B(&*main->getEntryBlock().getFirstInsertionPt());
354

36-
// 呼叫 debug(48763)
37-
B.CreateCall(debugFn, const48763);
38-
39-
// 抓取 main 的參數
40-
Argument *argc = nullptr, *argv = nullptr;
41-
auto it = main->arg_begin();
42-
if (it != main->arg_end()) argc = it++;
43-
if (it != main->arg_end()) argv = it;
5+
using namespace llvm;
446

45-
// 覆寫 argv[1] 的記憶體:argv[1] = strPtr
46-
Value *idx[] = { ConstantInt::get(i32, 1) };
47-
Value *argv1Ptr = B.CreateInBoundsGEP(i8ptr, argv, idx);
48-
B.CreateStore(strPtr, argv1Ptr);
7+
struct LLVMPass : public PassInfoMixin<LLVMPass> {
8+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
9+
};
4910

50-
// 遍歷整個函數,修改 argc 為常數,並處理 strcmp
51-
for (auto &BB : *main) {
52-
for (auto &I : BB) {
53-
// 修改使用 argc 的地方
54-
for (unsigned i = 0; i < I.getNumOperands(); ++i) {
55-
if (I.getOperand(i) == argc) {
56-
I.setOperand(i, const48763);
57-
}
58-
}
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);
5916

60-
// 如果遇到 strcmp(argv[1], ...) 則強制修改 argv[1] 為我們的字串
61-
if (auto *call = dyn_cast<CallInst>(&I)) {
62-
if (Function *callee = call->getCalledFunction()) {
63-
if (callee->getName() == "strcmp" && call->arg_size() >= 2) {
64-
call->setArgOperand(0, strPtr);
65-
}
66-
}
67-
}
68-
}
69-
}
17+
for (auto &F : M) {
18+
errs() << "func: " << F.getName() << "\n";
7019

71-
return PreservedAnalyses::none();
7220
}
73-
};
74-
75-
} // namespace
21+
return PreservedAnalyses::none();
22+
}
7623

77-
// 註冊 Pass 到 New Pass Manager
78-
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() {
79-
return {
80-
LLVM_PLUGIN_API_VERSION, "Lab6FinalPass", "v1.0",
24+
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
25+
llvmGetPassPluginInfo() {
26+
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
8127
[](PassBuilder &PB) {
82-
PB.registerPipelineStartEPCallback(
83-
[](ModulePassManager &MPM, OptimizationLevel) {
84-
MPM.addPass(Lab6FinalPass());
28+
PB.registerOptimizerLastEPCallback(
29+
[](ModulePassManager &MPM, OptimizationLevel OL) {
30+
MPM.addPass(LLVMPass());
8531
});
86-
}
87-
};
32+
}};
8833
}
34+

0 commit comments

Comments
 (0)