[go] cmd/cgo: improve error messages

2 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Oct 14, 2025, 12:44:53 PM (yesterday) Oct 14
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

cmd/cgo: improve error messages

Hello maintainers,

The PR aims to improve error messages in `cgoCheckPointer` and `cgoCheckResult`.

The issue details the problem in `cgoCheckResult`. Let me know if you want me to split the PR for `cgoCheckPointer`.

Here is a reproducer for `cgoCheckPointer`:
```go
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
}
```

And the error:
```# go run src/test.go
panic: runtime error: cgo argument has Go pointer to unpinned Go pointer

goroutine 1 [running]:
main.main.func1(...)
/home/ariel/src/go/src/test.go:15
main.main()
/home/ariel/src/go/src/test.go:15 +0x79
exit status 2
```

Fixes #75856
cc @ianlancetaylor, @randall77, @seankhliao
Change-Id: Ia72f01df016feeae0cddb2558ced51a1b07e4486
GitHub-Last-Rev: 74f2fb0986cb5ab998ec9f0fa56ec1e00cbde44f
GitHub-Pull-Request: golang/go#75894

Change diff

diff --git a/src/runtime/cgocall.go b/src/runtime/cgocall.go
index 18e1dc8..e51696b 100644
--- a/src/runtime/cgocall.go
+++ b/src/runtime/cgocall.go
@@ -535,10 +535,23 @@
// cgoCheckPointer checks if the argument contains a Go pointer that
// points to an unpinned Go pointer, and panics if it does.
func cgoCheckPointer(ptr any, arg any) {
+ var callerName string = " "
+
if !goexperiment.CgoCheck2 && debug.cgocheck == 0 {
return
}

+ pc, _, _, ok := Caller(1)
+ if ok {
+ function := FuncForPC(pc)
+
+ // Expected format of cgo callee name: A.B.C
+ if function != nil {
+ callerName += function.Name() + " "
+ }
+ }
+
+ cgoCheckPointerFail := "cgo argument of function" + callerName + "has Go pointer to unpinned Go pointer"
ep := efaceOf(&ptr)
t := ep._type

@@ -591,9 +604,6 @@
cgoCheckArg(t, ep.data, !t.IsDirectIface(), top, cgoCheckPointerFail)
}

