]> git.lizzy.rs Git - rust.git/blob - library/panic_unwind/src/gcc.rs
Rollup merge of #103159 - cuviper:check_pow-final-try_opt, r=Mark-Simulacrum
[rust.git] / library / panic_unwind / src / 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://monoinfinito.wordpress.com/series/exception-handling-in-c/>
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
39 use alloc::boxed::Box;
40 use core::any::Any;
41
42 use unwind as uw;
43
44 #[repr(C)]
45 struct Exception {
46     _uwe: uw::_Unwind_Exception,
47     cause: Box<dyn Any + Send>,
48 }
49
50 pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
51     let exception = Box::new(Exception {
52         _uwe: uw::_Unwind_Exception {
53             exception_class: rust_exception_class(),
54             exception_cleanup,
55             private: [0; uw::unwinder_private_data_size],
56         },
57         cause: data,
58     });
59     let exception_param = Box::into_raw(exception) as *mut uw::_Unwind_Exception;
60     return uw::_Unwind_RaiseException(exception_param) as u32;
61
62     extern "C" fn exception_cleanup(
63         _unwind_code: uw::_Unwind_Reason_Code,
64         exception: *mut uw::_Unwind_Exception,
65     ) {
66         unsafe {
67             let _: Box<Exception> = Box::from_raw(exception as *mut Exception);
68             super::__rust_drop_panic();
69         }
70     }
71 }
72
73 pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
74     let exception = ptr as *mut uw::_Unwind_Exception;
75     if (*exception).exception_class != rust_exception_class() {
76         uw::_Unwind_DeleteException(exception);
77         super::__rust_foreign_exception();
78     } else {
79         let exception = Box::from_raw(exception as *mut Exception);
80         exception.cause
81     }
82 }
83
84 // Rust's exception class identifier.  This is used by personality routines to
85 // determine whether the exception was thrown by their own runtime.
86 fn rust_exception_class() -> uw::_Unwind_Exception_Class {
87     // M O Z \0  R U S T -- vendor, language
88     0x4d4f5a_00_52555354
89 }