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 test for GetObjectMonitorUsage with value classes.
27  * @requires vm.jvmti
28  * @modules java.base/jdk.internal.vm.annotation
29  * @enablePreview
30  * @run main/othervm/native -agentlib:ValueGetObjectMonitorUsage -XX:+EnableValhalla ValueGetObjectMonitorUsage
31  */
32 
33 import jdk.internal.vm.annotation.LooselyConsistentValue;
34 import jdk.internal.vm.annotation.NullRestricted;
35 
36 public class ValueGetObjectMonitorUsage {
37 
38     private static final String agentLib = "ValueGetObjectMonitorUsage";
39 
40     @LooselyConsistentValue
41     private static value class ValueClass {
42         public int f1;
43         public int f2;
44 
45         public ValueClass(int v1, int v2) { f1 = v1; f2 = v2; }
46     }
47 
48     private static value class ValueHolder {
49         public ValueClass f1;
50         @NullRestricted
51         public ValueClass f2;
52 
53         public static ValueClass s1 = new ValueClass(0, 1);
54 
55         public ValueHolder(int v) {
56             f1 = new ValueClass(v, v + 100);
57             f2 = new ValueClass(v + 1, v + 200);
58         }
59     }
60 
61     public static void main(String[] args) throws Exception {
62         try {
63             System.loadLibrary(agentLib);
64         } catch (UnsatisfiedLinkError ex) {
65             System.err.println("Failed to load " + agentLib + " lib");
66             System.err.println("java.library.path: " + System.getProperty("java.library.path"));
67             throw ex;
68         }
69 
70         ValueClass testObj1 = new ValueClass(4, 5);
71         ValueHolder testObj2 = new ValueHolder(6);
72 
73         testGetObjectMonitorUsage(testObj1);
74         testGetObjectMonitorUsage(testObj2);
75     }
76 
77     private static void testGetObjectMonitorUsage(Object testObj) {
78         String className = testObj.getClass().getName();
79         log(">> Testing GetObjectMonitorUsage for " + className);
80         if (!nTestGetObjectMonitorUsage(testObj)) {
81             throw new RuntimeException("ERROR: " + className);
82         }
83         log("<< Testing " + className + " - OK");
84         log("");
85     }
86 
87     private static void log(String msg) {
88         System.out.println(msg);
89     }
90 
91     private static native boolean nTestGetObjectMonitorUsage(Object obj);
92 }