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