Skip to content

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

Add func-related API coverage to the public API fuzzer (#13442)

Nick Fitzgerald883f68fca976
diff --git a/crates/fuzzing/src/generators/api.rs b/crates/fuzzing/src/generators/api.rs
index e84694d774..a115ab1562 100644
--- a/crates/fuzzing/src/generators/api.rs
+++ b/crates/fuzzing/src/generators/api.rs
@@ -83,6 +83,13 @@ struct Swarm {
memory_ty: bool,
memory_drop: bool,
get_memory_export: bool,
+ func_type_new: bool,
+ func_type_drop: bool,
+ func_new: bool,
+ func_ty: bool,
+ func_call: bool,
+ get_func_export: bool,
+ func_drop: bool,
}
/// A call to one of Wasmtime's public APIs.
@@ -239,6 +246,33 @@ pub enum ApiCall {
instance: usize,
nth: usize,
},
+ FuncTypeNew {
+ id: usize,
+ params: Vec<FuzzValType>,
+ results: Vec<FuzzValType>,
+ },
+ FuncTypeDrop {
+ id: usize,
+ },
+ FuncNew {
+ id: usize,
+ func_ty: usize,
+ store: usize,
+ },
+ FuncTy {
+ func: usize,
+ },
+ FuncCall {
+ func: usize,
+ },
+ GetFuncExport {
+ id: usize,
+ instance: usize,
+ nth: usize,
+ },
+ FuncDrop {
+ id: usize,
+ },
}
use ApiCall::*;
@@ -276,6 +310,13 @@ struct Scope {
/// associated `store_id`.
memories: BTreeMap<usize, usize>, // memory_id -> store_id
+ /// Func types that are currently live.
+ func_types: BTreeSet<usize>,
+
+ /// Funcs that are currently live. Maps from `func_id` to the func's
+ /// associated `store_id`.
+ funcs: BTreeMap<usize, usize>, // func_id -> store_id
+
config: Config,
}
@@ -313,6 +354,8 @@ impl<'a> Arbitrary<'a> for ApiCalls {
tables: BTreeMap::default(),
memory_types: BTreeSet::default(),
memories: BTreeMap::default(),
+ func_types: BTreeSet::default(),
+ funcs: BTreeMap::default(),
config: config.clone(),
};
@@ -352,6 +395,7 @@ impl<'a> Arbitrary<'a> for ApiCalls {
scope.globals.retain(|_, store_id| *store_id != id);
scope.tables.retain(|_, store_id| *store_id != id);
scope.memories.retain(|_, store_id| *store_id != id);
+ scope.funcs.retain(|_, store_id| *store_id != id);
Ok(StoreDrop { id })
});
}
@@ -704,6 +748,73 @@ impl<'a> Arbitrary<'a> for ApiCalls {
Ok(GetMemoryExport { id, instance, nth })
});
}
+ if swarm.func_type_new {
+ choices.push(|input, scope| {
+ let id = scope.next_id();
+ let params = Vec::<FuzzValType>::arbitrary(input)?;
+ let params: Vec<_> = params.into_iter().take(4).collect();
+ let results = Vec::<FuzzValType>::arbitrary(input)?;
+ let results: Vec<_> = results.into_iter().take(4).collect();
+ scope.func_types.insert(id);
+ Ok(FuncTypeNew {
+ id,
+ params,
+ results,
+ })
+ });
+ }
+ if swarm.func_type_drop && !scope.func_types.is_empty() {
+ choices.push(|input, scope| {
+ let types: Vec<_> = scope.func_types.iter().collect();
+ let id = **input.choose(&types)?;
+ scope.func_types.remove(&id);
+ Ok(FuncTypeDrop { id })
+ });
+ }
+ if swarm.func_new && !scope.func_types.is_empty() && !scope.stores.is_empty() {
+ choices.push(|input, scope| {
+ let types: Vec<_> = scope.func_types.iter().collect();
+ let func_ty = **input.choose(&types)?;
+ let stores: Vec<_> = scope.stores.iter().collect();
+ let store = **input.choose(&stores)?;
+ let id = scope.next_id();
+ scope.funcs.insert(id, store);
+ Ok(FuncNew { id, func_ty, store })
+ });
+ }
+ if swarm.func_ty && !scope.funcs.is_empty() {
+ choices.push(|input, scope| {
+ let funcs: Vec<_> = scope.funcs.keys().collect();
+ let func = **input.choose(&funcs)?;
+ Ok(FuncTy { func })
+ });
+ }
+ if swarm.func_call && !scope.funcs.is_empty() {
+ choices.push(|input, scope| {
+ let funcs: Vec<_> = scope.funcs.keys().collect();
+ let func = **input.choose(&funcs)?;
+ Ok(FuncCall { func })
+ });
+ }
+ if swarm.get_func_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.funcs.insert(id, store);
+ Ok(GetFuncExport { id, instance, nth })
+ });
+ }
+ if swarm.func_drop && !scope.funcs.is_empty() {
+ choices.push(|input, scope| {
+ let funcs: Vec<_> = scope.funcs.keys().collect();
+ let id = **input.choose(&funcs)?;
+ scope.funcs.remove(&id);
+ Ok(FuncDrop { id })
+ });
+ }
if choices.is_empty() {
break;
diff --git a/crates/fuzzing/src/oracles/api.rs b/crates/fuzzing/src/oracles/api.rs
index 45abd61ff2..d04fda0dad 100644
--- a/crates/fuzzing/src/oracles/api.rs
+++ b/crates/fuzzing/src/oracles/api.rs
@@ -21,6 +21,8 @@ pub fn make_api_calls(api: ApiCalls) {
let mut tables: HashMap<usize, (Table, usize)> = Default::default();
let mut memory_types: HashMap<usize, MemoryType> = Default::default();
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();
for call in api.calls {
match call {
@@ -38,6 +40,7 @@ pub fn make_api_calls(api: ApiCalls) {
globals.retain(|_, (_, store_id)| *store_id != id);
tables.retain(|_, (_, store_id)| *store_id != id);
memories.retain(|_, (_, store_id)| *store_id != id);
+ funcs.retain(|_, (_, store_id)| *store_id != id);
stores.remove(&id);
}
@@ -565,6 +568,100 @@ pub fn make_api_calls(api: ApiCalls) {
}
memories.insert(id, (ms[nth % ms.len()], store_id));
}
+
+ ApiCall::FuncTypeNew {
+ id,
+ ref params,
+ ref results,
+ } => {
+ log::trace!("creating func type {id}");
+ let param_tys = params.iter().map(|p| p.to_wasmtime());
+ let result_tys = results.iter().map(|r| r.to_wasmtime());
+ let ft = FuncType::new(&engine, param_tys, result_tys);
+ let old = func_types.insert(id, ft);
+ assert!(old.is_none());
+ }
+
+ ApiCall::FuncTypeDrop { id } => {
+ log::trace!("dropping func type {id}");
+ func_types.remove(&id);
+ }
+
+ ApiCall::FuncNew { id, func_ty, store } => {
+ log::trace!("creating func {id} with type {func_ty} in store {store}");
+ let ft = match func_types.get(&func_ty) {
+ Some(t) => t.clone(),
+ None => continue,
+ };
+ let st = match stores.get_mut(&store) {
+ Some(s) => s,
+ None => continue,
+ };
+ let f = Func::new(&mut *st, ft, |_caller, _params, _results| Ok(()));
+ funcs.insert(id, (f, store));
+ }
+
+ ApiCall::FuncTy { func } => {
+ log::trace!("checking type of func {func}");
+ let (f, store_id) = match funcs.get(&func) {
+ Some(&x) => x,
+ None => continue,
+ };
+ let st = match stores.get(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let _ = f.ty(&*st);
+ }
+
+ ApiCall::FuncCall { func } => {
+ log::trace!("calling func {func}");
+ let (f, store_id) = match funcs.get(&func) {
+ Some(&x) => x,
+ None => continue,
+ };
+ let st = match stores.get_mut(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let ty = f.ty(&*st);
+ if let Some(params) = ty
+ .params()
+ .map(|p| p.default_value())
+ .collect::<Option<Vec<_>>>()
+ {
+ let mut results = vec![Val::I32(0); ty.results().len()];
+ let _ = f.call(st, &params, &mut results);
+ }
+ }
+
+ ApiCall::GetFuncExport { id, instance, nth } => {
+ log::trace!("getting {nth}th func 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 fs = inst
+ .exports(&mut *st)
+ .filter_map(|e| match e.into_extern() {
+ Extern::Func(f) => Some(f),
+ _ => None,
+ })
+ .collect::<Vec<_>>();
+ if fs.is_empty() {
+ continue;
+ }
+ funcs.insert(id, (fs[nth % fs.len()], store_id));
+ }
+
+ ApiCall::FuncDrop { id } => {
+ log::trace!("dropping func {id}");
+ funcs.remove(&id);
+ }
}
}
}