Skip to content

Commit 18b544b

Browse files
author
Your Name
committed
lab6
1 parent a305991 commit 18b544b

File tree

7 files changed

+128
-111
lines changed

7 files changed

+128
-111
lines changed

lab6/Makefile

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
all: target
2-
3-
llvm-pass.so: llvm-pass.so.cc
4-
clang-14 `llvm-config-14 --cxxflags` -shared -fPIC $< -o $@
5-
6-
target: target.c llvm-pass.so
7-
clang-14 `llvm-config-14 --cflags` -fexperimental-new-pass-manager \
8-
-fpass-plugin=./llvm-pass.so $< -o $@
9-
10-
run:
11-
./target 1
12-
13-
clean:
1+
all: target
2+
3+
llvm-pass.so: llvm-pass.so.cc
4+
clang-14 `llvm-config-14 --cxxflags` -shared -fPIC $< -o $@
5+
6+
target: target.c llvm-pass.so
7+
clang-14 `llvm-config-14 --cflags` -fexperimental-new-pass-manager \
8+
-fpass-plugin=./llvm-pass.so $< -o $@
9+
10+
run:
11+
./target 1
12+
13+
clean:
1414
rm -f llvm-pass.so target

lab6/README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
# Lab6
2-
3-
## Introduction
4-
5-
In this lab, you will write a llvm pass to instrument some codes to `target.c` in `llvm-pass.so.cc`.
6-
7-
## Preparation (Important!!!)
8-
9-
1. Sync fork your branch (e.g., `SQLab:311XXXXXX`)
10-
2. `git checkout -b lab6` (**NOT** your student ID !!!)
11-
12-
## Requirement
13-
14-
Write a llvm pass to instrument some codes to `target.c` in `llvm-pass.so.cc` and satisfy following requirements.
15-
1. (40%) Invoke debug function with the first argument is 48763 in main function.
16-
2. (30%) Overwrite argv[1] to "hayaku... motohayaku!" before checking.
17-
3. (30%) Overwrite argc to 48763 before checking.
18-
You can run `validate.sh` in your local to test if you satisfy the requirements.
19-
20-
Please note that you must not alter files other than `llvm-pass.so.cc`. You will get 0 points if
21-
22-
1. you modify other files to achieve requirements.
23-
2. you can't pass all CI on your PR.
24-
25-
## Submission
26-
27-
You need to open a pull request to your branch (e.g. 311XXXXXX, your student number) and contain the code that satisfies the abovementioned requirements.
28-
29-
Moreover, please submit the URL of your PR to E3. Your submission will only be accepted when you present at both places.
1+
# Lab6
2+
3+
## Introduction
4+
5+
In this lab, you will write a llvm pass to instrument some codes to `target.c` in `llvm-pass.so.cc`.
6+
7+
## Preparation (Important!!!)
8+
9+
1. Sync fork your branch (e.g., `SQLab:311XXXXXX`)
10+
2. `git checkout -b lab6` (**NOT** your student ID !!!)
11+
12+
## Requirement
13+
14+
Write a llvm pass to instrument some codes to `target.c` in `llvm-pass.so.cc` and satisfy following requirements.
15+
1. (40%) Invoke debug function with the first argument is 48763 in main function.
16+
2. (30%) Overwrite argv[1] to "hayaku... motohayaku!" before checking.
17+
3. (30%) Overwrite argc to 48763 before checking.
18+
You can run `validate.sh` in your local to test if you satisfy the requirements.
19+
20+
Please note that you must not alter files other than `llvm-pass.so.cc`. You will get 0 points if
21+
22+
1. you modify other files to achieve requirements.
23+
2. you can't pass all CI on your PR.
24+
25+
## Submission
26+
27+
You need to open a pull request to your branch (e.g. 311XXXXXX, your student number) and contain the code that satisfies the abovementioned requirements.
28+
29+
Moreover, please submit the URL of your PR to E3. Your submission will only be accepted when you present at both places.

lab6/ans

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
./target 1
2-
deubg mode
3-
argc = 48763
4-
argv[1] = hayaku... motohayaku!
5-
Your argc is so hayaku!
6-
Suta... basuto... sutorimu!
1+
./target 1
2+
deubg mode
3+
argc = 48763
4+
argv[1] = hayaku... motohayaku!
5+
Your argc is so hayaku!
6+
Suta... basuto... sutorimu!

