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

cmd/cgo: sometimes when passing a struct to C containing a go pointer to a struct field, the entire go struct is checked for go pointers instead of just the field #63460

Closed
AlexRouSg opened this issue Oct 9, 2023 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@AlexRouSg
Copy link
Contributor

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

$ go version
go version go1.21.2 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=~\AppData\Local\go-build
set GOENV=~\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=~\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=~\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.2
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\alexr\AppData\Local\Temp\go-build886793640=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

/*
 typedef struct foo_t *foo;

 typedef struct {
	foo* pfoo;
 }info;

 void test(info i) {}
*/
import "C"

type foo struct {
	cFoo C.foo
	bar  []interface{}
}

func main() {
	f := foo{bar: []interface{}{0}}
	info := C.info{&f.cFoo}
	C.test(info)
}

What did you expect to see?

Code compiles and runs with no output

What did you see instead?

panic: runtime error: cgo argument has Go pointer to unpinned Go pointer

Notes:
Not setting the field bar: []interface{}{0} or changing function call to look like C.test(&f.cFoo) does not trigger this

@seankhliao seankhliao changed the title runtime: cgo: sometimes when passing a struct to C containing a go pointer to a struct field, the entire go struct is checked for go pointers instead of just the field runtime/cgo: sometimes when passing a struct to C containing a go pointer to a struct field, the entire go struct is checked for go pointers instead of just the field Oct 9, 2023
@dmitshur dmitshur added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. compiler/runtime Issues related to the Go compiler and/or runtime. labels Oct 9, 2023
@dmitshur dmitshur added this to the Backlog milestone Oct 9, 2023
@dmitshur
Copy link
Contributor

dmitshur commented Oct 9, 2023

CC @golang/runtime.

@ianlancetaylor
Copy link
Contributor

This is similar to #14210. The cgo tool is not smart enough to pull apart what you are doing in your Go code. It loses the &f.cFoo and just checks the entire allocation to which the pointer points.

It would be nice to fix if somebody can figure out how. But nobody is working on this.

@ianlancetaylor ianlancetaylor changed the title runtime/cgo: sometimes when passing a struct to C containing a go pointer to a struct field, the entire go struct is checked for go pointers instead of just the field cmd/cgo: sometimes when passing a struct to C containing a go pointer to a struct field, the entire go struct is checked for go pointers instead of just the field Oct 9, 2023
@AlexRouSg
Copy link
Contributor Author

That's fair and I have no expectation/need of this getting fixed anytime soon since the workaround is just to add a c := f.cFoo
However, would it be a good idea to at a minimum point this out in the doc as a known limitation that may or may not get fixed in the future?

@bcmills
Copy link
Contributor

bcmills commented Oct 13, 2023

Is this a duplicate of #28606?

@ianlancetaylor
Copy link
Contributor

Thanks, I thought there was an open issue but I couldn't find it. Marking as a dup since it's essentially the same problem.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Oct 13, 2023
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. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants