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 /*
  26  * @test
  27  * @summary Test for Runtime.fieldOffsetOf with 32-bit compressed oops
  28  * @library /test/lib
  29  *
  30  * @run main/othervm -Xmx128m
  31  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  32  *                   -Xint
  33  *                   FieldOffsetOf
  34  *
  35  * @run main/othervm -Xmx128m
  36  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  37  *                   -XX:TieredStopAtLevel=1
  38  *                   FieldOffsetOf
  39  *
  40  * @run main/othervm -Xmx128m
  41  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  42  *                   -XX:TieredStopAtLevel=2
  43  *                   FieldOffsetOf
  44  *
  45  * @run main/othervm -Xmx128m
  46  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  47  *                   -XX:TieredStopAtLevel=3
  48  *                   FieldOffsetOf
  49  *
  50  * @run main/othervm -Xmx128m
  51  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  52  *                   -XX:TieredStopAtLevel=4
  53  *                   FieldOffsetOf
  54  *
  55  * @run main/othervm -Xmx128m
  56  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  57  *                   -XX:-TieredCompilation
  58  *                   FieldOffsetOf
  59  */
  60 
  61 /*
  62  * @test
  63  * @summary Test for Runtime.fieldOffsetOf with zero-based compressed oops
  64  * @library /test/lib
  65  * @requires vm.bits == 64
  66  *
  67  * @run main/othervm -Xmx4g
  68  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  69  *                   -Xint
  70  *                   FieldOffsetOf
  71  *
  72  * @run main/othervm -Xmx4g
  73  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  74  *                   -XX:TieredStopAtLevel=1
  75  *                   FieldOffsetOf
  76  *
  77  * @run main/othervm -Xmx4g
  78  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  79  *                   -XX:TieredStopAtLevel=2
  80  *                   FieldOffsetOf
  81  *
  82  * @run main/othervm -Xmx4g
  83  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  84  *                   -XX:TieredStopAtLevel=3
  85  *                   FieldOffsetOf
  86  *
  87  * @run main/othervm -Xmx4g
  88  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  89  *                   -XX:TieredStopAtLevel=4
  90  *                   FieldOffsetOf
  91  *
  92  * @run main/othervm -Xmx4g
  93  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  94  *                   -XX:-TieredCompilation
  95  *                   FieldOffsetOf
  96  */
  97 
  98 /*
  99  * @test
 100  * @summary Test for Runtime.fieldOffsetOf without compressed oops
 101  * @library /test/lib
 102  * @requires vm.bits == 64
 103  *
 104  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 105  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 106  *                   -Xint
 107  *                   FieldOffsetOf
 108  *
 109  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 110  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 111  *                   -XX:TieredStopAtLevel=1
 112  *                   FieldOffsetOf
 113  *
 114  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 115  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 116  *                   -XX:TieredStopAtLevel=2
 117  *                   FieldOffsetOf
 118  *
 119  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 120  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 121  *                   -XX:TieredStopAtLevel=3
 122  *                   FieldOffsetOf
 123  *
 124  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 125  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 126  *                   -XX:TieredStopAtLevel=4
 127  *                   FieldOffsetOf
 128  *
 129  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 130  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 131  *                   -XX:-TieredCompilation
 132  *                   FieldOffsetOf
 133  */
 134 
 135 import java.lang.reflect.Field;
 136 import jdk.test.lib.Platform;
 137 
 138 public class FieldOffsetOf {
 139 
 140     public static void main(String ... args) throws Exception {
 141         testInstanceOffsets();
 142         testStaticOffsets();
 143         testNulls();
 144     }
 145 
 146     private static void testInstanceOffsets() throws Exception {
 147         int expected = Platform.is64bit() ? 12 : 8;
 148 
 149         Field f = Holder.class.getDeclaredField("x");
 150         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 151             RuntimeOfUtil.assertEquals(expected, Runtime.fieldOffsetOf(f));
 152         }
 153     }
 154 
 155     private static void testStaticOffsets() throws Exception {
 156         Field f = Holder.class.getDeclaredField("staticX");
 157         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 158             RuntimeOfUtil.assertNotEquals(0,  Runtime.fieldOffsetOf(f));
 159             RuntimeOfUtil.assertNotEquals(-1, Runtime.fieldOffsetOf(f));
 160         }
 161     }
 162 
 163     private static void testNulls() {
 164         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 165             try {
 166                 Runtime.fieldOffsetOf(null);
 167                 RuntimeOfUtil.assertFail();
 168             } catch (NullPointerException e) {
 169                 // expected
 170             }
 171         }
 172     }
 173 
 174     static class Holder {
 175         static int staticX;
 176         int x;
 177     }
 178 
 179 }