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