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: go1.11beta1 for loop with multiple conditions goes into infinite loop #26194

Closed
bbrks opened this issue Jul 3, 2018 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Milestone

Comments

@bbrks
Copy link

bbrks commented Jul 3, 2018

I have produced a minimal example of a bug seen when running under go1.11beta1 which causes for loops to go into an infinite loop when the condition contains multiple statements.

This seems to be due to an over-aggressive compiler optimisation, as the issue does not occur if I run with -gcflags '-N -l'

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

13:00 $ go1.11beta1 version
go version go1.11beta1 darwin/amd64

Does this issue reproduce with the latest release?

  • go 1.11beta1 - yes
  • go 1.11beta1 with -gcflags '-N -l' - no
  • go 1.10.2 - no

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

12:57 $ go1.11beta1 env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/benbrooks/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/benbrooks/dev/go"
GOPROXY=""
GORACE=""
GOROOT="/Users/benbrooks/sdk/go1.11beta1"
GOTMPDIR=""
GOTOOLDIR="/Users/benbrooks/sdk/go1.11beta1/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/00/8p7hkgx9061b1msn2gmgn9bw0000gn/T/go-build153122456=/tmp/go-build -gno-record-gcc-switches -fno-common"
VGOMODROOT=""

What did you do?

Run this under both 1.10.2 and 1.11beta1 and compare.

https://play.golang.org/p/pdNCtWFnsGj

What did you expect to see?

The loop stop on the third iteration.

What did you see instead?

The loop never finishing.

screen shot 2018-07-03 at 12 41 42

If it's of any use, here's a diff of the generated instructions when building with and without -gcflags '-N -l' under go1.11beta1

