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  */
  23 
  24 
  25 package org.graalvm.compiler.phases.verify;
  26 
  27 import org.graalvm.compiler.core.common.PermanentBailoutException;
  28 import org.graalvm.compiler.core.common.RetryableBailoutException;
  29 import org.graalvm.compiler.debug.GraalError;
  30 import org.graalvm.compiler.nodes.StructuredGraph;
  31 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  32 import org.graalvm.compiler.phases.VerifyPhase;
  33 import org.graalvm.compiler.phases.tiers.PhaseContext;
  34 
  35 import jdk.vm.ci.code.BailoutException;
  36 import jdk.vm.ci.meta.ResolvedJavaMethod;
  37 import jdk.vm.ci.meta.ResolvedJavaType;
  38 
  39 public class VerifyBailoutUsage extends VerifyPhase<PhaseContext> {
  40 
  41     private static final String[] AllowedPackagePrefixes;
  42 
  43     private static String getPackageName(Class<?> c) {
  44         String classNameWithPackage = c.getName();
  45         String simpleName = c.getSimpleName();
  46         return classNameWithPackage.substring(0, classNameWithPackage.length() - simpleName.length() - 1);
  47     }
  48 
  49     static {
  50         try {
  51             AllowedPackagePrefixes = new String[]{getPackageName(PermanentBailoutException.class), "jdk.vm.ci"};
  52         } catch (Throwable t) {
  53             throw new GraalError(t);
  54         }
  55     }
  56 
  57     private static boolean matchesPrefix(String packageName) {
  58         for (String allowedPackagePrefix : AllowedPackagePrefixes) {
  59             if (packageName.startsWith(allowedPackagePrefix)) {
  60                 return true;
  61             }
  62         }
  63         return false;
  64     }
  65 
  66     @Override
  67     protected boolean verify(StructuredGraph graph, PhaseContext context) {
  68         final ResolvedJavaType bailoutType = context.getMetaAccess().lookupJavaType(BailoutException.class);
  69         ResolvedJavaMethod caller = graph.method();
  70         String holderQualified = caller.format("%H");
  71         String holderUnqualified = caller.format("%h");
  72         String packageName = holderQualified.substring(0, holderQualified.length() - holderUnqualified.length() - 1);
  73         if (!matchesPrefix(packageName)) {
  74             for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
  75                 ResolvedJavaMethod callee = t.targetMethod();
  76                 if (callee.getDeclaringClass().equals(bailoutType)) {
  77                     // we only allow the getter
  78                     if (!callee.getName().equals("isPermanent")) {
  79                         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)"),
  80                                         caller.format("%H.%n(%p)"), PermanentBailoutException.class.getName(),
  81                                         RetryableBailoutException.class.getName());
  82                     }
  83                 }
  84             }
  85         }
  86         return true;
  87     }
  88 
  89 }