Skip to content

Commit 0c40455

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 db71987 commit 0c40455

File tree

5 files changed

+157
-124
lines changed

5 files changed

+157
-124
lines changed

src/libpanic_unwind/dwarf/eh.rs

+63-24
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,18 @@ pub struct EHContext {
5252
pub data_start: usize, // 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]

src/libpanic_unwind/gcc.rs

+68-92
Original file line numberDiff line numberDiff line change
@@ -105,117 +105,93 @@ fn rust_exception_class() -> uw::_Unwind_Exception_Class {
105105
0x4d4f5a_00_52555354
106106
}
107107

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

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

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

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

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

175-
#[cfg(all(target_os = "ios", target_arch = "arm"))]
176-
pub mod eabi {
177-
use unwind as uw;
178-
use libc::c_int;
131+
#[cfg(target_arch = "aarch64")]
132+
const UNWIND_DATA_REG: (i32, i32) = (0, 1); // X0, X1
179133

180-
extern "C" {
181-
fn __gcc_personality_sj0(version: c_int,
182-
actions: uw::_Unwind_Action,
183-
exception_class: uw::_Unwind_Exception_Class,
184-
ue_header: *mut uw::_Unwind_Exception,
185-
context: *mut uw::_Unwind_Context)
186-
-> uw::_Unwind_Reason_Code;
187-
}
134+
// OSX actually has the symbols, but they assert when called.
135+
const HAS_GET_XXX_RELBASE: bool = cfg!(not(any(target_os = "ios", target_os = "macos")));
188136

189137
#[lang = "eh_personality"]
190138
#[no_mangle]
191-
pub extern "C" fn rust_eh_personality(version: c_int,
192-
actions: uw::_Unwind_Action,
193-
exception_class: uw::_Unwind_Exception_Class,
194-
ue_header: *mut uw::_Unwind_Exception,
195-
context: *mut uw::_Unwind_Context)
196-
-> uw::_Unwind_Reason_Code {
197-
unsafe { __gcc_personality_sj0(version, actions, exception_class, ue_header, context) }
139+
#[allow(unused)]
140+
unsafe extern "C" fn rust_eh_personality(version: c_int,
141+
actions: uw::_Unwind_Action,
142+
exception_class: uw::_Unwind_Exception_Class,
143+
exception_object: *mut uw::_Unwind_Exception,
144+
context: *mut uw::_Unwind_Context)
145+
-> uw::_Unwind_Reason_Code {
146+
if version != 1 {
147+
return uw::_URC_FATAL_PHASE1_ERROR;
148+
}
149+
let lsda = uw::_Unwind_GetLanguageSpecificData(context) as *const u8;
150+
let mut ip_before_instr: c_int = 0;
151+
let ip = uw::_Unwind_GetIPInfo(context, &mut ip_before_instr);
152+
let eh_context = EHContext {
153+
// The return address points 1 byte past the call instruction,
154+
// which could be in the next IP range in LSDA range table.
155+
ip: if ip_before_instr != 0 { ip } else { ip - 1 },
156+
func_start: uw::_Unwind_GetRegionStart(context),
157+
text_start: if HAS_GET_XXX_RELBASE { uw::_Unwind_GetTextRelBase(context) } else { 0 },
158+
data_start: if HAS_GET_XXX_RELBASE { uw::_Unwind_GetDataRelBase(context) } else { 0 },
159+
};
160+
let eh_action = find_eh_action(lsda, &eh_context);
161+
162+
if actions as i32 & uw::_UA_SEARCH_PHASE as i32 != 0 {
163+
match eh_action {
164+
EHAction::None | EHAction::Cleanup(_) => return uw::_URC_CONTINUE_UNWIND,
165+
EHAction::Catch(_) => return uw::_URC_HANDLER_FOUND,
166+
EHAction::Terminate => return uw::_URC_FATAL_PHASE1_ERROR,
167+
}
168+
} else {
169+
match eh_action {
170+
EHAction::None => return uw::_URC_CONTINUE_UNWIND,
171+
EHAction::Cleanup(lpad) | EHAction::Catch(lpad) => {
172+
uw::_Unwind_SetGR(context, UNWIND_DATA_REG.0, exception_object as uintptr_t);
173+
uw::_Unwind_SetGR(context, UNWIND_DATA_REG.1, 0);
174+
uw::_Unwind_SetIP(context, lpad);
175+
return uw::_URC_INSTALL_CONTEXT;
176+
}
177+
EHAction::Terminate => return uw::_URC_FATAL_PHASE2_ERROR,
178+
}
179+
}
198180
}
199181

182+
#[cfg(stage0)]
200183
#[lang = "eh_personality_catch"]
201184
#[no_mangle]
202-
pub extern "C" fn rust_eh_personality_catch(version: c_int,
203-
actions: uw::_Unwind_Action,
204-
exception_class: uw::_Unwind_Exception_Class,
205-
ue_header: *mut uw::_Unwind_Exception,
206-
context: *mut uw::_Unwind_Context)
207-
-> uw::_Unwind_Reason_Code {
208-
if (actions as c_int & uw::_UA_SEARCH_PHASE as c_int) != 0 {
209-
// search phase
210-
uw::_URC_HANDLER_FOUND // catch!
211-
} else {
212-
// cleanup phase
213-
unsafe { __gcc_personality_sj0(version, actions, exception_class, ue_header, context) }
214-
}
185+
pub unsafe extern "C" fn rust_eh_personality_catch(version: c_int,
186+
actions: uw::_Unwind_Action,
187+
exception_class: uw::_Unwind_Exception_Class,
188+
ue_header: *mut uw::_Unwind_Exception,
189+
context: *mut uw::_Unwind_Context)
190+
-> uw::_Unwind_Reason_Code {
191+
rust_eh_personality(version, actions, exception_class, ue_header, context)
215192
}
216193
}
217194

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

src/libpanic_unwind/seh64_gnu.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use alloc::boxed::Box;
1818

1919
use core::any::Any;
2020
use core::intrinsics;
21-
use dwarf::eh;
21+
use dwarf::eh::{EHContext, EHAction, find_eh_action};
2222
use windows as c;
2323

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

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

133134
unsafe fn find_landing_pad(dc: &c::DISPATCHER_CONTEXT) -> Option<usize> {
134-
let eh_ctx = eh::EHContext {
135-
ip: dc.ControlPc as usize,
135+
let eh_ctx = EHContext {
136+
ip: dc.ControlPc as usize - 1,
136137
func_start: dc.ImageBase as usize + (*dc.FunctionEntry).BeginAddress as usize,
137138
text_start: dc.ImageBase as usize,
138139
data_start: 0,
139140
};
140-
eh::find_landing_pad(dc.HandlerData, &eh_ctx)
141+
match find_eh_action(dc.HandlerData, &eh_ctx) {
142+
EHAction::None => None,
143+
EHAction::Cleanup(lpad) | EHAction::Catch(lpad) => Some(lpad),
144+
EHAction::Terminate => intrinsics::abort(),
145+
}
141146
}

src/librustc_trans/intrinsic.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1193,11 +1193,16 @@ fn trans_gnu_try<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
11931193
// managed by the standard library.
11941194

11951195
attributes::emit_uwtable(bcx.fcx.llfn, true);
1196-
let catch_pers = match tcx.lang_items.eh_personality_catch() {
1197-
Some(did) => {
1198-
Callee::def(ccx, did, tcx.mk_substs(Substs::empty())).reify(ccx).val
1196+
let catch_pers = if cfg!(all(target_arch = "arm", not(target_os = "ios"))) {
1197+
// Only ARM still uses a separate catch personality (for now)
1198+
match tcx.lang_items.eh_personality_catch() {
1199+
Some(did) => {
1200+
Callee::def(ccx, did, tcx.mk_substs(Substs::empty())).reify(ccx).val
1201+
}
1202+
None => bug!("eh_personality_catch not defined"),
11991203
}
1200-
None => bug!("eh_personality_catch not defined"),
1204+
} else {
1205+
bcx.fcx.eh_personality()
12011206
};
12021207

12031208
let then = bcx.fcx.new_temp_block("then");

src/libunwind/libunwind.rs

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub enum _Unwind_Reason_Code {
5858
pub type _Unwind_Exception_Class = u64;
5959

6060
pub type _Unwind_Word = libc::uintptr_t;
61+
pub type _Unwind_Ptr = libc::uintptr_t;
6162

6263
pub type _Unwind_Trace_Fn = extern "C" fn(ctx: *mut _Unwind_Context, arg: *mut libc::c_void)
6364
-> _Unwind_Reason_Code;
@@ -156,6 +157,13 @@ extern "C" {
156157
ip_before_insn: *mut libc::c_int)
157158
-> libc::uintptr_t;
158159

160+
pub fn _Unwind_GetLanguageSpecificData(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
161+
pub fn _Unwind_GetRegionStart(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
162+
pub fn _Unwind_GetTextRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
163+
pub fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
164+
pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: libc::c_int, value: _Unwind_Ptr);
165+
pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Ptr);
166+
159167
#[cfg(all(not(target_os = "android"),
160168
not(all(target_os = "linux", target_arch = "arm"))))]
161169
pub fn _Unwind_FindEnclosingFunction(pc: *mut libc::c_void) -> *mut libc::c_void;

0 commit comments

Comments
 (0)