|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package jdk.graal.compiler.truffle.test; |
| 26 | + |
| 27 | +import java.lang.invoke.MethodHandle; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import org.graalvm.polyglot.Context; |
| 31 | +import org.graalvm.polyglot.HostAccess; |
| 32 | +import org.junit.Assert; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | +import org.junit.runners.Parameterized; |
| 37 | +import org.junit.runners.Parameterized.Parameter; |
| 38 | +import org.junit.runners.Parameterized.Parameters; |
| 39 | + |
| 40 | +import com.oracle.truffle.api.CompilerDirectives; |
| 41 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 42 | +import com.oracle.truffle.api.interop.ArityException; |
| 43 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 44 | +import com.oracle.truffle.api.interop.UnknownIdentifierException; |
| 45 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 46 | +import com.oracle.truffle.api.interop.UnsupportedTypeException; |
| 47 | +import com.oracle.truffle.api.nodes.RootNode; |
| 48 | +import com.oracle.truffle.api.test.polyglot.ProxyLanguage; |
| 49 | + |
| 50 | +import jdk.graal.compiler.nodes.NodeView; |
| 51 | +import jdk.graal.compiler.nodes.StructuredGraph; |
| 52 | +import jdk.graal.compiler.nodes.java.MethodCallTargetNode; |
| 53 | + |
| 54 | +/** |
| 55 | + * Regression test for host interop call partial evaluation (HostExecuteNode). |
| 56 | + */ |
| 57 | +@RunWith(Parameterized.class) |
| 58 | +public class GR69684Test extends PartialEvaluationTest { |
| 59 | + |
| 60 | + @Parameter(0) public String methodName; |
| 61 | + |
| 62 | + @Parameters(name = "{0}") |
| 63 | + public static List<String> data() { |
| 64 | + return List.of("directMethod", "overloadedMethod"); |
| 65 | + } |
| 66 | + |
| 67 | + @Before |
| 68 | + public void setup() { |
| 69 | + setupContext(Context.newBuilder().allowExperimentalOptions(true).option("engine.CompilationFailureAction", "Throw").option("engine.BackgroundCompilation", "false").option( |
| 70 | + "compiler.TreatPerformanceWarningsAsErrors", "all").build()); |
| 71 | + getContext().initialize(ProxyLanguage.ID); |
| 72 | + } |
| 73 | + |
| 74 | + @HostAccess.Export |
| 75 | + public Object directMethod(@SuppressWarnings("unused") String a, final Object b) { |
| 76 | + return b; |
| 77 | + } |
| 78 | + |
| 79 | + @HostAccess.Export |
| 80 | + public Object overloadedMethod(@SuppressWarnings("unused") String a, final Object b) { |
| 81 | + return b; |
| 82 | + } |
| 83 | + |
| 84 | + @HostAccess.Export |
| 85 | + public Object overloadedMethod(@SuppressWarnings("unused") Object a, final Object b) { |
| 86 | + // not supposed to be called |
| 87 | + throw new AssertionError(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void test() { |
| 92 | + var rootNode = new RootNode(ProxyLanguage.get(null)) { |
| 93 | + @Child InteropLibrary interop = InteropLibrary.getFactory().createDispatched(1); |
| 94 | + |
| 95 | + @Override |
| 96 | + public Object execute(VirtualFrame frame) { |
| 97 | + try { |
| 98 | + Object receiver = ProxyLanguage.LanguageContext.get(this).getEnv().asGuestValue(GR69684Test.this); |
| 99 | + return interop.invokeMember(receiver, methodName, "a", "b"); |
| 100 | + } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) { |
| 101 | + throw CompilerDirectives.shouldNotReachHere(e); |
| 102 | + } |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + StructuredGraph graph = partialEval(rootNode); |
| 107 | + |
| 108 | + // After PE, only a single invoke taking a constant MethodHandle is expected. |
| 109 | + var invokedMethods = graph.getNodes(MethodCallTargetNode.TYPE).stream().filter(methodCall -> { |
| 110 | + return !methodCall.arguments().stream().anyMatch(argument -> argument.isConstant() && |
| 111 | + getMetaAccess().lookupJavaType(MethodHandle.class).isAssignableFrom( |
| 112 | + argument.stamp(NodeView.DEFAULT).javaType(getMetaAccess()))); |
| 113 | + }).map(MethodCallTargetNode::targetMethod).toList(); |
| 114 | + Assert.assertEquals("Unexpected invokes: " + invokedMethods, 0, invokedMethods.size()); |
| 115 | + } |
| 116 | +} |
0 commit comments