Skip to content

Commit 52805d2

Browse files
committed
std: Avoid panics in rust_eh_personality
This commit removes a few calls to panic and/or assert in `rust_eh_personality`. This function definitely can't itself panic (that'd probably segfault or do something else weird) and I was also noticing that a `pub extern fn foo() {}` cdylib was abnormally large. Turns out all that size was the panicking machinery brought in by the personality function! The change here is to return a `Result` internally so we can bubble up the fatal error, eventually translating to the appropriate error code for the libunwind ABI.
1 parent ae79201 commit 52805d2

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

src/libpanic_unwind/dwarf/eh.rs

+32-23
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ pub enum EHAction {
6161

6262
pub const USING_SJLJ_EXCEPTIONS: bool = cfg!(all(target_os = "ios", target_arch = "arm"));
6363

64-
pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext) -> EHAction {
64+
pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext)
65+
-> Result<EHAction, ()>
66+
{
6567
if lsda.is_null() {
66-
return EHAction::None;
68+
return Ok(EHAction::None)
6769
}
6870

6971
let func_start = context.func_start;
@@ -72,7 +74,7 @@ pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext) -> EHAction {
7274
let start_encoding = reader.read::<u8>();
7375
// base address for landing pad offsets
7476
let lpad_base = if start_encoding != DW_EH_PE_omit {
75-
read_encoded_pointer(&mut reader, context, start_encoding)
77+
read_encoded_pointer(&mut reader, context, start_encoding)?
7678
} else {
7779
func_start
7880
};
@@ -90,9 +92,9 @@ pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext) -> EHAction {
9092

9193
if !USING_SJLJ_EXCEPTIONS {
9294
while reader.ptr < action_table {
93-
let cs_start = read_encoded_pointer(&mut reader, context, call_site_encoding);
94-
let cs_len = read_encoded_pointer(&mut reader, context, call_site_encoding);
95-
let cs_lpad = read_encoded_pointer(&mut reader, context, call_site_encoding);
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)?;
9698
let cs_action = reader.read_uleb128();
9799
// Callsite table is sorted by cs_start, so if we've passed the ip, we
98100
// may stop searching.
@@ -101,23 +103,23 @@ pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext) -> EHAction {
101103
}
102104
if ip < func_start + cs_start + cs_len {
103105
if cs_lpad == 0 {
104-
return EHAction::None;
106+
return Ok(EHAction::None)
105107
} else {
106108
let lpad = lpad_base + cs_lpad;
107-
return interpret_cs_action(cs_action, lpad);
109+
return Ok(interpret_cs_action(cs_action, lpad))
108110
}
109111
}
110112
}
111113
// Ip is not present in the table. This should not happen... but it does: issue #35011.
112114
// So rather than returning EHAction::Terminate, we do this.
113-
EHAction::None
115+
Ok(EHAction::None)
114116
} else {
115117
// SjLj version:
116118
// The "IP" is an index into the call-site table, with two exceptions:
117119
// -1 means 'no-action', and 0 means 'terminate'.
118120
match ip as isize {
119-
-1 => return EHAction::None,
120-
0 => return EHAction::Terminate,
121+
-1 => return Ok(EHAction::None),
122+
0 => return Ok(EHAction::Terminate),
121123
_ => (),
122124
}
123125
let mut idx = ip;
@@ -129,7 +131,7 @@ pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext) -> EHAction {
129131
// Can never have null landing pad for sjlj -- that would have
130132
// been indicated by a -1 call site index.
131133
let lpad = (cs_lpad + 1) as usize;
132-
return interpret_cs_action(cs_action, lpad);
134+
return Ok(interpret_cs_action(cs_action, lpad))
133135
}
134136
}
135137
}
@@ -144,21 +146,26 @@ fn interpret_cs_action(cs_action: u64, lpad: usize) -> EHAction {
144146
}
145147

146148
#[inline]
147-
fn round_up(unrounded: usize, align: usize) -> usize {
148-
assert!(align.is_power_of_two());
149-
(unrounded + align - 1) & !(align - 1)
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+
}
150155
}
151156

