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

read: improved UnexpectedEof errors #408

Merged
merged 1 commit into from
Mar 14, 2019
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
103 changes: 36 additions & 67 deletions examples/dwarfdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::borrow::{Borrow, Cow};
use std::cmp::min;
use std::collections::HashMap;
use std::env;
use std::error;
use std::fmt::{self, Debug};
use std::fs;
use std::io;
Expand All @@ -25,7 +24,6 @@ use typed_arena::Arena;
pub enum Error {
GimliError(gimli::Error),
IoError,
MissingDIE,
}

impl fmt::Display for Error {
Expand All @@ -35,21 +33,21 @@ impl fmt::Display for Error {
}
}

impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::GimliError(ref err) => err.description(),
Error::IoError => "An I/O error occurred while writing.",
Error::MissingDIE => "Expected a DIE but none was found",
}
}

fn cause(&self) -> Option<&error::Error> {
match *self {
Error::GimliError(ref err) => Some(err),
_ => None,
fn writeln_error<W: Write, R: Reader>(
w: &mut W,
dwarf: &gimli::Dwarf<R>,
err: Error,
msg: &str,
) -> io::Result<()> {
writeln!(
w,
"{}: {}",
msg,
match err {
Error::GimliError(err) => dwarf.format_error(err),
Error::IoError => "An I/O error occurred while writing.".to_string(),
}
}
)
}

impl From<gimli::Error> for Error {
Expand Down Expand Up @@ -289,6 +287,16 @@ impl<'a, R: gimli::Reader<Offset = usize>> gimli::Reader for Relocate<'a, R> {
self.reader.offset_from(&base.reader)
}

#[inline]
fn offset_id(&self) -> gimli::ReaderOffsetId {
self.reader.offset_id()
}

#[inline]
fn lookup_offset_id(&self, id: gimli::ReaderOffsetId) -> Option<Self::Offset> {
self.reader.lookup_offset_id(id)
}

#[inline]
fn find(&self, byte: u8) -> gimli::Result<Self::Offset> {
self.reader.find(byte)
Expand Down Expand Up @@ -429,22 +437,14 @@ fn main() {
let file = match fs::File::open(&file_path) {
Ok(file) => file,
Err(err) => {
println!(
"Failed to open file '{}': {}",
file_path,
error::Error::description(&err)
);
println!("Failed to open file '{}': {}", file_path, err);
continue;
}
};
let file = match unsafe { memmap::Mmap::map(&file) } {
Ok(mmap) => mmap,
Err(err) => {
println!(
"Failed to map file '{}': {}",
file_path,
error::Error::description(&err)
);
println!("Failed to map file '{}': {}", file_path, err);
continue;
}
};
Expand All @@ -464,11 +464,7 @@ fn main() {
let ret = dump_file(&file, endian, &flags);
match ret {
Ok(_) => (),
Err(err) => println!(
"Failed to dump '{}': {}",
file_path,
error::Error::description(&err)
),
Err(err) => println!("Failed to dump '{}': {}", file_path, err,),
}
}
}
Expand All @@ -486,7 +482,8 @@ where
add_relocations(&mut relocations, file, section);
section.uncompressed_data()
}
None => Cow::Borrowed(&[][..]),
// Use a non-zero capacity so that `ReaderOffsetId`s are unique.
None => Cow::Owned(Vec::with_capacity(1)),
};
let data_ref = (*arena.0.alloc(data)).borrow();
let reader = gimli::EndianSlice::new(data_ref, endian);
Expand Down Expand Up @@ -840,22 +837,14 @@ where
let unit = match dwarf.unit(header) {
Ok(unit) => unit,
Err(err) => {
writeln!(
buf,
"Failed to parse unit root entry: {}",
error::Error::description(&err)
)?;
writeln_error(buf, dwarf, err.into(), "Failed to parse unit root entry")?;
return Ok(());
}
};

let entries_result = dump_entries(buf, unit, dwarf, flags);
if let Err(err) = entries_result {
writeln!(
buf,
"Failed to dump entries: {}",
error::Error::description(&err)
)?;
writeln_error(buf, dwarf, err, "Failed to dump entries")?;
}
if !flags
.match_units
Expand Down Expand Up @@ -894,21 +883,13 @@ fn dump_types<R: Reader, W: Write>(
let unit = match dwarf.type_unit(header) {
Ok(unit) => unit,
Err(err) => {
writeln!(
w,
"Failed to parse unit root entry: {}",
error::Error::description(&err)
)?;
writeln_error(w, dwarf, err.into(), "Failed to parse unit root entry")?;
continue;
}
};
let entries_result = dump_entries(w, unit, dwarf, flags);
if let Err(err) = entries_result {
writeln!(
w,
"Failed to dump entries: {}",
error::Error::description(&err)
)?;
writeln_error(w, dwarf, err, "Failed to dump entries")?;
}
}
Ok(())
Expand Down Expand Up @@ -959,11 +940,7 @@ fn dump_entries<R: Reader, W: Write>(
} else {
match dump_attr_value(w, &attr, &unit, dwarf) {
Ok(_) => (),
Err(ref err) => writeln!(
w,
"Failed to dump attribute value: {}",
error::Error::description(err)
)?,
Err(err) => writeln_error(w, dwarf, err, "Failed to dump attribute value")?,
};
}
}
Expand Down Expand Up @@ -1682,22 +1659,14 @@ fn dump_line<R: Reader, W: Write>(w: &mut W, dwarf: &gimli::Dwarf<R>) -> Result<
let unit = match dwarf.unit(header) {
Ok(unit) => unit,
Err(err) => {
writeln!(
w,
"Failed to parse unit root entry: {}",
error::Error::description(&err)
)?;
writeln_error(w, dwarf, err.into(), "Failed to parse unit root entry")?;
continue;
}
};
match dump_line_program(w, &unit, dwarf) {
Ok(_) => (),
Err(Error::IoError) => return Err(Error::IoError),
Err(err) => writeln!(
w,
"Failed to dump line program: {}",
error::Error::description(&err)
)?,
Err(err) => writeln_error(w, dwarf, err, "Failed to dump line program")?,
}
}
Ok(())
Expand Down
28 changes: 25 additions & 3 deletions src/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,26 @@ pub mod write {
mod tests {
use super::{low_bits_of_byte, low_bits_of_u64, read, write, CONTINUATION_BIT};
use crate::endianity::NativeEndian;
use crate::read::{EndianSlice, Error};
use crate::read::{EndianSlice, Error, ReaderOffsetId};
use std;
use std::io;

trait ResultExt {
fn map_eof(self, input: &[u8]) -> Self;
}

impl<T> ResultExt for Result<T, Error> {
fn map_eof(self, input: &[u8]) -> Self {
match self {
Err(Error::UnexpectedEof(id)) => {
let id = ReaderOffsetId(id.0 - input.as_ptr() as u64);
Err(Error::UnexpectedEof(id))
}
r => r,
}
}
}

#[test]
fn test_low_bits_of_byte() {
for i in 0..127 {
Expand Down Expand Up @@ -335,14 +351,20 @@ mod tests {
fn test_read_unsigned_not_enough_data() {
let buf = [CONTINUATION_BIT];
let mut readable = EndianSlice::new(&buf[..], NativeEndian);
assert_eq!(read::unsigned(&mut readable), Err(Error::UnexpectedEof));
assert_eq!(
read::unsigned(&mut readable).map_eof(&buf),
Err(Error::UnexpectedEof(ReaderOffsetId(1)))
);
}

#[test]
fn test_read_signed_not_enough_data() {
let buf = [CONTINUATION_BIT];
let mut readable = EndianSlice::new(&buf[..], NativeEndian);
assert_eq!(read::signed(&mut readable), Err(Error::UnexpectedEof));
assert_eq!(
read::signed(&mut readable).map_eof(&buf),
Err(Error::UnexpectedEof(ReaderOffsetId(1)))
);
}

#[test]
Expand Down
6 changes: 5 additions & 1 deletion src/read/abbrev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl<R> Section<R> for DebugAbbrev<R> {
fn id() -> SectionId {
SectionId::DebugAbbrev
}

fn reader(&self) -> &R {
&self.debug_abbrev_section
}
}

impl<R> From<R> for DebugAbbrev<R> {
Expand Down Expand Up @@ -774,7 +778,7 @@ pub mod tests {
let buf = &mut EndianSlice::new(&*buf, LittleEndian);

match Abbreviation::parse(buf) {
Err(Error::UnexpectedEof) => {}
Err(Error::UnexpectedEof(_)) => {}
otherwise => panic!("Unexpected result: {:?}", otherwise),
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/read/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl<R> Section<R> for DebugAddr<R> {
fn id() -> SectionId {
SectionId::DebugAddr
}

fn reader(&self) -> &R {
&self.section
}
}

impl<R> From<R> for DebugAddr<R> {
Expand Down
4 changes: 4 additions & 0 deletions src/read/aranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ impl<R: Reader> Section<R> for DebugAranges<R> {
fn id() -> SectionId {
SectionId::DebugAranges
}

fn reader(&self) -> &R {
self.0.reader()
}
}

impl<R: Reader> From<R> for DebugAranges<R> {
Expand Down
Loading