Version 1.5.26
Cherry pick: Add test for reusing names from dictionary when minifying
CL: https://r8-review.googlesource.com/c/r8/+/38366
Cherry pick: Reserve minified names to ensure they are not reused by
minifier
CL: https://r8-review.googlesource.com/c/r8/+/38367
Bug: 132612059
Change-Id: I415866301417e9b9e0aa00b0a5aca85ec9197a48
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index 12d6f8a..a4d3221 100644
--- a/src/main/java/com/android/tools/r8/Version.java
+++ b/src/main/java/com/android/tools/r8/Version.java
@@ -11,7 +11,7 @@
// This field is accessed from release scripts using simple pattern matching.
// Therefore, changing this field could break our release scripts.
- public static final String LABEL = "1.5.25";
+ public static final String LABEL = "1.5.26";
private Version() {
}
diff --git a/src/main/java/com/android/tools/r8/naming/Minifier.java b/src/main/java/com/android/tools/r8/naming/Minifier.java
index 659c12e..4bdf87c 100644
--- a/src/main/java/com/android/tools/r8/naming/Minifier.java
+++ b/src/main/java/com/android/tools/r8/naming/Minifier.java
@@ -24,6 +24,7 @@
import com.android.tools.r8.utils.Timing;
import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
@@ -141,7 +142,10 @@
static class MinifierMemberNamingStrategy implements MemberNamingStrategy {
private final DexItemFactory factory;
+ // We have to ensure that the names proposed by the minifier is not used in the obfuscation
+ // dictionary. We use a list for direct indexing based on a number and a set for looking up.
private final List<String> obfuscationDictionary;
+ private final Set<String> obfuscationDictionaryForLookup;
private final AppView<?> appView;
@@ -150,6 +154,7 @@
this.factory = appView.dexItemFactory();
this.obfuscationDictionary =
appView.options().getProguardConfiguration().getObfuscationDictionary();
+ this.obfuscationDictionaryForLookup = new HashSet<>(this.obfuscationDictionary);
assert this.obfuscationDictionary != null;
}
@@ -171,8 +176,12 @@
return factory.createString(
obfuscationDictionary.get(internalState.incrementDictionaryIndex()));
} else {
- int counter = internalState.incrementNameIndex(isDirectOrStatic);
- return factory.createString(StringUtils.numberToIdentifier(counter));
+ String nextString;
+ do {
+ int counter = internalState.incrementNameIndex(isDirectOrStatic);
+ nextString = StringUtils.numberToIdentifier(counter);
+ } while (obfuscationDictionaryForLookup.contains(nextString));
+ return factory.createString(nextString);
}
}
diff --git a/src/test/java/com/android/tools/r8/naming/FieldMinificationObfuscationDictionaryDuplicateTest.java b/src/test/java/com/android/tools/r8/naming/FieldMinificationObfuscationDictionaryDuplicateTest.java
new file mode 100644
index 0000000..9d0b506
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/FieldMinificationObfuscationDictionaryDuplicateTest.java
@@ -0,0 +1,56 @@
+// 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.naming;
+
+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.utils.FileUtils;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/** This is a reproduction of b/132612059. */
+@RunWith(Parameterized.class)
+public class FieldMinificationObfuscationDictionaryDuplicateTest extends TestBase {
+
+ public static class A {
+ public static String field1 = "HELLO ";
+ public static String field2 = "WORLD!";
+
+ public static void main(String[] args) {
+ System.out.print(field1 + field2);
+ }
+ }
+
+ private final TestParameters parameters;
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimes().build();
+ }
+
+ public FieldMinificationObfuscationDictionaryDuplicateTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void test() throws IOException, CompilationFailedException, ExecutionException {
+ Path dictionary = temp.getRoot().toPath().resolve("dictionary.txt");
+ FileUtils.writeTextFile(dictionary, "a");
+ testForR8(parameters.getBackend())
+ .addProgramClasses(A.class)
+ .noTreeShaking()
+ .addKeepRules("-obfuscationdictionary " + dictionary.toString())
+ .addKeepMainRule(A.class)
+ .setMinApi(parameters.getRuntime())
+ .run(parameters.getRuntime(), A.class)
+ .assertSuccessWithOutput("HELLO WORLD!");
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/naming/FieldNamingObfuscationDictionaryTest.java b/src/test/java/com/android/tools/r8/naming/FieldNamingObfuscationDictionaryTest.java
index 9cb616a..697c8b2 100644
--- a/src/test/java/com/android/tools/r8/naming/FieldNamingObfuscationDictionaryTest.java
+++ b/src/test/java/com/android/tools/r8/naming/FieldNamingObfuscationDictionaryTest.java
@@ -15,7 +15,6 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.ExecutionException;
-import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -95,7 +94,6 @@
.addInnerClasses(FieldNamingObfuscationDictionaryTest.class)
.enableInliningAnnotations()
.addKeepRules("-overloadaggressively", "-obfuscationdictionary " + dictionary.toString())
- .addKeepRules()
.addKeepMainRule(Runner.class)
.setMinApi(parameters.getRuntime())
.compile()