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