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