1 /*
   2  * Copyright (c) 2015, 2019, 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 org.graalvm.compiler.core.common.type.ObjectStamp;
  28 import org.graalvm.compiler.core.common.type.Stamp;
  29 import org.graalvm.compiler.graph.Graph;
  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.graph.NodeInputList;
  32 import org.graalvm.compiler.nodes.ConstantNode;
  33 import org.graalvm.compiler.nodes.NodeView;
  34 import org.graalvm.compiler.nodes.StructuredGraph;
  35 import org.graalvm.compiler.nodes.ValueNode;
  36 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  37 import org.graalvm.compiler.nodes.spi.CoreProviders;
  38 import org.graalvm.compiler.nodes.spi.Virtualizable;
  39 import org.graalvm.compiler.phases.VerifyPhase;
  40 
  41 import jdk.vm.ci.meta.ResolvedJavaMethod;
  42 import jdk.vm.ci.meta.ResolvedJavaType;
  43 
  44 /**
  45  * Verifies that node types implementing the {@link Virtualizable} interface use it correctly.
  46  * Implementors of {@link Virtualizable#virtualize(org.graalvm.compiler.nodes.spi.VirtualizerTool)}
  47  * must not apply effects on their {@link Graph graph} that cannot be easily undone.
  48  */
  49 public class VerifyVirtualizableUsage extends VerifyPhase<CoreProviders> {
  50     @Override
  51     public boolean checkContract() {
  52         return false;
  53     }
  54 
  55     @Override
  56     protected void verify(StructuredGraph graph, CoreProviders context) {
  57         final ResolvedJavaType graphType = context.getMetaAccess().lookupJavaType(Graph.class);
  58         final ResolvedJavaType virtualizableType = context.getMetaAccess().lookupJavaType(Virtualizable.class);
  59         final ResolvedJavaType constantNodeType = context.getMetaAccess().lookupJavaType(ConstantNode.class);
  60         if (virtualizableType.isAssignableFrom(graph.method().getDeclaringClass()) && graph.method().getName().equals("virtualize")) {
  61             for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
  62                 int bci = t.invoke().bci();
  63                 ResolvedJavaMethod callee = t.targetMethod();
  64                 String calleeName = callee.getName();
  65                 if (callee.getDeclaringClass().equals(graphType)) {
  66                     if (calleeName.equals("add") || calleeName.equals("addWithoutUnique") || calleeName.equals("addOrUnique") || calleeName.equals("addWithoutUniqueWithInputs") ||
  67                                     calleeName.equals("addOrUniqueWithInputs")) {
  68                         verifyVirtualizableEffectArguments(constantNodeType, graph.method(), callee, bci, t.arguments(), 1);
  69                     }
  70                 }
  71             }
  72         }
  73     }
  74 
  75     private static void verifyVirtualizableEffectArguments(ResolvedJavaType constantNodeType, ResolvedJavaMethod caller, ResolvedJavaMethod callee, int bciCaller,
  76                     NodeInputList<? extends Node> arguments, int startIdx) {
  77         /*
  78          * Virtualizable.virtualize should never apply effects on the graph during the execution of
  79          * the call as the handling of loops during pea might be speculative and does not hold. We
  80          * should only allow nodes changing the graph that do no harm like constants.
  81          */
  82         int i = 0;
  83         for (Node arg : arguments) {
  84             if (i >= startIdx) {
  85                 Stamp argStamp = ((ValueNode) arg).stamp(NodeView.DEFAULT);
  86                 if (argStamp instanceof ObjectStamp) {
  87                     ObjectStamp objectStamp = (ObjectStamp) argStamp;
  88                     ResolvedJavaType argStampType = objectStamp.type();
  89                     if (!(argStampType.equals(constantNodeType))) {
  90                         StackTraceElement e = caller.asStackTraceElement(bciCaller);
  91                         throw new VerificationError("%s:Parameter %d in call to %s (which has effects on the graph) is not a " +
  92                                         "constant and thus not safe to apply during speculative virtualization.", e, i, callee.format("%H.%n(%p)"));
  93                     }
  94                 }
  95             }
  96             i++;
  97         }
  98     }
  99 
 100 }