Debian Codegraph @

crates/wasm-smith/src/core/code_builder.rs

raw
1 use super::{
2 CompositeInnerType, Elements, FuncType, Instruction, InstructionKind::*, InstructionKinds,
3 Module, ValType,
4 };
5 use crate::{MemoryOffsetChoices, unique_string};
6 use arbitrary::{Result, Unstructured};
7 use std::collections::{BTreeMap, BTreeSet};
8 use std::rc::Rc;
9 use wasm_encoder::{
10 AbstractHeapType, ArrayType, BlockType, Catch, ConstExpr, ExportKind, FieldType, GlobalType,
11 HeapType, MemArg, RefType, StorageType, StructType,
12 };
13 mod no_traps;
14
15 macro_rules! instructions {
16 (
17 $(
18 ($predicate:expr, $generator_fn:ident, $instruction_kind:ident $(, $cost:tt)?),
19 )*
20 ) => {
21 static NUM_OPTIONS: usize = instructions!(
22 @count;
23 $( $generator_fn )*
24 );
25
26 fn choose_instruction(
27 u: &mut Unstructured<'_>,
28 module: &Module,
29 allowed_instructions: InstructionKinds,
30 builder: &mut CodeBuilder,
31 ) -> Option<
32 fn(&mut Unstructured<'_>, &Module, &mut CodeBuilder, &mut Vec<Instruction>) -> Result<()>
33 > {
34 builder.allocs.options.clear();
35 let mut cost = 0;
36 // Unroll the loop that checks whether each instruction is valid in
37 // the current context and, if it is valid, pushes it onto our
38 // options. Unrolling this loops lets us avoid dynamic calls through
39 // function pointers and, furthermore, each call site can be branch
40 // predicted and even inlined. This saved us about 30% of time in
41 // the `corpus` benchmark.
42 $(
43 let predicate: Option<fn(&Module, &mut CodeBuilder) -> bool> = $predicate;
44 if predicate.map_or(true, |f| f(module, builder))
45 && allowed_instructions.contains($instruction_kind) {
46 builder.allocs.options.push(($generator_fn, cost));
47 cost += 1000 $(- $cost)?;
48 }
49 )*
50
51 // If there aren't actually any candidate instructions due to
52 // various filters in place then return `None` to indicate the
53 // situation.
54 if cost == 0 {
55 return None;
56 }
57
58 let i = u.int_in_range(0..=cost).ok()?;
59 let idx = builder
60 .allocs
61 .options
62 .binary_search_by_key(&i,|p| p.1)
63 .unwrap_or_else(|i| i - 1);
64 Some(builder.allocs.options[idx].0)
65 }
66 };
67
68 ( @count; ) => {
69 0
70 };
71 ( @count; $x:ident $( $xs:ident )* ) => {
72 1 + instructions!( @count; $( $xs )* )
73 };
74 }
75
76 // The static set of options of instruction to generate that could be valid at
77 // some given time. One entry per Wasm instruction.
78 //
79 // Each entry is made up of up to three parts:
80 //
81 // 1. A predicate for whether this is a valid choice, if any. `None` means that
82 // the choice is always applicable.
83 //
84 // 2. The function to generate the instruction, given that we've made this
85 // choice.
86 //
87 // 3. The `InstructionKind` the instruction belongs to; this allows filtering
88 // out instructions by category.
89 //
90 // 4. An optional number used to weight how often this instruction is chosen.
91 // Higher numbers are less likely to be chosen, and number specified must be
92 // less than 1000.
93 instructions! {
94 // Control instructions.
95 (Some(unreachable_valid), unreachable, Control, 990),
96 (None, nop, Control, 800),
97 (None, block, Control),
98 (None, r#loop, Control),
99 (Some(try_table_valid), try_table, Control),
100 (Some(if_valid), r#if, Control),
101 (Some(else_valid), r#else, Control),
102 (Some(end_valid), end, Control),
103 (Some(br_valid), br, Control),
104 (Some(br_if_valid), br_if, Control),
105 (Some(br_table_valid), br_table, Control),
106 (Some(return_valid), r#return, Control, 900),
107 (Some(call_valid), call, Control),
108 (Some(call_ref_valid), call_ref, Control),
109 (Some(call_indirect_valid), call_indirect, Control),
110 (Some(return_call_valid), return_call, Control),
111 (Some(return_call_ref_valid), return_call_ref, Control),
112 (Some(return_call_indirect_valid), return_call_indirect, Control),
113 (Some(throw_valid), throw, Control, 850),
114 (Some(throw_ref_valid), throw_ref, Control, 850),
115 (Some(br_on_null_valid), br_on_null, Control),
116 (Some(br_on_non_null_valid), br_on_non_null, Control),
117 (Some(br_on_cast_valid), br_on_cast, Control),
118 (Some(br_on_cast_fail_valid), br_on_cast_fail, Control),
119 // Parametric instructions.
120 (Some(drop_valid), drop, Parametric, 990),
121 (Some(select_valid), select, Parametric),
122 // Variable instructions.
123 (Some(local_get_valid), local_get, Variable),
124 (Some(local_set_valid), local_set, Variable),
125 (Some(local_set_valid), local_tee, Variable),
126 (Some(global_get_valid), global_get, Variable),
127 (Some(global_set_valid), global_set, Variable),
128 // Memory instructions.
129 (Some(have_memory_and_offset), i32_load, MemoryInt),
130 (Some(have_memory_and_offset), i64_load, MemoryInt),
131 (Some(have_memory_and_offset), f32_load, Memory),
132 (Some(have_memory_and_offset), f64_load, Memory),
133 (Some(have_memory_and_offset), i32_load_8_s, MemoryInt),
134 (Some(have_memory_and_offset), i32_load_8_u, MemoryInt),
135 (Some(have_memory_and_offset), i32_load_16_s, MemoryInt),
136 (Some(have_memory_and_offset), i32_load_16_u, MemoryInt),
137 (Some(have_memory_and_offset), i64_load_8_s, MemoryInt),
138 (Some(have_memory_and_offset), i64_load_16_s, MemoryInt),
139 (Some(have_memory_and_offset), i64_load_32_s, MemoryInt),
140 (Some(have_memory_and_offset), i64_load_8_u, MemoryInt),
141 (Some(have_memory_and_offset), i64_load_16_u, MemoryInt),
142 (Some(have_memory_and_offset), i64_load_32_u, MemoryInt),
143 (Some(i32_store_valid), i32_store, MemoryInt),
144 (Some(i64_store_valid), i64_store, MemoryInt),
145 (Some(f32_store_valid), f32_store, Memory),
146 (Some(f64_store_valid), f64_store, Memory),
147 (Some(i32_store_valid), i32_store_8, MemoryInt),
148 (Some(i32_store_valid), i32_store_16, MemoryInt),
149 (Some(i64_store_valid), i64_store_8, MemoryInt),
150 (Some(i64_store_valid), i64_store_16, MemoryInt),
151 (Some(i64_store_valid), i64_store_32, MemoryInt),
152 (Some(have_memory), memory_size, MemoryInt),
153 (Some(memory_grow_valid), memory_grow, MemoryInt),
154 (Some(memory_init_valid), memory_init, MemoryInt),
155 (Some(data_drop_valid), data_drop, MemoryInt),
156 (Some(memory_copy_valid), memory_copy, MemoryInt),
157 (Some(memory_fill_valid), memory_fill, MemoryInt),
158 // Numeric instructions.
159 (None, i32_const, NumericInt),
160 (None, i64_const, NumericInt),
161 (None, f32_const, Numeric),
162 (None, f64_const, Numeric),
163 (Some(i32_on_stack), i32_eqz, NumericInt),
164 (Some(i32_i32_on_stack), i32_eq, NumericInt),
165 (Some(i32_i32_on_stack), i32_ne, NumericInt),
166 (Some(i32_i32_on_stack), i32_lt_s, NumericInt),
167 (Some(i32_i32_on_stack), i32_lt_u, NumericInt),
168 (Some(i32_i32_on_stack), i32_gt_s, NumericInt),
169 (Some(i32_i32_on_stack), i32_gt_u, NumericInt),
170 (Some(i32_i32_on_stack), i32_le_s, NumericInt),
171 (Some(i32_i32_on_stack), i32_le_u, NumericInt),
172 (Some(i32_i32_on_stack), i32_ge_s, NumericInt),
173 (Some(i32_i32_on_stack), i32_ge_u, NumericInt),
174 (Some(i64_on_stack), i64_eqz, NumericInt),
175 (Some(i64_i64_on_stack), i64_eq, NumericInt),
176 (Some(i64_i64_on_stack), i64_ne, NumericInt),
177 (Some(i64_i64_on_stack), i64_lt_s, NumericInt),
178 (Some(i64_i64_on_stack), i64_lt_u, NumericInt),
179 (Some(i64_i64_on_stack), i64_gt_s, NumericInt),
180 (Some(i64_i64_on_stack), i64_gt_u, NumericInt),
181 (Some(i64_i64_on_stack), i64_le_s, NumericInt),
182 (Some(i64_i64_on_stack), i64_le_u, NumericInt),
183 (Some(i64_i64_on_stack), i64_ge_s, NumericInt),
184 (Some(i64_i64_on_stack), i64_ge_u, NumericInt),
185 (Some(f32_f32_on_stack), f32_eq, Numeric),
186 (Some(f32_f32_on_stack), f32_ne, Numeric),
187 (Some(f32_f32_on_stack), f32_lt, Numeric),
188 (Some(f32_f32_on_stack), f32_gt, Numeric),
189 (Some(f32_f32_on_stack), f32_le, Numeric),
190 (Some(f32_f32_on_stack), f32_ge, Numeric),
191 (Some(f64_f64_on_stack), f64_eq, Numeric),
192 (Some(f64_f64_on_stack), f64_ne, Numeric),
193 (Some(f64_f64_on_stack), f64_lt, Numeric),
194 (Some(f64_f64_on_stack), f64_gt, Numeric),
195 (Some(f64_f64_on_stack), f64_le, Numeric),
196 (Some(f64_f64_on_stack), f64_ge, Numeric),
197 (Some(i32_on_stack), i32_clz, NumericInt),
198 (Some(i32_on_stack), i32_ctz, NumericInt),
199 (Some(i32_on_stack), i32_popcnt, NumericInt),
200 (Some(i32_i32_on_stack), i32_add, NumericInt),
201 (Some(i32_i32_on_stack), i32_sub, NumericInt),
202 (Some(i32_i32_on_stack), i32_mul, NumericInt),
203 (Some(i32_i32_on_stack), i32_div_s, NumericInt),
204 (Some(i32_i32_on_stack), i32_div_u, NumericInt),
205 (Some(i32_i32_on_stack), i32_rem_s, NumericInt),
206 (Some(i32_i32_on_stack), i32_rem_u, NumericInt),
207 (Some(i32_i32_on_stack), i32_and, NumericInt),
208 (Some(i32_i32_on_stack), i32_or, NumericInt),
209 (Some(i32_i32_on_stack), i32_xor, NumericInt),
210 (Some(i32_i32_on_stack), i32_shl, NumericInt),
211 (Some(i32_i32_on_stack), i32_shr_s, NumericInt),
212 (Some(i32_i32_on_stack), i32_shr_u, NumericInt),
213 (Some(i32_i32_on_stack), i32_rotl, NumericInt),
214 (Some(i32_i32_on_stack), i32_rotr, NumericInt),
215 (Some(i64_on_stack), i64_clz, NumericInt),
216 (Some(i64_on_stack), i64_ctz, NumericInt),
217 (Some(i64_on_stack), i64_popcnt, NumericInt),
218 (Some(i64_i64_on_stack), i64_add, NumericInt),
219 (Some(i64_i64_on_stack), i64_sub, NumericInt),
220 (Some(i64_i64_on_stack), i64_mul, NumericInt),
221 (Some(i64_i64_on_stack), i64_div_s, NumericInt),
222 (Some(i64_i64_on_stack), i64_div_u, NumericInt),
223 (Some(i64_i64_on_stack), i64_rem_s, NumericInt),
224 (Some(i64_i64_on_stack), i64_rem_u, NumericInt),
225 (Some(i64_i64_on_stack), i64_and, NumericInt),
226 (Some(i64_i64_on_stack), i64_or, NumericInt),
227 (Some(i64_i64_on_stack), i64_xor, NumericInt),
228 (Some(i64_i64_on_stack), i64_shl, NumericInt),
229 (Some(i64_i64_on_stack), i64_shr_s, NumericInt),
230 (Some(i64_i64_on_stack), i64_shr_u, NumericInt),
231 (Some(i64_i64_on_stack), i64_rotl, NumericInt),
232 (Some(i64_i64_on_stack), i64_rotr, NumericInt),
233 (Some(f32_on_stack), f32_abs, Numeric),
234 (Some(f32_on_stack), f32_neg, Numeric),
235 (Some(f32_on_stack), f32_ceil, Numeric),
236 (Some(f32_on_stack), f32_floor, Numeric),
237 (Some(f32_on_stack), f32_trunc, Numeric),
238 (Some(f32_on_stack), f32_nearest, Numeric),
239 (Some(f32_on_stack), f32_sqrt, Numeric),
240 (Some(f32_f32_on_stack), f32_add, Numeric),
241 (Some(f32_f32_on_stack), f32_sub, Numeric),
242 (Some(f32_f32_on_stack), f32_mul, Numeric),
243 (Some(f32_f32_on_stack), f32_div, Numeric),
244 (Some(f32_f32_on_stack), f32_min, Numeric),
245 (Some(f32_f32_on_stack), f32_max, Numeric),
246 (Some(f32_f32_on_stack), f32_copysign, Numeric),
247 (Some(f64_on_stack), f64_abs, Numeric),
248 (Some(f64_on_stack), f64_neg, Numeric),
249 (Some(f64_on_stack), f64_ceil, Numeric),
250 (Some(f64_on_stack), f64_floor, Numeric),
251 (Some(f64_on_stack), f64_trunc, Numeric),
252 (Some(f64_on_stack), f64_nearest, Numeric),
253 (Some(f64_on_stack), f64_sqrt, Numeric),
254 (Some(f64_f64_on_stack), f64_add, Numeric),
255 (Some(f64_f64_on_stack), f64_sub, Numeric),
256 (Some(f64_f64_on_stack), f64_mul, Numeric),
257 (Some(f64_f64_on_stack), f64_div, Numeric),
258 (Some(f64_f64_on_stack), f64_min, Numeric),
259 (Some(f64_f64_on_stack), f64_max, Numeric),
260 (Some(f64_f64_on_stack), f64_copysign, Numeric),
261 (Some(i64_on_stack), i32_wrap_i64, NumericInt),
262 (Some(f32_on_stack), i32_trunc_f32_s, Numeric),
263 (Some(f32_on_stack), i32_trunc_f32_u, Numeric),
264 (Some(f64_on_stack), i32_trunc_f64_s, Numeric),
265 (Some(f64_on_stack), i32_trunc_f64_u, Numeric),
266 (Some(i32_on_stack), i64_extend_i32_s, NumericInt),
267 (Some(i32_on_stack), i64_extend_i32_u, NumericInt),
268 (Some(f32_on_stack), i64_trunc_f32_s, Numeric),
269 (Some(f32_on_stack), i64_trunc_f32_u, Numeric),
270 (Some(f64_on_stack), i64_trunc_f64_s, Numeric),
271 (Some(f64_on_stack), i64_trunc_f64_u, Numeric),
272 (Some(i32_on_stack), f32_convert_i32_s, Numeric),
273 (Some(i32_on_stack), f32_convert_i32_u, Numeric),
274 (Some(i64_on_stack), f32_convert_i64_s, Numeric),
275 (Some(i64_on_stack), f32_convert_i64_u, Numeric),
276 (Some(f64_on_stack), f32_demote_f64, Numeric),
277 (Some(i32_on_stack), f64_convert_i32_s, Numeric),
278 (Some(i32_on_stack), f64_convert_i32_u, Numeric),
279 (Some(i64_on_stack), f64_convert_i64_s, Numeric),
280 (Some(i64_on_stack), f64_convert_i64_u, Numeric),
281 (Some(f32_on_stack), f64_promote_f32, Numeric),
282 (Some(f32_on_stack), i32_reinterpret_f32, Numeric),
283 (Some(f64_on_stack), i64_reinterpret_f64, Numeric),
284 (Some(i32_on_stack), f32_reinterpret_i32, Numeric),
285 (Some(i64_on_stack), f64_reinterpret_i64, Numeric),
286 (Some(extendable_i32_on_stack), i32_extend_8_s, NumericInt),
287 (Some(extendable_i32_on_stack), i32_extend_16_s, NumericInt),
288 (Some(extendable_i64_on_stack), i64_extend_8_s, NumericInt),
289 (Some(extendable_i64_on_stack), i64_extend_16_s, NumericInt),
290 (Some(extendable_i64_on_stack), i64_extend_32_s, NumericInt),
291 (Some(nontrapping_f32_on_stack), i32_trunc_sat_f32_s, Numeric),
292 (Some(nontrapping_f32_on_stack), i32_trunc_sat_f32_u, Numeric),
293 (Some(nontrapping_f64_on_stack), i32_trunc_sat_f64_s, Numeric),
294 (Some(nontrapping_f64_on_stack), i32_trunc_sat_f64_u, Numeric),
295 (Some(nontrapping_f32_on_stack), i64_trunc_sat_f32_s, Numeric),
296 (Some(nontrapping_f32_on_stack), i64_trunc_sat_f32_u, Numeric),
297 (Some(nontrapping_f64_on_stack), i64_trunc_sat_f64_s, Numeric),
298 (Some(nontrapping_f64_on_stack), i64_trunc_sat_f64_u, Numeric),
299 // Reference instructions.
300 (Some(ref_null_valid), ref_null, Reference),
301 (Some(ref_func_valid), ref_func, Reference),
302 (Some(ref_as_non_null_valid), ref_as_non_null, Reference),
303 (Some(ref_eq_valid), ref_eq, Reference),
304 (Some(ref_test_valid), ref_test, Reference),
305 (Some(ref_cast_valid), ref_cast, Reference),
306 (Some(ref_is_null_valid), ref_is_null, Reference),
307 (Some(table_fill_valid), table_fill, Reference),
308 (Some(table_set_valid), table_set, Reference),
309 (Some(table_get_valid), table_get, Reference),
310 (Some(table_size_valid), table_size, Reference),
311 (Some(table_grow_valid), table_grow, Reference),
312 (Some(table_copy_valid), table_copy, Reference),
313 (Some(table_init_valid), table_init, Reference),
314 (Some(elem_drop_valid), elem_drop, Reference),
315 // Aggregate instructions.
316 (Some(struct_new_valid), struct_new, Aggregate),
317 (Some(struct_new_default_valid), struct_new_default, Aggregate),
318 (Some(struct_get_valid), struct_get, Aggregate),
319 (Some(struct_set_valid), struct_set, Aggregate),
320 (Some(array_new_valid), array_new, Aggregate),
321 (Some(array_new_fixed_valid), array_new_fixed, Aggregate),
322 (Some(array_new_default_valid), array_new_default, Aggregate),
323 (Some(array_new_data_valid), array_new_data, Aggregate),
324 (Some(array_new_elem_valid), array_new_elem, Aggregate),
325 (Some(array_get_valid), array_get, Aggregate),
326 (Some(array_set_valid), array_set, Aggregate),
327 (Some(array_len_valid), array_len, Aggregate),
328 (Some(array_fill_valid), array_fill, Aggregate),
329 (Some(array_copy_valid), array_copy, Aggregate),
330 (Some(array_init_data_valid), array_init_data, Aggregate),
331 (Some(array_init_elem_valid), array_init_elem, Aggregate),
332 (Some(ref_i31_valid), ref_i31, Aggregate),
333 (Some(i31_get_valid), i31_get, Aggregate),
334 (Some(any_convert_extern_valid), any_convert_extern, Aggregate),
335 (Some(extern_convert_any_valid), extern_convert_any, Aggregate),
336 // SIMD instructions.
337 (Some(simd_have_memory_and_offset), v128_load, VectorInt),
338 (Some(simd_have_memory_and_offset), v128_load8x8s, VectorInt),
339 (Some(simd_have_memory_and_offset), v128_load8x8u, VectorInt),
340 (Some(simd_have_memory_and_offset), v128_load16x4s, VectorInt),
341 (Some(simd_have_memory_and_offset), v128_load16x4u, VectorInt),
342 (Some(simd_have_memory_and_offset), v128_load32x2s, VectorInt),
343 (Some(simd_have_memory_and_offset), v128_load32x2u, VectorInt),
344 (Some(simd_have_memory_and_offset), v128_load8_splat, VectorInt),
345 (Some(simd_have_memory_and_offset), v128_load16_splat, VectorInt),
346 (Some(simd_have_memory_and_offset), v128_load32_splat, VectorInt),
347 (Some(simd_have_memory_and_offset), v128_load64_splat, VectorInt),
348 (Some(simd_have_memory_and_offset), v128_load32_zero, VectorInt),
349 (Some(simd_have_memory_and_offset), v128_load64_zero, VectorInt),
350 (Some(simd_v128_store_valid), v128_store, VectorInt),
351 (Some(simd_load_lane_valid), v128_load8_lane, VectorInt),
352 (Some(simd_load_lane_valid), v128_load16_lane, VectorInt),
353 (Some(simd_load_lane_valid), v128_load32_lane, VectorInt),
354 (Some(simd_load_lane_valid), v128_load64_lane, VectorInt),
355 (Some(simd_store_lane_valid), v128_store8_lane, VectorInt),
356 (Some(simd_store_lane_valid), v128_store16_lane, VectorInt),
357 (Some(simd_store_lane_valid), v128_store32_lane, VectorInt),
358 (Some(simd_store_lane_valid), v128_store64_lane, VectorInt),
359 (Some(simd_enabled), v128_const, VectorInt),
360 (Some(simd_v128_v128_on_stack), i8x16_shuffle, VectorInt),
361 (Some(simd_v128_on_stack), i8x16_extract_lane_s, VectorInt),
362 (Some(simd_v128_on_stack), i8x16_extract_lane_u, VectorInt),
363 (Some(simd_v128_i32_on_stack), i8x16_replace_lane, VectorInt),
364 (Some(simd_v128_on_stack), i16x8_extract_lane_s, VectorInt),
365 (Some(simd_v128_on_stack), i16x8_extract_lane_u, VectorInt),
366 (Some(simd_v128_i32_on_stack), i16x8_replace_lane, VectorInt),
367 (Some(simd_v128_on_stack), i32x4_extract_lane, VectorInt),
368 (Some(simd_v128_i32_on_stack), i32x4_replace_lane, VectorInt),
369 (Some(simd_v128_on_stack), i64x2_extract_lane, VectorInt),
370 (Some(simd_v128_i64_on_stack), i64x2_replace_lane, VectorInt),
371 (Some(simd_v128_on_stack), f32x4_extract_lane, Vector),
372 (Some(simd_v128_f32_on_stack), f32x4_replace_lane, Vector),
373 (Some(simd_v128_on_stack), f64x2_extract_lane, Vector),
374 (Some(simd_v128_f64_on_stack), f64x2_replace_lane, Vector),
375 (Some(simd_i32_on_stack), i8x16_splat, VectorInt),
376 (Some(simd_i32_on_stack), i16x8_splat, VectorInt),
377 (Some(simd_i32_on_stack), i32x4_splat, VectorInt),
378 (Some(simd_i64_on_stack), i64x2_splat, VectorInt),
379 (Some(simd_f32_on_stack), f32x4_splat, Vector),
380 (Some(simd_f64_on_stack), f64x2_splat, Vector),
381 (Some(simd_v128_v128_on_stack), i8x16_swizzle, VectorInt),
382 (Some(simd_v128_v128_on_stack_relaxed), i8x16_relaxed_swizzle, VectorInt),
383 (Some(simd_v128_v128_v128_on_stack), v128_bitselect, VectorInt),
384 (Some(simd_v128_v128_v128_on_stack_relaxed), i8x16_relaxed_laneselect, VectorInt),
385 (Some(simd_v128_v128_v128_on_stack_relaxed), i16x8_relaxed_laneselect, VectorInt),
386 (Some(simd_v128_v128_v128_on_stack_relaxed), i32x4_relaxed_laneselect, VectorInt),
387 (Some(simd_v128_v128_v128_on_stack_relaxed), i64x2_relaxed_laneselect, VectorInt),
388 (Some(simd_v128_v128_on_stack), i8x16_eq, VectorInt),
389 (Some(simd_v128_v128_on_stack), i8x16_ne, VectorInt),
390 (Some(simd_v128_v128_on_stack), i8x16_lt_s, VectorInt),
391 (Some(simd_v128_v128_on_stack), i8x16_lt_u, VectorInt),
392 (Some(simd_v128_v128_on_stack), i8x16_gt_s, VectorInt),
393 (Some(simd_v128_v128_on_stack), i8x16_gt_u, VectorInt),
394 (Some(simd_v128_v128_on_stack), i8x16_le_s, VectorInt),
395 (Some(simd_v128_v128_on_stack), i8x16_le_u, VectorInt),
396 (Some(simd_v128_v128_on_stack), i8x16_ge_s, VectorInt),
397 (Some(simd_v128_v128_on_stack), i8x16_ge_u, VectorInt),
398 (Some(simd_v128_v128_on_stack), i16x8_eq, VectorInt),
399 (Some(simd_v128_v128_on_stack), i16x8_ne, VectorInt),
400 (Some(simd_v128_v128_on_stack), i16x8_lt_s, VectorInt),
401 (Some(simd_v128_v128_on_stack), i16x8_lt_u, VectorInt),
402 (Some(simd_v128_v128_on_stack), i16x8_gt_s, VectorInt),
403 (Some(simd_v128_v128_on_stack), i16x8_gt_u, VectorInt),
404 (Some(simd_v128_v128_on_stack), i16x8_le_s, VectorInt),
405 (Some(simd_v128_v128_on_stack), i16x8_le_u, VectorInt),
406 (Some(simd_v128_v128_on_stack), i16x8_ge_s, VectorInt),
407 (Some(simd_v128_v128_on_stack), i16x8_ge_u, VectorInt),
408 (Some(simd_v128_v128_on_stack), i32x4_eq, VectorInt),
409 (Some(simd_v128_v128_on_stack), i32x4_ne, VectorInt),
410 (Some(simd_v128_v128_on_stack), i32x4_lt_s, VectorInt),
411 (Some(simd_v128_v128_on_stack), i32x4_lt_u, VectorInt),
412 (Some(simd_v128_v128_on_stack), i32x4_gt_s, VectorInt),
413 (Some(simd_v128_v128_on_stack), i32x4_gt_u, VectorInt),
414 (Some(simd_v128_v128_on_stack), i32x4_le_s, VectorInt),
415 (Some(simd_v128_v128_on_stack), i32x4_le_u, VectorInt),
416 (Some(simd_v128_v128_on_stack), i32x4_ge_s, VectorInt),
417 (Some(simd_v128_v128_on_stack), i32x4_ge_u, VectorInt),
418 (Some(simd_v128_v128_on_stack), i64x2_eq, VectorInt),
419 (Some(simd_v128_v128_on_stack), i64x2_ne, VectorInt),
420 (Some(simd_v128_v128_on_stack), i64x2_lt_s, VectorInt),
421 (Some(simd_v128_v128_on_stack), i64x2_gt_s, VectorInt),
422 (Some(simd_v128_v128_on_stack), i64x2_le_s, VectorInt),
423 (Some(simd_v128_v128_on_stack), i64x2_ge_s, VectorInt),
424 (Some(simd_v128_v128_on_stack), f32x4_eq, Vector),
425 (Some(simd_v128_v128_on_stack), f32x4_ne, Vector),
426 (Some(simd_v128_v128_on_stack), f32x4_lt, Vector),
427 (Some(simd_v128_v128_on_stack), f32x4_gt, Vector),
428 (Some(simd_v128_v128_on_stack), f32x4_le, Vector),
429 (Some(simd_v128_v128_on_stack), f32x4_ge, Vector),
430 (Some(simd_v128_v128_on_stack), f64x2_eq, Vector),
431 (Some(simd_v128_v128_on_stack), f64x2_ne, Vector),
432 (Some(simd_v128_v128_on_stack), f64x2_lt, Vector),
433 (Some(simd_v128_v128_on_stack), f64x2_gt, Vector),
434 (Some(simd_v128_v128_on_stack), f64x2_le, Vector),
435 (Some(simd_v128_v128_on_stack), f64x2_ge, Vector),
436 (Some(simd_v128_on_stack), v128_not, VectorInt),
437 (Some(simd_v128_v128_on_stack), v128_and, VectorInt),
438 (Some(simd_v128_v128_on_stack), v128_and_not, VectorInt),
439 (Some(simd_v128_v128_on_stack), v128_or, VectorInt),
440 (Some(simd_v128_v128_on_stack), v128_xor, VectorInt),
441 (Some(simd_v128_v128_on_stack), v128_any_true, VectorInt),
442 (Some(simd_v128_on_stack), i8x16_abs, VectorInt),
443 (Some(simd_v128_on_stack), i8x16_neg, VectorInt),
444 (Some(simd_v128_on_stack), i8x16_popcnt, VectorInt),
445 (Some(simd_v128_on_stack), i8x16_all_true, VectorInt),
446 (Some(simd_v128_on_stack), i8x16_bitmask, VectorInt),
447 (Some(simd_v128_v128_on_stack), i8x16_narrow_i16x8s, VectorInt),
448 (Some(simd_v128_v128_on_stack), i8x16_narrow_i16x8u, VectorInt),
449 (Some(simd_v128_i32_on_stack), i8x16_shl, VectorInt),
450 (Some(simd_v128_i32_on_stack), i8x16_shr_s, VectorInt),
451 (Some(simd_v128_i32_on_stack), i8x16_shr_u, VectorInt),
452 (Some(simd_v128_v128_on_stack), i8x16_add, VectorInt),
453 (Some(simd_v128_v128_on_stack), i8x16_add_sat_s, VectorInt),
454 (Some(simd_v128_v128_on_stack), i8x16_add_sat_u, VectorInt),
455 (Some(simd_v128_v128_on_stack), i8x16_sub, VectorInt),
456 (Some(simd_v128_v128_on_stack), i8x16_sub_sat_s, VectorInt),
457 (Some(simd_v128_v128_on_stack), i8x16_sub_sat_u, VectorInt),
458 (Some(simd_v128_v128_on_stack), i8x16_min_s, VectorInt),
459 (Some(simd_v128_v128_on_stack), i8x16_min_u, VectorInt),
460 (Some(simd_v128_v128_on_stack), i8x16_max_s, VectorInt),
461 (Some(simd_v128_v128_on_stack), i8x16_max_u, VectorInt),
462 (Some(simd_v128_v128_on_stack), i8x16_avgr_u, VectorInt),
463 (Some(simd_v128_on_stack), i16x8_extadd_pairwise_i8x16s, VectorInt),
464 (Some(simd_v128_on_stack), i16x8_extadd_pairwise_i8x16u, VectorInt),
465 (Some(simd_v128_on_stack), i16x8_abs, VectorInt),
466 (Some(simd_v128_on_stack), i16x8_neg, VectorInt),
467 (Some(simd_v128_v128_on_stack), i16x8q15_mulr_sat_s, VectorInt),
468 (Some(simd_v128_on_stack), i16x8_all_true, VectorInt),
469 (Some(simd_v128_on_stack), i16x8_bitmask, VectorInt),
470 (Some(simd_v128_v128_on_stack), i16x8_narrow_i32x4s, VectorInt),
471 (Some(simd_v128_v128_on_stack), i16x8_narrow_i32x4u, VectorInt),
472 (Some(simd_v128_on_stack), i16x8_extend_low_i8x16s, VectorInt),
473 (Some(simd_v128_on_stack), i16x8_extend_high_i8x16s, VectorInt),
474 (Some(simd_v128_on_stack), i16x8_extend_low_i8x16u, VectorInt),
475 (Some(simd_v128_on_stack), i16x8_extend_high_i8x16u, VectorInt),
476 (Some(simd_v128_i32_on_stack), i16x8_shl, VectorInt),
477 (Some(simd_v128_i32_on_stack), i16x8_shr_s, VectorInt),
478 (Some(simd_v128_i32_on_stack), i16x8_shr_u, VectorInt),
479 (Some(simd_v128_v128_on_stack), i16x8_add, VectorInt),
480 (Some(simd_v128_v128_on_stack), i16x8_add_sat_s, VectorInt),
481 (Some(simd_v128_v128_on_stack), i16x8_add_sat_u, VectorInt),
482 (Some(simd_v128_v128_on_stack), i16x8_sub, VectorInt),
483 (Some(simd_v128_v128_on_stack), i16x8_sub_sat_s, VectorInt),
484 (Some(simd_v128_v128_on_stack), i16x8_sub_sat_u, VectorInt),
485 (Some(simd_v128_v128_on_stack), i16x8_mul, VectorInt),
486 (Some(simd_v128_v128_on_stack), i16x8_min_s, VectorInt),
487 (Some(simd_v128_v128_on_stack), i16x8_min_u, VectorInt),
488 (Some(simd_v128_v128_on_stack), i16x8_max_s, VectorInt),
489 (Some(simd_v128_v128_on_stack), i16x8_max_u, VectorInt),
490 (Some(simd_v128_v128_on_stack), i16x8_avgr_u, VectorInt),
491 (Some(simd_v128_v128_on_stack), i16x8_extmul_low_i8x16s, VectorInt),
492 (Some(simd_v128_v128_on_stack), i16x8_extmul_high_i8x16s, VectorInt),
493 (Some(simd_v128_v128_on_stack), i16x8_extmul_low_i8x16u, VectorInt),
494 (Some(simd_v128_v128_on_stack), i16x8_extmul_high_i8x16u, VectorInt),
495 (Some(simd_v128_on_stack), i32x4_extadd_pairwise_i16x8s, VectorInt),
496 (Some(simd_v128_on_stack), i32x4_extadd_pairwise_i16x8u, VectorInt),
497 (Some(simd_v128_on_stack), i32x4_abs, VectorInt),
498 (Some(simd_v128_on_stack), i32x4_neg, VectorInt),
499 (Some(simd_v128_on_stack), i32x4_all_true, VectorInt),
500 (Some(simd_v128_on_stack), i32x4_bitmask, VectorInt),
501 (Some(simd_v128_on_stack), i32x4_extend_low_i16x8s, VectorInt),
502 (Some(simd_v128_on_stack), i32x4_extend_high_i16x8s, VectorInt),
503 (Some(simd_v128_on_stack), i32x4_extend_low_i16x8u, VectorInt),
504 (Some(simd_v128_on_stack), i32x4_extend_high_i16x8u, VectorInt),
505 (Some(simd_v128_i32_on_stack), i32x4_shl, VectorInt),
506 (Some(simd_v128_i32_on_stack), i32x4_shr_s, VectorInt),
507 (Some(simd_v128_i32_on_stack), i32x4_shr_u, VectorInt),
508 (Some(simd_v128_v128_on_stack), i32x4_add, VectorInt),
509 (Some(simd_v128_v128_on_stack), i32x4_sub, VectorInt),
510 (Some(simd_v128_v128_on_stack), i32x4_mul, VectorInt),
511 (Some(simd_v128_v128_on_stack), i32x4_min_s, VectorInt),
512 (Some(simd_v128_v128_on_stack), i32x4_min_u, VectorInt),
513 (Some(simd_v128_v128_on_stack), i32x4_max_s, VectorInt),
514 (Some(simd_v128_v128_on_stack), i32x4_max_u, VectorInt),
515 (Some(simd_v128_v128_on_stack), i32x4_dot_i16x8s, VectorInt),
516 (Some(simd_v128_v128_on_stack), i32x4_extmul_low_i16x8s, VectorInt),
517 (Some(simd_v128_v128_on_stack), i32x4_extmul_high_i16x8s, VectorInt),
518 (Some(simd_v128_v128_on_stack), i32x4_extmul_low_i16x8u, VectorInt),
519 (Some(simd_v128_v128_on_stack), i32x4_extmul_high_i16x8u, VectorInt),
520 (Some(simd_v128_on_stack), i64x2_abs, VectorInt),
521 (Some(simd_v128_on_stack), i64x2_neg, VectorInt),
522 (Some(simd_v128_on_stack), i64x2_all_true, VectorInt),
523 (Some(simd_v128_on_stack), i64x2_bitmask, VectorInt),
524 (Some(simd_v128_on_stack), i64x2_extend_low_i32x4s, VectorInt),
525 (Some(simd_v128_on_stack), i64x2_extend_high_i32x4s, VectorInt),
526 (Some(simd_v128_on_stack), i64x2_extend_low_i32x4u, VectorInt),
527 (Some(simd_v128_on_stack), i64x2_extend_high_i32x4u, VectorInt),
528 (Some(simd_v128_i32_on_stack), i64x2_shl, VectorInt),
529 (Some(simd_v128_i32_on_stack), i64x2_shr_s, VectorInt),
530 (Some(simd_v128_i32_on_stack), i64x2_shr_u, VectorInt),
531 (Some(simd_v128_v128_on_stack), i64x2_add, VectorInt),
532 (Some(simd_v128_v128_on_stack), i64x2_sub, VectorInt),
533 (Some(simd_v128_v128_on_stack), i64x2_mul, VectorInt),
534 (Some(simd_v128_v128_on_stack), i64x2_extmul_low_i32x4s, VectorInt),
535 (Some(simd_v128_v128_on_stack), i64x2_extmul_high_i32x4s, VectorInt),
536 (Some(simd_v128_v128_on_stack), i64x2_extmul_low_i32x4u, VectorInt),
537 (Some(simd_v128_v128_on_stack), i64x2_extmul_high_i32x4u, VectorInt),
538 (Some(simd_v128_on_stack), f32x4_ceil, Vector),
539 (Some(simd_v128_on_stack), f32x4_floor, Vector),
540 (Some(simd_v128_on_stack), f32x4_trunc, Vector),
541 (Some(simd_v128_on_stack), f32x4_nearest, Vector),
542 (Some(simd_v128_on_stack), f32x4_abs, Vector),
543 (Some(simd_v128_on_stack), f32x4_neg, Vector),
544 (Some(simd_v128_on_stack), f32x4_sqrt, Vector),
545 (Some(simd_v128_v128_on_stack), f32x4_add, Vector),
546 (Some(simd_v128_v128_on_stack), f32x4_sub, Vector),
547 (Some(simd_v128_v128_on_stack), f32x4_mul, Vector),
548 (Some(simd_v128_v128_on_stack), f32x4_div, Vector),
549 (Some(simd_v128_v128_on_stack), f32x4_min, Vector),
550 (Some(simd_v128_v128_on_stack), f32x4_max, Vector),
551 (Some(simd_v128_v128_on_stack), f32x4p_min, Vector),
552 (Some(simd_v128_v128_on_stack), f32x4p_max, Vector),
553 (Some(simd_v128_on_stack), f64x2_ceil, Vector),
554 (Some(simd_v128_on_stack), f64x2_floor, Vector),
555 (Some(simd_v128_on_stack), f64x2_trunc, Vector),
556 (Some(simd_v128_on_stack), f64x2_nearest, Vector),
557 (Some(simd_v128_on_stack), f64x2_abs, Vector),
558 (Some(simd_v128_on_stack), f64x2_neg, Vector),
559 (Some(simd_v128_on_stack), f64x2_sqrt, Vector),
560 (Some(simd_v128_v128_on_stack), f64x2_add, Vector),
561 (Some(simd_v128_v128_on_stack), f64x2_sub, Vector),
562 (Some(simd_v128_v128_on_stack), f64x2_mul, Vector),
563 (Some(simd_v128_v128_on_stack), f64x2_div, Vector),
564 (Some(simd_v128_v128_on_stack), f64x2_min, Vector),
565 (Some(simd_v128_v128_on_stack), f64x2_max, Vector),
566 (Some(simd_v128_v128_on_stack), f64x2p_min, Vector),
567 (Some(simd_v128_v128_on_stack), f64x2p_max, Vector),
568 (Some(simd_v128_on_stack), i32x4_trunc_sat_f32x4s, Vector),
569 (Some(simd_v128_on_stack), i32x4_trunc_sat_f32x4u, Vector),
570 (Some(simd_v128_on_stack), f32x4_convert_i32x4s, Vector),
571 (Some(simd_v128_on_stack), f32x4_convert_i32x4u, Vector),
572 (Some(simd_v128_on_stack), i32x4_trunc_sat_f64x2s_zero, Vector),
573 (Some(simd_v128_on_stack), i32x4_trunc_sat_f64x2u_zero, Vector),
574 (Some(simd_v128_on_stack), f64x2_convert_low_i32x4s, Vector),
575 (Some(simd_v128_on_stack), f64x2_convert_low_i32x4u, Vector),
576 (Some(simd_v128_on_stack), f32x4_demote_f64x2_zero, Vector),
577 (Some(simd_v128_on_stack), f64x2_promote_low_f32x4, Vector),
578 (Some(simd_v128_on_stack_relaxed), i32x4_relaxed_trunc_f32x4s, Vector),
579 (Some(simd_v128_on_stack_relaxed), i32x4_relaxed_trunc_f32x4u, Vector),
580 (Some(simd_v128_on_stack_relaxed), i32x4_relaxed_trunc_f64x2s_zero, Vector),
581 (Some(simd_v128_on_stack_relaxed), i32x4_relaxed_trunc_f64x2u_zero, Vector),
582 (Some(simd_v128_v128_v128_on_stack_relaxed), f32x4_relaxed_madd, Vector),
583 (Some(simd_v128_v128_v128_on_stack_relaxed), f32x4_relaxed_nmadd, Vector),
584 (Some(simd_v128_v128_v128_on_stack_relaxed), f64x2_relaxed_madd, Vector),
585 (Some(simd_v128_v128_v128_on_stack_relaxed), f64x2_relaxed_nmadd, Vector),
586 (Some(simd_v128_v128_on_stack_relaxed), f32x4_relaxed_min, Vector),
587 (Some(simd_v128_v128_on_stack_relaxed), f32x4_relaxed_max, Vector),
588 (Some(simd_v128_v128_on_stack_relaxed), f64x2_relaxed_min, Vector),
589 (Some(simd_v128_v128_on_stack_relaxed), f64x2_relaxed_max, Vector),
590 (Some(simd_v128_v128_on_stack_relaxed), i16x8_relaxed_q15mulr_s, VectorInt),
591 (Some(simd_v128_v128_on_stack_relaxed), i16x8_relaxed_dot_i8x16_i7x16_s, VectorInt),
592 (Some(simd_v128_v128_v128_on_stack_relaxed), i32x4_relaxed_dot_i8x16_i7x16_add_s, VectorInt),
593 (Some(wide_arithmetic_binop128_on_stack), i64_add128, NumericInt),
594 (Some(wide_arithmetic_binop128_on_stack), i64_sub128, NumericInt),
595 (Some(wide_arithmetic_mul_wide_on_stack), i64_mul_wide_s, NumericInt),
596 (Some(wide_arithmetic_mul_wide_on_stack), i64_mul_wide_u, NumericInt),
597 }
598
599 pub(crate) struct CodeBuilderAllocations {
600 // The control labels in scope right now.
601 controls: Vec<Control>,
602
603 // The types on the operand stack right now.
604 operands: Vec<Option<ValType>>,
605
606 // Dynamic set of options of instruction we can generate that are known to
607 // be valid right now.
608 options: Vec<(
609 fn(&mut Unstructured, &Module, &mut CodeBuilder, &mut Vec<Instruction>) -> Result<()>,
610 u32,
611 )>,
612
613 // Cached information about the module that we're generating functions for,
614 // used to speed up validity checks. The mutable globals map is a map of the
615 // type of global to the global indices which have that type (and they're
616 // all mutable).
617 mutable_globals: BTreeMap<ValType, Vec<u32>>,
618
619 // Like mutable globals above this is a map from function types to the list
620 // of functions that have that function type.
621 functions: BTreeMap<Rc<FuncType>, Vec<u32>>,
622
623 // Like functions above this is a map from tag types to the list of tags
624 // have that tag type.
625 tags: BTreeMap<Vec<ValType>, Vec<u32>>,
626
627 // Tables in this module which have a funcref element type.
628 table32_with_funcref: Vec<u32>,
629 table64_with_funcref: Vec<u32>,
630
631 // Functions that are referenced in the module through globals and segments.
632 referenced_functions: Vec<u32>,
633
634 // Precomputed tables/element segments that can be used for `table.init`,
635 // stored as (segment, table).
636 table32_init: Vec<(u32, u32)>,
637 table64_init: Vec<(u32, u32)>,
638
639 // Precomputed valid tables to copy between, stored in (src, dst) order.
640 table_copy_32_to_32: Vec<(u32, u32)>,
641 table_copy_32_to_64: Vec<(u32, u32)>,
642 table_copy_64_to_32: Vec<(u32, u32)>,
643 table_copy_64_to_64: Vec<(u32, u32)>,
644
645 // Lists of table/memory indices which are either 32-bit or 64-bit. This is
646 // used for faster lookup in validating instructions to know which memories
647 // have which types. For example if there are no 64-bit memories then we
648 // shouldn't ever look for i64 on the stack for `i32.load`.
649 memory32: Vec<u32>,
650 memory64: Vec<u32>,
651 table32: Vec<u32>,
652 table64: Vec<u32>,
653
654 // State used when dropping operands to avoid dropping them into the ether
655 // but instead folding their final values into module state, at this time
656 // chosen to be exported globals.
657 globals_cnt: u32,
658 new_globals: Vec<(ValType, ConstExpr)>,
659 global_dropped_i32: Option<u32>,
660 global_dropped_i64: Option<u32>,
661 global_dropped_f32: Option<u32>,
662 global_dropped_f64: Option<u32>,
663 global_dropped_v128: Option<u32>,
664
665 // Indicates that additional exports cannot be generated. This will be true
666 // if the `Config` specifies exactly which exports should be present.
667 disallow_exporting: bool,
668 }
669
670 pub(crate) struct CodeBuilder<'a> {
671 #[allow(dead_code)]
672 shared: bool,
673 func_ty: &'a FuncType,
674 locals: &'a mut Vec<ValType>,
675 allocs: &'a mut CodeBuilderAllocations,
676
677 // Temporary locals injected and used by nan canonicalization. Note that
678 // this list of extra locals is appended to `self.locals` at the end of code
679 // generation, and it's kept separate here to avoid using these locals in
680 // `local.get` and similar instructions.
681 extra_locals: Vec<ValType>,
682 f32_scratch: Option<usize>,
683 f64_scratch: Option<usize>,
684 v128_scratch: Option<usize>,
685 }
686
687 /// A control frame.
688 #[derive(Debug, Clone)]
689 struct Control {
690 kind: ControlKind,
691 /// Value types that must be on the stack when entering this control frame.
692 params: Vec<ValType>,
693 /// Value types that are left on the stack when exiting this control frame.
694 results: Vec<ValType>,
695 /// How far down the operand stack instructions inside this control frame
696 /// can reach.
697 height: usize,
698 }
699
700 impl Control {
701 fn label_types(&self) -> &[ValType] {
702 if self.kind == ControlKind::Loop {
703 &self.params
704 } else {
705 &self.results
706 }
707 }
708 }
709
710 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
711 enum ControlKind {
712 Block,
713 If,
714 Loop,
715 TryTable,
716 }
717
718 enum Float {
719 F32,
720 F64,
721 F32x4,
722 F64x2,
723 }
724
725 impl CodeBuilderAllocations {
726 pub(crate) fn new(module: &Module, disallow_exporting: bool) -> Self {
727 let mut mutable_globals = BTreeMap::new();
728 for (i, global) in module.globals.iter().enumerate() {
729 if global.mutable {
730 mutable_globals
731 .entry(global.val_type)
732 .or_insert(Vec::new())
733 .push(i as u32);
734 }
735 }
736
737 let mut tags = BTreeMap::new();
738 for (idx, tag_type) in module.tags() {
739 tags.entry(tag_type.func_type.params.to_vec())
740 .or_insert(Vec::new())
741 .push(idx);
742 }
743
744 let mut functions = BTreeMap::new();
745 for (idx, func) in module.funcs() {
746 functions
747 .entry(func.clone())
748 .or_insert(Vec::new())
749 .push(idx);
750 }
751
752 let mut table32_with_funcref = Vec::new();
753 let mut table64_with_funcref = Vec::new();
754 let mut table32_tys = Vec::new();
755 let mut table64_tys = Vec::new();
756 for (i, table) in module.tables.iter().enumerate() {
757 let funcref_dst = if table.table64 {
758 table64_tys.push(table.element_type);
759 &mut table64_with_funcref
760 } else {
761 table32_tys.push(table.element_type);
762 &mut table32_with_funcref
763 };
764 if table.element_type == RefType::FUNCREF {
765 funcref_dst.push(i as u32);
766 }
767 }
768
769 let mut referenced_functions = BTreeSet::new();
770 for (_, expr) in module.defined_globals.iter() {
771 if let Some(i) = expr.get_ref_func() {
772 referenced_functions.insert(i);
773 }
774 }
775
776 let mut table32_init = Vec::new();
777 let mut table64_init = Vec::new();
778 for (i, g) in module.elems.iter().enumerate() {
779 match &g.items {
780 Elements::Expressions(e) => {
781 let iter = e.iter().filter_map(|e| e.get_ref_func());
782 referenced_functions.extend(iter);
783 }
784 Elements::Functions(e) => {
785 referenced_functions.extend(e.iter().cloned());
786 }
787 }
788 for (j, table) in module.tables.iter().enumerate() {
789 if module.ref_type_is_sub_type(g.ty, table.element_type) {
790 let dst = if table.table64 {
791 &mut table64_init
792 } else {
793 &mut table32_init
794 };
795 dst.push((i as u32, j as u32));
796 }
797 }
798 }
799
800 let mut memory32 = Vec::new();
801 let mut memory64 = Vec::new();
802 for (i, mem) in module.memories.iter().enumerate() {
803 if mem.memory64 {
804 memory64.push(i as u32);
805 } else {
806 memory32.push(i as u32);
807 }
808 }
809
810 let mut table32 = Vec::new();
811 let mut table64 = Vec::new();
812 let mut table_copy_32_to_32 = Vec::new();
813 let mut table_copy_32_to_64 = Vec::new();
814 let mut table_copy_64_to_32 = Vec::new();
815 let mut table_copy_64_to_64 = Vec::new();
816 for (i, t) in module.tables.iter().enumerate() {
817 if t.table64 {
818 table64.push(i as u32);
819 } else {
820 table32.push(i as u32);
821 }
822
823 for (j, t2) in module.tables.iter().enumerate() {
824 if module.val_type_is_sub_type(t.element_type.into(), t2.element_type.into()) {
825 let dst = match (t.table64, t2.table64) {
826 (false, false) => &mut table_copy_32_to_32,
827 (false, true) => &mut table_copy_32_to_64,
828 (true, false) => &mut table_copy_64_to_32,
829 (true, true) => &mut table_copy_64_to_64,
830 };
831 dst.push((i as u32, j as u32));
832 }
833 }
834 }
835
836 let mut global_dropped_i32 = None;
837 let mut global_dropped_i64 = None;
838 let mut global_dropped_f32 = None;
839 let mut global_dropped_f64 = None;
840 let mut global_dropped_v128 = None;
841
842 // If we can't export additional globals, try to use existing exported
843 // mutable globals for dropped values.
844 if disallow_exporting {
845 for (_, kind, index) in module.exports.iter() {
846 if *kind == ExportKind::Global {
847 let ty = module.globals[*index as usize];
848 if ty.mutable {
849 match ty.val_type {
850 ValType::I32 => {
851 if global_dropped_i32.is_none() {
852 global_dropped_i32 = Some(*index)
853 } else {
854 global_dropped_f32 = Some(*index)
855 }
856 }
857 ValType::I64 => {
858 if global_dropped_i64.is_none() {
859 global_dropped_i64 = Some(*index)
860 } else {
861 global_dropped_f64 = Some(*index)
862 }
863 }
864 ValType::V128 => global_dropped_v128 = Some(*index),
865 _ => {}
866 }
867 }
868 }
869 }
870 }
871
872 CodeBuilderAllocations {
873 controls: Vec::with_capacity(4),
874 operands: Vec::with_capacity(16),
875 options: Vec::with_capacity(NUM_OPTIONS),
876 functions,
877 tags,
878 mutable_globals,
879 table32_with_funcref,
880 table64_with_funcref,
881 referenced_functions: referenced_functions.into_iter().collect(),
882 table32_init,
883 table64_init,
884 table_copy_32_to_32,
885 table_copy_32_to_64,
886 table_copy_64_to_32,
887 table_copy_64_to_64,
888 memory32,
889 memory64,
890 table32,
891 table64,
892
893 global_dropped_i32,
894 global_dropped_i64,
895 global_dropped_f32,
896 global_dropped_f64,
897 global_dropped_v128,
898 globals_cnt: module.globals.len() as u32,
899 new_globals: Vec::new(),
900 disallow_exporting,
901 }
902 }
903
904 pub(crate) fn builder<'a>(
905 &'a mut self,
906 func_ty: &'a FuncType,
907 locals: &'a mut Vec<ValType>,
908 shared: bool,
909 ) -> CodeBuilder<'a> {
910 self.controls.clear();
911 self.controls.push(Control {
912 kind: ControlKind::Block,
913 params: vec![],
914 results: func_ty.results.to_vec(),
915 height: 0,
916 });
917
918 self.operands.clear();
919 self.options.clear();
920
921 CodeBuilder {
922 shared,
923 func_ty,
924 locals,
925 allocs: self,
926 extra_locals: Vec::new(),
927 f32_scratch: None,
928 f64_scratch: None,
929 v128_scratch: None,
930 }
931 }
932
933 pub fn finish(self, u: &mut Unstructured<'_>, module: &mut Module) -> arbitrary::Result<()> {
934 // Any globals injected as part of dropping operands on the stack get
935 // injected into the module here. Each global is then exported, most of
936 // the time (if additional exports are allowed), to ensure it's part of
937 // the "image" of this module available for differential execution for
938 // example.
939 for (ty, init) in self.new_globals {
940 let global_idx = module.globals.len() as u32;
941 module.globals.push(GlobalType {
942 val_type: ty,
943 mutable: true,
944 shared: false,
945 });
946 module.defined_globals.push((global_idx, init));
947
948 if self.disallow_exporting || u.ratio(1, 100).unwrap_or(false) {
949 continue;
950 }
951
952 let name = unique_string(1_000, &mut module.export_names, u)?;
953 module.add_arbitrary_export(name, ExportKind::Global, global_idx)?;
954 }
955 Ok(())
956 }
957 }
958
959 impl CodeBuilder<'_> {
960 fn pop_control(&mut self) -> Control {
961 let control = self.allocs.controls.pop().unwrap();
962
963 // Pop the actual types on the stack (which could be subtypes of the
964 // declared types) and then push the declared types. This avoids us
965 // accidentally generating code that relies on erased subtypes.
966 for _ in &control.results {
967 self.pop_operand();
968 }
969 for ty in &control.results {
970 self.push_operand(Some(*ty));
971 }
972
973 control
974 }
975
976 fn push_control(
977 &mut self,
978 kind: ControlKind,
979 params: impl Into<Vec<ValType>>,
980 results: impl Into<Vec<ValType>>,
981 ) {
982 let params = params.into();
983 let results = results.into();
984
985 // Similar to in `pop_control`, we want to pop the actual argument types
986 // off the stack (which could be subtypes of the declared parameter
987 // types) and then push the parameter types. This effectively does type
988 // erasure of any subtyping that exists so that we don't accidentally
989 // generate code that relies on the specific subtypes.
990 for _ in &params {
991 self.pop_operand();
992 }
993 self.push_operands(&params);
994
995 let height = self.allocs.operands.len() - params.len();
996 self.allocs.controls.push(Control {
997 kind,
998 params,
999 results,
1000 height,
1001 });
1002 }
1003
1004 /// Get the operands that are in-scope within the current control frame.
1005 #[inline]
1006 fn operands(&self) -> &[Option<ValType>] {
1007 let height = self.allocs.controls.last().map_or(0, |c| c.height);
1008 &self.allocs.operands[height..]
1009 }
1010
1011 /// Pop a single operand from the stack, regardless of expected type.
1012 #[inline]
1013 fn pop_operand(&mut self) -> Option<ValType> {
1014 self.allocs.operands.pop().unwrap()
1015 }
1016
1017 #[inline]
1018 fn pop_operands(&mut self, module: &Module, to_pop: &[ValType]) {
1019 debug_assert!(self.types_on_stack(module, to_pop));
1020 self.allocs
1021 .operands
1022 .truncate(self.allocs.operands.len() - to_pop.len());
1023 }
1024
1025 #[inline]
1026 fn push_operands(&mut self, to_push: &[ValType]) {
1027 self.allocs
1028 .operands
1029 .extend(to_push.iter().copied().map(Some));
1030 }
1031
1032 #[inline]
1033 fn push_operand(&mut self, ty: Option<ValType>) {
1034 self.allocs.operands.push(ty);
1035 }
1036
1037 fn pop_label_types(&mut self, module: &Module, target: u32) {
1038 let target = usize::try_from(target).unwrap();
1039 let control = &self.allocs.controls[self.allocs.controls.len() - 1 - target];
1040 debug_assert!(self.label_types_on_stack(module, control));
1041 self.allocs
1042 .operands
1043 .truncate(self.allocs.operands.len() - control.label_types().len());
1044 }
1045
1046 fn push_label_types(&mut self, target: u32) {
1047 let target = usize::try_from(target).unwrap();
1048 let control = &self.allocs.controls[self.allocs.controls.len() - 1 - target];
1049 self.allocs
1050 .operands
1051 .extend(control.label_types().iter().copied().map(Some));
1052 }
1053
1054 /// Pop the target label types, and then push them again.
1055 ///
1056 /// This is not a no-op due to subtyping: if we have a `T <: U` on the
1057 /// stack, and the target label's type is `[U]`, then this will erase the
1058 /// information about `T` and subsequent operations may only operate on `U`.
1059 fn pop_push_label_types(&mut self, module: &Module, target: u32) {
1060 self.pop_label_types(module, target);
1061 self.push_label_types(target)
1062 }
1063
1064 fn label_types_on_stack(&self, module: &Module, to_check: &Control) -> bool {
1065 self.types_on_stack(module, to_check.label_types())
1066 }
1067
1068 /// Is the given type on top of the stack?
1069 #[inline]
1070 fn type_on_stack(&self, module: &Module, ty: ValType) -> bool {
1071 self.type_on_stack_at(module, 0, ty)
1072 }
1073
1074 /// Is the given type on the stack at the given index (indexing from the top
1075 /// of the stack towards the bottom).
1076 #[inline]
1077 fn type_on_stack_at(&self, module: &Module, at: usize, expected: ValType) -> bool {
1078 let operands = self.operands();
1079 if at >= operands.len() {
1080 return false;
1081 }
1082 match operands[operands.len() - 1 - at] {
1083 None => true,
1084 Some(actual) => module.val_type_is_sub_type(actual, expected),
1085 }
1086 }
1087
1088 /// Are the given types on top of the stack?
1089 #[inline]
1090 fn types_on_stack(&self, module: &Module, types: &[ValType]) -> bool {
1091 self.operands().len() >= types.len()
1092 && types
1093 .iter()
1094 .rev()
1095 .enumerate()
1096 .all(|(idx, ty)| self.type_on_stack_at(module, idx, *ty))
1097 }
1098
1099 /// Are the given field types on top of the stack?
1100 #[inline]
1101 fn field_types_on_stack(&self, module: &Module, types: &[FieldType]) -> bool {
1102 self.operands().len() >= types.len()
1103 && types
1104 .iter()
1105 .rev()
1106 .enumerate()
1107 .all(|(idx, ty)| self.type_on_stack_at(module, idx, ty.element_type.unpack()))
1108 }
1109
1110 /// Is the given field type on top of the stack?
1111 #[inline]
1112 fn field_type_on_stack(&self, module: &Module, ty: FieldType) -> bool {
1113 self.type_on_stack(module, ty.element_type.unpack())
1114 }
1115
1116 /// Is the given field type on the stack at the given position (indexed from
1117 /// the top of the stack)?
1118 #[inline]
1119 fn field_type_on_stack_at(&self, module: &Module, at: usize, ty: FieldType) -> bool {
1120 self.type_on_stack_at(module, at, ty.element_type.unpack())
1121 }
1122
1123 /// Get the ref type on the top of the operand stack, if any.
1124 ///
1125 /// * `None` means no reftype on the stack.
1126 /// * `Some(None)` means that the stack is polymorphic.
1127 /// * `Some(Some(r))` means that `r` is the ref type on top of the stack.
1128 fn ref_type_on_stack(&self) -> Option<Option<RefType>> {
1129 match self.operands().last().copied()? {
1130 Some(ValType::Ref(r)) => Some(Some(r)),
1131 Some(_) => None,
1132 None => Some(None),
1133 }
1134 }
1135
1136 /// Is there a `(ref null? <index>)` on the stack at the given position? If
1137 /// so return its nullability and type index.
1138 fn concrete_ref_type_on_stack_at(&self, at: usize) -> Option<(bool, u32)> {
1139 match self.operands().iter().copied().rev().nth(at)?? {
1140 ValType::Ref(RefType {
1141 nullable,
1142 heap_type: HeapType::Concrete(ty),
1143 }) => Some((nullable, ty)),
1144 _ => None,
1145 }
1146 }
1147
1148 /// Is there a `(ref null? <index>)` at the given stack position that
1149 /// references a concrete array type?
1150 fn concrete_array_ref_type_on_stack_at(
1151 &self,
1152 module: &Module,
1153 at: usize,
1154 ) -> Option<(bool, u32, ArrayType)> {
1155 let (nullable, ty) = self.concrete_ref_type_on_stack_at(at)?;
1156 match &module.ty(ty).composite_type.inner {
1157 CompositeInnerType::Array(a) => Some((nullable, ty, *a)),
1158 _ => None,
1159 }
1160 }
1161
1162 /// Is there a `(ref null? <index>)` at the given stack position that
1163 /// references a concrete struct type?
1164 fn concrete_struct_ref_type_on_stack_at<'a>(
1165 &self,
1166 module: &'a Module,
1167 at: usize,
1168 ) -> Option<(bool, u32, &'a StructType)> {
1169 let (nullable, ty) = self.concrete_ref_type_on_stack_at(at)?;
1170 match &module.ty(ty).composite_type.inner {
1171 CompositeInnerType::Struct(s) => Some((nullable, ty, s)),
1172 _ => None,
1173 }
1174 }
1175
1176 /// Pop a reference type from the stack and return it.
1177 ///
1178 /// When in unreachable code and the stack is polymorphic, returns `None`.
1179 fn pop_ref_type(&mut self) -> Option<RefType> {
1180 let ref_ty = self.ref_type_on_stack().unwrap();
1181 self.pop_operand();
1182 ref_ty
1183 }
1184
1185 /// Pops a `(ref null? <index>)` from the stack and return its nullability
1186 /// and type index.
1187 fn pop_concrete_ref_type(&mut self) -> (bool, u32) {
1188 let ref_ty = self.pop_ref_type().unwrap();
1189 match ref_ty.heap_type {
1190 HeapType::Concrete(i) => (ref_ty.nullable, i),
1191 _ => panic!("not a concrete ref type"),
1192 }
1193 }
1194
1195 /// Get the `(ref null? <index>)` type on the top of the stack that
1196 /// references a function type, if any.
1197 fn concrete_funcref_on_stack(&self, module: &Module) -> Option<RefType> {
1198 match self.operands().last().copied()?? {
1199 ValType::Ref(r) => match r.heap_type {
1200 HeapType::Concrete(idx) => match &module.ty(idx).composite_type.inner {
1201 CompositeInnerType::Func(_) => Some(r),
1202 CompositeInnerType::Struct(_) | CompositeInnerType::Array(_) => None,
1203 },
1204 _ => None,
1205 },
1206 _ => None,
1207 }
1208 }
1209
1210 /// Is there a `(ref null? <index>)` on the top of the stack that references
1211 /// a struct type with at least one field?
1212 fn non_empty_struct_ref_on_stack(&self, module: &Module, allow_null_refs: bool) -> bool {
1213 match self.operands().last() {
1214 Some(Some(ValType::Ref(RefType {
1215 nullable,
1216 heap_type: HeapType::Concrete(idx),
1217 }))) => match &module.ty(*idx).composite_type.inner {
1218 CompositeInnerType::Struct(s) => {
1219 !s.fields.is_empty() && (!nullable || allow_null_refs)
1220 }
1221 _ => false,
1222 },
1223 _ => false,
1224 }
1225 }
1226
1227 #[inline(never)]
1228 fn arbitrary_block_type(&self, u: &mut Unstructured, module: &Module) -> Result<BlockType> {
1229 let mut options: Vec<Box<dyn Fn(&mut Unstructured) -> Result<BlockType>>> = vec![
1230 Box::new(|_| Ok(BlockType::Empty)),
1231 Box::new(|u| Ok(BlockType::Result(module.arbitrary_valtype(u)?))),
1232 ];
1233 if module.config.multi_value_enabled {
1234 for (i, ty) in module.func_types() {
1235 if self.types_on_stack(module, &ty.params) {
1236 options.push(Box::new(move |_| Ok(BlockType::FunctionType(i))));
1237 }
1238 }
1239 }
1240 let f = u.choose(&options)?;
1241 f(u)
1242 }
1243
1244 pub(crate) fn arbitrary(
1245 mut self,
1246 u: &mut Unstructured,
1247 module: &Module,
1248 ) -> Result<Vec<Instruction>> {
1249 let max_instructions = module.config.max_instructions;
1250 let allowed_instructions = if module.config.allow_floats {
1251 module.config.allowed_instructions
1252 } else {
1253 module.config.allowed_instructions.without_floats()
1254 };
1255 let mut instructions = vec![];
1256
1257 while !self.allocs.controls.is_empty() {
1258 let keep_going = instructions.len() < max_instructions && u.arbitrary::<u8>()? != 0;
1259 if !keep_going {
1260 self.end_active_control_frames(
1261 u,
1262 module,
1263 &mut instructions,
1264 module.config.disallow_traps,
1265 )?;
1266 break;
1267 }
1268
1269 match choose_instruction(u, module, allowed_instructions, &mut self) {
1270 Some(f) => {
1271 f(u, module, &mut self, &mut instructions)?;
1272 }
1273 // Choosing an instruction can fail because there is not enough
1274 // underlying data, so we really cannot generate any more
1275 // instructions. In this case we swallow that error and instead
1276 // just terminate our wasm function's frames.
1277 None => {
1278 self.end_active_control_frames(
1279 u,
1280 module,
1281 &mut instructions,
1282 module.config.disallow_traps,
1283 )?;
1284 break;
1285 }
1286 }
1287
1288 // If the configuration for this module requests nan
1289 // canonicalization then perform that here based on whether or not
1290 // the previous instruction needs canonicalization. Note that this
1291 // is based off Cranelift's pass for nan canonicalization for which
1292 // instructions to canonicalize, but the general idea is most
1293 // floating-point operations.
1294 if module.config.canonicalize_nans {
1295 match instructions.last().unwrap() {
1296 Instruction::F32Ceil
1297 | Instruction::F32Floor
1298 | Instruction::F32Nearest
1299 | Instruction::F32Sqrt
1300 | Instruction::F32Trunc
1301 | Instruction::F32Div
1302 | Instruction::F32Max
1303 | Instruction::F32Min
1304 | Instruction::F32Mul
1305 | Instruction::F32Sub
1306 | Instruction::F32Add => self.canonicalize_nan(Float::F32, &mut instructions),
1307 Instruction::F64Ceil
1308 | Instruction::F64Floor
1309 | Instruction::F64Nearest
1310 | Instruction::F64Sqrt
1311 | Instruction::F64Trunc
1312 | Instruction::F64Div
1313 | Instruction::F64Max
1314 | Instruction::F64Min
1315 | Instruction::F64Mul
1316 | Instruction::F64Sub
1317 | Instruction::F64Add => self.canonicalize_nan(Float::F64, &mut instructions),
1318 Instruction::F32x4Ceil
1319 | Instruction::F32x4Floor
1320 | Instruction::F32x4Nearest
1321 | Instruction::F32x4Sqrt
1322 | Instruction::F32x4Trunc
1323 | Instruction::F32x4Div
1324 | Instruction::F32x4Max
1325 | Instruction::F32x4Min
1326 | Instruction::F32x4Mul
1327 | Instruction::F32x4Sub
1328 | Instruction::F32x4Add => {
1329 self.canonicalize_nan(Float::F32x4, &mut instructions)
1330 }
1331 Instruction::F64x2Ceil
1332 | Instruction::F64x2Floor
1333 | Instruction::F64x2Nearest
1334 | Instruction::F64x2Sqrt
1335 | Instruction::F64x2Trunc
1336 | Instruction::F64x2Div
1337 | Instruction::F64x2Max
1338 | Instruction::F64x2Min
1339 | Instruction::F64x2Mul
1340 | Instruction::F64x2Sub
1341 | Instruction::F64x2Add => {
1342 self.canonicalize_nan(Float::F64x2, &mut instructions)
1343 }
1344 _ => {}
1345 }
1346 }
1347 }
1348
1349 self.locals.extend(self.extra_locals.drain(..));
1350
1351 Ok(instructions)
1352 }
1353
1354 fn canonicalize_nan(&mut self, ty: Float, ins: &mut Vec<Instruction>) {
1355 // We'll need to temporarily save the top of the stack into a local, so
1356 // figure out that local here. Note that this tries to use the same
1357 // local if canonicalization happens more than once in a function.
1358 let (local, val_ty) = match ty {
1359 Float::F32 => (&mut self.f32_scratch, ValType::F32),
1360 Float::F64 => (&mut self.f64_scratch, ValType::F64),
1361 Float::F32x4 | Float::F64x2 => (&mut self.v128_scratch, ValType::V128),
1362 };
1363 let local = match *local {
1364 Some(i) => i as u32,
1365 None => self.alloc_local(val_ty),
1366 };
1367
1368 // Save the previous instruction's result into a local. This also leaves
1369 // a value on the stack as `val1` for the `select` instruction.
1370 ins.push(Instruction::LocalTee(local));
1371
1372 // The `val2` value input to the `select` below, our nan pattern.
1373 //
1374 // The nan patterns here are chosen to be a canonical representation
1375 // which is still NaN but the wasm will always produce the same bits of
1376 // a nan so if the wasm takes a look at the nan inside it'll always see
1377 // the same representation.
1378 const CANON_32BIT_NAN: u32 = 0b01111111110000000000000000000000;
1379 const CANON_64BIT_NAN: u64 =
1380 0b0111111111111000000000000000000000000000000000000000000000000000;
1381 ins.push(match ty {
1382 Float::F32 => Instruction::F32Const(f32::from_bits(CANON_32BIT_NAN).into()),
1383 Float::F64 => Instruction::F64Const(f64::from_bits(CANON_64BIT_NAN).into()),
1384 Float::F32x4 => {
1385 let nan = CANON_32BIT_NAN as i128;
1386 let nan = nan | (nan << 32) | (nan << 64) | (nan << 96);
1387 Instruction::V128Const(nan)
1388 }
1389 Float::F64x2 => {
1390 let nan = CANON_64BIT_NAN as i128;
1391 let nan = nan | (nan << 64);
1392 Instruction::V128Const(nan)
1393 }
1394 });
1395
1396 // the condition of the `select`, which is the float's equality test
1397 // with itself.
1398 ins.push(Instruction::LocalGet(local));
1399 ins.push(Instruction::LocalGet(local));
1400 ins.push(match ty {
1401 Float::F32 => Instruction::F32Eq,
1402 Float::F64 => Instruction::F64Eq,
1403 Float::F32x4 => Instruction::F32x4Eq,
1404 Float::F64x2 => Instruction::F64x2Eq,
1405 });
1406
1407 // Select the result. If the condition is nonzero (aka the float is
1408 // equal to itself) it picks `val1`, otherwise if zero (aka the float
1409 // is nan) it picks `val2`.
1410 ins.push(match ty {
1411 Float::F32 | Float::F64 => Instruction::Select,
1412 Float::F32x4 | Float::F64x2 => Instruction::V128Bitselect,
1413 });
1414 }
1415
1416 fn alloc_local(&mut self, ty: ValType) -> u32 {
1417 let val = self.locals.len() + self.func_ty.params.len() + self.extra_locals.len();
1418 self.extra_locals.push(ty);
1419 u32::try_from(val).unwrap()
1420 }
1421
1422 fn end_active_control_frames(
1423 &mut self,
1424 u: &mut Unstructured<'_>,
1425 module: &Module,
1426 instructions: &mut Vec<Instruction>,
1427 disallow_traps: bool,
1428 ) -> Result<()> {
1429 while !self.allocs.controls.is_empty() {
1430 // Ensure that this label is valid by placing the right types onto
1431 // the operand stack for the end of the label.
1432 self.guarantee_label_results(u, module, instructions, disallow_traps)?;
1433
1434 // Remove the label and clear the operand stack since the label has
1435 // been removed.
1436 let label = self.allocs.controls.pop().unwrap();
1437 self.allocs.operands.truncate(label.height);
1438
1439 // If this is an `if` that is not stack neutral, then it
1440 // must have an `else`. Generate synthetic results here in the same
1441 // manner we did above.
1442 if label.kind == ControlKind::If && label.params != label.results {
1443 instructions.push(Instruction::Else);
1444 self.allocs.controls.push(label.clone());
1445 self.allocs
1446 .operands
1447 .extend(label.params.into_iter().map(Some));
1448 self.guarantee_label_results(u, module, instructions, disallow_traps)?;
1449 self.allocs.controls.pop();
1450 self.allocs.operands.truncate(label.height);
1451 }
1452
1453 // The last control frame for the function return does not
1454 // need an `end` instruction.
1455 if !self.allocs.controls.is_empty() {
1456 instructions.push(Instruction::End);
1457 }
1458
1459 // Place the results of the label onto the operand stack for use
1460 // after the label.
1461 self.allocs
1462 .operands
1463 .extend(label.results.into_iter().map(Some));
1464 }
1465 Ok(())
1466 }
1467
1468 /// Modifies the instruction stream to guarantee that the current control
1469 /// label's results are on the stack and ready for the control label to return.
1470 fn guarantee_label_results(
1471 &mut self,
1472 u: &mut Unstructured<'_>,
1473 module: &Module,
1474 instructions: &mut Vec<Instruction>,
1475 disallow_traps: bool,
1476 ) -> Result<()> {
1477 let operands = self.operands();
1478 let label = self.allocs.controls.last().unwrap();
1479
1480 // Already done, yay!
1481 if label.results.len() == operands.len() && self.types_on_stack(module, &label.results) {
1482 return Ok(());
1483 }
1484
1485 // Generating an unreachable instruction is always a valid way to
1486 // generate any types for a label, but it's not too interesting, so
1487 // don't favor it.
1488 if !disallow_traps && u.ratio(1, u16::MAX)? {
1489 instructions.push(Instruction::Unreachable);
1490 return Ok(());
1491 }
1492
1493 // Arbitrarily massage the stack to get the expected results. First we
1494 // drop all extraneous results to we're only dealing with those we want
1495 // to deal with. Afterwards we start at the bottom of the stack and move
1496 // up, figuring out what matches and what doesn't. As soon as something
1497 // doesn't match we throw out that and everything else remaining,
1498 // filling in results with dummy values.
1499 let operands = operands.to_vec();
1500 let mut operands = operands.as_slice();
1501 let label_results = label.results.to_vec();
1502 while operands.len() > label_results.len() {
1503 self.drop_operand(u, *operands.last().unwrap(), instructions)?;
1504 operands = &operands[..operands.len() - 1];
1505 }
1506 for (i, expected) in label_results.iter().enumerate() {
1507 if let Some(actual) = operands.get(i) {
1508 if Some(*expected) == *actual {
1509 continue;
1510 }
1511 for ty in operands[i..].iter().rev() {
1512 self.drop_operand(u, *ty, instructions)?;
1513 }
1514 operands = &[];
1515 }
1516 instructions.push(module.arbitrary_const_instruction(*expected, u)?);
1517 }
1518 Ok(())
1519 }
1520
1521 fn drop_operand(
1522 &mut self,
1523 u: &mut Unstructured<'_>,
1524 ty: Option<ValType>,
1525 instructions: &mut Vec<Instruction>,
1526 ) -> Result<()> {
1527 if !self.mix_operand_into_global(u, ty, instructions)? {
1528 instructions.push(Instruction::Drop);
1529 }
1530 Ok(())
1531 }
1532
1533 /// Attempts to drop the top operand on the stack by "mixing" it into a
1534 /// global.
1535 ///
1536 /// This is done to avoid dropping values on the floor to ensure that
1537 /// everything is part of some computation somewhere. Otherwise, for
1538 /// example, most function results are dropped on the floor as the stack
1539 /// won't happen to match the function type that we're generating.
1540 ///
1541 /// This will return `true` if the operand has been dropped, and `false` if
1542 /// it didn't for one reason or another.
1543 fn mix_operand_into_global(
1544 &mut self,
1545 u: &mut Unstructured<'_>,
1546 ty: Option<ValType>,
1547 instructions: &mut Vec<Instruction>,
1548 ) -> Result<bool> {
1549 // If the type of this operand isn't known, for example if it's relevant
1550 // to unreachable code, then it can't be combined, so return `false`.
1551 let ty = match ty {
1552 Some(ty) => ty,
1553 None => return Ok(false),
1554 };
1555
1556 // Use the input stream to allow a small chance of dropping the value
1557 // without combining it.
1558 if u.ratio(1, 100)? {
1559 return Ok(false);
1560 }
1561
1562 // Depending on the type lookup or inject a global to place this value
1563 // into.
1564 let (global, combine) = match ty {
1565 ValType::I32 => {
1566 let global = *self.allocs.global_dropped_i32.get_or_insert_with(|| {
1567 self.allocs.new_globals.push((ty, ConstExpr::i32_const(0)));
1568 inc(&mut self.allocs.globals_cnt)
1569 });
1570 (global, Instruction::I32Xor)
1571 }
1572 ValType::I64 => {
1573 let global = *self.allocs.global_dropped_i64.get_or_insert_with(|| {
1574 self.allocs.new_globals.push((ty, ConstExpr::i64_const(0)));
1575 inc(&mut self.allocs.globals_cnt)
1576 });
1577 (global, Instruction::I64Xor)
1578 }
1579 ValType::F32 => {
1580 let global = *self.allocs.global_dropped_f32.get_or_insert_with(|| {
1581 self.allocs
1582 .new_globals
1583 .push((ValType::I32, ConstExpr::i32_const(0)));
1584 inc(&mut self.allocs.globals_cnt)
1585 });
1586 instructions.push(Instruction::I32ReinterpretF32);
1587 (global, Instruction::I32Xor)
1588 }
1589 ValType::F64 => {
1590 let global = *self.allocs.global_dropped_f64.get_or_insert_with(|| {
1591 self.allocs
1592 .new_globals
1593 .push((ValType::I64, ConstExpr::i64_const(0)));
1594 inc(&mut self.allocs.globals_cnt)
1595 });
1596 instructions.push(Instruction::I64ReinterpretF64);
1597 (global, Instruction::I64Xor)
1598 }
1599 ValType::V128 => {
1600 let global = *self.allocs.global_dropped_v128.get_or_insert_with(|| {
1601 self.allocs.new_globals.push((ty, ConstExpr::v128_const(0)));
1602 inc(&mut self.allocs.globals_cnt)
1603 });
1604 (global, Instruction::V128Xor)
1605 }
1606
1607 // Don't know how to combine reference types at this time, so just
1608 // let it get dropped.
1609 ValType::Ref(_) => return Ok(false),
1610 };
1611 instructions.push(Instruction::GlobalGet(global));
1612 instructions.push(combine);
1613 instructions.push(Instruction::GlobalSet(global));
1614
1615 return Ok(true);
1616
1617 fn inc(val: &mut u32) -> u32 {
1618 let ret = *val;
1619 *val += 1;
1620 ret
1621 }
1622 }
1623 }
1624
1625 #[inline]
1626 fn unreachable_valid(module: &Module, _: &mut CodeBuilder) -> bool {
1627 !module.config.disallow_traps
1628 }
1629
1630 fn unreachable(
1631 _: &mut Unstructured,
1632 _: &Module,
1633 _: &mut CodeBuilder,
1634 instructions: &mut Vec<Instruction>,
1635 ) -> Result<()> {
1636 instructions.push(Instruction::Unreachable);
1637 Ok(())
1638 }
1639
1640 fn nop(
1641 _: &mut Unstructured,
1642 _: &Module,
1643 _: &mut CodeBuilder,
1644 instructions: &mut Vec<Instruction>,
1645 ) -> Result<()> {
1646 instructions.push(Instruction::Nop);
1647 Ok(())
1648 }
1649
1650 fn block(
1651 u: &mut Unstructured,
1652 module: &Module,
1653 builder: &mut CodeBuilder,
1654 instructions: &mut Vec<Instruction>,
1655 ) -> Result<()> {
1656 let block_ty = builder.arbitrary_block_type(u, module)?;
1657 let (params, results) = module.params_results(&block_ty);
1658 builder.push_control(ControlKind::Block, params, results);
1659 instructions.push(Instruction::Block(block_ty));
1660 Ok(())
1661 }
1662
1663 #[inline]
1664 fn try_table_valid(module: &Module, _: &mut CodeBuilder) -> bool {
1665 module.config.exceptions_enabled
1666 }
1667
1668 fn try_table(
1669 u: &mut Unstructured,
1670 module: &Module,
1671 builder: &mut CodeBuilder,
1672 instructions: &mut Vec<Instruction>,
1673 ) -> Result<()> {
1674 let block_ty = builder.arbitrary_block_type(u, module)?;
1675
1676 let mut catch_options: Vec<
1677 Box<dyn Fn(&mut Unstructured<'_>, &mut CodeBuilder<'_>) -> Result<Catch>>,
1678 > = Vec::new();
1679
1680 for (i, ctrl) in builder.allocs.controls.iter().rev().enumerate() {
1681 let i = i as u32;
1682
1683 let label_types = ctrl.label_types();
1684
1685 // Empty labels are candidates for a `catch_all` since nothing is
1686 // pushed in that case.
1687 if label_types.is_empty() {
1688 catch_options.push(Box::new(move |_, _| Ok(Catch::All { label: i })));
1689 }
1690
1691 // Labels with just an `externref` are suitable for `catch_all_refs`,
1692 // which first pushes nothing since there's no tag and then pushes
1693 // the caught exception value.
1694 if label_types == [ValType::EXNREF] {
1695 catch_options.push(Box::new(move |_, _| Ok(Catch::AllRef { label: i })));
1696 }
1697
1698 // If there is a tag which exactly matches the types of the label we're
1699 // looking at then that tag can be used as part of a `catch` branch.
1700 // That tag's parameters, which are the except values, are pushed
1701 // for the label.
1702 if builder.allocs.tags.contains_key(label_types) {
1703 let label_types = label_types.to_vec();
1704 catch_options.push(Box::new(move |u, builder| {
1705 Ok(Catch::One {
1706 tag: *u.choose(&builder.allocs.tags[&label_types])?,
1707 label: i,
1708 })
1709 }));
1710 }
1711
1712 // And finally the last type of catch label, `catch_ref`. If the label
1713 // ends with `exnref`, then use everything except the last `exnref` to
1714 // see if there's a matching tag. If so then `catch_ref` can be used
1715 // with that tag when branching to this label.
1716 if let Some((&ValType::EXNREF, rest)) = label_types.split_last() {
1717 if builder.allocs.tags.contains_key(rest) {
1718 let rest = rest.to_vec();
1719 catch_options.push(Box::new(move |u, builder| {
1720 Ok(Catch::OneRef {
1721 tag: *u.choose(&builder.allocs.tags[&rest])?,
1722 label: i,
1723 })
1724 }));
1725 }
1726 }
1727 }
1728
1729 let mut catches = Vec::new();
1730 if catch_options.len() > 0 {
1731 for _ in 0..u.int_in_range(0..=10)? {
1732 catches.push(u.choose(&catch_options)?(u, builder)?);
1733 }
1734 }
1735
1736 let (params, results) = module.params_results(&block_ty);
1737 builder.push_control(ControlKind::TryTable, params, results);
1738
1739 instructions.push(Instruction::TryTable(block_ty, catches.into()));
1740 Ok(())
1741 }
1742
1743 fn r#loop(
1744 u: &mut Unstructured,
1745 module: &Module,
1746 builder: &mut CodeBuilder,
1747 instructions: &mut Vec<Instruction>,
1748 ) -> Result<()> {
1749 let block_ty = builder.arbitrary_block_type(u, module)?;
1750 let (params, results) = module.params_results(&block_ty);
1751 builder.push_control(ControlKind::Loop, params, results);
1752 instructions.push(Instruction::Loop(block_ty));
1753 Ok(())
1754 }
1755
1756 #[inline]
1757 fn if_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1758 builder.type_on_stack(module, ValType::I32)
1759 }
1760
1761 fn r#if(
1762 u: &mut Unstructured,
1763 module: &Module,
1764 builder: &mut CodeBuilder,
1765 instructions: &mut Vec<Instruction>,
1766 ) -> Result<()> {
1767 builder.pop_operands(module, &[ValType::I32]);
1768 let block_ty = builder.arbitrary_block_type(u, module)?;
1769 let (params, results) = module.params_results(&block_ty);
1770 builder.push_control(ControlKind::If, params, results);
1771 instructions.push(Instruction::If(block_ty));
1772 Ok(())
1773 }
1774
1775 #[inline]
1776 fn else_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1777 let last_control = builder.allocs.controls.last().unwrap();
1778 last_control.kind == ControlKind::If
1779 && builder.operands().len() == last_control.results.len()
1780 && builder.types_on_stack(module, &last_control.results)
1781 }
1782
1783 fn r#else(
1784 _: &mut Unstructured,
1785 module: &Module,
1786 builder: &mut CodeBuilder,
1787 instructions: &mut Vec<Instruction>,
1788 ) -> Result<()> {
1789 let control = builder.pop_control();
1790 builder.pop_operands(module, &control.results);
1791 builder.push_operands(&control.params);
1792 builder.push_control(ControlKind::Block, control.params, control.results);
1793 instructions.push(Instruction::Else);
1794 Ok(())
1795 }
1796
1797 #[inline]
1798 fn end_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1799 // Note: first control frame is the function return's control frame, which
1800 // does not have an associated `end`.
1801 if builder.allocs.controls.len() <= 1 {
1802 return false;
1803 }
1804 let control = builder.allocs.controls.last().unwrap();
1805 builder.operands().len() == control.results.len()
1806 && builder.types_on_stack(module, &control.results)
1807 // `if`s that don't leave the stack as they found it must have an
1808 // `else`.
1809 && !(control.kind == ControlKind::If && control.params != control.results)
1810 }
1811
1812 fn end(
1813 _: &mut Unstructured,
1814 _: &Module,
1815 builder: &mut CodeBuilder,
1816 instructions: &mut Vec<Instruction>,
1817 ) -> Result<()> {
1818 builder.pop_control();
1819 instructions.push(Instruction::End);
1820 Ok(())
1821 }
1822
1823 #[inline]
1824 fn br_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1825 builder
1826 .allocs
1827 .controls
1828 .iter()
1829 .any(|l| builder.label_types_on_stack(module, l))
1830 }
1831
1832 fn br(
1833 u: &mut Unstructured,
1834 module: &Module,
1835 builder: &mut CodeBuilder,
1836 instructions: &mut Vec<Instruction>,
1837 ) -> Result<()> {
1838 let n = builder
1839 .allocs
1840 .controls
1841 .iter()
1842 .filter(|l| builder.label_types_on_stack(module, l))
1843 .count();
1844 debug_assert!(n > 0);
1845 let i = u.int_in_range(0..=n - 1)?;
1846 let (target, _) = builder
1847 .allocs
1848 .controls
1849 .iter()
1850 .rev()
1851 .enumerate()
1852 .filter(|(_, l)| builder.label_types_on_stack(module, l))
1853 .nth(i)
1854 .unwrap();
1855 let target = u32::try_from(target).unwrap();
1856 builder.pop_label_types(module, target);
1857 instructions.push(Instruction::Br(target));
1858 Ok(())
1859 }
1860
1861 #[inline]
1862 fn br_if_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1863 if !builder.type_on_stack(module, ValType::I32) {
1864 return false;
1865 }
1866 let ty = builder.allocs.operands.pop().unwrap();
1867 let is_valid = builder
1868 .allocs
1869 .controls
1870 .iter()
1871 .any(|l| builder.label_types_on_stack(module, l));
1872 builder.allocs.operands.push(ty);
1873 is_valid
1874 }
1875
1876 fn br_if(
1877 u: &mut Unstructured,
1878 module: &Module,
1879 builder: &mut CodeBuilder,
1880 instructions: &mut Vec<Instruction>,
1881 ) -> Result<()> {
1882 builder.pop_operands(module, &[ValType::I32]);
1883
1884 let n = builder
1885 .allocs
1886 .controls
1887 .iter()
1888 .filter(|l| builder.label_types_on_stack(module, l))
1889 .count();
1890 debug_assert!(n > 0);
1891 let i = u.int_in_range(0..=n - 1)?;
1892 let (target, _) = builder
1893 .allocs
1894 .controls
1895 .iter()
1896 .rev()
1897 .enumerate()
1898 .filter(|(_, l)| builder.label_types_on_stack(module, l))
1899 .nth(i)
1900 .unwrap();
1901 let target = u32::try_from(target).unwrap();
1902 builder.pop_push_label_types(module, target);
1903 instructions.push(Instruction::BrIf(target));
1904 Ok(())
1905 }
1906
1907 #[inline]
1908 fn br_table_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1909 if !builder.type_on_stack(module, ValType::I32) {
1910 return false;
1911 }
1912 let ty = builder.allocs.operands.pop().unwrap();
1913 let is_valid = br_valid(module, builder);
1914 builder.allocs.operands.push(ty);
1915 is_valid
1916 }
1917
1918 fn br_table(
1919 u: &mut Unstructured,
1920 module: &Module,
1921 builder: &mut CodeBuilder,
1922 instructions: &mut Vec<Instruction>,
1923 ) -> Result<()> {
1924 builder.pop_operands(module, &[ValType::I32]);
1925
1926 let n = builder
1927 .allocs
1928 .controls
1929 .iter()
1930 .filter(|l| builder.label_types_on_stack(module, l))
1931 .count();
1932 debug_assert!(n > 0);
1933
1934 let i = u.int_in_range(0..=n - 1)?;
1935 let (default_target, _) = builder
1936 .allocs
1937 .controls
1938 .iter()
1939 .rev()
1940 .enumerate()
1941 .filter(|(_, l)| builder.label_types_on_stack(module, l))
1942 .nth(i)
1943 .unwrap();
1944 let control = &builder.allocs.controls[builder.allocs.controls.len() - 1 - default_target];
1945
1946 let targets = builder
1947 .allocs
1948 .controls
1949 .iter()
1950 .rev()
1951 .enumerate()
1952 .filter(|(_, l)| l.label_types() == control.label_types())
1953 .map(|(t, _)| t as u32)
1954 .collect();
1955
1956 let tys = control.label_types().to_vec();
1957 builder.pop_operands(module, &tys);
1958
1959 instructions.push(Instruction::BrTable(targets, default_target as u32));
1960 Ok(())
1961 }
1962
1963 #[inline]
1964 fn return_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1965 builder.label_types_on_stack(module, &builder.allocs.controls[0])
1966 }
1967
1968 fn r#return(
1969 _: &mut Unstructured,
1970 module: &Module,
1971 builder: &mut CodeBuilder,
1972 instructions: &mut Vec<Instruction>,
1973 ) -> Result<()> {
1974 let results = builder.allocs.controls[0].results.clone();
1975 builder.pop_operands(module, &results);
1976 instructions.push(Instruction::Return);
1977 Ok(())
1978 }
1979
1980 #[inline]
1981 fn call_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
1982 builder
1983 .allocs
1984 .functions
1985 .keys()
1986 .any(|func_ty| builder.types_on_stack(module, &func_ty.params))
1987 }
1988
1989 fn call(
1990 u: &mut Unstructured,
1991 module: &Module,
1992 builder: &mut CodeBuilder,
1993 instructions: &mut Vec<Instruction>,
1994 ) -> Result<()> {
1995 let candidates = builder
1996 .allocs
1997 .functions
1998 .iter()
1999 .filter(|(func_ty, _)| builder.types_on_stack(module, &func_ty.params))
2000 .flat_map(|(_, v)| v.iter().copied())
2001 .collect::<Vec<_>>();
2002 assert!(candidates.len() > 0);
2003 let i = u.int_in_range(0..=candidates.len() - 1)?;
2004 let (func_idx, ty) = module.funcs().nth(candidates[i] as usize).unwrap();
2005 builder.pop_operands(module, &ty.params);
2006 builder.push_operands(&ty.results);
2007 instructions.push(Instruction::Call(func_idx));
2008 Ok(())
2009 }
2010
2011 #[inline]
2012 fn call_ref_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2013 if !module.config.gc_enabled {
2014 return false;
2015 }
2016 let funcref = match builder.concrete_funcref_on_stack(module) {
2017 Some(f) => f,
2018 None => return false,
2019 };
2020 if module.config.disallow_traps && funcref.nullable {
2021 return false;
2022 }
2023 match funcref.heap_type {
2024 HeapType::Concrete(idx) => {
2025 let ty = builder.allocs.operands.pop().unwrap();
2026 let params = &module.ty(idx).unwrap_func().params;
2027 let valid = builder.types_on_stack(module, params);
2028 builder.allocs.operands.push(ty);
2029 valid
2030 }
2031 _ => unreachable!(),
2032 }
2033 }
2034
2035 fn call_ref(
2036 _u: &mut Unstructured,
2037 module: &Module,
2038 builder: &mut CodeBuilder,
2039 instructions: &mut Vec<Instruction>,
2040 ) -> Result<()> {
2041 let heap_ty = match builder.pop_operand() {
2042 Some(ValType::Ref(r)) => r.heap_type,
2043 _ => unreachable!(),
2044 };
2045 let idx = match heap_ty {
2046 HeapType::Concrete(idx) => idx,
2047 _ => unreachable!(),
2048 };
2049 let func_ty = match &module.ty(idx).composite_type.inner {
2050 CompositeInnerType::Func(f) => f,
2051 _ => unreachable!(),
2052 };
2053 builder.pop_operands(module, &func_ty.params);
2054 builder.push_operands(&func_ty.results);
2055 instructions.push(Instruction::CallRef(idx));
2056 Ok(())
2057 }
2058
2059 #[inline]
2060 fn call_indirect_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2061 call_indirect_valid_impl(module, builder, false)
2062 }
2063
2064 fn call_indirect_valid_impl(
2065 module: &Module,
2066 builder: &mut CodeBuilder,
2067 is_return_call: bool,
2068 ) -> bool {
2069 if module.config.disallow_traps {
2070 // We have no way to reflect, at run time, on a `funcref` in
2071 // the `i`th slot in a table and dynamically avoid trapping
2072 // `call_indirect`s. Therefore, we can't emit *any*
2073 // `call_indirect` instructions if we want to avoid traps.
2074 return false;
2075 }
2076 let can_call32 = builder.type_on_stack(module, ValType::I32)
2077 && builder.allocs.table32_with_funcref.len() > 0;
2078 let can_call64 = builder.type_on_stack(module, ValType::I64)
2079 && builder.allocs.table64_with_funcref.len() > 0;
2080 if !can_call32 && !can_call64 {
2081 return false;
2082 }
2083 let ty = builder.allocs.operands.pop().unwrap();
2084 let is_valid = module.func_types().any(|(_, ty)| {
2085 builder.types_on_stack(module, &ty.params)
2086 && (!is_return_call || builder.allocs.controls[0].label_types() == &ty.results)
2087 });
2088 builder.allocs.operands.push(ty);
2089 is_valid
2090 }
2091
2092 fn call_indirect(
2093 u: &mut Unstructured,
2094 module: &Module,
2095 builder: &mut CodeBuilder,
2096 instructions: &mut Vec<Instruction>,
2097 ) -> Result<()> {
2098 let table = select_call_indirect_table(u, module, builder)?;
2099
2100 let choices = module
2101 .func_types()
2102 .filter(|(_, ty)| builder.types_on_stack(module, &ty.params))
2103 .collect::<Vec<_>>();
2104 let (type_idx, ty) = u.choose(&choices)?;
2105 builder.pop_operands(module, &ty.params);
2106 builder.push_operands(&ty.results);
2107 instructions.push(Instruction::CallIndirect {
2108 type_index: { *type_idx },
2109 table_index: table,
2110 });
2111 Ok(())
2112 }
2113
2114 fn select_call_indirect_table(
2115 u: &mut Unstructured,
2116 module: &Module,
2117 builder: &mut CodeBuilder,
2118 ) -> Result<u32> {
2119 let tables = if builder.type_on_stack(module, ValType::I32) {
2120 builder.pop_operands(module, &[ValType::I32]);
2121 &builder.allocs.table32_with_funcref
2122 } else {
2123 builder.pop_operands(module, &[ValType::I64]);
2124 &builder.allocs.table64_with_funcref
2125 };
2126 Ok(*u.choose(tables)?)
2127 }
2128
2129 #[inline]
2130 fn return_call_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2131 if !module.config.tail_call_enabled {
2132 return false;
2133 }
2134
2135 builder.allocs.functions.keys().any(|func_ty| {
2136 builder.types_on_stack(module, &func_ty.params)
2137 && builder.allocs.controls[0].label_types() == &func_ty.results
2138 })
2139 }
2140
2141 fn return_call(
2142 u: &mut Unstructured,
2143 module: &Module,
2144 builder: &mut CodeBuilder,
2145 instructions: &mut Vec<Instruction>,
2146 ) -> Result<()> {
2147 let candidates = builder
2148 .allocs
2149 .functions
2150 .iter()
2151 .filter(|(func_ty, _)| {
2152 builder.types_on_stack(module, &func_ty.params)
2153 && builder.allocs.controls[0].label_types() == &func_ty.results
2154 })
2155 .flat_map(|(_, v)| v.iter().copied())
2156 .collect::<Vec<_>>();
2157 assert!(candidates.len() > 0);
2158 let i = u.int_in_range(0..=candidates.len() - 1)?;
2159 let (func_idx, ty) = module.funcs().nth(candidates[i] as usize).unwrap();
2160 builder.pop_operands(module, &ty.params);
2161 builder.push_operands(&ty.results);
2162 instructions.push(Instruction::ReturnCall(func_idx));
2163 Ok(())
2164 }
2165
2166 #[inline]
2167 fn return_call_ref_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2168 if !module.config.gc_enabled {
2169 return false;
2170 }
2171
2172 let ref_ty = match builder.concrete_funcref_on_stack(module) {
2173 None => return false,
2174 Some(r) if r.nullable && module.config.disallow_traps => return false,
2175 Some(r) => r,
2176 };
2177
2178 let idx = match ref_ty.heap_type {
2179 HeapType::Concrete(idx) => idx,
2180 _ => unreachable!(),
2181 };
2182 let func_ty = match &module.ty(idx).composite_type.inner {
2183 CompositeInnerType::Func(f) => f,
2184 CompositeInnerType::Array(_) | CompositeInnerType::Struct(_) => return false,
2185 };
2186
2187 let ty = builder.allocs.operands.pop().unwrap();
2188 let valid = builder.types_on_stack(module, &func_ty.params)
2189 && builder.func_ty.results == func_ty.results;
2190 builder.allocs.operands.push(ty);
2191 valid
2192 }
2193
2194 fn return_call_ref(
2195 _u: &mut Unstructured,
2196 module: &Module,
2197 builder: &mut CodeBuilder,
2198 instructions: &mut Vec<Instruction>,
2199 ) -> Result<()> {
2200 let heap_ty = match builder.pop_operand() {
2201 Some(ValType::Ref(r)) => r.heap_type,
2202 _ => unreachable!(),
2203 };
2204 let idx = match heap_ty {
2205 HeapType::Concrete(idx) => idx,
2206 _ => unreachable!(),
2207 };
2208 let func_ty = match &module.ty(idx).composite_type.inner {
2209 CompositeInnerType::Func(f) => f,
2210 _ => unreachable!(),
2211 };
2212 builder.pop_operands(module, &func_ty.params);
2213 builder.push_operands(&func_ty.results);
2214 instructions.push(Instruction::ReturnCallRef(idx));
2215 Ok(())
2216 }
2217
2218 #[inline]
2219 fn return_call_indirect_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2220 if !module.config.tail_call_enabled {
2221 return false;
2222 }
2223 call_indirect_valid_impl(module, builder, true)
2224 }
2225
2226 fn return_call_indirect(
2227 u: &mut Unstructured,
2228 module: &Module,
2229 builder: &mut CodeBuilder,
2230 instructions: &mut Vec<Instruction>,
2231 ) -> Result<()> {
2232 let table = select_call_indirect_table(u, module, builder)?;
2233
2234 let choices = module
2235 .func_types()
2236 .filter(|(_, ty)| {
2237 builder.types_on_stack(module, &ty.params)
2238 && builder.allocs.controls[0].label_types() == &ty.results
2239 })
2240 .collect::<Vec<_>>();
2241 let (type_idx, ty) = u.choose(&choices)?;
2242 builder.pop_operands(module, &ty.params);
2243 builder.push_operands(&ty.results);
2244 instructions.push(Instruction::ReturnCallIndirect {
2245 type_index: { *type_idx },
2246 table_index: table,
2247 });
2248 Ok(())
2249 }
2250
2251 #[inline]
2252 fn throw_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2253 module.config.exceptions_enabled
2254 && builder
2255 .allocs
2256 .tags
2257 .keys()
2258 .any(|k| builder.types_on_stack(module, k))
2259 }
2260
2261 fn throw(
2262 u: &mut Unstructured,
2263 module: &Module,
2264 builder: &mut CodeBuilder,
2265 instructions: &mut Vec<Instruction>,
2266 ) -> Result<()> {
2267 let candidates = builder
2268 .allocs
2269 .tags
2270 .iter()
2271 .filter(|(k, _)| builder.types_on_stack(module, k))
2272 .flat_map(|(_, v)| v.iter().copied())
2273 .collect::<Vec<_>>();
2274 assert!(candidates.len() > 0);
2275 let i = u.int_in_range(0..=candidates.len() - 1)?;
2276 let (tag_idx, tag_type) = module.tags().nth(candidates[i] as usize).unwrap();
2277 // Tags have no results, throwing cannot return
2278 assert!(tag_type.func_type.results.len() == 0);
2279 builder.pop_operands(module, &tag_type.func_type.params);
2280 instructions.push(Instruction::Throw(tag_idx));
2281 Ok(())
2282 }
2283
2284 #[inline]
2285 fn throw_ref_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2286 module.config.exceptions_enabled && builder.types_on_stack(module, &[ValType::EXNREF])
2287 }
2288
2289 fn throw_ref(
2290 _u: &mut Unstructured,
2291 module: &Module,
2292 builder: &mut CodeBuilder,
2293 instructions: &mut Vec<Instruction>,
2294 ) -> Result<()> {
2295 builder.pop_operands(module, &[ValType::EXNREF]);
2296 instructions.push(Instruction::ThrowRef);
2297 Ok(())
2298 }
2299
2300 #[inline]
2301 fn br_on_null_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2302 if !module.config.gc_enabled {
2303 return false;
2304 }
2305 if builder.ref_type_on_stack().is_none() {
2306 return false;
2307 }
2308 let ty = builder.allocs.operands.pop().unwrap();
2309 let valid = br_valid(module, builder);
2310 builder.allocs.operands.push(ty);
2311 valid
2312 }
2313
2314 fn br_on_null(
2315 u: &mut Unstructured,
2316 module: &Module,
2317 builder: &mut CodeBuilder,
2318 instructions: &mut Vec<Instruction>,
2319 ) -> Result<()> {
2320 let heap_type = match builder.pop_ref_type() {
2321 Some(r) => r.heap_type,
2322 None => {
2323 if !module.types.is_empty() && u.arbitrary()? {
2324 HeapType::Concrete(u.int_in_range(0..=u32::try_from(module.types.len()).unwrap())?)
2325 } else {
2326 use AbstractHeapType::*;
2327 let ty = *u.choose(&[
2328 Func, Extern, Any, None, NoExtern, NoFunc, Eq, Struct, Array, I31,
2329 ])?;
2330 let shared = module.arbitrary_shared(u)?;
2331 HeapType::Abstract { shared, ty }
2332 }
2333 }
2334 };
2335
2336 let n = builder
2337 .allocs
2338 .controls
2339 .iter()
2340 .filter(|l| builder.label_types_on_stack(module, l))
2341 .count();
2342 debug_assert!(n > 0);
2343
2344 let i = u.int_in_range(0..=n - 1)?;
2345 let (target, _) = builder
2346 .allocs
2347 .controls
2348 .iter()
2349 .rev()
2350 .enumerate()
2351 .filter(|(_, l)| builder.label_types_on_stack(module, l))
2352 .nth(i)
2353 .unwrap();
2354 let target = u32::try_from(target).unwrap();
2355
2356 builder.pop_push_label_types(module, target);
2357 builder.push_operands(&[ValType::Ref(RefType {
2358 nullable: false,
2359 heap_type,
2360 })]);
2361
2362 instructions.push(Instruction::BrOnNull(target));
2363 Ok(())
2364 }
2365
2366 fn is_valid_br_on_non_null_control(
2367 module: &Module,
2368 control: &Control,
2369 builder: &CodeBuilder,
2370 ) -> bool {
2371 let ref_ty = match control.label_types().last() {
2372 Some(ValType::Ref(r)) => *r,
2373 Some(_) | None => return false,
2374 };
2375 let nullable_ref_ty = RefType {
2376 nullable: true,
2377 ..ref_ty
2378 };
2379 builder.type_on_stack(module, ValType::Ref(nullable_ref_ty))
2380 && control
2381 .label_types()
2382 .iter()
2383 .rev()
2384 .enumerate()
2385 .skip(1)
2386 .all(|(idx, ty)| builder.type_on_stack_at(module, idx, *ty))
2387 }
2388
2389 #[inline]
2390 fn br_on_non_null_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2391 module.config.gc_enabled
2392 && builder
2393 .allocs
2394 .controls
2395 .iter()
2396 .any(|l| is_valid_br_on_non_null_control(module, l, builder))
2397 }
2398
2399 fn br_on_non_null(
2400 u: &mut Unstructured,
2401 module: &Module,
2402 builder: &mut CodeBuilder,
2403 instructions: &mut Vec<Instruction>,
2404 ) -> Result<()> {
2405 let n = builder
2406 .allocs
2407 .controls
2408 .iter()
2409 .filter(|l| is_valid_br_on_non_null_control(module, l, builder))
2410 .count();
2411 debug_assert!(n > 0);
2412
2413 let i = u.int_in_range(0..=n - 1)?;
2414 let (target, _) = builder
2415 .allocs
2416 .controls
2417 .iter()
2418 .rev()
2419 .enumerate()
2420 .filter(|(_, l)| is_valid_br_on_non_null_control(module, l, builder))
2421 .nth(i)
2422 .unwrap();
2423 let target = u32::try_from(target).unwrap();
2424
2425 builder.pop_push_label_types(module, target);
2426 builder.pop_ref_type();
2427 instructions.push(Instruction::BrOnNonNull(target));
2428 Ok(())
2429 }
2430
2431 fn is_valid_br_on_cast_control(
2432 module: &Module,
2433 builder: &CodeBuilder,
2434 control: &Control,
2435 from_ref_ty: Option<RefType>,
2436 ) -> bool {
2437 // The last label type is a sub type of the type we are casting from...
2438 let to_ref_ty = match control.label_types().last() {
2439 Some(ValType::Ref(r)) => *r,
2440 _ => return false,
2441 };
2442 if let Some(from_ty) = from_ref_ty {
2443 if !module.ref_type_is_sub_type(to_ref_ty, from_ty) {
2444 return false;
2445 }
2446 }
2447 // ... and the rest of the label types are on the stack.
2448 control
2449 .label_types()
2450 .iter()
2451 .rev()
2452 .enumerate()
2453 .skip(1)
2454 .all(|(idx, ty)| builder.type_on_stack_at(module, idx, *ty))
2455 }
2456
2457 #[inline]
2458 fn br_on_cast_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2459 let from_ref_ty = match builder.ref_type_on_stack() {
2460 None => return false,
2461 Some(r) => r,
2462 };
2463 module.config.gc_enabled
2464 && builder
2465 .allocs
2466 .controls
2467 .iter()
2468 .any(|l| is_valid_br_on_cast_control(module, builder, l, from_ref_ty))
2469 }
2470
2471 /// Compute the [type difference] between the two given ref types.
2472 ///
2473 /// [type difference]: https://webassembly.github.io/gc/core/valid/conventions.html#aux-reftypediff
2474 fn ref_type_difference(a: RefType, b: RefType) -> RefType {
2475 RefType {
2476 nullable: if b.nullable { false } else { a.nullable },
2477 heap_type: a.heap_type,
2478 }
2479 }
2480
2481 fn br_on_cast(
2482 u: &mut Unstructured,
2483 module: &Module,
2484 builder: &mut CodeBuilder,
2485 instructions: &mut Vec<Instruction>,
2486 ) -> Result<()> {
2487 let from_ref_type = builder.ref_type_on_stack().unwrap();
2488
2489 let n = builder
2490 .allocs
2491 .controls
2492 .iter()
2493 .filter(|l| is_valid_br_on_cast_control(module, builder, l, from_ref_type))
2494 .count();
2495 debug_assert!(n > 0);
2496
2497 let i = u.int_in_range(0..=n - 1)?;
2498 let (relative_depth, control) = builder
2499 .allocs
2500 .controls
2501 .iter()
2502 .rev()
2503 .enumerate()
2504 .filter(|(_, l)| is_valid_br_on_cast_control(module, builder, l, from_ref_type))
2505 .nth(i)
2506 .unwrap();
2507 let relative_depth = u32::try_from(relative_depth).unwrap();
2508
2509 let num_label_types = control.label_types().len();
2510 let to_ref_type = match control.label_types().last() {
2511 Some(ValType::Ref(r)) => *r,
2512 _ => unreachable!(),
2513 };
2514
2515 let to_ref_type = module.arbitrary_matching_ref_type(u, to_ref_type)?;
2516 let from_ref_type = from_ref_type.unwrap_or(to_ref_type);
2517 let from_ref_type = module.arbitrary_super_type_of_ref_type(u, from_ref_type)?;
2518
2519 // Do `pop_push_label_types` but without its debug assert that the types are
2520 // on the stack, since we know that we have a `from_ref_type` but the label
2521 // requires a `to_ref_type`.
2522 for _ in 0..num_label_types {
2523 builder.pop_operand();
2524 }
2525 builder.push_label_types(relative_depth);
2526
2527 // Replace the label's `to_ref_type` with the type difference.
2528 builder.pop_operand();
2529 builder.push_operands(&[ValType::Ref(ref_type_difference(
2530 from_ref_type,
2531 to_ref_type,
2532 ))]);
2533
2534 instructions.push(Instruction::BrOnCast {
2535 from_ref_type,
2536 to_ref_type,
2537 relative_depth,
2538 });
2539 Ok(())
2540 }
2541
2542 fn is_valid_br_on_cast_fail_control(
2543 module: &Module,
2544 builder: &CodeBuilder,
2545 control: &Control,
2546 from_ref_type: Option<RefType>,
2547 ) -> bool {
2548 control
2549 .label_types()
2550 .last()
2551 .map_or(false, |label_ty| match (label_ty, from_ref_type) {
2552 (ValType::Ref(label_ty), Some(from_ty)) => {
2553 module.ref_type_is_sub_type(from_ty, *label_ty)
2554 }
2555 (ValType::Ref(_), None) => true,
2556 _ => false,
2557 })
2558 && control
2559 .label_types()
2560 .iter()
2561 .rev()
2562 .enumerate()
2563 .skip(1)
2564 .all(|(idx, ty)| builder.type_on_stack_at(module, idx, *ty))
2565 }
2566
2567 #[inline]
2568 fn br_on_cast_fail_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2569 let from_ref_ty = match builder.ref_type_on_stack() {
2570 None => return false,
2571 Some(r) => r,
2572 };
2573 module.config.gc_enabled
2574 && builder
2575 .allocs
2576 .controls
2577 .iter()
2578 .any(|l| is_valid_br_on_cast_fail_control(module, builder, l, from_ref_ty))
2579 }
2580
2581 fn br_on_cast_fail(
2582 u: &mut Unstructured,
2583 module: &Module,
2584 builder: &mut CodeBuilder,
2585 instructions: &mut Vec<Instruction>,
2586 ) -> Result<()> {
2587 let from_ref_type = builder.ref_type_on_stack().unwrap();
2588
2589 let n = builder
2590 .allocs
2591 .controls
2592 .iter()
2593 .filter(|l| is_valid_br_on_cast_fail_control(module, builder, l, from_ref_type))
2594 .count();
2595 debug_assert!(n > 0);
2596
2597 let i = u.int_in_range(0..=n - 1)?;
2598 let (relative_depth, control) = builder
2599 .allocs
2600 .controls
2601 .iter()
2602 .rev()
2603 .enumerate()
2604 .filter(|(_, l)| is_valid_br_on_cast_fail_control(module, builder, l, from_ref_type))
2605 .nth(i)
2606 .unwrap();
2607 let relative_depth = u32::try_from(relative_depth).unwrap();
2608
2609 let from_ref_type =
2610 from_ref_type.unwrap_or_else(|| match control.label_types().last().unwrap() {
2611 ValType::Ref(r) => *r,
2612 _ => unreachable!(),
2613 });
2614 let to_ref_type = module.arbitrary_matching_ref_type(u, from_ref_type)?;
2615
2616 // Pop-push the label types and then replace its last reference type with
2617 // our `to_ref_type`.
2618 builder.pop_push_label_types(module, relative_depth);
2619 builder.pop_operand();
2620 builder.push_operand(Some(ValType::Ref(to_ref_type)));
2621
2622 instructions.push(Instruction::BrOnCastFail {
2623 from_ref_type,
2624 to_ref_type,
2625 relative_depth,
2626 });
2627 Ok(())
2628 }
2629
2630 #[inline]
2631 fn drop_valid(_module: &Module, builder: &mut CodeBuilder) -> bool {
2632 !builder.operands().is_empty()
2633 }
2634
2635 fn drop(
2636 u: &mut Unstructured,
2637 _module: &Module,
2638 builder: &mut CodeBuilder,
2639 instructions: &mut Vec<Instruction>,
2640 ) -> Result<()> {
2641 let ty = builder.pop_operand();
2642 builder.drop_operand(u, ty, instructions)?;
2643 Ok(())
2644 }
2645
2646 #[inline]
2647 fn select_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2648 if !(builder.operands().len() >= 3 && builder.type_on_stack(module, ValType::I32)) {
2649 return false;
2650 }
2651 let t = builder.operands()[builder.operands().len() - 2];
2652 let u = builder.operands()[builder.operands().len() - 3];
2653 t.is_none() || u.is_none() || t == u
2654 }
2655
2656 fn select(
2657 _: &mut Unstructured,
2658 _module: &Module,
2659 builder: &mut CodeBuilder,
2660 instructions: &mut Vec<Instruction>,
2661 ) -> Result<()> {
2662 builder.pop_operand();
2663 let t = builder.pop_operand();
2664 let u = builder.pop_operand();
2665 let ty = t.or(u);
2666 builder.allocs.operands.push(ty);
2667 match ty {
2668 Some(ty @ ValType::Ref(_)) => instructions.push(Instruction::TypedSelect(ty)),
2669 Some(ValType::I32) | Some(ValType::I64) | Some(ValType::F32) | Some(ValType::F64)
2670 | Some(ValType::V128) | None => instructions.push(Instruction::Select),
2671 }
2672 Ok(())
2673 }
2674
2675 #[inline]
2676 fn local_get_valid(_module: &Module, builder: &mut CodeBuilder) -> bool {
2677 !builder.func_ty.params.is_empty() || !builder.locals.is_empty()
2678 }
2679
2680 fn local_get(
2681 u: &mut Unstructured,
2682 _module: &Module,
2683 builder: &mut CodeBuilder,
2684 instructions: &mut Vec<Instruction>,
2685 ) -> Result<()> {
2686 let num_params = builder.func_ty.params.len();
2687 let n = num_params + builder.locals.len();
2688 debug_assert!(n > 0);
2689 let i = u.int_in_range(0..=n - 1)?;
2690 builder.allocs.operands.push(Some(if i < num_params {
2691 builder.func_ty.params[i]
2692 } else {
2693 builder.locals[i - num_params]
2694 }));
2695 instructions.push(Instruction::LocalGet(i as u32));
2696 Ok(())
2697 }
2698
2699 #[inline]
2700 fn local_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2701 builder
2702 .func_ty
2703 .params
2704 .iter()
2705 .chain(builder.locals.iter())
2706 .any(|ty| builder.type_on_stack(module, *ty))
2707 }
2708
2709 fn local_set(
2710 u: &mut Unstructured,
2711 module: &Module,
2712 builder: &mut CodeBuilder,
2713 instructions: &mut Vec<Instruction>,
2714 ) -> Result<()> {
2715 let n = builder
2716 .func_ty
2717 .params
2718 .iter()
2719 .chain(builder.locals.iter())
2720 .filter(|ty| builder.type_on_stack(module, **ty))
2721 .count();
2722 debug_assert!(n > 0);
2723 let i = u.int_in_range(0..=n - 1)?;
2724 let (j, _) = builder
2725 .func_ty
2726 .params
2727 .iter()
2728 .chain(builder.locals.iter())
2729 .enumerate()
2730 .filter(|(_, ty)| builder.type_on_stack(module, **ty))
2731 .nth(i)
2732 .unwrap();
2733 builder.allocs.operands.pop();
2734 instructions.push(Instruction::LocalSet(j as u32));
2735 Ok(())
2736 }
2737
2738 fn local_tee(
2739 u: &mut Unstructured,
2740 module: &Module,
2741 builder: &mut CodeBuilder,
2742 instructions: &mut Vec<Instruction>,
2743 ) -> Result<()> {
2744 let n = builder
2745 .func_ty
2746 .params
2747 .iter()
2748 .chain(builder.locals.iter())
2749 .filter(|ty| builder.type_on_stack(module, **ty))
2750 .count();
2751 debug_assert!(n > 0);
2752 let i = u.int_in_range(0..=n - 1)?;
2753 let (j, ty) = builder
2754 .func_ty
2755 .params
2756 .iter()
2757 .chain(builder.locals.iter())
2758 .enumerate()
2759 .filter(|(_, ty)| builder.type_on_stack(module, **ty))
2760 .nth(i)
2761 .unwrap();
2762 builder.allocs.operands.pop();
2763 instructions.push(Instruction::LocalTee(j as u32));
2764 builder.push_operand(Some(*ty));
2765 Ok(())
2766 }
2767
2768 #[inline]
2769 fn global_get_valid(module: &Module, _: &mut CodeBuilder) -> bool {
2770 module.globals.len() > 0
2771 }
2772
2773 fn global_get(
2774 u: &mut Unstructured,
2775 module: &Module,
2776 builder: &mut CodeBuilder,
2777 instructions: &mut Vec<Instruction>,
2778 ) -> Result<()> {
2779 debug_assert!(module.globals.len() > 0);
2780 let global_idx = u.int_in_range(0..=module.globals.len() - 1)?;
2781 builder
2782 .allocs
2783 .operands
2784 .push(Some(module.globals[global_idx].val_type));
2785 instructions.push(Instruction::GlobalGet(global_idx as u32));
2786 Ok(())
2787 }
2788
2789 #[inline]
2790 fn global_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
2791 builder
2792 .allocs
2793 .mutable_globals
2794 .iter()
2795 .any(|(ty, _)| builder.type_on_stack(module, *ty))
2796 }
2797
2798 fn global_set(
2799 u: &mut Unstructured,
2800 module: &Module,
2801 builder: &mut CodeBuilder,
2802 instructions: &mut Vec<Instruction>,
2803 ) -> Result<()> {
2804 let candidates = builder
2805 .allocs
2806 .mutable_globals
2807 .iter()
2808 .find(|(ty, _)| builder.type_on_stack(module, **ty))
2809 .unwrap()
2810 .1;
2811 let i = u.int_in_range(0..=candidates.len() - 1)?;
2812 builder.allocs.operands.pop();
2813 instructions.push(Instruction::GlobalSet(candidates[i]));
2814 Ok(())
2815 }
2816
2817 #[inline]
2818 fn have_memory(module: &Module, _: &mut CodeBuilder) -> bool {
2819 module.memories.len() > 0
2820 }
2821
2822 #[inline]
2823 fn have_memory_and_offset(module: &Module, builder: &mut CodeBuilder) -> bool {
2824 (builder.allocs.memory32.len() > 0 && builder.type_on_stack(module, ValType::I32))
2825 || (builder.allocs.memory64.len() > 0 && builder.type_on_stack(module, ValType::I64))
2826 }
2827
2828 #[inline]
2829 fn have_data(module: &Module, _: &mut CodeBuilder) -> bool {
2830 module.data.len() > 0
2831 }
2832
2833 fn i32_load(
2834 u: &mut Unstructured,
2835 module: &Module,
2836 builder: &mut CodeBuilder,
2837 instructions: &mut Vec<Instruction>,
2838 ) -> Result<()> {
2839 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
2840 builder.allocs.operands.push(Some(ValType::I32));
2841 if module.config.disallow_traps {
2842 no_traps::load(Instruction::I32Load(memarg), module, builder, instructions);
2843 } else {
2844 instructions.push(Instruction::I32Load(memarg));
2845 }
2846 Ok(())
2847 }
2848
2849 fn i64_load(
2850 u: &mut Unstructured,
2851 module: &Module,
2852 builder: &mut CodeBuilder,
2853 instructions: &mut Vec<Instruction>,
2854 ) -> Result<()> {
2855 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3])?;
2856 builder.allocs.operands.push(Some(ValType::I64));
2857 if module.config.disallow_traps {
2858 no_traps::load(Instruction::I64Load(memarg), module, builder, instructions);
2859 } else {
2860 instructions.push(Instruction::I64Load(memarg));
2861 }
2862 Ok(())
2863 }
2864
2865 fn f32_load(
2866 u: &mut Unstructured,
2867 module: &Module,
2868 builder: &mut CodeBuilder,
2869 instructions: &mut Vec<Instruction>,
2870 ) -> Result<()> {
2871 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
2872 builder.allocs.operands.push(Some(ValType::F32));
2873 if module.config.disallow_traps {
2874 no_traps::load(Instruction::F32Load(memarg), module, builder, instructions);
2875 } else {
2876 instructions.push(Instruction::F32Load(memarg));
2877 }
2878 Ok(())
2879 }
2880
2881 fn f64_load(
2882 u: &mut Unstructured,
2883 module: &Module,
2884 builder: &mut CodeBuilder,
2885 instructions: &mut Vec<Instruction>,
2886 ) -> Result<()> {
2887 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3])?;
2888 builder.allocs.operands.push(Some(ValType::F64));
2889 if module.config.disallow_traps {
2890 no_traps::load(Instruction::F64Load(memarg), module, builder, instructions);
2891 } else {
2892 instructions.push(Instruction::F64Load(memarg));
2893 }
2894 Ok(())
2895 }
2896
2897 fn i32_load_8_s(
2898 u: &mut Unstructured,
2899 module: &Module,
2900 builder: &mut CodeBuilder,
2901 instructions: &mut Vec<Instruction>,
2902 ) -> Result<()> {
2903 let memarg = mem_arg(u, module, builder, &[0])?;
2904 builder.allocs.operands.push(Some(ValType::I32));
2905 if module.config.disallow_traps {
2906 no_traps::load(
2907 Instruction::I32Load8S(memarg),
2908 module,
2909 builder,
2910 instructions,
2911 );
2912 } else {
2913 instructions.push(Instruction::I32Load8S(memarg));
2914 }
2915 Ok(())
2916 }
2917
2918 fn i32_load_8_u(
2919 u: &mut Unstructured,
2920 module: &Module,
2921 builder: &mut CodeBuilder,
2922 instructions: &mut Vec<Instruction>,
2923 ) -> Result<()> {
2924 let memarg = mem_arg(u, module, builder, &[0])?;
2925 builder.allocs.operands.push(Some(ValType::I32));
2926 if module.config.disallow_traps {
2927 no_traps::load(
2928 Instruction::I32Load8U(memarg),
2929 module,
2930 builder,
2931 instructions,
2932 );
2933 } else {
2934 instructions.push(Instruction::I32Load8U(memarg));
2935 }
2936 Ok(())
2937 }
2938
2939 fn i32_load_16_s(
2940 u: &mut Unstructured,
2941 module: &Module,
2942 builder: &mut CodeBuilder,
2943 instructions: &mut Vec<Instruction>,
2944 ) -> Result<()> {
2945 let memarg = mem_arg(u, module, builder, &[0, 1])?;
2946 builder.allocs.operands.push(Some(ValType::I32));
2947 if module.config.disallow_traps {
2948 no_traps::load(
2949 Instruction::I32Load16S(memarg),
2950 module,
2951 builder,
2952 instructions,
2953 );
2954 } else {
2955 instructions.push(Instruction::I32Load16S(memarg));
2956 }
2957 Ok(())
2958 }
2959
2960 fn i32_load_16_u(
2961 u: &mut Unstructured,
2962 module: &Module,
2963 builder: &mut CodeBuilder,
2964 instructions: &mut Vec<Instruction>,
2965 ) -> Result<()> {
2966 let memarg = mem_arg(u, module, builder, &[0, 1])?;
2967 builder.allocs.operands.push(Some(ValType::I32));
2968 if module.config.disallow_traps {
2969 no_traps::load(
2970 Instruction::I32Load16U(memarg),
2971 module,
2972 builder,
2973 instructions,
2974 );
2975 } else {
2976 instructions.push(Instruction::I32Load16U(memarg));
2977 }
2978 Ok(())
2979 }
2980
2981 fn i64_load_8_s(
2982 u: &mut Unstructured,
2983 module: &Module,
2984 builder: &mut CodeBuilder,
2985 instructions: &mut Vec<Instruction>,
2986 ) -> Result<()> {
2987 let memarg = mem_arg(u, module, builder, &[0])?;
2988 builder.allocs.operands.push(Some(ValType::I64));
2989 if module.config.disallow_traps {
2990 no_traps::load(
2991 Instruction::I64Load8S(memarg),
2992 module,
2993 builder,
2994 instructions,
2995 );
2996 } else {
2997 instructions.push(Instruction::I64Load8S(memarg));
2998 }
2999 Ok(())
3000 }
3001
3002 fn i64_load_16_s(
3003 u: &mut Unstructured,
3004 module: &Module,
3005 builder: &mut CodeBuilder,
3006 instructions: &mut Vec<Instruction>,
3007 ) -> Result<()> {
3008 let memarg = mem_arg(u, module, builder, &[0, 1])?;
3009 builder.allocs.operands.push(Some(ValType::I64));
3010 if module.config.disallow_traps {
3011 no_traps::load(
3012 Instruction::I64Load16S(memarg),
3013 module,
3014 builder,
3015 instructions,
3016 );
3017 } else {
3018 instructions.push(Instruction::I64Load16S(memarg));
3019 }
3020 Ok(())
3021 }
3022
3023 fn i64_load_32_s(
3024 u: &mut Unstructured,
3025 module: &Module,
3026 builder: &mut CodeBuilder,
3027 instructions: &mut Vec<Instruction>,
3028 ) -> Result<()> {
3029 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3030 builder.allocs.operands.push(Some(ValType::I64));
3031 if module.config.disallow_traps {
3032 no_traps::load(
3033 Instruction::I64Load32S(memarg),
3034 module,
3035 builder,
3036 instructions,
3037 );
3038 } else {
3039 instructions.push(Instruction::I64Load32S(memarg));
3040 }
3041 Ok(())
3042 }
3043
3044 fn i64_load_8_u(
3045 u: &mut Unstructured,
3046 module: &Module,
3047 builder: &mut CodeBuilder,
3048 instructions: &mut Vec<Instruction>,
3049 ) -> Result<()> {
3050 let memarg = mem_arg(u, module, builder, &[0])?;
3051 builder.allocs.operands.push(Some(ValType::I64));
3052 if module.config.disallow_traps {
3053 no_traps::load(
3054 Instruction::I64Load8U(memarg),
3055 module,
3056 builder,
3057 instructions,
3058 );
3059 } else {
3060 instructions.push(Instruction::I64Load8U(memarg));
3061 }
3062 Ok(())
3063 }
3064
3065 fn i64_load_16_u(
3066 u: &mut Unstructured,
3067 module: &Module,
3068 builder: &mut CodeBuilder,
3069 instructions: &mut Vec<Instruction>,
3070 ) -> Result<()> {
3071 let memarg = mem_arg(u, module, builder, &[0, 1])?;
3072 builder.allocs.operands.push(Some(ValType::I64));
3073 if module.config.disallow_traps {
3074 no_traps::load(
3075 Instruction::I64Load16U(memarg),
3076 module,
3077 builder,
3078 instructions,
3079 );
3080 } else {
3081 instructions.push(Instruction::I64Load16U(memarg));
3082 }
3083 Ok(())
3084 }
3085
3086 fn i64_load_32_u(
3087 u: &mut Unstructured,
3088 module: &Module,
3089 builder: &mut CodeBuilder,
3090 instructions: &mut Vec<Instruction>,
3091 ) -> Result<()> {
3092 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3093 builder.allocs.operands.push(Some(ValType::I64));
3094 if module.config.disallow_traps {
3095 no_traps::load(
3096 Instruction::I64Load32U(memarg),
3097 module,
3098 builder,
3099 instructions,
3100 );
3101 } else {
3102 instructions.push(Instruction::I64Load32U(memarg));
3103 }
3104 Ok(())
3105 }
3106
3107 #[inline]
3108 fn store_valid(module: &Module, builder: &mut CodeBuilder, f: impl Fn() -> ValType) -> bool {
3109 (builder.allocs.memory32.len() > 0 && builder.types_on_stack(module, &[ValType::I32, f()]))
3110 || (builder.allocs.memory64.len() > 0
3111 && builder.types_on_stack(module, &[ValType::I64, f()]))
3112 }
3113
3114 #[inline]
3115 fn i32_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3116 store_valid(module, builder, || ValType::I32)
3117 }
3118
3119 fn i32_store(
3120 u: &mut Unstructured,
3121 module: &Module,
3122 builder: &mut CodeBuilder,
3123 instructions: &mut Vec<Instruction>,
3124 ) -> Result<()> {
3125 builder.pop_operands(module, &[ValType::I32]);
3126 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3127 if module.config.disallow_traps {
3128 no_traps::store(Instruction::I32Store(memarg), module, builder, instructions);
3129 } else {
3130 instructions.push(Instruction::I32Store(memarg));
3131 }
3132 Ok(())
3133 }
3134
3135 #[inline]
3136 fn i64_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3137 store_valid(module, builder, || ValType::I64)
3138 }
3139
3140 fn i64_store(
3141 u: &mut Unstructured,
3142 module: &Module,
3143 builder: &mut CodeBuilder,
3144 instructions: &mut Vec<Instruction>,
3145 ) -> Result<()> {
3146 builder.pop_operands(module, &[ValType::I64]);
3147 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3])?;
3148 if module.config.disallow_traps {
3149 no_traps::store(Instruction::I64Store(memarg), module, builder, instructions);
3150 } else {
3151 instructions.push(Instruction::I64Store(memarg));
3152 }
3153 Ok(())
3154 }
3155
3156 #[inline]
3157 fn f32_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3158 store_valid(module, builder, || ValType::F32)
3159 }
3160
3161 fn f32_store(
3162 u: &mut Unstructured,
3163 module: &Module,
3164 builder: &mut CodeBuilder,
3165 instructions: &mut Vec<Instruction>,
3166 ) -> Result<()> {
3167 builder.pop_operands(module, &[ValType::F32]);
3168 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3169 if module.config.disallow_traps {
3170 no_traps::store(Instruction::F32Store(memarg), module, builder, instructions);
3171 } else {
3172 instructions.push(Instruction::F32Store(memarg));
3173 }
3174 Ok(())
3175 }
3176
3177 #[inline]
3178 fn f64_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3179 store_valid(module, builder, || ValType::F64)
3180 }
3181
3182 fn f64_store(
3183 u: &mut Unstructured,
3184 module: &Module,
3185 builder: &mut CodeBuilder,
3186 instructions: &mut Vec<Instruction>,
3187 ) -> Result<()> {
3188 builder.pop_operands(module, &[ValType::F64]);
3189 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3])?;
3190 if module.config.disallow_traps {
3191 no_traps::store(Instruction::F64Store(memarg), module, builder, instructions);
3192 } else {
3193 instructions.push(Instruction::F64Store(memarg));
3194 }
3195 Ok(())
3196 }
3197
3198 fn i32_store_8(
3199 u: &mut Unstructured,
3200 module: &Module,
3201 builder: &mut CodeBuilder,
3202 instructions: &mut Vec<Instruction>,
3203 ) -> Result<()> {
3204 builder.pop_operands(module, &[ValType::I32]);
3205 let memarg = mem_arg(u, module, builder, &[0])?;
3206 if module.config.disallow_traps {
3207 no_traps::store(
3208 Instruction::I32Store8(memarg),
3209 module,
3210 builder,
3211 instructions,
3212 );
3213 } else {
3214 instructions.push(Instruction::I32Store8(memarg));
3215 }
3216 Ok(())
3217 }
3218
3219 fn i32_store_16(
3220 u: &mut Unstructured,
3221 module: &Module,
3222 builder: &mut CodeBuilder,
3223 instructions: &mut Vec<Instruction>,
3224 ) -> Result<()> {
3225 builder.pop_operands(module, &[ValType::I32]);
3226 let memarg = mem_arg(u, module, builder, &[0, 1])?;
3227 if module.config.disallow_traps {
3228 no_traps::store(
3229 Instruction::I32Store16(memarg),
3230 module,
3231 builder,
3232 instructions,
3233 );
3234 } else {
3235 instructions.push(Instruction::I32Store16(memarg));
3236 }
3237 Ok(())
3238 }
3239
3240 fn i64_store_8(
3241 u: &mut Unstructured,
3242 module: &Module,
3243 builder: &mut CodeBuilder,
3244 instructions: &mut Vec<Instruction>,
3245 ) -> Result<()> {
3246 builder.pop_operands(module, &[ValType::I64]);
3247 let memarg = mem_arg(u, module, builder, &[0])?;
3248 if module.config.disallow_traps {
3249 no_traps::store(
3250 Instruction::I64Store8(memarg),
3251 module,
3252 builder,
3253 instructions,
3254 );
3255 } else {
3256 instructions.push(Instruction::I64Store8(memarg));
3257 }
3258 Ok(())
3259 }
3260
3261 fn i64_store_16(
3262 u: &mut Unstructured,
3263 module: &Module,
3264 builder: &mut CodeBuilder,
3265 instructions: &mut Vec<Instruction>,
3266 ) -> Result<()> {
3267 builder.pop_operands(module, &[ValType::I64]);
3268 let memarg = mem_arg(u, module, builder, &[0, 1])?;
3269 if module.config.disallow_traps {
3270 no_traps::store(
3271 Instruction::I64Store16(memarg),
3272 module,
3273 builder,
3274 instructions,
3275 );
3276 } else {
3277 instructions.push(Instruction::I64Store16(memarg));
3278 }
3279 Ok(())
3280 }
3281
3282 fn i64_store_32(
3283 u: &mut Unstructured,
3284 module: &Module,
3285 builder: &mut CodeBuilder,
3286 instructions: &mut Vec<Instruction>,
3287 ) -> Result<()> {
3288 builder.pop_operands(module, &[ValType::I64]);
3289 let memarg = mem_arg(u, module, builder, &[0, 1, 2])?;
3290 if module.config.disallow_traps {
3291 no_traps::store(
3292 Instruction::I64Store32(memarg),
3293 module,
3294 builder,
3295 instructions,
3296 );
3297 } else {
3298 instructions.push(Instruction::I64Store32(memarg));
3299 }
3300 Ok(())
3301 }
3302
3303 fn memory_size(
3304 u: &mut Unstructured,
3305 module: &Module,
3306 builder: &mut CodeBuilder,
3307 instructions: &mut Vec<Instruction>,
3308 ) -> Result<()> {
3309 let i = u.int_in_range(0..=module.memories.len() - 1)?;
3310 let ty = if module.memories[i].memory64 {
3311 ValType::I64
3312 } else {
3313 ValType::I32
3314 };
3315 builder.push_operands(&[ty]);
3316 instructions.push(Instruction::MemorySize(i as u32));
3317 Ok(())
3318 }
3319
3320 #[inline]
3321 fn memory_grow_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3322 (builder.allocs.memory32.len() > 0 && builder.type_on_stack(module, ValType::I32))
3323 || (builder.allocs.memory64.len() > 0 && builder.type_on_stack(module, ValType::I64))
3324 }
3325
3326 fn memory_grow(
3327 u: &mut Unstructured,
3328 module: &Module,
3329 builder: &mut CodeBuilder,
3330 instructions: &mut Vec<Instruction>,
3331 ) -> Result<()> {
3332 let ty = if builder.type_on_stack(module, ValType::I32) {
3333 ValType::I32
3334 } else {
3335 ValType::I64
3336 };
3337 let index = memory_index(u, builder, ty)?;
3338 builder.pop_operands(module, &[ty]);
3339 builder.push_operands(&[ty]);
3340 instructions.push(Instruction::MemoryGrow(index));
3341 Ok(())
3342 }
3343
3344 #[inline]
3345 fn memory_init_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3346 module.config.bulk_memory_enabled
3347 && have_data(module, builder)
3348 && !module.config.disallow_traps // Non-trapping memory init not yet implemented
3349 && (builder.allocs.memory32.len() > 0
3350 && builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
3351 || (builder.allocs.memory64.len() > 0
3352 && builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32])))
3353 }
3354
3355 fn memory_init(
3356 u: &mut Unstructured,
3357 module: &Module,
3358 builder: &mut CodeBuilder,
3359 instructions: &mut Vec<Instruction>,
3360 ) -> Result<()> {
3361 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3362 let ty = if builder.type_on_stack(module, ValType::I32) {
3363 ValType::I32
3364 } else {
3365 ValType::I64
3366 };
3367 let mem = memory_index(u, builder, ty)?;
3368 let data_index = data_index(u, module)?;
3369 builder.pop_operands(module, &[ty]);
3370 instructions.push(Instruction::MemoryInit { mem, data_index });
3371 Ok(())
3372 }
3373
3374 #[inline]
3375 fn memory_fill_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3376 module.config.bulk_memory_enabled
3377 && !module.config.disallow_traps // Non-trapping memory fill generation not yet implemented
3378 && (builder.allocs.memory32.len() > 0
3379 && builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
3380 || (builder.allocs.memory64.len() > 0
3381 && builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I64])))
3382 }
3383
3384 fn memory_fill(
3385 u: &mut Unstructured,
3386 module: &Module,
3387 builder: &mut CodeBuilder,
3388 instructions: &mut Vec<Instruction>,
3389 ) -> Result<()> {
3390 let ty = if builder.type_on_stack(module, ValType::I32) {
3391 ValType::I32
3392 } else {
3393 ValType::I64
3394 };
3395 let mem = memory_index(u, builder, ty)?;
3396 builder.pop_operands(module, &[ty, ValType::I32, ty]);
3397 instructions.push(Instruction::MemoryFill(mem));
3398 Ok(())
3399 }
3400
3401 #[inline]
3402 fn memory_copy_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3403 if !module.config.bulk_memory_enabled {
3404 return false;
3405 }
3406
3407 // The non-trapping case for memory copy has not yet been implemented,
3408 // so we are excluding them for now
3409 if module.config.disallow_traps {
3410 return false;
3411 }
3412
3413 let n32 = builder.allocs.memory32.len();
3414 let n64 = builder.allocs.memory64.len();
3415
3416 if builder.types_on_stack(module, &[ValType::I64, ValType::I64, ValType::I64]) && n64 > 0 {
3417 return true;
3418 }
3419 if builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32]) && n32 > 0 {
3420 return true;
3421 }
3422 if builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32])
3423 && n32 > 0
3424 && n64 > 0
3425 {
3426 return true;
3427 }
3428 if builder.types_on_stack(module, &[ValType::I32, ValType::I64, ValType::I32])
3429 && n32 > 0
3430 && n64 > 0
3431 {
3432 return true;
3433 }
3434
3435 false
3436 }
3437
3438 fn memory_copy(
3439 u: &mut Unstructured,
3440 module: &Module,
3441 builder: &mut CodeBuilder,
3442 instructions: &mut Vec<Instruction>,
3443 ) -> Result<()> {
3444 let (src, dst) = gen_copy_src_and_dst(module, builder);
3445 let src_mem = src.choose(u, &builder.allocs.memory32, &builder.allocs.memory64)?;
3446 let dst_mem = dst.choose(u, &builder.allocs.memory32, &builder.allocs.memory64)?;
3447 instructions.push(Instruction::MemoryCopy { dst_mem, src_mem });
3448 Ok(())
3449 }
3450
3451 enum CopyIndexSize {
3452 I32,
3453 I64,
3454 }
3455
3456 impl CopyIndexSize {
3457 fn choose(&self, u: &mut Unstructured<'_>, n32: &[u32], n64: &[u32]) -> Result<u32> {
3458 Ok(match self {
3459 CopyIndexSize::I32 => *u.choose(n32)?,
3460 CopyIndexSize::I64 => *u.choose(n64)?,
3461 })
3462 }
3463 }
3464
3465 fn gen_copy_src_and_dst(
3466 module: &Module,
3467 builder: &mut CodeBuilder,
3468 ) -> (CopyIndexSize, CopyIndexSize) {
3469 if builder.types_on_stack(module, &[ValType::I64, ValType::I64, ValType::I64]) {
3470 builder.pop_operands(module, &[ValType::I64, ValType::I64, ValType::I64]);
3471 (CopyIndexSize::I64, CopyIndexSize::I64)
3472 } else if builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32]) {
3473 builder.pop_operands(module, &[ValType::I32, ValType::I32, ValType::I32]);
3474 (CopyIndexSize::I32, CopyIndexSize::I32)
3475 } else if builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32]) {
3476 builder.pop_operands(module, &[ValType::I64, ValType::I32, ValType::I32]);
3477 (CopyIndexSize::I32, CopyIndexSize::I64)
3478 } else if builder.types_on_stack(module, &[ValType::I32, ValType::I64, ValType::I32]) {
3479 builder.pop_operands(module, &[ValType::I32, ValType::I64, ValType::I32]);
3480 (CopyIndexSize::I64, CopyIndexSize::I32)
3481 } else {
3482 unreachable!()
3483 }
3484 }
3485
3486 #[inline]
3487 fn data_drop_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
3488 have_data(module, builder) && module.config.bulk_memory_enabled
3489 }
3490
3491 fn data_drop(
3492 u: &mut Unstructured,
3493 module: &Module,
3494 _builder: &mut CodeBuilder,
3495 instructions: &mut Vec<Instruction>,
3496 ) -> Result<()> {
3497 instructions.push(Instruction::DataDrop(data_index(u, module)?));
3498 Ok(())
3499 }
3500
3501 fn i32_const(
3502 u: &mut Unstructured,
3503 module: &Module,
3504 builder: &mut CodeBuilder,
3505 instructions: &mut Vec<Instruction>,
3506 ) -> Result<()> {
3507 builder.push_operands(&[ValType::I32]);
3508 instructions.push(module.arbitrary_const_instruction(ValType::I32, u)?);
3509 Ok(())
3510 }
3511
3512 fn i64_const(
3513 u: &mut Unstructured,
3514 module: &Module,
3515 builder: &mut CodeBuilder,
3516 instructions: &mut Vec<Instruction>,
3517 ) -> Result<()> {
3518 builder.push_operands(&[ValType::I64]);
3519 instructions.push(module.arbitrary_const_instruction(ValType::I64, u)?);
3520 Ok(())
3521 }
3522
3523 fn f32_const(
3524 u: &mut Unstructured,
3525 module: &Module,
3526 builder: &mut CodeBuilder,
3527 instructions: &mut Vec<Instruction>,
3528 ) -> Result<()> {
3529 builder.push_operands(&[ValType::F32]);
3530 instructions.push(module.arbitrary_const_instruction(ValType::F32, u)?);
3531 Ok(())
3532 }
3533
3534 fn f64_const(
3535 u: &mut Unstructured,
3536 module: &Module,
3537 builder: &mut CodeBuilder,
3538 instructions: &mut Vec<Instruction>,
3539 ) -> Result<()> {
3540 builder.push_operands(&[ValType::F64]);
3541 instructions.push(module.arbitrary_const_instruction(ValType::F64, u)?);
3542 Ok(())
3543 }
3544
3545 #[inline]
3546 fn i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3547 builder.type_on_stack(module, ValType::I32)
3548 }
3549
3550 fn i32_eqz(
3551 _: &mut Unstructured,
3552 module: &Module,
3553 builder: &mut CodeBuilder,
3554 instructions: &mut Vec<Instruction>,
3555 ) -> Result<()> {
3556 builder.pop_operands(module, &[ValType::I32]);
3557 builder.push_operands(&[ValType::I32]);
3558 instructions.push(Instruction::I32Eqz);
3559 Ok(())
3560 }
3561
3562 #[inline]
3563 fn i32_i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3564 builder.types_on_stack(module, &[ValType::I32, ValType::I32])
3565 }
3566
3567 fn i32_eq(
3568 _: &mut Unstructured,
3569 module: &Module,
3570 builder: &mut CodeBuilder,
3571 instructions: &mut Vec<Instruction>,
3572 ) -> Result<()> {
3573 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3574 builder.push_operands(&[ValType::I32]);
3575 instructions.push(Instruction::I32Eq);
3576 Ok(())
3577 }
3578
3579 fn i32_ne(
3580 _: &mut Unstructured,
3581 module: &Module,
3582 builder: &mut CodeBuilder,
3583 instructions: &mut Vec<Instruction>,
3584 ) -> Result<()> {
3585 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3586 builder.push_operands(&[ValType::I32]);
3587 instructions.push(Instruction::I32Ne);
3588 Ok(())
3589 }
3590
3591 fn i32_lt_s(
3592 _: &mut Unstructured,
3593 module: &Module,
3594 builder: &mut CodeBuilder,
3595 instructions: &mut Vec<Instruction>,
3596 ) -> Result<()> {
3597 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3598 builder.push_operands(&[ValType::I32]);
3599 instructions.push(Instruction::I32LtS);
3600 Ok(())
3601 }
3602
3603 fn i32_lt_u(
3604 _: &mut Unstructured,
3605 module: &Module,
3606 builder: &mut CodeBuilder,
3607 instructions: &mut Vec<Instruction>,
3608 ) -> Result<()> {
3609 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3610 builder.push_operands(&[ValType::I32]);
3611 instructions.push(Instruction::I32LtU);
3612 Ok(())
3613 }
3614
3615 fn i32_gt_s(
3616 _: &mut Unstructured,
3617 module: &Module,
3618 builder: &mut CodeBuilder,
3619 instructions: &mut Vec<Instruction>,
3620 ) -> Result<()> {
3621 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3622 builder.push_operands(&[ValType::I32]);
3623 instructions.push(Instruction::I32GtS);
3624 Ok(())
3625 }
3626
3627 fn i32_gt_u(
3628 _: &mut Unstructured,
3629 module: &Module,
3630 builder: &mut CodeBuilder,
3631 instructions: &mut Vec<Instruction>,
3632 ) -> Result<()> {
3633 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3634 builder.push_operands(&[ValType::I32]);
3635 instructions.push(Instruction::I32GtU);
3636 Ok(())
3637 }
3638
3639 fn i32_le_s(
3640 _: &mut Unstructured,
3641 module: &Module,
3642 builder: &mut CodeBuilder,
3643 instructions: &mut Vec<Instruction>,
3644 ) -> Result<()> {
3645 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3646 builder.push_operands(&[ValType::I32]);
3647 instructions.push(Instruction::I32LeS);
3648 Ok(())
3649 }
3650
3651 fn i32_le_u(
3652 _: &mut Unstructured,
3653 module: &Module,
3654 builder: &mut CodeBuilder,
3655 instructions: &mut Vec<Instruction>,
3656 ) -> Result<()> {
3657 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3658 builder.push_operands(&[ValType::I32]);
3659 instructions.push(Instruction::I32LeU);
3660 Ok(())
3661 }
3662
3663 fn i32_ge_s(
3664 _: &mut Unstructured,
3665 module: &Module,
3666 builder: &mut CodeBuilder,
3667 instructions: &mut Vec<Instruction>,
3668 ) -> Result<()> {
3669 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3670 builder.push_operands(&[ValType::I32]);
3671 instructions.push(Instruction::I32GeS);
3672 Ok(())
3673 }
3674
3675 fn i32_ge_u(
3676 _: &mut Unstructured,
3677 module: &Module,
3678 builder: &mut CodeBuilder,
3679 instructions: &mut Vec<Instruction>,
3680 ) -> Result<()> {
3681 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
3682 builder.push_operands(&[ValType::I32]);
3683 instructions.push(Instruction::I32GeU);
3684 Ok(())
3685 }
3686
3687 #[inline]
3688 fn i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3689 builder.types_on_stack(module, &[ValType::I64])
3690 }
3691
3692 fn i64_eqz(
3693 _: &mut Unstructured,
3694 module: &Module,
3695 builder: &mut CodeBuilder,
3696 instructions: &mut Vec<Instruction>,
3697 ) -> Result<()> {
3698 builder.pop_operands(module, &[ValType::I64]);
3699 builder.push_operands(&[ValType::I32]);
3700 instructions.push(Instruction::I64Eqz);
3701 Ok(())
3702 }
3703
3704 #[inline]
3705 fn i64_i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3706 builder.types_on_stack(module, &[ValType::I64, ValType::I64])
3707 }
3708
3709 fn i64_eq(
3710 _: &mut Unstructured,
3711 module: &Module,
3712 builder: &mut CodeBuilder,
3713 instructions: &mut Vec<Instruction>,
3714 ) -> Result<()> {
3715 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3716 builder.push_operands(&[ValType::I32]);
3717 instructions.push(Instruction::I64Eq);
3718 Ok(())
3719 }
3720
3721 fn i64_ne(
3722 _: &mut Unstructured,
3723 module: &Module,
3724 builder: &mut CodeBuilder,
3725 instructions: &mut Vec<Instruction>,
3726 ) -> Result<()> {
3727 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3728 builder.push_operands(&[ValType::I32]);
3729 instructions.push(Instruction::I64Ne);
3730 Ok(())
3731 }
3732
3733 fn i64_lt_s(
3734 _: &mut Unstructured,
3735 module: &Module,
3736 builder: &mut CodeBuilder,
3737 instructions: &mut Vec<Instruction>,
3738 ) -> Result<()> {
3739 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3740 builder.push_operands(&[ValType::I32]);
3741 instructions.push(Instruction::I64LtS);
3742 Ok(())
3743 }
3744
3745 fn i64_lt_u(
3746 _: &mut Unstructured,
3747 module: &Module,
3748 builder: &mut CodeBuilder,
3749 instructions: &mut Vec<Instruction>,
3750 ) -> Result<()> {
3751 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3752 builder.push_operands(&[ValType::I32]);
3753 instructions.push(Instruction::I64LtU);
3754 Ok(())
3755 }
3756
3757 fn i64_gt_s(
3758 _: &mut Unstructured,
3759 module: &Module,
3760 builder: &mut CodeBuilder,
3761 instructions: &mut Vec<Instruction>,
3762 ) -> Result<()> {
3763 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3764 builder.push_operands(&[ValType::I32]);
3765 instructions.push(Instruction::I64GtS);
3766 Ok(())
3767 }
3768
3769 fn i64_gt_u(
3770 _: &mut Unstructured,
3771 module: &Module,
3772 builder: &mut CodeBuilder,
3773 instructions: &mut Vec<Instruction>,
3774 ) -> Result<()> {
3775 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3776 builder.push_operands(&[ValType::I32]);
3777 instructions.push(Instruction::I64GtU);
3778 Ok(())
3779 }
3780
3781 fn i64_le_s(
3782 _: &mut Unstructured,
3783 module: &Module,
3784 builder: &mut CodeBuilder,
3785 instructions: &mut Vec<Instruction>,
3786 ) -> Result<()> {
3787 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3788 builder.push_operands(&[ValType::I32]);
3789 instructions.push(Instruction::I64LeS);
3790 Ok(())
3791 }
3792
3793 fn i64_le_u(
3794 _: &mut Unstructured,
3795 module: &Module,
3796 builder: &mut CodeBuilder,
3797 instructions: &mut Vec<Instruction>,
3798 ) -> Result<()> {
3799 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3800 builder.push_operands(&[ValType::I32]);
3801 instructions.push(Instruction::I64LeU);
3802 Ok(())
3803 }
3804
3805 fn i64_ge_s(
3806 _: &mut Unstructured,
3807 module: &Module,
3808 builder: &mut CodeBuilder,
3809 instructions: &mut Vec<Instruction>,
3810 ) -> Result<()> {
3811 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3812 builder.push_operands(&[ValType::I32]);
3813 instructions.push(Instruction::I64GeS);
3814 Ok(())
3815 }
3816
3817 fn i64_ge_u(
3818 _: &mut Unstructured,
3819 module: &Module,
3820 builder: &mut CodeBuilder,
3821 instructions: &mut Vec<Instruction>,
3822 ) -> Result<()> {
3823 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
3824 builder.push_operands(&[ValType::I32]);
3825 instructions.push(Instruction::I64GeU);
3826 Ok(())
3827 }
3828
3829 fn f32_f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3830 builder.types_on_stack(module, &[ValType::F32, ValType::F32])
3831 }
3832
3833 fn f32_eq(
3834 _: &mut Unstructured,
3835 module: &Module,
3836 builder: &mut CodeBuilder,
3837 instructions: &mut Vec<Instruction>,
3838 ) -> Result<()> {
3839 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3840 builder.push_operands(&[ValType::I32]);
3841 instructions.push(Instruction::F32Eq);
3842 Ok(())
3843 }
3844
3845 fn f32_ne(
3846 _: &mut Unstructured,
3847 module: &Module,
3848 builder: &mut CodeBuilder,
3849 instructions: &mut Vec<Instruction>,
3850 ) -> Result<()> {
3851 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3852 builder.push_operands(&[ValType::I32]);
3853 instructions.push(Instruction::F32Ne);
3854 Ok(())
3855 }
3856
3857 fn f32_lt(
3858 _: &mut Unstructured,
3859 module: &Module,
3860 builder: &mut CodeBuilder,
3861 instructions: &mut Vec<Instruction>,
3862 ) -> Result<()> {
3863 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3864 builder.push_operands(&[ValType::I32]);
3865 instructions.push(Instruction::F32Lt);
3866 Ok(())
3867 }
3868
3869 fn f32_gt(
3870 _: &mut Unstructured,
3871 module: &Module,
3872 builder: &mut CodeBuilder,
3873 instructions: &mut Vec<Instruction>,
3874 ) -> Result<()> {
3875 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3876 builder.push_operands(&[ValType::I32]);
3877 instructions.push(Instruction::F32Gt);
3878 Ok(())
3879 }
3880
3881 fn f32_le(
3882 _: &mut Unstructured,
3883 module: &Module,
3884 builder: &mut CodeBuilder,
3885 instructions: &mut Vec<Instruction>,
3886 ) -> Result<()> {
3887 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3888 builder.push_operands(&[ValType::I32]);
3889 instructions.push(Instruction::F32Le);
3890 Ok(())
3891 }
3892
3893 fn f32_ge(
3894 _: &mut Unstructured,
3895 module: &Module,
3896 builder: &mut CodeBuilder,
3897 instructions: &mut Vec<Instruction>,
3898 ) -> Result<()> {
3899 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
3900 builder.push_operands(&[ValType::I32]);
3901 instructions.push(Instruction::F32Ge);
3902 Ok(())
3903 }
3904
3905 fn f64_f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
3906 builder.types_on_stack(module, &[ValType::F64, ValType::F64])
3907 }
3908
3909 fn f64_eq(
3910 _: &mut Unstructured,
3911 module: &Module,
3912 builder: &mut CodeBuilder,
3913 instructions: &mut Vec<Instruction>,
3914 ) -> Result<()> {
3915 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3916 builder.push_operands(&[ValType::I32]);
3917 instructions.push(Instruction::F64Eq);
3918 Ok(())
3919 }
3920
3921 fn f64_ne(
3922 _: &mut Unstructured,
3923 module: &Module,
3924 builder: &mut CodeBuilder,
3925 instructions: &mut Vec<Instruction>,
3926 ) -> Result<()> {
3927 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3928 builder.push_operands(&[ValType::I32]);
3929 instructions.push(Instruction::F64Ne);
3930 Ok(())
3931 }
3932
3933 fn f64_lt(
3934 _: &mut Unstructured,
3935 module: &Module,
3936 builder: &mut CodeBuilder,
3937 instructions: &mut Vec<Instruction>,
3938 ) -> Result<()> {
3939 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3940 builder.push_operands(&[ValType::I32]);
3941 instructions.push(Instruction::F64Lt);
3942 Ok(())
3943 }
3944
3945 fn f64_gt(
3946 _: &mut Unstructured,
3947 module: &Module,
3948 builder: &mut CodeBuilder,
3949 instructions: &mut Vec<Instruction>,
3950 ) -> Result<()> {
3951 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3952 builder.push_operands(&[ValType::I32]);
3953 instructions.push(Instruction::F64Gt);
3954 Ok(())
3955 }
3956
3957 fn f64_le(
3958 _: &mut Unstructured,
3959 module: &Module,
3960 builder: &mut CodeBuilder,
3961 instructions: &mut Vec<Instruction>,
3962 ) -> Result<()> {
3963 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3964 builder.push_operands(&[ValType::I32]);
3965 instructions.push(Instruction::F64Le);
3966 Ok(())
3967 }
3968
3969 fn f64_ge(
3970 _: &mut Unstructured,
3971 module: &Module,
3972 builder: &mut CodeBuilder,
3973 instructions: &mut Vec<Instruction>,
3974 ) -> Result<()> {
3975 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
3976 builder.push_operands(&[ValType::I32]);
3977 instructions.push(Instruction::F64Ge);
3978 Ok(())
3979 }
3980
3981 fn i32_clz(
3982 _: &mut Unstructured,
3983 module: &Module,
3984 builder: &mut CodeBuilder,
3985 instructions: &mut Vec<Instruction>,
3986 ) -> Result<()> {
3987 builder.pop_operands(module, &[ValType::I32]);
3988 builder.push_operands(&[ValType::I32]);
3989 instructions.push(Instruction::I32Clz);
3990 Ok(())
3991 }
3992
3993 fn i32_ctz(
3994 _: &mut Unstructured,
3995 module: &Module,
3996 builder: &mut CodeBuilder,
3997 instructions: &mut Vec<Instruction>,
3998 ) -> Result<()> {
3999 builder.pop_operands(module, &[ValType::I32]);
4000 builder.push_operands(&[ValType::I32]);
4001 instructions.push(Instruction::I32Ctz);
4002 Ok(())
4003 }
4004
4005 fn i32_popcnt(
4006 _: &mut Unstructured,
4007 module: &Module,
4008 builder: &mut CodeBuilder,
4009 instructions: &mut Vec<Instruction>,
4010 ) -> Result<()> {
4011 builder.pop_operands(module, &[ValType::I32]);
4012 builder.push_operands(&[ValType::I32]);
4013 instructions.push(Instruction::I32Popcnt);
4014 Ok(())
4015 }
4016
4017 fn i32_add(
4018 _: &mut Unstructured,
4019 module: &Module,
4020 builder: &mut CodeBuilder,
4021 instructions: &mut Vec<Instruction>,
4022 ) -> Result<()> {
4023 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4024 builder.push_operands(&[ValType::I32]);
4025 instructions.push(Instruction::I32Add);
4026 Ok(())
4027 }
4028
4029 fn i32_sub(
4030 _: &mut Unstructured,
4031 module: &Module,
4032 builder: &mut CodeBuilder,
4033 instructions: &mut Vec<Instruction>,
4034 ) -> Result<()> {
4035 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4036 builder.push_operands(&[ValType::I32]);
4037 instructions.push(Instruction::I32Sub);
4038 Ok(())
4039 }
4040
4041 fn i32_mul(
4042 _: &mut Unstructured,
4043 module: &Module,
4044 builder: &mut CodeBuilder,
4045 instructions: &mut Vec<Instruction>,
4046 ) -> Result<()> {
4047 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4048 builder.push_operands(&[ValType::I32]);
4049 instructions.push(Instruction::I32Mul);
4050 Ok(())
4051 }
4052
4053 fn i32_div_s(
4054 _: &mut Unstructured,
4055 module: &Module,
4056 builder: &mut CodeBuilder,
4057 instructions: &mut Vec<Instruction>,
4058 ) -> Result<()> {
4059 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4060 builder.push_operands(&[ValType::I32]);
4061 if module.config.disallow_traps {
4062 no_traps::signed_div_rem(Instruction::I32DivS, builder, instructions);
4063 } else {
4064 instructions.push(Instruction::I32DivS);
4065 }
4066 Ok(())
4067 }
4068
4069 fn i32_div_u(
4070 _: &mut Unstructured,
4071 module: &Module,
4072 builder: &mut CodeBuilder,
4073 instructions: &mut Vec<Instruction>,
4074 ) -> Result<()> {
4075 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4076 builder.push_operands(&[ValType::I32]);
4077 if module.config.disallow_traps {
4078 no_traps::unsigned_div_rem(Instruction::I32DivU, builder, instructions);
4079 } else {
4080 instructions.push(Instruction::I32DivU);
4081 }
4082 Ok(())
4083 }
4084
4085 fn i32_rem_s(
4086 _: &mut Unstructured,
4087 module: &Module,
4088 builder: &mut CodeBuilder,
4089 instructions: &mut Vec<Instruction>,
4090 ) -> Result<()> {
4091 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4092 builder.push_operands(&[ValType::I32]);
4093 if module.config.disallow_traps {
4094 no_traps::signed_div_rem(Instruction::I32RemS, builder, instructions);
4095 } else {
4096 instructions.push(Instruction::I32RemS);
4097 }
4098 Ok(())
4099 }
4100
4101 fn i32_rem_u(
4102 _: &mut Unstructured,
4103 module: &Module,
4104 builder: &mut CodeBuilder,
4105 instructions: &mut Vec<Instruction>,
4106 ) -> Result<()> {
4107 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4108 builder.push_operands(&[ValType::I32]);
4109 if module.config.disallow_traps {
4110 no_traps::unsigned_div_rem(Instruction::I32RemU, builder, instructions);
4111 } else {
4112 instructions.push(Instruction::I32RemU);
4113 }
4114 Ok(())
4115 }
4116
4117 fn i32_and(
4118 _: &mut Unstructured,
4119 module: &Module,
4120 builder: &mut CodeBuilder,
4121 instructions: &mut Vec<Instruction>,
4122 ) -> Result<()> {
4123 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4124 builder.push_operands(&[ValType::I32]);
4125 instructions.push(Instruction::I32And);
4126 Ok(())
4127 }
4128
4129 fn i32_or(
4130 _: &mut Unstructured,
4131 module: &Module,
4132 builder: &mut CodeBuilder,
4133 instructions: &mut Vec<Instruction>,
4134 ) -> Result<()> {
4135 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4136 builder.push_operands(&[ValType::I32]);
4137 instructions.push(Instruction::I32Or);
4138 Ok(())
4139 }
4140
4141 fn i32_xor(
4142 _: &mut Unstructured,
4143 module: &Module,
4144 builder: &mut CodeBuilder,
4145 instructions: &mut Vec<Instruction>,
4146 ) -> Result<()> {
4147 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4148 builder.push_operands(&[ValType::I32]);
4149 instructions.push(Instruction::I32Xor);
4150 Ok(())
4151 }
4152
4153 fn i32_shl(
4154 _: &mut Unstructured,
4155 module: &Module,
4156 builder: &mut CodeBuilder,
4157 instructions: &mut Vec<Instruction>,
4158 ) -> Result<()> {
4159 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4160 builder.push_operands(&[ValType::I32]);
4161 instructions.push(Instruction::I32Shl);
4162 Ok(())
4163 }
4164
4165 fn i32_shr_s(
4166 _: &mut Unstructured,
4167 module: &Module,
4168 builder: &mut CodeBuilder,
4169 instructions: &mut Vec<Instruction>,
4170 ) -> Result<()> {
4171 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4172 builder.push_operands(&[ValType::I32]);
4173 instructions.push(Instruction::I32ShrS);
4174 Ok(())
4175 }
4176
4177 fn i32_shr_u(
4178 _: &mut Unstructured,
4179 module: &Module,
4180 builder: &mut CodeBuilder,
4181 instructions: &mut Vec<Instruction>,
4182 ) -> Result<()> {
4183 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4184 builder.push_operands(&[ValType::I32]);
4185 instructions.push(Instruction::I32ShrU);
4186 Ok(())
4187 }
4188
4189 fn i32_rotl(
4190 _: &mut Unstructured,
4191 module: &Module,
4192 builder: &mut CodeBuilder,
4193 instructions: &mut Vec<Instruction>,
4194 ) -> Result<()> {
4195 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4196 builder.push_operands(&[ValType::I32]);
4197 instructions.push(Instruction::I32Rotl);
4198 Ok(())
4199 }
4200
4201 fn i32_rotr(
4202 _: &mut Unstructured,
4203 module: &Module,
4204 builder: &mut CodeBuilder,
4205 instructions: &mut Vec<Instruction>,
4206 ) -> Result<()> {
4207 builder.pop_operands(module, &[ValType::I32, ValType::I32]);
4208 builder.push_operands(&[ValType::I32]);
4209 instructions.push(Instruction::I32Rotr);
4210 Ok(())
4211 }
4212
4213 fn i64_clz(
4214 _: &mut Unstructured,
4215 module: &Module,
4216 builder: &mut CodeBuilder,
4217 instructions: &mut Vec<Instruction>,
4218 ) -> Result<()> {
4219 builder.pop_operands(module, &[ValType::I64]);
4220 builder.push_operands(&[ValType::I64]);
4221 instructions.push(Instruction::I64Clz);
4222 Ok(())
4223 }
4224
4225 fn i64_ctz(
4226 _: &mut Unstructured,
4227 module: &Module,
4228 builder: &mut CodeBuilder,
4229 instructions: &mut Vec<Instruction>,
4230 ) -> Result<()> {
4231 builder.pop_operands(module, &[ValType::I64]);
4232 builder.push_operands(&[ValType::I64]);
4233 instructions.push(Instruction::I64Ctz);
4234 Ok(())
4235 }
4236
4237 fn i64_popcnt(
4238 _: &mut Unstructured,
4239 module: &Module,
4240 builder: &mut CodeBuilder,
4241 instructions: &mut Vec<Instruction>,
4242 ) -> Result<()> {
4243 builder.pop_operands(module, &[ValType::I64]);
4244 builder.push_operands(&[ValType::I64]);
4245 instructions.push(Instruction::I64Popcnt);
4246 Ok(())
4247 }
4248
4249 fn i64_add(
4250 _: &mut Unstructured,
4251 module: &Module,
4252 builder: &mut CodeBuilder,
4253 instructions: &mut Vec<Instruction>,
4254 ) -> Result<()> {
4255 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4256 builder.push_operands(&[ValType::I64]);
4257 instructions.push(Instruction::I64Add);
4258 Ok(())
4259 }
4260
4261 fn i64_sub(
4262 _: &mut Unstructured,
4263 module: &Module,
4264 builder: &mut CodeBuilder,
4265 instructions: &mut Vec<Instruction>,
4266 ) -> Result<()> {
4267 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4268 builder.push_operands(&[ValType::I64]);
4269 instructions.push(Instruction::I64Sub);
4270 Ok(())
4271 }
4272
4273 fn i64_mul(
4274 _: &mut Unstructured,
4275 module: &Module,
4276 builder: &mut CodeBuilder,
4277 instructions: &mut Vec<Instruction>,
4278 ) -> Result<()> {
4279 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4280 builder.push_operands(&[ValType::I64]);
4281 instructions.push(Instruction::I64Mul);
4282 Ok(())
4283 }
4284
4285 fn i64_div_s(
4286 _: &mut Unstructured,
4287 module: &Module,
4288 builder: &mut CodeBuilder,
4289 instructions: &mut Vec<Instruction>,
4290 ) -> Result<()> {
4291 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4292 builder.push_operands(&[ValType::I64]);
4293 if module.config.disallow_traps {
4294 no_traps::signed_div_rem(Instruction::I64DivS, builder, instructions);
4295 } else {
4296 instructions.push(Instruction::I64DivS);
4297 }
4298 Ok(())
4299 }
4300
4301 fn i64_div_u(
4302 _: &mut Unstructured,
4303 module: &Module,
4304 builder: &mut CodeBuilder,
4305 instructions: &mut Vec<Instruction>,
4306 ) -> Result<()> {
4307 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4308 builder.push_operands(&[ValType::I64]);
4309 if module.config.disallow_traps {
4310 no_traps::unsigned_div_rem(Instruction::I64DivU, builder, instructions);
4311 } else {
4312 instructions.push(Instruction::I64DivU);
4313 }
4314 Ok(())
4315 }
4316
4317 fn i64_rem_s(
4318 _: &mut Unstructured,
4319 module: &Module,
4320 builder: &mut CodeBuilder,
4321 instructions: &mut Vec<Instruction>,
4322 ) -> Result<()> {
4323 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4324 builder.push_operands(&[ValType::I64]);
4325 if module.config.disallow_traps {
4326 no_traps::signed_div_rem(Instruction::I64RemS, builder, instructions);
4327 } else {
4328 instructions.push(Instruction::I64RemS);
4329 }
4330 Ok(())
4331 }
4332
4333 fn i64_rem_u(
4334 _: &mut Unstructured,
4335 module: &Module,
4336 builder: &mut CodeBuilder,
4337 instructions: &mut Vec<Instruction>,
4338 ) -> Result<()> {
4339 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4340 builder.push_operands(&[ValType::I64]);
4341 if module.config.disallow_traps {
4342 no_traps::unsigned_div_rem(Instruction::I64RemU, builder, instructions);
4343 } else {
4344 instructions.push(Instruction::I64RemU);
4345 }
4346 Ok(())
4347 }
4348
4349 fn i64_and(
4350 _: &mut Unstructured,
4351 module: &Module,
4352 builder: &mut CodeBuilder,
4353 instructions: &mut Vec<Instruction>,
4354 ) -> Result<()> {
4355 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4356 builder.push_operands(&[ValType::I64]);
4357 instructions.push(Instruction::I64And);
4358 Ok(())
4359 }
4360
4361 fn i64_or(
4362 _: &mut Unstructured,
4363 module: &Module,
4364 builder: &mut CodeBuilder,
4365 instructions: &mut Vec<Instruction>,
4366 ) -> Result<()> {
4367 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4368 builder.push_operands(&[ValType::I64]);
4369 instructions.push(Instruction::I64Or);
4370 Ok(())
4371 }
4372
4373 fn i64_xor(
4374 _: &mut Unstructured,
4375 module: &Module,
4376 builder: &mut CodeBuilder,
4377 instructions: &mut Vec<Instruction>,
4378 ) -> Result<()> {
4379 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4380 builder.push_operands(&[ValType::I64]);
4381 instructions.push(Instruction::I64Xor);
4382 Ok(())
4383 }
4384
4385 fn i64_shl(
4386 _: &mut Unstructured,
4387 module: &Module,
4388 builder: &mut CodeBuilder,
4389 instructions: &mut Vec<Instruction>,
4390 ) -> Result<()> {
4391 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4392 builder.push_operands(&[ValType::I64]);
4393 instructions.push(Instruction::I64Shl);
4394 Ok(())
4395 }
4396
4397 fn i64_shr_s(
4398 _: &mut Unstructured,
4399 module: &Module,
4400 builder: &mut CodeBuilder,
4401 instructions: &mut Vec<Instruction>,
4402 ) -> Result<()> {
4403 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4404 builder.push_operands(&[ValType::I64]);
4405 instructions.push(Instruction::I64ShrS);
4406 Ok(())
4407 }
4408
4409 fn i64_shr_u(
4410 _: &mut Unstructured,
4411 module: &Module,
4412 builder: &mut CodeBuilder,
4413 instructions: &mut Vec<Instruction>,
4414 ) -> Result<()> {
4415 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4416 builder.push_operands(&[ValType::I64]);
4417 instructions.push(Instruction::I64ShrU);
4418 Ok(())
4419 }
4420
4421 fn i64_rotl(
4422 _: &mut Unstructured,
4423 module: &Module,
4424 builder: &mut CodeBuilder,
4425 instructions: &mut Vec<Instruction>,
4426 ) -> Result<()> {
4427 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4428 builder.push_operands(&[ValType::I64]);
4429 instructions.push(Instruction::I64Rotl);
4430 Ok(())
4431 }
4432
4433 fn i64_rotr(
4434 _: &mut Unstructured,
4435 module: &Module,
4436 builder: &mut CodeBuilder,
4437 instructions: &mut Vec<Instruction>,
4438 ) -> Result<()> {
4439 builder.pop_operands(module, &[ValType::I64, ValType::I64]);
4440 builder.push_operands(&[ValType::I64]);
4441 instructions.push(Instruction::I64Rotr);
4442 Ok(())
4443 }
4444
4445 #[inline]
4446 fn f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
4447 builder.types_on_stack(module, &[ValType::F32])
4448 }
4449
4450 fn f32_abs(
4451 _: &mut Unstructured,
4452 module: &Module,
4453 builder: &mut CodeBuilder,
4454 instructions: &mut Vec<Instruction>,
4455 ) -> Result<()> {
4456 builder.pop_operands(module, &[ValType::F32]);
4457 builder.push_operands(&[ValType::F32]);
4458 instructions.push(Instruction::F32Abs);
4459 Ok(())
4460 }
4461
4462 fn f32_neg(
4463 _: &mut Unstructured,
4464 module: &Module,
4465 builder: &mut CodeBuilder,
4466 instructions: &mut Vec<Instruction>,
4467 ) -> Result<()> {
4468 builder.pop_operands(module, &[ValType::F32]);
4469 builder.push_operands(&[ValType::F32]);
4470 instructions.push(Instruction::F32Neg);
4471 Ok(())
4472 }
4473
4474 fn f32_ceil(
4475 _: &mut Unstructured,
4476 module: &Module,
4477 builder: &mut CodeBuilder,
4478 instructions: &mut Vec<Instruction>,
4479 ) -> Result<()> {
4480 builder.pop_operands(module, &[ValType::F32]);
4481 builder.push_operands(&[ValType::F32]);
4482 instructions.push(Instruction::F32Ceil);
4483 Ok(())
4484 }
4485
4486 fn f32_floor(
4487 _: &mut Unstructured,
4488 module: &Module,
4489 builder: &mut CodeBuilder,
4490 instructions: &mut Vec<Instruction>,
4491 ) -> Result<()> {
4492 builder.pop_operands(module, &[ValType::F32]);
4493 builder.push_operands(&[ValType::F32]);
4494 instructions.push(Instruction::F32Floor);
4495 Ok(())
4496 }
4497
4498 fn f32_trunc(
4499 _: &mut Unstructured,
4500 module: &Module,
4501 builder: &mut CodeBuilder,
4502 instructions: &mut Vec<Instruction>,
4503 ) -> Result<()> {
4504 builder.pop_operands(module, &[ValType::F32]);
4505 builder.push_operands(&[ValType::F32]);
4506 instructions.push(Instruction::F32Trunc);
4507 Ok(())
4508 }
4509
4510 fn f32_nearest(
4511 _: &mut Unstructured,
4512 module: &Module,
4513 builder: &mut CodeBuilder,
4514 instructions: &mut Vec<Instruction>,
4515 ) -> Result<()> {
4516 builder.pop_operands(module, &[ValType::F32]);
4517 builder.push_operands(&[ValType::F32]);
4518 instructions.push(Instruction::F32Nearest);
4519 Ok(())
4520 }
4521
4522 fn f32_sqrt(
4523 _: &mut Unstructured,
4524 module: &Module,
4525 builder: &mut CodeBuilder,
4526 instructions: &mut Vec<Instruction>,
4527 ) -> Result<()> {
4528 builder.pop_operands(module, &[ValType::F32]);
4529 builder.push_operands(&[ValType::F32]);
4530 instructions.push(Instruction::F32Sqrt);
4531 Ok(())
4532 }
4533
4534 fn f32_add(
4535 _: &mut Unstructured,
4536 module: &Module,
4537 builder: &mut CodeBuilder,
4538 instructions: &mut Vec<Instruction>,
4539 ) -> Result<()> {
4540 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4541 builder.push_operands(&[ValType::F32]);
4542 instructions.push(Instruction::F32Add);
4543 Ok(())
4544 }
4545
4546 fn f32_sub(
4547 _: &mut Unstructured,
4548 module: &Module,
4549 builder: &mut CodeBuilder,
4550 instructions: &mut Vec<Instruction>,
4551 ) -> Result<()> {
4552 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4553 builder.push_operands(&[ValType::F32]);
4554 instructions.push(Instruction::F32Sub);
4555 Ok(())
4556 }
4557
4558 fn f32_mul(
4559 _: &mut Unstructured,
4560 module: &Module,
4561 builder: &mut CodeBuilder,
4562 instructions: &mut Vec<Instruction>,
4563 ) -> Result<()> {
4564 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4565 builder.push_operands(&[ValType::F32]);
4566 instructions.push(Instruction::F32Mul);
4567 Ok(())
4568 }
4569
4570 fn f32_div(
4571 _: &mut Unstructured,
4572 module: &Module,
4573 builder: &mut CodeBuilder,
4574 instructions: &mut Vec<Instruction>,
4575 ) -> Result<()> {
4576 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4577 builder.push_operands(&[ValType::F32]);
4578 instructions.push(Instruction::F32Div);
4579 Ok(())
4580 }
4581
4582 fn f32_min(
4583 _: &mut Unstructured,
4584 module: &Module,
4585 builder: &mut CodeBuilder,
4586 instructions: &mut Vec<Instruction>,
4587 ) -> Result<()> {
4588 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4589 builder.push_operands(&[ValType::F32]);
4590 instructions.push(Instruction::F32Min);
4591 Ok(())
4592 }
4593
4594 fn f32_max(
4595 _: &mut Unstructured,
4596 module: &Module,
4597 builder: &mut CodeBuilder,
4598 instructions: &mut Vec<Instruction>,
4599 ) -> Result<()> {
4600 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4601 builder.push_operands(&[ValType::F32]);
4602 instructions.push(Instruction::F32Max);
4603 Ok(())
4604 }
4605
4606 fn f32_copysign(
4607 _: &mut Unstructured,
4608 module: &Module,
4609 builder: &mut CodeBuilder,
4610 instructions: &mut Vec<Instruction>,
4611 ) -> Result<()> {
4612 builder.pop_operands(module, &[ValType::F32, ValType::F32]);
4613 builder.push_operands(&[ValType::F32]);
4614 instructions.push(Instruction::F32Copysign);
4615 Ok(())
4616 }
4617
4618 #[inline]
4619 fn f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
4620 builder.types_on_stack(module, &[ValType::F64])
4621 }
4622
4623 fn f64_abs(
4624 _: &mut Unstructured,
4625 module: &Module,
4626 builder: &mut CodeBuilder,
4627 instructions: &mut Vec<Instruction>,
4628 ) -> Result<()> {
4629 builder.pop_operands(module, &[ValType::F64]);
4630 builder.push_operands(&[ValType::F64]);
4631 instructions.push(Instruction::F64Abs);
4632 Ok(())
4633 }
4634
4635 fn f64_neg(
4636 _: &mut Unstructured,
4637 module: &Module,
4638 builder: &mut CodeBuilder,
4639 instructions: &mut Vec<Instruction>,
4640 ) -> Result<()> {
4641 builder.pop_operands(module, &[ValType::F64]);
4642 builder.push_operands(&[ValType::F64]);
4643 instructions.push(Instruction::F64Neg);
4644 Ok(())
4645 }
4646
4647 fn f64_ceil(
4648 _: &mut Unstructured,
4649 module: &Module,
4650 builder: &mut CodeBuilder,
4651 instructions: &mut Vec<Instruction>,
4652 ) -> Result<()> {
4653 builder.pop_operands(module, &[ValType::F64]);
4654 builder.push_operands(&[ValType::F64]);
4655 instructions.push(Instruction::F64Ceil);
4656 Ok(())
4657 }
4658
4659 fn f64_floor(
4660 _: &mut Unstructured,
4661 module: &Module,
4662 builder: &mut CodeBuilder,
4663 instructions: &mut Vec<Instruction>,
4664 ) -> Result<()> {
4665 builder.pop_operands(module, &[ValType::F64]);
4666 builder.push_operands(&[ValType::F64]);
4667 instructions.push(Instruction::F64Floor);
4668 Ok(())
4669 }
4670
4671 fn f64_trunc(
4672 _: &mut Unstructured,
4673 module: &Module,
4674 builder: &mut CodeBuilder,
4675 instructions: &mut Vec<Instruction>,
4676 ) -> Result<()> {
4677 builder.pop_operands(module, &[ValType::F64]);
4678 builder.push_operands(&[ValType::F64]);
4679 instructions.push(Instruction::F64Trunc);
4680 Ok(())
4681 }
4682
4683 fn f64_nearest(
4684 _: &mut Unstructured,
4685 module: &Module,
4686 builder: &mut CodeBuilder,
4687 instructions: &mut Vec<Instruction>,
4688 ) -> Result<()> {
4689 builder.pop_operands(module, &[ValType::F64]);
4690 builder.push_operands(&[ValType::F64]);
4691 instructions.push(Instruction::F64Nearest);
4692 Ok(())
4693 }
4694
4695 fn f64_sqrt(
4696 _: &mut Unstructured,
4697 module: &Module,
4698 builder: &mut CodeBuilder,
4699 instructions: &mut Vec<Instruction>,
4700 ) -> Result<()> {
4701 builder.pop_operands(module, &[ValType::F64]);
4702 builder.push_operands(&[ValType::F64]);
4703 instructions.push(Instruction::F64Sqrt);
4704 Ok(())
4705 }
4706
4707 fn f64_add(
4708 _: &mut Unstructured,
4709 module: &Module,
4710 builder: &mut CodeBuilder,
4711 instructions: &mut Vec<Instruction>,
4712 ) -> Result<()> {
4713 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4714 builder.push_operands(&[ValType::F64]);
4715 instructions.push(Instruction::F64Add);
4716 Ok(())
4717 }
4718
4719 fn f64_sub(
4720 _: &mut Unstructured,
4721 module: &Module,
4722 builder: &mut CodeBuilder,
4723 instructions: &mut Vec<Instruction>,
4724 ) -> Result<()> {
4725 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4726 builder.push_operands(&[ValType::F64]);
4727 instructions.push(Instruction::F64Sub);
4728 Ok(())
4729 }
4730
4731 fn f64_mul(
4732 _: &mut Unstructured,
4733 module: &Module,
4734 builder: &mut CodeBuilder,
4735 instructions: &mut Vec<Instruction>,
4736 ) -> Result<()> {
4737 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4738 builder.push_operands(&[ValType::F64]);
4739 instructions.push(Instruction::F64Mul);
4740 Ok(())
4741 }
4742
4743 fn f64_div(
4744 _: &mut Unstructured,
4745 module: &Module,
4746 builder: &mut CodeBuilder,
4747 instructions: &mut Vec<Instruction>,
4748 ) -> Result<()> {
4749 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4750 builder.push_operands(&[ValType::F64]);
4751 instructions.push(Instruction::F64Div);
4752 Ok(())
4753 }
4754
4755 fn f64_min(
4756 _: &mut Unstructured,
4757 module: &Module,
4758 builder: &mut CodeBuilder,
4759 instructions: &mut Vec<Instruction>,
4760 ) -> Result<()> {
4761 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4762 builder.push_operands(&[ValType::F64]);
4763 instructions.push(Instruction::F64Min);
4764 Ok(())
4765 }
4766
4767 fn f64_max(
4768 _: &mut Unstructured,
4769 module: &Module,
4770 builder: &mut CodeBuilder,
4771 instructions: &mut Vec<Instruction>,
4772 ) -> Result<()> {
4773 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4774 builder.push_operands(&[ValType::F64]);
4775 instructions.push(Instruction::F64Max);
4776 Ok(())
4777 }
4778
4779 fn f64_copysign(
4780 _: &mut Unstructured,
4781 module: &Module,
4782 builder: &mut CodeBuilder,
4783 instructions: &mut Vec<Instruction>,
4784 ) -> Result<()> {
4785 builder.pop_operands(module, &[ValType::F64, ValType::F64]);
4786 builder.push_operands(&[ValType::F64]);
4787 instructions.push(Instruction::F64Copysign);
4788 Ok(())
4789 }
4790
4791 fn i32_wrap_i64(
4792 _: &mut Unstructured,
4793 module: &Module,
4794 builder: &mut CodeBuilder,
4795 instructions: &mut Vec<Instruction>,
4796 ) -> Result<()> {
4797 builder.pop_operands(module, &[ValType::I64]);
4798 builder.push_operands(&[ValType::I32]);
4799 instructions.push(Instruction::I32WrapI64);
4800 Ok(())
4801 }
4802
4803 fn nontrapping_f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
4804 module.config.saturating_float_to_int_enabled && f32_on_stack(module, builder)
4805 }
4806
4807 fn i32_trunc_f32_s(
4808 _: &mut Unstructured,
4809 module: &Module,
4810 builder: &mut CodeBuilder,
4811 instructions: &mut Vec<Instruction>,
4812 ) -> Result<()> {
4813 builder.pop_operands(module, &[ValType::F32]);
4814 builder.push_operands(&[ValType::I32]);
4815 if module.config.disallow_traps {
4816 no_traps::trunc(Instruction::I32TruncF32S, builder, instructions);
4817 } else {
4818 instructions.push(Instruction::I32TruncF32S);
4819 }
4820 Ok(())
4821 }
4822
4823 fn i32_trunc_f32_u(
4824 _: &mut Unstructured,
4825 module: &Module,
4826 builder: &mut CodeBuilder,
4827 instructions: &mut Vec<Instruction>,
4828 ) -> Result<()> {
4829 builder.pop_operands(module, &[ValType::F32]);
4830 builder.push_operands(&[ValType::I32]);
4831 if module.config.disallow_traps {
4832 no_traps::trunc(Instruction::I32TruncF32U, builder, instructions);
4833 } else {
4834 instructions.push(Instruction::I32TruncF32U);
4835 }
4836 Ok(())
4837 }
4838
4839 fn nontrapping_f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
4840 module.config.saturating_float_to_int_enabled && f64_on_stack(module, builder)
4841 }
4842
4843 fn i32_trunc_f64_s(
4844 _: &mut Unstructured,
4845 module: &Module,
4846 builder: &mut CodeBuilder,
4847 instructions: &mut Vec<Instruction>,
4848 ) -> Result<()> {
4849 builder.pop_operands(module, &[ValType::F64]);
4850 builder.push_operands(&[ValType::I32]);
4851 if module.config.disallow_traps {
4852 no_traps::trunc(Instruction::I32TruncF64S, builder, instructions);
4853 } else {
4854 instructions.push(Instruction::I32TruncF64S);
4855 }
4856 Ok(())
4857 }
4858
4859 fn i32_trunc_f64_u(
4860 _: &mut Unstructured,
4861 module: &Module,
4862 builder: &mut CodeBuilder,
4863 instructions: &mut Vec<Instruction>,
4864 ) -> Result<()> {
4865 builder.pop_operands(module, &[ValType::F64]);
4866 builder.push_operands(&[ValType::I32]);
4867 if module.config.disallow_traps {
4868 no_traps::trunc(Instruction::I32TruncF64U, builder, instructions);
4869 } else {
4870 instructions.push(Instruction::I32TruncF64U);
4871 }
4872 Ok(())
4873 }
4874
4875 fn i64_extend_i32_s(
4876 _: &mut Unstructured,
4877 module: &Module,
4878 builder: &mut CodeBuilder,
4879 instructions: &mut Vec<Instruction>,
4880 ) -> Result<()> {
4881 builder.pop_operands(module, &[ValType::I32]);
4882 builder.push_operands(&[ValType::I64]);
4883 instructions.push(Instruction::I64ExtendI32S);
4884 Ok(())
4885 }
4886
4887 fn i64_extend_i32_u(
4888 _: &mut Unstructured,
4889 module: &Module,
4890 builder: &mut CodeBuilder,
4891 instructions: &mut Vec<Instruction>,
4892 ) -> Result<()> {
4893 builder.pop_operands(module, &[ValType::I32]);
4894 builder.push_operands(&[ValType::I64]);
4895 instructions.push(Instruction::I64ExtendI32U);
4896 Ok(())
4897 }
4898
4899 fn i64_trunc_f32_s(
4900 _: &mut Unstructured,
4901 module: &Module,
4902 builder: &mut CodeBuilder,
4903 instructions: &mut Vec<Instruction>,
4904 ) -> Result<()> {
4905 builder.pop_operands(module, &[ValType::F32]);
4906 builder.push_operands(&[ValType::I64]);
4907 if module.config.disallow_traps {
4908 no_traps::trunc(Instruction::I64TruncF32S, builder, instructions);
4909 } else {
4910 instructions.push(Instruction::I64TruncF32S);
4911 }
4912 Ok(())
4913 }
4914
4915 fn i64_trunc_f32_u(
4916 _: &mut Unstructured,
4917 module: &Module,
4918 builder: &mut CodeBuilder,
4919 instructions: &mut Vec<Instruction>,
4920 ) -> Result<()> {
4921 builder.pop_operands(module, &[ValType::F32]);
4922 builder.push_operands(&[ValType::I64]);
4923 if module.config.disallow_traps {
4924 no_traps::trunc(Instruction::I64TruncF32U, builder, instructions);
4925 } else {
4926 instructions.push(Instruction::I64TruncF32U);
4927 }
4928 Ok(())
4929 }
4930
4931 fn i64_trunc_f64_s(
4932 _: &mut Unstructured,
4933 module: &Module,
4934 builder: &mut CodeBuilder,
4935 instructions: &mut Vec<Instruction>,
4936 ) -> Result<()> {
4937 builder.pop_operands(module, &[ValType::F64]);
4938 builder.push_operands(&[ValType::I64]);
4939 if module.config.disallow_traps {
4940 no_traps::trunc(Instruction::I64TruncF64S, builder, instructions);
4941 } else {
4942 instructions.push(Instruction::I64TruncF64S);
4943 }
4944 Ok(())
4945 }
4946
4947 fn i64_trunc_f64_u(
4948 _: &mut Unstructured,
4949 module: &Module,
4950 builder: &mut CodeBuilder,
4951 instructions: &mut Vec<Instruction>,
4952 ) -> Result<()> {
4953 builder.pop_operands(module, &[ValType::F64]);
4954 builder.push_operands(&[ValType::I64]);
4955 if module.config.disallow_traps {
4956 no_traps::trunc(Instruction::I64TruncF64U, builder, instructions);
4957 } else {
4958 instructions.push(Instruction::I64TruncF64U);
4959 }
4960 Ok(())
4961 }
4962
4963 fn f32_convert_i32_s(
4964 _: &mut Unstructured,
4965 module: &Module,
4966 builder: &mut CodeBuilder,
4967 instructions: &mut Vec<Instruction>,
4968 ) -> Result<()> {
4969 builder.pop_operands(module, &[ValType::I32]);
4970 builder.push_operands(&[ValType::F32]);
4971 instructions.push(Instruction::F32ConvertI32S);
4972 Ok(())
4973 }
4974
4975 fn f32_convert_i32_u(
4976 _: &mut Unstructured,
4977 module: &Module,
4978 builder: &mut CodeBuilder,
4979 instructions: &mut Vec<Instruction>,
4980 ) -> Result<()> {
4981 builder.pop_operands(module, &[ValType::I32]);
4982 builder.push_operands(&[ValType::F32]);
4983 instructions.push(Instruction::F32ConvertI32U);
4984 Ok(())
4985 }
4986
4987 fn f32_convert_i64_s(
4988 _: &mut Unstructured,
4989 module: &Module,
4990 builder: &mut CodeBuilder,
4991 instructions: &mut Vec<Instruction>,
4992 ) -> Result<()> {
4993 builder.pop_operands(module, &[ValType::I64]);
4994 builder.push_operands(&[ValType::F32]);
4995 instructions.push(Instruction::F32ConvertI64S);
4996 Ok(())
4997 }
4998
4999 fn f32_convert_i64_u(
5000 _: &mut Unstructured,
5001 module: &Module,
5002 builder: &mut CodeBuilder,
5003 instructions: &mut Vec<Instruction>,
5004 ) -> Result<()> {
5005 builder.pop_operands(module, &[ValType::I64]);
5006 builder.push_operands(&[ValType::F32]);
5007 instructions.push(Instruction::F32ConvertI64U);
5008 Ok(())
5009 }
5010
5011 fn f32_demote_f64(
5012 _: &mut Unstructured,
5013 module: &Module,
5014 builder: &mut CodeBuilder,
5015 instructions: &mut Vec<Instruction>,
5016 ) -> Result<()> {
5017 builder.pop_operands(module, &[ValType::F64]);
5018 builder.push_operands(&[ValType::F32]);
5019 instructions.push(Instruction::F32DemoteF64);
5020 Ok(())
5021 }
5022
5023 fn f64_convert_i32_s(
5024 _: &mut Unstructured,
5025 module: &Module,
5026 builder: &mut CodeBuilder,
5027 instructions: &mut Vec<Instruction>,
5028 ) -> Result<()> {
5029 builder.pop_operands(module, &[ValType::I32]);
5030 builder.push_operands(&[ValType::F64]);
5031 instructions.push(Instruction::F64ConvertI32S);
5032 Ok(())
5033 }
5034
5035 fn f64_convert_i32_u(
5036 _: &mut Unstructured,
5037 module: &Module,
5038 builder: &mut CodeBuilder,
5039 instructions: &mut Vec<Instruction>,
5040 ) -> Result<()> {
5041 builder.pop_operands(module, &[ValType::I32]);
5042 builder.push_operands(&[ValType::F64]);
5043 instructions.push(Instruction::F64ConvertI32U);
5044 Ok(())
5045 }
5046
5047 fn f64_convert_i64_s(
5048 _: &mut Unstructured,
5049 module: &Module,
5050 builder: &mut CodeBuilder,
5051 instructions: &mut Vec<Instruction>,
5052 ) -> Result<()> {
5053 builder.pop_operands(module, &[ValType::I64]);
5054 builder.push_operands(&[ValType::F64]);
5055 instructions.push(Instruction::F64ConvertI64S);
5056 Ok(())
5057 }
5058
5059 fn f64_convert_i64_u(
5060 _: &mut Unstructured,
5061 module: &Module,
5062 builder: &mut CodeBuilder,
5063 instructions: &mut Vec<Instruction>,
5064 ) -> Result<()> {
5065 builder.pop_operands(module, &[ValType::I64]);
5066 builder.push_operands(&[ValType::F64]);
5067 instructions.push(Instruction::F64ConvertI64U);
5068 Ok(())
5069 }
5070
5071 fn f64_promote_f32(
5072 _: &mut Unstructured,
5073 module: &Module,
5074 builder: &mut CodeBuilder,
5075 instructions: &mut Vec<Instruction>,
5076 ) -> Result<()> {
5077 builder.pop_operands(module, &[ValType::F32]);
5078 builder.push_operands(&[ValType::F64]);
5079 instructions.push(Instruction::F64PromoteF32);
5080 Ok(())
5081 }
5082
5083 fn i32_reinterpret_f32(
5084 _: &mut Unstructured,
5085 module: &Module,
5086 builder: &mut CodeBuilder,
5087 instructions: &mut Vec<Instruction>,
5088 ) -> Result<()> {
5089 builder.pop_operands(module, &[ValType::F32]);
5090 builder.push_operands(&[ValType::I32]);
5091 instructions.push(Instruction::I32ReinterpretF32);
5092 Ok(())
5093 }
5094
5095 fn i64_reinterpret_f64(
5096 _: &mut Unstructured,
5097 module: &Module,
5098 builder: &mut CodeBuilder,
5099 instructions: &mut Vec<Instruction>,
5100 ) -> Result<()> {
5101 builder.pop_operands(module, &[ValType::F64]);
5102 builder.push_operands(&[ValType::I64]);
5103 instructions.push(Instruction::I64ReinterpretF64);
5104 Ok(())
5105 }
5106
5107 fn f32_reinterpret_i32(
5108 _: &mut Unstructured,
5109 module: &Module,
5110 builder: &mut CodeBuilder,
5111 instructions: &mut Vec<Instruction>,
5112 ) -> Result<()> {
5113 builder.pop_operands(module, &[ValType::I32]);
5114 builder.push_operands(&[ValType::F32]);
5115 instructions.push(Instruction::F32ReinterpretI32);
5116 Ok(())
5117 }
5118
5119 fn f64_reinterpret_i64(
5120 _: &mut Unstructured,
5121 module: &Module,
5122 builder: &mut CodeBuilder,
5123 instructions: &mut Vec<Instruction>,
5124 ) -> Result<()> {
5125 builder.pop_operands(module, &[ValType::I64]);
5126 builder.push_operands(&[ValType::F64]);
5127 instructions.push(Instruction::F64ReinterpretI64);
5128 Ok(())
5129 }
5130
5131 fn extendable_i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
5132 module.config.sign_extension_ops_enabled && i32_on_stack(module, builder)
5133 }
5134
5135 fn i32_extend_8_s(
5136 _: &mut Unstructured,
5137 module: &Module,
5138 builder: &mut CodeBuilder,
5139 instructions: &mut Vec<Instruction>,
5140 ) -> Result<()> {
5141 builder.pop_operands(module, &[ValType::I32]);
5142 builder.push_operands(&[ValType::I32]);
5143 instructions.push(Instruction::I32Extend8S);
5144 Ok(())
5145 }
5146
5147 fn i32_extend_16_s(
5148 _: &mut Unstructured,
5149 module: &Module,
5150 builder: &mut CodeBuilder,
5151 instructions: &mut Vec<Instruction>,
5152 ) -> Result<()> {
5153 builder.pop_operands(module, &[ValType::I32]);
5154 builder.push_operands(&[ValType::I32]);
5155 instructions.push(Instruction::I32Extend16S);
5156 Ok(())
5157 }
5158
5159 fn extendable_i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
5160 module.config.sign_extension_ops_enabled && i64_on_stack(module, builder)
5161 }
5162
5163 fn i64_extend_8_s(
5164 _: &mut Unstructured,
5165 module: &Module,
5166 builder: &mut CodeBuilder,
5167 instructions: &mut Vec<Instruction>,
5168 ) -> Result<()> {
5169 builder.pop_operands(module, &[ValType::I64]);
5170 builder.push_operands(&[ValType::I64]);
5171 instructions.push(Instruction::I64Extend8S);
5172 Ok(())
5173 }
5174
5175 fn i64_extend_16_s(
5176 _: &mut Unstructured,
5177 module: &Module,
5178 builder: &mut CodeBuilder,
5179 instructions: &mut Vec<Instruction>,
5180 ) -> Result<()> {
5181 builder.pop_operands(module, &[ValType::I64]);
5182 builder.push_operands(&[ValType::I64]);
5183 instructions.push(Instruction::I64Extend16S);
5184 Ok(())
5185 }
5186
5187 fn i64_extend_32_s(
5188 _: &mut Unstructured,
5189 module: &Module,
5190 builder: &mut CodeBuilder,
5191 instructions: &mut Vec<Instruction>,
5192 ) -> Result<()> {
5193 builder.pop_operands(module, &[ValType::I64]);
5194 builder.push_operands(&[ValType::I64]);
5195 instructions.push(Instruction::I64Extend32S);
5196 Ok(())
5197 }
5198
5199 fn i32_trunc_sat_f32_s(
5200 _: &mut Unstructured,
5201 module: &Module,
5202 builder: &mut CodeBuilder,
5203 instructions: &mut Vec<Instruction>,
5204 ) -> Result<()> {
5205 builder.pop_operands(module, &[ValType::F32]);
5206 builder.push_operands(&[ValType::I32]);
5207 instructions.push(Instruction::I32TruncSatF32S);
5208 Ok(())
5209 }
5210
5211 fn i32_trunc_sat_f32_u(
5212 _: &mut Unstructured,
5213 module: &Module,
5214 builder: &mut CodeBuilder,
5215 instructions: &mut Vec<Instruction>,
5216 ) -> Result<()> {
5217 builder.pop_operands(module, &[ValType::F32]);
5218 builder.push_operands(&[ValType::I32]);
5219 instructions.push(Instruction::I32TruncSatF32U);
5220 Ok(())
5221 }
5222
5223 fn i32_trunc_sat_f64_s(
5224 _: &mut Unstructured,
5225 module: &Module,
5226 builder: &mut CodeBuilder,
5227 instructions: &mut Vec<Instruction>,
5228 ) -> Result<()> {
5229 builder.pop_operands(module, &[ValType::F64]);
5230 builder.push_operands(&[ValType::I32]);
5231 instructions.push(Instruction::I32TruncSatF64S);
5232 Ok(())
5233 }
5234
5235 fn i32_trunc_sat_f64_u(
5236 _: &mut Unstructured,
5237 module: &Module,
5238 builder: &mut CodeBuilder,
5239 instructions: &mut Vec<Instruction>,
5240 ) -> Result<()> {
5241 builder.pop_operands(module, &[ValType::F64]);
5242 builder.push_operands(&[ValType::I32]);
5243 instructions.push(Instruction::I32TruncSatF64U);
5244 Ok(())
5245 }
5246
5247 fn i64_trunc_sat_f32_s(
5248 _: &mut Unstructured,
5249 module: &Module,
5250 builder: &mut CodeBuilder,
5251 instructions: &mut Vec<Instruction>,
5252 ) -> Result<()> {
5253 builder.pop_operands(module, &[ValType::F32]);
5254 builder.push_operands(&[ValType::I64]);
5255 instructions.push(Instruction::I64TruncSatF32S);
5256 Ok(())
5257 }
5258
5259 fn i64_trunc_sat_f32_u(
5260 _: &mut Unstructured,
5261 module: &Module,
5262 builder: &mut CodeBuilder,
5263 instructions: &mut Vec<Instruction>,
5264 ) -> Result<()> {
5265 builder.pop_operands(module, &[ValType::F32]);
5266 builder.push_operands(&[ValType::I64]);
5267 instructions.push(Instruction::I64TruncSatF32U);
5268 Ok(())
5269 }
5270
5271 fn i64_trunc_sat_f64_s(
5272 _: &mut Unstructured,
5273 module: &Module,
5274 builder: &mut CodeBuilder,
5275 instructions: &mut Vec<Instruction>,
5276 ) -> Result<()> {
5277 builder.pop_operands(module, &[ValType::F64]);
5278 builder.push_operands(&[ValType::I64]);
5279 instructions.push(Instruction::I64TruncSatF64S);
5280 Ok(())
5281 }
5282
5283 fn i64_trunc_sat_f64_u(
5284 _: &mut Unstructured,
5285 module: &Module,
5286 builder: &mut CodeBuilder,
5287 instructions: &mut Vec<Instruction>,
5288 ) -> Result<()> {
5289 builder.pop_operands(module, &[ValType::F64]);
5290 builder.push_operands(&[ValType::I64]);
5291 instructions.push(Instruction::I64TruncSatF64U);
5292 Ok(())
5293 }
5294
5295 fn memory_offset(u: &mut Unstructured, module: &Module, memory_index: u32) -> Result<u64> {
5296 let MemoryOffsetChoices(a, b, c) = module.config.memory_offset_choices;
5297 assert!(a + b + c != 0);
5298
5299 let memory_type = &module.memories[memory_index as usize];
5300 let min = memory_type
5301 .minimum
5302 .saturating_mul(crate::page_size(memory_type).into());
5303 let max = memory_type
5304 .maximum
5305 .map(|max| max.saturating_mul(crate::page_size(memory_type).into()))
5306 .unwrap_or(u64::MAX);
5307
5308 let (min, max, true_max) = match (memory_type.memory64, module.config.disallow_traps) {
5309 (true, false) => {
5310 // 64-bit memories can use the limits calculated above as-is
5311 (min, max, u64::MAX)
5312 }
5313 (false, false) => {
5314 // 32-bit memories can't represent a full 4gb offset, so if that's the
5315 // min/max sizes then we need to switch the m to `u32::MAX`.
5316 (
5317 u64::from(u32::try_from(min).unwrap_or(u32::MAX)),
5318 u64::from(u32::try_from(max).unwrap_or(u32::MAX)),
5319 u64::from(u32::MAX),
5320 )
5321 }
5322 // The logic for non-trapping versions of load/store involves pushing
5323 // the offset + load/store size onto the stack as either an i32 or i64
5324 // value. So even though offsets can normally be as high as u32 or u64,
5325 // we need to limit them to lower in order for our non-trapping logic to
5326 // work. 16 is the number of bytes of the largest load type (V128).
5327 (true, true) => {
5328 let no_trap_max = (i64::MAX - 16) as u64;
5329 (min.min(no_trap_max), no_trap_max, no_trap_max)
5330 }
5331 (false, true) => {
5332 let no_trap_max = (i32::MAX - 16) as u64;
5333 (min.min(no_trap_max), no_trap_max, no_trap_max)
5334 }
5335 };
5336
5337 let choice = u.int_in_range(0..=a + b + c - 1)?;
5338 if choice < a {
5339 u.int_in_range(0..=min)
5340 } else if choice < a + b {
5341 u.int_in_range(min..=max)
5342 } else {
5343 u.int_in_range(max..=true_max)
5344 }
5345 }
5346
5347 fn mem_arg(
5348 u: &mut Unstructured,
5349 module: &Module,
5350 builder: &mut CodeBuilder,
5351 alignments: &[u32],
5352 ) -> Result<MemArg> {
5353 let memory_index = if builder.type_on_stack(module, ValType::I32) {
5354 builder.pop_operands(module, &[ValType::I32]);
5355 memory_index(u, builder, ValType::I32)?
5356 } else {
5357 builder.pop_operands(module, &[ValType::I64]);
5358 memory_index(u, builder, ValType::I64)?
5359 };
5360 let offset = memory_offset(u, module, memory_index)?;
5361 let align = *u.choose(alignments)?;
5362 Ok(MemArg {
5363 memory_index,
5364 offset,
5365 align,
5366 })
5367 }
5368
5369 fn memory_index(u: &mut Unstructured, builder: &CodeBuilder, ty: ValType) -> Result<u32> {
5370 if ty == ValType::I32 {
5371 Ok(*u.choose(&builder.allocs.memory32)?)
5372 } else {
5373 Ok(*u.choose(&builder.allocs.memory64)?)
5374 }
5375 }
5376
5377 fn data_index(u: &mut Unstructured, module: &Module) -> Result<u32> {
5378 let data = module.data.len() as u32;
5379 assert!(data > 0);
5380 if data == 1 {
5381 Ok(0)
5382 } else {
5383 u.int_in_range(0..=data - 1)
5384 }
5385 }
5386
5387 #[inline]
5388 fn ref_null_valid(module: &Module, _: &mut CodeBuilder) -> bool {
5389 module.config.reference_types_enabled
5390 }
5391
5392 fn ref_null(
5393 u: &mut Unstructured,
5394 module: &Module,
5395 builder: &mut CodeBuilder,
5396 instructions: &mut Vec<Instruction>,
5397 ) -> Result<()> {
5398 let mut choices = vec![RefType::EXTERNREF, RefType::FUNCREF];
5399 if module.config.exceptions_enabled {
5400 choices.push(RefType::EXNREF);
5401 }
5402 if module.config.gc_enabled {
5403 use AbstractHeapType::*;
5404 let abs_ref_types = [Any, Eq, Array, Struct, I31, None, NoFunc, NoExtern];
5405 choices.extend(
5406 abs_ref_types
5407 .iter()
5408 .map(|&ty| RefType::new_abstract(ty, true, false)),
5409 );
5410 if module.config().shared_everything_threads_enabled {
5411 choices.extend(
5412 abs_ref_types
5413 .iter()
5414 .map(|&ty| RefType::new_abstract(ty, true, true)),
5415 );
5416 }
5417
5418 for i in 0..module.types.len() {
5419 let i = u32::try_from(i).unwrap();
5420 choices.push(RefType {
5421 nullable: true,
5422 heap_type: HeapType::Concrete(i),
5423 });
5424 }
5425 }
5426 let ty = *u.choose(&choices)?;
5427 builder.push_operand(Some(ty.into()));
5428 instructions.push(Instruction::RefNull(ty.heap_type));
5429 Ok(())
5430 }
5431
5432 #[inline]
5433 fn ref_func_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5434 module.config.reference_types_enabled && builder.allocs.referenced_functions.len() > 0
5435 }
5436
5437 fn ref_func(
5438 u: &mut Unstructured,
5439 module: &Module,
5440 builder: &mut CodeBuilder,
5441 instructions: &mut Vec<Instruction>,
5442 ) -> Result<()> {
5443 let i = *u.choose(&builder.allocs.referenced_functions)?;
5444 let ty = module.funcs[usize::try_from(i).unwrap()].0;
5445 builder.push_operand(Some(ValType::Ref(if module.config.gc_enabled {
5446 RefType {
5447 nullable: false,
5448 heap_type: HeapType::Concrete(ty),
5449 }
5450 } else {
5451 RefType::FUNCREF
5452 })));
5453 instructions.push(Instruction::RefFunc(i));
5454 Ok(())
5455 }
5456
5457 #[inline]
5458 fn ref_as_non_null_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5459 module.config.gc_enabled && builder.ref_type_on_stack().is_some()
5460 }
5461
5462 fn ref_as_non_null(
5463 u: &mut Unstructured,
5464 module: &Module,
5465 builder: &mut CodeBuilder,
5466 instructions: &mut Vec<Instruction>,
5467 ) -> Result<()> {
5468 let ref_ty = match builder.pop_ref_type() {
5469 Some(r) => r,
5470 None => module.arbitrary_ref_type(u)?,
5471 };
5472 builder.push_operand(Some(ValType::Ref(RefType {
5473 nullable: false,
5474 heap_type: ref_ty.heap_type,
5475 })));
5476 instructions.push(Instruction::RefAsNonNull);
5477 Ok(())
5478 }
5479
5480 #[inline]
5481 fn ref_eq_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5482 let eq_ref = ValType::Ref(RefType::EQREF);
5483 module.config.gc_enabled && builder.types_on_stack(module, &[eq_ref, eq_ref])
5484 }
5485
5486 fn ref_eq(
5487 _u: &mut Unstructured,
5488 _module: &Module,
5489 builder: &mut CodeBuilder,
5490 instructions: &mut Vec<Instruction>,
5491 ) -> Result<()> {
5492 builder.pop_operand();
5493 builder.pop_operand();
5494 builder.push_operand(Some(ValType::I32));
5495 instructions.push(Instruction::RefEq);
5496 Ok(())
5497 }
5498
5499 #[inline]
5500 fn ref_test_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5501 module.config.gc_enabled && builder.ref_type_on_stack().is_some()
5502 }
5503
5504 fn ref_test(
5505 u: &mut Unstructured,
5506 module: &Module,
5507 builder: &mut CodeBuilder,
5508 instructions: &mut Vec<Instruction>,
5509 ) -> Result<()> {
5510 let ref_ty = match builder.pop_ref_type() {
5511 Some(r) => r,
5512 None => module.arbitrary_ref_type(u)?,
5513 };
5514 builder.push_operand(Some(ValType::I32));
5515
5516 let sub_ty = module.arbitrary_matching_heap_type(u, ref_ty.heap_type)?;
5517 instructions.push(if !ref_ty.nullable || u.arbitrary()? {
5518 Instruction::RefTestNonNull(sub_ty)
5519 } else {
5520 Instruction::RefTestNullable(sub_ty)
5521 });
5522 Ok(())
5523 }
5524
5525 #[inline]
5526 fn ref_cast_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5527 !module.config.disallow_traps
5528 && module.config.gc_enabled
5529 && builder.ref_type_on_stack().is_some()
5530 }
5531
5532 fn ref_cast(
5533 u: &mut Unstructured,
5534 module: &Module,
5535 builder: &mut CodeBuilder,
5536 instructions: &mut Vec<Instruction>,
5537 ) -> Result<()> {
5538 let ref_ty = match builder.pop_ref_type() {
5539 Some(r) => r,
5540 None => module.arbitrary_ref_type(u)?,
5541 };
5542 let sub_ty = RefType {
5543 nullable: if !ref_ty.nullable {
5544 false
5545 } else {
5546 u.arbitrary()?
5547 },
5548 heap_type: module.arbitrary_matching_heap_type(u, ref_ty.heap_type)?,
5549 };
5550 builder.push_operand(Some(ValType::Ref(sub_ty)));
5551
5552 instructions.push(if !sub_ty.nullable {
5553 Instruction::RefCastNonNull(sub_ty.heap_type)
5554 } else {
5555 Instruction::RefCastNullable(sub_ty.heap_type)
5556 });
5557 Ok(())
5558 }
5559
5560 #[inline]
5561 fn ref_is_null_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5562 module.config.reference_types_enabled
5563 && (builder.type_on_stack(module, ValType::EXTERNREF)
5564 || builder.type_on_stack(module, ValType::FUNCREF))
5565 }
5566
5567 fn ref_is_null(
5568 _: &mut Unstructured,
5569 _module: &Module,
5570 builder: &mut CodeBuilder,
5571 instructions: &mut Vec<Instruction>,
5572 ) -> Result<()> {
5573 builder.pop_ref_type();
5574 builder.push_operands(&[ValType::I32]);
5575 instructions.push(Instruction::RefIsNull);
5576 Ok(())
5577 }
5578
5579 #[inline]
5580 fn table_fill_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5581 module.config.reference_types_enabled
5582 && module.config.bulk_memory_enabled
5583 && !module.config.disallow_traps // Non-trapping table fill generation not yet implemented
5584 && table_fill_candidates(module, builder).next().is_some()
5585 }
5586
5587 fn table_fill_candidates<'a>(
5588 module: &'a Module,
5589 builder: &'a CodeBuilder,
5590 ) -> impl Iterator<Item = u32> + 'a {
5591 module
5592 .tables
5593 .iter()
5594 .enumerate()
5595 .filter(move |(_, t)| {
5596 builder.types_on_stack(
5597 module,
5598 &[t.index_type(), t.element_type.into(), t.index_type()],
5599 )
5600 })
5601 .map(|(i, _)| i as u32)
5602 }
5603
5604 fn table_fill(
5605 u: &mut Unstructured,
5606 module: &Module,
5607 builder: &mut CodeBuilder,
5608 instructions: &mut Vec<Instruction>,
5609 ) -> Result<()> {
5610 let table = *u.choose(&table_fill_candidates(module, builder).collect::<Vec<_>>())?;
5611 let ty = &module.tables[table as usize];
5612 builder.pop_operands(
5613 module,
5614 &[ty.index_type(), ty.element_type.into(), ty.index_type()],
5615 );
5616 instructions.push(Instruction::TableFill(table));
5617 Ok(())
5618 }
5619
5620 #[inline]
5621 fn table_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5622 module.config.reference_types_enabled
5623 && !module.config.disallow_traps // Non-trapping table.set generation not yet implemented
5624 && table_set_candidates(module, builder).next().is_some()
5625 }
5626
5627 fn table_set_candidates<'a>(
5628 module: &'a Module,
5629 builder: &'a CodeBuilder,
5630 ) -> impl Iterator<Item = u32> + 'a {
5631 module
5632 .tables
5633 .iter()
5634 .enumerate()
5635 .filter(move |(_, t)| {
5636 builder.types_on_stack(module, &[t.index_type(), t.element_type.into()])
5637 })
5638 .map(|(i, _)| i as u32)
5639 }
5640
5641 fn table_set(
5642 u: &mut Unstructured,
5643 module: &Module,
5644 builder: &mut CodeBuilder,
5645 instructions: &mut Vec<Instruction>,
5646 ) -> Result<()> {
5647 let table = *u.choose(&table_set_candidates(module, builder).collect::<Vec<_>>())?;
5648 let ty = &module.tables[table as usize];
5649 builder.pop_operands(module, &[ty.index_type(), ty.element_type.into()]);
5650 instructions.push(Instruction::TableSet(table));
5651 Ok(())
5652 }
5653
5654 #[inline]
5655 fn table_get_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5656 if !module.config.reference_types_enabled {
5657 return false;
5658 }
5659 // Non-trapping table.get generation not yet implemented
5660 if module.config.disallow_traps {
5661 return false;
5662 }
5663 if builder.type_on_stack(module, ValType::I32) && builder.allocs.table32.len() > 0 {
5664 return true;
5665 }
5666 if builder.type_on_stack(module, ValType::I64) && builder.allocs.table64.len() > 0 {
5667 return true;
5668 }
5669 false
5670 }
5671
5672 fn table_get(
5673 u: &mut Unstructured,
5674 module: &Module,
5675 builder: &mut CodeBuilder,
5676 instructions: &mut Vec<Instruction>,
5677 ) -> Result<()> {
5678 let candidates = if builder.type_on_stack(module, ValType::I32) {
5679 builder.pop_operands(module, &[ValType::I32]);
5680 &builder.allocs.table32
5681 } else {
5682 builder.pop_operands(module, &[ValType::I64]);
5683 &builder.allocs.table64
5684 };
5685 let idx = *u.choose(candidates)?;
5686 let ty = module.tables[idx as usize].element_type;
5687 builder.push_operands(&[ty.into()]);
5688 instructions.push(Instruction::TableGet(idx));
5689 Ok(())
5690 }
5691
5692 #[inline]
5693 fn table_size_valid(module: &Module, _: &mut CodeBuilder) -> bool {
5694 module.config.reference_types_enabled && module.tables.len() > 0
5695 }
5696
5697 fn table_size(
5698 u: &mut Unstructured,
5699 module: &Module,
5700 builder: &mut CodeBuilder,
5701 instructions: &mut Vec<Instruction>,
5702 ) -> Result<()> {
5703 let table = u.int_in_range(0..=module.tables.len() - 1)?;
5704 let ty = &module.tables[table];
5705 builder.push_operands(&[ty.index_type()]);
5706 instructions.push(Instruction::TableSize(table as u32));
5707 Ok(())
5708 }
5709
5710 #[inline]
5711 fn table_grow_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5712 module.config.reference_types_enabled && table_grow_candidates(module, builder).next().is_some()
5713 }
5714
5715 fn table_grow_candidates<'a>(
5716 module: &'a Module,
5717 builder: &'a CodeBuilder,
5718 ) -> impl Iterator<Item = u32> + 'a {
5719 module
5720 .tables
5721 .iter()
5722 .enumerate()
5723 .filter(move |(_, t)| {
5724 builder.types_on_stack(module, &[t.element_type.into(), t.index_type()])
5725 })
5726 .map(|(i, _)| i as u32)
5727 }
5728
5729 fn table_grow(
5730 u: &mut Unstructured,
5731 module: &Module,
5732 builder: &mut CodeBuilder,
5733 instructions: &mut Vec<Instruction>,
5734 ) -> Result<()> {
5735 let table = *u.choose(&table_grow_candidates(module, builder).collect::<Vec<_>>())?;
5736 let ty = &module.tables[table as usize];
5737 builder.pop_operands(module, &[ty.element_type.into(), ty.index_type()]);
5738 builder.push_operands(&[ty.index_type()]);
5739 instructions.push(Instruction::TableGrow(table));
5740 Ok(())
5741 }
5742
5743 #[inline]
5744 fn table_copy_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5745 if !module.config.bulk_memory_enabled {
5746 return false;
5747 }
5748 // Non-trapping table.copy generation not yet implemented
5749 if module.config.disallow_traps {
5750 return false;
5751 }
5752 if builder.types_on_stack(module, &[ValType::I64, ValType::I64, ValType::I64]) {
5753 return builder.allocs.table_copy_64_to_64.len() > 0;
5754 }
5755 if builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32]) {
5756 return builder.allocs.table_copy_32_to_32.len() > 0;
5757 }
5758 if builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32]) {
5759 return builder.allocs.table_copy_32_to_64.len() > 0;
5760 }
5761 if builder.types_on_stack(module, &[ValType::I32, ValType::I64, ValType::I32]) {
5762 return builder.allocs.table_copy_64_to_32.len() > 0;
5763 }
5764 false
5765 }
5766
5767 fn table_copy(
5768 u: &mut Unstructured,
5769 module: &Module,
5770 builder: &mut CodeBuilder,
5771 instructions: &mut Vec<Instruction>,
5772 ) -> Result<()> {
5773 use CopyIndexSize::*;
5774
5775 let (src_table, dst_table) = match gen_copy_src_and_dst(module, builder) {
5776 (I32, I32) => *u.choose(&builder.allocs.table_copy_32_to_32)?,
5777 (I32, I64) => *u.choose(&builder.allocs.table_copy_32_to_64)?,
5778 (I64, I32) => *u.choose(&builder.allocs.table_copy_64_to_32)?,
5779 (I64, I64) => *u.choose(&builder.allocs.table_copy_64_to_64)?,
5780 };
5781 instructions.push(Instruction::TableCopy {
5782 src_table,
5783 dst_table,
5784 });
5785 Ok(())
5786 }
5787
5788 #[inline]
5789 fn table_init_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5790 if !module.config.bulk_memory_enabled {
5791 return false;
5792 }
5793 // Non-trapping table.init generation not yet implemented.
5794 if module.config.disallow_traps {
5795 return false;
5796 }
5797 if builder.allocs.table32_init.len() > 0
5798 && builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
5799 {
5800 return true;
5801 }
5802 if builder.allocs.table64_init.len() > 0
5803 && builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32])
5804 {
5805 return true;
5806 }
5807 false
5808 }
5809
5810 fn table_init(
5811 u: &mut Unstructured,
5812 module: &Module,
5813 builder: &mut CodeBuilder,
5814 instructions: &mut Vec<Instruction>,
5815 ) -> Result<()> {
5816 let candidates = if builder.types_on_stack(module, &[ValType::I64, ValType::I32, ValType::I32])
5817 {
5818 builder.pop_operands(module, &[ValType::I64, ValType::I32, ValType::I32]);
5819 &builder.allocs.table64_init
5820 } else {
5821 builder.pop_operands(module, &[ValType::I32, ValType::I32, ValType::I32]);
5822 &builder.allocs.table32_init
5823 };
5824 let (elem_index, table) = *u.choose(&candidates)?;
5825 instructions.push(Instruction::TableInit { elem_index, table });
5826 Ok(())
5827 }
5828
5829 #[inline]
5830 fn elem_drop_valid(module: &Module, _builder: &mut CodeBuilder) -> bool {
5831 module.config.bulk_memory_enabled && module.elems.len() > 0
5832 }
5833
5834 fn elem_drop(
5835 u: &mut Unstructured,
5836 module: &Module,
5837 _builder: &mut CodeBuilder,
5838 instructions: &mut Vec<Instruction>,
5839 ) -> Result<()> {
5840 let segment = u.int_in_range(0..=module.elems.len() - 1)? as u32;
5841 instructions.push(Instruction::ElemDrop(segment));
5842 Ok(())
5843 }
5844
5845 #[inline]
5846 fn struct_new_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5847 module.config.gc_enabled
5848 && module
5849 .struct_types
5850 .iter()
5851 .copied()
5852 .any(|i| builder.field_types_on_stack(module, &module.ty(i).unwrap_struct().fields))
5853 }
5854
5855 fn struct_new(
5856 u: &mut Unstructured,
5857 module: &Module,
5858 builder: &mut CodeBuilder,
5859 instructions: &mut Vec<Instruction>,
5860 ) -> Result<()> {
5861 let n = module
5862 .struct_types
5863 .iter()
5864 .filter(|i| builder.field_types_on_stack(module, &module.ty(**i).unwrap_struct().fields))
5865 .count();
5866 debug_assert!(n > 0);
5867 let i = u.int_in_range(0..=n - 1)?;
5868 let ty = module
5869 .struct_types
5870 .iter()
5871 .copied()
5872 .filter(|i| builder.field_types_on_stack(module, &module.ty(*i).unwrap_struct().fields))
5873 .nth(i)
5874 .unwrap();
5875
5876 for _ in module.ty(ty).unwrap_struct().fields.iter() {
5877 builder.pop_operand();
5878 }
5879 builder.push_operand(Some(ValType::Ref(RefType {
5880 nullable: false,
5881 heap_type: HeapType::Concrete(ty),
5882 })));
5883
5884 instructions.push(Instruction::StructNew(ty));
5885 Ok(())
5886 }
5887
5888 #[inline]
5889 fn struct_new_default_valid(module: &Module, _builder: &mut CodeBuilder) -> bool {
5890 module.config.gc_enabled
5891 && module.struct_types.iter().copied().any(|i| {
5892 module
5893 .ty(i)
5894 .unwrap_struct()
5895 .fields
5896 .iter()
5897 .all(|f| f.element_type.is_defaultable())
5898 })
5899 }
5900
5901 fn struct_new_default(
5902 u: &mut Unstructured,
5903 module: &Module,
5904 builder: &mut CodeBuilder,
5905 instructions: &mut Vec<Instruction>,
5906 ) -> Result<()> {
5907 let n = module
5908 .struct_types
5909 .iter()
5910 .filter(|i| {
5911 module
5912 .ty(**i)
5913 .unwrap_struct()
5914 .fields
5915 .iter()
5916 .all(|f| f.element_type.is_defaultable())
5917 })
5918 .count();
5919 debug_assert!(n > 0);
5920 let i = u.int_in_range(0..=n - 1)?;
5921 let ty = module
5922 .struct_types
5923 .iter()
5924 .copied()
5925 .filter(|i| {
5926 module
5927 .ty(*i)
5928 .unwrap_struct()
5929 .fields
5930 .iter()
5931 .all(|f| f.element_type.is_defaultable())
5932 })
5933 .nth(i)
5934 .unwrap();
5935
5936 builder.push_operand(Some(ValType::Ref(RefType {
5937 nullable: false,
5938 heap_type: HeapType::Concrete(ty),
5939 })));
5940
5941 instructions.push(Instruction::StructNewDefault(ty));
5942 Ok(())
5943 }
5944
5945 #[inline]
5946 fn struct_get_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5947 module.config.gc_enabled
5948 && !module.config.disallow_traps
5949 && builder.non_empty_struct_ref_on_stack(module, !module.config.disallow_traps)
5950 }
5951
5952 fn struct_get(
5953 u: &mut Unstructured,
5954 module: &Module,
5955 builder: &mut CodeBuilder,
5956 instructions: &mut Vec<Instruction>,
5957 ) -> Result<()> {
5958 let (_, struct_type_index) = builder.pop_concrete_ref_type();
5959 let struct_ty = module.ty(struct_type_index).unwrap_struct();
5960 let num_fields = u32::try_from(struct_ty.fields.len()).unwrap();
5961 debug_assert!(num_fields > 0);
5962 let field_index = u.int_in_range(0..=num_fields - 1)?;
5963 let (val_ty, ext) = match struct_ty.fields[usize::try_from(field_index).unwrap()].element_type {
5964 StorageType::I8 | StorageType::I16 => (ValType::I32, Some(u.arbitrary()?)),
5965 StorageType::Val(v) => (v, None),
5966 };
5967 builder.push_operand(Some(val_ty));
5968 instructions.push(match ext {
5969 None => Instruction::StructGet {
5970 struct_type_index,
5971 field_index,
5972 },
5973 Some(true) => Instruction::StructGetS {
5974 struct_type_index,
5975 field_index,
5976 },
5977 Some(false) => Instruction::StructGetU {
5978 struct_type_index,
5979 field_index,
5980 },
5981 });
5982 Ok(())
5983 }
5984
5985 #[inline]
5986 fn struct_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
5987 if !module.config.gc_enabled {
5988 return false;
5989 }
5990 match builder.concrete_struct_ref_type_on_stack_at(module, 1) {
5991 None => return false,
5992 Some((true, _, _)) if module.config.disallow_traps => return false,
5993 Some((_, _, ty)) => ty
5994 .fields
5995 .iter()
5996 .any(|f| f.mutable && builder.type_on_stack(module, f.element_type.unpack())),
5997 }
5998 }
5999
6000 fn struct_set(
6001 u: &mut Unstructured,
6002 module: &Module,
6003 builder: &mut CodeBuilder,
6004 instructions: &mut Vec<Instruction>,
6005 ) -> Result<()> {
6006 let val_ty = builder.pop_operand();
6007 let (_, struct_type_index) = builder.pop_concrete_ref_type();
6008 let struct_ty = module.ty(struct_type_index).unwrap_struct();
6009
6010 let valid_field = |f: &FieldType| -> bool {
6011 match val_ty {
6012 None => f.mutable,
6013 Some(val_ty) => {
6014 f.mutable && module.val_type_is_sub_type(val_ty, f.element_type.unpack())
6015 }
6016 }
6017 };
6018
6019 let n = struct_ty.fields.iter().filter(|f| valid_field(f)).count();
6020 debug_assert!(n > 0);
6021 let i = u.int_in_range(0..=n - 1)?;
6022 let (field_index, _) = struct_ty
6023 .fields
6024 .iter()
6025 .enumerate()
6026 .filter(|(_, f)| valid_field(f))
6027 .nth(i)
6028 .unwrap();
6029 let field_index = u32::try_from(field_index).unwrap();
6030
6031 instructions.push(Instruction::StructSet {
6032 struct_type_index,
6033 field_index,
6034 });
6035 Ok(())
6036 }
6037
6038 #[inline]
6039 fn array_new_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6040 module.config.gc_enabled
6041 && builder.type_on_stack(module, ValType::I32)
6042 && module
6043 .array_types
6044 .iter()
6045 .any(|i| builder.field_type_on_stack_at(module, 1, module.ty(*i).unwrap_array().0))
6046 }
6047
6048 fn array_new(
6049 u: &mut Unstructured,
6050 module: &Module,
6051 builder: &mut CodeBuilder,
6052 instructions: &mut Vec<Instruction>,
6053 ) -> Result<()> {
6054 let n = module
6055 .array_types
6056 .iter()
6057 .filter(|i| builder.field_type_on_stack_at(module, 1, module.ty(**i).unwrap_array().0))
6058 .count();
6059 debug_assert!(n > 0);
6060 let i = u.int_in_range(0..=n - 1)?;
6061 let ty = module
6062 .array_types
6063 .iter()
6064 .copied()
6065 .filter(|i| builder.field_type_on_stack_at(module, 1, module.ty(*i).unwrap_array().0))
6066 .nth(i)
6067 .unwrap();
6068
6069 builder.pop_operand();
6070 builder.pop_operand();
6071 builder.push_operand(Some(ValType::Ref(RefType {
6072 nullable: false,
6073 heap_type: HeapType::Concrete(ty),
6074 })));
6075
6076 instructions.push(Instruction::ArrayNew(ty));
6077 Ok(())
6078 }
6079
6080 #[inline]
6081 fn array_new_fixed_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6082 module.config.gc_enabled
6083 && module
6084 .array_types
6085 .iter()
6086 .any(|i| builder.field_type_on_stack(module, module.ty(*i).unwrap_array().0))
6087 }
6088
6089 fn array_new_fixed(
6090 u: &mut Unstructured,
6091 module: &Module,
6092 builder: &mut CodeBuilder,
6093 instructions: &mut Vec<Instruction>,
6094 ) -> Result<()> {
6095 let n = module
6096 .array_types
6097 .iter()
6098 .filter(|i| builder.field_type_on_stack(module, module.ty(**i).unwrap_array().0))
6099 .count();
6100 debug_assert!(n > 0);
6101 let i = u.int_in_range(0..=n - 1)?;
6102 let array_type_index = module
6103 .array_types
6104 .iter()
6105 .copied()
6106 .filter(|i| builder.field_type_on_stack(module, module.ty(*i).unwrap_array().0))
6107 .nth(i)
6108 .unwrap();
6109 let elem_ty = module
6110 .ty(array_type_index)
6111 .unwrap_array()
6112 .0
6113 .element_type
6114 .unpack();
6115
6116 let m = (0..builder.operands().len())
6117 .take_while(|i| builder.type_on_stack_at(module, *i, elem_ty))
6118 .count();
6119 debug_assert!(m > 0);
6120 let array_size = u.int_in_range(0..=m - 1)?;
6121 let array_size = u32::try_from(array_size).unwrap();
6122
6123 for _ in 0..array_size {
6124 builder.pop_operand();
6125 }
6126 builder.push_operand(Some(ValType::Ref(RefType {
6127 nullable: false,
6128 heap_type: HeapType::Concrete(array_type_index),
6129 })));
6130
6131 instructions.push(Instruction::ArrayNewFixed {
6132 array_type_index,
6133 array_size,
6134 });
6135 Ok(())
6136 }
6137
6138 #[inline]
6139 fn array_new_default_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6140 module.config.gc_enabled
6141 && builder.type_on_stack(module, ValType::I32)
6142 && module
6143 .array_types
6144 .iter()
6145 .any(|i| module.ty(*i).unwrap_array().0.element_type.is_defaultable())
6146 }
6147
6148 fn array_new_default(
6149 u: &mut Unstructured,
6150 module: &Module,
6151 builder: &mut CodeBuilder,
6152 instructions: &mut Vec<Instruction>,
6153 ) -> Result<()> {
6154 let n = module
6155 .array_types
6156 .iter()
6157 .filter(|i| {
6158 module
6159 .ty(**i)
6160 .unwrap_array()
6161 .0
6162 .element_type
6163 .is_defaultable()
6164 })
6165 .count();
6166 debug_assert!(n > 0);
6167 let i = u.int_in_range(0..=n - 1)?;
6168 let array_type_index = module
6169 .array_types
6170 .iter()
6171 .copied()
6172 .filter(|i| module.ty(*i).unwrap_array().0.element_type.is_defaultable())
6173 .nth(i)
6174 .unwrap();
6175
6176 builder.pop_operand();
6177 builder.push_operand(Some(ValType::Ref(RefType {
6178 nullable: false,
6179 heap_type: HeapType::Concrete(array_type_index),
6180 })));
6181 instructions.push(Instruction::ArrayNewDefault(array_type_index));
6182 Ok(())
6183 }
6184
6185 #[inline]
6186 fn array_new_data_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6187 module.config.gc_enabled
6188 && module.config.bulk_memory_enabled // Requires data count section
6189 && !module.config.disallow_traps
6190 && !module.data.is_empty()
6191 && builder.types_on_stack(module, &[ValType::I32, ValType::I32])
6192 && module.array_types.iter().any(|i| {
6193 let ty = module.ty(*i).unwrap_array().0.element_type.unpack();
6194 ty.is_numeric() | ty.is_vector()
6195 })
6196 }
6197
6198 fn array_new_data(
6199 u: &mut Unstructured,
6200 module: &Module,
6201 builder: &mut CodeBuilder,
6202 instructions: &mut Vec<Instruction>,
6203 ) -> Result<()> {
6204 let n = module
6205 .array_types
6206 .iter()
6207 .filter(|i| {
6208 let ty = module.ty(**i).unwrap_array().0.element_type.unpack();
6209 ty.is_numeric() | ty.is_vector()
6210 })
6211 .count();
6212 debug_assert!(n > 0);
6213 let i = u.int_in_range(0..=n - 1)?;
6214 let array_type_index = module
6215 .array_types
6216 .iter()
6217 .copied()
6218 .filter(|i| {
6219 let ty = module.ty(*i).unwrap_array().0.element_type.unpack();
6220 ty.is_numeric() | ty.is_vector()
6221 })
6222 .nth(i)
6223 .unwrap();
6224
6225 let m = module.data.len();
6226 debug_assert!(m > 0);
6227 let array_data_index = u.int_in_range(0..=m - 1)?;
6228 let array_data_index = u32::try_from(array_data_index).unwrap();
6229
6230 builder.pop_operand();
6231 builder.pop_operand();
6232 builder.push_operand(Some(ValType::Ref(RefType {
6233 nullable: false,
6234 heap_type: HeapType::Concrete(array_type_index),
6235 })));
6236 instructions.push(Instruction::ArrayNewData {
6237 array_type_index,
6238 array_data_index,
6239 });
6240 Ok(())
6241 }
6242
6243 fn module_has_elem_segment_of_array_type(module: &Module, ty: &ArrayType) -> bool {
6244 module
6245 .elems
6246 .iter()
6247 .any(|elem| module.val_type_is_sub_type(ValType::Ref(elem.ty), ty.0.element_type.unpack()))
6248 }
6249
6250 #[inline]
6251 fn array_new_elem_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6252 module.config.gc_enabled
6253 && !module.config.disallow_traps
6254 && builder.types_on_stack(module, &[ValType::I32, ValType::I32])
6255 && module
6256 .array_types
6257 .iter()
6258 .any(|i| module_has_elem_segment_of_array_type(module, module.ty(*i).unwrap_array()))
6259 }
6260
6261 fn array_new_elem(
6262 u: &mut Unstructured,
6263 module: &Module,
6264 builder: &mut CodeBuilder,
6265 instructions: &mut Vec<Instruction>,
6266 ) -> Result<()> {
6267 let n = module
6268 .array_types
6269 .iter()
6270 .filter(|i| module_has_elem_segment_of_array_type(module, module.ty(**i).unwrap_array()))
6271 .count();
6272 debug_assert!(n > 0);
6273 let i = u.int_in_range(0..=n - 1)?;
6274 let array_type_index = module
6275 .array_types
6276 .iter()
6277 .copied()
6278 .filter(|i| module_has_elem_segment_of_array_type(module, module.ty(*i).unwrap_array()))
6279 .nth(i)
6280 .unwrap();
6281 let elem_ty = module
6282 .ty(array_type_index)
6283 .unwrap_array()
6284 .0
6285 .element_type
6286 .unpack();
6287
6288 let m = module
6289 .elems
6290 .iter()
6291 .filter(|elem| module.val_type_is_sub_type(ValType::Ref(elem.ty), elem_ty))
6292 .count();
6293 debug_assert!(m > 0);
6294 let j = u.int_in_range(0..=m - 1)?;
6295 let (array_elem_index, _) = module
6296 .elems
6297 .iter()
6298 .enumerate()
6299 .filter(|(_, elem)| module.val_type_is_sub_type(ValType::Ref(elem.ty), elem_ty))
6300 .nth(j)
6301 .unwrap();
6302 let array_elem_index = u32::try_from(array_elem_index).unwrap();
6303
6304 builder.pop_operand();
6305 builder.pop_operand();
6306 builder.push_operand(Some(ValType::Ref(RefType {
6307 nullable: false,
6308 heap_type: HeapType::Concrete(array_type_index),
6309 })));
6310
6311 instructions.push(Instruction::ArrayNewElem {
6312 array_type_index,
6313 array_elem_index,
6314 });
6315 Ok(())
6316 }
6317
6318 #[inline]
6319 fn array_get_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6320 module.config.gc_enabled
6321 && !module.config.disallow_traps // TODO: add support for disallowing traps
6322 && builder.type_on_stack(module, ValType::I32)
6323 && builder.concrete_array_ref_type_on_stack_at(module, 1).is_some()
6324 }
6325
6326 fn array_get(
6327 u: &mut Unstructured,
6328 module: &Module,
6329 builder: &mut CodeBuilder,
6330 instructions: &mut Vec<Instruction>,
6331 ) -> Result<()> {
6332 builder.pop_operand();
6333 let (_, array_type_index) = builder.pop_concrete_ref_type();
6334 let elem_ty = module.ty(array_type_index).unwrap_array().0.element_type;
6335 builder.push_operand(Some(elem_ty.unpack()));
6336 instructions.push(match elem_ty {
6337 StorageType::I8 | StorageType::I16 => {
6338 if u.arbitrary()? {
6339 Instruction::ArrayGetS(array_type_index)
6340 } else {
6341 Instruction::ArrayGetU(array_type_index)
6342 }
6343 }
6344 StorageType::Val(_) => Instruction::ArrayGet(array_type_index),
6345 });
6346 Ok(())
6347 }
6348
6349 #[inline]
6350 fn array_set_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6351 if !module.config.gc_enabled
6352 // TODO: implement disallowing traps.
6353 || module.config.disallow_traps
6354 || !builder.type_on_stack_at(module, 1, ValType::I32)
6355 {
6356 return false;
6357 }
6358 match builder.concrete_array_ref_type_on_stack_at(module, 2) {
6359 None => false,
6360 Some((_nullable, _idx, array_ty)) => {
6361 array_ty.0.mutable && builder.field_type_on_stack(module, array_ty.0)
6362 }
6363 }
6364 }
6365
6366 fn array_set(
6367 _u: &mut Unstructured,
6368 _module: &Module,
6369 builder: &mut CodeBuilder,
6370 instructions: &mut Vec<Instruction>,
6371 ) -> Result<()> {
6372 builder.pop_operand();
6373 builder.pop_operand();
6374 let (_, ty) = builder.pop_concrete_ref_type();
6375 instructions.push(Instruction::ArraySet(ty));
6376 Ok(())
6377 }
6378
6379 #[inline]
6380 fn array_len_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6381 module.config.gc_enabled && builder.type_on_stack(module, ValType::Ref(RefType::ARRAYREF))
6382 }
6383
6384 fn array_len(
6385 _u: &mut Unstructured,
6386 _module: &Module,
6387 builder: &mut CodeBuilder,
6388 instructions: &mut Vec<Instruction>,
6389 ) -> Result<()> {
6390 builder.pop_operand();
6391 builder.push_operand(Some(ValType::I32));
6392 instructions.push(Instruction::ArrayLen);
6393 Ok(())
6394 }
6395
6396 #[inline]
6397 fn array_fill_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6398 if !module.config.gc_enabled
6399 // TODO: add support for disallowing traps
6400 || module.config.disallow_traps
6401 || !builder.type_on_stack_at(module, 0, ValType::I32)
6402 || !builder.type_on_stack_at(module, 2, ValType::I32)
6403 {
6404 return false;
6405 }
6406 match builder.concrete_array_ref_type_on_stack_at(module, 3) {
6407 None => return false,
6408 Some((_, _, array_ty)) => {
6409 array_ty.0.mutable && builder.field_type_on_stack_at(module, 1, array_ty.0)
6410 }
6411 }
6412 }
6413
6414 fn array_fill(
6415 _u: &mut Unstructured,
6416 _module: &Module,
6417 builder: &mut CodeBuilder,
6418 instructions: &mut Vec<Instruction>,
6419 ) -> Result<()> {
6420 builder.pop_operand();
6421 builder.pop_operand();
6422 builder.pop_operand();
6423 let (_, ty) = builder.pop_concrete_ref_type();
6424 instructions.push(Instruction::ArrayFill(ty));
6425 Ok(())
6426 }
6427
6428 #[inline]
6429 fn array_copy_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6430 if !module.config.gc_enabled
6431 // TODO: add support for disallowing traps
6432 || module.config.disallow_traps
6433 || !builder.type_on_stack_at(module, 0, ValType::I32)
6434 || !builder.type_on_stack_at(module, 1, ValType::I32)
6435 || !builder.type_on_stack_at(module, 3, ValType::I32)
6436 {
6437 return false;
6438 }
6439 let x = match builder.concrete_array_ref_type_on_stack_at(module, 4) {
6440 None => return false,
6441 Some((_, _, x)) => x,
6442 };
6443 if !x.0.mutable {
6444 return false;
6445 }
6446 let y = match builder.concrete_array_ref_type_on_stack_at(module, 2) {
6447 None => return false,
6448 Some((_, _, y)) => y,
6449 };
6450 match (x.0.element_type, y.0.element_type) {
6451 (StorageType::I8, StorageType::I8) => true,
6452 (StorageType::I8, _) => false,
6453 (StorageType::I16, StorageType::I16) => true,
6454 (StorageType::I16, _) => false,
6455 (StorageType::Val(x), StorageType::Val(y)) => module.val_type_is_sub_type(y, x),
6456 (StorageType::Val(_), _) => false,
6457 }
6458 }
6459
6460 fn array_copy(
6461 _u: &mut Unstructured,
6462 _module: &Module,
6463 builder: &mut CodeBuilder,
6464 instructions: &mut Vec<Instruction>,
6465 ) -> Result<()> {
6466 builder.pop_operand();
6467 builder.pop_operand();
6468 let (_, array_type_index_src) = builder.pop_concrete_ref_type();
6469 builder.pop_operand();
6470 let (_, array_type_index_dst) = builder.pop_concrete_ref_type();
6471 instructions.push(Instruction::ArrayCopy {
6472 array_type_index_dst,
6473 array_type_index_src,
6474 });
6475 Ok(())
6476 }
6477
6478 #[inline]
6479 fn array_init_data_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6480 if !module.config.gc_enabled
6481 || !module.config.bulk_memory_enabled // Requires data count section
6482 || module.config.disallow_traps
6483 || module.data.is_empty()
6484 || !builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
6485 {
6486 return false;
6487 }
6488 match builder.concrete_array_ref_type_on_stack_at(module, 3) {
6489 None => return false,
6490 Some((_, _, ty)) => {
6491 let elem_ty = ty.0.element_type.unpack();
6492 ty.0.mutable && (elem_ty.is_numeric() || elem_ty.is_vector())
6493 }
6494 }
6495 }
6496
6497 fn array_init_data(
6498 u: &mut Unstructured,
6499 module: &Module,
6500 builder: &mut CodeBuilder,
6501 instructions: &mut Vec<Instruction>,
6502 ) -> Result<()> {
6503 builder.pop_operand();
6504 builder.pop_operand();
6505 builder.pop_operand();
6506 let (_, array_type_index) = builder.pop_concrete_ref_type();
6507
6508 let n = module.data.len();
6509 debug_assert!(n > 0);
6510 let array_data_index = u.int_in_range(0..=n - 1)?;
6511 let array_data_index = u32::try_from(array_data_index).unwrap();
6512
6513 instructions.push(Instruction::ArrayInitData {
6514 array_type_index,
6515 array_data_index,
6516 });
6517 Ok(())
6518 }
6519
6520 #[inline]
6521 fn array_init_elem_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6522 if !module.config.gc_enabled
6523 || module.config.disallow_traps
6524 || !builder.types_on_stack(module, &[ValType::I32, ValType::I32, ValType::I32])
6525 {
6526 return false;
6527 }
6528 match builder.concrete_array_ref_type_on_stack_at(module, 3) {
6529 None => return false,
6530 Some((_, _, array_ty)) => {
6531 array_ty.0.mutable && module_has_elem_segment_of_array_type(module, &array_ty)
6532 }
6533 }
6534 }
6535
6536 fn array_init_elem(
6537 u: &mut Unstructured,
6538 module: &Module,
6539 builder: &mut CodeBuilder,
6540 instructions: &mut Vec<Instruction>,
6541 ) -> Result<()> {
6542 builder.pop_operand();
6543 builder.pop_operand();
6544 builder.pop_operand();
6545 let (_, array_type_index) = builder.pop_concrete_ref_type();
6546
6547 let elem_ty = module
6548 .ty(array_type_index)
6549 .unwrap_array()
6550 .0
6551 .element_type
6552 .unpack();
6553
6554 let n = module
6555 .elems
6556 .iter()
6557 .filter(|elem| module.val_type_is_sub_type(ValType::Ref(elem.ty), elem_ty))
6558 .count();
6559 debug_assert!(n > 0);
6560 let j = u.int_in_range(0..=n - 1)?;
6561 let (array_elem_index, _) = module
6562 .elems
6563 .iter()
6564 .enumerate()
6565 .filter(|(_, elem)| module.val_type_is_sub_type(ValType::Ref(elem.ty), elem_ty))
6566 .nth(j)
6567 .unwrap();
6568 let array_elem_index = u32::try_from(array_elem_index).unwrap();
6569
6570 instructions.push(Instruction::ArrayInitElem {
6571 array_type_index,
6572 array_elem_index,
6573 });
6574 Ok(())
6575 }
6576
6577 #[inline]
6578 fn ref_i31_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6579 module.config.gc_enabled && builder.type_on_stack(module, ValType::I32)
6580 }
6581
6582 fn ref_i31(
6583 _u: &mut Unstructured,
6584 _module: &Module,
6585 builder: &mut CodeBuilder,
6586 instructions: &mut Vec<Instruction>,
6587 ) -> Result<()> {
6588 builder.pop_operand();
6589 builder.push_operand(Some(ValType::Ref(RefType::I31REF)));
6590 instructions.push(Instruction::RefI31);
6591 Ok(())
6592 }
6593
6594 #[inline]
6595 fn i31_get_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6596 module.config.gc_enabled
6597 && builder.type_on_stack(
6598 module,
6599 ValType::Ref(RefType {
6600 nullable: true,
6601 heap_type: HeapType::I31,
6602 }),
6603 )
6604 }
6605
6606 fn i31_get(
6607 u: &mut Unstructured,
6608 _module: &Module,
6609 builder: &mut CodeBuilder,
6610 instructions: &mut Vec<Instruction>,
6611 ) -> Result<()> {
6612 builder.pop_operand();
6613 builder.push_operand(Some(ValType::I32));
6614 instructions.push(if u.arbitrary()? {
6615 Instruction::I31GetS
6616 } else {
6617 Instruction::I31GetU
6618 });
6619 Ok(())
6620 }
6621
6622 #[inline]
6623 fn any_convert_extern_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6624 module.config.gc_enabled
6625 && builder.type_on_stack(
6626 module,
6627 ValType::Ref(RefType {
6628 nullable: true,
6629 heap_type: HeapType::EXTERN,
6630 }),
6631 )
6632 }
6633
6634 fn any_convert_extern(
6635 u: &mut Unstructured,
6636 _module: &Module,
6637 builder: &mut CodeBuilder,
6638 instructions: &mut Vec<Instruction>,
6639 ) -> Result<()> {
6640 let nullable = match builder.pop_ref_type() {
6641 None => u.arbitrary()?,
6642 Some(r) => r.nullable,
6643 };
6644 builder.push_operand(Some(ValType::Ref(RefType {
6645 nullable,
6646 heap_type: HeapType::ANY,
6647 })));
6648 instructions.push(Instruction::AnyConvertExtern);
6649 Ok(())
6650 }
6651
6652 #[inline]
6653 fn extern_convert_any_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6654 module.config.gc_enabled && builder.type_on_stack(module, ValType::Ref(RefType::ANYREF))
6655 }
6656
6657 fn extern_convert_any(
6658 u: &mut Unstructured,
6659 _module: &Module,
6660 builder: &mut CodeBuilder,
6661 instructions: &mut Vec<Instruction>,
6662 ) -> Result<()> {
6663 let nullable = match builder.pop_ref_type() {
6664 None => u.arbitrary()?,
6665 Some(r) => r.nullable,
6666 };
6667 let ty = if nullable {
6668 RefType::EXTERNREF.nullable(true)
6669 } else {
6670 RefType::EXTERNREF
6671 };
6672 builder.push_operand(Some(ValType::Ref(ty)));
6673 instructions.push(Instruction::ExternConvertAny);
6674 Ok(())
6675 }
6676
6677 fn lane_index(u: &mut Unstructured, number_of_lanes: u8) -> Result<u8> {
6678 u.int_in_range(0..=(number_of_lanes - 1))
6679 }
6680
6681 #[inline]
6682 fn simd_v128_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6683 !module.config.disallow_traps
6684 && module.config.simd_enabled
6685 && builder.types_on_stack(module, &[ValType::V128])
6686 }
6687
6688 #[inline]
6689 fn simd_v128_on_stack_relaxed(module: &Module, builder: &mut CodeBuilder) -> bool {
6690 !module.config.disallow_traps
6691 && module.config.relaxed_simd_enabled
6692 && builder.types_on_stack(module, &[ValType::V128])
6693 }
6694
6695 #[inline]
6696 fn simd_v128_v128_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6697 !module.config.disallow_traps
6698 && module.config.simd_enabled
6699 && builder.types_on_stack(module, &[ValType::V128, ValType::V128])
6700 }
6701
6702 #[inline]
6703 fn simd_v128_v128_on_stack_relaxed(module: &Module, builder: &mut CodeBuilder) -> bool {
6704 !module.config.disallow_traps
6705 && module.config.relaxed_simd_enabled
6706 && builder.types_on_stack(module, &[ValType::V128, ValType::V128])
6707 }
6708
6709 #[inline]
6710 fn simd_v128_v128_v128_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6711 !module.config.disallow_traps
6712 && module.config.simd_enabled
6713 && builder.types_on_stack(module, &[ValType::V128, ValType::V128, ValType::V128])
6714 }
6715
6716 #[inline]
6717 fn simd_v128_v128_v128_on_stack_relaxed(module: &Module, builder: &mut CodeBuilder) -> bool {
6718 !module.config.disallow_traps
6719 && module.config.relaxed_simd_enabled
6720 && builder.types_on_stack(module, &[ValType::V128, ValType::V128, ValType::V128])
6721 }
6722
6723 #[inline]
6724 fn simd_v128_i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6725 !module.config.disallow_traps
6726 && module.config.simd_enabled
6727 && builder.types_on_stack(module, &[ValType::V128, ValType::I32])
6728 }
6729
6730 #[inline]
6731 fn simd_v128_i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6732 !module.config.disallow_traps
6733 && module.config.simd_enabled
6734 && builder.types_on_stack(module, &[ValType::V128, ValType::I64])
6735 }
6736
6737 #[inline]
6738 fn simd_v128_f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6739 !module.config.disallow_traps
6740 && module.config.simd_enabled
6741 && builder.types_on_stack(module, &[ValType::V128, ValType::F32])
6742 }
6743
6744 #[inline]
6745 fn simd_v128_f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6746 !module.config.disallow_traps
6747 && module.config.simd_enabled
6748 && builder.types_on_stack(module, &[ValType::V128, ValType::F64])
6749 }
6750
6751 #[inline]
6752 fn simd_i32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6753 !module.config.disallow_traps
6754 && module.config.simd_enabled
6755 && builder.type_on_stack(module, ValType::I32)
6756 }
6757
6758 #[inline]
6759 fn simd_i64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6760 !module.config.disallow_traps
6761 && module.config.simd_enabled
6762 && builder.type_on_stack(module, ValType::I64)
6763 }
6764
6765 #[inline]
6766 fn simd_f32_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6767 !module.config.disallow_traps
6768 && module.config.simd_enabled
6769 && builder.type_on_stack(module, ValType::F32)
6770 }
6771
6772 #[inline]
6773 fn simd_f64_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
6774 !module.config.disallow_traps
6775 && module.config.simd_enabled
6776 && builder.type_on_stack(module, ValType::F64)
6777 }
6778
6779 #[inline]
6780 fn simd_have_memory_and_offset(module: &Module, builder: &mut CodeBuilder) -> bool {
6781 !module.config.disallow_traps
6782 && module.config.simd_enabled
6783 && have_memory_and_offset(module, builder)
6784 }
6785
6786 #[inline]
6787 fn simd_have_memory_and_offset_and_v128(module: &Module, builder: &mut CodeBuilder) -> bool {
6788 !module.config.disallow_traps
6789 && module.config.simd_enabled
6790 && store_valid(module, builder, || ValType::V128)
6791 }
6792
6793 #[inline]
6794 fn simd_load_lane_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6795 // The SIMD non-trapping case is not yet implemented.
6796 !module.config.disallow_traps && simd_have_memory_and_offset_and_v128(module, builder)
6797 }
6798
6799 #[inline]
6800 fn simd_v128_store_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6801 !module.config.disallow_traps
6802 && module.config.simd_enabled
6803 && store_valid(module, builder, || ValType::V128)
6804 }
6805
6806 #[inline]
6807 fn simd_store_lane_valid(module: &Module, builder: &mut CodeBuilder) -> bool {
6808 // The SIMD non-trapping case is not yet implemented.
6809 !module.config.disallow_traps && simd_v128_store_valid(module, builder)
6810 }
6811
6812 #[inline]
6813 fn simd_enabled(module: &Module, _: &mut CodeBuilder) -> bool {
6814 module.config.simd_enabled
6815 }
6816
6817 macro_rules! simd_load {
6818 ($instruction:ident, $generator_fn_name:ident, $alignments:expr) => {
6819 fn $generator_fn_name(
6820 u: &mut Unstructured,
6821 module: &Module,
6822 builder: &mut CodeBuilder,
6823
6824 instructions: &mut Vec<Instruction>,
6825 ) -> Result<()> {
6826 let memarg = mem_arg(u, module, builder, $alignments)?;
6827 builder.push_operands(&[ValType::V128]);
6828 if module.config.disallow_traps {
6829 no_traps::load(
6830 Instruction::$instruction(memarg),
6831 module,
6832 builder,
6833 instructions,
6834 );
6835 } else {
6836 instructions.push(Instruction::$instruction(memarg));
6837 }
6838 Ok(())
6839 }
6840 };
6841 }
6842
6843 simd_load!(V128Load, v128_load, &[0, 1, 2, 3, 4]);
6844 simd_load!(V128Load8x8S, v128_load8x8s, &[0, 1, 2, 3]);
6845 simd_load!(V128Load8x8U, v128_load8x8u, &[0, 1, 2, 3]);
6846 simd_load!(V128Load16x4S, v128_load16x4s, &[0, 1, 2, 3]);
6847 simd_load!(V128Load16x4U, v128_load16x4u, &[0, 1, 2, 3]);
6848 simd_load!(V128Load32x2S, v128_load32x2s, &[0, 1, 2, 3]);
6849 simd_load!(V128Load32x2U, v128_load32x2u, &[0, 1, 2, 3]);
6850 simd_load!(V128Load8Splat, v128_load8_splat, &[0]);
6851 simd_load!(V128Load16Splat, v128_load16_splat, &[0, 1]);
6852 simd_load!(V128Load32Splat, v128_load32_splat, &[0, 1, 2]);
6853 simd_load!(V128Load64Splat, v128_load64_splat, &[0, 1, 2, 3]);
6854 simd_load!(V128Load32Zero, v128_load32_zero, &[0, 1, 2]);
6855 simd_load!(V128Load64Zero, v128_load64_zero, &[0, 1, 2, 3]);
6856
6857 fn v128_store(
6858 u: &mut Unstructured,
6859 module: &Module,
6860 builder: &mut CodeBuilder,
6861 instructions: &mut Vec<Instruction>,
6862 ) -> Result<()> {
6863 builder.pop_operands(module, &[ValType::V128]);
6864 let memarg = mem_arg(u, module, builder, &[0, 1, 2, 3, 4])?;
6865 if module.config.disallow_traps {
6866 no_traps::store(
6867 Instruction::V128Store(memarg),
6868 module,
6869 builder,
6870 instructions,
6871 );
6872 } else {
6873 instructions.push(Instruction::V128Store(memarg));
6874 }
6875 Ok(())
6876 }
6877
6878 macro_rules! simd_load_lane {
6879 ($instruction:ident, $generator_fn_name:ident, $alignments:expr, $number_of_lanes:expr) => {
6880 fn $generator_fn_name(
6881 u: &mut Unstructured,
6882 module: &Module,
6883 builder: &mut CodeBuilder,
6884 instructions: &mut Vec<Instruction>,
6885 ) -> Result<()> {
6886 builder.pop_operands(module, &[ValType::V128]);
6887 let memarg = mem_arg(u, module, builder, $alignments)?;
6888 builder.push_operands(&[ValType::V128]);
6889 instructions.push(Instruction::$instruction {
6890 memarg,
6891 lane: lane_index(u, $number_of_lanes)?,
6892 });
6893 Ok(())
6894 }
6895 };
6896 }
6897
6898 simd_load_lane!(V128Load8Lane, v128_load8_lane, &[0], 16);
6899 simd_load_lane!(V128Load16Lane, v128_load16_lane, &[0, 1], 8);
6900 simd_load_lane!(V128Load32Lane, v128_load32_lane, &[0, 1, 2], 4);
6901 simd_load_lane!(V128Load64Lane, v128_load64_lane, &[0, 1, 2, 3], 2);
6902
6903 macro_rules! simd_store_lane {
6904 ($instruction:ident, $generator_fn_name:ident, $alignments:expr, $number_of_lanes:expr) => {
6905 fn $generator_fn_name(
6906 u: &mut Unstructured,
6907 module: &Module,
6908 builder: &mut CodeBuilder,
6909 instructions: &mut Vec<Instruction>,
6910 ) -> Result<()> {
6911 builder.pop_operands(module, &[ValType::V128]);
6912 let memarg = mem_arg(u, module, builder, $alignments)?;
6913 instructions.push(Instruction::$instruction {
6914 memarg,
6915 lane: lane_index(u, $number_of_lanes)?,
6916 });
6917 Ok(())
6918 }
6919 };
6920 }
6921
6922 simd_store_lane!(V128Store8Lane, v128_store8_lane, &[0], 16);
6923 simd_store_lane!(V128Store16Lane, v128_store16_lane, &[0, 1], 8);
6924 simd_store_lane!(V128Store32Lane, v128_store32_lane, &[0, 1, 2], 4);
6925 simd_store_lane!(V128Store64Lane, v128_store64_lane, &[0, 1, 2, 3], 2);
6926
6927 fn v128_const(
6928 u: &mut Unstructured,
6929 _module: &Module,
6930 builder: &mut CodeBuilder,
6931 instructions: &mut Vec<Instruction>,
6932 ) -> Result<()> {
6933 builder.push_operands(&[ValType::V128]);
6934 let c = i128::from_le_bytes(u.arbitrary()?);
6935 instructions.push(Instruction::V128Const(c));
6936 Ok(())
6937 }
6938
6939 fn i8x16_shuffle(
6940 u: &mut Unstructured,
6941 module: &Module,
6942 builder: &mut CodeBuilder,
6943 instructions: &mut Vec<Instruction>,
6944 ) -> Result<()> {
6945 builder.pop_operands(module, &[ValType::V128, ValType::V128]);
6946 builder.push_operands(&[ValType::V128]);
6947 let mut lanes = [0; 16];
6948 for i in 0..16 {
6949 lanes[i] = u.int_in_range(0..=31)?;
6950 }
6951 instructions.push(Instruction::I8x16Shuffle(lanes));
6952 Ok(())
6953 }
6954
6955 macro_rules! simd_lane_access {
6956 ($instruction:ident, $generator_fn_name:ident, $in_types:expr => $out_types:expr, $number_of_lanes:expr) => {
6957 fn $generator_fn_name(
6958 u: &mut Unstructured,
6959 module: &Module,
6960 builder: &mut CodeBuilder,
6961 instructions: &mut Vec<Instruction>,
6962 ) -> Result<()> {
6963 builder.pop_operands(module, $in_types);
6964 builder.push_operands($out_types);
6965 instructions.push(Instruction::$instruction(lane_index(u, $number_of_lanes)?));
6966 Ok(())
6967 }
6968 };
6969 }
6970
6971 simd_lane_access!(I8x16ExtractLaneS, i8x16_extract_lane_s, &[ValType::V128] => &[ValType::I32], 16);
6972 simd_lane_access!(I8x16ExtractLaneU, i8x16_extract_lane_u, &[ValType::V128] => &[ValType::I32], 16);
6973 simd_lane_access!(I8x16ReplaceLane, i8x16_replace_lane, &[ValType::V128, ValType::I32] => &[ValType::V128], 16);
6974 simd_lane_access!(I16x8ExtractLaneS, i16x8_extract_lane_s, &[ValType::V128] => &[ValType::I32], 8);
6975 simd_lane_access!(I16x8ExtractLaneU, i16x8_extract_lane_u, &[ValType::V128] => &[ValType::I32], 8);
6976 simd_lane_access!(I16x8ReplaceLane, i16x8_replace_lane, &[ValType::V128, ValType::I32] => &[ValType::V128], 8);
6977 simd_lane_access!(I32x4ExtractLane, i32x4_extract_lane, &[ValType::V128] => &[ValType::I32], 4);
6978 simd_lane_access!(I32x4ReplaceLane, i32x4_replace_lane, &[ValType::V128, ValType::I32] => &[ValType::V128], 4);
6979 simd_lane_access!(I64x2ExtractLane, i64x2_extract_lane, &[ValType::V128] => &[ValType::I64], 2);
6980 simd_lane_access!(I64x2ReplaceLane, i64x2_replace_lane, &[ValType::V128, ValType::I64] => &[ValType::V128], 2);
6981 simd_lane_access!(F32x4ExtractLane, f32x4_extract_lane, &[ValType::V128] => &[ValType::F32], 4);
6982 simd_lane_access!(F32x4ReplaceLane, f32x4_replace_lane, &[ValType::V128, ValType::F32] => &[ValType::V128], 4);
6983 simd_lane_access!(F64x2ExtractLane, f64x2_extract_lane, &[ValType::V128] => &[ValType::F64], 2);
6984 simd_lane_access!(F64x2ReplaceLane, f64x2_replace_lane, &[ValType::V128, ValType::F64] => &[ValType::V128], 2);
6985
6986 macro_rules! simd_binop {
6987 ($instruction:ident, $generator_fn_name:ident) => {
6988 fn $generator_fn_name(
6989 _: &mut Unstructured,
6990 module: &Module,
6991 builder: &mut CodeBuilder,
6992
6993 instructions: &mut Vec<Instruction>,
6994 ) -> Result<()> {
6995 builder.pop_operands(module, &[ValType::V128, ValType::V128]);
6996 builder.push_operands(&[ValType::V128]);
6997 instructions.push(Instruction::$instruction);
6998 Ok(())
6999 }
7000 };
7001 }
7002
7003 macro_rules! simd_unop {
7004 ($instruction:ident, $generator_fn_name:ident) => {
7005 simd_unop!($instruction, $generator_fn_name, V128 -> V128);
7006 };
7007
7008 ($instruction:ident, $generator_fn_name:ident, $in_type:ident -> $out_type:ident) => {
7009 fn $generator_fn_name(
7010 _: &mut Unstructured,
7011 module: &Module,
7012 builder: &mut CodeBuilder,
7013
7014 instructions: &mut Vec<Instruction>, ) -> Result<()> {
7015 builder.pop_operands(module, &[ValType::$in_type]);
7016 builder.push_operands(&[ValType::$out_type]);
7017 instructions.push(Instruction::$instruction);
7018 Ok(())
7019 }
7020 };
7021 }
7022
7023 macro_rules! simd_ternop {
7024 ($instruction:ident, $generator_fn_name:ident) => {
7025 fn $generator_fn_name(
7026 _: &mut Unstructured,
7027 module: &Module,
7028 builder: &mut CodeBuilder,
7029
7030 instructions: &mut Vec<Instruction>,
7031 ) -> Result<()> {
7032 builder.pop_operands(module, &[ValType::V128, ValType::V128, ValType::V128]);
7033 builder.push_operands(&[ValType::V128]);
7034 instructions.push(Instruction::$instruction);
7035 Ok(())
7036 }
7037 };
7038 }
7039
7040 macro_rules! simd_shift {
7041 ($instruction:ident, $generator_fn_name:ident) => {
7042 fn $generator_fn_name(
7043 _: &mut Unstructured,
7044 module: &Module,
7045 builder: &mut CodeBuilder,
7046
7047 instructions: &mut Vec<Instruction>,
7048 ) -> Result<()> {
7049 builder.pop_operands(module, &[ValType::V128, ValType::I32]);
7050 builder.push_operands(&[ValType::V128]);
7051 instructions.push(Instruction::$instruction);
7052 Ok(())
7053 }
7054 };
7055 }
7056
7057 simd_unop!(I8x16Splat, i8x16_splat, I32 -> V128);
7058 simd_unop!(I16x8Splat, i16x8_splat, I32 -> V128);
7059 simd_unop!(I32x4Splat, i32x4_splat, I32 -> V128);
7060 simd_unop!(I64x2Splat, i64x2_splat, I64 -> V128);
7061 simd_unop!(F32x4Splat, f32x4_splat, F32 -> V128);
7062 simd_unop!(F64x2Splat, f64x2_splat, F64 -> V128);
7063 simd_binop!(I8x16Swizzle, i8x16_swizzle);
7064 simd_binop!(I8x16Eq, i8x16_eq);
7065 simd_binop!(I8x16Ne, i8x16_ne);
7066 simd_binop!(I8x16LtS, i8x16_lt_s);
7067 simd_binop!(I8x16LtU, i8x16_lt_u);
7068 simd_binop!(I8x16GtS, i8x16_gt_s);
7069 simd_binop!(I8x16GtU, i8x16_gt_u);
7070 simd_binop!(I8x16LeS, i8x16_le_s);
7071 simd_binop!(I8x16LeU, i8x16_le_u);
7072 simd_binop!(I8x16GeS, i8x16_ge_s);
7073 simd_binop!(I8x16GeU, i8x16_ge_u);
7074 simd_binop!(I16x8Eq, i16x8_eq);
7075 simd_binop!(I16x8Ne, i16x8_ne);
7076 simd_binop!(I16x8LtS, i16x8_lt_s);
7077 simd_binop!(I16x8LtU, i16x8_lt_u);
7078 simd_binop!(I16x8GtS, i16x8_gt_s);
7079 simd_binop!(I16x8GtU, i16x8_gt_u);
7080 simd_binop!(I16x8LeS, i16x8_le_s);
7081 simd_binop!(I16x8LeU, i16x8_le_u);
7082 simd_binop!(I16x8GeS, i16x8_ge_s);
7083 simd_binop!(I16x8GeU, i16x8_ge_u);
7084 simd_binop!(I32x4Eq, i32x4_eq);
7085 simd_binop!(I32x4Ne, i32x4_ne);
7086 simd_binop!(I32x4LtS, i32x4_lt_s);
7087 simd_binop!(I32x4LtU, i32x4_lt_u);
7088 simd_binop!(I32x4GtS, i32x4_gt_s);
7089 simd_binop!(I32x4GtU, i32x4_gt_u);
7090 simd_binop!(I32x4LeS, i32x4_le_s);
7091 simd_binop!(I32x4LeU, i32x4_le_u);
7092 simd_binop!(I32x4GeS, i32x4_ge_s);
7093 simd_binop!(I32x4GeU, i32x4_ge_u);
7094 simd_binop!(I64x2Eq, i64x2_eq);
7095 simd_binop!(I64x2Ne, i64x2_ne);
7096 simd_binop!(I64x2LtS, i64x2_lt_s);
7097 simd_binop!(I64x2GtS, i64x2_gt_s);
7098 simd_binop!(I64x2LeS, i64x2_le_s);
7099 simd_binop!(I64x2GeS, i64x2_ge_s);
7100 simd_binop!(F32x4Eq, f32x4_eq);
7101 simd_binop!(F32x4Ne, f32x4_ne);
7102 simd_binop!(F32x4Lt, f32x4_lt);
7103 simd_binop!(F32x4Gt, f32x4_gt);
7104 simd_binop!(F32x4Le, f32x4_le);
7105 simd_binop!(F32x4Ge, f32x4_ge);
7106 simd_binop!(F64x2Eq, f64x2_eq);
7107 simd_binop!(F64x2Ne, f64x2_ne);
7108 simd_binop!(F64x2Lt, f64x2_lt);
7109 simd_binop!(F64x2Gt, f64x2_gt);
7110 simd_binop!(F64x2Le, f64x2_le);
7111 simd_binop!(F64x2Ge, f64x2_ge);
7112 simd_unop!(V128Not, v128_not);
7113 simd_binop!(V128And, v128_and);
7114 simd_binop!(V128AndNot, v128_and_not);
7115 simd_binop!(V128Or, v128_or);
7116 simd_binop!(V128Xor, v128_xor);
7117 simd_unop!(V128AnyTrue, v128_any_true, V128 -> I32);
7118 simd_unop!(I8x16Abs, i8x16_abs);
7119 simd_unop!(I8x16Neg, i8x16_neg);
7120 simd_unop!(I8x16Popcnt, i8x16_popcnt);
7121 simd_unop!(I8x16AllTrue, i8x16_all_true, V128 -> I32);
7122 simd_unop!(I8x16Bitmask, i8x16_bitmask, V128 -> I32);
7123 simd_binop!(I8x16NarrowI16x8S, i8x16_narrow_i16x8s);
7124 simd_binop!(I8x16NarrowI16x8U, i8x16_narrow_i16x8u);
7125 simd_shift!(I8x16Shl, i8x16_shl);
7126 simd_shift!(I8x16ShrS, i8x16_shr_s);
7127 simd_shift!(I8x16ShrU, i8x16_shr_u);
7128 simd_binop!(I8x16Add, i8x16_add);
7129 simd_binop!(I8x16AddSatS, i8x16_add_sat_s);
7130 simd_binop!(I8x16AddSatU, i8x16_add_sat_u);
7131 simd_binop!(I8x16Sub, i8x16_sub);
7132 simd_binop!(I8x16SubSatS, i8x16_sub_sat_s);
7133 simd_binop!(I8x16SubSatU, i8x16_sub_sat_u);
7134 simd_binop!(I8x16MinS, i8x16_min_s);
7135 simd_binop!(I8x16MinU, i8x16_min_u);
7136 simd_binop!(I8x16MaxS, i8x16_max_s);
7137 simd_binop!(I8x16MaxU, i8x16_max_u);
7138 simd_binop!(I8x16AvgrU, i8x16_avgr_u);
7139 simd_unop!(I16x8ExtAddPairwiseI8x16S, i16x8_extadd_pairwise_i8x16s);
7140 simd_unop!(I16x8ExtAddPairwiseI8x16U, i16x8_extadd_pairwise_i8x16u);
7141 simd_unop!(I16x8Abs, i16x8_abs);
7142 simd_unop!(I16x8Neg, i16x8_neg);
7143 simd_binop!(I16x8Q15MulrSatS, i16x8q15_mulr_sat_s);
7144 simd_unop!(I16x8AllTrue, i16x8_all_true, V128 -> I32);
7145 simd_unop!(I16x8Bitmask, i16x8_bitmask, V128 -> I32);
7146 simd_binop!(I16x8NarrowI32x4S, i16x8_narrow_i32x4s);
7147 simd_binop!(I16x8NarrowI32x4U, i16x8_narrow_i32x4u);
7148 simd_unop!(I16x8ExtendLowI8x16S, i16x8_extend_low_i8x16s);
7149 simd_unop!(I16x8ExtendHighI8x16S, i16x8_extend_high_i8x16s);
7150 simd_unop!(I16x8ExtendLowI8x16U, i16x8_extend_low_i8x16u);
7151 simd_unop!(I16x8ExtendHighI8x16U, i16x8_extend_high_i8x16u);
7152 simd_shift!(I16x8Shl, i16x8_shl);
7153 simd_shift!(I16x8ShrS, i16x8_shr_s);
7154 simd_shift!(I16x8ShrU, i16x8_shr_u);
7155 simd_binop!(I16x8Add, i16x8_add);
7156 simd_binop!(I16x8AddSatS, i16x8_add_sat_s);
7157 simd_binop!(I16x8AddSatU, i16x8_add_sat_u);
7158 simd_binop!(I16x8Sub, i16x8_sub);
7159 simd_binop!(I16x8SubSatS, i16x8_sub_sat_s);
7160 simd_binop!(I16x8SubSatU, i16x8_sub_sat_u);
7161 simd_binop!(I16x8Mul, i16x8_mul);
7162 simd_binop!(I16x8MinS, i16x8_min_s);
7163 simd_binop!(I16x8MinU, i16x8_min_u);
7164 simd_binop!(I16x8MaxS, i16x8_max_s);
7165 simd_binop!(I16x8MaxU, i16x8_max_u);
7166 simd_binop!(I16x8AvgrU, i16x8_avgr_u);
7167 simd_binop!(I16x8ExtMulLowI8x16S, i16x8_extmul_low_i8x16s);
7168 simd_binop!(I16x8ExtMulHighI8x16S, i16x8_extmul_high_i8x16s);
7169 simd_binop!(I16x8ExtMulLowI8x16U, i16x8_extmul_low_i8x16u);
7170 simd_binop!(I16x8ExtMulHighI8x16U, i16x8_extmul_high_i8x16u);
7171 simd_unop!(I32x4ExtAddPairwiseI16x8S, i32x4_extadd_pairwise_i16x8s);
7172 simd_unop!(I32x4ExtAddPairwiseI16x8U, i32x4_extadd_pairwise_i16x8u);
7173 simd_unop!(I32x4Abs, i32x4_abs);
7174 simd_unop!(I32x4Neg, i32x4_neg);
7175 simd_unop!(I32x4AllTrue, i32x4_all_true, V128 -> I32);
7176 simd_unop!(I32x4Bitmask, i32x4_bitmask, V128 -> I32);
7177 simd_unop!(I32x4ExtendLowI16x8S, i32x4_extend_low_i16x8s);
7178 simd_unop!(I32x4ExtendHighI16x8S, i32x4_extend_high_i16x8s);
7179 simd_unop!(I32x4ExtendLowI16x8U, i32x4_extend_low_i16x8u);
7180 simd_unop!(I32x4ExtendHighI16x8U, i32x4_extend_high_i16x8u);
7181 simd_shift!(I32x4Shl, i32x4_shl);
7182 simd_shift!(I32x4ShrS, i32x4_shr_s);
7183 simd_shift!(I32x4ShrU, i32x4_shr_u);
7184 simd_binop!(I32x4Add, i32x4_add);
7185 simd_binop!(I32x4Sub, i32x4_sub);
7186 simd_binop!(I32x4Mul, i32x4_mul);
7187 simd_binop!(I32x4MinS, i32x4_min_s);
7188 simd_binop!(I32x4MinU, i32x4_min_u);
7189 simd_binop!(I32x4MaxS, i32x4_max_s);
7190 simd_binop!(I32x4MaxU, i32x4_max_u);
7191 simd_binop!(I32x4DotI16x8S, i32x4_dot_i16x8s);
7192 simd_binop!(I32x4ExtMulLowI16x8S, i32x4_extmul_low_i16x8s);
7193 simd_binop!(I32x4ExtMulHighI16x8S, i32x4_extmul_high_i16x8s);
7194 simd_binop!(I32x4ExtMulLowI16x8U, i32x4_extmul_low_i16x8u);
7195 simd_binop!(I32x4ExtMulHighI16x8U, i32x4_extmul_high_i16x8u);
7196 simd_unop!(I64x2Abs, i64x2_abs);
7197 simd_unop!(I64x2Neg, i64x2_neg);
7198 simd_unop!(I64x2AllTrue, i64x2_all_true, V128 -> I32);
7199 simd_unop!(I64x2Bitmask, i64x2_bitmask, V128 -> I32);
7200 simd_unop!(I64x2ExtendLowI32x4S, i64x2_extend_low_i32x4s);
7201 simd_unop!(I64x2ExtendHighI32x4S, i64x2_extend_high_i32x4s);
7202 simd_unop!(I64x2ExtendLowI32x4U, i64x2_extend_low_i32x4u);
7203 simd_unop!(I64x2ExtendHighI32x4U, i64x2_extend_high_i32x4u);
7204 simd_shift!(I64x2Shl, i64x2_shl);
7205 simd_shift!(I64x2ShrS, i64x2_shr_s);
7206 simd_shift!(I64x2ShrU, i64x2_shr_u);
7207 simd_binop!(I64x2Add, i64x2_add);
7208 simd_binop!(I64x2Sub, i64x2_sub);
7209 simd_binop!(I64x2Mul, i64x2_mul);
7210 simd_binop!(I64x2ExtMulLowI32x4S, i64x2_extmul_low_i32x4s);
7211 simd_binop!(I64x2ExtMulHighI32x4S, i64x2_extmul_high_i32x4s);
7212 simd_binop!(I64x2ExtMulLowI32x4U, i64x2_extmul_low_i32x4u);
7213 simd_binop!(I64x2ExtMulHighI32x4U, i64x2_extmul_high_i32x4u);
7214 simd_unop!(F32x4Ceil, f32x4_ceil);
7215 simd_unop!(F32x4Floor, f32x4_floor);
7216 simd_unop!(F32x4Trunc, f32x4_trunc);
7217 simd_unop!(F32x4Nearest, f32x4_nearest);
7218 simd_unop!(F32x4Abs, f32x4_abs);
7219 simd_unop!(F32x4Neg, f32x4_neg);
7220 simd_unop!(F32x4Sqrt, f32x4_sqrt);
7221 simd_binop!(F32x4Add, f32x4_add);
7222 simd_binop!(F32x4Sub, f32x4_sub);
7223 simd_binop!(F32x4Mul, f32x4_mul);
7224 simd_binop!(F32x4Div, f32x4_div);
7225 simd_binop!(F32x4Min, f32x4_min);
7226 simd_binop!(F32x4Max, f32x4_max);
7227 simd_binop!(F32x4PMin, f32x4p_min);
7228 simd_binop!(F32x4PMax, f32x4p_max);
7229 simd_unop!(F64x2Ceil, f64x2_ceil);
7230 simd_unop!(F64x2Floor, f64x2_floor);
7231 simd_unop!(F64x2Trunc, f64x2_trunc);
7232 simd_unop!(F64x2Nearest, f64x2_nearest);
7233 simd_unop!(F64x2Abs, f64x2_abs);
7234 simd_unop!(F64x2Neg, f64x2_neg);
7235 simd_unop!(F64x2Sqrt, f64x2_sqrt);
7236 simd_binop!(F64x2Add, f64x2_add);
7237 simd_binop!(F64x2Sub, f64x2_sub);
7238 simd_binop!(F64x2Mul, f64x2_mul);
7239 simd_binop!(F64x2Div, f64x2_div);
7240 simd_binop!(F64x2Min, f64x2_min);
7241 simd_binop!(F64x2Max, f64x2_max);
7242 simd_binop!(F64x2PMin, f64x2p_min);
7243 simd_binop!(F64x2PMax, f64x2p_max);
7244 simd_unop!(I32x4TruncSatF32x4S, i32x4_trunc_sat_f32x4s);
7245 simd_unop!(I32x4TruncSatF32x4U, i32x4_trunc_sat_f32x4u);
7246 simd_unop!(F32x4ConvertI32x4S, f32x4_convert_i32x4s);
7247 simd_unop!(F32x4ConvertI32x4U, f32x4_convert_i32x4u);
7248 simd_unop!(I32x4TruncSatF64x2SZero, i32x4_trunc_sat_f64x2s_zero);
7249 simd_unop!(I32x4TruncSatF64x2UZero, i32x4_trunc_sat_f64x2u_zero);
7250 simd_unop!(F64x2ConvertLowI32x4S, f64x2_convert_low_i32x4s);
7251 simd_unop!(F64x2ConvertLowI32x4U, f64x2_convert_low_i32x4u);
7252 simd_unop!(F32x4DemoteF64x2Zero, f32x4_demote_f64x2_zero);
7253 simd_unop!(F64x2PromoteLowF32x4, f64x2_promote_low_f32x4);
7254 simd_ternop!(V128Bitselect, v128_bitselect);
7255 simd_binop!(I8x16RelaxedSwizzle, i8x16_relaxed_swizzle);
7256 simd_unop!(I32x4RelaxedTruncF32x4S, i32x4_relaxed_trunc_f32x4s);
7257 simd_unop!(I32x4RelaxedTruncF32x4U, i32x4_relaxed_trunc_f32x4u);
7258 simd_unop!(I32x4RelaxedTruncF64x2SZero, i32x4_relaxed_trunc_f64x2s_zero);
7259 simd_unop!(I32x4RelaxedTruncF64x2UZero, i32x4_relaxed_trunc_f64x2u_zero);
7260 simd_ternop!(F32x4RelaxedMadd, f32x4_relaxed_madd);
7261 simd_ternop!(F32x4RelaxedNmadd, f32x4_relaxed_nmadd);
7262 simd_ternop!(F64x2RelaxedMadd, f64x2_relaxed_madd);
7263 simd_ternop!(F64x2RelaxedNmadd, f64x2_relaxed_nmadd);
7264 simd_ternop!(I8x16RelaxedLaneselect, i8x16_relaxed_laneselect);
7265 simd_ternop!(I16x8RelaxedLaneselect, i16x8_relaxed_laneselect);
7266 simd_ternop!(I32x4RelaxedLaneselect, i32x4_relaxed_laneselect);
7267 simd_ternop!(I64x2RelaxedLaneselect, i64x2_relaxed_laneselect);
7268 simd_binop!(F32x4RelaxedMin, f32x4_relaxed_min);
7269 simd_binop!(F32x4RelaxedMax, f32x4_relaxed_max);
7270 simd_binop!(F64x2RelaxedMin, f64x2_relaxed_min);
7271 simd_binop!(F64x2RelaxedMax, f64x2_relaxed_max);
7272 simd_binop!(I16x8RelaxedQ15mulrS, i16x8_relaxed_q15mulr_s);
7273 simd_binop!(I16x8RelaxedDotI8x16I7x16S, i16x8_relaxed_dot_i8x16_i7x16_s);
7274 simd_ternop!(
7275 I32x4RelaxedDotI8x16I7x16AddS,
7276 i32x4_relaxed_dot_i8x16_i7x16_add_s
7277 );
7278
7279 #[inline]
7280 fn wide_arithmetic_binop128_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
7281 module.config.wide_arithmetic_enabled && builder.types_on_stack(module, &[ValType::I64; 4])
7282 }
7283
7284 #[inline]
7285 fn wide_arithmetic_mul_wide_on_stack(module: &Module, builder: &mut CodeBuilder) -> bool {
7286 module.config.wide_arithmetic_enabled && builder.types_on_stack(module, &[ValType::I64; 2])
7287 }
7288
7289 fn i64_add128(
7290 _: &mut Unstructured,
7291 module: &Module,
7292 builder: &mut CodeBuilder,
7293 instructions: &mut Vec<Instruction>,
7294 ) -> Result<()> {
7295 builder.pop_operands(module, &[ValType::I64; 4]);
7296 builder.push_operands(&[ValType::I64; 2]);
7297 instructions.push(Instruction::I64Add128);
7298 Ok(())
7299 }
7300
7301 fn i64_sub128(
7302 _: &mut Unstructured,
7303 module: &Module,
7304 builder: &mut CodeBuilder,
7305 instructions: &mut Vec<Instruction>,
7306 ) -> Result<()> {
7307 builder.pop_operands(module, &[ValType::I64; 4]);
7308 builder.push_operands(&[ValType::I64; 2]);
7309 instructions.push(Instruction::I64Sub128);
7310 Ok(())
7311 }
7312
7313 fn i64_mul_wide_s(
7314 _: &mut Unstructured,
7315 module: &Module,
7316 builder: &mut CodeBuilder,
7317 instructions: &mut Vec<Instruction>,
7318 ) -> Result<()> {
7319 builder.pop_operands(module, &[ValType::I64; 2]);
7320 builder.push_operands(&[ValType::I64; 2]);
7321 instructions.push(Instruction::I64MulWideS);
7322 Ok(())
7323 }
7324
7325 fn i64_mul_wide_u(
7326 _: &mut Unstructured,
7327 module: &Module,
7328 builder: &mut CodeBuilder,
7329 instructions: &mut Vec<Instruction>,
7330 ) -> Result<()> {
7331 builder.pop_operands(module, &[ValType::I64; 2]);
7332 builder.push_operands(&[ValType::I64; 2]);
7333 instructions.push(Instruction::I64MulWideU);
7334 Ok(())
7335 }