src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/VerifyDebugUsage.java
author jwilhelm
Thu, 28 Mar 2019 19:39:14 +0100
changeset 54328 37648a9c4a6a
parent 54084 84f10bbf993f
child 55509 d58442b8abc1
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8221341: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     1
/*
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     4
 *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     7
 * published by the Free Software Foundation.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     8
 *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    13
 * accompanied this code).
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    14
 *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    18
 *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    21
 * questions.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    22
 */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    23
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    24
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    25
package org.graalvm.compiler.core.test;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    26
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    27
import static org.graalvm.compiler.debug.DebugContext.BASIC_LEVEL;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    28
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    29
import java.util.ArrayList;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    30
import java.util.Arrays;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    31
import java.util.HashSet;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    32
import java.util.List;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    33
import java.util.Set;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    34
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    35
import org.graalvm.compiler.core.common.type.ObjectStamp;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    36
import org.graalvm.compiler.debug.DebugContext;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    37
import org.graalvm.compiler.debug.GraalError;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    38
import org.graalvm.compiler.graph.Graph;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    39
import org.graalvm.compiler.graph.Node;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    40
import org.graalvm.compiler.graph.NodeInputList;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    41
import org.graalvm.compiler.nodes.CallTargetNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    42
import org.graalvm.compiler.nodes.Invoke;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    43
import org.graalvm.compiler.nodes.NodeView;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    44
import org.graalvm.compiler.nodes.StructuredGraph;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    45
import org.graalvm.compiler.nodes.ValueNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    46
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    47
import org.graalvm.compiler.nodes.java.NewArrayNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    48
import org.graalvm.compiler.nodes.java.StoreIndexedNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    49
import org.graalvm.compiler.phases.VerifyPhase;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    50
import org.graalvm.compiler.phases.tiers.PhaseContext;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    51
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    52
import jdk.vm.ci.meta.Constant;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    53
import jdk.vm.ci.meta.MetaAccessProvider;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    54
import jdk.vm.ci.meta.PrimitiveConstant;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    55
import jdk.vm.ci.meta.ResolvedJavaMethod;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    56
import jdk.vm.ci.meta.ResolvedJavaType;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    57
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    58
/**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    59
 * Verifies that call sites calling one of the methods in {@link DebugContext} use them correctly.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    60
 * Correct usage of the methods in {@link DebugContext} requires call sites to not eagerly evaluate
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    61
 * their arguments. Additionally this phase verifies that no argument is the result of a call to
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    62
 * {@link StringBuilder#toString()} or {@link StringBuffer#toString()}. Ideally the parameters at
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    63
 * call sites of {@link DebugContext} are eliminated, and do not produce additional allocations, if
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    64
 * {@link DebugContext#isDumpEnabled(int)} (or {@link DebugContext#isLogEnabled(int)}, ...) is
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    65
 * {@code false}.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    66
 *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    67
 * Methods in {@link DebugContext} checked by this phase are various different versions of
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    68
 * {@link DebugContext#log(String)} , {@link DebugContext#dump(int, Object, String)},
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    69
 * {@link DebugContext#logAndIndent(String)} and {@link DebugContext#verify(Object, String)}.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    70
 */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    71
