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

Refactor - Make Executable::ro_section offset explicit #606

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
31 changes: 17 additions & 14 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl<C: ContextObject> Executable<C> {
Ok(Self {
elf_bytes,
sbpf_version,
ro_section: Section::Borrowed(0, 0..text_bytes.len()),
ro_section: Section::Borrowed(ebpf::MM_PROGRAM_START as usize, 0..text_bytes.len()),
text_section_vaddr: ebpf::MM_PROGRAM_START,
text_section_range: 0..text_bytes.len(),
entry_pc,
Expand Down Expand Up @@ -720,12 +720,12 @@ impl<C: ContextObject> Executable<C> {
// ebpf::MM_PROGRAM_START so if the linker has already put the
// sections within ebpf::MM_PROGRAM_START, we need to subtract
// it now.
lowest_addr.saturating_sub(ebpf::MM_PROGRAM_START as usize)
lowest_addr
} else {
if sbpf_version.enable_elf_vaddr() {
return Err(ElfError::ValueOutOfBounds);
}
lowest_addr
lowest_addr.saturating_add(ebpf::MM_PROGRAM_START as usize)
};

Section::Borrowed(addr_offset, buf_offset_start..buf_offset_end)
Expand Down Expand Up @@ -759,9 +759,9 @@ impl<C: ContextObject> Executable<C> {
}

let addr_offset = if lowest_addr >= ebpf::MM_PROGRAM_START as usize {
lowest_addr.saturating_sub(ebpf::MM_PROGRAM_START as usize)
} else {
lowest_addr
} else {
lowest_addr.saturating_add(ebpf::MM_PROGRAM_START as usize)
};
Section::Owned(addr_offset, ro_section)
};
Expand Down Expand Up @@ -1138,10 +1138,7 @@ pub(crate) fn get_ro_region(ro_section: &Section, elf: &[u8]) -> MemoryRegion {
// If offset > 0, the region will start at MM_PROGRAM_START + the offset of
// the first read only byte. [MM_PROGRAM_START, MM_PROGRAM_START + offset)
// will be unmappable, see MemoryRegion::vm_to_host.
MemoryRegion::new_readonly(
ro_data,
ebpf::MM_PROGRAM_START.saturating_add(offset as u64),
)
MemoryRegion::new_readonly(ro_data, offset as u64)
}

#[cfg(test)]
Expand Down Expand Up @@ -1419,7 +1416,7 @@ mod test {
sections,
&elf_bytes,
),
Ok(Section::Owned(offset, data)) if offset == 10 && data.len() == 30
Ok(Section::Owned(offset, data)) if offset == ebpf::MM_PROGRAM_START as usize + 10 && data.len() == 30
));
}

Expand All @@ -1446,7 +1443,7 @@ mod test {
sections,
&elf_bytes,
),
Ok(Section::Owned(offset, data)) if offset == 10 && data.len() == 20
Ok(Section::Owned(offset, data)) if offset == ebpf::MM_PROGRAM_START as usize + 10 && data.len() == 20
));
}

Expand Down Expand Up @@ -1543,7 +1540,10 @@ mod test {
[(Some(b".text"), &s1), (Some(b".rodata"), &s2)];
assert_eq!(
ElfExecutable::parse_ro_sections(&config, &SBPFVersion::V2, sections, &elf_bytes),
Ok(Section::Borrowed(10, 100..120))
Ok(Section::Borrowed(
ebpf::MM_PROGRAM_START as usize + 10,
100..120
))
);
}

Expand Down Expand Up @@ -1708,7 +1708,7 @@ mod test {
sections,
&elf_bytes,
),
Ok(Section::Owned(offset, data)) if offset == 0 && data.len() == 20
Ok(Section::Owned(offset, data)) if offset == ebpf::MM_PROGRAM_START as usize && data.len() == 20
));
}

Expand All @@ -1732,7 +1732,10 @@ mod test {
];
assert_eq!(
ElfExecutable::parse_ro_sections(&config, &sbpf_version, sections, &elf_bytes),
Ok(Section::Borrowed(20, 20..50))
Ok(Section::Borrowed(
ebpf::MM_PROGRAM_START as usize + 20,
20..50
))
);
}
}
Expand Down