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