Skip to content

Commit 051c2d1

Browse files
committed
Implement rust_eh_personality in Rust, remove rust_eh_personality_catch.
Well, not quite: ARM EHABI platforms still use the old scheme -- for now.
1 parent 3cc3ad1 commit 051c2d1

File tree

6 files changed

+176
-138
lines changed

6 files changed

+176
-138
lines changed

src/libpanic_unwind/dwarf/eh.rs

+72-35
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,25 @@ pub const DW_EH_PE_aligned: u8 = 0x50;
4545
pub const DW_EH_PE_indirect: u8 = 0x80;
4646

4747
#[derive(Copy, Clone)]
48-
pub struct EHContext {
48+
pub struct EHContext<'a> {
4949
pub ip: usize, // Current instruction pointer
5050
pub func_start: usize, // Address of the current function
51-
pub text_start: usize, // Address of the code section
52-
pub data_start: usize, // Address of the data section
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
5353
}
5454

55-
pub unsafe fn find_landing_pad(lsda: *const u8, context: &EHContext) -> Option<usize> {
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) -> EHAction {
5665
if lsda.is_null() {
57-
return None;
66+
return EHAction::None;
5867
}
5968

6069
let func_start = context.func_start;
@@ -77,32 +86,62 @@ pub unsafe fn find_landing_pad(lsda: *const u8, context: &EHContext) -> Option<u
7786
let call_site_encoding = reader.read::<u8>();
7887
let call_site_table_length = reader.read_uleb128();
7988
let action_table = reader.ptr.offset(call_site_table_length as isize);
80-
// Return addresses point 1 byte past the call instruction, which could
81-
// be in the next IP range.
82-
let ip = context.ip - 1;
83-
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 = 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;
89+
let ip = context.ip;
90+
91+
if !USING_SJLJ_EXCEPTIONS {
92+
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);
96+
let cs_action = reader.read_uleb128();
97+
// Callsite table is sorted by cs_start, so if we've passed the ip, we
98+
// may stop searching.
99+
if ip < func_start + cs_start {
100+
break;
101+
}
102+
if ip < func_start + cs_start + cs_len {
103+
if cs_lpad == 0 {
104+
return EHAction::None;
105+
} else {
106+
let lpad = lpad_base + cs_lpad;
107+
return interpret_cs_action(cs_action, lpad);
108+
}
109+
}
93110
}
94-
if ip < func_start + cs_start + cs_len {
95-
if cs_lpad != 0 {
96-
return Some(lpad_base + cs_lpad);
97-
} else {
98-
return None;
111+
// If ip is not present in the table, call terminate. This is for
112+
// a destructor inside a cleanup, or a library routine the compiler
113+
// was not expecting to throw
114+
EHAction::Terminate
115+
} else {
116+
// SjLj version:
117+
// The "IP" is an index into the call-site table, with two exceptions:
118+
// -1 means 'no-action', and 0 means 'terminate'.
119+
match ip as isize {
120+
-1 => return EHAction::None,
121+
0 => return EHAction::Terminate,
122+
_ => (),
123+
}
124+
let mut idx = ip;
125+
loop {
126+
let cs_lpad = reader.read_uleb128();
127+
let cs_action = reader.read_uleb128();
128+
idx -= 1;
129+
if idx == 0 {
130+
// Can never have null landing pad for sjlj -- that would have
131+
// been indicated by a -1 call site index.
132+
let lpad = (cs_lpad + 1) as usize;
133+
return interpret_cs_action(cs_action, lpad);
99134
}
100135
}
101136
}
102-
// IP range not found: gcc's C++ personality calls terminate() here,
103-
// however the rest of the languages treat this the same as cs_lpad == 0.
104-
// We follow this suit.
105-
None
137+
}
138+
139+
fn interpret_cs_action(cs_action: u64, lpad: usize) -> EHAction {
140+
if cs_action == 0 {
141+
EHAction::Cleanup(lpad)
142+
} else {
143+
EHAction::Catch(lpad)
144+
}
106145
}
107146

108147
#[inline]
@@ -140,18 +179,16 @@ unsafe fn read_encoded_pointer(reader: &mut DwarfReader,
140179
DW_EH_PE_absptr => 0,
141180
// relative to address of the encoded value, despite the name
142181
DW_EH_PE_pcrel => reader.ptr as usize,
143-
DW_EH_PE_textrel => {
144-
assert!(context.text_start != 0);
145-
context.text_start
146-
}
147-
DW_EH_PE_datarel => {
148-
assert!(context.data_start != 0);
149-
context.data_start
150-
}
151182
DW_EH_PE_funcrel => {
152183
assert!(context.func_start != 0);
153184
context.func_start
154185
}
186+
DW_EH_PE_textrel => {
187+
(*context.get_text_start)()
188+
}
189+
DW_EH_PE_datarel => {
190+
(*context.get_data_start)()
191+
}
155192
_ => panic!(),
156193
};
157194

