Skip to content

Commit b278cc6

Browse files
authored
Add some low level tests (#19)
``` (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% ``` * import shared memory in wat tests as suggested in WebAssembly/wasi-libc#369 the corresponding toywasm change: yamt/toywasm@4d81846 ``` spacetanuki% TOYWASM=~/git/toywasm/b/toywasm python3 ~/git/wasm/wasi-testsuite/test-runner/wasi_test_runner.py -t ./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 spacetanuki% ```
1 parent 74824ba commit b278cc6

17 files changed

+407
-10
lines changed

test/build.sh

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
#!/bin/bash
1+
#! /bin/sh
22

3-
CC=${CC:=clang}
3+
set -e
44

5-
for input in testsuite/*.c; do
6-
output="testsuite/$(basename $input .c).wasm"
7-
8-
if [ "$input" -nt "$output" ]; then
9-
echo "Compiling $input"
10-
$CC "$input" testsuite/wasi_thread_spawn.S -o "$output"
11-
fi
12-
done
5+
./scripts/build-wat.sh
6+
./scripts/build-c.sh

test/scripts/build-c.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
CC=${CC:=clang}
4+
5+
for input in testsuite/*.c; do
6+
output="testsuite/$(basename $input .c).wasm"
7+
8+
if [ "$input" -nt "$output" ]; then
9+
echo "Compiling $input"
10+
$CC "$input" testsuite/wasi_thread_spawn.S -o "$output"
11+
fi
12+
done

test/scripts/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,45 @@
1+
;; When the main thread calls proc_exit, it should terminate
2+
;; a thread blocking in `memory.atomic.wait32` opcode.
3+
;;
4+
;; linear memory usage:
5+
;; 0: notify/wait
6+
7+
(module
8+
(memory (export "memory") (import "foo" "bar") 1 1 shared)
9+
(func $thread_spawn (import "wasi" "thread-spawn") (param i32) (result i32))
10+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
11+
(func (export "wasi_thread_start") (param i32 i32)
12+
;; infinite wait
13+
i32.const 0
14+
i32.const 0
15+
i64.const -1
16+
memory.atomic.wait32
17+
unreachable
18+
)
19+
(func (export "_start")
20+
;; spawn a thread
21+
i32.const 0
22+
call $thread_spawn
23+
;; check error
24+
i32.const 0
25+
i32.le_s
26+
if
27+
unreachable
28+
end
29+
;; wait 500ms to ensure the other thread block
30+
i32.const 0
31+
i32.const 0
32+
i64.const 500_000_000
33+
memory.atomic.wait32
34+
;; assert a timeout
35+
i32.const 2
36+
i32.ne
37+
if
38+
unreachable
39+
end
40+
;; exit
41+
i32.const 99
42+
call $proc_exit
43+
unreachable
44+
)
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,44 @@
1+
;; When the main thread calls proc_exit, it should terminate
2+
;; a busy-looping thread.
3+
;;
4+
;; linear memory usage:
5+
;; 0: wait
6+
7+
(module
8+
(memory (export "memory") (import "foo" "bar") 1 1 shared)
9+
(func $thread_spawn (import "wasi" "thread-spawn") (param i32) (result i32))
10+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
11+
(func (export "wasi_thread_start") (param i32 i32)
12+
;; infinite loop
13+
loop
14+
br 0
15+
end
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 to enter the busy loop
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+
)
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,54 @@
1+
;; When the main thread calls proc_exit, it should terminate
2+
;; a thread blocking in a WASI call. (poll_oneoff)
3+
;;
4+
;; linear memory usage:
5+
;; 0: wait
6+
;; 100: poll_oneoff subscription
7+
;; 200: poll_oneoff event
8+
;; 300: poll_oneoff return value
9+
10+
(module
11+
(memory (export "memory") (import "foo" "bar") 1 1 shared)
12+
(func $thread_spawn (import "wasi" "thread-spawn") (param i32) (result i32))
13+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
14+
(func $poll_oneoff (import "wasi_snapshot_preview1" "poll_oneoff") (param i32 i32 i32 i32) (result i32))
15+
(func (export "wasi_thread_start") (param i32 i32)
16+
;; long enough block
17+
;; clock_realtime, !abstime (zeros)
18+
i32.const 124 ;; 100 + offsetof(subscription, timeout)
19+
i64.const 1_000_000_000 ;; 1s
20+
i64.store
21+
i32.const 100 ;; subscription
22+
i32.const 200 ;; event (out)
23+
i32.const 1 ;; nsubscriptions
24+
i32.const 300 ;; retp (out)
25+
call $poll_oneoff
26+
unreachable
27+
)
28+
(func (export "_start")
29+
;; spawn a thread
30+
i32.const 0
31+
call $thread_spawn
32+
;; check error
33+
i32.const 0
34+
i32.le_s
35+
if
36+
unreachable
37+
end
38+
;; wait 500ms to ensure the other thread block
39+
i32.const 0
40+
i32.const 0
41+
i64.const 500_000_000
42+
memory.atomic.wait32
43+
;; assert a timeout
44+
i32.const 2
45+
i32.ne
46+
if
47+
unreachable
48+
end
49+
;; exit
50+
i32.const 99
51+
call $proc_exit
52+
unreachable
53+
)
54+
)
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+
;; When a non-main thread calls proc_exit, it should terminate
2+
;; the main thread blocking in `memory.atomic.wait32` opcode.
3+
;;
4+
;; linear memory usage:
5+
;; 0: wait
6+
7+
(module
8+
(memory (export "memory") (import "foo" "bar") 1 1 shared)
9+
(func $thread_spawn (import "wasi" "thread-spawn") (param i32) (result i32))
10+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
11+
(func (export "wasi_thread_start") (param i32 i32)
12+
;; wait 500ms to ensure the other thread block
13+
i32.const 0
14+
i32.const 0
15+
i64.const 500_000_000
16+
memory.atomic.wait32
17+
;; assert a timeout
18+
i32.const 2
19+
i32.ne
20+
if
21+
unreachable
22+
end
23+
;; exit
24+
i32.const 99
25+
call $proc_exit
26+
unreachable
27+
)
28+
(func (export "_start")
29+
;; spawn a thread
30+
i32.const 0
31+
call $thread_spawn
32+
;; check error
33+
i32.const 0
34+
i32.le_s
35+
if
36+
unreachable
37+
end
38+
;; infinite wait
39+
i32.const 0
40+
i32.const 0
41+
i64.const -1
42+
memory.atomic.wait32
43+
unreachable
44+
)
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,44 @@
1+
;; When a non-main thread calls proc_exit, it should terminate
2+
;; the main thread which is busy-looping.
3+
;;
4+
;; linear memory usage:
5+
;; 0: wait
6+
7+
(module
8+
(memory (export "memory") (import "foo" "bar") 1 1 shared)
9+
(func $thread_spawn (import "wasi" "thread-spawn") (param i32) (result i32))
10+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
11+
(func (export "wasi_thread_start") (param i32 i32)
12+
;; wait 500ms to ensure the other thread to enter the busy loop
13+
i32.const 0
14+
i32.const 0
15+
i64.const 500_000_000
16+
memory.atomic.wait32
17+
;; assert a timeout
18+
i32.const 2
19+
i32.ne
20+
if
21+
unreachable
22+
end
23+
;; exit
24+
i32.const 99
25+
call $proc_exit
26+
unreachable
27+
)
28+
(func (export "_start")
29+
;; spawn a thread
30+
i32.const 0
31+
call $thread_spawn
32+
;; check error
33+
i32.const 0
34+
i32.le_s
35+
if
36+
unreachable
37+
end
38+
;; infinite loop
39+
loop
40+
br 0
41+
end
42+
unreachable
43+
)
44+
)
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,54 @@
1+
;; When a non-main thread calls proc_exit, it should terminate
2+
;; the main thread which is blocking in a WASI call. (poll_oneoff)
3+
;;
4+
;; linear memory usage:
5+
;; 0: wait
6+
;; 100: poll_oneoff subscription
7+
;; 200: poll_oneoff event
8+
;; 300: poll_oneoff return value
9+
10+
(module
11+
(memory (export "memory") (import "foo" "bar") 1 1 shared)
12+
(func $thread_spawn (import "wasi" "thread-spawn") (param i32) (result i32))
13+
(func $proc_exit (import "wasi_snapshot_preview1" "proc_exit") (param i32))
14+
(func $poll_oneoff (import "wasi_snapshot_preview1" "poll_oneoff") (param i32 i32 i32 i32) (result i32))
15+
(func (export "wasi_thread_start") (param i32 i32)
16+
;; wait 500ms to ensure the other thread block
17+
i32.const 0
18+
i32.const 0
19+
i64.const 500_000_000
20+
memory.atomic.wait32
21+
;; assert a timeout
22+
i32.const 2
23+
i32.ne
24+
if
25+
unreachable
26+
end
27+
;; exit
28+
i32.const 99
29+
call $proc_exit
30+
unreachable
31+
)
32+
(func (export "_start")
33+
;; spawn a thread
34+
i32.const 0
35+
call $thread_spawn
36+
;; check error
37+
i32.const 0
38+
i32.le_s
39+
if
40+
unreachable
41+
end
42+
;; long enough block
43+
;; clock_realtime, !abstime (zeros)
44+
i32.const 124 ;; 100 + offsetof(subscription, timeout)
45+
i64.const 1_000_000_000 ;; 1s
46+
i64.store
47+
i32.const 100 ;; subscription
48+
i32.const 200 ;; event (out)
49+
i32.const 1 ;; nsubscriptions
50+
i32.const 300 ;; retp (out)
51+
call $poll_oneoff
52+
unreachable
53+
)
54+
)
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exit_code": 22
3+
}

0 commit comments

Comments
 (0)