Go version

go version go1.24.1 darwin/arm64; go version go1.24.1 windows/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/traduality/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/traduality/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/l4/p1_djmc55xq5rxlpmdxqfb8c0000gp/T/go-build2536227112=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/traduality/Projects/Traduality/go.mod'
GOMODCACHE='/Users/traduality/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/traduality/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/nix/store/jp3z5jp9gaxsw7fdzbqn29aabmrxq62j-go-1.24.1/share/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/Users/traduality/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/nix/store/jp3z5jp9gaxsw7fdzbqn29aabmrxq62j-go-1.24.1/share/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Run this program (requires CGo):

package main

import (
    "runtime"
)

/*
typedef struct {
   void *p;
} Engine;

void foo(Engine* e) {
   e->p = e;
}
*/
import "C"

type State struct {
    dummy *int
    engine C.Engine
}

func main() {
    var state *State = &State{}

    var d int = 42
    state.dummy = &d

    var pinner runtime.Pinner = runtime.Pinner{}
    pinner.Pin(state)
    //pinner.Pin(state.dummy)
    defer pinner.Unpin()

    C.foo(&state.engine)
    C.foo(&state.engine)
}

What did you see happen?

The program crashes:

$ go run ./test/
panic: runtime error: cgo argument has Go pointer to unpinned Go pointer

goroutine 1 [running]:
main.main.func2(...)
        /Users/traduality/Projects/Traduality/beedo/beedovsm/cmd/test/main.go:35
main.main()
        /Users/traduality/Projects/Traduality/beedo/beedovsm/cmd/test/main.go:35 +0x104
exit status 2

What did you expect to see?

I expect no crash.

If I uncomment the pinner.Pin(state.dummy) line, then I see no crash. However, this is surprising. I am not passing &state to C.foo(); I am passing &state.engine which cannot [easily] access state.dummy.

Go's current checking seems to contradict the CGo docs:

Go code may pass a Go pointer to C provided the memory to which it points does not contain any Go pointers to memory that is unpinned. When passing a pointer to a field in a struct, the Go memory in question is the memory occupied by the field, not the entire struct. When passing a pointer to an element in an array or slice, the Go memory in question is the entire array or the entire backing array of the slice.

Comment From: strager

Possible duplicate of: https://github.com/golang/go/issues/28606 (but I'm not sure)

Comment From: seankhliao

same as #63460

Comment From: gabyhelp

Related Issues

Related Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)