1 /*
   2  * Copyright (c) 2015, 2016, 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.jvmci
  28  * @library / /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  *        sun.hotspot.WhiteBox
  38  * @run driver ClassFileInstaller 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 jdk.internal.misc.Unsafe;
  55 import jdk.test.lib.Asserts;
  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.vm.ci.meta.ConstantPool;
  61 import sun.hotspot.WhiteBox;
  62 
  63 import java.lang.reflect.Field;
  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         ;
 153         abstract HotSpotResolvedObjectType getResolvedJavaType();
 154     }
 155 
 156     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
 157     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 158     private static final Class TEST_CLASS = GetResolvedJavaTypeTest.class;
 159     /* a compressed parameter for tested method is set to false because
 160        unsafe.getKlassPointer always returns uncompressed pointer */
 161     private static final boolean COMPRESSED = false;
 162             // = WB.getBooleanVMFlag("UseCompressedClassPointers");
 163 
 164     private static long getPtrToKlass() {
 165         Field field;
 166         try {
 167             field = TEST_CLASS.getDeclaredField("PTR");
 168         } catch (NoSuchFieldException e) {
 169             throw new Error("TEST BUG : " + e, e);
 170         }
 171         Object base = UNSAFE.staticFieldBase(field);
 172         return WB.getObjectAddress(base) + UNSAFE.staticFieldOffset(field);
 173     }
 174 
 175     public void test(TestCase testCase) {
 176         System.out.println(testCase.name());
 177         HotSpotResolvedObjectType type = testCase.getResolvedJavaType();
 178         Asserts.assertEQ(TEST_CLASS,
 179                 CompilerToVMHelper.getMirror(type),
 180                 testCase +  " : unexpected class returned");
 181     }
 182 
 183     public static void main(String[] args) {
 184         GetResolvedJavaTypeTest test = new GetResolvedJavaTypeTest();
 185         for (TestCase testCase : TestCase.values()) {
 186             test.test(testCase);
 187         }
 188         testObjectBase();
 189         testMetaspaceWrapperBase();
 190     }
 191 
 192     private static void testMetaspaceWrapperBase() {
 193         try {
 194             HotSpotResolvedObjectType type
 195                     = CompilerToVMHelper.getResolvedJavaType(
 196                             new PublicMetaspaceWrapperObject() {
 197                                 @Override
 198                                 public long getMetaspacePointer() {
 199                                     return getPtrToKlass();
 200                                 }
 201                             }, 0L, COMPRESSED);
 202             throw new AssertionError("Test METASPACE_WRAPPER_BASE."
 203                     + " Expected IllegalArgumentException has not been caught");
 204         } catch (IllegalArgumentException iae) {
 205             // expected
 206         }
 207     }
 208 
 209     private static void testObjectBase() {
 210         try {
 211             HotSpotResolvedObjectType type
 212                     = CompilerToVMHelper.getResolvedJavaType(new Object(), 0L,
 213                             COMPRESSED);
 214             throw new AssertionError("Test OBJECT_BASE."
 215                 + " Expected IllegalArgumentException has not been caught");
 216         } catch (IllegalArgumentException iae) {
 217             // expected
 218         }
 219     }
 220 }