Skip to content

Commit

Permalink
read/cfi: check for overflow when evaluating DW_CFA_advance_loc (#730)
Browse files Browse the repository at this point in the history
Addresses for CFI rows should only increase. This is equivalent
to the existing check for DW_CFA_set_loc.
  • Loading branch information
philipc committed Jul 4, 2024
1 parent 8fbfb2a commit 9257192
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/read/cfi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2361,7 +2361,13 @@ where
}
AdvanceLoc { delta } => {
let delta = Wrapping(u64::from(delta)) * self.code_alignment_factor;
self.next_start_address = (Wrapping(self.ctx.start_address()) + delta).0;
let address = self
.ctx
.start_address()
.checked_add(delta.0)
.ok_or(Error::AddressOverflow)?;

self.next_start_address = address;
self.ctx.row_mut().end_address = self.next_start_address;
return Ok(true);
}
Expand Down Expand Up @@ -5481,9 +5487,11 @@ mod tests {
let cie = make_test_cie();
let mut ctx = UnwindContext::new();
ctx.row_mut().start_address = u64::MAX;
let mut expected = ctx.clone();
expected.row_mut().end_address = 42 * cie.code_alignment_factor - 1;
let instructions = [(Ok(true), CallFrameInstruction::AdvanceLoc { delta: 42 })];
let expected = ctx.clone();
let instructions = [(
Err(Error::AddressOverflow),
CallFrameInstruction::AdvanceLoc { delta: 42 },
)];
assert_eval(ctx, expected, cie, None, instructions);
}

Expand Down
6 changes: 6 additions & 0 deletions src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ pub enum Error {
UnknownCallFrameInstruction(constants::DwCfa),
/// The end of an address range was before the beginning.
InvalidAddressRange,
/// An address calculation overflowed.
///
/// This is returned in cases where the address is expected to be
/// larger than a previous address, but the calculation overflowed.
AddressOverflow,
/// Encountered a call frame instruction in a context in which it is not
/// valid.
CfiInstructionInInvalidContext,
Expand Down Expand Up @@ -543,6 +548,7 @@ impl Error {
Error::InvalidAddressRange => {
"The end of an address range must not be before the beginning."
}
Error::AddressOverflow => "An address calculation overflowed.",
Error::CfiInstructionInInvalidContext => {
"Encountered a call frame instruction in a context in which it is not valid."
}
Expand Down

0 comments on commit 9257192

Please sign in to comment.