Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2074,6 +2074,15 @@ private void createAntProject() throws IOException, LibraryPropertiesException,
writeStringToFile(stagedInputsFile, stagedInputs);
}

// Dependencies are merged as they are; the bytecode-compliance goal
// rewrote only this module's classes. See rewriteInvocationsInJar. Run
// on a reused jar too, which a plugin without this step may have
// staged; on an already rewritten jar it reads and writes nothing.
int rewritten = BytecodeCompliance.rewriteInvocationsInJar(jarWithDependencies);
if (rewritten > 0) {
getLog().info("Rewrote " + rewritten + " JDK API callsite(s) in dependencies of " + jarWithDependencies.getName());
}

verifyApplicationClassClosure(jarWithDependencies, cpElements);

if (stageOnly) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,63 @@ static byte[] rewriteInvocations(byte[] classBytes) {
return rewriteClassInvocations(classBytes, INVOCATION_REWRITE_RULES).bytes;
}

/// Applies the invocation rewrites to every class in `jar`, in place, and
/// answers how many callsites were redirected.
///
/// The goal itself rewrites only the module's own output directory. A
/// dependency jar is merged into the staged jar as it is, so a library
/// calling `String.replaceAll` reached the iOS translator unchanged and
/// became a call to `virtual_java_lang_String_replaceAll...`, a function
/// vm/JavaAPI never defines: a green build and an Xcode compile error
/// (issue #6363). The staged jar is everything that ships, so rewriting it
/// covers dependencies and cn1libs built before a rule existed alike. The
/// rewrite is idempotent; classes the goal already rewrote are unchanged.
public static int rewriteInvocationsInJar(File jar) throws IOException {
File rewritten = new File(jar.getPath() + ".rewrite.tmp");
int callsites = 0;
// Read in physical order and write with Ant's stream: the staged jar is
// merged by Ant's zip task, which keeps duplicate entries (one per
// input jar that carries, say, META-INF/cn1/orm-enhanced-dependencies.list),
// and java.util.zip.ZipOutputStream refuses a duplicate name. Every
// entry is copied as it is, duplicates included, so the jar the
// builder reads differs only in the rewritten classes.
java.util.zip.ZipInputStream in = new java.util.zip.ZipInputStream(
new java.io.BufferedInputStream(new java.io.FileInputStream(jar)));
try {
org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(rewritten);
try {
java.util.zip.ZipEntry entry;
while ((entry = in.getNextEntry()) != null) {
byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(in);
if (!entry.isDirectory() && entry.getName().endsWith(".class")
&& !entry.getName().startsWith("META-INF/")) {
InvocationRewriteResult result = rewriteClassInvocations(bytes, INVOCATION_REWRITE_RULES);
if (result.rewrittenCallsites > 0) {
bytes = result.bytes;
callsites += result.rewrittenCallsites;
}
}
org.apache.tools.zip.ZipEntry copy = new org.apache.tools.zip.ZipEntry(entry.getName());
copy.setTime(entry.getTime());
out.putNextEntry(copy);
out.write(bytes);
out.closeEntry();
}
} finally {
out.close();
}
} finally {
in.close();
}
if (callsites == 0) {
FileUtils.deleteQuietly(rewritten);
return 0;
}
FileUtils.deleteQuietly(jar);
FileUtils.moveFile(rewritten, jar);
return callsites;
}

private static InvocationRewriteResult rewriteClassInvocations(byte[] classBytes,
final Map<MethodRef, MethodRef> rules) {
final InvocationRewriteResult result = new InvocationRewriteResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,62 @@ void backendMovesRegularExpressionsOntoTheSharedEngine(@TempDir Path dir) throws
org.junit.jupiter.api.Assertions.assertTrue(rewritten.contains(r + "StringRegex"));
}

/// Issue #6363: a dependency jar calling String.replaceAll was merged into
/// the staged jar unrewritten and reached the iOS translator as a call to a
/// method vm/JavaAPI does not have.
@Test
public void stagedJarDependencyRegexCallsAreRewritten(@TempDir Path dir) throws Exception {
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "dep/Squash", null, "java/lang/Object", null);
MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "sq",
"(Ljava/lang/String;)Ljava/lang/String;", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitLdcInsn(" +");
mv.visitLdcInsn(" ");
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "replaceAll",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", false);
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
writer.visitEnd();
File jar = dir.resolve("staged.jar").toFile();
JarOutputStream out = new JarOutputStream(Files.newOutputStream(jar.toPath()));
out.putNextEntry(new JarEntry("dep/Squash.class"));
out.write(writer.toByteArray());
out.closeEntry();
out.putNextEntry(new JarEntry("res.txt"));
out.write("kept".getBytes("UTF-8"));
out.closeEntry();
out.close();
// The staged jar is merged by Ant, which keeps duplicate entries.
org.apache.tools.zip.ZipOutputStream dup = new org.apache.tools.zip.ZipOutputStream(
dir.resolve("dup.jar").toFile());
java.util.zip.ZipFile single = new java.util.zip.ZipFile(jar);
for (String name : new String[] {"dep/Squash.class", "res.txt", "res.txt"}) {
dup.putNextEntry(new org.apache.tools.zip.ZipEntry(name));
dup.write(org.apache.commons.io.IOUtils.toByteArray(single.getInputStream(single.getEntry(name))));
dup.closeEntry();
}
single.close();
dup.close();
Files.move(dir.resolve("dup.jar"), jar.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);

assertEquals(1, BytecodeCompliance.rewriteInvocationsInJar(jar));
java.util.zip.ZipFile zip = new java.util.zip.ZipFile(jar);
try {
String cls = new String(org.apache.commons.io.IOUtils.toByteArray(
zip.getInputStream(zip.getEntry("dep/Squash.class"))), "ISO-8859-1");
assertTrue(cls.contains("com/codename1/util/regex/StringRegex"));
assertEquals("kept", new String(org.apache.commons.io.IOUtils.toByteArray(
zip.getInputStream(zip.getEntry("res.txt"))), "UTF-8"));
} finally {
zip.close();
}
// Idempotent: a second pass finds nothing to do.
assertEquals(0, BytecodeCompliance.rewriteInvocationsInJar(jar));
}

/// A class whose methods are native: present for the compliance index,
/// nothing to run. A method spec is `[static ]name descriptor`.
private void writeStub(Path root, String className, String... methods) throws Exception {
Expand Down
Loading