Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a panic calling host functions with refs in async mode #8434

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 0 additions & 18 deletions crates/wasmtime/src/runtime/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,24 +1346,6 @@ impl Func {
let (params, results) = val_vec.split_at_mut(nparams);
func(caller.sub_caller(), params, results)?;

#[cfg(feature = "gc")]
{
// See the comment in `Func::call_impl_check_args`.
let num_gc_refs = ty.as_wasm_func_type().non_i31_gc_ref_returns_count();
if let Some(num_gc_refs) = NonZeroUsize::new(num_gc_refs) {
if caller
.as_context()
.0
.gc_store()?
.gc_heap
.need_gc_before_entering_wasm(num_gc_refs)
{
assert!(!caller.as_context().0.async_support());
caller.as_context_mut().gc();
}
}
}

// Unlike our arguments we need to dynamically check that the return
// values produced are correct. There could be a bug in `func` that
// produces the wrong number, wrong types, or wrong stores of
Expand Down
62 changes: 62 additions & 0 deletions tests/all/async_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,3 +983,65 @@ async fn gc_preserves_externref_on_historical_async_stacks() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn async_gc_with_func_new_and_func_wrap() -> Result<()> {
let _ = env_logger::try_init();

let mut config = Config::new();
config.async_support(true);
let engine = Engine::new(&config)?;

let module = Module::new(
&engine,
r#"
(module $m1
(import "" "a" (func $a (result externref)))
(import "" "b" (func $b (result externref)))

(table 2 funcref)
(elem (i32.const 0) func $a $b)

(func (export "a")
(call $call (i32.const 0))
)
(func (export "b")
(call $call (i32.const 1))
)

(func $call (param i32)
(local $cnt i32)
(loop $l
(drop (call_indirect (result externref) (local.get 0)))
(local.set $cnt (i32.add (local.get $cnt) (i32.const 1)))

(if (i32.lt_u (local.get $cnt) (i32.const 1000))
(then (br $l)))
)
)
)
"#,
)?;

let mut linker = Linker::new(&engine);
linker.func_wrap("", "a", |mut cx: Caller<'_, _>| {
Ok(Some(ExternRef::new(&mut cx, 100)?))
})?;
let ty = FuncType::new(&engine, [], [ValType::EXTERNREF]);
linker.func_new("", "b", ty, |mut cx, _, results| {
results[0] = ExternRef::new(&mut cx, 100)?.into();
Ok(())
})?;

let mut store = Store::new(&engine, ());
let instance = linker.instantiate_async(&mut store, &module).await?;
let a = instance.get_typed_func::<(), ()>(&mut store, "a")?;
a.call_async(&mut store, ()).await?;

let mut store = Store::new(&engine, ());
let instance = linker.instantiate_async(&mut store, &module).await?;
let b = instance.get_typed_func::<(), ()>(&mut store, "b")?;
b.call_async(&mut store, ()).await?;

Ok(())
}