Skip to content

Commit

Permalink
read: Report more specific errors for unknown constants (#714)
Browse files Browse the repository at this point in the history
In case someone encounters some DWARF containing a constant value not
known to gimli, let's ensure that the error has the actual constant
available in it.

These values are not visible in `Error::description` or the various
other error-reporting functions which use it, such as the `Display`
implementation. I think it would be good to improve that too, but wanted
to keep this PR relatively small.
  • Loading branch information
jameysharp authored May 23, 2024
1 parent b921aa3 commit 0fe475c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 26 deletions.
12 changes: 6 additions & 6 deletions src/read/cfi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl<'a, 'bases, R: Reader> EhHdrTableIter<'a, 'bases, R> {
constants::DW_EH_PE_sdata2 | constants::DW_EH_PE_udata2 => 2,
constants::DW_EH_PE_sdata4 | constants::DW_EH_PE_udata4 => 4,
constants::DW_EH_PE_sdata8 | constants::DW_EH_PE_udata8 => 8,
_ => return Err(Error::UnknownPointerEncoding),
_ => return Err(Error::UnknownPointerEncoding(self.hdr.table_enc)),
};

let row_size = size * 2;
Expand Down Expand Up @@ -335,7 +335,7 @@ impl<'a, R: Reader + 'a> EhHdrTable<'a, R> {
constants::DW_EH_PE_sdata2 | constants::DW_EH_PE_udata2 => 2,
constants::DW_EH_PE_sdata4 | constants::DW_EH_PE_udata4 => 4,
constants::DW_EH_PE_sdata8 | constants::DW_EH_PE_udata8 => 8,
_ => return Err(Error::UnknownPointerEncoding),
_ => return Err(Error::UnknownPointerEncoding(self.hdr.table_enc)),
};

let row_size = size * 2;
Expand Down Expand Up @@ -3493,7 +3493,7 @@ fn parse_pointer_encoding<R: Reader>(input: &mut R) -> Result<constants::DwEhPe>
if eh_pe.is_valid_encoding() {
Ok(eh_pe)
} else {
Err(Error::UnknownPointerEncoding)
Err(Error::UnknownPointerEncoding(eh_pe))
}
}

