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