Debian Codegraph @

third_party/rust/framehop/src/x86_64/macho.rs

raw
1 use super::arch::ArchX86_64;
2 use super::unwind_rule::UnwindRuleX86_64;
3 use crate::instruction_analysis::InstructionAnalysis;
4 use crate::macho::{CompactUnwindInfoUnwinderError, CompactUnwindInfoUnwinding, CuiUnwindResult};
5 use macho_unwind_info::opcodes::{OpcodeX86_64, RegisterNameX86_64};
6 use macho_unwind_info::Function;
7
8 impl CompactUnwindInfoUnwinding for ArchX86_64 {
9 fn unwind_frame(
10 function: Function,
11 is_first_frame: bool,
12 address_offset_within_function: usize,
13 function_bytes: Option<&[u8]>,
14 ) -> Result<CuiUnwindResult<UnwindRuleX86_64>, CompactUnwindInfoUnwinderError> {
15 let opcode = OpcodeX86_64::parse(function.opcode);
16 if is_first_frame {
17 // The pc might be in a prologue or an epilogue. The compact unwind info format ignores
18 // prologues and epilogues; the opcodes only describe the function body. So we do some
19 // instruction analysis to check for prologues and epilogues.
20 if let Some(function_bytes) = function_bytes {
21 if let Some(rule) = Self::rule_from_instruction_analysis(
22 function_bytes,
23 address_offset_within_function,
24 ) {
25 // We are inside a prologue / epilogue. Ignore the opcode and use the rule from
26 // instruction analysis.
27 return Ok(CuiUnwindResult::ExecRule(rule));
28 }
29 if opcode == OpcodeX86_64::Null
30 && function_bytes.starts_with(&[0x55, 0x48, 0x89, 0xe5])
31 {
32 // The function is uncovered but it has a `push rbp; mov rbp, rsp` prologue.
33 return Ok(CuiUnwindResult::ExecRule(UnwindRuleX86_64::UseFramePointer));
34 }
35 }
36 if opcode == OpcodeX86_64::Null {
37 return Ok(CuiUnwindResult::ExecRule(UnwindRuleX86_64::JustReturn));
38 }
39 }
40
41 // At this point we know with high certainty that we are in a function body.
42 let r = match opcode {
43 OpcodeX86_64::Null => {
44 return Err(CompactUnwindInfoUnwinderError::FunctionHasNoInfo);
45 }
46 OpcodeX86_64::FramelessImmediate {
47 stack_size_in_bytes,
48 saved_regs,
49 } => {
50 if stack_size_in_bytes == 8 {
51 CuiUnwindResult::ExecRule(UnwindRuleX86_64::JustReturn)
52 } else {
53 let bp_positon_from_outside = saved_regs
54 .iter()
55 .rev()
56 .flatten()
57 .position(|r| *r == RegisterNameX86_64::Rbp);
58 match bp_positon_from_outside {
59 Some(pos) => {
60 let bp_offset_from_sp =
61 stack_size_in_bytes as i32 - 2 * 8 - pos as i32 * 8;
62 let bp_storage_offset_from_sp_by_8 =
63 i16::try_from(bp_offset_from_sp / 8).map_err(|_| {
64 CompactUnwindInfoUnwinderError::BpOffsetDoesNotFit
65 })?;
66 CuiUnwindResult::ExecRule(UnwindRuleX86_64::OffsetSpAndRestoreBp {
67 sp_offset_by_8: stack_size_in_bytes / 8,
68 bp_storage_offset_from_sp_by_8,
69 })
70 }
71 None => CuiUnwindResult::ExecRule(UnwindRuleX86_64::OffsetSp {
72 sp_offset_by_8: stack_size_in_bytes / 8,
73 }),
74 }
75 }
76 }
77 OpcodeX86_64::FramelessIndirect {
78 immediate_offset_from_function_start,
79 stack_adjust_in_bytes,
80 saved_regs,
81 } => {
82 let function_bytes = function_bytes.ok_or(
83 CompactUnwindInfoUnwinderError::NoTextBytesToLookUpIndirectStackOffset,
84 )?;
85 let sub_immediate_bytes = function_bytes
86 .get(
87 immediate_offset_from_function_start as usize
88 ..immediate_offset_from_function_start as usize + 4,
89 )
90 .ok_or(CompactUnwindInfoUnwinderError::IndirectStackOffsetOutOfBounds)?;
91 let sub_immediate = u32::from_le_bytes([
92 sub_immediate_bytes[0],
93 sub_immediate_bytes[1],
94 sub_immediate_bytes[2],
95 sub_immediate_bytes[3],
96 ]);
97 let stack_size_in_bytes =
98 sub_immediate
99 .checked_add(stack_adjust_in_bytes.into())
100 .ok_or(CompactUnwindInfoUnwinderError::StackAdjustOverflow)?;
101 let sp_offset_by_8 = u16::try_from(stack_size_in_bytes / 8)
102 .map_err(|_| CompactUnwindInfoUnwinderError::StackSizeDoesNotFit)?;
103 let bp_positon_from_outside = saved_regs
104 .iter()
105 .rev()
106 .flatten()
107 .position(|r| *r == RegisterNameX86_64::Rbp);
108 match bp_positon_from_outside {
109 Some(pos) => {
110 let bp_offset_from_sp = stack_size_in_bytes as i32 - 2 * 8 - pos as i32 * 8;
111 let bp_storage_offset_from_sp_by_8 =
112 i16::try_from(bp_offset_from_sp / 8)
113 .map_err(|_| CompactUnwindInfoUnwinderError::BpOffsetDoesNotFit)?;
114 CuiUnwindResult::ExecRule(UnwindRuleX86_64::OffsetSpAndRestoreBp {
115 sp_offset_by_8,
116 bp_storage_offset_from_sp_by_8,
117 })
118 }
119 None => {
120 CuiUnwindResult::ExecRule(UnwindRuleX86_64::OffsetSp { sp_offset_by_8 })
121 }
122 }
123 }
124 OpcodeX86_64::Dwarf { eh_frame_fde } => CuiUnwindResult::NeedDwarf(eh_frame_fde),
125 OpcodeX86_64::FrameBased { .. } => {
126 CuiUnwindResult::ExecRule(UnwindRuleX86_64::UseFramePointer)
127 }
128 OpcodeX86_64::UnrecognizedKind(kind) => {
129 return Err(CompactUnwindInfoUnwinderError::BadOpcodeKind(kind))
130 }
131 OpcodeX86_64::InvalidFrameless => {
132 return Err(CompactUnwindInfoUnwinderError::InvalidFrameless)
133 }
134 };
135 Ok(r)
136 }
137
138 fn rule_for_stub_helper(
139 offset: u32,
140 ) -> Result<CuiUnwindResult<UnwindRuleX86_64>, CompactUnwindInfoUnwinderError> {
141 // shared:
142 // +0x0 235cc4 4C 8D 1D 3D 03 04 00 lea r11, qword [dyld_stub_binder_276000+8]
143 // +0x7 235ccb 41 53 push r11
144 // +0x9 235ccd FF 25 2D 03 04 00 jmp qword [dyld_stub_binder_276000] ; tail call
145 // +0xf 235cd3 90 nop
146 // first stub:
147 // +0x10 235cd4 68 F1 61 00 00 push 0x61f1
148 // +0x15 235cd9 E9 E6 FF FF FF jmp 0x235cc4 ; jump to shared
149 // second stub:
150 // +0x1a 235cde 68 38 62 00 00 push 0x6238
151 // +0x1f 235ce3 E9 DC FF FF FF jmp 0x235cc4 ; jump to shared
152 let rule = if offset < 0x7 {
153 // pop 1 and return
154 UnwindRuleX86_64::OffsetSp { sp_offset_by_8: 2 }
155 } else if offset < 0x10 {
156 // pop 2 and return
157 UnwindRuleX86_64::OffsetSp { sp_offset_by_8: 3 }
158 } else {
159 let offset_after_shared = offset - 0x10;
160 let offset_within_stub = offset_after_shared % 10;
161 if offset_within_stub < 5 {
162 UnwindRuleX86_64::JustReturn
163 // just return
164 } else {
165 // pop 1 and return
166 UnwindRuleX86_64::OffsetSp { sp_offset_by_8: 2 }
167 }
168 };
169 Ok(CuiUnwindResult::ExecRule(rule))
170 }
171 }