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.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 Runtime.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 Runtime.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 public class AddressOf {
  92 
  93     public static void main(String ... args) throws Exception {
  94         testAddress_newInteger();
  95         testAddress_IntegerValueOf();
  96         testAddress_newObject();
  97         testAddress_localObject();
  98         testAddress_fieldObject();
  99         testNulls();
 100     }
 101 
 102     private static void testAddress_newInteger() throws Exception {
 103         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 104             RuntimeOfUtil.assertNotEquals(0, Runtime.addressOf(new Integer(c)));
 105         }
 106     }
 107 
 108     private static void testAddress_IntegerValueOf() throws Exception {
 109         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 110             RuntimeOfUtil.assertNotEquals(0, Runtime.addressOf(Integer.valueOf(c)));
 111         }
 112     }
 113 
 114     private static void testAddress_newObject() throws Exception {
 115         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 116             RuntimeOfUtil.assertNotEquals(0, Runtime.addressOf(new Object()));
 117         }
 118     }
 119 
 120     private static void testAddress_localObject() throws Exception {
 121         Object o = new Object();
 122         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 123             RuntimeOfUtil.assertNotEquals(0, Runtime.addressOf(o));
 124         }
 125     }
 126 
 127     static Object staticO = new Object();
 128 
 129     private static void testAddress_fieldObject() throws Exception {
 130         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 131             RuntimeOfUtil.assertNotEquals(0, Runtime.addressOf(staticO));
 132         }
 133     }
 134 
 135     private static void testNulls() {
 136         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 137             try {
 138                 Runtime.addressOf(null);
 139                 RuntimeOfUtil.assertFail();
 140             } catch (NullPointerException e) {
 141                 // expected
 142             }
 143         }
 144     }
 145 
 146 }