src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/VerifyInstanceOfUsage.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 org.graalvm.compiler.lir.StandardOp.LoadConstantOp;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    28
import org.graalvm.compiler.lir.StandardOp.MoveOp;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    29
import org.graalvm.compiler.lir.StandardOp.ValueMoveOp;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    30
import org.graalvm.compiler.nodes.StructuredGraph;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    31
import org.graalvm.compiler.nodes.java.InstanceOfNode;
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    32
import org.graalvm.compiler.nodes.spi.CoreProviders;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    33
import org.graalvm.compiler.phases.VerifyPhase;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    34
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    35
import jdk.vm.ci.meta.MetaAccessProvider;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    36
import jdk.vm.ci.meta.ResolvedJavaMethod;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    37
import jdk.vm.ci.meta.ResolvedJavaType;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    38
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    39
/**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    40
 * Checks that we do not use {@code instanceof} for types where faster alternatives are available.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    41
 */
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    42
public class VerifyInstanceOfUsage extends VerifyPhase<CoreProviders> {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    43
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    44
    private static final Class<?>[] FORBIDDEN_INSTANCE_OF_CHECKS = {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    45
                    MoveOp.class,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    46
                    ValueMoveOp.class,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    47
                    LoadConstantOp.class
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    48
    };
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    49
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    50
    @Override
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    51
    public boolean checkContract() {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    52
        return false;
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
    @Override
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    56
    protected void verify(StructuredGraph graph, CoreProviders context) {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    57
        final ResolvedJavaType[] bailoutType = new ResolvedJavaType[FORBIDDEN_INSTANCE_OF_CHECKS.length];
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    58
        for (int i = 0; i < FORBIDDEN_INSTANCE_OF_CHECKS.length; i++) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    59
            bailoutType[i] = context.getMetaAccess().lookupJavaType(FORBIDDEN_INSTANCE_OF_CHECKS[i]);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    60
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    61
        ResolvedJavaMethod method = graph.method();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    62
        ResolvedJavaType declaringClass = method.getDeclaringClass();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    63
        if (!isTrustedInterface(declaringClass, context.getMetaAccess())) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    64
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    65
            for (InstanceOfNode io : graph.getNodes().filter(InstanceOfNode.class)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    66
                ResolvedJavaType type = io.type().getType();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    67
                for (ResolvedJavaType forbiddenType : bailoutType) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    68
                    if (forbiddenType.equals(type)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    69
                        String name = forbiddenType.getUnqualifiedName();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    70
                        // strip outer class
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    71
                        ResolvedJavaType enclosingType = forbiddenType.getEnclosingType();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    72
                        if (enclosingType != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    73
                            name = name.substring(enclosingType.getUnqualifiedName().length() + "$".length());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    74
                        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    75
                        throw new VerificationError("Using `op instanceof %s` is not allowed. Use `%s.is%s(op)` instead. (in %s)", name, name, name, method.format("%H.%n(%p)"));
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
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    79
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    80
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    81
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    82
    private static boolean isTrustedInterface(ResolvedJavaType declaringClass, MetaAccessProvider metaAccess) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    83
        for (Class<?> trustedCls : FORBIDDEN_INSTANCE_OF_CHECKS) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    84
            ResolvedJavaType trusted = metaAccess.lookupJavaType(trustedCls);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    85
            if (trusted.equals(declaringClass)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    86
                return true;
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
        return false;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    90
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    91
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    92
}