-const cgoCheckPointerFail = "cgo argument has Go pointer to unpinned Go pointer"
-const cgoResultFail = "cgo result is unpinned Go pointer or points to unpinned Go pointer"
-
// cgoCheckArg is the real work of cgoCheckPointer. The argument p
// is either a pointer to the value (of type t), or the value itself,
// depending on indir. The top parameter is whether we are at the top
@@ -786,10 +796,23 @@
// exported Go function. It panics if the result is or contains any
// other pointer into unpinned Go memory.
func cgoCheckResult(val any) {
+ var callerName string = " "
+
if !goexperiment.CgoCheck2 && debug.cgocheck == 0 {
return
}

+ pc, _, _, ok := Caller(1)
+ if ok {
+ function := FuncForPC(pc)
+
+ // Expected format of cgo caller name: _cgoexp_3c910ddb72c4_foo
+ if function != nil {
+ callerName += function.Name()[21:] + " "
+ }
+ }
+
+ cgoResultFail := "result of cgo function" + callerName + "is unpinned Go pointer or points to unpinned Go pointer"
ep := efaceOf(&val)
t := ep._type
cgoCheckArg(t, ep.data, !t.IsDirectIface(), false, cgoResultFail)

Change information

Files:
  • M src/runtime/cgocall.go
Change size: S
Delta: 1 file changed, 26 insertions(+), 3 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia72f01df016feeae0cddb2558ced51a1b07e4486
Gerrit-Change-Number: 711801
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Oct 14, 2025, 12:44:54 PM (yesterday) Oct 14
to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems with your PR:

  1. The commit title should start with the primary affected package name followed by a colon, like "net/http: improve [...]".
2. You have a long 116 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
3. It looks like you are using markdown in the commit message. If so, please remove it. Be sure to double-check the plain text shown in the Gerrit commit message above for any markdown backticks, markdown links, or other markdown formatting.
4. It looks like you have a properly formated bug reference, but the convention is to put bug references at the bottom of the commit message, even if a bug is also mentioned in the body of the message.

Please address any problems by updating the GitHub PR.

When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

For more details, see:

(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia72f01df016feeae0cddb2558ced51a1b07e4486
    Gerrit-Change-Number: 711801
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Tue, 14 Oct 2025 16:44:49 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gopher Robot (Gerrit)

    unread,
    Oct 14, 2025, 12:47:58 PM (yesterday) Oct 14
    to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Message from Gopher Robot

    Congratulations on opening your first change. Thank you for your contribution!

    Next steps:
    A maintainer will review your change and provide feedback. See
    https://go.dev/doc/contribute#review for more info and tips to get your
    patch through code review.

    Most changes in the Go project go through a few rounds of revision. This can be
    surprising to people new to the project. The careful, iterative review process
    is our way of helping mentor contributors and ensuring that their contributions
    have a lasting impact.

    During May-July and Nov-Jan the Go project is in a code freeze, during which
    little code gets reviewed or merged. If a reviewer responds with a comment like
    R=go1.11 or adds a tag like "wait-release", it means that this CL will be
    reviewed as part of the next development cycle. See https://go.dev/s/release
    for more details.

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia72f01df016feeae0cddb2558ced51a1b07e4486
    Gerrit-Change-Number: 711801
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Tue, 14 Oct 2025 16:47:53 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gerrit Bot (Gerrit)

    unread,
    Oct 14, 2025, 3:49:00 PM (yesterday) Oct 14
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Carlos Amedee, Dmitry Vyukov and Ian Lance Taylor

    Gerrit Bot uploaded new patchset

    Gerrit Bot uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Carlos Amedee
    • Dmitry Vyukov
    • Ian Lance Taylor
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia72f01df016feeae0cddb2558ced51a1b07e4486
    Gerrit-Change-Number: 711801
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
    Gerrit-Reviewer: Dmitry Vyukov <dvy...@google.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Dmitry Vyukov <dvy...@google.com>
    Gerrit-Attention: Carlos Amedee <car...@golang.org>
    unsatisfied_requirement
    open
    diffy

    Ariel Otilibili Anieli (Gerrit)

    unread,
    Oct 14, 2025, 3:50:21 PM (yesterday) Oct 14
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Carlos Amedee, Dmitry Vyukov, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Carlos Amedee, Dmitry Vyukov and Ian Lance Taylor

    Ariel Otilibili Anieli added 2 comments

    Patchset-level comments
    File-level comment, Patchset 1:
    Gopher Robot . resolved

    I spotted some possible problems with your PR:

      1. The commit title should start with the primary affected package name followed by a colon, like "net/http: improve [...]".
    2. You have a long 116 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
    3. It looks like you are using markdown in the commit message. If so, please remove it. Be sure to double-check the plain text shown in the Gerrit commit message above for any markdown backticks, markdown links, or other markdown formatting.
    4. It looks like you have a properly formated bug reference, but the convention is to put bug references at the bottom of the commit message, even if a bug is also mentioned in the body of the message.

    Please address any problems by updating the GitHub PR.

    When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

    To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

    For more details, see:

    (In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

    Ariel Otilibili Anieli

    Done

    File-level comment, Patchset 2 (Latest):
    Ariel Otilibili Anieli . resolved

    Improved on feedback, and rebase on master.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Carlos Amedee
    • Dmitry Vyukov
    • Ian Lance Taylor
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia72f01df016feeae0cddb2558ced51a1b07e4486
      Gerrit-Change-Number: 711801
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
      Gerrit-Reviewer: Dmitry Vyukov <dvy...@google.com>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Ariel Otilibili Anieli <ariel.o...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Attention: Dmitry Vyukov <dvy...@google.com>
      Gerrit-Attention: Carlos Amedee <car...@golang.org>
      Gerrit-Comment-Date: Tue, 14 Oct 2025 19:50:12 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Gopher Robot <go...@golang.org>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Ariel Otilibili Anieli (Gerrit)

      unread,
      Oct 14, 2025, 3:57:05 PM (yesterday) Oct 14
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Keith Randall, Carlos Amedee, Dmitry Vyukov, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Carlos Amedee, Dmitry Vyukov, Ian Lance Taylor, Keith Randall and Sean Liao

      Ariel Otilibili Anieli added 1 comment

      Patchset-level comments
      Ariel Otilibili Anieli . resolved

      Cc: Sean and Keith. They were mentioned in the GitHub issue.

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Carlos Amedee
      • Dmitry Vyukov
      • Ian Lance Taylor
      • Keith Randall
      • Sean Liao
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia72f01df016feeae0cddb2558ced51a1b07e4486
      Gerrit-Change-Number: 711801
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
      Gerrit-Reviewer: Dmitry Vyukov <dvy...@google.com>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Reviewer: Keith Randall <k...@google.com>
      Gerrit-Reviewer: Sean Liao <se...@liao.dev>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Sean Liao <se...@liao.dev>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Attention: Dmitry Vyukov <dvy...@google.com>
      Gerrit-Attention: Keith Randall <k...@google.com>
      Gerrit-Attention: Carlos Amedee <car...@golang.org>
      Gerrit-Comment-Date: Tue, 14 Oct 2025 19:56:56 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Gerrit Bot (Gerrit)

      unread,
      Oct 14, 2025, 3:57:11 PM (yesterday) Oct 14
      to Ariel Otilibili Anieli, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Carlos Amedee, Dmitry Vyukov, Ian Lance Taylor, Keith Randall and Sean Liao

      Gerrit Bot uploaded new patchset

      Gerrit Bot uploaded patch set #3 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Carlos Amedee
      • Dmitry Vyukov
      • Ian Lance Taylor
      • Keith Randall
      • Sean Liao
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia72f01df016feeae0cddb2558ced51a1b07e4486
      Gerrit-Change-Number: 711801
      Gerrit-PatchSet: 3
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
      Gerrit-Reviewer: Dmitry Vyukov <dvy...@google.com>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Reviewer: Keith Randall <k...@google.com>
      Gerrit-Reviewer: Sean Liao <se...@liao.dev>
      Gerrit-CC: Ariel Otilibili Anieli <otil...@eurecom.fr>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Keith Randall (Gerrit)

      unread,
      4:27 PM (7 hours ago) 4:27 PM
      to Ariel Otilibili Anieli, Gerrit Bot, goph...@pubsubhelper.golang.org, Keith Randall, Keith Randall, Carlos Amedee, Dmitry Vyukov, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Carlos Amedee, Dmitry Vyukov, Ian Lance Taylor, Keith Randall and Sean Liao

      Keith Randall added 1 comment

      File src/runtime/cgocall.go
      Line 544, Patchset 3 (Latest): pc, _, _, ok := Caller(1)
      Keith Randall . unresolved

      I don't think we want to do this work until we know we will be printing a failure. This looks like it will add an allocations to every `cgoCheckPointer` call.

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Carlos Amedee
      • Dmitry Vyukov
      • Ian Lance Taylor
      • Keith Randall
      • Sean Liao
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia72f01df016feeae0cddb2558ced51a1b07e4486
        Gerrit-Change-Number: 711801
        Gerrit-PatchSet: 3
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
        Gerrit-Reviewer: Dmitry Vyukov <dvy...@google.com>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Reviewer: Keith Randall <k...@google.com>
        Gerrit-Reviewer: Sean Liao <se...@liao.dev>
        Gerrit-CC: Ariel Otilibili Anieli <otil...@eurecom.fr>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Keith Randall <k...@golang.org>
        Gerrit-Attention: Sean Liao <se...@liao.dev>
        Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Attention: Dmitry Vyukov <dvy...@google.com>
        Gerrit-Attention: Keith Randall <k...@google.com>
        Gerrit-Attention: Carlos Amedee <car...@golang.org>
        Gerrit-Comment-Date: Wed, 15 Oct 2025 20:27:12 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        unsatisfied_requirement
        open
        diffy
        Reply all
        Reply to author
        Forward
        0 new messages