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