From 2e34a2ebf0f14043b129461b0397495e7e75a38b Mon Sep 17 00:00:00 2001 From: Thomas Stuefe Date: Wed, 15 Nov 2023 09:55:51 +0000 Subject: [PATCH] 8318671: Potential uninitialized uintx value after JDK-8317683 Reviewed-by: thartmann, shade --- src/hotspot/share/compiler/compilerOracle.cpp | 43 ++++++++----- .../compilercontrol/commands/MemStatTest.java | 62 +++++++++++++++++++ .../compiler/CompilerMemoryStatisticTest.java | 12 ---- 3 files changed, 88 insertions(+), 29 deletions(-) create mode 100644 test/hotspot/jtreg/compiler/compilercontrol/commands/MemStatTest.java diff --git a/src/hotspot/share/compiler/compilerOracle.cpp b/src/hotspot/share/compiler/compilerOracle.cpp index 56fed1394c2..4ce0cd772e7 100644 --- a/src/hotspot/share/compiler/compilerOracle.cpp +++ b/src/hotspot/share/compiler/compilerOracle.cpp @@ -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; diff --git a/test/hotspot/jtreg/compiler/compilercontrol/commands/MemStatTest.java b/test/hotspot/jtreg/compiler/compilercontrol/commands/MemStatTest.java new file mode 100644 index 00000000000..2b6208652d3 --- /dev/null +++ b/test/hotspot/jtreg/compiler/compilercontrol/commands/MemStatTest.java @@ -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 + } +} diff --git a/test/hotspot/jtreg/serviceability/dcmd/compiler/CompilerMemoryStatisticTest.java b/test/hotspot/jtreg/serviceability/dcmd/compiler/CompilerMemoryStatisticTest.java index 3a4f34c2c99..7d4a38db47b 100644 --- a/test/hotspot/jtreg/serviceability/dcmd/compiler/CompilerMemoryStatisticTest.java +++ b/test/hotspot/jtreg/serviceability/dcmd/compiler/CompilerMemoryStatisticTest.java @@ -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 {