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: big array not allocated on heap, which make program panic. #20021

Open
go101 opened this issue Apr 18, 2017 · 8 comments
Labels
Milestone

Comments

@go101
Copy link

@go101 go101 commented Apr 18, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8.1 linux/amd64

What did you do?

package main

const N = 1000 * 1000 * 537
var a [N]byte

func main() {
	// this will stack overflow
	for _, v := range a {
	  _ = v
	}

	// edit: blow solo also stack overflow
	var x [N]byte
	var y [N]byte
	_, _ = x, y
}

[edit]: now(Go SDK 1.12.2), the above program doesn't crash. But below @agnivade shows one case which will still crash.

What did you expect to see?

runs ok

What did you see instead?

crash

runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

runtime stack:
runtime.throw(0x469cdc, 0xe)
	/sdks/go/src/runtime/panic.go:596 +0x95
runtime.newstack(0x0)
	/sdks/go/src/runtime/stack.go:1089 +0x3f2
runtime.morestack()
	/sdks/go/src/runtime/asm_amd64.s:398 +0x86

goroutine 1 [running]:
main.main()
	/tmp/main.go:6 +0x88 fp=0xc460057f88 sp=0xc460057f80
runtime.main()
	/sdks/go/src/runtime/proc.go:185 +0x20a fp=0xc460057fe0 sp=0xc460057f88
runtime.goexit()
	/sdks/go/src/runtime/asm_amd64.s:2197 +0x1 fp=0xc460057fe8 sp=0xc460057fe0
exit status 2
@mvdan

This comment has been minimized.

Copy link
Member

@mvdan mvdan commented Apr 18, 2017

Worth noting that the spec does not talk of heap nor stack, so if this is a bug it would likely be a compiler one.

Did you try forcing an allocation at run-time, like a := make([]byte, 1000 * 1000 * 537)?

@ianlancetaylor ianlancetaylor changed the title big array not allocated on heap, which make program panic. cmd/compile: big array not allocated on heap, which make program panic. Apr 18, 2017
@ianlancetaylor ianlancetaylor added this to the Go1.9Maybe milestone Apr 18, 2017
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Apr 18, 2017

@go101

This comment has been minimized.

Copy link
Author

@go101 go101 commented Apr 18, 2017

@mvdan

package main

const N = 1000 * 1000 * 537

func main() {
	// make is ok
	p := make([]byte, N)
	q := make([]byte, N)
	_, _ = p, q

	// new is also ok
	var u = new([N]byte)
	var v = new([N]byte)
	_, _ = u, v
	
	// blow also stack overflow
	var x [N]byte
	var y [N]byte
	_, _ = x, y
}
@josharian

This comment has been minimized.

Copy link
Contributor

@josharian josharian commented Apr 18, 2017

Related: #10936 and comments on CL 10268.

cc @dr2chase for escape analysis

@valyala

This comment has been minimized.

Copy link
Contributor

@valyala valyala commented Apr 18, 2017

package main

const N = 1000 * 1000 * 537
var a [N]byte

func main() {
	for _, v := range a {
	  _ = v
	}
}

This is a typical trap that the #19817 should address. The workaround is to rewrite the line with range to:

	for _, v := range a[:] {
@bradfitz bradfitz modified the milestones: Go1.9Maybe, Go1.10 Jul 20, 2017
@bradfitz

This comment has been minimized.

Copy link
Member

@bradfitz bradfitz commented Nov 29, 2017

Go tip (as of 992ce90) has a better error message:

$ go run x.go
# command-line-arguments
./x.go:6:6: stack frame too large (>1GB)

So, some progress. But bumping to Go 1.11.

@bradfitz bradfitz modified the milestones: Go1.10, Go1.11 Nov 29, 2017
@bradfitz bradfitz added the NeedsFix label Nov 29, 2017
@gopherbot gopherbot modified the milestones: Go1.11, Unplanned May 23, 2018
@agnivade

This comment has been minimized.

Copy link
Contributor

@agnivade agnivade commented Apr 10, 2019

As of Go 1.12, it does not crash if the code is

	for _, v := range a {
		_ = v
	}

But if the variable is assigned to something else and printed later -

package main

import "fmt"

const N = 1000 * 1000 * 537

var a [N]byte
var tmp byte

func main() {
	for _, v := range a {
		tmp = v
	}
	fmt.Println(tmp)
}

it goes back to the original error

runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

runtime stack:
runtime.throw(0x4b917b, 0xe)
	/usr/local/go/src/runtime/panic.go:617 +0x72
runtime.newstack()
	/usr/local/go/src/runtime/stack.go:1041 +0x6f0
runtime.morestack()
	/usr/local/go/src/runtime/asm_amd64.s:429 +0x8f

goroutine 1 [running]:
main.main()
	/home/agniva/play/go/src/bigint.go:10 +0xfa fp=0xc043ffff98 sp=0xc043ffff90 pc=0x486aba
runtime.main()
	/usr/local/go/src/runtime/proc.go:200 +0x20c fp=0xc043ffffe0 sp=0xc043ffff98 pc=0x4294ec
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:1337 +0x1 fp=0xc043ffffe8 sp=0xc043ffffe0 pc=0x450ad1
exit status 2

Same behavior with tip (go version devel +f947c4dcfe Fri Apr 5 00:52:55 2019 +0000 linux/amd64)

@randall77

This comment has been minimized.

Copy link
Contributor

@randall77 randall77 commented Apr 10, 2019

I believe this is, or is closely related to, #27447.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.