-
Notifications
You must be signed in to change notification settings - Fork 18.4k
runtime/cgo: improve error messages after pointer panic #75894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
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:
|
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
74f2fb0 to
e723643
Compare
|
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:
|
|
Message from Ariel Otilibili Anieli: Patch Set 2: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Ariel Otilibili Anieli: Patch Set 2: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Keith Randall: Patch Set 3: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
e723643 to
b5f4ef2
Compare
|
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:
|
|
Message from Ariel Otilibili Anieli: Patch Set 3: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Ariel Otilibili Anieli: Patch Set 4: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
b5f4ef2 to
405155c
Compare
|
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:
|
|
Message from Ariel Otilibili Anieli: Patch Set 4: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
405155c to
aca5ab4
Compare
|
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:
|
|
Message from Ariel Otilibili Anieli: Patch Set 6: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Keith Randall: Patch Set 7: (5 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
aca5ab4 to
fe7c70a
Compare
|
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:
|
|
Message from Ariel Otilibili Anieli: Patch Set 8: (6 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from t hepudds: Patch Set 9: Commit-Queue+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
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. |
|
Message from t hepudds: Patch Set 9: -Commit-Queue Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
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. |
|
Message from Go LUCI: Patch Set 9: LUCI-TryBot-Result+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from t hepudds: Patch Set 10: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Ariel Otilibili Anieli: Patch Set 10: (3 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
5e9a432 to
89d562b
Compare
|
This PR (HEAD: 89d562b) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801. Important tips:
|
|
Message from Keith Randall: Patch Set 12: (2 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
89d562b to
1dcd6a1
Compare
|
This PR (HEAD: 1dcd6a1) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801. Important tips:
|
|
Message from Ariel Otilibili Anieli: Patch Set 13: (3 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from t hepudds: Patch Set 13: (3 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Ariel Otilibili Anieli: Patch Set 13: (4 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from t hepudds: Patch Set 13: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Keith Randall: Patch Set 13: (3 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Keith Randall: Patch Set 13: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
1dcd6a1 to
6c386be
Compare
|
This PR (HEAD: 6c386be) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801. Important tips:
|
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 (add new tests, and a field errTextRegexp
to the struct ptrTest)
- remove an unused assignment in TestPointerChecks.
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 <[email protected]>
Fixes golang#75856
6c386be to
78e1263
Compare
|
Message from Ariel Otilibili Anieli: Patch Set 13: (6 comments) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Ariel Otilibili Anieli: Patch Set 14: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
This PR (HEAD: 78e1263) has been imported to Gerrit for code review. Please visit Gerrit at https://go-review.googlesource.com/c/go/+/711801. Important tips:
|
|
Message from t hepudds: Patch Set 15: Commit-Queue+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Go LUCI: Patch Set 15: Dry run: CV is trying the patch. Bot data: {"action":"start","triggered_at":"2025-10-22T17:33:24Z","revision":"99ca1923991e944dc98195a689ce243d73da1fd7"} Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from t hepudds: Patch Set 15: -Commit-Queue Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Go LUCI: Patch Set 15: This CL has failed the run. Reason: Tryjob golang/try/gotip-linux-amd64_avx512 has failed with summary (view all results):
To reproduce, try Additional links for debugging: Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Go LUCI: Patch Set 15: LUCI-TryBot-Result-1 Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from t hepudds: Patch Set 15: Commit-Queue+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Go LUCI: Patch Set 15: Dry run: CV is trying the patch. Bot data: {"action":"start","triggered_at":"2025-10-22T17:48:01Z","revision":"99ca1923991e944dc98195a689ce243d73da1fd7"} Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from t hepudds: Patch Set 15: -Commit-Queue Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Go LUCI: Patch Set 15: This CL has passed the run Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
|
Message from Go LUCI: Patch Set 15: LUCI-TryBot-Result+1 Please don’t reply on this GitHub thread. Visit golang.org/cl/711801. |
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:
An unpinned Go pointer refers to a memory location that might be moved or
freed by the garbage collector.
Therefore:
cgoCheckResult and cgoCheckPointer)
for checking unexpected pointers)
to the struct ptrTest)
When an unpinned Go pointer (or a pointer to an unpinned Go pointer) is
returned from Go to C,
This error shows up at runtime:
_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:
When an unpinned Go pointer (or a pointer to an unpinned Go pointer) is
passed to a C function,
This error shows up at runtime:
Retrieve callee name and pointer kind; use them in the error message.
Link: https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers
Suggested-by: Ian Lance Taylor [email protected]
Fixes #75856