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