1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*
* Copyright (c) 2023, Red Hat, Inc. 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 8303737
* @summary C2: Load can bypass subtype check that enforces it's from the right object type
* @requires vm.gc.Parallel
* @requires vm.compiler2.enabled
* @run main/othervm -XX:-TieredCompilation -XX:-BackgroundCompilation -XX:-UseOnStackReplacement -XX:CompileOnly=TestLoadBypassesClassCast::test
* -XX:CompileThreshold=20000 -XX:LoopMaxUnroll=1 -XX:-LoopUnswitching -XX:+UseParallelGC TestLoadBypassesClassCast
*
*/
public class TestLoadBypassesClassCast {
private static Object saved_o;
private static Object field_o = new A();
private static Object saved_casted_o;
private static float barrier;
private static Object[] memory = new Object[100];
public static void main(String[] args) {
float[] array = new float[100];
A a = new A();
B b = new B();
C c = new C();
D d = new D();
// create garbage so GC runs
Thread thread = new Thread() {
public void run() {
while (true) {
int[] array = new int[1000];
}
}
};
thread.setDaemon(true);
thread.start();
for (int i = 0; i < 20_000; i++) {
test(true, a, array, true, false);
test(false, b, array, true, false);
test(false, d, array, true, true);
test(true, a, array, false, false);
test(false, b, array, false, false);
testHelper2(42);
testHelper3(true, 42);
}
for (int j = 0; j < 1000; j++) {
for (int i = 0; i < 1_000_000; i++) {
test(false, d, array, true, true);
}
}
}
private static int test(boolean flag, Object o, float[] array, boolean flag2, boolean flag3) {
int ret = (int)array[2];
if (o == null) {
}
saved_o = o; // (CastPP o): cast to not null
// A.objectField load from o hosted here even though o was not checked to be of type A
// result of the load doesn't hold an oop if o is not an A
if (flag2) {
for (int i = 1; i < 100; i *= 2) {
// safepoint here with result of load above live and expected to be an oop. Not the case
// if o is of type D: crash in gc code
}
if (flag3) {
} else {
saved_casted_o = (A) o; // (CheckCastPP (CastPP o)): cast to not null A
int j;
for (j = 1; j < 2; j *= 2) {
}
testHelper3(flag, j); // goes away after CCP
int i;
for (i = 0; i < 2; i++) {
}
// array[2] after one round of loop opts, control
// dependent on range check, range check replaced by
// array[2] range check above, control dependent
// nodes become control dependent on that range check
ret += array[i];
Object o2;
if (flag) {
o2 = saved_casted_o; // (CheckCastPP (CastPP o)): cast to to not null A
} else {
o2 = testHelper2(i); // (CastPP o) after 1 round of loop opts: cast to not null
}
// subtype check split thru Phi. CheckCastPP becomes control dependent on merge point
// phi becomes (CastPP o) after 1 round of loop opts: cast to not null
// subtype check from split thru phi in one branch of the if replaced by dominating one
// empty if blocks, if goes away. CheckCastPP becomes control dependent on range check above
// CastPP replaced by dominating CastPP for null check
A a = (A) o2;
ret += a.objectField.intField;
}
} else {
// same logic as above so if this a.objectField load and
// the one above lose their dependency on the type check
// they common above all ifs
saved_casted_o = (A) o;
int j;
for (j = 1; j < 2; j *= 2) {
}
testHelper3(flag, j);
int i;
for (i = 0; i < 2; i++) {
}
ret += array[i];
Object o2;
if (flag) {
o2 = saved_casted_o;
} else {
o2 = testHelper2(i);
}
A a = (A) o2;
ret += a.objectField.intField;
ret += barrier;
}
return ret;
}
private static void testHelper3(boolean flag, int j) {
if (j == 2) {
if (flag) {
barrier = 42;
}
}
}
private static Object testHelper2(int i) {
Object o2;
if (i == 2) {
o2 = saved_o;
} else {
o2 = field_o;
if (o2 == null) {
}
}
return o2;
}
private static class C {
}
private static class A extends C {
public E objectField = new E();
}
private static class B extends A {
}
private static class D extends C {
public int neverAccessedField = 0x12345678;
}
private static class E {
public int intField;
}
}
|