Stream: general

Topic: ✔ WASI with WASM Components


view this post on Zulip 很酷的傢伙 (Feb 15 2024 at 20:55):

Hello again :) I'm trying to load a WASM component module that is compiled to target WASI. I have so far created the Engine, hooked up the WasiHostCtx, and created the store. However, it seems like wasmtime_wasi::preview2::preview1::add_to_linker expects a wasmtime::Linker vs a wastime::component::Linker, whereas MyWorld::add_to_linker expects a wasmtime::component::Linker instead :sweat_smile:

I _think_ I need a single linker with both of those hooked up in order to be able to run code from the WASM component module, is that correct? If so, is what I'm trying to do possible today? and how can I accomplish that? I'm using version 17.0.1 of both wasmtime and wasmtime-wasi for reference

Thanks in advance! :)

view this post on Zulip Alex Crichton (Feb 15 2024 at 22:41):

You're correct that you'll need to use a single linker. If you're using wasmtime::component::bindgen! then you'll need to use wasmtime::component::Linker since you're working with components rather than modules. In that case you'll want to call wasmtime_wasi::preview2::bindings::add_to_linker. The preview1 module is specifically intended for compatibility with modules, not components.

(you probably already heard this before but apologies for the state of the docs, we're working on making them better!)

view this post on Zulip 很酷的傢伙 (Feb 15 2024 at 22:55):

ah ok great! thanks for the info :)

view this post on Zulip 很酷的傢伙 (Feb 16 2024 at 21:28):

hey @Alex Crichton , sorry to bother you again. I'm looking for wasmtime_wasi::preview2::bindings::add_to_linker but I'm not seeing that symbol. Should I be looking for something else?

view this post on Zulip 很酷的傢伙 (Feb 16 2024 at 21:41):

hmmmmm ok I found the add_to_linker functions under sync_io::* and those seem to work with a component::Linker. I'm still running into a runtime error when trying to run my component though :) The guest is built from Rust targeting wasm32-wasi and I've then run wasm-tools component new out.wasm -o out.component.wasm --adapt wasi_snapshot_preview1.reactor.wasm to create the component. However I'm seeing the following error:

called `Result::unwrap()` on an `Err` value: import `wasi:cli/environment@0.2.0` has the wrong type

Caused by:
    0: instance export `get-environment` has the wrong type
    1: expected func found nothing

view this post on Zulip 很酷的傢伙 (Feb 16 2024 at 21:47):

Ok interesting, if I compile my guest with target wasm32-unknown-unknown it seems to load and run correctly and everything, which is exciting. However, I believe that means I don't have access to e.g. the filesystem from the guest, is that correct? Is... there currently a way to do I/O in the guest?

view this post on Zulip Joel Dice (Feb 16 2024 at 21:48):

https://docs.rs/wasmtime-wasi/17.0.1/wasmtime_wasi/preview2/command/sync/fn.add_to_linker.html might be what you're looking for.

view this post on Zulip Notification Bot (Feb 16 2024 at 21:48):

A message was moved here from #general > Begining the process of proposing an Embedded SIG by Joel Dice.

view this post on Zulip 很酷的傢伙 (Feb 16 2024 at 21:49):

I see, thanks! I'll give that a shot :)

view this post on Zulip 很酷的傢伙 (Feb 16 2024 at 21:54):

nice! new error

called `Result::unwrap()` on an `Err` value: Custom { kind: Uncategorized, error: "failed to find a pre-opened file descriptor through which \"/tmp/test\" could be opened" }

which makes me think I need to do something with cap-std to allow that to be read :thinking: will continue investigating, but thanks for the help so far!

view this post on Zulip Joel Dice (Feb 16 2024 at 21:55):

When you create a WasiCtx using WasiCtxBuilder, you have the opportunity to map host directories into the guest filesystem. You'll need to do that in order for the guest to access any part of the host filesystem.

view this post on Zulip Joel Dice (Feb 16 2024 at 21:56):

Might want to look at the wasmtime run implementation as an example.

view this post on Zulip Joel Dice (Feb 16 2024 at 21:56):

https://github.com/bytecodealliance/wasmtime/blob/main/src/commands/run.rs

view this post on Zulip 很酷的傢伙 (Feb 16 2024 at 21:57):

ahhh perfect! thank you :)

view this post on Zulip Joel Dice (Feb 16 2024 at 21:57):

https://docs.rs/wasmtime-wasi/17.0.1/wasmtime_wasi/preview2/struct.WasiCtxBuilder.html#method.preopened_dir

view this post on Zulip Joel Dice (Feb 16 2024 at 21:59):

Or maybe this one: https://docs.rs/wasmtime-wasi/17.0.1/wasmtime_wasi/sync/struct.WasiCtxBuilder.html#method.preopened_dir. I guess there are a few of them :) I think the one above is the right one.

view this post on Zulip 很酷的傢伙 (Feb 16 2024 at 22:04):

ahhhhhh there we go! finally :D was able to read a file from the guest! thank you all so much for the help :grinning_face_with_smiling_eyes:

view this post on Zulip Notification Bot (Sep 12 2024 at 15:38):

Till Schneidereit has marked this topic as resolved.


Last updated: Nov 22 2024 at 16:03 UTC