Skip to content

Commit 4c1a7cb

Browse files
committed
feat: lab5
1 parent 38d11b1 commit 4c1a7cb

File tree

12 files changed

+221
-95
lines changed

12 files changed

+221
-95
lines changed

.github/workflows/lab-autograding.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ 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-
if (!changedFiles.every((file) => allowedFileRegex.test(file))) {
48+
const specialChangedFiles = ["lab5/Answer.md", "lab5/antiasan.c"];
49+
if (!changedFiles.every((file) => (allowedFileRegex.test(file) || specialChangedFiles.includes(file))) {
4950
core.setFailed('The PR contains changes to files other than the allowed files.');
5051
}
5152
return labNumber;

lab2/main_test.js

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,6 @@
11
const test = require('node:test');
22
const assert = require('assert');
3-
const fs = require('fs');
4-
5-
// 模擬 fs.readFile 方法,返回假數據
6-
test.mock.method(fs, 'readFile', (file, options, callback) => {
7-
callback(null, 'Alice\njohn\nBob');
8-
});
9-
10-
// 從 main.js 導入 Application 和 MailSystem 類
113
const { Application, MailSystem } = require('./main');
124

13-
// 測試 MailSystem_write() 方法
14-
test('MailSystem_write()', () => {
15-
const ms = new MailSystem();
16-
assert.strictEqual(ms.write('Alice'), 'Congrats, Alice!'); // 確認寫入郵件的功能
17-
assert.strictEqual(ms.write(null), 'Congrats, null!'); // 確認處理空值的功能
18-
assert.strictEqual(ms.write(512558014), 'Congrats, 512558014!'); // 確認處理數字名稱的功能
19-
});
20-
21-
// 測試 MailSystem_send() 方法
22-
test('MailSystem_send()', () => {
23-
const ms = new MailSystem();
24-
const name = 'Alice';
25-
test.mock.method(Math, 'random', () => 0.6); // 假設 Math.random() 始終返回 0.6
26-
assert.strictEqual(ms.send(name, 'success'), true); // 確認發送成功郵件的功能
27-
test.mock.method(Math, 'random', () => 0.4); // 假設 Math.random() 始終返回 0.4
28-
assert.strictEqual(ms.send(name, 'fail'), false); // 確認發送失敗郵件的功能
29-
});
30-
31-
// 測試 Application_getNames() 方法
32-
test('Application_getNames()', async () => {
33-
const app = new Application();
34-
const name_list = ['Alice', 'john', 'Bob'];
35-
const names = await app.getNames();
36-
assert.deepStrictEqual(names, [name_list, []]); // 確認獲取名字列表的功能
37-
});
38-
39-
// 測試 Application_getRandomPerson() 方法
40-
test('Application_getRandomPerson()', async () => {
41-
const app = new Application();
42-
const [names] = await app.getNames(); // 等待獲取名字列表
43-
const randomPerson = app.getRandomPerson();
44-
assert.ok(names.includes(randomPerson)); // 確認隨機獲取的人員在名字列表中
45-
});
46-
47-
// 測試 Application_selectNextPerson() 方法
48-
test('Application_selectNextPerson()', async () => {
49-
const app = new Application();
50-
const [names] = await app.getNames();
51-
app.selected = ['Alice'];
52-
let cnt = 0;
53-
test.mock.method(app, 'getRandomPerson', () => {
54-
if (cnt <= names.length) {
55-
return names[cnt++];
56-
}
57-
});
58-
assert.strictEqual(app.selectNextPerson(), 'john'); // 確認選擇下一個人員的功能
59-
assert.deepStrictEqual(app.selected, ['Alice', 'john']); // 確認已選擇的人員列表
60-
assert.strictEqual(app.selectNextPerson(), 'Bob'); // 確認選擇下一個人員的功能
61-
assert.deepStrictEqual(app.selected, ['Alice', 'john', 'Bob']); // 確認已選擇的人員列表
62-
assert.strictEqual(app.selectNextPerson(), null); // 確認已無可選擇的人員
63-
});
64-
65-
// 測試 Application_notifySelected() 方法
66-
test('Application_notifySelected()', async () => {
67-
const app = new Application();
68-
const [people] = await app.getNames();
69-
app.selected = [...people];
70-
app.mailSystem.send = test.mock.fn(app.mailSystem.send);
71-
app.mailSystem.write = test.mock.fn(app.mailSystem.write);
72-
app.notifySelected();
73-
assert.strictEqual(app.mailSystem.send.mock.calls.length, people.length); // 確認發送郵件的次數
74-
assert.strictEqual(app.mailSystem.write.mock.calls.length, people.length); // 確認編寫郵件的次數
75-
});
5+
// TODO: write your tests here
6+
// Remember to use Stub, Mock, and Spy when necessary

lab3/main_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { describe, it } = require('node:test');
2+
const assert = require('assert');
3+
const { Calculator } = require('./main');
4+
5+
// TODO: write your tests here

lab4/main_test.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,15 @@ const puppeteer = require('puppeteer');
88
// Navigate the page to a URL
99
await page.goto('https://pptr.dev/');
1010

11-
// Wait for the search button to appear
12-
await page.waitForSelector('.DocSearch.DocSearch-Button');
13-
11+
// Hints:
1412
// Click search button
15-
await page.click('.DocSearch.DocSearch-Button');
16-
17-
// Wait for the search input box to appear
18-
await page.waitForSelector('#docsearch-input');
19-
2013
// Type into search box
21-
await page.type('#docsearch-input', 'chipi chipi chapa chapa');
22-
2314
// Wait for search result
24-
await page.waitForSelector('.devsite-result-item-link');
25-
2615
// Get the `Docs` result section
27-
const docSection = await page.$('.devsite-result-item-link');
28-
2916
// Click on first result in `Docs` section
30-
await docSection.click();
31-
3217
// Locate the title
33-
const titleElement = await page.waitForSelector('h1');
34-
35-
// Get the text content of the title element
36-
const title = await page.evaluate(titleElement => titleElement.textContent, titleElement);
37-
3818
// Print the title
39-
console.log(title);
4019

4120
// Close the browser
4221
await browser.close();
43-
})();
22+
})();

