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 jdk.vm.ci.meta.ResolvedJavaMethod; 26 import jdk.vm.ci.meta.ResolvedJavaType; 27 import org.graalvm.compiler.debug.GraalError; 28 import org.graalvm.compiler.graph.Graph; 29 import org.graalvm.compiler.graph.Node; 30 import org.graalvm.compiler.nodes.ConstantNode; 31 import org.graalvm.compiler.nodes.Invoke; 32 import org.graalvm.compiler.nodes.ParameterNode; 33 import org.graalvm.compiler.nodes.PiNode; 34 import org.graalvm.compiler.nodes.StructuredGraph; 35 import org.graalvm.compiler.nodes.ValueNode; 36 import org.graalvm.compiler.nodes.ValuePhiNode; 37 import org.graalvm.compiler.nodes.ValueProxyNode; 38 import org.graalvm.compiler.nodes.java.MethodCallTargetNode; 39 import org.graalvm.compiler.nodes.java.NewInstanceNode; 40 import org.graalvm.compiler.nodes.spi.LoweringProvider; 41 import org.graalvm.compiler.phases.VerifyPhase; 42 import org.graalvm.compiler.phases.tiers.PhaseContext; 43 import org.graalvm.util.EconomicSet; 44 45 import java.lang.reflect.Constructor; 46 import java.lang.reflect.Method; 47 48 public class VerifyGraphAddUsage extends VerifyPhase<PhaseContext> { 49 private static final Method ADD_OR_UNIQUE; 50 private static final Method CONSTRUCTOR_NEW_INSTANCE; 51 private static final EconomicSet<Class<?>> ALLOWED_CLASSES = EconomicSet.create(); 52 53 static { 54 try { 55 ADD_OR_UNIQUE = Graph.class.getDeclaredMethod("addOrUnique", Node.class); 56 CONSTRUCTOR_NEW_INSTANCE = Constructor.class.getDeclaredMethod("newInstance", Object[].class); 57 } catch (NoSuchMethodException e) { 58 throw new GraalError(e); 59 } 60 61 ALLOWED_CLASSES.add(Graph.class); 62 ALLOWED_CLASSES.add(LoweringProvider.class); 63 } 64 65 @Override 66 protected boolean verify(StructuredGraph graph, PhaseContext context) { 67 boolean allowed = false; 68 for (Class<?> cls : ALLOWED_CLASSES) { 69 ResolvedJavaType declaringClass = graph.method().getDeclaringClass(); 70 if (context.getMetaAccess().lookupJavaType(cls).isAssignableFrom(declaringClass)) { 71 allowed = true; 72 } 73 } 74 if (!allowed) { 75 ResolvedJavaMethod addOrUniqueMethod = context.getMetaAccess().lookupJavaMethod(ADD_OR_UNIQUE); 76 for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) { 77 ResolvedJavaMethod callee = t.targetMethod(); 78 if (callee.equals(addOrUniqueMethod)) { 79 ValueNode nodeArgument = t.arguments().get(1); 80 EconomicSet<Node> seen = EconomicSet.create(); 81 checkNonFactory(graph, seen, context, nodeArgument); 82 } 83 } 84 } 85 86 return true; 87 } 88 89 private void checkNonFactory(StructuredGraph graph, EconomicSet<Node> seen, PhaseContext context, ValueNode node) { 90 if (seen.contains(node)) { 91 return; 92 } 93 seen.add(node); 94 95 // Check where the value came from recursively, or if it is allowed. 96 if (node instanceof ValuePhiNode) { 97 for (ValueNode input : ((ValuePhiNode) node).values()) { 98 checkNonFactory(graph, seen, context, input); 99 } 100 } else if (node instanceof PiNode) { 101 checkNonFactory(graph, seen, context, ((PiNode) node).object()); 102 } else if (node instanceof ParameterNode) { 103 return; 104 } else if (node instanceof ConstantNode) { 105 return; 106 } else if (node instanceof ValueProxyNode) { 107 checkNonFactory(graph, seen, context, ((ValueProxyNode) node).value()); 108 } else if (node instanceof Invoke && ((Invoke) node).callTarget().targetMethod().equals(context.getMetaAccess().lookupJavaMethod(CONSTRUCTOR_NEW_INSTANCE))) { 109 return; 110 } else if (!(node instanceof NewInstanceNode)) { 111 // In all other cases, the argument must be a new instance. 112 throw new VerificationError("Must add node '%s' with inputs in method '%s' of class '%s'.", 113 node, graph.method().getName(), graph.method().getDeclaringClass().getName()); 114 } 115 } 116 117 }