Skip to content

Commit 70ddb1b

Browse files
Alexey Pavlyutkinchhagedorn
authored andcommitted
8295322: Tests for JDK-8271459 were not backported to 11u
Co-authored-by: Christian Hagedorn <chagedorn@openjdk.org> Reviewed-by: phh
1 parent 8608557 commit 70ddb1b

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
import org.testng.Assert;
25+
import org.testng.annotations.Test;
26+
27+
/*
28+
* @test
29+
* @bug 8271459
30+
* @requires vm.compiler2.enabled
31+
* @summary C2 applies string opts to StringBuilder object created with a negative size and misses the NegativeArraySizeException.
32+
* @run testng TestNegativeStringBuilderCapacity
33+
*/
34+
class TestNegativeStringBuilderCapacity {
35+
36+
static final int pass_count = 10000;
37+
38+
static final String doIdenticalPositiveConst() throws Throwable {
39+
// C2 knows that argument is 5 and applies string opts without runtime check.
40+
StringBuilder sb = new StringBuilder(5); // StringBuilder object optimized away by string opts.
41+
return sb.toString(); // Call optimized away by string opts.
42+
}
43+
44+
@Test
45+
static final void testIdenticalPositiveConst() {
46+
int throw_count = 0;
47+
for ( int pass = 0; pass < pass_count; pass++ ) {
48+
try {
49+
String s = doIdenticalPositiveConst();
50+
} catch (NegativeArraySizeException e) {
51+
throw_count++;
52+
} catch (Throwable e) {
53+
Assert.fail("Unexpected exception thrown");
54+
}
55+
}
56+
Assert.assertEquals( throw_count, 0, String.format("%d exception were thrown, 0 expected", throw_count));
57+
}
58+
59+
static final String doIdenticalNegativeConst() throws Throwable {
60+
// C2 knows that we always have a negative int -> bail out of string opts
61+
StringBuilder sb = new StringBuilder(-5);
62+
return sb.toString(); // Call stays due to bailout.
63+
}
64+
65+
@Test
66+
static final void testIdenticalNegativeConst() {
67+
int throw_count = 0;
68+
for ( int pass = 0; pass < pass_count; pass++ ) {
69+
try {
70+
String s = doIdenticalNegativeConst();
71+
} catch (NegativeArraySizeException e) {
72+
throw_count++;
73+
} catch (Throwable e) {
74+
Assert.fail("Unexpected exception thrown");
75+
}
76+
}
77+
Assert.assertEquals( throw_count, pass_count, String.format("%d exception were thrown, %d expected", throw_count, pass_count));
78+
}
79+
80+
static int aField;
81+
82+
static final String doField() throws Throwable {
83+
// C2 does not know if iFld is positive or negative. It applies string opts but inserts a runtime check to
84+
// bail out to interpreter
85+
StringBuilder sb = new StringBuilder(aField);
86+
return sb.toString();
87+
}
88+
89+
@Test
90+
static final void testPositiveField() {
91+
aField = 4;
92+
int throw_count = 0;
93+
for ( int pass = 0; pass < pass_count; pass++ ) {
94+
try {
95+
String s = doField();
96+
} catch (NegativeArraySizeException e) {
97+
throw_count++;
98+
} catch (Throwable e) {
99+
Assert.fail("Unexpected exception thrown");
100+
}
101+
}
102+
Assert.assertEquals( throw_count, 0, String.format("%d exception were thrown, 0 expected", throw_count));
103+
}
104+
105+
@Test
106+
static final void testNegativeField() {
107+
aField = -4;
108+
int throw_count = 0;
109+
for ( int pass = 0; pass < pass_count; pass++ ) {
110+
try {
111+
String s = doField();
112+
} catch (NegativeArraySizeException e) {
113+
throw_count++;
114+
} catch (Throwable e) {
115+
Assert.fail("Unexpected exception thrown");
116+
}
117+
}
118+
Assert.assertEquals( throw_count, pass_count, String.format("%d exception were thrown, %d expected", throw_count, pass_count));
119+
}
120+
121+
static final String doPossiblyNegativeConst(boolean flag) throws Throwable {
122+
// C2 knows that cap is between -5 and 5. It applies string opts but inserts a runtime check to
123+
// bail out to interpreter. This path is sometimes taken and sometimes not.
124+
final int capacity = flag ? 5 : -5;
125+
StringBuilder sb = new StringBuilder(capacity);
126+
return sb.toString();
127+
}
128+
129+
@Test
130+
static final void testPossiblyNegativeConst() {
131+
int throw_count = 0;
132+
for ( int pass = 0; pass < pass_count; pass++ ) {
133+
try {
134+
String s = doPossiblyNegativeConst((pass % 2) == 0);
135+
} catch (NegativeArraySizeException e) {
136+
throw_count++;
137+
} catch (Throwable e) {
138+
Assert.fail("Unexpected exception thrown");
139+
}
140+
}
141+
Assert.assertEquals( throw_count, pass_count/2, String.format("%d exception were thrown, %d expected", throw_count, pass_count/2));
142+
}
143+
144+
static final String doPositiveConst(boolean flag) throws Throwable {
145+
// C2 knows that cap is between 1 and 100 and applies string opts without runtime check.
146+
final int capacity = flag ? 1 : 100;
147+
StringBuilder sb = new StringBuilder(capacity);
148+
return sb.toString();
149+
}
150+
151+
@Test
152+
static final void testPositiveConst() {
153+
int throw_count = 0;
154+
for ( int pass = 0; pass < pass_count; pass++ ) {
155+
try {
156+
String s = doPositiveConst((pass % 2) == 0);
157+
} catch (NegativeArraySizeException e) {
158+
throw_count++;
159+
} catch (Throwable e) {
160+
Assert.fail("Unexpected exception thrown");
161+
}
162+
}
163+
Assert.assertEquals( throw_count, 0, String.format("%d exception were thrown, 0 expected", throw_count));
164+
}
165+
166+
static final String doArg(int capacity) throws Throwable {
167+
// C2 does not know if cap is positive or negative. It applies string opts but inserts a runtime check to
168+
// bail out to interpreter. This path is always taken because cap is always negative.
169+
StringBuilder sb = new StringBuilder(capacity);
170+
return sb.toString();
171+
}
172+
173+
@Test
174+
static final void testArg() {
175+
int throw_count = 0;
176+
for ( int pass = 0; pass < pass_count; pass++ ) {
177+
try {
178+
String s = doArg((pass % 2) == 0 ? 3 : -3 );
179+
} catch (NegativeArraySizeException e) {
180+
throw_count++;
181+
} catch (Throwable e) {
182+
Assert.fail("Unexpected exception thrown");
183+
}
184+
}
185+
Assert.assertEquals( throw_count, pass_count/2, String.format("%d exception were thrown, %d expected", throw_count, pass_count/2));
186+
}
187+
}

0 commit comments

Comments
 (0)