Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ijar: fix manifest sections handling #12771

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
20 changes: 14 additions & 6 deletions third_party/ijar/ijar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ u1 *JarCopierProcessor::AppendTargetLabelToManifest(
const char *target_label, const char *injecting_rule_kind) {
const char *line_start = (const char *)manifest_data;
const char *data_end = (const char *)manifest_data + size;
while (line_start < data_end) {

// Write main attributes part
while (line_start < data_end && line_start[0] != '\r' && line_start[0] != '\n') {
const char *line_end = strchr(line_start, '\n');
// Go past return char to point to next line, or to end of data buffer
line_end = line_end != nullptr ? line_end + 1 : data_end;
Expand All @@ -309,18 +311,24 @@ u1 *JarCopierProcessor::AppendTargetLabelToManifest(
strncmp(line_start, INJECTING_RULE_KIND_KEY,
INJECTING_RULE_KIND_KEY_LENGTH) != 0) {
size_t len = line_end - line_start;
// Skip empty lines
if (len > 0 && line_start[0] != '\r' && line_start[0] != '\n') {
memcpy(buf, line_start, len);
buf += len;
}
memcpy(buf, line_start, len);
buf += len;
}
line_start = line_end;
}

// Append target label and, if given, rule kind
buf = WriteManifestAttr(buf, TARGET_LABEL_KEY, target_label);
if (injecting_rule_kind != nullptr) {
buf = WriteManifestAttr(buf, INJECTING_RULE_KIND_KEY, injecting_rule_kind);
}

// Write the rest of the manifest file
size_t sections_len = data_end - line_start;
if (sections_len > 0) {
memcpy(buf, line_start, sections_len);
buf += sections_len;
}
return buf;
}

Expand Down
10 changes: 10 additions & 0 deletions third_party/ijar/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ genrule(
tools = ["//third_party/ijar"],
)

genrule(
name = "jar_with_manifest_sections",
srcs = ["jar-with-manifest-sections.jar"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference:

Archive:  jar-with-manifest-sections.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
     1382  2010-01-01 05:00   AnnotatedClass.class
      476  2010-01-01 05:00   Annotations.class
      381  2010-01-01 05:00   Annotations$ParametersOnlyAnnotation.class
      468  2010-01-01 05:00   Annotations$RuntimeInvisible.class
      561  2010-01-01 05:00   Annotations$RuntimeVisible.class
        0  2010-01-01 05:00   META-INF/
       96  2021-01-04 09:23   META-INF/MANIFEST.MF
       19  2018-03-29 15:53   textfile.txt
---------                     -------
     3383                     8 files
$ cat META-INF/MANIFEST.MF 
Manifest-Version: 1.0
Created-By: test-code

Name: foo
Foo: bar

Name: baz
Another: bar

outs = ["jar-with-manifest-sections-nostrip.jar"],
cmd = "$(location //third_party/ijar) --target_label //foo:foo --nostrip_jar $< $@",
tools = ["//third_party/ijar"],
)

genrule(
name = "jar_without_manifest_nostrip_idempotence",
srcs = ["jar-without-manifest-nostrip.jar"],
Expand Down Expand Up @@ -305,6 +313,7 @@ java_test(
"UseRestrictedAnnotation.java",
"jar-with-manifest.jar",
"jar-with-manifest-and-target-label.jar",
"jar-with-manifest-sections.jar",
"jar-without-manifest.jar",
"package-info.java",
":empty_with_target_label",
Expand All @@ -314,6 +323,7 @@ java_test(
":interface_ijar_testlib_with_target_label",
":jar_with_manifest_and_target_label_nostrip",
":jar_with_manifest_nostrip",
":jar_with_manifest_sections",
":jar_without_manifest_nostrip",
":jar_without_manifest_nostrip_idempotence",
":kotlin_module-interface.jar",
Expand Down
21 changes: 21 additions & 0 deletions third_party/ijar/test/IjarTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,27 @@ public void testNoStripJarWithoutManifest() throws Exception {
}
}

@Test
public void testPreserveManifestSections() throws Exception {
try (JarFile stripped = new JarFile(
"third_party/ijar/test/jar-with-manifest-sections-nostrip.jar")) {
ImmutableList<String> strippedEntries =
stripped.stream().map(JarEntry::getName).collect(toImmutableList());

assertThat(strippedEntries.get(0)).isEqualTo("META-INF/");
assertThat(strippedEntries.get(1)).isEqualTo("META-INF/MANIFEST.MF");
Manifest manifest = stripped.getManifest();
Attributes attributes = manifest.getMainAttributes();
assertThat(attributes.getValue("Target-Label")).isEqualTo("//foo:foo");

Attributes sectionAttributes1 = manifest.getAttributes("foo");
assertThat(sectionAttributes1.getValue("Foo")).isEqualTo("bar");

Attributes sectionAttributes2 = manifest.getAttributes("baz");
assertThat(sectionAttributes2.getValue("Another")).isEqualTo("bar");
}
}

// Tests idempotence of --nostrip
@Test
public void testNoStripIdempotence() throws Exception {
Expand Down
Binary file not shown.