lab5/Answer.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Answer
2+
3+
Name:
4+
ID:
5+
6+
## Test Valgrind and ASan
7+
### Result
8+
| | Valgrind | Asan |
9+
| -------------------- | -------- | ---- |
10+
| Heap out-of-bounds | | |
11+
| Stack out-of-bounds | | |
12+
| Global out-of-bounds | | |
13+
| Use-after-free | | |
14+
| Use-after-return | | |
15+
16+
### Heap out-of-bounds
17+
#### Source code
18+
```
19+
20+
```
21+
#### Valgrind Report
22+
```
23+
24+
```
25+
### ASan Report
26+
```
27+
28+
```
29+
30+
### Stack out-of-bounds
31+
#### Source code
32+
```
33+
34+
```
35+
#### Valgrind Report
36+
```
37+
38+
```
39+
### ASan Report
40+
```
41+
42+
```
43+
44+
### Global out-of-bounds
45+
#### Source code
46+
```
47+
48+
```
49+
#### Valgrind Report
50+
```
51+
52+
```
53+
### ASan Report
54+
```
55+
56+
```
57+
58+
### Use-after-free
59+
#### Source code
60+
```
61+
62+
```
63+
#### Valgrind Report
64+
```
65+
66+
```
67+
### ASan Report
68+
```
69+
70+
```
71+
72+
### Use-after-return
73+
#### Source code
74+
```
75+
76+
```
77+
#### Valgrind Report
78+
```
79+
80+
```
81+
### ASan Report
82+
```
83+
84+
```
85+
86+
## ASan Out-of-bound Write bypass Redzone
87+
### Source code
88+
```
89+
90+
```
91+
### Why
92+

lab5/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.PHONY: all
2+
all: uaf_asan
3+
4+
uaf_asan: uaf.c libantiasan.so
5+
gcc -fsanitize=address -Og -g -o $@ $< -lantiasan -L.
6+
7+
libantiasan.so: antiasan.c
8+
gcc -g -fPIC -c antiasan.c
9+
gcc -shared antiasan.o -o libantiasan.so
10+
11+
.PHINY: run
12+
run:
13+
LD_LIBRARY_PATH=. ./uaf_asan
14+
15+
.PHONY: clean
16+
clean:
17+
rm uaf_asan antiasan.o libantiasan.so

lab5/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Lab4
2+
3+
## Introduction
4+
5+
In this lab, you will write tests in `main_test.js`. You can learn how to use [Puppeteer](https://pptr.dev/) to tests a web UI.
6+
7+
## Preparation (Important!!!)
8+
9+
1. Sync fork your branch (e.g., `SQLab:311XXXXXX`)
10+
2. `git checkout -b lab4` (**NOT** your student ID !!!)
11+
12+
## Requirement
13+
14+
1. (100%) Goto https://pptr.dev/, type `chipi chipi chapa chapa` into the search box, click on **1st** result in the **Docs** section, and print the title.
15+
16+
For the detailed steps and hints, please check the slide of this lab.
17+
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 `main_test.js`. 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.

lab5/ans

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
LD_LIBRARY_PATH=. ./uaf_asan
2+
s[0x10] = H
3+
s[0x10] = H

lab5/antiasan.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// TODO:
2+
void antiasan(unsigned long addr)
3+
{
4+
5+
}

lab5/antiasan.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef HIJACK_H
2+
#define HIJACK_H
3+
4+
void antiasan(unsigned long);
5+
6+
#endif

0 commit comments

Comments
 (0)