Skip to content

Commit 52e17c2

Browse files
ianlancetaylorgopherbot
authored andcommitted
runtime/cgo: get getstackbound for set_stacklo
Change-Id: Ia63a4604449b5e460e6f54c962fb7d6db2bc6a43 Reviewed-on: https://go-review.googlesource.com/c/go/+/519457 Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 21293b6 commit 52e17c2

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

src/runtime/cgo/gcc_freebsd_amd64.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ static void (*setg_gcc)(void*);
1717
void
1818
x_cgo_init(G *g, void (*setg)(void*))
1919
{
20-
pthread_attr_t *attr;
20+
uintptr *pbounds;
2121

2222
// Deal with memory sanitizer/clang interaction.
2323
// See gcc_linux_amd64.c for details.
2424
setg_gcc = setg;
25-
attr = (pthread_attr_t*)malloc(sizeof *attr);
26-
if (attr == NULL) {
25+
pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
26+
if (pbounds == NULL) {
2727
fatalf("malloc failed: %s", strerror(errno));
2828
}
29-
_cgo_set_stacklo(g, attr);
30-
free(attr);
29+
_cgo_set_stacklo(g, pbounds);
30+
free(pbounds);
3131
}
3232

3333
void

src/runtime/cgo/gcc_libinit.c

+7-11
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,25 @@ _cgo_wait_runtime_init_done(void) {
8585
// _cgo_set_stacklo sets g->stacklo based on the stack size.
8686
// This is common code called from x_cgo_init, which is itself
8787
// called by rt0_go in the runtime package.
88-
void _cgo_set_stacklo(G *g, pthread_attr_t *pattr)
88+
void _cgo_set_stacklo(G *g, uintptr *pbounds)
8989
{
90-
pthread_attr_t attr;
91-
size_t size;
90+
uintptr bounds[2];
9291

93-
// pattr can be passed in by the caller; see gcc_linux_amd64.c.
94-
if (pattr == NULL) {
95-
pattr = &attr;
92+
// pbounds can be passed in by the caller; see gcc_linux_amd64.c.
93+
if (pbounds == NULL) {
94+
pbounds = &bounds[0];
9695
}
9796

98-
pthread_attr_init(pattr);
99-
pthread_attr_getstacksize(pattr, &size);
97+
x_cgo_getstackbound(pbounds);
10098

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

103101
// Sanity check the results now, rather than getting a
104102
// morestack on g0 crash.
105103
if (g->stacklo >= g->stackhi) {
106104
fprintf(stderr, "runtime/cgo: bad stack bounds: lo=%p hi=%p\n", (void*)(g->stacklo), (void*)(g->stackhi));
107105
abort();
108106
}
109-
110-
pthread_attr_destroy(pattr);
111107
}
112108

113109
// Store the g into a thread-specific value associated with the pthread key pthread_g.

src/runtime/cgo/gcc_linux_amd64.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void (*x_cgo_inittls)(void **tlsg, void **tlsbase) __attribute__((common));
1919
void
2020
x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
2121
{
22-
pthread_attr_t *attr;
22+
uintptr *pbounds;
2323

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

3939
setg_gcc = setg;
40-
attr = (pthread_attr_t*)malloc(sizeof *attr);
41-
if (attr == NULL) {
40+
pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
41+
if (pbounds == NULL) {
4242
fatalf("malloc failed: %s", strerror(errno));
4343
}
44-
_cgo_set_stacklo(g, attr);
45-
free(attr);
44+
_cgo_set_stacklo(g, pbounds);
45+
free(pbounds);
4646

4747
if (x_cgo_inittls) {
4848
x_cgo_inittls(tlsg, tlsbase);

src/runtime/cgo/gcc_linux_arm64.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ threadentry(void *v)
5656
void
5757
x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
5858
{
59-
pthread_attr_t *attr;
59+
uintptr *pbounds;
6060

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

7676
setg_gcc = setg;
77-
attr = (pthread_attr_t*)malloc(sizeof *attr);
78-
if (attr == NULL) {
77+
pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
78+
if (pbounds == NULL) {
7979
fatalf("malloc failed: %s", strerror(errno));
8080
}
81-
_cgo_set_stacklo(g, attr);
82-
free(attr);
81+
_cgo_set_stacklo(g, pbounds);
82+
free(pbounds);
8383

8484
if (x_cgo_inittls) {
8585
x_cgo_inittls(tlsg, tlsbase);

src/runtime/cgo/libcgo.h

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ void _cgo_sys_thread_start(ThreadStart *ts);
6868
*/
6969
uintptr_t _cgo_wait_runtime_init_done(void);
7070

71+
/*
72+
* Get the low and high boundaries of the stack.
73+
*/
74+
void x_cgo_getstackbound(uintptr bounds[2]);
75+
7176
/*
7277
* Prints error then calls abort. For linux and android.
7378
*/

src/runtime/cgo/libcgo_unix.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/*
66
* Initialize g->stacklo.
77
*/
8-
extern void _cgo_set_stacklo(G *, pthread_attr_t*);
8+
extern void _cgo_set_stacklo(G *, uintptr *);
99

1010
/*
1111
* Call pthread_create, retrying on EAGAIN.

0 commit comments

Comments
 (0)