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