Skip to content

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

Add global-related API coverage to the public API fuzzer (#13427)

Nick Fitzgeraldb827812664c0
diff --git a/crates/fuzzing/src/generators/api.rs b/crates/fuzzing/src/generators/api.rs
index bd7e073b80..4728d1783a 100644
--- a/crates/fuzzing/src/generators/api.rs
+++ b/crates/fuzzing/src/generators/api.rs
@@ -17,6 +17,29 @@
use crate::generators::Config;
use arbitrary::{Arbitrary, Unstructured};
use std::collections::{BTreeMap, BTreeSet};
+use wasmtime::ValType;
+
+/// The subset of Wasm value types that can be used for host-created globals.
+#[derive(Arbitrary, Debug, Clone, Copy)]
+#[expect(missing_docs, reason = "self-describing variants")]
+pub enum FuzzValType {
+ I32,
+ I64,
+ F32,
+ F64,
+}
+
+impl FuzzValType {
+ /// Convert to the corresponding `wasmtime::ValType`.
+ pub fn to_wasmtime(self) -> ValType {
+ match self {
+ Self::I32 => ValType::I32,
+ Self::I64 => ValType::I64,
+ Self::F32 => ValType::F32,
+ Self::F64 => ValType::F64,
+ }
+ }
+}
#[derive(Arbitrary, Debug)]
struct Swarm {
@@ -27,6 +50,14 @@ struct Swarm {
instance_new: bool,
instance_drop: bool,
call_exported_func: bool,
+ global_type_new: bool,
+ global_type_drop: bool,
+ global_new: bool,
+ global_get: bool,
+ global_set: bool,
+ global_ty: bool,
+ global_drop: bool,
+ get_global_export: bool,
}
/// A call to one of Wasmtime's public APIs.
@@ -59,6 +90,36 @@ pub enum ApiCall {
instance: usize,
nth: usize,
},
+ GlobalTypeNew {
+ id: usize,
+ ty: FuzzValType,
+ mutable: bool,
+ },
+ GlobalTypeDrop {
+ id: usize,
+ },
+ GlobalNew {
+ id: usize,
+ global_ty: usize,
+ store: usize,
+ },
+ GlobalGet {
+ global: usize,
+ },
+ GlobalSet {
+ global: usize,
+ },
+ GlobalTy {
+ global: usize,
+ },
+ GlobalDrop {
+ id: usize,
+ },
+ GetGlobalExport {
+ id: usize,
+ instance: usize,
+ nth: usize,
+ },
}
use ApiCall::*;
@@ -75,6 +136,13 @@ struct Scope {
/// instance's associated `store_id`.
instances: BTreeMap<usize, usize>,
+ /// Global types that are currently live.
+ global_types: BTreeSet<usize>,
+
+ /// Globals that are currently live. Maps from `global_id` to the global's
+ /// associated `store_id`.
+ globals: BTreeMap<usize, usize>, // global_id -> store_id
+
config: Config,
}
@@ -106,6 +174,8 @@ impl<'a> Arbitrary<'a> for ApiCalls {
stores: BTreeSet::default(),
modules: BTreeSet::default(),
instances: BTreeMap::default(),
+ global_types: BTreeSet::default(),
+ globals: BTreeMap::default(),
config: config.clone(),
};
@@ -142,6 +212,7 @@ impl<'a> Arbitrary<'a> for ApiCalls {
let id = **input.choose(&stores)?;
scope.stores.remove(&id);
scope.instances.retain(|_, store_id| *store_id != id);
+ scope.globals.retain(|_, store_id| *store_id != id);
Ok(StoreDrop { id })
});
}
@@ -191,6 +262,78 @@ impl<'a> Arbitrary<'a> for ApiCalls {
Ok(CallExportedFunc { instance, nth })
});
}
+ if swarm.global_type_new {
+ choices.push(|input, scope| {
+ let id = scope.next_id();
+ let ty = FuzzValType::arbitrary(input)?;
+ let mutable = bool::arbitrary(input)?;
+ scope.global_types.insert(id);
+ Ok(GlobalTypeNew { id, ty, mutable })
+ });
+ }
+ if swarm.global_type_drop && !scope.global_types.is_empty() {
+ choices.push(|input, scope| {
+ let types: Vec<_> = scope.global_types.iter().collect();
+ let id = **input.choose(&types)?;
+ scope.global_types.remove(&id);
+ Ok(GlobalTypeDrop { id })
+ });
+ }
+ if swarm.global_new && !scope.global_types.is_empty() && !scope.stores.is_empty() {
+ choices.push(|input, scope| {
+ let types: Vec<_> = scope.global_types.iter().collect();
+ let global_ty = **input.choose(&types)?;
+ let stores: Vec<_> = scope.stores.iter().collect();
+ let store = **input.choose(&stores)?;
+ let id = scope.next_id();
+ scope.globals.insert(id, store);
+ Ok(GlobalNew {
+ id,
+ global_ty,
+ store,
+ })
+ });
+ }
+ if swarm.global_get && !scope.globals.is_empty() {
+ choices.push(|input, scope| {
+ let globals: Vec<_> = scope.globals.keys().collect();
+ let global = **input.choose(&globals)?;
+ Ok(GlobalGet { global })
+ });
+ }
+ if swarm.global_set && !scope.globals.is_empty() {
+ choices.push(|input, scope| {
+ let globals: Vec<_> = scope.globals.keys().collect();
+ let global = **input.choose(&globals)?;
+ Ok(GlobalSet { global })
+ });
+ }
+ if swarm.global_ty && !scope.globals.is_empty() {
+ choices.push(|input, scope| {
+ let globals: Vec<_> = scope.globals.keys().collect();
+ let global = **input.choose(&globals)?;
+ Ok(GlobalTy { global })
+ });
+ }
+ if swarm.global_drop && !scope.globals.is_empty() {
+ choices.push(|input, scope| {
+ let globals: Vec<_> = scope.globals.keys().collect();
+ let id = **input.choose(&globals)?;
+ scope.globals.remove(&id);
+ Ok(GlobalDrop { id })
+ });
+ }
+ if swarm.get_global_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.globals.insert(id, store);
+ Ok(GetGlobalExport { id, instance, nth })
+ });
+ }
if choices.is_empty() {
break;
diff --git a/crates/fuzzing/src/oracles/api.rs b/crates/fuzzing/src/oracles/api.rs
index f99d348271..d9ea1116b7 100644
--- a/crates/fuzzing/src/oracles/api.rs
+++ b/crates/fuzzing/src/oracles/api.rs
@@ -15,6 +15,8 @@ pub fn make_api_calls(api: ApiCalls) {
let mut stores: HashMap<usize, Store<StoreLimits>> = Default::default();
let mut modules: HashMap<usize, Module> = Default::default();
let mut instances: HashMap<usize, (Instance, usize)> = Default::default();
+ let mut global_types: HashMap<usize, GlobalType> = Default::default();
+ let mut globals: HashMap<usize, (Global, usize)> = Default::default();
for call in api.calls {
match call {
@@ -29,6 +31,7 @@ pub fn make_api_calls(api: ApiCalls) {
ApiCall::StoreDrop { id } => {
log::trace!("dropping store {id}");
instances.retain(|_, (_, store_id)| *store_id != id);
+ globals.retain(|_, (_, store_id)| *store_id != id);
stores.remove(&id);
}
@@ -111,6 +114,115 @@ pub fn make_api_calls(api: ApiCalls) {
let _ = f.call(store, &params, &mut results);
}
}
+
+ ApiCall::GlobalTypeNew { id, ty, mutable } => {
+ log::trace!("creating global type {id}");
+ let mutability = if mutable {
+ Mutability::Var
+ } else {
+ Mutability::Const
+ };
+ let old = global_types.insert(id, GlobalType::new(ty.to_wasmtime(), mutability));
+ assert!(old.is_none());
+ }
+
+ ApiCall::GlobalTypeDrop { id } => {
+ log::trace!("dropping global type {id}");
+ global_types.remove(&id);
+ }
+
+ ApiCall::GlobalNew {
+ id,
+ global_ty,
+ store,
+ } => {
+ log::trace!("creating global {id} with type {global_ty} in store {store}");
+ let gt = match global_types.get(&global_ty) {
+ Some(t) => t.clone(),
+ None => continue,
+ };
+ let st = match stores.get_mut(&store) {
+ Some(s) => s,
+ None => continue,
+ };
+ match gt.default_value(&mut *st) {
+ Ok(g) => {
+ globals.insert(id, (g, store));
+ }
+ Err(_) => continue,
+ }
+ }
+
+ ApiCall::GlobalGet { global } => {
+ log::trace!("getting global {global}");
+ let (g, store_id) = match globals.get(&global) {
+ Some(&x) => x,
+ None => continue,
+ };
+ let st = match stores.get_mut(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let _ = g.get(&mut *st);
+ }
+
+ ApiCall::GlobalSet { global } => {
+ log::trace!("setting global {global}");
+ let (g, store_id) = match globals.get(&global) {
+ Some(&x) => x,
+ None => continue,
+ };
+ let st = match stores.get_mut(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ // ty() takes &Store (immutable reborrow); returns owned GlobalType.
+ let gt = g.ty(&*st);
+ if gt.mutability().is_const() {
+ continue;
+ }
+ if let Some(val) = gt.content().default_value() {
+ let _ = g.set(&mut *st, val);
+ }
+ }
+
+ ApiCall::GlobalTy { global } => {
+ log::trace!("checking type of global {global}");
+ let (g, store_id) = match globals.get(&global) {
+ Some(&x) => x,
+ None => continue,
+ };
+ let st = match stores.get(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
+ let _ = g.ty(st);
+ }
+
+ ApiCall::GlobalDrop { id } => {
+ log::trace!("dropping global {id}");
+ globals.remove(&id);
+ }
+
+ ApiCall::GetGlobalExport { id, instance, nth } => {
+ log::trace!("getting {nth}th global 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 gs = inst
+ .exports(&mut *st)
+ .filter_map(|e| e.into_global())
+ .collect::<Vec<_>>();
+ if gs.is_empty() {
+ continue;
+ }
+ globals.insert(id, (gs[nth % gs.len()], store_id));
+ }
}
}
}