| 1 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build unix && !darwin |
| 6 |
|
| 7 |
#ifndef _GNU_SOURCE // pthread_getattr_np |
| 8 |
#define _GNU_SOURCE |
| 9 |
#endif |
| 10 |
|
| 11 |
#include <pthread.h> |
| 12 |
#include "libcgo.h" |
| 13 |
|
| 14 |
void |
| 15 |
x_cgo_getstackbound(uintptr bounds[2]) |
| 16 |
{ |
| 17 |
pthread_attr_t attr; |
| 18 |
void *addr; |
| 19 |
size_t size; |
| 20 |
|
| 21 |
// Needed before pthread_getattr_np, too, since before glibc 2.32 |
| 22 |
// it did not call pthread_attr_init in all cases (see #65625). |
| 23 |
pthread_attr_init(&attr); |
| 24 |
#if defined(__GLIBC__) || defined(__BIONIC__) || (defined(__sun) && !defined(__illumos__)) |
| 25 |
// pthread_getattr_np is a GNU extension supported in glibc. |
| 26 |
// Solaris is not glibc but does support pthread_getattr_np |
| 27 |
// (and the fallback doesn't work...). Illumos does not. |
| 28 |
pthread_getattr_np(pthread_self(), &attr); // GNU extension |
| 29 |
pthread_attr_getstack(&attr, &addr, &size); // low address |
| 30 |
#elif defined(__illumos__) |
| 31 |
pthread_attr_get_np(pthread_self(), &attr); |
| 32 |
pthread_attr_getstack(&attr, &addr, &size); // low address |
| 33 |
#else |
| 34 |
// We don't know how to get the current stacks, leave it as |
| 35 |
// 0 and the caller will use an estimate based on the current |
| 36 |
// SP. |
| 37 |
addr = 0; |
| 38 |
size = 0; |
| 39 |
#endif |
| 40 |
pthread_attr_destroy(&attr); |
| 41 |
|
| 42 |
// bounds points into the Go stack. TSAN can't see the synchronization |
| 43 |
// in Go around stack reuse. |
| 44 |
_cgo_tsan_acquire(); |
| 45 |
bounds[0] = (uintptr)addr; |
| 46 |
bounds[1] = (uintptr)addr + size; |
| 47 |
_cgo_tsan_release(); |
| 48 |
} |