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