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

cmd/compile: sync/atomic.SwapPointer arguments escape for the wrong reason #15283

aclements opened this Issue Apr 13, 2016 · 2 comments


None yet
7 participants
Copy link

aclements commented Apr 13, 2016

In the implementation of sync/atomic.SwapPointer (which is in runtime/atomic_pointer.go), there's nothing that causes the "new" argument to escape, even though it needs to (because you could be swapping it in to a global). The compiler agrees:

$ go build -gcflags "-m -m" -a runtime |& grep sync_atomic_SwapPointer                                                                     
runtime/atomic_pointer.go:58: sync_atomic_SwapPointer ptr does not escape
runtime/atomic_pointer.go:58: sync_atomic_SwapPointer new does not escape

However, you can't actually tickle this. For example, in principle the following program should sneak the address of stack variable y into a global variable:

package main

import (

var x unsafe.Pointer

func main() {
    var y int
    z := unsafe.Pointer(&y)
    atomic.SwapPointer(&x, z)

But it doesn't!

$ go build -gcflags "-m -m" x.go
# command-line-arguments                        
./aesc.go:13: &x escapes to heap
./aesc.go:13:   from &x (passed to function[unknown]) at ./aesc.go:13
./aesc.go:11: moved to heap: y
./aesc.go:12: &y escapes to heap
./aesc.go:12:   from z (assigned) at ./aesc.go:12
./aesc.go:12:   from z (passed to function[unknown]) at ./aesc.go:12

This happens to work right now, but for the wrong reason: because SwapPointer is defined in the runtime and we use a "go:linkname" comment to expose it from sync/atomic, there's no escape information at all when the test program calls SwapPointer (hence the "passed to function[unknown]"), so the compiler conservatively assumes everything escapes. (In fact, the first argument doesn't have to escape and we have annotations in the runtime to that effect, but they never make it through.)

This all seems very fragile.

/cc @dr2chase @randall77

@bradfitz bradfitz added this to the Go1.7 milestone Apr 14, 2016


This comment has been minimized.

Copy link

rsc commented May 17, 2016

Fragile, but not broken.

@rsc rsc modified the milestones: Go1.8, Go1.7 May 17, 2016

@quentinmit quentinmit added the NeedsFix label Oct 11, 2016

@rsc rsc modified the milestones: Go1.9, Go1.8 Oct 21, 2016

@randall77 randall77 modified the milestones: Go1.10, Go1.9 Jun 6, 2017

@mdempsky mdempsky modified the milestones: Go1.10, Go1.11 Nov 29, 2017


This comment has been minimized.

Copy link

mdempsky commented Nov 29, 2017

Still fragile, but still not broken.

@gopherbot gopherbot modified the milestones: Go1.11, Unplanned May 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.