Stream: wasmtime

Topic: panic: expected 14 imports, found 0


view this post on Zulip Alistair Cunningham (Mar 28 2024 at 14:28):

Hello! I'm working on a new open source project in Go using WASM, and am considering using wasmtime. I've got the basic demo working, but when I change it to run the WASM from a file, I get an error "panic: expected 14 imports, found 0". Any ideas on how to fix this? Attached is my test code.
hello.go

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 14:59):

And here is my test WASM code.
test.go

view this post on Zulip Alex Crichton (Mar 28 2024 at 15:11):

I might recommend using wasmtime.Linker instead of wasmtime.NewInstance since that'll make it a bit easier to understand what's going on here

view this post on Zulip Alex Crichton (Mar 28 2024 at 15:11):

more-or-less the problem is that the module you're instantiating has imports, and you're not providing any imports, so they'll need to be provided somewhow.

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 15:19):

Thank you Alex. Would you have example code for using the linker to load a .wasm file? If the example would have WASI support for giving the .wasm code access to files in a chosen directory, that would be great as that's my next thing to test.

view this post on Zulip Alex Crichton (Mar 28 2024 at 15:20):

would this perhaps suffice?

Go WebAssembly runtime powered by Wasmtime. Contribute to bytecodealliance/wasmtime-go development by creating an account on GitHub.

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 15:22):

I did see that, and unfortunately it uses .wat inside the host code, rather than loading a .wasm file. It's loading a .wasm file that I'm specifically having problems with.

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 15:22):

Let me try modifying it to load a .wasm file and see how far I get...

view this post on Zulip Alex Crichton (Mar 28 2024 at 15:23):

Yeah for the purposes there the wat and wasm formats are interchangeable, so you should be able to change just that one detail

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 15:31):

That works. Thank you! Now moving on to test PreopenDir()...

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 15:41):

Unfortunately if I do wasiConfig.PreopenDir("/", "/") in the host, and ioutil.WriteFile("/tmp/helloworld.txt", []byte("Hello world\n"), 0644) in the .wasm, the ioutil.WriteFile() returns Error: open /tmp/helloworld.txt: file does not exist. Any ideas?

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 15:42):

test.go
wasi.go

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 16:00):

I've tried a few different combinations of paths in PreopenDir() and paths in the .wasm. All return a file does not exist error. Would anyone have any ideas? Are there any examples of using PreopenDir()?

view this post on Zulip Alex Crichton (Mar 28 2024 at 18:51):

This may or may not be related to TinyGo's support for wasm, I vaguely recall issues along these lines in the past around things like _initialize or _start not being called in the right order or something that. Either that or TinyGo's runtime wasn't initialized and so wasn't picking up preopens.

Beyond that though I unfortunately may not be able to help a whole lot

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 18:56):

Okay, thanks.

view this post on Zulip Alistair Cunningham (Mar 28 2024 at 18:57):

I now have it working without WASI, using the linker as you recommended. I'm now trying to get WASI to allow the .wasm code to read and write files in a chosen directory.


Last updated: Nov 22 2024 at 16:03 UTC