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  * @run main/othervm -XX:+UseCompressedClassPointers -XX:-UseEmptySlotsInSupers OldLayoutCheck
33  */
34 
35 /*
36  * @test
37  * @requires vm.bits == "32"
38  * @library /test/lib
39  * @modules java.base/jdk.internal.misc
40  *          java.management
41  * @run main/othervm -XX:-UseEmptySlotsInSupers OldLayoutCheck
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 
52 public class OldLayoutCheck {
53 
54     static class LIClass {
55         public long l;
56         public int i;
57     }
58 
59     // 32-bit VMs: @0:  8 byte header,  @8: long field, @16:  int field
60     // 64-bit VMs: @0: 12 byte header, @12:  int field, @16: long field
61     static final long INT_OFFSET  = Platform.is64bit() ? 12L : 16L;
62     static final long LONG_OFFSET = Platform.is64bit() ? 16L :  8L;
63 
64     static public void main(String[] args) {
65         Unsafe unsafe = Unsafe.getUnsafe();
66         Class c = LIClass.class;
67         Field[] fields = c.getFields();
68         for (int i = 0; i < fields.length; i++) {
69             long offset = unsafe.objectFieldOffset(fields[i]);
70             if (fields[i].getType() == int.class) {
71                 Asserts.assertEquals(offset, INT_OFFSET, "Misplaced int field");
72             } else if (fields[i].getType() == long.class) {
73                 Asserts.assertEquals(offset, LONG_OFFSET, "Misplaced long field");
74             } else {
75                 Asserts.fail("Unexpected field type");
76             }
77         }
78     }
79 }