|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | + |
| 26 | +package com.oracle.svm.hosted.webimage.codegen; |
| 27 | + |
| 28 | +import java.lang.reflect.Executable; |
| 29 | +import java.lang.reflect.Method; |
| 30 | +import java.util.Set; |
| 31 | + |
| 32 | +import org.graalvm.nativeimage.Platforms; |
| 33 | + |
| 34 | +import com.oracle.graal.pointsto.meta.AnalysisMethod; |
| 35 | +import com.oracle.svm.core.ParsingReason; |
| 36 | +import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature; |
| 37 | +import com.oracle.svm.core.feature.InternalFeature; |
| 38 | +import com.oracle.svm.hosted.FeatureImpl; |
| 39 | +import com.oracle.svm.hosted.ImageClassLoader; |
| 40 | +import com.oracle.svm.hosted.webimage.codegen.node.InterceptJSInvokeNode; |
| 41 | +import com.oracle.svm.hosted.webimage.util.ReflectUtil; |
| 42 | +import com.oracle.svm.webimage.platform.WebImagePlatform; |
| 43 | + |
| 44 | +import jdk.graal.compiler.nodes.ValueNode; |
| 45 | +import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
| 46 | +import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderContext; |
| 47 | +import jdk.graal.compiler.nodes.graphbuilderconf.NodePlugin; |
| 48 | +import jdk.graal.compiler.phases.util.Providers; |
| 49 | +import jdk.vm.ci.meta.ResolvedJavaMethod; |
| 50 | + |
| 51 | +/** |
| 52 | + * Inserts {@link InterceptJSInvokeNode} into the IR at any invoke that may target a |
| 53 | + * {@link org.graalvm.webimage.api.JS} annotated method to deal with types that may leak to |
| 54 | + * JavaScript. |
| 55 | + * <p> |
| 56 | + * TODO GR-62854 Merge into JSBodyFeature once it is enabled by default |
| 57 | + */ |
| 58 | +@AutomaticallyRegisteredFeature |
| 59 | +@Platforms(WebImagePlatform.class) |
| 60 | +public class JSBodyTypeFlowFeature implements InternalFeature { |
| 61 | + /** |
| 62 | + * The set of methods that are potentially overridden by a JS-annotated method. |
| 63 | + * <p> |
| 64 | + * Any call to such a method needs to have a {@link InterceptJSInvokeNode} attached to it. |
| 65 | + */ |
| 66 | + private Set<Method> jsOverridden; |
| 67 | + |
| 68 | + @Override |
| 69 | + public void afterRegistration(AfterRegistrationAccess access) { |
| 70 | + FeatureImpl.AfterRegistrationAccessImpl accessImpl = (FeatureImpl.AfterRegistrationAccessImpl) access; |
| 71 | + ImageClassLoader imageClassLoader = accessImpl.getImageClassLoader(); |
| 72 | + jsOverridden = ReflectUtil.findBaseMethodsOfJSAnnotated(imageClassLoader); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void registerGraphBuilderPlugins(Providers providers, GraphBuilderConfiguration.Plugins plugins, ParsingReason reason) { |
| 77 | + plugins.appendNodePlugin(new NodePlugin() { |
| 78 | + @Override |
| 79 | + public boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, ValueNode[] args) { |
| 80 | + if (canBeJavaScriptCall((AnalysisMethod) method)) { |
| 81 | + InterceptJSInvokeNode intercept = b.append(new InterceptJSInvokeNode(method, b.bci())); |
| 82 | + for (final ValueNode arg : args) { |
| 83 | + intercept.arguments().add(arg); |
| 84 | + } |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + private boolean canBeJavaScriptCall(AnalysisMethod method) { |
| 90 | + Executable executable; |
| 91 | + try { |
| 92 | + executable = method.getJavaMethod(); |
| 93 | + } catch (Throwable e) { |
| 94 | + // Either a substituted method, or a method with a malformed bytecode signature. |
| 95 | + // This is most likely not a JS-annotated method. |
| 96 | + return false; |
| 97 | + } |
| 98 | + if (executable instanceof Method) { |
| 99 | + return jsOverridden.contains(executable); |
| 100 | + } |
| 101 | + // Not a normal method (constructor). |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void afterAnalysis(AfterAnalysisAccess access) { |
| 110 | + jsOverridden = null; |
| 111 | + } |
| 112 | +} |
0 commit comments