Stream: general

Topic: Wasi & reactors


view this post on Zulip Léo Gaspard (Jan 14 2021 at 00:34):

Just to ask a question about what should be done for wasm configuration: My use case is that I have a wasm module that I load with wasmtime, and then call various functions of it as hooks. IIUC, it means that my module is semantically a "reactor".

I'm compiling this module from Rust code. So I came upon https://github.com/rust-lang/rust/pull/79997 .
However, I couldn't make it work in practice, be it from my program or using wasmtime --invoke. It looks to me, like rustc doesn't actually create the _initialize function. (I posted more details on the PR thread itself)

Is what I'm looking for actually wasm reactors? And if yes, am I doing something wrong there, or is it just that rustc is currently unable to compile a WASI-able reactor? (The only idea for a workaround I'd have would be to make my thing not a reactor but a command, and have it just call a "dothething" function in the host that'd then do all the callbacks, thus ensuring that datastructures are initialized by virtue of being in the wasm's main scope... But that sounds awful)

Fixes #79199, and relevant to #73432 Implements wasi reactors, as described in WebAssembly/WASI#13 and design/application-abi.md Empty lib.rs, lib.crate-type = ["cdylib"]: $ cargo +reacto...

view this post on Zulip Dan Gohman (Jan 14 2021 at 00:45):

At first glance, it does sound like what you're doing is a reactor. I think the problem is that Rust needs to switch to crt1-reactor.o (instead of crt1.o), but I haven't dug into it yet.

view this post on Zulip Dan Gohman (Jan 14 2021 at 00:45):

Doing so may also require an update to a newer WASI libc

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:10):

Thank you for your feedback! According to a recent message on the PR, it looks like I did something wrong in the compilation process — I've documented the procedure I took to make things work on there :)

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:15):

Ah, yes, using --entry _initialize means some things may work without crt1-initialize.o

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:15):

er, crt1-reactor.o

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:19):

Thanks for helping test this and figure out something that works! Hopefully in the future we'll fix the pub fn main requirement and generally smooth out the process :smile:

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:19):

:heart:

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:21):

Ah, also while I have this topic paged in here, _initialize is called automatically if you use wasmtime's Linker::module function

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:23):

And not otherwise. When we added support for that, there were differing opinions about whether we should always run _initialize on any instantiate call, and we ended up deciding not to. But this may evolve as our understanding of what a reactor is evolves.

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:26):

Ooh right I missed it. That said there's something I don't get about Linker::module: usually the Command/Reactor would be the last thing one would want to add to a linker... yet it looks to me like after Linker::module one has to run Linker::instantiate again with... another module?

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:27):

After Linker::module, the reactor should be instantiated and its exports available for get etc.

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:27):

IOW, it would sound more logical to me if Linker::module did not do reactor initialization, but Linker::instantiate did (or allowed to do)... but I may be missing something?

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:28):

Oooh you're running .get() directly on the Linker, and not on an Instance?

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:29):

Hmm I guess no it doesn't have the necessary things for that

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:29):

when you're using a Linker, it generally owns the instances, and you can do get to get their exports

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:30):

Hmm the thing I'm wondering about is, to get an Instance from a Linker, I see only Linker::instantiate; which requires giving another Module?

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:30):

or, actually, that's not quite right. It doesn't own the instances. But it does collect all the exports and makes them available

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:31):

the linker doesn't generally hand out instances; it hands out exports.

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:31):

Oooh I misread the signature of Linker::get ._.

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:33):

Ok so if I understand correctly, Linker::instantiate should very rarely be used?

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:35):

I think it comes down to whether you want a lower-level interface where you have more control, or a higher-level interface that does more things for you

view this post on Zulip Dan Gohman (Jan 14 2021 at 01:36):

instantiate directly corresponds to an operation in the wasm spec, so if you know that that's exactly what you want, it's there

view this post on Zulip Léo Gaspard (Jan 14 2021 at 01:51):

Hmm ok thank you! I've just checked and was indeed able to get things working this way :) So I've repurposed https://github.com/bytecodealliance/wasmtime/issues/2580 for the documentation issue. Thank you! :D

Feature Automatically call _initialize for reactors, maybe using a special function to create a reactor Module? Benefit Knowing one has to call _initialize is something that is not very documented ...

Last updated: Dec 23 2024 at 12:05 UTC