diff --git a/src/java.base/share/native/libjli/args.c b/src/java.base/share/native/libjli/args.c index 379291fe8ec..a08574a1b49 100644 --- a/src/java.base/share/native/libjli/args.c +++ b/src/java.base/share/native/libjli/args.c @@ -23,6 +23,12 @@ * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== + */ + #include #include #include @@ -78,6 +84,7 @@ static jboolean expectingNoDashArg = JNI_FALSE; static size_t argsCount = 1; static jboolean stopExpansion = JNI_FALSE; static jboolean relaunch = JNI_FALSE; +static jboolean parsingOpenJ9Args = JNI_FALSE; /* * Prototypes for internal functions. @@ -422,6 +429,10 @@ JLI_PreprocessArg(const char *arg, jboolean expandSourceOpt) { return expandArg(arg); } + if (parsingOpenJ9Args && (JLI_StrCCmp(arg, "-Xoptionsfile=") == 0)) { + return expandArgFile(arg + 14); + } + if (arg[0] != '@') { checkArg(arg); return NULL; @@ -484,6 +495,42 @@ JLI_AddArgsFromEnvVar(JLI_List args, const char *var_name) { return expand(args, env, var_name); } +JNIEXPORT jboolean JNICALL +JLI_ParseOpenJ9ArgsFromEnvVar(JLI_List args, const char *var_name) { + const char *env = getenv(var_name); + jboolean result = JNI_FALSE; + + /* Save the state. */ + int savedFirstAppArgIndex = firstAppArgIndex; + jboolean savedExpectingNoDashArg = expectingNoDashArg; + size_t savedArgsCount = argsCount; + jboolean savedStopExpansion = stopExpansion; + jboolean savedRelaunch = relaunch; + + if (NULL == env) { + return JNI_FALSE; + } + + parsingOpenJ9Args = JNI_TRUE; + firstAppArgIndex = NOT_FOUND; + expectingNoDashArg = JNI_FALSE; + argsCount = 1; + stopExpansion = JNI_FALSE; + relaunch = JNI_FALSE; + + result = expand(args, env, var_name); + + /* Restore the state. */ + parsingOpenJ9Args = JNI_FALSE; + firstAppArgIndex = savedFirstAppArgIndex; + expectingNoDashArg = savedExpectingNoDashArg; + argsCount = savedArgsCount; + stopExpansion = savedStopExpansion; + relaunch = savedRelaunch; + + return result; +} + /* * Expand a string into a list of args. * If the string is the result of looking up an environment variable, @@ -497,9 +544,13 @@ static jboolean expand(JLI_List args, const char *str, const char *var_name) { char *p, *arg; char quote; JLI_List argsInFile; + char *argMem = NULL; // This is retained until the process terminates as it is saved as the args p = JLI_MemAlloc(JLI_StrLen(str) + 1); + if (parsingOpenJ9Args) { + argMem = p; + } while (*str != '\0') { while (*str != '\0' && isspace(*str)) { str++; @@ -541,6 +592,10 @@ static jboolean expand(JLI_List args, const char *str, const char *var_name) { } exit(1); } + if (parsingOpenJ9Args) { + /* Dup the string so it can easily be freed later. */ + arg = JLI_StringDup(arg); + } JLI_List_add(args, arg); } else { size_t cnt, idx; @@ -580,6 +635,9 @@ static jboolean expand(JLI_List args, const char *str, const char *var_name) { assert (*str == '\0' || isspace(*str)); } + if (parsingOpenJ9Args) { + JLI_MemFree(argMem); + } return JNI_TRUE; } diff --git a/src/java.base/share/native/libjli/java.c b/src/java.base/share/native/libjli/java.c index fee8cb2130d..b506aff9440 100644 --- a/src/java.base/share/native/libjli/java.c +++ b/src/java.base/share/native/libjli/java.c @@ -25,7 +25,7 @@ /* * =========================================================================== - * (c) Copyright IBM Corp. 2022, 2022 All Rights Reserved + * (c) Copyright IBM Corp. 2022, 2023 All Rights Reserved * =========================================================================== */ @@ -139,6 +139,8 @@ static jboolean ValidateModules(JNIEnv* env); static void SetPaths(int argc, char **argv); +static int parse_size(const char *s, jlong *result); + static void DumpState(); enum OptionKind { @@ -325,6 +327,31 @@ JLI_Launch(int argc, char ** argv, /* main argc, argv */ } } + { + /* Process -Xmso in the OPENJ9_JAVA_OPTIONS environment variable to + * set the main thread stack size. May be overridden by a later command + * line option. + */ + JLI_List openj9Args = JLI_List_new(8); /* 8 is arbitrary */ + if (JLI_ParseOpenJ9ArgsFromEnvVar(openj9Args, "OPENJ9_JAVA_OPTIONS")) { + size_t i = openj9Args->size; + while (i > 0) { + i -= 1; + if (JLI_StrCCmp(openj9Args->elements[i], "-Xmso") == 0) { + jlong tmp = 0; + if (parse_size(openj9Args->elements[i] + 5, &tmp)) { + threadStackSize = tmp; + if (threadStackSize > 0 && threadStackSize < (jlong)STACK_SIZE_MINIMUM) { + threadStackSize = STACK_SIZE_MINIMUM; + } + } + break; + } + } + JLI_List_free(openj9Args); + } + } + /* Parse command line options; if the return value of * ParseArguments is false, the program should exit. */ diff --git a/src/java.base/share/native/libjli/jli_util.h b/src/java.base/share/native/libjli/jli_util.h index d2ea01b1d41..e83227dc8b1 100644 --- a/src/java.base/share/native/libjli/jli_util.h +++ b/src/java.base/share/native/libjli/jli_util.h @@ -23,6 +23,12 @@ * questions. */ +/* + * =========================================================================== + * (c) Copyright IBM Corp. 2023, 2023 All Rights Reserved + * =========================================================================== + */ + #ifndef _JLI_UTIL_H #define _JLI_UTIL_H @@ -158,4 +164,7 @@ JLI_PreprocessArg(const char *arg, jboolean expandSourceOpt); JNIEXPORT jboolean JNICALL JLI_AddArgsFromEnvVar(JLI_List args, const char *var_name); +JNIEXPORT jboolean JNICALL +JLI_ParseOpenJ9ArgsFromEnvVar(JLI_List args, const char *var_name); + #endif /* _JLI_UTIL_H */