Skip to content

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

Add tag-related API coverage to the public API fuzzer (#13451)

Nick Fitzgeralddbf1c3765caa
diff --git a/crates/fuzzing/src/generators/api.rs b/crates/fuzzing/src/generators/api.rs
index a115ab1562..ca3d1a177a 100644
--- a/crates/fuzzing/src/generators/api.rs
+++ b/crates/fuzzing/src/generators/api.rs
@@ -90,6 +90,13 @@ struct Swarm {
func_call: bool,
get_func_export: bool,
func_drop: bool,
+ tag_type_new: bool,
+ tag_type_drop: bool,
+ tag_new: bool,
+ tag_ty: bool,
+ tag_eq: bool,
+ get_tag_export: bool,
+ tag_drop: bool,
}
/// A call to one of Wasmtime's public APIs.
@@ -273,6 +280,33 @@ pub enum ApiCall {
FuncDrop {
id: usize,
},
+ TagTypeNew {
+ id: usize,
+ func_ty: usize,
+ },
+ TagTypeDrop {
+ id: usize,
+ },
+ TagNew {
+ id: usize,
+ tag_ty: usize,
+ store: usize,
+ },
+ TagTy {
+ tag: usize,
+ },
+ TagEq {
+ a: usize,
+ b: usize,
+ },
+ GetTagExport {
+ id: usize,
+ instance: usize,
+ nth: usize,
+ },
+ TagDrop {
+ id: usize,
+ },
}
use ApiCall::*;
@@ -317,6 +351,13 @@ struct Scope {
/// associated `store_id`.
funcs: BTreeMap<usize, usize>, // func_id -> store_id
+ /// Tag types that are currently live.
+ tag_types: BTreeSet<usize>,
+
+ /// Tags that are currently live. Maps from `tag_id` to the tag's
+ /// associated `store_id`.
+ tags: BTreeMap<usize, usize>, // tag_id -> store_id
+
config: Config,
}
@@ -356,6 +397,8 @@ impl<'a> Arbitrary<'a> for ApiCalls {
memories: BTreeMap::default(),
func_types: BTreeSet::default(),
funcs: BTreeMap::default(),
+ tag_types: BTreeSet::default(),
+ tags: BTreeMap::default(),
config: config.clone(),
};
@@ -396,6 +439,7 @@ impl<'a> Arbitrary<'a> for ApiCalls {
scope.tables.retain(|_, store_id| *store_id != id);
scope.memories.retain(|_, store_id| *store_id != id);
scope.funcs.retain(|_, store_id| *store_id != id);
+ scope.tags.retain(|_, store_id| *store_id != id);
Ok(StoreDrop { id })
});
}
@@ -815,6 +859,68 @@ impl<'a> Arbitrary<'a> for ApiCalls {
Ok(FuncDrop { id })
});
}
+ if swarm.tag_type_new && !scope.func_types.is_empty() {
+ choices.push(|input, scope| {
+ let types: Vec<_> = scope.func_types.iter().collect();
+ let func_ty = **input.choose(&types)?;
+ let id = scope.next_id();
+ scope.tag_types.insert(id);
+ Ok(TagTypeNew { id, func_ty })
+ });
+ }
+ if swarm.tag_type_drop && !scope.tag_types.is_empty() {
+ choices.push(|input, scope| {
+ let types: Vec<_> = scope.tag_types.iter().collect();
+ let id = **input.choose(&types)?;
+ scope.tag_types.remove(&id);
+ Ok(TagTypeDrop { id })
+ });
+ }
+ if swarm.tag_new && !scope.tag_types.is_empty() && !scope.stores.is_empty() {
+ choices.push(|input, scope| {
+ let types: Vec<_> = scope.tag_types.iter().collect();
+ let tag_ty = **input.choose(&types)?;
+ let stores: Vec<_> = scope.stores.iter().collect();
+ let store = **input.choose(&stores)?;
+ let id = scope.next_id();
+ scope.tags.insert(id, store);
+ Ok(TagNew { id, tag_ty, store })
+ });
+ }
+ if swarm.tag_ty && !scope.tags.is_empty() {
+ choices.push(|input, scope| {
+ let tags: Vec<_> = scope.tags.keys().collect();
+ let tag = **input.choose(&tags)?;
+ Ok(TagTy { tag })
+ });
+ }
+ if swarm.tag_eq && scope.tags.len() >= 2 {
+ choices.push(|input, scope| {
+ let tags: Vec<_> = scope.tags.keys().collect();
+ let a = **input.choose(&tags)?;
+ let b = **input.choose(&tags)?;
+ Ok(TagEq { a, b })
+ });
+ }
+ if swarm.get_tag_export && !scope.instances.is_empty() {
+ choices.push(|input, scope| {
+ let instances: Vec<_> = scope.instances.keys().collect();
+ let instance = **input.choose(&instances)?;
+ let nth = usize::arbitrary(input)?;
+ let id = scope.next_id();
+ let store = *scope.instances.get(&instance).unwrap();
+ scope.tags.insert(id, store);
+ Ok(GetTagExport { id, instance, nth })
+ });
+ }
+ if swarm.tag_drop && !scope.tags.is_empty() {
+ choices.push(|input, scope| {
+ let tags: Vec<_> = scope.tags.keys().collect();
+ let id = **input.choose(&tags)?;
+ scope.tags.remove(&id);
+ Ok(TagDrop { id })
+ });
+ }
if choices.is_empty() {
break;
diff --git a/crates/fuzzing/src/oracles/api.rs b/crates/fuzzing/src/oracles/api.rs
index d04fda0dad..44ffca3b47 100644
--- a/crates/fuzzing/src/oracles/api.rs
+++ b/crates/fuzzing/src/oracles/api.rs
@@ -23,6 +23,8 @@ pub fn make_api_calls(api: ApiCalls) {
let mut memories: HashMap<usize, (Memory, usize)> = Default::default();
let mut func_types: HashMap<usize, FuncType> = Default::default();
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();
for call in api.calls {
match call {
@@ -41,6 +43,7 @@ pub fn make_api_calls(api: ApiCalls) {
tables.retain(|_, (_, store_id)| *store_id != id);
memories.retain(|_, (_, store_id)| *store_id != id);
funcs.retain(|_, (_, store_id)| *store_id != id);
+ tags.retain(|_, (_, store_id)| *store_id != id);
stores.remove(&id);
}
@@ -662,6 +665,97 @@ pub fn make_api_calls(api: ApiCalls) {
log::trace!("dropping func {id}");
funcs.remove(&id);
}
+
+ ApiCall::TagTypeNew { id, func_ty } => {
+ log::trace!("creating tag type {id} from func type {func_ty}");
+ let ft = match func_types.get(&func_ty) {
+ Some(t) => t.clone(),
+ None => continue,
+ };
+ let old = tag_types.insert(id, TagType::new(ft));
+ assert!(old.is_none());
+ }
+
+ ApiCall::TagTypeDrop { id } => {
+ log::trace!("dropping tag type {id}");
+ tag_types.remove(&id);
+ }
+
+ ApiCall::TagNew { id, tag_ty, store } => {
+ log::trace!("creating tag {id} with type {tag_ty} in store {store}");
+ let tt = match tag_types.get(&tag_ty) {
+ Some(t) => t.clone(),
+ None => continue,
+ };
+ let st = match stores.get_mut(&store) {
+ Some(s) => s,
+ None => continue,
+ };
+ match Tag::new(&mut *st, &tt) {
+ Ok(t) => {
+ tags.insert(id, (t, store));
+ }
+ Err(_) => continue,
+ }
+ }
+
+ ApiCall::TagTy { tag } => {
+ log::trace!("checking type of tag {tag}");
+ let (t, store_id) = match tags.get(&tag) {
+ Some(&x) => x,
+ None => continue,
+ };
+ let st = match stores.get(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let _ = t.ty(st);
+ }
+
+ ApiCall::TagEq { a, b } => {
+ log::trace!("comparing tags {a} and {b}");
+ let (ta, store_id) = match tags.get(&a) {
+ Some(&x) => x,
+ None => continue,
+ };
+ let (tb, store_id_b) = match tags.get(&b) {
+ Some(&x) => x,
+ None => continue,
+ };
+ if store_id != store_id_b {
+ continue;
+ }
+ let st = match stores.get(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let _ = Tag::eq(&ta, &tb, st);
+ }
+
+ ApiCall::GetTagExport { id, instance, nth } => {
+ log::trace!("getting {nth}th tag export of instance {instance} as {id}");
+ let (inst, store_id) = match instances.get(&instance) {
+ Some(&x) => x,
+ None => continue,
+ };
+ let st = match stores.get_mut(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let ts = inst
+ .exports(&mut *st)
+ .filter_map(|e| e.into_tag())
+ .collect::<Vec<_>>();
+ if ts.is_empty() {
+ continue;
+ }
+ tags.insert(id, (ts[nth % ts.len()], store_id));
+ }
+
+ ApiCall::TagDrop { id } => {
+ log::trace!("dropping tag {id}");
+ tags.remove(&id);
+ }
}
}
}