Stream: wasi

Topic: _initialize in components


view this post on Zulip Randy Reddig (Aug 28 2024 at 17:30):

We’re working on wasip2 for Go (and TinyGo), and generate a component from a module using wasm-tools component new. When the same program is compiled and run in wasmtime under wasip1, the host calls _initialize. Under wasip2, when the generated component is executed, the _initialize export is not called.

What is the best/right way to have the runtime initialized when running as a component? What function can the module export that is guaranteed to be called before any other exports?

Also, how would this interact with the *-module compilation target? https://github.com/WebAssembly/component-model/pull/378

Thanks!

This PR adds BuildTargets.md to define this new concept of "build targets" as presented in both CG-06 and WASI-06-12, which itself was a revision of the earlier "wasit2" idea th...

view this post on Zulip Joel Dice (Aug 28 2024 at 17:49):

A start function would certainly work, as long as your init function doesn't need to call any imports. If it does need to call imports, I don't know that there's currently a component-level equivalent to _initialize. I know there's been talk about such a thing, but I don't know of anything official, yet.

view this post on Zulip Joel Dice (Aug 28 2024 at 17:52):

The component tooling I'm familiar with for e.g. C, Rust, Python, and .NET either uses lazy initialization or build-time pre-initialization and snapshotting (via e.g. Wizer).

view this post on Zulip Randy Reddig (Aug 28 2024 at 17:57):

_start?

view this post on Zulip Joel Dice (Aug 28 2024 at 17:58):

https://webassembly.github.io/spec/core/valid/modules.html?highlight=start#start-function

view this post on Zulip Pat Hickey (Aug 28 2024 at 18:00):

_start is the (unfortunately named, because of frequent confusion) entry point for wasi p1 commands

view this post on Zulip Pat Hickey (Aug 28 2024 at 18:00):

whereas start is the wasm core spec construct

view this post on Zulip Randy Reddig (Aug 28 2024 at 18:43):

Hm, tried start and _start and neither are called in wasip2 context

view this post on Zulip Luke Wagner (Aug 28 2024 at 19:10):

I could totally be misremembering but @Alex Crichton I vaguely recall that the <prefix>_initialize function in the BuildTargets.md PR was a variation on something already implemented by wasm-tools component new... is that the case and, if so, is it not _initialize?

view this post on Zulip Alex Crichton (Aug 28 2024 at 19:15):

ah no this is just in BuildTargets.md for now, wasm-tools component new doesn't have support for _initialize right now

view this post on Zulip Alex Crichton (Aug 28 2024 at 19:16):

that being said it would be pretty easy to add support for it, basically just instantiate the "main module", setup tables, then have a core wasm start sectoin pointing to _initialize get instantiated

view this post on Zulip Joel Dice (Aug 28 2024 at 19:25):

Randy Reddig said:

Hm, tried start and _start and neither are called in wasip2 context

Just to be clear: I was referring to a core Wasm start function, not an export named start or _start. I'm having trouble finding a link in the spec the describes the semantics, but a module's start function, if present, is guaranteed to be run before any export is called.

view this post on Zulip Randy Reddig (Aug 28 2024 at 19:55):

Alex Crichton said:

that being said it would be pretty easy to add support for it, basically just instantiate the "main module", setup tables, then have a core wasm start sectoin pointing to _initialize get instantiated

That’d be great. It’d map to the WASI 0.1 behavior we see now. Are there any restrictions on what _initialize can do?

view this post on Zulip Randy Reddig (Aug 28 2024 at 21:16):

I’m happy to try to make this work in wasm-tools if I know roughly where to start. Where would the call to _initialize be inserted?

view this post on Zulip Alex Crichton (Aug 28 2024 at 21:19):

Oh that'd be awesome, thanks! I actually had just started to take a look at this and whipped up https://github.com/alexcrichton/wasm-tools/commit/a111b057d7c4b45ee2c1f501903cbcf66dd6382f so far, but that could use some double-checking and some real-world testing too (e.g. making sure it's working for Go modules). Would you be up for starting from that diff and finishing it up?

Low level tooling for WebAssembly . Contribute to alexcrichton/wasm-tools development by creating an account on GitHub.

view this post on Zulip Randy Reddig (Aug 28 2024 at 22:46):

Can confirm this works with TinyGo + wasip2!
image.png

view this post on Zulip Alex Crichton (Aug 29 2024 at 14:44):

ok great! I'll look to clean up and send a PR today

view this post on Zulip Alex Crichton (Aug 29 2024 at 15:04):

https://github.com/bytecodealliance/wasm-tools/pull/1747

This commit implements recognition of the _initialize function from WASIp1 in the componentization process of wasm-tools component new. This additionally corresponds to the same function in the pro...

view this post on Zulip Randy Reddig (Aug 29 2024 at 21:05):

Thanks!


Last updated: Oct 23 2024 at 20:03 UTC