43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 |
package org.graalvm.compiler.hotspot.meta;
|
|
24 |
|
46459
|
25 |
import static org.graalvm.compiler.serviceprovider.JDK9Method.Java8OrEarlier;
|
|
26 |
|
43972
|
27 |
import java.lang.reflect.Type;
|
46459
|
28 |
import java.util.Set;
|
43972
|
29 |
|
48861
|
30 |
import org.graalvm.collections.EconomicSet;
|
43972
|
31 |
import org.graalvm.compiler.core.common.GraalOptions;
|
46459
|
32 |
import org.graalvm.compiler.debug.GraalError;
|
43972
|
33 |
import org.graalvm.compiler.graph.Node;
|
|
34 |
import org.graalvm.compiler.graph.iterators.NodeIterable;
|
|
35 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
|
|
36 |
import org.graalvm.compiler.hotspot.phases.AheadOfTimeVerificationPhase;
|
|
37 |
import org.graalvm.compiler.nodes.ConstantNode;
|
|
38 |
import org.graalvm.compiler.nodes.FrameState;
|
|
39 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
|
|
40 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin;
|
|
41 |
import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins;
|
|
42 |
import org.graalvm.compiler.nodes.type.StampTool;
|
46459
|
43 |
import org.graalvm.compiler.phases.tiers.CompilerConfiguration;
|
43972
|
44 |
import org.graalvm.compiler.replacements.nodes.MacroNode;
|
46459
|
45 |
import org.graalvm.compiler.serviceprovider.JDK9Method;
|
43972
|
46 |
|
46459
|
47 |
import jdk.vm.ci.hotspot.HotSpotResolvedJavaType;
|
43972
|
48 |
import jdk.vm.ci.meta.JavaKind;
|
|
49 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Extension of {@link InvocationPlugins} that disables plugins based on runtime configuration.
|
|
53 |
*/
|
|
54 |
final class HotSpotInvocationPlugins extends InvocationPlugins {
|
46459
|
55 |
private final GraalHotSpotVMConfig config;
|
|
56 |
private final EconomicSet<Object> trustedModules;
|
|
57 |
private final ClassLoader extLoader;
|
43972
|
58 |
|
46459
|
59 |
HotSpotInvocationPlugins(GraalHotSpotVMConfig config, CompilerConfiguration compilerConfiguration) {
|
43972
|
60 |
this.config = config;
|
46459
|
61 |
if (Java8OrEarlier) {
|
|
62 |
extLoader = getExtLoader();
|
|
63 |
trustedModules = null;
|
|
64 |
} else {
|
|
65 |
extLoader = null;
|
|
66 |
trustedModules = initTrustedModules(compilerConfiguration);
|
|
67 |
}
|
43972
|
68 |
}
|
|
69 |
|
|
70 |
@Override
|
|
71 |
public void register(InvocationPlugin plugin, Type declaringClass, String name, Type... argumentTypes) {
|
|
72 |
if (!config.usePopCountInstruction) {
|
|
73 |
if (name.equals("bitCount")) {
|
|
74 |
assert declaringClass.equals(Integer.class) || declaringClass.equals(Long.class);
|
|
75 |
return;
|
|
76 |
}
|
|
77 |
}
|
|
78 |
super.register(plugin, declaringClass, name, argumentTypes);
|
|
79 |
}
|
|
80 |
|
|
81 |
@Override
|
|
82 |
public void checkNewNodes(GraphBuilderContext b, InvocationPlugin plugin, NodeIterable<Node> newNodes) {
|
|
83 |
for (Node node : newNodes) {
|
|
84 |
if (node instanceof MacroNode) {
|
|
85 |
// MacroNode based plugins can only be used for inlining since they
|
|
86 |
// require a valid bci should they need to replace themselves with
|
|
87 |
// an InvokeNode during lowering.
|
|
88 |
assert plugin.inlineOnly() : String.format("plugin that creates a %s (%s) must return true for inlineOnly(): %s", MacroNode.class.getSimpleName(), node, plugin);
|
|
89 |
}
|
|
90 |
}
|
46344
|
91 |
if (GraalOptions.ImmutableCode.getValue(b.getOptions())) {
|
43972
|
92 |
for (Node node : newNodes) {
|
|
93 |
if (node.hasUsages() && node instanceof ConstantNode) {
|
|
94 |
ConstantNode c = (ConstantNode) node;
|
|
95 |
if (c.getStackKind() == JavaKind.Object && AheadOfTimeVerificationPhase.isIllegalObjectConstant(c)) {
|
|
96 |
if (isClass(c)) {
|
|
97 |
// This will be handled later by LoadJavaMirrorWithKlassPhase
|
|
98 |
} else {
|
|
99 |
// Tolerate uses in unused FrameStates
|
|
100 |
if (node.usages().filter((n) -> !(n instanceof FrameState) || n.hasUsages()).isNotEmpty()) {
|
|
101 |
throw new AssertionError("illegal constant node in AOT: " + node);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|
|
108 |
super.checkNewNodes(b, plugin, newNodes);
|
|
109 |
}
|
|
110 |
|
|
111 |
private static boolean isClass(ConstantNode node) {
|
|
112 |
ResolvedJavaType type = StampTool.typeOrNull(node);
|
|
113 |
return type != null && "Ljava/lang/Class;".equals(type.getName());
|
|
114 |
}
|
46459
|
115 |
|
|
116 |
/**
|
|
117 |
* {@inheritDoc}
|
|
118 |
*
|
|
119 |
* On JDK 8, only classes loaded by the boot, JVMCI or extension class loaders are trusted.
|
|
120 |
*
|
|
121 |
* On JDK 9 and later, only classes in the {@link CompilerConfiguration} defining module or any
|
|
122 |
* of its module dependencies are trusted.
|
|
123 |
*/
|
|
124 |
@Override
|
48861
|
125 |
public boolean canBeIntrinsified(ResolvedJavaType declaringClass) {
|
46459
|
126 |
if (declaringClass instanceof HotSpotResolvedJavaType) {
|
|
127 |
Class<?> javaClass = ((HotSpotResolvedJavaType) declaringClass).mirror();
|
|
128 |
if (Java8OrEarlier) {
|
|
129 |
ClassLoader cl = javaClass.getClassLoader();
|
|
130 |
return cl == null || cl == getClass().getClassLoader() || cl == extLoader;
|
|
131 |
} else {
|
|
132 |
Object module = JDK9Method.getModule.invoke(javaClass);
|
|
133 |
return trustedModules.contains(module);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
return false;
|
|
137 |
}
|
|
138 |
|
|
139 |
private static ClassLoader getExtLoader() {
|
|
140 |
try {
|
|
141 |
Object launcher = Class.forName("sun.misc.Launcher").getMethod("getLauncher").invoke(null);
|
|
142 |
ClassLoader appLoader = (ClassLoader) launcher.getClass().getMethod("getClassLoader").invoke(launcher);
|
|
143 |
ClassLoader extLoader = appLoader.getParent();
|
|
144 |
assert extLoader.getClass().getName().equals("sun.misc.Launcher$ExtClassLoader") : extLoader;
|
|
145 |
return extLoader;
|
|
146 |
} catch (Exception e) {
|
|
147 |
throw new GraalError(e);
|
|
148 |
}
|
|
149 |
}
|
|
150 |
|
|
151 |
private static EconomicSet<Object> initTrustedModules(CompilerConfiguration compilerConfiguration) throws GraalError {
|
|
152 |
try {
|
|
153 |
EconomicSet<Object> res = EconomicSet.create();
|
|
154 |
Object compilerConfigurationModule = JDK9Method.getModule.invoke(compilerConfiguration.getClass());
|
|
155 |
res.add(compilerConfigurationModule);
|
|
156 |
Class<?> moduleClass = compilerConfigurationModule.getClass();
|
|
157 |
Object layer = new JDK9Method(moduleClass, "getLayer").invoke(compilerConfigurationModule);
|
|
158 |
Class<? extends Object> layerClass = layer.getClass();
|
|
159 |
JDK9Method getName = new JDK9Method(moduleClass, "getName");
|
|
160 |
Set<Object> modules = new JDK9Method(layerClass, "modules").invoke(layer);
|
|
161 |
Object descriptor = new JDK9Method(moduleClass, "getDescriptor").invoke(compilerConfigurationModule);
|
|
162 |
Class<?> moduleDescriptorClass = descriptor.getClass();
|
|
163 |
Set<Object> requires = new JDK9Method(moduleDescriptorClass, "requires").invoke(descriptor);
|
|
164 |
JDK9Method requireNameGetter = null;
|
|
165 |
for (Object require : requires) {
|
|
166 |
if (requireNameGetter == null) {
|
|
167 |
requireNameGetter = new JDK9Method(require.getClass(), "name");
|
|
168 |
}
|
|
169 |
String name = requireNameGetter.invoke(require);
|
|
170 |
for (Object module : modules) {
|
|
171 |
String moduleName = getName.invoke(module);
|
|
172 |
if (moduleName.equals(name)) {
|
|
173 |
res.add(module);
|
|
174 |
}
|
|
175 |
}
|
|
176 |
}
|
|
177 |
return res;
|
|
178 |
} catch (Exception e) {
|
|
179 |
throw new GraalError(e);
|
|
180 |
}
|
|
181 |
}
|
43972
|
182 |
}
|