Skip to content
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

runtime/cgo: clarify whether Go code can write Go pointers to C memory #65349

Open
mknyszek opened this issue Jan 29, 2024 · 1 comment
Open
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation
Milestone

Comments

@mknyszek
Copy link
Contributor

Before the pointer passing rules (https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers) were updated for pinning, Go code was forbidden from writing Go pointers (including pointers like *C.x) to C memory. With pinning, this is no longer explicitly stated, but only implied by an implementation note at the bottom of the section:

Note: the current implementation has a bug. While Go code is permitted to write nil or a C pointer (but not a Go pointer) to C memory, the current implementation may sometimes cause a runtime error if the contents of the C memory appear to be a Go pointer. Therefore, avoid passing uninitialized C memory to Go code if the Go code is going to store pointer values in it. Zero out the memory in C before passing it to Go.

Given that pinning exists, I don't think it's unreasonable to allow Go code to write Go pointers to C memory, but more thought needs to be given as to what restrictions that places on the GC implementation. (Note that C code can write pinned Go pointers to C memory.)

This is further confused by the fact that this implementation note still applies. (Specifically, it's referring to the deletion part of the write barrier. If there's a Go-heap-pointer-looking value that's being deleted by a write to C memory, then the GC will get confused.)

This issue was created in the context of https://groups.google.com/g/golang-nuts/c/_7bUuHX0ca4.

@mknyszek mknyszek self-assigned this Jan 29, 2024
@mknyszek mknyszek added this to the Backlog milestone Jan 29, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 29, 2024
@ianlancetaylor
Copy link
Contributor

The current docs don't make sense to me, so thanks for opening this issue. I'm not clear on why the paragraph starting with "Go code may not store a Go pointer in C memory" was deleted.

I think you are suggesting that we replace the old rule "Go code may not store a Go pointer in C memory" with a new rule "Go code may only store a Go pointer in C memory if the Go pointer points to pinned memory." As the current docs say, that is only OK if the value in C memory is initialized: it must contain zero, or a C pointer, or a Go pointer to pinned memory. Nothing else is OK. With that proviso, this seems fine to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation
Projects
Development

No branches or pull requests

3 participants