Add additional tests for invokespecial to virtual members
Bug: 144450911
Change-Id: Ifb21f993a6086c213aa5170e793de5f18cf894f8
diff --git a/src/test/java/com/android/tools/r8/graph/InvokeSpecialForNonDeclaredInvokeVirtualTest.java b/src/test/java/com/android/tools/r8/graph/InvokeSpecialForNonDeclaredInvokeVirtualTest.java
new file mode 100644
index 0000000..0b13360
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/InvokeSpecialForNonDeclaredInvokeVirtualTest.java
@@ -0,0 +1,92 @@
+// Copyright (c) 2019, 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;
+
+import static org.hamcrest.CoreMatchers.anyOf;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
+import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRunResult;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+// This is a reproduction of b/144450911.
+@RunWith(Parameterized.class)
+public class InvokeSpecialForNonDeclaredInvokeVirtualTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public InvokeSpecialForNonDeclaredInvokeVirtualTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testRuntime() throws IOException, CompilationFailedException, ExecutionException {
+ TestRunResult<?> runResult =
+ testForRuntime(parameters.getRuntime(), parameters.getApiLevel())
+ .addProgramClasses(A.class, B.class, Main.class)
+ .addProgramClassFileData(getClassCWithTransformedInvoked())
+ .run(parameters.getRuntime(), Main.class);
+ // TODO(b/144450911): Remove when fixed.
+ if (parameters.isCfRuntime()) {
+ runResult.assertSuccessWithOutputLines("Hello World!");
+ } else {
+ runResult.assertFailureWithErrorThatMatches(
+ anyOf(
+ containsString("IncompatibleClassChangeError"),
+ containsString(
+ "com.android.tools.r8.graph.InvokeSpecialForInvokeVirtualTest$B.foo")));
+ }
+ }
+
+ private byte[] getClassCWithTransformedInvoked() throws IOException {
+ return transformer(C.class)
+ .transformMethodInsnInMethod(
+ "bar",
+ (opcode, owner, name, descriptor, isInterface, continuation) -> {
+ assertEquals(INVOKEVIRTUAL, opcode);
+ continuation.apply(INVOKESPECIAL, owner, name, descriptor, isInterface);
+ })
+ .transform();
+ }
+
+ public static class A {
+
+ void foo() {
+ System.out.println("Hello World!");
+ }
+ }
+
+ public static class B extends A {}
+
+ public static class C extends B {
+
+ void bar() {
+ foo();
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ new C().bar();
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/graph/InvokeSpecialInterfaceTest.java b/src/test/java/com/android/tools/r8/graph/InvokeSpecialInterfaceTest.java
new file mode 100644
index 0000000..bd97457
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/InvokeSpecialInterfaceTest.java
@@ -0,0 +1,93 @@
+// Copyright (c) 2019, 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;
+
+import static org.hamcrest.CoreMatchers.anyOf;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
+import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRunResult;
+import com.android.tools.r8.ToolHelper.DexVm;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+// This is a reproduction of b/144450911.
+@RunWith(Parameterized.class)
+public class InvokeSpecialInterfaceTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public InvokeSpecialInterfaceTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testRuntime() throws IOException, CompilationFailedException, ExecutionException {
+ TestRunResult<?> runResult =
+ testForRuntime(parameters.getRuntime(), parameters.getApiLevel())
+ .addProgramClasses(I.class, Main.class)
+ .addProgramClassFileData(getClassWithTransformedInvoked())
+ .run(parameters.getRuntime(), Main.class);
+ // TODO(b/144450911): Remove when fixed.
+ if (parameters.isCfRuntime()) {
+ runResult.assertSuccessWithOutputLines("Hello World!");
+ } else if (parameters.isDexRuntime()
+ && parameters.getRuntime().asDex().getVm().isOlderThanOrEqual(DexVm.ART_4_4_4_TARGET)) {
+ runResult.assertFailureWithErrorThatMatches(containsString("NoSuchMethodError"));
+ } else {
+ runResult.assertFailureWithErrorThatMatches(
+ anyOf(
+ containsString("IncompatibleClassChangeError"),
+ containsString(
+ "com.android.tools.r8.graph.InvokeSpecialForInvokeVirtualTest$B.foo")));
+ }
+ }
+
+ private byte[] getClassWithTransformedInvoked() throws IOException {
+ return transformer(B.class)
+ .transformMethodInsnInMethod(
+ "bar",
+ (opcode, owner, name, descriptor, isInterface, continuation) -> {
+ assertEquals(INVOKEVIRTUAL, opcode);
+ continuation.apply(INVOKESPECIAL, owner, name, descriptor, isInterface);
+ })
+ .transform();
+ }
+
+ public interface I {
+ default void foo() {
+ System.out.println("Hello World!");
+ }
+ }
+
+ public static class B implements I {
+
+ public void bar() {
+ foo(); // Will be rewritten to invoke-special I.foo()
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ new B().bar();
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/graph/InvokeSpecialInterfaceWithBridgeTest.java b/src/test/java/com/android/tools/r8/graph/InvokeSpecialInterfaceWithBridgeTest.java
new file mode 100644
index 0000000..62b5aba
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/InvokeSpecialInterfaceWithBridgeTest.java
@@ -0,0 +1,97 @@
+// Copyright (c) 2019, 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;
+
+import static org.hamcrest.CoreMatchers.anyOf;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
+import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRunResult;
+import com.android.tools.r8.ToolHelper.DexVm;
+import com.android.tools.r8.utils.DescriptorUtils;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+// This is a reproduction of b/144450911.
+@RunWith(Parameterized.class)
+public class InvokeSpecialInterfaceWithBridgeTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public InvokeSpecialInterfaceWithBridgeTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testRuntime() throws IOException, CompilationFailedException, ExecutionException {
+ TestRunResult<?> runResult =
+ testForRuntime(parameters.getRuntime(), parameters.getApiLevel())
+ .addProgramClasses(I.class, A.class, Main.class)
+ .addProgramClassFileData(getClassWithTransformedInvoked())
+ .run(parameters.getRuntime(), Main.class);
+ // TODO(b/144450911): Remove when fixed.
+ if (parameters.isCfRuntime()) {
+ runResult.assertSuccessWithOutputLines("Hello World!");
+ } else if (parameters.isDexRuntime()
+ && parameters.getRuntime().asDex().getVm().isOlderThanOrEqual(DexVm.ART_4_4_4_TARGET)) {
+ runResult.assertFailureWithErrorThatMatches(containsString("NoSuchMethodError"));
+ } else {
+ runResult.assertFailureWithErrorThatMatches(
+ anyOf(
+ containsString("IncompatibleClassChangeError"),
+ containsString(
+ "com.android.tools.r8.graph.InvokeSpecialForInvokeVirtualTest$B.foo")));
+ }
+ }
+
+ private byte[] getClassWithTransformedInvoked() throws IOException {
+ return transformer(B.class)
+ .transformMethodInsnInMethod(
+ "bar",
+ (opcode, owner, name, descriptor, isInterface, continuation) -> {
+ assertEquals(INVOKEVIRTUAL, opcode);
+ assertEquals(owner, DescriptorUtils.getBinaryNameFromJavaType(B.class.getTypeName()));
+ continuation.apply(INVOKESPECIAL, owner, name, descriptor, isInterface);
+ })
+ .transform();
+ }
+
+ public interface I {
+ default void foo() {
+ System.out.println("Hello World!");
+ }
+ }
+
+ public static class A implements I {}
+
+ public static class B extends A {
+
+ public void bar() {
+ foo(); // Will be rewritten to invoke-special A.foo()
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ new B().bar();
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/graph/InvokeSpecialMissingInvokeVirtualTest.java b/src/test/java/com/android/tools/r8/graph/InvokeSpecialMissingInvokeVirtualTest.java
new file mode 100644
index 0000000..59f8e99
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/graph/InvokeSpecialMissingInvokeVirtualTest.java
@@ -0,0 +1,82 @@
+// Copyright (c) 2019, 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;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
+import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRunResult;
+import com.android.tools.r8.utils.DescriptorUtils;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+// This is a reproduction of b/144450911.
+@RunWith(Parameterized.class)
+public class InvokeSpecialMissingInvokeVirtualTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public InvokeSpecialMissingInvokeVirtualTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testRuntime() throws IOException, CompilationFailedException, ExecutionException {
+ TestRunResult<?> runResult =
+ testForRuntime(parameters.getRuntime(), parameters.getApiLevel())
+ .addProgramClasses(A.class, Main.class)
+ .addProgramClassFileData(getClassWithTransformedInvoked())
+ .run(parameters.getRuntime(), Main.class);
+ runResult.assertFailureWithErrorThatMatches(containsString("NoSuchMethodError"));
+ }
+
+ private byte[] getClassWithTransformedInvoked() throws IOException {
+ return transformer(B.class)
+ .transformMethodInsnInMethod(
+ "bar",
+ (opcode, owner, name, descriptor, isInterface, continuation) -> {
+ assertEquals(INVOKEVIRTUAL, opcode);
+ assertEquals("notify", name);
+ continuation.apply(
+ INVOKESPECIAL,
+ DescriptorUtils.getBinaryNameFromJavaType(A.class.getTypeName()),
+ "foo",
+ descriptor,
+ isInterface);
+ })
+ .transform();
+ }
+
+ public static class A {}
+
+ public static class B extends A {
+
+ public void bar() {
+ notify(); // Will be rewritten to invoke-special A.foo() which is missing.
+ }
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ new B().bar();
+ }
+ }
+}