1 /*
 2  * Copyright (c) 2023, 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  * @requires vm.gc.Z
27  * @bug 8313667
28  * @summary Test that GenZ uses correct array copy stub for flat value class arrays when expanding ArrayCopyNode.
29  * @library /test/lib
30  * @enablePreview
31  * @modules java.base/jdk.internal.value
32  *          java.base/jdk.internal.vm.annotation
33  * @run main/othervm -Xbatch -XX:+UseZGC
34  *                   -XX:CompileCommand=exclude,compiler.valhalla.inlinetypes.TestWrongFlatArrayCopyStubWithZGC::check
35  *                   -XX:CompileCommand=dontinline,compiler.valhalla.inlinetypes.TestWrongFlatArrayCopyStubWithZGC::test*
36  *                   compiler.valhalla.inlinetypes.TestWrongFlatArrayCopyStubWithZGC
37  */
38 
39 package compiler.valhalla.inlinetypes;
40 
41 import jdk.test.lib.Asserts;
42 import jdk.test.lib.Utils;
43 
44 import jdk.internal.value.ValueClass;
45 import jdk.internal.vm.annotation.LooselyConsistentValue;
46 
47 public class TestWrongFlatArrayCopyStubWithZGC {
48 
49     public static void main(String[] args) {
50         ValueWithLong[] arrWithLong = (ValueWithLong[])ValueClass.newNullRestrictedNonAtomicArray(ValueWithLong.class, 3, new ValueWithLong(0));
51         arrWithLong[0] = new ValueWithLong(0x408BE000000fffffL);
52         arrWithLong[1] = new ValueWithLong(0x408BE0000000000L);
53         long randomValue = Utils.getRandomInstance().nextLong();
54         arrWithLong[2] = new ValueWithLong(randomValue);
55 
56         for (int i = 0; i < 10000; i++) {
57             ValueWithLong[] result = testLong(arrWithLong);
58             check(result[0].l, 0x408BE000000fffffL);
59             check(result[1].l, 0x408BE0000000000L);
60             check(result[2].l, randomValue);
61         }
62 
63         ValueWithOop[] arrWithOop = (ValueWithOop[])ValueClass.newNullRestrictedNonAtomicArray(ValueWithOop.class, 2, new ValueWithOop());
64         for (int i = 0; i < 10000; i++) {
65             testOop(arrWithOop);
66         }
67     }
68 
69     static void check(long result, long expected) {
70         Asserts.assertEQ(result, expected);
71     }
72 
73     static ValueWithLong[] testLong(ValueWithLong[] arr) {
74         return arr.clone();
75     }
76 
77     static ValueWithOop[] testOop(ValueWithOop[] arr) {
78         return arr.clone();
79     }
80 }
81 
82 @LooselyConsistentValue
83 value class ValueWithLong {
84     long l;
85 
86     public ValueWithLong(long l) {
87         this.l = l;
88     }
89 }
90 
91 @LooselyConsistentValue
92 value class ValueWithOop {
93     Object v;
94 
95     public ValueWithOop() {
96         this.v = new Object();
97     }
98 }
99