Skip to content

Commit af3d442

Browse files
committed
cmd/cgo: improve cgoCheckResult error message
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. Retrieve the caller name, and use it in the error message: ``` panic: runtime error: result of cgo function foo is unpinned Go pointer or points to unpinned Go pointer 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 ``` GH: #75856 Cc: Keith Randall <khr@golang.org> Cc: Sean Liao <sean@liao.dev> Suggested-by: Ian Lance Taylor <iant@golang.org>
1 parent 3765758 commit af3d442

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/runtime/cgocall.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,6 @@ func cgoCheckPointer(ptr any, arg any) {
592592
}
593593

594594
const cgoCheckPointerFail = "cgo argument has Go pointer to unpinned Go pointer"
595-
const cgoResultFail = "cgo result is unpinned Go pointer or points to unpinned Go pointer"
596595

597596
// cgoCheckArg is the real work of cgoCheckPointer. The argument p
598597
// is either a pointer to the value (of type t), or the value itself,
@@ -786,10 +785,23 @@ func cgoInRange(p unsafe.Pointer, start, end uintptr) bool {
786785
// exported Go function. It panics if the result is or contains any
787786
// other pointer into unpinned Go memory.
788787
func cgoCheckResult(val any) {
788+
var callerName string = " "
789+
789790
if !goexperiment.CgoCheck2 && debug.cgocheck == 0 {
790791
return
791792
}
792793

794+
pc, _, _, ok := Caller(1)
795+
if ok {
796+
function := FuncForPC(pc)
797+
798+
// Expected format of cgo caller name: _cgoexp_3c910ddb72c4_foo
799+
if function != nil {
800+
callerName += function.Name()[21:] + " "
801+
}
802+
}
803+
804+
cgoResultFail := "result of cgo function" + callerName + "is unpinned Go pointer or points to unpinned Go pointer"
793805
ep := efaceOf(&val)
794806
t := ep._type
795807
cgoCheckArg(t, ep.data, !t.IsDirectIface(), false, cgoResultFail)

0 commit comments

Comments
 (0)