Skip to content

Commit

Permalink
runtime/cgo: get getstackbound for set_stacklo
Browse files Browse the repository at this point in the history
Change-Id: Ia63a4604449b5e460e6f54c962fb7d6db2bc6a43
Reviewed-on: https://go-review.googlesource.com/c/go/+/519457
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Aug 22, 2023
1 parent 21293b6 commit 52e17c2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
10 changes: 5 additions & 5 deletions src/runtime/cgo/gcc_freebsd_amd64.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ static void (*setg_gcc)(void*);
void
x_cgo_init(G *g, void (*setg)(void*))
{
pthread_attr_t *attr;
uintptr *pbounds;

// Deal with memory sanitizer/clang interaction.
// See gcc_linux_amd64.c for details.
setg_gcc = setg;
attr = (pthread_attr_t*)malloc(sizeof *attr);
if (attr == NULL) {
pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
if (pbounds == NULL) {
fatalf("malloc failed: %s", strerror(errno));
}
_cgo_set_stacklo(g, attr);
free(attr);
_cgo_set_stacklo(g, pbounds);
free(pbounds);
}

void
Expand Down
18 changes: 7 additions & 11 deletions src/runtime/cgo/gcc_libinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,25 @@ _cgo_wait_runtime_init_done(void) {
// _cgo_set_stacklo sets g->stacklo based on the stack size.
// This is common code called from x_cgo_init, which is itself
// called by rt0_go in the runtime package.
void _cgo_set_stacklo(G *g, pthread_attr_t *pattr)
void _cgo_set_stacklo(G *g, uintptr *pbounds)
{
pthread_attr_t attr;
size_t size;
uintptr bounds[2];

// pattr can be passed in by the caller; see gcc_linux_amd64.c.
if (pattr == NULL) {
pattr = &attr;
// pbounds can be passed in by the caller; see gcc_linux_amd64.c.
if (pbounds == NULL) {
pbounds = &bounds[0];
}

pthread_attr_init(pattr);
pthread_attr_getstacksize(pattr, &size);
x_cgo_getstackbound(pbounds);

g->stacklo = (uintptr)(__builtin_frame_address(0)) - size + 4096;
g->stacklo = *pbounds;

// Sanity check the results now, rather than getting a
// morestack on g0 crash.
if (g->stacklo >= g->stackhi) {
fprintf(stderr, "runtime/cgo: bad stack bounds: lo=%p hi=%p\n", (void*)(g->stacklo), (void*)(g->stackhi));
abort();
}

pthread_attr_destroy(pattr);
}

// Store the g into a thread-specific value associated with the pthread key pthread_g.
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/cgo/gcc_linux_amd64.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void (*x_cgo_inittls)(void **tlsg, void **tlsbase) __attribute__((common));
void
x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
{
pthread_attr_t *attr;
uintptr *pbounds;

/* The memory sanitizer distributed with versions of clang
before 3.8 has a bug: if you call mmap before malloc, mmap
Expand All @@ -37,12 +37,12 @@ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
malloc, so we actually use the memory we allocate. */

setg_gcc = setg;
attr = (pthread_attr_t*)malloc(sizeof *attr);
if (attr == NULL) {
pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
if (pbounds == NULL) {
fatalf("malloc failed: %s", strerror(errno));
}
_cgo_set_stacklo(g, attr);
free(attr);
_cgo_set_stacklo(g, pbounds);
free(pbounds);

if (x_cgo_inittls) {
x_cgo_inittls(tlsg, tlsbase);
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/cgo/gcc_linux_arm64.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ threadentry(void *v)
void
x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
{
pthread_attr_t *attr;
uintptr *pbounds;

/* The memory sanitizer distributed with versions of clang
before 3.8 has a bug: if you call mmap before malloc, mmap
Expand All @@ -74,12 +74,12 @@ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
malloc, so we actually use the memory we allocate. */

setg_gcc = setg;
attr = (pthread_attr_t*)malloc(sizeof *attr);
if (attr == NULL) {
pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
if (pbounds == NULL) {
fatalf("malloc failed: %s", strerror(errno));
}
_cgo_set_stacklo(g, attr);
free(attr);
_cgo_set_stacklo(g, pbounds);
free(pbounds);

if (x_cgo_inittls) {
x_cgo_inittls(tlsg, tlsbase);
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/cgo/libcgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ void _cgo_sys_thread_start(ThreadStart *ts);
*/
uintptr_t _cgo_wait_runtime_init_done(void);

/*
* Get the low and high boundaries of the stack.
*/
void x_cgo_getstackbound(uintptr bounds[2]);

/*
* Prints error then calls abort. For linux and android.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/cgo/libcgo_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/*
* Initialize g->stacklo.
*/
extern void _cgo_set_stacklo(G *, pthread_attr_t*);
extern void _cgo_set_stacklo(G *, uintptr *);

/*
* Call pthread_create, retrying on EAGAIN.
Expand Down

0 comments on commit 52e17c2

Please sign in to comment.