1 /*
   2  * Copyright (c) 2013, 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.util.List;
  28 
  29 import org.graalvm.compiler.graph.Node;
  30 import org.graalvm.compiler.graph.Node.Input;
  31 import org.graalvm.compiler.graph.Node.OptionalInput;
  32 import org.graalvm.compiler.graph.NodeInputList;
  33 import org.graalvm.compiler.nodes.StructuredGraph;
  34 import org.graalvm.compiler.nodes.java.LoadFieldNode;
  35 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  36 import org.graalvm.compiler.nodes.java.StoreFieldNode;
  37 import org.graalvm.compiler.nodes.spi.CoreProviders;
  38 import org.graalvm.compiler.phases.VerifyPhase;
  39 
  40 import jdk.vm.ci.meta.ResolvedJavaField;
  41 import jdk.vm.ci.meta.ResolvedJavaMethod;
  42 import jdk.vm.ci.meta.ResolvedJavaType;
  43 
  44 /**
  45  * Try to ensure that methods which update {@link Input} or {@link OptionalInput} fields also
  46  * include a call to {@link Node#updateUsages} or {@link Node#updateUsagesInterface}.
  47  */
  48 public class VerifyUpdateUsages extends VerifyPhase<CoreProviders> {
  49 
  50     @Override
  51     public boolean checkContract() {
  52         return false;
  53     }
  54 
  55     public VerifyUpdateUsages() {
  56     }
  57 
  58     @Override
  59     protected void verify(StructuredGraph graph, CoreProviders context) {
  60         if (graph.method().isConstructor()) {
  61             return;
  62         }
  63         /*
  64          * There are only two acceptable patterns for methods which update Node inputs, either a
  65          * single StoreField node and invoke of updateUsages or updateUsagesInterface, or 2
  66          * StoreFields that come from LoadFields on the same object. Other patterns can be added as
  67          * needed but it would be best to keep things simple so that verification can be simple.
  68          */
  69         List<StoreFieldNode> stores = graph.getNodes().filter(StoreFieldNode.class).snapshot();
  70         ResolvedJavaType declaringClass = graph.method().getDeclaringClass();
  71         ResolvedJavaType nodeInputList = context.getMetaAccess().lookupJavaType(NodeInputList.class);
  72         StoreFieldNode storeField1 = null;
  73         StoreFieldNode storeField2 = null;
  74         for (StoreFieldNode store : stores) {
  75             if (isNodeInput(store.field(), declaringClass, nodeInputList)) {
  76                 if (storeField1 == null) {
  77                     storeField1 = store;
  78                 } else if (storeField2 == null) {
  79                     storeField2 = store;
  80                 } else {
  81                     throw new VerificationError("More than 2 stores to %s or %s fields found in %s",
  82                                     Input.class.getSimpleName(),
  83                                     OptionalInput.class.getSimpleName(),
  84                                     graph.method().format("%H.%n(%p)"));
  85                 }
  86             }
  87         }
  88         if (storeField1 == null) {
  89             return;
  90         }
  91         if (storeField2 == null) {
  92             // Single input field update so just check for updateUsages
  93             // or updateUsagesInterface call
  94             ResolvedJavaType nodeType = context.getMetaAccess().lookupJavaType(Node.class);
  95             for (MethodCallTargetNode call : graph.getNodes().filter(MethodCallTargetNode.class)) {
  96                 ResolvedJavaMethod callee = call.targetMethod();
  97                 if (callee.getDeclaringClass().equals(nodeType) && (callee.getName().equals("updateUsages") || callee.getName().equals("updateUsagesInterface"))) {
  98                     return;
  99                 }
 100             }
 101             throw new VerificationError("%s updates field '%s' without calling %s.updateUsages() or %s.updateUsagesInterface()",
 102                             graph.method().format("%H.%n(%p)"),
 103                             storeField1.field().getName(),
 104                             Node.class.getName(),
 105                             Node.class.getName());
 106         } else {
 107             if (storeField1.value() instanceof LoadFieldNode && storeField2.value() instanceof LoadFieldNode) {
 108                 LoadFieldNode load1 = (LoadFieldNode) storeField1.value();
 109                 LoadFieldNode load2 = (LoadFieldNode) storeField2.value();
 110                 // Check for swapping values within the same object
 111                 if (load1.object() == storeField1.object() &&
 112                                 load2.object() == storeField2.object() &&
 113                                 storeField1.object() == storeField2.object() &&
 114                                 load1.field().equals(storeField2.field()) &&
 115                                 load2.field().equals(storeField1.field())) {
 116                     return;
 117                 }
 118             }
 119             throw new VerificationError("%s performs non-swap update to fields '%s' and '%s' without calling %s.updateUsages() or %s.updateUsagesInterface()",
 120                             graph.method().format("%H.%n(%p)"),
 121                             storeField1.field().getName(),
 122                             storeField2.field().getName(),
 123                             Node.class.getName(),
 124                             Node.class.getName());
 125         }
 126     }
 127 
 128     boolean isNodeInput(ResolvedJavaField field, ResolvedJavaType declaringClass, ResolvedJavaType nodeInputList) {
 129         return declaringClass.isAssignableFrom(field.getDeclaringClass()) && (field.getAnnotation(Input.class) != null || field.getAnnotation(OptionalInput.class) != null) &&
 130                         !field.getType().equals(nodeInputList);
 131     }
 132 }