1 /* 2 * Copyright (c) 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 8152343 27 * @bug 8161068 28 * @requires (vm.simpleArch == "x64" | vm.simpleArch == "sparcv9" | vm.simpleArch == "aarch64") 29 * @library /test/lib /compiler/jvmci/jdk.vm.ci.hotspot.test/src 30 * @modules jdk.vm.ci/jdk.vm.ci.meta 31 * jdk.vm.ci/jdk.vm.ci.runtime 32 * jdk.vm.ci/jdk.vm.ci.hotspot 33 * @run testng/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI 34 * jdk.vm.ci.hotspot.test.MethodHandleAccessProviderTest 35 */ 36 37 package jdk.vm.ci.hotspot.test; 38 39 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider; 40 import jdk.vm.ci.meta.JavaConstant; 41 import jdk.vm.ci.meta.MetaAccessProvider; 42 import jdk.vm.ci.meta.MethodHandleAccessProvider; 43 import jdk.vm.ci.meta.MethodHandleAccessProvider.IntrinsicMethod; 44 import jdk.vm.ci.meta.ResolvedJavaMethod; 45 import jdk.vm.ci.runtime.JVMCI; 46 import org.testng.Assert; 47 import org.testng.annotations.Test; 48 49 import java.lang.invoke.MethodHandle; 50 import java.lang.invoke.MethodHandles; 51 import java.lang.reflect.Method; 52 53 public class MethodHandleAccessProviderTest { 54 private static final HotSpotConstantReflectionProvider CONSTANT_REFLECTION = (HotSpotConstantReflectionProvider) JVMCI.getRuntime().getHostJVMCIBackend().getConstantReflection(); 55 private static final MethodHandleAccessProvider PROVIDER = CONSTANT_REFLECTION.getMethodHandleAccess(); 56 private static final MetaAccessProvider META_ACCESS = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess(); 57 58 @Test(dataProvider = "intrinsicsPositive", dataProviderClass = MethodHandleAccessProviderData.class) 59 public void testLookupMethodHandleIntrinsic(ResolvedJavaMethod mtd, IntrinsicMethod expected) { 60 Assert.assertEquals(expected, PROVIDER.lookupMethodHandleIntrinsic(mtd), "Unexpected intrinsic returned for " + mtd); 61 } 62 63 @Test(dataProvider = "intrinsicsNegative", dataProviderClass = MethodHandleAccessProviderData.class) 64 public void testLookupMethodHandleIntrinsicNegative(ResolvedJavaMethod mtd) { 65 Assert.assertNull(PROVIDER.lookupMethodHandleIntrinsic(mtd), "Expected null return for " + mtd); 66 } 67 68 @Test(expectedExceptions = {NullPointerException.class}) 69 public void testLookupMethodHandleIntrinsicNull() { 70 PROVIDER.lookupMethodHandleIntrinsic(null); 71 } 72 73 @Test(dataProvider = "invokeBasicPositive", dataProviderClass = MethodHandleAccessProviderData.class) 74 public void testResolveInvokeBasicTarget(JavaConstant javaConstantMethodHandle, boolean force, String expected) { 75 ResolvedJavaMethod mtd = PROVIDER.resolveInvokeBasicTarget(javaConstantMethodHandle, force); 76 Assert.assertTrue(mtd.getName().startsWith(expected), "Unexpected method resolved: " + mtd); 77 } 78 79 @Test(dataProvider = "invokeBasicNegative1", dataProviderClass = MethodHandleAccessProviderData.class) 80 public void testResolveInvokeBasicTargetNegative1(JavaConstant javaConstantMethodHandle, boolean force) { 81 Assert.assertNull(PROVIDER.resolveInvokeBasicTarget(javaConstantMethodHandle, force), 82 "Expected null return for " + javaConstantMethodHandle + " with force=" + force); 83 } 84 85 @Test(dataProvider = "invokeBasicNegative2", dataProviderClass = MethodHandleAccessProviderData.class, expectedExceptions = {NullPointerException.class}) 86 public void testResolveInvokeBasicTargetNegative2(JavaConstant javaConstantMethodHandle, boolean force) { 87 PROVIDER.resolveInvokeBasicTarget(javaConstantMethodHandle, force); 88 } 89 90 @Test 91 public void testResolveLinkToTarget() { 92 Method self; 93 try { 94 self = getClass().getDeclaredMethod("testResolveLinkToTarget"); 95 } catch (NoSuchMethodException e) { 96 throw new Error("TESTBUG: can't find method: " + e, e); 97 } 98 MethodHandle mh; 99 try { 100 mh = MethodHandles.lookup().unreflect(self); 101 } catch (IllegalAccessException e) { 102 throw new Error("TESTBUG: can't get MHandle: " + e, e); 103 } 104 Method internalMemberNameMethod; 105 try { 106 internalMemberNameMethod = mh.getClass().getDeclaredMethod("internalMemberName"); 107 } catch (NoSuchMethodException e) { 108 throw new Error("TESTBUG: can't find method: " + e, e); 109 } 110 internalMemberNameMethod.setAccessible(true); 111 Object memberName; 112 try { 113 memberName = internalMemberNameMethod.invoke(mh); 114 } catch (ReflectiveOperationException e) { 115 throw new Error("TESTBUG: can't invoke internalMemberName method", e); 116 } 117 JavaConstant jcMemberName = CONSTANT_REFLECTION.forObject(memberName); 118 ResolvedJavaMethod mtd = PROVIDER.resolveLinkToTarget(jcMemberName); 119 Assert.assertEquals(mtd, META_ACCESS.lookupJavaMethod(self), "Got unexpected method: " + mtd); 120 } 121 122 @Test(expectedExceptions = {NullPointerException.class}) 123 public void testResolveLinkToTargetNegativeNull() { 124 PROVIDER.resolveLinkToTarget(null); 125 } 126 127 @Test 128 public void testResolveLinkToTargetNegativeNullConstant() { 129 Assert.assertNull(PROVIDER.resolveLinkToTarget(JavaConstant.NULL_POINTER), "Expected null return"); 130 } 131 132 @Test(expectedExceptions = {IllegalArgumentException.class}) 133 public void testResolveLinkToTargetNegativeWrongConstant() { 134 PROVIDER.resolveLinkToTarget(CONSTANT_REFLECTION.forObject("42")); 135 } 136 }