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 * @test 26 * @bug 8136421 27 * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64" 28 * @library / /testlibrary /../../test/lib 29 * @compile ../common/CompilerToVMHelper.java 30 * ../common/PublicMetaspaceWrapperObject.java 31 * @build compiler.jvmci.compilerToVM.GetResolvedJavaMethodTest 32 * @run main ClassFileInstaller 33 * sun.hotspot.WhiteBox 34 * sun.hotspot.WhiteBox$WhiteBoxPermission 35 * jdk.vm.ci.hotspot.CompilerToVMHelper 36 * jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject 37 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockExperimentalVMOptions 38 * -XX:+EnableJVMCI -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 39 * compiler.jvmci.compilerToVM.GetResolvedJavaMethodTest 40 */ 41 42 package compiler.jvmci.compilerToVM; 43 44 import jdk.vm.ci.hotspot.CompilerToVMHelper; 45 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 46 import jdk.vm.ci.hotspot.PublicMetaspaceWrapperObject; 47 import jdk.test.lib.Asserts; 48 import jdk.test.lib.Utils; 49 import sun.hotspot.WhiteBox; 50 import sun.misc.Unsafe; 51 52 import java.lang.reflect.Field; 53 54 public class GetResolvedJavaMethodTest { 55 private static enum TestCase { 56 NULL_BASE { 57 @Override 58 HotSpotResolvedJavaMethod getResolvedJavaMethod() { 59 return CompilerToVMHelper.getResolvedJavaMethod( 60 null, getPtrToMethod()); 61 } 62 }, 63 JAVA_METHOD_BASE { 64 @Override 65 HotSpotResolvedJavaMethod getResolvedJavaMethod() { 66 HotSpotResolvedJavaMethod methodInstance 67 = CompilerToVMHelper.getResolvedJavaMethodAtSlot( 68 TEST_CLASS, 0); 69 try { 70 METASPACE_METHOD_FIELD.set(methodInstance, 71 getPtrToMethod()); 72 } catch (ReflectiveOperationException e) { 73 throw new Error("TEST BUG : " + e, e); 74 } 75 return CompilerToVMHelper.getResolvedJavaMethod( 76 methodInstance, 0L); 77 } 78 }, 79 JAVA_METHOD_BASE_IN_TWO { 80 @Override 81 HotSpotResolvedJavaMethod getResolvedJavaMethod() { 82 long ptr = getPtrToMethod(); 83 HotSpotResolvedJavaMethod methodInstance 84 = CompilerToVMHelper.getResolvedJavaMethodAtSlot( 85 TEST_CLASS, 0); 86 try { 87 METASPACE_METHOD_FIELD.set(methodInstance, ptr / 2L); 88 } catch (ReflectiveOperationException e) { 89 throw new Error("TESTBUG : " + e, e); 90 } 91 return CompilerToVMHelper.getResolvedJavaMethod(methodInstance, 92 ptr - ptr / 2L); 93 } 94 }, 95 JAVA_METHOD_BASE_ZERO { 96 @Override 97 HotSpotResolvedJavaMethod getResolvedJavaMethod() { 98 long ptr = getPtrToMethod(); 99 HotSpotResolvedJavaMethod methodInstance 100 = CompilerToVMHelper.getResolvedJavaMethodAtSlot( 101 TEST_CLASS, 0); 102 try { 103 METASPACE_METHOD_FIELD.set(methodInstance, 0L); 104 } catch (ReflectiveOperationException e) { 105 throw new Error("TESTBUG : " + e, e); 106 } 107 return CompilerToVMHelper.getResolvedJavaMethod(methodInstance, 108 ptr); 109 } 110 } 111 ; 112 abstract HotSpotResolvedJavaMethod getResolvedJavaMethod(); 113 } 114 115 private static final Unsafe UNSAFE = Utils.getUnsafe(); 116 private static final WhiteBox WB = WhiteBox.getWhiteBox(); 117 private static final Field METASPACE_METHOD_FIELD; 118 private static final Class<?> TEST_CLASS = GetResolvedJavaMethodTest.class; 119 private static final long PTR; 120 static { 121 HotSpotResolvedJavaMethod method 122 = CompilerToVMHelper.getResolvedJavaMethodAtSlot(TEST_CLASS, 0); 123 try { 124 // jdk.vm.ci.hotspot.HotSpotResolvedJavaMethodImpl.metaspaceMethod 125 METASPACE_METHOD_FIELD = method.getClass() 126 .getDeclaredField("metaspaceMethod"); 127 METASPACE_METHOD_FIELD.setAccessible(true); 128 PTR = (long) METASPACE_METHOD_FIELD.get(method); 129 } catch (ReflectiveOperationException e) { 130 throw new Error("TESTBUG : " + e, e); 131 } 132 133 } 134 135 private static long getPtrToMethod() { 136 Field field; 137 try { 138 field = TEST_CLASS.getDeclaredField("PTR"); 139 } catch (NoSuchFieldException e) { 140 throw new Error("TEST BUG : " + e, e); 141 } 142 Object base = UNSAFE.staticFieldBase(field); 143 return WB.getObjectAddress(base) + UNSAFE.staticFieldOffset(field); 144 } 145 146 public void test(TestCase testCase) { 147 System.out.println(testCase.name()); 148 HotSpotResolvedJavaMethod result = testCase.getResolvedJavaMethod(); 149 Asserts.assertNotNull(result, testCase + " : got null"); 150 Asserts.assertEQ(TEST_CLASS, 151 CompilerToVMHelper.getMirror(result.getDeclaringClass()), 152 testCase + " : unexpected declaring class"); 153 } 154 155 public static void main(String[] args) { 156 GetResolvedJavaMethodTest test = new GetResolvedJavaMethodTest(); 157 for (TestCase testCase : TestCase.values()) { 158 test.test(testCase); 159 } 160 testObjectBase(); 161 testMetaspaceWrapperBase(); 162 } 163 164 private static void testMetaspaceWrapperBase() { 165 try { 166 HotSpotResolvedJavaMethod method 167 = CompilerToVMHelper.getResolvedJavaMethod( 168 new PublicMetaspaceWrapperObject() { 169 @Override 170 public long getMetaspacePointer() { 171 return getPtrToMethod(); 172 } 173 }, 0L); 174 throw new AssertionError("Test METASPACE_WRAPPER_BASE." 175 + " Expected IllegalArgumentException has not been caught"); 176 } catch (IllegalArgumentException e) { 177 // expected 178 } 179 } 180 181 private static void testObjectBase() { 182 try { 183 HotSpotResolvedJavaMethod method 184 = CompilerToVMHelper.getResolvedJavaMethod(new Object(), 0L); 185 throw new AssertionError("Test OBJECT_BASE." 186 + " Expected IllegalArgumentException has not been caught"); 187 } catch (IllegalArgumentException e) { 188 // expected 189 } 190 } 191 }