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

[llvm-objcopy] --gap-fill and 0-size sections #75837

Merged
merged 2 commits into from
Dec 19, 2023

Conversation

quic-akaryaki
Copy link
Contributor

In the change that added --gap-fill, the condition to choose the sections to write in BinaryWriter::write() did not exclude zero-size sections. However, zero-size sections did not have correct offsets assigned in BinaryWriter::finalize(). The result is either a failed assertion, or memory corruption due to writing to the buffer beyond its size.
To fix this, exclude zero-size sections and add a zero-size section to the test, which would trigger the bug.

In the change that added `--gap-fill`, the condition on choosing the
sections to write in `BinaryWriter::write()` did not exclude zero-size
sections. However, such sections did not have correct offsets assigned
in `BinaryWriter::finalize()`. The result is either a failed assertion,
or memory corruption due to writing to the buffer beyond its size.
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 18, 2023

@llvm/pr-subscribers-llvm-binary-utilities

Author: None (quic-akaryaki)

Changes

In the change that added --gap-fill, the condition to choose the sections to write in BinaryWriter::write() did not exclude zero-size sections. However, zero-size sections did not have correct offsets assigned in BinaryWriter::finalize(). The result is either a failed assertion, or memory corruption due to writing to the buffer beyond its size.
To fix this, exclude zero-size sections and add a zero-size section to the test, which would trigger the bug.


Full diff: https://github.com/llvm/llvm-project/pull/75837.diff

2 Files Affected:

  • (modified) llvm/lib/ObjCopy/ELF/ELFObject.cpp (+1-1)
  • (modified) llvm/test/tools/llvm-objcopy/ELF/gap-fill.test (+5)
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index 5352736bdcb9b8..c8b66d6fcb5ebf 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -2638,7 +2638,7 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
 Error BinaryWriter::write() {
   SmallVector<const SectionBase *, 30> SectionsToWrite;
   for (const SectionBase &Sec : Obj.allocSections()) {
-    if (Sec.Type != SHT_NOBITS)
+    if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
       SectionsToWrite.push_back(&Sec);
   }
 
diff --git a/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test b/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test
index c11909746330bb..6bfd27924bf244 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test
@@ -106,6 +106,11 @@ Sections:
     Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
     Address:         0x0108
     Content:         'AABBCCDDFEDCBA'
+  - Name:            .zero_size
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:         0x0110
+    Size:            0
   - Name:            .space2
     Type:            Fill
     Pattern:         'DC'

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

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

LGTM, with nits.

Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x0110
Size: 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe this Size field is unnecessary, although if you want to leave it in for clarity, I won't object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'd like to leave it.

@@ -106,6 +106,11 @@ Sections:
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x0108
Content: 'AABBCCDDFEDCBA'
- Name: .zero_size
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Copy link
Collaborator

Choose a reason for hiding this comment

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

EXECINSTR is irrelevant to the test, right? So let's omit it to avoid confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@quic-akaryaki
Copy link
Contributor Author

Thank you for the feedback @jh7370 .

@quic-akaryaki quic-akaryaki merged commit 535520c into llvm:main Dec 19, 2023
4 checks passed
@quic-akaryaki quic-akaryaki deleted the gap-fill-zero-size branch April 2, 2024 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants