Skip to content

Memory "leak" when passing funcrefs to the host #10868

@alexcrichton

Description

@alexcrichton

This program:

use wasmtime::*;

fn main() -> Result<()> {
    let engine = Engine::default();
    let mut store = Store::new(&engine, ());

    let module = Module::new(
        &engine,
        r#"
            (module
                (import "" "" (func (param funcref)))

                (elem declare func $x)

                (func $x)
                (func (export "")
                    ref.func $x
                    call 0)
            )
        "#,
    )?;

    let func = Func::wrap(&mut store, |_: Option<Func>| {});
    let i = Instance::new(&mut store, &module, &[func.into()]).unwrap();
    let f = i.get_typed_func::<(), ()>(&mut store, "")?;
    loop {
        f.call(&mut store, ())?;
    }
}

will infinitely consume memory until killed. Basically what happens here is that each time a host function with a funcref parameter is invoked Wasmtime creates a new Func which pushes onto an internal store-local vector that is never shrunk. That means that the memory usage of a store is O(calls) which is generally not a desirable property we want to have.

This is not a regression but is something that's been around for quite a long time. I'm working on fixes in this area though and wanted to file an issue about this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    wasmtime:apiRelated to the API of the `wasmtime` crate itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions