001/*
002 * Copyright (c) 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.compiler.test;
024
025import jdk.internal.jvmci.meta.*;
026import jdk.internal.jvmci.meta.Assumptions.*;
027
028import org.junit.*;
029
030public class FindUniqueConcreteMethodBugTest extends GraalCompilerTest {
031
032    // To cause a C1 or C2 crash: -DFindUniqueConcreteMethodBugTest.ITERATIONS=10000
033    private static final int ITERATIONS = Integer.getInteger("FindUniqueConcreteMethodBugTest.ITERATIONS", 100);
034
035    /**
036     * Executing {@link ResolvedJavaType#findUniqueConcreteMethod(ResolvedJavaMethod)} for the
037     * method {@link Person#getName()} on the type {@link AbstractPerson} should return null as both
038     * {@link PersonImpl} and {@link TenantImpl} provide implementations (namely
039     * {@link PersonImpl#getName()} and {@link Tenant#getName()}).
040     */
041    @Test
042    @Ignore("fix HotSpotResolvedObjectTypeImpl.findUniqueConcreteMethod")
043    public void test() throws NoSuchMethodException {
044        ResolvedJavaMethod ifaceMethod = getMetaAccess().lookupJavaMethod(Person.class.getDeclaredMethod("getName"));
045
046        PersonImpl person = new PersonImpl("maya");
047        TenantImpl tenant = new TenantImpl(0xdeadbeef);
048
049        // Ensure the relevant methods are linked
050        person.getName();
051        tenant.getName();
052
053        for (int i = 0; i < ITERATIONS; i++) {
054            getLabelLength(person);
055            getLabelLength(tenant);
056        }
057
058        // Until HotSpotResolvedObjectTypeImpl.findUniqueConcreteMethod is fixed,
059        // this causes a VM crash as getLabelLength() directly invokes PersonImpl.getName().
060        test("getLabelLength", tenant);
061
062        ResolvedJavaMethod expected = null;
063        AssumptionResult<ResolvedJavaMethod> actual = getMetaAccess().lookupJavaType(AbstractPerson.class).findUniqueConcreteMethod(ifaceMethod);
064        Assert.assertEquals(expected, actual.getResult());
065
066    }
067
068    public int getLabelLength(AbstractPerson person) {
069        return person.getName().length();
070    }
071
072    interface Person {
073        String getName();
074
075        default int getId() {
076            return -1;
077        }
078    }
079
080    interface Tenant extends Person {
081        default String getName() {
082            return getAddress();
083        }
084
085        String getAddress();
086    }
087
088    abstract static class AbstractPerson implements Person {
089    }
090
091    static class PersonImpl extends AbstractPerson {
092        public String name;
093
094        public PersonImpl(String name) {
095            this.name = name;
096        }
097
098        public String getName() {
099            return name;
100        }
101    }
102
103    static class TenantImpl extends AbstractPerson implements Tenant {
104        public int id;
105
106        public TenantImpl(int id) {
107            this.id = id;
108        }
109
110        public String getAddress() {
111            return String.valueOf(id);
112        }
113    }
114}