1 /* 2 * Copyright (c) 2020, 2024, 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 // TODO 8325106 Fix and enable _arraycopy intrinsic for this test (fails with -XX:-TieredCompilation). We need more test coverage because other tests don't seem to trigger the failure. 25 26 /** 27 * @test 28 * @bug 8252506 29 * @summary Verify that arraycopy intrinsics properly handle flat value class arrays with oop fields. 30 * @library /test/lib 31 * @enablePreview 32 * @modules java.base/jdk.internal.value 33 * java.base/jdk.internal.vm.annotation 34 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_arraycopy 35 * -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.TestArrayCopyWithOops::test* 36 * -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.TestArrayCopyWithOops::create* 37 * -Xbatch 38 * compiler.valhalla.inlinetypes.TestArrayCopyWithOops 39 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_arraycopy 40 * -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.TestArrayCopyWithOops::test* 41 * -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.TestArrayCopyWithOops::create* 42 * -Xbatch -XX:FlatArrayElementMaxSize=0 43 * compiler.valhalla.inlinetypes.TestArrayCopyWithOops 44 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_arraycopy 45 * compiler.valhalla.inlinetypes.TestArrayCopyWithOops 46 */ 47 48 package compiler.valhalla.inlinetypes; 49 50 import java.util.Arrays; 51 52 import jdk.test.lib.Asserts; 53 54 import jdk.internal.value.ValueClass; 55 import jdk.internal.vm.annotation.ImplicitlyConstructible; 56 import jdk.internal.vm.annotation.LooselyConsistentValue; 57 import jdk.internal.vm.annotation.NullRestricted; 58 59 public class TestArrayCopyWithOops { 60 static final int LEN = 200; 61 62 static class MyObject { 63 long val = Integer.MAX_VALUE; 64 } 65 66 @ImplicitlyConstructible 67 @LooselyConsistentValue 68 static value class ManyOops { 69 MyObject o1 = new MyObject(); 70 MyObject o2 = new MyObject(); 71 MyObject o3 = new MyObject(); 72 MyObject o4 = new MyObject(); 73 74 long hash() { 75 return o1.val + o2.val + o3.val + o4.val; 76 } 77 } 78 79 static ManyOops[] createValueClassArray() { 80 ManyOops[] array = (ManyOops[])ValueClass.newNullRestrictedArray(ManyOops.class, LEN); 81 for (int i = 0; i < LEN; ++i) { 82 array[i] = new ManyOops(); 83 } 84 return array; 85 } 86 87 static Object[] createObjectArray() { 88 return createValueClassArray(); 89 } 90 91 static Object createObject() { 92 return createValueClassArray(); 93 } 94 95 // System.arraycopy tests 96 97 static void test1(ManyOops[] dst) { 98 System.arraycopy(createValueClassArray(), 0, dst, 0, LEN); 99 } 100 101 static void test2(Object[] dst) { 102 System.arraycopy(createObjectArray(), 0, dst, 0, LEN); 103 } 104 105 static void test3(ManyOops[] dst) { 106 System.arraycopy(createObjectArray(), 0, dst, 0, LEN); 107 } 108 109 static void test4(Object[] dst) { 110 System.arraycopy(createValueClassArray(), 0, dst, 0, LEN); 111 } 112 113 // System.arraycopy tests (tightly coupled with allocation of dst array) 114 115 static Object[] test5() { 116 ManyOops[] dst = (ManyOops[])ValueClass.newNullRestrictedArray(ManyOops.class, LEN); 117 System.arraycopy(createValueClassArray(), 0, dst, 0, LEN); 118 return dst; 119 } 120 121 static Object[] test6() { 122 Object[] dst = new Object[LEN]; 123 System.arraycopy(createObjectArray(), 0, dst, 0, LEN); 124 return dst; 125 } 126 127 static Object[] test7() { 128 ManyOops[] dst = (ManyOops[])ValueClass.newNullRestrictedArray(ManyOops.class, LEN); 129 System.arraycopy(createObjectArray(), 0, dst, 0, LEN); 130 return dst; 131 } 132 133 static Object[] test8() { 134 Object[] dst = new Object[LEN]; 135 System.arraycopy(createValueClassArray(), 0, dst, 0, LEN); 136 return dst; 137 } 138 139 // Arrays.copyOf tests 140 141 // TODO 8325106 test9/test11 and test10/test12 are equivalent 142 // Using ManyOops[].class in both test9/11 and running with -XX:+IgnoreUnrecognizedVMOptions -XX:-TieredCompilation -XX:-DoEscapeAnalysis -XX:+AlwaysIncrementalInline triggers an exception 143 static Object[] test9() { 144 ManyOops[] src = createValueClassArray(); 145 return Arrays.copyOf(src, LEN, src.getClass()); 146 } 147 148 static Object[] test10() { 149 return Arrays.copyOf(createObjectArray(), LEN, Object[].class); 150 } 151 152 static Object[] test11() { 153 ManyOops[] src = createValueClassArray(); 154 return Arrays.copyOf(src, LEN, src.getClass()); 155 } 156 157 static Object[] test12() { 158 return Arrays.copyOf(createObjectArray(), LEN, Object[].class); 159 } 160 161 // System.arraycopy test using generic_copy stub 162 163 static void test13(Object dst) { 164 System.arraycopy(createObject(), 0, dst, 0, LEN); 165 } 166 167 static void produceGarbage() { 168 for (int i = 0; i < 100; ++i) { 169 Object[] arrays = new Object[1024]; 170 for (int j = 0; j < arrays.length; j++) { 171 arrays[i] = new int[1024]; 172 } 173 } 174 System.gc(); 175 } 176 177 public static void main(String[] args) { 178 ManyOops[] dst1 = createValueClassArray(); 179 ManyOops[] dst2 = createValueClassArray(); 180 ManyOops[] dst3 = createValueClassArray(); 181 ManyOops[] dst4 = createValueClassArray(); 182 ManyOops[] dst13 = createValueClassArray(); 183 184 // Warmup runs to trigger compilation 185 for (int i = 0; i < 50_000; ++i) { 186 test1(dst1); 187 test2(dst2); 188 test3(dst3); 189 test4(dst4); 190 test5(); 191 test6(); 192 test7(); 193 test8(); 194 test9(); 195 test10(); 196 test11(); 197 test12(); 198 test13(dst13); 199 } 200 201 // Trigger GC to make sure dst arrays are moved to old gen 202 produceGarbage(); 203 204 // Move data from flat src to flat dest 205 test1(dst1); 206 test2(dst2); 207 test3(dst3); 208 test4(dst4); 209 Object[] dst5 = test5(); 210 Object[] dst6 = test6(); 211 Object[] dst7 = test7(); 212 Object[] dst8 = test8(); 213 Object[] dst9 = test9(); 214 Object[] dst10 = test10(); 215 Object[] dst11 = test11(); 216 Object[] dst12 = test12(); 217 test13(dst13); 218 219 // Trigger GC again to make sure that the now dead src arrays are collected. 220 // MyObjects should be kept alive via oop references from the dst array. 221 produceGarbage(); 222 223 // Verify content 224 long expected = 4L*Integer.MAX_VALUE; 225 for (int i = 0; i < LEN; ++i) { 226 Asserts.assertEquals(dst1[i].hash(), expected); 227 Asserts.assertEquals(dst2[i].hash(), expected); 228 Asserts.assertEquals(dst3[i].hash(), expected); 229 Asserts.assertEquals(dst4[i].hash(), expected); 230 Asserts.assertEquals(((ManyOops)dst5[i]).hash(), expected); 231 Asserts.assertEquals(((ManyOops)dst7[i]).hash(), expected); 232 Asserts.assertEquals(((ManyOops)dst8[i]).hash(), expected); 233 Asserts.assertEquals(((ManyOops)dst8[i]).hash(), expected); 234 Asserts.assertEquals(((ManyOops)dst9[i]).hash(), expected); 235 Asserts.assertEquals(((ManyOops)dst10[i]).hash(), expected); 236 Asserts.assertEquals(((ManyOops)dst11[i]).hash(), expected); 237 Asserts.assertEquals(((ManyOops)dst12[i]).hash(), expected); 238 Asserts.assertEquals(dst13[i].hash(), expected); 239 } 240 } 241 }