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

runtime: gc does not work with wasmexport and "void" functions #69584

Closed
ajwerner opened this issue Sep 22, 2024 · 4 comments
Closed

runtime: gc does not work with wasmexport and "void" functions #69584

ajwerner opened this issue Sep 22, 2024 · 4 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@ajwerner
Copy link

ajwerner commented Sep 22, 2024

Go version

go version devel go1.24-c208b91 Fri Sep 20 13:54:44 2024 +0000 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/ajwerner/.cache/go-build'
GOENV='/home/ajwerner/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/ajwerner/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/ajwerner/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/ajwerner/sdk/gotip'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/ajwerner/sdk/gotip/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='devel go1.24-c208b91 Fri Sep 20 13:54:44 2024 +0000'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/ajwerner/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/ajwerner/src/wasmexport-test/go-runner/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build899058900=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I attempted to use the new functionality introduced in #65199 which allows for greatly reducing the cost to call a go function compiled to webassembly. At first it worked great, but then I noticed it failing whenever it needed to grow the stack. I also notice that if you just call runtime.GC, similar bad stuff happens.

Consider the following program:

//go:build wasm

package main

import (
	"runtime"
)

func main() {
}

//go:wasmexport void-gc
func voidGc() {
	runtime.GC()
}

//go:wasmexport not-void-gc
func notVoidGc() int32 {
	runtime.GC()
	return 0
}

You can compile this program like:

GOOS=wasip1 GOARCH=wasm gotip build -buildmode=c-shared -o test.wasm ./test_wasm.go

Then you can have a runner program like:

package main

import (
	"context"
	_ "embed"
	"log"
	"os"

	"github.com/tetratelabs/wazero"
	"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)

//go:embed test.wasm
var wasm []byte

func main() {
	ctx := context.Background()
	r := wazero.NewRuntime(ctx)
	defer r.Close(ctx) // This closes everything this Runtime created.
	wasi_snapshot_preview1.MustInstantiate(ctx, r)
	mod, err := r.InstantiateWithConfig(ctx, wasm, wazero.NewModuleConfig().
		WithStartFunctions("_initialize").
		WithArgs("arg0").
		WithStderr(os.Stderr).
		WithStdout(os.Stdout).
		WithSysNanosleep())
	if err != nil {
		log.Panicln(err)
	}
	_, err = mod.ExportedFunction("void-gc").Call(ctx)
	if err != nil {
		log.Panicln(err)
	}
}

which can be run with go run ./main.go

go run ./main.go

What did you see happen?

I saw the runtime crash inside wasm, see output. I'll note that in my initial issue, I was not calling runtime.GC, I just happened to see similar failures inside things like growstack and background gc assist. I also noticed that if I add a return value to the entrypoint function, the problems seem to go away.

What did you expect to see?

I expected to not see it crash.

@ajwerner
Copy link
Author

@johanbrandhorst this one is likely to interest you.

@seankhliao seankhliao changed the title compiler: gc does not work with wasmexport and "void" functions runtime: gc does not work with wasmexport and "void" functions Sep 23, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Sep 23, 2024
@johanbrandhorst
Copy link
Member

CC @golang/wasm @cherrymui

@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 24, 2024
@mknyszek mknyszek added this to the Go1.24 milestone Sep 25, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/624000 mentions this issue: cmd/internal/obj/wasm: correct return PC for frameless wasmexport wrappers

@github-project-automation github-project-automation bot moved this from Todo to Done in Go Compiler / Runtime Nov 13, 2024
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

8 participants