Skip to content

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

Migrate fuzzing to `wasmtime::error` (#12263)

Nick Fitzgerald93d22fcdfaab
diff --git a/Cargo.lock b/Cargo.lock
index 4149b9566f..5e0c9972d2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4793,7 +4793,6 @@ dependencies = [
name = "wasmtime-fuzzing"
version = "0.0.0"
dependencies = [
- "anyhow",
"arbitrary",
"backtrace",
"env_logger 0.11.5",
diff --git a/crates/fuzzing/Cargo.toml b/crates/fuzzing/Cargo.toml
index 913d0a0980..9b929297d6 100644
--- a/crates/fuzzing/Cargo.toml
+++ b/crates/fuzzing/Cargo.toml
@@ -15,7 +15,6 @@ workspace = true
wasmtime-test-util = { workspace = true, features = ['wast'] }
[dependencies]
-anyhow = { workspace = true }
backtrace = { workspace = true }
arbitrary = { workspace = true, features = ["derive"] }
env_logger = { workspace = true }
diff --git a/crates/fuzzing/src/generators/config.rs b/crates/fuzzing/src/generators/config.rs
index 3a948117ea..d4782a77c5 100644
--- a/crates/fuzzing/src/generators/config.rs
+++ b/crates/fuzzing/src/generators/config.rs
@@ -2,9 +2,9 @@
use super::{AsyncConfig, CodegenSettings, InstanceAllocationStrategy, MemoryConfig, ModuleConfig};
use crate::oracles::{StoreLimits, Timeout};
-use anyhow::Result;
use arbitrary::{Arbitrary, Unstructured};
use std::time::Duration;
+use wasmtime::Result;
use wasmtime::{Enabled, Engine, Module, Store};
use wasmtime_test_util::wast::{WastConfig, WastTest, limits};
diff --git a/crates/fuzzing/src/lib.rs b/crates/fuzzing/src/lib.rs
index bb538d8f2d..dcc69d1ab8 100644
--- a/crates/fuzzing/src/lib.rs
+++ b/crates/fuzzing/src/lib.rs
@@ -90,7 +90,7 @@ mod test {
use rand::prelude::*;
pub fn gen_until_pass<T: for<'a> Arbitrary<'a>>(
- mut f: impl FnMut(T, &mut Unstructured<'_>) -> anyhow::Result<bool>,
+ mut f: impl FnMut(T, &mut Unstructured<'_>) -> wasmtime::Result<bool>,
) -> bool {
let mut rng = SmallRng::seed_from_u64(0);
let mut buf = vec![0; 2048];
diff --git a/crates/fuzzing/src/oracles.rs b/crates/fuzzing/src/oracles.rs
index 255c4e9325..4ad56c80cb 100644
--- a/crates/fuzzing/src/oracles.rs
+++ b/crates/fuzzing/src/oracles.rs
@@ -321,7 +321,7 @@ fn compile_module(
) -> Option<Module> {
log_wasm(bytes);
- fn is_pcc_error(e: &anyhow::Error) -> bool {
+ fn is_pcc_error(e: &wasmtime::Error) -> bool {
// NOTE: please keep this predicate in sync with the display format of CodegenError,
// defined in `wasmtime/cranelift/codegen/src/result.rs`
e.to_string().to_lowercase().contains("proof-carrying-code")
@@ -400,7 +400,7 @@ pub fn instantiate_with_dummy(store: &mut Store<StoreLimits>, module: &Module) -
fn unwrap_instance(
store: &Store<StoreLimits>,
- instance: anyhow::Result<Instance>,
+ instance: wasmtime::Result<Instance>,
) -> Option<Instance> {
let e = match instance {
Ok(i) => return Some(i),
@@ -460,7 +460,7 @@ pub fn differential(
name: &str,
args: &[DiffValue],
result_tys: &[DiffValueType],
-) -> anyhow::Result<bool> {
+) -> wasmtime::Result<bool> {
log::debug!("Evaluating: `{name}` with {args:?}");
let lhs_results = match lhs.evaluate(name, args, result_tys) {
Ok(Some(results)) => Ok(results),
@@ -1324,7 +1324,7 @@ mod tests {
let ok =
Validator::new_with_features(WasmFeatures::all() ^ feature).validate_all(&wasm);
if ok.is_err() {
- anyhow::bail!("generated a module with {feature:?} but that wasn't expected");
+ wasmtime::bail!("generated a module with {feature:?} but that wasn't expected");
}
}
diff --git a/crates/fuzzing/src/oracles/component_api.rs b/crates/fuzzing/src/oracles/component_api.rs
index 27be4a1759..47f0faae57 100644
--- a/crates/fuzzing/src/oracles/component_api.rs
+++ b/crates/fuzzing/src/oracles/component_api.rs
@@ -226,7 +226,7 @@ where
fn host_function<P, R>(
cx: StoreContextMut<'_, Box<dyn Any + Send>>,
params: P,
- ) -> anyhow::Result<R>
+ ) -> wasmtime::Result<R>
where
P: Debug + PartialEq + 'static,
R: Debug + Clone + 'static,
diff --git a/crates/fuzzing/src/oracles/diff_spec.rs b/crates/fuzzing/src/oracles/diff_spec.rs
index 21987ddad7..7c0d546c32 100644
--- a/crates/fuzzing/src/oracles/diff_spec.rs
+++ b/crates/fuzzing/src/oracles/diff_spec.rs
@@ -3,9 +3,8 @@
use crate::generators::{Config, DiffValue, DiffValueType};
use crate::oracles::engine::{DiffEngine, DiffInstance};
-use anyhow::{Error, Result, anyhow};
use wasm_spec_interpreter::SpecValue;
-use wasmtime::Trap;
+use wasmtime::{Error, Result, Trap, format_err};
/// A wrapper for `wasm-spec-interpreter` as a [`DiffEngine`].
pub struct SpecInterpreter;
@@ -41,7 +40,7 @@ impl DiffEngine for SpecInterpreter {
fn instantiate(&mut self, wasm: &[u8]) -> Result<Box<dyn DiffInstance>> {
let instance = wasm_spec_interpreter::instantiate(wasm)
- .map_err(|e| anyhow!("failed to instantiate in spec interpreter: {e}"))?;
+ .map_err(|e| format_err!("failed to instantiate in spec interpreter: {e}"))?;
Ok(Box::new(SpecInstance { instance }))
}
@@ -73,7 +72,7 @@ impl DiffInstance for SpecInstance {
let arguments = arguments.iter().map(SpecValue::from).collect();
match wasm_spec_interpreter::interpret(&self.instance, function_name, Some(arguments)) {
Ok(results) => Ok(Some(results.into_iter().map(SpecValue::into).collect())),
- Err(err) => Err(anyhow!(err)),
+ Err(err) => Err(format_err!(err)),
}
}
diff --git a/crates/fuzzing/src/oracles/diff_v8.rs b/crates/fuzzing/src/oracles/diff_v8.rs
index eaf612a4be..d6c2f4575c 100644
--- a/crates/fuzzing/src/oracles/diff_v8.rs
+++ b/crates/fuzzing/src/oracles/diff_v8.rs
@@ -1,10 +1,9 @@
use crate::generators::{Config, DiffValue, DiffValueType};
use crate::oracles::engine::{DiffEngine, DiffInstance};
-use anyhow::{Error, Result, bail};
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Once;
-use wasmtime::Trap;
+use wasmtime::{Error, Result, Trap, bail};
pub struct V8Engine {
isolate: Rc<RefCell<v8::OwnedIsolate>>,
diff --git a/crates/fuzzing/src/oracles/diff_wasmi.rs b/crates/fuzzing/src/oracles/diff_wasmi.rs
index fed0330419..8e01da029d 100644
--- a/crates/fuzzing/src/oracles/diff_wasmi.rs
+++ b/crates/fuzzing/src/oracles/diff_wasmi.rs
@@ -2,8 +2,7 @@
use crate::generators::{Config, DiffValue, DiffValueType};
use crate::oracles::engine::{DiffEngine, DiffInstance};
-use anyhow::{Context, Error, Result};
-use wasmtime::Trap;
+use wasmtime::{Error, Result, Trap, error::Context as _};
/// A wrapper for `wasmi` as a [`DiffEngine`].
pub struct WasmiEngine {
diff --git a/crates/fuzzing/src/oracles/diff_wasmtime.rs b/crates/fuzzing/src/oracles/diff_wasmtime.rs
index f600005164..0a1cc04d3f 100644
--- a/crates/fuzzing/src/oracles/diff_wasmtime.rs
+++ b/crates/fuzzing/src/oracles/diff_wasmtime.rs
@@ -5,9 +5,10 @@ use crate::oracles::dummy;
use crate::oracles::engine::DiffInstance;
use crate::oracles::{StoreLimits, compile_module, engine::DiffEngine};
use crate::single_module_fuzzer::KnownValid;
-use anyhow::{Context, Error, Result};
use arbitrary::Unstructured;
-use wasmtime::{Extern, FuncType, Instance, Module, Store, Trap, Val};
+use wasmtime::{
+ Error, Extern, FuncType, Instance, Module, Result, Store, Trap, Val, error::Context as _,
+};
/// A wrapper for using Wasmtime as a [`DiffEngine`].
pub struct WasmtimeEngine {
diff --git a/crates/fuzzing/src/oracles/dummy.rs b/crates/fuzzing/src/oracles/dummy.rs
index 16a66e7834..248cfbb524 100644
--- a/crates/fuzzing/src/oracles/dummy.rs
+++ b/crates/fuzzing/src/oracles/dummy.rs
@@ -1,7 +1,6 @@
//! Dummy implementations of things that a Wasm module can import.
-use anyhow::Context;
-use wasmtime::*;
+use wasmtime::{error::Context as _, *};
/// Create a set of dummy functions/globals/etc for the given imports.
pub fn dummy_linker<T>(store: &mut Store<T>, module: &Module) -> Result<Linker<T>> {
diff --git a/crates/fuzzing/src/oracles/engine.rs b/crates/fuzzing/src/oracles/engine.rs
index 0da6c0beb2..57857a6ec6 100644
--- a/crates/fuzzing/src/oracles/engine.rs
+++ b/crates/fuzzing/src/oracles/engine.rs
@@ -2,8 +2,8 @@
use crate::generators::{CompilerStrategy, Config, DiffValue, DiffValueType};
use crate::oracles::{diff_wasmi::WasmiEngine, diff_wasmtime::WasmtimeEngine};
-use anyhow::Error;
use arbitrary::Unstructured;
+use wasmtime::Error;
use wasmtime::Trap;
/// Returns a function which can be used to build the engine name specified.
@@ -55,7 +55,7 @@ pub trait DiffEngine {
fn name(&self) -> &'static str;
/// Create a new instance with the given engine.
- fn instantiate(&mut self, wasm: &[u8]) -> anyhow::Result<Box<dyn DiffInstance>>;
+ fn instantiate(&mut self, wasm: &[u8]) -> wasmtime::Result<Box<dyn DiffInstance>>;
/// Tests that the wasmtime-originating `trap` matches the error this engine
/// generated.
@@ -85,7 +85,7 @@ pub trait DiffInstance {
function_name: &str,
arguments: &[DiffValue],
results: &[DiffValueType],
- ) -> anyhow::Result<Option<Vec<DiffValue>>>;
+ ) -> wasmtime::Result<Option<Vec<DiffValue>>>;
/// Attempts to return the value of the specified global, returning `None`
/// if this engine doesn't support retrieving globals at this time.
diff --git a/crates/fuzzing/src/oracles/stacks.rs b/crates/fuzzing/src/oracles/stacks.rs
index 7a9ec51e1a..fd5fa1b644 100644
--- a/crates/fuzzing/src/oracles/stacks.rs
+++ b/crates/fuzzing/src/oracles/stacks.rs
@@ -1,5 +1,5 @@
use crate::generators::Stacks;
-use anyhow::bail;
+use wasmtime::bail;
use wasmtime::*;
/// Run the given `Stacks` test case and assert that the host's view of the Wasm
diff --git a/crates/fuzzing/src/single_module_fuzzer.rs b/crates/fuzzing/src/single_module_fuzzer.rs
index ff499b1096..392ef2c8c2 100644
--- a/crates/fuzzing/src/single_module_fuzzer.rs
+++ b/crates/fuzzing/src/single_module_fuzzer.rs
@@ -239,7 +239,7 @@ struct FuzzInput<'a> {
/// and the module ends with a custom section indicating it's a fuzz input
/// then the contents of the custom section are returned along with the
/// contents of the original module.
-fn extract_fuzz_input(data: &[u8]) -> anyhow::Result<FuzzInput<'_>> {
+fn extract_fuzz_input(data: &[u8]) -> wasmtime::Result<FuzzInput<'_>> {
use wasmparser::{Parser, Payload};
let mut prev_end = 8;
for section in Parser::new(0).parse_all(data) {
@@ -267,7 +267,7 @@ fn extract_fuzz_input(data: &[u8]) -> anyhow::Result<FuzzInput<'_>> {
prev_end = range.end;
}
}
- anyhow::bail!("no input found")
+ wasmtime::bail!("no input found")
}
#[cfg(test)]