src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/VerifyBailoutUsage.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) 2016, 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.core.common.PermanentBailoutException;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    28
import org.graalvm.compiler.core.common.RetryableBailoutException;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    29
import org.graalvm.compiler.debug.GraalError;
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.MethodCallTargetNode;
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.code.BailoutException;
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
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    39
public class VerifyBailoutUsage extends VerifyPhase<CoreProviders> {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    40
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    41
    private static final String[] AllowedPackagePrefixes;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    42
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    43
    private static String getPackageName(Class<?> c) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    44
        String classNameWithPackage = c.getName();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    45
        String simpleName = c.getSimpleName();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    46
        return classNameWithPackage.substring(0, classNameWithPackage.length() - simpleName.length() - 1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    47
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    48
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    49
    static {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    50
        try {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    51
            AllowedPackagePrefixes = new String[]{
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    52
                            getPackageName(PermanentBailoutException.class),
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    53
                            "jdk.vm.ci",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    54
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    55
                            // Allows GraalTruffleRuntime.handleAnnotationFailure to throw
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    56
                            // a BailoutException since the org.graalvm.compiler.truffle.runtime
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    57
                            // project can not see the PermanentBailoutException or
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    58
                            // RetryableBailoutException types.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    59
                            "org.graalvm.compiler.truffle.runtime"
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    60
            };
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    61
        } catch (Throwable t) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    62
            throw new GraalError(t);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    63
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    64
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    65
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    66
    private static boolean matchesPrefix(String packageName) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    67
        for (String allowedPackagePrefix : AllowedPackagePrefixes) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    68
            if (packageName.startsWith(allowedPackagePrefix)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    69
                return true;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    70
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    71
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    72
        return false;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    73
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    74
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    75
    @Override
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    76
    protected void verify(StructuredGraph graph, CoreProviders context) {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    77
        final ResolvedJavaType bailoutType = context.getMetaAccess().lookupJavaType(BailoutException.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    78
        ResolvedJavaMethod caller = graph.method();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    79
        String holderQualified = caller.format("%H");
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    80
        String holderUnqualified = caller.format("%h");
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    81
        String packageName = holderQualified.substring(0, holderQualified.length() - holderUnqualified.length() - 1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    82
        if (!matchesPrefix(packageName)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    83
            for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    84
                ResolvedJavaMethod callee = t.targetMethod();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    85
                if (callee.getDeclaringClass().equals(bailoutType)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    86
                    // we only allow the getter
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    87
                    if (!callee.getName().equals("isPermanent")) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    88
                        throw new VerificationError("Call to %s at callsite %s is prohibited. Consider using %s for permanent bailouts or %s for retryables.", callee.format("%H.%n(%p)"),
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    89
                                        caller.format("%H.%n(%p)"), PermanentBailoutException.class.getName(),
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    90
                                        RetryableBailoutException.class.getName());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    91
                    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    92
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    93
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    94
        }
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
}