go: cmd/compile: fatal error: out of memory on reslice with negative index

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

$ go version
go version go1.11.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ccooper/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/ccooper/work/polaris"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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/kz/p8_8brl12k1gjbc79cjvpdtr0000gn/T/go-build092783061=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Performed a reslice with a negative index.

https://play.golang.org/p/6TIXJwN55zR

What did you expect to see?

1 -1 panic: runtime error: slice bounds out of range

What did you see instead?

1 -1 fatal error: out of memory

runtime stack: runtime.throw(0x10c30b4, 0xd) /usr/local/go/src/runtime/panic.go:608 +0x72 runtime.largeAlloc(0xfffffffffffffffe, 0x1040100, 0x11be000) /usr/local/go/src/runtime/malloc.go:1007 +0x181 runtime.mallocgc.func1() /usr/local/go/src/runtime/malloc.go:914 +0x46 runtime.systemstack(0x0) /usr/local/go/src/runtime/asm_amd64.s:351 +0x66 runtime.mstart() /usr/local/go/src/runtime/proc.go:1229

goroutine 1 [running]: runtime.systemstack_switch() /usr/local/go/src/runtime/asm_amd64.s:311 fp=0xc00007ae18 sp=0xc00007ae10 pc=0x104eae0 runtime.mallocgc(0xfffffffffffffffe, 0x0, 0x0, 0x0) /usr/local/go/src/runtime/malloc.go:913 +0x896 fp=0xc00007aeb8 sp=0xc00007ae18 pc=0x100aaa6 runtime.slicebytetostring(0x0, 0xc000014099, 0xfffffffffffffffe, 0x7, 0x0, 0x0) /usr/local/go/src/runtime/string.go:102 +0x9f fp=0xc00007aee8 sp=0xc00007aeb8 pc=0x103e9af main.main() /Users/ccooper/work/polaris/src/github.com/alistanis/oom/main.go:12 +0x173 fp=0xc00007af98 sp=0xc00007aee8 pc=0x10910c3 runtime.main() /usr/local/go/src/runtime/proc.go:201 +0x207 fp=0xc00007afe0 sp=0xc00007af98 pc=0x10287c7 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:1333 +0x1 fp=0xc00007afe8 sp=0xc00007afe0 pc=0x1050a21 exit status 2

I would expect this to fail in either case, but I was surprised by the error message. I found it when I mistakenly missed a bounds check on a reslice and thought I had actually reached an out of memory issue - the machine I was using had ~200MB of free RAM so I thought that was actually the case at first.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

It fails for me on 1.11, 1.11.1, 1.11.2, and tip on both linux and darwin.

It looks it is ok for Go 1.11.2 on Linux amd64, but bad for Go 1.11 and 1.11.1 on Linux amd64.