Skip to content

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

Add StructType/StructRefPre/StructRef coverage to the public API fuzzer (#13454)

Nick Fitzgerald2872c104a5e3
diff --git a/crates/fuzzing/src/generators/api.rs b/crates/fuzzing/src/generators/api.rs
index 1eceea3455..aa19583b2a 100644
--- a/crates/fuzzing/src/generators/api.rs
+++ b/crates/fuzzing/src/generators/api.rs
@@ -105,6 +105,15 @@ struct Swarm {
module_serialize_deserialize: bool,
table_copy: bool,
table_fill: bool,
+ struct_type_new: bool,
+ struct_type_drop: bool,
+ struct_ref_pre_new: bool,
+ struct_ref_pre_drop: bool,
+ struct_ref_new: bool,
+ struct_ref_ty: bool,
+ struct_ref_field: bool,
+ struct_ref_set_field: bool,
+ struct_ref_drop: bool,
}
/// A call to one of Wasmtime's public APIs.
@@ -347,6 +356,39 @@ pub enum ApiCall {
dst: u64,
len: u64,
},
+ StructTypeNew {
+ id: usize,
+ fields: Vec<(FuzzValType, bool)>,
+ },
+ StructTypeDrop {
+ id: usize,
+ },
+ StructRefPreNew {
+ id: usize,
+ struct_ty: usize,
+ store: usize,
+ },
+ StructRefPreDrop {
+ id: usize,
+ },
+ StructRefNew {
+ id: usize,
+ pre: usize,
+ },
+ StructRefTy {
+ struct_ref: usize,
+ },
+ StructRefField {
+ struct_ref: usize,
+ index: usize,
+ },
+ StructRefSetField {
+ struct_ref: usize,
+ index: usize,
+ },
+ StructRefDrop {
+ id: usize,
+ },
}
use ApiCall::*;
@@ -398,6 +440,15 @@ struct Scope {
/// associated `store_id`.
tags: BTreeMap<usize, usize>, // tag_id -> store_id
+ /// Struct types that are currently live.
+ struct_types: BTreeSet<usize>,
+
+ /// StructRefPres that are currently live. Maps from `pre_id` to the store's id.
+ struct_ref_pres: BTreeMap<usize, usize>, // pre_id -> store_id
+
+ /// StructRefs that are currently live. Maps from `ref_id` to the store's id.
+ struct_refs: BTreeMap<usize, usize>, // ref_id -> store_id
+
config: Config,
}
@@ -439,6 +490,9 @@ impl<'a> Arbitrary<'a> for ApiCalls {
funcs: BTreeMap::default(),
tag_types: BTreeSet::default(),
tags: BTreeMap::default(),
+ struct_types: BTreeSet::default(),
+ struct_ref_pres: BTreeMap::default(),
+ struct_refs: BTreeMap::default(),
config: config.clone(),
};
@@ -480,6 +534,8 @@ impl<'a> Arbitrary<'a> for ApiCalls {
scope.memories.retain(|_, store_id| *store_id != id);
scope.funcs.retain(|_, store_id| *store_id != id);
scope.tags.retain(|_, store_id| *store_id != id);
+ scope.struct_ref_pres.retain(|_, store_id| *store_id != id);
+ scope.struct_refs.retain(|_, store_id| *store_id != id);
Ok(StoreDrop { id })
});
}
@@ -1063,6 +1119,89 @@ impl<'a> Arbitrary<'a> for ApiCalls {
Ok(TableFill { table, dst, len })
});
}
+ if swarm.struct_type_new {
+ choices.push(|input, scope| {
+ let id = scope.next_id();
+ let fields = Vec::<(FuzzValType, bool)>::arbitrary(input)?;
+ let fields: Vec<_> = fields.into_iter().take(4).collect();
+ scope.struct_types.insert(id);
+ Ok(StructTypeNew { id, fields })
+ });
+ }
+ if swarm.struct_type_drop && !scope.struct_types.is_empty() {
+ choices.push(|input, scope| {
+ let types: Vec<_> = scope.struct_types.iter().collect();
+ let id = **input.choose(&types)?;
+ scope.struct_types.remove(&id);
+ Ok(StructTypeDrop { id })
+ });
+ }
+ if swarm.struct_ref_pre_new
+ && !scope.struct_types.is_empty()
+ && !scope.stores.is_empty()
+ {
+ choices.push(|input, scope| {
+ let types: Vec<_> = scope.struct_types.iter().collect();
+ let struct_ty = **input.choose(&types)?;
+ let stores: Vec<_> = scope.stores.iter().collect();
+ let store = **input.choose(&stores)?;
+ let id = scope.next_id();
+ scope.struct_ref_pres.insert(id, store);
+ Ok(StructRefPreNew {
+ id,
+ struct_ty,
+ store,
+ })
+ });
+ }
+ if swarm.struct_ref_pre_drop && !scope.struct_ref_pres.is_empty() {
+ choices.push(|input, scope| {
+ let pres: Vec<_> = scope.struct_ref_pres.keys().collect();
+ let id = **input.choose(&pres)?;
+ scope.struct_ref_pres.remove(&id);
+ Ok(StructRefPreDrop { id })
+ });
+ }
+ if swarm.struct_ref_new && !scope.struct_ref_pres.is_empty() {
+ choices.push(|input, scope| {
+ let pres: Vec<_> = scope.struct_ref_pres.iter().collect();
+ let (&pre, &store_id) = *input.choose(&pres)?;
+ let id = scope.next_id();
+ scope.struct_refs.insert(id, store_id);
+ Ok(StructRefNew { id, pre })
+ });
+ }
+ if swarm.struct_ref_ty && !scope.struct_refs.is_empty() {
+ choices.push(|input, scope| {
+ let refs: Vec<_> = scope.struct_refs.keys().collect();
+ let struct_ref = **input.choose(&refs)?;
+ Ok(StructRefTy { struct_ref })
+ });
+ }
+ if swarm.struct_ref_field && !scope.struct_refs.is_empty() {
+ choices.push(|input, scope| {
+ let refs: Vec<_> = scope.struct_refs.keys().collect();
+ let struct_ref = **input.choose(&refs)?;
+ let index = usize::arbitrary(input)?;
+ Ok(StructRefField { struct_ref, index })
+ });
+ }
+ if swarm.struct_ref_set_field && !scope.struct_refs.is_empty() {
+ choices.push(|input, scope| {
+ let refs: Vec<_> = scope.struct_refs.keys().collect();
+ let struct_ref = **input.choose(&refs)?;
+ let index = usize::arbitrary(input)?;
+ Ok(StructRefSetField { struct_ref, index })
+ });
+ }
+ if swarm.struct_ref_drop && !scope.struct_refs.is_empty() {
+ choices.push(|input, scope| {
+ let refs: Vec<_> = scope.struct_refs.keys().collect();
+ let id = **input.choose(&refs)?;
+ scope.struct_refs.remove(&id);
+ Ok(StructRefDrop { id })
+ });
+ }
if choices.is_empty() {
break;
diff --git a/crates/fuzzing/src/oracles/api.rs b/crates/fuzzing/src/oracles/api.rs
index e39210fbb4..13897f29b5 100644
--- a/crates/fuzzing/src/oracles/api.rs
+++ b/crates/fuzzing/src/oracles/api.rs
@@ -28,6 +28,9 @@ pub fn make_api_calls(api: ApiCalls) {
let mut funcs: HashMap<usize, (Func, usize)> = Default::default();
let mut tag_types: HashMap<usize, TagType> = Default::default();
let mut tags: HashMap<usize, (Tag, usize)> = Default::default();
+ let mut struct_types: HashMap<usize, StructType> = Default::default();
+ let mut struct_ref_pres: HashMap<usize, (StructRefPre, StructType, usize)> = Default::default();
+ let mut struct_refs: HashMap<usize, (OwnedRooted<StructRef>, usize)> = Default::default();
for call in api.calls {
match call {
@@ -47,6 +50,8 @@ pub fn make_api_calls(api: ApiCalls) {
memories.retain(|_, (_, store_id)| *store_id != id);
funcs.retain(|_, (_, store_id)| *store_id != id);
tags.retain(|_, (_, store_id)| *store_id != id);
+ struct_ref_pres.retain(|_, (_, _, store_id)| *store_id != id);
+ struct_refs.retain(|_, (_, store_id)| *store_id != id);
stores.remove(&id);
}
@@ -866,6 +871,142 @@ pub fn make_api_calls(api: ApiCalls) {
let _ = t.fill(&mut *st, dst, val.ref_().unwrap(), len);
}
}
+
+ ApiCall::StructTypeNew { id, ref fields } => {
+ log::trace!("creating struct type {id}");
+ if !gc_enabled {
+ continue;
+ }
+ let field_types = fields.iter().map(|(fvt, mutable)| {
+ let mutability = if *mutable {
+ Mutability::Var
+ } else {
+ Mutability::Const
+ };
+ FieldType::new(mutability, StorageType::ValType(fvt.to_wasmtime()))
+ });
+ match StructType::new(&engine, field_types) {
+ Ok(st) => {
+ struct_types.insert(id, st);
+ }
+ Err(_) => continue,
+ }
+ }
+
+ ApiCall::StructTypeDrop { id } => {
+ log::trace!("dropping struct type {id}");
+ struct_types.remove(&id);
+ }
+
+ ApiCall::StructRefPreNew {
+ id,
+ struct_ty,
+ store,
+ } => {
+ log::trace!("creating struct ref pre {id} with type {struct_ty} in store {store}");
+ let sty = match struct_types.get(&struct_ty) {
+ Some(t) => t.clone(),
+ None => continue,
+ };
+ let st = match stores.get_mut(&store) {
+ Some(s) => s,
+ None => continue,
+ };
+ if !gc_enabled {
+ continue;
+ }
+ let pre = StructRefPre::new(&mut *st, sty.clone());
+ struct_ref_pres.insert(id, (pre, sty, store));
+ }
+
+ ApiCall::StructRefPreDrop { id } => {
+ log::trace!("dropping struct ref pre {id}");
+ struct_ref_pres.remove(&id);
+ }
+
+ ApiCall::StructRefNew { id, pre } => {
+ log::trace!("creating struct ref {id} from pre {pre}");
+ let (allocator, sty, store_id) = match struct_ref_pres.get(&pre) {
+ Some(x) => x,
+ None => continue,
+ };
+ let store_id = *store_id;
+ let fields: Option<Vec<Val>> = sty
+ .fields()
+ .map(|f| f.element_type().unpack().default_value())
+ .collect();
+ let fields = match fields {
+ Some(f) => f,
+ None => continue,
+ };
+ let st = match stores.get_mut(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let mut st = RootScope::new(st);
+ match StructRef::new(&mut st, allocator, &fields) {
+ Ok(r) => {
+ struct_refs.insert(id, (r.to_owned_rooted(st).unwrap(), store_id));
+ }
+ Err(_) => continue,
+ }
+ }
+
+ ApiCall::StructRefTy { struct_ref } => {
+ log::trace!("getting type of struct ref {struct_ref}");
+ let (r, store_id) = match struct_refs.get(&struct_ref) {
+ Some(x) => x,
+ None => continue,
+ };
+ let st = match stores.get(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let _ = r.ty(st);
+ }
+
+ ApiCall::StructRefField { struct_ref, index } => {
+ log::trace!("getting field {index} of struct ref {struct_ref}");
+ let (r, store_id) = match struct_refs.get(&struct_ref) {
+ Some(x) => x,
+ None => continue,
+ };
+ let st = match stores.get_mut(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let _ = r.field(&mut *st, index);
+ }
+
+ ApiCall::StructRefSetField { struct_ref, index } => {
+ log::trace!("setting field {index} of struct ref {struct_ref}");
+ let (r, store_id) = match struct_refs.get(&struct_ref) {
+ Some(x) => x,
+ None => continue,
+ };
+ let st = match stores.get_mut(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let sty = match r.ty(&*st) {
+ Ok(t) => t,
+ Err(_) => continue,
+ };
+ let field_tys: Vec<_> = sty.fields().collect();
+ if index >= field_tys.len() {
+ continue;
+ }
+ let val = match field_tys[index].element_type().unpack().default_value() {
+ Some(v) => v,
+ None => continue,
+ };
+ let _ = r.set_field(&mut *st, index, val);
+ }
+
+ ApiCall::StructRefDrop { id } => {
+ log::trace!("dropping struct ref {id}");
+ struct_refs.remove(&id);
+ }
}
}
}