Skip to content- Sources: primary
- Summary: Shraddha Agrawal shows that cgo passes field-level information to runtime.cgoCheckPointer only when it recognizes how the argument is written. Otherwise runtime.cgoCheckUnknownPointer scans the entire heap object and panics on any unpinned Go pointer, even in fields C never touches, although the docs allow passing a pointer to a field. The deficiency has been reported since 2016, and Ian Lance Taylor wrote that fixing it would need full type checking of cgo input.
- Why it matters: Go code calling C should take addresses inside the call expression or pin nested pointers with runtime.Pinner, and measure call overhead after refactors.
- Background: Go's garbage collector cannot see pointers once they are handed to C, so the cgo rules forbid passing C memory that contains unpinned Go pointers. cgo enforces this at runtime by inserting checks into the code it generates, and GODEBUG=cgocheck=0 turns them off. runtime.Pinner, added in Go 1.21, lets a program pin nested objects so the check accepts them.