Skip to content

Commit a95edee

Browse files
author
Harold Seigel
committedMar 1, 2022
8281472: JVM options processing silently truncates large illegal options values
Reviewed-by: dholmes, iklam
1 parent 44d599a commit a95edee

File tree

5 files changed

+135
-12
lines changed

5 files changed

+135
-12
lines changed
 

‎src/hotspot/share/runtime/arguments.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -862,14 +862,29 @@ static bool set_numeric_flag(JVMFlag* flag, char* value, JVMFlagOrigin origin) {
862862
if (is_neg) {
863863
int_v = -int_v;
864864
}
865+
if ((!is_neg && v > max_jint) || (is_neg && -(intx)v < min_jint)) {
866+
return false;
867+
}
865868
return JVMFlagAccess::set_int(flag, &int_v, origin) == JVMFlag::SUCCESS;
866869
} else if (flag->is_uint()) {
870+
if (v > max_juint) {
871+
return false;
872+
}
867873
uint uint_v = (uint) v;
868874
return JVMFlagAccess::set_uint(flag, &uint_v, origin) == JVMFlag::SUCCESS;
869875
} else if (flag->is_intx()) {
870876
intx_v = (intx) v;
871877
if (is_neg) {
872-
intx_v = -intx_v;
878+
if (intx_v != min_intx) {
879+
intx_v = - intx_v;
880+
if (intx_v > 0) {
881+
return false; // underflow
882+
}
883+
}
884+
} else {
885+
if (intx_v < 0) {
886+
return false; // overflow
887+
}
873888
}
874889
return JVMFlagAccess::set_intx(flag, &intx_v, origin) == JVMFlag::SUCCESS;
875890
} else if (flag->is_uintx()) {

‎src/hotspot/share/runtime/arguments.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class Arguments : AllStatic {
237237
friend class JvmtiExport;
238238
friend class CodeCacheExtensions;
239239
friend class ArgumentsTest;
240+
friend class LargeOptionsTest;
240241
public:
241242
// Operation modi
242243
enum Mode {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
#include "precompiled.hpp"
25+
#include "compiler/compiler_globals.hpp"
26+
#include "runtime/arguments.hpp"
27+
#include "runtime/flags/jvmFlag.hpp"
28+
#include "runtime/globals.hpp"
29+
#include "unittest.hpp"
30+
31+
class LargeOptionsTest : public ::testing::Test {
32+
public:
33+
static bool test_option_value(const char* option, intx value) {
34+
char buffer[100];
35+
UnlockDiagnosticVMOptions = true;
36+
os::snprintf(buffer, 100, "%s=" INTX_FORMAT, option, value);
37+
return Arguments::parse_argument(buffer, JVMFlagOrigin::COMMAND_LINE);
38+
}
39+
40+
static bool test_option_value(const char* option) {
41+
UnlockDiagnosticVMOptions = true;
42+
return Arguments::parse_argument(option, JVMFlagOrigin::COMMAND_LINE);
43+
}
44+
};
45+
46+
47+
// CompilerDirectivesLimit is a diagnostic int option.
48+
TEST_VM(LARGE_OPTION, large_ints) {
49+
for (intx x = max_jint - 1; x <= (intx)max_jint + 1; x++) {
50+
bool result = LargeOptionsTest::test_option_value("CompilerDirectivesLimit", x);
51+
if (x > max_jint) {
52+
ASSERT_FALSE(result);
53+
} else {
54+
ASSERT_TRUE(result);
55+
ASSERT_EQ(CompilerDirectivesLimit, x);
56+
}
57+
}
58+
}
59+
60+
61+
TEST_VM(LARGE_OPTION, small_ints) {
62+
for (intx x = min_jint + 1; x >= (intx)min_jint - 1; x--) {
63+
bool result = LargeOptionsTest::test_option_value("CompilerDirectivesLimit", x);
64+
if (x < min_jint) {
65+
ASSERT_FALSE(result);
66+
} else {
67+
ASSERT_TRUE(result);
68+
ASSERT_EQ(CompilerDirectivesLimit, x);
69+
}
70+
}
71+
}
72+
73+
74+
TEST_VM(LARGE_OPTION, large_int_overflow) { // Test 0x100000000
75+
ASSERT_FALSE(LargeOptionsTest::test_option_value("CompilerDirectivesLimit", 4294967296));
76+
}
77+
78+
79+
// HandshakeTimeout is a diagnostic uint option.
80+
TEST_VM(LARGE_OPTION, large_uints) {
81+
for (uintx x = max_juint - 1; x <= (uintx)max_juint + 1; x++) {
82+
bool result = LargeOptionsTest::test_option_value("HandshakeTimeout", x);
83+
if (x <= max_juint) {
84+
ASSERT_TRUE(result);
85+
ASSERT_EQ(HandshakeTimeout, x);
86+
} else {
87+
ASSERT_FALSE(result);
88+
}
89+
}
90+
}
91+
92+
93+
// MaxJNILocalCapacity is an intx option.
94+
TEST_VM(LARGE_OPTION, large_intxs) {
95+
// max_intx + 1 equals min_intx!
96+
for (julong x = max_intx - 1; x <= (julong)max_intx + 1; x++) {
97+
ASSERT_TRUE(LargeOptionsTest::test_option_value("MaxJNILocalCapacity", x));
98+
ASSERT_EQ((julong)MaxJNILocalCapacity, x);
99+
}
100+
}
101+
102+
103+
TEST_VM(LARGE_OPTION, small_intxs) {
104+
ASSERT_TRUE(LargeOptionsTest::test_option_value("MaxJNILocalCapacity", min_intx + 1));
105+
ASSERT_EQ(MaxJNILocalCapacity, -9223372036854775807);
106+
ASSERT_TRUE(LargeOptionsTest::test_option_value("MaxJNILocalCapacity", min_intx));
107+
ASSERT_EQ(MaxJNILocalCapacity, min_intx);
108+
// Test value that's less than min_intx (-0x8000000000000001).
109+
ASSERT_FALSE(LargeOptionsTest::test_option_value("MaxJNILocalCapacity=-9223372036854775809"));
110+
}

‎test/hotspot/jtreg/gc/arguments/TestParallelGCThreads.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,12 @@ public static void testFlags() throws Exception {
115115

116116
// 4294967295 == (unsigned int) -1
117117
// So setting ParallelGCThreads=4294967295 should give back 4294967295
118-
// and setting ParallelGCThreads=4294967296 should give back 0. (SerialGC is ok with ParallelGCThreads=0)
119-
for (long i = 4294967295L; i <= 4294967296L; i++) {
120-
long count = getParallelGCThreadCount(
121-
"-XX:+UseSerialGC",
122-
"-XX:ParallelGCThreads=" + i,
123-
"-XX:+PrintFlagsFinal",
124-
"-version");
125-
Asserts.assertEQ(count, i % 4294967296L, "Specifying ParallelGCThreads=" + i + " does not set the thread count properly!");
126-
}
118+
long count = getParallelGCThreadCount(
119+
"-XX:+UseSerialGC",
120+
"-XX:ParallelGCThreads=4294967295",
121+
"-XX:+PrintFlagsFinal",
122+
"-version");
123+
Asserts.assertEQ(count, 4294967295L, "Specifying ParallelGCThreads=4294967295 does not set the thread count properly!");
127124
}
128125

129126
public static long getParallelGCThreadCount(String... flags) throws Exception {

‎test/hotspot/jtreg/testlibrary/ctw/src/sun/hotspot/tools/ctw/CtwRunner.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2022, 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
@@ -291,7 +291,7 @@ private String[] cmd(long classStart, long classStop) {
291291
"-XX:+StressIGVN",
292292
"-XX:+StressCCP",
293293
// StressSeed is uint
294-
"-XX:StressSeed=" + Math.abs(rng.nextLong()),
294+
"-XX:StressSeed=" + Math.abs(rng.nextInt()),
295295
// CTW entry point
296296
CompileTheWorld.class.getName(),
297297
target));

0 commit comments

Comments
 (0)
Please sign in to comment.