std/sys/personality/gcc.rs
1//! Implementation of panics backed by libgcc/libunwind (in some form).
2//!
3//! For background on exception handling and stack unwinding please see
4//! "Exception Handling in LLVM" (llvm.org/docs/ExceptionHandling.html) and
5//! documents linked from it.
6//! These are also good reads:
7//! * <https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html>
8//! * <https://nicolasbrailo.github.io/blog/projects_texts/13exceptionsunderthehood.html>
9//! * <https://www.airs.com/blog/index.php?s=exception+frames>
10//!
11//! ## A brief summary
12//!
13//! Exception handling happens in two phases: a search phase and a cleanup
14//! phase.
15//!
16//! In both phases the unwinder walks stack frames from top to bottom using
17//! information from the stack frame unwind sections of the current process's
18//! modules ("module" here refers to an OS module, i.e., an executable or a
19//! dynamic library).
20//!
21//! For each stack frame, it invokes the associated "personality routine", whose
22//! address is also stored in the unwind info section.
23//!
24//! In the search phase, the job of a personality routine is to examine
25//! exception object being thrown, and to decide whether it should be caught at
26//! that stack frame. Once the handler frame has been identified, cleanup phase
27//! begins.
28//!
29//! In the cleanup phase, the unwinder invokes each personality routine again.
30//! This time it decides which (if any) cleanup code needs to be run for
31//! the current stack frame. If so, the control is transferred to a special
32//! branch in the function body, the "landing pad", which invokes destructors,
33//! frees memory, etc. At the end of the landing pad, control is transferred
34//! back to the unwinder and unwinding resumes.
35//!
36//! Once stack has been unwound down to the handler frame level, unwinding stops
37//! and the last personality routine transfers control to the catch block.
38#![forbid(unsafe_op_in_unsafe_fn)]
39
40use unwind as uw;
41
42use super::dwarf::eh::{self, EHAction, EHContext};
43use crate::ffi::c_int;
44
45// Register ids were lifted from LLVM's TargetLowering::getExceptionPointerRegister()
46// and TargetLowering::getExceptionSelectorRegister() for each architecture,
47// then mapped to DWARF register numbers via register definition tables
48// (typically <arch>RegisterInfo.td, search for "DwarfRegNum").
49// See also https://llvm.org/docs/WritingAnLLVMBackend.html#defining-a-register.
50
51#[cfg(target_arch = "x86")]
52const UNWIND_DATA_REG: (i32, i32) = (0, 2); // EAX, EDX
53
54#[cfg(target_arch = "x86_64")]
55const UNWIND_DATA_REG: (i32, i32) = (0, 1); // RAX, RDX
56
57#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
58const UNWIND_DATA_REG: (i32, i32) = (0, 1); // R0, R1 / X0, X1
59
60#[cfg(target_arch = "m68k")]
61const UNWIND_DATA_REG: (i32, i32) = (0, 1); // D0, D1
62
63#[cfg(any(
64 target_arch = "mips",
65 target_arch = "mips32r6",
66 target_arch = "mips64",
67 target_arch = "mips64r6"
68))]
69const UNWIND_DATA_REG: (i32, i32) = (4, 5); // A0, A1
70
71#[cfg(target_arch = "csky")]
72const UNWIND_DATA_REG: (i32, i32) = (0, 1); // R0, R1
73
74#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
75const UNWIND_DATA_REG: (i32, i32) = (3, 4); // R3, R4 / X3, X4
76
77#[cfg(target_arch = "s390x")]
78const UNWIND_DATA_REG: (i32, i32) = (6, 7); // R6, R7
79
80#[cfg(any(target_arch = "sparc", target_arch = "sparc64"))]
81const UNWIND_DATA_REG: (i32, i32) = (24, 25); // I0, I1
82
83#[cfg(target_arch = "hexagon")]
84const UNWIND_DATA_REG: (i32, i32) = (0, 1); // R0, R1
85
86#[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
87const UNWIND_DATA_REG: (i32, i32) = (10, 11); // x10, x11
88
89#[cfg(target_arch = "loongarch64")]
90const UNWIND_DATA_REG: (i32, i32) = (4, 5); // a0, a1
91
92#[cfg(target_arch = "sw64")]
93const UNWIND_DATA_REG: (i32, i32) = (16, 17); // R16, R17
94
95// The following code is based on GCC's C and C++ personality routines. For reference, see:
96// https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/libsupc++/eh_personality.cc
97// https://github.com/gcc-mirror/gcc/blob/trunk/libgcc/unwind-c.c
98
99cfg_if::cfg_if! {
100 if #[cfg(all(
101 target_arch = "arm",
102 not(target_vendor = "apple"),
103 not(target_os = "netbsd"),
104 ))] {
105 /// personality fn called by [ARM EHABI][armeabi-eh]
106 ///
107 /// 32-bit ARM on iOS/tvOS/watchOS does not use ARM EHABI, it uses
108 /// either "setjmp-longjmp" unwinding or DWARF CFI unwinding, which is
109 /// handled by the default routine.
110 ///
111 /// [armeabi-eh]: https://web.archive.org/web/20190728160938/https://infocenter.arm.com/help/topic/com.arm.doc.ihi0038b/IHI0038B_ehabi.pdf
112 #[lang = "eh_personality"]
113 unsafe extern "C" fn rust_eh_personality(
114 state: uw::_Unwind_State,
115 exception_object: *mut uw::_Unwind_Exception,
116 context: *mut uw::_Unwind_Context,
117 ) -> uw::_Unwind_Reason_Code {
118 unsafe {
119 let state = state as c_int;
120 let action = state & uw::_US_ACTION_MASK as c_int;
121 let search_phase = if action == uw::_US_VIRTUAL_UNWIND_FRAME as c_int {
122 // Backtraces on ARM will call the personality routine with
123 // state == _US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND. In those cases
124 // we want to continue unwinding the stack, otherwise all our backtraces
125 // would end at __rust_try
126 if state & uw::_US_FORCE_UNWIND as c_int != 0 {
127 return continue_unwind(exception_object, context);
128 }
129 true
130 } else if action == uw::_US_UNWIND_FRAME_STARTING as c_int {
131 false
132 } else if action == uw::_US_UNWIND_FRAME_RESUME as c_int {
133 return continue_unwind(exception_object, context);
134 } else {
135 return uw::_URC_FAILURE;
136 };
137
138 // The DWARF unwinder assumes that _Unwind_Context holds things like the function
139 // and LSDA pointers, however ARM EHABI places them into the exception object.
140 // To preserve signatures of functions like _Unwind_GetLanguageSpecificData(), which
141 // take only the context pointer, GCC personality routines stash a pointer to
142 // exception_object in the context, using location reserved for ARM's
143 // "scratch register" (r12).
144 uw::_Unwind_SetGR(context, uw::UNWIND_POINTER_REG, exception_object as uw::_Unwind_Ptr);
145 // ...A more principled approach would be to provide the full definition of ARM's
146 // _Unwind_Context in our libunwind bindings and fetch the required data from there
147 // directly, bypassing DWARF compatibility functions.
148
149 let eh_action = match find_eh_action(context) {
150 Ok(action) => action,
151 Err(_) => return uw::_URC_FAILURE,
152 };
153 if search_phase {
154 match eh_action {
155 EHAction::None | EHAction::Cleanup(_) => {
156 return continue_unwind(exception_object, context);
157 }
158 EHAction::Catch(_) | EHAction::Filter(_) => {
159 // EHABI requires the personality routine to update the
160 // SP value in the barrier cache of the exception object.
161 (*exception_object).private[5] =
162 uw::_Unwind_GetGR(context, uw::UNWIND_SP_REG);
163 return uw::_URC_HANDLER_FOUND;
164 }
165 EHAction::Terminate => return uw::_URC_FAILURE,
166 }
167 } else {
168 match eh_action {
169 EHAction::None => return continue_unwind(exception_object, context),
170 EHAction::Filter(_) if state & uw::_US_FORCE_UNWIND as c_int != 0 => return continue_unwind(exception_object, context),
171 EHAction::Cleanup(lpad) | EHAction::Catch(lpad) | EHAction::Filter(lpad) => {
172 uw::_Unwind_SetGR(
173 context,
174 UNWIND_DATA_REG.0,
175 exception_object as uw::_Unwind_Ptr,
176 );
177 uw::_Unwind_SetGR(context, UNWIND_DATA_REG.1, core::ptr::null());
178 uw::_Unwind_SetIP(context, lpad);
179 return uw::_URC_INSTALL_CONTEXT;
180 }
181 EHAction::Terminate => return uw::_URC_FAILURE,
182 }
183 }
184
185 // On ARM EHABI the personality routine is responsible for actually
186 // unwinding a single stack frame before returning (ARM EHABI Sec. 6.1).
187 unsafe fn continue_unwind(
188 exception_object: *mut uw::_Unwind_Exception,
189 context: *mut uw::_Unwind_Context,
190 ) -> uw::_Unwind_Reason_Code {
191 unsafe {
192 if __gnu_unwind_frame(exception_object, context) == uw::_URC_NO_REASON {
193 uw::_URC_CONTINUE_UNWIND
194 } else {
195 uw::_URC_FAILURE
196 }
197 }
198 }
199 // defined in libgcc
200 unsafe extern "C" {
201 fn __gnu_unwind_frame(
202 exception_object: *mut uw::_Unwind_Exception,
203 context: *mut uw::_Unwind_Context,
204 ) -> uw::_Unwind_Reason_Code;
205 }
206 }
207 }
208 } else {
209 /// Default personality routine, which is used directly on most targets
210 /// and indirectly on Windows x86_64 and AArch64 via SEH.
211 unsafe extern "C" fn rust_eh_personality_impl(
212 version: c_int,
213 actions: uw::_Unwind_Action,
214 _exception_class: uw::_Unwind_Exception_Class,
215 exception_object: *mut uw::_Unwind_Exception,
216 context: *mut uw::_Unwind_Context,
217 ) -> uw::_Unwind_Reason_Code {
218 unsafe {
219 if version != 1 {
220 return uw::_URC_FATAL_PHASE1_ERROR;
221 }
222 let eh_action = match find_eh_action(context) {
223 Ok(action) => action,
224 Err(_) => return uw::_URC_FATAL_PHASE1_ERROR,
225 };
226 if actions as i32 & uw::_UA_SEARCH_PHASE as i32 != 0 {
227 match eh_action {
228 EHAction::None | EHAction::Cleanup(_) => uw::_URC_CONTINUE_UNWIND,
229 EHAction::Catch(_) | EHAction::Filter(_) => uw::_URC_HANDLER_FOUND,
230 EHAction::Terminate => uw::_URC_FATAL_PHASE1_ERROR,
231 }
232 } else {
233 match eh_action {
234 EHAction::None => uw::_URC_CONTINUE_UNWIND,
235 // Forced unwinding hits a terminate action.
236 EHAction::Filter(_) if actions as i32 & uw::_UA_FORCE_UNWIND as i32 != 0 => uw::_URC_CONTINUE_UNWIND,
237 EHAction::Cleanup(lpad) | EHAction::Catch(lpad) | EHAction::Filter(lpad) => {
238 uw::_Unwind_SetGR(
239 context,
240 UNWIND_DATA_REG.0,
241 exception_object.cast(),
242 );
243 uw::_Unwind_SetGR(context, UNWIND_DATA_REG.1, core::ptr::null());
244 uw::_Unwind_SetIP(context, lpad);
245 uw::_URC_INSTALL_CONTEXT
246 }
247 EHAction::Terminate => uw::_URC_FATAL_PHASE2_ERROR,
248 }
249 }
250 }
251 }
252
253 cfg_if::cfg_if! {
254 if #[cfg(all(windows, any(target_arch = "aarch64", target_arch = "x86_64"), target_env = "gnu"))] {
255 /// personality fn called by [Windows Structured Exception Handling][windows-eh]
256 ///
257 /// On x86_64 and AArch64 MinGW targets, the unwinding mechanism is SEH,
258 /// however the unwind handler data (aka LSDA) uses GCC-compatible encoding
259 ///
260 /// [windows-eh]: https://learn.microsoft.com/en-us/cpp/cpp/structured-exception-handling-c-cpp?view=msvc-170
261 #[lang = "eh_personality"]
262 #[allow(nonstandard_style)]
263 unsafe extern "C" fn rust_eh_personality(
264 exceptionRecord: *mut uw::EXCEPTION_RECORD,
265 establisherFrame: uw::LPVOID,
266 contextRecord: *mut uw::CONTEXT,
267 dispatcherContext: *mut uw::DISPATCHER_CONTEXT,
268 ) -> uw::EXCEPTION_DISPOSITION {
269 // SAFETY: the cfg is still target_os = "windows" and target_env = "gnu",
270 // which means that this is the correct function to call, passing our impl fn
271 // as the callback which gets actually used
272 unsafe {
273 uw::_GCC_specific_handler(
274 exceptionRecord,
275 establisherFrame,
276 contextRecord,
277 dispatcherContext,
278 rust_eh_personality_impl,
279 )
280 }
281 }
282 } else {
283 /// personality fn called by [Itanium C++ ABI Exception Handling][itanium-eh]
284 ///
285 /// The personality routine for most non-Windows targets. This will be called by
286 /// the unwinding library:
287 /// - "In the search phase, the framework repeatedly calls the personality routine,
288 /// with the _UA_SEARCH_PHASE flag as described below, first for the current PC
289 /// and register state, and then unwinding a frame to a new PC at each step..."
290 /// - "If the search phase reports success, the framework restarts in the cleanup
291 /// phase. Again, it repeatedly calls the personality routine, with the
292 /// _UA_CLEANUP_PHASE flag as described below, first for the current PC and
293 /// register state, and then unwinding a frame to a new PC at each step..."i
294 ///
295 /// [itanium-eh]: https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
296 #[lang = "eh_personality"]
297 unsafe extern "C" fn rust_eh_personality(
298 version: c_int,
299 actions: uw::_Unwind_Action,
300 exception_class: uw::_Unwind_Exception_Class,
301 exception_object: *mut uw::_Unwind_Exception,
302 context: *mut uw::_Unwind_Context,
303 ) -> uw::_Unwind_Reason_Code {
304 // SAFETY: the platform support must modify the cfg for the inner fn
305 // if it needs something different than what is currently invoked.
306 unsafe {
307 rust_eh_personality_impl(
308 version,
309 actions,
310 exception_class,
311 exception_object,
312 context,
313 )
314 }
315 }
316 }
317 }
318 }
319}
320
321unsafe fn find_eh_action(context: *mut uw::_Unwind_Context) -> Result<EHAction, ()> {
322 unsafe {
323 let lsda = uw::_Unwind_GetLanguageSpecificData(context) as *const u8;
324 let mut ip_before_instr: c_int = 0;
325 let ip = uw::_Unwind_GetIPInfo(context, &mut ip_before_instr);
326 let eh_context = EHContext {
327 // The return address points 1 byte past the call instruction,
328 // which could be in the next IP range in LSDA range table.
329 //
330 // `ip = -1` has special meaning, so use wrapping sub to allow for that
331 ip: if ip_before_instr != 0 { ip } else { ip.wrapping_sub(1) },
332 func_start: uw::_Unwind_GetRegionStart(context),
333 get_text_start: &|| uw::_Unwind_GetTextRelBase(context),
334 get_data_start: &|| uw::_Unwind_GetDataRelBase(context),
335 };
336 eh::find_eh_action(lsda, &eh_context)
337 }
338}