Skip to content

[BOLT] Fix 32-bit overflow in checkOffsets/checkVMA #68274

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

Merged
merged 1 commit into from
Oct 5, 2023
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
10 changes: 6 additions & 4 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,9 @@ static bool checkOffsets(const typename ELFT::Phdr &Phdr,
return true;

// Only non-empty sections can be at the end of a segment.
uint64_t SectionSize = Sec.sh_size ? Sec.sh_size : 1;
AddressRange SectionAddressRange(Sec.sh_offset, Sec.sh_offset + SectionSize);
uint64_t SectionSize = Sec.sh_size ? Sec.sh_size : 1ull;
AddressRange SectionAddressRange((uint64_t)Sec.sh_offset,
Sec.sh_offset + SectionSize);
AddressRange SegmentAddressRange(Phdr.p_offset,
Phdr.p_offset + Phdr.p_filesz);
if (SegmentAddressRange.contains(SectionAddressRange))
Expand All @@ -425,8 +426,9 @@ template <class ELFT>
static bool checkVMA(const typename ELFT::Phdr &Phdr,
const typename ELFT::Shdr &Sec, bool &Overlap) {
// Only non-empty sections can be at the end of a segment.
uint64_t SectionSize = Sec.sh_size ? Sec.sh_size : 1;
AddressRange SectionAddressRange(Sec.sh_addr, Sec.sh_addr + SectionSize);
uint64_t SectionSize = Sec.sh_size ? Sec.sh_size : 1ull;
AddressRange SectionAddressRange((uint64_t)Sec.sh_addr,
Sec.sh_addr + SectionSize);
AddressRange SegmentAddressRange(Phdr.p_vaddr, Phdr.p_vaddr + Phdr.p_memsz);

if (SegmentAddressRange.contains(SectionAddressRange))
Expand Down
35 changes: 35 additions & 0 deletions bolt/test/checkvma-large-section.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This test reproduces the issue with a section which ends at >4G address
REQUIRES: asserts
RUN: split-file %s %t
RUN: yaml2obj %t/yaml -o %t.exe --max-size=0
RUN: llvm-bolt %t.exe -o /dev/null --allow-stripped
#--- yaml
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
ProgramHeaders:
- Type: PT_LOAD
FirstSec: .a
LastSec: .a
Align: 0x1000
- Type: PT_LOAD
Flags: [ PF_R, PF_W ]
FirstSec: .large_sec
LastSec: .large_sec
VAddr: 0x4a0279a8
- Type: PT_GNU_RELRO
Flags: [ PF_R ]
Sections:
- Name: .a
Type: SHT_PROGBITS
Content: 00
AddressAlign: 0x1
- Name: .large_sec
Type: SHT_PROGBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
Address: 0x4a0279a8
Size: 0xdf8bb1a0
...