]> git.lizzy.rs Git - rust.git/blob - library/std/src/personality/dwarf/eh.rs
Move to intepret_cs_action
[rust.git] / library / std / src / personality / dwarf / eh.rs
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     Terminate,
51 }
52
53 pub const USING_SJLJ_EXCEPTIONS: bool = cfg!(all(target_os = "ios", target_arch = "arm"));
54
55 pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result<EHAction, ()> {
56     if lsda.is_null() {
57         return Ok(EHAction::None);
58     }
59
60     let func_start = context.func_start;
61     let mut reader = DwarfReader::new(lsda);
62
63     let start_encoding = reader.read::<u8>();
64     // base address for landing pad offsets
65     let lpad_base = if start_encoding != DW_EH_PE_omit {
66         read_encoded_pointer(&mut reader, context, start_encoding)?
67     } else {
68         func_start
69     };
70
71     let ttype_encoding = reader.read::<u8>();
72     if ttype_encoding != DW_EH_PE_omit {
73         // Rust doesn't analyze exception types, so we don't care about the type table
74         reader.read_uleb128();
75     }
76
77     let call_site_encoding = reader.read::<u8>();
78     let call_site_table_length = reader.read_uleb128();
79     let action_table = reader.ptr.add(call_site_table_length as usize);
80     let ip = context.ip;
81
82     if !USING_SJLJ_EXCEPTIONS {
83         while reader.ptr < action_table {
84             let cs_start = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
85             let cs_len = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
86             let cs_lpad = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
87             let cs_action_entry = reader.read_uleb128();
88             // Callsite table is sorted by cs_start, so if we've passed the ip, we
89             // may stop searching.
90             if ip < func_start + cs_start {
91                 break;
92             }
93             if ip < func_start + cs_start + cs_len {
94                 if cs_lpad == 0 {
95                     return Ok(EHAction::None);
96                 } else {
97                     let lpad = lpad_base + cs_lpad;
98                     return Ok(interpret_cs_action(action_table as *mut u8, cs_action_entry, lpad));
99                 }
100             }
101         }
102         // Ip is not present in the table. This indicates a nounwind call.
103         Ok(EHAction::Terminate)
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_entry = 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(action_table as *mut u8, cs_action_entry, lpad));
123             }
124         }
125     }
126 }
127
128 unsafe fn interpret_cs_action(
129     action_table: *mut u8,
130     cs_action_entry: u64,
131     lpad: usize,
132 ) -> EHAction {
133     if cs_action_entry == 0 {
134         // If cs_action is 0 then this is a cleanup (Drop::drop). We run these
135         // for both Rust panics and foreign exceptions.
136         EHAction::Cleanup(lpad)
137     } else {
138         let action_record = (action_table as *mut u8).offset(cs_action_entry as isize - 1);
139         let mut action_reader = DwarfReader::new(action_record);
140         let ttype_index = action_reader.read_sleb128();
141         if ttype_index == 0 {
142             EHAction::Cleanup(lpad)
143         } else {
144             // Stop unwinding Rust panics at catch_unwind.
145             EHAction::Catch(lpad)
146         }
147     }
148 }
149
150 #[inline]
151 fn round_up(unrounded: usize, align: usize) -> Result<usize, ()> {
152     if align.is_power_of_two() { Ok((unrounded + align - 1) & !(align - 1)) } else { Err(()) }
153 }
154
155 unsafe fn read_encoded_pointer(
156     reader: &mut DwarfReader,
157     context: &EHContext<'_>,
158     encoding: u8,
159 ) -> Result<usize, ()> {
160     if encoding == DW_EH_PE_omit {
161         return Err(());
162     }
163
164     // DW_EH_PE_aligned implies it's an absolute pointer value
165     if encoding == DW_EH_PE_aligned {
166         reader.ptr = reader.ptr.with_addr(round_up(reader.ptr.addr(), mem::size_of::<usize>())?);
167         return Ok(reader.read::<usize>());
168     }
169
170     let mut result = match encoding & 0x0F {
171         DW_EH_PE_absptr => reader.read::<usize>(),
172         DW_EH_PE_uleb128 => reader.read_uleb128() as usize,
173         DW_EH_PE_udata2 => reader.read::<u16>() as usize,
174         DW_EH_PE_udata4 => reader.read::<u32>() as usize,
175         DW_EH_PE_udata8 => reader.read::<u64>() as usize,
176         DW_EH_PE_sleb128 => reader.read_sleb128() as usize,
177         DW_EH_PE_sdata2 => reader.read::<i16>() as usize,
178         DW_EH_PE_sdata4 => reader.read::<i32>() as usize,
179         DW_EH_PE_sdata8 => reader.read::<i64>() as usize,
180         _ => return Err(()),
181     };
182
183     result += match encoding & 0x70 {
184         DW_EH_PE_absptr => 0,
185         // relative to address of the encoded value, despite the name
186         DW_EH_PE_pcrel => reader.ptr.expose_addr(),
187         DW_EH_PE_funcrel => {
188             if context.func_start == 0 {
189                 return Err(());
190             }
191             context.func_start
192         }
193         DW_EH_PE_textrel => (*context.get_text_start)(),
194         DW_EH_PE_datarel => (*context.get_data_start)(),
195         _ => return Err(()),
196     };
197
198     if encoding & DW_EH_PE_indirect != 0 {
199         result = *ptr::from_exposed_addr::<usize>(result);
200     }
201
202     Ok(result)
203 }