Skip to content

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

Move make_api_calls to its own module at crates/fuzzing/src/oracles/api.rs (#13425)

Nick Fitzgerald4596e8ac043e
diff --git a/crates/fuzzing/src/oracles.rs b/crates/fuzzing/src/oracles.rs
index 78f525b234..21e71891a7 100644
--- a/crates/fuzzing/src/oracles.rs
+++ b/crates/fuzzing/src/oracles.rs
@@ -21,6 +21,10 @@ mod gc_access;
pub mod memory;
mod stacks;
+/// Oracle for sequences of Wasmtime API calls.
+pub mod api;
+pub use api::make_api_calls;
+
use self::diff_wasmtime::WasmtimeInstance;
use self::engine::{DiffEngine, DiffInstance};
use crate::generators::ExceptionOps;
@@ -604,101 +608,6 @@ impl<T, U> DiffEqResult<T, U> {
}
}
-/// Invoke the given API calls.
-pub fn make_api_calls(api: generators::api::ApiCalls) {
- use crate::generators::api::ApiCall;
- use std::collections::HashMap;
-
- let mut store: Option<Store<StoreLimits>> = None;
- let mut modules: HashMap<usize, Module> = Default::default();
- let mut instances: HashMap<usize, Instance> = 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::ModuleNew { id, wasm } => {
- log::debug!("creating module: {id}");
- log_wasm(&wasm);
- let module = match Module::new(store.as_ref().unwrap().engine(), &wasm) {
- Ok(m) => m,
- Err(_) => continue,
- };
- let old = modules.insert(id, module);
- assert!(old.is_none());
- }
-
- ApiCall::ModuleDrop { id } => {
- log::trace!("dropping module: {id}");
- drop(modules.remove(&id));
- }
-
- ApiCall::InstanceNew { id, module } => {
- log::trace!("instantiating module {module} as {id}");
- 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);
- }
- }
-
- ApiCall::InstanceDrop { id } => {
- log::trace!("dropping instance {id}");
- instances.remove(&id);
- }
-
- ApiCall::CallExportedFunc { instance, nth } => {
- log::trace!("calling instance export {instance} / {nth}");
- let instance = match instances.get(&instance) {
- Some(i) => i,
- None => {
- // Note that we aren't guaranteed to instantiate valid
- // modules, see comments in `InstanceNew` for details on
- // that. But the API call generator can't know if
- // instantiation failed, so we might not actually have
- // this instance. When that's the case, just skip the
- // API call and keep going.
- continue;
- }
- };
- let store = store.as_mut().unwrap();
-
- let funcs = instance
- .exports(&mut *store)
- .filter_map(|e| match e.into_extern() {
- Extern::Func(f) => Some(f),
- _ => None,
- })
- .collect::<Vec<_>>();
-
- if funcs.is_empty() {
- continue;
- }
-
- let nth = nth % funcs.len();
- let f = &funcs[nth];
- let ty = f.ty(&store);
- 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(store, &params, &mut results);
- }
- }
- }
- }
-}
-
/// Executes the wast `test` with the `config` specified.
///
/// Ensures that wast tests pass regardless of the `Config`.
diff --git a/crates/fuzzing/src/oracles/api.rs b/crates/fuzzing/src/oracles/api.rs
new file mode 100644
index 0000000000..b45c15504e
--- /dev/null
+++ b/crates/fuzzing/src/oracles/api.rs
@@ -0,0 +1,96 @@
+use crate::generators::api::{ApiCall, ApiCalls};
+use crate::oracles::{StoreLimits, instantiate_with_dummy, log_wasm};
+use std::collections::HashMap;
+use wasmtime::*;
+
+/// Invoke the given API calls.
+pub fn make_api_calls(api: ApiCalls) {
+ let mut store: Option<Store<StoreLimits>> = None;
+ let mut modules: HashMap<usize, Module> = Default::default();
+ let mut instances: HashMap<usize, Instance> = 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::ModuleNew { id, wasm } => {
+ log::debug!("creating module: {id}");
+ log_wasm(&wasm);
+ let module = match Module::new(store.as_ref().unwrap().engine(), &wasm) {
+ Ok(m) => m,
+ Err(_) => continue,
+ };
+ let old = modules.insert(id, module);
+ assert!(old.is_none());
+ }
+
+ ApiCall::ModuleDrop { id } => {
+ log::trace!("dropping module: {id}");
+ drop(modules.remove(&id));
+ }
+
+ ApiCall::InstanceNew { id, module } => {
+ log::trace!("instantiating module {module} as {id}");
+ 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);
+ }
+ }
+
+ ApiCall::InstanceDrop { id } => {
+ log::trace!("dropping instance {id}");
+ instances.remove(&id);
+ }
+
+ ApiCall::CallExportedFunc { instance, nth } => {
+ log::trace!("calling instance export {instance} / {nth}");
+ let instance = match instances.get(&instance) {
+ Some(i) => i,
+ None => {
+ // Note that we aren't guaranteed to instantiate valid
+ // modules, see comments in `InstanceNew` for details on
+ // that. But the API call generator can't know if
+ // instantiation failed, so we might not actually have
+ // this instance. When that's the case, just skip the
+ // API call and keep going.
+ continue;
+ }
+ };
+ let store = store.as_mut().unwrap();
+
+ let funcs = instance
+ .exports(&mut *store)
+ .filter_map(|e| match e.into_extern() {
+ Extern::Func(f) => Some(f),
+ _ => None,
+ })
+ .collect::<Vec<_>>();
+
+ if funcs.is_empty() {
+ continue;
+ }
+
+ let nth = nth % funcs.len();
+ let f = &funcs[nth];
+ let ty = f.ty(&store);
+ 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(store, &params, &mut results);
+ }
+ }
+ }
+ }
+}