1 /*
   2  * Copyright (c) 2017, 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 package org.graalvm.compiler.phases.verify;
  24 
  25 import java.lang.reflect.MalformedParametersException;
  26 import java.lang.reflect.Method;
  27 
  28 import org.graalvm.compiler.graph.Node;
  29 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  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.meta.MetaAccessProvider;
  36 import jdk.vm.ci.meta.ResolvedJavaMethod;
  37 import jdk.vm.ci.meta.ResolvedJavaType;
  38 
  39 /**
  40  * {@link Node#getOptions()} is unsafe for use during canonicalization so try to verify that it
  41  * isn't used when a {@link CanonicalizerTool} is available in the arguments. This is slightly more
  42  * general but since there are several canonical methods with varying signatures this covers more
  43  * cases.
  44  */
  45 public class VerifyGetOptionsUsage extends VerifyPhase<PhaseContext> {
  46     static Method lookupMethod(Class<?> klass, String name) {
  47         for (Method m : klass.getDeclaredMethods()) {
  48             if (m.getName().equals(name)) {
  49                 return m;
  50             }
  51         }
  52         throw new InternalError();
  53     }
  54 
  55     @Override
  56     protected boolean verify(StructuredGraph graph, PhaseContext context) {
  57         MetaAccessProvider metaAccess = context.getMetaAccess();
  58         ResolvedJavaType canonicalizerToolClass = metaAccess.lookupJavaType(CanonicalizerTool.class);
  59         boolean hasTool = false;
  60         try {
  61             for (ResolvedJavaMethod.Parameter parameter : graph.method().getParameters()) {
  62                 if (parameter.getType().getName().equals(canonicalizerToolClass.getName())) {
  63                     hasTool = true;
  64                     break;
  65                 }
  66             }
  67         } catch (MalformedParametersException e) {
  68             // Lambdas sometimes have malformed parameters so ignore this.
  69         }
  70         if (hasTool) {
  71             ResolvedJavaMethod getOptionsMethod = metaAccess.lookupJavaMethod(lookupMethod(Node.class, "getOptions"));
  72             for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
  73                 ResolvedJavaMethod callee = t.targetMethod();
  74                 if (callee.equals(getOptionsMethod)) {
  75                     if (hasTool) {
  76                         throw new VerificationError("Must use CanonicalizerTool.getOptions() instead of Node.getOptions() in method '%s' of class '%s'.",
  77                                         graph.method().getName(), graph.method().getDeclaringClass().getName());
  78                     }
  79                 }
  80             }
  81         }
  82 
  83         return true;
  84     }
  85 
  86 }