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

Function::new_native support for closures with captured environments #1811

Closed
matthew-a-thomas opened this issue Nov 11, 2020 · 4 comments · Fixed by #1841
Closed

Function::new_native support for closures with captured environments #1811

matthew-a-thomas opened this issue Nov 11, 2020 · 4 comments · Fixed by #1841
Labels
1.0 Wasmer at 1.0 bug Something isn't working

Comments

@matthew-a-thomas
Copy link

Describe the bug

Function::new_native seems to be playing fast and loose with closures.

Cargo.toml:

[dependencies]
anyhow = "1.0.34"
wasmer = "1.0.0-alpha5"
wasmer-compiler-cranelift = "1.0.0-alpha5"
wasmer-engine-jit = "1.0.0-alpha5"

For reference this (correctly) prints "42":

use anyhow::Result;
use wasmer::*;

fn main() -> Result<()> {
    let wat = r#"(module
        (import "console" "log" (func $log (param i32)))
        (func (export "main")
          i32.const 42
          call $log
        )
    )"#;
    let cranelift = Cranelift::default();
    let store = Store::new(&JIT::new(&cranelift).engine());
    let module = Module::new(&store, wat)?;
    let import_object = imports! {
        "console" => {
            "log" => Function::new_native(&store, |value: i32| {
                println!("{}", value);
            })
        }
    };
    let instance = Instance::new(&module, &import_object)?;
    let main: NativeFunc<(), ()> = instance
        .exports
        .get_native_function("main")?;
    main.call()?;
    Ok(())
}

This (incorrectly) crashes the program with a stack overflow:

use anyhow::Result;
use wasmer::*;
use std::sync::Arc;

fn main() -> Result<()> {
    let wat = r#"(module
        (import "console" "log" (func $log (param i32)))
        (func (export "main")
          i32.const 42
          call $log
        )
    )"#;
    let cranelift = Cranelift::default();
    let store = Store::new(&JIT::new(&cranelift).engine());
    let module = Module::new(&store, wat)?;
    let message = "message";
    let message_arc = Arc::new(message);
    let import_object = imports! {
        "console" => {
            "log" => Function::new_native(&store, move |value: i32| {
                println!("{}, {}", message_arc, value);
            })
        }
    };
    let instance = Instance::new(&module, &import_object)?;
    let main: NativeFunc<(), ()> = instance
        .exports
        .get_native_function("main")?;
    main.call()?;
    Ok(())
}

This (incorrectly) prints 0:

use anyhow::Result;
use wasmer::*;

fn main() -> Result<()> {
    let wat = r#"(module
        (import "console" "log" (func $log (param i32)))
        (func (export "main")
          i32.const 42
          call $log
        )
    )"#;
    let cranelift = Cranelift::default();
    let store = Store::new(&JIT::new(&cranelift).engine());
    let module = Module::new(&store, wat)?;
    let state = 1337;
    let import_object = imports! {
        "console" => {
            "log" => Function::new_native(&store, move |_: i32| {
                println!("{}", state);
            })
        }
    };
    let instance = Instance::new(&module, &import_object)?;
    let main: NativeFunc<(), ()> = instance
        .exports
        .get_native_function("main")?;
    main.call()?;
    Ok(())
}

Additional context

Globally-installed things:

wasmer 1.0.0-alpha4
rustc 1.47.0 (18bf6b4f0 2020-10-07)
Windows 10 10.0.19042 N/A Build 19042
@matthew-a-thomas matthew-a-thomas added the bug Something isn't working label Nov 11, 2020
@syrusakbary
Copy link
Member

Thanks for creating the issue!

Wasmer currently doesn't support closures in Functions, but we have been investigating in the last days to see if we can add support for them. We'll post here our findings!

@jubianchi jubianchi added the 1.0 Wasmer at 1.0 label Nov 20, 2020
bors bot added a commit that referenced this issue Nov 30, 2020
1841: Disable closures as host functions for now + docs + tests r=syrusakbary a=MarkMcCaskey

Resolves #1811 for now

Part of #1840 

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Mark McCaskey <mark@wasmer.io>
Co-authored-by: Syrus Akbary <me@syrusakbary.com>
@bors bors bot closed this as completed in 5fe3929 Nov 30, 2020
@syrusakbary
Copy link
Member

Even though we merged the PR that panics if closures with captured environments are used for native functions, I'd like to keep this issue open until we fully support closures with captured environments in native functions

@syrusakbary syrusakbary reopened this Dec 1, 2020
@syrusakbary syrusakbary changed the title Funny business with Function::new_native Function::new_native support for closures with captured environments Dec 1, 2020
@webmaster128
Copy link
Contributor

I'd like to keep this issue open until we fully support closures with captured environments in native functions

Wasn't #1840 supposed to track that? What's the difference between the two tickets?

@Hywan
Copy link
Contributor

Hywan commented Jul 16, 2021

Closing in favor of #1840. I had to pick one :-).

@Hywan Hywan closed this as completed Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.0 Wasmer at 1.0 bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants