Regression test for invalid signature polymorphic parameter check.

This also adds a test for the now fixed issue in ART b/115964401.

Bug: 116283747
Change-Id: I3764af10e415745c061c976ac2bb2ce20202ef6d
diff --git a/src/test/examplesAndroidP/invokecustom/InvokeCustom.java b/src/test/examplesAndroidP/invokecustom/InvokeCustom.java
index c3cb86f..76fed75 100644
--- a/src/test/examplesAndroidP/invokecustom/InvokeCustom.java
+++ b/src/test/examplesAndroidP/invokecustom/InvokeCustom.java
@@ -35,8 +35,6 @@
   }
 }
 
-// TODO(116283747): Add the same test where the interface method is not overriden but inherited
-// from the interface. Currently, that works on the reference implementation but fails on Art.
 class Impl implements I2 {
   @Override
   public ReturnType targetMethodTest5(ArgumentType arg) {
diff --git a/src/test/java/com/android/tools/r8/TestBase.java b/src/test/java/com/android/tools/r8/TestBase.java
index 2ccb378..1fda6a4 100644
--- a/src/test/java/com/android/tools/r8/TestBase.java
+++ b/src/test/java/com/android/tools/r8/TestBase.java
@@ -1776,6 +1776,10 @@
     return AndroidApiLevel.O;
   }
 
+  public static AndroidApiLevel apiLevelWithConstMethodHandleSupport() {
+    return AndroidApiLevel.P;
+  }
+
   public static AndroidApiLevel apiLevelWithNativeMultiDexSupport() {
     return AndroidApiLevel.L;
   }
diff --git a/src/test/java/com/android/tools/r8/resolution/InvokeCustomOnNonOverriddenInterfaceMethodTest.java b/src/test/java/com/android/tools/r8/resolution/InvokeCustomOnNonOverriddenInterfaceMethodTest.java
new file mode 100644
index 0000000..eaf6a35
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/resolution/InvokeCustomOnNonOverriddenInterfaceMethodTest.java
@@ -0,0 +1,115 @@
+// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.resolution;
+
+import static com.android.tools.r8.references.Reference.classFromClass;
+import static com.android.tools.r8.references.Reference.methodFromMethod;
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.SingleTestRunResult;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.ToolHelper.DexVm.Version;
+import com.android.tools.r8.references.ClassReference;
+import com.android.tools.r8.references.MethodReference;
+import com.android.tools.r8.utils.structural.Ordered;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.WrongMethodTypeException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+import org.objectweb.asm.Handle;
+import org.objectweb.asm.Opcodes;
+
+/** Regression for b/116283747. */
+@RunWith(Parameterized.class)
+public class InvokeCustomOnNonOverriddenInterfaceMethodTest extends TestBase {
+
+  private static final String[] EXPECTED = new String[] {"I.target"};
+
+  private final TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters()
+        .withAllRuntimes()
+        .withApiLevelsStartingAtIncluding(
+            Ordered.max(apiLevelWithInvokeCustomSupport(), apiLevelWithConstMethodHandleSupport()))
+        .build();
+  }
+
+  public InvokeCustomOnNonOverriddenInterfaceMethodTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testRuntime() throws Throwable {
+    testForRuntime(parameters)
+        .addProgramClasses(Main.class, I.class, Super.class)
+        .addProgramClassFileData(getInvokeCustomTransform())
+        .run(parameters.getRuntime(), Main.class)
+        .apply(this::checkRunResult);
+  }
+
+  private void checkRunResult(SingleTestRunResult<?> result) {
+    if (parameters.isCfRuntime()
+        || parameters.asDexRuntime().getVersion().isNewerThanOrEqual(Version.V10_0_0)) {
+      result.assertSuccessWithOutputLines(EXPECTED);
+    } else {
+      // Fails due to b/115964401.
+      assertEquals(Version.V9_0_0, parameters.getDexRuntimeVersion());
+      result.assertFailureWithErrorThatThrows(WrongMethodTypeException.class);
+    }
+  }
+
+  private static byte[] getInvokeCustomTransform() throws Throwable {
+    ClassReference symbolicHolder = classFromClass(InvokeCustom.class);
+    MethodReference method = methodFromMethod(InvokeCustom.class.getMethod("target"));
+    return transformer(InvokeCustom.class)
+        .transformMethodInsnInMethod(
+            "test",
+            (opcode, owner, name, descriptor, isInterface, visitor) -> {
+              // Replace null argument by a const method handle.
+              visitor.visitInsn(Opcodes.POP);
+              visitor.visitLdcInsn(
+                  new Handle(
+                      Opcodes.H_INVOKEVIRTUAL,
+                      symbolicHolder.getBinaryName(),
+                      method.getMethodName(),
+                      method.getMethodDescriptor(),
+                      false));
+              visitor.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
+            })
+        .transform();
+  }
+
+  interface I {
+    default void target() {
+      System.out.println("I.target");
+    }
+  }
+
+  static class Super implements I {}
+
+  static class InvokeCustom extends Super {
+
+    public static void doInvokeExact(MethodHandle handle) throws Throwable {
+      handle.invokeExact(new InvokeCustom());
+    }
+
+    public static void test() throws Throwable {
+      doInvokeExact(null /* will be const method handle */);
+    }
+  }
+
+  static class Main {
+
+    public static void main(String[] args) throws Throwable {
+      InvokeCustom.test();
+    }
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/resolution/InvokePolymorphicResolutionTest.java b/src/test/java/com/android/tools/r8/resolution/InvokePolymorphicResolutionTest.java
new file mode 100644
index 0000000..1b0af62
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/resolution/InvokePolymorphicResolutionTest.java
@@ -0,0 +1,70 @@
+// Copyright (c) 2021, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.resolution;
+
+import static com.android.tools.r8.references.Reference.classFromClass;
+import static com.android.tools.r8.references.Reference.methodFromMethod;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import com.android.tools.r8.TestAppViewBuilder;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.graph.AppInfoWithClassHierarchy;
+import com.android.tools.r8.graph.AppView;
+import com.android.tools.r8.graph.MethodResolutionResult;
+import com.android.tools.r8.references.MethodReference;
+import com.android.tools.r8.references.Reference;
+import java.lang.invoke.MethodHandle;
+import java.util.Collections;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class InvokePolymorphicResolutionTest extends TestBase {
+
+  @Parameterized.Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withNoneRuntime().build();
+  }
+
+  public InvokePolymorphicResolutionTest(TestParameters parameters) {
+    parameters.assertNoneRuntime();
+  }
+
+  @Test
+  public void testResolution() throws Exception {
+    // Note: this could just as well resolve without liveness.
+    AppView<? extends AppInfoWithClassHierarchy> appView =
+        TestAppViewBuilder.builder()
+            .addLibraryFiles(ToolHelper.getJava8RuntimeJar())
+            .buildWithLiveness();
+
+    // An exact resolution will find invokeExact.
+    MethodReference invokeExact =
+        methodFromMethod(MethodHandle.class.getMethod("invokeExact", Object[].class));
+    MethodResolutionResult resolution1 =
+        appView.appInfo().resolveMethod(buildMethod(invokeExact, appView.dexItemFactory()), false);
+    assertFalse(resolution1.isFailedResolution());
+
+    // An inexact signature should also find invokeExact.
+    MethodReference inexactInvokeExact =
+        Reference.method(
+            invokeExact.getHolderClass(),
+            invokeExact.getMethodName(),
+            Collections.singletonList(Reference.array(classFromClass(getClass()), 1)),
+            invokeExact.getReturnType());
+    MethodResolutionResult resolution2 =
+        appView
+            .appInfo()
+            .resolveMethod(buildMethod(inexactInvokeExact, appView.dexItemFactory()), false);
+    assertFalse(resolution2.isFailedResolution());
+
+    // The should both be the same MethodHandle.invokeExact method.
+    assertEquals(resolution1.getSingleTarget(), resolution2.getSingleTarget());
+  }
+}