1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
(module
(type $struct (struct (mut i32)))
(type $extendedstruct (struct (mut i32) f64))
(type $bytes (array (mut i8)))
(type $void_func (func))
(type $int_func (func (result i32)))
(import "fuzzing-support" "log-i32" (func $log (param i32)))
(func "structs"
(local $x (ref null $struct))
(local $y (ref null $struct))
(local.set $x
(struct.new_default_with_rtt $struct
(rtt.canon $struct)
)
)
;; The value is initialized to 0
;; Note: -Oz will optimize all these to constants thanks to Precompute
(call $log
(struct.get $struct 0 (local.get $x))
)
;; Assigning a value works
(struct.set $struct 0
(local.get $x)
(i32.const 42)
)
(call $log
(struct.get $struct 0 (local.get $x))
)
;; References are references, so writing to one's value affects the other's
(local.set $y (local.get $x))
(struct.set $struct 0
(local.get $y)
(i32.const 100)
)
(call $log
(struct.get $struct 0 (local.get $x))
)
(call $log
(struct.get $struct 0 (local.get $y))
)
)
(func "arrays"
(local $x (ref null $bytes))
(local.set $x
(array.new_with_rtt $bytes
(i32.const 42) ;; value to splat into the array
(i32.const 50) ;; size
(rtt.canon $bytes)
)
)
;; The length should be 50
(call $log
(array.len $bytes (local.get $x))
)
;; The value should be 42
(call $log
(array.get_u $bytes (local.get $x) (i32.const 10))
)
;; Write a value that will be truncated into an i8
(array.set $bytes (local.get $x) (i32.const 10) (i32.const 0xff80))
;; The value should be 0x80 (-128 or 128 depending on signed/unsigned)
(call $log
(array.get_u $bytes (local.get $x) (i32.const 10))
)
(call $log
(array.get_s $bytes (local.get $x) (i32.const 10))
)
;; Other items than the one at index 10 are unaffected.
(call $log
(array.get_s $bytes (local.get $x) (i32.const 20))
)
)
(func "rtts"
(local $any anyref)
;; Casting null returns null.
(call $log (ref.is_null
(ref.cast (ref.null $struct) (rtt.canon $struct))
))
;; Testing null returns 0.
(call $log
(ref.test (ref.null $struct) (rtt.canon $struct))
)
;; Testing something completely wrong (struct vs array) returns 0.
(call $log
(ref.test
(array.new_with_rtt $bytes
(i32.const 20)
(i32.const 10)
(rtt.canon $bytes)
)
(rtt.canon $struct)
)
)
;; Testing a thing with the same RTT returns 1.
(call $log
(ref.test
(struct.new_default_with_rtt $struct
(rtt.canon $struct)
)
(rtt.canon $struct)
)
)
;; A bad downcast returns 0: we create a struct, which is not a extendedstruct.
(call $log
(ref.test
(struct.new_default_with_rtt $struct
(rtt.canon $struct)
)
(rtt.canon $extendedstruct)
)
)
;; Create a extendedstruct with RTT y, and upcast statically to anyref.
(local.set $any
(struct.new_default_with_rtt $extendedstruct
(rtt.sub $extendedstruct (rtt.canon $struct))
)
)
;; Casting to y, the exact same RTT, works.
(call $log
(ref.test
(local.get $any)
(rtt.sub $extendedstruct (rtt.canon $struct))
)
)
;; Casting to z, another RTT of the same data type, fails.
(call $log
(ref.test
(local.get $any)
(rtt.canon $extendedstruct)
)
)
;; Casting to x, the parent of y, works.
(call $log
(ref.test
(local.get $any)
(rtt.canon $struct)
)
)
)
(func "br_on_cast"
(local $any anyref)
;; create a simple $struct, store it in an anyref
(local.set $any
(struct.new_default_with_rtt $struct (rtt.canon $struct))
)
(drop
(block $block (result ($ref $struct))
(drop
(block $extendedblock (result (ref $extendedstruct))
(drop
;; second, try to cast our simple $struct to what it is, which will work
(br_on_cast $block
;; first, try to cast our simple $struct to an extended, which will fail
(br_on_cast $extendedblock
(local.get $any) (rtt.canon $extendedstruct)
)
(rtt.canon $struct)
)
)
(call $log (i32.const -1)) ;; we should never get here
(return)
)
)
(call $log (i32.const -2)) ;; we should never get here either
(return)
)
)
(call $log (i32.const 3)) ;; we should get here
(drop
(block $never (result (ref $extendedstruct))
;; an untaken br_on_cast, with unreachable rtt - so we cannot use the
;; RTT in binaryen IR to find the cast type.
(br_on_cast $never (ref.null $struct) (unreachable))
(unreachable)
)
)
)
(func "cast-null-anyref-to-gc"
;; a null anyref is a literal which is not even of GC data, as it's not an
;; array or a struct, so our casting code should not assume it is. it is ok
;; to try to cast it, and the result should be 0.
(call $log
(ref.test
(ref.null any)
(rtt.canon $struct)
)
)
)
(func "br_on_data" (param $x anyref)
(local $y anyref)
(drop
(block $data (result dataref)
(local.set $y
(br_on_data $data (local.get $x))
)
(call $log (i32.const 1))
(ref.null data)
)
)
)
(func $a-void-func
(call $log (i32.const 1337))
)
(func "$rtt-and-cast-on-func"
(call $log (i32.const 0))
(drop
(rtt.canon $void_func)
)
(call $log (i32.const 1))
(drop
(rtt.canon $int_func)
)
(call $log (i32.const 2))
;; a valid cast
(call_ref
(ref.cast (ref.func $a-void-func) (rtt.canon $void_func))
)
(call $log (i32.const 3))
;; an invalid cast
(drop (call_ref
(ref.cast (ref.func $a-void-func) (rtt.canon $int_func))
))
;; will never be reached
(call $log (i32.const 4))
)
)
|