Skip to content

Conversation

ariel-anieli
Copy link

@ariel-anieli ariel-anieli commented Oct 14, 2025

Hello maintainers,

This patchset contains two commits:

The first commit fixes the description of cgoCheckArg.

The second 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 formats an error message with the caller name)
  • update the cgo pointer tests.

cc @ianlancetaylor, @randall77, @seankhliao
Fixes #75856

@gopherbot
Copy link
Contributor

This PR (HEAD: 74f2fb0) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

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.


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

This PR (HEAD: e723643) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 2:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Keith Randall:

Patch Set 3:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

This PR (HEAD: b5f4ef2) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 3:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 4:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

This PR (HEAD: 405155c) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 4:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@ariel-anieli ariel-anieli changed the title cmd/cgo: improve error messages runtime/cgo: improve error messages after panic Oct 17, 2025
@ariel-anieli ariel-anieli changed the title runtime/cgo: improve error messages after panic runtime/cgo: improve error messages after pointer panic Oct 17, 2025
@gopherbot
Copy link
Contributor

This PR (HEAD: aca5ab4) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 6:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Keith Randall:

Patch Set 7:

(5 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

This PR (HEAD: fe7c70a) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 8:

(6 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 9: Commit-Queue+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 9:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2025-10-19T14:43:18Z","revision":"ac5f861fb7b5c6a86cd39fec1bec0e9bc8292471"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 9: -Commit-Queue


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 9:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 9: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Keith Randall:

Patch Set 9:

(4 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

pt is defined in range, then re-assigned to itself.

Remove it.

For golang#75856
The description is misleading: cgoCheckArg is called by both
cgoCheckPointer and cgoCheckResult.

fix by mentioning cgoCheckResult in the cgoCheckArg description.

As well, remove extra spaces between words.

For golang#75856
This commit 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 formats an error message with the caller name)
- update the cgo pointer tests.

1. cgoCheckResult

When an unpinned Go pointer (or a pointer to an unpinned Go pointer) is
returned from Go to C,

> package main
>
> import (
>         "C"
> )
>
> //export foo
> func foo() map[int]int {
>         return map[int]int{0: 1,}
> }

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({0x78ac36001d40?, 0xc000194000?})
>         /usr/lib/go/src/runtime/panic.go:802 +0x168
> runtime.cgoCheckArg(0x78ac36002f40, 0xc000190000, 0xd8?, 0x0, {0x78ac35fdda62, 0x42})
>         /usr/lib/go/src/runtime/cgocall.go:628 +0x4c5
> runtime.cgoCheckResult({0x78ac36002f40, 0xc000190000})
>         /usr/lib/go/src/runtime/cgocall.go:795 +0x4b
> _cgoexp_1c29bd2c0b96_foo(0x7fffc16d87b0)
>         _cgo_gotypes.go:46 +0x6f
> runtime.cgocallbackg1(0x78ac35fd4d20, 0x7fffc16d87b0, 0x0)
>         /usr/lib/go/src/runtime/cgocall.go:446 +0x289
> runtime.cgocallbackg(0x78ac35fd4d20, 0x7fffc16d87b0, 0x0)
>         /usr/lib/go/src/runtime/cgocall.go:350 +0x132
> runtime.cgocallbackg(0x78ac35fd4d20, 0x7fffc16d87b0, 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_1c29bd2c0b96_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; for instance, a Go map.

Retrieve caller name and pointer kind; use them in the error message:

> panic: runtime error: result of cgo function foo is unpinned Go map or points to unpinned Go map
>
> goroutine 17 [running, locked to thread]:
> panic({0x76f2fd69b3c0?, 0x1fba8c79c000?})
>         /mnt/go/src/runtime/panic.go:877 +0x16f
> runtime.cgoCheckArg(0x76f2fd69c5c0, 0x1fba8c790000, 0x0?, 0x0, 0x1)
>         /mnt/go/src/runtime/cgocall.go:631 +0x499
> runtime.cgoCheckResult({0x76f2fd69c5c0, 0x1fba8c790000})
>         /mnt/go/src/runtime/cgocall.go:798 +0x45
> _cgoexp_1c29bd2c0b96_foo(0x7ffd0803b8c0)
>         _cgo_gotypes.go:46 +0x6f
> runtime.cgocallbackg1(0x76f2fd667680, 0x7ffd0803b8c0, 0x0)
>         /mnt/go/src/runtime/cgocall.go:446 +0x289
> runtime.cgocallbackg(0x76f2fd667680, 0x7ffd0803b8c0, 0x0)
>         /mnt/go/src/runtime/cgocall.go:350 +0x132
> runtime.cgocallbackg(0x76f2fd667680, 0x7ffd0803b8c0, 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 an unpinned Go pointer (or a pointer to an unpinned Go pointer) is
passed to a C function,

> package main
>
> /*
> #include <stdio.h>
> void foo(void *bar) {}
> */
> import "C"
> import "unsafe"
>
> func main() {
>         m := map[int]int{0: 1,}
>         C.foo(unsafe.Pointer(&m))
> }

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/chexit/cgomap.go:12
> main.main()
>         /mnt/chexit/cgomap.go:12 +0x91
> 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 map
>
> goroutine 1 [running]:
> main.main.func1(...)
>         /mnt/chexit/cgomap.go:12
> main.main()
>         /mnt/chexit/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 golang#75856
@gopherbot
Copy link
Contributor

This PR (HEAD: 5e9a432) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 10:

(5 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 10: Commit-Queue+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 10:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2025-10-21T15:22:59Z","revision":"8076a07c332d6780188d44270b3c0bd441b86655"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 10:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 10: -Commit-Queue


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 10:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 10: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 10: Commit-Queue+1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 10:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2025-10-21T15:44:34Z","revision":"8076a07c332d6780188d44270b3c0bd441b86655"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 10: -Commit-Queue


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 10:

This CL has failed the run. Reason:

Tryjob golang/try/gotip-linux-amd64-race has failed with summary (view all results):


To reproduce, try gomote repro 8700386911934221153.

Additional links for debugging:


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 10: LUCI-TryBot-Result-1


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 10:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 10:

(3 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/711801.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

runtime/cgo: improve error message if a Go function called by C returns an unpinned pointer

2 participants