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

spec: document safe use of unsafe.Pointer and uintptr conversions #8994

Closed
griesemer opened this issue Oct 24, 2014 · 7 comments
Closed

spec: document safe use of unsafe.Pointer and uintptr conversions #8994

griesemer opened this issue Oct 24, 2014 · 7 comments

Comments

@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 24, 2014

Should be documented outside the spec.

See closed issue #7192 for details.
@griesemer
Copy link
Contributor Author

@griesemer griesemer commented Oct 24, 2014

Comment 1:

Labels changed: added repo-main.

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Oct 24, 2014

Comment 2:

I'm interested in what people think about the test program below (similar to what I
described in issue #7192, comment 12).  "go vet" is happy with it, yet "go run" reliably
panics with "not 42".
Is "go vet" at fault for not warning about this?  Is cmd/gc at fault for not ensuring
that spilled (reflect.Value).Pointer() values are tracked as pointers?  Or is the
developer at fault for writing something tricky that "obviously" isn't supported?
This particular use case is fabricated, but I've seen real world code like "if
v1.Pointer() == v2.Pointer()" that I suspect could spuriously trigger similar failures
if a concurrent goroutine triggers a stop-the-world GC run that interrupts v2.Pointer().
package main
import (
    "reflect"
    "runtime"
    "unsafe"
)
type array [1000]int
func arrayPointer() reflect.Value {
    return reflect.ValueOf(&array{42})
}
func zero() int {
    runtime.GC()
    return 0
}
func main() {
    for i := 0; i < 20; i++ {
        if (*array)(unsafe.Pointer(arrayPointer().Pointer()))[zero()] != 42 {
            panic("not 42")
        }
    }
}
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Oct 24, 2014

Comment 3:

That's a nice example.  The informal rules seem to be something like "you can only
convert unsafe.Pointer to uintptr if you convert back in the same expression."  That
informal rule makes it impossible to convert the result of reflect.Value.Pointer back to
unsafe.Pointer, which makes the method somewhat useless.
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Oct 25, 2014

Comment 5:

Rob: you changed the summary to say "spec", but issue #7192 and
https://golang.org/cl/153110044 seemed to argue that any detailed restrictions
should not be in the spec.
@rsc
Copy link
Contributor

@rsc rsc commented Oct 28, 2014

Comment 6:

Labels changed: added release-go1.4maybe, removed release-go1.4.

@rsc
Copy link
Contributor

@rsc rsc commented Nov 6, 2014

Comment 7:

Labels changed: added release-go1.5, removed release-go1.4maybe.

@gopherbot
Copy link

@gopherbot gopherbot commented Jan 14, 2016

CL https://golang.org/cl/18640 mentions this issue.

@gopherbot gopherbot closed this in 2a09a68 Jan 22, 2016
@golang golang locked and limited conversation to collaborators Jan 23, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.