Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic when calling ExportedFunction with WASI function name #2257

Closed
asubiotto opened this issue Jun 18, 2024 · 1 comment · Fixed by #2259
Closed

Panic when calling ExportedFunction with WASI function name #2257

asubiotto opened this issue Jun 18, 2024 · 1 comment · Fixed by #2259
Labels
bug Something isn't working

Comments

@asubiotto
Copy link

asubiotto commented Jun 18, 2024

Describe the bug
Panic when calling ExportedFunction on an instantiated WASI module. This might stem from a misunderstanding of how to use wazero.

To Reproduce
To reproduce run the following test:

func TestWasi(t *testing.T) {
	ctx := context.Background()
	wasiRuntime := wazero.NewRuntime(ctx)
	wasi_snapshot_preview1.MustInstantiate(ctx, wasiRuntime)
	wasiModule := wasiRuntime.Module(wasi_snapshot_preview1.ModuleName)
	_ = wasiModule.ExportedFunction("fd_write")
}

This panics with:

panic: runtime error: index out of range [8] with length 0 [recovered]
	panic: runtime error: index out of range [8] with length 0

goroutine 22 [running]:
testing.tRunner.func1.2({0x1016176e0, 0x140001b28a0})
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1631 +0x1c4
testing.tRunner.func1()
	/opt/homebrew/opt/go/libexec/src/testing/testing.go:1634 +0x33c
panic({0x1016176e0?, 0x140001b28a0?})
	/opt/homebrew/opt/go/libexec/src/runtime/panic.go:770 +0x124
github.com/tetratelabs/wazero/internal/engine/wazevo.(*moduleEngine).NewFunction(0x140001eec00, 0x1a)
	/Users/asubiotto/go/pkg/mod/github.com/tetratelabs/wazero@v1.7.2/internal/engine/wazevo/module_engine.go:187 +0x404
github.com/tetratelabs/wazero/internal/wasm.(*ModuleInstance).ExportedFunction(0x140000b6a00, {0x10120833b?, 0x1016ea000?})
	/Users/asubiotto/go/pkg/mod/github.com/tetratelabs/wazero@v1.7.2/internal/wasm/module_instance.go:210 +0x5c

Expected behavior
No panic an an api.Function object, or a clear error message as to why calling ExportedFunction in this case is not valid.

Environment (please complete the relevant information):

  • Go version: go version go1.22.4 darwin/arm64
  • wazero Version: github.com/tetratelabs/wazero v1.7.2
  • Host architecture: arm64
@mathetake
Copy link
Member

sorry for the delay - yeah first of all this cryptic panic is indeed a bug, and ExportedFunction should return nil or either panic saying this is forbidden. I will fix it. Having said that, host functions are not designed to be called directly like this vs normal Wasm modules in wazero - host module’s functions are invoked with the caller modules context, meaning it should have the access to the memory of the caller (e.g. wasip1 “host module” should be able to read/write the memory of the caller to perform system call). Which has the totally different semantics of the normal wasm modules obviously. (please note host module is not defined in the spec, but this is how generally host functions are implemented).
That’s the reason why api.Module is not returned by wasi_snapshot_preview1.MustInstantiate or similar functions but instead they just return api.Closer. That is intentional api decision.

copypasta my comment from slack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants