Skip to content

Commit 5279f5c

Browse files
authored
Don't discard errors in default_value helpers (#10584)
This commit updates the `default_value` helpers added recently to return a `Result` instead of returning an `Option<T>` and throwing away error information. This fixes a fuzz bug showing up recently which happened because the error in question was one we've flagged to ignore, but because the error was discarded we didn't know to ignore it so it ended up causing a fuzz failure.
1 parent 6ba842d commit 5279f5c

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

crates/fuzzing/src/oracles.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,9 +1141,9 @@ pub fn call_async(wasm: &[u8], config: &generators::Config, mut poll_amts: &[u32
11411141
.into()
11421142
}
11431143
other_ty => match other_ty.default_value(&mut store) {
1144-
Some(item) => item,
1145-
None => {
1146-
log::warn!("couldn't create import for {import:?}");
1144+
Ok(item) => item,
1145+
Err(e) => {
1146+
log::warn!("couldn't create import for {import:?}: {e:?}");
11471147
return;
11481148
}
11491149
},

crates/fuzzing/src/oracles/dummy.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
//! Dummy implementations of things that a Wasm module can import.
22
3+
use anyhow::Context;
34
use wasmtime::*;
45

56
/// Create a set of dummy functions/globals/etc for the given imports.
67
pub fn dummy_linker<T>(store: &mut Store<T>, module: &Module) -> Result<Linker<T>> {
78
let mut linker = Linker::new(store.engine());
89
linker.allow_shadowing(true);
910
for import in module.imports() {
10-
let extern_ = import
11-
.ty()
12-
.default_value(&mut *store)
13-
.ok_or(anyhow::anyhow!("ERROR"))?;
11+
let extern_ = import.ty().default_value(&mut *store).with_context(|| {
12+
format!(
13+
"failed to create dummy value of `{}::{}` - {:?}",
14+
import.module(),
15+
import.name(),
16+
import.ty(),
17+
)
18+
})?;
1419
linker
1520
.define(&store, import.module(), import.name(), extern_)
1621
.unwrap();

crates/wasmtime/src/runtime/linker.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,17 @@ impl<T> Linker<T> {
282282
for import in module.imports() {
283283
if let Err(import_err) = self._get_by_import(&import) {
284284
let default_extern =
285-
import_err.ty().default_value(&mut *store).ok_or_else(|| {
286-
anyhow!("no default value exists for type `{:?}`", import_err.ty())
287-
})?;
285+
import_err
286+
.ty()
287+
.default_value(&mut *store)
288+
.with_context(|| {
289+
anyhow!(
290+
"no default value exists for `{}::{}` with type `{:?}`",
291+
import.module(),
292+
import.name(),
293+
import_err.ty(),
294+
)
295+
})?;
288296
self.define(
289297
store.as_context(),
290298
import.module(),

crates/wasmtime/src/runtime/types.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,13 +1249,13 @@ impl ExternType {
12491249
}
12501250
}
12511251
/// Construct a default value, if possible for the underlying type. Tags do not have a default value.
1252-
pub fn default_value(&self, store: impl AsContextMut) -> Option<Extern> {
1252+
pub fn default_value(&self, store: impl AsContextMut) -> Result<Extern> {
12531253
match self {
12541254
ExternType::Func(func_ty) => func_ty.default_value(store).map(Extern::Func),
12551255
ExternType::Global(global_ty) => global_ty.default_value(store).map(Extern::Global),
12561256
ExternType::Table(table_ty) => table_ty.default_value(store).map(Extern::Table),
12571257
ExternType::Memory(mem_ty) => mem_ty.default_value(store).map(Extern::Memory),
1258-
ExternType::Tag(_) => None, // FIXME: #10252
1258+
ExternType::Tag(_) => bail!("default tags not supported yet"), // FIXME: #10252
12591259
}
12601260
}
12611261
}
@@ -2434,12 +2434,13 @@ impl FuncType {
24342434
Self { registered_type }
24352435
}
24362436
/// Construct a func which returns results of default value, if each result type has a default value.
2437-
pub fn default_value(&self, mut store: impl AsContextMut) -> Option<Func> {
2437+
pub fn default_value(&self, mut store: impl AsContextMut) -> Result<Func> {
24382438
let dummy_results = self
24392439
.results()
24402440
.map(|ty| ty.default_value())
2441-
.collect::<Option<Vec<_>>>()?;
2442-
Some(Func::new(&mut store, self.clone(), move |_, _, results| {
2441+
.collect::<Option<Vec<_>>>()
2442+
.ok_or_else(|| anyhow!("function results do not have a default value"))?;
2443+
Ok(Func::new(&mut store, self.clone(), move |_, _, results| {
24432444
for (slot, dummy) in results.iter_mut().zip(dummy_results.iter()) {
24442445
*slot = *dummy;
24452446
}
@@ -2502,9 +2503,12 @@ impl GlobalType {
25022503
GlobalType::new(ty, mutability)
25032504
}
25042505
///
2505-
pub fn default_value(&self, store: impl AsContextMut) -> Option<RuntimeGlobal> {
2506-
let val = self.content().default_value()?;
2507-
RuntimeGlobal::new(store, self.clone(), val).ok()
2506+
pub fn default_value(&self, store: impl AsContextMut) -> Result<RuntimeGlobal> {
2507+
let val = self
2508+
.content()
2509+
.default_value()
2510+
.ok_or_else(|| anyhow!("global type has no default value"))?;
2511+
RuntimeGlobal::new(store, self.clone(), val)
25082512
}
25092513
}
25102514

@@ -2637,10 +2641,14 @@ impl TableType {
26372641
&self.ty
26382642
}
26392643
///
2640-
pub fn default_value(&self, store: impl AsContextMut) -> Option<RuntimeTable> {
2644+
pub fn default_value(&self, store: impl AsContextMut) -> Result<RuntimeTable> {
26412645
let val: ValType = self.element().clone().into();
2642-
let init_val = val.default_value()?.ref_()?;
2643-
RuntimeTable::new(store, self.clone(), init_val).ok()
2646+
let init_val = val
2647+
.default_value()
2648+
.context("table element type does not have a default value")?
2649+
.ref_()
2650+
.unwrap();
2651+
RuntimeTable::new(store, self.clone(), init_val)
26442652
}
26452653
}
26462654

@@ -2974,8 +2982,8 @@ impl MemoryType {
29742982
&self.ty
29752983
}
29762984
///
2977-
pub fn default_value(&self, store: impl AsContextMut) -> Option<RuntimeMemory> {
2978-
RuntimeMemory::new(store, self.clone()).ok()
2985+
pub fn default_value(&self, store: impl AsContextMut) -> Result<RuntimeMemory> {
2986+
RuntimeMemory::new(store, self.clone())
29792987
}
29802988
}
29812989

0 commit comments

Comments
 (0)