Skip to content

Commit 4ad2f95

Browse files
committed
Add some low level tests
``` (venv) spacetanuki% TOYWASM=~/git/toywasm/b/toywasm python3 test-runner/wasi_test_runner.py -t ~/git/wasm/wasi-threads/test/testsuite -r ~/git/toywasm/test/wasi-testsuite-adapter.py Test wasi_threads_exit_nonmain_wasi passed Test wasi_threads_exit_main_busy passed Test wasi_threads_exit_main_wasi passed Test wasi_threads_exit_nonmain_busy passed Test wasi_threads_spawn passed Test wasi_threads_exit_main_block passed Test wasi_threads_exit_nonmain_block passed ===== Test results ===== Runtime: toywasm v0.0 Suite: WASI threads proposal Total: 7 Passed: 7 Failed: 0 Test suites: 1 passed, 0 total Tests: 7 passed, 0 total (venv) spacetanuki% ```
1 parent 372f6fe commit 4ad2f95

15 files changed

+338
-0
lines changed

test/build-wat.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#! /bin/sh
2+
3+
WAT2WASM=${WAT2WASM:-wat2wasm}
4+
for wat in testsuite/*.wat; do
5+
${WAT2WASM} --enable-threads -o ${wat%%.wat}.wasm ${wat}
6+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_code": 99
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(module
2+
(func $thread_spawn (import "wasi" "thread_spawn") (param i32) (result i32))
3+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
4+
(func (export "wasi_thread_start") (param i32 i32)
5+
;; infinite wait
6+
i32.const 0
7+
i32.const 0
8+
i64.const -1
9+
memory.atomic.wait32
10+
unreachable
11+
)
12+
(func (export "_start")
13+
;; spawn a thread
14+
i32.const 0
15+
call $thread_spawn
16+
;; check error
17+
i32.const 0
18+
i32.le_s
19+
if
20+
unreachable
21+
end
22+
;; wait 500ms to ensure the other thread block
23+
i32.const 0
24+
i32.const 0
25+
i64.const 500_000_000
26+
memory.atomic.wait32
27+
;; assert a timeout
28+
i32.const 2
29+
i32.ne
30+
if
31+
unreachable
32+
end
33+
;; exit
34+
i32.const 99
35+
call $proc_exit
36+
unreachable
37+
)
38+
(memory 1 1 shared)
39+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_code": 99
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(module
2+
(func $thread_spawn (import "wasi" "thread_spawn") (param i32) (result i32))
3+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
4+
(func (export "wasi_thread_start") (param i32 i32)
5+
;; infinite loop
6+
loop
7+
br 0
8+
end
9+
unreachable
10+
)
11+
(func (export "_start")
12+
;; spawn a thread
13+
i32.const 0
14+
call $thread_spawn
15+
;; check error
16+
i32.const 0
17+
i32.le_s
18+
if
19+
unreachable
20+
end
21+
;; wait 500ms to ensure the other thread to enter the busy loop
22+
i32.const 0
23+
i32.const 0
24+
i64.const 500_000_000
25+
memory.atomic.wait32
26+
;; assert a timeout
27+
i32.const 2
28+
i32.ne
29+
if
30+
unreachable
31+
end
32+
;; exit
33+
i32.const 99
34+
call $proc_exit
35+
unreachable
36+
)
37+
(memory 1 1 shared)
38+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_code": 99
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(module
2+
(func $thread_spawn (import "wasi" "thread_spawn") (param i32) (result i32))
3+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
4+
(func $poll_oneoff (import "wasi_snapshot_preview1" "poll_oneoff") (param i32 i32 i32 i32) (result i32))
5+
(func (export "wasi_thread_start") (param i32 i32)
6+
;; long enough block
7+
;; clock_realtime, !abstime (zeros)
8+
i32.const 124 ;; 100 + offsetof(subscription, timeout)
9+
i64.const 1_000_000_000 ;; 1s
10+
i64.store
11+
i32.const 100 ;; subscription
12+
i32.const 200 ;; event (out)
13+
i32.const 1 ;; nsubscriptions
14+
i32.const 300 ;; retp (out)
15+
call $poll_oneoff
16+
unreachable
17+
)
18+
(func (export "_start")
19+
;; spawn a thread
20+
i32.const 0
21+
call $thread_spawn
22+
;; check error
23+
i32.const 0
24+
i32.le_s
25+
if
26+
unreachable
27+
end
28+
;; wait 500ms to ensure the other thread block
29+
i32.const 0
30+
i32.const 0
31+
i64.const 500_000_000
32+
memory.atomic.wait32
33+
;; assert a timeout
34+
i32.const 2
35+
i32.ne
36+
if
37+
unreachable
38+
end
39+
;; exit
40+
i32.const 99
41+
call $proc_exit
42+
unreachable
43+
)
44+
(memory 1 1 shared)
45+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_code": 99
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(module
2+
(func $thread_spawn (import "wasi" "thread_spawn") (param i32) (result i32))
3+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
4+
(func (export "wasi_thread_start") (param i32 i32)
5+
;; wait 500ms to ensure the other thread block
6+
i32.const 0
7+
i32.const 0
8+
i64.const 500_000_000
9+
memory.atomic.wait32
10+
;; assert a timeout
11+
i32.const 2
12+
i32.ne
13+
if
14+
unreachable
15+
end
16+
;; exit
17+
i32.const 99
18+
call $proc_exit
19+
unreachable
20+
)
21+
(func (export "_start")
22+
;; spawn a thread
23+
i32.const 0
24+
call $thread_spawn
25+
;; check error
26+
i32.const 0
27+
i32.le_s
28+
if
29+
unreachable
30+
end
31+
;; infinite wait
32+
i32.const 0
33+
i32.const 0
34+
i64.const -1
35+
memory.atomic.wait32
36+
unreachable
37+
)
38+
(memory 1 1 shared)
39+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_code": 99
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(module
2+
(func $thread_spawn (import "wasi" "thread_spawn") (param i32) (result i32))
3+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
4+
(func (export "wasi_thread_start") (param i32 i32)
5+
;; wait 500ms to ensure the other thread to enter the busy loop
6+
i32.const 0
7+
i32.const 0
8+
i64.const 500_000_000
9+
memory.atomic.wait32
10+
;; assert a timeout
11+
i32.const 2
12+
i32.ne
13+
if
14+
unreachable
15+
end
16+
;; exit
17+
i32.const 99
18+
call $proc_exit
19+
unreachable
20+
)
21+
(func (export "_start")
22+
;; spawn a thread
23+
i32.const 0
24+
call $thread_spawn
25+
;; check error
26+
i32.const 0
27+
i32.le_s
28+
if
29+
unreachable
30+
end
31+
;; infinite loop
32+
loop
33+
br 0
34+
end
35+
unreachable
36+
)
37+
(memory 1 1 shared)
38+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_code": 99
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(module
2+
(func $thread_spawn (import "wasi" "thread_spawn") (param i32) (result i32))
3+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
4+
(func $poll_oneoff (import "wasi_snapshot_preview1" "poll_oneoff") (param i32 i32 i32 i32) (result i32))
5+
(func (export "wasi_thread_start") (param i32 i32)
6+
;; wait 500ms to ensure the other thread block
7+
i32.const 0
8+
i32.const 0
9+
i64.const 500_000_000
10+
memory.atomic.wait32
11+
;; assert a timeout
12+
i32.const 2
13+
i32.ne
14+
if
15+
unreachable
16+
end
17+
;; exit
18+
i32.const 99
19+
call $proc_exit
20+
unreachable
21+
)
22+
(func (export "_start")
23+
;; spawn a thread
24+
i32.const 0
25+
call $thread_spawn
26+
;; check error
27+
i32.const 0
28+
i32.le_s
29+
if
30+
unreachable
31+
end
32+
;; long enough block
33+
;; clock_realtime, !abstime (zeros)
34+
i32.const 124 ;; 100 + offsetof(subscription, timeout)
35+
i64.const 1_000_000_000 ;; 1s
36+
i64.store
37+
i32.const 100 ;; subscription
38+
i32.const 200 ;; event (out)
39+
i32.const 1 ;; nsubscriptions
40+
i32.const 300 ;; retp (out)
41+
call $poll_oneoff
42+
unreachable
43+
)
44+
(memory 1 1 shared)
45+
)
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_code": 22
3+
}

test/testsuite/wasi_threads_spawn.wat

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
(module
2+
(func $thread_spawn (import "wasi" "thread_spawn") (param i32) (result i32))
3+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
4+
(func (export "wasi_thread_start") (param i32 i32)
5+
;; store tid
6+
i32.const 4
7+
local.get 0
8+
i32.store
9+
;; store user pointer
10+
i32.const 8
11+
local.get 1
12+
i32.store
13+
;; notify the main
14+
i32.const 0
15+
i32.const 1
16+
i32.atomic.store
17+
i32.const 0
18+
i32.const 1
19+
memory.atomic.notify
20+
drop
21+
;; returning from wasi_thread_start terminates only this thread
22+
)
23+
(func (export "_start") (local i32)
24+
;; spawn a thread
25+
i32.const 12345 ;; user pointer
26+
call $thread_spawn
27+
;; check error
28+
local.tee 0 ;; save the tid to check later
29+
i32.const 0
30+
i32.le_s
31+
if
32+
unreachable
33+
end
34+
;; wait for the spawned thread to run
35+
i32.const 0
36+
i32.const 0
37+
i64.const -1
38+
memory.atomic.wait32
39+
;; assert it was not a timeout
40+
i32.const 2
41+
i32.eq
42+
if
43+
unreachable
44+
end
45+
;; check the tid
46+
local.get 0
47+
i32.const 4
48+
i32.load
49+
i32.ne
50+
if
51+
unreachable
52+
end
53+
;; check the user pointer
54+
i32.const 8
55+
i32.load
56+
i32.const 12345
57+
i32.ne
58+
if
59+
unreachable
60+
end
61+
;; exit
62+
i32.const 22
63+
call $proc_exit
64+
unreachable
65+
)
66+
(memory 1 1 shared)
67+
)

0 commit comments

Comments
 (0)