Skip to content

fix: improve errors for elf decode #2179

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
Mar 28, 2025
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
34 changes: 30 additions & 4 deletions crates/core/executor/src/disassembler/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use elf::{
file::Class,
ElfBytes,
};
use eyre::OptionExt;
use hashbrown::HashMap;
use sp1_primitives::consts::{MAXIMUM_MEMORY_SIZE, WORD_SIZE};

Expand Down Expand Up @@ -82,6 +83,11 @@ impl Elf {
let mut instructions: Vec<u32> = Vec::new();
let mut base_address = u32::MAX;

// Data about the last segment.
let mut prev_segment_end_addr = None;

// Check that the segments are sorted and disjoint.

// Only read segments that are executable instructions that are also PT_LOAD.
for segment in segments.iter().filter(|x| x.p_type == PT_LOAD) {
// Get the file size of the segment as an u32.
Expand All @@ -102,10 +108,30 @@ impl Elf {
eyre::bail!("vaddr {vaddr:08x} is unaligned");
}

// If the virtual address is less than the first memory address, then update the first
// memory address.
if (segment.p_flags & PF_X) != 0 && base_address > vaddr {
base_address = vaddr;
// Check that the ELF structure is supported.
if let Some(last_addr) = prev_segment_end_addr {
eyre::ensure!(last_addr <= vaddr, "unsupported elf structure");
}
prev_segment_end_addr =
Some(vaddr.checked_add(mem_size).ok_or_eyre("last addr overflow")?);

if (segment.p_flags & PF_X) != 0 {
if base_address == u32::MAX {
base_address = vaddr;
eyre::ensure!(
base_address > 0x20,
"base address {base_address} should be greater than 0x20"
);
} else {
let instr_len: u32 = WORD_SIZE
.checked_mul(instructions.len())
.ok_or_eyre("instructions length overflow")?
.try_into()?;
let last_instruction_addr = base_address
.checked_add(instr_len)
.ok_or_eyre("instruction addr overflow")?;
eyre::ensure!(vaddr == last_instruction_addr, "unsupported elf structure");
}
}

// Get the offset to the segment.
Expand Down
Loading