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