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