Skip to content

Commit c6c451a

Browse files
author
Afshin Zafari
committed
8353468: [ubsan] arguments.cpp:2422:23: runtime error: 2.14748e+11 is outside the range of representable values of type 'int'
Reviewed-by: stefank, dholmes
1 parent a17058b commit c6c451a

File tree

2 files changed

+83
-29
lines changed

2 files changed

+83
-29
lines changed

src/hotspot/share/runtime/arguments.cpp

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,30 +2414,54 @@ jint Arguments::parse_each_vm_init_arg(const JavaVMInitArgs* args, JVMFlagOrigin
24142414
// Xmaxf
24152415
} else if (match_option(option, "-Xmaxf", &tail)) {
24162416
char* err;
2417-
int maxf = (int)(strtod(tail, &err) * 100);
2417+
double dmaxf = strtod(tail, &err);
24182418
if (*err != '\0' || *tail == '\0') {
24192419
jio_fprintf(defaultStream::error_stream(),
2420-
"Bad max heap free percentage size: %s\n",
2420+
"Bad max heap free ratio: %s\n",
2421+
option->optionString);
2422+
return JNI_EINVAL;
2423+
}
2424+
if (dmaxf < 0.0 || dmaxf > 1.0) {
2425+
jio_fprintf(defaultStream::error_stream(),
2426+
"-Xmaxf value (%s) is outside the allowed range [ 0.0 ... 1.0 ]\n",
24212427
option->optionString);
24222428
return JNI_EINVAL;
2423-
} else {
2424-
if (FLAG_SET_CMDLINE(MaxHeapFreeRatio, maxf) != JVMFlag::SUCCESS) {
2425-
return JNI_EINVAL;
2426-
}
2429+
}
2430+
const uintx umaxf = (uintx)(dmaxf * 100);
2431+
if (MinHeapFreeRatio > umaxf) {
2432+
jio_fprintf(defaultStream::error_stream(),
2433+
"-Xmaxf value (%s) must be greater than or equal to the implicit -Xminf value (%.2f)\n",
2434+
tail, MinHeapFreeRatio / 100.0f);
2435+
return JNI_EINVAL;
2436+
}
2437+
if (FLAG_SET_CMDLINE(MaxHeapFreeRatio, umaxf) != JVMFlag::SUCCESS) {
2438+
return JNI_EINVAL;
24272439
}
24282440
// Xminf
24292441
} else if (match_option(option, "-Xminf", &tail)) {
24302442
char* err;
2431-
int minf = (int)(strtod(tail, &err) * 100);
2443+
double dminf = strtod(tail, &err);
24322444
if (*err != '\0' || *tail == '\0') {
24332445
jio_fprintf(defaultStream::error_stream(),
2434-
"Bad min heap free percentage size: %s\n",
2446+
"Bad min heap free ratio: %s\n",
24352447
option->optionString);
24362448
return JNI_EINVAL;
2437-
} else {
2438-
if (FLAG_SET_CMDLINE(MinHeapFreeRatio, minf) != JVMFlag::SUCCESS) {
2439-
return JNI_EINVAL;
2440-
}
2449+
}
2450+
if (dminf < 0.0 || dminf > 1.0) {
2451+
jio_fprintf(defaultStream::error_stream(),
2452+
"-Xminf value (%s) is outside the allowed range [ 0.0 ... 1.0 ]\n",
2453+
tail);
2454+
return JNI_EINVAL;
2455+
}
2456+
const uintx uminf = (uintx)(dminf * 100);
2457+
if (MaxHeapFreeRatio < uminf) {
2458+
jio_fprintf(defaultStream::error_stream(),
2459+
"-Xminf value (%s) must be less than or equal to the implicit -Xmaxf value (%.2f)\n",
2460+
tail, MaxHeapFreeRatio / 100.0f);
2461+
return JNI_EINVAL;
2462+
}
2463+
if (FLAG_SET_CMDLINE(MinHeapFreeRatio, uminf) != JVMFlag::SUCCESS) {
2464+
return JNI_EINVAL;
24412465
}
24422466
// -Xss
24432467
} else if (match_option(option, "-Xss", &tail)) {

test/hotspot/jtreg/gc/arguments/TestHeapFreeRatio.java

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, 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
@@ -47,24 +47,19 @@ enum Validation {
4747
COMBINATION_INVALID
4848
}
4949

50-
private static void testMinMaxFreeRatio(String min, String max, Validation type) throws Exception {
51-
OutputAnalyzer output = GCArguments.executeTestJava(
52-
"-Xminf" + min,
53-
"-Xmaxf" + max,
54-
"-version");
55-
50+
private static void checkValidity(OutputAnalyzer output, String min, String max, Validation type) {
5651
switch (type) {
5752
case VALID:
5853
output.shouldNotContain("Error");
5954
output.shouldHaveExitValue(0);
6055
break;
6156
case MIN_INVALID:
62-
output.shouldContain("Bad min heap free percentage size: -Xminf" + min);
57+
output.shouldContain("Bad min heap free ratio: -Xminf" + min);
6358
output.shouldContain("Error");
6459
output.shouldHaveExitValue(1);
6560
break;
6661
case MAX_INVALID:
67-
output.shouldContain("Bad max heap free percentage size: -Xmaxf" + max);
62+
output.shouldContain("Bad max heap free ratio: -Xmaxf" + max);
6863
output.shouldContain("Error");
6964
output.shouldHaveExitValue(1);
7065
break;
@@ -74,7 +69,7 @@ private static void testMinMaxFreeRatio(String min, String max, Validation type)
7469
output.shouldHaveExitValue(1);
7570
break;
7671
case COMBINATION_INVALID:
77-
output.shouldContain("must be less than or equal to MaxHeapFreeRatio");
72+
output.shouldMatch("must be (less|greater) than or equal to the implicit -Xm..f value");
7873
output.shouldContain("Error");
7974
output.shouldHaveExitValue(1);
8075
break;
@@ -85,10 +80,29 @@ private static void testMinMaxFreeRatio(String min, String max, Validation type)
8580
System.out.println(output.getOutput());
8681
}
8782

83+
private static void testMinMaxFreeRatio_Reordered(String min, String max, Validation type) throws Exception {
84+
OutputAnalyzer output = GCArguments.executeTestJava(
85+
"-Xmaxf" + max,
86+
"-Xminf" + min,
87+
"-version");
88+
89+
checkValidity(output, min, max, type);
90+
}
91+
92+
private static void testMinMaxFreeRatio(String min, String max, Validation type) throws Exception {
93+
OutputAnalyzer output = GCArguments.executeTestJava(
94+
"-Xminf" + min,
95+
"-Xmaxf" + max,
96+
"-version");
97+
98+
checkValidity(output, min, max, type);
99+
}
100+
88101
public static void main(String args[]) throws Exception {
89-
testMinMaxFreeRatio( "0.1", "0.5", Validation.VALID);
90-
testMinMaxFreeRatio( ".1", ".5", Validation.VALID);
91-
testMinMaxFreeRatio( "0.5", "0.5", Validation.VALID);
102+
testMinMaxFreeRatio( "0.1", "0.5", Validation.VALID);
103+
testMinMaxFreeRatio( ".1", ".5", Validation.VALID);
104+
testMinMaxFreeRatio( "0.5", "0.5", Validation.VALID);
105+
testMinMaxFreeRatio( "0.0","0.001", Validation.VALID);
92106

93107
testMinMaxFreeRatio("=0.1", "0.5", Validation.MIN_INVALID);
94108
testMinMaxFreeRatio("0.1f", "0.5", Validation.MIN_INVALID);
@@ -103,14 +117,30 @@ public static void main(String args[]) throws Exception {
103117
testMinMaxFreeRatio("-0.1", "0.5", Validation.OUT_OF_RANGE);
104118
testMinMaxFreeRatio( "1.1", "0.5", Validation.OUT_OF_RANGE);
105119
testMinMaxFreeRatio(
106-
"2147483647", "0.5", Validation.OUT_OF_RANGE);
120+
"2147483647", "0.5", Validation.OUT_OF_RANGE);
121+
testMinMaxFreeRatio(
122+
"-2147483647", "0.5", Validation.OUT_OF_RANGE);
107123
testMinMaxFreeRatio( "0.1", "-0.5", Validation.OUT_OF_RANGE);
108124
testMinMaxFreeRatio( "0.1", "1.5", Validation.OUT_OF_RANGE);
109125
testMinMaxFreeRatio(
110126
"0.1", "2147483647", Validation.OUT_OF_RANGE);
127+
testMinMaxFreeRatio(
128+
"0.1", "-2147483647", Validation.OUT_OF_RANGE);
129+
130+
testMinMaxFreeRatio( "0.5", "0.1", Validation.COMBINATION_INVALID);
131+
testMinMaxFreeRatio( ".5", ".10", Validation.COMBINATION_INVALID);
132+
testMinMaxFreeRatio("0.12", "0.100", Validation.COMBINATION_INVALID);
133+
134+
// Default range is [0.40, 0.70]
135+
// setting minf to 0.80 violates minf < maxf
136+
testMinMaxFreeRatio("0.80", "0.90", Validation.COMBINATION_INVALID);
137+
138+
// Options are re-ordered: -Xmaxf is given before -Xminf
139+
140+
// valid range, but acceptable only if maxf be set first
141+
testMinMaxFreeRatio_Reordered("0.80", "0.90", Validation.VALID);
111142

112-
testMinMaxFreeRatio( "0.5", "0.1", Validation.COMBINATION_INVALID);
113-
testMinMaxFreeRatio( ".5", ".10", Validation.COMBINATION_INVALID);
114-
testMinMaxFreeRatio("0.12","0.100", Validation.COMBINATION_INVALID);
143+
// although a valid range, but setting maxf first violates minf < maxf
144+
testMinMaxFreeRatio_Reordered("0.10", "0.30", Validation.COMBINATION_INVALID);
115145
}
116146
}

0 commit comments

Comments
 (0)