]> Cypherpunks.ru repositories - gostls13.git/commit
runtime: clear g0 stack bounds in dropm
authorMichael Pratt <mpratt@google.com>
Wed, 25 Oct 2023 15:40:56 +0000 (11:40 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 26 Oct 2023 15:17:33 +0000 (15:17 +0000)
commit1af424c196584cd0b05e559c2740f046d1f32042
tree338781dcde5c9ae95de2a2bccff2a1e51e9e6ba6
parent5fe2035927bc9fac8a375a96d9ef47735c10fba4
runtime: clear g0 stack bounds in dropm

After CL 527715, needm uses callbackUpdateSystemStack to set the stack
bounds for g0 on an M from the extra M list. Since
callbackUpdateSystemStack is also used for recursive cgocallback, it
does nothing if the stack is already in bounds.

Currently, the stack bounds in an extra M may contain stale bounds from
a previous thread that used this M and then returned it to the extra
list in dropm.

Typically a new thread will not have an overlapping stack with an old
thread, but because the old thread has exited there is a small chance
that the C memory allocator will allocate the new thread's stack
partially or fully overlapping with the old thread's stack.

If this occurs, then callbackUpdateSystemStack will not update the stack
bounds. If in addition, the overlap is partial such that SP on
cgocallback is close to the recorded stack lower bound, then Go may
quickly "overflow" the stack and crash with "morestack on g0".

Fix this by clearing the stack bounds in dropm, which ensures that
callbackUpdateSystemStack will unconditionally update the bounds in
needm.

For #62440.

Change-Id: Ic9e2052c2090dd679ed716d1a23a86d66cbcada7
Reviewed-on: https://go-review.googlesource.com/c/go/+/537695
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
TryBot-Bypass: Michael Pratt <mpratt@google.com>
src/runtime/proc.go
src/runtime/testdata/testprogcgo/stackswitch.c