Fix a memory leak on returning incompatible values (#2424)
This fixes an issue where if a store-incompatible value is returned from a host-defined function then that value is leaked. Practically this means that it's possible to accidentally leak `Func` values, but a simple insertion of a `drop` does the trick!
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
use super::ref_types_module;
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
use wasmtime::*;
|
||||
|
||||
#[test]
|
||||
@@ -83,3 +85,28 @@ fn receive_null_funcref_from_wasm() -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_store() -> anyhow::Result<()> {
|
||||
let dropped = Rc::new(Cell::new(false));
|
||||
{
|
||||
let store1 = Store::default();
|
||||
let store2 = Store::default();
|
||||
|
||||
let set = SetOnDrop(dropped.clone());
|
||||
let f1 = Func::wrap(&store1, move || drop(&set));
|
||||
let f2 = Func::wrap(&store2, move || Some(f1.clone()));
|
||||
assert!(f2.call(&[]).is_err());
|
||||
}
|
||||
assert!(dropped.get());
|
||||
|
||||
return Ok(());
|
||||
|
||||
struct SetOnDrop(Rc<Cell<bool>>);
|
||||
|
||||
impl Drop for SetOnDrop {
|
||||
fn drop(&mut self) {
|
||||
self.0.set(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user