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
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)
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
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.)
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
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.
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
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:
- [how to update commit messages](https://go.dev/wiki/GerritBot/#how-does-gerritbot-determine-the-final-commit-message) for PRs imported into Gerrit.
- the Go project's [conventions for commit messages](https://go.dev/doc/contribute#commit_messages) that you should follow.
(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.)
Done
Improved on feedback, and rebase on master.
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
pc, _, _, ok := Caller(1)
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.
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |