I've found out that TinyGo 0.33 wasip2
reactor components are currently incompatible with Wasmtime, due to the fact that _initialize
is never called before, for example, wasi:http/incoming-handler.handle
is invoked.
I've found https://github.com/bytecodealliance/wasmtime/issues/7592 related to the topic and here's the TinyGo PR for more context https://github.com/tinygo-org/tinygo/pull/4482
Do I understand correctly that the suggested solution is to build a custom adapter, which would be _initialize
-aware and call it as part of e.g. wasi:http/incoming-handler.handle
call?
Is there some way to make this part of instantiation of the component directly to be done ahead of time?
Or is there some other way this is supposed to be handled?
The wit-component
crate creating components was updated in https://github.com/bytecodealliance/wasm-tools/pull/1747 to recognize _initialize
and handle that, so this may not have propagated to the TinyGo toolchain yet?
(e.g. if they're using a wasi-sdk release then it's not been tagged in wasi-sdk yet)
Oh, cool, thanks! I'll follow up with them
FWIW, they just shell-out to wasm-tools component new
https://github.com/tinygo-org/tinygo/blob/ef4f46f1d1550beb62324d750c496b2b4a7f76d0/builder/build.go#L890-L896, but even after updating to wasm-tools 0.218 seems that TinyGo init()
is still ignored :thinking:
Are you able to double-check if _initialize
is called with the updated version of wasm-tools? If that doesn't fix the issue then it may be something else at play?
This might be related to the fact that the tinygo linked wasi-libc is still SDK 20
https://github.com/tinygo-org/tinygo/tree/release/lib
Good catch, I'll try updating wasi-libc and test!
Oh wasi-libc hasn't had a release yet with an updated version of wasm-component-ld so you'd have to build from source currently (or overwrite the wasm-component-ld binary)
"simply" updating the wasi-libc to latest tag or just main
did not seem to help, but I'm not too familiar with tinygo build setup. Also not completely sure where wasm-component-ld
binary fits in here.
I've filed an issue here https://github.com/tinygo-org/tinygo/issues/4488, btw
I'll also admit I don't know the particulars of TinyGo's linking process. Using wasi-sdk with rust/c/c++ components are created with the wasm-component-ld
linker which is executed in lieu of wasm-ld
. The wasm-component-ld
linker links to wit-component
, the Rust crate to create components, and the workhorse of wasm-tools component new
. The code you linked though looks like it's executing wasm-tools
directly, however, so the support may be entirely unrelated to the version of wasi-sdk in use.
The latest version of wasi-sdk, the wasi-sdk-24 tag, does not have a wasm-component-ld
with support for _initialize
. The latest wasm-component-ld
, if you install via cargo install
, does however. Support for _initialize
first showed up in 217 for wasm-tools.
if that helps, it seems that they use wasm-ld
https://github.com/tinygo-org/tinygo/blob/ef4f46f1d1550beb62324d750c496b2b4a7f76d0/targets/wasip2.json#L8
so perhaps the fix is to change the linker to wasm-component-ld
?
unexpected: linker wasm-component-ld should be ld.lld or wasm-ld
I don't think so, it looks like TinyGo is doing some wasm transforms like asyncify which wouldn't work with a component. They're also executing wasm-tools component new
which means that wasm-component-ld
isn't needed.
If you're sure that your newer version of wasm-tools
was used during linking for the component then at least the way I might be able to help would be to capture the wasm-tools component new
command. For example what was the input module and what was the output?
here's the captured input and output Wasm used for wasm-tools component new
really appreciate the help!
It looks like in.wasm
doesn't export _initialize
?
well, yeah, it looks like they wouldn't https://github.com/tinygo-org/tinygo/blob/ef4f46f1d1550beb62324d750c496b2b4a7f76d0/src/runtime/runtime_wasm_wasip2.go#L14-L26 :D
looks like this could be the actual issue here (they simply never exported the function)
yep, that's it, it works now! Thanks @Alex Crichton
up-to-date wasm-tools
and a small fix to tinygo to actually export the _initialize
is what's necessary here
https://github.com/tinygo-org/tinygo/pull/4490
according to https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md#current-unstable-abi, it's either _initialize
or _start
that's called, but not both.
For wasip2 command components, is _initialize
meant to be ignored or is it meant to be called the same as for reactors?
There's less of a distinction of sorts in components about commands/reactors. Currently though the _start function is not handled at all by wasm-tools component new
. It's not recognized or called at all. All wasm-tools component new
does is ensure that if _initialize
is exported it's called before all exports.
In that sense TinyGo will have to protect against this internally where if _start
is accessible via wasi:cli/run
then it'll have to be prepared for _initialize
to already have run.
Last updated: Nov 22 2024 at 16:03 UTC