13:32 $ diff -w -d --suppress-common-lines default-main.asm noopt-main.asm
4,40c4,36
<   main.go:3             0x104d94d               764b                    JBE 0x104d99a
<   main.go:3             0x104d94f               4883ec18                SUBQ $0x18, SP
<   main.go:3             0x104d953               48896c2410              MOVQ BP, 0x10(SP)
<   main.go:3             0x104d958               488d6c2410              LEAQ 0x10(SP), BP
<   main.go:3             0x104d95d               31c0                    XORL AX, AX
<   main.go:6             0x104d95f               eb2a                    JMP 0x104d98b
<   main.go:6             0x104d961               4889442408              MOVQ AX, 0x8(SP)
<   main.go:7             0x104d966               e8a549fdff              CALL runtime.printlock(SB)
<   main.go:7             0x104d96b               488b442408              MOVQ 0x8(SP), AX
<   main.go:7             0x104d970               48890424                MOVQ AX, 0(SP)
<   main.go:7             0x104d974               e80751fdff              CALL runtime.printint(SB)
<   main.go:7             0x104d979               e8124cfdff              CALL runtime.printnl(SB)
<   main.go:7             0x104d97e               e80d4afdff              CALL runtime.printunlock(SB)
<   main.go:6             0x104d983               488b442408              MOVQ 0x8(SP), AX
<   main.go:6             0x104d988               48ffc0                  INCQ AX
<   main.go:6             0x104d98b               4885c0                  TESTQ AX, AX
<   main.go:6             0x104d98e               7dd1                    JGE 0x104d961
<   main.go:6             0x104d990               488b6c2410              MOVQ 0x10(SP), BP
<   main.go:6             0x104d995               4883c418                ADDQ $0x18, SP
<   main.go:6             0x104d999               c3                      RET
<   main.go:3             0x104d99a               e82189ffff              CALL runtime.morestack_noctxt(SB)
<   main.go:3             0x104d99f               eb9f                    JMP main.main(SB)
<   :-1                   0x104d9a1               cc                      INT $0x3
<   :-1                   0x104d9a2               cc                      INT $0x3
<   :-1                   0x104d9a3               cc                      INT $0x3
<   :-1                   0x104d9a4               cc                      INT $0x3
<   :-1                   0x104d9a5               cc                      INT $0x3
<   :-1                   0x104d9a6               cc                      INT $0x3
<   :-1                   0x104d9a7               cc                      INT $0x3
<   :-1                   0x104d9a8               cc                      INT $0x3
<   :-1                   0x104d9a9               cc                      INT $0x3
<   :-1                   0x104d9aa               cc                      INT $0x3
<   :-1                   0x104d9ab               cc                      INT $0x3
<   :-1                   0x104d9ac               cc                      INT $0x3
<   :-1                   0x104d9ad               cc                      INT $0x3
<   :-1                   0x104d9ae               cc                      INT $0x3
<   :-1                   0x104d9af               cc                      INT $0x3
---
>   main.go:3             0x104d94d               7676                    JBE 0x104d9c5
>   main.go:3             0x104d94f               4883ec20                SUBQ $0x20, SP
>   main.go:3             0x104d953               48896c2418              MOVQ BP, 0x18(SP)
>   main.go:3             0x104d958               488d6c2418              LEAQ 0x18(SP), BP
>   main.go:4             0x104d95d               48c744240803000000      MOVQ $0x3, 0x8(SP)
>   main.go:6             0x104d966               48c744241000000000      MOVQ $0x0, 0x10(SP)
>   main.go:6             0x104d96f               eb00                    JMP 0x104d971
>   main.go:6             0x104d971               48837c241000            CMPQ $0x0, 0x10(SP)
>   main.go:6             0x104d977               7d02                    JGE 0x104d97b
>   main.go:6             0x104d979               eb48                    JMP 0x104d9c3
>   main.go:6             0x104d97b               488b442408              MOVQ 0x8(SP), AX
>   main.go:6             0x104d980               4839442410              CMPQ AX, 0x10(SP)
>   main.go:6             0x104d985               7c02                    JL 0x104d989
>   main.go:6             0x104d987               eb2e                    JMP 0x104d9b7
>   main.go:7             0x104d989               e88249fdff              CALL runtime.printlock(SB)
>   main.go:7             0x104d98e               488b442410              MOVQ 0x10(SP), AX
>   main.go:7             0x104d993               48890424                MOVQ AX, 0(SP)
>   main.go:7             0x104d997               e8e450fdff              CALL runtime.printint(SB)
>   main.go:7             0x104d99c               e8ef4bfdff              CALL runtime.printnl(SB)
>   main.go:7             0x104d9a1               e8ea49fdff              CALL runtime.printunlock(SB)
>   main.go:7             0x104d9a6               eb00                    JMP 0x104d9a8
>   main.go:6             0x104d9a8               488b442410              MOVQ 0x10(SP), AX
>   main.go:6             0x104d9ad               48ffc0                  INCQ AX
>   main.go:6             0x104d9b0               4889442410              MOVQ AX, 0x10(SP)
>   main.go:6             0x104d9b5               ebba                    JMP 0x104d971
>   main.go:6             0x104d9b7               eb00                    JMP 0x104d9b9
>   main.go:6             0x104d9b9               488b6c2418              MOVQ 0x18(SP), BP
>   main.go:6             0x104d9be               4883c420                ADDQ $0x20, SP
>   main.go:6             0x104d9c2               c3                      RET
>   main.go:6             0x104d9c3               ebf4                    JMP 0x104d9b9
>   main.go:3             0x104d9c5               e8f688ffff              CALL runtime.morestack_noctxt(SB)
>   main.go:3             0x104d9ca               e971ffffff              JMP main.main(SB)
>   :-1                   0x104d9cf               cc                      INT $0x3
@ianlancetaylor ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker labels Jul 3, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Jul 3, 2018
@ianlancetaylor
Copy link
Contributor

CC @randall77

May be a dup of #26116.

@randall77
Copy link
Contributor

Yes, this is a dup of #26116. My CL for that issue also fixes this one.

@golang golang locked and limited conversation to collaborators Jul 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Projects
None yet
Development

No branches or pull requests

4 participants