1 //! Parsing of GCC-style Language-Specific Data Area (LSDA)
2 //! For details see:
3 //! * <https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html>
4 //! * <https://itanium-cxx-abi.github.io/cxx-abi/exceptions.pdf>
5 //! * <https://www.airs.com/blog/archives/460>
6 //! * <https://www.airs.com/blog/archives/464>
7 //!
8 //! A reference implementation may be found in the GCC source tree
9 //! (`<root>/libgcc/unwind-c.c` as of this writing).
10
11 #![allow(non_upper_case_globals)]
12 #![allow(unused)]
13
14 use crate::dwarf::DwarfReader;
15 use core::mem;
16
17 pub const DW_EH_PE_omit: u8 = 0xFF;
18 pub const DW_EH_PE_absptr: u8 = 0x00;
19
20 pub const DW_EH_PE_uleb128: u8 = 0x01;
21 pub const DW_EH_PE_udata2: u8 = 0x02;
22 pub const DW_EH_PE_udata4: u8 = 0x03;
23 pub const DW_EH_PE_udata8: u8 = 0x04;
24 pub const DW_EH_PE_sleb128: u8 = 0x09;
25 pub const DW_EH_PE_sdata2: u8 = 0x0A;
26 pub const DW_EH_PE_sdata4: u8 = 0x0B;
27 pub const DW_EH_PE_sdata8: u8 = 0x0C;
28
29 pub const DW_EH_PE_pcrel: u8 = 0x10;
30 pub const DW_EH_PE_textrel: u8 = 0x20;
31 pub const DW_EH_PE_datarel: u8 = 0x30;
32 pub const DW_EH_PE_funcrel: u8 = 0x40;
33 pub const DW_EH_PE_aligned: u8 = 0x50;
34
35 pub const DW_EH_PE_indirect: u8 = 0x80;
36
37 #[derive(Copy, Clone)]
38 pub struct EHContext<'a> {
39 pub ip: usize, // Current instruction pointer
40 pub func_start: usize, // Address of the current function
41 pub get_text_start: &'a dyn Fn() -> usize, // Get address of the code section
42 pub get_data_start: &'a dyn Fn() -> usize, // Get address of the data section
43 }
44
45 pub enum EHAction {
46 None,
47 Cleanup(usize),
48 Catch(usize),
49 Terminate,
50 }
51
52 pub const USING_SJLJ_EXCEPTIONS: bool = cfg!(all(target_os = "ios", target_arch = "arm"));
53
find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result<EHAction, ()>54 pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result<EHAction, ()> {
55 if lsda.is_null() {
56 return Ok(EHAction::None);
57 }
58
59 let func_start = context.func_start;
60 let mut reader = DwarfReader::new(lsda);
61
62 let start_encoding = reader.read::<u8>();
63 // base address for landing pad offsets
64 let lpad_base = if start_encoding != DW_EH_PE_omit {
65 read_encoded_pointer(&mut reader, context, start_encoding)?
66 } else {
67 func_start
68 };
69
70 let ttype_encoding = reader.read::<u8>();
71 if ttype_encoding != DW_EH_PE_omit {
72 // Rust doesn't analyze exception types, so we don't care about the type table
73 reader.read_uleb128();
74 }
75
76 let call_site_encoding = reader.read::<u8>();
77 let call_site_table_length = reader.read_uleb128();
78 let action_table = reader.ptr.offset(call_site_table_length as isize);
79 let ip = context.ip;
80
81 if !USING_SJLJ_EXCEPTIONS {
82 while reader.ptr < action_table {
83 let cs_start = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
84 let cs_len = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
85 let cs_lpad = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
86 let cs_action = reader.read_uleb128();
87 // Callsite table is sorted by cs_start, so if we've passed the ip, we
88 // may stop searching.
89 if ip < func_start + cs_start {
90 break;
91 }
92 if ip < func_start + cs_start + cs_len {
93 if cs_lpad == 0 {
94 return Ok(EHAction::None);
95 } else {
96 let lpad = lpad_base + cs_lpad;
97 return Ok(interpret_cs_action(cs_action, lpad));
98 }
99 }
100 }
101 // Ip is not present in the table. This should not happen... but it does: issue #35011.
102 // So rather than returning EHAction::Terminate, we do this.
103 Ok(EHAction::None)
104 } else {
105 // SjLj version:
106 // The "IP" is an index into the call-site table, with two exceptions:
107 // -1 means 'no-action', and 0 means 'terminate'.
108 match ip as isize {
109 -1 => return Ok(EHAction::None),
110 0 => return Ok(EHAction::Terminate),
111 _ => (),
112 }
113 let mut idx = ip;
114 loop {
115 let cs_lpad = reader.read_uleb128();
116 let cs_action = reader.read_uleb128();
117 idx -= 1;
118 if idx == 0 {
119 // Can never have null landing pad for sjlj -- that would have
120 // been indicated by a -1 call site index.
121 let lpad = (cs_lpad + 1) as usize;
122 return Ok(interpret_cs_action(cs_action, lpad));
123 }
124 }
125 }
126 }
127
interpret_cs_action(cs_action: u64, lpad: usize) -> EHAction128 fn interpret_cs_action(cs_action: u64, lpad: usize) -> EHAction {
129 if cs_action == 0 {
130 // If cs_action is 0 then this is a cleanup (Drop::drop). We run these
131 // for both Rust panics and foreign exceptions.
132 EHAction::Cleanup(lpad)
133 } else {
134 // Stop unwinding Rust panics at catch_unwind.
135 EHAction::Catch(lpad)
136 }
137 }
138
139 #[inline]
round_up(unrounded: usize, align: usize) -> Result<usize, ()>140 fn round_up(unrounded: usize, align: usize) -> Result<usize, ()> {
141 if align.is_power_of_two() { Ok((unrounded + align - 1) & !(align - 1)) } else { Err(()) }
142 }
143
read_encoded_pointer( reader: &mut DwarfReader, context: &EHContext<'_>, encoding: u8, ) -> Result<usize, ()>144 unsafe fn read_encoded_pointer(
145 reader: &mut DwarfReader,
146 context: &EHContext<'_>,
147 encoding: u8,
148 ) -> Result<usize, ()> {
149 if encoding == DW_EH_PE_omit {
150 return Err(());
151 }
152
153 // DW_EH_PE_aligned implies it's an absolute pointer value
154 if encoding == DW_EH_PE_aligned {
155 reader.ptr = round_up(reader.ptr as usize, mem::size_of::<usize>())? as *const u8;
156 return Ok(reader.read::<usize>());
157 }
158
159 let mut result = match encoding & 0x0F {
160 DW_EH_PE_absptr => reader.read::<usize>(),
161 DW_EH_PE_uleb128 => reader.read_uleb128() as usize,
162 DW_EH_PE_udata2 => reader.read::<u16>() as usize,
163 DW_EH_PE_udata4 => reader.read::<u32>() as usize,
164 DW_EH_PE_udata8 => reader.read::<u64>() as usize,
165 DW_EH_PE_sleb128 => reader.read_sleb128() as usize,
166 DW_EH_PE_sdata2 => reader.read::<i16>() as usize,
167 DW_EH_PE_sdata4 => reader.read::<i32>() as usize,
168 DW_EH_PE_sdata8 => reader.read::<i64>() as usize,
169 _ => return Err(()),
170 };
171
172 result += match encoding & 0x70 {
173 DW_EH_PE_absptr => 0,
174 // relative to address of the encoded value, despite the name
175 DW_EH_PE_pcrel => reader.ptr as usize,
176 DW_EH_PE_funcrel => {
177 if context.func_start == 0 {
178 return Err(());
179 }
180 context.func_start
181 }
182 DW_EH_PE_textrel => (*context.get_text_start)(),
183 DW_EH_PE_datarel => (*context.get_data_start)(),
184 _ => return Err(()),
185 };
186
187 if encoding & DW_EH_PE_indirect != 0 {
188 result = *(result as *const usize);
189 }
190
191 Ok(result)
192 }
193