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: document that KeepAlive doesn't have anything to do with unsafe.Pointer safety rules #47562

Closed
mdempsky opened this issue Aug 5, 2021 · 1 comment · Fixed by ferrmin/go#122
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mdempsky
Copy link
Member

mdempsky commented Aug 5, 2021

There's nothing in the runtime.KeepAlive or unsafe.Pointer documentation to suggest they interact, but I've seen experienced Go engineers mistakenly interpret that runtime.KeepAlive can be used as a quick-fix for working around misuse of unsafe.Pointer.

This often happens to work today, but it's not guaranteed to be safe. In particular, misuse of unsafe.Pointer can lead to objects being allocated on the stack, which can result in unintended program behavior due to memory corruption: https://play.golang.org/p/s2G90hvQUOZ

We don't normally document what things aren't used for, but this seems like a subtle enough point to call out, IMO.

@gopherbot
Copy link

Change https://golang.org/cl/340269 mentions this issue: runtime: warn that KeepAlive is not an unsafe.Pointer workaround

@dmitshur dmitshur added the NeedsFix The path to resolution is known, but the work has not been done. label Aug 5, 2021
@dmitshur dmitshur added this to the Go1.17 milestone Aug 5, 2021
@golang golang locked and limited conversation to collaborators Aug 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants