001/*
002 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.phases.verify;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.nodes.*;
029import com.oracle.graal.nodes.calc.*;
030import com.oracle.graal.nodes.type.*;
031import com.oracle.graal.phases.*;
032import com.oracle.graal.phases.tiers.*;
033
034/**
035 * For certain types, object identity should not be used for object equality check. This phase
036 * checks the correct usage of the given type. Equality checks with == or != (except null checks)
037 * results in an {@link AssertionError}.
038 *
039 * Note that only {@link TrustedInterface}s can be verified.
040 */
041public class VerifyUsageWithEquals extends VerifyPhase<PhaseContext> {
042
043    /**
044     * The type of values that must not use identity for testing object equality.
045     */
046    private final Class<?> restrictedClass;
047
048    public VerifyUsageWithEquals(Class<?> restrictedClass) {
049        this.restrictedClass = restrictedClass;
050        assert !restrictedClass.isInterface() || TrustedInterface.class.isAssignableFrom(restrictedClass);
051    }
052
053    /**
054     * Determines whether the type of {@code node} is assignable to the {@link #restrictedClass}.
055     */
056    private boolean isAssignableToRestrictedType(ValueNode node, MetaAccessProvider metaAccess) {
057        if (node.stamp() instanceof ObjectStamp) {
058            ResolvedJavaType restrictedType = metaAccess.lookupJavaType(restrictedClass);
059            ResolvedJavaType nodeType = StampTool.typeOrNull(node);
060
061            if (nodeType != null && restrictedType.isAssignableFrom(nodeType)) {
062                return true;
063            }
064        }
065        return false;
066    }
067
068    private static boolean isNullConstant(ValueNode node) {
069        return node.isConstant() && node.isNullConstant();
070    }
071
072    private static boolean isEqualsMethod(ResolvedJavaMethod method) {
073        if (method.getName().equals("equals")) {
074            Signature sig = method.getSignature();
075            if (sig.getReturnKind() == Kind.Boolean) {
076                if (sig.getParameterCount(false) == 1) {
077                    ResolvedJavaType ptype = (ResolvedJavaType) sig.getParameterType(0, method.getDeclaringClass());
078                    if (ptype.isJavaLangObject()) {
079                        return true;
080                    }
081
082                }
083            }
084        }
085        return false;
086    }
087
088    private static boolean isThisParameter(ValueNode node) {
089        return node instanceof ParameterNode && ((ParameterNode) node).index() == 0;
090    }
091
092    /**
093     * Checks whether the type of {@code x} is assignable to the restricted type and that {@code y}
094     * is not a null constant.
095     */
096    private boolean isIllegalUsage(ResolvedJavaMethod method, ValueNode x, ValueNode y, MetaAccessProvider metaAccess) {
097        if (isAssignableToRestrictedType(x, metaAccess) && !isNullConstant(y)) {
098            if (isEqualsMethod(method) && isThisParameter(x) || isThisParameter(y)) {
099                return false;
100            }
101            return true;
102        }
103        return false;
104    }
105
106    @Override
107    protected boolean verify(StructuredGraph graph, PhaseContext context) {
108        for (ObjectEqualsNode cn : graph.getNodes().filter(ObjectEqualsNode.class)) {
109            // bail out if we compare an object of type klass with == or != (except null checks)
110            ResolvedJavaMethod method = graph.method();
111            ResolvedJavaType restrictedType = context.getMetaAccess().lookupJavaType(restrictedClass);
112
113            if (method.getDeclaringClass().equals(restrictedType)) {
114                // Allow violation in methods of the restricted type itself.
115            } else if (isIllegalUsage(method, cn.getX(), cn.getY(), context.getMetaAccess()) || isIllegalUsage(method, cn.getY(), cn.getX(), context.getMetaAccess())) {
116                throw new VerificationError("Verification of " + restrictedClass.getName() + " usage failed: Comparing " + cn.getX() + " and " + cn.getY() + " in " + method +
117                                " must use .equals() for object equality, not '==' or '!='");
118            }
119        }
120        return true;
121    }
122}