Skip to content

Commit 54d39ec

Browse files
committed
fix: lab8 no angr
1 parent 36a0b76 commit 54d39ec

File tree

2 files changed

+24
-50
lines changed

2 files changed

+24
-50
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 & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,34 @@
1-
#include "llvm/IR/Constants.h"
2-
#include "llvm/IR/GlobalVariable.h"
3-
#include "llvm/IR/IRBuilder.h"
4-
#include "llvm/IR/Instructions.h"
5-
#include "llvm/Passes/PassBuilder.h"
61
#include "llvm/Passes/PassPlugin.h"
2+
#include "llvm/Passes/PassBuilder.h"
3+
#include "llvm/IR/IRBuilder.h"
74

85
using namespace llvm;
96

107
struct LLVMPass : public PassInfoMixin<LLVMPass> {
11-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
8+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
129
};
1310

1411
PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
15-
LLVMContext &ctx = M.getContext();
16-
17-
// Find the main function
18-
Function *main_fn = M.getFunction("main");
19-
assert(main_fn && main_fn->arg_size() >= 2);
20-
21-
// Find the debug function
22-
auto debug_fn = M.getFunction("debug");
23-
assert(debug_fn);
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);
2416

25-
// Insert the debug function call at the beginning of main
26-
IRBuilder<> main_ir_builder(&main_fn->getEntryBlock().front());
27-
// call the debug function with 48763
28-
main_ir_builder.CreateCall(debug_fn, {ConstantInt::get(Type::getInt32Ty(ctx), 48763)});
17+
for (auto &F : M) {
18+
errs() << "func: " << F.getName() << "\n";
2919

30-
// Find argc and argv
31-
auto args_iter = main_fn->arg_begin();
32-
Argument &argc = *args_iter++;
33-
Argument &argv = *args_iter++;
34-
35-
// Replace argc to 48763
36-
auto new_argc = ConstantInt::get(argc.getType(), 48763);
37-
argc.replaceAllUsesWith(new_argc);
38-
39-
// create the target string
40-
auto target_value = ConstantDataArray::getString(ctx, "hayaku... motohayaku!", true);
41-
auto target = cast<GlobalVariable>(M.getOrInsertGlobal("target", target_value->getType()));
42-
target->setInitializer(target_value);
43-
target->setLinkage(GlobalValue::InternalLinkage);
44-
45-
// create a {..., target} array
46-
auto new_argv_value = ConstantArray::get(ArrayType::get(target->getType(), 2),
47-
ArrayRef<Constant *>{target, target});
48-
auto new_argv = cast<GlobalVariable>(M.getOrInsertGlobal("my_argv", new_argv_value->getType()));
49-
new_argv->setInitializer(new_argv_value);
50-
new_argv->setLinkage(GlobalValue::InternalLinkage);
51-
52-
// Replace argv to target_array
53-
argv.replaceAllUsesWith(new_argv);
54-
55-
return PreservedAnalyses::none();
20+
}
21+
return PreservedAnalyses::none();
5622
}
5723

58-
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK llvmGetPassPluginInfo() {
59-
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0", [](PassBuilder &PB) {
60-
PB.registerOptimizerLastEPCallback(
61-
[](ModulePassManager &MPM, OptimizationLevel OL) { MPM.addPass(LLVMPass()); });
62-
}};
24+
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
25+
llvmGetPassPluginInfo() {
26+
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
27+
[](PassBuilder &PB) {
28+
PB.registerOptimizerLastEPCallback(
29+
[](ModulePassManager &MPM, OptimizationLevel OL) {
30+
MPM.addPass(LLVMPass());
31+
});
32+
}};
6333
}
34+

0 commit comments

Comments
 (0)