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: panic during sccp while compiling main runtime error: index out of range [-5] #64826

Closed
tchung1118 opened this issue Dec 20, 2023 · 4 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Milestone

Comments

@tchung1118
Copy link

Go version

go version go1.22rc1 X:nocoverageredesign darwin/arm64

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

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/taiwon/Library/Caches/go-build'
GOENV='/Users/taiwon/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT='nocoverageredesign'
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/taiwon/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/taiwon/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/taiwon/go/src/github.com/golang/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/taiwon/go/src/github.com/golang/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22rc1 X:nocoverageredesign'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/taiwon/go/src/github.com/tchung1118/test-app/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/t0/jhcv48q51ydbyzqn85y8rsgm0000gn/T/go-build3057666933=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

https://go.dev/play/p/tuaTn9Ke1VG?v=gotip

What did you expect to see?

I expect the program to compile

What did you see instead?

./prog.go:123:2: internal compiler error: 'main': panic during sccp while compiling main:

runtime error: index out of range [-5]

goroutine 9 [running]:
cmd/compile/internal/ssa.Compile.func1()
	/usr/local/go/src/cmd/compile/internal/ssa/compile.go:49 +0x6c
panic({0xdb62e0?, 0xc00002b638?})
	/usr/local/go/src/runtime/panic.go:770 +0x132
cmd/compile/internal/ssa.(*Block).swapSuccessorsByIdx(...)
	/usr/local/go/src/cmd/compile/internal/ssa/block.go:334
cmd/compile/internal/ssa.rewireSuccessor(0xc00062aac0, 0xc0006630b0?)
	/usr/local/go/src/cmd/compile/internal/ssa/sccp.go:538 +0x20d
cmd/compile/internal/ssa.(*worklist).replaceConst(...)
	/usr/local/go/src/cmd/compile/internal/ssa/sccp.go:568
cmd/compile/internal/ssa.sccp(0xc00048e9c0)
	/usr/local/go/src/cmd/compile/internal/ssa/sccp.go:118 +0xc95
cmd/compile/internal/ssa.Compile(0xc00048e9c0)
	/usr/local/go/src/cmd/compile/internal/ssa/compile.go:97 +0xa44
cmd/compile/internal/ssagen.buildssa(0xc000448480, 0x3)
	/usr/local/go/src/cmd/compile/internal/ssagen/ssa.go:575 +0x2869
cmd/compile/internal/ssagen.Compile(0xc000448480, 0x3)
	/usr/local/go/src/cmd/compile/internal/ssagen/pgen.go:216 +0x3e
cmd/compile/internal/gc.compileFunctions.func5.1(0x45215d?)
	/usr/local/go/src/cmd/compile/internal/gc/compile.go:182 +0x34
cmd/compile/internal/gc.compileFunctions.func3.1()
	/usr/local/go/src/cmd/compile/internal/gc/compile.go:164 +0x30
created by cmd/compile/internal/gc.compileFunctions.func3 in goroutine 8
	/usr/local/go/src/cmd/compile/internal/gc/compile.go:163 +0x247



goroutine 9 [running]:
runtime/debug.Stack()
	/usr/local/go/src/runtime/debug/stack.go:24 +0x5e
cmd/compile/internal/base.FatalfAt({0x667690?, 0xc0?}, {0xc0004cf680, 0x32}, {0xc0006431d0, 0x5, 0x5})
	/usr/local/go/src/cmd/compile/internal/base/print.go:225 +0x1d7
cmd/compile/internal/base.Fatalf(...)
	/usr/local/go/src/cmd/compile/internal/base/print.go:194
cmd/compile/internal/ssagen.(*ssafn).Fatalf(0x0?, {0x0?, 0x0?}, {0xe155e5, 0x2c}, {0xc000647880, 0x4, 0x4000?})
	/usr/local/go/src/cmd/compile/internal/ssagen/ssa.go:8225 +0x16a
