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,cmd}/cgo: improve error messages after panic
This commit improves the error messages returned by cgoCheckResult
and cgoCheckPointer after pointer panics.
Therefore:
- change the signature of cgoCheckArg (it does the real work behind them)
- change the signature of cgoCheckUnknownPointer
- introduce cgoFormatErr (it formats an error message with the caller name)
- update pointer tests.
1. cgoCheckResult
When an unpinned Go pointer is returned to a C function, this error shows up:
> panic: runtime error: cgo result is unpinned Go pointer or points to unpinned Go pointer
>
> goroutine 17 [running, locked to thread]:
> panic({0x78fa15d1e5c0?, 0xc00001e050?})
> /usr/lib/go/src/runtime/panic.go:802 +0x168
> runtime.cgoCheckArg(0x78fa15d1bf20, 0xc000066e50, 0x0?, 0x0, {0x78fa15cfaa62, 0x42})
> /usr/lib/go/src/runtime/cgocall.go:679 +0x35b
> runtime.cgoCheckResult({0x78fa15d1bf20, 0xc000066e50})
> /usr/lib/go/src/runtime/cgocall.go:795 +0x4b
> _cgoexp_1ff3739d54a6_foo(0x7fff7f7b0220)
> _cgo_gotypes.go:65 +0x5d
> runtime.cgocallbackg1(0x78fa15cf16c0, 0x7fff7f7b0220, 0x0)
> /usr/lib/go/src/runtime/cgocall.go:446 +0x289
> runtime.cgocallbackg(0x78fa15cf16c0, 0x7fff7f7b0220, 0x0)
> /usr/lib/go/src/runtime/cgocall.go:350 +0x132
> runtime.cgocallbackg(0x78fa15cf16c0, 0x7fff7f7b0220, 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_1ff3739d54a6_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({0x7feae6c50640?, 0x26fcc9896000?})
> /mnt/go/src/runtime/panic.go:877 +0x16f
> runtime.cgoCheckArg(0x7feae6c4dee0, 0x26fcc9774e50, 0x0?, 0x0, {0x26fcc9894000, 0x52})
> /mnt/go/src/runtime/cgocall.go:678 +0x35b
> runtime.cgoCheckResult({0x7feae6c4dee0, 0x26fcc9774e50})
> /mnt/go/src/runtime/cgocall.go:797 +0x85
> _cgoexp_9ed4cd31b2e5_foo(0x7ffcc89baf10)
> _cgo_gotypes.go:65 +0x5d
> runtime.cgocallbackg1(0x7feae6c1d340, 0x7ffcc89baf10, 0x0)
> /mnt/go/src/runtime/cgocall.go:446 +0x289
> runtime.cgocallbackg(0x7feae6c1d340, 0x7ffcc89baf10, 0x0)
> /mnt/go/src/runtime/cgocall.go:350 +0x132
> runtime.cgocallbackg(0x7feae6c1d340, 0x7ffcc89baf10, 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 is passed to a C function, this error shows up:
> 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
Cc: Keith Randall <khr@golang.org>
Cc: Sean Liao <sean@liao.dev>
Suggested-by: Ian Lance Taylor <iant@golang.org>
GH: #75856
0 commit comments