Skip to content

Commit ce604a9

Browse files
add some string test cases
1 parent c5d7283 commit ce604a9

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

test/values/strings.wast

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@
2424
(assert_return (invoke "f1") (str.const "a"))
2525
(assert_return (invoke "f2") (str.const "☃☺️öツ"))
2626

27+
;; empty string with ptr=0, len=0
28+
(component
29+
(core module $M
30+
(memory (export "mem") 1)
31+
(func (export "f") (result i32)
32+
(i32.store (i32.const 0) (i32.const 0))
33+
(i32.store (i32.const 4) (i32.const 0))
34+
(i32.const 0)
35+
)
36+
)
37+
(core instance $m (instantiate $M))
38+
(func (export "f") (result string) (canon lift (core func $m "f") (memory $m "mem")))
39+
)
40+
(assert_return (invoke "f") (str.const ""))
41+
42+
;; empty string with non-zero in-bounds ptr, len=0
43+
(component
44+
(core module $M
45+
(memory (export "mem") 1)
46+
(func (export "f") (result i32)
47+
(i32.store (i32.const 0) (i32.const 100))
48+
(i32.store (i32.const 4) (i32.const 0))
49+
(i32.const 0)
50+
)
51+
)
52+
(core instance $m (instantiate $M))
53+
(func (export "f") (result string) (canon lift (core func $m "f") (memory $m "mem")))
54+
)
55+
(assert_return (invoke "f") (str.const ""))
56+
57+
;; out-of-bounds pointer traps even with len=0
2758
(component
2859
(core module $M
2960
(memory (export "mem") 1)
@@ -36,4 +67,70 @@
3667
(core instance $m (instantiate $M))
3768
(func (export "f") (result string) (canon lift (core func $m "f") (memory $m "mem")))
3869
)
70+
(assert_trap (invoke "f") "string pointer/length oob of memory")
71+
72+
;; invalid UTF-8: 0xFF is never valid
73+
(component
74+
(core module $M
75+
(memory (export "mem") 1)
76+
(func (export "f") (result i32)
77+
(i32.store (i32.const 0) (i32.const 8))
78+
(i32.store (i32.const 4) (i32.const 1))
79+
(i32.store8 (i32.const 8) (i32.const 0xff))
80+
(i32.const 0)
81+
)
82+
)
83+
(core instance $m (instantiate $M))
84+
(func (export "f") (result string) (canon lift (core func $m "f") (memory $m "mem")))
85+
)
86+
(assert_trap (invoke "f") "invalid utf-8")
87+
88+
;; truncated multibyte UTF-8: leading byte 0xC3 expects a continuation byte
89+
(component
90+
(core module $M
91+
(memory (export "mem") 1)
92+
(func (export "f") (result i32)
93+
(i32.store (i32.const 0) (i32.const 8))
94+
(i32.store (i32.const 4) (i32.const 1))
95+
(i32.store8 (i32.const 8) (i32.const 0xc3))
96+
(i32.const 0)
97+
)
98+
)
99+
(core instance $m (instantiate $M))
100+
(func (export "f") (result string) (canon lift (core func $m "f") (memory $m "mem")))
101+
)
102+
(assert_trap (invoke "f") "invalid utf-8")
103+
104+
;; string at end of memory page boundary
105+
(component
106+
(core module $M
107+
(memory (export "mem") 1)
108+
(func (export "f") (result i32)
109+
;; place "ok" at the very end of the first page (65536 - 2 = 65534)
110+
(i32.store (i32.const 0) (i32.const 65534))
111+
(i32.store (i32.const 4) (i32.const 2))
112+
(i32.store8 (i32.const 65534) (i32.const 111)) ;; 'o'
113+
(i32.store8 (i32.const 65535) (i32.const 107)) ;; 'k'
114+
(i32.const 0)
115+
)
116+
)
117+
(core instance $m (instantiate $M))
118+
(func (export "f") (result string) (canon lift (core func $m "f") (memory $m "mem")))
119+
)
120+
(assert_return (invoke "f") (str.const "ok"))
121+
122+
;; string one byte past end of memory traps
123+
(component
124+
(core module $M
125+
(memory (export "mem") 1)
126+
(func (export "f") (result i32)
127+
(i32.store (i32.const 0) (i32.const 65535))
128+
(i32.store (i32.const 4) (i32.const 2))
129+
(i32.store8 (i32.const 65535) (i32.const 111))
130+
(i32.const 0)
131+
)
132+
)
133+
(core instance $m (instantiate $M))
134+
(func (export "f") (result string) (canon lift (core func $m "f") (memory $m "mem")))
135+
)
39136
(assert_trap (invoke "f") "string pointer/length out of bounds of memory")

0 commit comments

Comments
 (0)