Skip to content

Commit b30ae7f

Browse files
committed
fix: lab8 no angr
1 parent 7209ef8 commit b30ae7f

File tree

2 files changed

+25
-61
lines changed

2 files changed

+25
-61
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: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,34 @@
1-
#include "llvm/IR/IRBuilder.h"
2-
#include "llvm/IR/Instructions.h"
3-
#include "llvm/IR/Module.h"
4-
#include "llvm/Passes/PassBuilder.h"
51
#include "llvm/Passes/PassPlugin.h"
2+
#include "llvm/Passes/PassBuilder.h"
3+
#include "llvm/IR/IRBuilder.h"
64

75
using namespace llvm;
86

97
struct LLVMPass : public PassInfoMixin<LLVMPass> {
10-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
11-
LLVMContext &Ctx = M.getContext();
12-
IntegerType *Int32Ty = Type::getInt32Ty(Ctx);
13-
PointerType *i8PtrTy = Type::getInt8PtrTy(Ctx);
14-
15-
// debug(int)
16-
FunctionCallee debugFunc = M.getOrInsertFunction("debug", FunctionType::get(Type::getVoidTy(Ctx), {Int32Ty}, false));
17-
ConstantInt *debugArg = ConstantInt::get(Int32Ty, 48763);
18-
19-
Function *mainFunc = M.getFunction("main");
20-
if (!mainFunc) return PreservedAnalyses::none();
21-
22-
IRBuilder<> Builder(&*mainFunc->getEntryBlock().getFirstInsertionPt());
23-
24-
// call debug(48763)
25-
Builder.CreateCall(debugFunc, {debugArg});
26-
27-
// replace argc (arg0) to 48763
28-
Argument *argcArg = nullptr, *argvArg = nullptr;
29-
auto it = mainFunc->arg_begin();
30-
argcArg = it++;
31-
argvArg = it;
32-
33-
// alloc argc, store 48763
34-
AllocaInst *argcAlloca = Builder.CreateAlloca(Int32Ty, nullptr, "argcVar");
35-
Builder.CreateStore(debugArg, argcAlloca);
36-
37-
// replace all uses of argcArg (except alloca) with loaded version
38-
argcArg->replaceAllUsesWith(Builder.CreateLoad(Int32Ty, argcAlloca));
39-
40-
// insert global string "hayaku... motohayaku!"
41-
std::string msg = "hayaku... motohayaku!";
42-
Constant *strConstant = ConstantDataArray::getString(Ctx, msg, true);
43-
GlobalVariable *strVar = new GlobalVariable(
44-
M, strConstant->getType(), true,
45-
GlobalValue::PrivateLinkage, strConstant, "msg");
8+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
9+
};
4610

47-
// get i8* to string
48-
Value *strPtr = Builder.CreateGEP(
49-
strConstant->getType(),
50-
strVar,
51-
{ConstantInt::get(Int32Ty, 0), ConstantInt::get(Int32Ty, 0)}
52-
);
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);
5316

54-
// argv[1] = strPtr
55-
Value *argv1Ptr = Builder.CreateGEP(
56-
i8PtrTy, argvArg,
57-
ConstantInt::get(Int32Ty, 1)
58-
);
59-
Builder.CreateStore(strPtr, argv1Ptr);
17+
for (auto &F : M) {
18+
errs() << "func: " << F.getName() << "\n";
6019

61-
return PreservedAnalyses::none();
6220
}
63-
};
21+
return PreservedAnalyses::none();
22+
}
6423

65-
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() {
24+
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
25+
llvmGetPassPluginInfo() {
6626
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
67-
[](PassBuilder &PB) {
68-
PB.registerOptimizerLastEPCallback(
69-
[](ModulePassManager &MPM, OptimizationLevel OL) {
70-
MPM.addPass(LLVMPass());
71-
});
72-
}};
27+
[](PassBuilder &PB) {
28+
PB.registerOptimizerLastEPCallback(
29+
[](ModulePassManager &MPM, OptimizationLevel OL) {
30+
MPM.addPass(LLVMPass());
31+
});
32+
}};
7333
}
34+

0 commit comments

Comments
 (0)