src/libpanic_unwind/gcc.rs

+72-93
Original file line numberDiff line numberDiff line change
@@ -106,117 +106,96 @@ fn rust_exception_class() -> uw::_Unwind_Exception_Class {
106106
0x4d4f5a_00_52555354
107107
}
108108

109-
// We could implement our personality routine in Rust, however exception
110-
// info decoding is tedious. More importantly, personality routines have to
111-
// handle various platform quirks, which are not fun to maintain. For this
112-
// reason, we attempt to reuse personality routine of the C language:
113-
// __gcc_personality_v0.
114-
//
115-
// Since C does not support exception catching, __gcc_personality_v0 simply
116-
// always returns _URC_CONTINUE_UNWIND in search phase, and always returns
117-
// _URC_INSTALL_CONTEXT (i.e. "invoke cleanup code") in cleanup phase.
118-
//
119-
// This is pretty close to Rust's exception handling approach, except that Rust
120-
// does have a single "catch-all" handler at the bottom of each thread's stack.
121-
// So we have two versions of the personality routine:
122-
// - rust_eh_personality, used by all cleanup landing pads, which never catches,
123-
// so the behavior of __gcc_personality_v0 is perfectly adequate there, and
124-
// - rust_eh_personality_catch, used only by rust_try(), which always catches.
125-
//
126-
// See also: rustc_trans::trans::intrinsic::trans_gnu_try
127-
128-
#[cfg(all(not(target_arch = "arm"),
129-
not(all(windows, target_arch = "x86_64"))))]
109+
// All targets, except ARM which uses a slightly different ABI (however, iOS goes here as it uses
110+
// SjLj unwinding). Also, 64-bit Windows implementation lives in seh64_gnu.rs
111+
#[cfg(all(any(target_os = "ios", not(target_arch = "arm"))))]
130112
pub mod eabi {
131113
use unwind as uw;
132-
use libc::c_int;
114+
use libc::{c_int, uintptr_t};
115+
use dwarf::eh::{EHContext, EHAction, find_eh_action};
133116

134-
extern "C" {
135-
fn __gcc_personality_v0(version: c_int,
136-
actions: uw::_Unwind_Action,
137-
exception_class: uw::_Unwind_Exception_Class,
138-
ue_header: *mut uw::_Unwind_Exception,
139-
context: *mut uw::_Unwind_Context)
140-
-> uw::_Unwind_Reason_Code;
141-
}
117+
// Register ids were lifted from LLVM's TargetLowering::getExceptionPointerRegister()
118+
// and TargetLowering::getExceptionSelectorRegister() for each architecture,
119+
// then mapped to DWARF register numbers via register definition tables
120+
// (typically <arch>RegisterInfo.td, search for "DwarfRegNum").
121+
// See also http://llvm.org/docs/WritingAnLLVMBackend.html#defining-a-register.
142122

143-
#[lang = "eh_personality"]
144-
#[no_mangle]
145-
extern "C" fn rust_eh_personality(version: c_int,
146-
actions: uw::_Unwind_Action,
147-
exception_class: uw::_Unwind_Exception_Class,
148-
ue_header: *mut uw::_Unwind_Exception,
149-
context: *mut uw::_Unwind_Context)
150-
-> uw::_Unwind_Reason_Code {
151-
unsafe { __gcc_personality_v0(version, actions, exception_class, ue_header, context) }
152-
}
123+
#[cfg(target_arch = "x86")]
124+
const UNWIND_DATA_REG: (i32, i32) = (0, 2); // EAX, EDX
153125

154-
#[lang = "eh_personality_catch"]
155-
#[no_mangle]
156-
pub extern "C" fn rust_eh_personality_catch(version: c_int,
157-
actions: uw::_Unwind_Action,
158-
exception_class: uw::_Unwind_Exception_Class,
159-
ue_header: *mut uw::_Unwind_Exception,
160-
context: *mut uw::_Unwind_Context)
161-
-> uw::_Unwind_Reason_Code {
126+
#[cfg(target_arch = "x86_64")]
127+
const UNWIND_DATA_REG: (i32, i32) = (0, 1); // RAX, RDX
162128

163-
if (actions as c_int & uw::_UA_SEARCH_PHASE as c_int) != 0 {
164-
// search phase
165-
uw::_URC_HANDLER_FOUND // catch!
166-
} else {
167-
// cleanup phase
168-
unsafe { __gcc_personality_v0(version, actions, exception_class, ue_header, context) }
169-
}
170-
}
171-
}
172-
173-
// iOS on armv7 is using SjLj exceptions and therefore requires to use
174-
// a specialized personality routine: __gcc_personality_sj0
129+
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
130+
const UNWIND_DATA_REG: (i32, i32) = (0, 1); // R0, R1 / X0, X1
175131

