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