Skip to content

Commit

Permalink
Add tests for file I/O based ELF parser
Browse files Browse the repository at this point in the history
Add some tests for our file I/O based ELF parser.

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o authored and danielocfb committed Dec 23, 2024
1 parent 0139dc3 commit 3f4b694
Showing 1 changed file with 67 additions and 16 deletions.
83 changes: 67 additions & 16 deletions src/elf/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,24 @@ where
_backend: B::ObjTy,
}

impl ElfParser<File> {
#[cfg(test)]
pub(crate) fn open_file_io<P>(file: File, path: P) -> Self
where
P: Into<PathBuf>,
{
let _backend = Box::new(file);
let file_ref = unsafe { mem::transmute::<&File, &'static File>(_backend.deref()) };

let parser = Self {
cache: Cache::new(file_ref),
path: Some(path.into()),
_backend,
};
parser
}
}

impl ElfParser<Mmap> {
/// Create an `ElfParser` from an open file.
pub(crate) fn open_file<P>(file: &File, path: P) -> Result<Self>
Expand Down Expand Up @@ -1340,10 +1358,21 @@ mod tests {
let () = file.write_all(dump).unwrap();
let () = file.rewind().unwrap();

let parser = ElfParser::open_file(file.as_file(), file.path()).unwrap();
let ehdr = parser.cache.ensure_ehdr().unwrap();
assert_eq!(ehdr.shnum, usize::from(SHNUM));
assert_eq!(ehdr.phnum, usize::try_from(PHNUM).unwrap());
fn test<B>(parser: ElfParser<B>)
where
B: Backend,
{
let ehdr = parser.cache.ensure_ehdr().unwrap();
assert_eq!(ehdr.shnum, usize::from(SHNUM));
assert_eq!(ehdr.phnum, usize::try_from(PHNUM).unwrap());
}

let path = file.path().to_path_buf();
let parser_mmap = ElfParser::open_file(file.as_file(), &path).unwrap();
let () = test(parser_mmap);

let parser_io = ElfParser::open_file_io(file.into_file(), &path);
let () = test(parser_io);
}

/// Test that our `ElfParser` can handle a `shstrndx` larger than
Expand Down Expand Up @@ -1397,10 +1426,21 @@ mod tests {
let () = file.write_all(dump).unwrap();
let () = file.rewind().unwrap();

let parser = ElfParser::open_file(file.as_file(), file.path()).unwrap();
let ehdr = parser.cache.ensure_ehdr().unwrap();
let shstrndx = parser.cache.shstrndx(&ehdr.ehdr).unwrap();
assert_eq!(shstrndx, usize::from(SHSTRNDX));
fn test<B>(parser: ElfParser<B>)
where
B: Backend,
{
let ehdr = parser.cache.ensure_ehdr().unwrap();
let shstrndx = parser.cache.shstrndx(&ehdr.ehdr).unwrap();
assert_eq!(shstrndx, usize::from(SHSTRNDX));
}

let path = file.path().to_path_buf();
let parser_mmap = ElfParser::open_file(file.as_file(), &path).unwrap();
let () = test(parser_mmap);

let parser_io = ElfParser::open_file_io(file.into_file(), &path);
let () = test(parser_io);
}


Expand Down Expand Up @@ -1542,14 +1582,25 @@ mod tests {
let () = file.write_all(dump).unwrap();
let () = file.rewind().unwrap();

let parser = ElfParser::open_file(file.as_file(), file.path()).unwrap();
// A file offset as produced by normalization.
let file_offset = 0x1b63b4d0;
let virt_offset = parser
.file_offset_to_virt_offset(file_offset)
.unwrap()
.unwrap();
assert_eq!(virt_offset, 0x1c23b4d0);
fn test<B>(parser: ElfParser<B>)
where
B: Backend,
{
// A file offset as produced by normalization.
let file_offset = 0x1b63b4d0;
let virt_offset = parser
.file_offset_to_virt_offset(file_offset)
.unwrap()
.unwrap();
assert_eq!(virt_offset, 0x1c23b4d0);
}

let path = file.path().to_path_buf();
let parser_mmap = ElfParser::open_file(file.as_file(), &path).unwrap();
let () = test(parser_mmap);

let parser_io = ElfParser::open_file_io(file.into_file(), &path);
let () = test(parser_io);
}

/// Make sure that we can look up a symbol in an ELF file.
Expand Down

0 comments on commit 3f4b694

Please sign in to comment.