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

Trap in host function leaks memory, 0.25 regression #2802

Closed
leoyvens opened this issue Apr 5, 2021 · 1 comment · Fixed by #2803
Closed

Trap in host function leaks memory, 0.25 regression #2802

leoyvens opened this issue Apr 5, 2021 · 1 comment · Fixed by #2803
Labels
bug Incorrect behavior in the current implementation that needs fixing

Comments

@leoyvens
Copy link
Contributor

leoyvens commented Apr 5, 2021

Test Case

use wasmtime::*;

fn main() {
    let wat = r#"
        (module
            (import "" "" (func $host_hello))
            (memory (export "memory") 0)
            (func (export "hello") call $host_hello)
        )
        "#;
    let big = vec![1; u32::MAX as usize / 8];

    loop {
        let store = Store::default();
        let module = Module::new(store.engine(), wat).unwrap();
        let host_hello = Func::wrap(&store, || -> Result<(), Trap> {
            // If `Ok(())` is returned instead, memory will not leak.
            Err(Trap::new("it's a trap"))
        });
        let instance = Instance::new(&store, &module, &[host_hello.into()]).unwrap();

        // Do a large allocation to speed up memory leak.
        let mem = instance.get_memory("memory").unwrap();
        mem.grow(10_000).unwrap();
        mem.write(0, &big).unwrap();

        let hello = instance.get_typed_func::<(), ()>("hello").unwrap();
        let _ = hello.call(());

        // In wasmtime 0.24, this does not reproduce:
        // let hello = instance.get_func("hello").unwrap().get0::<()>().unwrap();
        // let _ = hello();
    }
}

Steps to Reproduce

Run the above program on Linux x86_64.

Expected Results

Program can run forever without exhausting system memory.

Actual Results

Program leaks memory until the system memory is exhausted.

Versions and Environment

Wasmtime version or commit: 0.25
Operating system: Linux
Architecture: x86_64

Extra Info

It does not reproduce on version 0.24, indicating a 0.25 regression. It seems the Store is being leaked. This issue was observed in our application as the error Insufficient resources: mmap failed: Cannot allocate memory (os error 12), because the virtual memory space would be exhausted.

@leoyvens leoyvens added the bug Incorrect behavior in the current implementation that needs fixing label Apr 5, 2021
alexcrichton added a commit to alexcrichton/wasmtime that referenced this issue Apr 5, 2021
Some recent refactorings accidentally had a local `Store` on the stack
when a longjmp was initiated, bypassing its destructor and causing
`Store` to leak.

Closes bytecodealliance#2802
@alexcrichton
Copy link
Member

Oh dear, thanks for the report! I've opened #2803 with a fix for this.

alexcrichton added a commit that referenced this issue Apr 5, 2021
Some recent refactorings accidentally had a local `Store` on the stack
when a longjmp was initiated, bypassing its destructor and causing
`Store` to leak.

Closes #2802
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behavior in the current implementation that needs fixing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants