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: incorrect evaluation order of select statement clauses #43111

Closed
mdempsky opened this issue Dec 10, 2020 · 2 comments
Closed

cmd/compile: incorrect evaluation order of select statement clauses #43111

mdempsky opened this issue Dec 10, 2020 · 2 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.

Comments

@mdempsky
Copy link
Member

mdempsky commented Dec 10, 2020

The program below should run and terminate successfully. It does so when compiled with gccgo, but it fatal errors due to deadlock when compiled with cmd/compile.

package main

var ch = make(chan int)

func f() chan int {
	close(ch)
	ch = nil
	return nil
}

func main() {
	select {
	case <-ch:
	case f() <- 0:
	}
}

The issue is this code responsible for ordering receive clauses within select statements:

r.Right.Left = o.expr(r.Right.Left, nil)
if r.Right.Left.Op != ONAME {
r.Right.Left = o.copyExpr(r.Right.Left, r.Right.Left.Type, false)
}

It should actually be checking IsAutoTmp like the code responsible for ordering send clauses:

r.Left = o.expr(r.Left, nil)
if !r.Left.IsAutoTmp() {
r.Left = o.copyExpr(r.Left, r.Left.Type, false)
}
r.Right = o.expr(r.Right, nil)
if !r.Right.IsAutoTmp() {
r.Right = o.copyExpr(r.Right, r.Right.Type, false)
}

Discovered with @cuonglm.

@mdempsky mdempsky added the NeedsFix The path to resolution is known, but the work has not been done. label Dec 10, 2020
@cuonglm
Copy link
Member

cuonglm commented Dec 10, 2020

I fixed in https://go-review.googlesource.com/c/go/+/276132/3, not sure this is qualified to fix in go 1.16. Willing to make the fix to master if so.

@mdempsky mdempsky self-assigned this Dec 10, 2020
@gopherbot
Copy link

Change https://golang.org/cl/277132 mentions this issue: cmd/compile: fix select statement evaluation order corner case

@golang golang locked and limited conversation to collaborators Dec 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants