]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: allocate crash stack via stackalloc
authorJoel Sing <joel@sing.id.au>
Mon, 30 Oct 2023 13:27:58 +0000 (00:27 +1100)
committerCherry Mui <cherryyz@google.com>
Tue, 31 Oct 2023 16:28:14 +0000 (16:28 +0000)
On some platforms (notably OpenBSD), stacks must be specifically allocated
and marked as being stack memory. Allocate the crash stack using stackalloc,
which ensures these requirements are met, rather than using a global Go
variable.

Fixes #63794

Change-Id: I6513575797dd69ff0a36f3bfd4e5fc3bd95cbf50
Reviewed-on: https://go-review.googlesource.com/c/go/+/538457
Run-TryBot: Joel Sing <joel@sing.id.au>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Mauri de Souza Meneguzzo <mauri870@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/runtime/proc.go

index 408f26cf7a91531e3812a565d8ab922f12917be9..7189a0650ada59dcaa132e05ee9049c8785ab62e 100644 (file)
@@ -543,16 +543,9 @@ func badctxt() {
        throw("ctxt != 0")
 }
 
-// crashstack is a space that can be used as the stack when it is
-// crashing on bad stack conditions, e.g. morestack on g0.
-// gcrash is the corresponding (fake) g.
-var crashstack [16384]byte
-
-var gcrash = g{
-       stack:       stack{uintptr(unsafe.Pointer(&crashstack)), uintptr(unsafe.Pointer(&crashstack)) + unsafe.Sizeof(crashstack)},
-       stackguard0: uintptr(unsafe.Pointer(&crashstack)) + 1000,
-       stackguard1: uintptr(unsafe.Pointer(&crashstack)) + 1000,
-}
+// gcrash is a fake g that can be used when crashing due to bad
+// stack conditions.
+var gcrash g
 
 var crashingG atomic.Pointer[g]
 
@@ -803,6 +796,12 @@ func schedinit() {
        parsedebugvars()
        gcinit()
 
+       // Allocate stack space that can be used when crashing due to bad stack
+       // conditions, e.g. morestack on g0.
+       gcrash.stack = stackalloc(16384)
+       gcrash.stackguard0 = gcrash.stack.lo + 1000
+       gcrash.stackguard1 = gcrash.stack.lo + 1000
+
        // if disableMemoryProfiling is set, update MemProfileRate to 0 to turn off memprofile.
        // Note: parsedebugvars may update MemProfileRate, but when disableMemoryProfiling is
        // set to true by the linker, it means that nothing is consuming the profile, it is