Skip to content

Commit

Permalink
read/cfi: consistently use O: ReaderOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
philipc committed Mar 14, 2024
1 parent 9dc496f commit 18eaae4
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions src/read/cfi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,9 +1898,9 @@ You may want to supply your own storage type for one of the following reasons:
/// #
/// struct StoreOnStack;
///
/// impl<R: ReaderOffset> UnwindContextStorage<R> for StoreOnStack {
/// type Rules = [(Register, RegisterRule<R>); 192];
/// type Stack = [UnwindTableRow<R, Self>; 4];
/// impl<O: ReaderOffset> UnwindContextStorage<O> for StoreOnStack {
/// type Rules = [(Register, RegisterRule<O>); 192];
/// type Stack = [UnwindTableRow<O, Self>; 4];
/// }
///
/// let mut ctx = UnwindContext::<_, StoreOnStack>::new_in();
Expand All @@ -1915,14 +1915,14 @@ You may want to supply your own storage type for one of the following reasons:
/// # unreachable!()
/// # }
/// ```
pub trait UnwindContextStorage<R: ReaderOffset>: Sized {
pub trait UnwindContextStorage<O: ReaderOffset>: Sized {
/// The storage used for register rules in a unwind table row.
///
/// Note that this is nested within the stack.
type Rules: ArrayLike<Item = (Register, RegisterRule<R>)>;
type Rules: ArrayLike<Item = (Register, RegisterRule<O>)>;

/// The storage used for unwind table row stack.
type Stack: ArrayLike<Item = UnwindTableRow<R, Self>>;
type Stack: ArrayLike<Item = UnwindTableRow<O, Self>>;
}

#[cfg(feature = "read")]
Expand All @@ -1931,9 +1931,9 @@ const MAX_RULES: usize = 192;
const MAX_UNWIND_STACK_DEPTH: usize = 4;

#[cfg(feature = "read")]
impl<R: ReaderOffset> UnwindContextStorage<R> for StoreOnHeap {
type Rules = [(Register, RegisterRule<R>); MAX_RULES];
type Stack = Box<[UnwindTableRow<R, Self>; MAX_UNWIND_STACK_DEPTH]>;
impl<O: ReaderOffset> UnwindContextStorage<O> for StoreOnHeap {
type Rules = [(Register, RegisterRule<O>); MAX_RULES];
type Stack = Box<[UnwindTableRow<O, Self>; MAX_UNWIND_STACK_DEPTH]>;
}

/// Common context needed when evaluating the call frame unwinding information.
Expand Down Expand Up @@ -1970,7 +1970,7 @@ impl<R: ReaderOffset> UnwindContextStorage<R> for StoreOnHeap {
/// # }
/// ```
#[derive(Clone, PartialEq, Eq)]
pub struct UnwindContext<R: ReaderOffset, A: UnwindContextStorage<R> = StoreOnHeap> {
pub struct UnwindContext<O: ReaderOffset, A: UnwindContextStorage<O> = StoreOnHeap> {
// Stack of rows. The last row is the row currently being built by the
// program. There is always at least one row. The vast majority of CFI
// programs will only ever have one row on the stack.
Expand All @@ -1984,12 +1984,12 @@ pub struct UnwindContext<R: ReaderOffset, A: UnwindContextStorage<R> = StoreOnHe
// `DW_CFA_restore`. Otherwise, when we are currently evaluating a CIE's
// initial instructions, `is_initialized` will be `false` and initial rules
// cannot be read.
initial_rule: Option<(Register, RegisterRule<R>)>,
initial_rule: Option<(Register, RegisterRule<O>)>,

is_initialized: bool,
}

impl<R: ReaderOffset, S: UnwindContextStorage<R>> Debug for UnwindContext<R, S> {
impl<O: ReaderOffset, S: UnwindContextStorage<O>> Debug for UnwindContext<O, S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("UnwindContext")
.field("stack", &self.stack)
Expand All @@ -1999,14 +1999,14 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> Debug for UnwindContext<R, S>
}
}

impl<R: ReaderOffset, A: UnwindContextStorage<R>> Default for UnwindContext<R, A> {
impl<O: ReaderOffset, A: UnwindContextStorage<O>> Default for UnwindContext<O, A> {
fn default() -> Self {
Self::new_in()
}
}

#[cfg(feature = "read")]
impl<R: ReaderOffset> UnwindContext<R> {
impl<O: ReaderOffset> UnwindContext<O> {
/// Construct a new call frame unwinding context.
pub fn new() -> Self {
Self::new_in()
Expand Down Expand Up @@ -2515,27 +2515,27 @@ impl<'a, 'ctx, R: Reader, A: UnwindContextStorage<R::Offset>> UnwindTable<'a, 'c
// - https://github.com/libunwind/libunwind/blob/11fd461095ea98f4b3e3a361f5a8a558519363fa/include/tdep-aarch64/dwarf-config.h#L32
// - https://github.com/libunwind/libunwind/blob/11fd461095ea98f4b3e3a361f5a8a558519363fa/include/tdep-arm/dwarf-config.h#L31
// - https://github.com/libunwind/libunwind/blob/11fd461095ea98f4b3e3a361f5a8a558519363fa/include/tdep-mips/dwarf-config.h#L31
struct RegisterRuleMap<R: ReaderOffset, S: UnwindContextStorage<R> = StoreOnHeap> {
struct RegisterRuleMap<O: ReaderOffset, S: UnwindContextStorage<O> = StoreOnHeap> {
rules: ArrayVec<S::Rules>,
}

impl<R: ReaderOffset, S: UnwindContextStorage<R>> Debug for RegisterRuleMap<R, S> {
impl<O: ReaderOffset, S: UnwindContextStorage<O>> Debug for RegisterRuleMap<O, S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("RegisterRuleMap")
.field("rules", &self.rules)
.finish()
}
}

impl<R: ReaderOffset, S: UnwindContextStorage<R>> Clone for RegisterRuleMap<R, S> {
impl<O: ReaderOffset, S: UnwindContextStorage<O>> Clone for RegisterRuleMap<O, S> {
fn clone(&self) -> Self {
Self {
rules: self.rules.clone(),
}
}
}

impl<R: ReaderOffset, S: UnwindContextStorage<R>> Default for RegisterRuleMap<R, S> {
impl<O: ReaderOffset, S: UnwindContextStorage<O>> Default for RegisterRuleMap<O, S> {
fn default() -> Self {
RegisterRuleMap {
rules: Default::default(),
Expand All @@ -2547,12 +2547,12 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> Default for RegisterRuleMap<R,
///
/// These methods are guaranteed not to allocate, acquire locks, or perform any
/// other signal-unsafe operations.
impl<R: ReaderOffset, S: UnwindContextStorage<R>> RegisterRuleMap<R, S> {
impl<O: ReaderOffset, S: UnwindContextStorage<O>> RegisterRuleMap<O, S> {
fn is_default(&self) -> bool {
self.rules.is_empty()
}

fn get(&self, register: Register) -> RegisterRule<R> {
fn get(&self, register: Register) -> RegisterRule<O> {
self.rules
.iter()
.find(|rule| rule.0 == register)
Expand All @@ -2563,7 +2563,7 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> RegisterRuleMap<R, S> {
.unwrap_or(RegisterRule::Undefined)
}

fn set(&mut self, register: Register, rule: RegisterRule<R>) -> Result<()> {
fn set(&mut self, register: Register, rule: RegisterRule<O>) -> Result<()> {
if !rule.is_defined() {
let idx = self
.rules
Expand All @@ -2590,7 +2590,7 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> RegisterRuleMap<R, S> {
.map_err(|_| Error::TooManyRegisterRules)
}

fn iter(&self) -> RegisterRuleIter<R> {
fn iter(&self) -> RegisterRuleIter<O> {
RegisterRuleIter(self.rules.iter())
}
}
Expand All @@ -2616,9 +2616,9 @@ where
}
}

impl<R, S: UnwindContextStorage<R>> PartialEq for RegisterRuleMap<R, S>
impl<O, S: UnwindContextStorage<O>> PartialEq for RegisterRuleMap<O, S>
where
R: ReaderOffset + PartialEq,
O: ReaderOffset + PartialEq,
{
fn eq(&self, rhs: &Self) -> bool {
for &(reg, ref rule) in &*self.rules {
Expand All @@ -2639,16 +2639,16 @@ where
}
}

impl<R, S: UnwindContextStorage<R>> Eq for RegisterRuleMap<R, S> where R: ReaderOffset + Eq {}
impl<O, S: UnwindContextStorage<O>> Eq for RegisterRuleMap<O, S> where O: ReaderOffset + Eq {}

/// An unordered iterator for register rules.
#[derive(Debug, Clone)]
pub struct RegisterRuleIter<'iter, R>(::core::slice::Iter<'iter, (Register, RegisterRule<R>)>)
pub struct RegisterRuleIter<'iter, O>(::core::slice::Iter<'iter, (Register, RegisterRule<O>)>)
where
R: ReaderOffset;
O: ReaderOffset;

impl<'iter, R: ReaderOffset> Iterator for RegisterRuleIter<'iter, R> {
type Item = &'iter (Register, RegisterRule<R>);
impl<'iter, O: ReaderOffset> Iterator for RegisterRuleIter<'iter, O> {
type Item = &'iter (Register, RegisterRule<O>);

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
Expand All @@ -2658,15 +2658,15 @@ impl<'iter, R: ReaderOffset> Iterator for RegisterRuleIter<'iter, R> {
/// A row in the virtual unwind table that describes how to find the values of
/// the registers in the *previous* frame for a range of PC addresses.
#[derive(PartialEq, Eq)]
pub struct UnwindTableRow<R: ReaderOffset, S: UnwindContextStorage<R> = StoreOnHeap> {
pub struct UnwindTableRow<O: ReaderOffset, S: UnwindContextStorage<O> = StoreOnHeap> {
start_address: u64,
end_address: u64,
saved_args_size: u64,
cfa: CfaRule<R>,
registers: RegisterRuleMap<R, S>,
cfa: CfaRule<O>,
registers: RegisterRuleMap<O, S>,
}

impl<R: ReaderOffset, S: UnwindContextStorage<R>> Debug for UnwindTableRow<R, S> {
impl<O: ReaderOffset, S: UnwindContextStorage<O>> Debug for UnwindTableRow<O, S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("UnwindTableRow")
.field("start_address", &self.start_address)
Expand All @@ -2678,7 +2678,7 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> Debug for UnwindTableRow<R, S>
}
}

impl<R: ReaderOffset, S: UnwindContextStorage<R>> Clone for UnwindTableRow<R, S> {
impl<O: ReaderOffset, S: UnwindContextStorage<O>> Clone for UnwindTableRow<O, S> {
fn clone(&self) -> Self {
Self {
start_address: self.start_address,
Expand All @@ -2690,7 +2690,7 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> Clone for UnwindTableRow<R, S>
}
}

impl<R: ReaderOffset, S: UnwindContextStorage<R>> Default for UnwindTableRow<R, S> {
impl<O: ReaderOffset, S: UnwindContextStorage<O>> Default for UnwindTableRow<O, S> {
fn default() -> Self {
UnwindTableRow {
start_address: 0,
Expand All @@ -2702,7 +2702,7 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> Default for UnwindTableRow<R,
}
}

impl<R: ReaderOffset, S: UnwindContextStorage<R>> UnwindTableRow<R, S> {
impl<O: ReaderOffset, S: UnwindContextStorage<O>> UnwindTableRow<O, S> {
fn is_default(&self) -> bool {
self.start_address == 0
&& self.end_address == 0
Expand Down Expand Up @@ -2741,7 +2741,7 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> UnwindTableRow<R, S> {
}

/// Get the canonical frame address (CFA) recovery rule for this row.
pub fn cfa(&self) -> &CfaRule<R> {
pub fn cfa(&self) -> &CfaRule<O> {
&self.cfa
}

Expand Down Expand Up @@ -2789,7 +2789,7 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> UnwindTableRow<R, S> {
/// > <tr><td>Vector Mask Registers 0–7</td> <td>118-125</td> <td>%k0–%k7</td></tr>
/// > <tr><td>Reserved</td> <td>126-129</td> <td></td></tr>
/// > </table>
pub fn register(&self, register: Register) -> RegisterRule<R> {
pub fn register(&self, register: Register) -> RegisterRule<O> {
self.registers.get(register)
}

Expand All @@ -2808,14 +2808,14 @@ impl<R: ReaderOffset, S: UnwindContextStorage<R>> UnwindTableRow<R, S> {
/// }
/// # }
/// ```
pub fn registers(&self) -> RegisterRuleIter<R> {
pub fn registers(&self) -> RegisterRuleIter<O> {
self.registers.iter()
}
}

/// The canonical frame address (CFA) recovery rules.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CfaRule<R: ReaderOffset> {
pub enum CfaRule<O: ReaderOffset> {
/// The CFA is given offset from the given register's value.
RegisterAndOffset {
/// The register containing the base value.
Expand All @@ -2825,10 +2825,10 @@ pub enum CfaRule<R: ReaderOffset> {
},
/// The CFA is obtained by evaluating this `Reader` as a DWARF expression
/// program.
Expression(UnwindExpression<R>),
Expression(UnwindExpression<O>),
}

impl<R: ReaderOffset> Default for CfaRule<R> {
impl<O: ReaderOffset> Default for CfaRule<O> {
fn default() -> Self {
CfaRule::RegisterAndOffset {
register: Register(0),
Expand All @@ -2837,7 +2837,7 @@ impl<R: ReaderOffset> Default for CfaRule<R> {
}
}

impl<R: ReaderOffset> CfaRule<R> {
impl<O: ReaderOffset> CfaRule<O> {
fn is_default(&self) -> bool {
match *self {
CfaRule::RegisterAndOffset { register, offset } => {
Expand All @@ -2856,7 +2856,7 @@ impl<R: ReaderOffset> CfaRule<R> {
/// previous frame."
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RegisterRule<R: ReaderOffset> {
pub enum RegisterRule<O: ReaderOffset> {
/// > A register that has this rule has no recoverable value in the previous
/// > frame. (By convention, it is not preserved by a callee.)
Undefined,
Expand All @@ -2880,11 +2880,11 @@ pub enum RegisterRule<R: ReaderOffset> {

/// "The previous value of this register is located at the address produced
/// by executing the DWARF expression."
Expression(UnwindExpression<R>),
Expression(UnwindExpression<O>),

/// "The previous value of this register is the value produced by executing
/// the DWARF expression."
ValExpression(UnwindExpression<R>),
ValExpression(UnwindExpression<O>),

/// "The rule is defined externally to this specification by the augmenter."
Architectural,
Expand All @@ -2893,7 +2893,7 @@ pub enum RegisterRule<R: ReaderOffset> {
Constant(u64),
}

impl<R: ReaderOffset> RegisterRule<R> {
impl<O: ReaderOffset> RegisterRule<O> {
fn is_defined(&self) -> bool {
!matches!(*self, RegisterRule::Undefined)
}
Expand All @@ -2902,7 +2902,7 @@ impl<R: ReaderOffset> RegisterRule<R> {
/// A parsed call frame instruction.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CallFrameInstruction<R: ReaderOffset> {
pub enum CallFrameInstruction<O: ReaderOffset> {
// 6.4.2.1 Row Creation Methods
/// > 1. DW_CFA_set_loc
/// >
Expand Down Expand Up @@ -3007,7 +3007,7 @@ pub enum CallFrameInstruction<R: ReaderOffset> {
/// > means by which the current CFA is computed.
DefCfaExpression {
/// The location of the DWARF expression.
expression: UnwindExpression<R>,
expression: UnwindExpression<O>,
},

// 6.4.2.3 Register Rule Instructions
Expand Down Expand Up @@ -3118,7 +3118,7 @@ pub enum CallFrameInstruction<R: ReaderOffset> {
/// The target register's number.
register: Register,
/// The location of the DWARF expression.
expression: UnwindExpression<R>,
expression: UnwindExpression<O>,
},

/// > 10. DW_CFA_val_expression
Expand All @@ -3135,7 +3135,7 @@ pub enum CallFrameInstruction<R: ReaderOffset> {
/// The target register's number.
register: Register,
/// The location of the DWARF expression.
expression: UnwindExpression<R>,
expression: UnwindExpression<O>,
},

/// The `Restore` instruction represents both `DW_CFA_restore` and
Expand Down

0 comments on commit 18eaae4

Please sign in to comment.