| 1 |
use core::marker::PhantomData; |
| 2 |
|
| 3 |
use crate::dwarf::DwarfUnwinderError; |
| 4 |
use crate::{arch::Arch, unwind_rule::UnwindRule}; |
| 5 |
use macho_unwind_info::UnwindInfo; |
| 6 |
|
| 7 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 8 |
pub enum CompactUnwindInfoUnwinderError { |
| 9 |
BadFormat(macho_unwind_info::Error), |
| 10 |
AddressOutsideRange(u32), |
| 11 |
CallerCannotBeFrameless, |
| 12 |
FunctionHasNoInfo, |
| 13 |
BpOffsetDoesNotFit, |
| 14 |
BadOpcodeKind(u8), |
| 15 |
BadDwarfUnwinding(DwarfUnwinderError), |
| 16 |
NoTextBytesToLookUpIndirectStackOffset, |
| 17 |
IndirectStackOffsetOutOfBounds, |
| 18 |
StackAdjustOverflow, |
| 19 |
StackSizeDoesNotFit, |
| 20 |
StubFunctionCannotBeCaller, |
| 21 |
InvalidFrameless, |
| 22 |
} |
| 23 |
|
| 24 |
impl core::fmt::Display for CompactUnwindInfoUnwinderError { |
| 25 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 26 |
match self { |
| 27 |
Self::BadFormat(err) => write!(f, "Bad __unwind_info format: {err}"), |
| 28 |
Self::AddressOutsideRange(addr) => write!(f, "Address 0x{addr:x} outside of the range covered by __unwind_info"), |
| 29 |
Self::CallerCannotBeFrameless => write!(f, "Encountered a non-leaf function which was marked as frameless."), |
| 30 |
Self::FunctionHasNoInfo => write!(f, "No unwind info (null opcode) for this function in __unwind_info"), |
| 31 |
Self::BpOffsetDoesNotFit => write!(f, "rbp offset from the stack pointer divided by 8 does not fit into i16"), |
| 32 |
Self::BadOpcodeKind(kind) => write!(f, "Unrecognized __unwind_info opcode kind {kind}"), |
| 33 |
Self::BadDwarfUnwinding(err) => write!(f, "DWARF unwinding failed: {err}"), |
| 34 |
Self::NoTextBytesToLookUpIndirectStackOffset => write!(f, "Don't have the function bytes to look up the offset for frameless function with indirect stack offset"), |
| 35 |
Self::IndirectStackOffsetOutOfBounds => write!(f, "Stack offset not found inside the bounds of the text bytes"), |
| 36 |
Self::StackAdjustOverflow => write!(f, "Stack adjust addition overflowed"), |
| 37 |
Self::StackSizeDoesNotFit => write!(f, "Stack size does not fit into the rule representation"), |
| 38 |
Self::StubFunctionCannotBeCaller => write!(f, "A caller had its address in the __stubs section"), |
| 39 |
Self::InvalidFrameless => write!(f, "Encountered invalid unwind entry"), |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl From<macho_unwind_info::Error> for CompactUnwindInfoUnwinderError { |
| 45 |
fn from(e: macho_unwind_info::Error) -> Self { |
| 46 |
Self::BadFormat(e) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl From<DwarfUnwinderError> for CompactUnwindInfoUnwinderError { |
| 51 |
fn from(e: DwarfUnwinderError) -> Self { |
| 52 |
Self::BadDwarfUnwinding(e) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[cfg(feature = "std")] |
| 57 |
impl std::error::Error for CompactUnwindInfoUnwinderError { |
| 58 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 59 |
match self { |
| 60 |
Self::BadFormat(e) => Some(e), |
| 61 |
Self::BadDwarfUnwinding(e) => Some(e), |
| 62 |
_ => None, |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#[derive(Clone, Debug)] |
| 68 |
pub enum CuiUnwindResult<R: UnwindRule> { |
| 69 |
ExecRule(R), |
| 70 |
NeedDwarf(u32), |
| 71 |
} |
| 72 |
|
| 73 |
pub trait CompactUnwindInfoUnwinding: Arch { |
| 74 |
fn unwind_frame( |
| 75 |
function: macho_unwind_info::Function, |
| 76 |
is_first_frame: bool, |
| 77 |
address_offset_within_function: usize, |
| 78 |
function_bytes: Option<&[u8]>, |
| 79 |
) -> Result<CuiUnwindResult<Self::UnwindRule>, CompactUnwindInfoUnwinderError>; |
| 80 |
|
| 81 |
fn rule_for_stub_helper( |
| 82 |
offset: u32, |
| 83 |
) -> Result<CuiUnwindResult<Self::UnwindRule>, CompactUnwindInfoUnwinderError>; |
| 84 |
} |
| 85 |
|
| 86 |
#[derive(Clone, Copy)] |
| 87 |
pub struct TextBytes<'a> { |
| 88 |
offset_from_base_address: u32, |
| 89 |
bytes: &'a [u8], |
| 90 |
} |
| 91 |
|
| 92 |
impl<'a> TextBytes<'a> { |
| 93 |
pub fn new(offset_from_base_address: u32, bytes: &'a [u8]) -> Self { |
| 94 |
Self { |
| 95 |
offset_from_base_address, |
| 96 |
bytes, |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
pub struct CompactUnwindInfoUnwinder<'a, A: CompactUnwindInfoUnwinding> { |
| 102 |
unwind_info_data: &'a [u8], |
| 103 |
text_bytes: Option<TextBytes<'a>>, |
| 104 |
stubs_range: (u32, u32), |
| 105 |
stub_helper_range: (u32, u32), |
| 106 |
_arch: PhantomData<A>, |
| 107 |
} |
| 108 |
|
| 109 |
impl<'a, A: CompactUnwindInfoUnwinding> CompactUnwindInfoUnwinder<'a, A> { |
| 110 |
pub fn new( |
| 111 |
unwind_info_data: &'a [u8], |
| 112 |
text_bytes: Option<TextBytes<'a>>, |
| 113 |
stubs_range: (u32, u32), |
| 114 |
stub_helper_range: (u32, u32), |
| 115 |
) -> Self { |
| 116 |
Self { |
| 117 |
unwind_info_data, |
| 118 |
text_bytes, |
| 119 |
stubs_range, |
| 120 |
stub_helper_range, |
| 121 |
_arch: PhantomData, |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
pub fn function_for_address( |
| 126 |
&self, |
| 127 |
address: u32, |
| 128 |
) -> Result<macho_unwind_info::Function, CompactUnwindInfoUnwinderError> { |
| 129 |
let unwind_info = UnwindInfo::parse(self.unwind_info_data) |
| 130 |
.map_err(CompactUnwindInfoUnwinderError::BadFormat)?; |
| 131 |
let function = unwind_info |
| 132 |
.lookup(address) |
| 133 |
.map_err(CompactUnwindInfoUnwinderError::BadFormat)?; |
| 134 |
function.ok_or(CompactUnwindInfoUnwinderError::AddressOutsideRange(address)) |
| 135 |
} |
| 136 |
|
| 137 |
pub fn unwind_frame( |
| 138 |
&mut self, |
| 139 |
rel_lookup_address: u32, |
| 140 |
is_first_frame: bool, |
| 141 |
) -> Result<CuiUnwindResult<A::UnwindRule>, CompactUnwindInfoUnwinderError> { |
| 142 |
// Exclude __stubs and __stub_helper sections. The __unwind_info does not describe those |
| 143 |
// sections. These sections need to be manually excluded because the addresses in |
| 144 |
// __unwind_info can be both before and after the stubs/stub_helper sections, if there is |
| 145 |
// both a __text and a text_env section. |
| 146 |
if self.stubs_range.0 <= rel_lookup_address && rel_lookup_address < self.stubs_range.1 { |
| 147 |
if !is_first_frame { |
| 148 |
return Err(CompactUnwindInfoUnwinderError::StubFunctionCannotBeCaller); |
| 149 |
} |
| 150 |
// All stub functions are frameless. |
| 151 |
return Ok(CuiUnwindResult::ExecRule( |
| 152 |
A::UnwindRule::rule_for_stub_functions(), |
| 153 |
)); |
| 154 |
} |
| 155 |
if self.stub_helper_range.0 <= rel_lookup_address |
| 156 |
&& rel_lookup_address < self.stub_helper_range.1 |
| 157 |
{ |
| 158 |
if !is_first_frame { |
| 159 |
return Err(CompactUnwindInfoUnwinderError::StubFunctionCannotBeCaller); |
| 160 |
} |
| 161 |
let lookup_address_relative_to_section = rel_lookup_address - self.stub_helper_range.0; |
| 162 |
return <A as CompactUnwindInfoUnwinding>::rule_for_stub_helper( |
| 163 |
lookup_address_relative_to_section, |
| 164 |
); |
| 165 |
} |
| 166 |
let function = match self.function_for_address(rel_lookup_address) { |
| 167 |
Ok(f) => f, |
| 168 |
Err(CompactUnwindInfoUnwinderError::AddressOutsideRange(_)) if is_first_frame => { |
| 169 |
// pc is falling into this module's address range, but it's not covered by __unwind_info. |
| 170 |
// This could mean that we're inside a stub function, in the __stubs section. |
| 171 |
// All stub functions are frameless. |
| 172 |
// TODO: Obtain the actual __stubs address range and do better checking here. |
| 173 |
return Ok(CuiUnwindResult::ExecRule( |
| 174 |
A::UnwindRule::rule_for_stub_functions(), |
| 175 |
)); |
| 176 |
} |
| 177 |
Err(err) => return Err(err), |
| 178 |
}; |
| 179 |
if is_first_frame && rel_lookup_address == function.start_address { |
| 180 |
return Ok(CuiUnwindResult::ExecRule( |
| 181 |
A::UnwindRule::rule_for_function_start(), |
| 182 |
)); |
| 183 |
} |
| 184 |
let address_offset_within_function = |
| 185 |
usize::try_from(rel_lookup_address - function.start_address).unwrap(); |
| 186 |
let function_bytes = self.text_bytes.and_then(|text_bytes| { |
| 187 |
let TextBytes { |
| 188 |
offset_from_base_address, |
| 189 |
bytes, |
| 190 |
} = text_bytes; |
| 191 |
let function_start_relative_to_text = function |
| 192 |
.start_address |
| 193 |
.checked_sub(offset_from_base_address)? |
| 194 |
as usize; |
| 195 |
let function_end_relative_to_text = |
| 196 |
function.end_address.checked_sub(offset_from_base_address)? as usize; |
| 197 |
bytes.get(function_start_relative_to_text..function_end_relative_to_text) |
| 198 |
}); |
| 199 |
<A as CompactUnwindInfoUnwinding>::unwind_frame( |
| 200 |
function, |
| 201 |
is_first_frame, |
| 202 |
address_offset_within_function, |
| 203 |
function_bytes, |
| 204 |
) |
| 205 |
} |
| 206 |
} |