Skip to content

runtime: Pinner doesn't work for global struct variables  #74116

Open
@strager

Description

@strager

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 {
	engine C.Engine
}

var state *State = &State{}

func main() {
	var pinner runtime.Pinner = runtime.Pinner{}
	pinner.Pin(state)
	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:30
main.main()
        /Users/traduality/Projects/Traduality/beedo/beedovsm/cmd/test/main.go:30 +0xcc
exit status 2

What did you expect to see?

I expect no crash.

If I move the initialization of state ( = &State{}) into main() (or init()), the crash goes away.

My guess at what's happening (probably wrong): &State{} in the variable initialization does not allocate the memory on the heap; it instead uses .data memory. runtime.Pinner.Pin() does nothing for .data memory, but runtime.cgoCheckArg() thinks that .data memory is unpinned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions