VerifyBailoutUsage.java revision 12657:6ef01bd40ce2
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.phases.verify;
24
25import org.graalvm.compiler.common.PermanentBailoutException;
26import org.graalvm.compiler.common.RetryableBailoutException;
27import org.graalvm.compiler.debug.GraalError;
28import org.graalvm.compiler.nodes.StructuredGraph;
29import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
30import org.graalvm.compiler.phases.VerifyPhase;
31import org.graalvm.compiler.phases.tiers.PhaseContext;
32
33import jdk.vm.ci.code.BailoutException;
34import jdk.vm.ci.meta.ResolvedJavaMethod;
35import jdk.vm.ci.meta.ResolvedJavaType;
36
37public class VerifyBailoutUsage extends VerifyPhase<PhaseContext> {
38
39    private static final String[] AllowedPackagePrefixes;
40
41    private static String getPackageName(Class<?> c) {
42        String classNameWithPackage = c.getName();
43        String simpleName = c.getSimpleName();
44        return classNameWithPackage.substring(0, classNameWithPackage.length() - simpleName.length() - 1);
45    }
46
47    static {
48        try {
49            AllowedPackagePrefixes = new String[]{getPackageName(PermanentBailoutException.class), "jdk.vm.ci"};
50        } catch (Throwable t) {
51            throw new GraalError(t);
52        }
53    }
54
55    private static boolean matchesPrefix(String packageName) {
56        for (String allowedPackagePrefix : AllowedPackagePrefixes) {
57            if (packageName.startsWith(allowedPackagePrefix)) {
58                return true;
59            }
60        }
61        return false;
62    }
63
64    @Override
65    protected boolean verify(StructuredGraph graph, PhaseContext context) {
66        final ResolvedJavaType bailoutType = context.getMetaAccess().lookupJavaType(BailoutException.class);
67        ResolvedJavaMethod caller = graph.method();
68        String holderQualified = caller.format("%H");
69        String holderUnqualified = caller.format("%h");
70        String packageName = holderQualified.substring(0, holderQualified.length() - holderUnqualified.length() - 1);
71        if (!matchesPrefix(packageName)) {
72            for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
73                ResolvedJavaMethod callee = t.targetMethod();
74                if (callee.getDeclaringClass().equals(bailoutType)) {
75                    // we only allow the getter
76                    if (!callee.getName().equals("isPermanent")) {
77                        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)"),
78                                        caller.format("%H.%n(%p)"), PermanentBailoutException.class.getName(),
79                                        RetryableBailoutException.class.getName());
80                    }
81                }
82            }
83        }
84        return true;
85    }
86
87}
88