1 use crate::config::Config;
2 use crate::cranelift_arbitrary::CraneliftArbitrary;
3 use crate::target_isa_extras::TargetIsaExtras;
4 use anyhow::Result;
5 use arbitrary::{Arbitrary, Unstructured};
6 use cranelift::codegen::data_value::DataValue;
7 use cranelift::codegen::ir::immediates::Offset32;
8 use cranelift::codegen::ir::instructions::{InstructionFormat, ResolvedConstraint};
9 use cranelift::codegen::ir::stackslot::StackSize;
10 
11 use cranelift::codegen::ir::{
12     types::*, AliasRegion, AtomicRmwOp, Block, ConstantData, Endianness, ExternalName, FuncRef,
13     Function, LibCall, Opcode, SigRef, Signature, StackSlot, UserExternalName, UserFuncName, Value,
14 };
15 use cranelift::codegen::isa::CallConv;
16 use cranelift::frontend::{FunctionBuilder, FunctionBuilderContext, Switch, Variable};
17 use cranelift::prelude::isa::OwnedTargetIsa;
18 use cranelift::prelude::{
19     EntityRef, ExtFuncData, FloatCC, InstBuilder, IntCC, JumpTableData, MemFlags, StackSlotData,
20     StackSlotKind,
21 };
22 use once_cell::sync::Lazy;
23 use std::collections::HashMap;
24 use std::ops::RangeInclusive;
25 use std::str::FromStr;
26 use target_lexicon::{Architecture, Triple};
27 
28 type BlockSignature = Vec<Type>;
29 
30 fn insert_opcode(
31     fgen: &mut FunctionGenerator,
32     builder: &mut FunctionBuilder,
33     opcode: Opcode,
34     args: &[Type],
35     rets: &[Type],
36 ) -> Result<()> {
37     let mut vals = Vec::with_capacity(args.len());
38     for &arg in args.into_iter() {
39         let var = fgen.get_variable_of_type(arg)?;
40         let val = builder.use_var(var);
41         vals.push(val);
42     }
43 
44     // Some opcodes require us to look at their input arguments to determine the
45     // controlling type. This is not the general case, but we can neatly check this
46     // using `requires_typevar_operand`.
47     let ctrl_type = if opcode.constraints().requires_typevar_operand() {
48         args.first()
49     } else {
50         rets.first()
51     }
52     .copied()
53     .unwrap_or(INVALID);
54 
55     // Choose the appropriate instruction format for this opcode
56     let (inst, dfg) = match opcode.format() {
57         InstructionFormat::NullAry => builder.ins().NullAry(opcode, ctrl_type),
58         InstructionFormat::Unary => builder.ins().Unary(opcode, ctrl_type, vals[0]),
59         InstructionFormat::Binary => builder.ins().Binary(opcode, ctrl_type, vals[0], vals[1]),
60         InstructionFormat::Ternary => builder
61             .ins()
62             .Ternary(opcode, ctrl_type, vals[0], vals[1], vals[2]),
63         _ => unimplemented!(),
64     };
65     let results = dfg.inst_results(inst).to_vec();
66 
67     for (val, &ty) in results.into_iter().zip(rets) {
68         let var = fgen.get_variable_of_type(ty)?;
69         builder.def_var(var, val);
70     }
71     Ok(())
72 }
73 
74 fn insert_call_to_function(
75     fgen: &mut FunctionGenerator,
76     builder: &mut FunctionBuilder,
77     call_opcode: Opcode,
78     sig: &Signature,
79     sig_ref: SigRef,
80     func_ref: FuncRef,
81 ) -> Result<()> {
82     let actuals = fgen.generate_values_for_signature(
83         builder,
84         sig.params.iter().map(|abi_param| abi_param.value_type),
85     )?;
86 
87     let addr_ty = fgen.isa.pointer_type();
88     let call = match call_opcode {
89         Opcode::Call => builder.ins().call(func_ref, &actuals),
90         Opcode::ReturnCall => builder.ins().return_call(func_ref, &actuals),
91         Opcode::CallIndirect => {
92             let addr = builder.ins().func_addr(addr_ty, func_ref);
93             builder.ins().call_indirect(sig_ref, addr, &actuals)
94         }
95         Opcode::ReturnCallIndirect => {
96             let addr = builder.ins().func_addr(addr_ty, func_ref);
97             builder.ins().return_call_indirect(sig_ref, addr, &actuals)
98         }
99         _ => unreachable!(),
100     };
101 
102     // Assign the return values to random variables
103     let ret_values = builder.inst_results(call).to_vec();
104     let ret_types = sig.returns.iter().map(|p| p.value_type);
105     for (ty, val) in ret_types.zip(ret_values) {
106         let var = fgen.get_variable_of_type(ty)?;
107         builder.def_var(var, val);
108     }
109 
110     Ok(())
111 }
112 
113 fn insert_call(
114     fgen: &mut FunctionGenerator,
115     builder: &mut FunctionBuilder,
116     opcode: Opcode,
117     _args: &[Type],
118     _rets: &[Type],
119 ) -> Result<()> {
120     assert!(matches!(opcode, Opcode::Call | Opcode::CallIndirect));
121     let (sig, sig_ref, func_ref) = fgen.u.choose(&fgen.resources.func_refs)?.clone();
122 
123     insert_call_to_function(fgen, builder, opcode, &sig, sig_ref, func_ref)
124 }
125 
126 fn insert_stack_load(
127     fgen: &mut FunctionGenerator,
128     builder: &mut FunctionBuilder,
129     _opcode: Opcode,
130     _args: &[Type],
131     rets: &[Type],
132 ) -> Result<()> {
133     let typevar = rets[0];
134     let type_size = typevar.bytes();
135     let (slot, slot_size, _align, category) = fgen.stack_slot_with_size(type_size)?;
136 
137     // `stack_load` doesn't support setting MemFlags, and it does not set any
138     // alias analysis bits, so we can only emit it for `Other` slots.
139     if category != AACategory::Other {
140         return Err(arbitrary::Error::IncorrectFormat.into());
141     }
142 
143     let offset = fgen.u.int_in_range(0..=(slot_size - type_size))? as i32;
144 
145     let val = builder.ins().stack_load(typevar, slot, offset);
146     let var = fgen.get_variable_of_type(typevar)?;
147     builder.def_var(var, val);
148 
149     Ok(())
150 }
151 
152 fn insert_stack_store(
153     fgen: &mut FunctionGenerator,
154     builder: &mut FunctionBuilder,
155     _opcode: Opcode,
156     args: &[Type],
157     _rets: &[Type],
158 ) -> Result<()> {
159     let typevar = args[0];
160     let type_size = typevar.bytes();
161 
162     let (slot, slot_size, _align, category) = fgen.stack_slot_with_size(type_size)?;
163 
164     // `stack_store` doesn't support setting MemFlags, and it does not set any
165     // alias analysis bits, so we can only emit it for `Other` slots.
166     if category != AACategory::Other {
167         return Err(arbitrary::Error::IncorrectFormat.into());
168     }
169 
170     let offset = fgen.u.int_in_range(0..=(slot_size - type_size))? as i32;
171 
172     let arg0 = fgen.get_variable_of_type(typevar)?;
173     let arg0 = builder.use_var(arg0);
174 
175     builder.ins().stack_store(arg0, slot, offset);
176     Ok(())
177 }
178 
179 fn insert_cmp(
180     fgen: &mut FunctionGenerator,
181     builder: &mut FunctionBuilder,
182     opcode: Opcode,
183     args: &[Type],
184     rets: &[Type],
185 ) -> Result<()> {
186     let lhs = fgen.get_variable_of_type(args[0])?;
187     let lhs = builder.use_var(lhs);
188 
189     let rhs = fgen.get_variable_of_type(args[1])?;
190     let rhs = builder.use_var(rhs);
191 
192     let res = if opcode == Opcode::Fcmp {
193         let cc = *fgen.u.choose(FloatCC::all())?;
194 
195         // We filter out condition codes that aren't supported by the target at
196         // this point after randomly choosing one, instead of randomly choosing a
197         // supported one, to avoid invalidating the corpus when these get implemented.
198         let unimplemented_cc = match (fgen.isa.triple().architecture, cc) {
199             // Some FloatCC's are not implemented on AArch64, see:
200             // https://github.com/bytecodealliance/wasmtime/issues/4850
201             (Architecture::Aarch64(_), FloatCC::OrderedNotEqual) => true,
202             (Architecture::Aarch64(_), FloatCC::UnorderedOrEqual) => true,
203             (Architecture::Aarch64(_), FloatCC::UnorderedOrLessThan) => true,
204             (Architecture::Aarch64(_), FloatCC::UnorderedOrLessThanOrEqual) => true,
205             (Architecture::Aarch64(_), FloatCC::UnorderedOrGreaterThan) => true,
206             (Architecture::Aarch64(_), FloatCC::UnorderedOrGreaterThanOrEqual) => true,
207 
208             // These are not implemented on x86_64, for vectors.
209             (Architecture::X86_64, FloatCC::UnorderedOrEqual | FloatCC::OrderedNotEqual) => {
210                 args[0].is_vector()
211             }
212             _ => false,
213         };
214         if unimplemented_cc {
215             return Err(arbitrary::Error::IncorrectFormat.into());
216         }
217 
218         builder.ins().fcmp(cc, lhs, rhs)
219     } else {
220         let cc = *fgen.u.choose(IntCC::all())?;
221         builder.ins().icmp(cc, lhs, rhs)
222     };
223 
224     let var = fgen.get_variable_of_type(rets[0])?;
225     builder.def_var(var, res);
226 
227     Ok(())
228 }
229 
230 fn insert_const(
231     fgen: &mut FunctionGenerator,
232     builder: &mut FunctionBuilder,
233     _opcode: Opcode,
234     _args: &[Type],
235     rets: &[Type],
236 ) -> Result<()> {
237     let typevar = rets[0];
238     let var = fgen.get_variable_of_type(typevar)?;
239     let val = fgen.generate_const(builder, typevar)?;
240     builder.def_var(var, val);
241     Ok(())
242 }
243 
244 fn insert_bitcast(
245     fgen: &mut FunctionGenerator,
246     builder: &mut FunctionBuilder,
247     args: &[Type],
248     rets: &[Type],
249 ) -> Result<()> {
250     let from_var = fgen.get_variable_of_type(args[0])?;
251     let from_val = builder.use_var(from_var);
252 
253     let to_var = fgen.get_variable_of_type(rets[0])?;
254 
255     // TODO: We can generate little/big endian flags here.
256     let mut memflags = MemFlags::new();
257 
258     // When bitcasting between vectors of different lane counts, we need to
259     // specify the endianness.
260     if args[0].lane_count() != rets[0].lane_count() {
261         memflags.set_endianness(Endianness::Little);
262     }
263 
264     let res = builder.ins().bitcast(rets[0], memflags, from_val);
265     builder.def_var(to_var, res);
266     Ok(())
267 }
268 
269 fn insert_load_store(
270     fgen: &mut FunctionGenerator,
271     builder: &mut FunctionBuilder,
272     opcode: Opcode,
273     args: &[Type],
274     rets: &[Type],
275 ) -> Result<()> {
276     if opcode == Opcode::Bitcast {
277         return insert_bitcast(fgen, builder, args, rets);
278     }
279 
280     let ctrl_type = *rets.first().or(args.first()).unwrap();
281     let type_size = ctrl_type.bytes();
282 
283     let is_atomic = [Opcode::AtomicLoad, Opcode::AtomicStore].contains(&opcode);
284     let (address, flags, offset) =
285         fgen.generate_address_and_memflags(builder, type_size, is_atomic)?;
286 
287     // The variable being loaded or stored into
288     let var = fgen.get_variable_of_type(ctrl_type)?;
289 
290     match opcode.format() {
291         InstructionFormat::LoadNoOffset => {
292             let (inst, dfg) = builder
293                 .ins()
294                 .LoadNoOffset(opcode, ctrl_type, flags, address);
295 
296             let new_val = dfg.first_result(inst);
297             builder.def_var(var, new_val);
298         }
299         InstructionFormat::StoreNoOffset => {
300             let val = builder.use_var(var);
301 
302             builder
303                 .ins()
304                 .StoreNoOffset(opcode, ctrl_type, flags, val, address);
305         }
306         InstructionFormat::Store => {
307             let val = builder.use_var(var);
308 
309             builder
310                 .ins()
311                 .Store(opcode, ctrl_type, flags, offset, val, address);
312         }
313         InstructionFormat::Load => {
314             let (inst, dfg) = builder
315                 .ins()
316                 .Load(opcode, ctrl_type, flags, offset, address);
317 
318             let new_val = dfg.first_result(inst);
319             builder.def_var(var, new_val);
320         }
321         _ => unimplemented!(),
322     }
323 
324     Ok(())
325 }
326 
327 fn insert_atomic_rmw(
328     fgen: &mut FunctionGenerator,
329     builder: &mut FunctionBuilder,
330     _: Opcode,
331     _: &[Type],
332     rets: &[Type],
333 ) -> Result<()> {
334     let ctrl_type = *rets.first().unwrap();
335     let type_size = ctrl_type.bytes();
336 
337     let rmw_op = *fgen.u.choose(AtomicRmwOp::all())?;
338 
339     let (address, flags, offset) = fgen.generate_address_and_memflags(builder, type_size, true)?;
340 
341     // AtomicRMW does not directly support offsets, so add the offset to the address separately.
342     let address = builder.ins().iadd_imm(address, i64::from(offset));
343 
344     // Load and store target variables
345     let source_var = fgen.get_variable_of_type(ctrl_type)?;
346     let target_var = fgen.get_variable_of_type(ctrl_type)?;
347 
348     let source_val = builder.use_var(source_var);
349     let new_val = builder
350         .ins()
351         .atomic_rmw(ctrl_type, flags, rmw_op, address, source_val);
352 
353     builder.def_var(target_var, new_val);
354     Ok(())
355 }
356 
357 fn insert_atomic_cas(
358     fgen: &mut FunctionGenerator,
359     builder: &mut FunctionBuilder,
360     _: Opcode,
361     _: &[Type],
362     rets: &[Type],
363 ) -> Result<()> {
364     let ctrl_type = *rets.first().unwrap();
365     let type_size = ctrl_type.bytes();
366 
367     let (address, flags, offset) = fgen.generate_address_and_memflags(builder, type_size, true)?;
368 
369     // AtomicCas does not directly support offsets, so add the offset to the address separately.
370     let address = builder.ins().iadd_imm(address, i64::from(offset));
371 
372     // Source and Target variables
373     let expected_var = fgen.get_variable_of_type(ctrl_type)?;
374     let store_var = fgen.get_variable_of_type(ctrl_type)?;
375     let loaded_var = fgen.get_variable_of_type(ctrl_type)?;
376 
377     let expected_val = builder.use_var(expected_var);
378     let store_val = builder.use_var(store_var);
379     let new_val = builder
380         .ins()
381         .atomic_cas(flags, address, expected_val, store_val);
382 
383     builder.def_var(loaded_var, new_val);
384     Ok(())
385 }
386 
387 fn insert_shuffle(
388     fgen: &mut FunctionGenerator,
389     builder: &mut FunctionBuilder,
390     opcode: Opcode,
391     _: &[Type],
392     rets: &[Type],
393 ) -> Result<()> {
394     let ctrl_type = *rets.first().unwrap();
395 
396     let lhs = builder.use_var(fgen.get_variable_of_type(ctrl_type)?);
397     let rhs = builder.use_var(fgen.get_variable_of_type(ctrl_type)?);
398 
399     let mask = {
400         let mut lanes = [0u8; 16];
401         for lane in lanes.iter_mut() {
402             *lane = fgen.u.int_in_range(0..=31)?;
403         }
404         let lanes = ConstantData::from(lanes.as_ref());
405         builder.func.dfg.immediates.push(lanes)
406     };
407 
408     // This function is called for any `InstructionFormat::Shuffle`. Which today is just
409     // `shuffle`, but lets assert that, just to be sure we don't accidentally insert
410     // something else.
411     assert_eq!(opcode, Opcode::Shuffle);
412     let res = builder.ins().shuffle(lhs, rhs, mask);
413 
414     let target_var = fgen.get_variable_of_type(ctrl_type)?;
415     builder.def_var(target_var, res);
416 
417     Ok(())
418 }
419 
420 fn insert_ins_ext_lane(
421     fgen: &mut FunctionGenerator,
422     builder: &mut FunctionBuilder,
423     opcode: Opcode,
424     args: &[Type],
425     rets: &[Type],
426 ) -> Result<()> {
427     let vector_type = *args.first().unwrap();
428     let ret_type = *rets.first().unwrap();
429 
430     let lhs = builder.use_var(fgen.get_variable_of_type(vector_type)?);
431     let max_lane = (vector_type.lane_count() as u8) - 1;
432     let lane = fgen.u.int_in_range(0..=max_lane)?;
433 
434     let res = match opcode {
435         Opcode::Insertlane => {
436             let rhs = builder.use_var(fgen.get_variable_of_type(args[1])?);
437             builder.ins().insertlane(lhs, rhs, lane)
438         }
439         Opcode::Extractlane => builder.ins().extractlane(lhs, lane),
440         _ => todo!(),
441     };
442 
443     let target_var = fgen.get_variable_of_type(ret_type)?;
444     builder.def_var(target_var, res);
445 
446     Ok(())
447 }
448 
449 type OpcodeInserter = fn(
450     fgen: &mut FunctionGenerator,
451     builder: &mut FunctionBuilder,
452     Opcode,
453     &[Type],
454     &[Type],
455 ) -> Result<()>;
456 
457 macro_rules! exceptions {
458     ($op:expr, $args:expr, $rets:expr, $(($($cases:pat),*)),* $(,)?) => {
459         match ($op, $args, $rets) {
460             $( ($($cases,)* ..) => return false, )*
461             _ => true,
462         }
463     }
464 }
465 
466 /// Returns true if we believe this `OpcodeSignature` should compile correctly
467 /// for the given target triple. We currently have a range of known issues
468 /// with specific lowerings on specific backends, and we don't want to get
469 /// fuzz bug reports for those. Over time our goal is to eliminate all of these
470 /// exceptions.
471 fn valid_for_target(triple: &Triple, op: Opcode, args: &[Type], rets: &[Type]) -> bool {
472     // Rule out invalid combinations that we don't yet have a good way of rejecting with the
473     // instruction DSL type constraints.
474     match op {
475         Opcode::FcvtToUintSat | Opcode::FcvtToSintSat => {
476             assert_eq!(args.len(), 1);
477             assert_eq!(rets.len(), 1);
478 
479             let arg = args[0];
480             let ret = rets[0];
481 
482             // Vector arguments must produce vector results, and scalar arguments must produce
483             // scalar results.
484             if arg.is_vector() != ret.is_vector() {
485                 return false;
486             }
487 
488             if arg.is_vector() && ret.is_vector() {
489                 // Vector conversions must have the same number of lanes, and the lanes must be the
490                 // same bit-width.
491                 if arg.lane_count() != ret.lane_count() {
492                     return false;
493                 }
494 
495                 if arg.lane_of().bits() != ret.lane_of().bits() {
496                     return false;
497                 }
498             }
499         }
500 
501         Opcode::Bitcast => {
502             assert_eq!(args.len(), 1);
503             assert_eq!(rets.len(), 1);
504 
505             let arg = args[0];
506             let ret = rets[0];
507 
508             // The opcode generator still allows bitcasts between different sized types, but these
509             // are rejected in the verifier.
510             if arg.bits() != ret.bits() {
511                 return false;
512             }
513         }
514 
515         _ => {}
516     }
517 
518     match triple.architecture {
519         Architecture::X86_64 => {
520             exceptions!(
521                 op,
522                 args,
523                 rets,
524                 (Opcode::UmulOverflow | Opcode::SmulOverflow, &[I128, I128]),
525                 (Opcode::Imul, &[I8X16, I8X16]),
526                 // https://github.com/bytecodealliance/wasmtime/issues/4756
527                 (Opcode::Udiv | Opcode::Sdiv, &[I128, I128]),
528                 // https://github.com/bytecodealliance/wasmtime/issues/5474
529                 (Opcode::Urem | Opcode::Srem, &[I128, I128]),
530                 // https://github.com/bytecodealliance/wasmtime/issues/3370
531                 (
532                     Opcode::Smin | Opcode::Umin | Opcode::Smax | Opcode::Umax,
533                     &[I128, I128]
534                 ),
535                 // https://github.com/bytecodealliance/wasmtime/issues/5107
536                 (Opcode::Cls, &[I8], &[I8]),
537                 (Opcode::Cls, &[I16], &[I16]),
538                 (Opcode::Cls, &[I32], &[I32]),
539                 (Opcode::Cls, &[I64], &[I64]),
540                 (Opcode::Cls, &[I128], &[I128]),
541                 // TODO
542                 (Opcode::Bitselect, &[_, _, _], &[F32 | F64]),
543                 // https://github.com/bytecodealliance/wasmtime/issues/4897
544                 // https://github.com/bytecodealliance/wasmtime/issues/4899
545                 (
546                     Opcode::FcvtToUint
547                         | Opcode::FcvtToUintSat
548                         | Opcode::FcvtToSint
549                         | Opcode::FcvtToSintSat,
550                     &[F32 | F64],
551                     &[I8 | I16 | I128]
552                 ),
553                 (Opcode::FcvtToUint | Opcode::FcvtToSint, &[F32X4], &[I32X4]),
554                 (
555                     Opcode::FcvtToUint
556                         | Opcode::FcvtToUintSat
557                         | Opcode::FcvtToSint
558                         | Opcode::FcvtToSintSat,
559                     &[F64X2],
560                     &[I64X2]
561                 ),
562                 // https://github.com/bytecodealliance/wasmtime/issues/4900
563                 (Opcode::FcvtFromUint, &[I128], &[F32 | F64]),
564                 // This has a lowering, but only when preceded by `uwiden_low`.
565                 (Opcode::FcvtFromUint, &[I64X2], &[F64X2]),
566                 // https://github.com/bytecodealliance/wasmtime/issues/4900
567                 (Opcode::FcvtFromSint, &[I128], &[F32 | F64]),
568                 (Opcode::FcvtFromSint, &[I64X2], &[F64X2]),
569                 (
570                     Opcode::Umulhi | Opcode::Smulhi,
571                     &([I8X16, I8X16] | [I16X8, I16X8] | [I32X4, I32X4] | [I64X2, I64X2])
572                 ),
573                 (
574                     Opcode::UaddSat | Opcode::SaddSat | Opcode::UsubSat | Opcode::SsubSat,
575                     &([I32X4, I32X4] | [I64X2, I64X2])
576                 ),
577                 (Opcode::Fcopysign, &([F32X4, F32X4] | [F64X2, F64X2])),
578                 (Opcode::Popcnt, &([I8X16] | [I16X8] | [I32X4] | [I64X2])),
579                 (
580                     Opcode::Umax | Opcode::Smax | Opcode::Umin | Opcode::Smin,
581                     &[I64X2, I64X2]
582                 ),
583                 // https://github.com/bytecodealliance/wasmtime/issues/6104
584                 (Opcode::Bitcast, &[I128], &[_]),
585                 (Opcode::Bitcast, &[_], &[I128]),
586                 (Opcode::Uunarrow),
587                 (Opcode::Snarrow | Opcode::Unarrow, &[I64X2, I64X2]),
588                 (Opcode::SqmulRoundSat, &[I32X4, I32X4]),
589                 // This Icmp is not implemented: #5529
590                 (Opcode::Icmp, &[I64X2, I64X2]),
591                 // IaddPairwise is implemented, but only for some types, and with some preceding ops.
592                 (Opcode::IaddPairwise),
593                 // Nothing wrong with this select. But we have an isle rule that can optimize it
594                 // into a `min`/`max` instructions, which we don't have implemented yet.
595                 (Opcode::Select, &[_, I128, I128]),
596                 // These stack accesses can cause segfaults if they are merged into an SSE instruction.
597                 // See: #5922
598                 (
599                     Opcode::StackStore,
600                     &[I8X16 | I16X8 | I32X4 | I64X2 | F32X4 | F64X2]
601                 ),
602                 (
603                     Opcode::StackLoad,
604                     &[],
605                     &[I8X16 | I16X8 | I32X4 | I64X2 | F32X4 | F64X2]
606                 ),
607                 // TODO
608                 (
609                     Opcode::Sshr | Opcode::Ushr | Opcode::Ishl,
610                     &[I8X16 | I16X8 | I32X4 | I64X2, I128]
611                 ),
612                 (
613                     Opcode::Rotr | Opcode::Rotl,
614                     &[I8X16 | I16X8 | I32X4 | I64X2, _]
615                 ),
616             )
617         }
618 
619         Architecture::Aarch64(_) => {
620             exceptions!(
621                 op,
622                 args,
623                 rets,
624                 (Opcode::UmulOverflow | Opcode::SmulOverflow, &[I128, I128]),
625                 // https://github.com/bytecodealliance/wasmtime/issues/4864
626                 (Opcode::Udiv | Opcode::Sdiv, &[I128, I128]),
627                 // https://github.com/bytecodealliance/wasmtime/issues/5472
628                 (Opcode::Urem | Opcode::Srem, &[I128, I128]),
629                 // https://github.com/bytecodealliance/wasmtime/issues/4313
630                 (
631                     Opcode::Smin | Opcode::Umin | Opcode::Smax | Opcode::Umax,
632                     &[I128, I128]
633                 ),
634                 // https://github.com/bytecodealliance/wasmtime/issues/4870
635                 (Opcode::Bnot, &[F32 | F64]),
636                 (
637                     Opcode::Band
638                         | Opcode::Bor
639                         | Opcode::Bxor
640                         | Opcode::BandNot
641                         | Opcode::BorNot
642                         | Opcode::BxorNot,
643                     &([F32, F32] | [F64, F64])
644                 ),
645                 // https://github.com/bytecodealliance/wasmtime/issues/5198
646                 (Opcode::Bitselect, &[I128, I128, I128]),
647                 // https://github.com/bytecodealliance/wasmtime/issues/4934
648                 (
649                     Opcode::FcvtToUint
650                         | Opcode::FcvtToUintSat
651                         | Opcode::FcvtToSint
652                         | Opcode::FcvtToSintSat,
653                     &[F32 | F64],
654                     &[I128]
655                 ),
656                 // https://github.com/bytecodealliance/wasmtime/issues/4933
657                 (
658                     Opcode::FcvtFromUint | Opcode::FcvtFromSint,
659                     &[I128],
660                     &[F32 | F64]
661                 ),
662                 (
663                     Opcode::Umulhi | Opcode::Smulhi,
664                     &([I8X16, I8X16] | [I16X8, I16X8] | [I32X4, I32X4] | [I64X2, I64X2])
665                 ),
666                 (Opcode::Popcnt, &[I16X8 | I32X4 | I64X2]),
667                 // Nothing wrong with this select. But we have an isle rule that can optimize it
668                 // into a `min`/`max` instructions, which we don't have implemented yet.
669                 (Opcode::Select, &[I8, I128, I128]),
670                 // https://github.com/bytecodealliance/wasmtime/issues/6104
671                 (Opcode::Bitcast, &[I128], &[_]),
672                 (Opcode::Bitcast, &[_], &[I128]),
673                 // TODO
674                 (
675                     Opcode::Sshr | Opcode::Ushr | Opcode::Ishl,
676                     &[I8X16 | I16X8 | I32X4 | I64X2, I128]
677                 ),
678                 (
679                     Opcode::Rotr | Opcode::Rotl,
680                     &[I8X16 | I16X8 | I32X4 | I64X2, _]
681                 ),
682                 // TODO
683                 (Opcode::Bitselect, &[_, _, _], &[F32 | F64]),
684                 (Opcode::VhighBits, &[F32X4 | F64X2]),
685             )
686         }
687 
688         Architecture::S390x => {
689             exceptions!(
690                 op,
691                 args,
692                 rets,
693                 (Opcode::UaddOverflow | Opcode::SaddOverflow),
694                 (Opcode::UsubOverflow | Opcode::SsubOverflow),
695                 (Opcode::UmulOverflow | Opcode::SmulOverflow),
696                 (
697                     Opcode::Udiv | Opcode::Sdiv | Opcode::Urem | Opcode::Srem,
698                     &[I128, I128]
699                 ),
700                 (Opcode::Bnot, &[F32 | F64]),
701                 (
702                     Opcode::Band
703                         | Opcode::Bor
704                         | Opcode::Bxor
705                         | Opcode::BandNot
706                         | Opcode::BorNot
707                         | Opcode::BxorNot,
708                     &([F32, F32] | [F64, F64])
709                 ),
710                 (
711                     Opcode::FcvtToUint
712                         | Opcode::FcvtToUintSat
713                         | Opcode::FcvtToSint
714                         | Opcode::FcvtToSintSat,
715                     &[F32 | F64],
716                     &[I128]
717                 ),
718                 (
719                     Opcode::FcvtFromUint | Opcode::FcvtFromSint,
720                     &[I128],
721                     &[F32 | F64]
722                 ),
723                 (Opcode::SsubSat | Opcode::SaddSat, &[I64X2, I64X2]),
724                 // https://github.com/bytecodealliance/wasmtime/issues/6104
725                 (Opcode::Bitcast, &[I128], &[_]),
726                 (Opcode::Bitcast, &[_], &[I128]),
727                 // TODO
728                 (Opcode::Bitselect, &[_, _, _], &[F32 | F64]),
729             )
730         }
731 
732         Architecture::Riscv64(_) => {
733             exceptions!(
734                 op,
735                 args,
736                 rets,
737                 // TODO
738                 (Opcode::UaddOverflow | Opcode::SaddOverflow),
739                 (Opcode::UsubOverflow | Opcode::SsubOverflow),
740                 (Opcode::UmulOverflow | Opcode::SmulOverflow),
741                 // TODO
742                 (
743                     Opcode::Udiv | Opcode::Sdiv | Opcode::Urem | Opcode::Srem,
744                     &[I128, I128]
745                 ),
746                 // TODO
747                 (Opcode::Iabs, &[I128]),
748                 // TODO
749                 (Opcode::Bitselect, &[I128, I128, I128]),
750                 // https://github.com/bytecodealliance/wasmtime/issues/5528
751                 (
752                     Opcode::FcvtToUint | Opcode::FcvtToSint,
753                     [F32 | F64],
754                     &[I128]
755                 ),
756                 (
757                     Opcode::FcvtToUintSat | Opcode::FcvtToSintSat,
758                     &[F32 | F64],
759                     &[I128]
760                 ),
761                 // https://github.com/bytecodealliance/wasmtime/issues/5528
762                 (
763                     Opcode::FcvtFromUint | Opcode::FcvtFromSint,
764                     &[I128],
765                     &[F32 | F64]
766                 ),
767                 // TODO
768                 (
769                     Opcode::SelectSpectreGuard,
770                     &[_, _, _],
771                     &[F32 | F64 | I8X16 | I16X8 | I32X4 | I64X2 | F64X2 | F32X4]
772                 ),
773                 // TODO
774                 (Opcode::Bitselect, &[_, _, _], &[F32 | F64]),
775                 (
776                     Opcode::Rotr | Opcode::Rotl,
777                     &[I8X16 | I16X8 | I32X4 | I64X2, _]
778                 ),
779             )
780         }
781 
782         _ => true,
783     }
784 }
785 
786 type OpcodeSignature = (Opcode, Vec<Type>, Vec<Type>);
787 
788 static OPCODE_SIGNATURES: Lazy<Vec<OpcodeSignature>> = Lazy::new(|| {
789     let types = &[
790         I8, I16, I32, I64, I128, // Scalar Integers
791         F32, F64, // Scalar Floats
792         I8X16, I16X8, I32X4, I64X2, // SIMD Integers
793         F32X4, F64X2, // SIMD Floats
794     ];
795 
796     // When this env variable is passed, we only generate instructions for the opcodes listed in
797     // the comma-separated list. This is useful for debugging, as it allows us to focus on a few
798     // specific opcodes.
799     let allowed_opcodes = std::env::var("FUZZGEN_ALLOWED_OPS").ok().map(|s| {
800         s.split(',')
801             .map(|s| s.trim())
802             .filter(|s| !s.is_empty())
803             .map(|s| Opcode::from_str(s).expect("Unrecoginzed opcode"))
804             .collect::<Vec<_>>()
805     });
806 
807     Opcode::all()
808         .iter()
809         .filter(|op| {
810             match op {
811                 // Control flow opcodes should not be generated through `generate_instructions`.
812                 Opcode::BrTable
813                 | Opcode::Brif
814                 | Opcode::Jump
815                 | Opcode::Return
816                 | Opcode::ReturnCall
817                 | Opcode::ReturnCallIndirect => false,
818 
819                 // Constants are generated outside of `generate_instructions`
820                 Opcode::Iconst => false,
821 
822                 // TODO: extract_vector raises exceptions during return type generation because it
823                 // uses dynamic vectors.
824                 Opcode::ExtractVector => false,
825 
826                 _ => true,
827             }
828         })
829         .flat_map(|op| {
830             let constraints = op.constraints();
831 
832             let ctrl_types = if let Some(ctrls) = constraints.ctrl_typeset() {
833                 Vec::from_iter(types.iter().copied().filter(|ty| ctrls.contains(*ty)))
834             } else {
835                 vec![INVALID]
836             };
837 
838             ctrl_types.into_iter().flat_map(move |ctrl_type| {
839                 let rets = Vec::from_iter(
840                     (0..constraints.num_fixed_results())
841                         .map(|i| constraints.result_type(i, ctrl_type)),
842                 );
843 
844                 // Cols is a vector whose length will match `num_fixed_value_arguments`, and whose
845                 // elements will be vectors of types that are valid for that fixed argument
846                 // position.
847                 let mut cols = vec![];
848 
849                 for i in 0..constraints.num_fixed_value_arguments() {
850                     match constraints.value_argument_constraint(i, ctrl_type) {
851                         ResolvedConstraint::Bound(ty) => cols.push(Vec::from([ty])),
852                         ResolvedConstraint::Free(tys) => cols.push(Vec::from_iter(
853                             types.iter().copied().filter(|ty| tys.contains(*ty)),
854                         )),
855                     }
856                 }
857 
858                 // Generate the cartesian product of cols to produce a vector of argument lists,
859                 // argss. The argss vector is seeded with the empty argument list, so there's an
860                 // initial value to be extended in the loop below.
861                 let mut argss = vec![vec![]];
862                 let mut cols = cols.as_slice();
863                 while let Some((col, rest)) = cols.split_last() {
864                     cols = rest;
865 
866                     let mut next = vec![];
867                     for current in argss.iter() {
868                         // Extend the front of each argument candidate with every type in `col`.
869                         for ty in col {
870                             let mut args = vec![*ty];
871                             args.extend_from_slice(&current);
872                             next.push(args);
873                         }
874                     }
875 
876                     let _ = std::mem::replace(&mut argss, next);
877                 }
878 
879                 argss.into_iter().map(move |args| (*op, args, rets.clone()))
880             })
881         })
882         .filter(|(op, args, rets)| {
883             // These op/signature combinations need to be vetted
884             exceptions!(
885                 op,
886                 args.as_slice(),
887                 rets.as_slice(),
888                 (Opcode::Debugtrap),
889                 (Opcode::Trap),
890                 (Opcode::Trapz),
891                 (Opcode::ResumableTrap),
892                 (Opcode::Trapnz),
893                 (Opcode::ResumableTrapnz),
894                 (Opcode::CallIndirect, &[I32]),
895                 (Opcode::FuncAddr),
896                 (Opcode::X86Pshufb),
897                 (Opcode::AvgRound),
898                 (Opcode::Uload8x8),
899                 (Opcode::Sload8x8),
900                 (Opcode::Uload16x4),
901                 (Opcode::Sload16x4),
902                 (Opcode::Uload32x2),
903                 (Opcode::Sload32x2),
904                 (Opcode::StackAddr),
905                 (Opcode::DynamicStackLoad),
906                 (Opcode::DynamicStackStore),
907                 (Opcode::DynamicStackAddr),
908                 (Opcode::GlobalValue),
909                 (Opcode::SymbolValue),
910                 (Opcode::TlsValue),
911                 (Opcode::GetPinnedReg),
912                 (Opcode::SetPinnedReg),
913                 (Opcode::GetFramePointer),
914                 (Opcode::GetStackPointer),
915                 (Opcode::GetReturnAddress),
916                 (Opcode::Null),
917                 (Opcode::X86Blendv),
918                 (Opcode::IcmpImm),
919                 (Opcode::X86Pmulhrsw),
920                 (Opcode::IaddImm),
921                 (Opcode::ImulImm),
922                 (Opcode::UdivImm),
923                 (Opcode::SdivImm),
924                 (Opcode::UremImm),
925                 (Opcode::SremImm),
926                 (Opcode::IrsubImm),
927                 (Opcode::IaddCin),
928                 (Opcode::IaddCarry),
929                 (Opcode::UaddOverflowTrap),
930                 (Opcode::IsubBin),
931                 (Opcode::IsubBorrow),
932                 (Opcode::BandImm),
933                 (Opcode::BorImm),
934                 (Opcode::BxorImm),
935                 (Opcode::RotlImm),
936                 (Opcode::RotrImm),
937                 (Opcode::IshlImm),
938                 (Opcode::UshrImm),
939                 (Opcode::SshrImm),
940                 (Opcode::IsNull),
941                 (Opcode::IsInvalid),
942                 (Opcode::ScalarToVector),
943                 (Opcode::X86Pmaddubsw),
944                 (Opcode::X86Cvtt2dq),
945                 (Opcode::Umulhi, &[I128, I128], &[I128]),
946                 (Opcode::Smulhi, &[I128, I128], &[I128]),
947                 // https://github.com/bytecodealliance/wasmtime/issues/6073
948                 (Opcode::Iconcat, &[I32, I32], &[I64]),
949                 (Opcode::Iconcat, &[I16, I16], &[I32]),
950                 (Opcode::Iconcat, &[I8, I8], &[I16]),
951                 // https://github.com/bytecodealliance/wasmtime/issues/6073
952                 (Opcode::Isplit, &[I64], &[I32, I32]),
953                 (Opcode::Isplit, &[I32], &[I16, I16]),
954                 (Opcode::Isplit, &[I16], &[I8, I8]),
955                 (Opcode::Fmin, &[F32X4, F32X4], &[F32X4]),
956                 (Opcode::Fmin, &[F64X2, F64X2], &[F64X2]),
957                 (Opcode::Fmax, &[F32X4, F32X4], &[F32X4]),
958                 (Opcode::Fmax, &[F64X2, F64X2], &[F64X2]),
959                 (Opcode::FcvtToUintSat, &[F32X4], &[I8]),
960                 (Opcode::FcvtToUintSat, &[F64X2], &[I8]),
961                 (Opcode::FcvtToUintSat, &[F32X4], &[I16]),
962                 (Opcode::FcvtToUintSat, &[F64X2], &[I16]),
963                 (Opcode::FcvtToUintSat, &[F32X4], &[I32]),
964                 (Opcode::FcvtToUintSat, &[F64X2], &[I32]),
965                 (Opcode::FcvtToUintSat, &[F32X4], &[I64]),
966                 (Opcode::FcvtToUintSat, &[F64X2], &[I64]),
967                 (Opcode::FcvtToUintSat, &[F32X4], &[I128]),
968                 (Opcode::FcvtToUintSat, &[F64X2], &[I128]),
969                 (Opcode::FcvtToUintSat, &[F32], &[I8X16]),
970                 (Opcode::FcvtToUintSat, &[F64], &[I8X16]),
971                 (Opcode::FcvtToUintSat, &[F32X4], &[I8X16]),
972                 (Opcode::FcvtToUintSat, &[F64X2], &[I8X16]),
973                 (Opcode::FcvtToUintSat, &[F32], &[I16X8]),
974                 (Opcode::FcvtToUintSat, &[F64], &[I16X8]),
975                 (Opcode::FcvtToUintSat, &[F32X4], &[I16X8]),
976                 (Opcode::FcvtToUintSat, &[F64X2], &[I16X8]),
977                 (Opcode::FcvtToUintSat, &[F32], &[I32X4]),
978                 (Opcode::FcvtToUintSat, &[F64], &[I32X4]),
979                 (Opcode::FcvtToUintSat, &[F64X2], &[I32X4]),
980                 (Opcode::FcvtToUintSat, &[F32], &[I64X2]),
981                 (Opcode::FcvtToUintSat, &[F64], &[I64X2]),
982                 (Opcode::FcvtToUintSat, &[F32X4], &[I64X2]),
983                 (Opcode::FcvtToSintSat, &[F32X4], &[I8]),
984                 (Opcode::FcvtToSintSat, &[F64X2], &[I8]),
985                 (Opcode::FcvtToSintSat, &[F32X4], &[I16]),
986                 (Opcode::FcvtToSintSat, &[F64X2], &[I16]),
987                 (Opcode::FcvtToSintSat, &[F32X4], &[I32]),
988                 (Opcode::FcvtToSintSat, &[F64X2], &[I32]),
989                 (Opcode::FcvtToSintSat, &[F32X4], &[I64]),
990                 (Opcode::FcvtToSintSat, &[F64X2], &[I64]),
991                 (Opcode::FcvtToSintSat, &[F32X4], &[I128]),
992                 (Opcode::FcvtToSintSat, &[F64X2], &[I128]),
993                 (Opcode::FcvtToSintSat, &[F32], &[I8X16]),
994                 (Opcode::FcvtToSintSat, &[F64], &[I8X16]),
995                 (Opcode::FcvtToSintSat, &[F32X4], &[I8X16]),
996                 (Opcode::FcvtToSintSat, &[F64X2], &[I8X16]),
997                 (Opcode::FcvtToSintSat, &[F32], &[I16X8]),
998                 (Opcode::FcvtToSintSat, &[F64], &[I16X8]),
999                 (Opcode::FcvtToSintSat, &[F32X4], &[I16X8]),
1000                 (Opcode::FcvtToSintSat, &[F64X2], &[I16X8]),
1001                 (Opcode::FcvtToSintSat, &[F32], &[I32X4]),
1002                 (Opcode::FcvtToSintSat, &[F64], &[I32X4]),
1003                 (Opcode::FcvtToSintSat, &[F64X2], &[I32X4]),
1004                 (Opcode::FcvtToSintSat, &[F32], &[I64X2]),
1005                 (Opcode::FcvtToSintSat, &[F64], &[I64X2]),
1006                 (Opcode::FcvtToSintSat, &[F32X4], &[I64X2]),
1007                 (Opcode::FcvtFromUint, &[I8X16], &[F32]),
1008                 (Opcode::FcvtFromUint, &[I16X8], &[F32]),
1009                 (Opcode::FcvtFromUint, &[I32X4], &[F32]),
1010                 (Opcode::FcvtFromUint, &[I64X2], &[F32]),
1011                 (Opcode::FcvtFromUint, &[I8X16], &[F64]),
1012                 (Opcode::FcvtFromUint, &[I16X8], &[F64]),
1013                 (Opcode::FcvtFromUint, &[I32X4], &[F64]),
1014                 (Opcode::FcvtFromUint, &[I64X2], &[F64]),
1015                 (Opcode::FcvtFromUint, &[I8], &[F32X4]),
1016                 (Opcode::FcvtFromUint, &[I16], &[F32X4]),
1017                 (Opcode::FcvtFromUint, &[I32], &[F32X4]),
1018                 (Opcode::FcvtFromUint, &[I64], &[F32X4]),
1019                 (Opcode::FcvtFromUint, &[I128], &[F32X4]),
1020                 (Opcode::FcvtFromUint, &[I8X16], &[F32X4]),
1021                 (Opcode::FcvtFromUint, &[I16X8], &[F32X4]),
1022                 (Opcode::FcvtFromUint, &[I64X2], &[F32X4]),
1023                 (Opcode::FcvtFromUint, &[I8], &[F64X2]),
1024                 (Opcode::FcvtFromUint, &[I16], &[F64X2]),
1025                 (Opcode::FcvtFromUint, &[I32], &[F64X2]),
1026                 (Opcode::FcvtFromUint, &[I64], &[F64X2]),
1027                 (Opcode::FcvtFromUint, &[I128], &[F64X2]),
1028                 (Opcode::FcvtFromUint, &[I8X16], &[F64X2]),
1029                 (Opcode::FcvtFromUint, &[I16X8], &[F64X2]),
1030                 (Opcode::FcvtFromUint, &[I32X4], &[F64X2]),
1031                 (Opcode::FcvtFromSint, &[I8X16], &[F32]),
1032                 (Opcode::FcvtFromSint, &[I16X8], &[F32]),
1033                 (Opcode::FcvtFromSint, &[I32X4], &[F32]),
1034                 (Opcode::FcvtFromSint, &[I64X2], &[F32]),
1035                 (Opcode::FcvtFromSint, &[I8X16], &[F64]),
1036                 (Opcode::FcvtFromSint, &[I16X8], &[F64]),
1037                 (Opcode::FcvtFromSint, &[I32X4], &[F64]),
1038                 (Opcode::FcvtFromSint, &[I64X2], &[F64]),
1039                 (Opcode::FcvtFromSint, &[I8], &[F32X4]),
1040                 (Opcode::FcvtFromSint, &[I16], &[F32X4]),
1041                 (Opcode::FcvtFromSint, &[I32], &[F32X4]),
1042                 (Opcode::FcvtFromSint, &[I64], &[F32X4]),
1043                 (Opcode::FcvtFromSint, &[I128], &[F32X4]),
1044                 (Opcode::FcvtFromSint, &[I8X16], &[F32X4]),
1045                 (Opcode::FcvtFromSint, &[I16X8], &[F32X4]),
1046                 (Opcode::FcvtFromSint, &[I64X2], &[F32X4]),
1047                 (Opcode::FcvtFromSint, &[I8], &[F64X2]),
1048                 (Opcode::FcvtFromSint, &[I16], &[F64X2]),
1049                 (Opcode::FcvtFromSint, &[I32], &[F64X2]),
1050                 (Opcode::FcvtFromSint, &[I64], &[F64X2]),
1051                 (Opcode::FcvtFromSint, &[I128], &[F64X2]),
1052                 (Opcode::FcvtFromSint, &[I8X16], &[F64X2]),
1053                 (Opcode::FcvtFromSint, &[I16X8], &[F64X2]),
1054                 (Opcode::FcvtFromSint, &[I32X4], &[F64X2]),
1055             )
1056         })
1057         .filter(|(op, ..)| {
1058             allowed_opcodes
1059                 .as_ref()
1060                 .map_or(true, |opcodes| opcodes.contains(op))
1061         })
1062         .collect()
1063 });
1064 
1065 fn inserter_for_format(fmt: InstructionFormat) -> OpcodeInserter {
1066     match fmt {
1067         InstructionFormat::AtomicCas => insert_atomic_cas,
1068         InstructionFormat::AtomicRmw => insert_atomic_rmw,
1069         InstructionFormat::Binary => insert_opcode,
1070         InstructionFormat::BinaryImm64 => todo!(),
1071         InstructionFormat::BinaryImm8 => insert_ins_ext_lane,
1072         InstructionFormat::Call => insert_call,
1073         InstructionFormat::CallIndirect => insert_call,
1074         InstructionFormat::CondTrap => todo!(),
1075         InstructionFormat::DynamicStackLoad => todo!(),
1076         InstructionFormat::DynamicStackStore => todo!(),
1077         InstructionFormat::FloatCompare => insert_cmp,
1078         InstructionFormat::FuncAddr => todo!(),
1079         InstructionFormat::IntAddTrap => todo!(),
1080         InstructionFormat::IntCompare => insert_cmp,
1081         InstructionFormat::IntCompareImm => todo!(),
1082         InstructionFormat::Load => insert_load_store,
1083         InstructionFormat::LoadNoOffset => insert_load_store,
1084         InstructionFormat::NullAry => insert_opcode,
1085         InstructionFormat::Shuffle => insert_shuffle,
1086         InstructionFormat::StackLoad => insert_stack_load,
1087         InstructionFormat::StackStore => insert_stack_store,
1088         InstructionFormat::Store => insert_load_store,
1089         InstructionFormat::StoreNoOffset => insert_load_store,
1090         InstructionFormat::Ternary => insert_opcode,
1091         InstructionFormat::TernaryImm8 => insert_ins_ext_lane,
1092         InstructionFormat::Trap => todo!(),
1093         InstructionFormat::Unary => insert_opcode,
1094         InstructionFormat::UnaryConst => insert_const,
1095         InstructionFormat::UnaryGlobalValue => todo!(),
1096         InstructionFormat::UnaryIeee32 => insert_const,
1097         InstructionFormat::UnaryIeee64 => insert_const,
1098         InstructionFormat::UnaryImm => insert_const,
1099 
1100         InstructionFormat::BranchTable
1101         | InstructionFormat::Brif
1102         | InstructionFormat::Jump
1103         | InstructionFormat::MultiAry => {
1104             panic!(
1105                 "Control-flow instructions should be handled by 'insert_terminator': {:?}",
1106                 fmt
1107             )
1108         }
1109     }
1110 }
1111 
1112 pub struct FunctionGenerator<'r, 'data>
1113 where
1114     'data: 'r,
1115 {
1116     u: &'r mut Unstructured<'data>,
1117     config: &'r Config,
1118     resources: Resources,
1119     isa: OwnedTargetIsa,
1120     name: UserFuncName,
1121     signature: Signature,
1122 }
1123 
1124 #[derive(Debug, Clone)]
1125 enum BlockTerminator {
1126     Return,
1127     Jump(Block),
1128     Br(Block, Block),
1129     BrTable(Block, Vec<Block>),
1130     Switch(Type, Block, HashMap<u128, Block>),
1131     TailCall(FuncRef),
1132     TailCallIndirect(FuncRef),
1133 }
1134 
1135 #[derive(Debug, Clone)]
1136 enum BlockTerminatorKind {
1137     Return,
1138     Jump,
1139     Br,
1140     BrTable,
1141     Switch,
1142     TailCall,
1143     TailCallIndirect,
1144 }
1145 
1146 /// Alias Analysis Category
1147 ///
1148 /// Our alias analysis pass supports 4 categories of accesses to distinguish
1149 /// different regions. The "Other" region is the general case, and is the default
1150 /// Although they have highly suggestive names there is no difference between any
1151 /// of the categories.
1152 ///
1153 /// We assign each stack slot a category when we first generate them, and then
1154 /// ensure that all accesses to that stack slot are correctly tagged. We already
1155 /// ensure that memory accesses never cross stack slots, so there is no risk
1156 /// of a memory access being tagged with the wrong category.
1157 #[derive(Debug, PartialEq, Clone, Copy)]
1158 enum AACategory {
1159     Other,
1160     Heap,
1161     Table,
1162     VmCtx,
1163 }
1164 
1165 impl AACategory {
1166     pub fn all() -> &'static [Self] {
1167         &[
1168             AACategory::Other,
1169             AACategory::Heap,
1170             AACategory::Table,
1171             AACategory::VmCtx,
1172         ]
1173     }
1174 
1175     pub fn update_memflags(&self, flags: &mut MemFlags) {
1176         flags.set_alias_region(match self {
1177             AACategory::Other => None,
1178             AACategory::Heap => Some(AliasRegion::Heap),
1179             AACategory::Table => Some(AliasRegion::Table),
1180             AACategory::VmCtx => Some(AliasRegion::Vmctx),
1181         })
1182     }
1183 }
1184 
1185 pub type StackAlignment = StackSize;
1186 
1187 #[derive(Default)]
1188 struct Resources {
1189     vars: HashMap<Type, Vec<Variable>>,
1190     blocks: Vec<(Block, BlockSignature)>,
1191     blocks_without_params: Vec<Block>,
1192     block_terminators: Vec<BlockTerminator>,
1193     func_refs: Vec<(Signature, SigRef, FuncRef)>,
1194     /// This field is required to be sorted by stack slot size at all times.
1195     /// We use this invariant when searching for stack slots with a given size.
1196     /// See [FunctionGenerator::stack_slot_with_size]
1197     stack_slots: Vec<(StackSlot, StackSize, StackAlignment, AACategory)>,
1198     usercalls: Vec<(UserExternalName, Signature)>,
1199     libcalls: Vec<LibCall>,
1200 }
1201 
1202 impl Resources {
1203     /// Partitions blocks at `block`. Only blocks that can be targeted by branches are considered.
1204     ///
1205     /// The first slice includes all blocks up to and including `block`.
1206     /// The second slice includes all remaining blocks.
1207     fn partition_target_blocks(
1208         &self,
1209         block: Block,
1210     ) -> (&[(Block, BlockSignature)], &[(Block, BlockSignature)]) {
1211         // Blocks are stored in-order and have no gaps, this means that we can simply index them by
1212         // their number. We also need to exclude the entry block since it isn't a valid target.
1213         let target_blocks = &self.blocks[1..];
1214         target_blocks.split_at(block.as_u32() as usize)
1215     }
1216 
1217     /// Returns blocks forward of `block`. Only blocks that can be targeted by branches are considered.
1218     fn forward_blocks(&self, block: Block) -> &[(Block, BlockSignature)] {
1219         let (_, forward_blocks) = self.partition_target_blocks(block);
1220         forward_blocks
1221     }
1222 
1223     /// Generates a slice of `blocks_without_params` ahead of `block`
1224     fn forward_blocks_without_params(&self, block: Block) -> &[Block] {
1225         let partition_point = self.blocks_without_params.partition_point(|b| *b <= block);
1226         &self.blocks_without_params[partition_point..]
1227     }
1228 
1229     /// Generates an iterator of all valid tail call targets. This includes all functions with both
1230     ///  the `tail` calling convention and the same return values as the caller.
1231     fn tail_call_targets<'a>(
1232         &'a self,
1233         caller_sig: &'a Signature,
1234     ) -> impl Iterator<Item = &'a (Signature, SigRef, FuncRef)> {
1235         self.func_refs.iter().filter(|(sig, _, _)| {
1236             sig.call_conv == CallConv::Tail && sig.returns == caller_sig.returns
1237         })
1238     }
1239 }
1240 
1241 impl<'r, 'data> FunctionGenerator<'r, 'data>
1242 where
1243     'data: 'r,
1244 {
1245     pub fn new(
1246         u: &'r mut Unstructured<'data>,
1247         config: &'r Config,
1248         isa: OwnedTargetIsa,
1249         name: UserFuncName,
1250         signature: Signature,
1251         usercalls: Vec<(UserExternalName, Signature)>,
1252         libcalls: Vec<LibCall>,
1253     ) -> Self {
1254         Self {
1255             u,
1256             config,
1257             resources: Resources {
1258                 usercalls,
1259                 libcalls,
1260                 ..Resources::default()
1261             },
1262             isa,
1263             name,
1264             signature,
1265         }
1266     }
1267 
1268     /// Generates a random value for config `param`
1269     fn param(&mut self, param: &RangeInclusive<usize>) -> Result<usize> {
1270         Ok(self.u.int_in_range(param.clone())?)
1271     }
1272 
1273     fn system_callconv(&mut self) -> CallConv {
1274         // TODO: This currently only runs on linux, so this is the only choice
1275         // We should improve this once we generate flags and targets
1276         CallConv::SystemV
1277     }
1278 
1279     /// Finds a stack slot with size of at least n bytes
1280     fn stack_slot_with_size(
1281         &mut self,
1282         n: u32,
1283     ) -> Result<(StackSlot, StackSize, StackAlignment, AACategory)> {
1284         let first = self
1285             .resources
1286             .stack_slots
1287             .partition_point(|&(_slot, size, _align, _category)| size < n);
1288         Ok(*self.u.choose(&self.resources.stack_slots[first..])?)
1289     }
1290 
1291     /// Generates an address that should allow for a store or a load.
1292     ///
1293     /// Addresses aren't generated like other values. They are never stored in variables so that
1294     /// we don't run the risk of returning them from a function, which would make the fuzzer
1295     /// complain since they are different from the interpreter to the backend.
1296     ///
1297     /// `min_size`: Controls the amount of space that the address should have.
1298     ///
1299     /// `aligned`: When passed as true, the resulting address is guaranteed to be aligned
1300     /// on an 8 byte boundary.
1301     ///
1302     /// Returns a valid address and the maximum possible offset that still respects `min_size`.
1303     fn generate_load_store_address(
1304         &mut self,
1305         builder: &mut FunctionBuilder,
1306         min_size: u32,
1307         aligned: bool,
1308     ) -> Result<(Value, u32, AACategory)> {
1309         // TODO: Currently our only source of addresses is stack_addr, but we
1310         // should add global_value, symbol_value eventually
1311         let (addr, available_size, category) = {
1312             let (ss, slot_size, _align, category) = self.stack_slot_with_size(min_size)?;
1313 
1314             // stack_slot_with_size guarantees that slot_size >= min_size
1315             let max_offset = slot_size - min_size;
1316             let offset = if aligned {
1317                 self.u.int_in_range(0..=max_offset / min_size)? * min_size
1318             } else {
1319                 self.u.int_in_range(0..=max_offset)?
1320             };
1321 
1322             let base_addr = builder.ins().stack_addr(I64, ss, offset as i32);
1323             let available_size = slot_size.saturating_sub(offset);
1324             (base_addr, available_size, category)
1325         };
1326 
1327         // TODO: Insert a bunch of amode opcodes here to modify the address!
1328 
1329         // Now that we have an address and a size, we just choose a random offset to return to the
1330         // caller. Preserving min_size bytes.
1331         let max_offset = available_size.saturating_sub(min_size);
1332         Ok((addr, max_offset, category))
1333     }
1334 
1335     // Generates an address and memflags for a load or store.
1336     fn generate_address_and_memflags(
1337         &mut self,
1338         builder: &mut FunctionBuilder,
1339         min_size: u32,
1340         is_atomic: bool,
1341     ) -> Result<(Value, MemFlags, Offset32)> {
1342         // Should we generate an aligned address
1343         // Some backends have issues with unaligned atomics.
1344         // AArch64: https://github.com/bytecodealliance/wasmtime/issues/5483
1345         // RISCV: https://github.com/bytecodealliance/wasmtime/issues/5882
1346         let requires_aligned_atomics = matches!(
1347             self.isa.triple().architecture,
1348             Architecture::Aarch64(_) | Architecture::Riscv64(_)
1349         );
1350         let aligned = if is_atomic && requires_aligned_atomics {
1351             true
1352         } else if min_size > 8 {
1353             // TODO: We currently can't guarantee that a stack_slot will be aligned on a 16 byte
1354             // boundary. We don't have a way to specify alignment when creating stack slots, and
1355             // cranelift only guarantees 8 byte alignment between stack slots.
1356             // See: https://github.com/bytecodealliance/wasmtime/issues/5922#issuecomment-1457926624
1357             false
1358         } else {
1359             bool::arbitrary(self.u)?
1360         };
1361 
1362         let mut flags = MemFlags::new();
1363         // Even if we picked an aligned address, we can always generate unaligned memflags
1364         if aligned && bool::arbitrary(self.u)? {
1365             flags.set_aligned();
1366         }
1367         // If the address is aligned, then we know it won't trap
1368         if aligned && bool::arbitrary(self.u)? {
1369             flags.set_notrap();
1370         }
1371 
1372         let (address, max_offset, category) =
1373             self.generate_load_store_address(builder, min_size, aligned)?;
1374 
1375         // Set the Alias Analysis bits on the memflags
1376         category.update_memflags(&mut flags);
1377 
1378         // Pick an offset to pass into the load/store.
1379         let offset = if aligned {
1380             0
1381         } else {
1382             self.u.int_in_range(0..=max_offset)? as i32
1383         }
1384         .into();
1385 
1386         Ok((address, flags, offset))
1387     }
1388 
1389     /// Get a variable of type `ty` from the current function
1390     fn get_variable_of_type(&mut self, ty: Type) -> Result<Variable> {
1391         let opts = self.resources.vars.get(&ty).map_or(&[][..], Vec::as_slice);
1392         let var = self.u.choose(opts)?;
1393         Ok(*var)
1394     }
1395 
1396     /// Generates an instruction(`iconst`/`fconst`/etc...) to introduce a constant value
1397     fn generate_const(&mut self, builder: &mut FunctionBuilder, ty: Type) -> Result<Value> {
1398         Ok(match self.u.datavalue(ty)? {
1399             DataValue::I8(i) => builder.ins().iconst(ty, i as u8 as i64),
1400             DataValue::I16(i) => builder.ins().iconst(ty, i as u16 as i64),
1401             DataValue::I32(i) => builder.ins().iconst(ty, i as u32 as i64),
1402             DataValue::I64(i) => builder.ins().iconst(ty, i),
1403             DataValue::I128(i) => {
1404                 let hi = builder.ins().iconst(I64, (i >> 64) as i64);
1405                 let lo = builder.ins().iconst(I64, i as i64);
1406                 builder.ins().iconcat(lo, hi)
1407             }
1408             DataValue::F32(f) => builder.ins().f32const(f),
1409             DataValue::F64(f) => builder.ins().f64const(f),
1410             DataValue::V128(bytes) => {
1411                 let data = bytes.to_vec().into();
1412                 let handle = builder.func.dfg.constants.insert(data);
1413                 builder.ins().vconst(ty, handle)
1414             }
1415             _ => unimplemented!(),
1416         })
1417     }
1418 
1419     /// Chooses a random block which can be targeted by a jump / branch.
1420     /// This means any block that is not the first block.
1421     fn generate_target_block(&mut self, source_block: Block) -> Result<Block> {
1422         // We try to mostly generate forward branches to avoid generating an excessive amount of
1423         // infinite loops. But they are still important, so give them a small chance of existing.
1424         let (backwards_blocks, forward_blocks) =
1425             self.resources.partition_target_blocks(source_block);
1426         let ratio = self.config.backwards_branch_ratio;
1427         let block_targets = if !backwards_blocks.is_empty() && self.u.ratio(ratio.0, ratio.1)? {
1428             backwards_blocks
1429         } else {
1430             forward_blocks
1431         };
1432         assert!(!block_targets.is_empty());
1433 
1434         let (block, _) = self.u.choose(block_targets)?.clone();
1435         Ok(block)
1436     }
1437 
1438     fn generate_values_for_block(
1439         &mut self,
1440         builder: &mut FunctionBuilder,
1441         block: Block,
1442     ) -> Result<Vec<Value>> {
1443         let (_, sig) = self.resources.blocks[block.as_u32() as usize].clone();
1444         self.generate_values_for_signature(builder, sig.iter().copied())
1445     }
1446 
1447     fn generate_values_for_signature<I: Iterator<Item = Type>>(
1448         &mut self,
1449         builder: &mut FunctionBuilder,
1450         signature: I,
1451     ) -> Result<Vec<Value>> {
1452         signature
1453             .map(|ty| {
1454                 let var = self.get_variable_of_type(ty)?;
1455                 let val = builder.use_var(var);
1456                 Ok(val)
1457             })
1458             .collect()
1459     }
1460 
1461     /// The terminator that we need to insert has already been picked ahead of time
1462     /// we just need to build the instructions for it
1463     fn insert_terminator(
1464         &mut self,
1465         builder: &mut FunctionBuilder,
1466         source_block: Block,
1467     ) -> Result<()> {
1468         let terminator = self.resources.block_terminators[source_block.as_u32() as usize].clone();
1469 
1470         match terminator {
1471             BlockTerminator::Return => {
1472                 let types: Vec<Type> = {
1473                     let rets = &builder.func.signature.returns;
1474                     rets.iter().map(|p| p.value_type).collect()
1475                 };
1476                 let vals = self.generate_values_for_signature(builder, types.into_iter())?;
1477 
1478                 builder.ins().return_(&vals[..]);
1479             }
1480             BlockTerminator::Jump(target) => {
1481                 let args = self.generate_values_for_block(builder, target)?;
1482                 builder.ins().jump(target, &args[..]);
1483             }
1484             BlockTerminator::Br(left, right) => {
1485                 let left_args = self.generate_values_for_block(builder, left)?;
1486                 let right_args = self.generate_values_for_block(builder, right)?;
1487 
1488                 let condbr_types = [I8, I16, I32, I64, I128];
1489                 let _type = *self.u.choose(&condbr_types[..])?;
1490                 let val = builder.use_var(self.get_variable_of_type(_type)?);
1491                 builder
1492                     .ins()
1493                     .brif(val, left, &left_args[..], right, &right_args[..]);
1494             }
1495             BlockTerminator::BrTable(default, targets) => {
1496                 // Create jump tables on demand
1497                 let mut jt = Vec::with_capacity(targets.len());
1498                 for block in targets {
1499                     let args = self.generate_values_for_block(builder, block)?;
1500                     jt.push(builder.func.dfg.block_call(block, &args))
1501                 }
1502 
1503                 let args = self.generate_values_for_block(builder, default)?;
1504                 let jt_data = JumpTableData::new(builder.func.dfg.block_call(default, &args), &jt);
1505                 let jt = builder.create_jump_table(jt_data);
1506 
1507                 // br_table only supports I32
1508                 let val = builder.use_var(self.get_variable_of_type(I32)?);
1509 
1510                 builder.ins().br_table(val, jt);
1511             }
1512             BlockTerminator::Switch(_type, default, entries) => {
1513                 let mut switch = Switch::new();
1514                 for (&entry, &block) in entries.iter() {
1515                     switch.set_entry(entry, block);
1516                 }
1517 
1518                 let switch_val = builder.use_var(self.get_variable_of_type(_type)?);
1519 
1520                 switch.emit(builder, switch_val, default);
1521             }
1522             BlockTerminator::TailCall(target) | BlockTerminator::TailCallIndirect(target) => {
1523                 let (sig, sig_ref, func_ref) = self
1524                     .resources
1525                     .func_refs
1526                     .iter()
1527                     .find(|(_, _, f)| *f == target)
1528                     .expect("Failed to find previously selected function")
1529                     .clone();
1530 
1531                 let opcode = match terminator {
1532                     BlockTerminator::TailCall(_) => Opcode::ReturnCall,
1533                     BlockTerminator::TailCallIndirect(_) => Opcode::ReturnCallIndirect,
1534                     _ => unreachable!(),
1535                 };
1536 
1537                 insert_call_to_function(self, builder, opcode, &sig, sig_ref, func_ref)?;
1538             }
1539         }
1540 
1541         Ok(())
1542     }
1543 
1544     /// Fills the current block with random instructions
1545     fn generate_instructions(&mut self, builder: &mut FunctionBuilder) -> Result<()> {
1546         for _ in 0..self.param(&self.config.instructions_per_block)? {
1547             let (op, args, rets) = self.u.choose(&OPCODE_SIGNATURES)?;
1548 
1549             // We filter out instructions that aren't supported by the target at this point instead
1550             // of building a single vector of valid instructions at the beginning of function
1551             // generation, to avoid invalidating the corpus when instructions are enabled/disabled.
1552             if !valid_for_target(&self.isa.triple(), *op, &args, &rets) {
1553                 return Err(arbitrary::Error::IncorrectFormat.into());
1554             }
1555 
1556             let inserter = inserter_for_format(op.format());
1557             inserter(self, builder, *op, &args, &rets)?;
1558         }
1559 
1560         Ok(())
1561     }
1562 
1563     fn generate_funcrefs(&mut self, builder: &mut FunctionBuilder) -> Result<()> {
1564         let usercalls: Vec<(ExternalName, Signature)> = self
1565             .resources
1566             .usercalls
1567             .iter()
1568             .map(|(name, signature)| {
1569                 let user_func_ref = builder.func.declare_imported_user_function(name.clone());
1570                 let name = ExternalName::User(user_func_ref);
1571                 (name, signature.clone())
1572             })
1573             .collect();
1574 
1575         let lib_callconv = self.system_callconv();
1576         let libcalls: Vec<(ExternalName, Signature)> = self
1577             .resources
1578             .libcalls
1579             .iter()
1580             .map(|libcall| {
1581                 let pointer_type = Type::int_with_byte_size(
1582                     self.isa.triple().pointer_width().unwrap().bytes().into(),
1583                 )
1584                 .unwrap();
1585                 let signature = libcall.signature(lib_callconv, pointer_type);
1586                 let name = ExternalName::LibCall(*libcall);
1587                 (name, signature)
1588             })
1589             .collect();
1590 
1591         for (name, signature) in usercalls.into_iter().chain(libcalls) {
1592             let sig_ref = builder.import_signature(signature.clone());
1593             let func_ref = builder.import_function(ExtFuncData {
1594                 name,
1595                 signature: sig_ref,
1596                 colocated: self.u.arbitrary()?,
1597             });
1598 
1599             self.resources
1600                 .func_refs
1601                 .push((signature, sig_ref, func_ref));
1602         }
1603 
1604         Ok(())
1605     }
1606 
1607     fn generate_stack_slots(&mut self, builder: &mut FunctionBuilder) -> Result<()> {
1608         for _ in 0..self.param(&self.config.static_stack_slots_per_function)? {
1609             let bytes = self.param(&self.config.static_stack_slot_size)? as u32;
1610             let alignment = self.param(&self.config.stack_slot_alignment_log2)? as u8;
1611             let alignment_bytes = 1 << alignment;
1612 
1613             let ss_data = StackSlotData::new(StackSlotKind::ExplicitSlot, bytes, alignment);
1614             let slot = builder.create_sized_stack_slot(ss_data);
1615 
1616             // Generate one Alias Analysis Category for each slot
1617             let category = *self.u.choose(AACategory::all())?;
1618 
1619             self.resources
1620                 .stack_slots
1621                 .push((slot, bytes, alignment_bytes, category));
1622         }
1623 
1624         self.resources
1625             .stack_slots
1626             .sort_unstable_by_key(|&(_slot, bytes, _align, _category)| bytes);
1627 
1628         Ok(())
1629     }
1630 
1631     /// Zero initializes the stack slot by inserting `stack_store`'s.
1632     fn initialize_stack_slots(&mut self, builder: &mut FunctionBuilder) -> Result<()> {
1633         let i8_zero = builder.ins().iconst(I8, 0);
1634         let i16_zero = builder.ins().iconst(I16, 0);
1635         let i32_zero = builder.ins().iconst(I32, 0);
1636         let i64_zero = builder.ins().iconst(I64, 0);
1637         let i128_zero = builder.ins().uextend(I128, i64_zero);
1638 
1639         for &(slot, init_size, _align, category) in self.resources.stack_slots.iter() {
1640             let mut size = init_size;
1641 
1642             // Insert the largest available store for the remaining size.
1643             while size != 0 {
1644                 let offset = (init_size - size) as i32;
1645                 let (val, filled) = match size {
1646                     sz if sz / 16 > 0 => (i128_zero, 16),
1647                     sz if sz / 8 > 0 => (i64_zero, 8),
1648                     sz if sz / 4 > 0 => (i32_zero, 4),
1649                     sz if sz / 2 > 0 => (i16_zero, 2),
1650                     _ => (i8_zero, 1),
1651                 };
1652                 let addr = builder.ins().stack_addr(I64, slot, offset);
1653 
1654                 // Each stack slot has an associated category, that means we have to set the
1655                 // correct memflags for it. So we can't use `stack_store` directly.
1656                 let mut flags = MemFlags::new();
1657                 flags.set_notrap();
1658                 category.update_memflags(&mut flags);
1659 
1660                 builder.ins().store(flags, val, addr, 0);
1661 
1662                 size -= filled;
1663             }
1664         }
1665         Ok(())
1666     }
1667 
1668     /// Creates a random amount of blocks in this function
1669     fn generate_blocks(&mut self, builder: &mut FunctionBuilder) -> Result<()> {
1670         let extra_block_count = self.param(&self.config.blocks_per_function)?;
1671 
1672         // We must always have at least one block, so we generate the "extra" blocks and add 1 for
1673         // the entry block.
1674         let block_count = 1 + extra_block_count;
1675 
1676         // Blocks need to be sorted in ascending order
1677         self.resources.blocks = (0..block_count)
1678             .map(|i| {
1679                 let is_entry = i == 0;
1680                 let block = builder.create_block();
1681 
1682                 // Optionally mark blocks that are not the entry block as cold
1683                 if !is_entry {
1684                     if bool::arbitrary(self.u)? {
1685                         builder.set_cold_block(block);
1686                     }
1687                 }
1688 
1689                 // The first block has to have the function signature, but for the rest of them we generate
1690                 // a random signature;
1691                 if is_entry {
1692                     builder.append_block_params_for_function_params(block);
1693                     Ok((
1694                         block,
1695                         self.signature.params.iter().map(|a| a.value_type).collect(),
1696                     ))
1697                 } else {
1698                     let sig = self.generate_block_signature()?;
1699                     sig.iter().for_each(|ty| {
1700                         builder.append_block_param(block, *ty);
1701                     });
1702                     Ok((block, sig))
1703                 }
1704             })
1705             .collect::<Result<Vec<_>>>()?;
1706 
1707         // Valid blocks for jump tables have to have no parameters in the signature, and must also
1708         // not be the first block.
1709         self.resources.blocks_without_params = self.resources.blocks[1..]
1710             .iter()
1711             .filter(|(_, sig)| sig.len() == 0)
1712             .map(|(b, _)| *b)
1713             .collect();
1714 
1715         // Compute the block CFG
1716         //
1717         // cranelift-frontend requires us to never generate unreachable blocks
1718         // To ensure this property we start by constructing a main "spine" of blocks. So block1 can
1719         // always jump to block2, and block2 can always jump to block3, etc...
1720         //
1721         // That is not a very interesting CFG, so we introduce variations on that, but always
1722         // ensuring that the property of pointing to the next block is maintained whatever the
1723         // branching mechanism we use.
1724         let blocks = self.resources.blocks.clone();
1725         self.resources.block_terminators = blocks
1726             .iter()
1727             .map(|&(block, _)| {
1728                 let next_block = Block::with_number(block.as_u32() + 1).unwrap();
1729                 let forward_blocks = self.resources.forward_blocks(block);
1730                 let paramless_targets = self.resources.forward_blocks_without_params(block);
1731                 let has_paramless_targets = !paramless_targets.is_empty();
1732                 let next_block_is_paramless = paramless_targets.contains(&next_block);
1733 
1734                 let mut valid_terminators = vec![];
1735 
1736                 if forward_blocks.is_empty() {
1737                     // Return is only valid on the last block.
1738                     valid_terminators.push(BlockTerminatorKind::Return);
1739                 } else {
1740                     // If we have more than one block we can allow terminators that target blocks.
1741                     // TODO: We could add some kind of BrReturn here, to explore edges where we
1742                     // exit in the middle of the function
1743                     valid_terminators.extend_from_slice(&[
1744                         BlockTerminatorKind::Jump,
1745                         BlockTerminatorKind::Br,
1746                         BlockTerminatorKind::BrTable,
1747                     ]);
1748                 }
1749 
1750                 // As the Switch interface only allows targeting blocks without params we need
1751                 // to ensure that the next block has no params, since that one is guaranteed to be
1752                 // picked in either case.
1753                 if has_paramless_targets && next_block_is_paramless {
1754                     valid_terminators.push(BlockTerminatorKind::Switch);
1755                 }
1756 
1757                 // Tail Calls are a block terminator, so we should insert them as any other block
1758                 // terminator. We should ensure that we can select at least one target before considering
1759                 // them as candidate instructions.
1760                 let has_tail_callees = self
1761                     .resources
1762                     .tail_call_targets(&self.signature)
1763                     .next()
1764                     .is_some();
1765                 let is_tail_caller = self.signature.call_conv == CallConv::Tail;
1766 
1767                 let supports_tail_calls = match self.isa.triple().architecture {
1768                     Architecture::Aarch64(_) | Architecture::Riscv64(_) => true,
1769                     // TODO: x64 currently requires frame pointers for tail calls.
1770                     Architecture::X86_64 => self.isa.flags().preserve_frame_pointers(),
1771                     // TODO: Other platforms do not support tail calls yet.
1772                     _ => false,
1773                 };
1774 
1775                 if is_tail_caller && has_tail_callees && supports_tail_calls {
1776                     valid_terminators.extend([
1777                         BlockTerminatorKind::TailCall,
1778                         BlockTerminatorKind::TailCallIndirect,
1779                     ]);
1780                 }
1781 
1782                 let terminator = self.u.choose(&valid_terminators)?;
1783 
1784                 // Choose block targets for the terminators that we picked above
1785                 Ok(match terminator {
1786                     BlockTerminatorKind::Return => BlockTerminator::Return,
1787                     BlockTerminatorKind::Jump => BlockTerminator::Jump(next_block),
1788                     BlockTerminatorKind::Br => {
1789                         BlockTerminator::Br(next_block, self.generate_target_block(block)?)
1790                     }
1791                     // TODO: Allow generating backwards branches here
1792                     BlockTerminatorKind::BrTable => {
1793                         // Make the default the next block, and then we don't have to worry
1794                         // that we can reach it via the targets
1795                         let default = next_block;
1796 
1797                         let target_count = self.param(&self.config.jump_table_entries)?;
1798                         let targets = Result::from_iter(
1799                             (0..target_count).map(|_| self.generate_target_block(block)),
1800                         )?;
1801 
1802                         BlockTerminator::BrTable(default, targets)
1803                     }
1804                     BlockTerminatorKind::Switch => {
1805                         // Make the default the next block, and then we don't have to worry
1806                         // that we can reach it via the entries below
1807                         let default_block = next_block;
1808 
1809                         let _type = *self.u.choose(&[I8, I16, I32, I64, I128][..])?;
1810 
1811                         // Build this into a HashMap since we cannot have duplicate entries.
1812                         let mut entries = HashMap::new();
1813                         for _ in 0..self.param(&self.config.switch_cases)? {
1814                             // The Switch API only allows for entries that are addressable by the index type
1815                             // so we need to limit the range of values that we generate.
1816                             let (ty_min, ty_max) = _type.bounds(false);
1817                             let range_start = self.u.int_in_range(ty_min..=ty_max)?;
1818 
1819                             // We can either insert a contiguous range of blocks or a individual block
1820                             // This is done because the Switch API specializes contiguous ranges.
1821                             let range_size = if bool::arbitrary(self.u)? {
1822                                 1
1823                             } else {
1824                                 self.param(&self.config.switch_max_range_size)?
1825                             } as u128;
1826 
1827                             // Build the switch entries
1828                             for i in 0..range_size {
1829                                 let index = range_start.wrapping_add(i) % ty_max;
1830                                 let block = *self
1831                                     .u
1832                                     .choose(self.resources.forward_blocks_without_params(block))?;
1833 
1834                                 entries.insert(index, block);
1835                             }
1836                         }
1837 
1838                         BlockTerminator::Switch(_type, default_block, entries)
1839                     }
1840                     BlockTerminatorKind::TailCall => {
1841                         let targets = self
1842                             .resources
1843                             .tail_call_targets(&self.signature)
1844                             .collect::<Vec<_>>();
1845                         let (_, _, funcref) = *self.u.choose(&targets[..])?;
1846                         BlockTerminator::TailCall(*funcref)
1847                     }
1848                     BlockTerminatorKind::TailCallIndirect => {
1849                         let targets = self
1850                             .resources
1851                             .tail_call_targets(&self.signature)
1852                             .collect::<Vec<_>>();
1853                         let (_, _, funcref) = *self.u.choose(&targets[..])?;
1854                         BlockTerminator::TailCallIndirect(*funcref)
1855                     }
1856                 })
1857             })
1858             .collect::<Result<_>>()?;
1859 
1860         Ok(())
1861     }
1862 
1863     fn generate_block_signature(&mut self) -> Result<BlockSignature> {
1864         let param_count = self.param(&self.config.block_signature_params)?;
1865 
1866         let mut params = Vec::with_capacity(param_count);
1867         for _ in 0..param_count {
1868             params.push(self.u._type((&*self.isa).supports_simd())?);
1869         }
1870         Ok(params)
1871     }
1872 
1873     fn build_variable_pool(&mut self, builder: &mut FunctionBuilder) -> Result<()> {
1874         let block = builder.current_block().unwrap();
1875 
1876         // Define variables for the function signature
1877         let mut vars: Vec<_> = builder
1878             .func
1879             .signature
1880             .params
1881             .iter()
1882             .map(|param| param.value_type)
1883             .zip(builder.block_params(block).iter().copied())
1884             .collect();
1885 
1886         // Create a pool of vars that are going to be used in this function
1887         for _ in 0..self.param(&self.config.vars_per_function)? {
1888             let ty = self.u._type((&*self.isa).supports_simd())?;
1889             let value = self.generate_const(builder, ty)?;
1890             vars.push((ty, value));
1891         }
1892 
1893         for (id, (ty, value)) in vars.into_iter().enumerate() {
1894             let var = Variable::new(id);
1895             builder.declare_var(var, ty);
1896             builder.def_var(var, value);
1897             self.resources
1898                 .vars
1899                 .entry(ty)
1900                 .or_insert_with(Vec::new)
1901                 .push(var);
1902         }
1903 
1904         Ok(())
1905     }
1906 
1907     /// We generate a function in multiple stages:
1908     ///
1909     /// * First we generate a random number of empty blocks
1910     /// * Then we generate a random pool of variables to be used throughout the function
1911     /// * We then visit each block and generate random instructions
1912     ///
1913     /// Because we generate all blocks and variables up front we already know everything that
1914     /// we need when generating instructions (i.e. jump targets / variables)
1915     pub fn generate(mut self) -> Result<Function> {
1916         let mut fn_builder_ctx = FunctionBuilderContext::new();
1917         let mut func = Function::with_name_signature(self.name.clone(), self.signature.clone());
1918 
1919         let mut builder = FunctionBuilder::new(&mut func, &mut fn_builder_ctx);
1920 
1921         // Build the function references before generating the block CFG since we store
1922         // function references in the CFG.
1923         self.generate_funcrefs(&mut builder)?;
1924         self.generate_blocks(&mut builder)?;
1925 
1926         // Function preamble
1927         self.generate_stack_slots(&mut builder)?;
1928 
1929         // Main instruction generation loop
1930         for (block, block_sig) in self.resources.blocks.clone().into_iter() {
1931             let is_block0 = block.as_u32() == 0;
1932             builder.switch_to_block(block);
1933 
1934             if is_block0 {
1935                 // The first block is special because we must create variables both for the
1936                 // block signature and for the variable pool. Additionally, we must also define
1937                 // initial values for all variables that are not the function signature.
1938                 self.build_variable_pool(&mut builder)?;
1939 
1940                 // Stack slots have random bytes at the beginning of the function
1941                 // initialize them to a constant value so that execution stays predictable.
1942                 self.initialize_stack_slots(&mut builder)?;
1943             } else {
1944                 // Define variables for the block params
1945                 for (i, ty) in block_sig.iter().enumerate() {
1946                     let var = self.get_variable_of_type(*ty)?;
1947                     let block_param = builder.block_params(block)[i];
1948                     builder.def_var(var, block_param);
1949                 }
1950             }
1951 
1952             // Generate block instructions
1953             self.generate_instructions(&mut builder)?;
1954 
1955             // Insert a terminator to safely exit the block
1956             self.insert_terminator(&mut builder, block)?;
1957         }
1958 
1959         builder.seal_all_blocks();
1960         builder.finalize();
1961 
1962         Ok(func)
1963     }
1964 }
1965