You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.)
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.
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: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.
The text was updated successfully, but these errors were encountered: