Skip to content

Commit 7846971

Browse files
committed
8236569: -Xss not multiple of 4K does not work for the main thread on macOS
Reviewed-by: dholmes, stuefe
1 parent 7159976 commit 7846971

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

src/java.base/macosx/native/libjli/java_md_macosx.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
#include <errno.h>
4848
#include <spawn.h>
49+
#include <unistd.h>
4950

5051
struct NSAppArgs {
5152
int argc;
@@ -722,6 +723,20 @@ static void MacOSXStartup(int argc, char *argv[]) {
722723
return (void*)(intptr_t)JavaMain(args);
723724
}
724725

726+
static size_t adjustStackSize(size_t stack_size) {
727+
long page_size = getpagesize();
728+
if (stack_size % page_size == 0) {
729+
return stack_size;
730+
} else {
731+
long pages = stack_size / page_size;
732+
// Ensure we don't go over limit
733+
if (stack_size <= SIZE_MAX - page_size) {
734+
pages++;
735+
}
736+
return page_size * pages;
737+
}
738+
}
739+
725740
/*
726741
* Block current thread and continue execution in a new thread.
727742
*/
@@ -734,7 +749,7 @@ static void MacOSXStartup(int argc, char *argv[]) {
734749
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
735750

736751
if (stack_size > 0) {
737-
pthread_attr_setstacksize(&attr, stack_size);
752+
pthread_attr_setstacksize(&attr, adjustStackSize(stack_size));
738753
}
739754
pthread_attr_setguardsize(&attr, 0); // no pthread guard page on java threads
740755

src/java.base/share/classes/sun/launcher/resources/launcher.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ java.launcher.X.usage=\n\
172172
\ (Linux Only) show host system or container\n\
173173
\ configuration and continue\n\
174174
\ -Xss<size> set java thread stack size\n\
175+
\ The actual size may be rounded up to a multiple of the\n\
176+
\ system page size as required by the operating system.\n\
175177
\ -Xverify sets the mode of the bytecode verifier\n\
176178
\ Note that option -Xverify:none is deprecated and\n\
177179
\ may be removed in a future release.\n\

src/java.base/share/man/java.1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,8 @@ continues.
11511151
Sets the thread stack size (in bytes).
11521152
Append the letter \f[CB]k\f[R] or \f[CB]K\f[R] to indicate KB, \f[CB]m\f[R] or
11531153
\f[CB]M\f[R] to indicate MB, or \f[CB]g\f[R] or \f[CB]G\f[R] to indicate GB.
1154+
The actual size may be rounded up to a multiple of the system page size as
1155+
required by the operating system.
11541156
The default value depends on the platform:
11551157
.RS
11561158
.IP \[bu] 2

src/java.base/unix/native/libjli/java_md.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,20 @@ static void* ThreadJavaMain(void* args) {
651651
return (void*)(intptr_t)JavaMain(args);
652652
}
653653

654+
static size_t adjustStackSize(size_t stack_size) {
655+
long page_size = sysconf(_SC_PAGESIZE);
656+
if (stack_size % page_size == 0) {
657+
return stack_size;
658+
} else {
659+
long pages = stack_size / page_size;
660+
// Ensure we don't go over limit
661+
if (stack_size <= SIZE_MAX - page_size) {
662+
pages++;
663+
}
664+
return page_size * pages;
665+
}
666+
}
667+
654668
/*
655669
* Block current thread and continue execution in a new thread.
656670
*/
@@ -661,9 +675,17 @@ CallJavaMainInNewThread(jlong stack_size, void* args) {
661675
pthread_attr_t attr;
662676
pthread_attr_init(&attr);
663677
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
678+
size_t adjusted_stack_size;
664679

665680
if (stack_size > 0) {
666-
pthread_attr_setstacksize(&attr, stack_size);
681+
if (pthread_attr_setstacksize(&attr, stack_size) == EINVAL) {
682+
// System may require stack size to be multiple of page size
683+
// Retry with adjusted value
684+
adjusted_stack_size = adjustStackSize(stack_size);
685+
if (adjusted_stack_size != (size_t) stack_size) {
686+
pthread_attr_setstacksize(&attr, adjusted_stack_size);
687+
}
688+
}
667689
}
668690
pthread_attr_setguardsize(&attr, 0); // no pthread guard page on java threads
669691

0 commit comments

Comments
 (0)