package org.graalvm.compiler.phases.verify;
import static org.graalvm.compiler.debug.DebugContext.BASIC_LEVEL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.graalvm.compiler.core.common.type.ObjectStamp;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.Graph;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeInputList;
import org.graalvm.compiler.nodes.CallTargetNode;
import org.graalvm.compiler.nodes.Invoke;
import org.graalvm.compiler.nodes.NodeView;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
import org.graalvm.compiler.nodes.java.NewArrayNode;
import org.graalvm.compiler.nodes.java.StoreIndexedNode;
import org.graalvm.compiler.phases.VerifyPhase;
import org.graalvm.compiler.phases.tiers.PhaseContext;
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.PrimitiveConstant;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
public class VerifyDebugUsage extends VerifyPhase<PhaseContext> {
@Override
public boolean checkContract() {
return false;
}
MetaAccessProvider metaAccess;
@Override
protected boolean verify(StructuredGraph graph, PhaseContext context) {
metaAccess = context.getMetaAccess();
ResolvedJavaType debugType = metaAccess.lookupJavaType(DebugContext.class);
ResolvedJavaType nodeType = metaAccess.lookupJavaType(Node.class);
ResolvedJavaType stringType = metaAccess.lookupJavaType(String.class);
ResolvedJavaType graalErrorType = metaAccess.lookupJavaType(GraalError.class);
for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
ResolvedJavaMethod callee = t.targetMethod();
String calleeName = callee.getName();
if (callee.getDeclaringClass().equals(debugType)) {
boolean isDump = calleeName.equals("dump");
if (calleeName.equals("log") || calleeName.equals("logAndIndent") || calleeName.equals("verify") || isDump) {
verifyParameters(t, graph, t.arguments(), stringType, isDump ? 2 : 1);
}
}
if (callee.getDeclaringClass().isAssignableFrom(nodeType)) {
if (calleeName.equals("assertTrue") || calleeName.equals("assertFalse")) {
verifyParameters(t, graph, t.arguments(), stringType, 1);
}
}
if (callee.getDeclaringClass().isAssignableFrom(graalErrorType) && !graph.method().getDeclaringClass().isAssignableFrom(graalErrorType)) {
if (calleeName.equals("guarantee")) {
verifyParameters(t, graph, t.arguments(), stringType, 0);
}
if (calleeName.equals("<init>") && callee.getSignature().getParameterCount(false) == 2) {
verifyParameters(t, graph, t.arguments(), stringType, 1);
}
}
}
return true;
}
private void verifyParameters(MethodCallTargetNode callTarget, StructuredGraph callerGraph, NodeInputList<? extends ValueNode> args, ResolvedJavaType stringType, int startArgIdx) {
if (callTarget.targetMethod().isVarArgs() && args.get(args.count() - 1) instanceof NewArrayNode) {
List<ValueNode> unpacked = new ArrayList<>(args.snapshot());
NewArrayNode varArgParameter = (NewArrayNode) unpacked.remove(unpacked.size() - 1);
int firstVarArg = unpacked.size();
for (Node usage : varArgParameter.usages()) {
if (usage instanceof StoreIndexedNode) {
StoreIndexedNode si = (StoreIndexedNode) usage;
unpacked.add(si.value());
}
}
verifyParameters(callerGraph, callTarget, unpacked, stringType, startArgIdx, firstVarArg);
} else {
verifyParameters(callerGraph, callTarget, args, stringType, startArgIdx, -1);
}
}
private static final Set<Integer> DebugLevels = new HashSet<>(
Arrays.asList(DebugContext.ENABLED_LEVEL, BASIC_LEVEL, DebugContext.INFO_LEVEL, DebugContext.VERBOSE_LEVEL, DebugContext.DETAILED_LEVEL, DebugContext.VERY_DETAILED_LEVEL));
private static final Set<String> BasicLevelStructuredGraphDumpWhitelist = new HashSet<>(Arrays.asList(
"org.graalvm.compiler.phases.BasePhase.dumpAfter",
"org.graalvm.compiler.phases.BasePhase.dumpBefore",
"org.graalvm.compiler.core.GraalCompiler.emitFrontEnd",
"org.graalvm.compiler.truffle.compiler.PartialEvaluator.fastPartialEvaluation",
"org.graalvm.compiler.truffle.compiler.PartialEvaluator$PerformanceInformationHandler.reportPerformanceWarnings",
"org.graalvm.compiler.truffle.compiler.TruffleCompilerImpl.compilePEGraph",
"org.graalvm.compiler.core.test.VerifyDebugUsageTest$ValidDumpUsagePhase.run",
"org.graalvm.compiler.core.test.VerifyDebugUsageTest$InvalidConcatDumpUsagePhase.run",
"org.graalvm.compiler.core.test.VerifyDebugUsageTest$InvalidDumpUsagePhase.run"));
private static final Set<String> InfoLevelStructuredGraphDumpWhitelist = new HashSet<>(Arrays.asList(
"org.graalvm.compiler.core.GraalCompiler.emitFrontEnd",
"org.graalvm.compiler.phases.BasePhase.dumpAfter",
"org.graalvm.compiler.replacements.ReplacementsImpl$GraphMaker.makeGraph",
"org.graalvm.compiler.replacements.SnippetTemplate.instantiate"));
private void verifyParameters(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, List<? extends ValueNode> args, ResolvedJavaType stringType, int startArgIdx,
int varArgsIndex) {
ResolvedJavaMethod verifiedCallee = debugCallTarget.targetMethod();
Integer dumpLevel = null;
int argIdx = startArgIdx;
int varArgsElementIndex = 0;
boolean reportVarArgs = false;
for (int i = 0; i < args.size(); i++) {
ValueNode arg = args.get(i);
if (arg instanceof Invoke) {
reportVarArgs = varArgsIndex >= 0 && argIdx >= varArgsIndex;
Invoke invoke = (Invoke) arg;
CallTargetNode callTarget = invoke.callTarget();
if (callTarget instanceof MethodCallTargetNode) {
ResolvedJavaMethod m = ((MethodCallTargetNode) callTarget).targetMethod();
if (m.getName().equals("toString")) {
int bci = invoke.bci();
int nonVarArgIdx = reportVarArgs ? argIdx - varArgsElementIndex : argIdx;
verifyStringConcat(callerGraph, verifiedCallee, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1, m);
verifyToStringCall(callerGraph, verifiedCallee, stringType, m, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1);
} else if (m.getName().equals("format")) {
int bci = invoke.bci();
int nonVarArgIdx = reportVarArgs ? argIdx - varArgsElementIndex : argIdx;
verifyFormatCall(callerGraph, verifiedCallee, stringType, m, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1);
}
}
}
if (i == 1) {
if (verifiedCallee.getName().equals("dump")) {
dumpLevel = verifyDumpLevelParameter(callerGraph, debugCallTarget, verifiedCallee, arg);
}
} else if (i == 2) {
if (dumpLevel != null) {
verifyDumpObjectParameter(callerGraph, debugCallTarget, arg, verifiedCallee, dumpLevel);
}
}
if (varArgsIndex >= 0 && i >= varArgsIndex) {
varArgsElementIndex++;
}
argIdx++;
}
}
protected Integer verifyDumpLevelParameter(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ResolvedJavaMethod verifiedCallee, ValueNode arg)
throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
Constant c = arg.asConstant();
if (c != null) {
Integer dumpLevel = ((PrimitiveConstant) c).asInt();
if (!DebugLevels.contains(dumpLevel)) {
StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
throw new VerificationError(
"In %s: parameter 0 of call to %s does not match a Debug.*_LEVEL constant: %s.%n", e, verifiedCallee.format("%H.%n(%p)"), dumpLevel);
}
return dumpLevel;
}
StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
throw new VerificationError(
"In %s: parameter 0 of call to %s must be a constant, not %s.%n", e, verifiedCallee.format("%H.%n(%p)"), arg);
}
protected void verifyDumpObjectParameter(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ValueNode arg, ResolvedJavaMethod verifiedCallee, Integer dumpLevel)
throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
ResolvedJavaType argType = ((ObjectStamp) arg.stamp(NodeView.DEFAULT)).type();
if (metaAccess.lookupJavaType(Graph.class).isAssignableFrom(argType)) {
verifyStructuredGraphDumping(callerGraph, debugCallTarget, verifiedCallee, dumpLevel);
}
}
protected void verifyStructuredGraphDumping(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ResolvedJavaMethod verifiedCallee, Integer dumpLevel)
throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
if (dumpLevel == DebugContext.BASIC_LEVEL) {
StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
String qualifiedMethod = e.getClassName() + "." + e.getMethodName();
if (!BasicLevelStructuredGraphDumpWhitelist.contains(qualifiedMethod)) {
throw new VerificationError(
"In %s: call to %s with level == DebugContext.BASIC_LEVEL not in %s.BasicLevelDumpWhitelist.%n", e, verifiedCallee.format("%H.%n(%p)"),
getClass().getName());
}
} else if (dumpLevel == DebugContext.INFO_LEVEL) {
StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
String qualifiedMethod = e.getClassName() + "." + e.getMethodName();
if (!InfoLevelStructuredGraphDumpWhitelist.contains(qualifiedMethod)) {
throw new VerificationError(
"In %s: call to %s with level == Debug.INFO_LEVEL not in %s.InfoLevelDumpWhitelist.%n", e, verifiedCallee.format("%H.%n(%p)"),
getClass().getName());
}
}
}
private static void verifyStringConcat(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, int bci, int argIdx, int varArgsElementIndex, ResolvedJavaMethod callee) {
if (callee.getDeclaringClass().getName().equals("Ljava/lang/StringBuilder;") || callee.getDeclaringClass().getName().equals("Ljava/lang/StringBuffer;")) {
StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
if (varArgsElementIndex >= 0) {
throw new VerificationError(
"In %s: element %d of parameter %d of call to %s appears to be a String concatenation expression.%n", e, varArgsElementIndex, argIdx,
verifiedCallee.format("%H.%n(%p)"));
} else {
throw new VerificationError(
"In %s: parameter %d of call to %s appears to be a String concatenation expression.", e, argIdx, verifiedCallee.format("%H.%n(%p)"));
}
}
}
private static void verifyToStringCall(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, ResolvedJavaType stringType, ResolvedJavaMethod callee, int bci, int argIdx,
int varArgsElementIndex) {
if (callee.getSignature().getParameterCount(false) == 0 && callee.getSignature().getReturnType(callee.getDeclaringClass()).equals(stringType)) {
StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
if (varArgsElementIndex >= 0) {
throw new VerificationError(
"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.",
e, varArgsElementIndex, argIdx, verifiedCallee.format("%H.%n(%p)"));
} else {
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,
verifiedCallee.format("%H.%n(%p)"));
}
}
}
private static void verifyFormatCall(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, ResolvedJavaType stringType, ResolvedJavaMethod callee, int bci, int argIdx,
int varArgsElementIndex) {
if (callee.getDeclaringClass().equals(stringType) && callee.getSignature().getReturnType(callee.getDeclaringClass()).equals(stringType)) {
StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
if (varArgsElementIndex >= 0) {
throw new VerificationError(
"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.",
e, varArgsElementIndex, argIdx, verifiedCallee.format("%H.%n(%p)"), verifiedCallee.format("%h.%n"));
} else {
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,
argIdx,
verifiedCallee.format("%H.%n(%p)"), verifiedCallee.format("%h.%n"));
}
}
}
}