Skip to content

Commit

Permalink
runtime,cmd/link: increase stack guard space when building with -race
Browse files Browse the repository at this point in the history
More stuff to do = more stack needed. Bump up the guard space when
building with the race detector.

Fixes golang#54291

Change-Id: I701bc8800507921bed568047d35b8f49c26e7df7
Reviewed-on: https://go-review.googlesource.com/c/go/+/451217
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
  • Loading branch information
randall77 committed Nov 18, 2022
1 parent e18d07d commit 893964b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/cmd/internal/objabi/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ const (
StackSmall = 128
)

// Initialize StackGuard and StackLimit according to target system.
var StackGuard = 928*stackGuardMultiplier() + StackSystem
var StackLimit = StackGuard - StackSystem - StackSmall
func StackLimit(race bool) int {
// This arithmetic must match that in runtime/stack.go:{_StackGuard,_StackLimit}.
stackGuard := 928*stackGuardMultiplier(race) + StackSystem
stackLimit := stackGuard - StackSystem - StackSmall
return stackLimit
}

// stackGuardMultiplier returns a multiplier to apply to the default
// stack guard size. Larger multipliers are used for non-optimized
// builds that have larger stack frames or for specific targets.
func stackGuardMultiplier() int {
func stackGuardMultiplier(race bool) int {
// This arithmetic must match that in runtime/internal/sys/consts.go:StackGuardMultiplier.
n := 1
// On AIX, a larger stack is needed for syscalls.
if buildcfg.GOOS == "aix" {
return 2
n += 1
}
// The race build also needs more stack.
if race {
n += 1
}
return 1
return n
}
2 changes: 1 addition & 1 deletion src/cmd/link/internal/ld/stackcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (ctxt *Link) doStackCheck() {
// The call to morestack in every splittable function ensures
// that there are at least StackLimit bytes available below SP
// when morestack returns.
limit := objabi.StackLimit - sc.callSize
limit := objabi.StackLimit(*flagRace) - sc.callSize
if buildcfg.GOARCH == "arm64" {
// Need an extra 8 bytes below SP to save FP.
limit -= 8
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/internal/sys/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
)

// AIX requires a larger stack for syscalls.
const StackGuardMultiplier = 1*(1-goos.IsAix) + 2*goos.IsAix
// The race build also needs more stack. See issue 54291.
// This arithmetic must match that in cmd/internal/objabi/stack.go:stackGuardMultiplier.
const StackGuardMultiplier = 1 + goos.IsAix + isRace

// DefaultPhysPageSize is the default physical page size.
const DefaultPhysPageSize = goarch.DefaultPhysPageSize
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/internal/sys/consts_norace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !race

package sys

const isRace = 0
9 changes: 9 additions & 0 deletions src/runtime/internal/sys/consts_race.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build race

package sys

const isRace = 1
2 changes: 2 additions & 0 deletions src/runtime/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const (
// The guard leaves enough room for one _StackSmall frame plus
// a _StackLimit chain of NOSPLIT calls plus _StackSystem
// bytes for the OS.
// This arithmetic must match that in cmd/internal/objabi/stack.go:StackLimit.
_StackGuard = 928*sys.StackGuardMultiplier + _StackSystem

// After a stack split check the SP is allowed to be this
Expand All @@ -107,6 +108,7 @@ const (

// The maximum number of bytes that a chain of NOSPLIT
// functions can use.
// This arithmetic must match that in cmd/internal/objabi/stack.go:StackLimit.
_StackLimit = _StackGuard - _StackSystem - _StackSmall
)

Expand Down

0 comments on commit 893964b

Please sign in to comment.