1 /*
  2  * Copyright (c) 2020, Red Hat, Inc. 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  * @summary Test for Magic.fieldSizeOf with 32-bit compressed oops
 27  * @library /test/lib
 28  *
 29  * @build jdk.test.whitebox.WhiteBox
 30  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 31  *
 32  * @run main/othervm -Xmx128m
 33  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 34  *                   -Xint
 35  *                   FieldSizeOf
 36  *
 37  * @run main/othervm -Xmx128m
 38  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 39  *                   -XX:TieredStopAtLevel=1
 40  *                   FieldSizeOf
 41  *
 42  * @run main/othervm -Xmx128m
 43  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 44  *                   -XX:-TieredCompilation
 45  *                   FieldSizeOf
 46  */
 47 
 48 /*
 49  * @test
 50  * @summary Test for Magic.fieldSizeOf with zero-based compressed oops
 51  * @library /test/lib
 52  * @requires vm.bits == 64
 53  *
 54  * @build jdk.test.whitebox.WhiteBox
 55  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 56  *
 57  * @run main/othervm -Xmx4g
 58  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 59  *                   -Xint
 60  *                   FieldSizeOf
 61  *
 62  * @run main/othervm -Xmx4g
 63  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 64  *                   -XX:TieredStopAtLevel=1
 65  *                   FieldSizeOf
 66  *
 67  * @run main/othervm -Xmx4g
 68  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 69  *                   -XX:-TieredCompilation
 70  *                   FieldSizeOf
 71  */
 72 
 73 /*
 74  * @test
 75  * @summary Test for Magic.fieldSizeOf without compressed oops
 76  * @library /test/lib
 77  * @requires vm.bits == 64
 78  *
 79  * @build jdk.test.whitebox.WhiteBox
 80  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 81  *
 82  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 83  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 84  *                   -Xint
 85  *                   FieldSizeOf
 86  *
 87  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 88  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 89  *                   -XX:TieredStopAtLevel=1
 90  *                   FieldSizeOf
 91  *
 92  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 93  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:. 
 94  *                   -XX:-TieredCompilation
 95  *                   FieldSizeOf
 96  */
 97 
 98 import java.lang.reflect.Field;
 99 import jdk.test.whitebox.WhiteBox;
100 
101 import net.shipilev.Magic;
102 
103 public class FieldSizeOf {
104 
105     static final Boolean compressedOops = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedOops");
106     static final int R = ((compressedOops == null) || (compressedOops == true)) ?  4 : 8;
107 
108     public static void main(String ... args) throws Exception {
109         testInstanceOffsets();
110         testStaticOffsets();
111         testNulls();
112     }
113 
114     private static void testInstanceOffsets() throws Exception {
115         testWith(1, Holder.class.getDeclaredField("f_boolean"));
116         testWith(1, Holder.class.getDeclaredField("f_byte"));
117         testWith(2, Holder.class.getDeclaredField("f_char"));
118         testWith(2, Holder.class.getDeclaredField("f_short"));
119         testWith(4, Holder.class.getDeclaredField("f_int"));
120         testWith(4, Holder.class.getDeclaredField("f_float"));
121         testWith(8, Holder.class.getDeclaredField("f_long"));
122         testWith(8, Holder.class.getDeclaredField("f_double"));
123         testWith(R, Holder.class.getDeclaredField("f_object"));
124         testWith(R, Holder.class.getDeclaredField("f_array"));
125     }
126 
127     private static void testStaticOffsets() throws Exception {
128         testWith(1, Holder.class.getDeclaredField("s_boolean"));
129         testWith(1, Holder.class.getDeclaredField("s_byte"));
130         testWith(2, Holder.class.getDeclaredField("s_char"));
131         testWith(2, Holder.class.getDeclaredField("s_short"));
132         testWith(4, Holder.class.getDeclaredField("s_int"));
133         testWith(4, Holder.class.getDeclaredField("s_float"));
134         testWith(8, Holder.class.getDeclaredField("s_long"));
135         testWith(8, Holder.class.getDeclaredField("s_double"));
136         testWith(R, Holder.class.getDeclaredField("s_object"));
137         testWith(R, Holder.class.getDeclaredField("s_array"));
138     }
139 
140     private static void testWith(int expected, Field f) {
141         for (int c = 0; c < MagicUtil.ITERS; c++) {
142             MagicUtil.assertEquals(expected, Magic.fieldSizeOf(f));
143         }
144     }
145 
146     private static void testNulls() {
147         for (int c = 0; c < MagicUtil.ITERS; c++) {
148             try {
149                 Magic.fieldSizeOf(null);
150                 MagicUtil.assertFail();
151             } catch (NullPointerException e) {
152                 // expected
153             }
154         }
155     }
156 
157     public static class Holder {
158         static boolean  s_boolean;
159         static byte     s_byte;
160         static char     s_char;
161         static short    s_short;
162         static int      s_int;
163         static float    s_float;
164         static double   s_double;
165         static long     s_long;
166         static Object   s_object;
167         static Object[] s_array;
168         boolean  f_boolean;
169         byte     f_byte;
170         char     f_char;
171         short    f_short;
172         int      f_int;
173         float    f_float;
174         double   f_double;
175         long     f_long;
176         Object   f_object;
177         Object[] f_array;
178     }
179 
180 }