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)
- change the failure pattern in cgo pointer tests.
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"
>
> func main(){
> }
>
> //export foo
> func foo(bar *C.char) string {
> return C.GoString(bar)
> }
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({0x7448b71c85c0?, 0xc00001e050?})
> /usr/lib/go/src/runtime/panic.go:802 +0x168
> runtime.cgoCheckArg(0x7448b71c5f20, 0xc000066e50, 0x0?, 0x0, {0x7448b71a4a62, 0x42})
> /usr/lib/go/src/runtime/cgocall.go:679 +0x35b
> runtime.cgoCheckResult({0x7448b71c5f20, 0xc000066e50})
> /usr/lib/go/src/runtime/cgocall.go:795 +0x4b
> _cgoexp_62fc19d078a9_foo(0x7ffc8f6ca510)
> _cgo_gotypes.go:65 +0x5d
> runtime.cgocallbackg1(0x7448b719b6c0, 0x7ffc8f6ca510, 0x0)
> /usr/lib/go/src/runtime/cgocall.go:446 +0x289
> runtime.cgocallbackg(0x7448b719b6c0, 0x7ffc8f6ca510, 0x0)
> /usr/lib/go/src/runtime/cgocall.go:350 +0x132
> runtime.cgocallbackg(0x7448b719b6c0, 0x7ffc8f6ca510, 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_62fc19d078a9_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.
Retrieve caller name and pointer kind; use them in the error message:
> panic: runtime error: result of cgo function foo is unpinned Go string or points to unpinned Go string
>
> goroutine 17 [running, locked to thread]:
> panic({0x743542d35cc0?, 0x3515a4ae0040?})
> /mnt/go/src/runtime/panic.go:877 +0x16f
> runtime.cgoCheckArg(0x743542d33560, 0x3515a4b3ae50, 0x3?, 0x0, 0x1)
> /mnt/go/src/runtime/cgocall.go:682 +0x325
> runtime.cgoCheckResult({0x743542d33560, 0x3515a4b3ae50})
> /mnt/go/src/runtime/cgocall.go:798 +0x45
> _cgoexp_62fc19d078a9_foo(0x7ffee111c660)
> _cgo_gotypes.go:65 +0x5d
> runtime.cgocallbackg1(0x743542d030e0, 0x7ffee111c660, 0x0)
> /mnt/go/src/runtime/cgocall.go:446 +0x289
> runtime.cgocallbackg(0x743542d030e0, 0x7ffee111c660, 0x0)
> /mnt/go/src/runtime/cgocall.go:350 +0x132
> runtime.cgocallbackg(0x743542d030e0, 0x7ffee111c660, 0x0)
> <autogenerated>:1 +0x2b
> runtime.cgocallback(0x0, 0x0, 0x0)
> /mnt/go/src/runtime/asm_amd64.s:1098 +0xcd
> runtime.goexit({})
> /mnt/go/src/runtime/asm_amd64.s:1709 +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 print_ptr(void *p) {
> printf("Pointer: %p\n", p);
> }
> */
> import "C"
> import "unsafe"
>
> func main() {
> x := 10
> p := &x // p is a Go pointer
> C.print_ptr(unsafe.Pointer(&p)) // passing Go pointer to Go pointer
> }
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/go/src/test.go:15
> main.main()
> /mnt/go/src/test.go:15 +0x79
> 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 pointer
>
> goroutine 1 [running]:
> main.main.func1(...)
> /mnt/go/src/test.go:15
> main.main()
> /mnt/go/src/test.go:15 +0x88
> exit status 2
Link: https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers
Suggested-by: Ian Lance Taylor <iant@golang.org>
Fixes#75856
0 commit comments