1 /*
  2  * Copyright (c) 2022, 2023, 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 id=with-coops-with-ccp
 26  * @library /test/lib /
 27  * @requires vm.bits == "64"
 28  * @modules java.base/jdk.internal.misc
 29  *          java.management
 30  * @build jdk.test.whitebox.WhiteBox
 31  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:-UseCompactObjectHeaders BaseOffsets
 33  */
 34 /*
 35  * @test id=no-coops-with-ccp
 36  * @library /test/lib /
 37  * @requires vm.bits == "64"
 38  * @modules java.base/jdk.internal.misc
 39  *          java.management
 40  * @build jdk.test.whitebox.WhiteBox
 41  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 42  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:-UseCompressedOops -XX:+UseCompressedClassPointers -XX:-UseCompactObjectHeaders BaseOffsets
 43  */
 44 /*
 45  * @test id=with-coops-no-ccp
 46  * @library /test/lib /
 47  * @requires vm.bits == "64"
 48  * @modules java.base/jdk.internal.misc
 49  *          java.management
 50  * @build jdk.test.whitebox.WhiteBox
 51  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 52  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+UseCompressedOops -XX:-UseCompressedClassPointers -XX:-UseCompactObjectHeaders BaseOffsets
 53  */
 54 /*
 55  * @test id=no-coops-no-ccp
 56  * @library /test/lib /
 57  * @requires vm.bits == "64"
 58  * @modules java.base/jdk.internal.misc
 59  *          java.management
 60  * @build jdk.test.whitebox.WhiteBox
 61  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 62  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:-UseCompactObjectHeaders BaseOffsets
 63  */
 64 /*
 65  * @test id=with-coop--with-coh
 66  * @library /test/lib /
 67  * @requires vm.bits == "64"
 68  * @modules java.base/jdk.internal.misc
 69  *          java.management
 70  * @build jdk.test.whitebox.WhiteBox
 71  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 72  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+UseCompressedOops -XX:+UseCompactObjectHeaders BaseOffsets
 73  */
 74 /*
 75  * @test id=no-coops-with-coh
 76  * @library /test/lib /
 77  * @requires vm.bits == "64"
 78  * @modules java.base/jdk.internal.misc
 79  *          java.management
 80  * @build jdk.test.whitebox.WhiteBox
 81  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 82  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:-UseCompressedOops -XX:+UseCompactObjectHeaders BaseOffsets
 83  */
 84 /*
 85  * @test id=32bit
 86  * @library /test/lib /
 87  * @requires vm.bits == "32"
 88  * @modules java.base/jdk.internal.misc
 89  *          java.management
 90  * @build jdk.test.whitebox.WhiteBox
 91  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 92  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI BaseOffsets
 93  */
 94 
 95 import java.lang.reflect.Field;
 96 import java.util.Arrays;
 97 import java.util.Comparator;
 98 import jdk.internal.misc.Unsafe;
 99 
100 import jdk.test.lib.Asserts;
101 import jdk.test.lib.Platform;
102 import jdk.test.whitebox.WhiteBox;
103 
104 public class BaseOffsets {
105 
106     static class LIClass {
107         public int i;
108     }
109 
110     public static final WhiteBox WB = WhiteBox.getWhiteBox();
111 
112     static final long INT_OFFSET;
113     static final int  INT_ARRAY_OFFSET;
114     static final int  LONG_ARRAY_OFFSET;
115     static {
116         if (!Platform.is64bit() || WB.getBooleanVMFlag("UseCompactObjectHeaders")) {
117             INT_OFFSET = 8;
118             INT_ARRAY_OFFSET = 12;
119             LONG_ARRAY_OFFSET = 16;
120         } else if (WB.getBooleanVMFlag("UseCompressedClassPointers")) {
121             INT_OFFSET = 12;
122             INT_ARRAY_OFFSET = 16;
123             LONG_ARRAY_OFFSET = 16;
124         } else {
125             INT_OFFSET = 16;
126             INT_ARRAY_OFFSET = 20;
127             LONG_ARRAY_OFFSET = 24;
128         }
129     }
130 
131     static public void main(String[] args) {
132         Unsafe unsafe = Unsafe.getUnsafe();
133         Class c = LIClass.class;
134         Field[] fields = c.getFields();
135         for (int i = 0; i < fields.length; i++) {
136             long offset = unsafe.objectFieldOffset(fields[i]);
137             if (fields[i].getType() == int.class) {
138                 Asserts.assertEquals(offset, INT_OFFSET, "Misplaced int field");
139             } else {
140                 Asserts.fail("Unexpected field type");
141             }
142         }
143 
144         Asserts.assertEquals(unsafe.arrayBaseOffset(boolean[].class), INT_ARRAY_OFFSET,  "Misplaced boolean array base");
145         Asserts.assertEquals(unsafe.arrayBaseOffset(byte[].class),    INT_ARRAY_OFFSET,  "Misplaced byte    array base");
146         Asserts.assertEquals(unsafe.arrayBaseOffset(char[].class),    INT_ARRAY_OFFSET,  "Misplaced char    array base");
147         Asserts.assertEquals(unsafe.arrayBaseOffset(short[].class),   INT_ARRAY_OFFSET,  "Misplaced short   array base");
148         Asserts.assertEquals(unsafe.arrayBaseOffset(int[].class),     INT_ARRAY_OFFSET,  "Misplaced int     array base");
149         Asserts.assertEquals(unsafe.arrayBaseOffset(long[].class),    LONG_ARRAY_OFFSET, "Misplaced long    array base");
150         Asserts.assertEquals(unsafe.arrayBaseOffset(float[].class),   INT_ARRAY_OFFSET,  "Misplaced float   array base");
151         Asserts.assertEquals(unsafe.arrayBaseOffset(double[].class),  LONG_ARRAY_OFFSET, "Misplaced double  array base");
152         boolean narrowOops = System.getProperty("java.vm.compressedOopsMode") != null ||
153                              !Platform.is64bit();
154         int expected_objary_offset = narrowOops ? INT_ARRAY_OFFSET : LONG_ARRAY_OFFSET;
155         Asserts.assertEquals(unsafe.arrayBaseOffset(Object[].class),  expected_objary_offset, "Misplaced object  array base");
156     }
157 }