A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Rec struct (#11605)
Khagan (Khan) Karimov370042d0bc84
diff --git a/crates/fuzzing/src/generators/table_ops.rs b/crates/fuzzing/src/generators/table_ops.rsindex 11244239a9..88b631006c 100644--- a/crates/fuzzing/src/generators/table_ops.rs+++ b/crates/fuzzing/src/generators/table_ops.rs@@ -10,20 +10,49 @@ use wasm_encoder::{TypeSection, ValType,};+use std::collections::{BTreeMap, BTreeSet};+const NUM_PARAMS_RANGE: RangeInclusive<u32> = 0..=10;+const MAX_TYPES_RANGE: RangeInclusive<u32> = 0..=32;const NUM_GLOBALS_RANGE: RangeInclusive<u32> = 0..=10;const TABLE_SIZE_RANGE: RangeInclusive<u32> = 0..=100;-const NUM_REC_GROUPS_RANGE: RangeInclusive<u32> = 0..=10;+const MAX_REC_GROUPS_RANGE: RangeInclusive<u32> = 0..=10;const MAX_OPS: usize = 100;/// RecGroup ID struct definition.-#[derive(Debug, Clone, Eq, PartialOrd, PartialEq, Ord, Hash, Default, Serialize, Deserialize)]+#[derive(+ Debug, Copy, Clone, Eq, PartialOrd, PartialEq, Ord, Hash, Default, Serialize, Deserialize,+)]pub struct RecGroupId(u32);+/// TypeID struct definition.+#[derive(Debug, Clone, Eq, PartialOrd, PartialEq, Ord, Hash, Default, Serialize, Deserialize)]+pub struct TypeId(u32);++/// StructType definition+#[derive(Debug, Default, Serialize, Deserialize)]+pub struct StructType {+ // Empty for now; fields will come in a future PR.+}++/// CompsiteType definition+#[derive(Debug, Serialize, Deserialize)]+pub enum CompositeType {+ /// Struct Type definition+ Struct(StructType),+}++/// SubType definition+#[derive(Debug, Serialize, Deserialize)]+pub struct SubType {+ pub(crate) rec_group: RecGroupId,+ pub(crate) composite_type: CompositeType,+}/// Struct types definition.#[derive(Debug, Default, Serialize, Deserialize)]pub struct Types {- rec_groups: std::collections::BTreeSet<RecGroupId>,+ rec_groups: BTreeSet<RecGroupId>,+ type_defs: BTreeMap<TypeId, SubType>,}impl Types {@@ -31,6 +60,7 @@ impl Types {pub fn new() -> Self {Self {rec_groups: Default::default(),+ type_defs: Default::default(),}}@@ -39,9 +69,31 @@ impl Types {self.rec_groups.insert(id)}- /// Iterate over all allocated recursive groups.- pub fn groups(&self) -> impl Iterator<Item = &RecGroupId> {- self.rec_groups.iter()+ /// Insert a rec-group id.+ pub fn insert_empty_struct(&mut self, id: TypeId, group: RecGroupId) {+ self.type_defs.insert(+ id,+ SubType {+ rec_group: group,+ composite_type: CompositeType::Struct(StructType::default()),+ },+ );+ }++ /// Removes any entries beyond the given limit.+ pub fn fixup(&mut self, limits: &TableOpsLimits) {+ while self.rec_groups.len() > limits.max_rec_groups as usize {+ self.rec_groups.pop_last();+ }+ while self.type_defs.len() > limits.max_types as usize {+ self.type_defs.pop_last();+ }++ debug_assert!(+ self.type_defs+ .values()+ .all(|ty| self.rec_groups.contains(&ty.rec_group))+ );}}@@ -51,7 +103,8 @@ pub struct TableOpsLimits {pub(crate) num_params: u32,pub(crate) num_globals: u32,pub(crate) table_size: u32,- pub(crate) num_rec_groups: u32,+ pub(crate) max_rec_groups: u32,+ pub(crate) max_types: u32,}impl TableOpsLimits {@@ -62,7 +115,8 @@ impl TableOpsLimits {num_params,num_globals,table_size,- num_rec_groups,+ max_rec_groups,+ max_types,} = self;let clamp = |limit: &mut u32, range: RangeInclusive<u32>| {@@ -71,7 +125,8 @@ impl TableOpsLimits {clamp(table_size, TABLE_SIZE_RANGE);clamp(num_params, NUM_PARAMS_RANGE);clamp(num_globals, NUM_GLOBALS_RANGE);- clamp(num_rec_groups, NUM_REC_GROUPS_RANGE);+ clamp(max_rec_groups, MAX_REC_GROUPS_RANGE);+ clamp(max_types, MAX_TYPES_RANGE);}}@@ -136,6 +191,39 @@ impl TableOps {vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],);+ let mut rec_groups: BTreeMap<RecGroupId, Vec<TypeId>> = self+ .types+ .rec_groups+ .iter()+ .copied()+ .map(|id| (id, Vec::new()))+ .collect();++ for (id, ty) in self.types.type_defs.iter() {+ rec_groups.entry(ty.rec_group).or_default().push(id.clone());+ }++ let encode_ty_id = |ty_id: &TypeId| -> wasm_encoder::SubType {+ let def = &self.types.type_defs[ty_id];+ match &def.composite_type {+ CompositeType::Struct(StructType {}) => wasm_encoder::SubType {+ is_final: true,+ supertype_idx: None,+ composite_type: wasm_encoder::CompositeType {+ inner: wasm_encoder::CompositeInnerType::Struct(wasm_encoder::StructType {+ fields: Box::new([]),+ }),+ shared: false,+ },+ },+ }+ };++ for type_ids in rec_groups.values() {+ let members: Vec<wasm_encoder::SubType> = type_ids.iter().map(encode_ty_id).collect();+ types.ty().rec(members);+ }+// Import the GC function.let mut imports = ImportSection::new();imports.import("", "gc", EntityType::Function(0));@@ -184,11 +272,6 @@ impl TableOps {func.instruction(&Instruction::End);func.instruction(&Instruction::End);- // Emit one empty (rec ...) per declared group.- for _ in self.types.groups() {- types.ty().rec(Vec::<wasm_encoder::SubType>::new());- }-let mut code = CodeSection::new();code.function(&func);@@ -230,6 +313,7 @@ impl TableOps {/// Wasm binary.fn fixup(&mut self) {self.limits.fixup();+ self.types.fixup(&self.limits);let mut new_ops = Vec::with_capacity(self.ops.len());let mut stack = 0;@@ -322,32 +406,50 @@ impl Generate<TableOps> for TableOpsMutator {let num_globals = m::range(NUM_GLOBALS_RANGE).generate(ctx)?;let table_size = m::range(TABLE_SIZE_RANGE).generate(ctx)?;- let num_rec_groups = m::range(NUM_REC_GROUPS_RANGE).generate(ctx)?;+ let max_rec_groups = m::range(MAX_REC_GROUPS_RANGE).generate(ctx)?;+ let max_types = m::range(MAX_TYPES_RANGE).generate(ctx)?;let mut ops = TableOps {limits: TableOpsLimits {num_params,num_globals,table_size,- num_rec_groups,+ max_rec_groups,+ max_types,+ },+ ops: {+ let mut v = vec![TableOp::Null(), TableOp::Drop(), TableOp::Gc()];+ if num_params > 0 {+ v.push(TableOp::LocalSet(0));+ v.push(TableOp::LocalGet(0));+ }+ if num_globals > 0 {+ v.push(TableOp::GlobalSet(0));+ v.push(TableOp::GlobalGet(0));+ }+ if max_types > 0 {+ v.push(TableOp::StructNew(0));+ }+ v},- ops: vec![- TableOp::Null(),- TableOp::Drop(),- TableOp::Gc(),- TableOp::LocalSet(0),- TableOp::LocalGet(0),- TableOp::GlobalSet(0),- TableOp::GlobalGet(0),- ],types: Types::new(),};- for i in 0..ops.limits.num_rec_groups {+ for i in 0..ops.limits.max_rec_groups {ops.types.insert_rec_group(RecGroupId(i));}+ if ops.limits.max_rec_groups > 0 {+ for i in 0..ops.limits.max_types {+ let tid = TypeId(i);+ let gid = RecGroupId(m::range(0..=ops.limits.max_rec_groups - 1).generate(ctx)?);++ ops.types.insert_empty_struct(tid, gid);+ }+ }+let mut stack: usize = 0;+while ops.ops.len() < MAX_OPS {let (op, new_stack_len) = TableOp::generate(ctx, &ops, stack)?;ops.ops.push(op);@@ -497,6 +599,8 @@ define_table_ops! {LocalGet(local_index: |ops| ops.num_params => u32) : 0 => 1,LocalSet(local_index: |ops| ops.num_params => u32) : 1 => 0,+ StructNew(type_index: |ops| ops.max_types => u32) : 0 => 0,+Drop : 1 => 0,Null : 0 => 1,@@ -546,6 +650,10 @@ impl TableOp {Self::Null() => {func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::EXTERN));}+ Self::StructNew(x) => {+ func.instruction(&Instruction::StructNew(x + 4));+ func.instruction(&Instruction::Drop);+ }}}}@@ -554,6 +662,8 @@ impl TableOp {mod tests {use super::*;+ use rand::rngs::StdRng;+ use rand::{Rng, SeedableRng};/// Creates empty TableOpsfn empty_test_ops() -> TableOps {let mut t = TableOps {@@ -561,12 +671,13 @@ mod tests {num_params: 5,num_globals: 5,table_size: 5,- num_rec_groups: 5,+ max_rec_groups: 5,+ max_types: 5,},ops: vec![],types: Types::new(),};- for i in 0..t.limits.num_rec_groups {+ for i in 0..t.limits.max_rec_groups {t.types.insert_rec_group(RecGroupId(i));}t@@ -579,7 +690,8 @@ mod tests {num_params,num_globals,table_size,- num_rec_groups: 3,+ max_rec_groups: 7,+ max_types: 10,},ops: vec![TableOp::Null(),@@ -589,21 +701,21 @@ mod tests {TableOp::LocalGet(0),TableOp::GlobalSet(0),TableOp::GlobalGet(0),- TableOp::Null(),- TableOp::Drop(),- TableOp::Gc(),- TableOp::LocalSet(0),- TableOp::LocalGet(0),- TableOp::GlobalSet(0),- TableOp::GlobalGet(0),- TableOp::Null(),- TableOp::Drop(),+ TableOp::StructNew(0),],types: Types::new(),};- for i in 0..t.limits.num_rec_groups {+ for i in 0..t.limits.max_rec_groups {t.types.insert_rec_group(RecGroupId(i));}++ if t.limits.max_rec_groups > 0 {+ let mut rng = StdRng::seed_from_u64(0xC0FFEE);+ for i in 0..t.limits.max_types {+ let gid = RecGroupId(rng.gen_range(0..t.limits.max_rec_groups));+ t.types.insert_empty_struct(TypeId(i), gid);+ }+ }t}@@ -614,7 +726,7 @@ mod tests {let mut session = mutatis::Session::new();- for _ in 0..1024 {+ for _ in 0..5 {session.mutate(&mut res)?;let wasm = res.to_wasm_binary();@@ -626,6 +738,7 @@ mod tests {let wat = wasmprinter::print_bytes(&wasm).expect("[-] Failed .print_bytes(&wasm).");let result = validator.validate_all(&wasm);log::debug!("{wat}");+ println!("{wat}");assert!(result.is_ok(),"\n[-] Invalid wat: {}\n\t\t==== Failed Wat ====\n{}",@@ -675,9 +788,29 @@ mod tests {(type (;1;) (func (param externref externref)))(type (;2;) (func (param externref externref externref)))(type (;3;) (func (result externref externref externref)))+ (rec+ (type (;4;) (struct))+ )(rec)- (rec)- (rec)+ (rec+ (type (;5;) (struct))+ )+ (rec+ (type (;6;) (struct))+ (type (;7;) (struct))+ (type (;8;) (struct))+ )+ (rec+ (type (;9;) (struct))+ (type (;10;) (struct))+ )+ (rec+ (type (;11;) (struct))+ (type (;12;) (struct))+ )+ (rec+ (type (;13;) (struct))+ )(import "" "gc" (func (;0;) (type 0)))(import "" "take_refs" (func (;1;) (type 2)))(import "" "make_refs" (func (;2;) (type 3)))@@ -695,17 +828,7 @@ mod tests {local.get 0global.set 0global.get 0- ref.null extern- drop- call 0- local.set 0- local.get 0- global.set 0- global.get 0- ref.null extern- drop- drop- drop+ struct.new 4dropdropdrop@@ -731,20 +854,7 @@ mod tests {fn emits_empty_rec_groups_and_validates() -> mutatis::Result<()> {let _ = env_logger::try_init();- let mut ops = TableOps {- limits: TableOpsLimits {- num_params: 2,- num_globals: 1,- table_size: 5,- num_rec_groups: 2,- },- ops: vec![TableOp::Null(), TableOp::Drop()],- types: Types::new(),- };-- for i in 0..ops.limits.num_rec_groups {- ops.types.insert_rec_group(RecGroupId(i));- }+ let mut ops = test_ops(5, 5, 5);let wasm = ops.to_wasm_binary();@@ -761,9 +871,8 @@ mod tests {let recs = wat.matches("(rec").count();let structs = wat.matches("(struct)").count();- assert_eq!(recs, 2, "expected 2 (rec) blocks, got {recs}");- // Still keep as zero. Will update in the next PR- assert_eq!(structs, 0, "expected no struct types, got {structs}");+ assert_eq!(recs, 7, "expected 2 (rec) blocks, got {recs}");+ assert_eq!(structs, 10, "expected no struct types, got {structs}");Ok(())}