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!
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.
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).
_start
?
https://webassembly.github.io/spec/core/valid/modules.html?highlight=start#start-function
_start is the (unfortunately named, because of frequent confusion) entry point for wasi p1 commands
whereas start
is the wasm core spec construct
Hm, tried start
and _start
and neither are called in wasip2 context
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
?
ah no this is just in BuildTargets.md for now, wasm-tools component new
doesn't have support for _initialize
right now
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
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.
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?
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?
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?
Can confirm this works with TinyGo + wasip2!
image.png
ok great! I'll look to clean up and send a PR today
https://github.com/bytecodealliance/wasm-tools/pull/1747
Thanks!
Last updated: Dec 23 2024 at 12:05 UTC