Abort member rebinding on missing library class definition.
Bug: 149890887
Change-Id: I0c373c51364f07544dd67a5e23646f4d41da9b3f
diff --git a/src/main/java/com/android/tools/r8/graph/DexMember.java b/src/main/java/com/android/tools/r8/graph/DexMember.java
index 96afd57..d04808b 100644
--- a/src/main/java/com/android/tools/r8/graph/DexMember.java
+++ b/src/main/java/com/android/tools/r8/graph/DexMember.java
@@ -9,6 +9,7 @@
public final DexType holder;
public DexMember(DexType holder) {
+ assert holder != null;
this.holder = holder;
}
diff --git a/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java b/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java
index 703deea..4d20f6b 100644
--- a/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java
+++ b/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java
@@ -53,7 +53,9 @@
} else {
newHolder = firstLibraryClass(target.holder, original.holder);
}
- return appView.dexItemFactory().createMethod(newHolder, original.proto, original.name);
+ return newHolder == null
+ ? original
+ : appView.dexItemFactory().createMethod(newHolder, original.proto, original.name);
}
private DexField validTargetFor(DexField target, DexField original,
@@ -69,12 +71,17 @@
} else {
newHolder = firstLibraryClass(target.holder, original.holder);
}
- return appView.dexItemFactory().createField(newHolder, original.type, original.name);
+ return newHolder == null
+ ? original
+ : appView.dexItemFactory().createField(newHolder, original.type, original.name);
}
private <T> DexType firstLibraryClassForInterfaceTarget(T target, DexType current,
BiFunction<DexClass, T, ?> lookup) {
DexClass clazz = appView.definitionFor(current);
+ if (clazz == null) {
+ return null;
+ }
Object potential = lookup.apply(clazz, target);
if (potential != null) {
// Found, return type.
diff --git a/src/test/java/com/android/tools/r8/TestBase.java b/src/test/java/com/android/tools/r8/TestBase.java
index 12bc06c..34b7305 100644
--- a/src/test/java/com/android/tools/r8/TestBase.java
+++ b/src/test/java/com/android/tools/r8/TestBase.java
@@ -1424,7 +1424,7 @@
return JarBuilder.builder(temp);
}
- public Collection<Path> buildOnDexRuntime(TestParameters parameters, Collection<Path> paths)
+ public List<Path> buildOnDexRuntime(TestParameters parameters, List<Path> paths)
throws CompilationFailedException, IOException {
if (parameters.isCfRuntime()) {
return paths;
@@ -1437,7 +1437,7 @@
.writeToZip());
}
- public Collection<Path> buildOnDexRuntime(TestParameters parameters, Path... paths)
+ public List<Path> buildOnDexRuntime(TestParameters parameters, Path... paths)
throws IOException, CompilationFailedException {
return buildOnDexRuntime(parameters, Arrays.asList(paths));
}
diff --git a/src/test/java/com/android/tools/r8/regress/b149890887/MissingLibraryTargetTest.java b/src/test/java/com/android/tools/r8/regress/b149890887/MissingLibraryTargetTest.java
new file mode 100644
index 0000000..58e43469
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/regress/b149890887/MissingLibraryTargetTest.java
@@ -0,0 +1,106 @@
+// 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.regress.b149890887;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import com.android.tools.r8.utils.StringUtils;
+import com.google.common.collect.ImmutableList;
+import java.nio.file.Path;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class MissingLibraryTargetTest extends TestBase {
+
+ static final String EXPECTED = StringUtils.lines("A::foo");
+
+ private final TestParameters parameters;
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimes().withAllApiLevels().build();
+ }
+
+ public MissingLibraryTargetTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ private static Class<?> MAIN = TestClass.class;
+ private static List<Class<?>> PROGRAM = ImmutableList.of(MAIN, DerivedProgramClass.class);
+ private static List<Class<?>> LIBRARY =
+ ImmutableList.of(PresentLibraryInterface.class, PresentLibraryClass.class);
+
+ private List<Path> runtimeClasspath() throws Exception {
+ return buildOnDexRuntime(
+ parameters,
+ jarTestClasses(
+ ImmutableList.<Class<?>>builder()
+ .addAll(LIBRARY)
+ .add(MissingLibraryClass.class)
+ .build()));
+ }
+
+ private boolean isDesugaring() {
+ return parameters.isDexRuntime() && parameters.getApiLevel().isLessThan(AndroidApiLevel.N);
+ }
+
+ @Test
+ public void testReference() throws Exception {
+ testForRuntime(parameters)
+ .addProgramClasses(PROGRAM)
+ .addRunClasspathFiles(runtimeClasspath())
+ .run(parameters.getRuntime(), MAIN)
+ .assertSuccessWithOutput(EXPECTED);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ testForR8(parameters.getBackend())
+ .allowDiagnosticWarningMessages(isDesugaring())
+ .addProgramClasses(PROGRAM)
+ .addKeepMainRule(MAIN)
+ .addClasspathClasses(LIBRARY)
+ .setMinApi(parameters.getApiLevel())
+ .addKeepRules("-dontwarn")
+ .compile()
+ .addRunClasspathFiles(runtimeClasspath())
+ .run(parameters.getRuntime(), MAIN)
+ .assertSuccessWithOutput(EXPECTED);
+ }
+
+ // Non-present class with the actual definition.
+ static class MissingLibraryClass {
+ public void foo() {
+ System.out.println("A::foo");
+ }
+ }
+
+ // Present library interface declaring the method. This is needed to hit the member rebinding
+ // issue. If not here the initial resolution will fail and no search will be done.
+ interface PresentLibraryInterface {
+ void foo();
+ }
+
+ // Present library class to trigger the search for the "first library definition".
+ static class PresentLibraryClass extends MissingLibraryClass implements PresentLibraryInterface {
+ // Intentionally empty.
+ }
+
+ // Program type that needs to be targeted to initiate rebinding.
+ static class DerivedProgramClass extends PresentLibraryClass {
+ // Intentionally empty.
+ }
+
+ static class TestClass {
+
+ public static void main(String[] args) {
+ new DerivedProgramClass().foo();
+ }
+ }
+}