Add regression test for unsupported invoke-special

Bug: b/313065227
Change-Id: I4d6237aa7a76ea5de5bb3c0259272ab624a2484d
diff --git a/src/test/java/com/android/tools/r8/graph/invokespecial/InvokeSpecialToImmediateInterfaceTest.java b/src/test/java/com/android/tools/r8/graph/invokespecial/InvokeSpecialToImmediateInterfaceTest.java
new file mode 100644
index 0000000..15f1e9c
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/invokespecial/InvokeSpecialToImmediateInterfaceTest.java
@@ -0,0 +1,129 @@
+// Copyright (c) 2023, 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.graph.invokespecial;
+
+import static com.android.tools.r8.utils.DescriptorUtils.getBinaryNameFromJavaType;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
+import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
+
+import com.android.tools.r8.R8TestCompileResult;
+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.utils.Box;
+import com.android.tools.r8.utils.codeinspector.AssertUtils;
+import java.io.IOException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class InvokeSpecialToImmediateInterfaceTest extends TestBase {
+
+  @Parameter(0)
+  public TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  @Test
+  public void testJvm() throws Exception {
+    parameters.assumeJvmTestParameters();
+    testForJvm(parameters)
+        .addProgramClasses(I.class, Main.class)
+        .addProgramClassFileData(getClassWithTransformedInvoked())
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines("Hello, world!");
+  }
+
+  @Test
+  public void testD8Release() throws Exception {
+    parameters.assumeDexRuntime();
+    testForD8(parameters.getBackend())
+        .addProgramClasses(I.class, Main.class)
+        .addProgramClassFileData(getClassWithTransformedInvoked())
+        .release()
+        .setMinApi(parameters)
+        .run(parameters.getRuntime(), Main.class)
+        // TODO(b/313065227): Should succeed.
+        .applyIf(
+            parameters.getDexRuntimeVersion().isEqualToOneOf(Version.V5_1_1, Version.V6_0_1),
+            runResult -> runResult.assertFailureWithErrorThatMatches(containsString("SIGSEGV")),
+            runResult -> runResult.assertFailureWithErrorThatThrows(NoSuchMethodError.class));
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    Box<R8TestCompileResult> compileResult = new Box<>();
+
+    // TODO(b/313065227): Should succeed.
+    AssertUtils.assertFailsCompilationIf(
+        parameters.isCfRuntime(),
+        () ->
+            testForR8(parameters.getBackend())
+                .addProgramClasses(I.class, Main.class)
+                .addProgramClassFileData(getClassWithTransformedInvoked())
+                .addKeepMainRule(Main.class)
+                .setMinApi(parameters)
+                .compile()
+                .apply(compileResult::set));
+
+    if (!compileResult.isSet()) {
+      assertTrue(parameters.isCfRuntime());
+      return;
+    }
+
+    // TODO(b/313065227): Should succeed.
+    compileResult
+        .get()
+        .run(parameters.getRuntime(), Main.class)
+        .assertFailureWithErrorThatThrows(NullPointerException.class);
+  }
+
+  private byte[] getClassWithTransformedInvoked() throws IOException {
+    return transformer(A.class)
+        .transformMethodInsnInMethod(
+            "bar",
+            (opcode, owner, name, descriptor, isInterface, continuation) -> {
+              assertEquals(INVOKEVIRTUAL, opcode);
+              assertEquals(owner, getBinaryNameFromJavaType(A.class.getTypeName()));
+              continuation.visitMethodInsn(
+                  INVOKESPECIAL,
+                  getBinaryNameFromJavaType(A.class.getTypeName()),
+                  name,
+                  descriptor,
+                  isInterface);
+            })
+        .transform();
+  }
+
+  public interface I {
+
+    default void foo() {
+      System.out.println("Hello, world!");
+    }
+  }
+
+  public static class A implements I {
+
+    public void bar() {
+      foo(); // Will be rewritten to invoke-special A.foo()
+    }
+  }
+
+  public static class Main {
+
+    public static void main(String[] args) {
+      new A().bar();
+    }
+  }
+}