public class VerifyDebugUsage extends VerifyPhase<PhaseContext> {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    72
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    73
    @Override
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    74
    public boolean checkContract() {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    75
        return false;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    76
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    77
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    78
    MetaAccessProvider metaAccess;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    79
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    80
    @Override
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 54084
diff changeset
    81
    protected void verify(StructuredGraph graph, PhaseContext context) {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    82
        metaAccess = context.getMetaAccess();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    83
        ResolvedJavaType debugType = metaAccess.lookupJavaType(DebugContext.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    84
        ResolvedJavaType nodeType = metaAccess.lookupJavaType(Node.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    85
        ResolvedJavaType stringType = metaAccess.lookupJavaType(String.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    86
        ResolvedJavaType graalErrorType = metaAccess.lookupJavaType(GraalError.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    87
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    88
        for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    89
            ResolvedJavaMethod callee = t.targetMethod();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    90
            String calleeName = callee.getName();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    91
            if (callee.getDeclaringClass().equals(debugType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    92
                boolean isDump = calleeName.equals("dump");
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    93
                if (calleeName.equals("log") || calleeName.equals("logAndIndent") || calleeName.equals("verify") || isDump) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    94
                    verifyParameters(t, graph, t.arguments(), stringType, isDump ? 2 : 1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    95
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    96
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    97
            if (callee.getDeclaringClass().isAssignableFrom(nodeType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    98
                if (calleeName.equals("assertTrue") || calleeName.equals("assertFalse")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    99
                    verifyParameters(t, graph, t.arguments(), stringType, 1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   100
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   101
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   102
            if (callee.getDeclaringClass().isAssignableFrom(graalErrorType) && !graph.method().getDeclaringClass().isAssignableFrom(graalErrorType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   103
                if (calleeName.equals("guarantee")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   104
                    verifyParameters(t, graph, t.arguments(), stringType, 0);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   105
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   106
                if (calleeName.equals("<init>") && callee.getSignature().getParameterCount(false) == 2) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   107
                    verifyParameters(t, graph, t.arguments(), stringType, 1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   108
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   109
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   110
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   111
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   112
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   113
    private void verifyParameters(MethodCallTargetNode callTarget, StructuredGraph callerGraph, NodeInputList<? extends ValueNode> args, ResolvedJavaType stringType, int startArgIdx) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   114
        if (callTarget.targetMethod().isVarArgs() && args.get(args.count() - 1) instanceof NewArrayNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   115
            // unpack the arguments to the var args
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   116
            List<ValueNode> unpacked = new ArrayList<>(args.snapshot());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   117
            NewArrayNode varArgParameter = (NewArrayNode) unpacked.remove(unpacked.size() - 1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   118
            int firstVarArg = unpacked.size();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   119
            for (Node usage : varArgParameter.usages()) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   120
                if (usage instanceof StoreIndexedNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   121
                    StoreIndexedNode si = (StoreIndexedNode) usage;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   122
                    unpacked.add(si.value());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   123
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   124
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   125
            verifyParameters(callerGraph, callTarget, unpacked, stringType, startArgIdx, firstVarArg);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   126
        } else {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   127
            verifyParameters(callerGraph, callTarget, args, stringType, startArgIdx, -1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   128
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   129
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   130
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   131
    private static final Set<Integer> DebugLevels = new HashSet<>(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   132
                    Arrays.asList(DebugContext.ENABLED_LEVEL, BASIC_LEVEL, DebugContext.INFO_LEVEL, DebugContext.VERBOSE_LEVEL, DebugContext.DETAILED_LEVEL, DebugContext.VERY_DETAILED_LEVEL));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   133
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   134
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   135
     * The set of methods allowed to call a {@code Debug.dump(...)} method with the {@code level}
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   136
     * parameter bound to {@link DebugContext#BASIC_LEVEL} and the {@code object} parameter bound to
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   137
     * a {@link StructuredGraph} value.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   138
     *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   139
     * This whitelist exists to ensure any increase in graph dumps is in line with the policy
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   140
     * outlined by {@link DebugContext#BASIC_LEVEL}. If you add a *justified* graph dump at this
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   141
     * level, then update the whitelist.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   142
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   143
    private static final Set<String> BasicLevelStructuredGraphDumpWhitelist = new HashSet<>(Arrays.asList(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   144
                    "org.graalvm.compiler.phases.BasePhase.dumpAfter",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   145
                    "org.graalvm.compiler.phases.BasePhase.dumpBefore",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   146
                    "org.graalvm.compiler.core.GraalCompiler.emitFrontEnd",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   147
                    "org.graalvm.compiler.truffle.compiler.PartialEvaluator.fastPartialEvaluation",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   148
                    "org.graalvm.compiler.truffle.compiler.PartialEvaluator$PerformanceInformationHandler.reportPerformanceWarnings",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   149
                    "org.graalvm.compiler.truffle.compiler.TruffleCompilerImpl.compilePEGraph",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   150
                    "org.graalvm.compiler.core.test.VerifyDebugUsageTest$ValidDumpUsagePhase.run",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   151
                    "org.graalvm.compiler.core.test.VerifyDebugUsageTest$InvalidConcatDumpUsagePhase.run",
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   152
                    "org.graalvm.compiler.core.test.VerifyDebugUsageTest$InvalidDumpUsagePhase.run",
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   153
                    "org.graalvm.compiler.hotspot.SymbolicSnippetEncoder.verifySnippetEncodeDecode"));
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   154
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   155
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   156
     * The set of methods allowed to call a {@code Debug.dump(...)} method with the {@code level}
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   157
     * parameter bound to {@link DebugContext#INFO_LEVEL} and the {@code object} parameter bound to
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   158
     * a {@link StructuredGraph} value.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   159
     *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   160
     * This whitelist exists to ensure any increase in graph dumps is in line with the policy
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   161
     * outlined by {@link DebugContext#INFO_LEVEL}. If you add a *justified* graph dump at this
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   162
     * level, then update the whitelist.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   163
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   164
    private static final Set<String> InfoLevelStructuredGraphDumpWhitelist = new HashSet<>(Arrays.asList(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   165
                    "org.graalvm.compiler.core.GraalCompiler.emitFrontEnd",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   166
                    "org.graalvm.compiler.phases.BasePhase.dumpAfter",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   167
                    "org.graalvm.compiler.replacements.ReplacementsImpl$GraphMaker.makeGraph",
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   168
                    "org.graalvm.compiler.replacements.SnippetTemplate.instantiate",
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 54084
diff changeset
   169
                    "org.graalvm.compiler.replacements.SnippetTemplate.<init>",
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   170
                    "org.graalvm.compiler.hotspot.SymbolicSnippetEncoder.verifySnippetEncodeDecode"));
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   171
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   172
    private void verifyParameters(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, List<? extends ValueNode> args, ResolvedJavaType stringType, int startArgIdx,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   173
                    int varArgsIndex) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   174
        ResolvedJavaMethod verifiedCallee = debugCallTarget.targetMethod();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   175
        Integer dumpLevel = null;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   176
        int argIdx = startArgIdx;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   177
        int varArgsElementIndex = 0;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   178
        boolean reportVarArgs = false;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   179
        for (int i = 0; i < args.size(); i++) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   180
            ValueNode arg = args.get(i);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   181
            if (arg instanceof Invoke) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   182
                reportVarArgs = varArgsIndex >= 0 && argIdx >= varArgsIndex;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   183
                Invoke invoke = (Invoke) arg;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   184
                CallTargetNode callTarget = invoke.callTarget();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   185
                if (callTarget instanceof MethodCallTargetNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   186
                    ResolvedJavaMethod m = ((MethodCallTargetNode) callTarget).targetMethod();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   187
                    if (m.getName().equals("toString")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   188
                        int bci = invoke.bci();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   189
                        int nonVarArgIdx = reportVarArgs ? argIdx - varArgsElementIndex : argIdx;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   190
                        verifyStringConcat(callerGraph, verifiedCallee, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1, m);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   191
                        verifyToStringCall(callerGraph, verifiedCallee, stringType, m, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   192
                    } else if (m.getName().equals("format")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   193
                        int bci = invoke.bci();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   194
                        int nonVarArgIdx = reportVarArgs ? argIdx - varArgsElementIndex : argIdx;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   195
                        verifyFormatCall(callerGraph, verifiedCallee, stringType, m, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   196
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   197
                    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   198
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   199
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   200
            if (i == 1) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   201
                if (verifiedCallee.getName().equals("dump")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   202
                    dumpLevel = verifyDumpLevelParameter(callerGraph, debugCallTarget, verifiedCallee, arg);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   203
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   204
            } else if (i == 2) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   205
                if (dumpLevel != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   206
                    verifyDumpObjectParameter(callerGraph, debugCallTarget, arg, verifiedCallee, dumpLevel);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   207
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   208
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   209
            if (varArgsIndex >= 0 && i >= varArgsIndex) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   210
                varArgsElementIndex++;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   211
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   212
            argIdx++;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   213
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   214
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   215
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   216
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   217
     * The {@code level} arg for the {@code Debug.dump(...)} methods must be a reference to one of
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   218
     * the {@code Debug.*_LEVEL} constants.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   219
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   220
    protected Integer verifyDumpLevelParameter(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ResolvedJavaMethod verifiedCallee, ValueNode arg)
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   221
                    throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   222
        // The 'level' arg for the Debug.dump(...) methods must be a reference to one of
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   223
        // the Debug.*_LEVEL constants.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   224
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   225
        Constant c = arg.asConstant();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   226
        if (c != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   227
            Integer dumpLevel = ((PrimitiveConstant) c).asInt();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   228
            if (!DebugLevels.contains(dumpLevel)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   229
                StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   230
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   231
                                "In %s: parameter 0 of call to %s does not match a Debug.*_LEVEL constant: %s.%n", e, verifiedCallee.format("%H.%n(%p)"), dumpLevel);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   232
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   233
            return dumpLevel;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   234
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   235
        StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   236
        throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   237
                        "In %s: parameter 0 of call to %s must be a constant, not %s.%n", e, verifiedCallee.format("%H.%n(%p)"), arg);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   238
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   239
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   240
    protected void verifyDumpObjectParameter(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ValueNode arg, ResolvedJavaMethod verifiedCallee, Integer dumpLevel)
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   241
                    throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   242
        ResolvedJavaType argType = ((ObjectStamp) arg.stamp(NodeView.DEFAULT)).type();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   243
        if (metaAccess.lookupJavaType(Graph.class).isAssignableFrom(argType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   244
            verifyStructuredGraphDumping(callerGraph, debugCallTarget, verifiedCallee, dumpLevel);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   245
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   246
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   247
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   248
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   249
     * Verifies that dumping a {@link StructuredGraph} at level {@link DebugContext#BASIC_LEVEL} or
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   250
     * {@link DebugContext#INFO_LEVEL} only occurs in white-listed methods.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   251
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   252
    protected void verifyStructuredGraphDumping(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ResolvedJavaMethod verifiedCallee, Integer dumpLevel)
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   253
                    throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   254
        if (dumpLevel == DebugContext.BASIC_LEVEL) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   255
            StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   256
            String qualifiedMethod = e.getClassName() + "." + e.getMethodName();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   257
            if (!BasicLevelStructuredGraphDumpWhitelist.contains(qualifiedMethod)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   258
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   259
                                "In %s: call to %s with level == DebugContext.BASIC_LEVEL not in %s.BasicLevelDumpWhitelist.%n", e, verifiedCallee.format("%H.%n(%p)"),
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   260
                                getClass().getName());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   261
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   262
        } else if (dumpLevel == DebugContext.INFO_LEVEL) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   263
            StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   264
            String qualifiedMethod = e.getClassName() + "." + e.getMethodName();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   265
            if (!InfoLevelStructuredGraphDumpWhitelist.contains(qualifiedMethod)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   266
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   267
                                "In %s: call to %s with level == Debug.INFO_LEVEL not in %s.InfoLevelDumpWhitelist.%n", e, verifiedCallee.format("%H.%n(%p)"),
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   268
                                getClass().getName());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   269
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   270
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   271
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   272
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   273
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   274
     * Checks that a given call is not to {@link StringBuffer#toString()} or
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   275
     * {@link StringBuilder#toString()}.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   276
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   277
    private static void verifyStringConcat(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, int bci, int argIdx, int varArgsElementIndex, ResolvedJavaMethod callee) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   278
        if (callee.getDeclaringClass().getName().equals("Ljava/lang/StringBuilder;") || callee.getDeclaringClass().getName().equals("Ljava/lang/StringBuffer;")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   279
            StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   280
            if (varArgsElementIndex >= 0) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   281
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   282
                                "In %s: element %d of parameter %d of call to %s appears to be a String concatenation expression.%n", e, varArgsElementIndex, argIdx,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   283
                                verifiedCallee.format("%H.%n(%p)"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   284
            } else {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   285
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   286
                                "In %s: parameter %d of call to %s appears to be a String concatenation expression.", e, argIdx, verifiedCallee.format("%H.%n(%p)"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   287
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   288
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   289
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   290
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   291
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   292
     * Checks that a given call is not to {@link Object#toString()}.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   293
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   294
    private static void verifyToStringCall(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, ResolvedJavaType stringType, ResolvedJavaMethod callee, int bci, int argIdx,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   295
                    int varArgsElementIndex) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   296
        if (callee.getSignature().getParameterCount(false) == 0 && callee.getSignature().getReturnType(callee.getDeclaringClass()).equals(stringType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   297
            StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   298
            if (varArgsElementIndex >= 0) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   299
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   300
                                "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.",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   301
                                e, varArgsElementIndex, argIdx, verifiedCallee.format("%H.%n(%p)"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   302
            } else {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   303
                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,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   304
                                verifiedCallee.format("%H.%n(%p)"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   305
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   306
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   307
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   308
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   309
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   310
     * Checks that a given call is not to {@link String#format(String, Object...)} or
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   311
     * {@link String#format(java.util.Locale, String, Object...)}.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   312
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   313
    private static void verifyFormatCall(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, ResolvedJavaType stringType, ResolvedJavaMethod callee, int bci, int argIdx,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   314
                    int varArgsElementIndex) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   315
        if (callee.getDeclaringClass().equals(stringType) && callee.getSignature().getReturnType(callee.getDeclaringClass()).equals(stringType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   316
            StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   317
            if (varArgsElementIndex >= 0) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   318
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   319
                                "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.",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   320
                                e, varArgsElementIndex, argIdx, verifiedCallee.format("%H.%n(%p)"), verifiedCallee.format("%h.%n"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   321
            } else {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   322
                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,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   323
                                argIdx,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   324
                                verifiedCallee.format("%H.%n(%p)"), verifiedCallee.format("%h.%n"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   325
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   326
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   327
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   328
}