Skip to content

Commit 8fd02d5

Browse files
authored
[BOLT] Fix 32-bit overflow in checkOffsets/checkVMA (#68274)
1 parent 6e9ee42 commit 8fd02d5

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,9 @@ static bool checkOffsets(const typename ELFT::Phdr &Phdr,
408408
return true;
409409

410410
// Only non-empty sections can be at the end of a segment.
411-
uint64_t SectionSize = Sec.sh_size ? Sec.sh_size : 1;
412-
AddressRange SectionAddressRange(Sec.sh_offset, Sec.sh_offset + SectionSize);
411+
uint64_t SectionSize = Sec.sh_size ? Sec.sh_size : 1ull;
412+
AddressRange SectionAddressRange((uint64_t)Sec.sh_offset,
413+
Sec.sh_offset + SectionSize);
413414
AddressRange SegmentAddressRange(Phdr.p_offset,
414415
Phdr.p_offset + Phdr.p_filesz);
415416
if (SegmentAddressRange.contains(SectionAddressRange))
@@ -425,8 +426,9 @@ template <class ELFT>
425426
static bool checkVMA(const typename ELFT::Phdr &Phdr,
426427
const typename ELFT::Shdr &Sec, bool &Overlap) {
427428
// Only non-empty sections can be at the end of a segment.
428-
uint64_t SectionSize = Sec.sh_size ? Sec.sh_size : 1;
429-
AddressRange SectionAddressRange(Sec.sh_addr, Sec.sh_addr + SectionSize);
429+
uint64_t SectionSize = Sec.sh_size ? Sec.sh_size : 1ull;
430+
AddressRange SectionAddressRange((uint64_t)Sec.sh_addr,
431+
Sec.sh_addr + SectionSize);
430432
AddressRange SegmentAddressRange(Phdr.p_vaddr, Phdr.p_vaddr + Phdr.p_memsz);
431433

432434
if (SegmentAddressRange.contains(SectionAddressRange))

bolt/test/checkvma-large-section.test

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This test reproduces the issue with a section which ends at >4G address
2+
REQUIRES: asserts
3+
RUN: split-file %s %t
4+
RUN: yaml2obj %t/yaml -o %t.exe --max-size=0
5+
RUN: llvm-bolt %t.exe -o /dev/null --allow-stripped
6+
#--- yaml
7+
--- !ELF
8+
FileHeader:
9+
Class: ELFCLASS64
10+
Data: ELFDATA2LSB
11+
Type: ET_EXEC
12+
Machine: EM_X86_64
13+
ProgramHeaders:
14+
- Type: PT_LOAD
15+
FirstSec: .a
16+
LastSec: .a
17+
Align: 0x1000
18+
- Type: PT_LOAD
19+
Flags: [ PF_R, PF_W ]
20+
FirstSec: .large_sec
21+
LastSec: .large_sec
22+
VAddr: 0x4a0279a8
23+
- Type: PT_GNU_RELRO
24+
Flags: [ PF_R ]
25+
Sections:
26+
- Name: .a
27+
Type: SHT_PROGBITS
28+
Content: 00
29+
AddressAlign: 0x1
30+
- Name: .large_sec
31+
Type: SHT_PROGBITS
32+
Flags: [ SHF_WRITE, SHF_ALLOC ]
33+
Address: 0x4a0279a8
34+
Size: 0xdf8bb1a0
35+
...

0 commit comments

Comments
 (0)