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 /*
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  29  * @library /testlibrary /test/lib /
  30  * @library ../common/patches
  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  * @build jdk.vm.ci/jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject
  35  * @build compiler.jvmci.compilerToVM.GetConstantPoolTest
  36  * @build sun.hotspot.WhiteBox
  37  * @run main ClassFileInstaller 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  *                   compiler.jvmci.compilerToVM.GetConstantPoolTest
  43  */
  44 package compiler.jvmci.compilerToVM;
  45 
  46 import java.lang.reflect.Field;
  47 import jdk.vm.ci.meta.ConstantPool;
  48 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  49 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  50 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  51 import jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject;
  52 import jdk.test.lib.Utils;
  53 import sun.hotspot.WhiteBox;
  54 import sun.misc.Unsafe;
  55 
  56 /**
  57  * Tests for jdk.vm.ci.hotspot.CompilerToVM::getConstantPool method
  58  */
  59 public class GetConstantPoolTest {
  60     private static enum TestCase {
  61         NULL_BASE {
  62             @Override
  63             ConstantPool getConstantPool() {
  64                 return CompilerToVMHelper.getConstantPool(null,
  65                         getPtrToCpAddress());
  66             }
  67         },
  68         JAVA_METHOD_BASE {
  69             @Override
  70             ConstantPool getConstantPool() {
  71                 HotSpotResolvedJavaMethod methodInstance
  72                         = CompilerToVMHelper.getResolvedJavaMethodAtSlot(
  73                                 TEST_CLASS, 0);
  74                 Field field;
  75                 try {
  76                     // jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl.metaspaceMethod
  77                     field = methodInstance.getClass()
  78                             .getDeclaredField("metaspaceMethod");
  79                     field.setAccessible(true);
  80                     field.set(methodInstance, getPtrToCpAddress());
  81                 } catch (ReflectiveOperationException e) {
  82                     throw new Error("TESTBUG : " + e, e);
  83                 }
  84 
  85                 return CompilerToVMHelper.getConstantPool(methodInstance, 0L);
  86             }
  87         },
  88         CONSTANT_POOL_BASE {
  89             @Override
  90             ConstantPool getConstantPool() {
  91                 ConstantPool cpInst;
  92                 try {
  93                     cpInst = CompilerToVMHelper.getConstantPool(null,
  94                             getPtrToCpAddress());
  95                     Field field = CompilerToVMHelper.HotSpotConstantPoolClass()
  96                             .getDeclaredField("metaspaceConstantPool");
  97                     field.setAccessible(true);
  98                     field.set(cpInst, getPtrToCpAddress());
  99                 } catch (ReflectiveOperationException e) {
 100                     throw new Error("TESTBUG : " + e.getMessage(), e);
 101                 }
 102                 return CompilerToVMHelper.getConstantPool(cpInst, 0L);
 103             }
 104         },
 105         CONSTANT_POOL_BASE_IN_TWO {
 106             @Override
 107             ConstantPool getConstantPool() {
 108                 long ptr = getPtrToCpAddress();
 109                 ConstantPool cpInst;
 110                 try {
 111                     cpInst = CompilerToVMHelper.getConstantPool(null, ptr);
 112                     Field field = CompilerToVMHelper.HotSpotConstantPoolClass()
 113                             .getDeclaredField("metaspaceConstantPool");
 114                     field.setAccessible(true);
 115                     field.set(cpInst, ptr / 2L);
 116                 } catch (ReflectiveOperationException e) {
 117                     throw new Error("TESTBUG : " + e.getMessage(), e);
 118                 }
 119                 return CompilerToVMHelper.getConstantPool(cpInst,
 120                         ptr - ptr / 2L);
 121             }
 122         },
 123         CONSTANT_POOL_BASE_ZERO {
 124             @Override
 125             ConstantPool getConstantPool() {
 126                 long ptr = getPtrToCpAddress();
 127                 ConstantPool cpInst;
 128                 try {
 129                     cpInst = CompilerToVMHelper.getConstantPool(null, ptr);
 130                     Field field = CompilerToVMHelper.HotSpotConstantPoolClass()
 131                             .getDeclaredField("metaspaceConstantPool");
 132                     field.setAccessible(true);
 133                     field.set(cpInst, 0L);
 134                 } catch (ReflectiveOperationException e) {
 135                     throw new Error("TESTBUG : " + e.getMessage(), e);
 136                 }
 137                 return CompilerToVMHelper.getConstantPool(cpInst, ptr);
 138             }
 139         },
 140         OBJECT_TYPE_BASE {
 141             @Override
 142             ConstantPool getConstantPool() {
 143                 HotSpotResolvedObjectType type
 144                         = HotSpotResolvedObjectType.fromObjectClass(
 145                                 OBJECT_TYPE_BASE.getClass());
 146                 long ptrToClass = UNSAFE.getKlassPointer(OBJECT_TYPE_BASE);
 147                 return CompilerToVMHelper.getConstantPool(type,
 148                         getPtrToCpAddress() - ptrToClass);
 149             }
 150         },
 151         ;
 152         abstract ConstantPool getConstantPool();
 153     }
 154 
 155     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 156     private static final Unsafe UNSAFE = Utils.getUnsafe();
 157 
 158     private static final Class TEST_CLASS = GetConstantPoolTest.class;
 159     private static final long CP_ADDRESS
 160             = WB.getConstantPool(GetConstantPoolTest.class);
 161 
 162     public void test(TestCase testCase) {
 163         System.out.println(testCase.name());
 164         ConstantPool cp = testCase.getConstantPool();
 165         String cpStringRep = cp.toString();
 166         String cpClassSimpleName
 167                 = CompilerToVMHelper.HotSpotConstantPoolClass().getSimpleName();
 168         if (!cpStringRep.contains(cpClassSimpleName)
 169                 || !cpStringRep.contains(TEST_CLASS.getName())) {
 170             String msg = String.format("%s : "
 171                     + " Constant pool is not valid."
 172                     + " String representation should contain \"%s\" and \"%s\"",
 173                     testCase.name(), cpClassSimpleName,
 174                     TEST_CLASS.getName());
 175             throw new AssertionError(msg);
 176         }
 177     }
 178 
 179     public static void main(String[] args) {
 180         GetConstantPoolTest test = new GetConstantPoolTest();
 181         for (TestCase testCase : TestCase.values()) {
 182             test.test(testCase);
 183         }
 184         testObjectBase();
 185         testMetaspaceWrapperBase();
 186     }
 187 
 188     private static void testObjectBase() {
 189         try {
 190             Object cp = CompilerToVMHelper.getConstantPool(new Object(), 0L);
 191             throw new AssertionError("Test OBJECT_BASE."
 192                 + " Expected IllegalArgumentException has not been caught");
 193         } catch (IllegalArgumentException iae) {
 194             // expected
 195         }
 196     }
 197     private static void testMetaspaceWrapperBase() {
 198         try {
 199             Object cp = CompilerToVMHelper.getConstantPool(
 200                     new PublicMetaspaceWrapperObject() {
 201                         @Override
 202                         public long getMetaspacePointer() {
 203                             return getPtrToCpAddress();
 204                         }
 205                     }, 0L);
 206             throw new AssertionError("Test METASPACE_WRAPPER_BASE."
 207                 + " Expected IllegalArgumentException has not been caught");
 208         } catch (IllegalArgumentException iae) {
 209             // expected
 210         }
 211     }
 212 
 213     private static long getPtrToCpAddress() {
 214         Field field;
 215         try {
 216             field = TEST_CLASS.getDeclaredField("CP_ADDRESS");
 217         } catch (NoSuchFieldException nsfe) {
 218             throw new Error("TESTBUG : cannot find field \"CP_ADDRESS\" : "
 219                     + nsfe.getMessage(), nsfe);
 220         }
 221         Object base = UNSAFE.staticFieldBase(field);
 222         return WB.getObjectAddress(base) + UNSAFE.staticFieldOffset(field);
 223     }
 224 }