Skip to content

Passing GC objects multiple times to wasm can cause leaks/refcount corruption #13037

@alexcrichton

Description

@alexcrichton

This test:

    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, Ordering};
    use wasmtime::*;

    #[derive(Clone)]
    struct SetOnDrop(Arc<AtomicBool>);

    impl Drop for SetOnDrop {
        fn drop(&mut self) {
            self.0.store(true, Ordering::SeqCst);
        }
    }

    #[test]
    fn doit() -> Result<()> {
        let engine = Engine::default();
        let mut store = Store::new(&engine, ());

        let module = Module::new(
            &engine,
            r#"(module (func (export "nop") (param externref)))"#,
        )?;

        let instance = Instance::new(&mut store, &module, &[])?;
        let nop = instance.get_typed_func::<Option<Rooted<ExternRef>>, ()>(&mut store, "nop")?;

        let dropped = Arc::new(AtomicBool::new(false));
        {
            let mut scope = RootScope::new(&mut store);
            let ext = ExternRef::new(&mut scope, SetOnDrop(dropped.clone()))?;

            nop.call(&mut scope, Some(ext))?;
            nop.call(&mut scope, Some(ext))?;
        }

        store.gc(None)?;

        assert!(dropped.load(Ordering::SeqCst));

        Ok(())
    }

currently fails:

$ cargo test -q

running 1 test
tests::doit --- FAILED

failures:

---- tests::doit stdout ----

thread 'tests::doit' (743272) panicked at src/lib.rs:65:9:
assertion failed: dropped.load(Ordering::SeqCst)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::doit

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

error: test failed, to rerun pass `--lib`

Metadata

Metadata

Assignees

No one assigned

    Labels

    wasm-proposal:gcIssues with the implementation of the gc wasm proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions