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: KeepAlive doesn't actually keep stack object alive #30476

Closed
cherrymui opened this issue Feb 28, 2019 · 4 comments

Comments

Projects
None yet
3 participants
@cherrymui
Copy link
Contributor

commented Feb 28, 2019

What version of Go are you using (go version)?

tip (62f5e81)

Does this issue reproduce with the latest release?

Yes with Go 1.12. No with Go 1.11.

What operating system and processor architecture are you using (go env)?

darwin/amd64

What did you do?

package main

import "runtime"

var c = make(chan int)

func main() {
	x := new([10]int)
	runtime.SetFinalizer(x, func(*[10]int){ println("finalizer runs"); c <- 0 })
	p := &T{x, 0} // T{x, 0} is allocated on stack
	use(p)
	runtime.GC()
	runtime.GC()
	runtime.GC()
	runtime.KeepAlive(p)
	<-c
}

type T struct { x *[10]int; y int }

//go:noinline
func use(*T) {}

(no playground link as the playground seems down...)

What did you expect to see?

KeepAlive(p) keeps p alive, which in turn keeps x alive, so the finalizer does not run, and the program deadlocks.

This is the behavior with Go 1.11.

$ go1.11 run keepalive2.go 
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
	/tmp/keepalive2.go:16 +0xb4
exit status 2

This is also the behavior if we use a dummy use call to keep p alive (i.e. the equivalent of the non-intrinsic runtime.KeepAlive).

What did you see instead?

Finalizer runs.

$ go-tip run keepalive2.go 
finalizer runs

This is because the address of the stack object is rematerializeable, and the compiler just ignores KeepAlive on rematerializeable values. I'll send a CL.

@gopherbot

This comment has been minimized.

Copy link

commented Feb 28, 2019

Change https://golang.org/cl/164537 mentions this issue: cmd/compile: make KeepAlive work on stack object

@bcmills bcmills added the NeedsFix label Feb 28, 2019

@bcmills bcmills added this to the Go1.13 milestone Feb 28, 2019

@bcmills

This comment has been minimized.

Copy link
Member

commented Feb 28, 2019

@gopherbot, please backport to Go 1.12: this is a regression in a core runtime behavior.

@gopherbot

This comment has been minimized.

Copy link

commented Feb 28, 2019

Backport issue(s) opened: #30478 (for 1.12).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

@gopherbot

This comment has been minimized.

Copy link

commented Mar 1, 2019

Change https://golang.org/cl/164627 mentions this issue: [release-branch.go1.12] cmd/compile: make KeepAlive work on stack object

gopherbot pushed a commit that referenced this issue Mar 13, 2019

[release-branch.go1.12] cmd/compile: make KeepAlive work on stack object
Currently, runtime.KeepAlive applied on a stack object doesn't
actually keeps the stack object alive, and the heap object
referenced from it could be collected. This is because the
address of the stack object is rematerializeable, and we just
ignored KeepAlive on rematerializeable values. This CL fixes it.

Updates #30476.
Fixes #30478.

Change-Id: Ic1f75ee54ed94ea79bd46a8ddcd9e81d01556d1d
Reviewed-on: https://go-review.googlesource.com/c/164537
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
(cherry picked from commit 40df9cc)
Reviewed-on: https://go-review.googlesource.com/c/go/+/164627
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.