Skip to content

Commit 97d1fd3

Browse files
authored
Merge branch '510558017' into lab5
2 parents 4e90742 + f930783 commit 97d1fd3

File tree

5 files changed

+116
-22
lines changed

5 files changed

+116
-22
lines changed

.github/workflows/lab-autograding.yml

+21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
os: [ubuntu-22.04]
1313
fail-fast: false
1414
steps:
15+
1516
- name: Checkout repository
1617
uses: actions/checkout@v4
1718
with:
@@ -23,6 +24,15 @@ jobs:
2324
with:
2425
node-version: '20'
2526

27+
28+
- uses: actions/checkout@v4
29+
with:
30+
ref: "${{ github.event.pull_request.merge_commit_sha }}"
31+
fetch-depth: 1
32+
- uses: actions/setup-node@v4
33+
with:
34+
node-version: latest
35+
2636
- name: Extract lab number and Check no changes other than specific files
2737
uses: actions/github-script@v5
2838
id: lab
@@ -44,17 +54,28 @@ jobs:
4454
return { number: 0 };
4555
}
4656
const labNumber = labNumberMatch[1];
57+
4758
console.log(`Lab number: ${labNumber}`);
4859
60+
console.log(`Lab number: ${labNumber}`)
61+
62+
4963
const files = await github.rest.pulls.listFiles({ owner, repo, pull_number: issue_number });
5064
const changedFiles = files.data.map((file) => file.filename);
5165
const allowedFileRegex = /^lab\d+\/main_test.js$/;
5266
const specialChangedFiles = ["lab5/Answer.md", "lab5/antiasan.c", "lab6/Answer.md", "lab7/sol.py"];
67+
5368
if (!changedFiles.every((file) => allowedFileRegex.test(file) || specialChangedFiles.includes(file))) {
5469
core.setFailed('The PR contains changes to files other than the allowed files.');
5570
}
5671
return labNumber;
5772
73+
74+
if (!changedFiles.every((file) => (allowedFileRegex.test(file) || specialChangedFiles.includes(file)))) {
75+
core.setFailed('The PR contains changes to files other than the allowed files.');
76+
}
77+
return labNumber;
78+
5879
- name: Grading
5980
run: |
6081
cd lab${{ steps.lab.outputs.result }}

lab1/main.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ class Student {
4242
}
4343
}
4444

45-
// const myClass = new MyClass();
46-
// const names = ['John', 'Jane', 'Doe', 'Smith'];
47-
// names.forEach(name => {
48-
// const student = new Student();
49-
// student.setName(name);
50-
// const newStudentId = myClass.addStudent(student);
51-
// const newStudentName = myClass.getStudentById(newStudentId).getName();
52-
// console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
45+
//const myClass = new MyClass();
46+
//const names = ['John', 'Jane', 'Doe', 'Smith'];
47+
//names.forEach(name => {
48+
// const student = new Student();
49+
//student.setName(name);
50+
// const newStudentId = myClass.addStudent(student);
51+
//const newStudentName = myClass.getStudentById(newStudentId).getName();
52+
// console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
5353
// });
5454

55-
module.exports = { MyClass, Student };
55+
module.exports = { MyClass, Student };

lab1/main_test.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,39 @@ const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

55
test("Test MyClass's addStudent", () => {
6-
// TODO
7-
throw new Error("Test not implemented");
6+
const myClass = new MyClass();
7+
const student = new Student();
8+
student.setName("John");
9+
const index = myClass.addStudent(student);
10+
assert.strictEqual(index, 0, "addStudent should return index 0 for the first student");
11+
const notAStudent = {};
12+
const indexForNotAStudent = myClass.addStudent(notAStudent);
13+
assert.strictEqual(indexForNotAStudent, -1, "addStudent should return -1 when adding a non-Student instance");
814
});
915

1016
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
17+
const myClass = new MyClass();
18+
const student = new Student();
19+
student.setName("Jane");
20+
const index = myClass.addStudent(student);
21+
const fetchedStudent = myClass.getStudentById(index);
22+
assert.strictEqual(fetchedStudent.getName(), "Jane", "getStudentById should retrieve the student with the correct name");
23+
const invalidFetchedStudent = myClass.getStudentById(-1);
24+
assert.strictEqual(invalidFetchedStudent, null, "getStudentById should return null for an invalid id");
1325
});
1426

1527
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
28+
const student = new Student();
29+
student.setName("Doe");
30+
assert.strictEqual(student.name, "Doe", "setName should correctly set the student's name");
31+
student.setName(123);
32+
assert.strictEqual(student.name, "Doe", "setName should not set name when the input is not a string");
1833
});
1934

2035
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
36+
const student = new Student();
37+
student.setName("Smith");
38+
assert.strictEqual(student.getName(), "Smith", "getName should return the correct name");
39+
const newStudent = new Student();
40+
assert.strictEqual(newStudent.getName(), '', "getName should return an empty string for a student without a name");
41+
});

lab5/Answer.md

+54-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
# Answer
22

3+
34
Name: 楊杰峰
45
ID: 510558017
56

7+
Name:
8+
ID:
9+
10+
611
## Test Valgrind and ASan
712
### Result
813
| | Valgrind | Asan |
914
| -------------------- | -------- | ---- |
15+
1016
| Heap out-of-bounds | Yes | Yes |
1117
| Stack out-of-bounds | Yes | Yes |
1218
| Global out-of-bounds | Yes | Yes |
1319
| Use-after-free | Yes | Yes |
1420
| Use-after-return | Yes | Yes |
1521

