Skip to content

Commit 4b493d5

Browse files
committed
feat: lab6
1 parent 73e0ce2 commit 4b493d5

File tree

7 files changed

+159
-1
lines changed

7 files changed

+159
-1
lines changed

.github/workflows/lab-autograding.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ jobs:
4545
const files = await github.rest.pulls.listFiles({ owner, repo, pull_number: issue_number });
4646
const changedFiles = files.data.map((file) => file.filename);
4747
const allowedFileRegex = /^lab\d+\/main_test.js$/;
48-
const specialChangedFiles = ["lab0/lab0.js", "lab5/antiasan.c"];
48+
const specialChangedFiles = ["lab0/lab0.js", "lab5/antiasan.c", "lab6/llvm-pass.so.cc"];
4949
if (!changedFiles.every((file) => (allowedFileRegex.test(file) || specialChangedFiles.includes(file)))) {
5050
core.setFailed('The PR contains changes to files other than the allowed files.');
5151
}
5252
return labNumber;
5353
- name: Grading
5454
run: |
5555
cd lab${{ steps.lab.outputs.result }}
56+
if [ ${{ steps.lab.outputs.result }} -eq 6 ]; then
57+
sudo apt install -y llvm-14
58+
fi
5659
./validate.sh

lab6/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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:
14+
rm -f llvm-pass.so target

lab6/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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.

lab6/ans

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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!

lab6/llvm-pass.so.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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+
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+

lab6/target.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
}

lab6/validate.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
# Check for unwanted files
4+
for file in *; do
5+
if [[ $file != "llvm-pass.so.cc" && $file != "target.c" && $file != "Makefile" && $file != "README.md" && $file != "validate.sh" && $file != "ans" ]]; then
6+
echo "[!] Unwanted file detected: $file."
7+
exit 1
8+
fi
9+
done
10+
11+
test_path="${BASH_SOURCE[0]}"
12+
solution_path="$(realpath .)"
13+
tmp_dir=$(mktemp -d -t lab5-XXXXXXXXXX)
14+
answer=""
15+
16+
cd $tmp_dir
17+
18+
rm -rf *
19+
cp $solution_path/Makefile .
20+
cp $solution_path/*.c .
21+
cp $solution_path/*.cc .
22+
cp $solution_path/ans .
23+
24+
make
25+
make run > out 2>&1
26+
result=$(diff --strip-trailing-cr ans out)
27+
if [[ -n $result ]]; then
28+
echo "[!] Expected: "
29+
cat ans
30+
echo ""
31+
echo "[!] Actual: "
32+
cat out
33+
echo ""
34+
exit 1
35+
else
36+
echo "[V] Pass"
37+
fi
38+
39+
rm -rf $tmp_dir
40+
41+
exit 0
42+
43+
# vim: set fenc=utf8 ff=unix et sw=2 ts=2 sts=2:

0 commit comments

Comments
 (0)