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