Expand Down Expand Up @@ -3562,7 +3562,7 @@ fn parse_encoded_pointer<R: Reader>(
) -> Result<Pointer> {
// TODO: check this once only in parse_pointer_encoding
if !encoding.is_valid_encoding() {
return Err(Error::UnknownPointerEncoding);
return Err(Error::UnknownPointerEncoding(encoding));
}

if encoding == constants::DW_EH_PE_omit {
Expand Down Expand Up @@ -7383,7 +7383,7 @@ mod tests {
let input = [expected.0, 1, 2, 3, 4];
let input = &mut EndianSlice::new(&input, NativeEndian);
assert_eq!(
Err(Error::UnknownPointerEncoding),
Err(Error::UnknownPointerEncoding(expected)),
parse_pointer_encoding(input)
);
}
Expand Down Expand Up @@ -7840,7 +7840,7 @@ mod tests {
};
assert_eq!(
parse_encoded_pointer(encoding, &parameters, &mut rest),
Err(Error::UnknownPointerEncoding)
Err(Error::UnknownPointerEncoding(encoding))
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/read/dwarf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ impl<R: Reader> DwarfPackage<R> {
SectionId::DebugMacro | SectionId::DebugMacinfo => {
// These are valid but we can't parse these yet.
}
_ => return Err(Error::UnknownIndexSection),
_ => return Err(Error::UnknownSection(section.section)),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/read/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl<R: Reader> UnitIndex<R> {
constants::DW_SECT_V2_STR_OFFSETS => SectionId::DebugStrOffsets,
constants::DW_SECT_V2_MACINFO => SectionId::DebugMacinfo,
constants::DW_SECT_V2_MACRO => SectionId::DebugMacro,
_ => return Err(Error::UnknownIndexSection),
section => return Err(Error::UnknownIndexSectionV2(section)),
}
} else {
match constants::DwSect(section) {
Expand All @@ -200,7 +200,7 @@ impl<R: Reader> UnitIndex<R> {
constants::DW_SECT_STR_OFFSETS => SectionId::DebugStrOffsets,
constants::DW_SECT_MACRO => SectionId::DebugMacro,
constants::DW_SECT_RNGLISTS => SectionId::DebugRngLists,
_ => return Err(Error::UnknownIndexSection),
section => return Err(Error::UnknownIndexSection(section)),
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/read/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ fn parse_attribute<R: Reader>(
AttributeValue::DebugStrOffsetsIndex(DebugStrOffsetsIndex(index))
}
_ => {
return Err(Error::UnknownForm);
return Err(Error::UnknownForm(form));
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/read/loclists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ impl<R: Reader> RawLocListEntry<R> {
length: input.read_uleb128()?,
data: parse_data(input, encoding)?,
}),
_ => {
return Err(Error::InvalidAddressRange);
entry => {
return Err(Error::UnknownLocListsEntry(entry));
}
},
})
Expand Down
28 changes: 20 additions & 8 deletions src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ pub enum Error {
/// The specified length is impossible.
BadLength,
/// Found an unknown `DW_FORM_*` type.
UnknownForm,
UnknownForm(constants::DwForm),
/// Expected a zero, found something else.
ExpectedZero,
/// Found an abbreviation code that has already been used.
Expand All @@ -317,7 +317,7 @@ pub enum Error {
/// Found an unknown DWARF version.
UnknownVersion(u64),
/// Found a record with an unknown abbreviation code.
UnknownAbbreviation,
UnknownAbbreviation(u64),
/// Hit the end of input before it was expected.
UnexpectedEof(ReaderOffsetId),
/// Read a null entry before it was expected.
Expand All @@ -326,6 +326,10 @@ pub enum Error {
UnknownStandardOpcode(constants::DwLns),
/// Found an unknown extended opcode.
UnknownExtendedOpcode(constants::DwLne),
/// Found an unknown location-lists format.
UnknownLocListsEntry(constants::DwLle),
/// Found an unknown range-lists format.
UnknownRangeListsEntry(constants::DwRle),
/// The specified address size is not supported.
UnsupportedAddressSize(u8),
/// The specified offset size is not supported.
Expand Down Expand Up @@ -395,7 +399,7 @@ pub enum Error {
/// An offset value was larger than the maximum supported value.
UnsupportedOffset,
/// The given pointer encoding is either unknown or invalid.
UnknownPointerEncoding,
UnknownPointerEncoding(constants::DwEhPe),
/// Did not find an entry at the given offset.
NoEntryAtGivenOffset,
/// The given offset is out of bounds.
Expand Down Expand Up @@ -436,8 +440,12 @@ pub enum Error {
InvalidIndexSlotCount,
/// Invalid hash row in `.dwp` index.
InvalidIndexRow,
/// Unknown section type.
UnknownSection(SectionId),
/// Unknown section type in `.dwp` index.
UnknownIndexSection,
UnknownIndexSection(constants::DwSect),
/// Unknown section type in version 2 `.dwp` index.
UnknownIndexSectionV2(constants::DwSectV2),
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -482,19 +490,21 @@ impl Error {
`DW_CHILDREN_{yes,no}`"
}
Error::BadLength => "The specified length is impossible",
Error::UnknownForm => "Found an unknown `DW_FORM_*` type",
Error::UnknownForm(_) => "Found an unknown `DW_FORM_*` type",
Error::ExpectedZero => "Expected a zero, found something else",
Error::DuplicateAbbreviationCode => {
"Found an abbreviation code that has already been used"
}
Error::DuplicateArange => "Found a duplicate arange",
Error::UnknownReservedLength => "Found an unknown reserved length value",
Error::UnknownVersion(_) => "Found an unknown DWARF version",
Error::UnknownAbbreviation => "Found a record with an unknown abbreviation code",
Error::UnknownAbbreviation(_) => "Found a record with an unknown abbreviation code",
Error::UnexpectedEof(_) => "Hit the end of input before it was expected",
Error::UnexpectedNull => "Read a null entry before it was expected.",
Error::UnknownStandardOpcode(_) => "Found an unknown standard opcode",
Error::UnknownExtendedOpcode(_) => "Found an unknown extended opcode",
Error::UnknownLocListsEntry(_) => "Found an unknown location lists entry",
Error::UnknownRangeListsEntry(_) => "Found an unknown range lists entry",
Error::UnsupportedAddressSize(_) => "The specified address size is not supported",
Error::UnsupportedOffsetSize(_) => "The specified offset size is not supported",
Error::UnsupportedFieldSize(_) => "The specified field size is not supported",
Expand Down Expand Up @@ -551,7 +561,7 @@ impl Error {
Error::UnsupportedOffset => {
"An offset value was larger than the maximum supported value."
}
Error::UnknownPointerEncoding => {
Error::UnknownPointerEncoding(_) => {
"The given pointer encoding is either unknown or invalid."
}
Error::NoEntryAtGivenOffset => "Did not find an entry at the given offset.",
Expand Down Expand Up @@ -586,7 +596,9 @@ impl Error {
Error::InvalidIndexSectionCount => "Invalid section count in `.dwp` index.",
Error::InvalidIndexSlotCount => "Invalid slot count in `.dwp` index.",
Error::InvalidIndexRow => "Invalid hash row in `.dwp` index.",
Error::UnknownIndexSection => "Unknown section type in `.dwp` index.",
Error::UnknownSection(_) => "Unknown section type.",
Error::UnknownIndexSection(_) => "Unknown section type in `.dwp` index.",
Error::UnknownIndexSectionV2(_) => "Unknown section type in version 2 `.dwp` index.",
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/read/rnglists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ impl<T: ReaderOffset> RawRngListEntry<T> {
begin: input.read_address(encoding.address_size)?,
length: input.read_uleb128()?,
}),
_ => {
return Err(Error::InvalidAddressRange);
entry => {
return Err(Error::UnknownRangeListsEntry(entry));
}
},
})
Expand Down
10 changes: 6 additions & 4 deletions src/read/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,9 @@ where
if code == 0 {
return Ok(None);
};
let abbrev = abbreviations.get(code).ok_or(Error::UnknownAbbreviation)?;
let abbrev = abbreviations
.get(code)
.ok_or(Error::UnknownAbbreviation(code))?;
Ok(Some(DebuggingInformationEntry {
offset: UnitOffset(offset),
attrs_slice: input.clone(),
Expand Down Expand Up @@ -2180,7 +2182,7 @@ pub(crate) fn parse_attribute<R: Reader>(
AttributeValue::DebugRngListsIndex(DebugRngListsIndex(index))
}
_ => {
return Err(Error::UnknownForm);
return Err(Error::UnknownForm(form));
}
};
let attr = Attribute {
Expand Down Expand Up @@ -2246,7 +2248,7 @@ pub(crate) fn skip_attributes<R: Reader>(
input.skip_leb128()?;
}
_ => {
return Err(Error::UnknownForm);
return Err(Error::UnknownForm(form));
}
};
break;
Expand Down Expand Up @@ -2425,7 +2427,7 @@ impl<'abbrev, 'unit, R: Reader> EntriesRaw<'abbrev, 'unit, R> {
let abbrev = self
.abbreviations
.get(code)
.ok_or(Error::UnknownAbbreviation)?;
.ok_or(Error::UnknownAbbreviation(code))?;
if abbrev.has_children() {
self.depth += 1;
}
Expand Down

0 comments on commit 0fe475c

Please sign in to comment.