cmd/compile/internal/ssa.(*Func).Fatalf(0xc00048e9c0, {0xe155e5, 0x2c}, {0xc000647880, 0x4, 0x4})
	/usr/local/go/src/cmd/compile/internal/ssa/func.go:741 +0x279
cmd/compile/internal/ssa.Compile.func1()
	/usr/local/go/src/cmd/compile/internal/ssa/compile.go:54 +0x1a7
panic({0xdb62e0?, 0xc00002b638?})
	/usr/local/go/src/runtime/panic.go:770 +0x132
cmd/compile/internal/ssa.(*Block).swapSuccessorsByIdx(...)
	/usr/local/go/src/cmd/compile/internal/ssa/block.go:334
cmd/compile/internal/ssa.rewireSuccessor(0xc00062aac0, 0xc0006630b0?)
	/usr/local/go/src/cmd/compile/internal/ssa/sccp.go:538 +0x20d
cmd/compile/internal/ssa.(*worklist).replaceConst(...)
	/usr/local/go/src/cmd/compile/internal/ssa/sccp.go:568
cmd/compile/internal/ssa.sccp(0xc00048e9c0)
	/usr/local/go/src/cmd/compile/internal/ssa/sccp.go:118 +0xc95
cmd/compile/internal/ssa.Compile(0xc00048e9c0)
	/usr/local/go/src/cmd/compile/internal/ssa/compile.go:97 +0xa44
cmd/compile/internal/ssagen.buildssa(0xc000448480, 0x3)
	/usr/local/go/src/cmd/compile/internal/ssagen/ssa.go:575 +0x2869
cmd/compile/internal/ssagen.Compile(0xc000448480, 0x3)
	/usr/local/go/src/cmd/compile/internal/ssagen/pgen.go:216 +0x3e
cmd/compile/internal/gc.compileFunctions.func5.1(0x45215d?)
	/usr/local/go/src/cmd/compile/internal/gc/compile.go:182 +0x34
cmd/compile/internal/gc.compileFunctions.func3.1()
	/usr/local/go/src/cmd/compile/internal/gc/compile.go:164 +0x30
created by cmd/compile/internal/gc.compileFunctions.func3 in goroutine 8
	/usr/local/go/src/cmd/compile/internal/gc/compile.go:163 +0x247


Go build failed.
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Dec 20, 2023
@randall77
Copy link
Contributor

This looks kinda like #64606, but I don't think it is directly related.

I think what is going on here is that sccp is trying to optimize unreachable code. That unreachable code fails the invariant that jump table indexes are in range. I'm not entirely sure whether some previous phase is generating bad unreachable jump tables, or whether we just need to defend against unreachable code. I'll look some more.

@randall77
Copy link
Contributor

(The unreachable code is the jump table in the body of NewNotificationFromString inlined into main at the last callsite in main. It is unreachable because we compare the string length and it is shorter than any string in the case statement.)

@randall77
Copy link
Contributor

Here's a simpler reproducer:

package main

func main() {
	f(g(false))
}
func g(b bool) string {
	if b {
		return "z"
	}
	return "q"
}
func f(x string) int {
	switch len(x) {
	case 4:
		return 4
	case 5:
		return 5
	case 6:
		return 6
	case 7:
		return 7
	case 8:
		return 8
	case 9:
		return 9
	case 10:
		return 10
	case 11:
		return 11
	}
	return 0
}

@randall77 randall77 added this to the Go1.22 milestone Dec 20, 2023
@randall77 randall77 self-assigned this Dec 20, 2023
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/552055 mentions this issue: cmd/compile: handle constant-folding of an out-of-range jump table index

ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
The input index to a jump table can be out of range for unreachable code.

Dynamically the compiler ensures that an out-of-range index can never
reach a jump table, but that guarantee doesn't extend to the static
realm.

Fixes golang#64826

Change-Id: I5829f3933ae5124ffad8337dfd7dd75e67a8ec33
Reviewed-on: https://go-review.googlesource.com/c/go/+/552055
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants