diff options
author | Cherry Mui <cherryyz@google.com> | 2022-07-25 11:02:56 -0400 |
---|---|---|
committer | Cherry Mui <cherryyz@google.com> | 2022-07-26 01:49:02 +0000 |
commit | 3e97294663d978bf8abb7acec7cc615ef2f1ea75 (patch) | |
tree | 17f2bbd5dc9635f8034cef901765f0f6dfc4bcba | |
parent | 24dc27a3c084b901ee456637541ea818495888b0 (diff) | |
download | go-3e97294663d978bf8abb7acec7cc615ef2f1ea75.tar.gz go-3e97294663d978bf8abb7acec7cc615ef2f1ea75.zip |
runtime/cgo: use frame address to set g0 stack bound
For a cgo binary, at startup we set g0's stack bounds using the
address of a local variable (&size) in a C function x_cgo_init and
the stack size from pthread_attr_getstacksize. Normally, &size is
an address within the current stack frame. However, when it is
compiled with ASAN, it may be instrumented to __asan_stack_malloc_0
and the address may not live in the current stack frame, causing
the stack bound to be set incorrectly, e.g. lo > hi.
Using __builtin_frame_address(0) to get the stack address instead.
Change-Id: I41df929e5ed24d8bbf3e15027af6dcdfc3736e37
Reviewed-on: https://go-review.googlesource.com/c/go/+/419434
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
-rw-r--r-- | src/runtime/cgo/gcc_linux_amd64.c | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/src/runtime/cgo/gcc_linux_amd64.c b/src/runtime/cgo/gcc_linux_amd64.c index c25e7e769b..fb164c1a1d 100644 --- a/src/runtime/cgo/gcc_linux_amd64.c +++ b/src/runtime/cgo/gcc_linux_amd64.c @@ -44,7 +44,9 @@ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase) } pthread_attr_init(attr); pthread_attr_getstacksize(attr, &size); - g->stacklo = (uintptr)&size - size + 4096; + g->stacklo = (uintptr)__builtin_frame_address(0) - size + 4096; + if (g->stacklo >= g->stackhi) + fatalf("bad stack bounds: lo=%p hi=%p\n", g->stacklo, g->stackhi); pthread_attr_destroy(attr); free(attr); |