| 1 |
use crate::{arch::Arch, unwind_result::UnwindResult}; |
| 2 |
use core::ops::Range; |
| 3 |
|
| 4 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 5 |
pub enum PeUnwinderError { |
| 6 |
MissingUnwindInfoData(u32), |
| 7 |
MissingInstructionData(u32), |
| 8 |
MissingStackData(Option<u64>), |
| 9 |
UnwindInfoParseError, |
| 10 |
Aarch64Unsupported, |
| 11 |
} |
| 12 |
|
| 13 |
impl core::fmt::Display for PeUnwinderError { |
| 14 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 15 |
match self { |
| 16 |
Self::MissingUnwindInfoData(rva) => { |
| 17 |
write!(f, "failed to read unwind info memory at RVA {rva:x}") |
| 18 |
} |
| 19 |
Self::MissingInstructionData(rva) => { |
| 20 |
write!(f, "failed to read instruction memory at RVA {rva:x}") |
| 21 |
} |
| 22 |
Self::MissingStackData(addr) => { |
| 23 |
write!(f, "failed to read stack")?; |
| 24 |
if let Some(addr) = addr { |
| 25 |
write!(f, " at address {addr:x}")?; |
| 26 |
} |
| 27 |
Ok(()) |
| 28 |
} |
| 29 |
Self::UnwindInfoParseError => write!(f, "failed to parse UnwindInfo"), |
| 30 |
Self::Aarch64Unsupported => write!(f, "AArch64 is not yet supported"), |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
#[cfg(feature = "std")] |
| 36 |
impl std::error::Error for PeUnwinderError {} |
| 37 |
|
| 38 |
/// Data and the related RVA range within the binary. |
| 39 |
/// |
| 40 |
/// This is only used by PE unwinding. |
| 41 |
/// |
| 42 |
/// Type arguments: |
| 43 |
/// - `D`: The type for unwind section data. This allows carrying owned data on the |
| 44 |
/// module, e.g. `Vec<u8>`. But it could also be a wrapper around mapped memory from |
| 45 |
/// a file or a different process, for example. It just needs to provide a slice of |
| 46 |
/// bytes via its `Deref` implementation. |
| 47 |
pub struct DataAtRvaRange<D> { |
| 48 |
pub data: D, |
| 49 |
pub rva_range: Range<u32>, |
| 50 |
} |
| 51 |
|
| 52 |
pub struct PeSections<'a, D> { |
| 53 |
pub pdata: &'a D, |
| 54 |
pub rdata: Option<&'a DataAtRvaRange<D>>, |
| 55 |
pub xdata: Option<&'a DataAtRvaRange<D>>, |
| 56 |
pub text: Option<&'a DataAtRvaRange<D>>, |
| 57 |
} |
| 58 |
|
| 59 |
impl<'a, D> PeSections<'a, D> |
| 60 |
where |
| 61 |
D: core::ops::Deref<Target = [u8]>, |
| 62 |
{ |
| 63 |
pub fn unwind_info_memory_at_rva(&self, rva: u32) -> Result<&'a [u8], PeUnwinderError> { |
| 64 |
[&self.rdata, &self.xdata] |
| 65 |
.into_iter() |
| 66 |
.find_map(|o| o.and_then(|m| memory_at_rva(m, rva))) |
| 67 |
.ok_or(PeUnwinderError::MissingUnwindInfoData(rva)) |
| 68 |
} |
| 69 |
|
| 70 |
pub fn text_memory_at_rva(&self, rva: u32) -> Result<&'a [u8], PeUnwinderError> { |
| 71 |
self.text |
| 72 |
.and_then(|m| memory_at_rva(m, rva)) |
| 73 |
.ok_or(PeUnwinderError::MissingInstructionData(rva)) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
fn memory_at_rva<D: core::ops::Deref<Target = [u8]>>( |
| 78 |
DataAtRvaRange { data, rva_range }: &DataAtRvaRange<D>, |
| 79 |
address: u32, |
| 80 |
) -> Option<&[u8]> { |
| 81 |
if rva_range.contains(&address) { |
| 82 |
let offset = address - rva_range.start; |
| 83 |
Some(&data[(offset as usize)..]) |
| 84 |
} else { |
| 85 |
None |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
pub trait PeUnwinding: Arch { |
| 90 |
fn unwind_frame<F, D>( |
| 91 |
sections: PeSections<D>, |
| 92 |
address: u32, |
| 93 |
regs: &mut Self::UnwindRegs, |
| 94 |
is_first_frame: bool, |
| 95 |
read_stack: &mut F, |
| 96 |
) -> Result<UnwindResult<Self::UnwindRule>, PeUnwinderError> |
| 97 |
where |
| 98 |
F: FnMut(u64) -> Result<u64, ()>, |
| 99 |
D: core::ops::Deref<Target = [u8]>; |
| 100 |
} |