A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Add array-related API coverage to the public API fuzzer (#13455)
Nick Fitzgerald4b94a7d89850
diff --git a/crates/fuzzing/src/generators/api.rs b/crates/fuzzing/src/generators/api.rsindex aa19583b2a..6b8baf1acb 100644--- a/crates/fuzzing/src/generators/api.rs+++ b/crates/fuzzing/src/generators/api.rs@@ -114,6 +114,17 @@ struct Swarm {struct_ref_field: bool,struct_ref_set_field: bool,struct_ref_drop: bool,+ array_type_new: bool,+ array_type_drop: bool,+ array_ref_pre_new: bool,+ array_ref_pre_drop: bool,+ array_ref_new: bool,+ array_ref_new_fixed: bool,+ array_ref_ty: bool,+ array_ref_len: bool,+ array_ref_get: bool,+ array_ref_set: bool,+ array_ref_drop: bool,}/// A call to one of Wasmtime's public APIs.@@ -389,6 +400,49 @@ pub enum ApiCall {StructRefDrop {id: usize,},+ ArrayTypeNew {+ id: usize,+ elem_ty: FuzzValType,+ mutable: bool,+ },+ ArrayTypeDrop {+ id: usize,+ },+ ArrayRefPreNew {+ id: usize,+ array_ty: usize,+ store: usize,+ },+ ArrayRefPreDrop {+ id: usize,+ },+ ArrayRefNew {+ id: usize,+ pre: usize,+ len: u32,+ },+ ArrayRefNewFixed {+ id: usize,+ pre: usize,+ count: u8,+ },+ ArrayRefTy {+ array_ref: usize,+ },+ ArrayRefLen {+ array_ref: usize,+ },+ ArrayRefGet {+ array_ref: usize,+ index: u32,+ },+ ArrayRefSet {+ array_ref: usize,+ index: u32,+ },+ ArrayRefDrop {+ id: usize,+ },}use ApiCall::*;@@ -449,6 +503,15 @@ struct Scope {/// StructRefs that are currently live. Maps from `ref_id` to the store's id.struct_refs: BTreeMap<usize, usize>, // ref_id -> store_id+ /// Array types that are currently live.+ array_types: BTreeSet<usize>,++ /// ArrayRefPres that are currently live. Maps from `pre_id` to the store's id.+ array_ref_pres: BTreeMap<usize, usize>, // pre_id -> store_id++ /// ArrayRefs that are currently live. Maps from `ref_id` to the store's id.+ array_refs: BTreeMap<usize, usize>, // ref_id -> store_id+config: Config,}@@ -493,6 +556,9 @@ impl<'a> Arbitrary<'a> for ApiCalls {struct_types: BTreeSet::default(),struct_ref_pres: BTreeMap::default(),struct_refs: BTreeMap::default(),+ array_types: BTreeSet::default(),+ array_ref_pres: BTreeMap::default(),+ array_refs: BTreeMap::default(),config: config.clone(),};@@ -536,6 +602,8 @@ impl<'a> Arbitrary<'a> for ApiCalls {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);+ scope.array_ref_pres.retain(|_, store_id| *store_id != id);+ scope.array_refs.retain(|_, store_id| *store_id != id);Ok(StoreDrop { id })});}@@ -1202,6 +1270,109 @@ impl<'a> Arbitrary<'a> for ApiCalls {Ok(StructRefDrop { id })});}+ if swarm.array_type_new {+ choices.push(|input, scope| {+ let id = scope.next_id();+ let elem_ty = FuzzValType::arbitrary(input)?;+ let mutable = bool::arbitrary(input)?;+ scope.array_types.insert(id);+ Ok(ArrayTypeNew {+ id,+ elem_ty,+ mutable,+ })+ });+ }+ if swarm.array_type_drop && !scope.array_types.is_empty() {+ choices.push(|input, scope| {+ let types: Vec<_> = scope.array_types.iter().collect();+ let id = **input.choose(&types)?;+ scope.array_types.remove(&id);+ Ok(ArrayTypeDrop { id })+ });+ }+ if swarm.array_ref_pre_new && !scope.array_types.is_empty() && !scope.stores.is_empty()+ {+ choices.push(|input, scope| {+ let types: Vec<_> = scope.array_types.iter().collect();+ let array_ty = **input.choose(&types)?;+ let stores: Vec<_> = scope.stores.iter().collect();+ let store = **input.choose(&stores)?;+ let id = scope.next_id();+ scope.array_ref_pres.insert(id, store);+ Ok(ArrayRefPreNew {+ id,+ array_ty,+ store,+ })+ });+ }+ if swarm.array_ref_pre_drop && !scope.array_ref_pres.is_empty() {+ choices.push(|input, scope| {+ let pres: Vec<_> = scope.array_ref_pres.keys().collect();+ let id = **input.choose(&pres)?;+ scope.array_ref_pres.remove(&id);+ Ok(ArrayRefPreDrop { id })+ });+ }+ if swarm.array_ref_new && !scope.array_ref_pres.is_empty() {+ choices.push(|input, scope| {+ let pres: Vec<_> = scope.array_ref_pres.iter().collect();+ let (&pre, &store_id) = *input.choose(&pres)?;+ let id = scope.next_id();+ let len = u32::arbitrary(input)? % 17;+ scope.array_refs.insert(id, store_id);+ Ok(ArrayRefNew { id, pre, len })+ });+ }+ if swarm.array_ref_new_fixed && !scope.array_ref_pres.is_empty() {+ choices.push(|input, scope| {+ let pres: Vec<_> = scope.array_ref_pres.iter().collect();+ let (&pre, &store_id) = *input.choose(&pres)?;+ let id = scope.next_id();+ let count = u8::arbitrary(input)? % 9;+ scope.array_refs.insert(id, store_id);+ Ok(ArrayRefNewFixed { id, pre, count })+ });+ }+ if swarm.array_ref_ty && !scope.array_refs.is_empty() {+ choices.push(|input, scope| {+ let refs: Vec<_> = scope.array_refs.keys().collect();+ let array_ref = **input.choose(&refs)?;+ Ok(ArrayRefTy { array_ref })+ });+ }+ if swarm.array_ref_len && !scope.array_refs.is_empty() {+ choices.push(|input, scope| {+ let refs: Vec<_> = scope.array_refs.keys().collect();+ let array_ref = **input.choose(&refs)?;+ Ok(ArrayRefLen { array_ref })+ });+ }+ if swarm.array_ref_get && !scope.array_refs.is_empty() {+ choices.push(|input, scope| {+ let refs: Vec<_> = scope.array_refs.keys().collect();+ let array_ref = **input.choose(&refs)?;+ let index = u32::arbitrary(input)?;+ Ok(ArrayRefGet { array_ref, index })+ });+ }+ if swarm.array_ref_set && !scope.array_refs.is_empty() {+ choices.push(|input, scope| {+ let refs: Vec<_> = scope.array_refs.keys().collect();+ let array_ref = **input.choose(&refs)?;+ let index = u32::arbitrary(input)?;+ Ok(ArrayRefSet { array_ref, index })+ });+ }+ if swarm.array_ref_drop && !scope.array_refs.is_empty() {+ choices.push(|input, scope| {+ let refs: Vec<_> = scope.array_refs.keys().collect();+ let id = **input.choose(&refs)?;+ scope.array_refs.remove(&id);+ Ok(ArrayRefDrop { id })+ });+ }if choices.is_empty() {break;diff --git a/crates/fuzzing/src/oracles/api.rs b/crates/fuzzing/src/oracles/api.rsindex 13897f29b5..1f4891c790 100644--- a/crates/fuzzing/src/oracles/api.rs+++ b/crates/fuzzing/src/oracles/api.rs@@ -31,6 +31,9 @@ pub fn make_api_calls(api: ApiCalls) {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();+ let mut array_types: HashMap<usize, ArrayType> = Default::default();+ let mut array_ref_pres: HashMap<usize, (ArrayRefPre, ArrayType, usize)> = Default::default();+ let mut array_refs: HashMap<usize, (OwnedRooted<ArrayRef>, usize)> = Default::default();for call in api.calls {match call {@@ -52,6 +55,8 @@ pub fn make_api_calls(api: ApiCalls) {tags.retain(|_, (_, store_id)| *store_id != id);struct_ref_pres.retain(|_, (_, _, store_id)| *store_id != id);struct_refs.retain(|_, (_, store_id)| *store_id != id);+ array_ref_pres.retain(|_, (_, _, store_id)| *store_id != id);+ array_refs.retain(|_, (_, store_id)| *store_id != id);stores.remove(&id);}@@ -1007,6 +1012,172 @@ pub fn make_api_calls(api: ApiCalls) {log::trace!("dropping struct ref {id}");struct_refs.remove(&id);}++ ApiCall::ArrayTypeNew {+ id,+ elem_ty,+ mutable,+ } => {+ log::trace!("creating array type {id}");+ if !gc_enabled {+ continue;+ }+ let mutability = if mutable {+ Mutability::Var+ } else {+ Mutability::Const+ };+ let ft = FieldType::new(mutability, StorageType::ValType(elem_ty.to_wasmtime()));+ let at = ArrayType::new(&engine, ft);+ array_types.insert(id, at);+ }++ ApiCall::ArrayTypeDrop { id } => {+ log::trace!("dropping array type {id}");+ array_types.remove(&id);+ }++ ApiCall::ArrayRefPreNew {+ id,+ array_ty,+ store,+ } => {+ log::trace!("creating array ref pre {id} with type {array_ty} in store {store}");+ let aty = match array_types.get(&array_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 = ArrayRefPre::new(&mut *st, aty.clone());+ array_ref_pres.insert(id, (pre, aty, store));+ }++ ApiCall::ArrayRefPreDrop { id } => {+ log::trace!("dropping array ref pre {id}");+ array_ref_pres.remove(&id);+ }++ ApiCall::ArrayRefNew { id, pre, len } => {+ log::trace!("creating array ref {id} from pre {pre} with len {len}");+ let (allocator, aty, store_id) = match array_ref_pres.get(&pre) {+ Some(x) => x,+ None => continue,+ };+ let store_id = *store_id;+ let len = len % 17;+ let elem_val = match aty.field_type().element_type().unpack().default_value() {+ Some(v) => v,+ None => continue,+ };+ let st = match stores.get_mut(&store_id) {+ Some(s) => s,+ None => continue,+ };+ let mut st = RootScope::new(st);+ match ArrayRef::new(&mut st, allocator, &elem_val, len) {+ Ok(r) => {+ array_refs.insert(id, (r.to_owned_rooted(st).unwrap(), store_id));+ }+ Err(_) => continue,+ }+ }++ ApiCall::ArrayRefNewFixed { id, pre, count } => {+ log::trace!("creating array ref {id} from pre {pre} with {count} elements");+ let (allocator, aty, store_id) = match array_ref_pres.get(&pre) {+ Some(x) => x,+ None => continue,+ };+ let store_id = *store_id;+ let count = count % 9;+ let elem_val = match aty.field_type().element_type().unpack().default_value() {+ Some(v) => v,+ None => continue,+ };+ let elems: Vec<Val> = (0..count).map(|_| elem_val).collect();+ let st = match stores.get_mut(&store_id) {+ Some(s) => s,+ None => continue,+ };+ let mut st = RootScope::new(st);+ match ArrayRef::new_fixed(&mut st, allocator, &elems) {+ Ok(r) => {+ array_refs.insert(id, (r.to_owned_rooted(st).unwrap(), store_id));+ }+ Err(_) => continue,+ }+ }++ ApiCall::ArrayRefTy { array_ref } => {+ log::trace!("getting type of array ref {array_ref}");+ let (r, store_id) = match array_refs.get(&array_ref) {+ Some(x) => x,+ None => continue,+ };+ let st = match stores.get(&store_id) {+ Some(s) => s,+ None => continue,+ };+ let _ = r.ty(st);+ }++ ApiCall::ArrayRefLen { array_ref } => {+ log::trace!("getting length of array ref {array_ref}");+ let (r, store_id) = match array_refs.get(&array_ref) {+ Some(x) => x,+ None => continue,+ };+ let st = match stores.get(&store_id) {+ Some(s) => s,+ None => continue,+ };+ let _ = r.len(st);+ }++ ApiCall::ArrayRefGet { array_ref, index } => {+ log::trace!("getting index {index} of array ref {array_ref}");+ let (r, store_id) = match array_refs.get(&array_ref) {+ Some(x) => x,+ None => continue,+ };+ let st = match stores.get_mut(&store_id) {+ Some(s) => s,+ None => continue,+ };+ let _ = r.get(&mut *st, index);+ }++ ApiCall::ArrayRefSet { array_ref, index } => {+ log::trace!("setting index {index} of array ref {array_ref}");+ let (r, store_id) = match array_refs.get(&array_ref) {+ Some(x) => x,+ None => continue,+ };+ let st = match stores.get_mut(&store_id) {+ Some(s) => s,+ None => continue,+ };+ let aty = match r.ty(&*st) {+ Ok(t) => t,+ Err(_) => continue,+ };+ let val = match aty.field_type().element_type().unpack().default_value() {+ Some(v) => v,+ None => continue,+ };+ let _ = r.set(&mut *st, index, val);+ }++ ApiCall::ArrayRefDrop { id } => {+ log::trace!("dropping array ref {id}");+ array_refs.remove(&id);+ }}}}