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