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: out of range integer in range loop crashes the compiler #65133

Closed
aykevl opened this issue Jan 17, 2024 · 6 comments
Closed

cmd/compile: out of range integer in range loop crashes the compiler #65133

aykevl opened this issue Jan 17, 2024 · 6 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@aykevl
Copy link

aykevl commented Jan 17, 2024

Go version

go version devel go1.22-8e658eee9c Wed Jan 17 03:56:30 2024 +0000 linux/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/home/ayke/.cache/go-build'
GOENV='/home/ayke/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/ayke/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/ayke'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/ayke/src/tinygo/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/ayke/src/tinygo/go/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='devel go1.22-8e658eee9c Wed Jan 17 03:56:30 2024 +0000'
GCCGO='gccgo'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
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 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2139171962=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I ran the following program in Go 1.22 (tip):

package main

func main() {
	for range 1 << 64 {
	}
}

On the Playground: https://go.dev/play/p/MCs2ntWII_I?v=gotip

What did you see happen?

The compiler crashes with the following stack trace:

# command-line-arguments
./test.go:4:6: internal compiler error: 18446744073709551616 out of range for uint

goroutine 21 [running]:
runtime/debug.Stack()
        ./src/runtime/debug/stack.go:24 +0x64
cmd/compile/internal/base.FatalfAt({0xabbaf8?, 0x0?}, {0x95b435, 0x16}, {0x40004ee6f8, 0x2, 0x2})
        ./src/cmd/compile/internal/base/print.go:225 +0x1fc
cmd/compile/internal/base.Fatalf(...)
        ./src/cmd/compile/internal/base/print.go:194
cmd/compile/internal/ir.IntVal(0x400047e480, {0xabbaf8?, 0x40000dea60?})
        ./src/cmd/compile/internal/ir/val.go:33 +0x1a0
cmd/compile/internal/ssagen.(*state).exprCheckPtr(0x4000210300, {0xac4818, 0x40004d0730}, 0x1)
        ./src/cmd/compile/internal/ssagen/ssa.go:2826 +0x9fc
cmd/compile/internal/ssagen.(*state).expr(...)
        ./src/cmd/compile/internal/ssagen/ssa.go:2756
cmd/compile/internal/ssagen.(*state).stmt(0x4000210300, {0xac4158, 0x40004d0d70})
        ./src/cmd/compile/internal/ssagen/ssa.go:1679 +0x5ce8
cmd/compile/internal/ssagen.(*state).stmtList(...)
        ./src/cmd/compile/internal/ssagen/ssa.go:1426
cmd/compile/internal/ssagen.(*state).stmt(0x4000210300, {0xac42d8, 0x40000d50a0})
        ./src/cmd/compile/internal/ssagen/ssa.go:1441 +0x210
cmd/compile/internal/ssagen.(*state).stmtList(...)
        ./src/cmd/compile/internal/ssagen/ssa.go:1426
cmd/compile/internal/ssagen.buildssa(0x40004c8fc0, 0x3)
        ./src/cmd/compile/internal/ssagen/ssa.go:555 +0x1f34
cmd/compile/internal/ssagen.Compile(0x40004c8fc0, 0x3)
        ./src/cmd/compile/internal/ssagen/pgen.go:216 +0x30
cmd/compile/internal/gc.compileFunctions.func5.1(0x40004d2080?)
        ./src/cmd/compile/internal/gc/compile.go:182 +0x3c
cmd/compile/internal/gc.compileFunctions.func3.1()
        ./src/cmd/compile/internal/gc/compile.go:164 +0x3c
created by cmd/compile/internal/gc.compileFunctions.func3 in goroutine 20
        ./src/cmd/compile/internal/gc/compile.go:163 +0x1e0

What did you expect to see?

This code should probably show a regular compiler error because the integer is out of range.

This is what the specification says:

For an integer value n, the iteration values 0 through n-1 are produced in increasing order, with the same type as n. If n <= 0, the loop does not run any iterations.

This doesn't explicitly say which type should be used for untyped integers. My assumption would be that it would be the same as this for example:

n := 5

...meaning that it would default to int (not uint as the stack trace suggests). uint would also seem reasonable to me for range loops, which would be a bit inconsistent but would make sense for range loops.

In fact, the following code does compile, but I'm not sure it should (it is out of range for int):

package main

func main() {
	for range 1 << 63 {
	}
}
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 17, 2024
@mauri870 mauri870 added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 17, 2024
@mauri870
Copy link
Member

cc @golang/compiler

@cuonglm
Copy link
Member

cuonglm commented Jan 17, 2024

Related: #62466

types2 leaves 1 << 64 as untyped constant, then cmd/compile set idea type to uint. However, cmd/compile relies on types2 for reporting any overflow error mandated by the spec, causing this ICE.

cc @griesemer @mdempsky

@aykevl
Copy link
Author

aykevl commented Jan 17, 2024

Just checked golang.org/x/tools/go/ssa and it just lets the integer silenty overflow, without error. Should I report that as a separate bug? (It does appear to use a signed integer though).

EDIT: looks more like a bug in go/types, because go vet also doesn't catch the error.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/556295 mentions this issue: cmd/compile: check overflows in range over untyped integer

@griesemer
Copy link
Contributor

Thanks for reporting. This is both a bug in the type checkers and an issue with the spec. In fact there are more issues here. See #65137.

@griesemer griesemer self-assigned this Jan 17, 2024
@griesemer griesemer added NeedsFix The path to resolution is known, but the work has not been done. release-blocker and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 17, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/556398 mentions this issue: types2: fix range clause checks for constant range expressions

@mknyszek mknyszek added this to the Go1.22 milestone Jan 17, 2024
ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
Add missing checks for the case where the range expression is
a (possibly untyped) constant integer expression.

Add context parameter to assignVar for better error message
where the expression is part of a range clause.

Also, rename s/expr/Expr/ where it denotes an AST expression,
for clarity.

Fixes golang#65133.
For golang#65137.

Change-Id: I72962d76741abe79f613e251f7b060e99261d3ae
Reviewed-on: https://go-review.googlesource.com/c/go/+/556398
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@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. NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
None yet
Development

No branches or pull requests

6 participants