Skip to content

Conversation

@ariel-anieli
Copy link
Contributor

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

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

 1  package main
 2
 3  import (
 4          "C"
 5  )
 6
 7  //export foo
 8  func foo(CLine *C.char) string {
 9          return C.GoString(CLine)
10  }
11
12
13  func main() {
14  }

The error message mentions the details of the cgo wrapper,

panic: runtime error: cgo result is unpinned Go pointer or points to unpinned Go pointer

goroutine 17 [running, locked to thread]:
panic({0x798f2341a4c0?, 0xc000112000?})
        /usr/lib/go/src/runtime/panic.go:802 +0x168
runtime.cgoCheckArg(0x798f23417e20, 0xc000066e50, 0x0?, 0x0, {0x798f233f5a62, 0x42})
        /usr/lib/go/src/runtime/cgocall.go:679 +0x35b
runtime.cgoCheckResult({0x798f23417e20, 0xc000066e50})
        /usr/lib/go/src/runtime/cgocall.go:795 +0x4b
_cgoexp_3c910ddb72c4_foo(0x7ffc9fa9bfa0)
        _cgo_gotypes.go:65 +0x5d
runtime.cgocallbackg1(0x798f233ec780, 0x7ffc9fa9bfa0, 0x0)
        /usr/lib/go/src/runtime/cgocall.go:446 +0x289
runtime.cgocallbackg(0x798f233ec780, 0x7ffc9fa9bfa0, 0x0)
        /usr/lib/go/src/runtime/cgocall.go:350 +0x132
