1 /*
   2  * Copyright (c) 2015, 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  * @test
  26  * @bug 8136421
  27  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9" | os.simpleArch == "aarch64")
  28  * @library / /testlibrary /test/lib
  29  * @library ../common/patches
  30  * @modules java.base/jdk.internal.org.objectweb.asm
  31  *          java.base/jdk.internal.org.objectweb.asm.tree
  32  *          jdk.vm.ci/jdk.vm.ci.hotspot
  33  *          jdk.vm.ci/jdk.vm.ci.code
  34  * @build jdk.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper
  35  * @build compiler.jvmci.compilerToVM.GetVtableIndexForInterfaceTest
  36  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  37  *                   compiler.jvmci.compilerToVM.GetVtableIndexForInterfaceTest
  38  */
  39 
  40 package compiler.jvmci.compilerToVM;
  41 
  42 import compiler.jvmci.common.testcases.AbstractClass;
  43 import compiler.jvmci.common.testcases.DoNotExtendClass;
  44 import compiler.jvmci.common.testcases.MultipleAbstractImplementer;
  45 import compiler.jvmci.common.testcases.MultipleImplementersInterface;
  46 import compiler.jvmci.common.testcases.MultipleImplementersInterfaceExtender;
  47 import compiler.jvmci.common.testcases.SingleImplementer;
  48 import compiler.jvmci.common.testcases.SingleImplementerInterface;
  49 import compiler.jvmci.common.testcases.SingleSubclass;
  50 import compiler.jvmci.common.testcases.SingleSubclassedClass;
  51 import compiler.jvmci.common.CTVMUtilities;
  52 import compiler.jvmci.common.testcases.AnotherSingleImplementer;
  53 import compiler.jvmci.common.testcases.AnotherSingleImplementerInterface;
  54 import java.lang.reflect.Method;
  55 import java.util.HashSet;
  56 import java.util.Set;
  57 import java.util.stream.Stream;
  58 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  59 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  60 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  61 import jdk.test.lib.Asserts;
  62 import jdk.test.lib.Utils;
  63 
  64 public class GetVtableIndexForInterfaceTest {
  65     private static final int INVALID_VTABLE_INDEX = -4; // see method.hpp: VtableIndexFlag
  66 
  67     public static void main(String args[]) {
  68         GetVtableIndexForInterfaceTest test
  69                 = new GetVtableIndexForInterfaceTest();
  70         try {
  71             for (TestCase tcase : createTestCases()) {
  72                 test.runTest(tcase);
  73             }
  74         } catch (NoSuchMethodException e) {
  75             throw new Error("TEST BUG: can't find requested method", e);
  76         }
  77     }
  78 
  79     private static Set<TestCase> createTestCases() {
  80         Set<TestCase> result = new HashSet<>();
  81         Stream.of(
  82                     AbstractClass.class,
  83                     SingleImplementer.class,
  84                     SingleImplementerInterface.class,
  85                     MultipleImplementersInterface.class,
  86                     MultipleImplementersInterfaceExtender.class,
  87                     SingleSubclass.class,
  88                     SingleSubclassedClass.class,
  89                     DoNotExtendClass.class,
  90                     MultipleAbstractImplementer.class
  91                 )
  92                 .forEach(Utils::ensureClassIsLoaded);
  93         // non iface method
  94         result.add(new TestCase(SingleImplementer.class,
  95                 SingleImplementer.class, "nonInterfaceMethod",
  96                 false, InternalError.class));
  97         // iface method w/o default implementation
  98         result.add(new TestCase(SingleImplementer.class,
  99                 SingleImplementerInterface.class, "interfaceMethod", false));
 100         /* another iface which provides default implementation for the
 101            original iface*/
 102         result.add(new TestCase(MultipleImplementersInterfaceExtender.class,
 103                 MultipleImplementersInterface.class, "testMethod", false,
 104                 InternalError.class));
 105         // iface method w/ default implementation
 106         result.add(new TestCase(SingleImplementer.class,
 107                 SingleImplementerInterface.class, "defaultMethod", true));
 108         // non iface class
 109         result.add(new TestCase(SingleSubclass.class,
 110                 SingleSubclassedClass.class, "inheritedMethod", false,
 111                 InternalError.class));
 112         // class not implementing iface
 113         result.add(new TestCase(DoNotExtendClass.class,
 114                 SingleImplementerInterface.class, "defaultMethod", false));
 115         // abstract class which doesn't implement iface
 116         result.add(new TestCase(AbstractClass.class,
 117                 SingleImplementerInterface.class, "defaultMethod", false));
 118         // abstract class which implements iface
 119         result.add(new TestCase(MultipleAbstractImplementer.class,
 120                 MultipleImplementersInterface.class, "defaultMethod", true));
 121         // class not initialized
 122         result.add(new TestCase(AnotherSingleImplementer.class,
 123                 AnotherSingleImplementerInterface.class, "defaultMethod",
 124                 false, InternalError.class));
 125         return result;
 126     }
 127 
 128     private void runTest(TestCase tcase) throws NoSuchMethodException {
 129         System.out.println(tcase);
 130         Method method = tcase.holder.getDeclaredMethod(tcase.methodName);
 131         HotSpotResolvedObjectType metaspaceKlass = CompilerToVMHelper
 132                 .lookupType(Utils.toJVMTypeSignature(tcase.receiver),
 133                         getClass(), /* resolve = */ true);
 134         HotSpotResolvedJavaMethod metaspaceMethod = CTVMUtilities
 135                 .getResolvedMethod(tcase.holder, method);
 136         int index = 0;
 137         try {
 138             index = CompilerToVMHelper
 139                     .getVtableIndexForInterfaceMethod(metaspaceKlass,
 140                             metaspaceMethod);
 141         } catch (Throwable t) {
 142             if (tcase.isPositive || tcase.expectedException == null) {
 143                 throw new Error("Caught unexpected exception " + t);
 144             }
 145             if (!tcase.expectedException.equals(t.getClass())) {
 146                 throw new Error(String.format("Caught %s while expected %s",
 147                         t.getClass().getName(),
 148                         tcase.expectedException.getName()));
 149             }
 150             return;
 151         }
 152         if (tcase.expectedException != null) {
 153             throw new AssertionError("Expected exception wasn't caught: "
 154                     + tcase.expectedException.getName());
 155         }
 156         if (tcase.isPositive) {
 157             Asserts.assertNE(index, INVALID_VTABLE_INDEX,
 158                     "Unexpected: got invalid index");
 159         } else {
 160             Asserts.assertEQ(index, INVALID_VTABLE_INDEX,
 161                     "Unexpected: got valid index ");
 162         }
 163     }
 164 
 165     private static class TestCase {
 166         public final Class<?> receiver;
 167         public final Class<?> holder;
 168         public final String methodName;
 169         public final boolean isPositive;
 170         public final Class<? extends Throwable> expectedException;
 171 
 172         public TestCase(Class<?> receiver, Class<?> holder, String methodName,
 173                 boolean isPositive,
 174                 Class<? extends Throwable> expectedException) {
 175             this.receiver = receiver;
 176             this.holder = holder;
 177             this.methodName = methodName;
 178             this.isPositive = isPositive;
 179             this.expectedException = expectedException;
 180         }
 181 
 182         public TestCase(Class<?> receiver, Class<?> holder, String methodName,
 183                 boolean isPositive) {
 184             this(receiver, holder, methodName, isPositive, null);
 185         }
 186 
 187         @Override
 188         public String toString() {
 189             return String.format("CASE: receiver=%s, holder=%s, method=%s,"
 190                     + " isPositive=%s%n", receiver.getName(), holder.getName(),
 191                     methodName, isPositive);
 192         }
 193     }
 194 }