1 /*
 2  * Copyright (c) 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  * @summary Sanity tests for GetLocalObject/SetLocalObject/GetLocalInstance with value classes.
27  * @requires vm.jvmti
28  * @modules java.base/jdk.internal.vm.annotation
29  * @enablePreview
30  * @run main/othervm/native -agentlib:ValueGetSetLocal -XX:+EnableValhalla ValueGetSetLocal
31  */
32 
33 import java.util.Objects;
34 import jdk.internal.vm.annotation.ImplicitlyConstructible;
35 import jdk.internal.vm.annotation.LooselyConsistentValue;
36 import jdk.internal.vm.annotation.NullRestricted;
37 
38 public class ValueGetSetLocal {
39 
40     private static final String agentLib = "ValueGetSetLocal";
41 
42     @ImplicitlyConstructible
43     @LooselyConsistentValue
44     private static value class ValueClass {
45         public int f1;
46         public int f2;
47 
48         public ValueClass(int v1, int v2) { f1 = v1; f2 = v2; }
49     }
50 
51     private static value class ValueHolder {
52         public ValueClass f1;
53         @NullRestricted
54         public ValueClass f2;
55 
56         public static ValueClass s1 = new ValueClass(0, 1);
57 
58         public ValueHolder(int v) {
59             f1 = new ValueClass(v, v + 100);
60             f2 = new ValueClass(v + 1, v + 200);
61         }
62 
63         // slot 0 is "this"
64         public void meth(ValueClass obj1,       // slot 1
65                          ValueHolder obj2) {    // slot 2
66             Object obj3 = obj2;                 // slot 3
67             if (!nTestLocals(Thread.currentThread())) {
68                 throw new RuntimeException("ERROR: nTestLocals failed");
69             }
70             // nTestLocals sets obj3 = obj1
71             if (!Objects.equals(obj3, obj1)) {
72                 throw new RuntimeException("ERROR: obj3 != obj1" + " (obj3 = " + obj3 + ")");
73             }
74         }
75     }
76 
77     public static void main(String[] args) throws Exception {
78         try {
79             System.loadLibrary(agentLib);
80         } catch (UnsatisfiedLinkError ex) {
81             System.err.println("Failed to load " + agentLib + " lib");
82             System.err.println("java.library.path: " + System.getProperty("java.library.path"));
83             throw ex;
84         }
85 
86         ValueClass testObj1 = new ValueClass(7, 8);
87         ValueHolder testObj2 = new ValueHolder(9);
88         testObj2.meth(testObj1, testObj2);
89     }
90 
91     private static native boolean nTestLocals(Thread thread);
92 }