runtime.cgocallbackg(0x798f233ec780, 0x7ffc9fa9bfa0, 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

The cgo wrapper (_cgoexp_3c910ddb72c4_foo) is located in a temporary
build artifact (_cgo_gotypes.go)

$ go tool cgo -objdir objdir parse.go
$ cat -n objdir/_cgo_gotypes.go | sed -n '55,70p'
    55  //go:cgo_export_dynamic foo
    56  //go:linkname _cgoexp_d48770e267d1_foo _cgoexp_d48770e267d1_foo
    57  //go:cgo_export_static _cgoexp_d48770e267d1_foo
    58  func _cgoexp_d48770e267d1_foo(a *struct {
    59                  p0 *_Ctype_char
    60                  r0 string
    61          }) {
    62          a.r0 = foo(a.p0)
    63          _cgoCheckResult(a.r0)
    64  }

The details of the export'ed function are expected in the error message.

Use them in error messages.

panic: runtime error: cgo result is unpinned Go pointer or points to unpinned Go pointer

goroutine 17 [running, locked to thread]:
panic({0x7df72b1d8ae0?, 0x3ec8a1790030?})
        /mnt/go/src/runtime/panic.go:877 +0x16f
runtime.cgoCheckArg(0x7df72b1d62c0, 0x3ec8a16eee50, 0x68?, 0x0, {0x7df72b1ad44c, 0x42})
        /mnt/go/src/runtime/cgocall.go:679 +0x35b
runtime.cgoCheckResult({0x7df72b1d62c0, 0x3ec8a16eee50})
        /mnt/go/src/runtime/cgocall.go:795 +0x4b
_cgoexp_3c910ddb72c4_foo(0x7ffca1b21020)
        /mnt/tmp/parse.go:8 +0x5d
runtime.cgocallbackg1(0x7df72b1a4360, 0x7ffca1b21020, 0x0)
        /mnt/go/src/runtime/cgocall.go:446 +0x289
runtime.cgocallbackg(0x7df72b1a4360, 0x7ffca1b21020, 0x0)
        /mnt/go/src/runtime/cgocall.go:350 +0x132
runtime.cgocallbackg(0x7df72b1a4360, 0x7ffca1b21020, 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

Link: https://web.archive.org/web/20251008114504/https://dave.cheney.net/2018/01/08/gos-hidden-pragmas
Suggested-by: Keith Randall [email protected]
For #75856

@gopherbot
Copy link
Contributor

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

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

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/716441.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Ariel Otilibili Anieli:

Patch Set 1:

(2 comments)


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

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 1: Commit-Queue+1


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

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 1:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2025-10-30T13:06:19Z","revision":"5eb0a3d78893485106a8837e76486d73857acc9d"}


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

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 1: -Commit-Queue


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

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 1:

This CL has passed the run


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

@gopherbot
Copy link
Contributor

Message from Go LUCI:

Patch Set 1: LUCI-TryBot-Result+1


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

@gopherbot
Copy link
Contributor

Message from t hepudds:

Patch Set 1:

(1 comment)


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

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

     1  package main
     2
     3  import (
     4          "C"
     5  )
     6
     7  //export foo
     8  func foo(CLine *C.char) string {
     9          return C.GoString(CLine)
    10  }
    11
    12
    13  func main() {
    14  }

The error message mentions the details of the cgo wrapper,

    panic: runtime error: cgo result is unpinned Go pointer or points to unpinned Go pointer

    goroutine 17 [running, locked to thread]:
    panic({0x798f2341a4c0?, 0xc000112000?})
            /usr/lib/go/src/runtime/panic.go:802 +0x168
    runtime.cgoCheckArg(0x798f23417e20, 0xc000066e50, 0x0?, 0x0, {0x798f233f5a62, 0x42})
            /usr/lib/go/src/runtime/cgocall.go:679 +0x35b
    runtime.cgoCheckResult({0x798f23417e20, 0xc000066e50})
            /usr/lib/go/src/runtime/cgocall.go:795 +0x4b
    _cgoexp_3c910ddb72c4_foo(0x7ffc9fa9bfa0)
            _cgo_gotypes.go:65 +0x5d
    runtime.cgocallbackg1(0x798f233ec780, 0x7ffc9fa9bfa0, 0x0)
            /usr/lib/go/src/runtime/cgocall.go:446 +0x289
    runtime.cgocallbackg(0x798f233ec780, 0x7ffc9fa9bfa0, 0x0)
            /usr/lib/go/src/runtime/cgocall.go:350 +0x132
    runtime.cgocallbackg(0x798f233ec780, 0x7ffc9fa9bfa0, 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

The cgo wrapper (_cgoexp_3c910ddb72c4_foo) is located in a temporary
build artifact (_cgo_gotypes.go)

    $ go tool cgo -objdir objdir parse.go
    $ cat -n objdir/_cgo_gotypes.go | sed -n '55,70p'
        55  //go:cgo_export_dynamic foo
        56  //go:linkname _cgoexp_d48770e267d1_foo _cgoexp_d48770e267d1_foo
        57  //go:cgo_export_static _cgoexp_d48770e267d1_foo
        58  func _cgoexp_d48770e267d1_foo(a *struct {
        59                  p0 *_Ctype_char
        60                  r0 string
        61          }) {
        62          a.r0 = foo(a.p0)
        63          _cgoCheckResult(a.r0)
        64  }

The details of the export'ed function are expected in the error message.

Use them in error messages.

    panic: runtime error: cgo result is unpinned Go pointer or points to unpinned Go pointer

    goroutine 17 [running, locked to thread]:
    panic({0x7df72b1d8ae0?, 0x3ec8a1790030?})
            /mnt/go/src/runtime/panic.go:877 +0x16f
    runtime.cgoCheckArg(0x7df72b1d62c0, 0x3ec8a16eee50, 0x68?, 0x0, {0x7df72b1ad44c, 0x42})
            /mnt/go/src/runtime/cgocall.go:679 +0x35b
    runtime.cgoCheckResult({0x7df72b1d62c0, 0x3ec8a16eee50})
            /mnt/go/src/runtime/cgocall.go:795 +0x4b
    _cgoexp_3c910ddb72c4_foo(0x7ffca1b21020)
            /mnt/tmp/parse.go:8 +0x5d
    runtime.cgocallbackg1(0x7df72b1a4360, 0x7ffca1b21020, 0x0)
            /mnt/go/src/runtime/cgocall.go:446 +0x289
    runtime.cgocallbackg(0x7df72b1a4360, 0x7ffca1b21020, 0x0)
            /mnt/go/src/runtime/cgocall.go:350 +0x132
    runtime.cgocallbackg(0x7df72b1a4360, 0x7ffca1b21020, 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

Link: https://web.archive.org/web/20251008114504/https://dave.cheney.net/2018/01/08/gos-hidden-pragmas
Suggested-by: Keith Randall <[email protected]>
For golang#75856
@ariel-anieli ariel-anieli force-pushed the cgo-export-orig-func-name branch from 956aa4b to eb376d9 Compare October 30, 2025 14:39
@gopherbot
Copy link
Contributor

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

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

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 1:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/716441.
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.

2 participants