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