diff options
author | Cherry Mui <cherryyz@google.com> | 2023-03-28 14:48:59 -0400 |
---|---|---|
committer | Cherry Mui <cherryyz@google.com> | 2023-03-30 23:23:55 +0000 |
commit | 443eb9757cd09ada43ceb9ee80f33bc84e892645 (patch) | |
tree | 504ba70bd45461dbc739137dcc4237839e16393a /misc | |
parent | c5ccff405ee9ef6ae30ba2ac900feae4f5abbfd6 (diff) | |
download | go-443eb9757cd09ada43ceb9ee80f33bc84e892645.tar.gz go-443eb9757cd09ada43ceb9ee80f33bc84e892645.zip |
runtime: get a better g0 stack bound in needm
Currently, when C calls into Go the first time, we grab an M
using needm, which sets m.g0's stack bounds using the SP. We don't
know how big the stack is, so we simply assume 32K. Previously,
when the Go function returns to C, we drop the M, and the next
time C calls into Go, we put a new stack bound on the g0 based on
the current SP. After CL 392854, we don't drop the M, and the next
time C calls into Go, we reuse the same g0, without recomputing
the stack bounds. If the C code uses quite a bit of stack space
before calling into Go, the SP may be well below the 32K stack
bound we assumed, so the runtime thinks the g0 stack overflows.
This CL makes needm get a more accurate stack bound from
pthread. (In some platforms this may still be a guess as we don't
know exactly where we are in the C stack), but it is probably
better than simply assuming 32K.
For #59294.
Change-Id: Ie52a8f931e0648d8753e4c1dbe45468b8748b527
Reviewed-on: https://go-review.googlesource.com/c/go/+/479915
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'misc')
-rw-r--r-- | misc/cgo/testcarchive/carchive_test.go | 54 | ||||
-rw-r--r-- | misc/cgo/testcarchive/testdata/libgo9/a.go | 14 | ||||
-rw-r--r-- | misc/cgo/testcarchive/testdata/main9.c | 24 |
3 files changed, 92 insertions, 0 deletions
diff --git a/misc/cgo/testcarchive/carchive_test.go b/misc/cgo/testcarchive/carchive_test.go index 8a39c24a6d..5996268018 100644 --- a/misc/cgo/testcarchive/carchive_test.go +++ b/misc/cgo/testcarchive/carchive_test.go @@ -1247,3 +1247,57 @@ func TestPreemption(t *testing.T) { t.Error(err) } } + +// Issue 59294. Test calling Go function from C after using some +// stack space. +func TestDeepStack(t *testing.T) { + t.Parallel() + + if !testWork { + defer func() { + os.Remove("testp9" + exeSuffix) + os.Remove("libgo9.a") + os.Remove("libgo9.h") + }() + } + + cmd := exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo9.a", "./libgo9") + out, err := cmd.CombinedOutput() + t.Logf("%v\n%s", cmd.Args, out) + if err != nil { + t.Fatal(err) + } + checkLineComments(t, "libgo9.h") + checkArchive(t, "libgo9.a") + + // build with -O0 so the C compiler won't optimize out the large stack frame + ccArgs := append(cc, "-O0", "-o", "testp9"+exeSuffix, "main9.c", "libgo9.a") + out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput() + t.Logf("%v\n%s", ccArgs, out) + if err != nil { + t.Fatal(err) + } + + argv := cmdToRun("./testp9") + cmd = exec.Command(argv[0], argv[1:]...) + sb := new(strings.Builder) + cmd.Stdout = sb + cmd.Stderr = sb + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + + timer := time.AfterFunc(time.Minute, + func() { + t.Error("test program timed out") + cmd.Process.Kill() + }, + ) + defer timer.Stop() + + err = cmd.Wait() + t.Logf("%v\n%s", cmd.Args, sb) + if err != nil { + t.Error(err) + } +} diff --git a/misc/cgo/testcarchive/testdata/libgo9/a.go b/misc/cgo/testcarchive/testdata/libgo9/a.go new file mode 100644 index 0000000000..acb08d90ec --- /dev/null +++ b/misc/cgo/testcarchive/testdata/libgo9/a.go @@ -0,0 +1,14 @@ +// Copyright 2023 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. + +package main + +import "runtime" + +import "C" + +func main() {} + +//export GoF +func GoF() { runtime.GC() } diff --git a/misc/cgo/testcarchive/testdata/main9.c b/misc/cgo/testcarchive/testdata/main9.c new file mode 100644 index 0000000000..95ad4dea49 --- /dev/null +++ b/misc/cgo/testcarchive/testdata/main9.c @@ -0,0 +1,24 @@ +// Copyright 2023 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. + +#include "libgo9.h" + +void use(int *x) { (*x)++; } + +void callGoFWithDeepStack() { + int x[10000]; + + use(&x[0]); + use(&x[9999]); + + GoF(); + + use(&x[0]); + use(&x[9999]); +} + +int main() { + GoF(); // call GoF without using much stack + callGoFWithDeepStack(); // call GoF with a deep stack +} |