Skip to content

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

Add support for creating and dropping multiple stores (#13426)

Nick Fitzgeraldd57951685173
diff --git a/crates/fuzzing/src/generators/api.rs b/crates/fuzzing/src/generators/api.rs
index a52e04c13e..bd7e073b80 100644
--- a/crates/fuzzing/src/generators/api.rs
+++ b/crates/fuzzing/src/generators/api.rs
@@ -16,10 +16,12 @@
use crate::generators::Config;
use arbitrary::{Arbitrary, Unstructured};
-use std::collections::BTreeSet;
+use std::collections::{BTreeMap, BTreeSet};
#[derive(Arbitrary, Debug)]
struct Swarm {
+ store_new: bool,
+ store_drop: bool,
module_new: bool,
module_drop: bool,
instance_new: bool,
@@ -31,19 +33,48 @@ struct Swarm {
#[derive(Arbitrary, Debug)]
#[expect(missing_docs, reason = "self-describing fields")]
pub enum ApiCall {
- StoreNew(Config),
- ModuleNew { id: usize, wasm: Vec<u8> },
- ModuleDrop { id: usize },
- InstanceNew { id: usize, module: usize },
- InstanceDrop { id: usize },
- CallExportedFunc { instance: usize, nth: usize },
+ StoreNew {
+ id: usize,
+ config: Config,
+ },
+ StoreDrop {
+ id: usize,
+ },
+ ModuleNew {
+ id: usize,
+ wasm: Vec<u8>,
+ },
+ ModuleDrop {
+ id: usize,
+ },
+ InstanceNew {
+ id: usize,
+ module: usize,
+ store: usize,
+ },
+ InstanceDrop {
+ id: usize,
+ },
+ CallExportedFunc {
+ instance: usize,
+ nth: usize,
+ },
}
use ApiCall::*;
struct Scope {
id_counter: usize,
+
+ /// Stores that are currently live.
+ stores: BTreeSet<usize>,
+
+ /// Modules that are currently live.
modules: BTreeSet<usize>,
- instances: BTreeSet<usize>,
+
+ /// Instances that are currently live. Maps from `instance_id` to the
+ /// instance's associated `store_id`.
+ instances: BTreeMap<usize, usize>,
+
config: Config,
}
@@ -70,26 +101,50 @@ impl<'a> Arbitrary<'a> for ApiCalls {
let mut calls = vec![];
let config = Config::arbitrary(input)?;
- calls.push(StoreNew(config.clone()));
-
let mut scope = Scope {
id_counter: 0,
+ stores: BTreeSet::default(),
modules: BTreeSet::default(),
- instances: BTreeSet::default(),
- config,
+ instances: BTreeMap::default(),
+ config: config.clone(),
};
+ let store_id = scope.next_id();
+ scope.stores.insert(store_id);
+ calls.push(StoreNew {
+ id: store_id,
+ config,
+ });
+
// Total limit on number of API calls we'll generate. This exists to
// avoid libFuzzer timeouts.
let max_calls = 100;
- for _ in 0..input.arbitrary_len::<ApiCall>()? {
- if calls.len() > max_calls {
- break;
- }
+ let mut choices: Vec<fn(&mut Unstructured<'a>, &mut Scope) -> arbitrary::Result<ApiCall>> =
+ vec![];
- let mut choices: Vec<fn(_, &mut Scope) -> arbitrary::Result<ApiCall>> = vec![];
+ for _ in 0..std::cmp::min(max_calls, input.arbitrary_len::<ApiCall>()?) {
+ choices.clear();
+ if swarm.store_new {
+ choices.push(|_input, scope| {
+ let id = scope.next_id();
+ scope.stores.insert(id);
+ Ok(StoreNew {
+ id,
+ config: scope.config.clone(),
+ })
+ });
+ }
+ if swarm.store_drop && scope.stores.len() > 1 {
+ choices.push(|input, scope| {
+ let stores: Vec<_> = scope.stores.iter().collect();
+ let id = **input.choose(&stores)?;
+ scope.stores.remove(&id);
+ scope.instances.retain(|_, store_id| *store_id != id);
+ Ok(StoreDrop { id })
+ });
+ }
if swarm.module_new {
choices.push(|input, scope| {
let id = scope.next_id();
@@ -109,18 +164,20 @@ impl<'a> Arbitrary<'a> for ApiCalls {
Ok(ModuleDrop { id })
});
}
- if swarm.instance_new && !scope.modules.is_empty() {
+ if swarm.instance_new && !scope.modules.is_empty() && !scope.stores.is_empty() {
choices.push(|input, scope| {
let modules: Vec<_> = scope.modules.iter().collect();
let module = **input.choose(&modules)?;
+ let stores: Vec<_> = scope.stores.iter().collect();
+ let store = **input.choose(&stores)?;
let id = scope.next_id();
- scope.instances.insert(id);
- Ok(InstanceNew { id, module })
+ scope.instances.insert(id, store);
+ Ok(InstanceNew { id, module, store })
});
}
if swarm.instance_drop && !scope.instances.is_empty() {
choices.push(|input, scope| {
- let instances: Vec<_> = scope.instances.iter().collect();
+ let instances: Vec<_> = scope.instances.keys().collect();
let id = **input.choose(&instances)?;
scope.instances.remove(&id);
Ok(InstanceDrop { id })
@@ -128,7 +185,7 @@ impl<'a> Arbitrary<'a> for ApiCalls {
}
if swarm.call_exported_func && !scope.instances.is_empty() {
choices.push(|input, scope| {
- let instances: Vec<_> = scope.instances.iter().collect();
+ let instances: Vec<_> = scope.instances.keys().collect();
let instance = **input.choose(&instances)?;
let nth = usize::arbitrary(input)?;
Ok(CallExportedFunc { instance, nth })
diff --git a/crates/fuzzing/src/oracles/api.rs b/crates/fuzzing/src/oracles/api.rs
index b45c15504e..f99d348271 100644
--- a/crates/fuzzing/src/oracles/api.rs
+++ b/crates/fuzzing/src/oracles/api.rs
@@ -5,22 +5,37 @@ use wasmtime::*;
/// Invoke the given API calls.
pub fn make_api_calls(api: ApiCalls) {
- let mut store: Option<Store<StoreLimits>> = None;
+ // The generator always starts with StoreNew; use its config to build the
+ // shared engine that all stores in this sequence will use.
+ let engine = match api.calls.first() {
+ Some(ApiCall::StoreNew { config, .. }) => Engine::new(&config.to_wasmtime()).unwrap(),
+ _ => return,
+ };
+
+ let mut stores: HashMap<usize, Store<StoreLimits>> = Default::default();
let mut modules: HashMap<usize, Module> = Default::default();
- let mut instances: HashMap<usize, Instance> = Default::default();
+ let mut instances: HashMap<usize, (Instance, usize)> = Default::default();
for call in api.calls {
match call {
- ApiCall::StoreNew(config) => {
- log::trace!("creating store");
- assert!(store.is_none());
- store = Some(config.to_store());
+ ApiCall::StoreNew { id, config } => {
+ log::trace!("creating store {id}");
+ let mut store = Store::new(&engine, StoreLimits::new());
+ config.configure_store(&mut store);
+ let old = stores.insert(id, store);
+ assert!(old.is_none());
+ }
+
+ ApiCall::StoreDrop { id } => {
+ log::trace!("dropping store {id}");
+ instances.retain(|_, (_, store_id)| *store_id != id);
+ stores.remove(&id);
}
ApiCall::ModuleNew { id, wasm } => {
log::debug!("creating module: {id}");
log_wasm(&wasm);
- let module = match Module::new(store.as_ref().unwrap().engine(), &wasm) {
+ let module = match Module::new(&engine, &wasm) {
Ok(m) => m,
Err(_) => continue,
};
@@ -33,16 +48,18 @@ pub fn make_api_calls(api: ApiCalls) {
drop(modules.remove(&id));
}
- ApiCall::InstanceNew { id, module } => {
- log::trace!("instantiating module {module} as {id}");
+ ApiCall::InstanceNew { id, module, store } => {
+ log::trace!("instantiating module {module} as {id} in store {store}");
let module = match modules.get(&module) {
Some(m) => m,
None => continue,
};
-
- let store = store.as_mut().unwrap();
- if let Some(instance) = instantiate_with_dummy(store, module) {
- instances.insert(id, instance);
+ let st = match stores.get_mut(&store) {
+ Some(s) => s,
+ None => continue,
+ };
+ if let Some(instance) = instantiate_with_dummy(st, module) {
+ instances.insert(id, (instance, store));
}
}
@@ -53,8 +70,8 @@ pub fn make_api_calls(api: ApiCalls) {
ApiCall::CallExportedFunc { instance, nth } => {
log::trace!("calling instance export {instance} / {nth}");
- let instance = match instances.get(&instance) {
- Some(i) => i,
+ let (inst, store_id) = match instances.get(&instance) {
+ Some(&x) => x,
None => {
// Note that we aren't guaranteed to instantiate valid
// modules, see comments in `InstanceNew` for details on
@@ -65,9 +82,12 @@ pub fn make_api_calls(api: ApiCalls) {
continue;
}
};
- let store = store.as_mut().unwrap();
+ let store = match stores.get_mut(&store_id) {
+ Some(s) => s,
+ None => continue,
+ };
- let funcs = instance
+ let funcs = inst
.exports(&mut *store)
.filter_map(|e| match e.into_extern() {
Extern::Func(f) => Some(f),
@@ -81,7 +101,7 @@ pub fn make_api_calls(api: ApiCalls) {
let nth = nth % funcs.len();
let f = &funcs[nth];
- let ty = f.ty(&store);
+ let ty = f.ty(&*store);
if let Some(params) = ty
.params()
.map(|p| p.default_value())