Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime/cgo: add error checks for safe stack retrieval on Unix #71897

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/runtime/cgo/gcc_stack_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@ x_cgo_getstackbound(uintptr bounds[2])
pthread_attr_t attr;
void *addr;
size_t size;
int err;

// Needed before pthread_getattr_np, too, since before glibc 2.32
// it did not call pthread_attr_init in all cases (see #65625).
pthread_attr_init(&attr);
err = pthread_attr_init(&attr);
if (err != 0)
fatalf("pthread_attr_init failed: %d", err);
#if defined(__GLIBC__) || defined(__BIONIC__) || (defined(__sun) && !defined(__illumos__))
// pthread_getattr_np is a GNU extension supported in glibc.
// Solaris is not glibc but does support pthread_getattr_np
// (and the fallback doesn't work...). Illumos does not.
pthread_getattr_np(pthread_self(), &attr); // GNU extension
pthread_attr_getstack(&attr, &addr, &size); // low address
err = pthread_getattr_np(pthread_self(), &attr);
if (err != 0)
fatalf("pthread_getattr_np failed: %d", err);
err = pthread_attr_getstack(&attr, &addr, &size);
if (err != 0)
fatalf("pthread_attr_getstack failed: %d", err);
#elif defined(__illumos__)
pthread_attr_get_np(pthread_self(), &attr);
pthread_attr_getstack(&attr, &addr, &size); // low address
err = pthread_attr_get_np(pthread_self(), &attr);
if (err != 0)
fatalf("pthread_attr_get_np failed: %d", err);
err = pthread_attr_getstack(&attr, &addr, &size);
if (err != 0)
fatalf("pthread_attr_getstack failed: %d", err);
#else
// We don't know how to get the current stacks, leave it as
// 0 and the caller will use an estimate based on the current
Expand Down