176-
#[cfg(all(target_os = "ios", target_arch = "arm"))]
177-
pub mod eabi {
178-
use unwind as uw;
179-
use libc::c_int;
132+
#[cfg(any(target_arch = "mips", target_arch = "mipsel"))]
133+
const UNWIND_DATA_REG: (i32, i32) = (4, 5); // A0, A1
180134

181-
extern "C" {
182-
fn __gcc_personality_sj0(version: c_int,
183-
actions: uw::_Unwind_Action,
184-
exception_class: uw::_Unwind_Exception_Class,
185-
ue_header: *mut uw::_Unwind_Exception,
186-
context: *mut uw::_Unwind_Context)
187-
-> uw::_Unwind_Reason_Code;
188-
}
135+
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
136+
const UNWIND_DATA_REG: (i32, i32) = (3, 4); // R3, R4 / X3, X4
189137

138+
// Based on GCC's C and C++ personality routines. For reference, see:
139+
// https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/libsupc++/eh_personality.cc
140+
// https://github.com/gcc-mirror/gcc/blob/trunk/libgcc/unwind-c.c
190141
#[lang = "eh_personality"]
191142
#[no_mangle]
192-
pub extern "C" fn rust_eh_personality(version: c_int,
193-
actions: uw::_Unwind_Action,
194-
exception_class: uw::_Unwind_Exception_Class,
195-
ue_header: *mut uw::_Unwind_Exception,
196-
context: *mut uw::_Unwind_Context)
197-
-> uw::_Unwind_Reason_Code {
198-
unsafe { __gcc_personality_sj0(version, actions, exception_class, ue_header, context) }
143+
#[allow(unused)]
144+
unsafe extern "C" fn rust_eh_personality(version: c_int,
145+
actions: uw::_Unwind_Action,
146+
exception_class: uw::_Unwind_Exception_Class,
147+
exception_object: *mut uw::_Unwind_Exception,
148+
context: *mut uw::_Unwind_Context)
149+
-> uw::_Unwind_Reason_Code {
150+
if version != 1 {
151+
return uw::_URC_FATAL_PHASE1_ERROR;
152+
}
153+
let lsda = uw::_Unwind_GetLanguageSpecificData(context) as *const u8;
154+
let mut ip_before_instr: c_int = 0;
155+
let ip = uw::_Unwind_GetIPInfo(context, &mut ip_before_instr);
156+
let eh_context = EHContext {
157+
// The return address points 1 byte past the call instruction,
158+
// which could be in the next IP range in LSDA range table.
159+
ip: if ip_before_instr != 0 { ip } else { ip - 1 },
160+
func_start: uw::_Unwind_GetRegionStart(context),
161+
get_text_start: &|| uw::_Unwind_GetTextRelBase(context),
162+
get_data_start: &|| uw::_Unwind_GetDataRelBase(context),
163+
};
164+
let eh_action = find_eh_action(lsda, &eh_context);
165+
166+
if actions as i32 & uw::_UA_SEARCH_PHASE as i32 != 0 {
167+
match eh_action {
168+
EHAction::None | EHAction::Cleanup(_) => return uw::_URC_CONTINUE_UNWIND,
169+
EHAction::Catch(_) => return uw::_URC_HANDLER_FOUND,
170+
EHAction::Terminate => return uw::_URC_FATAL_PHASE1_ERROR,
171+
}
172+
} else {
173+
match eh_action {
174+
EHAction::None => return uw::_URC_CONTINUE_UNWIND,
175+
EHAction::Cleanup(lpad) | EHAction::Catch(lpad) => {
176+
uw::_Unwind_SetGR(context, UNWIND_DATA_REG.0, exception_object as uintptr_t);
177+
uw::_Unwind_SetGR(context, UNWIND_DATA_REG.1, 0);
178+
uw::_Unwind_SetIP(context, lpad);
179+
return uw::_URC_INSTALL_CONTEXT;
180+
}
181+
EHAction::Terminate => return uw::_URC_FATAL_PHASE2_ERROR,
182+
}
183+
}
199184
}
200185

186+
#[cfg(stage0)]
201187
#[lang = "eh_personality_catch"]
202188
#[no_mangle]
203-
pub extern "C" fn rust_eh_personality_catch(version: c_int,
204-
actions: uw::_Unwind_Action,
205-
exception_class: uw::_Unwind_Exception_Class,
206-
ue_header: *mut uw::_Unwind_Exception,
207-
context: *mut uw::_Unwind_Context)
208-
-> uw::_Unwind_Reason_Code {
209-
if (actions as c_int & uw::_UA_SEARCH_PHASE as c_int) != 0 {
210-
// search phase
211-
uw::_URC_HANDLER_FOUND // catch!
212-
} else {
213-
// cleanup phase
214-
unsafe { __gcc_personality_sj0(version, actions, exception_class, ue_header, context) }
215-
}
189+
pub unsafe extern "C" fn rust_eh_personality_catch(version: c_int,
190+
actions: uw::_Unwind_Action,
191+
exception_class: uw::_Unwind_Exception_Class,
192+
ue_header: *mut uw::_Unwind_Exception,
193+
context: *mut uw::_Unwind_Context)
194+
-> uw::_Unwind_Reason_Code {
195+
rust_eh_personality(version, actions, exception_class, ue_header, context)
216196
}
217197
}
218198

219-
220199
// ARM EHABI uses a slightly different personality routine signature,
221200
// but otherwise works the same.
222201
#[cfg(all(target_arch = "arm", not(target_os = "ios")))]

src/libpanic_unwind/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub unsafe extern "C" fn __rust_maybe_catch_panic(f: fn(*mut u8),
101101
// Entry point for raising an exception, just delegates to the platform-specific
102102
// implementation.
103103
#[no_mangle]
104+
#[unwind]
104105
pub unsafe extern "C" fn __rust_start_panic(data: usize, vtable: usize) -> u32 {
105106
imp::panic(mem::transmute(raw::TraitObject {
106107
data: data as *mut (),

src/libpanic_unwind/seh64_gnu.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use alloc::boxed::Box;
1919
use core::any::Any;
2020
use core::intrinsics;
2121
use core::ptr;
22-
use dwarf::eh;
22+
use dwarf::eh::{EHContext, EHAction, find_eh_action};
2323
use windows as c;
2424

2525
// Define our exception codes:
@@ -81,6 +81,7 @@ pub unsafe fn cleanup(ptr: *mut u8) -> Box<Any + Send> {
8181
// This is considered acceptable, because the behavior of throwing exceptions
8282
// through a C ABI boundary is undefined.
8383

84+
#[cfg(stage0)]
8485
#[lang = "eh_personality_catch"]
8586
#[cfg(not(test))]
8687
unsafe extern "C" fn rust_eh_personality_catch(exceptionRecord: *mut c::EXCEPTION_RECORD,
@@ -132,11 +133,17 @@ unsafe extern "C" fn rust_eh_unwind_resume(panic_ctx: c::LPVOID) -> ! {
132133
}
133134

134135
unsafe fn find_landing_pad(dc: &c::DISPATCHER_CONTEXT) -> Option<usize> {
135-
let eh_ctx = eh::EHContext {
136-
ip: dc.ControlPc as usize,
136+
let eh_ctx = EHContext {
137+
// The return address points 1 byte past the call instruction,
138+
// which could be in the next IP range in LSDA range table.
139+
ip: dc.ControlPc as usize - 1,
137140
func_start: dc.ImageBase as usize + (*dc.FunctionEntry).BeginAddress as usize,
138-
text_start: dc.ImageBase as usize,
139-
data_start: 0,
141+
get_text_start: &|| dc.ImageBase as usize,
142+
get_data_start: &|| unimplemented!(),
140143
};
141-
eh::find_landing_pad(dc.HandlerData, &eh_ctx)
144+
match find_eh_action(dc.HandlerData, &eh_ctx) {
145+
EHAction::None => None,
146+
EHAction::Cleanup(lpad) | EHAction::Catch(lpad) => Some(lpad),
147+
EHAction::Terminate => intrinsics::abort(),
148+
}
142149
}

0 commit comments

Comments
 (0)