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 pointer panic
This CL 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 is called by cgoCheckArg and
cgoCheckUnknownPointer to format panic error messages)
- update the cgo pointer tests (add new tests, and a field errTextRegexp
to the struct ptrTest)
- remove a loop variable in TestPointerChecks (similar to CL 711640).
1. cgoCheckResult
When an unpinned Go pointer (or a pointer to an unpinned Go pointer) is
returned from Go to C,
$ cat -n parse.go
1 package main
2
3 import (
4 "C"
5 )
6
7 //export foo
8 func foo() map[int]int {
9 return map[int]int{0: 1,}
10 }
11
12 func main() {
13 }
$ cat -n parse.py
1 import os
2 from cffi import FFI
3
4 cffi = FFI()
5 cffi.cdef("char* foo();")
6 dll = cffi.dlopen(os.path.abspath("parse.so"))
7
8 dll.foo()
9 cffi.dlclose(dll)
$ go build -buildmode=c-shared -o parse.so parse.go
$ python parse.py
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({0x7bf1b12ce460?, 0x3c4f1131c000?})
/mnt/go/src/runtime/panic.go:871 +0x16f
runtime.cgoCheckArg(0x7bf1b12cf660, 0x3c4f11318000, 0xd8?, 0x0, {0x7bf1b12a341c, 0x42})
/mnt/go/src/runtime/cgocall.go:628 +0x4c5
runtime.cgoCheckResult({0x7bf1b12cf660, 0x3c4f11318000})
/mnt/go/src/runtime/cgocall.go:795 +0x4b
_cgoexp_1c29bd2c0b96_foo(0x7ffd49d02380)
/mnt/tmp/parse.go:8 +0x6f
runtime.cgocallbackg1(0x7bf1b1299520, 0x7ffd49d02380, 0x0)
/mnt/go/src/runtime/cgocall.go:446 +0x289
runtime.cgocallbackg(0x7bf1b1299520, 0x7ffd49d02380, 0x0)
/mnt/go/src/runtime/cgocall.go:350 +0x132
runtime.cgocallbackg(0x7bf1b1299520, 0x7ffd49d02380, 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
foo is the faulty Go function; it is not obvious to find it in the stack
trace: the stack trace mentions its cgo wrapper (_cgoexp_1c29bd2c0b96_foo).
Moreover the error does not say which kind of pointer caused the panic; for
instance, a Go map.
Retrieve name and file/line of the Go function, as well as the kind of
pointer; use them in the error message:
panic: runtime error: /mnt/tmp/parse.go:8: result of Go function foo called from cgo is unpinned Go map or points to unpinned Go map
goroutine 17 [running, locked to thread]:
panic({0x785a83b15cc0?, 0x7ead553a050?})
/mnt/go/src/runtime/panic.go:871 +0x16f
runtime.cgoCheckArg(0x785a83b16ec0, 0x7ead55c4060, 0x0?, 0x0, 0x1)
/mnt/go/src/runtime/cgocall.go:631 +0x499
runtime.cgoCheckResult({0x785a83b16ec0, 0x7ead55c4060})
/mnt/go/src/runtime/cgocall.go:798 +0x45
_cgoexp_1c29bd2c0b96_foo(0x7ffef3c75230)
/mnt/tmp/parse.go:8 +0x6f
runtime.cgocallbackg1(0x785a83ae1d80, 0x7ffef3c75230, 0x0)
/mnt/go/src/runtime/cgocall.go:446 +0x289
runtime.cgocallbackg(0x785a83ae1d80, 0x7ffef3c75230, 0x0)
/mnt/go/src/runtime/cgocall.go:350 +0x132
runtime.cgocallbackg(0x785a83ae1d80, 0x7ffef3c75230, 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 a pointer to an unpinned Go pointer is passed to a C function,
1 package main
2
3 /*
4 #include <stdio.h>
5 void foo(void *bar) {}
6 */
7 import "C"
8 import "unsafe"
9
10 func main() {
11 m := map[int]int{0: 1,}
12 C.foo(unsafe.Pointer(&m))
13 }
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/tmp/cgomap.go:12
main.main()
/mnt/tmp/cgomap.go:12 +0x91
exit status 2
Retrieve kind of pointer; use it in the error message.
panic: runtime error: argument of cgo function has Go pointer to unpinned Go map
goroutine 1 [running]:
main.main.func1(...)
/mnt/tmp/cgomap.go:12
main.main()
/mnt/tmp/cgomap.go:12 +0x9b
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