1 /* 2 * Copyright (c) 2022, 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 import java.lang.reflect.Field; 45 import java.util.Arrays; 46 import java.util.Comparator; 47 import jdk.internal.misc.Unsafe; 48 49 import jdk.test.lib.Asserts; 50 import jdk.test.lib.Platform; 51 import jdk.test.whitebox.WhiteBox; 52 53 public class BaseOffsets { 54 55 static class LIClass { 56 public int i; 57 } 58 59 static final long INT_OFFSET; 60 static final int INT_ARRAY_OFFSET; 61 static final int LONG_ARRAY_OFFSET; 62 static { 63 WhiteBox WB = WhiteBox.getWhiteBox(); 64 if (!Platform.is64bit() || WB.getBooleanVMFlag("UseCompactObjectHeaders")) { 65 INT_OFFSET = 8; 66 INT_ARRAY_OFFSET = 12; 67 LONG_ARRAY_OFFSET = 16; 68 } else if (WB.getBooleanVMFlag("UseCompressedClassPointers")) { 69 INT_OFFSET = 12; 70 INT_ARRAY_OFFSET = 16; 71 LONG_ARRAY_OFFSET = 16; 72 } else { 73 INT_OFFSET = 16; 74 INT_ARRAY_OFFSET = 20; 75 LONG_ARRAY_OFFSET = 24; 76 } 77 } 78 79 static public void main(String[] args) { 80 Unsafe unsafe = Unsafe.getUnsafe(); 81 Class c = LIClass.class; 82 Field[] fields = c.getFields(); 83 for (int i = 0; i < fields.length; i++) { 84 long offset = unsafe.objectFieldOffset(fields[i]); 85 if (fields[i].getType() == int.class) { 86 Asserts.assertEquals(offset, INT_OFFSET, "Misplaced int field"); 87 } else { 88 Asserts.fail("Unexpected field type"); 89 } 90 } 91 92 Asserts.assertEquals(unsafe.arrayBaseOffset(boolean[].class), INT_ARRAY_OFFSET, "Misplaced boolean array base"); 93 Asserts.assertEquals(unsafe.arrayBaseOffset(byte[].class), INT_ARRAY_OFFSET, "Misplaced byte array base"); 94 Asserts.assertEquals(unsafe.arrayBaseOffset(char[].class), INT_ARRAY_OFFSET, "Misplaced char array base"); 95 Asserts.assertEquals(unsafe.arrayBaseOffset(short[].class), INT_ARRAY_OFFSET, "Misplaced short array base"); 96 Asserts.assertEquals(unsafe.arrayBaseOffset(int[].class), INT_ARRAY_OFFSET, "Misplaced int array base"); 97 Asserts.assertEquals(unsafe.arrayBaseOffset(long[].class), LONG_ARRAY_OFFSET, "Misplaced long array base"); 98 Asserts.assertEquals(unsafe.arrayBaseOffset(float[].class), INT_ARRAY_OFFSET, "Misplaced float array base"); 99 Asserts.assertEquals(unsafe.arrayBaseOffset(double[].class), LONG_ARRAY_OFFSET, "Misplaced double array base"); 100 boolean narrowOops = System.getProperty("java.vm.compressedOopsMode") != null || 101 !Platform.is64bit(); 102 int expected_objary_offset = narrowOops ? INT_ARRAY_OFFSET : LONG_ARRAY_OFFSET; 103 Asserts.assertEquals(unsafe.arrayBaseOffset(Object[].class), expected_objary_offset, "Misplaced object array base"); 104 } 105 }