Skip to content

Commit

Permalink
runtime/cgo: add assertions for safe stack retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
froz42 committed Feb 22, 2025
1 parent f062d7b commit 0302d11
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 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,7 @@
#define _GNU_SOURCE
#endif

#include <assert.h>
#include <pthread.h>
#include "libcgo.h"

Expand All @@ -20,16 +21,16 @@ 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);
assert(pthread_attr_init(&attr) == 0);
#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
assert(pthread_getattr_np(pthread_self(), &attr) == 0); // GNU extension
assert(pthread_attr_getstack(&attr, &addr, &size) == 0); // low address
#elif defined(__illumos__)
pthread_attr_get_np(pthread_self(), &attr);
pthread_attr_getstack(&attr, &addr, &size); // low address
assert(pthread_attr_get_np(pthread_self(), &attr) == 0); // Solaris extension
assert(pthread_attr_getstack(&attr, &addr, &size) == 0); // low address
#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 0302d11

Please sign in to comment.