Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Split modules (#11993)

Khagan (Khan) Karimovb6f59f05e8e3
diff --git a/crates/fuzzing/src/generators.rs b/crates/fuzzing/src/generators.rs
index c64f2dcbd9..3412eb8794 100644
--- a/crates/fuzzing/src/generators.rs
+++ b/crates/fuzzing/src/generators.rs
@@ -13,13 +13,13 @@ mod async_config;
mod codegen_settings;
pub mod component_types;
mod config;
+pub mod gc_ops;
mod instance_allocation_strategy;
mod memory;
mod module;
mod pooling_config;
mod single_inst_module;
mod stacks;
-pub mod table_ops;
mod value;
mod wast_test;
@@ -27,6 +27,7 @@ pub use async_config::AsyncConfig;
pub use codegen_settings::CodegenSettings;
pub use config::CompilerStrategy;
pub use config::{Config, WasmtimeConfig};
+pub use gc_ops::{limits::GcOpsLimits, ops::GcOps};
pub use instance_allocation_strategy::InstanceAllocationStrategy;
pub use memory::{HeapImage, MemoryAccesses, MemoryConfig};
pub use module::ModuleConfig;
diff --git a/crates/fuzzing/src/generators/gc_ops/limits.rs b/crates/fuzzing/src/generators/gc_ops/limits.rs
new file mode 100644
index 0000000000..dd97888cd0
--- /dev/null
+++ b/crates/fuzzing/src/generators/gc_ops/limits.rs
@@ -0,0 +1,50 @@
+//! Limits for the `gc` operations.
+
+use serde::{Deserialize, Serialize};
+use std::ops::RangeInclusive;
+
+/// Range for the number of parameters.
+pub const NUM_PARAMS_RANGE: RangeInclusive<u32> = 0..=10;
+/// Range for the maximum number of types.
+pub const MAX_TYPES_RANGE: RangeInclusive<u32> = 0..=32;
+/// Range for the number of globals.
+pub const NUM_GLOBALS_RANGE: RangeInclusive<u32> = 0..=10;
+/// Range for the table size.
+pub const TABLE_SIZE_RANGE: RangeInclusive<u32> = 0..=100;
+/// Range for the maximum number of rec groups.
+pub const MAX_REC_GROUPS_RANGE: RangeInclusive<u32> = 0..=10;
+/// Maximum number of operations.
+pub const MAX_OPS: usize = 100;
+
+/// Limits controlling the structure of a generated Wasm module.
+#[derive(Debug, Default, Serialize, Deserialize)]
+pub struct GcOpsLimits {
+ pub(crate) num_params: u32,
+ pub(crate) num_globals: u32,
+ pub(crate) table_size: u32,
+ pub(crate) max_rec_groups: u32,
+ pub(crate) max_types: u32,
+}
+impl GcOpsLimits {
+ /// Fixup the limits to ensure they are within the valid range.
+ pub(crate) fn fixup(&mut self) {
+ // NB: Exhaustively match so that we remember to fixup any other new
+ // limits we add in the future.
+ let Self {
+ num_params,
+ num_globals,
+ table_size,
+ max_rec_groups,
+ max_types,
+ } = self;
+
+ let clamp = |limit: &mut u32, range: RangeInclusive<u32>| {
+ *limit = (*limit).clamp(*range.start(), *range.end())
+ };
+ clamp(table_size, TABLE_SIZE_RANGE);
+ clamp(num_params, NUM_PARAMS_RANGE);
+ clamp(num_globals, NUM_GLOBALS_RANGE);
+ clamp(max_rec_groups, MAX_REC_GROUPS_RANGE);
+ clamp(max_types, MAX_TYPES_RANGE);
+ }
+}
diff --git a/crates/fuzzing/src/generators/gc_ops/mod.rs b/crates/fuzzing/src/generators/gc_ops/mod.rs
new file mode 100644
index 0000000000..9a492cbee1
--- /dev/null
+++ b/crates/fuzzing/src/generators/gc_ops/mod.rs
@@ -0,0 +1,9 @@
+//! GC ops generator: split from the previous monolithic file.
+
+pub mod limits;
+pub mod mutator;
+pub mod ops;
+pub mod types;
+
+#[cfg(test)]
+mod tests;
diff --git a/crates/fuzzing/src/generators/gc_ops/mutator.rs b/crates/fuzzing/src/generators/gc_ops/mutator.rs
new file mode 100644
index 0000000000..581408d383
--- /dev/null
+++ b/crates/fuzzing/src/generators/gc_ops/mutator.rs
@@ -0,0 +1,128 @@
+//! Mutators for the `gc` operations.
+
+use crate::generators::gc_ops::{
+ limits::{
+ GcOpsLimits, MAX_OPS, MAX_REC_GROUPS_RANGE, MAX_TYPES_RANGE, NUM_GLOBALS_RANGE,
+ NUM_PARAMS_RANGE, TABLE_SIZE_RANGE,
+ },
+ ops::{GcOp, GcOps},
+ types::{RecGroupId, TypeId, Types},
+};
+
+use mutatis::mutators as m;
+use mutatis::{Candidates, Context, DefaultMutate, Generate, Mutate, Result as MutResult};
+
+/// A mutator for the gc ops
+#[derive(Debug)]
+pub struct GcOpsMutator;
+
+impl Mutate<GcOps> for GcOpsMutator {
+ fn mutate(&mut self, c: &mut Candidates<'_>, ops: &mut GcOps) -> mutatis::Result<()> {
+ if !c.shrink() {
+ c.mutation(|ctx| {
+ if let Some(idx) = ctx.rng().gen_index(ops.ops.len() + 1) {
+ let stack = ops.abstract_stack_depth(idx);
+ let (op, _new_stack_size) = GcOp::generate(ctx, &ops, stack)?;
+ ops.ops.insert(idx, op);
+ }
+ Ok(())
+ })?;
+ }
+
+ if !ops.ops.is_empty() {
+ c.mutation(|ctx| {
+ let idx = ctx
+ .rng()
+ .gen_index(ops.ops.len())
+ .expect("ops is not empty");
+ ops.ops.remove(idx);
+ Ok(())
+ })?;
+ }
+ Ok(())
+ }
+}
+
+impl DefaultMutate for GcOps {
+ type DefaultMutate = GcOpsMutator;
+}
+
+impl Default for GcOpsMutator {
+ fn default() -> Self {
+ GcOpsMutator
+ }
+}
+
+impl<'a> arbitrary::Arbitrary<'a> for GcOps {
+ fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
+ let mut session = mutatis::Session::new().seed(u.arbitrary()?);
+ session
+ .generate()
+ .map_err(|_| arbitrary::Error::IncorrectFormat)
+ }
+}
+
+impl Generate<GcOps> for GcOpsMutator {
+ fn generate(&mut self, ctx: &mut Context) -> MutResult<GcOps> {
+ let num_params = m::range(NUM_PARAMS_RANGE).generate(ctx)?;
+ let num_globals = m::range(NUM_GLOBALS_RANGE).generate(ctx)?;
+ let table_size = m::range(TABLE_SIZE_RANGE).generate(ctx)?;
+
+ let max_rec_groups = m::range(MAX_REC_GROUPS_RANGE).generate(ctx)?;
+ let max_types = m::range(MAX_TYPES_RANGE).generate(ctx)?;
+
+ let mut ops = GcOps {
+ limits: GcOpsLimits {
+ num_params,
+ num_globals,
+ table_size,
+ max_rec_groups,
+ max_types,
+ },
+ ops: {
+ let mut v = vec![GcOp::Null(), GcOp::Drop(), GcOp::Gc()];
+ if num_params > 0 {
+ v.push(GcOp::LocalSet(0));
+ v.push(GcOp::LocalGet(0));
+ }
+ if num_globals > 0 {
+ v.push(GcOp::GlobalSet(0));
+ v.push(GcOp::GlobalGet(0));
+ }
+ if max_types > 0 {
+ v.push(GcOp::StructNew(0));
+ }
+ v
+ },
+ types: Types::new(),
+ };
+
+ for i in 0..ops.limits.max_rec_groups {
+ ops.types.insert_rec_group(RecGroupId(i));
+ }
+
+ if ops.limits.max_rec_groups > 0 {
+ for i in 0..ops.limits.max_types {
+ let tid = TypeId(i);
+ let gid = RecGroupId(m::range(0..=ops.limits.max_rec_groups - 1).generate(ctx)?);
+
+ ops.types.insert_empty_struct(tid, gid);
+ }
+ }
+
+ let mut stack: usize = 0;
+
+ while ops.ops.len() < MAX_OPS {
+ let (op, new_stack_len) = GcOp::generate(ctx, &ops, stack)?;
+ ops.ops.push(op);
+ stack = new_stack_len;
+ }
+
+ // Drop any leftover refs on the stack.
+ for _ in 0..stack {
+ ops.ops.push(GcOp::Drop());
+ }
+
+ Ok(ops)
+ }
+}
diff --git a/crates/fuzzing/src/generators/gc_ops/ops.rs b/crates/fuzzing/src/generators/gc_ops/ops.rs
new file mode 100644
index 0000000000..4933e2c36c
--- /dev/null
+++ b/crates/fuzzing/src/generators/gc_ops/ops.rs
@@ -0,0 +1,514 @@
+//! Operations for the `gc` operations.
+
+use crate::generators::gc_ops::{
+ limits::GcOpsLimits,
+ types::{CompositeType, RecGroupId, StructType, TypeId, Types},
+};
+use mutatis::{Context, Generate, mutators as m};
+use serde::{Deserialize, Serialize};
+use smallvec::SmallVec;
+use std::collections::BTreeMap;
+use wasm_encoder::{
+ CodeSection, ConstExpr, EntityType, ExportKind, ExportSection, Function, FunctionSection,
+ GlobalSection, ImportSection, Instruction, Module, RefType, TableSection, TableType,
+ TypeSection, ValType,
+};
+
+/// A description of a Wasm module that makes a series of `externref` table
+/// operations.
+#[derive(Debug, Default, Serialize, Deserialize)]
+pub struct GcOps {
+ pub(crate) limits: GcOpsLimits,
+ pub(crate) ops: Vec<GcOp>,
+ pub(crate) types: Types,
+}
+
+impl GcOps {
+ /// Serialize this module into a Wasm binary.
+ ///
+ /// The module requires several function imports. See this function's
+ /// implementation for their exact types.
+ ///
+ /// The single export of the module is a function "run" that takes
+ /// `self.num_params` parameters of type `externref`.
+ ///
+ /// The "run" function does not terminate; you should run it with limited
+ /// fuel. It also is not guaranteed to avoid traps: it may access
+ /// out-of-bounds of the table.
+ pub fn to_wasm_binary(&mut self) -> Vec<u8> {
+ self.fixup();
+
+ let mut module = Module::new();
+
+ // Encode the types for all functions that we are using.
+ let mut types = TypeSection::new();
+
+ // 0: "gc"
+ types.ty().function(
+ vec![],
+ // Return a bunch of stuff from `gc` so that we exercise GCing when
+ // there is return pointer space allocated on the stack. This is
+ // especially important because the x64 backend currently
+ // dynamically adjusts the stack pointer for each call that uses
+ // return pointers rather than statically allocating space in the
+ // stack frame.
+ vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
+ );
+
+ // 1: "run"
+ let mut params: Vec<ValType> = Vec::with_capacity(self.limits.num_params as usize);
+ for _i in 0..self.limits.num_params {
+ params.push(ValType::EXTERNREF);
+ }
+ let results = vec![];
+ types.ty().function(params, results);
+
+ // 2: `take_refs`
+ types.ty().function(
+ vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
+ vec![],
+ );
+
+ // 3: `make_refs`
+ types.ty().function(
+ vec![],
+ vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
+ );
+
+ // 4: `take_struct`
+ types.ty().function(
+ vec![ValType::Ref(RefType {
+ nullable: false,
+ heap_type: wasm_encoder::HeapType::ANY,
+ })],
+ vec![],
+ );
+
+ let struct_type_base: u32 = types.len();
+
+ let mut rec_groups: BTreeMap<RecGroupId, Vec<TypeId>> = self
+ .types
+ .rec_groups
+ .iter()
+ .copied()
+ .map(|id| (id, Vec::new()))
+ .collect();
+
+ for (id, ty) in self.types.type_defs.iter() {
+ rec_groups.entry(ty.rec_group).or_default().push(id.clone());
+ }
+
+ let encode_ty_id = |ty_id: &TypeId| -> wasm_encoder::SubType {
+ let def = &self.types.type_defs[ty_id];
+ match &def.composite_type {
+ CompositeType::Struct(StructType {}) => wasm_encoder::SubType {
+ is_final: true,
+ supertype_idx: None,
+ composite_type: wasm_encoder::CompositeType {
+ inner: wasm_encoder::CompositeInnerType::Struct(wasm_encoder::StructType {
+ fields: Box::new([]),
+ }),
+ shared: false,
+ },
+ },
+ }
+ };
+
+ let mut struct_count = 0;
+
+ for type_ids in rec_groups.values() {
+ let members: Vec<wasm_encoder::SubType> = type_ids.iter().map(encode_ty_id).collect();
+ types.ty().rec(members);
+ struct_count += type_ids.len() as u32;
+ }
+
+ let typed_fn_type_base: u32 = struct_type_base + struct_count;
+
+ for i in 0..struct_count {
+ let concrete = struct_type_base + i;
+ types.ty().function(
+ vec![ValType::Ref(RefType {
+ nullable: false,
+ heap_type: wasm_encoder::HeapType::Concrete(concrete),
+ })],
+ vec![],
+ );
+ }
+
+ // Import the GC function.
+ let mut imports = ImportSection::new();
+ imports.import("", "gc", EntityType::Function(0));
+ imports.import("", "take_refs", EntityType::Function(2));
+ imports.import("", "make_refs", EntityType::Function(3));
+ imports.import("", "take_struct", EntityType::Function(4));
+
+ // For each of our concrete struct types, define a function
+ // import that takes an argument of that concrete type.
+ let typed_first_func_index: u32 = imports.len();
+
+ for i in 0..struct_count {
+ let ty_idx = typed_fn_type_base + i;
+ let name = format!("take_struct_{}", struct_type_base + i);
+ imports.import("", &name, EntityType::Function(ty_idx));
+ }
+
+ // Define our table.
+ let mut tables = TableSection::new();
+ tables.table(TableType {
+ element_type: RefType::EXTERNREF,
+ minimum: u64::from(self.limits.table_size),
+ maximum: None,
+ table64: false,
+ shared: false,
+ });
+ // Define our globals.
+ let mut globals = GlobalSection::new();
+ for _ in 0..self.limits.num_globals {
+ globals.global(
+ wasm_encoder::GlobalType {
+ val_type: wasm_encoder::ValType::EXTERNREF,
+ mutable: true,
+ shared: false,
+ },
+ &ConstExpr::ref_null(wasm_encoder::HeapType::EXTERN),
+ );
+ }
+
+ // Define the "run" function export.
+ let mut functions = FunctionSection::new();
+ let mut exports = ExportSection::new();
+
+ let run_defined_idx = functions.len();
+ functions.function(1);
+ let run_func_index = imports.len() + run_defined_idx;
+ exports.export("run", ExportKind::Func, run_func_index);
+
+ // Give ourselves one scratch local that we can use in various `GcOp`
+ // implementations.
+ let local_decls: Vec<(u32, ValType)> = vec![(1, ValType::EXTERNREF)];
+
+ let mut func = Function::new(local_decls);
+ func.instruction(&Instruction::Loop(wasm_encoder::BlockType::Empty));
+ for op in &self.ops {
+ op.insert(
+ &mut func,
+ self.limits.num_params,
+ struct_type_base,
+ typed_first_func_index,
+ );
+ }
+ func.instruction(&Instruction::Br(0));
+ func.instruction(&Instruction::End);
+ func.instruction(&Instruction::End);
+
+ let mut code = CodeSection::new();
+ code.function(&func);
+
+ module
+ .section(&types)
+ .section(&imports)
+ .section(&functions)
+ .section(&tables)
+ .section(&globals)
+ .section(&exports)
+ .section(&code);
+
+ module.finish()
+ }
+
+ /// Computes the abstract stack depth after executing all operations
+ pub fn abstract_stack_depth(&self, index: usize) -> usize {
+ debug_assert!(index <= self.ops.len());
+ let mut stack: usize = 0;
+ for op in self.ops.iter().take(index) {
+ let pop = op.operands_len();
+ let push = op.results_len();
+ stack = stack.saturating_sub(pop);
+ stack += push;
+ }
+ stack
+ }
+
+ /// Fixes this test case such that it becomes valid.
+ ///
+ /// This is necessary because a random mutation (e.g. removing an op in the
+ /// middle of our sequence) might have made it so that subsequent ops won't
+ /// have their expected operand types on the Wasm stack
+ /// anymore. Furthermore, because we serialize and deserialize test cases,
+ /// and libFuzzer will occasionally mutate those serialized bytes directly,
+ /// rather than use one of our custom mutations, we have no guarantee that
+ /// pre-mutation test cases are even valid! Therefore, we always call this
+ /// method before translating this "AST"-style representation into a raw
+ /// Wasm binary.
+ pub fn fixup(&mut self) {
+ self.limits.fixup();
+ self.types.fixup(&self.limits);
+
+ let mut new_ops = Vec::with_capacity(self.ops.len());
+ let mut stack = 0;
+
+ for mut op in self.ops.iter().copied() {
+ if self.limits.max_types == 0
+ && matches!(
+ op,
+ GcOp::StructNew(..) | GcOp::TakeStructCall(..) | GcOp::TakeTypedStructCall(..)
+ )
+ {
+ continue;
+ }
+ if self.limits.num_params == 0 && matches!(op, GcOp::LocalGet(..) | GcOp::LocalSet(..))
+ {
+ continue;
+ }
+ if self.limits.num_globals == 0
+ && matches!(op, GcOp::GlobalGet(..) | GcOp::GlobalSet(..))
+ {
+ continue;
+ }
+
+ op.fixup(&self.limits);
+
+ let mut temp = SmallVec::<[_; 4]>::new();
+
+ while stack < op.operands_len() {
+ temp.push(GcOp::Null());
+ stack += 1;
+ }
+
+ temp.push(op);
+ stack = stack - op.operands_len() + op.results_len();
+
+ new_ops.extend(temp);
+ }
+
+ // Insert drops to balance the final stack state
+ for _ in 0..stack {
+ new_ops.push(GcOp::Drop());
+ }
+
+ self.ops = new_ops;
+ }
+
+ /// Attempts to remove the last opcode from the sequence.
+ ///
+ /// Returns `true` if an opcode was successfully removed, or `false` if the
+ /// list was already empty.
+ pub fn pop(&mut self) -> bool {
+ self.ops.pop().is_some()
+ }
+}
+
+macro_rules! define_gc_ops {
+ (
+ $(
+ $op:ident $( ( $($limit_var:ident : $limit:expr => $ty:ty),* ) )? : $params:expr => $results:expr ,
+ )*
+ ) => {
+ /// The operations for the `gc` operations.
+ #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
+ pub(crate) enum GcOp {
+ $(
+ $op ( $( $($ty),* )? ),
+ )*
+ }
+
+ /// Names of the operations for testing purposes.
+ #[cfg(test)]
+ pub const OP_NAMES: &'static[&'static str] = &[
+ $(
+ stringify!($op),
+ )*
+ ];
+
+ impl GcOp {
+ #[cfg(test)]
+ pub fn name(&self) -> &'static str {
+ match self {
+ $(
+ Self::$op (..) => stringify!($op),
+ )*
+ }
+ }
+
+ pub fn operands_len(&self) -> usize {
+ match self {
+ $(
+ Self::$op (..) => $params,
+ )*
+ }
+ }
+
+ pub fn results_len(&self) -> usize {
+ match self {
+ $(
+ Self::$op (..) => $results,
+ )*
+ }
+ }
+ }
+
+ $(
+ #[allow(non_snake_case, reason = "macro-generated code")]
+ fn $op(
+ _ctx: &mut mutatis::Context,
+ _limits: &GcOpsLimits,
+ stack: usize,
+ ) -> mutatis::Result<(GcOp, usize)> {
+ #[allow(unused_comparisons, reason = "macro-generated code")]
+ {
+ debug_assert!(stack >= $params);
+ }
+
+ let op = GcOp::$op(
+ $($({
+ let limit_fn = $limit as fn(&GcOpsLimits) -> $ty;
+ let limit = (limit_fn)(_limits);
+ debug_assert!(limit > 0);
+ m::range(0..=limit - 1).generate(_ctx)?
+ }),*)?
+ );
+ let new_stack = stack - $params + $results;
+ Ok((op, new_stack))
+ }
+ )*
+
+ impl GcOp {
+ fn fixup(&mut self, limits: &GcOpsLimits) {
+ match self {
+ $(
+ Self::$op( $( $( $limit_var ),* )? ) => {
+ $( $(
+ let limit_fn = $limit as fn(&GcOpsLimits) -> $ty;
+ let limit = (limit_fn)(limits);
+ debug_assert!(limit > 0);
+ *$limit_var = *$limit_var % limit;
+ )* )?
+ }
+ )*
+ }
+ }
+
+ pub(crate) fn generate(
+ ctx: &mut mutatis::Context,
+ ops: &GcOps,
+ stack: usize,
+ ) -> mutatis::Result<(GcOp, usize)> {
+ let mut valid_choices: Vec<
+ fn(&mut Context, &GcOpsLimits, usize) -> mutatis::Result<(GcOp, usize)>
+ > = vec![];
+ $(
+ #[allow(unused_comparisons, reason = "macro-generated code")]
+ if stack >= $params $($(
+ && {
+ let limit_fn = $limit as fn(&GcOpsLimits) -> $ty;
+ let limit = (limit_fn)(&ops.limits);
+ limit > 0
+ }
+ )*)? {
+ valid_choices.push($op);
+ }
+ )*
+
+ let f = *ctx.rng()
+ .choose(&valid_choices)
+ .expect("should always have a valid op choice");
+
+ (f)(ctx, &ops.limits, stack)
+ }
+ }
+ };
+}
+
+define_gc_ops! {
+ Gc : 0 => 3,
+
+ MakeRefs : 0 => 3,
+ TakeRefs : 3 => 0,
+
+ // Add one to make sure that out of bounds table accesses are possible, but still rare.
+ TableGet(elem_index: |ops| ops.table_size + 1 => u32) : 0 => 1,
+ TableSet(elem_index: |ops| ops.table_size + 1 => u32) : 1 => 0,
+
+ GlobalGet(global_index: |ops| ops.num_globals => u32) : 0 => 1,
+ GlobalSet(global_index: |ops| ops.num_globals => u32) : 1 => 0,
+
+ LocalGet(local_index: |ops| ops.num_params => u32) : 0 => 1,
+ LocalSet(local_index: |ops| ops.num_params => u32) : 1 => 0,
+
+ StructNew(type_index: |ops| ops.max_types => u32) : 0 => 0,
+ TakeStructCall(type_index: |ops| ops.max_types => u32) : 1 => 0,
+ TakeTypedStructCall(type_index: |ops| ops.max_types => u32) : 1 => 0,
+
+ Drop : 1 => 0,
+
+ Null : 0 => 1,
+}
+
+impl GcOp {
+ fn insert(
+ self,
+ func: &mut Function,
+ scratch_local: u32,
+ struct_type_base: u32,
+ typed_first_func_index: u32,
+ ) {
+ let gc_func_idx = 0;
+ let take_refs_func_idx = 1;
+ let make_refs_func_idx = 2;
+ let take_structref_idx = 3;
+
+ match self {
+ Self::Gc() => {
+ func.instruction(&Instruction::Call(gc_func_idx));
+ }
+ Self::MakeRefs() => {
+ func.instruction(&Instruction::Call(make_refs_func_idx));
+ }
+ Self::TakeRefs() => {
+ func.instruction(&Instruction::Call(take_refs_func_idx));
+ }
+ Self::TableGet(x) => {
+ func.instruction(&Instruction::I32Const(x.cast_signed()));
+ func.instruction(&Instruction::TableGet(0));
+ }
+ Self::TableSet(x) => {
+ func.instruction(&Instruction::LocalSet(scratch_local));
+ func.instruction(&Instruction::I32Const(x.cast_signed()));
+ func.instruction(&Instruction::LocalGet(scratch_local));
+ func.instruction(&Instruction::TableSet(0));
+ }
+ Self::GlobalGet(x) => {
+ func.instruction(&Instruction::GlobalGet(x));
+ }
+ Self::GlobalSet(x) => {
+ func.instruction(&Instruction::GlobalSet(x));
+ }
+ Self::LocalGet(x) => {
+ func.instruction(&Instruction::LocalGet(x));
+ }
+ Self::LocalSet(x) => {
+ func.instruction(&Instruction::LocalSet(x));
+ }
+ Self::Drop() => {
+ func.instruction(&Instruction::Drop);
+ }
+ Self::Null() => {
+ func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::EXTERN));
+ }
+ Self::StructNew(x) => {
+ func.instruction(&Instruction::StructNew(x + struct_type_base));
+ func.instruction(&Instruction::Call(take_structref_idx));
+ }
+ Self::TakeStructCall(x) => {
+ func.instruction(&Instruction::StructNew(x + struct_type_base));
+ func.instruction(&Instruction::Call(take_structref_idx));
+ }
+ Self::TakeTypedStructCall(x) => {
+ let s = struct_type_base + x;
+ let f = typed_first_func_index + x;
+ func.instruction(&Instruction::StructNew(s));
+ func.instruction(&Instruction::Call(f));
+ }
+ }
+ }
+}
diff --git a/crates/fuzzing/src/generators/gc_ops/tests.rs b/crates/fuzzing/src/generators/gc_ops/tests.rs
new file mode 100644
index 0000000000..b5907c0f84
--- /dev/null
+++ b/crates/fuzzing/src/generators/gc_ops/tests.rs
@@ -0,0 +1,299 @@
+use crate::generators::gc_ops::{
+ limits::GcOpsLimits,
+ ops::{GcOp, GcOps, OP_NAMES},
+ types::{RecGroupId, TypeId, Types},
+};
+use mutatis;
+use rand::rngs::StdRng;
+use rand::{Rng, SeedableRng};
+use wasmparser;
+use wasmprinter;
+
+/// Creates empty GcOps
+fn empty_test_ops() -> GcOps {
+ let mut t = GcOps {
+ limits: GcOpsLimits {
+ num_params: 5,
+ num_globals: 5,
+ table_size: 5,
+ max_rec_groups: 5,
+ max_types: 5,
+ },
+ ops: vec![],
+ types: Types::new(),
+ };
+ for i in 0..t.limits.max_rec_groups {
+ t.types.insert_rec_group(RecGroupId(i));
+ }
+ t
+}
+
+/// Creates GcOps with all default opcodes
+fn test_ops(num_params: u32, num_globals: u32, table_size: u32) -> GcOps {
+ let mut t = GcOps {
+ limits: GcOpsLimits {
+ num_params,
+ num_globals,
+ table_size,
+ max_rec_groups: 7,
+ max_types: 10,
+ },
+ ops: vec![
+ GcOp::Null(),
+ GcOp::Drop(),
+ GcOp::Gc(),
+ GcOp::LocalSet(0),
+ GcOp::LocalGet(0),
+ GcOp::GlobalSet(0),
+ GcOp::GlobalGet(0),
+ GcOp::StructNew(0),
+ ],
+ types: Types::new(),
+ };
+ for i in 0..t.limits.max_rec_groups {
+ t.types.insert_rec_group(RecGroupId(i));
+ }
+
+ if t.limits.max_rec_groups > 0 {
+ let mut rng = StdRng::seed_from_u64(0xC0FFEE);
+ for i in 0..t.limits.max_types {
+ let gid = RecGroupId(rng.gen_range(0..t.limits.max_rec_groups));
+ t.types.insert_empty_struct(TypeId(i), gid);
+ }
+ }
+ t
+}
+
+#[test]
+fn mutate_gc_ops_with_default_mutator() -> mutatis::Result<()> {
+ let _ = env_logger::try_init();
+ let mut res = test_ops(5, 5, 5);
+
+ let mut session = mutatis::Session::new();
+
+ for _ in 0..2048 {
+ session.mutate(&mut res)?;
+ let wasm = res.to_wasm_binary();
+
+ let feats = wasmparser::WasmFeatures::default();
+ feats.reference_types();
+ feats.gc();
+ let mut validator = wasmparser::Validator::new_with_features(feats);
+
+ let wat = wasmprinter::print_bytes(&wasm).expect("[-] Failed .print_bytes(&wasm).");
+ let result = validator.validate_all(&wasm);
+ log::debug!("{wat}");
+ assert!(
+ result.is_ok(),
+ "\n[-] Invalid wat: {}\n\t\t==== Failed Wat ====\n{}",
+ result.err().expect("[-] Failed .err() in assert macro."),
+ wat
+ );
+ }
+ Ok(())
+}
+
+#[test]
+fn struct_new_removed_when_no_types() -> mutatis::Result<()> {
+ let _ = env_logger::try_init();
+
+ let mut ops = test_ops(0, 0, 0);
+ ops.limits.max_types = 0;
+ ops.ops = vec![GcOp::StructNew(42)];
+
+ let _ = ops.fixup();
+
+ assert!(
+ ops.ops.iter().all(|op| !matches!(op, GcOp::StructNew(..))),
+ "StructNew should be removed when there are no types"
+ );
+ Ok(())
+}
+
+#[test]
+fn local_ops_removed_when_no_params() -> mutatis::Result<()> {
+ let _ = env_logger::try_init();
+
+ let mut ops = test_ops(0, 0, 0);
+ ops.limits.num_params = 0;
+ ops.ops = vec![GcOp::LocalGet(42), GcOp::LocalSet(99)];
+
+ ops.fixup();
+
+ assert!(
+ ops.ops
+ .iter()
+ .all(|op| !matches!(op, GcOp::LocalGet(..) | GcOp::LocalSet(..))),
+ "LocalGet/LocalSet should be removed when there are no params"
+ );
+ Ok(())
+}
+
+#[test]
+fn global_ops_removed_when_no_globals() -> mutatis::Result<()> {
+ let _ = env_logger::try_init();
+
+ let mut ops = test_ops(0, 0, 0);
+ ops.limits.num_globals = 0;
+ ops.ops = vec![GcOp::GlobalGet(42), GcOp::GlobalSet(99)];
+
+ ops.fixup();
+
+ assert!(
+ ops.ops
+ .iter()
+ .all(|op| !matches!(op, GcOp::GlobalGet(..) | GcOp::GlobalSet(..))),
+ "GlobalGet/GlobalSet should be removed when there are no globals"
+ );
+ Ok(())
+}
+
+#[test]
+fn every_op_generated() -> mutatis::Result<()> {
+ let _ = env_logger::try_init();
+ let mut unseen_ops: std::collections::HashSet<_> = OP_NAMES.iter().copied().collect();
+
+ let mut res = empty_test_ops();
+ let mut session = mutatis::Session::new().seed(0xC0FFEE);
+
+ 'outer: for _ in 0..=1024 {
+ session.mutate(&mut res)?;
+ for op in &res.ops {
+ unseen_ops.remove(op.name());
+ if unseen_ops.is_empty() {
+ break 'outer;
+ }
+ }
+ }
+
+ assert!(unseen_ops.is_empty(), "Failed to generate {unseen_ops:?}");
+ Ok(())
+}
+
+#[test]
+fn test_wat_string() -> mutatis::Result<()> {
+ let _ = env_logger::try_init();
+
+ let mut gc_ops = test_ops(2, 2, 5);
+
+ let wasm = gc_ops.to_wasm_binary();
+
+ let actual_wat = wasmprinter::print_bytes(&wasm).expect("Failed to convert to WAT");
+ let actual_wat = actual_wat.trim();
+
+ let expected_wat = r#"
+(module
+ (type (;0;) (func (result externref externref externref)))
+ (type (;1;) (func (param externref externref)))
+ (type (;2;) (func (param externref externref externref)))
+ (type (;3;) (func (result externref externref externref)))
+ (type (;4;) (func (param (ref any))))
+ (rec
+ (type (;5;) (struct))
+ )
+ (rec)
+ (rec
+ (type (;6;) (struct))
+ )
+ (rec
+ (type (;7;) (struct))
+ (type (;8;) (struct))
+ (type (;9;) (struct))
+ )
+ (rec
+ (type (;10;) (struct))
+ (type (;11;) (struct))
+ )
+ (rec
+ (type (;12;) (struct))
+ (type (;13;) (struct))
+ )
+ (rec
+ (type (;14;) (struct))
+ )
+ (type (;15;) (func (param (ref 5))))
+ (type (;16;) (func (param (ref 6))))
+ (type (;17;) (func (param (ref 7))))
+ (type (;18;) (func (param (ref 8))))
+ (type (;19;) (func (param (ref 9))))
+ (type (;20;) (func (param (ref 10))))
+ (type (;21;) (func (param (ref 11))))
+ (type (;22;) (func (param (ref 12))))
+ (type (;23;) (func (param (ref 13))))
+ (type (;24;) (func (param (ref 14))))
+ (import "" "gc" (func (;0;) (type 0)))
+ (import "" "take_refs" (func (;1;) (type 2)))
+ (import "" "make_refs" (func (;2;) (type 3)))
+ (import "" "take_struct" (func (;3;) (type 4)))
+ (import "" "take_struct_5" (func (;4;) (type 15)))
+ (import "" "take_struct_6" (func (;5;) (type 16)))
+ (import "" "take_struct_7" (func (;6;) (type 17)))
+ (import "" "take_struct_8" (func (;7;) (type 18)))
+ (import "" "take_struct_9" (func (;8;) (type 19)))
+ (import "" "take_struct_10" (func (;9;) (type 20)))
+ (import "" "take_struct_11" (func (;10;) (type 21)))
+ (import "" "take_struct_12" (func (;11;) (type 22)))
+ (import "" "take_struct_13" (func (;12;) (type 23)))
+ (import "" "take_struct_14" (func (;13;) (type 24)))
+ (table (;0;) 5 externref)
+ (global (;0;) (mut externref) ref.null extern)
+ (global (;1;) (mut externref) ref.null extern)
+ (export "run" (func 14))
+ (func (;14;) (type 1) (param externref externref)
+ (local externref)
+ loop ;; label = @1
+ ref.null extern
+ drop
+ call 0
+ local.set 0
+ local.get 0
+ global.set 0
+ global.get 0
+ struct.new 5
+ call 3
+ drop
+ drop
+ drop
+ br 0 (;@1;)
+ end
+ )
+)
+ "#;
+ let expected_wat = expected_wat.trim();
+
+ eprintln!("=== actual ===\n{actual_wat}");
+ eprintln!("=== expected ===\n{expected_wat}");
+ assert_eq!(
+ actual_wat, expected_wat,
+ "actual WAT does not match expected"
+ );
+
+ Ok(())
+}
+
+#[test]
+fn emits_empty_rec_groups_and_validates() -> mutatis::Result<()> {
+ let _ = env_logger::try_init();
+
+ let mut ops = test_ops(5, 5, 5);
+
+ let wasm = ops.to_wasm_binary();
+
+ let feats = wasmparser::WasmFeatures::default();
+ feats.reference_types();
+ feats.gc();
+ let mut validator = wasmparser::Validator::new_with_features(feats);
+ assert!(
+ validator.validate_all(&wasm).is_ok(),
+ "GC validation failed"
+ );
+
+ let wat = wasmprinter::print_bytes(&wasm).expect("to WAT");
+ let recs = wat.matches("(rec").count();
+ let structs = wat.matches("(struct)").count();
+
+ assert_eq!(recs, 7, "expected 2 (rec) blocks, got {recs}");
+ assert_eq!(structs, 10, "expected no struct types, got {structs}");
+
+ Ok(())
+}
diff --git a/crates/fuzzing/src/generators/gc_ops/types.rs b/crates/fuzzing/src/generators/gc_ops/types.rs
new file mode 100644
index 0000000000..5cd44ae618
--- /dev/null
+++ b/crates/fuzzing/src/generators/gc_ops/types.rs
@@ -0,0 +1,90 @@
+//! Types for the `gc` operations.
+
+use crate::generators::gc_ops::limits::GcOpsLimits;
+use serde::{Deserialize, Serialize};
+use std::collections::{BTreeMap, BTreeSet};
+
+/// RecGroup ID struct definition.
+#[derive(
+ Debug, Copy, Clone, Eq, PartialOrd, PartialEq, Ord, Hash, Default, Serialize, Deserialize,
+)]
+pub struct RecGroupId(pub(crate) u32);
+
+/// TypeID struct definition.
+#[derive(Debug, Clone, Eq, PartialOrd, PartialEq, Ord, Hash, Default, Serialize, Deserialize)]
+pub struct TypeId(pub(crate) u32);
+
+/// StructType definition
+#[derive(Debug, Default, Serialize, Deserialize)]
+pub struct StructType {
+ // Empty for now; fields will come in a future PR.
+}
+
+/// CompsiteType definition
+#[derive(Debug, Serialize, Deserialize)]
+pub enum CompositeType {
+ /// Struct Type definition
+ Struct(StructType),
+}
+
+/// SubType definition
+#[derive(Debug, Serialize, Deserialize)]
+pub struct SubType {
+ pub(crate) rec_group: RecGroupId,
+ pub(crate) composite_type: CompositeType,
+}
+/// Struct types definition.
+#[derive(Debug, Default, Serialize, Deserialize)]
+pub struct Types {
+ pub(crate) rec_groups: BTreeSet<RecGroupId>,
+ pub(crate) type_defs: BTreeMap<TypeId, SubType>,
+}
+
+impl Types {
+ /// Create a fresh `Types` allocator with no recursive groups defined yet.
+ pub fn new() -> Self {
+ Self {
+ rec_groups: Default::default(),
+ type_defs: Default::default(),
+ }
+ }
+
+ /// Insert a rec-group id. Returns true if newly inserted, false if it already existed.
+ pub fn insert_rec_group(&mut self, id: RecGroupId) -> bool {
+ self.rec_groups.insert(id)
+ }
+
+ /// Insert a rec-group id.
+ pub fn insert_empty_struct(&mut self, id: TypeId, group: RecGroupId) {
+ self.type_defs.insert(
+ id,
+ SubType {
+ rec_group: group,
+ composite_type: CompositeType::Struct(StructType::default()),
+ },
+ );
+ }
+
+ /// Removes any entries beyond the given limit.
+ pub fn fixup(&mut self, limits: &GcOpsLimits) {
+ while self.rec_groups.len() > limits.max_rec_groups as usize {
+ self.rec_groups.pop_last();
+ }
+
+ // Drop any types whose rec-group has been trimmed out.
+ self.type_defs
+ .retain(|_, ty| self.rec_groups.contains(&ty.rec_group));
+
+ // Then enforce the max types limit.
+ while self.type_defs.len() > limits.max_types as usize {
+ self.type_defs.pop_last();
+ }
+
+ debug_assert!(
+ self.type_defs
+ .values()
+ .all(|ty| self.rec_groups.contains(&ty.rec_group)),
+ "type_defs must only reference existing rec_groups"
+ );
+ }
+}
diff --git a/crates/fuzzing/src/generators/table_ops.rs b/crates/fuzzing/src/generators/table_ops.rs
deleted file mode 100644
index 737bbb394b..0000000000
--- a/crates/fuzzing/src/generators/table_ops.rs
+++ /dev/null
@@ -1,1048 +0,0 @@
-//! Generating series of `table.get` and `table.set` operations.
-use mutatis::mutators as m;
-use mutatis::{Candidates, Context, DefaultMutate, Generate, Mutate, Result as MutResult};
-use serde::{Deserialize, Serialize};
-use smallvec::SmallVec;
-use std::ops::RangeInclusive;
-use wasm_encoder::{
- CodeSection, ConstExpr, EntityType, ExportKind, ExportSection, Function, FunctionSection,
- GlobalSection, ImportSection, Instruction, Module, RefType, TableSection, TableType,
- TypeSection, ValType,
-};
-
-use std::collections::{BTreeMap, BTreeSet};
-
-const NUM_PARAMS_RANGE: RangeInclusive<u32> = 0..=10;
-const MAX_TYPES_RANGE: RangeInclusive<u32> = 0..=32;
-const NUM_GLOBALS_RANGE: RangeInclusive<u32> = 0..=10;
-const TABLE_SIZE_RANGE: RangeInclusive<u32> = 0..=100;
-const MAX_REC_GROUPS_RANGE: RangeInclusive<u32> = 0..=10;
-const MAX_OPS: usize = 100;
-
-/// RecGroup ID struct definition.
-#[derive(
- Debug, Copy, Clone, Eq, PartialOrd, PartialEq, Ord, Hash, Default, Serialize, Deserialize,
-)]
-pub struct RecGroupId(u32);
-
-/// TypeID struct definition.
-#[derive(Debug, Clone, Eq, PartialOrd, PartialEq, Ord, Hash, Default, Serialize, Deserialize)]
-pub struct TypeId(u32);
-
-/// StructType definition
-#[derive(Debug, Default, Serialize, Deserialize)]
-pub struct StructType {
- // Empty for now; fields will come in a future PR.
-}
-
-/// CompsiteType definition
-#[derive(Debug, Serialize, Deserialize)]
-pub enum CompositeType {
- /// Struct Type definition
- Struct(StructType),
-}
-
-/// SubType definition
-#[derive(Debug, Serialize, Deserialize)]
-pub struct SubType {
- pub(crate) rec_group: RecGroupId,
- pub(crate) composite_type: CompositeType,
-}
-/// Struct types definition.
-#[derive(Debug, Default, Serialize, Deserialize)]
-pub struct Types {
- rec_groups: BTreeSet<RecGroupId>,
- type_defs: BTreeMap<TypeId, SubType>,
-}
-
-impl Types {
- /// Create a fresh `Types` allocator with no recursive groups defined yet.
- pub fn new() -> Self {
- Self {
- rec_groups: Default::default(),
- type_defs: Default::default(),
- }
- }
-
- /// Insert a rec-group id. Returns true if newly inserted, false if it already existed.
- pub fn insert_rec_group(&mut self, id: RecGroupId) -> bool {
- self.rec_groups.insert(id)
- }
-
- /// Insert a rec-group id.
- pub fn insert_empty_struct(&mut self, id: TypeId, group: RecGroupId) {
- self.type_defs.insert(
- id,
- SubType {
- rec_group: group,
- composite_type: CompositeType::Struct(StructType::default()),
- },
- );
- }
-
- /// Removes any entries beyond the given limit.
- pub fn fixup(&mut self, limits: &TableOpsLimits) {
- while self.rec_groups.len() > limits.max_rec_groups as usize {
- self.rec_groups.pop_last();
- }
-
- // Drop any types whose rec-group has been trimmed out.
- self.type_defs
- .retain(|_, ty| self.rec_groups.contains(&ty.rec_group));
-
- // Then enforce the max types limit.
- while self.type_defs.len() > limits.max_types as usize {
- self.type_defs.pop_last();
- }
-
- debug_assert!(
- self.type_defs
- .values()
- .all(|ty| self.rec_groups.contains(&ty.rec_group)),
- "type_defs must only reference existing rec_groups"
- );
- }
-}
-
-/// Limits controlling the structure of a generated Wasm module.
-#[derive(Debug, Default, Serialize, Deserialize)]
-pub struct TableOpsLimits {
- pub(crate) num_params: u32,
- pub(crate) num_globals: u32,
- pub(crate) table_size: u32,
- pub(crate) max_rec_groups: u32,
- pub(crate) max_types: u32,
-}
-
-impl TableOpsLimits {
- fn fixup(&mut self) {
- // NB: Exhaustively match so that we remember to fixup any other new
- // limits we add in the future.
- let Self {
- num_params,
- num_globals,
- table_size,
- max_rec_groups,
- max_types,
- } = self;
-
- let clamp = |limit: &mut u32, range: RangeInclusive<u32>| {
- *limit = (*limit).clamp(*range.start(), *range.end())
- };
- clamp(table_size, TABLE_SIZE_RANGE);
- clamp(num_params, NUM_PARAMS_RANGE);
- clamp(num_globals, NUM_GLOBALS_RANGE);
- clamp(max_rec_groups, MAX_REC_GROUPS_RANGE);
- clamp(max_types, MAX_TYPES_RANGE);
- }
-}
-
-/// A description of a Wasm module that makes a series of `externref` table
-/// operations.
-#[derive(Debug, Default, Serialize, Deserialize)]
-pub struct TableOps {
- pub(crate) limits: TableOpsLimits,
- pub(crate) ops: Vec<TableOp>,
- pub(crate) types: Types,
-}
-
-impl TableOps {
- /// Serialize this module into a Wasm binary.
- ///
- /// The module requires several function imports. See this function's
- /// implementation for their exact types.
- ///
- /// The single export of the module is a function "run" that takes
- /// `self.num_params` parameters of type `externref`.
- ///
- /// The "run" function does not terminate; you should run it with limited
- /// fuel. It also is not guaranteed to avoid traps: it may access
- /// out-of-bounds of the table.
- pub fn to_wasm_binary(&mut self) -> Vec<u8> {
- self.fixup();
-
- let mut module = Module::new();
-
- // Encode the types for all functions that we are using.
- let mut types = TypeSection::new();
-
- // 0: "gc"
- types.ty().function(
- vec![],
- // Return a bunch of stuff from `gc` so that we exercise GCing when
- // there is return pointer space allocated on the stack. This is
- // especially important because the x64 backend currently
- // dynamically adjusts the stack pointer for each call that uses
- // return pointers rather than statically allocating space in the
- // stack frame.
- vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
- );
-
- // 1: "run"
- let mut params: Vec<ValType> = Vec::with_capacity(self.limits.num_params as usize);
- for _i in 0..self.limits.num_params {
- params.push(ValType::EXTERNREF);
- }
- let results = vec![];
- types.ty().function(params, results);
-
- // 2: `take_refs`
- types.ty().function(
- vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
- vec![],
- );
-
- // 3: `make_refs`
- types.ty().function(
- vec![],
- vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
- );
-
- // 4: `take_struct`
- types.ty().function(
- vec![ValType::Ref(RefType {
- nullable: false,
- heap_type: wasm_encoder::HeapType::ANY,
- })],
- vec![],
- );
-
- let struct_type_base: u32 = types.len();
-
- let mut rec_groups: BTreeMap<RecGroupId, Vec<TypeId>> = self
- .types
- .rec_groups
- .iter()
- .copied()
- .map(|id| (id, Vec::new()))
- .collect();
-
- for (id, ty) in self.types.type_defs.iter() {
- rec_groups.entry(ty.rec_group).or_default().push(id.clone());
- }
-
- let encode_ty_id = |ty_id: &TypeId| -> wasm_encoder::SubType {
- let def = &self.types.type_defs[ty_id];
- match &def.composite_type {
- CompositeType::Struct(StructType {}) => wasm_encoder::SubType {
- is_final: true,
- supertype_idx: None,
- composite_type: wasm_encoder::CompositeType {
- inner: wasm_encoder::CompositeInnerType::Struct(wasm_encoder::StructType {
- fields: Box::new([]),
- }),
- shared: false,
- },
- },
- }
- };
-
- let mut struct_count = 0;
-
- for type_ids in rec_groups.values() {
- let members: Vec<wasm_encoder::SubType> = type_ids.iter().map(encode_ty_id).collect();
- types.ty().rec(members);
- struct_count += type_ids.len() as u32;
- }
-
- let typed_fn_type_base: u32 = struct_type_base + struct_count;
-
- for i in 0..struct_count {
- let concrete = struct_type_base + i;
- types.ty().function(
- vec![ValType::Ref(RefType {
- nullable: false,
- heap_type: wasm_encoder::HeapType::Concrete(concrete),
- })],
- vec![],
- );
- }
-
- // Import the GC function.
- let mut imports = ImportSection::new();
- imports.import("", "gc", EntityType::Function(0));
- imports.import("", "take_refs", EntityType::Function(2));
- imports.import("", "make_refs", EntityType::Function(3));
- imports.import("", "take_struct", EntityType::Function(4));
-
- // For each of our concrete struct types, define a function
- // import that takes an argument of that concrete type.
- let typed_first_func_index: u32 = imports.len();
-
- for i in 0..struct_count {
- let ty_idx = typed_fn_type_base + i;
- let name = format!("take_struct_{}", struct_type_base + i);
- imports.import("", &name, EntityType::Function(ty_idx));
- }
-
- // Define our table.
- let mut tables = TableSection::new();
- tables.table(TableType {
- element_type: RefType::EXTERNREF,
- minimum: u64::from(self.limits.table_size),
- maximum: None,
- table64: false,
- shared: false,
- });
- // Define our globals.
- let mut globals = GlobalSection::new();
- for _ in 0..self.limits.num_globals {
- globals.global(
- wasm_encoder::GlobalType {
- val_type: wasm_encoder::ValType::EXTERNREF,
- mutable: true,
- shared: false,
- },
- &ConstExpr::ref_null(wasm_encoder::HeapType::EXTERN),
- );
- }
-
- // Define the "run" function export.
- let mut functions = FunctionSection::new();
- let mut exports = ExportSection::new();
-
- let run_defined_idx = functions.len();
- functions.function(1);
- let run_func_index = imports.len() + run_defined_idx;
- exports.export("run", ExportKind::Func, run_func_index);
-
- // Give ourselves one scratch local that we can use in various `TableOp`
- // implementations.
- let local_decls: Vec<(u32, ValType)> = vec![(1, ValType::EXTERNREF)];
-
- let mut func = Function::new(local_decls);
- func.instruction(&Instruction::Loop(wasm_encoder::BlockType::Empty));
- for op in &self.ops {
- op.insert(
- &mut func,
- self.limits.num_params,
- struct_type_base,
- typed_first_func_index,
- );
- }
- func.instruction(&Instruction::Br(0));
- func.instruction(&Instruction::End);
- func.instruction(&Instruction::End);
-
- let mut code = CodeSection::new();
- code.function(&func);
-
- module
- .section(&types)
- .section(&imports)
- .section(&functions)
- .section(&tables)
- .section(&globals)
- .section(&exports)
- .section(&code);
-
- module.finish()
- }
- /// Computes the abstract stack depth after executing all operations
- pub fn abstract_stack_depth(&self, index: usize) -> usize {
- debug_assert!(index <= self.ops.len());
- let mut stack: usize = 0;
- for op in self.ops.iter().take(index) {
- let pop = op.operands_len();
- let push = op.results_len();
- stack = stack.saturating_sub(pop);
- stack += push;
- }
- stack
- }
-
- /// Fixes this test case such that it becomes valid.
- ///
- /// This is necessary because a random mutation (e.g. removing an op in the
- /// middle of our sequence) might have made it so that subsequent ops won't
- /// have their expected operand types on the Wasm stack
- /// anymore. Furthermore, because we serialize and deserialize test cases,
- /// and libFuzzer will occasionally mutate those serialized bytes directly,
- /// rather than use one of our custom mutations, we have no guarantee that
- /// pre-mutation test cases are even valid! Therefore, we always call this
- /// method before translating this "AST"-style representation into a raw
- /// Wasm binary.
- fn fixup(&mut self) {
- self.limits.fixup();
- self.types.fixup(&self.limits);
-
- let mut new_ops = Vec::with_capacity(self.ops.len());
- let mut stack = 0;
-
- for mut op in self.ops.iter().copied() {
- if self.limits.max_types == 0
- && matches!(
- op,
- TableOp::StructNew(..)
- | TableOp::TakeStructCall(..)
- | TableOp::TakeTypedStructCall(..)
- )
- {
- continue;
- }
- if self.limits.num_params == 0
- && matches!(op, TableOp::LocalGet(..) | TableOp::LocalSet(..))
- {
- continue;
- }
- if self.limits.num_globals == 0
- && matches!(op, TableOp::GlobalGet(..) | TableOp::GlobalSet(..))
- {
- continue;
- }
-
- op.fixup(&self.limits);
-
- let mut temp = SmallVec::<[_; 4]>::new();
-
- while stack < op.operands_len() {
- temp.push(TableOp::Null());
- stack += 1;
- }
-
- temp.push(op);
- stack = stack - op.operands_len() + op.results_len();
-
- new_ops.extend(temp);
- }
-
- // Insert drops to balance the final stack state
- for _ in 0..stack {
- new_ops.push(TableOp::Drop());
- }
-
- self.ops = new_ops;
- }
-
- /// Attempts to remove the last opcode from the sequence.
- ///
- /// Returns `true` if an opcode was successfully removed, or `false` if the list was already empty.
- pub fn pop(&mut self) -> bool {
- self.ops.pop().is_some()
- }
-}
-
-/// A mutator for the table ops
-#[derive(Debug)]
-pub struct TableOpsMutator;
-
-impl Mutate<TableOps> for TableOpsMutator {
- fn mutate(&mut self, c: &mut Candidates<'_>, ops: &mut TableOps) -> mutatis::Result<()> {
- if !c.shrink() {
- c.mutation(|ctx| {
- if let Some(idx) = ctx.rng().gen_index(ops.ops.len() + 1) {
- let stack = ops.abstract_stack_depth(idx);
- let (op, _new_stack_size) = TableOp::generate(ctx, &ops, stack)?;
- ops.ops.insert(idx, op);
- }
- Ok(())
- })?;
- }
- if !ops.ops.is_empty() {
- c.mutation(|ctx| {
- let idx = ctx
- .rng()
- .gen_index(ops.ops.len())
- .expect("ops is not empty");
- ops.ops.remove(idx);
- Ok(())
- })?;
- }
-
- Ok(())
- }
-}
-
-impl DefaultMutate for TableOps {
- type DefaultMutate = TableOpsMutator;
-}
-
-impl Default for TableOpsMutator {
- fn default() -> Self {
- TableOpsMutator
- }
-}
-
-impl<'a> arbitrary::Arbitrary<'a> for TableOps {
- fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
- let mut session = mutatis::Session::new().seed(u.arbitrary()?);
- session
- .generate()
- .map_err(|_| arbitrary::Error::IncorrectFormat)
- }
-}
-
-impl Generate<TableOps> for TableOpsMutator {
- fn generate(&mut self, ctx: &mut Context) -> MutResult<TableOps> {
- let num_params = m::range(NUM_PARAMS_RANGE).generate(ctx)?;
- let num_globals = m::range(NUM_GLOBALS_RANGE).generate(ctx)?;
- let table_size = m::range(TABLE_SIZE_RANGE).generate(ctx)?;
-
- let max_rec_groups = m::range(MAX_REC_GROUPS_RANGE).generate(ctx)?;
- let max_types = m::range(MAX_TYPES_RANGE).generate(ctx)?;
-
- let mut ops = TableOps {
- limits: TableOpsLimits {
- num_params,
- num_globals,
- table_size,
- max_rec_groups,
- max_types,
- },
- ops: {
- let mut v = vec![TableOp::Null(), TableOp::Drop(), TableOp::Gc()];
- if num_params > 0 {
- v.push(TableOp::LocalSet(0));
- v.push(TableOp::LocalGet(0));
- }
- if num_globals > 0 {
- v.push(TableOp::GlobalSet(0));
- v.push(TableOp::GlobalGet(0));
- }
- if max_types > 0 {
- v.push(TableOp::StructNew(0));
- }
- v
- },
- types: Types::new(),
- };
-
- for i in 0..ops.limits.max_rec_groups {
- ops.types.insert_rec_group(RecGroupId(i));
- }
-
- if ops.limits.max_rec_groups > 0 {
- for i in 0..ops.limits.max_types {
- let tid = TypeId(i);
- let gid = RecGroupId(m::range(0..=ops.limits.max_rec_groups - 1).generate(ctx)?);
-
- ops.types.insert_empty_struct(tid, gid);
- }
- }
-
- let mut stack: usize = 0;
-
- while ops.ops.len() < MAX_OPS {
- let (op, new_stack_len) = TableOp::generate(ctx, &ops, stack)?;
- ops.ops.push(op);
- stack = new_stack_len;
- }
-
- // Drop any leftover refs on the stack.
- for _ in 0..stack {
- ops.ops.push(TableOp::Drop());
- }
-
- Ok(ops)
- }
-}
-
-macro_rules! define_table_ops {
- (
- $(
- $op:ident $( ( $($limit_var:ident : $limit:expr => $ty:ty),* ) )? : $params:expr => $results:expr ,
- )*
- ) => {
- #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
- pub(crate) enum TableOp {
- $(
- $op ( $( $($ty),* )? ),
- )*
- }
- #[cfg(test)]
- const OP_NAMES: &'static[&'static str] = &[
- $(
- stringify!($op),
- )*
- ];
-
- impl TableOp {
- #[cfg(test)]
- fn name(&self) -> &'static str {
- match self {
- $(
- Self::$op (..) => stringify!($op),
- )*
- }
- }
-
- pub fn operands_len(&self) -> usize {
- match self {
- $(
- Self::$op (..) => $params,
- )*
- }
- }
-
- pub fn results_len(&self) -> usize {
- match self {
- $(
- Self::$op (..) => $results,
- )*
- }
- }
- }
-
- $(
- #[allow(non_snake_case, reason = "macro-generated code")]
- fn $op(
- _ctx: &mut mutatis::Context,
- _limits: &TableOpsLimits,
- stack: usize,
- ) -> mutatis::Result<(TableOp, usize)> {
- #[allow(unused_comparisons, reason = "macro-generated code")]
- {
- debug_assert!(stack >= $params);
- }
-
- let op = TableOp::$op(
- $($({
- let limit_fn = $limit as fn(&TableOpsLimits) -> $ty;
- let limit = (limit_fn)(_limits);
- debug_assert!(limit > 0);
- m::range(0..=limit - 1).generate(_ctx)?
- })*)?
- );
- let new_stack = stack - $params + $results;
- Ok((op, new_stack))
- }
- )*
-
- impl TableOp {
- fn fixup(&mut self, limits: &TableOpsLimits) {
- match self {
- $(
- Self::$op( $( $( $limit_var ),* )? ) => {
- $( $(
- let limit_fn = $limit as fn(&TableOpsLimits) -> $ty;
- let limit = (limit_fn)(limits);
- debug_assert!(limit > 0);
- *$limit_var = *$limit_var % limit;
- )* )?
- }
- )*
- }
- }
-
- fn generate(
- ctx: &mut mutatis::Context,
- ops: &TableOps,
- stack: usize,
- ) -> mutatis::Result<(TableOp, usize)> {
- let mut valid_choices: Vec<
- fn(&mut Context, &TableOpsLimits, usize) -> mutatis::Result<(TableOp, usize)>
- > = vec![];
- $(
- #[allow(unused_comparisons, reason = "macro-generated code")]
- if stack >= $params $($(
- && {
- let limit_fn = $limit as fn(&TableOpsLimits) -> $ty;
- let limit = (limit_fn)(&ops.limits);
- limit > 0
- }
- )*)? {
- valid_choices.push($op);
- }
- )*
-
- let f = *ctx.rng()
- .choose(&valid_choices)
- .expect("should always have a valid op choice");
-
- (f)(ctx, &ops.limits, stack)
- }
- }
- };
-}
-
-define_table_ops! {
- Gc : 0 => 3,
-
- MakeRefs : 0 => 3,
- TakeRefs : 3 => 0,
-
- // Add one to make sure that out of bounds table accesses are possible, but still rare.
- TableGet(elem_index: |ops| ops.table_size + 1 => u32) : 0 => 1,
- TableSet(elem_index: |ops| ops.table_size + 1 => u32) : 1 => 0,
-
- GlobalGet(global_index: |ops| ops.num_globals => u32) : 0 => 1,
- GlobalSet(global_index: |ops| ops.num_globals => u32) : 1 => 0,
-
- LocalGet(local_index: |ops| ops.num_params => u32) : 0 => 1,
- LocalSet(local_index: |ops| ops.num_params => u32) : 1 => 0,
-
- StructNew(type_index: |ops| ops.max_types => u32) : 0 => 0,
- TakeStructCall(type_index: |ops| ops.max_types => u32) : 0 => 0,
- TakeTypedStructCall(type_index: |ops| ops.max_types => u32) : 0 => 0,
-
- Drop : 1 => 0,
-
- Null : 0 => 1,
-}
-
-impl TableOp {
- fn insert(
- self,
- func: &mut Function,
- scratch_local: u32,
- struct_type_base: u32,
- typed_first_func_index: u32,
- ) {
- let gc_func_idx = 0;
- let take_refs_func_idx = 1;
- let make_refs_func_idx = 2;
- let take_structref_idx = 3;
-
- match self {
- Self::Gc() => {
- func.instruction(&Instruction::Call(gc_func_idx));
- }
- Self::MakeRefs() => {
- func.instruction(&Instruction::Call(make_refs_func_idx));
- }
- Self::TakeRefs() => {
- func.instruction(&Instruction::Call(take_refs_func_idx));
- }
- Self::TableGet(x) => {
- func.instruction(&Instruction::I32Const(x.cast_signed()));
- func.instruction(&Instruction::TableGet(0));
- }
- Self::TableSet(x) => {
- func.instruction(&Instruction::LocalSet(scratch_local));
- func.instruction(&Instruction::I32Const(x.cast_signed()));
- func.instruction(&Instruction::LocalGet(scratch_local));
- func.instruction(&Instruction::TableSet(0));
- }
- Self::GlobalGet(x) => {
- func.instruction(&Instruction::GlobalGet(x));
- }
- Self::GlobalSet(x) => {
- func.instruction(&Instruction::GlobalSet(x));
- }
- Self::LocalGet(x) => {
- func.instruction(&Instruction::LocalGet(x));
- }
- Self::LocalSet(x) => {
- func.instruction(&Instruction::LocalSet(x));
- }
- Self::Drop() => {
- func.instruction(&Instruction::Drop);
- }
- Self::Null() => {
- func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::EXTERN));
- }
- Self::StructNew(x) => {
- func.instruction(&Instruction::StructNew(x + struct_type_base));
- func.instruction(&Instruction::Call(take_structref_idx));
- }
- Self::TakeStructCall(x) => {
- func.instruction(&Instruction::StructNew(x + struct_type_base));
- func.instruction(&Instruction::Call(take_structref_idx));
- }
- Self::TakeTypedStructCall(x) => {
- let s = struct_type_base + x;
- let f = typed_first_func_index + x;
- func.instruction(&Instruction::StructNew(s));
- func.instruction(&Instruction::Call(f));
- }
- }
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- use rand::rngs::StdRng;
- use rand::{Rng, SeedableRng};
- /// Creates empty TableOps
- fn empty_test_ops() -> TableOps {
- let mut t = TableOps {
- limits: TableOpsLimits {
- num_params: 5,
- num_globals: 5,
- table_size: 5,
- max_rec_groups: 5,
- max_types: 5,
- },
- ops: vec![],
- types: Types::new(),
- };
- for i in 0..t.limits.max_rec_groups {
- t.types.insert_rec_group(RecGroupId(i));
- }
- t
- }
-
- /// Creates TableOps with all default opcodes
- fn test_ops(num_params: u32, num_globals: u32, table_size: u32) -> TableOps {
- let mut t = TableOps {
- limits: TableOpsLimits {
- num_params,
- num_globals,
- table_size,
- max_rec_groups: 7,
- max_types: 10,
- },
- ops: vec![
- TableOp::Null(),
- TableOp::Drop(),
- TableOp::Gc(),
- TableOp::LocalSet(0),
- TableOp::LocalGet(0),
- TableOp::GlobalSet(0),
- TableOp::GlobalGet(0),
- TableOp::StructNew(0),
- ],
- types: Types::new(),
- };
- for i in 0..t.limits.max_rec_groups {
- t.types.insert_rec_group(RecGroupId(i));
- }
-
- if t.limits.max_rec_groups > 0 {
- let mut rng = StdRng::seed_from_u64(0xC0FFEE);
- for i in 0..t.limits.max_types {
- let gid = RecGroupId(rng.gen_range(0..t.limits.max_rec_groups));
- t.types.insert_empty_struct(TypeId(i), gid);
- }
- }
- t
- }
-
- #[test]
- fn mutate_table_ops_with_default_mutator() -> mutatis::Result<()> {
- let _ = env_logger::try_init();
- let mut res = test_ops(5, 5, 5);
-
- let mut session = mutatis::Session::new();
-
- for _ in 0..2048 {
- session.mutate(&mut res)?;
- let wasm = res.to_wasm_binary();
-
- let feats = wasmparser::WasmFeatures::default();
- feats.reference_types();
- feats.gc();
- let mut validator = wasmparser::Validator::new_with_features(feats);
-
- let wat = wasmprinter::print_bytes(&wasm).expect("[-] Failed .print_bytes(&wasm).");
- let result = validator.validate_all(&wasm);
- log::debug!("{wat}");
- assert!(
- result.is_ok(),
- "\n[-] Invalid wat: {}\n\t\t==== Failed Wat ====\n{}",
- result.err().expect("[-] Failed .err() in assert macro."),
- wat
- );
- }
- Ok(())
- }
-
- #[test]
- fn struct_new_removed_when_no_types() -> mutatis::Result<()> {
- let _ = env_logger::try_init();
-
- let mut ops = test_ops(0, 0, 0);
- ops.limits.max_types = 0;
- ops.ops = vec![TableOp::StructNew(42)];
-
- let _ = ops.fixup();
-
- assert!(
- ops.ops
- .iter()
- .all(|op| !matches!(op, TableOp::StructNew(..))),
- "StructNew should be removed when there are no types"
- );
- Ok(())
- }
-
- #[test]
- fn local_ops_removed_when_no_params() -> mutatis::Result<()> {
- let _ = env_logger::try_init();
-
- let mut ops = test_ops(0, 0, 0);
- ops.limits.num_params = 0;
- ops.ops = vec![TableOp::LocalGet(42), TableOp::LocalSet(99)];
-
- ops.fixup();
-
- assert!(
- ops.ops
- .iter()
- .all(|op| !matches!(op, TableOp::LocalGet(..) | TableOp::LocalSet(..))),
- "LocalGet/LocalSet should be removed when there are no params"
- );
- Ok(())
- }
-
- #[test]
- fn global_ops_removed_when_no_globals() -> mutatis::Result<()> {
- let _ = env_logger::try_init();
-
- let mut ops = test_ops(0, 0, 0);
- ops.limits.num_globals = 0;
- ops.ops = vec![TableOp::GlobalGet(42), TableOp::GlobalSet(99)];
-
- ops.fixup();
-
- assert!(
- ops.ops
- .iter()
- .all(|op| !matches!(op, TableOp::GlobalGet(..) | TableOp::GlobalSet(..))),
- "GlobalGet/GlobalSet should be removed when there are no globals"
- );
- Ok(())
- }
-
- #[test]
- fn every_op_generated() -> mutatis::Result<()> {
- let _ = env_logger::try_init();
- let mut unseen_ops: std::collections::HashSet<_> = OP_NAMES.iter().copied().collect();
-
- let mut res = empty_test_ops();
- let mut session = mutatis::Session::new();
-
- 'outer: for _ in 0..=1024 {
- session.mutate(&mut res)?;
- for op in &res.ops {
- unseen_ops.remove(op.name());
- if unseen_ops.is_empty() {
- break 'outer;
- }
- }
- }
-
- assert!(unseen_ops.is_empty(), "Failed to generate {unseen_ops:?}");
- Ok(())
- }
-
- #[test]
- fn test_wat_string() -> mutatis::Result<()> {
- let _ = env_logger::try_init();
-
- let mut table_ops = test_ops(2, 2, 5);
-
- let wasm = table_ops.to_wasm_binary();
-
- let actual_wat = wasmprinter::print_bytes(&wasm).expect("Failed to convert to WAT");
- let actual_wat = actual_wat.trim();
-
- let expected_wat = r#"
-(module
- (type (;0;) (func (result externref externref externref)))
- (type (;1;) (func (param externref externref)))
- (type (;2;) (func (param externref externref externref)))
- (type (;3;) (func (result externref externref externref)))
- (type (;4;) (func (param (ref any))))
- (rec
- (type (;5;) (struct))
- )
- (rec)
- (rec
- (type (;6;) (struct))
- )
- (rec
- (type (;7;) (struct))
- (type (;8;) (struct))
- (type (;9;) (struct))
- )
- (rec
- (type (;10;) (struct))
- (type (;11;) (struct))
- )
- (rec
- (type (;12;) (struct))
- (type (;13;) (struct))
- )
- (rec
- (type (;14;) (struct))
- )
- (type (;15;) (func (param (ref 5))))
- (type (;16;) (func (param (ref 6))))
- (type (;17;) (func (param (ref 7))))
- (type (;18;) (func (param (ref 8))))
- (type (;19;) (func (param (ref 9))))
- (type (;20;) (func (param (ref 10))))
- (type (;21;) (func (param (ref 11))))
- (type (;22;) (func (param (ref 12))))
- (type (;23;) (func (param (ref 13))))
- (type (;24;) (func (param (ref 14))))
- (import "" "gc" (func (;0;) (type 0)))
- (import "" "take_refs" (func (;1;) (type 2)))
- (import "" "make_refs" (func (;2;) (type 3)))
- (import "" "take_struct" (func (;3;) (type 4)))
- (import "" "take_struct_5" (func (;4;) (type 15)))
- (import "" "take_struct_6" (func (;5;) (type 16)))
- (import "" "take_struct_7" (func (;6;) (type 17)))
- (import "" "take_struct_8" (func (;7;) (type 18)))
- (import "" "take_struct_9" (func (;8;) (type 19)))
- (import "" "take_struct_10" (func (;9;) (type 20)))
- (import "" "take_struct_11" (func (;10;) (type 21)))
- (import "" "take_struct_12" (func (;11;) (type 22)))
- (import "" "take_struct_13" (func (;12;) (type 23)))
- (import "" "take_struct_14" (func (;13;) (type 24)))
- (table (;0;) 5 externref)
- (global (;0;) (mut externref) ref.null extern)
- (global (;1;) (mut externref) ref.null extern)
- (export "run" (func 14))
- (func (;14;) (type 1) (param externref externref)
- (local externref)
- loop ;; label = @1
- ref.null extern
- drop
- call 0
- local.set 0
- local.get 0
- global.set 0
- global.get 0
- struct.new 5
- call 3
- drop
- drop
- drop
- br 0 (;@1;)
- end
- )
-)
- "#;
- let expected_wat = expected_wat.trim();
-
- eprintln!("=== actual ===\n{actual_wat}");
- eprintln!("=== expected ===\n{expected_wat}");
- assert_eq!(
- actual_wat, expected_wat,
- "actual WAT does not match expected"
- );
-
- Ok(())
- }
-
- #[test]
- fn emits_empty_rec_groups_and_validates() -> mutatis::Result<()> {
- let _ = env_logger::try_init();
-
- let mut ops = test_ops(5, 5, 5);
-
- let wasm = ops.to_wasm_binary();
-
- let feats = wasmparser::WasmFeatures::default();
- feats.reference_types();
- feats.gc();
- let mut validator = wasmparser::Validator::new_with_features(feats);
- assert!(
- validator.validate_all(&wasm).is_ok(),
- "GC validation failed"
- );
-
- let wat = wasmprinter::print_bytes(&wasm).expect("to WAT");
- let recs = wat.matches("(rec").count();
- let structs = wat.matches("(struct)").count();
-
- assert_eq!(recs, 7, "expected 2 (rec) blocks, got {recs}");
- assert_eq!(structs, 10, "expected no struct types, got {structs}");
-
- Ok(())
- }
-}
diff --git a/crates/fuzzing/src/oracles.rs b/crates/fuzzing/src/oracles.rs
index ae9d1da07b..9f6ef62ab9 100644
--- a/crates/fuzzing/src/oracles.rs
+++ b/crates/fuzzing/src/oracles.rs
@@ -21,6 +21,7 @@ mod stacks;
use self::diff_wasmtime::WasmtimeInstance;
use self::engine::{DiffEngine, DiffInstance};
+use crate::generators::GcOps;
use crate::generators::{self, CompilerStrategy, DiffValue, DiffValueType};
use crate::single_module_fuzzer::KnownValid;
use arbitrary::Arbitrary;
@@ -775,14 +776,11 @@ pub fn wast_test(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<()> {
Ok(())
}
-/// Execute a series of `table.get` and `table.set` operations.
+/// Execute a series of `gc` operations.
///
/// Returns the number of `gc` operations which occurred throughout the test
/// case -- used to test below that gc happens reasonably soon and eventually.
-pub fn table_ops(
- mut fuzz_config: generators::Config,
- mut ops: generators::table_ops::TableOps,
-) -> Result<usize> {
+pub fn gc_ops(mut fuzz_config: generators::Config, mut ops: GcOps) -> Result<usize> {
let expected_drops = Arc::new(AtomicUsize::new(0));
let num_dropped = Arc::new(AtomicUsize::new(0));
@@ -815,7 +813,7 @@ pub fn table_ops(
let expected_drops = expected_drops.clone();
let num_gcs = num_gcs.clone();
move |mut caller: Caller<'_, StoreLimits>, _params, results| {
- log::info!("table_ops: GC");
+ log::info!("gc_ops: GC");
if num_gcs.fetch_add(1, SeqCst) < MAX_GCS {
caller.gc(None);
}
@@ -833,7 +831,7 @@ pub fn table_ops(
CountDrops::new(&expected_drops, num_dropped.clone()),
)?;
- log::info!("table_ops: gc() -> ({a:?}, {b:?}, {c:?})");
+ log::info!("gc_ops: gc() -> ({a:?}, {b:?}, {c:?})");
results[0] = Some(a).into();
results[1] = Some(b).into();
results[2] = Some(c).into();
@@ -850,7 +848,7 @@ pub fn table_ops(
b: Option<Rooted<ExternRef>>,
c: Option<Rooted<ExternRef>>|
-> Result<()> {
- log::info!("table_ops: take_refs({a:?}, {b:?}, {c:?})",);
+ log::info!("gc_ops: take_refs({a:?}, {b:?}, {c:?})",);
// Do the assertion on each ref's inner data, even though it
// all points to the same atomic, so that if we happen to
@@ -894,7 +892,7 @@ pub fn table_ops(
let num_dropped = num_dropped.clone();
let expected_drops = expected_drops.clone();
move |mut caller, _params, results| {
- log::info!("table_ops: make_refs");
+ log::info!("gc_ops: make_refs");
let a = ExternRef::new(
&mut caller,
@@ -909,7 +907,7 @@ pub fn table_ops(
CountDrops::new(&expected_drops, num_dropped.clone()),
)?;
- log::info!("table_ops: make_refs() -> ({a:?}, {b:?}, {c:?})");
+ log::info!("gc_ops: make_refs() -> ({a:?}, {b:?}, {c:?})");
results[0] = Some(a).into();
results[1] = Some(b).into();
@@ -928,7 +926,7 @@ pub fn table_ops(
let func = Func::new(&mut store, func_ty, {
move |_caller: Caller<'_, StoreLimits>, _params, _results| {
- log::info!("table_ops: take_struct(<ref any>)");
+ log::info!("gc_ops: take_struct(<ref any>)");
Ok(())
}
});
@@ -943,7 +941,7 @@ pub fn table_ops(
let imp_name = name.to_string();
let func =
Func::new(&mut store, ft.clone(), move |_caller, _params, _results| {
- log::info!("table_ops: {imp_name}(<typed structref>)");
+ log::info!("gc_ops: {imp_name}(<typed structref>)");
Ok(())
});
linker.define(&store, "", name, func).unwrap();
@@ -959,7 +957,7 @@ pub fn table_ops(
let mut scope = RootScope::new(&mut store);
log::info!(
- "table_ops: begin allocating {} externref arguments",
+ "gc_ops: begin allocating {} externref arguments",
ops.limits.num_globals
);
let args: Vec<_> = (0..ops.limits.num_params)
@@ -971,7 +969,7 @@ pub fn table_ops(
})
.collect::<Result<_>>()?;
log::info!(
- "table_ops: end allocating {} externref arguments",
+ "gc_ops: end allocating {} externref arguments",
ops.limits.num_globals
);
@@ -979,7 +977,7 @@ pub fn table_ops(
// valid traps are table-out-of-bounds which happens through `table.get`
// and `table.set` generated or an out-of-fuel trap. Otherwise any other
// error is unexpected and should fail fuzzing.
- log::info!("table_ops: calling into Wasm `run` function");
+ log::info!("gc_ops: calling into Wasm `run` function");
let err = run.call(&mut scope, &args, &mut []).unwrap_err();
if err.is::<GcHeapOutOfMemory<CountDrops>>() || err.is::<GcHeapOutOfMemory<()>>() {
// Accept GC OOM as an allowed outcome for this fuzzer.
@@ -1410,12 +1408,12 @@ mod tests {
assert!(ok);
}
- // Test that the `table_ops` fuzzer eventually runs the gc function in the host.
+ // Test that the `gc_ops` fuzzer eventually runs the gc function in the host.
// We've historically had issues where this fuzzer accidentally wasn't fuzzing
// anything for a long time so this is an attempt to prevent that from happening
// again.
#[test]
- fn table_ops_eventually_gcs() {
+ fn gc_ops_eventually_gcs() {
// Skip if we're under emulation because some fuzz configurations will do
// large address space reservations that QEMU doesn't handle well.
if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {
@@ -1423,7 +1421,7 @@ mod tests {
}
let ok = gen_until_pass(|(config, test), _| {
- let result = table_ops(config, test)?;
+ let result = gc_ops(config, test)?;
Ok(result > 0)
});
diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml
index edca9087c7..137b2d3d76 100644
--- a/fuzz/Cargo.toml
+++ b/fuzz/Cargo.toml
@@ -110,8 +110,8 @@ doc = false
bench = false
[[bin]]
-name = "table_ops"
-path = "fuzz_targets/table_ops.rs"
+name = "gc_ops"
+path = "fuzz_targets/gc_ops.rs"
test = false
doc = false
bench = false
diff --git a/fuzz/fuzz_targets/table_ops.rs b/fuzz/fuzz_targets/gc_ops.rs
similarity index 82%
rename from fuzz/fuzz_targets/table_ops.rs
rename to fuzz/fuzz_targets/gc_ops.rs
index 3eef3c0e5b..823732f2e5 100644
--- a/fuzz/fuzz_targets/table_ops.rs
+++ b/fuzz/fuzz_targets/gc_ops.rs
@@ -5,11 +5,11 @@ use libfuzzer_sys::{fuzz_mutator, fuzz_target, fuzzer_mutate};
use mutatis::Session;
use postcard::{from_bytes, to_slice};
use rand::{Rng, SeedableRng};
-use wasmtime_fuzzing::generators::table_ops::TableOps;
-use wasmtime_fuzzing::oracles::table_ops;
+use wasmtime_fuzzing::generators::GcOps;
+use wasmtime_fuzzing::oracles::gc_ops;
fuzz_target!(|data: &[u8]| {
- let Ok((seed, ops)) = postcard::from_bytes::<(u64, TableOps)>(data) else {
+ let Ok((seed, ops)) = postcard::from_bytes::<(u64, GcOps)>(data) else {
return;
};
@@ -22,7 +22,7 @@ fuzz_target!(|data: &[u8]| {
return;
};
- let _ = table_ops(config, ops);
+ let _ = gc_ops(config, ops);
});
fuzz_mutator!(|data: &mut [u8], size: usize, max_size: usize, seed: u32| {
@@ -34,7 +34,7 @@ fuzz_mutator!(|data: &mut [u8], size: usize, max_size: usize, seed: u32| {
}
// Try to decode using postcard; fallback to default input on failure
- let mut tuple: (u64, TableOps) = from_bytes(&data[..size]).ok().unwrap_or_default();
+ let mut tuple: (u64, GcOps) = from_bytes(&data[..size]).ok().unwrap_or_default();
let mut session = Session::new().seed(seed.into()).shrink(max_size < size);