From e293c4b509de6e7ceaeabb0c8d9f2a4d3d3b4e6d Mon Sep 17 00:00:00 2001 From: Joel Sing Date: Tue, 31 Oct 2023 00:27:58 +1100 Subject: [PATCH] runtime: allocate crash stack via stackalloc 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 Reviewed-by: Bryan Mills Reviewed-by: Mauri de Souza Meneguzzo Reviewed-by: Cherry Mui TryBot-Result: Gopher Robot --- src/runtime/proc.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/runtime/proc.go b/src/runtime/proc.go index 408f26cf7a..7189a0650a 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -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 -- 2.50.0