bytecodealliance / wizer

The WebAssembly Pre-Initializer
Apache License 2.0
942 stars 55 forks source link

Supporting both non-WASI imports and WASI imports #92

Closed bhelx closed 1 year ago

bhelx commented 1 year ago

I need some advice. I'm wizening a program which requires WASI imports, but it also needs a couple non-WASI imports as well. I understand this is not allowed, but I'm also curious if there are any ways around this. I tried some tricks to get the functions to not execute during wizening time, but enable them during "runtime", but was having trouble making this work.

Because I know these functions have no side-effect on the program (they basically fire and forget events to the host) I also tried just commenting out the Err trap in the dummy_func: https://github.com/bytecodealliance/wizer/blob/main/src/dummy.rs#L51

That worked for me. I was thinking of adding a new option to Wizer's config, something like this:

Wizer::new()
        .allow_wasi(true)?
        .allow_namespace("my_namespace")
        .inherit_stdio(true)
        // ... etc

This imports non-trapping implementations for dummy_funcs for every function under the module name "my_namespace" and leaves the trapping dummy_func for the rest.

Is this a bad idea? Are there consequences to this I'm not seeing? Would you accept a change along these lines?

cfallin commented 1 year ago

Hi @bhelx -- I had a really similar need recently and added "preloads" in #81. The idea is that you can write a module with stubbed-out intrinsics and specify that its exports are available as imports to the main module. Wizer trusts that the intrinsics really do have no side-effects (if they do, then the snapshotting does not capture them) but this is sufficient for "notify rest of system"-kind of calls, as you describe yours to be.

There's a smoketest in the PR that shows how to use it programmatically, and it also has a CLI option. Hopefully it works for your case too!

fitzgen commented 1 year ago

The other option is to use a custom wasmtime::Linker, which will let you make anything you want available.

https://docs.rs/wizer/latest/wizer/struct.Wizer.html#method.make_linker

fitzgen commented 1 year ago

Closing this issue because there are two approaches available for doing this, and we don't need a third. Thanks for filing an issue.

bhelx commented 1 year ago

@fitzgen @cfallin Thanks!