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