1 /*
2 * Copyright (c) 2024, 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 * @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.LooselyConsistentValue;
35 import jdk.internal.vm.annotation.NullRestricted;
36
37 public class ValueGetSetLocal {
38
39 private static final String agentLib = "ValueGetSetLocal";
40
41 @LooselyConsistentValue
42 private static value class ValueClass {
43 public int f1;
44 public int f2;
45
46 public ValueClass(int v1, int v2) { f1 = v1; f2 = v2; }
47 }
48
49 private static value class ValueHolder {
50 public ValueClass f1;
51 @NullRestricted
52 public ValueClass f2;
53
54 public static ValueClass s1 = new ValueClass(0, 1);
55
56 public ValueHolder(int v) {
57 f1 = new ValueClass(v, v + 100);
58 f2 = new ValueClass(v + 1, v + 200);
59 }
60
61 // slot 0 is "this"
62 public void meth(ValueClass obj1, // slot 1
63 ValueHolder obj2) { // slot 2
64 Object obj3 = obj2; // slot 3
65 // SetLocalObject can only set locals for top frame of virtual threads.
66 boolean testSetLocal = !Thread.currentThread().isVirtual();
67 if (!nTestLocals(Thread.currentThread(), testSetLocal)) {
68 throw new RuntimeException("ERROR: nTestLocals failed");
69 }
70 // nTestLocals sets obj3 = obj1
71 if (testSetLocal && !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, boolean testSetLocal);
92 }