lab6/llvm-pass.so

215 KB
Binary file not shown.

lab6/llvm-pass.so.cc

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
1-
#include "llvm/Passes/PassPlugin.h"
2-
#include "llvm/Passes/PassBuilder.h"
3-
#include "llvm/IR/IRBuilder.h"
4-
5-
using namespace llvm;
6-
7-
struct LLVMPass : public PassInfoMixin<LLVMPass> {
8-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
9-
};
10-
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);
16-
17-
for (auto &F : M) {
18-
errs() << "func: " << F.getName() << "\n";
19-
20-
}
21-
return PreservedAnalyses::none();
22-
}
23-
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-
}};
33-
}
34-
1+
#include "llvm/Passes/PassPlugin.h"
2+
#include "llvm/Passes/PassBuilder.h"
3+
#include "llvm/IR/IRBuilder.h"
4+
5+
using namespace llvm;
6+
7+
struct LLVMPass : public PassInfoMixin<LLVMPass> {
8+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
9+
};
10+
11+
PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
12+
LLVMContext &Ctx = M.getContext();
13+
IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx);
14+
Type *Int8PtrTy = Type::getInt8PtrTy(Ctx); ///add
15+
FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty);
16+
ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763);
17+
18+
for (auto &F : M) {
19+
errs() << "func: " << F.getName() << "\n";
20+
if (F.getName() == "main") {
21+
IRBuilder<> Builder(&*F.getEntryBlock().getFirstInsertionPt());
22+
23+
Builder.CreateCall(debug_func, debug_arg);
24+
25+
Argument *ArgcArg = F.getArg(0);
26+
ArgcArg->replaceAllUsesWith(debug_arg);
27+
28+
Argument *ArgvArg = F.getArg(1);
29+
Value *GEP = Builder.CreateInBoundsGEP(
30+
Int8PtrTy,
31+
ArgvArg,
32+
ConstantInt::get(Int32Ty, 1)
33+
);
34+
Value *StrVal = Builder.CreateGlobalStringPtr("hayaku... motohayaku!");
35+
Builder.CreateStore(StrVal, GEP);
36+
}
37+
}
38+
return PreservedAnalyses::none();
39+
}
40+
41+
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
42+
llvmGetPassPluginInfo() {
43+
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
44+
[](PassBuilder &PB) {
45+
PB.registerOptimizerLastEPCallback(
46+
[](ModulePassManager &MPM, OptimizationLevel OL) {
47+
MPM.addPass(LLVMPass());
48+
});
49+
}};
50+
}
51+

lab6/target

15.7 KB
Binary file not shown.

lab6/target.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <string.h>
4-
5-
void debug(int id)
6-
{
7-
if (id == 48763)
8-
printf("deubg mode\n");
9-
else
10-
printf("bad id!\n");
11-
}
12-
13-
int main(int argc, char **argv)
14-
{
15-
printf("argc = %d\n", argc);
16-
if (argc >= 2)
17-
printf("argv[1] = %s\n", argv[1]);
18-
else
19-
return 0;
20-
if (argc == 48763)
21-
printf("Your argc is so hayaku!\n");
22-
else
23-
printf("Your argc need to be modohayaku!\n");
24-
if (strcmp(argv[1], "hayaku... motohayaku!") == 0)
25-
printf("Suta... basuto... sutorimu!\n");
26-
else
27-
printf("You dead\n");
28-
return 0;
29-
}
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
void debug(int id)
6+
{
7+
if (id == 48763)
8+
printf("deubg mode\n");
9+
else
10+
printf("bad id!\n");
11+
}
12+
13+
int main(int argc, char **argv)
14+
{
15+
printf("argc = %d\n", argc);
16+
if (argc >= 2)
17+
printf("argv[1] = %s\n", argv[1]);
18+
else
19+
return 0;
20+
if (argc == 48763)
21+
printf("Your argc is so hayaku!\n");
22+
else
23+
printf("Your argc need to be modohayaku!\n");
24+
if (strcmp(argv[1], "hayaku... motohayaku!") == 0)
25+
printf("Suta... basuto... sutorimu!\n");
26+
else
27+
printf("You dead\n");
28+
return 0;
29+
}

0 commit comments

Comments
 (0)