Skip to content

Commit 74f2fb0

Browse files
committed
cmd/cgo: improve cgoCheckPointer error message
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 the callee name, and use it 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 ``` GH: #75856 Cc: Keith Randall <khr@golang.org> Cc: Sean Liao <sean@liao.dev> Cc: Ian Lance Taylor <iant@golang.org>
1 parent af3d442 commit 74f2fb0

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/runtime/cgocall.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,23 @@ var racecgosync uint64 // represents possible synchronization in C code
535535
// cgoCheckPointer checks if the argument contains a Go pointer that
536536
// points to an unpinned Go pointer, and panics if it does.
537537
func cgoCheckPointer(ptr any, arg any) {
538+
var callerName string = " "
539+
538540
if !goexperiment.CgoCheck2 && debug.cgocheck == 0 {
539541
return
540542
}
541543

544+
pc, _, _, ok := Caller(1)
545+
if ok {
546+
function := FuncForPC(pc)
547+
548+
// Expected format of cgo callee name: A.B.C
549+
if function != nil {
550+
callerName += function.Name() + " "
551+
}
552+
}
553+
554+
cgoCheckPointerFail := "cgo argument of function" + callerName + "has Go pointer to unpinned Go pointer"
542555
ep := efaceOf(&ptr)
543556
t := ep._type
544557

@@ -591,8 +604,6 @@ func cgoCheckPointer(ptr any, arg any) {
591604
cgoCheckArg(t, ep.data, !t.IsDirectIface(), top, cgoCheckPointerFail)
592605
}
593606

594-
const cgoCheckPointerFail = "cgo argument has Go pointer to unpinned Go pointer"
595-
596607
// cgoCheckArg is the real work of cgoCheckPointer. The argument p
597608
// is either a pointer to the value (of type t), or the value itself,
598609
// depending on indir. The top parameter is whether we are at the top

0 commit comments

Comments
 (0)