Skip to content

Commit b228d7c

Browse files
authored
[test] Improve array.init_elem tests (#2097)
Test the identities of the initialized elements. This would have caught a bug in the binaryen interpreter where the first element of the range was copied `n` times. Also add a test that initializing from the same element segment multiple times writes the same references each time (rather than re-evaluating the initializer expressions, for example). This catches another bug in the binaryen interpreter. See WebAssembly/binaryen#8349.
1 parent c5a8cab commit b228d7c

File tree

1 file changed

+62
-13
lines changed

1 file changed

+62
-13
lines changed

test/core/gc/array_init_elem.wast

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,30 @@
4242
)
4343

4444
(module
45-
(type $t_f (func))
46-
(type $arrref (array (ref $t_f)))
4745
(type $arrref_mut (array (mut funcref)))
4846

49-
(global $g_arrref (ref $arrref) (array.new $arrref (ref.func $dummy) (i32.const 12)))
5047
(global $g_arrref_mut (ref $arrref_mut) (array.new_default $arrref_mut (i32.const 12)))
5148

5249
(table $t 1 funcref)
5350

54-
(elem $e1 func $dummy $dummy $dummy $dummy $dummy $dummy $dummy $dummy $dummy $dummy $dummy $dummy)
55-
56-
(func $dummy
57-
)
58-
59-
(func (export "array_call_nth") (param $1 i32)
60-
(table.set $t (i32.const 0) (array.get $arrref_mut (global.get $g_arrref_mut) (local.get $1)))
61-
(call_indirect $t (i32.const 0))
51+
(elem $e1 func $zero $one $two $three $four $five $six $seven $eight $nine $ten $eleven)
52+
53+
(func $zero (result i32) (i32.const 0))
54+
(func $one (result i32) (i32.const 1))
55+
(func $two (result i32) (i32.const 2))
56+
(func $three (result i32) (i32.const 3))
57+
(func $four (result i32) (i32.const 4))
58+
(func $five (result i32) (i32.const 5))
59+
(func $six (result i32) (i32.const 6))
60+
(func $seven (result i32) (i32.const 7))
61+
(func $eight (result i32) (i32.const 8))
62+
(func $nine (result i32) (i32.const 9))
63+
(func $ten (result i32) (i32.const 10))
64+
(func $eleven (result i32) (i32.const 11))
65+
66+
(func (export "array_call_nth") (param $n i32) (result i32)
67+
(table.set $t (i32.const 0) (array.get $arrref_mut (global.get $g_arrref_mut) (local.get $n)))
68+
(call_indirect $t (result i32) (i32.const 0))
6269
)
6370

6471
(func (export "array_init_elem-null")
@@ -98,11 +105,53 @@
98105
;; normal cases
99106
(assert_return (invoke "array_init_elem" (i32.const 2) (i32.const 3) (i32.const 2)))
100107
(assert_trap (invoke "array_call_nth" (i32.const 1)) "uninitialized element")
101-
(assert_return (invoke "array_call_nth" (i32.const 2)))
102-
(assert_return (invoke "array_call_nth" (i32.const 3)))
108+
(assert_return (invoke "array_call_nth" (i32.const 2)) (i32.const 3))
109+
(assert_return (invoke "array_call_nth" (i32.const 3)) (i32.const 4))
103110
(assert_trap (invoke "array_call_nth" (i32.const 4)) "uninitialized element")
104111

105112
;; init_data/elem with dropped segments traps for non-zero length
106113
(assert_return (invoke "drop_segs"))
107114
(assert_return (invoke "array_init_elem" (i32.const 0) (i32.const 0) (i32.const 0)))
108115
(assert_trap (invoke "array_init_elem" (i32.const 0) (i32.const 0) (i32.const 1)) "out of bounds table access")
116+
117+
(module
118+
(type $arrref_mut (array (mut arrayref)))
119+
120+
(global $g_arrref_mut (ref $arrref_mut) (array.new_default $arrref_mut (i32.const 2)))
121+
122+
(elem $e1 arrayref
123+
(item (array.new_default $arrref_mut (i32.const 1)))
124+
(item (array.new_default $arrref_mut (i32.const 2)))
125+
)
126+
127+
(func (export "array_init_elem") (param $1 i32) (param $2 i32) (param $3 i32)
128+
(array.init_elem $arrref_mut $e1 (global.get $g_arrref_mut) (local.get $1) (local.get $2) (local.get $3))
129+
)
130+
131+
(func (export "array_len_nth") (param $n i32) (result i32)
132+
(array.len (array.get $arrref_mut (global.get $g_arrref_mut) (local.get $n)))
133+
)
134+
135+
(func (export "array_eq_elems") (param $i i32) (param $j i32) (result i32)
136+
(ref.eq
137+
(array.get $arrref_mut (global.get $g_arrref_mut) (local.get $i))
138+
(array.get $arrref_mut (global.get $g_arrref_mut) (local.get $j))
139+
)
140+
)
141+
)
142+
143+
;; Array starts uninitialized
144+
(assert_trap (invoke "array_len_nth" (i32.const 0)) "null array reference")
145+
(assert_trap (invoke "array_len_nth" (i32.const 1)) "null array reference")
146+
147+
;; Initialize the array
148+
(assert_return (invoke "array_init_elem" (i32.const 0) (i32.const 0) (i32.const 2)))
149+
(assert_return (invoke "array_len_nth" (i32.const 0)) (i32.const 1))
150+
(assert_return (invoke "array_len_nth" (i32.const 1)) (i32.const 2))
151+
(assert_return (invoke "array_eq_elems" (i32.const 0) (i32.const 1)) (i32.const 0))
152+
153+
;; Copy the first element at the second index and check that they are equal.
154+
(assert_return (invoke "array_init_elem" (i32.const 1) (i32.const 0) (i32.const 1)))
155+
(assert_return (invoke "array_len_nth" (i32.const 0)) (i32.const 1))
156+
(assert_return (invoke "array_len_nth" (i32.const 1)) (i32.const 1))
157+
(assert_return (invoke "array_eq_elems" (i32.const 0) (i32.const 1)) (i32.const 1))

0 commit comments

Comments
 (0)