Skip to content

Fix VM options parsing from /proc/fs #9255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,57 +40,11 @@ private String[] readProcFsCmdLine() {
return null;
}

private List<String> findVmOptions() {
return findVmOptions(PROCFS_CMDLINE);
}

@SuppressForbidden // Class.forName() as backup
// Visible for testing
List<String> findVmOptions(String[] procfsCmdline) {
private List<String> findVmOptions() {
// Try ProcFS on Linux
// Be aware that when running a native image, the command line in /proc/self/cmdline is just the
// executable
if (procfsCmdline != null) {
// Create list of VM options
List<String> vmOptions = new ArrayList<>();
// Start at 1 to skip "java" command itself
int index = 1;
// Look for first self-standing argument that is not prefixed with "-" or end of VM options
// Skip "-jar" and the jar file
// Simultaneously, collect all arguments in the VM options
for (; index < procfsCmdline.length; index++) {
String argument = procfsCmdline[index];
if (argument.startsWith("@")) {
vmOptions.addAll(getArgumentsFromFile(argument));
} else {
if ("-jar".equals(argument)) {
// skip "-jar" and the jar file
index++;
continue;
} else if ("-cp".equals(argument)) {
// slurp '-cp' and the classpath
vmOptions.add(argument);
if (index + 1 < procfsCmdline.length) {
argument = procfsCmdline[++index];
}
} else if (!argument.startsWith("-")) {
// end of VM options
break;
}
vmOptions.add(argument);
}
}
// Insert JDK_JAVA_OPTIONS at the start if present and supported
List<String> jdkJavaOptions = getJdkJavaOptions();
if (!jdkJavaOptions.isEmpty()) {
vmOptions.addAll(0, jdkJavaOptions);
}
// Insert JAVA_TOOL_OPTIONS at the start if present
List<String> javaToolOptions = getJavaToolOptions();
if (!javaToolOptions.isEmpty()) {
vmOptions.addAll(0, javaToolOptions);
}
return vmOptions;
if (PROCFS_CMDLINE != null) {
return findVmOptionsFromProcFs(PROCFS_CMDLINE);
}

// Try Oracle-based
Expand Down Expand Up @@ -137,6 +91,48 @@ List<String> findVmOptions(String[] procfsCmdline) {
return emptyList();
}

// Be aware that when running a native image, the command line in /proc/self/cmdline is just the
// executable
// Visible for testing
List<String> findVmOptionsFromProcFs(String[] procfsCmdline) {
// Create list of VM options
List<String> vmOptions = new ArrayList<>();
// Look for first self-standing argument that is not prefixed with "-" or end of VM options
// while simultaneously, collect all arguments in the VM options
// Starts from 1 as 0 is the java command itself (or native-image)
for (int index = 1; index < procfsCmdline.length; index++) {
String argument = procfsCmdline[index];
// Inflate arg files
if (argument.startsWith("@")) {
vmOptions.addAll(getArgumentsFromFile(argument));
}
// Skip classpath argument (not part of VM options)
else if ("-cp".equals(argument)) {
index++;
}
// Check "-jar" or class name argument as the end of the VM options
else if ("-jar".equals(argument) || !argument.startsWith("-")) {
// End of VM options
break;
}
// Otherwise add as VM option
else {
vmOptions.add(argument);
}
}
// Insert JDK_JAVA_OPTIONS at the start if present and supported
List<String> jdkJavaOptions = getJdkJavaOptions();
if (!jdkJavaOptions.isEmpty()) {
vmOptions.addAll(0, jdkJavaOptions);
}
// Insert JAVA_TOOL_OPTIONS at the start if present
List<String> javaToolOptions = getJavaToolOptions();
if (!javaToolOptions.isEmpty()) {
vmOptions.addAll(0, javaToolOptions);
}
return vmOptions;
}

private static List<String> getArgumentsFromFile(String argFile) {
String filename = argFile.substring(1);
Path path = Paths.get(filename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ private static Stream<Arguments> procFsCmdLine() {
arguments(
"Java from class and options",
new String[]{"java", "-Xmx512m", "-Xms256m", "-cp", "app.jar", "Main"},
asList("-Xmx512m", "-Xms256m", "-cp", "app.jar")
asList("-Xmx512m", "-Xms256m")
),
arguments(
"Java from class and options, mixed",
new String[]{"java", "-Xms256m", "-cp", "app.jar", "-Xmx512m", "Main"},
asList("-Xms256m", "-cp", "app.jar", "-Xmx512m")
asList("-Xms256m", "-Xmx512m")
),
arguments(
"Args from file",
Expand All @@ -167,10 +167,9 @@ private static Stream<Arguments> procFsCmdLine() {

@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("procFsCmdLine")
void testFindVmOptionsWithProcFsCmdLine(
String useCase, String[] procfsCmdline, List<String> expected) throws Exception {
void testFindVmOptionsFromProcFs(String useCase, String[] procfsCmdline, List<String> expected) {
JvmOptions vmOptions = new JvmOptions();
List<String> found = vmOptions.findVmOptions(procfsCmdline);
List<String> found = vmOptions.findVmOptionsFromProcFs(procfsCmdline);
assertEquals(expected, found);
}

Expand Down