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 Runtime.fieldSizeOf with 32-bit compressed oops
  27  * @library /test/lib
  28  *
  29  * @build sun.hotspot.WhiteBox
  30  * @run driver ClassFileInstaller sun.hotspot.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 Runtime.fieldSizeOf with zero-based compressed oops
  51  * @library /test/lib
  52  * @requires vm.bits == 64
  53  *
  54  * @build sun.hotspot.WhiteBox
  55  * @run driver ClassFileInstaller sun.hotspot.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 Runtime.fieldSizeOf without compressed oops
  76  * @library /test/lib
  77  * @requires vm.bits == 64
  78  *
  79  * @build sun.hotspot.WhiteBox
  80  * @run driver ClassFileInstaller sun.hotspot.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 sun.hotspot.WhiteBox;
 100 
 101 public class FieldSizeOf {
 102 
 103     static final Boolean compressedOops = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedOops");
 104     static final int R = ((compressedOops == null) || (compressedOops == true)) ?  4 : 8;
 105 
 106     public static void main(String ... args) throws Exception {
 107         testInstanceOffsets();
 108         testStaticOffsets();
 109         testNulls();
 110     }
 111 
 112     private static void testInstanceOffsets() throws Exception {
 113         testWith(1, Holder.class.getDeclaredField("f_boolean"));
 114         testWith(1, Holder.class.getDeclaredField("f_byte"));
 115         testWith(2, Holder.class.getDeclaredField("f_char"));
 116         testWith(2, Holder.class.getDeclaredField("f_short"));
 117         testWith(4, Holder.class.getDeclaredField("f_int"));
 118         testWith(4, Holder.class.getDeclaredField("f_float"));
 119         testWith(8, Holder.class.getDeclaredField("f_long"));
 120         testWith(8, Holder.class.getDeclaredField("f_double"));
 121         testWith(R, Holder.class.getDeclaredField("f_object"));
 122         testWith(R, Holder.class.getDeclaredField("f_array"));
 123     }
 124 
 125     private static void testStaticOffsets() throws Exception {
 126         testWith(1, Holder.class.getDeclaredField("s_boolean"));
 127         testWith(1, Holder.class.getDeclaredField("s_byte"));
 128         testWith(2, Holder.class.getDeclaredField("s_char"));
 129         testWith(2, Holder.class.getDeclaredField("s_short"));
 130         testWith(4, Holder.class.getDeclaredField("s_int"));
 131         testWith(4, Holder.class.getDeclaredField("s_float"));
 132         testWith(8, Holder.class.getDeclaredField("s_long"));
 133         testWith(8, Holder.class.getDeclaredField("s_double"));
 134         testWith(R, Holder.class.getDeclaredField("s_object"));
 135         testWith(R, Holder.class.getDeclaredField("s_array"));
 136     }
 137 
 138     private static void testWith(int expected, Field f) {
 139         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 140             RuntimeOfUtil.assertEquals(expected, Runtime.fieldSizeOf(f));
 141         }
 142     }
 143 
 144     private static void testNulls() {
 145         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 146             try {
 147                 Runtime.fieldSizeOf(null);
 148                 RuntimeOfUtil.assertFail();
 149             } catch (NullPointerException e) {
 150                 // expected
 151             }
 152         }
 153     }
 154 
 155     public static class Holder {
 156         static boolean  s_boolean;
 157         static byte     s_byte;
 158         static char     s_char;
 159         static short    s_short;
 160         static int      s_int;
 161         static float    s_float;
 162         static double   s_double;
 163         static long     s_long;
 164         static Object   s_object;
 165         static Object[] s_array;
 166         boolean  f_boolean;
 167         byte     f_byte;
 168         char     f_char;
 169         short    f_short;
 170         int      f_int;
 171         float    f_float;
 172         double   f_double;
 173         long     f_long;
 174         Object   f_object;
 175         Object[] f_array;
 176     }
 177 
 178 }