152157
unsafe fn read_encoded_pointer(reader: &mut DwarfReader,
153158
context: &EHContext,
154159
encoding: u8)
155-
-> usize {
156-
assert!(encoding != DW_EH_PE_omit);
160+
-> Result<usize, ()> {
161+
if encoding == DW_EH_PE_omit {
162+
return Err(())
163+
}
157164

158165
// DW_EH_PE_aligned implies it's an absolute pointer value
159166
if encoding == DW_EH_PE_aligned {
160-
reader.ptr = round_up(reader.ptr as usize, mem::size_of::<usize>()) as *const u8;
161-
return reader.read::<usize>();
167+
reader.ptr = round_up(reader.ptr as usize, mem::size_of::<usize>())? as *const u8;
168+
return Ok(reader.read::<usize>())
162169
}
163170

164171
let mut result = match encoding & 0x0F {
@@ -171,25 +178,27 @@ unsafe fn read_encoded_pointer(reader: &mut DwarfReader,
171178
DW_EH_PE_sdata2 => reader.read::<i16>() as usize,
172179
DW_EH_PE_sdata4 => reader.read::<i32>() as usize,
173180
DW_EH_PE_sdata8 => reader.read::<i64>() as usize,
174-
_ => panic!(),
181+
_ => return Err(()),
175182
};
176183

177184
result += match encoding & 0x70 {
178185
DW_EH_PE_absptr => 0,
179186
// relative to address of the encoded value, despite the name
180187
DW_EH_PE_pcrel => reader.ptr as usize,
181188
DW_EH_PE_funcrel => {
182-
assert!(context.func_start != 0);
189+
if context.func_start == 0 {
190+
return Err(())
191+
}
183192
context.func_start
184193
}
185194
DW_EH_PE_textrel => (*context.get_text_start)(),
186195
DW_EH_PE_datarel => (*context.get_data_start)(),
187-
_ => panic!(),
196+
_ => return Err(()),
188197
};
189198

190199
if encoding & DW_EH_PE_indirect != 0 {
191200
result = *(result as *const usize);
192201
}
193202

194-
result
203+
Ok(result)
195204
}

src/libpanic_unwind/gcc.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ unsafe extern "C" fn rust_eh_personality(version: c_int,
156156
if version != 1 {
157157
return uw::_URC_FATAL_PHASE1_ERROR;
158158
}
159-
let eh_action = find_eh_action(context);
159+
let eh_action = match find_eh_action(context) {
160+
Ok(action) => action,
161+
Err(_) => return uw::_URC_FATAL_PHASE1_ERROR,
162+
};
160163
if actions as i32 & uw::_UA_SEARCH_PHASE as i32 != 0 {
161164
match eh_action {
162165
EHAction::None |
@@ -219,7 +222,10 @@ unsafe extern "C" fn rust_eh_personality(state: uw::_Unwind_State,
219222
// _Unwind_Context in our libunwind bindings and fetch the required data from there directly,
220223
// bypassing DWARF compatibility functions.
221224

222-
let eh_action = find_eh_action(context);
225+
let eh_action = match find_eh_action(context) {
226+
Ok(action) => action,
227+
Err(_) => return uw::_URC_FAILURE,
228+
};
223229
if search_phase {
224230
match eh_action {
225231
EHAction::None |
@@ -260,7 +266,9 @@ unsafe extern "C" fn rust_eh_personality(state: uw::_Unwind_State,
260266
}
261267
}
262268

263-
unsafe fn find_eh_action(context: *mut uw::_Unwind_Context) -> EHAction {
269+
unsafe fn find_eh_action(context: *mut uw::_Unwind_Context)
270+
-> Result<EHAction, ()>
271+
{
264272
let lsda = uw::_Unwind_GetLanguageSpecificData(context) as *const u8;
265273
let mut ip_before_instr: c_int = 0;
266274
let ip = uw::_Unwind_GetIPInfo(context, &mut ip_before_instr);

src/libpanic_unwind/seh64_gnu.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ unsafe fn find_landing_pad(dc: &c::DISPATCHER_CONTEXT) -> Option<usize> {
128128
get_data_start: &|| unimplemented!(),
129129
};
130130
match find_eh_action(dc.HandlerData, &eh_ctx) {
131-
EHAction::None => None,
132-
EHAction::Cleanup(lpad) |
133-
EHAction::Catch(lpad) => Some(lpad),
134-
EHAction::Terminate => intrinsics::abort(),
131+
Err(_) |
132+
Ok(EHAction::None) => None,
133+
Ok(EHAction::Cleanup(lpad)) |
134+
Ok(EHAction::Catch(lpad)) => Some(lpad),
135+
Ok(EHAction::Terminate) => intrinsics::abort(),
135136
}
136137
}

0 commit comments

Comments
 (0)