Skip to content

Commit be0b9b1

Browse files
committed
8266615: C2 incorrectly folds subtype checks involving an interface array
Backport-of: ce88b33
1 parent ab9d3de commit be0b9b1

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

src/hotspot/share/opto/compile.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4316,16 +4316,22 @@ int Compile::static_subtype_check(ciKlass* superk, ciKlass* subk) {
43164316
}
43174317

43184318
ciType* superelem = superk;
4319-
if (superelem->is_array_klass())
4319+
ciType* subelem = subk;
4320+
if (superelem->is_array_klass()) {
43204321
superelem = superelem->as_array_klass()->base_element_type();
4322+
}
4323+
if (subelem->is_array_klass()) {
4324+
subelem = subelem->as_array_klass()->base_element_type();
4325+
}
43214326

43224327
if (!subk->is_interface()) { // cannot trust static interface types yet
43234328
if (subk->is_subtype_of(superk)) {
43244329
return SSC_always_true; // (1) false path dead; no dynamic test needed
43254330
}
43264331
if (!(superelem->is_klass() && superelem->as_klass()->is_interface()) &&
4332+
!(subelem->is_klass() && subelem->as_klass()->is_interface()) &&
43274333
!superk->is_subtype_of(subk)) {
4328-
return SSC_always_false;
4334+
return SSC_always_false; // (2) true path dead; no dynamic test needed
43294335
}
43304336
}
43314337

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2021, 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+
25+
/*
26+
* @test
27+
* @bug 8266615
28+
* @summary C2 incorrectly folds subtype checks involving an interface array.
29+
* @run main/othervm -Xbatch
30+
* compiler.types.TestInterfaceArraySubtypeCheck
31+
*/
32+
33+
package compiler.types;
34+
35+
public class TestInterfaceArraySubtypeCheck {
36+
37+
static interface MyInterface { }
38+
39+
static class MyClassA { }
40+
41+
static class MyClassB extends MyClassA implements MyInterface { }
42+
43+
static MyInterface[] getMyInterfaceArray() {
44+
return new MyClassB[0];
45+
}
46+
47+
static MyInterface getMyInterface() {
48+
return new MyClassB();
49+
}
50+
51+
static MyClassA[] test1() {
52+
return (MyClassA[])getMyInterfaceArray();
53+
}
54+
55+
static void test2() {
56+
if (!(getMyInterfaceArray() instanceof MyClassA[])) {
57+
throw new RuntimeException("test2 failed");
58+
}
59+
}
60+
61+
static MyClassA test3() {
62+
return (MyClassA)getMyInterface();
63+
}
64+
65+
static void test4() {
66+
if (!(getMyInterface() instanceof MyClassA)) {
67+
throw new RuntimeException("test4 failed");
68+
}
69+
}
70+
71+
public static void main(String[] args) {
72+
for (int i = 0; i < 50_000; ++i) {
73+
test1();
74+
test2();
75+
test3();
76+
test4();
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)