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:TieredStopAtLevel=2
  45  *                   FieldSizeOf
  46  *
  47  * @run main/othervm -Xmx128m
  48  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  49  *                   -XX:TieredStopAtLevel=3
  50  *                   FieldSizeOf
  51  *
  52  * @run main/othervm -Xmx128m
  53  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  54  *                   -XX:TieredStopAtLevel=4
  55  *                   FieldSizeOf
  56  *
  57  * @run main/othervm -Xmx128m
  58  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  59  *                   -XX:-TieredCompilation
  60  *                   FieldSizeOf
  61  */
  62 
  63 /*
  64  * @test
  65  * @summary Test for Runtime.fieldSizeOf with zero-based compressed oops
  66  * @library /test/lib
  67  * @requires vm.bits == 64
  68  *
  69  * @build sun.hotspot.WhiteBox
  70  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  71  *
  72  * @run main/othervm -Xmx4g
  73  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  74  *                   -Xint
  75  *                   FieldSizeOf
  76  *
  77  * @run main/othervm -Xmx4g
  78  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  79  *                   -XX:TieredStopAtLevel=1
  80  *                   FieldSizeOf
  81  *
  82  * @run main/othervm -Xmx4g
  83  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  84  *                   -XX:TieredStopAtLevel=2
  85  *                   FieldSizeOf
  86  *
  87  * @run main/othervm -Xmx4g
  88  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  89  *                   -XX:TieredStopAtLevel=3
  90  *                   FieldSizeOf
  91  *
  92  * @run main/othervm -Xmx4g
  93  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  94  *                   -XX:TieredStopAtLevel=4
  95  *                   FieldSizeOf
  96  *
  97  * @run main/othervm -Xmx4g
  98  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  99  *                   -XX:-TieredCompilation
 100  *                   FieldSizeOf
 101  */
 102 
 103 /*
 104  * @test
 105  * @summary Test for Runtime.fieldSizeOf without compressed oops
 106  * @library /test/lib
 107  * @requires vm.bits == 64
 108  *
 109  * @build sun.hotspot.WhiteBox
 110  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 111  *
 112  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 113  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 114  *                   -Xint
 115  *                   FieldSizeOf
 116  *
 117  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 118  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 119  *                   -XX:TieredStopAtLevel=1
 120  *                   FieldSizeOf
 121  *
 122  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 123  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 124  *                   -XX:TieredStopAtLevel=2
 125  *                   FieldSizeOf
 126  *
 127  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 128  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 129  *                   -XX:TieredStopAtLevel=3
 130  *                   FieldSizeOf
 131  *
 132  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 133  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 134  *                   -XX:TieredStopAtLevel=4
 135  *                   FieldSizeOf
 136  *
 137  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 138  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:. 
 139  *                   -XX:-TieredCompilation
 140  *                   FieldSizeOf
 141  */
 142 
 143 import java.lang.reflect.Field;
 144 import sun.hotspot.WhiteBox;
 145 
 146 public class FieldSizeOf {
 147 
 148     static final Boolean compressedOops = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedOops");
 149     static final int R = ((compressedOops == null) || (compressedOops == true)) ?  4 : 8;
 150 
 151     public static void main(String ... args) throws Exception {
 152         testInstanceOffsets();
 153         testStaticOffsets();
 154         testNulls();
 155     }
 156 
 157     private static void testInstanceOffsets() throws Exception {
 158         testWith(1, Holder.class.getDeclaredField("f_boolean"));
 159         testWith(1, Holder.class.getDeclaredField("f_byte"));
 160         testWith(2, Holder.class.getDeclaredField("f_char"));
 161         testWith(2, Holder.class.getDeclaredField("f_short"));
 162         testWith(4, Holder.class.getDeclaredField("f_int"));
 163         testWith(4, Holder.class.getDeclaredField("f_float"));
 164         testWith(8, Holder.class.getDeclaredField("f_long"));
 165         testWith(8, Holder.class.getDeclaredField("f_double"));
 166         testWith(R, Holder.class.getDeclaredField("f_object"));
 167         testWith(R, Holder.class.getDeclaredField("f_array"));
 168     }
 169 
 170     private static void testStaticOffsets() throws Exception {
 171         testWith(1, Holder.class.getDeclaredField("s_boolean"));
 172         testWith(1, Holder.class.getDeclaredField("s_byte"));
 173         testWith(2, Holder.class.getDeclaredField("s_char"));
 174         testWith(2, Holder.class.getDeclaredField("s_short"));
 175         testWith(4, Holder.class.getDeclaredField("s_int"));
 176         testWith(4, Holder.class.getDeclaredField("s_float"));
 177         testWith(8, Holder.class.getDeclaredField("s_long"));
 178         testWith(8, Holder.class.getDeclaredField("s_double"));
 179         testWith(R, Holder.class.getDeclaredField("s_object"));
 180         testWith(R, Holder.class.getDeclaredField("s_array"));
 181     }
 182 
 183     private static void testWith(int expected, Field f) {
 184         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 185             RuntimeOfUtil.assertEquals(expected, Runtime.fieldSizeOf(f));
 186         }
 187     }
 188 
 189     private static void testNulls() {
 190         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 191             try {
 192                 Runtime.fieldSizeOf(null);
 193                 RuntimeOfUtil.assertFail();
 194             } catch (NullPointerException e) {
 195                 // expected
 196             }
 197         }
 198     }
 199 
 200     public static class Holder {
 201         static boolean  s_boolean;
 202         static byte     s_byte;
 203         static char     s_char;
 204         static short    s_short;
 205         static int      s_int;
 206         static float    s_float;
 207         static double   s_double;
 208         static long     s_long;
 209         static Object   s_object;
 210         static Object[] s_array;
 211         boolean  f_boolean;
 212         byte     f_byte;
 213         char     f_char;
 214         short    f_short;
 215         int      f_int;
 216         float    f_float;
 217         double   f_double;
 218         long     f_long;
 219         Object   f_object;
 220         Object[] f_array;
 221     }
 222 
 223 }