Reproduce IllegalAccessError from bridge removal
Bug: b/274724134
Change-Id: I9b33840038c2ab8c4255a8eeebc4824a1efc65ff
diff --git a/src/test/java/com/android/tools/r8/TestParameters.java b/src/test/java/com/android/tools/r8/TestParameters.java
index b484959..0cb9b51 100644
--- a/src/test/java/com/android/tools/r8/TestParameters.java
+++ b/src/test/java/com/android/tools/r8/TestParameters.java
@@ -5,6 +5,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
import com.android.tools.r8.TestBase.Backend;
import com.android.tools.r8.TestRuntime.CfRuntime;
@@ -164,6 +165,34 @@
assertEquals(NoneRuntime.getInstance(), runtime);
}
+ public TestParameters assumeCfRuntime() {
+ assumeTrue(isCfRuntime());
+ return this;
+ }
+
+ public TestParameters assumeDexRuntime() {
+ assumeTrue(isDexRuntime());
+ return this;
+ }
+
+ public TestParameters assumeJvmTestParameters() {
+ assumeCfRuntime();
+ return this;
+ }
+
+ public TestParameters assumeProguardTestParameters() {
+ assumeCfRuntime();
+ return this;
+ }
+
+ public TestParameters assumeR8TestParameters() {
+ return this;
+ }
+
+ public TestParameters assumeRuntimeTestParameters() {
+ return this;
+ }
+
public DexVm.Version getDexRuntimeVersion() {
assertTrue(isDexRuntime());
return getRuntime().asDex().getVm().getVersion();
diff --git a/src/test/java/com/android/tools/r8/bridgeremoval/ProtectedBridgeRemovalTest.java b/src/test/java/com/android/tools/r8/bridgeremoval/ProtectedBridgeRemovalTest.java
new file mode 100644
index 0000000..737924d
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/bridgeremoval/ProtectedBridgeRemovalTest.java
@@ -0,0 +1,111 @@
+// 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.bridgeremoval;
+
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NoVerticalClassMerging;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.StringUtils;
+import com.google.common.collect.ImmutableList;
+import java.util.List;
+import org.junit.BeforeClass;
+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 ProtectedBridgeRemovalTest extends TestBase {
+
+ private static final String A_DESCRIPTOR = "LA;";
+ private static final String EXPECTED_OUTPUT = StringUtils.lines("IllegalAccessError", "A.foo()");
+
+ private static List<byte[]> programClassFileData;
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ @BeforeClass
+ public static void setup() throws Exception {
+ programClassFileData =
+ ImmutableList.of(
+ transformer(Main.class)
+ .replaceClassDescriptorInMethodInstructions(descriptor(A.class), A_DESCRIPTOR)
+ .transform(),
+ transformer(A.class).setClassDescriptor(A_DESCRIPTOR).transform(),
+ transformer(B.class)
+ .replaceClassDescriptorInMethodInstructions(descriptor(A.class), A_DESCRIPTOR)
+ .setBridge(B.class.getDeclaredMethod("foo"))
+ .setSuper(A_DESCRIPTOR)
+ .transform());
+ }
+
+ @Test
+ public void testJvm() throws Exception {
+ parameters.assumeJvmTestParameters();
+ testForJvm(parameters)
+ .addProgramClassFileData(programClassFileData)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .addProgramClassFileData(programClassFileData)
+ .addKeepMainRule(Main.class)
+ .enableInliningAnnotations()
+ .enableNoVerticalClassMergingAnnotations()
+ .setMinApi(parameters)
+ .compile()
+ .run(parameters.getRuntime(), Main.class)
+ // TODO(b/274724134): Should not fail.
+ .assertFailureWithErrorThatThrows(IllegalAccessError.class);
+ }
+
+ public static class Main {
+
+ public static void main(String[] args) {
+ for (A a : new A[] {new A(), new B()}) {
+ if (a instanceof B) {
+ B b = (B) a;
+ b.foo();
+ } else {
+ try {
+ a.foo();
+ } catch (IllegalAccessError e) {
+ System.out.println("IllegalAccessError");
+ }
+ }
+ }
+ }
+ }
+
+ @NoVerticalClassMerging
+ public static class /*other package.*/ A {
+
+ @NeverInline
+ protected void foo() {
+ System.out.println("A.foo()");
+ }
+ }
+
+ public static class B extends A {
+
+ @NeverInline
+ @Override
+ protected /*bridge*/ void foo() {
+ super.foo();
+ }
+ }
+}