Skip to content

Commit a19fed2

Browse files
committed
Merge remote-tracking branch 'origin/313560003' into lab8
2 parents 9a90aab + f46d6eb commit a19fed2

File tree

2 files changed

+21
-55
lines changed

2 files changed

+21
-55
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: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,34 @@
11
#include "llvm/Passes/PassPlugin.h"
22
#include "llvm/Passes/PassBuilder.h"
33
#include "llvm/IR/IRBuilder.h"
4-
#include "llvm/IR/Instructions.h"
5-
#include "llvm/IR/Constants.h"
6-
#include "llvm/Support/raw_ostream.h"
74

85
using namespace llvm;
96

10-
bool registerCustomPipeline(StringRef, ModulePassManager&, ArrayRef<PassBuilder::PipelineElement>);
11-
void injectAtStart(ModulePassManager&, OptimizationLevel);
12-
13-
namespace {
147
struct LLVMPass : public PassInfoMixin<LLVMPass> {
15-
PreservedAnalyses run(Module &Mod, ModuleAnalysisManager &) {
16-
Function *MainFunc = Mod.getFunction("main");
17-
if (!MainFunc) return PreservedAnalyses::all();
18-
19-
LLVMContext &Context = Mod.getContext();
20-
IRBuilder<> Builder(&*MainFunc->getEntryBlock().getFirstInsertionPt());
21-
22-
FunctionCallee DebugCall = Mod.getOrInsertFunction(
23-
"debug", FunctionType::get(Type::getVoidTy(Context), {Type::getInt32Ty(Context)}, false));
24-
Builder.CreateCall(DebugCall, ConstantInt::get(Type::getInt32Ty(Context), 48763));
25-
26-
auto ArgIter = MainFunc->arg_begin();
27-
Argument *ArgCount = &*ArgIter++;
28-
Argument *ArgList = &*ArgIter;
29-
30-
Value *FixedVal = ConstantInt::get(Type::getInt32Ty(Context), 48763);
31-
AllocaInst *FakeArgc = Builder.CreateAlloca(Type::getInt32Ty(Context), nullptr, "argc_temp");
32-
Builder.CreateStore(FixedVal, FakeArgc);
8+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
9+
};
3310

34-
for (auto UI = ArgCount->use_begin(), UE = ArgCount->use_end(); UI != UE;) {
35-
Use &U = *UI++;
36-
U.set(Builder.CreateLoad(Type::getInt32Ty(Context), FakeArgc));
37-
}
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);
3816

39-
Value *Str = Builder.CreateGlobalStringPtr("hayaku... motohayaku!", "msg_str");
40-
Value *Index = ConstantInt::get(Type::getInt32Ty(Context), 1);
41-
Value *ArgvSlot = Builder.CreateGEP(
42-
ArgList->getType()->getPointerElementType(), ArgList, Index);
43-
Builder.CreateStore(Str, ArgvSlot);
17+
for (auto &F : M) {
18+
errs() << "func: " << F.getName() << "\n";
4419

45-
return PreservedAnalyses::none();
4620
}
47-
};
21+
return PreservedAnalyses::none();
4822
}
4923

50-
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() {
51-
static const PassPluginLibraryInfo Info{
52-
LLVM_PLUGIN_API_VERSION, "LLVMPass", LLVM_VERSION_STRING,
24+
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
25+
llvmGetPassPluginInfo() {
26+
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
5327
[](PassBuilder &PB) {
54-
PB.registerPipelineParsingCallback(registerCustomPipeline);
55-
PB.registerPipelineStartEPCallback(injectAtStart);
56-
}
57-
};
58-
return Info;
28+
PB.registerOptimizerLastEPCallback(
29+
[](ModulePassManager &MPM, OptimizationLevel OL) {
30+
MPM.addPass(LLVMPass());
31+
});
32+
}};
5933
}
6034

61-
bool registerCustomPipeline(StringRef Name, ModulePassManager &PM, ArrayRef<PassBuilder::PipelineElement>) {
62-
if (Name == "llvm-pass") {
63-
PM.addPass(LLVMPass());
64-
return true;
65-
}
66-
return false;
67-
}
68-
69-
void injectAtStart(ModulePassManager &PM, OptimizationLevel) {
70-
PM.addPass(LLVMPass());
71-
}

0 commit comments

Comments
 (0)