Skip to content

Commit 7d3a5a5

Browse files
fengyoulincherrymui
authored andcommitted
runtime/cgo: let darwin pthread stacksize follow rlimit
On Mac OS X, the default stack size for non-main threads created by cgo is fixed at 512KB and cannot be altered by setrlimit. This stack size is too small for some recursive scenarios. We can solve this problem by explicitly copying the stack size of the main thread when creating a new thread. Change-Id: I400d5b2e929a1ee261502914a991e208759f64a8 GitHub-Last-Rev: b29c745 GitHub-Pull-Request: #53667 Reviewed-on: https://go-review.googlesource.com/c/go/+/415915 Run-TryBot: Cherry Mui <cherryyz@google.com> Reviewed-by: hopehook <hopehook@golangcn.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
1 parent c318f19 commit 7d3a5a5

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/runtime/cgo/gcc_darwin_amd64.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ static void (*setg_gcc)(void*);
1414
void
1515
x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
1616
{
17-
pthread_attr_t attr;
1817
size_t size;
1918

2019
setg_gcc = setg;
2120

22-
pthread_attr_init(&attr);
23-
pthread_attr_getstacksize(&attr, &size);
24-
g->stacklo = (uintptr)&attr - size + 4096;
25-
pthread_attr_destroy(&attr);
21+
size = pthread_get_stacksize_np(pthread_self());
22+
g->stacklo = (uintptr)&size - size + 4096;
2623
}
2724

2825

@@ -38,8 +35,9 @@ _cgo_sys_thread_start(ThreadStart *ts)
3835
sigfillset(&ign);
3936
pthread_sigmask(SIG_SETMASK, &ign, &oset);
4037

38+
size = pthread_get_stacksize_np(pthread_self());
4139
pthread_attr_init(&attr);
42-
pthread_attr_getstacksize(&attr, &size);
40+
pthread_attr_setstacksize(&attr, size);
4341
// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
4442
ts->g->stackhi = size;
4543
err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);

src/runtime/cgo/gcc_darwin_arm64.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ _cgo_sys_thread_start(ThreadStart *ts)
3636
sigfillset(&ign);
3737
pthread_sigmask(SIG_SETMASK, &ign, &oset);
3838

39+
size = pthread_get_stacksize_np(pthread_self());
3940
pthread_attr_init(&attr);
40-
pthread_attr_getstacksize(&attr, &size);
41+
pthread_attr_setstacksize(&attr, size);
4142
// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
4243
ts->g->stackhi = size;
4344
err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
@@ -126,15 +127,12 @@ init_working_dir()
126127
void
127128
x_cgo_init(G *g, void (*setg)(void*))
128129
{
129-
pthread_attr_t attr;
130130
size_t size;
131131

132132
//fprintf(stderr, "x_cgo_init = %p\n", &x_cgo_init); // aid debugging in presence of ASLR
133133
setg_gcc = setg;
134-
pthread_attr_init(&attr);
135-
pthread_attr_getstacksize(&attr, &size);
136-
g->stacklo = (uintptr)&attr - size + 4096;
137-
pthread_attr_destroy(&attr);
134+
size = pthread_get_stacksize_np(pthread_self());
135+
g->stacklo = (uintptr)&size - size + 4096;
138136

139137
#if TARGET_OS_IPHONE
140138
darwin_arm_init_mach_exception_handler();

0 commit comments

Comments
 (0)