Skip to content

Commit

Permalink
8318671: Potential uninitialized uintx value after JDK-8317683
Browse files Browse the repository at this point in the history
Reviewed-by: thartmann, shade
  • Loading branch information
tstuefe committed Nov 15, 2023
1 parent fac6b51 commit 2e34a2e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 29 deletions.
43 changes: 26 additions & 17 deletions src/hotspot/share/compiler/compilerOracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@
* @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 {
Expand Down

1 comment on commit 2e34a2e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.