]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/cgo: let darwin pthread stacksize follow rlimit
authorYoulin Feng <fengyoulin@live.com>
Mon, 3 Oct 2022 13:15:12 +0000 (13:15 +0000)
committerCherry Mui <cherryyz@google.com>
Wed, 5 Oct 2022 14:48:48 +0000 (14:48 +0000)
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: b29c74599ea1cce4683d25e1ac8a70ffd9b86381
GitHub-Pull-Request: golang/go#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>
src/runtime/cgo/gcc_darwin_amd64.c
src/runtime/cgo/gcc_darwin_arm64.c

index d5b7fd8fd802bf21d734db635e2a5455fb83403f..955b81da0be9dd74360cb5e5ee7c4287773940cf 100644 (file)
@@ -14,15 +14,12 @@ static void (*setg_gcc)(void*);
 void
 x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
 {
-       pthread_attr_t attr;
        size_t size;
 
        setg_gcc = setg;
 
-       pthread_attr_init(&attr);
-       pthread_attr_getstacksize(&attr, &size);
-       g->stacklo = (uintptr)&attr - size + 4096;
-       pthread_attr_destroy(&attr);
+       size = pthread_get_stacksize_np(pthread_self());
+       g->stacklo = (uintptr)&size - size + 4096;
 }
 
 
@@ -38,8 +35,9 @@ _cgo_sys_thread_start(ThreadStart *ts)
        sigfillset(&ign);
        pthread_sigmask(SIG_SETMASK, &ign, &oset);
 
+       size = pthread_get_stacksize_np(pthread_self());
        pthread_attr_init(&attr);
-       pthread_attr_getstacksize(&attr, &size);
+       pthread_attr_setstacksize(&attr, size);
        // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
        ts->g->stackhi = size;
        err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
index 24be6758d928acc2e9728db968326eed7610b707..5b77a4294a415ac77a35ebb34e3399c3e9d687f7 100644 (file)
@@ -36,8 +36,9 @@ _cgo_sys_thread_start(ThreadStart *ts)
        sigfillset(&ign);
        pthread_sigmask(SIG_SETMASK, &ign, &oset);
 
+       size = pthread_get_stacksize_np(pthread_self());
        pthread_attr_init(&attr);
-       pthread_attr_getstacksize(&attr, &size);
+       pthread_attr_setstacksize(&attr, size);
        // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
        ts->g->stackhi = size;
        err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
@@ -126,15 +127,12 @@ init_working_dir()
 void
 x_cgo_init(G *g, void (*setg)(void*))
 {
-       pthread_attr_t attr;
        size_t size;
 
        //fprintf(stderr, "x_cgo_init = %p\n", &x_cgo_init); // aid debugging in presence of ASLR
        setg_gcc = setg;
-       pthread_attr_init(&attr);
-       pthread_attr_getstacksize(&attr, &size);
-       g->stacklo = (uintptr)&attr - size + 4096;
-       pthread_attr_destroy(&attr);
+       size = pthread_get_stacksize_np(pthread_self());
+       g->stacklo = (uintptr)&size - size + 4096;
 
 #if TARGET_OS_IPHONE
        darwin_arm_init_mach_exception_handler();