1 /*
   2  * Copyright (c) 2017, 2018, 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.core.test;
  26 
  27 import org.graalvm.compiler.lir.StandardOp.LoadConstantOp;
  28 import org.graalvm.compiler.lir.StandardOp.MoveOp;
  29 import org.graalvm.compiler.lir.StandardOp.ValueMoveOp;
  30 import org.graalvm.compiler.nodes.StructuredGraph;
  31 import org.graalvm.compiler.nodes.java.InstanceOfNode;
  32 import org.graalvm.compiler.nodes.spi.CoreProviders;
  33 import org.graalvm.compiler.phases.VerifyPhase;
  34 
  35 import jdk.vm.ci.meta.MetaAccessProvider;
  36 import jdk.vm.ci.meta.ResolvedJavaMethod;
  37 import jdk.vm.ci.meta.ResolvedJavaType;
  38 
  39 /**
  40  * Checks that we do not use {@code instanceof} for types where faster alternatives are available.
  41  */
  42 public class VerifyInstanceOfUsage extends VerifyPhase<CoreProviders> {
  43 
  44     private static final Class<?>[] FORBIDDEN_INSTANCE_OF_CHECKS = {
  45                     MoveOp.class,
  46                     ValueMoveOp.class,
  47                     LoadConstantOp.class
  48     };
  49 
  50     @Override
  51     public boolean checkContract() {
  52         return false;
  53     }
  54 
  55     @Override
  56     protected void verify(StructuredGraph graph, CoreProviders context) {
  57         final ResolvedJavaType[] bailoutType = new ResolvedJavaType[FORBIDDEN_INSTANCE_OF_CHECKS.length];
  58         for (int i = 0; i < FORBIDDEN_INSTANCE_OF_CHECKS.length; i++) {
  59             bailoutType[i] = context.getMetaAccess().lookupJavaType(FORBIDDEN_INSTANCE_OF_CHECKS[i]);
  60         }
  61         ResolvedJavaMethod method = graph.method();
  62         ResolvedJavaType declaringClass = method.getDeclaringClass();
  63         if (!isTrustedInterface(declaringClass, context.getMetaAccess())) {
  64 
  65             for (InstanceOfNode io : graph.getNodes().filter(InstanceOfNode.class)) {
  66                 ResolvedJavaType type = io.type().getType();
  67                 for (ResolvedJavaType forbiddenType : bailoutType) {
  68                     if (forbiddenType.equals(type)) {
  69                         String name = forbiddenType.getUnqualifiedName();
  70                         // strip outer class
  71                         ResolvedJavaType enclosingType = forbiddenType.getEnclosingType();
  72                         if (enclosingType != null) {
  73                             name = name.substring(enclosingType.getUnqualifiedName().length() + "$".length());
  74                         }
  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)"));
  76                     }
  77                 }
  78             }
  79         }
  80     }
  81 
  82     private static boolean isTrustedInterface(ResolvedJavaType declaringClass, MetaAccessProvider metaAccess) {
  83         for (Class<?> trustedCls : FORBIDDEN_INSTANCE_OF_CHECKS) {
  84             ResolvedJavaType trusted = metaAccess.lookupJavaType(trustedCls);
  85             if (trusted.equals(declaringClass)) {
  86                 return true;
  87             }
  88         }
  89         return false;
  90     }
  91 
  92 }