1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. 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  * @bug 8136421
  27  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  28  * @library / /testlibrary /test/lib
  29  * @library ../common/patches
  30  * @modules java.base/jdk.internal.misc
  31  * @modules jdk.vm.ci/jdk.vm.ci.hotspot
  32  *          jdk.vm.ci/jdk.vm.ci.meta
  33  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  34  *        jdk.vm.ci/jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject
  35  * @build compiler.jvmci.compilerToVM.GetResolvedJavaTypeTest
  36  * @build sun.hotspot.WhiteBox
  37  * @run main ClassFileInstaller
  38  *      sun.hotspot.WhiteBox
  39  *      sun.hotspot.WhiteBox$WhiteBoxPermission
  40  * @run main/othervm -Xbootclasspath/a:.
  41  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  42  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  43  *                   -XX:+UseCompressedOops
  44  *                   compiler.jvmci.compilerToVM.GetResolvedJavaTypeTest
  45  * @run main/othervm -Xbootclasspath/a:.
  46  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  47  *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  48  *                   -XX:-UseCompressedOops
  49  *                   compiler.jvmci.compilerToVM.GetResolvedJavaTypeTest
  50  */
  51 
  52 package compiler.jvmci.compilerToVM;
  53 
  54 import java.lang.reflect.Field;
  55 import jdk.vm.ci.meta.ConstantPool;
  56 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  57 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  58 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  59 import jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject;
  60 import jdk.test.lib.Asserts;
  61 import jdk.test.lib.Utils;
  62 import sun.hotspot.WhiteBox;
  63 import jdk.internal.misc.Unsafe;
  64 
  65 public class GetResolvedJavaTypeTest {
  66     private static enum TestCase {
  67         NULL_BASE {
  68             @Override
  69             HotSpotResolvedObjectType getResolvedJavaType() {
  70                 return CompilerToVMHelper.getResolvedJavaType(
  71                         null, getPtrToKlass(), COMPRESSED);
  72             }
  73         },
  74         JAVA_METHOD_BASE {
  75             @Override
  76             HotSpotResolvedObjectType getResolvedJavaType() {
  77                 HotSpotResolvedJavaMethod methodInstance
  78                         = CompilerToVMHelper.getResolvedJavaMethodAtSlot(
  79                         TEST_CLASS, 0);
  80                 Field field;
  81                 try {
  82                     // jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl.metaspaceMethod
  83                     field = methodInstance.getClass()
  84                             .getDeclaredField("metaspaceMethod");
  85                     field.setAccessible(true);
  86                     field.set(methodInstance, getPtrToKlass());
  87                 } catch (ReflectiveOperationException e) {
  88                     throw new Error("TEST BUG : " + e, e);
  89                 }
  90 
  91                 return CompilerToVMHelper.getResolvedJavaType(methodInstance,
  92                         0L, COMPRESSED);
  93             }
  94         },
  95         CONSTANT_POOL_BASE {
  96             @Override
  97             HotSpotResolvedObjectType getResolvedJavaType() {
  98                 ConstantPool cpInst;
  99                 try {
 100                     cpInst = CompilerToVMHelper.getConstantPool(null,
 101                             getPtrToKlass());
 102                     // jdk.vm.ci.hotspot.HotSpotConstantPool.metaspaceConstantPool
 103                     Field field = cpInst.getClass()
 104                             .getDeclaredField("metaspaceConstantPool");
 105                     field.setAccessible(true);
 106                     field.set(cpInst, getPtrToKlass());
 107                 } catch (ReflectiveOperationException e) {
 108                     throw new Error("TESTBUG : " + e, e);
 109                 }
 110                 return CompilerToVMHelper.getResolvedJavaType(cpInst,
 111                         0L, COMPRESSED);
 112             }
 113         },
 114         CONSTANT_POOL_BASE_IN_TWO {
 115             @Override
 116             HotSpotResolvedObjectType getResolvedJavaType() {
 117                 long ptr = getPtrToKlass();
 118                 ConstantPool cpInst = HotSpotResolvedObjectType
 119                         .fromObjectClass(TEST_CLASS)
 120                         .getConstantPool();
 121                 try {
 122                     Field field = cpInst.getClass()
 123                             .getDeclaredField("metaspaceConstantPool");
 124                     field.setAccessible(true);
 125                     field.set(cpInst, ptr / 2L);
 126                 } catch (ReflectiveOperationException e) {
 127                     throw new Error("TESTBUG : " + e, e);
 128                 }
 129                 return CompilerToVMHelper.getResolvedJavaType(cpInst,
 130                         ptr - ptr / 2L, COMPRESSED);
 131             }
 132         },
 133         CONSTANT_POOL_BASE_ZERO {
 134             @Override
 135             HotSpotResolvedObjectType getResolvedJavaType() {
 136                 long ptr = getPtrToKlass();
 137                 ConstantPool cpInst = HotSpotResolvedObjectType
 138                         .fromObjectClass(TEST_CLASS)
 139                         .getConstantPool();
 140                 try {
 141                     Field field = cpInst.getClass()
 142                             .getDeclaredField("metaspaceConstantPool");
 143                     field.setAccessible(true);
 144                     field.set(cpInst, 0L);
 145                 } catch (ReflectiveOperationException e) {
 146                     throw new Error("TESTBUG : " + e, e);
 147                 }
 148                 return CompilerToVMHelper.getResolvedJavaType(cpInst,
 149                         ptr, COMPRESSED);
 150             }
 151         },
 152         OBJECT_TYPE_BASE {
 153             @Override
 154             HotSpotResolvedObjectType getResolvedJavaType() {
 155                 HotSpotResolvedObjectType type
 156                         = HotSpotResolvedObjectType.fromObjectClass(
 157                         OBJECT_TYPE_BASE.getClass());
 158                 long ptrToClass = UNSAFE.getKlassPointer(OBJECT_TYPE_BASE);
 159                 return CompilerToVMHelper.getResolvedJavaType(type,
 160                         getPtrToKlass() - ptrToClass, COMPRESSED);
 161             }
 162         },
 163         ;
 164         abstract HotSpotResolvedObjectType getResolvedJavaType();
 165     }
 166 
 167     private static final Unsafe UNSAFE = Utils.getUnsafe();
 168     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 169     private static final long PTR = UNSAFE.getKlassPointer(
 170             new GetResolvedJavaTypeTest());
 171     private static final Class TEST_CLASS = GetResolvedJavaTypeTest.class;
 172     /* a compressed parameter for tested method is set to false because
 173        unsafe.getKlassPointer always returns uncompressed pointer */
 174     private static final boolean COMPRESSED = false;
 175             // = WB.getBooleanVMFlag("UseCompressedClassPointers");
 176 
 177     private static long getPtrToKlass() {
 178         Field field;
 179         try {
 180             field = TEST_CLASS.getDeclaredField("PTR");
 181         } catch (NoSuchFieldException e) {
 182             throw new Error("TEST BUG : " + e, e);
 183         }
 184         Object base = UNSAFE.staticFieldBase(field);
 185         return WB.getObjectAddress(base) + UNSAFE.staticFieldOffset(field);
 186     }
 187 
 188     public void test(TestCase testCase) {
 189         System.out.println(testCase.name());
 190         HotSpotResolvedObjectType type = testCase.getResolvedJavaType();
 191         Asserts.assertEQ(TEST_CLASS,
 192                 CompilerToVMHelper.getMirror(type),
 193                 testCase +  " : unexpected class returned");
 194     }
 195 
 196     public static void main(String[] args) {
 197         GetResolvedJavaTypeTest test = new GetResolvedJavaTypeTest();
 198         for (TestCase testCase : TestCase.values()) {
 199             test.test(testCase);
 200         }
 201         testObjectBase();
 202         testMetaspaceWrapperBase();
 203     }
 204 
 205     private static void testMetaspaceWrapperBase() {
 206         try {
 207             HotSpotResolvedObjectType type
 208                     = CompilerToVMHelper.getResolvedJavaType(
 209                             new PublicMetaspaceWrapperObject() {
 210                                 @Override
 211                                 public long getMetaspacePointer() {
 212                                     return getPtrToKlass();
 213                                 }
 214                             }, 0L, COMPRESSED);
 215             throw new AssertionError("Test METASPACE_WRAPPER_BASE."
 216                     + " Expected IllegalArgumentException has not been caught");
 217         } catch (IllegalArgumentException iae) {
 218             // expected
 219         }
 220     }
 221 
 222     private static void testObjectBase() {
 223         try {
 224             HotSpotResolvedObjectType type
 225                     = CompilerToVMHelper.getResolvedJavaType(new Object(), 0L,
 226                             COMPRESSED);
 227             throw new AssertionError("Test OBJECT_BASE."
 228                 + " Expected IllegalArgumentException has not been caught");
 229         } catch (IllegalArgumentException iae) {
 230             // expected
 231         }
 232     }
 233 }