]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/emcc.rs
873135414bd9d46b46cb4397386a3fb3795891a5
[rust.git] / src / libpanic_unwind / emcc.rs
1 //! Unwinding for *emscripten* target.
2 //!
3 //! Whereas Rust's usual unwinding implementation for Unix platforms
4 //! calls into the libunwind APIs directly, on Emscripten we instead
5 //! call into the C++ unwinding APIs. This is just an expedience since
6 //! Emscripten's runtime always implements those APIs and does not
7 //! implement libunwind.
8
9 #![allow(private_no_mangle_fns)]
10
11 use alloc::boxed::Box;
12 use core::any::Any;
13 use core::mem;
14 use core::ptr;
15 use libc::{self, c_int};
16 use unwind as uw;
17
18 // This matches the layout of std::type_info in C++
19 #[repr(C)]
20 struct TypeInfo {
21     vtable: *const usize,
22     name: *const u8,
23 }
24 unsafe impl Sync for TypeInfo {}
25
26 extern "C" {
27     // The leading `\x01` byte here is actually a magical signal to LLVM to
28     // *not* apply any other mangling like prefixing with a `_` character.
29     //
30     // This symbol is the vtable used by C++'s `std::type_info`. Objects of type
31     // `std::type_info`, type descriptors, have a pointer to this table. Type
32     // descriptors are referenced by the C++ EH structures defined above and
33     // that we construct below.
34     //
35     // Note that the real size is larger than 3 usize, but we only need our
36     // vtable to point to the third element.
37     #[link_name = "\x01_ZTVN10__cxxabiv117__class_type_infoE"]
38     static CLASS_TYPE_INFO_VTABLE: [usize; 3];
39 }
40
41 // std::type_info for a rust_panic class
42 #[lang = "eh_catch_typeinfo"]
43 static EXCEPTION_TYPE_INFO: TypeInfo = TypeInfo {
44     // Normally we would use .as_ptr().add(2) but this doesn't work in a const context.
45     vtable: unsafe { &CLASS_TYPE_INFO_VTABLE[2] },
46     // This intentionally doesn't use the normal name mangling scheme because
47     // we don't want C++ to be able to produce or catch Rust panics.
48     name: b"rust_panic\0".as_ptr(),
49 };
50
51 pub type Payload = *mut u8;
52
53 struct Exception {
54     // This needs to be an Option because the object's lifetime follows C++
55     // semantics: when catch_unwind moves the Box out of the exception it must
56     // still leave the exception object in a valid state because its destructor
57     // is still going to be called by __cxa_end_catch..
58     data: Option<Box<dyn Any + Send>>,
59 }
60
61 pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
62     assert!(!ptr.is_null());
63     let adjusted_ptr = __cxa_begin_catch(ptr as *mut libc::c_void) as *mut Exception;
64     let ex = (*adjusted_ptr).data.take();
65     __cxa_end_catch();
66     ex.unwrap()
67 }
68
69 pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
70     let sz = mem::size_of_val(&data);
71     let exception = __cxa_allocate_exception(sz) as *mut Exception;
72     if exception.is_null() {
73         return uw::_URC_FATAL_PHASE1_ERROR as u32;
74     }
75     ptr::write(exception, Exception { data: Some(data) });
76     __cxa_throw(exception as *mut _, &EXCEPTION_TYPE_INFO, exception_cleanup);
77 }
78
79 // On WASM and ARM, the destructor returns the pointer to the object.
80 cfg_if::cfg_if! {
81     if #[cfg(any(target_arch = "arm", target_arch = "wasm32"))] {
82         type DestructorRet = *mut libc::c_void;
83     } else {
84         type DestructorRet = ();
85     }
86 }
87 extern "C" fn exception_cleanup(ptr: *mut libc::c_void) -> DestructorRet {
88     unsafe {
89         if let Some(b) = (ptr as *mut Exception).read().data {
90             drop(b);
91             super::__rust_drop_panic();
92         }
93         #[cfg(any(target_arch = "arm", target_arch = "wasm32"))]
94         ptr
95     }
96 }
97
98 #[lang = "eh_personality"]
99 #[no_mangle]
100 unsafe extern "C" fn rust_eh_personality(
101     version: c_int,
102     actions: uw::_Unwind_Action,
103     exception_class: uw::_Unwind_Exception_Class,
104     exception_object: *mut uw::_Unwind_Exception,
105     context: *mut uw::_Unwind_Context,
106 ) -> uw::_Unwind_Reason_Code {
107     __gxx_personality_v0(version, actions, exception_class, exception_object, context)
108 }
109
110 extern "C" {
111     fn __cxa_allocate_exception(thrown_size: libc::size_t) -> *mut libc::c_void;
112     fn __cxa_begin_catch(thrown_exception: *mut libc::c_void) -> *mut libc::c_void;
113     fn __cxa_end_catch();
114     fn __cxa_throw(
115         thrown_exception: *mut libc::c_void,
116         tinfo: *const TypeInfo,
117         dest: extern "C" fn(*mut libc::c_void) -> DestructorRet,
118     ) -> !;
119     fn __gxx_personality_v0(
120         version: c_int,
121         actions: uw::_Unwind_Action,
122         exception_class: uw::_Unwind_Exception_Class,
123         exception_object: *mut uw::_Unwind_Exception,
124         context: *mut uw::_Unwind_Context,
125     ) -> uw::_Unwind_Reason_Code;
126 }