src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/VerifyGetOptionsUsage.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) 2017, 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 java.lang.reflect.MalformedParametersException;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    28
import java.lang.reflect.Method;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    29
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    30
import org.graalvm.compiler.graph.Node;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    31
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    32
import org.graalvm.compiler.nodes.StructuredGraph;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    33
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    34
import org.graalvm.compiler.nodes.spi.CoreProviders;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    35
import org.graalvm.compiler.phases.VerifyPhase;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    36
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    37
import jdk.vm.ci.meta.MetaAccessProvider;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    38
import jdk.vm.ci.meta.ResolvedJavaMethod;
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    39
import jdk.vm.ci.meta.ResolvedJavaMethod.Parameter;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    40
import jdk.vm.ci.meta.ResolvedJavaType;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    41
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    42
/**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    43
 * {@link Node#getOptions()} is unsafe for use during canonicalization so try to verify that it
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    44
 * isn't used when a {@link CanonicalizerTool} is available in the arguments. This is slightly more
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    45
 * general but since there are several canonical methods with varying signatures this covers more
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    46
 * cases.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    47
 */
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    48
public class VerifyGetOptionsUsage extends VerifyPhase<CoreProviders> {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    49
    static Method lookupMethod(Class<?> klass, String name) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    50
        for (Method m : klass.getDeclaredMethods()) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    51
            if (m.getName().equals(name)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    52
                return m;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    53
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    54
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    55
        throw new InternalError();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    56
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    57
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    58
    @Override
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    59
    protected void verify(StructuredGraph graph, CoreProviders context) {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    60
        MetaAccessProvider metaAccess = context.getMetaAccess();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    61
        ResolvedJavaType canonicalizerToolClass = metaAccess.lookupJavaType(CanonicalizerTool.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    62
        boolean hasTool = false;
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    63
        ResolvedJavaMethod method = graph.method();
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    64
        try {
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    65
            Parameter[] parameters = method.getParameters();
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    66
            if (parameters != null) {
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    67
                for (ResolvedJavaMethod.Parameter parameter : parameters) {
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    68
                    if (parameter.getType().getName().equals(canonicalizerToolClass.getName())) {
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    69
                        hasTool = true;
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    70
                        break;
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    71
                    }
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    72
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    73
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    74
        } catch (MalformedParametersException e) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    75
            // Lambdas sometimes have malformed parameters so ignore this.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    76
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    77
        if (hasTool) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    78
            ResolvedJavaMethod getOptionsMethod = metaAccess.lookupJavaMethod(lookupMethod(Node.class, "getOptions"));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    79
            for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    80
                ResolvedJavaMethod callee = t.targetMethod();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    81
                if (callee.equals(getOptionsMethod)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    82
                    if (hasTool) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    83
                        throw new VerificationError("Must use CanonicalizerTool.getOptions() instead of Node.getOptions() in method '%s' of class '%s'.",
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    84
                                        method.getName(), method.getDeclaringClass().getName());
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    85
                    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    86
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    87
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    88
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    89
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    90
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    91
}