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