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  * @run main/othervm BaseOffsets
30  */
31 /*
32  * @test id=no-coops
33  * @library /test/lib
34  * @requires vm.bits == "64"
35  * @modules java.base/jdk.internal.misc
36  *          java.management
37  * @run main/othervm -XX:-UseCompressedOops BaseOffsets
38  */
39 
40 import java.lang.reflect.Field;
41 import java.util.Arrays;
42 import java.util.Comparator;
43 import jdk.internal.misc.Unsafe;
44 
45 import jdk.test.lib.Asserts;
46 import jdk.test.lib.Platform;
47 
48 public class BaseOffsets {
49 
50     static class LIClass {
51         public int i;
52     }
53 
54     // @0:  8 byte header,  @8: int field
55     static final long INT_OFFSET  = 8L;
56 
57     static public void main(String[] args) {
58         Unsafe unsafe = Unsafe.getUnsafe();
59         Class c = LIClass.class;
60         Field[] fields = c.getFields();
61         for (int i = 0; i < fields.length; i++) {
62             long offset = unsafe.objectFieldOffset(fields[i]);
63             if (fields[i].getType() == int.class) {
64                 Asserts.assertEquals(offset, INT_OFFSET, "Misplaced int field");
65             } else {
66                 Asserts.fail("Unexpected field type");
67             }
68         }
69 
70         Asserts.assertEquals(unsafe.arrayBaseOffset(boolean[].class), 12, "Misplaced boolean array base");
71         Asserts.assertEquals(unsafe.arrayBaseOffset(byte[].class),    12, "Misplaced byte    array base");
72         Asserts.assertEquals(unsafe.arrayBaseOffset(char[].class),    12, "Misplaced char    array base");
73         Asserts.assertEquals(unsafe.arrayBaseOffset(short[].class),   12, "Misplaced short   array base");
74         Asserts.assertEquals(unsafe.arrayBaseOffset(int[].class),     12, "Misplaced int     array base");
75         Asserts.assertEquals(unsafe.arrayBaseOffset(long[].class),    16, "Misplaced long    array base");
76         Asserts.assertEquals(unsafe.arrayBaseOffset(float[].class),   12, "Misplaced float   array base");
77         Asserts.assertEquals(unsafe.arrayBaseOffset(double[].class),  16, "Misplaced double  array base");
78         boolean narrowOops = System.getProperty("java.vm.compressedOopsMode") != null ||
79                              !Platform.is64bit();
80         int expected_objary_offset = narrowOops ? 12 : 16;
81         Asserts.assertEquals(unsafe.arrayBaseOffset(Object[].class),  expected_objary_offset, "Misplaced object  array base");
82     }
83 }