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 Magic.addressOf with 32-bit compressed oops
 27  * @library /test/lib
 28  *
 29  * @run main/othervm -Xmx128m
 30  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 31  *                   -Xint
 32  *                   AddressOf
 33  *
 34  * @run main/othervm -Xmx128m
 35  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 36  *                   -XX:TieredStopAtLevel=1
 37  *                   AddressOf
 38  *
 39  * @run main/othervm -Xmx128m
 40  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 41  *                   -XX:-TieredCompilation
 42  *                   AddressOf
 43  */
 44 
 45 /*
 46  * @test
 47  * @summary Test for Magic.addressOf with zero-based compressed oops
 48  * @library /test/lib
 49  * @requires vm.bits == 64
 50  *
 51  * @run main/othervm -Xmx4g
 52  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 53  *                   -Xint
 54  *                   AddressOf
 55  *
 56  * @run main/othervm -Xmx4g
 57  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 58  *                   -XX:TieredStopAtLevel=1
 59  *                   AddressOf
 60  *
 61  * @run main/othervm -Xmx4g
 62  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 63  *                   -XX:-TieredCompilation
 64  *                   AddressOf
 65  */
 66 
 67 /*
 68  * @test
 69  * @summary Test for Magic.addressOf without compressed oops
 70  * @library /test/lib
 71  * @requires vm.bits == 64
 72  *
 73  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 74  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 75  *                   -Xint
 76  *                   AddressOf
 77  *
 78  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 79  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 80  *                   -XX:TieredStopAtLevel=1
 81  *                   AddressOf
 82  *
 83  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 84  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 85  *                   -XX:-TieredCompilation
 86  *                   AddressOf
 87  */
 88 
 89 import java.lang.reflect.Field;
 90 
 91 import net.shipilev.Magic;
 92 
 93 public class AddressOf {
 94 
 95     public static void main(String ... args) throws Exception {
 96         testAddress_newInteger();
 97         testAddress_IntegerValueOf();
 98         testAddress_newObject();
 99         testAddress_localObject();
100         testAddress_fieldObject();
101         testNulls();
102     }
103 
104     private static void testAddress_newInteger() throws Exception {
105         for (int c = 0; c < MagicUtil.ITERS; c++) {
106             MagicUtil.assertNotEquals(0, Magic.addressOf(new Integer(c)));
107         }
108     }
109 
110     private static void testAddress_IntegerValueOf() throws Exception {
111         for (int c = 0; c < MagicUtil.ITERS; c++) {
112             MagicUtil.assertNotEquals(0, Magic.addressOf(Integer.valueOf(c)));
113         }
114     }
115 
116     private static void testAddress_newObject() throws Exception {
117         for (int c = 0; c < MagicUtil.ITERS; c++) {
118             MagicUtil.assertNotEquals(0, Magic.addressOf(new Object()));
119         }
120     }
121 
122     private static void testAddress_localObject() throws Exception {
123         Object o = new Object();
124         for (int c = 0; c < MagicUtil.ITERS; c++) {
125             MagicUtil.assertNotEquals(0, Magic.addressOf(o));
126         }
127     }
128 
129     static Object staticO = new Object();
130 
131     private static void testAddress_fieldObject() throws Exception {
132         for (int c = 0; c < MagicUtil.ITERS; c++) {
133             MagicUtil.assertNotEquals(0, Magic.addressOf(staticO));
134         }
135     }
136 
137     private static void testNulls() {
138         for (int c = 0; c < MagicUtil.ITERS; c++) {
139             try {
140                 Magic.addressOf(null);
141                 MagicUtil.assertFail();
142             } catch (NullPointerException e) {
143                 // expected
144             }
145         }
146     }
147 
148 }