52910
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
|
52910
|
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 |
package org.graalvm.compiler.core.test;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.debug.DebugContext.BASIC_LEVEL;
|
|
28 |
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.Arrays;
|
|
31 |
import java.util.HashSet;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.Set;
|
|
34 |
|
|
35 |
import org.graalvm.compiler.core.common.type.ObjectStamp;
|
|
36 |
import org.graalvm.compiler.debug.DebugContext;
|
|
37 |
import org.graalvm.compiler.debug.GraalError;
|
|
38 |
import org.graalvm.compiler.graph.Graph;
|
|
39 |
import org.graalvm.compiler.graph.Node;
|
|
40 |
import org.graalvm.compiler.graph.NodeInputList;
|
|
41 |
import org.graalvm.compiler.nodes.CallTargetNode;
|
|
42 |
import org.graalvm.compiler.nodes.Invoke;
|
|
43 |
import org.graalvm.compiler.nodes.NodeView;
|
|
44 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
45 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
46 |
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
|
|
47 |
import org.graalvm.compiler.nodes.java.NewArrayNode;
|
|
48 |
import org.graalvm.compiler.nodes.java.StoreIndexedNode;
|
55509
|
49 |
import org.graalvm.compiler.nodes.spi.CoreProviders;
|
52910
|
50 |
import org.graalvm.compiler.phases.VerifyPhase;
|
|
51 |
|
|
52 |
import jdk.vm.ci.meta.Constant;
|
|
53 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
54 |
import jdk.vm.ci.meta.PrimitiveConstant;
|
|
55 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
56 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Verifies that call sites calling one of the methods in {@link DebugContext} use them correctly.
|
|
60 |
* Correct usage of the methods in {@link DebugContext} requires call sites to not eagerly evaluate
|
|
61 |
* their arguments. Additionally this phase verifies that no argument is the result of a call to
|
|
62 |
* {@link StringBuilder#toString()} or {@link StringBuffer#toString()}. Ideally the parameters at
|
|
63 |
* call sites of {@link DebugContext} are eliminated, and do not produce additional allocations, if
|
|
64 |
* {@link DebugContext#isDumpEnabled(int)} (or {@link DebugContext#isLogEnabled(int)}, ...) is
|
|
65 |
* {@code false}.
|
|
66 |
*
|
|
67 |
* Methods in {@link DebugContext} checked by this phase are various different versions of
|
|
68 |
* {@link DebugContext#log(String)} , {@link DebugContext#dump(int, Object, String)},
|
|
69 |
* {@link DebugContext#logAndIndent(String)} and {@link DebugContext#verify(Object, String)}.
|
|
70 |
*/
|
55509
|
71 |
public class VerifyDebugUsage extends VerifyPhase<CoreProviders> {
|
52910
|
72 |
|
|
73 |
@Override
|
|
74 |
public boolean checkContract() {
|
|
75 |
return false;
|
|
76 |
}
|
|
77 |
|
|
78 |
MetaAccessProvider metaAccess;
|
|
79 |
|
|
80 |
@Override
|
55509
|
81 |
protected void verify(StructuredGraph graph, CoreProviders context) {
|
52910
|
82 |
metaAccess = context.getMetaAccess();
|
|
83 |
ResolvedJavaType debugType = metaAccess.lookupJavaType(DebugContext.class);
|
|
84 |
ResolvedJavaType nodeType = metaAccess.lookupJavaType(Node.class);
|
|
85 |
ResolvedJavaType stringType = metaAccess.lookupJavaType(String.class);
|
|
86 |
ResolvedJavaType graalErrorType = metaAccess.lookupJavaType(GraalError.class);
|
|
87 |
|
|
88 |
for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
|
|
89 |
ResolvedJavaMethod callee = t.targetMethod();
|
|
90 |
String calleeName = callee.getName();
|
|
91 |
if (callee.getDeclaringClass().equals(debugType)) {
|
|
92 |
boolean isDump = calleeName.equals("dump");
|
|
93 |
if (calleeName.equals("log") || calleeName.equals("logAndIndent") || calleeName.equals("verify") || isDump) {
|
|
94 |
verifyParameters(t, graph, t.arguments(), stringType, isDump ? 2 : 1);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
if (callee.getDeclaringClass().isAssignableFrom(nodeType)) {
|
|
98 |
if (calleeName.equals("assertTrue") || calleeName.equals("assertFalse")) {
|
|
99 |
verifyParameters(t, graph, t.arguments(), stringType, 1);
|
|
100 |
}
|
|
101 |
}
|
|
102 |
if (callee.getDeclaringClass().isAssignableFrom(graalErrorType) && !graph.method().getDeclaringClass().isAssignableFrom(graalErrorType)) {
|
|
103 |
if (calleeName.equals("guarantee")) {
|
|
104 |
verifyParameters(t, graph, t.arguments(), stringType, 0);
|
|
105 |
}
|
|
106 |
if (calleeName.equals("<init>") && callee.getSignature().getParameterCount(false) == 2) {
|
|
107 |
verifyParameters(t, graph, t.arguments(), stringType, 1);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
private void verifyParameters(MethodCallTargetNode callTarget, StructuredGraph callerGraph, NodeInputList<? extends ValueNode> args, ResolvedJavaType stringType, int startArgIdx) {
|
|
114 |
if (callTarget.targetMethod().isVarArgs() && args.get(args.count() - 1) instanceof NewArrayNode) {
|
|
115 |
// unpack the arguments to the var args
|
|
116 |
List<ValueNode> unpacked = new ArrayList<>(args.snapshot());
|
|
117 |
NewArrayNode varArgParameter = (NewArrayNode) unpacked.remove(unpacked.size() - 1);
|
|
118 |
int firstVarArg = unpacked.size();
|
|
119 |
for (Node usage : varArgParameter.usages()) {
|
|
120 |
if (usage instanceof StoreIndexedNode) {
|
|
121 |
StoreIndexedNode si = (StoreIndexedNode) usage;
|
|
122 |
unpacked.add(si.value());
|
|
123 |
}
|
|
124 |
}
|
|
125 |
verifyParameters(callerGraph, callTarget, unpacked, stringType, startArgIdx, firstVarArg);
|
|
126 |
} else {
|
|
127 |
verifyParameters(callerGraph, callTarget, args, stringType, startArgIdx, -1);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
private static final Set<Integer> DebugLevels = new HashSet<>(
|
|
132 |
Arrays.asList(DebugContext.ENABLED_LEVEL, BASIC_LEVEL, DebugContext.INFO_LEVEL, DebugContext.VERBOSE_LEVEL, DebugContext.DETAILED_LEVEL, DebugContext.VERY_DETAILED_LEVEL));
|
|
133 |
|
|
134 |
/**
|
|
135 |
* The set of methods allowed to call a {@code Debug.dump(...)} method with the {@code level}
|
|
136 |
* parameter bound to {@link DebugContext#BASIC_LEVEL} and the {@code object} parameter bound to
|
|
137 |
* a {@link StructuredGraph} value.
|
|
138 |
*
|
|
139 |
* This whitelist exists to ensure any increase in graph dumps is in line with the policy
|
|
140 |
* outlined by {@link DebugContext#BASIC_LEVEL}. If you add a *justified* graph dump at this
|
|
141 |
* level, then update the whitelist.
|
|
142 |
*/
|
|
143 |
private static final Set<String> BasicLevelStructuredGraphDumpWhitelist = new HashSet<>(Arrays.asList(
|
|
144 |
"org.graalvm.compiler.phases.BasePhase.dumpAfter",
|
|
145 |
"org.graalvm.compiler.phases.BasePhase.dumpBefore",
|
|
146 |
"org.graalvm.compiler.core.GraalCompiler.emitFrontEnd",
|
|
147 |
"org.graalvm.compiler.truffle.compiler.PartialEvaluator.fastPartialEvaluation",
|
|
148 |
"org.graalvm.compiler.truffle.compiler.PartialEvaluator$PerformanceInformationHandler.reportPerformanceWarnings",
|
|
149 |
"org.graalvm.compiler.truffle.compiler.TruffleCompilerImpl.compilePEGraph",
|
|
150 |
"org.graalvm.compiler.core.test.VerifyDebugUsageTest$ValidDumpUsagePhase.run",
|
|
151 |
"org.graalvm.compiler.core.test.VerifyDebugUsageTest$InvalidConcatDumpUsagePhase.run",
|
54084
|
152 |
"org.graalvm.compiler.core.test.VerifyDebugUsageTest$InvalidDumpUsagePhase.run",
|
58299
|
153 |
"org.graalvm.compiler.hotspot.SymbolicSnippetEncoder.verifySnippetEncodeDecode",
|
|
154 |
"org.graalvm.compiler.truffle.compiler.phases.inlining.CallTree.dumpBasic"));
|
52910
|
155 |
|
|
156 |
/**
|
|
157 |
* The set of methods allowed to call a {@code Debug.dump(...)} method with the {@code level}
|
|
158 |
* parameter bound to {@link DebugContext#INFO_LEVEL} and the {@code object} parameter bound to
|
|
159 |
* a {@link StructuredGraph} value.
|
|
160 |
*
|
|
161 |
* This whitelist exists to ensure any increase in graph dumps is in line with the policy
|
|
162 |
* outlined by {@link DebugContext#INFO_LEVEL}. If you add a *justified* graph dump at this
|
|
163 |
* level, then update the whitelist.
|
|
164 |
*/
|
|
165 |
private static final Set<String> InfoLevelStructuredGraphDumpWhitelist = new HashSet<>(Arrays.asList(
|
|
166 |
"org.graalvm.compiler.core.GraalCompiler.emitFrontEnd",
|
|
167 |
"org.graalvm.compiler.phases.BasePhase.dumpAfter",
|
|
168 |
"org.graalvm.compiler.replacements.ReplacementsImpl$GraphMaker.makeGraph",
|
54084
|
169 |
"org.graalvm.compiler.replacements.SnippetTemplate.instantiate",
|
54328
|
170 |
"org.graalvm.compiler.replacements.SnippetTemplate.<init>",
|
58299
|
171 |
"org.graalvm.compiler.hotspot.SymbolicSnippetEncoder.verifySnippetEncodeDecode",
|
|
172 |
"org.graalvm.compiler.truffle.compiler.phases.inlining.CallTree.dumpInfo"));
|
52910
|
173 |
|
|
174 |
private void verifyParameters(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, List<? extends ValueNode> args, ResolvedJavaType stringType, int startArgIdx,
|
|
175 |
int varArgsIndex) {
|
|
176 |
ResolvedJavaMethod verifiedCallee = debugCallTarget.targetMethod();
|
|
177 |
Integer dumpLevel = null;
|
|
178 |
int argIdx = startArgIdx;
|
|
179 |
int varArgsElementIndex = 0;
|
|
180 |
boolean reportVarArgs = false;
|
|
181 |
for (int i = 0; i < args.size(); i++) {
|
|
182 |
ValueNode arg = args.get(i);
|
|
183 |
if (arg instanceof Invoke) {
|
|
184 |
reportVarArgs = varArgsIndex >= 0 && argIdx >= varArgsIndex;
|
|
185 |
Invoke invoke = (Invoke) arg;
|
|
186 |
CallTargetNode callTarget = invoke.callTarget();
|
|
187 |
if (callTarget instanceof MethodCallTargetNode) {
|
|
188 |
ResolvedJavaMethod m = ((MethodCallTargetNode) callTarget).targetMethod();
|
|
189 |
if (m.getName().equals("toString")) {
|
|
190 |
int bci = invoke.bci();
|
|
191 |
int nonVarArgIdx = reportVarArgs ? argIdx - varArgsElementIndex : argIdx;
|
|
192 |
verifyStringConcat(callerGraph, verifiedCallee, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1, m);
|
|
193 |
verifyToStringCall(callerGraph, verifiedCallee, stringType, m, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1);
|
|
194 |
} else if (m.getName().equals("format")) {
|
|
195 |
int bci = invoke.bci();
|
|
196 |
int nonVarArgIdx = reportVarArgs ? argIdx - varArgsElementIndex : argIdx;
|
|
197 |
verifyFormatCall(callerGraph, verifiedCallee, stringType, m, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1);
|
|
198 |
|
|
199 |
}
|
|
200 |
}
|
|
201 |
}
|
|
202 |
if (i == 1) {
|
|
203 |
if (verifiedCallee.getName().equals("dump")) {
|
|
204 |
dumpLevel = verifyDumpLevelParameter(callerGraph, debugCallTarget, verifiedCallee, arg);
|
|
205 |
}
|
|
206 |
} else if (i == 2) {
|
|
207 |
if (dumpLevel != null) {
|
|
208 |
verifyDumpObjectParameter(callerGraph, debugCallTarget, arg, verifiedCallee, dumpLevel);
|
|
209 |
}
|
|
210 |
}
|
|
211 |
if (varArgsIndex >= 0 && i >= varArgsIndex) {
|
|
212 |
varArgsElementIndex++;
|
|
213 |
}
|
|
214 |
argIdx++;
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
/**
|
|
219 |
* The {@code level} arg for the {@code Debug.dump(...)} methods must be a reference to one of
|
|
220 |
* the {@code Debug.*_LEVEL} constants.
|
|
221 |
*/
|
|
222 |
protected Integer verifyDumpLevelParameter(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ResolvedJavaMethod verifiedCallee, ValueNode arg)
|
|
223 |
throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
|
|
224 |
// The 'level' arg for the Debug.dump(...) methods must be a reference to one of
|
|
225 |
// the Debug.*_LEVEL constants.
|
|
226 |
|
|
227 |
Constant c = arg.asConstant();
|
|
228 |
if (c != null) {
|
|
229 |
Integer dumpLevel = ((PrimitiveConstant) c).asInt();
|
|
230 |
if (!DebugLevels.contains(dumpLevel)) {
|
|
231 |
StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
|
|
232 |
throw new VerificationError(
|
|
233 |
"In %s: parameter 0 of call to %s does not match a Debug.*_LEVEL constant: %s.%n", e, verifiedCallee.format("%H.%n(%p)"), dumpLevel);
|
|
234 |
}
|
|
235 |
return dumpLevel;
|
|
236 |
}
|
|
237 |
StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
|
|
238 |
throw new VerificationError(
|
|
239 |
"In %s: parameter 0 of call to %s must be a constant, not %s.%n", e, verifiedCallee.format("%H.%n(%p)"), arg);
|
|
240 |
}
|
|
241 |
|
|
242 |
protected void verifyDumpObjectParameter(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ValueNode arg, ResolvedJavaMethod verifiedCallee, Integer dumpLevel)
|
|
243 |
throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
|
|
244 |
ResolvedJavaType argType = ((ObjectStamp) arg.stamp(NodeView.DEFAULT)).type();
|
|
245 |
if (metaAccess.lookupJavaType(Graph.class).isAssignableFrom(argType)) {
|
|
246 |
verifyStructuredGraphDumping(callerGraph, debugCallTarget, verifiedCallee, dumpLevel);
|
|
247 |
}
|
|
248 |
}
|
|
249 |
|
|
250 |
/**
|
|
251 |
* Verifies that dumping a {@link StructuredGraph} at level {@link DebugContext#BASIC_LEVEL} or
|
|
252 |
* {@link DebugContext#INFO_LEVEL} only occurs in white-listed methods.
|
|
253 |
*/
|
|
254 |
protected void verifyStructuredGraphDumping(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ResolvedJavaMethod verifiedCallee, Integer dumpLevel)
|
|
255 |
throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
|
|
256 |
if (dumpLevel == DebugContext.BASIC_LEVEL) {
|
|
257 |
StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
|
|
258 |
String qualifiedMethod = e.getClassName() + "." + e.getMethodName();
|
|
259 |
if (!BasicLevelStructuredGraphDumpWhitelist.contains(qualifiedMethod)) {
|
|
260 |
throw new VerificationError(
|
|
261 |
"In %s: call to %s with level == DebugContext.BASIC_LEVEL not in %s.BasicLevelDumpWhitelist.%n", e, verifiedCallee.format("%H.%n(%p)"),
|
|
262 |
getClass().getName());
|
|
263 |
}
|
|
264 |
} else if (dumpLevel == DebugContext.INFO_LEVEL) {
|
|
265 |
StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
|
|
266 |
String qualifiedMethod = e.getClassName() + "." + e.getMethodName();
|
|
267 |
if (!InfoLevelStructuredGraphDumpWhitelist.contains(qualifiedMethod)) {
|
|
268 |
throw new VerificationError(
|
|
269 |
"In %s: call to %s with level == Debug.INFO_LEVEL not in %s.InfoLevelDumpWhitelist.%n", e, verifiedCallee.format("%H.%n(%p)"),
|
|
270 |
getClass().getName());
|
|
271 |
}
|
|
272 |
}
|
|
273 |
}
|
|
274 |
|
|
275 |
/**
|
|
276 |
* Checks that a given call is not to {@link StringBuffer#toString()} or
|
|
277 |
* {@link StringBuilder#toString()}.
|
|
278 |
*/
|
|
279 |
private static void verifyStringConcat(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, int bci, int argIdx, int varArgsElementIndex, ResolvedJavaMethod callee) {
|
|
280 |
if (callee.getDeclaringClass().getName().equals("Ljava/lang/StringBuilder;") || callee.getDeclaringClass().getName().equals("Ljava/lang/StringBuffer;")) {
|
|
281 |
StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
|
|
282 |
if (varArgsElementIndex >= 0) {
|
|
283 |
throw new VerificationError(
|
|
284 |
"In %s: element %d of parameter %d of call to %s appears to be a String concatenation expression.%n", e, varArgsElementIndex, argIdx,
|
|
285 |
verifiedCallee.format("%H.%n(%p)"));
|
|
286 |
} else {
|
|
287 |
throw new VerificationError(
|
|
288 |
"In %s: parameter %d of call to %s appears to be a String concatenation expression.", e, argIdx, verifiedCallee.format("%H.%n(%p)"));
|
|
289 |
}
|
|
290 |
}
|
|
291 |
}
|
|
292 |
|
|
293 |
/**
|
|
294 |
* Checks that a given call is not to {@link Object#toString()}.
|
|
295 |
*/
|
|
296 |
private static void verifyToStringCall(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, ResolvedJavaType stringType, ResolvedJavaMethod callee, int bci, int argIdx,
|
|
297 |
int varArgsElementIndex) {
|
|
298 |
if (callee.getSignature().getParameterCount(false) == 0 && callee.getSignature().getReturnType(callee.getDeclaringClass()).equals(stringType)) {
|
|
299 |
StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
|
|
300 |
if (varArgsElementIndex >= 0) {
|
|
301 |
throw new VerificationError(
|
|
302 |
"In %s: element %d of parameter %d of call to %s is a call to toString() which is redundant (the callee will do it) and forces unnecessary eager evaluation.",
|
|
303 |
e, varArgsElementIndex, argIdx, verifiedCallee.format("%H.%n(%p)"));
|
|
304 |
} else {
|
|
305 |
throw new VerificationError("In %s: parameter %d of call to %s is a call to toString() which is redundant (the callee will do it) and forces unnecessary eager evaluation.", e, argIdx,
|
|
306 |
verifiedCallee.format("%H.%n(%p)"));
|
|
307 |
}
|
|
308 |
}
|
|
309 |
}
|
|
310 |
|
|
311 |
/**
|
|
312 |
* Checks that a given call is not to {@link String#format(String, Object...)} or
|
|
313 |
* {@link String#format(java.util.Locale, String, Object...)}.
|
|
314 |
*/
|
|
315 |
private static void verifyFormatCall(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, ResolvedJavaType stringType, ResolvedJavaMethod callee, int bci, int argIdx,
|
|
316 |
int varArgsElementIndex) {
|
|
317 |
if (callee.getDeclaringClass().equals(stringType) && callee.getSignature().getReturnType(callee.getDeclaringClass()).equals(stringType)) {
|
|
318 |
StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
|
|
319 |
if (varArgsElementIndex >= 0) {
|
|
320 |
throw new VerificationError(
|
|
321 |
"In %s: element %d of parameter %d of call to %s is a call to String.format() which is redundant (%s does formatting) and forces unnecessary eager evaluation.",
|
|
322 |
e, varArgsElementIndex, argIdx, verifiedCallee.format("%H.%n(%p)"), verifiedCallee.format("%h.%n"));
|
|
323 |
} else {
|
|
324 |
throw new VerificationError("In %s: parameter %d of call to %s is a call to String.format() which is redundant (%s does formatting) and forces unnecessary eager evaluation.", e,
|
|
325 |
argIdx,
|
|
326 |
verifiedCallee.format("%H.%n(%p)"), verifiedCallee.format("%h.%n"));
|
|
327 |
}
|
|
328 |
}
|
|
329 |
}
|
|
330 |
}
|