Stream: wasi

Topic: Using wasi API in a wasm+wasi module


view this post on Zulip kellytk (Oct 30 2023 at 05:29):

I'm writing, in Rust, a wasm+wasi (wasm32-wasi) module that's run with wasmtime. In this module, I'd like to use https://docs.wasmtime.dev/api/wasmtime_wasi/preview2/command/sync/wasi/sockets/ip_name_lookup/index.html. Where in the wasm book, examples, or elsewhere most closely demonstrates how? The main disconnect I have is, how do I 'use' wasi API in Rust code that'll itself become wasm+wasi.

view this post on Zulip kellytk (Oct 30 2023 at 06:21):

*wasmtime book. I attempted to edit my original message however that doesn't appear to be allowed.

view this post on Zulip Ramon Klass (Oct 30 2023 at 09:34):

(I think you can edit your messages for a few minutes and then they become locked, general Zulip feature)

view this post on Zulip Ramon Klass (Oct 30 2023 at 09:35):

I'm not entirely sure what you mean with wasm+wasi

view this post on Zulip kellytk (Oct 30 2023 at 09:35):

Well, wasm that uses wasi API

view this post on Zulip Ramon Klass (Oct 30 2023 at 09:38):

in general you just use the libc functions, as in, rust's builtin name lookup calls, wasi translates that to use wasi_sockets and then the runtime either allows the calls or blocks them

view this post on Zulip Ramon Klass (Oct 30 2023 at 09:39):

I haven't worked with wasi_sockets at all yet, so I can't help with specifics

view this post on Zulip kellytk (Oct 30 2023 at 09:40):

Makes sense! I'll try that, thank you :-)

view this post on Zulip kellytk (Oct 30 2023 at 12:16):

@Ramon Klass Which Rust builtin name lookup calls did you have in mind?

view this post on Zulip Joel Dice (Oct 30 2023 at 13:39):

Neither wasi-libc nor the Rust standard library (which is based on wasi-libc when targeting wasm32-wasi) have wasi-sockets support yet. I'm planning to start working on that in a few weeks if nobody beats me to it.
Meanwhile, you could consider calling the wasi-sockets host functions directly using bindings generated by wit-bindgen; I haven't tried that myself, though.

view this post on Zulip kellytk (Oct 30 2023 at 13:42):

@Joel Dice Oh and so there's no Rust API available, whether in std or a crate, to perform host name resolving to addresses in a wasm module? The wit-bindgen bindings are currently the only possible option?

view this post on Zulip Joel Dice (Oct 30 2023 at 13:43):

To my knowledge, yes, that's correct. wasi-sockets is quite new, and the infrastructure and implementations are still being built.

view this post on Zulip kellytk (Oct 30 2023 at 13:49):

Would you happen to know if that approach is documented somewhere, say, in the wasmtime book?

view this post on Zulip Joel Dice (Oct 30 2023 at 13:56):

I'm not aware of any docs specific to wasi-sockets, but there is general documentation about importing host interfaces with wit-bindgen/cargo-component at https://component-model.bytecodealliance.org/language-support/rust.html#importing-an-interface-with-cargo-component. In this case, you'll want to copy the WIT files from the crates/wasi/wit directory of the version of Wasmtime you plan to use. You'll want to make sure it's the exact same (and fairly recent) version since the interfaces have not yet stabilized and have changed from version to version.

view this post on Zulip kellytk (Oct 30 2023 at 14:02):

Thank you for the helpful primer


Last updated: Oct 23 2024 at 20:03 UTC