You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
runtime/cgo: improve error messages after ptr panic
This commit improves the error messages after panics due to the
sharing of an unpinned Go pointer (or a pointer to an unpinned Go
pointer) between Go and C.
This occurs when it is:
1. returned from Go to C (through cgoCheckResult)
2. passed as argument to a C function (through cgoCheckPointer).
An unpinned Go pointer refers to a memory location that might be moved or
freed by the garbage collector.
Therefore:
- change the signature of cgoCheckArg (it does the real work behind
cgoCheckResult and cgoCheckPointer)
- change the signature of cgoCheckUnknownPointer (called by cgoCheckArg
for checking unexpected pointers)
- introduce cgoFormatErr (it formats an error message with the caller name)
- update the cgo pointer tests (add new tests, and a field errTextRegexp
to the struct ptrTest)
- remove an unused assignment in TestPointerChecks.
1. cgoCheckResult
When an unpinned Go pointer (or a pointer to an unpinned Go pointer) is
returned from Go to C,
> package main
>
> import (
> "C"
> )
>
> //export foo
> func foo() map[int]int {
> return map[int]int{0: 1,}
> }
This error shows up at runtime:
> panic: runtime error: cgo result is unpinned Go pointer or points to unpinned Go pointer
>
> goroutine 17 [running, locked to thread]:
> panic({0x78ac36001d40?, 0xc000194000?})
> /usr/lib/go/src/runtime/panic.go:802 +0x168
> runtime.cgoCheckArg(0x78ac36002f40, 0xc000190000, 0xd8?, 0x0, {0x78ac35fdda62, 0x42})
> /usr/lib/go/src/runtime/cgocall.go:628 +0x4c5
> runtime.cgoCheckResult({0x78ac36002f40, 0xc000190000})
> /usr/lib/go/src/runtime/cgocall.go:795 +0x4b
> _cgoexp_1c29bd2c0b96_foo(0x7fffc16d87b0)
> _cgo_gotypes.go:46 +0x6f
> runtime.cgocallbackg1(0x78ac35fd4d20, 0x7fffc16d87b0, 0x0)
> /usr/lib/go/src/runtime/cgocall.go:446 +0x289
> runtime.cgocallbackg(0x78ac35fd4d20, 0x7fffc16d87b0, 0x0)
> /usr/lib/go/src/runtime/cgocall.go:350 +0x132
> runtime.cgocallbackg(0x78ac35fd4d20, 0x7fffc16d87b0, 0x0)
> <autogenerated>:1 +0x2b
> runtime.cgocallback(0x0, 0x0, 0x0)
> /usr/lib/go/src/runtime/asm_amd64.s:1082 +0xcd
> runtime.goexit({})
> /usr/lib/go/src/runtime/asm_amd64.s:1693 +0x1
_cgoexp_1c29bd2c0b96_foo is the faulty caller; it is not obvious to
find it in the stack trace. Moreover the error does say which kind of
pointer caused the panic; for instance, a Go map.
Retrieve caller name and pointer kind; use them in the error message:
> panic: runtime error: result of cgo function foo is unpinned Go map or points to unpinned Go map
>
> goroutine 17 [running, locked to thread]:
> panic({0x76f2fd69b3c0?, 0x1fba8c79c000?})
> /mnt/go/src/runtime/panic.go:877 +0x16f
> runtime.cgoCheckArg(0x76f2fd69c5c0, 0x1fba8c790000, 0x0?, 0x0, 0x1)
> /mnt/go/src/runtime/cgocall.go:631 +0x499
> runtime.cgoCheckResult({0x76f2fd69c5c0, 0x1fba8c790000})
> /mnt/go/src/runtime/cgocall.go:798 +0x45
> _cgoexp_1c29bd2c0b96_foo(0x7ffd0803b8c0)
> _cgo_gotypes.go:46 +0x6f
> runtime.cgocallbackg1(0x76f2fd667680, 0x7ffd0803b8c0, 0x0)
> /mnt/go/src/runtime/cgocall.go:446 +0x289
> runtime.cgocallbackg(0x76f2fd667680, 0x7ffd0803b8c0, 0x0)
> /mnt/go/src/runtime/cgocall.go:350 +0x132
> runtime.cgocallbackg(0x76f2fd667680, 0x7ffd0803b8c0, 0x0)
> <autogenerated>:1 +0x2b
> runtime.cgocallback(0x0, 0x0, 0x0)
> /mnt/go/src/runtime/asm_amd64.s:1101 +0xcd
> runtime.goexit({})
> /mnt/go/src/runtime/asm_amd64.s:1712 +0x1
2. cgoCheckPointer
When an unpinned Go pointer (or a pointer to an unpinned Go pointer) is
passed to a C function,
> package main
>
> /*
> #include <stdio.h>
> void foo(void *bar) {}
> */
> import "C"
> import "unsafe"
>
> func main() {
> m := map[int]int{0: 1,}
> C.foo(unsafe.Pointer(&m))
> }
This error shows up at runtime:
> panic: runtime error: cgo argument has Go pointer to unpinned Go pointer
>
> goroutine 1 [running]:
> main.main.func1(...)
> /mnt/chexit/cgomap.go:12
> main.main()
> /mnt/chexit/cgomap.go:12 +0x91
> exit status 2
Retrieve callee name and pointer kind; use them in the error message.
> panic: runtime error: cgo argument of function main.main.func1 has Go pointer to unpinned Go map
>
> goroutine 1 [running]:
> main.main.func1(...)
> /mnt/chexit/cgomap.go:12
> main.main()
> /mnt/chexit/cgomap.go:12 +0x9b
> exit status 2
Link: https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers
Suggested-by: Ian Lance Taylor <[email protected]>
Fixes#75856
0 commit comments