Skip to content

Commit

Permalink
runtime/cgo: add error checks for safe stack retrieval on Unix
Browse files Browse the repository at this point in the history
  • Loading branch information
froz42 committed Feb 24, 2025
1 parent f062d7b commit d71df4a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/runtime/cgo/gcc_stack_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define _GNU_SOURCE
#endif

#include <errno.h>
#include <string.h>
#include <pthread.h>
#include "libcgo.h"

Expand All @@ -20,16 +22,21 @@ x_cgo_getstackbound(uintptr bounds[2])

// 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);
if (pthread_attr_init(&attr) != 0)
fatalf("pthread_attr_init failed: %s", strerror(errno));
#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
if (pthread_getattr_np(pthread_self(), &attr) != 0) // GNU extension
fatalf("pthread_getattr_np failed: %s", strerror(errno));
if (pthread_attr_getstack(&attr, &addr, &size) != 0) // low address
fatalf("pthread_attr_getstack failed: %s", strerror(errno));
#elif defined(__illumos__)
pthread_attr_get_np(pthread_self(), &attr);
pthread_attr_getstack(&attr, &addr, &size); // low address
if (pthread_attr_get_np(pthread_self(), &attr) != 0) // Solaris extension
fatalf("pthread_attr_get_np failed: %s", strerror(errno));
if (pthread_attr_getstack(&attr, &addr, &size) != 0) // low address
fatalf("pthread_attr_getstack failed: %s", strerror(errno));
#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

0 comments on commit d71df4a

Please sign in to comment.