Skip to content

Commit f5ccb02

Browse files
committed
fix: lab8 no angr
1 parent 4eaa244 commit f5ccb02

File tree

3 files changed

+28
-142
lines changed

3 files changed

+28
-142
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

lab3/main_test.js

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,3 @@ const assert = require('assert');
33
const { Calculator } = require('./main');
44

55
// TODO: write your tests here
6-
7-
describe('Calculator', () => {
8-
9-
it('should calculate exp correctly', () => {
10-
const testcases = [
11-
[Infinity, 'unsupported operand type'],
12-
[Number.MAX_VALUE, 'overflow'],
13-
[1, Math.exp(1)],
14-
[0, Math.exp(0)],
15-
[-1, Math.exp(-1)]
16-
];
17-
const calculator = new Calculator();
18-
for (const [input, expected] of testcases) {
19-
if (typeof expected === 'string') {
20-
assert.throws(() => calculator.exp(input), Error);
21-
} else {
22-
assert.strictEqual(calculator.exp(input), expected);
23-
}
24-
}
25-
});
26-
27-
it('should calculate log correctly', () => {
28-
const testcases = [
29-
[Infinity, 'unsupported operand type'],
30-
[0, 'unsupported operand type'],
31-
[-1, 'unsupported operand type'],
32-
[1, Math.log(1)],
33-
[100, Math.log(100)],
34-
[Math.E, Math.log(Math.E)]
35-
];
36-
const calculator = new Calculator();
37-
for (const [input, expected] of testcases) {
38-
if (typeof expected === 'string') {
39-
assert.throws(() => calculator.log(input), Error);
40-
} else {
41-
assert.strictEqual(calculator.log(input), expected);
42-
}
43-
}
44-
});
45-
});
46-

lab6/llvm-pass.so.cc

Lines changed: 25 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,34 @@
11
#include "llvm/Passes/PassPlugin.h"
22
#include "llvm/Passes/PassBuilder.h"
3-
#include "llvm/IR/Constants.h"
43
#include "llvm/IR/IRBuilder.h"
5-
#include "llvm/IR/GlobalValue.h"
6-
#include "llvm/Support/raw_ostream.h"
74

8-
namespace {
5+
using namespace llvm;
96

10-
// Module transformation to modify main function
11-
class ModuleTransform : PassInfoMixin<ModuleTransform> {
12-
public:
13-
PreservedAnalyses run(llvm::Module& Module, llvm::ModuleAnalysisManager& AM) {
14-
// Debug constant value
15-
const uint32_t DEBUG_MAGIC = 48763;
16-
17-
// Get necessary types
18-
auto& Context = Module.getContext();
19-
auto* Int32Type = llvm::Type::getInt32Ty(Context);
20-
auto* CharPtrType = llvm::Type::getInt8PtrTy(Context);
21-
22-
// Create debug function declaration
23-
auto* DebugFuncType = llvm::FunctionType::get(
24-
llvm::Type::getVoidTy(Context),
25-
{Int32Type},
26-
false
27-
);
28-
auto DebugFunction = Module.getOrInsertFunction("debug", DebugFuncType);
29-
30-
// Create magic number constant
31-
auto* MagicNumber = llvm::ConstantInt::get(Int32Type, DEBUG_MAGIC);
32-
33-
// Create string constant
34-
auto* HiddenMessage = llvm::ConstantDataArray::getString(
35-
Context,
36-
"hayaku... motohayaku!",
37-
true
38-
);
39-
40-
// Create global variable for the string
41-
auto* StringGlobal = new llvm::GlobalVariable(
42-
Module,
43-
HiddenMessage->getType(),
44-
true,
45-
llvm::GlobalValue::PrivateLinkage,
46-
HiddenMessage,
47-
".str.hayaku"
48-
);
49-
50-
// Create GEP indices for string pointer
51-
auto* ZeroIndex = llvm::ConstantInt::get(Int32Type, 0);
52-
llvm::Constant* Indices[2] = {ZeroIndex, ZeroIndex};
53-
54-
// Get pointer to string
55-
auto* StringPointer = llvm::ConstantExpr::getGetElementPtr(
56-
HiddenMessage->getType(),
57-
StringGlobal,
58-
Indices
59-
);
60-
61-
// Find and instrument main function
62-
for (auto& Func : Module) {
63-
if (Func.getName() != "main")
64-
continue;
65-
66-
llvm::errs() << "Found and instrumenting: " << Func.getName() << "\n";
67-
68-
// Create builder at entry point
69-
llvm::IRBuilder<> Builder(&Func.getEntryBlock().front());
70-
71-
// Insert debug call
72-
Builder.CreateCall(DebugFunction, {MagicNumber});
73-
74-
// Get function args
75-
auto Args = Func.arg_begin();
76-
auto* ArgCount = Args++;
77-
auto* ArgVector = Args;
78-
79-
// Modify argv[1] to point to our string
80-
auto* SecondArgPtr = Builder.CreateGEP(CharPtrType, ArgVector,
81-
llvm::ConstantInt::get(Int32Type, 1));
82-
Builder.CreateStore(StringPointer, SecondArgPtr);
83-
84-
// Replace all uses of argc with our magic value
85-
ArgCount->replaceAllUsesWith(MagicNumber);
86-
}
87-
88-
// Mark all analyses as invalidated
89-
return PreservedAnalyses::none();
90-
}
7+
struct LLVMPass : public PassInfoMixin<LLVMPass> {
8+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
919
};
9210

93-
} // anonymous namespace
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";
9419

95-
// Plugin registration
96-
extern "C" LLVM_ATTRIBUTE_WEAK
97-
::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() {
98-
return {
99-
LLVM_PLUGIN_API_VERSION,
100-
"ModuleInstrumenter", // plugin name
101-
"v1.0", // plugin version
102-
[](llvm::PassBuilder &PB) {
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) {
10328
PB.registerOptimizerLastEPCallback(
104-
[](llvm::ModulePassManager &MPM, llvm::OptimizationLevel Level) {
105-
MPM.addPass(ModuleTransform());
106-
}
107-
);
108-
}
109-
};
110-
}
29+
[](ModulePassManager &MPM, OptimizationLevel OL) {
30+
MPM.addPass(LLVMPass());
31+
});
32+
}};
33+
}
34+

0 commit comments

Comments
 (0)