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