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:-TieredCompilation
  43  *                   FieldOffsetOf
  44  */
  45 
  46 /*
  47  * @test
  48  * @summary Test for Runtime.fieldOffsetOf with zero-based compressed oops
  49  * @library /test/lib
  50  * @requires vm.bits == 64
  51  *
  52  * @run main/othervm -Xmx4g
  53  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  54  *                   -Xint
  55  *                   FieldOffsetOf
  56  *
  57  * @run main/othervm -Xmx4g
  58  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  59  *                   -XX:TieredStopAtLevel=1
  60  *                   FieldOffsetOf
  61  *
  62  * @run main/othervm -Xmx4g
  63  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  64  *                   -XX:-TieredCompilation
  65  *                   FieldOffsetOf
  66  */
  67 
  68 /*
  69  * @test
  70  * @summary Test for Runtime.fieldOffsetOf without compressed oops
  71  * @library /test/lib
  72  * @requires vm.bits == 64
  73  *
  74  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
  75  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  76  *                   -Xint
  77  *                   FieldOffsetOf
  78  *
  79  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
  80  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  81  *                   -XX:TieredStopAtLevel=1
  82  *                   FieldOffsetOf
  83  *
  84  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
  85  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  86  *                   -XX:-TieredCompilation
  87  *                   FieldOffsetOf
  88  */
  89 
  90 import java.lang.reflect.Field;
  91 import jdk.test.lib.Platform;
  92 
  93 public class FieldOffsetOf {
  94 
  95     public static void main(String ... args) throws Exception {
  96         testInstanceOffsets();
  97         testStaticOffsets();
  98         testNulls();
  99     }
 100 
 101     private static void testInstanceOffsets() throws Exception {
 102         int expected = Platform.is64bit() ? 12 : 8;
 103 
 104         Field f = Holder.class.getDeclaredField("x");
 105         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 106             RuntimeOfUtil.assertEquals(expected, Runtime.fieldOffsetOf(f));
 107         }
 108     }
 109 
 110     private static void testStaticOffsets() throws Exception {
 111         Field f = Holder.class.getDeclaredField("staticX");
 112         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 113             RuntimeOfUtil.assertNotEquals(0,  Runtime.fieldOffsetOf(f));
 114             RuntimeOfUtil.assertNotEquals(-1, Runtime.fieldOffsetOf(f));
 115         }
 116     }
 117 
 118     private static void testNulls() {
 119         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 120             try {
 121                 Runtime.fieldOffsetOf(null);
 122                 RuntimeOfUtil.assertFail();
 123             } catch (NullPointerException e) {
 124                 // expected
 125             }
 126         }
 127     }
 128 
 129     static class Holder {
 130         static int staticX;
 131         int x;
 132     }
 133 
 134 }