• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 super::DwarfReader;
15 use core::mem;
16 use core::ptr;
17 
18 pub const DW_EH_PE_omit: u8 = 0xFF;
19 pub const DW_EH_PE_absptr: u8 = 0x00;
20 
21 pub const DW_EH_PE_uleb128: u8 = 0x01;
22 pub const DW_EH_PE_udata2: u8 = 0x02;
23 pub const DW_EH_PE_udata4: u8 = 0x03;
24 pub const DW_EH_PE_udata8: u8 = 0x04;
25 pub const DW_EH_PE_sleb128: u8 = 0x09;
26 pub const DW_EH_PE_sdata2: u8 = 0x0A;
27 pub const DW_EH_PE_sdata4: u8 = 0x0B;
28 pub const DW_EH_PE_sdata8: u8 = 0x0C;
29 
30 pub const DW_EH_PE_pcrel: u8 = 0x10;
31 pub const DW_EH_PE_textrel: u8 = 0x20;
32 pub const DW_EH_PE_datarel: u8 = 0x30;
33 pub const DW_EH_PE_funcrel: u8 = 0x40;
34 pub const DW_EH_PE_aligned: u8 = 0x50;
35 
36 pub const DW_EH_PE_indirect: u8 = 0x80;
37 
38 #[derive(Copy, Clone)]
39 pub struct EHContext<'a> {
40     pub ip: usize,                             // Current instruction pointer
41     pub func_start: usize,                     // Address of the current function
42     pub get_text_start: &'a dyn Fn() -> usize, // Get address of the code section
43     pub get_data_start: &'a dyn Fn() -> usize, // Get address of the data section
44 }
45 
46 pub enum EHAction {
47     None,
48     Cleanup(usize),
49     Catch(usize),
50     Filter(usize),
51     Terminate,
52 }
53 
54 pub const USING_SJLJ_EXCEPTIONS: bool = cfg!(all(target_os = "ios", target_arch = "arm"));
55 
find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result<EHAction, ()>56 pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result<EHAction, ()> {
57     if lsda.is_null() {
58         return Ok(EHAction::None);
59     }
60 
61     let func_start = context.func_start;
62     let mut reader = DwarfReader::new(lsda);
63 
64     let start_encoding = reader.read::<u8>();
65     // base address for landing pad offsets
66     let lpad_base = if start_encoding != DW_EH_PE_omit {
67         read_encoded_pointer(&mut reader, context, start_encoding)?
68     } else {
69         func_start
70     };
71 
72     let ttype_encoding = reader.read::<u8>();
73     if ttype_encoding != DW_EH_PE_omit {
74         // Rust doesn't analyze exception types, so we don't care about the type table
75         reader.read_uleb128();
76     }
77 
78     let call_site_encoding = reader.read::<u8>();
79     let call_site_table_length = reader.read_uleb128();
80     let action_table = reader.ptr.add(call_site_table_length as usize);
81     let ip = context.ip;
82 
83     if !USING_SJLJ_EXCEPTIONS {
84         while reader.ptr < action_table {
85             let cs_start = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
86             let cs_len = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
87             let cs_lpad = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
88             let cs_action_entry = reader.read_uleb128();
89             // Callsite table is sorted by cs_start, so if we've passed the ip, we
90             // may stop searching.
91             if ip < func_start + cs_start {
92                 break;
93             }
94             if ip < func_start + cs_start + cs_len {
95                 if cs_lpad == 0 {
96                     return Ok(EHAction::None);
97                 } else {
98                     let lpad = lpad_base + cs_lpad;
99                     return Ok(interpret_cs_action(action_table as *mut u8, cs_action_entry, lpad));
100                 }
101             }
102         }
103         // Ip is not present in the table. This indicates a nounwind call.
104         Ok(EHAction::Terminate)
105     } else {
106         // SjLj version:
107         // The "IP" is an index into the call-site table, with two exceptions:
108         // -1 means 'no-action', and 0 means 'terminate'.
109         match ip as isize {
110             -1 => return Ok(EHAction::None),
111             0 => return Ok(EHAction::Terminate),
112             _ => (),
113         }
114         let mut idx = ip;
115         loop {
116             let cs_lpad = reader.read_uleb128();
117             let cs_action_entry = reader.read_uleb128();
118             idx -= 1;
119             if idx == 0 {
120                 // Can never have null landing pad for sjlj -- that would have
121                 // been indicated by a -1 call site index.
122                 let lpad = (cs_lpad + 1) as usize;
123                 return Ok(interpret_cs_action(action_table as *mut u8, cs_action_entry, lpad));
124             }
125         }
126     }
127 }
128 
interpret_cs_action( action_table: *mut u8, cs_action_entry: u64, lpad: usize, ) -> EHAction129 unsafe fn interpret_cs_action(
130     action_table: *mut u8,
131     cs_action_entry: u64,
132     lpad: usize,
133 ) -> EHAction {
134     if cs_action_entry == 0 {
135         // If cs_action_entry is 0 then this is a cleanup (Drop::drop). We run these
136         // for both Rust panics and foreign exceptions.
137         EHAction::Cleanup(lpad)
138     } else {
139         // If lpad != 0 and cs_action_entry != 0, we have to check ttype_index.
140         // If ttype_index == 0 under the condition, we take cleanup action.
141         let action_record = (action_table as *mut u8).offset(cs_action_entry as isize - 1);
142         let mut action_reader = DwarfReader::new(action_record);
143         let ttype_index = action_reader.read_sleb128();
144         if ttype_index == 0 {
145             EHAction::Cleanup(lpad)
146         } else if ttype_index > 0 {
147             // Stop unwinding Rust panics at catch_unwind.
148             EHAction::Catch(lpad)
149         } else {
150             EHAction::Filter(lpad)
151         }
152     }
153 }
154 
155 #[inline]
round_up(unrounded: usize, align: usize) -> Result<usize, ()>156 fn round_up(unrounded: usize, align: usize) -> Result<usize, ()> {
157     if align.is_power_of_two() { Ok((unrounded + align - 1) & !(align - 1)) } else { Err(()) }
158 }
159 
read_encoded_pointer( reader: &mut DwarfReader, context: &EHContext<'_>, encoding: u8, ) -> Result<usize, ()>160 unsafe fn read_encoded_pointer(
161     reader: &mut DwarfReader,
162     context: &EHContext<'_>,
163     encoding: u8,
164 ) -> Result<usize, ()> {
165     if encoding == DW_EH_PE_omit {
166         return Err(());
167     }
168 
169     // DW_EH_PE_aligned implies it's an absolute pointer value
170     if encoding == DW_EH_PE_aligned {
171         reader.ptr = reader.ptr.with_addr(round_up(reader.ptr.addr(), mem::size_of::<usize>())?);
172         return Ok(reader.read::<usize>());
173     }
174 
175     let mut result = match encoding & 0x0F {
176         DW_EH_PE_absptr => reader.read::<usize>(),
177         DW_EH_PE_uleb128 => reader.read_uleb128() as usize,
178         DW_EH_PE_udata2 => reader.read::<u16>() as usize,
179         DW_EH_PE_udata4 => reader.read::<u32>() as usize,
180         DW_EH_PE_udata8 => reader.read::<u64>() as usize,
181         DW_EH_PE_sleb128 => reader.read_sleb128() as usize,
182         DW_EH_PE_sdata2 => reader.read::<i16>() as usize,
183         DW_EH_PE_sdata4 => reader.read::<i32>() as usize,
184         DW_EH_PE_sdata8 => reader.read::<i64>() as usize,
185         _ => return Err(()),
186     };
187 
188     result += match encoding & 0x70 {
189         DW_EH_PE_absptr => 0,
190         // relative to address of the encoded value, despite the name
191         DW_EH_PE_pcrel => reader.ptr.expose_addr(),
192         DW_EH_PE_funcrel => {
193             if context.func_start == 0 {
194                 return Err(());
195             }
196             context.func_start
197         }
198         DW_EH_PE_textrel => (*context.get_text_start)(),
199         DW_EH_PE_datarel => (*context.get_data_start)(),
200         _ => return Err(()),
201     };
202 
203     if encoding & DW_EH_PE_indirect != 0 {
204         result = *ptr::from_exposed_addr::<usize>(result);
205     }
206 
207     Ok(result)
208 }
209