1 /* 2 * Copyright (c) 2020, 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 * @bug 8239014 27 * @summary -XX:-UseEmptySlotsInSupers sometime fails to reproduce the layout of the old code 28 * @library /test/lib / 29 * @modules java.base/jdk.internal.misc 30 * java.management 31 * @requires vm.bits == "64" & vm.opt.final.UseCompressedOops == true & vm.gc != "Z" 32 * @build jdk.test.whitebox.WhiteBox 33 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox 34 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UseCompressedClassPointers -XX:-UseEmptySlotsInSupers OldLayoutCheck 35 */ 36 37 /* 38 * @test 39 * @requires vm.bits == "32" 40 * @library /test/lib / 41 * @modules java.base/jdk.internal.misc 42 * java.management 43 * @build jdk.test.whitebox.WhiteBox 44 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox 45 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:-UseEmptySlotsInSupers OldLayoutCheck 46 */ 47 48 import java.lang.reflect.Field; 49 import java.util.Arrays; 50 import java.util.Comparator; 51 import jdk.internal.misc.Unsafe; 52 53 import jdk.test.lib.Asserts; 54 import jdk.test.lib.Platform; 55 import jdk.test.whitebox.WhiteBox; 56 57 public class OldLayoutCheck { 58 59 static class LIClass { 60 public long l; 61 public int i; 62 } 63 64 public static final WhiteBox WB = WhiteBox.getWhiteBox(); 65 66 // 32-bit VMs/compact headers: @0: 8 byte header, @8: long field, @16: int field 67 // 64-bit VMs: @0: 12 byte header, @12: int field, @16: long field 68 static final long INT_OFFSET; 69 static final long LONG_OFFSET; 70 static { 71 if (!Platform.is64bit() || WB.getBooleanVMFlag("UseCompactObjectHeaders")) { 72 INT_OFFSET = 16L; 73 LONG_OFFSET = 8L; 74 } else { 75 INT_OFFSET = 12L; 76 LONG_OFFSET = 16L; 77 } 78 } 79 80 static public void main(String[] args) { 81 Unsafe unsafe = Unsafe.getUnsafe(); 82 Class c = LIClass.class; 83 Field[] fields = c.getFields(); 84 for (int i = 0; i < fields.length; i++) { 85 long offset = unsafe.objectFieldOffset(fields[i]); 86 if (fields[i].getType() == int.class) { 87 Asserts.assertEquals(offset, INT_OFFSET, "Misplaced int field"); 88 } else if (fields[i].getType() == long.class) { 89 Asserts.assertEquals(offset, LONG_OFFSET, "Misplaced long field"); 90 } else { 91 Asserts.fail("Unexpected field type"); 92 } 93 } 94 } 95 }