src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/VerifyDebugUsage.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58299 6df94ce3ab2f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
     2
 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
52910
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;
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    49
import org.graalvm.compiler.nodes.spi.CoreProviders;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    50
import org.graalvm.compiler.phases.VerifyPhase;
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
 */
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    71
public class VerifyDebugUsage extends VerifyPhase<CoreProviders> {
52910
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
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    81
    protected void verify(StructuredGraph graph, CoreProviders 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",
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
   153
                    "org.graalvm.compiler.hotspot.SymbolicSnippetEncoder.verifySnippetEncodeDecode",
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
   154
                    "org.graalvm.compiler.truffle.compiler.phases.inlining.CallTree.dumpBasic"));
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   155
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   156
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   157
     * The set of methods allowed to call a {@code Debug.dump(...)} method with the {@code level}
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   158
     * parameter bound to {@link DebugContext#INFO_LEVEL} and the {@code object} parameter bound to
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   159
     * a {@link StructuredGraph} value.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   160
     *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   161
     * This whitelist exists to ensure any increase in graph dumps is in line with the policy
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   162
     * outlined by {@link DebugContext#INFO_LEVEL}. If you add a *justified* graph dump at this
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   163
     * level, then update the whitelist.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   164
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   165
    private static final Set<String> InfoLevelStructuredGraphDumpWhitelist = new HashSet<>(Arrays.asList(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   166
                    "org.graalvm.compiler.core.GraalCompiler.emitFrontEnd",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   167
                    "org.graalvm.compiler.phases.BasePhase.dumpAfter",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   168
                    "org.graalvm.compiler.replacements.ReplacementsImpl$GraphMaker.makeGraph",
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   169
                    "org.graalvm.compiler.replacements.SnippetTemplate.instantiate",
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 54084
diff changeset
   170
                    "org.graalvm.compiler.replacements.SnippetTemplate.<init>",
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
   171
                    "org.graalvm.compiler.hotspot.SymbolicSnippetEncoder.verifySnippetEncodeDecode",
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
   172
                    "org.graalvm.compiler.truffle.compiler.phases.inlining.CallTree.dumpInfo"));
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   173
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   174
    private void verifyParameters(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, List<? extends ValueNode> args, ResolvedJavaType stringType, int startArgIdx,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   175
                    int varArgsIndex) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   176
        ResolvedJavaMethod verifiedCallee = debugCallTarget.targetMethod();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   177
        Integer dumpLevel = null;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   178
        int argIdx = startArgIdx;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   179
        int varArgsElementIndex = 0;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   180
        boolean reportVarArgs = false;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   181
        for (int i = 0; i < args.size(); i++) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   182
            ValueNode arg = args.get(i);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   183
            if (arg instanceof Invoke) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   184
                reportVarArgs = varArgsIndex >= 0 && argIdx >= varArgsIndex;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   185
                Invoke invoke = (Invoke) arg;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   186
                CallTargetNode callTarget = invoke.callTarget();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   187
                if (callTarget instanceof MethodCallTargetNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   188
                    ResolvedJavaMethod m = ((MethodCallTargetNode) callTarget).targetMethod();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   189
                    if (m.getName().equals("toString")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   190
                        int bci = invoke.bci();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   191
                        int nonVarArgIdx = reportVarArgs ? argIdx - varArgsElementIndex : argIdx;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   192
                        verifyStringConcat(callerGraph, verifiedCallee, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1, m);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   193
                        verifyToStringCall(callerGraph, verifiedCallee, stringType, m, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   194
                    } else if (m.getName().equals("format")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   195
                        int bci = invoke.bci();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   196
                        int nonVarArgIdx = reportVarArgs ? argIdx - varArgsElementIndex : argIdx;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   197
                        verifyFormatCall(callerGraph, verifiedCallee, stringType, m, bci, nonVarArgIdx, reportVarArgs ? varArgsElementIndex : -1);
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
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   201
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   202
            if (i == 1) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   203
                if (verifiedCallee.getName().equals("dump")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   204
                    dumpLevel = verifyDumpLevelParameter(callerGraph, debugCallTarget, verifiedCallee, arg);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   205
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   206
            } else if (i == 2) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   207
                if (dumpLevel != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   208
                    verifyDumpObjectParameter(callerGraph, debugCallTarget, arg, verifiedCallee, dumpLevel);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   209
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   210
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   211
            if (varArgsIndex >= 0 && i >= varArgsIndex) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   212
                varArgsElementIndex++;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   213
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   214
            argIdx++;
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
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   218
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   219
     * 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
   220
     * the {@code Debug.*_LEVEL} constants.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   221
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   222
    protected Integer verifyDumpLevelParameter(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ResolvedJavaMethod verifiedCallee, ValueNode arg)
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   223
                    throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   224
        // The 'level' arg for the Debug.dump(...) methods must be a reference to one of
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   225
        // the Debug.*_LEVEL constants.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   226
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   227
        Constant c = arg.asConstant();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   228
        if (c != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   229
            Integer dumpLevel = ((PrimitiveConstant) c).asInt();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   230
            if (!DebugLevels.contains(dumpLevel)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   231
                StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   232
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   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);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   234
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   235
            return dumpLevel;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   236
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   237
        StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   238
        throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   239
                        "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
   240
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   241
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   242
    protected void verifyDumpObjectParameter(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ValueNode arg, ResolvedJavaMethod verifiedCallee, Integer dumpLevel)
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   243
                    throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   244
        ResolvedJavaType argType = ((ObjectStamp) arg.stamp(NodeView.DEFAULT)).type();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   245
        if (metaAccess.lookupJavaType(Graph.class).isAssignableFrom(argType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   246
            verifyStructuredGraphDumping(callerGraph, debugCallTarget, verifiedCallee, dumpLevel);
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
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   250
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   251
     * Verifies that dumping a {@link StructuredGraph} at level {@link DebugContext#BASIC_LEVEL} or
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   252
     * {@link DebugContext#INFO_LEVEL} only occurs in white-listed methods.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   253
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   254
    protected void verifyStructuredGraphDumping(StructuredGraph callerGraph, MethodCallTargetNode debugCallTarget, ResolvedJavaMethod verifiedCallee, Integer dumpLevel)
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   255
                    throws org.graalvm.compiler.phases.VerifyPhase.VerificationError {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   256
        if (dumpLevel == DebugContext.BASIC_LEVEL) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   257
            StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   258
            String qualifiedMethod = e.getClassName() + "." + e.getMethodName();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   259
            if (!BasicLevelStructuredGraphDumpWhitelist.contains(qualifiedMethod)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   260
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   261
                                "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
   262
                                getClass().getName());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   263
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   264
        } else if (dumpLevel == DebugContext.INFO_LEVEL) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   265
            StackTraceElement e = callerGraph.method().asStackTraceElement(debugCallTarget.invoke().bci());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   266
            String qualifiedMethod = e.getClassName() + "." + e.getMethodName();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   267
            if (!InfoLevelStructuredGraphDumpWhitelist.contains(qualifiedMethod)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   268
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   269
                                "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
   270
                                getClass().getName());
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
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   275
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   276
     * Checks that a given call is not to {@link StringBuffer#toString()} or
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   277
     * {@link StringBuilder#toString()}.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   278
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   279
    private static void verifyStringConcat(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, int bci, int argIdx, int varArgsElementIndex, ResolvedJavaMethod callee) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   280
        if (callee.getDeclaringClass().getName().equals("Ljava/lang/StringBuilder;") || callee.getDeclaringClass().getName().equals("Ljava/lang/StringBuffer;")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   281
            StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   282
            if (varArgsElementIndex >= 0) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   283
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   284
                                "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
   285
                                verifiedCallee.format("%H.%n(%p)"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   286
            } else {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   287
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   288
                                "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
   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
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   293
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   294
     * Checks that a given call is not to {@link Object#toString()}.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   295
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   296
    private static void verifyToStringCall(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, ResolvedJavaType stringType, ResolvedJavaMethod callee, int bci, int argIdx,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   297
                    int varArgsElementIndex) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   298
        if (callee.getSignature().getParameterCount(false) == 0 && callee.getSignature().getReturnType(callee.getDeclaringClass()).equals(stringType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   299
            StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   300
            if (varArgsElementIndex >= 0) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   301
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   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.",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   303
                                e, varArgsElementIndex, argIdx, verifiedCallee.format("%H.%n(%p)"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   304
            } else {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   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,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   306
                                verifiedCallee.format("%H.%n(%p)"));
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
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   311
    /**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   312
     * Checks that a given call is not to {@link String#format(String, Object...)} or
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   313
     * {@link String#format(java.util.Locale, String, Object...)}.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   314
     */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   315
    private static void verifyFormatCall(StructuredGraph callerGraph, ResolvedJavaMethod verifiedCallee, ResolvedJavaType stringType, ResolvedJavaMethod callee, int bci, int argIdx,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   316
                    int varArgsElementIndex) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   317
        if (callee.getDeclaringClass().equals(stringType) && callee.getSignature().getReturnType(callee.getDeclaringClass()).equals(stringType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   318
            StackTraceElement e = callerGraph.method().asStackTraceElement(bci);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   319
            if (varArgsElementIndex >= 0) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   320
                throw new VerificationError(
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   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.",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   322
                                e, varArgsElementIndex, argIdx, verifiedCallee.format("%H.%n(%p)"), verifiedCallee.format("%h.%n"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   323
            } else {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   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,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   325
                                argIdx,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   326
                                verifiedCallee.format("%H.%n(%p)"), verifiedCallee.format("%h.%n"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   327
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   328
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   329
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   330
}