A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
[gc_fuzz]: Clean up the source of gc_ops (#14435)
Khagan (Khan) Karimov3553d5af77c8
diff --git a/crates/fuzzing/src/generators/gc_ops/limits.rs b/crates/fuzzing/src/generators/gc_ops/limits.rsindex b4830321c1..c538b3c72b 100644--- a/crates/fuzzing/src/generators/gc_ops/limits.rs+++ b/crates/fuzzing/src/generators/gc_ops/limits.rs@@ -23,18 +23,25 @@ pub const MAX_INLINE_CONSTRUCTION: u32 = 8;/// Limits controlling the structure of a generated Wasm module.#[derive(Clone, Debug, Serialize, Deserialize, mutatis::Mutate)]pub struct GcOpsLimits {+ /// Number of `externref` parameters of `run`.#[mutatis(default_mutate)]pub(crate) num_params: u32,+ /// Number of `externref` globals.#[mutatis(default_mutate)]pub(crate) num_globals: u32,+ /// Number of elements in every table.#[mutatis(default_mutate)]pub(crate) table_size: u32,+ /// Maximum number of rec groups.#[mutatis(default_mutate)]pub(crate) max_rec_groups: u32,+ /// Maximum number of concrete types.#[mutatis(default_mutate)]pub(crate) max_types: u32,+ /// Maximum number of fields per struct type.#[mutatis(default_mutate)]pub(crate) max_fields: u32,+ /// Length of every array created by `ArrayNew` and `ArrayNewDefault`.#[mutatis(default_mutate)]pub(crate) array_length: u32,}@@ -55,7 +62,7 @@ impl Default for GcOpsLimits {impl GcOpsLimits {/// Fixup the limits to ensure they are within the valid range.- pub(crate) fn fixup(&mut self) {+ pub(crate) fn fixup_limits(&mut self) {let Self {num_params,num_globals,diff --git a/crates/fuzzing/src/generators/gc_ops/mod.rs b/crates/fuzzing/src/generators/gc_ops/mod.rsindex 9a492cbee1..719c300d03 100644--- a/crates/fuzzing/src/generators/gc_ops/mod.rs+++ b/crates/fuzzing/src/generators/gc_ops/mod.rs@@ -3,6 +3,7 @@pub mod limits;pub mod mutator;pub mod ops;+pub mod stack;pub mod types;#[cfg(test)]diff --git a/crates/fuzzing/src/generators/gc_ops/mutator.rs b/crates/fuzzing/src/generators/gc_ops/mutator.rsindex a0ca1a35a5..b920269b96 100644--- a/crates/fuzzing/src/generators/gc_ops/mutator.rs+++ b/crates/fuzzing/src/generators/gc_ops/mutator.rs@@ -405,6 +405,7 @@ impl TypesMutator {/// Mutator for `GcOps`.#[derive(Debug, Default)]pub struct GcOpsMutator {+ /// Mutates the type definitions and rec groups.types_mutator: TypesMutator,}@@ -437,7 +438,7 @@ impl Mutate<GcOps> for GcOpsMutator {fn mutate(&mut self, c: &mut Candidates<'_>, ops: &mut GcOps) -> MutResult<()> {m::default::<GcOpsLimits>().map(|_ctx, limits: &mut GcOpsLimits| {- limits.fixup();+ limits.fixup_limits();Ok(())}).mutate(c, &mut ops.limits)?;diff --git a/crates/fuzzing/src/generators/gc_ops/ops.rs b/crates/fuzzing/src/generators/gc_ops/ops.rsindex 3e5d54718c..11a1269ded 100644--- a/crates/fuzzing/src/generators/gc_ops/ops.rs+++ b/crates/fuzzing/src/generators/gc_ops/ops.rs@@ -1,19 +1,51 @@//! Operations for the `gc` operations.-use crate::generators::gc_ops::types::StackType;+use crate::generators::gc_ops::stack::StackType;use crate::generators::gc_ops::{limits::{GcOpsLimits, MAX_INLINE_CONSTRUCTION},- types::{CompositeType, EmitCtx, RecGroupId, StructField, TypeId, Types, emit_new},+ types::{CompositeType, EmitCtx, FieldType, RecGroupId, StructField, TypeId, Types, emit_new},};use mutatis::Generate;use serde::{Deserialize, Serialize};use std::collections::BTreeMap;use wasm_encoder::{- CodeSection, ConstExpr, EntityType, ExportKind, ExportSection, Function, FunctionSection,- GlobalSection, ImportSection, Instruction, Module, RefType, TableSection, TableType,- TypeSection, ValType,+ AbstractHeapType, BlockType, CodeSection, ConstExpr, EntityType, ExportKind, ExportSection,+ Function, FunctionSection, GlobalSection, GlobalType, HeapType, ImportSection, Instruction,+ Module, RefType, TableSection, TableType, TypeSection, ValType,};+/// The abstract `struct` heap type, which `wasm_encoder` has no shorthand for.+const STRUCT: HeapType = HeapType::Abstract {+ shared: false,+ ty: AbstractHeapType::Struct,+};++/// The abstract `array` heap type.+const ARRAY: HeapType = HeapType::Abstract {+ shared: false,+ ty: AbstractHeapType::Array,+};++/// The abstract `eq` heap type.+const EQ: HeapType = HeapType::Abstract {+ shared: false,+ ty: AbstractHeapType::Eq,+};++/// `structref`, i.e. `(ref null struct)`.+const STRUCTREF: RefType = RefType {+ nullable: true,+ heap_type: STRUCT,+};++/// `(ref null $index)`.+fn concrete(index: u32) -> RefType {+ RefType {+ nullable: true,+ heap_type: HeapType::Concrete(index),+ }+}+/// Pick a same-kind concrete type index for `raw`, or `None` when there are no/// types of that kind (so the caller drops the op).fn pick_type_index(indices: &[u32], raw: u32) -> Option<u32> {@@ -55,37 +87,532 @@ fn struct_fields<'a>(})}-/// The base offsets and indices for various Wasm entities within-/// their index spaces in the the encoded Wasm binary.+/// A table of `size` nullable references with no maximum.+fn nullable_table(element_type: RefType, size: u32) -> TableType {+ TableType {+ element_type,+ minimum: u64::from(size),+ maximum: None,+ table64: false,+ shared: false,+ }+}++/// Append a mutable null-initialized `(ref null heap_type)` global and return its index.+fn null_ref_global(globals: &mut GlobalSection, heap_type: HeapType) -> u32 {+ let index = globals.len();+ globals.global(+ GlobalType {+ val_type: ValType::Ref(RefType {+ nullable: true,+ heap_type,+ }),+ mutable: true,+ shared: false,+ },+ &ConstExpr::ref_null(heap_type),+ );+ index+}++/// `table.get` of a constant index.+fn table_get(func: &mut Function, elem_index: u32, table: u32) {+ func.instruction(&Instruction::I32Const(elem_index.cast_signed()));+ func.instruction(&Instruction::TableGet(table));+}++/// `table.set` of a constant index with the value on top of the stack, parked+/// in `tmp` (a local of the table's element type) while the index is pushed.+fn table_set_via(func: &mut Function, tmp: u32, elem_index: u32, table: u32) {+ func.instruction(&Instruction::LocalSet(tmp));+ func.instruction(&Instruction::I32Const(elem_index.cast_signed()));+ func.instruction(&Instruction::LocalGet(tmp));+ func.instruction(&Instruction::TableSet(table));+}++/// Pop the reference on top of the stack into `tmp` and run `body` only if it is+/// non-null, so `body` reads it back with `local.get tmp`.+fn if_non_null(func: &mut Function, tmp: u32, body: impl FnOnce(&mut Function)) {+ func.instruction(&Instruction::LocalTee(tmp));+ func.instruction(&Instruction::RefIsNull);+ func.instruction(&Instruction::If(BlockType::Empty));+ func.instruction(&Instruction::Else);+ body(func);+ func.instruction(&Instruction::End);+}++/// The `struct.get` variant for a field: `_s`/`_u` if packed, plain otherwise.+fn struct_get_instruction(+ struct_type_index: u32,+ field_index: u32,+ field_type: FieldType,+ unsigned: bool,+) -> Instruction<'static> {+ match (field_type.is_packed(), unsigned) {+ (false, _) => Instruction::StructGet {+ struct_type_index,+ field_index,+ },+ (true, true) => Instruction::StructGetU {+ struct_type_index,+ field_index,+ },+ (true, false) => Instruction::StructGetS {+ struct_type_index,+ field_index,+ },+ }+}++/// The `array.get` variant for an element type: `_s`/`_u` if packed, plain otherwise.+fn array_get_instruction(+ array_type_index: u32,+ element_type: FieldType,+ unsigned: bool,+) -> Instruction<'static> {+ match (element_type.is_packed(), unsigned) {+ (false, _) => Instruction::ArrayGet(array_type_index),+ (true, true) => Instruction::ArrayGetU(array_type_index),+ (true, false) => Instruction::ArrayGetS(array_type_index),+ }+}++/// Indices of the function types of the host imports and `run`.+struct HostTypes {+ /// Type index of `gc`.+ gc: u32,+ /// Type index of `run`.+ run: u32,+ /// Type index of `take_refs`.+ take_refs: u32,+ /// Type index of `make_refs`.+ make_refs: u32,+ /// Type index of `take_struct`.+ take_struct: u32,+ /// Type index of `take_eq`.+ take_eq: u32,+ /// Type index of `take_i31`.+ take_i31: u32,+ /// Type index of `take_array`.+ take_array: u32,+}++/// Function indices of the host imports.+#[derive(Clone, Copy)]+struct HostFuncs {+ /// Function index of `gc`.+ gc: u32,+ /// Function index of `take_refs`.+ take_refs: u32,+ /// Function index of `make_refs`.+ make_refs: u32,+ /// Function index of `take_struct`.+ take_struct: u32,+ /// Function index of `take_eq`.+ take_eq: u32,+ /// Function index of `take_i31`.+ take_i31: u32,+ /// Function index of `take_array`.+ take_array: u32,+ /// Bank of `take_struct_N` and `take_array_N` imports, one per concrete type.+ typed_base: u32,+}++impl HostFuncs {+ /// Function index of the `take_*` import for the concrete type at `dense`.+ fn typed(&self, dense: u32) -> u32 {+ self.typed_base + dense+ }+}++/// One slot per abstract reference type plus a bank of typed slots. A bank is a+/// run of consecutive slots, one per concrete type, at `base + dense`.#[derive(Clone, Copy)]-struct WasmEncodingBases {+struct RootBanks {+ /// The `structref` slot.+ structref: u32,+ /// The `eqref` slot.+ eqref: u32,+ /// The `i31ref` slot.+ i31ref: u32,+ /// The `arrayref` slot.+ arrayref: u32,+ /// First slot of the bank of typed slots.+ typed_base: u32,+}++impl RootBanks {+ /// Slot of the concrete type at `dense`.+ fn typed(&self, dense: u32) -> u32 {+ self.typed_base + dense+ }+}++/// Locals of `run` after its `externref` parameters; see `RootBanks` for banks.+#[derive(Clone, Copy)]+struct LocalBanks {+ /// Temporary for `table.set` on the `externref` table.+ extern_scratch: u32,+ /// The `structref` root local.+ structref: u32,+ /// The `eqref` root local.+ eqref: u32,+ /// The `i31ref` root local.+ i31ref: u32,+ /// The `arrayref` root local.+ arrayref: u32,+ /// Typed roots, also the temporaries of null-guarded ops on that type.+ typed_base: u32,+ /// Second operand for ops on two values of one type (`array.copy`).+ typed2_base: u32,+ /// One shared prototype per type, chosen by `Types::prototype_types`.+ proto_base: u32,+}++impl LocalBanks {+ /// Typed root local of the concrete type at `dense`.+ fn typed(&self, dense: u32) -> u32 {+ self.typed_base + dense+ }++ /// Second operand local of the concrete type at `dense`.+ fn typed2(&self, dense: u32) -> u32 {+ self.typed2_base + dense+ }++ /// Prototype local of the concrete type at `dense`.+ fn proto(&self, dense: u32) -> u32 {+ self.proto_base + dense+ }+}++/// Where everything lives in the encoded module. `dense` arguments are positions+/// in the encoding order of the concrete types, which is what `type_index` holds.+#[derive(Clone, Copy)]+pub(crate) struct WasmEncodingBases {+ /// Function indices of the host imports.+ funcs: HostFuncs,+ /// Wasm index of the first concrete type.struct_type_base: u32,- typed_first_func_index: u32,- struct_local_idx: u32,- eq_local_idx: u32,- i31_local_idx: u32,- array_local_idx: u32,- typed_local_base: u32,- typed_local2_base: u32,- struct_global_idx: u32,- eq_global_idx: u32,- i31_global_idx: u32,- array_global_idx: u32,- typed_global_base: u32,- struct_table_idx: u32,- eq_table_idx: u32,- i31_table_idx: u32,- array_table_idx: u32,- typed_table_base: u32,+ /// Locals of `run`.+ locals: LocalBanks,+ /// Globals of the module.+ globals: RootBanks,+ /// Tables of the module.+ tables: RootBanks,+ /// Length of every array `ArrayNew` / `ArrayNewDefault` creates.array_length: u32,}-/// A description of a Wasm module that makes a series of `externref` table-/// operations.+impl WasmEncodingBases {+ /// Wasm type index of the concrete type at `dense`.+ fn wasm_type(&self, dense: u32) -> u32 {+ self.struct_type_base + dense+ }++ /// The slots of one root kind.+ fn root_slots(&self, kind: RefKind) -> RootSlots {+ let (l, g, t) = (&self.locals, &self.globals, &self.tables);+ match kind {+ RefKind::Extern { slot } => RootSlots {+ local: slot,+ global: slot,+ table: 0,+ tmp: l.extern_scratch,+ },+ RefKind::Struct => RootSlots {+ local: l.structref,+ global: g.structref,+ table: t.structref,+ tmp: l.structref,+ },+ RefKind::Eq => RootSlots {+ local: l.eqref,+ global: g.eqref,+ table: t.eqref,+ tmp: l.eqref,+ },+ RefKind::I31 => RootSlots {+ local: l.i31ref,+ global: g.i31ref,+ table: t.i31ref,+ tmp: l.i31ref,+ },+ RefKind::Array => RootSlots {+ local: l.arrayref,+ global: g.arrayref,+ table: t.arrayref,+ tmp: l.arrayref,+ },+ RefKind::Typed(d) => RootSlots {+ local: l.typed(d),+ global: g.typed(d),+ table: t.typed(d),+ tmp: l.typed(d),+ },+ }+ }+}++/// The kinds of reference the module keeps roots for, with `Typed` holding a dense type index.+#[derive(Clone, Copy, Debug)]+enum RefKind {+ /// An `externref`, in the parameter or global `slot` (unused for a table access).+ Extern { slot: u32 },+ /// A `structref`.+ Struct,+ /// An `eqref`.+ Eq,+ /// An `i31ref`.+ I31,+ /// An `arrayref`.+ Array,+ /// A reference to the concrete type at this dense index.+ Typed(u32),+}++/// Where a root access reads or writes.+#[derive(Clone, Copy, Debug)]+enum Storage {+ /// A local of `run`.+ Local,+ /// A global.+ Global,+ /// The element at this index of a table.+ Table(u32),+}++/// Whether a root access reads or writes.+#[derive(Clone, Copy, Debug)]+enum Dir {+ /// Read the root.+ Get,+ /// Write the root.+ Set,+}++/// The local, global, table and `table.set` temporary of one root kind.+#[derive(Clone, Copy)]+struct RootSlots {+ /// The local.+ local: u32,+ /// The global.+ global: u32,+ /// The table.+ table: u32,+ /// The local `table.set` parks its value in.+ tmp: u32,+}++/// Local declarations of a function, handing out indices in declaration order.+struct LocalDecls {+ /// The declarations, one entry per local.+ decls: Vec<(u32, ValType)>,+ /// Index of the next local to declare.+ next: u32,+}++impl LocalDecls {+ /// No locals yet, after `num_params` parameters.+ fn new(num_params: u32) -> Self {+ Self {+ decls: Vec::new(),+ next: num_params,+ }+ }++ /// Declare one local of type `ty` and return its index.+ fn declare(&mut self, ty: ValType) -> u32 {+ let index = self.next;+ self.next += 1;+ self.decls.push((1, ty));+ index+ }++ /// Declare a bank of `count` locals and return its base index.+ fn declare_bank(&mut self, count: u32, ty: impl Fn(u32) -> ValType) -> u32 {+ let base = self.next;+ for i in 0..count {+ self.declare(ty(i));+ }+ base+ }+}++/// The function types of the host imports and of `run`, which takes `num_params` `externref`s.+fn host_function_types(types: &mut TypeSection, num_params: u32) -> HostTypes {+ let three_refs = vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF];++ // `gc` returns a bunch of stuff 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.+ let gc = types.len();+ types.ty().function(vec![], three_refs.clone());++ let run = types.len();+ types.ty().function(+ vec![ValType::EXTERNREF; usize::try_from(num_params).unwrap()],+ vec![],+ );++ let take_refs = types.len();+ types.ty().function(three_refs.clone(), vec![]);++ let make_refs = types.len();+ types.ty().function(vec![], three_refs);++ let take_struct = types.len();+ types.ty().function(vec![ValType::Ref(STRUCTREF)], vec![]);++ let take_eq = types.len();+ types+ .ty()+ .function(vec![ValType::Ref(RefType::EQREF)], vec![]);++ // `take_i31` also receives the guest's inline `i31.get_s` / `i31.get_u`+ // results, so the host can check its view of the i31 against them.+ let take_i31 = types.len();+ types.ty().function(+ vec![ValType::Ref(RefType::I31REF), ValType::I32, ValType::I32],+ vec![],+ );++ let take_array = types.len();+ types+ .ty()+ .function(vec![ValType::Ref(RefType::ARRAYREF)], vec![]);++ HostTypes {+ gc,+ run,+ take_refs,+ make_refs,+ take_struct,+ take_eq,+ take_i31,+ take_array,+ }+}++/// Add one `(func (param (ref null $t)))` type per concrete type and return the first index.+fn typed_take_types(types: &mut TypeSection, struct_type_base: u32, concrete_count: u32) -> u32 {+ // Not `types.len()`: a rec group is one section entry but `concrete_count` type indices.+ let base = struct_type_base + concrete_count;+ for i in 0..concrete_count {+ types+ .ty()+ .function(vec![ValType::Ref(concrete(struct_type_base + i))], vec![]);+ }+ base+}++/// Tables of `table_size` elements, the `externref` one first, then one per abstract kind and one per concrete type.+fn encode_tables(+ table_size: u32,+ struct_type_base: u32,+ concrete_count: u32,+) -> (TableSection, RootBanks) {+ let mut tables = TableSection::new();+ tables.table(nullable_table(RefType::EXTERNREF, table_size));++ let structref = tables.len();+ tables.table(nullable_table(STRUCTREF, table_size));++ let eqref = tables.len();+ tables.table(nullable_table(RefType::EQREF, table_size));++ let i31ref = tables.len();+ tables.table(nullable_table(RefType::I31REF, table_size));++ let arrayref = tables.len();+ tables.table(nullable_table(RefType::ARRAYREF, table_size));++ let typed_base = tables.len();+ for i in 0..concrete_count {+ tables.table(nullable_table(concrete(struct_type_base + i), table_size));+ }++ let banks = RootBanks {+ structref,+ eqref,+ i31ref,+ arrayref,+ typed_base,+ };+ (tables, banks)+}++/// Null-initialized globals, `num_globals` `externref`s first, then one per abstract kind and one per concrete type.+fn encode_globals(+ num_globals: u32,+ struct_type_base: u32,+ concrete_count: u32,+) -> (GlobalSection, RootBanks) {+ let mut globals = GlobalSection::new();+ for _ in 0..num_globals {+ null_ref_global(&mut globals, HeapType::EXTERN);+ }+ let structref = null_ref_global(&mut globals, STRUCT);+ let eqref = null_ref_global(&mut globals, EQ);+ let i31ref = null_ref_global(&mut globals, HeapType::I31);+ let arrayref = null_ref_global(&mut globals, ARRAY);+ let typed_base = globals.len();+ for i in 0..concrete_count {+ null_ref_global(&mut globals, HeapType::Concrete(struct_type_base + i));+ }++ let banks = RootBanks {+ structref,+ eqref,+ i31ref,+ arrayref,+ typed_base,+ };+ (globals, banks)+}++/// The locals of `run`; see `LocalBanks` for what each one is.+fn declare_locals(+ num_params: u32,+ struct_type_base: u32,+ concrete_count: u32,+) -> (LocalDecls, LocalBanks) {+ let mut locals = LocalDecls::new(num_params);+ let extern_scratch = locals.declare(ValType::EXTERNREF);+ let structref = locals.declare(ValType::Ref(STRUCTREF));+ let eqref = locals.declare(ValType::Ref(RefType::EQREF));+ let i31ref = locals.declare(ValType::Ref(RefType::I31REF));+ let arrayref = locals.declare(ValType::Ref(RefType::ARRAYREF));++ let typed = |i| ValType::Ref(concrete(struct_type_base + i));+ let typed_base = locals.declare_bank(concrete_count, typed);+ let typed2_base = locals.declare_bank(concrete_count, typed);+ let proto_base = locals.declare_bank(concrete_count, typed);++ let banks = LocalBanks {+ extern_scratch,+ structref,+ eqref,+ i31ref,+ arrayref,+ typed_base,+ typed2_base,+ proto_base,+ };+ (locals, banks)+}++/// A description of a Wasm module that performs a series of GC operations on+/// `externref`s, `i31`s, and struct and array objects of its own types.#[derive(Clone, Debug, Default, Serialize, Deserialize)]pub struct GcOps {+ /// Limits controlling the structure of the module.pub(crate) limits: GcOpsLimits,+ /// The operations `run` performs, in order.pub(crate) ops: Vec<GcOp>,+ /// The struct and array types of the module.pub(crate) types: Types,}@@ -105,114 +632,84 @@ impl GcOps {let mut encoding_order_grouped = Vec::with_capacity(self.types.rec_groups.len());self.fixup(&mut encoding_order_grouped);- let mut module = Module::new();+ // Flat encoding order (dense index -> TypeId), for naming the typed+ // host imports and for field lookups during encoding.+ let encoding_order: Vec<TypeId> = encoding_order_grouped+ .iter()+ .flat_map(|(_, members)| members.iter().copied())+ .collect();- // 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(usize::try_from(self.limits.num_params).unwrap());- for _i in 0..self.limits.num_params {- params.push(ValType::EXTERNREF);- }- let params_len = u32::try_from(params.len()).unwrap();- 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: true,- heap_type: wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Struct,- },- })],- vec![],- );-- // 5: `take_eq`- types.ty().function(- vec![ValType::Ref(RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Eq,- },- })],- vec![],+ let host_types = host_function_types(&mut types, self.limits.num_params);+ let struct_type_base = types.len();+ let type_ids_to_index =+ self.encode_concrete_types(&mut types, &encoding_order_grouped, struct_type_base);+ let concrete_count = u32::try_from(type_ids_to_index.len()).unwrap();+ let typed_fn_type_base = typed_take_types(&mut types, struct_type_base, concrete_count);++ let (imports, funcs) = self.encode_imports(+ &host_types,+ typed_fn_type_base,+ struct_type_base,+ &encoding_order,);+ let (tables, table_banks) =+ encode_tables(self.limits.table_size, struct_type_base, concrete_count);+ let (globals, global_banks) =+ encode_globals(self.limits.num_globals, struct_type_base, concrete_count);+ let (local_decls, local_banks) =+ declare_locals(self.limits.num_params, struct_type_base, concrete_count);++ // `run` is the first (and only) defined function, right after the imports.+ let mut functions = FunctionSection::new();+ let mut exports = ExportSection::new();+ functions.function(host_types.run);+ exports.export("run", ExportKind::Func, imports.len());- // 6: `take_i31`- //- // Takes a `(ref null i31)` along with the guest's inline `i31.get_s`- // and `i31.get_u` results, so the host can re-derive them and assert- // that its view of the i31 matches the Wasm instructions'.- let take_i31_type_idx = types.len();- types.ty().function(- vec![ValType::Ref(RefType::I31REF), ValType::I32, ValType::I32],- vec![],- );+ let bases = WasmEncodingBases {+ funcs,+ struct_type_base,+ locals: local_banks,+ globals: global_banks,+ tables: table_banks,+ array_length: self.limits.array_length,+ };+ let func = self.encode_run_body(local_decls, bases, &type_ids_to_index, &encoding_order);+ let mut code = CodeSection::new();+ code.function(&func);- // 7: `take_array`- let take_array_type_idx = types.len();- types.ty().function(- vec![ValType::Ref(RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Array,- },- })],- vec![],- );+ let mut module = Module::new();+ module+ .section(&types)+ .section(&imports)+ .section(&functions)+ .section(&tables)+ .section(&globals)+ .section(&exports)+ .section(&code);- let struct_type_base: u32 = types.len();+ module.finish()+ }+ /// Emit every rec group in encoding order and return the Wasm type index of each type.+ fn encode_concrete_types(+ &self,+ types: &mut TypeSection,+ encoding_order_grouped: &[(RecGroupId, Vec<TypeId>)],+ struct_type_base: u32,+ ) -> BTreeMap<TypeId, u32> {// Build the type-id-to-wasm-index map from the pre-computed// encoding order (rec groups in topo order, members sorted by// supertype-first within each group).let mut type_ids_to_index: BTreeMap<TypeId, u32> = BTreeMap::new();let mut next_idx = struct_type_base;- for (_, members) in &encoding_order_grouped {+ for (_, members) in encoding_order_grouped {for &tid in members {type_ids_to_index.insert(tid, next_idx);next_idx += 1;}}- // Flat encoding order (dense index -> TypeId), used below for naming the- // typed host imports and, later, for field lookups during encoding.- let encoding_order: Vec<TypeId> = encoding_order_grouped- .iter()- .flat_map(|(_, members)| members.iter().copied())- .collect();-let encode_ty_id = |ty_id: &TypeId| -> wasm_encoder::SubType {let def = &self.types.type_defs[ty_id];let inner = match &def.composite_type {@@ -251,50 +748,45 @@ impl GcOps {}};- let mut concrete_count = 0;-- // Emit rec groups in the pre-computed order.- for (_, group_members) in &encoding_order_grouped {+ for (_, group_members) in encoding_order_grouped {let members: Vec<wasm_encoder::SubType> =group_members.iter().map(encode_ty_id).collect();types.ty().rec(members);- concrete_count += u32::try_from(group_members.len()).unwrap();}- let typed_fn_type_base: u32 = struct_type_base + concrete_count;-- for i in 0..concrete_count {- let concrete = struct_type_base + i;- types.ty().function(- vec![ValType::Ref(RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Concrete(concrete),- })],- vec![],- );- }+ type_ids_to_index+ }- // Import the GC function.+ /// The host imports, the fixed ones first and then one `take_*` per concrete type.+ fn encode_imports(+ &self,+ host_types: &HostTypes,+ typed_fn_type_base: u32,+ struct_type_base: u32,+ encoding_order: &[TypeId],+ ) -> (ImportSection, HostFuncs) {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));- imports.import("", "take_eq", EntityType::Function(5));- imports.import("", "take_i31", EntityType::Function(take_i31_type_idx));- imports.import("", "take_array", EntityType::Function(take_array_type_idx));-- // For each of our concrete struct/array types, define a function- // import that takes an argument of that concrete type. The import name- // records the kind so the host can define it appropriately.- let typed_first_func_index: u32 = imports.len();-- for i in 0..concrete_count {- let ty_idx = typed_fn_type_base + i;+ let mut import_func = |name: &str, type_idx: u32| -> u32 {+ let index = imports.len();+ imports.import("", name, EntityType::Function(type_idx));+ index+ };+ let gc = import_func("gc", host_types.gc);+ let take_refs = import_func("take_refs", host_types.take_refs);+ let make_refs = import_func("make_refs", host_types.make_refs);+ let take_struct = import_func("take_struct", host_types.take_struct);+ let take_eq = import_func("take_eq", host_types.take_eq);+ let take_i31 = import_func("take_i31", host_types.take_i31);+ let take_array = import_func("take_array", host_types.take_array);++ // The import name records the kind so the host can define it appropriately.+ let typed_base = imports.len();+ for (i, tid) in (0u32..).zip(encoding_order) {let wasm_idx = struct_type_base + i;- let is_array = encoding_order- .get(usize::try_from(i).unwrap())- .and_then(|tid| self.types.type_defs.get(tid))+ let is_array = self+ .types+ .type_defs+ .get(tid).map(|def| def.composite_type.is_array()).unwrap_or(false);let name = if is_array {@@ -302,261 +794,30 @@ impl GcOps {} else {format!("take_struct_{wasm_idx}")};- 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,- });-- let struct_table_idx = tables.len();- tables.table(TableType {- element_type: RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Struct,- },- },- minimum: u64::from(self.limits.table_size),- maximum: None,- table64: false,- shared: false,- });-- let eq_table_idx = tables.len();- tables.table(TableType {- element_type: RefType::EQREF,- minimum: u64::from(self.limits.table_size),- maximum: None,- table64: false,- shared: false,- });-- let i31_table_idx = tables.len();- tables.table(TableType {- element_type: RefType::I31REF,- minimum: u64::from(self.limits.table_size),- maximum: None,- table64: false,- shared: false,- });-- let array_table_idx = tables.len();- tables.table(TableType {- element_type: RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Array,- },- },- minimum: u64::from(self.limits.table_size),- maximum: None,- table64: false,- shared: false,- });-- let typed_table_base = tables.len();- for i in 0..concrete_count {- let concrete = struct_type_base + i;- tables.table(TableType {- element_type: RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Concrete(concrete),- },- minimum: u64::from(self.limits.table_size),- maximum: None,- table64: false,- shared: false,- });+ imports.import("", &name, EntityType::Function(typed_fn_type_base + i));}- // 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),- );- }-- // Add exactly one (ref.null struct) global.- let struct_global_idx = globals.len();- globals.global(- wasm_encoder::GlobalType {- val_type: ValType::Ref(RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Struct,- },- }),- mutable: true,- shared: false,- },- &ConstExpr::ref_null(wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Struct,- }),- );-- // Add exactly one (ref.null eq) global.- let eq_global_idx = globals.len();- globals.global(- wasm_encoder::GlobalType {- val_type: ValType::Ref(RefType::EQREF),- mutable: true,- shared: false,- },- &ConstExpr::ref_null(wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Eq,- }),- );-- // Add exactly one (ref.null i31) global.- let i31_global_idx = globals.len();- globals.global(- wasm_encoder::GlobalType {- val_type: ValType::Ref(RefType::I31REF),- mutable: true,- shared: false,- },- &ConstExpr::ref_null(wasm_encoder::HeapType::I31),- );-- // Add exactly one (ref.null array) global.- let array_global_idx = globals.len();- globals.global(- wasm_encoder::GlobalType {- val_type: ValType::Ref(RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Array,- },- }),- mutable: true,- shared: false,- },- &ConstExpr::ref_null(wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Array,- }),- );-- // Add one typed (ref <type>) global per struct/array type.- let typed_global_base = globals.len();- for i in 0..concrete_count {- let concrete = struct_type_base + i;- globals.global(- wasm_encoder::GlobalType {- val_type: ValType::Ref(RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Concrete(concrete),- }),- mutable: true,- shared: false,- },- &ConstExpr::ref_null(wasm_encoder::HeapType::Concrete(concrete)),- );- }-- // 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 mut local_decls: Vec<(u32, ValType)> = vec![(1, ValType::EXTERNREF)];-- let scratch_local = params_len;- let struct_local_idx = scratch_local + 1;- local_decls.push((- 1,- ValType::Ref(RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Struct,- },- }),- ));-- let eq_local_idx = struct_local_idx + 1;- local_decls.push((1, ValType::Ref(RefType::EQREF)));-- let i31_local_idx = eq_local_idx + 1;- local_decls.push((1, ValType::Ref(RefType::I31REF)));-- let array_local_idx = i31_local_idx + 1;- local_decls.push((- 1,- ValType::Ref(RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Array,- },- }),- ));-- let typed_local_base: u32 = array_local_idx + 1;- let typed_local2_base: u32 = typed_local_base + concrete_count;- // A third set holds one shared prototype per concrete type; see- // `Types::prototype_types`.- let proto_local_base: u32 = typed_local2_base + concrete_count;- for _ in 0..3 {- for i in 0..concrete_count {- let concrete = struct_type_base + i;- local_decls.push((- 1,- ValType::Ref(RefType {- nullable: true,- heap_type: wasm_encoder::HeapType::Concrete(concrete),- }),- ));- }- }-- let storage_bases = WasmEncodingBases {- struct_type_base,- typed_first_func_index,- struct_local_idx,- eq_local_idx,- i31_local_idx,- array_local_idx,- typed_local_base,- typed_local2_base,- struct_global_idx,- eq_global_idx,- i31_global_idx,- array_global_idx,- typed_global_base,- struct_table_idx,- eq_table_idx,- i31_table_idx,- array_table_idx,- typed_table_base,- array_length: self.limits.array_length,+ let funcs = HostFuncs {+ gc,+ take_refs,+ make_refs,+ take_struct,+ take_eq,+ take_i31,+ take_array,+ typed_base,};+ (imports, funcs)+ }+ /// The body of `run`, an endless loop that refills the prototypes and then runs every op.+ fn encode_run_body(+ &self,+ locals: LocalDecls,+ bases: WasmEncodingBases,+ type_ids_to_index: &BTreeMap<TypeId, u32>,+ encoding_order: &[TypeId],+ ) -> Function {let mut inhabitable = BTreeMap::new();self.types.inhabitable(&mut inhabitable);let struct_ref_target = self.types.least_rank_inhabitable_struct(&inhabitable);@@ -569,8 +830,8 @@ impl GcOps {let protos: BTreeMap<TypeId, u32> = proto_order.iter().map(|tid| {- let dense = type_ids_to_index[tid] - struct_type_base;- (*tid, proto_local_base + dense)+ let dense = type_ids_to_index[tid] - bases.struct_type_base;+ (*tid, bases.locals.proto(dense))}).collect();@@ -578,11 +839,13 @@ impl GcOps {types: &self.types,struct_ref_target,protos: &protos,- type_ids_to_index: &type_ids_to_index,+ type_ids_to_index,+ bases,+ encoding_order,};- let mut func = Function::new(local_decls);- func.instruction(&Instruction::Loop(wasm_encoder::BlockType::Empty));+ let mut func = Function::new(locals.decls);+ func.instruction(&Instruction::Loop(BlockType::Empty));// Refill the prototypes at the top of every iteration, so the ops below// never read a null local and each iteration allocates a fresh set of@@ -593,31 +856,12 @@ impl GcOps {func.instruction(&Instruction::LocalSet(protos[tid]));}for op in &self.ops {- op.encode(- &mut func,- scratch_local,- storage_bases,- ctx,- &encoding_order,- );+ op.encode(&mut func, ctx);}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()+ func}/// Fixes this test case such that it becomes valid.@@ -632,8 +876,8 @@ impl GcOps {/// method before translating this "AST"-style representation into a raw/// Wasm binary.pub fn fixup(&mut self, encoding_order_grouped: &mut Vec<(RecGroupId, Vec<TypeId>)>) {- self.limits.fixup();- self.types.fixup(&self.limits, encoding_order_grouped);+ self.limits.fixup_limits();+ self.types.fixup_types(&self.limits, encoding_order_grouped);let encoding_order: Vec<TypeId> = encoding_order_grouped.iter().flat_map(|(_, members)| members.iter().copied())@@ -658,7 +902,7 @@ impl GcOps {let mut operand_types = Vec::new();for op in &self.ops {- let Some(op) = op.fixup(+ let Some(op) = op.fixup_immediates(&self.limits,num_types,&struct_type_indices,@@ -671,7 +915,7 @@ impl GcOps {debug_assert!(operand_types.is_empty());op.operand_types(&mut operand_types);for ty in operand_types.drain(..) {- StackType::fixup(+ StackType::fixup_operand(ty,&mut stack,&mut new_ops,@@ -1287,6 +1531,7 @@ macro_rules! define_op_names {for_each_gc_op!(define_op_names);impl GcOp {+ /// The variant name, for tests.#[cfg(test)]pub(crate) fn name(&self) -> &'static str {macro_rules! define_gc_op_name {@@ -1306,6 +1551,7 @@ impl GcOp {for_each_gc_op!(define_gc_op_name)}+ /// The types this op pops, with `None` accepting any.pub(crate) fn operand_types(&self, out: &mut Vec<Option<StackType>>) {macro_rules! define_gc_op_operand_types {(@@ -1335,6 +1581,7 @@ impl GcOp {for_each_gc_op!(define_gc_op_operand_types)}+ /// The types this op pushes.pub(crate) fn result_types(&self, out: &mut Vec<StackType>) {macro_rules! define_gc_op_result_types {(@@ -1369,7 +1616,7 @@ impl GcOp {/// are the concrete encoding indices of each kind; a typed op remaps its/// type index into the matching set so struct ops never point at an array/// (or vice versa), and drops itself if no type of that kind exists.- pub(crate) fn fixup(+ pub(crate) fn fixup_immediates(&self,limits: &GcOpsLimits,num_types: u32,@@ -1413,276 +1660,79 @@ impl GcOp {for_each_gc_op!(define_gc_op_fixup)}- fn encode(- &self,- func: &mut Function,- scratch_local: u32,- encoding_bases: WasmEncodingBases,- ctx: EmitCtx<'_>,- encoding_order: &[TypeId],- ) {- let types = ctx.types;- let gc_func_idx = 0;- let take_refs_func_idx = 1;- let make_refs_func_idx = 2;- let take_structref_idx = 3;- let take_eqref_idx = 4;- let take_i31_idx = 5;- let take_arrayref_idx = 6;-+ /// Emit the Wasm instructions of this op into `func`.+ fn encode(&self, func: &mut Function, cx: EmitCtx<'_>) {+ let bases = cx.bases;+ use Dir::{Get, Set};+ use RefKind::{Array, Eq, Extern, I31, Struct, Typed};+ use Storage::{Global, Local, Table};match *self {+ // Host calls.Self::Gc => {- func.instruction(&Instruction::Call(gc_func_idx));+ func.instruction(&Instruction::Call(bases.funcs.gc));}Self::MakeRefs => {- func.instruction(&Instruction::Call(make_refs_func_idx));+ func.instruction(&Instruction::Call(bases.funcs.make_refs));}Self::TakeRefs => {- func.instruction(&Instruction::Call(take_refs_func_idx));- }- Self::TableGet { elem_index: x } => {- func.instruction(&Instruction::I32Const(x.cast_signed()));- func.instruction(&Instruction::TableGet(0));- }- Self::TableSet { elem_index: 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));+ func.instruction(&Instruction::Call(bases.funcs.take_refs));}- Self::GlobalGet { global_index: x } => {- func.instruction(&Instruction::GlobalGet(x));+ Self::TakeStructCall => {+ func.instruction(&Instruction::Call(bases.funcs.take_struct));}- Self::GlobalSet { global_index: x } => {- func.instruction(&Instruction::GlobalSet(x));+ Self::TakeEqCall => {+ func.instruction(&Instruction::Call(bases.funcs.take_eq));}- Self::LocalGet { local_index: x } => {- func.instruction(&Instruction::LocalGet(x));+ Self::TakeArrayCall => {+ func.instruction(&Instruction::Call(bases.funcs.take_array));}- Self::LocalSet { local_index: x } => {- func.instruction(&Instruction::LocalSet(x));- }- Self::Drop => {- func.instruction(&Instruction::Drop);+ Self::TakeTypedStructCall { type_index } | Self::TakeTypedArrayCall { type_index } => {+ func.instruction(&Instruction::Call(bases.funcs.typed(type_index)));}+ Self::TakeI31Call => encode_take_i31(func, bases),++ // Nulls and `i31` values.Self::NullExtern => {- func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::EXTERN));+ func.instruction(&Instruction::RefNull(HeapType::EXTERN));}Self::NullStruct => {- func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Struct,- }));+ func.instruction(&Instruction::RefNull(STRUCT));}Self::NullEq => {- func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Eq,- }));- }- Self::TakeEqCall => {- func.instruction(&Instruction::Call(take_eqref_idx));+ func.instruction(&Instruction::RefNull(EQ));}- Self::EqLocalGet => {- func.instruction(&Instruction::LocalGet(encoding_bases.eq_local_idx));- }- Self::EqLocalSet => {- func.instruction(&Instruction::LocalSet(encoding_bases.eq_local_idx));- }- Self::EqGlobalGet => {- func.instruction(&Instruction::GlobalGet(encoding_bases.eq_global_idx));- }- Self::EqGlobalSet => {- func.instruction(&Instruction::GlobalSet(encoding_bases.eq_global_idx));- }- Self::EqTableGet { elem_index } => {- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::TableGet(encoding_bases.eq_table_idx));+ Self::NullI31 => {+ func.instruction(&Instruction::RefNull(HeapType::I31));}- Self::EqTableSet { elem_index } => {- // Use eq_local_idx (eqref) to temporarily store the value before table.set.- func.instruction(&Instruction::LocalSet(encoding_bases.eq_local_idx));- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::LocalGet(encoding_bases.eq_local_idx));- func.instruction(&Instruction::TableSet(encoding_bases.eq_table_idx));+ Self::NullArray => {+ func.instruction(&Instruction::RefNull(ARRAY));}- // `NullTypedStruct` / `NullTypedArray` both produce a typed null; the- // concrete type index already resolves to the right kind.Self::NullTypedStruct { type_index } | Self::NullTypedArray { type_index } => {- func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::Concrete(- encoding_bases.struct_type_base + type_index,- )));- }- Self::StructNew { type_index: x } => {- for field in struct_fields(types, encoding_order, x).unwrap_or(&[]) {- field.field_type.emit_default_const(func, ctx);- }- func.instruction(&Instruction::StructNew(encoding_bases.struct_type_base + x));- }- Self::StructNewDefault { type_index: x } => {- // `struct.new_default` requires every field to be defaultable,- // which a non-nullable reference is not. Build those fields- // explicitly instead; the resulting value and stack effect are- // the same.- let fields = struct_fields(types, encoding_order, x).unwrap_or(&[]);- if fields.iter().all(|f| f.field_type.is_defaultable()) {- func.instruction(&Instruction::StructNewDefault(- encoding_bases.struct_type_base + x,- ));- } else {- for field in fields {- field.field_type.emit_default_const(func, ctx);- }- func.instruction(&Instruction::StructNew(encoding_bases.struct_type_base + x));- }- }- Self::ArrayNewDefault { type_index: x } => {- // Create a default-initialized array of a fixed length so most- // subsequent indexed accesses are in-bounds.- match array_element(types, encoding_order, x) {- // As above, `array.new_default` needs a defaultable- // element. `array.new` takes the initial value explicitly,- // so it covers non-nullable elements at the same length.- Some(element) if !element.field_type.is_defaultable() => {- element.field_type.emit_default_const(func, ctx);- func.instruction(&Instruction::I32Const(- encoding_bases.array_length.cast_signed(),- ));- func.instruction(&Instruction::ArrayNew(- encoding_bases.struct_type_base + x,- ));- }- _ => {- func.instruction(&Instruction::I32Const(- encoding_bases.array_length.cast_signed(),- ));- func.instruction(&Instruction::ArrayNewDefault(- encoding_bases.struct_type_base + x,- ));- }- }- }- Self::ArrayNew { type_index: x } => {- if let Some(element) = array_element(types, encoding_order, x) {- element.field_type.emit_default_const(func, ctx);- }- func.instruction(&Instruction::I32Const(- encoding_bases.array_length.cast_signed(),- ));- func.instruction(&Instruction::ArrayNew(encoding_bases.struct_type_base + x));- }- Self::ArrayNewFixed { type_index: x, n } => {- if let Some(element) = array_element(types, encoding_order, x) {- for _ in 0..n {- element.field_type.emit_default_const(func, ctx);- }- }- func.instruction(&Instruction::ArrayNewFixed {- array_type_index: encoding_bases.struct_type_base + x,- array_size: n,- });- }- Self::TakeStructCall => {- func.instruction(&Instruction::Call(take_structref_idx));- }- // Typed struct/array calls and storage share identical encodings- // (they index the same kind-agnostic typed func/local/global/table- // slots); only their abstract stack type differs.- Self::TakeTypedStructCall { type_index: x }- | Self::TakeTypedArrayCall { type_index: x } => {- let f = encoding_bases.typed_first_func_index + x;- func.instruction(&Instruction::Call(f));- }- Self::StructLocalGet => {- func.instruction(&Instruction::LocalGet(encoding_bases.struct_local_idx));- }- Self::TypedStructLocalGet { type_index: x }- | Self::TypedArrayLocalGet { type_index: x } => {- func.instruction(&Instruction::LocalGet(encoding_bases.typed_local_base + x));- }- Self::StructLocalSet => {- func.instruction(&Instruction::LocalSet(encoding_bases.struct_local_idx));- }- Self::TypedStructLocalSet { type_index: x }- | Self::TypedArrayLocalSet { type_index: x } => {- func.instruction(&Instruction::LocalSet(encoding_bases.typed_local_base + x));- }- Self::StructGlobalGet => {- func.instruction(&Instruction::GlobalGet(encoding_bases.struct_global_idx));- }- Self::TypedStructGlobalGet { type_index: x }- | Self::TypedArrayGlobalGet { type_index: x } => {- func.instruction(&Instruction::GlobalGet(- encoding_bases.typed_global_base + x,- ));- }- Self::StructGlobalSet => {- func.instruction(&Instruction::GlobalSet(encoding_bases.struct_global_idx));- }- Self::TypedStructGlobalSet { type_index: x }- | Self::TypedArrayGlobalSet { type_index: x } => {- func.instruction(&Instruction::GlobalSet(- encoding_bases.typed_global_base + x,- ));- }- Self::StructTableGet { elem_index } => {- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::TableGet(encoding_bases.struct_table_idx));+ let ty = HeapType::Concrete(bases.wasm_type(type_index));+ func.instruction(&Instruction::RefNull(ty));}- Self::TypedStructTableGet {- elem_index,- type_index,- }- | Self::TypedArrayTableGet {- elem_index,- type_index,- } => {- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::TableGet(- encoding_bases.typed_table_base + type_index,- ));- }- Self::StructTableSet { elem_index } => {- // Use struct_local_idx (anyref) to temporarily store the value before table.set- func.instruction(&Instruction::LocalSet(encoding_bases.struct_local_idx));- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::LocalGet(encoding_bases.struct_local_idx));- func.instruction(&Instruction::TableSet(encoding_bases.struct_table_idx));- }- Self::TypedStructTableSet {- elem_index,- type_index,+ Self::RefI31 { value } => {+ func.instruction(&Instruction::I32Const(value.cast_signed()));+ func.instruction(&Instruction::RefI31);}- | Self::TypedArrayTableSet {- elem_index,- type_index,- } => {- func.instruction(&Instruction::LocalSet(- encoding_bases.typed_local_base + type_index,- ));- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::LocalGet(- encoding_bases.typed_local_base + type_index,- ));- func.instruction(&Instruction::TableSet(- encoding_bases.typed_table_base + type_index,- ));++ // Allocation.+ Self::StructNew { type_index } => encode_struct_new(func, cx, type_index, false),+ Self::StructNewDefault { type_index } => encode_struct_new(func, cx, type_index, true),+ Self::ArrayNew { type_index } => encode_array_new(func, cx, type_index),+ Self::ArrayNewDefault { type_index } => encode_array_new_default(func, cx, type_index),+ Self::ArrayNewFixed { type_index, n } => {+ encode_array_new_fixed(func, cx, type_index, n)}++ // Casts. Upcasting to `eqref` is implicit in Wasm subtyping, so+ // those ops emit nothing; only the abstract stack type changes.Self::RefCastUpward {- sub_type_index: _,- super_type_index,+ super_type_index, ..}| Self::ArrayRefCastUpward {- sub_type_index: _,- super_type_index,- } => {- // The value on the stack is already the subtype, so this- // cast always succeeds.- let heap_type = wasm_encoder::HeapType::Concrete(- encoding_bases.struct_type_base + super_type_index,- );- func.instruction(&Instruction::RefCastNullable(heap_type));- }+ super_type_index, ..+ } => encode_upcast(func, bases, super_type_index),Self::RefCastDownward {sub_type_index,super_type_index,@@ -1690,412 +1740,472 @@ impl GcOp {| Self::ArrayRefCastDownward {sub_type_index,super_type_index,- } => {- // Fallible downcast that never traps:- //- // local.tee $my_temp- // ;; Test if the downcast will succeed.- // ref.test ...- // if (result (ref null $my_sub))- // ;; The downcast will succeed, do a downcast-or-trap- // ;; operation which we know will not trap.- // local.get $my_temp- // ref.cast ...- // else- // ;; The downcast would fail, so just create a null- // ;; reference instead.- // ref.null ...- // end- let sub_wasm_type = encoding_bases.struct_type_base + sub_type_index;- let sub_heap_type = wasm_encoder::HeapType::Concrete(sub_wasm_type);- let temp_local = encoding_bases.typed_local_base + super_type_index;-- // Tee the supertype value into a temp local (saves and- // leaves the value on the stack for ref.test).- func.instruction(&Instruction::LocalTee(temp_local));-- // Test if the downcast will succeed.- func.instruction(&Instruction::RefTestNullable(sub_heap_type));-- // if (result (ref null $sub_type))- func.instruction(&Instruction::If(wasm_encoder::BlockType::Result(- ValType::Ref(RefType {- nullable: true,- heap_type: sub_heap_type,- }),- )));-- // The downcast will succeed; do the cast.- func.instruction(&Instruction::LocalGet(temp_local));- func.instruction(&Instruction::RefCastNullable(sub_heap_type));-- func.instruction(&Instruction::Else);-- // The downcast would fail; produce null instead.- func.instruction(&Instruction::RefNull(sub_heap_type));-- func.instruction(&Instruction::End);- }+ } => encode_downcast(func, bases, sub_type_index, super_type_index),+ Self::StructRefAsEq+ | Self::TypedStructRefAsEq { .. }+ | Self::ArrayRefAsEq+ | Self::TypedArrayRefAsEq { .. }+ | Self::I31RefAsEq => {}++ // Field, element and payload access.Self::StructGet {type_index,field_index,- }- | Self::StructGetU {+ } => encode_struct_get(func, cx, type_index, field_index, false),+ Self::StructGetU {type_index,field_index,- } => {- let wasm_type = encoding_bases.struct_type_base + type_index;- let fields = encoding_order- .get(usize::try_from(type_index).unwrap())- .and_then(|tid| types.type_defs.get(tid))- .and_then(|def| match &def.composite_type {- CompositeType::Struct(st) => Some(&st.fields[..]),- CompositeType::Array(_) => None,- });-- match fields {- Some(fields) if !fields.is_empty() => {- let typed_local = encoding_bases.typed_local_base + type_index;- // Guard against null: save ref, check, skip if null.- func.instruction(&Instruction::LocalTee(typed_local));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));- func.instruction(&Instruction::Else);- func.instruction(&Instruction::LocalGet(typed_local));- let idx = field_index % u32::try_from(fields.len()).unwrap();- if fields[usize::try_from(idx).unwrap()].field_type.is_packed() {- if matches!(self, Self::StructGetU { .. }) {- func.instruction(&Instruction::StructGetU {- struct_type_index: wasm_type,- field_index: idx,- });- } else {- func.instruction(&Instruction::StructGetS {- struct_type_index: wasm_type,- field_index: idx,- });- }- } else {- func.instruction(&Instruction::StructGet {- struct_type_index: wasm_type,- field_index: idx,- });- }- // Drop the result — field values are not tracked- // on the abstract stack.- func.instruction(&Instruction::Drop);- func.instruction(&Instruction::End);- }- _ => {- func.instruction(&Instruction::Drop);- }- }- }+ } => encode_struct_get(func, cx, type_index, field_index, true),Self::StructSet {type_index,field_index,- } => {- let wasm_type = encoding_bases.struct_type_base + type_index;- let fields = encoding_order- .get(usize::try_from(type_index).unwrap())- .and_then(|tid| types.type_defs.get(tid))- .and_then(|def| match &def.composite_type {- CompositeType::Struct(st) => Some(&st.fields[..]),- CompositeType::Array(_) => None,- });-- match fields {- Some(fields) if !fields.is_empty() => {- let len = fields.len();- let start = (usize::try_from(field_index).unwrap()) % len;- let mutable_field = (0..len)- .map(|offset| (start + offset) % len)- .find(|&i| fields[i].mutable);-- match mutable_field {- Some(idx) => {- let typed_local = encoding_bases.typed_local_base + type_index;-- // Wasm stack: [struct_ref]- func.instruction(&Instruction::LocalTee(typed_local));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));- func.instruction(&Instruction::Else);- func.instruction(&Instruction::LocalGet(typed_local));- fields[idx].field_type.emit_default_const(func, ctx);- let idx = u32::try_from(idx).unwrap();- func.instruction(&Instruction::StructSet {- struct_type_index: wasm_type,- field_index: idx,- });- func.instruction(&Instruction::End);- }- None => {- func.instruction(&Instruction::Drop);- }- }- }- _ => {- func.instruction(&Instruction::Drop);- }- }- }- Self::NullI31 => {- func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::I31));- }- Self::RefI31 { value } => {- func.instruction(&Instruction::I32Const(value.cast_signed()));- func.instruction(&Instruction::RefI31);- }- Self::I31LocalGet => {- func.instruction(&Instruction::LocalGet(encoding_bases.i31_local_idx));- }- Self::I31LocalSet => {- func.instruction(&Instruction::LocalSet(encoding_bases.i31_local_idx));- }- Self::I31GlobalGet => {- func.instruction(&Instruction::GlobalGet(encoding_bases.i31_global_idx));+ } => encode_struct_set(func, cx, type_index, field_index),+ Self::ArrayGet { type_index, index } => {+ encode_array_get(func, cx, type_index, index, false)}- Self::I31GlobalSet => {- func.instruction(&Instruction::GlobalSet(encoding_bases.i31_global_idx));+ Self::ArrayGetU { type_index, index } => {+ encode_array_get(func, cx, type_index, index, true)}- Self::I31TableGet { elem_index } => {- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::TableGet(encoding_bases.i31_table_idx));+ Self::ArraySet { type_index, index } => encode_array_set(func, cx, type_index, index),+ Self::ArrayFill {+ type_index,+ offset,+ len,+ } => encode_array_fill(func, cx, type_index, offset, len),+ Self::ArrayCopy {+ type_index,+ dst_offset,+ src_offset,+ len,+ } => encode_array_copy(func, cx, type_index, dst_offset, src_offset, len),+ Self::ArrayLen => encode_array_len(func, bases),+ Self::I31GetS => encode_i31_get(func, bases, true),+ Self::I31GetU => encode_i31_get(func, bases, false),++ Self::Drop => {+ func.instruction(&Instruction::Drop);}- Self::I31TableSet { elem_index } => {- // Use i31_local_idx (i31ref) to temporarily store the value before table.set.- func.instruction(&Instruction::LocalSet(encoding_bases.i31_local_idx));- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::LocalGet(encoding_bases.i31_local_idx));- func.instruction(&Instruction::TableSet(encoding_bases.i31_table_idx));++ // Root reads and writes; see `encode_root`.+ Self::LocalGet { local_index } => {+ encode_root(func, bases, Extern { slot: local_index }, Local, Get)}- Self::StructRefAsEq- | Self::TypedStructRefAsEq { .. }- | Self::ArrayRefAsEq- | Self::TypedArrayRefAsEq { .. }- | Self::I31RefAsEq => {- // Upcasting to `eqref` is implicit in Wasm subtyping: `struct`,- // `array`, and `i31` are all subtypes of `eq`, so the value- // already on the stack is a valid `eqref` and no instruction is- // required. Only the abstract stack type changes (via- // `result_types`).+ Self::LocalSet { local_index } => {+ encode_root(func, bases, Extern { slot: local_index }, Local, Set)}- Self::I31GetS | Self::I31GetU => {- // `i31.get_s`/`i31.get_u` trap on a null reference, so guard- // against null: save the ref, test it, and only perform the- // get when non-null. The i32 result is not tracked on the- // abstract stack, so drop it.- func.instruction(&Instruction::LocalTee(encoding_bases.i31_local_idx));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));- func.instruction(&Instruction::Else);- func.instruction(&Instruction::LocalGet(encoding_bases.i31_local_idx));- if matches!(self, Self::I31GetS) {- func.instruction(&Instruction::I31GetS);- } else {- func.instruction(&Instruction::I31GetU);- }- func.instruction(&Instruction::Drop);- func.instruction(&Instruction::End);+ Self::GlobalGet { global_index } => {+ encode_root(func, bases, Extern { slot: global_index }, Global, Get)}- Self::TakeI31Call => {- // Differential check: pass the i31ref plus the guest's inline- // `i31.get_s` and `i31.get_u` results to the host, which- // re-derives them and asserts they match. The get instructions- // trap on null, so guard against it.- func.instruction(&Instruction::LocalTee(encoding_bases.i31_local_idx));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));- // Null branch: `take_i31(null, 0, 0)`.- func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::I31));- func.instruction(&Instruction::I32Const(0));- func.instruction(&Instruction::I32Const(0));- func.instruction(&Instruction::Call(take_i31_idx));- func.instruction(&Instruction::Else);- // Non-null branch: `take_i31(ref, i31.get_s, i31.get_u)`.- func.instruction(&Instruction::LocalGet(encoding_bases.i31_local_idx));- func.instruction(&Instruction::LocalGet(encoding_bases.i31_local_idx));- func.instruction(&Instruction::I31GetS);- func.instruction(&Instruction::LocalGet(encoding_bases.i31_local_idx));- func.instruction(&Instruction::I31GetU);- func.instruction(&Instruction::Call(take_i31_idx));- func.instruction(&Instruction::End);+ Self::GlobalSet { global_index } => {+ encode_root(func, bases, Extern { slot: global_index }, Global, Set)}- Self::NullArray => {- func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::Abstract {- shared: false,- ty: wasm_encoder::AbstractHeapType::Array,- }));+ Self::TableGet { elem_index } => {+ encode_root(func, bases, Extern { slot: 0 }, Table(elem_index), Get)}- Self::TakeArrayCall => {- func.instruction(&Instruction::Call(take_arrayref_idx));+ Self::TableSet { elem_index } => {+ encode_root(func, bases, Extern { slot: 0 }, Table(elem_index), Set)}- Self::ArrayLocalGet => {- func.instruction(&Instruction::LocalGet(encoding_bases.array_local_idx));++ Self::StructLocalGet => encode_root(func, bases, Struct, Local, Get),+ Self::StructLocalSet => encode_root(func, bases, Struct, Local, Set),+ Self::StructGlobalGet => encode_root(func, bases, Struct, Global, Get),+ Self::StructGlobalSet => encode_root(func, bases, Struct, Global, Set),+ Self::StructTableGet { elem_index } => {+ encode_root(func, bases, Struct, Table(elem_index), Get)}- Self::ArrayLocalSet => {- func.instruction(&Instruction::LocalSet(encoding_bases.array_local_idx));+ Self::StructTableSet { elem_index } => {+ encode_root(func, bases, Struct, Table(elem_index), Set)}- Self::ArrayGlobalGet => {- func.instruction(&Instruction::GlobalGet(encoding_bases.array_global_idx));++ Self::EqLocalGet => encode_root(func, bases, Eq, Local, Get),+ Self::EqLocalSet => encode_root(func, bases, Eq, Local, Set),+ Self::EqGlobalGet => encode_root(func, bases, Eq, Global, Get),+ Self::EqGlobalSet => encode_root(func, bases, Eq, Global, Set),+ Self::EqTableGet { elem_index } => encode_root(func, bases, Eq, Table(elem_index), Get),+ Self::EqTableSet { elem_index } => encode_root(func, bases, Eq, Table(elem_index), Set),++ Self::I31LocalGet => encode_root(func, bases, I31, Local, Get),+ Self::I31LocalSet => encode_root(func, bases, I31, Local, Set),+ Self::I31GlobalGet => encode_root(func, bases, I31, Global, Get),+ Self::I31GlobalSet => encode_root(func, bases, I31, Global, Set),+ Self::I31TableGet { elem_index } => {+ encode_root(func, bases, I31, Table(elem_index), Get)}- Self::ArrayGlobalSet => {- func.instruction(&Instruction::GlobalSet(encoding_bases.array_global_idx));+ Self::I31TableSet { elem_index } => {+ encode_root(func, bases, I31, Table(elem_index), Set)}++ Self::ArrayLocalGet => encode_root(func, bases, Array, Local, Get),+ Self::ArrayLocalSet => encode_root(func, bases, Array, Local, Set),+ Self::ArrayGlobalGet => encode_root(func, bases, Array, Global, Get),+ Self::ArrayGlobalSet => encode_root(func, bases, Array, Global, Set),Self::ArrayTableGet { elem_index } => {- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::TableGet(encoding_bases.array_table_idx));+ encode_root(func, bases, Array, Table(elem_index), Get)}Self::ArrayTableSet { elem_index } => {- // Use array_local_idx (arrayref) to temporarily store the value before table.set.- func.instruction(&Instruction::LocalSet(encoding_bases.array_local_idx));- func.instruction(&Instruction::I32Const(elem_index.cast_signed()));- func.instruction(&Instruction::LocalGet(encoding_bases.array_local_idx));- func.instruction(&Instruction::TableSet(encoding_bases.array_table_idx));+ encode_root(func, bases, Array, Table(elem_index), Set)}- Self::ArrayGet { type_index, index } | Self::ArrayGetU { type_index, index } => {- let wasm_type = encoding_bases.struct_type_base + type_index;- let typed_local = encoding_bases.typed_local_base + type_index;- let element = encoding_order- .get(usize::try_from(type_index).unwrap())- .and_then(|tid| types.type_defs.get(tid))- .and_then(|def| match &def.composite_type {- CompositeType::Array(at) => Some(&at.element),- CompositeType::Struct(_) => None,- });-- match element {- Some(element) => {- // Null-guard: array.get traps on null, so skip if null.- // The index is kept mostly in-bounds by fixup.- func.instruction(&Instruction::LocalTee(typed_local));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));- func.instruction(&Instruction::Else);- func.instruction(&Instruction::LocalGet(typed_local));- func.instruction(&Instruction::I32Const(index.cast_signed()));- if element.field_type.is_packed() {- if matches!(self, Self::ArrayGetU { .. }) {- func.instruction(&Instruction::ArrayGetU(wasm_type));- } else {- func.instruction(&Instruction::ArrayGetS(wasm_type));- }- } else {- func.instruction(&Instruction::ArrayGet(wasm_type));- }- // The element value is not tracked on the abstract stack.- func.instruction(&Instruction::Drop);- func.instruction(&Instruction::End);- }- None => {- func.instruction(&Instruction::Drop);- }- }++ // Typed struct and array ops share the typed banks; only their+ // abstract stack type differs.+ Self::TypedStructLocalGet { type_index } | Self::TypedArrayLocalGet { type_index } => {+ encode_root(func, bases, Typed(type_index), Local, Get)}- Self::ArraySet { type_index, index } => {- let wasm_type = encoding_bases.struct_type_base + type_index;- let typed_local = encoding_bases.typed_local_base + type_index;- let element = encoding_order- .get(usize::try_from(type_index).unwrap())- .and_then(|tid| types.type_defs.get(tid))- .and_then(|def| match &def.composite_type {- CompositeType::Array(at) => Some(at.element.clone()),- CompositeType::Struct(_) => None,- });-- match element {- Some(element) if element.mutable => {- // Null-guard: array.set traps on null, so skip if null.- func.instruction(&Instruction::LocalTee(typed_local));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));- func.instruction(&Instruction::Else);- func.instruction(&Instruction::LocalGet(typed_local));- func.instruction(&Instruction::I32Const(index.cast_signed()));- element.field_type.emit_default_const(func, ctx);- func.instruction(&Instruction::ArraySet(wasm_type));- func.instruction(&Instruction::End);- }- // Immutable element or non-array: just drop the operand.- _ => {- func.instruction(&Instruction::Drop);- }- }+ Self::TypedStructLocalSet { type_index } | Self::TypedArrayLocalSet { type_index } => {+ encode_root(func, bases, Typed(type_index), Local, Set)}- Self::ArrayFill {+ Self::TypedStructGlobalGet { type_index }+ | Self::TypedArrayGlobalGet { type_index } => {+ encode_root(func, bases, Typed(type_index), Global, Get)+ }+ Self::TypedStructGlobalSet { type_index }+ | Self::TypedArrayGlobalSet { type_index } => {+ encode_root(func, bases, Typed(type_index), Global, Set)+ }+ Self::TypedStructTableGet {+ elem_index,type_index,- offset,- len,- } => {- let wasm_type = encoding_bases.struct_type_base + type_index;- let typed_local = encoding_bases.typed_local_base + type_index;- match array_element(types, encoding_order, type_index) {- Some(element) if element.mutable => {- func.instruction(&Instruction::LocalTee(typed_local));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));- func.instruction(&Instruction::Else);- func.instruction(&Instruction::LocalGet(typed_local));- func.instruction(&Instruction::I32Const(offset.cast_signed()));- element.field_type.emit_default_const(func, ctx);- func.instruction(&Instruction::I32Const(len.cast_signed()));- func.instruction(&Instruction::ArrayFill(wasm_type));- func.instruction(&Instruction::End);- }- _ => {- func.instruction(&Instruction::Drop);- }- }}- Self::ArrayCopy {+ | Self::TypedArrayTableGet {+ elem_index,+ type_index,+ } => encode_root(func, bases, Typed(type_index), Table(elem_index), Get),+ Self::TypedStructTableSet {+ elem_index,type_index,- dst_offset,- src_offset,- len,- } => {- let wasm_type = encoding_bases.struct_type_base + type_index;- let dst_local = encoding_bases.typed_local_base + type_index;- let src_local = encoding_bases.typed_local2_base + type_index;- match array_element(types, encoding_order, type_index) {- Some(element) if element.mutable => {- func.instruction(&Instruction::LocalSet(src_local));- func.instruction(&Instruction::LocalSet(dst_local));- func.instruction(&Instruction::LocalGet(dst_local));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::LocalGet(src_local));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::I32Or);- func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));- func.instruction(&Instruction::Else);- func.instruction(&Instruction::LocalGet(dst_local));- func.instruction(&Instruction::I32Const(dst_offset.cast_signed()));- func.instruction(&Instruction::LocalGet(src_local));- func.instruction(&Instruction::I32Const(src_offset.cast_signed()));- func.instruction(&Instruction::I32Const(len.cast_signed()));- func.instruction(&Instruction::ArrayCopy {- array_type_index_dst: wasm_type,- array_type_index_src: wasm_type,- });- func.instruction(&Instruction::End);- }- _ => {- func.instruction(&Instruction::Drop);- func.instruction(&Instruction::Drop);- }- }}- Self::ArrayLen => {- // array.len traps on null, so guard; the length is not tracked.- func.instruction(&Instruction::LocalTee(encoding_bases.array_local_idx));- func.instruction(&Instruction::RefIsNull);- func.instruction(&Instruction::If(wasm_encoder::BlockType::Empty));- func.instruction(&Instruction::Else);- func.instruction(&Instruction::LocalGet(encoding_bases.array_local_idx));- func.instruction(&Instruction::ArrayLen);+ | Self::TypedArrayTableSet {+ elem_index,+ type_index,+ } => encode_root(func, bases, Typed(type_index), Table(elem_index), Set),+ };+ }+}++/// A root read or write, one instruction or the `table.set` idiom.+fn encode_root(+ func: &mut Function,+ bases: WasmEncodingBases,+ kind: RefKind,+ storage: Storage,+ dir: Dir,+) {+ let slots = bases.root_slots(kind);+ match (storage, dir) {+ (Storage::Local, Dir::Get) => {+ func.instruction(&Instruction::LocalGet(slots.local));+ }+ (Storage::Local, Dir::Set) => {+ func.instruction(&Instruction::LocalSet(slots.local));+ }+ (Storage::Global, Dir::Get) => {+ func.instruction(&Instruction::GlobalGet(slots.global));+ }+ (Storage::Global, Dir::Set) => {+ func.instruction(&Instruction::GlobalSet(slots.global));+ }+ (Storage::Table(elem), Dir::Get) => table_get(func, elem, slots.table),+ (Storage::Table(elem), Dir::Set) => table_set_via(func, slots.tmp, elem, slots.table),+ }+}++/// Pass the i31ref and the guest's own `i31.get_s` and `i31.get_u` results to the+/// host, which re-derives and compares them.+fn encode_take_i31(func: &mut Function, bases: WasmEncodingBases) {+ let i31 = bases.locals.i31ref;+ func.instruction(&Instruction::LocalTee(i31));+ func.instruction(&Instruction::RefIsNull);+ func.instruction(&Instruction::If(BlockType::Empty));+ // Null branch: `take_i31(null, 0, 0)`.+ func.instruction(&Instruction::RefNull(HeapType::I31));+ func.instruction(&Instruction::I32Const(0));+ func.instruction(&Instruction::I32Const(0));+ func.instruction(&Instruction::Call(bases.funcs.take_i31));+ func.instruction(&Instruction::Else);+ // Non-null branch: `take_i31(ref, i31.get_s, i31.get_u)`.+ func.instruction(&Instruction::LocalGet(i31));+ func.instruction(&Instruction::LocalGet(i31));+ func.instruction(&Instruction::I31GetS);+ func.instruction(&Instruction::LocalGet(i31));+ func.instruction(&Instruction::I31GetU);+ func.instruction(&Instruction::Call(bases.funcs.take_i31));+ func.instruction(&Instruction::End);+}++/// `struct.new` of default field values, or `struct.new_default` when asked and+/// every field is defaultable (a non-nullable reference is not).+fn encode_struct_new(func: &mut Function, cx: EmitCtx<'_>, type_index: u32, prefer_default: bool) {+ let fields = struct_fields(cx.types, cx.encoding_order, type_index).unwrap_or(&[]);+ let wasm_type = cx.bases.wasm_type(type_index);+ if prefer_default && fields.iter().all(|f| f.field_type.is_defaultable()) {+ func.instruction(&Instruction::StructNewDefault(wasm_type));+ } else {+ for field in fields {+ field.field_type.emit_default_const(func, cx);+ }+ func.instruction(&Instruction::StructNew(wasm_type));+ }+}++/// `array.new` of `array_length` default elements.+fn encode_array_new(func: &mut Function, cx: EmitCtx<'_>, type_index: u32) {+ if let Some(element) = array_element(cx.types, cx.encoding_order, type_index) {+ element.field_type.emit_default_const(func, cx);+ }+ func.instruction(&Instruction::I32Const(cx.bases.array_length.cast_signed()));+ func.instruction(&Instruction::ArrayNew(cx.bases.wasm_type(type_index)));+}++/// `array.new_default` of `array_length` elements, or `array.new` when the+/// element is not defaultable.+fn encode_array_new_default(func: &mut Function, cx: EmitCtx<'_>, type_index: u32) {+ let wasm_type = cx.bases.wasm_type(type_index);+ let len = cx.bases.array_length.cast_signed();+ match array_element(cx.types, cx.encoding_order, type_index) {+ Some(element) if !element.field_type.is_defaultable() => {+ element.field_type.emit_default_const(func, cx);+ func.instruction(&Instruction::I32Const(len));+ func.instruction(&Instruction::ArrayNew(wasm_type));+ }+ _ => {+ func.instruction(&Instruction::I32Const(len));+ func.instruction(&Instruction::ArrayNewDefault(wasm_type));+ }+ }+}++/// `array.new_fixed` of `n` default elements.+fn encode_array_new_fixed(func: &mut Function, cx: EmitCtx<'_>, type_index: u32, n: u32) {+ if let Some(element) = array_element(cx.types, cx.encoding_order, type_index) {+ for _ in 0..n {+ element.field_type.emit_default_const(func, cx);+ }+ }+ func.instruction(&Instruction::ArrayNewFixed {+ array_type_index: cx.bases.wasm_type(type_index),+ array_size: n,+ });+}++/// The value on the stack is already the subtype, so this cast always succeeds.+fn encode_upcast(func: &mut Function, bases: WasmEncodingBases, super_type_index: u32) {+ let heap_type = HeapType::Concrete(bases.wasm_type(super_type_index));+ func.instruction(&Instruction::RefCastNullable(heap_type));+}++/// A downcast that never traps, testing with `ref.test` first and producing `ref.null` on failure.+fn encode_downcast(+ func: &mut Function,+ bases: WasmEncodingBases,+ sub_type_index: u32,+ super_type_index: u32,+) {+ let sub_wasm_type = bases.wasm_type(sub_type_index);+ let sub_heap_type = HeapType::Concrete(sub_wasm_type);+ let temp_local = bases.locals.typed(super_type_index);++ func.instruction(&Instruction::LocalTee(temp_local));+ func.instruction(&Instruction::RefTestNullable(sub_heap_type));+ func.instruction(&Instruction::If(BlockType::Result(ValType::Ref(concrete(+ sub_wasm_type,+ )))));+ func.instruction(&Instruction::LocalGet(temp_local));+ func.instruction(&Instruction::RefCastNullable(sub_heap_type));+ func.instruction(&Instruction::Else);+ func.instruction(&Instruction::RefNull(sub_heap_type));+ func.instruction(&Instruction::End);+}++/// Null-guarded `struct.get` of field `field_index % len`, dropping the value.+fn encode_struct_get(+ func: &mut Function,+ cx: EmitCtx<'_>,+ type_index: u32,+ field_index: u32,+ unsigned: bool,+) {+ let wasm_type = cx.bases.wasm_type(type_index);+ match struct_fields(cx.types, cx.encoding_order, type_index) {+ Some(fields) if !fields.is_empty() => {+ let typed_local = cx.bases.locals.typed(type_index);+ let idx = field_index % u32::try_from(fields.len()).unwrap();+ let field_type = fields[usize::try_from(idx).unwrap()].field_type;+ let get = struct_get_instruction(wasm_type, idx, field_type, unsigned);+ if_non_null(func, typed_local, |func| {+ func.instruction(&Instruction::LocalGet(typed_local));+ func.instruction(&get);+ // Field values are not tracked on the abstract stack.func.instruction(&Instruction::Drop);- func.instruction(&Instruction::End);- }+ });+ }+ _ => {+ func.instruction(&Instruction::Drop);}}}++/// Null-guarded `struct.set` of a default value into the first mutable field at or+/// after `field_index`, wrapping around, or a drop when no field is mutable.+fn encode_struct_set(func: &mut Function, cx: EmitCtx<'_>, type_index: u32, field_index: u32) {+ let wasm_type = cx.bases.wasm_type(type_index);+ let mutable_field = struct_fields(cx.types, cx.encoding_order, type_index)+ .filter(|fields| !fields.is_empty())+ .and_then(|fields| {+ let len = fields.len();+ let start = usize::try_from(field_index).unwrap() % len;+ (0..len)+ .map(|offset| (start + offset) % len)+ .find(|&i| fields[i].mutable)+ .map(|i| (u32::try_from(i).unwrap(), fields[i].field_type))+ });++ match mutable_field {+ Some((idx, field_type)) => {+ let typed_local = cx.bases.locals.typed(type_index);+ if_non_null(func, typed_local, |func| {+ func.instruction(&Instruction::LocalGet(typed_local));+ field_type.emit_default_const(func, cx);+ func.instruction(&Instruction::StructSet {+ struct_type_index: wasm_type,+ field_index: idx,+ });+ });+ }+ None => {+ func.instruction(&Instruction::Drop);+ }+ }+}++/// Null-guarded `array.get`, dropping the element, with `index` kept mostly in bounds by fixup.+fn encode_array_get(+ func: &mut Function,+ cx: EmitCtx<'_>,+ type_index: u32,+ index: u32,+ unsigned: bool,+) {+ let wasm_type = cx.bases.wasm_type(type_index);+ let typed_local = cx.bases.locals.typed(type_index);+ match array_element(cx.types, cx.encoding_order, type_index) {+ Some(element) => {+ let get = array_get_instruction(wasm_type, element.field_type, unsigned);+ if_non_null(func, typed_local, |func| {+ func.instruction(&Instruction::LocalGet(typed_local));+ func.instruction(&Instruction::I32Const(index.cast_signed()));+ func.instruction(&get);+ // The element value is not tracked on the abstract stack.+ func.instruction(&Instruction::Drop);+ });+ }+ None => {+ func.instruction(&Instruction::Drop);+ }+ }+}++/// Null-guarded `array.set` of a default value, or a drop when the element is immutable.+fn encode_array_set(func: &mut Function, cx: EmitCtx<'_>, type_index: u32, index: u32) {+ let wasm_type = cx.bases.wasm_type(type_index);+ let typed_local = cx.bases.locals.typed(type_index);+ match array_element(cx.types, cx.encoding_order, type_index) {+ Some(element) if element.mutable => {+ if_non_null(func, typed_local, |func| {+ func.instruction(&Instruction::LocalGet(typed_local));+ func.instruction(&Instruction::I32Const(index.cast_signed()));+ element.field_type.emit_default_const(func, cx);+ func.instruction(&Instruction::ArraySet(wasm_type));+ });+ }+ _ => {+ func.instruction(&Instruction::Drop);+ }+ }+}++/// Null-guarded `array.fill` with a default value, or a drop when the element is immutable.+fn encode_array_fill(func: &mut Function, cx: EmitCtx<'_>, type_index: u32, offset: u32, len: u32) {+ let wasm_type = cx.bases.wasm_type(type_index);+ let typed_local = cx.bases.locals.typed(type_index);+ match array_element(cx.types, cx.encoding_order, type_index) {+ Some(element) if element.mutable => {+ if_non_null(func, typed_local, |func| {+ func.instruction(&Instruction::LocalGet(typed_local));+ func.instruction(&Instruction::I32Const(offset.cast_signed()));+ element.field_type.emit_default_const(func, cx);+ func.instruction(&Instruction::I32Const(len.cast_signed()));+ func.instruction(&Instruction::ArrayFill(wasm_type));+ });+ }+ _ => {+ func.instruction(&Instruction::Drop);+ }+ }+}++/// `array.copy` between the two arrays on the stack, skipped if either is null.+/// The source is parked in the second typed bank while the destination is checked.+fn encode_array_copy(+ func: &mut Function,+ cx: EmitCtx<'_>,+ type_index: u32,+ dst_offset: u32,+ src_offset: u32,+ len: u32,+) {+ let wasm_type = cx.bases.wasm_type(type_index);+ let dst_local = cx.bases.locals.typed(type_index);+ let src_local = cx.bases.locals.typed2(type_index);+ match array_element(cx.types, cx.encoding_order, type_index) {+ Some(element) if element.mutable => {+ func.instruction(&Instruction::LocalSet(src_local));+ func.instruction(&Instruction::LocalSet(dst_local));+ func.instruction(&Instruction::LocalGet(dst_local));+ func.instruction(&Instruction::RefIsNull);+ func.instruction(&Instruction::LocalGet(src_local));+ func.instruction(&Instruction::RefIsNull);+ func.instruction(&Instruction::I32Or);+ func.instruction(&Instruction::If(BlockType::Empty));+ func.instruction(&Instruction::Else);+ func.instruction(&Instruction::LocalGet(dst_local));+ func.instruction(&Instruction::I32Const(dst_offset.cast_signed()));+ func.instruction(&Instruction::LocalGet(src_local));+ func.instruction(&Instruction::I32Const(src_offset.cast_signed()));+ func.instruction(&Instruction::I32Const(len.cast_signed()));+ func.instruction(&Instruction::ArrayCopy {+ array_type_index_dst: wasm_type,+ array_type_index_src: wasm_type,+ });+ func.instruction(&Instruction::End);+ }+ _ => {+ func.instruction(&Instruction::Drop);+ func.instruction(&Instruction::Drop);+ }+ }+}++/// Null-guarded `array.len`, dropping the length.+fn encode_array_len(func: &mut Function, bases: WasmEncodingBases) {+ let array_local = bases.locals.arrayref;+ if_non_null(func, array_local, |func| {+ func.instruction(&Instruction::LocalGet(array_local));+ func.instruction(&Instruction::ArrayLen);+ func.instruction(&Instruction::Drop);+ });+}++/// Null-guarded `i31.get_s` or `i31.get_u`, dropping the value.+fn encode_i31_get(func: &mut Function, bases: WasmEncodingBases, signed: bool) {+ let i31_local = bases.locals.i31ref;+ let get = if signed {+ Instruction::I31GetS+ } else {+ Instruction::I31GetU+ };+ if_non_null(func, i31_local, |func| {+ func.instruction(&Instruction::LocalGet(i31_local));+ func.instruction(&get);+ func.instruction(&Instruction::Drop);+ });+}diff --git a/crates/fuzzing/src/generators/gc_ops/stack.rs b/crates/fuzzing/src/generators/gc_ops/stack.rsnew file mode 100644index 0000000000..babad27458--- /dev/null+++ b/crates/fuzzing/src/generators/gc_ops/stack.rs@@ -0,0 +1,256 @@+//! The abstract operand stack `GcOps::fixup` tracks to make an op sequence valid.++use crate::generators::gc_ops::ops::GcOp;+use crate::generators::gc_ops::types::{TypeId, Types};++/// Tracks the required operand type on the abstract value stack.+#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]+pub enum StackType {+ /// `externref`.+ ExternRef,+ /// `eqref`.+ Eq,+ /// `i31ref`.+ I31,+ /// `(ref $*)`, optionally with a concrete type index.+ Struct(Option<u32>),+ /// `(ref array)` or `(ref $t)`, optionally with a concrete type index.+ Array(Option<u32>),+}++/// Whether the dense type index `sub` is the same as, or a subtype of, `sup`.+fn is_subtype_dense(sub: u32, sup: u32, types: &Types, encoding_order: &[TypeId]) -> bool {+ let sub = encoding_order.get(usize::try_from(sub).unwrap());+ let sup = encoding_order.get(usize::try_from(sup).unwrap());+ match (sub, sup) {+ (Some(&sub), Some(&sup)) => types.is_subtype(sub, sup),+ _ => false,+ }+}++/// Whether `top` can serve as an operand of type `req`, including by subtyping.+fn satisfies(req: StackType, top: StackType, types: &Types, encoding_order: &[TypeId]) -> bool {+ use StackType::{Array, Eq, ExternRef, I31, Struct};+ match (req, top) {+ (ExternRef, ExternRef) | (I31, I31) => true,+ // struct, array and i31 are all subtypes of eq.+ (Eq, Eq | Struct(_) | Array(_) | I31) => true,+ (Struct(None), Struct(_)) | (Array(None), Array(_)) => true,+ (Struct(Some(want)), Struct(Some(have))) | (Array(Some(want)), Array(Some(have))) => {+ is_subtype_dense(have, want, types, encoding_order)+ }+ _ => false,+ }+}++/// The op that produces a value of type `req` from nothing, a null or a fresh+/// object for a concrete type. Abstract nulls also cover the case of no types at all.+fn synthesize(req: StackType, num_types: u32) -> GcOp {+ match req {+ StackType::ExternRef => GcOp::NullExtern,+ StackType::Eq => GcOp::NullEq,+ StackType::I31 => GcOp::RefI31 { value: 0 },+ StackType::Struct(None) => GcOp::NullStruct,+ StackType::Array(None) => GcOp::NullArray,+ StackType::Struct(Some(t)) => {+ debug_assert_ne!(num_types, 0, "typed struct requirement with no types");+ GcOp::StructNew {+ type_index: StackType::clamp(t, num_types),+ }+ }+ StackType::Array(Some(t)) => {+ debug_assert_ne!(num_types, 0, "typed array requirement with no types");+ GcOp::ArrayNewDefault {+ type_index: StackType::clamp(t, num_types),+ }+ }+ }+}++impl StackType {+ /// Ensure the top of `stack` satisfies `req`, emitting a fixup op if it does+ /// not, then pop it. `None` (a `Drop` operand) takes anything.+ pub fn fixup_operand(+ req: Option<StackType>,+ stack: &mut Vec<StackType>,+ out: &mut Vec<GcOp>,+ num_types: u32,+ types: &Types,+ encoding_order: &[TypeId],+ ) {+ let mut result_types = Vec::new();+ match req {+ None => {+ if stack.is_empty() {+ Self::emit(GcOp::NullExtern, stack, out, num_types, &mut result_types);+ }+ }+ Some(req) => {+ let ok = stack+ .last()+ .is_some_and(|&top| satisfies(req, top, types, encoding_order));+ if !ok {+ let op = synthesize(req, num_types);+ Self::emit(op, stack, out, num_types, &mut result_types);+ }+ }+ }+ let popped = stack.pop();+ log::trace!("[StackType::fixup_operand] req={req:?} popped={popped:?} stack={stack:?}");+ }++ /// Emit an opcode and update the stack.+ pub(crate) fn emit(+ op: GcOp,+ stack: &mut Vec<Self>,+ out: &mut Vec<GcOp>,+ num_types: u32,+ result_types: &mut Vec<Self>,+ ) {+ log::trace!(+ "[StackType::emit] op={op:?} stack_len_before={} num_types={num_types}",+ stack.len()+ );+ out.push(op);+ result_types.clear();+ op.result_types(result_types);+ for ty in result_types {+ let clamped_ty = match ty {+ Self::Struct(Some(t)) => Self::Struct(Some(Self::clamp(*t, num_types))),+ Self::Array(Some(t)) => Self::Array(Some(Self::clamp(*t, num_types))),+ other => *other,+ };+ log::trace!("[StackType::emit] push result {clamped_ty:?}");+ stack.push(clamped_ty);+ }+ log::trace!("[StackType::emit] leave stack={stack:?}");+ }++ /// Fixup for cast ops: ensures the sub/super type relationship actually+ /// holds. Always repairs the op rather than dropping it.+ ///+ /// For upcast the operand (sub) is on the stack, so we keep sub fixed+ /// and adjust super. For downcast the operand (super) is on the stack,+ /// so we keep super fixed and adjust sub.+ pub fn fixup_cast(op: GcOp, types: &Types, encoding_order: &[TypeId]) -> GcOp {+ match op {+ GcOp::RefCastUpward {+ sub_type_index,+ super_type_index,+ } => {+ // Operand is sub (on the stack) — keep it, fix super.+ let super_type_index = Self::find_supertype_of(+ sub_type_index,+ super_type_index,+ types,+ encoding_order,+ );+ GcOp::RefCastUpward {+ sub_type_index,+ super_type_index,+ }+ }+ GcOp::RefCastDownward {+ sub_type_index,+ super_type_index,+ } => {+ // Operand is super (on the stack) — keep it, fix sub.+ let sub_type_index =+ Self::find_subtype_of(super_type_index, sub_type_index, types, encoding_order);+ GcOp::RefCastDownward {+ sub_type_index,+ super_type_index,+ }+ }+ // Array casts use the same index repair (subtyping is kind-agnostic).+ GcOp::ArrayRefCastUpward {+ sub_type_index,+ super_type_index,+ } => {+ let super_type_index = Self::find_supertype_of(+ sub_type_index,+ super_type_index,+ types,+ encoding_order,+ );+ GcOp::ArrayRefCastUpward {+ sub_type_index,+ super_type_index,+ }+ }+ GcOp::ArrayRefCastDownward {+ sub_type_index,+ super_type_index,+ } => {+ let sub_type_index =+ Self::find_subtype_of(super_type_index, sub_type_index, types, encoding_order);+ GcOp::ArrayRefCastDownward {+ sub_type_index,+ super_type_index,+ }+ }+ other => other,+ }+ }++ /// Given a sub type on the stack, find a valid super_type_index such+ /// that sub <: super. Keeps sub fixed. Falls back to self-cast.+ fn find_supertype_of(+ sub_type_index: u32,+ super_type_index: u32,+ types: &Types,+ encoding_order: &[TypeId],+ ) -> u32 {+ if let (Some(&sub_tid), Some(&super_tid)) = (+ encoding_order.get(usize::try_from(sub_type_index).unwrap()),+ encoding_order.get(usize::try_from(super_type_index).unwrap()),+ ) {+ // Already valid.+ if types.is_subtype(sub_tid, super_tid) {+ return super_type_index;+ }+ // Try sub's direct supertype.+ if let Some(actual_super) = types.type_defs.get(&sub_tid).and_then(|d| d.supertype) {+ if let Some(idx) = encoding_order.iter().position(|&t| t == actual_super) {+ return u32::try_from(idx).unwrap();+ }+ }+ }+ // Self-cast.+ sub_type_index+ }++ /// Given a super type on the stack, find a valid sub_type_index such+ /// that sub <: super. Keeps super fixed. Falls back to self-cast.+ fn find_subtype_of(+ super_type_index: u32,+ sub_type_index: u32,+ types: &Types,+ encoding_order: &[TypeId],+ ) -> u32 {+ if let (Some(&sub_tid), Some(&super_tid)) = (+ encoding_order.get(usize::try_from(sub_type_index).unwrap()),+ encoding_order.get(usize::try_from(super_type_index).unwrap()),+ ) {+ // Already valid.+ if types.is_subtype(sub_tid, super_tid) {+ return sub_type_index;+ }+ // Try to find any direct subtype of super.+ for (idx, tid) in encoding_order.iter().enumerate() {+ if let Some(def) = types.type_defs.get(tid) {+ if def.supertype == Some(super_tid) {+ return u32::try_from(idx).unwrap();+ }+ }+ }+ }+ // Self-cast.+ super_type_index+ }++ /// Clamp a type index to the number of types.+ fn clamp(t: u32, n: u32) -> u32 {+ if n == 0 { 0 } else { t % n }+ }+}diff --git a/crates/fuzzing/src/generators/gc_ops/tests.rs b/crates/fuzzing/src/generators/gc_ops/tests.rsindex c0acfb4288..19550c8e9c 100644--- a/crates/fuzzing/src/generators/gc_ops/tests.rs+++ b/crates/fuzzing/src/generators/gc_ops/tests.rs@@ -1,9 +1,8 @@use crate::generators::gc_ops::{limits::GcOpsLimits,ops::{GcOp, GcOps, OP_NAMES},- types::{- ArrayType, CompositeType, FieldType, RecGroupId, StackType, StructField, TypeId, Types,- },+ stack::StackType,+ types::{ArrayType, CompositeType, FieldType, RecGroupId, StructField, TypeId, Types},};use mutatis;use mutatis::mutators as m;@@ -179,7 +178,7 @@ fn limits_stay_in_range() -> mutatis::Result<()> {let _ = env_logger::try_init();let mut ops = test_ops(5, 5, 5);- ops.limits.fixup();+ ops.limits.fixup_limits();let mut session = mutatis::Session::new().seed(0xC0FFEE);for _ in 0..2048 {@@ -466,7 +465,7 @@ fn fixup_preserves_subtyping_within_same_rec_group() {array_length: 5,};- types.fixup(&limits, &mut Vec::new());+ types.fixup_types(&limits, &mut Vec::new());assert_eq!(types.rec_group_of(super_ty), Some(g));assert_eq!(types.rec_group_of(sub_ty), Some(g));@@ -515,7 +514,7 @@ fn fixup_breaks_one_edge_in_multi_rec_group_type_cycle() {array_length: 5,};- types.fixup(&limits, &mut Vec::new());+ types.fixup_types(&limits, &mut Vec::new());let a_super = types.type_defs.get(&a).unwrap().supertype;let c_super = types.type_defs.get(&c).unwrap().supertype;@@ -797,7 +796,7 @@ fn stacktype_fixup_accepts_subtype_for_supertype_requirement() {let mut stack = vec![StackType::Struct(Some(2))];let mut out = vec![];- StackType::fixup(+ StackType::fixup_operand(Some(StackType::Struct(Some(1))),&mut stack,&mut out,@@ -819,7 +818,7 @@ fn stacktype_fixup_accepts_subtype_for_supertype_requirement() {let mut stack = vec![StackType::Struct(Some(2))];let mut out = vec![];- StackType::fixup(+ StackType::fixup_operand(Some(StackType::Struct(Some(0))),&mut stack,&mut out,@@ -838,7 +837,7 @@ fn stacktype_fixup_accepts_subtype_for_supertype_requirement() {let mut stack = vec![StackType::Struct(Some(0))];let mut out = vec![];- StackType::fixup(+ StackType::fixup_operand(Some(StackType::Struct(Some(1))),&mut stack,&mut out,@@ -1162,7 +1161,7 @@ fn fixup_repoints_dangling_references() {}],);- types.fixup(&limits, &mut Vec::new());+ types.fixup_types(&limits, &mut Vec::new());let def = types.type_defs.get(&TypeId(40)).unwrap();assert_eq!(diff --git a/crates/fuzzing/src/generators/gc_ops/types.rs b/crates/fuzzing/src/generators/gc_ops/types.rsindex f9704ecab6..1b81276881 100644--- a/crates/fuzzing/src/generators/gc_ops/types.rs+++ b/crates/fuzzing/src/generators/gc_ops/types.rs@@ -1,7 +1,7 @@//! Types for the `gc` operations.use crate::generators::gc_ops::limits::GcOpsLimits;-use crate::generators::gc_ops::ops::GcOp;+use crate::generators::gc_ops::ops::WasmEncodingBases;use mutatis::Generate;use serde::{Deserialize, Serialize};use std::collections::btree_map::Entry;@@ -178,7 +178,7 @@ macro_rules! define_field_type_enum {}FieldType::StructRef { nullable: false } => {// `(ref struct)` is satisfied by any struct, so build- // the cheapest one. `fix_uninhabitable` only leaves this+ // the cheapest one. `fixup_uninhabitable` only leaves this// field non-nullable when such a struct exists.let tid = ctx.struct_ref_target.expect("non-nullable struct ref must have a target");emit_ref_to(tid, func, ctx);@@ -210,6 +210,10 @@ pub(crate) struct EmitCtx<'a> {pub(crate) protos: &'a BTreeMap<TypeId, u32>,/// The Wasm type index assigned to each `TypeId`.pub(crate) type_ids_to_index: &'a BTreeMap<TypeId, u32>,+ /// Where the encoded module keeps its imports, locals, globals and tables.+ pub(crate) bases: WasmEncodingBases,+ /// Dense type index -> `TypeId`, the order the concrete types are encoded in.+ pub(crate) encoding_order: &'a [TypeId],}/// Emit a reference to the given type, constructing a new instance if necessary.@@ -314,16 +318,20 @@ impl CompositeType {/// A sub-type definition (the per-type payload).#[derive(Clone, Debug, Serialize, Deserialize, mutatis::Mutate)]pub struct SubType {+ /// Whether the type may not be used as a supertype.#[mutatis(default_mutate)]pub(crate) is_final: bool,+ /// The direct supertype, if any.#[mutatis(default_mutate)]pub(crate) supertype: Option<TypeId>,+ /// The struct or array definition.#[mutatis(default_mutate)]pub(crate) composite_type: CompositeType,}/// Supertype graph: edges go from a type to its supertype.struct SupertypeGraph<'a> {+ /// The type definitions the edges are read from.type_defs: &'a BTreeMap<TypeId, SubType>,}@@ -353,8 +361,11 @@ impl Graph<TypeId> for SupertypeGraph<'_> {/// Rec-group dependency graph: group A depends on group B when a type/// in A has a supertype in B.struct RecGroupGraph<'a> {+ /// The type definitions.type_defs: &'a BTreeMap<TypeId, SubType>,+ /// The members of every rec group.rec_groups: &'a BTreeMap<RecGroupId, BTreeSet<TypeId>>,+ /// The rec group of every type.type_to_group: &'a BTreeMap<TypeId, RecGroupId>,}@@ -418,6 +429,7 @@ entity_impl!(RecGroupNode);/// A densely-indexed view of the rec-group dependency graph, suitable forstruct DenseRecGroupGraph {+ /// Successor nodes of every node.adjacency: Vec<Vec<RecGroupNode>>,}@@ -814,16 +826,38 @@ impl Types {}}- /// Fix up the types to ensure they are within the limits.- pub fn fixup(+ /// Make the type graph valid and within `limits`, then compute its encoding order.+ pub fn fixup_types(&mut self,limits: &GcOpsLimits,encoding_order_grouped: &mut Vec<(RecGroupId, Vec<TypeId>)>,) {+ self.fixup_counts(limits);+ self.fixup_orphans();+ self.fixup_supertypes();+ self.fixup_field_counts(limits);+ self.fixup_reference_fields();++ // Make the type graph well-founded before encoding it.+ self.break_supertype_cycles();+ let type_to_group = self.type_to_group_map();+ self.merge_rec_group_cycles(&type_to_group);+ // Merging changes group membership, so recompute the map for the encoding order.+ let type_to_group = self.type_to_group_map();++ self.fixup_prefix_compatibility();+ self.fixup_uninhabitable();++ debug_assert!(self.is_well_formed(limits));++ self.encoding_order_grouped(encoding_order_grouped, &type_to_group);+ }++ /// Trim types and rec groups to `limits` and drop dangling and duplicate group members.+ fn fixup_counts(&mut self, limits: &GcOpsLimits) {let max_rec_groups = usize::try_from(limits.max_rec_groups).unwrap();let max_types = usize::try_from(limits.max_types).unwrap();- // 1. Trim excess types.while self.type_defs.len() > max_types {if let Some((tid, _)) = self.type_defs.pop_last() {for members in self.rec_groups.values_mut() {@@ -832,18 +866,18 @@ impl Types {}}- // 2. Drop dangling references and deduplicate across groups.let mut seen = BTreeSet::new();for members in self.rec_groups.values_mut() {members.retain(|tid| self.type_defs.contains_key(tid) && seen.insert(*tid));}- // 3. Trim excess rec groups.while self.rec_groups.len() > max_rec_groups {self.rec_groups.pop_last();}+ }- // 4. Find all orphans (from trimmed groups or never in any group).+ /// House every type that has no rec group in the first group, or drop it if there is none.+ fn fixup_orphans(&mut self) {let housed: BTreeSet<TypeId> = self.rec_groups.values()@@ -856,7 +890,6 @@ impl Types {.copied().collect();- // 5. Adopt orphans or drop them.if let Some(first_members) = self.rec_groups.values_mut().next() {first_members.extend(orphans);} else {@@ -864,25 +897,24 @@ impl Types {self.type_defs.remove(tid);}}+ }- // 6. Repair supertype edges.- self.fixup_supertypes();-- // 7. Trim struct fields to max_fields limit (arrays always have exactly- // one element).+ /// Trim struct fields to `max_fields`, leaving arrays with their single element.+ fn fixup_field_counts(&mut self, limits: &GcOpsLimits) {let max_fields = usize::try_from(limits.max_fields).unwrap();for def in self.type_defs.values_mut() {if let CompositeType::Struct(ref mut st) = def.composite_type {st.fields.truncate(max_fields);}}+ }- // 8. Normalize reference fields (struct fields and array elements alike).+ /// Point every concrete reference field (struct fields and array elements alike)+ /// at a live type. Nullability is left to `fixup_uninhabitable`.+ fn fixup_reference_fields(&mut self) {let valid_type_ids: BTreeSet<TypeId> = self.type_defs.keys().copied().collect();for def in self.type_defs.values_mut() {for field in def.composite_type.fields_mut() {- // Nullability is left alone here; step 11 relaxes only the- // non-nullable references that cannot be satisfied.if let FieldType::Ref { type_id, .. } = &mut field.field_type {if !valid_type_ids.contains(type_id) {if let Some(live) = nearest_live_type_id(&valid_type_ids, *type_id, None) {@@ -897,18 +929,11 @@ impl Types {}}}+ }- // 9. Break supertype cycles and merge rec-group reference cycles, so- // the type graph is well-founded before we encode it.- self.break_supertype_cycles();- let type_to_group = self.type_to_group_map();- self.merge_rec_group_cycles(&type_to_group);- // Merging changes group membership, so recompute the reverse map for- // the encoding-order computation below.- let type_to_group = self.type_to_group_map();-- // 10. Ensure subtype fields are prefix-compatible with supertype fields.- // Process in topological order (supertype before subtype).+ /// Make every subtype's fields a prefix-compatible extension of its supertype's,+ /// in topological order (supertype before subtype).+ fn fixup_prefix_compatibility(&mut self) {let mut topo_order = Vec::new();self.sort_types_topo(&mut topo_order);for tid in &topo_order {@@ -921,8 +946,8 @@ impl Types {let Some(super_def) = self.type_defs.get(&super_id) else {continue;};- // Step 6 guarantees the subtype and supertype share a composite- // kind. so match on the supertype and repair the subtype to match.+ // `fixup_supertypes` guarantees the subtype and supertype share a+ // composite kind, so match on the supertype and repair the subtype.match &super_def.composite_type {CompositeType::Struct(super_st) => {let super_fields = super_st.fields.clone();@@ -953,14 +978,6 @@ impl Types {}}}-- // 11. Relax non-nullable reference fields that cannot be satisfied.- self.fix_uninhabitable();-- debug_assert!(self.is_well_formed(limits));-- // 12. Compute encoding order (reuses type_to_group from step 9).- self.encoding_order_grouped(encoding_order_grouped, &type_to_group);}/// Whether `field` can be given a value using only the types in `ok`.@@ -1030,7 +1047,7 @@ impl Types {}/// Relax fields of a struct type to be nullable when uninhabitable, so that it can be constructed.- pub(crate) fn fix_uninhabitable(&mut self) {+ pub(crate) fn fixup_uninhabitable(&mut self) {let mut ok = BTreeMap::new();loop {self.inhabitable(&mut ok);@@ -1063,7 +1080,7 @@ impl Types {/// Relax a field of a struct type to be nullable, and propagate the change to all subtypes.fn relax_field(&mut self, tid: TypeId, index: usize) {// Walk up to the highest ancestor that still has this field. Supertype- // cycles are already broken by step 9, so this terminates.+ // cycles are already broken by `break_supertype_cycles`, so this terminates.let mut root = tid;while let Some(sup) = self.type_defs[&root].supertype {if self.type_defs[&sup].composite_type.fields().len() <= index {@@ -1258,7 +1275,7 @@ impl Types {// Every type must be constructible. A non-nullable reference field has// no default value, so a cycle of them leaves types that validate but- // can never be instantiated. See `fix_uninhabitable`.+ // can never be instantiated. See `fixup_uninhabitable`.let mut inhabitable = BTreeMap::new();self.inhabitable(&mut inhabitable);if inhabitable.len() != self.type_defs.len() {@@ -1273,389 +1290,3 @@ impl Types {true}}--/// Tracks the required operand type on the abstract value stack.-#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]-pub enum StackType {- /// `externref`.- ExternRef,- /// `eqref`.- Eq,- /// `i31ref`.- I31,- /// `(ref $*)` — optionally with a concrete type index.- Struct(Option<u32>),- /// `(ref array)` or `(ref $t)` — optionally with a concrete type index.- Array(Option<u32>),-}--impl StackType {- /// Ensure the top of `stack` satisfies `req`, emitting fixup ops as needed.- pub fn fixup(- req: Option<StackType>,- stack: &mut Vec<StackType>,- out: &mut Vec<GcOp>,- num_types: u32,- types: &Types,- encoding_order: &[TypeId],- ) {- log::trace!(- "[StackType::fixup] enter req={req:?} num_types={num_types} stack_len={} stack={stack:?}",- stack.len()- );- let mut result_types = Vec::new();- match req {- None => {- if stack.is_empty() {- log::trace!("[StackType::fixup] None: empty stack -> emit NullExtern");- Self::emit(GcOp::NullExtern, stack, out, num_types, &mut result_types);- }- let popped = stack.pop();- log::trace!("[StackType::fixup] None: pop -> {popped:?} stack={stack:?}");- }- Some(Self::ExternRef) => match stack.last() {- Some(Self::ExternRef) => {- log::trace!("[StackType::fixup] ExternRef: top ok -> pop");- stack.pop();- }- other => {- log::trace!(- "[StackType::fixup] ExternRef: mismatch top={other:?} -> emit NullExtern+pop"- );- Self::emit(GcOp::NullExtern, stack, out, num_types, &mut result_types);- let popped = stack.pop();- log::trace!(- "[StackType::fixup] ExternRef: after emit pop -> {popped:?} stack={stack:?}"- );- }- },- Some(Self::Eq) => match stack.last() {- // struct, array, and i31 are all subtypes of eq, so any of them- // on the stack satisfies an eqref requirement.- Some(Self::Eq) | Some(Self::Struct(_)) | Some(Self::Array(_)) | Some(Self::I31) => {- log::trace!("[StackType::fixup] Eq: top ok -> pop");- stack.pop();- }- other => {- log::trace!("[StackType::fixup] Eq: mismatch top={other:?} -> emit NullEq+pop");- Self::emit(GcOp::NullEq, stack, out, num_types, &mut result_types);- let popped = stack.pop();- log::trace!(- "[StackType::fixup] Eq: after emit pop -> {popped:?} stack={stack:?}"- );- }- },- Some(Self::I31) => match stack.last() {- Some(Self::I31) => {- log::trace!("[StackType::fixup] I31: top ok -> pop");- stack.pop();- }- other => {- log::trace!(- "[StackType::fixup] I31: mismatch top={other:?} -> emit RefI31+pop"- );- Self::emit(- GcOp::RefI31 { value: 0 },- stack,- out,- num_types,- &mut result_types,- );- let popped = stack.pop();- log::trace!(- "[StackType::fixup] I31: after emit pop -> {popped:?} stack={stack:?}"- );- }- },- Some(Self::Struct(wanted)) => {- let ok = match (wanted, stack.last()) {- (Some(wanted), Some(Self::Struct(Some(actual)))) => {- let sub = encoding_order- .get(usize::try_from(*actual).unwrap())- .copied();- let sup = encoding_order- .get(usize::try_from(wanted).unwrap())- .copied();- let st = match (sub, sup) {- (Some(sub), Some(sup)) => types.is_subtype(sub, sup),- _ => false,- };- log::trace!(- "[StackType::fixup] Struct: actual={actual} wanted={wanted} is_subtype={st}"- );- st- }- (None, Some(Self::Struct(_))) => {- log::trace!(- "[StackType::fixup] Struct: abstract wanted, concrete stack -> ok"- );- true- }- _ => {- log::trace!(- "[StackType::fixup] Struct: no match wanted={wanted:?} last={:?} -> ok=false",- stack.last()- );- false- }- };-- if ok {- let popped = stack.pop();- log::trace!("[StackType::fixup] Struct: ok -> pop {popped:?} stack={stack:?}");- } else {- match wanted {- // When num_types == 0, GcOp::fixup() should have dropped the ops- // that require a concrete type.- // But it keeps the ops that work with abstract types.- // Since our mutator can legally remove all the types,- // StackType::fixup() should insert GcOp::NullStruct()- // to satisfy the undropped ops that work with abstract types.- None => {- log::trace!(- "[StackType::fixup] Struct synthesize NullStruct stack_before={stack:?}"- );- Self::emit(GcOp::NullStruct, stack, out, num_types, &mut result_types);- let popped = stack.pop();- log::trace!(- "[StackType::fixup] NullStruct: after emit pop -> {popped:?} stack={stack:?}"- );- }- Some(t) => {- debug_assert_ne!(- num_types, 0,- "typed struct requirement with num_types == 0; op should have been removed"- );- let t = Self::clamp(t, num_types);-- log::trace!(- "[StackType::fixup] Struct synthesize StructNew type_index={t} stack_before={stack:?}"- );- Self::emit(- GcOp::StructNew { type_index: t },- stack,- out,- num_types,- &mut result_types,- );- log::trace!(- "[StackType::fixup] StructNew: after emit stack={stack:?} (next: pop operand)"- );- let popped = stack.pop();- log::trace!(- "[StackType::fixup] StructNew: pop -> {popped:?} stack={stack:?}"- );- }- }- }- }- Some(Self::Array(wanted)) => {- let ok = match (wanted, stack.last()) {- (Some(wanted), Some(Self::Array(Some(actual)))) => {- let sub = encoding_order- .get(usize::try_from(*actual).unwrap())- .copied();- let sup = encoding_order- .get(usize::try_from(wanted).unwrap())- .copied();- match (sub, sup) {- (Some(sub), Some(sup)) => types.is_subtype(sub, sup),- _ => false,- }- }- // Abstract arrayref requirement accepts any array on the stack.- (None, Some(Self::Array(_))) => true,- _ => false,- };-- if ok {- stack.pop();- } else {- match wanted {- // Abstract requirement: a null arrayref satisfies it.- None => {- Self::emit(GcOp::NullArray, stack, out, num_types, &mut result_types);- stack.pop();- }- // Concrete requirement: synthesize a fresh array of that type.- Some(t) => {- debug_assert_ne!(- num_types, 0,- "typed array requirement with num_types == 0; op should have been removed"- );- let t = Self::clamp(t, num_types);- Self::emit(- GcOp::ArrayNewDefault { type_index: t },- stack,- out,- num_types,- &mut result_types,- );- stack.pop();- }- }- }- }- }- log::trace!(- "[StackType::fixup] leave stack_len={} stack={stack:?} out_len={}",- stack.len(),- out.len()- );- }-- /// Emit an opcode and update the stack.- pub(crate) fn emit(- op: GcOp,- stack: &mut Vec<Self>,- out: &mut Vec<GcOp>,- num_types: u32,- result_types: &mut Vec<Self>,- ) {- log::trace!(- "[StackType::emit] op={op:?} stack_len_before={} num_types={num_types}",- stack.len()- );- out.push(op);- result_types.clear();- op.result_types(result_types);- for ty in result_types {- let clamped_ty = match ty {- Self::Struct(Some(t)) => Self::Struct(Some(Self::clamp(*t, num_types))),- Self::Array(Some(t)) => Self::Array(Some(Self::clamp(*t, num_types))),- other => *other,- };- log::trace!("[StackType::emit] push result {clamped_ty:?}");- stack.push(clamped_ty);- }- log::trace!("[StackType::emit] leave stack={stack:?}");- }-- /// Fixup for cast ops: ensures the sub/super type relationship actually- /// holds. Always repairs the op rather than dropping it.- ///- /// For upcast the operand (sub) is on the stack, so we keep sub fixed- /// and adjust super. For downcast the operand (super) is on the stack,- /// so we keep super fixed and adjust sub.- pub fn fixup_cast(op: GcOp, types: &Types, encoding_order: &[TypeId]) -> GcOp {- match op {- GcOp::RefCastUpward {- sub_type_index,- super_type_index,- } => {- // Operand is sub (on the stack) — keep it, fix super.- let super_type_index = Self::find_supertype_of(- sub_type_index,- super_type_index,- types,- encoding_order,- );- GcOp::RefCastUpward {- sub_type_index,- super_type_index,- }- }- GcOp::RefCastDownward {- sub_type_index,- super_type_index,- } => {- // Operand is super (on the stack) — keep it, fix sub.- let sub_type_index =- Self::find_subtype_of(super_type_index, sub_type_index, types, encoding_order);- GcOp::RefCastDownward {- sub_type_index,- super_type_index,- }- }- // Array casts use the same index repair (subtyping is kind-agnostic).- GcOp::ArrayRefCastUpward {- sub_type_index,- super_type_index,- } => {- let super_type_index = Self::find_supertype_of(- sub_type_index,- super_type_index,- types,- encoding_order,- );- GcOp::ArrayRefCastUpward {- sub_type_index,- super_type_index,- }- }- GcOp::ArrayRefCastDownward {- sub_type_index,- super_type_index,- } => {- let sub_type_index =- Self::find_subtype_of(super_type_index, sub_type_index, types, encoding_order);- GcOp::ArrayRefCastDownward {- sub_type_index,- super_type_index,- }- }- other => other,- }- }-- /// Given a sub type on the stack, find a valid super_type_index such- /// that sub <: super. Keeps sub fixed. Falls back to self-cast.- fn find_supertype_of(- sub_type_index: u32,- super_type_index: u32,- types: &Types,- encoding_order: &[TypeId],- ) -> u32 {- if let (Some(&sub_tid), Some(&super_tid)) = (- encoding_order.get(usize::try_from(sub_type_index).unwrap()),- encoding_order.get(usize::try_from(super_type_index).unwrap()),- ) {- // Already valid.- if types.is_subtype(sub_tid, super_tid) {- return super_type_index;- }- // Try sub's direct supertype.- if let Some(actual_super) = types.type_defs.get(&sub_tid).and_then(|d| d.supertype) {- if let Some(idx) = encoding_order.iter().position(|&t| t == actual_super) {- return u32::try_from(idx).unwrap();- }- }- }- // Self-cast.- sub_type_index- }-- /// Given a super type on the stack, find a valid sub_type_index such- /// that sub <: super. Keeps super fixed. Falls back to self-cast.- fn find_subtype_of(- super_type_index: u32,- sub_type_index: u32,- types: &Types,- encoding_order: &[TypeId],- ) -> u32 {- if let (Some(&sub_tid), Some(&super_tid)) = (- encoding_order.get(usize::try_from(sub_type_index).unwrap()),- encoding_order.get(usize::try_from(super_type_index).unwrap()),- ) {- // Already valid.- if types.is_subtype(sub_tid, super_tid) {- return sub_type_index;- }- // Try to find any direct subtype of super.- for (idx, tid) in encoding_order.iter().enumerate() {- if let Some(def) = types.type_defs.get(tid) {- if def.supertype == Some(super_tid) {- return u32::try_from(idx).unwrap();- }- }- }- }- // Self-cast.- super_type_index- }-- /// Clamp a type index to the number of types.- fn clamp(t: u32, n: u32) -> u32 {- if n == 0 { 0 } else { t % n }- }-}