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