8318671: Potential uninitialized uintx value after JDK-8317683

Reviewed-by: thartmann, shade
This commit is contained in:
Thomas Stuefe 2023-11-15 09:55:51 +00:00
parent fac6b51699
commit 2e34a2ebf0
3 changed files with 88 additions and 29 deletions

View File

@ -673,20 +673,27 @@ static bool parseMemLimit(const char* line, intx& value, int& bytes_read, char*
return true;
}
static bool parseEnumValueAsUintx(enum CompileCommand option, const char* line, uintx& value, int& bytes_read, char* errorbuf, const int buf_size) {
if (option == CompileCommand::MemStat) {
if (strncasecmp(line, "collect", 7) == 0) {
value = (uintx)MemStatAction::collect;
} else if (strncasecmp(line, "print", 5) == 0) {
value = (uintx)MemStatAction::print;
print_final_memstat_report = true;
} else {
jio_snprintf(errorbuf, buf_size, "MemStat: invalid value expected 'collect' or 'print' (omitting value means 'collect')");
}
return true; // handled
static bool parseMemStat(const char* line, uintx& value, int& bytes_read, char* errorbuf, const int buf_size) {
#define IF_ENUM_STRING(S, CMD) \
if (strncasecmp(line, S, strlen(S)) == 0) { \
bytes_read += (int)strlen(S); \
CMD \
return true; \
}
IF_ENUM_STRING("collect", {
value = (uintx)MemStatAction::collect;
});
IF_ENUM_STRING("print", {
value = (uintx)MemStatAction::print;
print_final_memstat_report = true;
});
#undef IF_ENUM_STRING
jio_snprintf(errorbuf, buf_size, "MemStat: invalid option");
return false;
#undef HANDLE_VALUE
}
static void scan_value(enum OptionType type, char* line, int& total_bytes_read,
@ -714,11 +721,13 @@ static void scan_value(enum OptionType type, char* line, int& total_bytes_read,
}
} else if (type == OptionType::Uintx) {
uintx value;
// Is it a named enum?
bool success = parseEnumValueAsUintx(option, line, value, bytes_read, errorbuf, buf_size);
if (!success) {
// Is it a raw number?
success = (sscanf(line, "" UINTX_FORMAT "%n", &value, &bytes_read) == 1);
bool success = false;
if (option == CompileCommand::MemStat) {
// Special parsing for MemStat
success = parseMemStat(line, value, bytes_read, errorbuf, buf_size);
} else {
// parse as raw number
success = sscanf(line, "" UINTX_FORMAT "%n", &value, &bytes_read) == 1;
}
if (success) {
total_bytes_read += bytes_read;

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8318671
* @summary Tests various ways to call memstat
* @library /test/lib /
*
* @run driver compiler.compilercontrol.commands.MemStatTest
*/
package compiler.compilercontrol.commands;
import jdk.test.lib.process.ProcessTools;
public class MemStatTest {
public static void main(String[] args) throws Exception {
// default => collect
ProcessTools.executeTestJvm("-XX:CompileCommand=MemStat,*.*", "-version")
.shouldHaveExitValue(0)
.shouldNotContain("CompileCommand: An error occurred during parsing")
.shouldContain("CompileCommand: MemStat *.* uintx MemStat = 1"); // should be registered
// collect explicit
ProcessTools.executeTestJvm("-XX:CompileCommand=MemStat,*.*,collect", "-version")
.shouldHaveExitValue(0)
.shouldNotContain("CompileCommand: An error occurred during parsing")
.shouldContain("CompileCommand: MemStat *.* uintx MemStat = 1"); // should be registered
// print explicit
ProcessTools.executeTestJvm("-XX:CompileCommand=MemStat,*.*,print", "-version")
.shouldHaveExitValue(0)
.shouldNotContain("CompileCommand: An error occurred during parsing")
.shouldContain("CompileCommand: MemStat *.* uintx MemStat = 2");
// invalid suboption
ProcessTools.executeTestJvm("-XX:CompileCommand=MemStat,*.*,invalid", "-version")
.shouldNotHaveExitValue(0)
.shouldContain("CompileCommand: An error occurred during parsing")
.shouldContain("Error: Value cannot be read for option 'MemStat'")
.shouldNotContain("CompileCommand: MemStat"); // should *NOT* be registered
}
}

View File

@ -39,18 +39,6 @@ import java.util.Iterator;
* @run main/othervm -XX:CompileCommand=memstat,*.* CompilerMemoryStatisticTest
*/
/*
* @test CompilerMemoryStatisticTest
* @summary Test Compiler.memory
* @requires vm.compiler1.enabled
* @requires vm.compiler2.enabled
*
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.management
* @run main/othervm -XX:CompileCommand=memstat,*.*,collect CompilerMemoryStatisticTest
*/
public class CompilerMemoryStatisticTest {
public static void main(String args[]) throws Exception {