1 /* 2 * Copyright (c) 2014, 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 jdk.vm.ci.meta.Assumptions.AssumptionResult; 28 import jdk.vm.ci.meta.ResolvedJavaMethod; 29 import jdk.vm.ci.meta.ResolvedJavaType; 30 31 import org.junit.Assert; 32 import org.junit.Test; 33 34 public class FindUniqueConcreteMethodBugTest extends GraalCompilerTest { 35 36 // To cause a C1 or C2 crash: -DFindUniqueConcreteMethodBugTest.ITERATIONS=10000 37 private static final int ITERATIONS = Integer.getInteger("FindUniqueConcreteMethodBugTest.ITERATIONS", 100); 38 39 /** 40 * Executing {@link ResolvedJavaType#findUniqueConcreteMethod(ResolvedJavaMethod)} for the 41 * method {@link Person#getName()} on the type {@link AbstractPerson} should return null as both 42 * {@link PersonImpl} and {@link TenantImpl} provide implementations (namely 43 * {@link PersonImpl#getName()} and {@link Tenant#getName()}). 44 */ 45 @Test 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 AssumptionResult<ResolvedJavaMethod> actual = getMetaAccess().lookupJavaType(AbstractPerson.class).findUniqueConcreteMethod(ifaceMethod); 66 Assert.assertNull(String.valueOf(actual), actual); 67 68 } 69 70 public int getLabelLength(AbstractPerson person) { 71 return person.getName().length(); 72 } 73 74 interface Person { 75 String getName(); 76 77 default int getId() { 78 return -1; 79 } 80 } 81 82 interface Tenant extends Person { 83 @Override 84 default String getName() { 85 return getAddress(); 86 } 87 88 String getAddress(); 89 } 90 91 abstract static class AbstractPerson implements Person { 92 } 93 94 static class PersonImpl extends AbstractPerson { 95 public String name; 96 97 PersonImpl(String name) { 98 this.name = name; 99 } 100 101 @Override 102 public String getName() { 103 return name; 104 } 105 } 106 107 static class TenantImpl extends AbstractPerson implements Tenant { 108 public int id; 109 110 TenantImpl(int id) { 111 this.id = id; 112 } 113 114 @Override 115 public String getAddress() { 116 return String.valueOf(id); 117 } 118 } 119 }