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 Magic.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 Magic.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 Magic.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 import net.shipilev.Magic;
 94 
 95 public class FieldOffsetOf {
 96 
 97     public static void main(String ... args) throws Exception {
 98         testInstanceOffsets();
 99         testStaticOffsets();
100         testNulls();
101     }
102 
103     private static void testInstanceOffsets() throws Exception {
104         int expected = Platform.is64bit() ? 12 : 8;
105 
106         Field f = Holder.class.getDeclaredField("x");
107         for (int c = 0; c < MagicUtil.ITERS; c++) {
108             MagicUtil.assertEquals(expected, Magic.fieldOffsetOf(f));
109         }
110     }
111 
112     private static void testStaticOffsets() throws Exception {
113         Field f = Holder.class.getDeclaredField("staticX");
114         for (int c = 0; c < MagicUtil.ITERS; c++) {
115             MagicUtil.assertNotEquals(0,  Magic.fieldOffsetOf(f));
116             MagicUtil.assertNotEquals(-1, Magic.fieldOffsetOf(f));
117         }
118     }
119 
120     private static void testNulls() {
121         for (int c = 0; c < MagicUtil.ITERS; c++) {
122             try {
123                 Magic.fieldOffsetOf(null);
124                 MagicUtil.assertFail();
125             } catch (NullPointerException e) {
126                 // expected
127             }
128         }
129     }
130 
131     static class Holder {
132         static int staticX;
133         int x;
134     }
135 
136 }