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/cgo: the handle pointer is freed during C asynchronous callback #57463

Closed
huskar-t opened this issue Dec 26, 2022 · 3 comments
Closed
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge

Comments

@huskar-t
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.19 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1056322479=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://go.dev/play/p/k68Hr3YCWdP
when the C asynchronous callback, the runtime/cgo handle pointer has been freed by the gc, causing the wrong memory to be used during the callback

What did you expect to see?

keep the handle pointer until handle.Delete() is called

What did you see instead?

handle pointer has been freed by the gc

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Dec 26, 2022
@doujiang24
Copy link
Contributor

I think it's just a wrong usage.

handle could be GC freed, after C.callback_wrapper returning, it's by design.

according to your code:

func DoAsync() {
	handle := cgo.NewHandle("test")
	pointer := unsafe.Pointer(&handle)
	fmt.Printf("handle:%d,pointer: %v\n", handle, pointer)
	C.callback_wrapper(pointer)
}

@huskar-t
Copy link
Author

@doujiang24 I temporarily use sync.map to save the pointer, I'm not sure if cgo handle should provide this feature.This example is just to show the problem I'm having

@seankhliao
Copy link
Member

I believe this violates cgo pointer rules https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers
specifically:

C code may not keep a copy of a Go pointer after the call returns.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 26, 2022
@golang golang locked and limited conversation to collaborators Dec 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

4 participants