Skip to content

Commit bdc305e

Browse files
author
Alex Menkov
committed
8258917: NativeMemoryTracking is handled by launcher inconsistenly
Reviewed-by: zgu
1 parent 7be9113 commit bdc305e

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/java.base/share/native/libjli/java.c

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -284,9 +284,8 @@ JLI_Launch(int argc, char ** argv, /* main argc, argv */
284284
jvmpath, sizeof(jvmpath),
285285
jvmcfg, sizeof(jvmcfg));
286286

287-
if (!IsJavaArgs()) {
288-
SetJvmEnvironment(argc,argv);
289-
}
287+
/* Set env. Must be done before LoadJavaVM. */
288+
SetJvmEnvironment(argc, argv);
290289

291290
ifn.CreateJavaVM = 0;
292291
ifn.GetDefaultJavaVMInitArgs = 0;
@@ -806,18 +805,22 @@ static void
806805
SetJvmEnvironment(int argc, char **argv) {
807806

808807
static const char* NMT_Env_Name = "NMT_LEVEL_";
808+
const char* NMT_Arg_Name = IsJavaArgs() ? "-J-XX:NativeMemoryTracking=" : "-XX:NativeMemoryTracking=";
809809
int i;
810810
/* process only the launcher arguments */
811811
for (i = 0; i < argc; i++) {
812812
char *arg = argv[i];
813813
/*
814-
* Since this must be a VM flag we stop processing once we see
815-
* an argument the launcher would not have processed beyond (such
816-
* as -version or -h), or an argument that indicates the following
817-
* arguments are for the application (i.e. the main class name, or
818-
* the -jar argument).
814+
* Java launcher (!IsJavaArgs()):
815+
* Since this must be a VM flag we stop processing once we see
816+
* an argument the launcher would not have processed beyond (such
817+
* as -version or -h), or an argument that indicates the following
818+
* arguments are for the application (i.e. the main class name, or
819+
* the -jar argument).
820+
* Other launchers (IsJavaArgs()):
821+
* All arguments have to be scanned to see if it is a -J argument.
819822
*/
820-
if (i > 0) {
823+
if (!IsJavaArgs() && i > 0) {
821824
char *prev = argv[i - 1];
822825
// skip non-dash arg preceded by class path specifiers
823826
if (*arg != '-' && IsWhiteSpaceOption(prev)) {
@@ -835,10 +838,10 @@ SetJvmEnvironment(int argc, char **argv) {
835838
* The argument is passed to the JVM, which will check validity.
836839
* The JVM is responsible for removing the env variable.
837840
*/
838-
if (JLI_StrCCmp(arg, "-XX:NativeMemoryTracking=") == 0) {
841+
if (JLI_StrCCmp(arg, NMT_Arg_Name) == 0) {
839842
int retval;
840843
// get what follows this parameter, include "="
841-
size_t pnlen = JLI_StrLen("-XX:NativeMemoryTracking=");
844+
size_t pnlen = JLI_StrLen(NMT_Arg_Name);
842845
if (JLI_StrLen(arg) > pnlen) {
843846
char* value = arg + pnlen;
844847
size_t pbuflen = pnlen + JLI_StrLen(value) + 10; // 10 max pid digits

test/jdk/tools/launcher/TestSpecialArgs.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 7124089 7131021 8042469 8066185 8074373
26+
* @bug 7124089 7131021 8042469 8066185 8074373 8258917
2727
* @summary Checks for Launcher special flags, such as MacOSX specific flags,
2828
* and JVM NativeMemoryTracking flags.
2929
* @modules jdk.compiler
@@ -252,6 +252,18 @@ void testNMArgumentProcessing() throws FileNotFoundException {
252252
ensureNoWarnings(tr);
253253
}
254254

255+
@Test
256+
void testNMTTools() throws FileNotFoundException {
257+
TestResult tr;
258+
// Tools (non-java launchers) should handle NTM (no "wrong launcher" warning).
259+
tr = doExec(jarCmd, "-J-XX:NativeMemoryTracking=summary", "--help");
260+
ensureNoWarnings(tr);
261+
262+
// And java terminal args (like "--help") don't stop "-J" args parsing.
263+
tr = doExec(jarCmd, "--help", "-J-XX:NativeMemoryTracking=summary");
264+
ensureNoWarnings(tr);
265+
}
266+
255267
void ensureNoWarnings(TestResult tr) {
256268
checkTestResult(tr);
257269
if (tr.contains("warning: Native Memory Tracking")) {

0 commit comments

Comments
 (0)