22+
| Heap out-of-bounds | | |
23+
| Stack out-of-bounds | | |
24+
| Global out-of-bounds | | |
25+
| Use-after-free | | |
26+
| Use-after-return | | |
27+
28+
1629
### Heap out-of-bounds
1730
#### Source code
1831
```
32+
1933
#include <stdio.h>
2034
#include <stdlib.h>
2135
@@ -31,6 +45,7 @@ int main() {
3145
return 0;
3246
}
3347
48+
3449
```
3550
#### Valgrind Report
3651
```
@@ -52,7 +67,6 @@ int main() {
5267
```
5368
### ASan Report
5469
```
55-
=================================================================
5670
==10519==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000024 at pc 0x56433e0122a2 bp 0x7ffcb47c35e0 sp 0x7ffcb47c35d0
5771
WRITE of size 4 at 0x603000000024 thread T0
5872
#0 0x56433e0122a1 in heap_out_of_bounds (/home/jf/510558017/510558017/lab5/heap_out_of_bounds+0x12a1)
@@ -101,6 +115,12 @@ Shadow byte legend (one shadow byte represents 8 application bytes):
101115
Right alloca redzone: cb
102116
Shadow gap: cc
103117
==10519==ABORTING
118+
119+
```
120+
### ASan Report
121+
```
122+
123+
104124
```
105125

106126
### Stack out-of-bounds
@@ -119,6 +139,7 @@ int main() {
119139
return 0;
120140
}
121141
142+
122143
```
123144
#### Valgrind Report
124145
```
@@ -140,7 +161,6 @@ valgrind: ./stack_out_of_bounds: No such file or directory==10523== Memcheck, a
140161
```
141162
### ASan Report
142163
```
143-
=================================================================
144164
==10522==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffe0476bb84 at pc 0x563a4ff0a31c bp 0x7ffe0476bb40 sp 0x7ffe0476bb30
145165
WRITE of size 4 at 0x7ffe0476bb84 thread T0
146166
#0 0x563a4ff0a31b in stack_out_of_bounds (/home/jf/510558017/510558017/lab5/stack_out_of_bounds+0x131b)
@@ -189,6 +209,12 @@ Shadow byte legend (one shadow byte represents 8 application bytes):
189209
Right alloca redzone: cb
190210
Shadow gap: cc
191211
==10522==ABORTING
212+
213+
214+
```
215+
### ASan Report
216+
```
217+
192218
```
193219

194220
### Global out-of-bounds
@@ -208,6 +234,7 @@ int main() {
208234
return 0;
209235
}
210236
237+
211238
```
212239
#### Valgrind Report
213240
```
@@ -230,7 +257,6 @@ int main() {
230257
### ASan Report
231258
```
232259
AddressSanitizer:DEADLYSIGNAL
233-
=================================================================
234260
==4271==ERROR: AddressSanitizer: SEGV on unknown address 0x55f0360a2010 (pc 0x55f03609e223 bp 0x7ffccdefbd80 sp 0x7ffccdefbd80 T0)
235261
==4271==The signal is caused by a WRITE memory access.
236262
#0 0x55f03609e222 in global_out_of_bounds (/home/jf/510558017/510558017/lab5/global_out_of_bounds+0x1222)
@@ -241,6 +267,12 @@ AddressSanitizer:DEADLYSIGNAL
241267
AddressSanitizer can not provide additional info.
242268
SUMMARY: AddressSanitizer: SEGV (/home/jf/510558017/510558017/lab5/global_out_of_bounds+0x1222) in global_out_of_bounds
243269
==4271==ABORTING
270+
271+
272+
```
273+
### ASan Report
274+
```
275+
244276
```
245277

246278
### Use-after-free
@@ -261,6 +293,7 @@ int main() {
261293
return 0;
262294
}
263295
296+
264297
```
265298
#### Valgrind Report
266299
```
@@ -337,6 +370,12 @@ Shadow byte legend (one shadow byte represents 8 application bytes):
337370
Right alloca redzone: cb
338371
Shadow gap: cc
339372
==11127==ABORTING: No such file or directory
373+
374+
375+
```
376+
### ASan Report
377+
```
378+
340379
```
341380

342381
### Use-after-return
@@ -359,6 +398,7 @@ int main() {
359398
return 0;
360399
}
361400
401+
362402
```
363403
#### Valgrind Report
364404
```
@@ -381,7 +421,6 @@ int main() {
381421
### ASan Report
382422
```
383423
AddressSanitizer:DEADLYSIGNAL
384-
=================================================================
385424
==11129==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x55c39c7c93ac bp 0x7fffe81607a0 sp 0x7fffe8160790 T0)
386425
==11129==The signal is caused by a READ memory access.
387426
==11129==Hint: address points to the zero page.
@@ -393,6 +432,12 @@ AddressSanitizer:DEADLYSIGNAL
393432
AddressSanitizer can not provide additional info.
394433
SUMMARY: AddressSanitizer: SEGV (/home/jf/510558017/510558017/lab5/use_after_return+0x13ab) in use_after_return
395434
==11129==ABORTING
435+
436+
437+
```
438+
### ASan Report
439+
```
440+
396441
```
397442

398443
## ASan Out-of-bound Write bypass Redzone
@@ -417,3 +462,8 @@ int main() {
417462
```
418463
### Why
419464
ASan 能夠偵測到剛好超出 redzone 的越界寫操作,因為它會在每個分配的內存區域前後添加 redzones,並在訪問這些區域時檢測到越界操作。
465+
466+
467+
```
468+
### Why
469+

lab5/antiasan.c

+5
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ void antiasan(unsigned long addr)
55
{
66
uintptr_t shadow_addr = (addr >> 3) + 0x7fff8000;
77
*(volatile char *)shadow_addr = 0;
8+
9+
// TODO:
10+
void antiasan(unsigned long addr)
11+
{
12+
813
}

0 commit comments

Comments
 (0)