1 /*
   2  * Copyright (c) 2014, 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.core.test;
  24 
  25 import jdk.vm.ci.meta.Assumptions.AssumptionResult;
  26 import jdk.vm.ci.meta.ResolvedJavaMethod;
  27 import jdk.vm.ci.meta.ResolvedJavaType;
  28 
  29 import org.junit.Assert;
  30 import org.junit.Ignore;
  31 import org.junit.Test;
  32 
  33 public class FindUniqueConcreteMethodBugTest extends GraalCompilerTest {
  34 
  35     // To cause a C1 or C2 crash: -DFindUniqueConcreteMethodBugTest.ITERATIONS=10000
  36     private static final int ITERATIONS = Integer.getInteger("FindUniqueConcreteMethodBugTest.ITERATIONS", 100);
  37 
  38     /**
  39      * Executing {@link ResolvedJavaType#findUniqueConcreteMethod(ResolvedJavaMethod)} for the
  40      * method {@link Person#getName()} on the type {@link AbstractPerson} should return null as both
  41      * {@link PersonImpl} and {@link TenantImpl} provide implementations (namely
  42      * {@link PersonImpl#getName()} and {@link Tenant#getName()}).
  43      */
  44     @Test
  45     @Ignore("fix HotSpotResolvedObjectTypeImpl.findUniqueConcreteMethod")
  46     public void test() throws NoSuchMethodException {
  47         ResolvedJavaMethod ifaceMethod = getMetaAccess().lookupJavaMethod(Person.class.getDeclaredMethod("getName"));
  48 
  49         PersonImpl person = new PersonImpl("maya");
  50         TenantImpl tenant = new TenantImpl(0xdeadbeef);
  51 
  52         // Ensure the relevant methods are linked
  53         person.getName();
  54         tenant.getName();
  55 
  56         for (int i = 0; i < ITERATIONS; i++) {
  57             getLabelLength(person);
  58             getLabelLength(tenant);
  59         }
  60 
  61         // Until HotSpotResolvedObjectTypeImpl.findUniqueConcreteMethod is fixed,
  62         // this causes a VM crash as getLabelLength() directly invokes PersonImpl.getName().
  63         test("getLabelLength", tenant);
  64 
  65         ResolvedJavaMethod expected = null;
  66         AssumptionResult<ResolvedJavaMethod> actual = getMetaAccess().lookupJavaType(AbstractPerson.class).findUniqueConcreteMethod(ifaceMethod);
  67         Assert.assertEquals(expected, actual.getResult());
  68 
  69     }
  70 
  71     public int getLabelLength(AbstractPerson person) {
  72         return person.getName().length();
  73     }
  74 
  75     interface Person {
  76         String getName();
  77 
  78         default int getId() {
  79             return -1;
  80         }
  81     }
  82 
  83     interface Tenant extends Person {
  84         @Override
  85         default String getName() {
  86             return getAddress();
  87         }
  88 
  89         String getAddress();
  90     }
  91 
  92     abstract static class AbstractPerson implements Person {
  93     }
  94 
  95     static class PersonImpl extends AbstractPerson {
  96         public String name;
  97 
  98         PersonImpl(String name) {
  99             this.name = name;
 100         }
 101 
 102         @Override
 103         public String getName() {
 104             return name;
 105         }
 106     }
 107 
 108     static class TenantImpl extends AbstractPerson implements Tenant {
 109         public int id;
 110 
 111         TenantImpl(int id) {
 112             this.id = id;
 113         }
 114 
 115         @Override
 116         public String getAddress() {
 117             return String.valueOf(id);
 118         }
 119     }
 120 }