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