1 //! Unwinding for *emscripten* target.
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.
14 use core::sync::atomic::{AtomicBool, Ordering};
15 use libc::{self, c_int};
18 // This matches the layout of std::type_info in C++
24 unsafe impl Sync for TypeInfo {}
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.
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.
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];
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(),
52 // This is necessary because C++ code can capture our exception with
53 // std::exception_ptr and rethrow it multiple times, possibly even in
57 // This needs to be an Option because the object's lifetime follows C++
58 // semantics: when catch_unwind moves the Box out of the exception it must
59 // still leave the exception object in a valid state because its destructor
60 // is still going to be called by __cxa_end_catch.
61 data: Option<Box<dyn Any + Send>>,
64 pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
65 // intrinsics::try actually gives us a pointer to this structure.
71 let catch_data = &*(ptr as *mut CatchData);
73 let adjusted_ptr = __cxa_begin_catch(catch_data.ptr as *mut libc::c_void) as *mut Exception;
74 let out = if catch_data.is_rust_panic {
75 let was_caught = (*adjusted_ptr).caught.swap(true, Ordering::SeqCst);
77 // Since cleanup() isn't allowed to panic, we just abort instead.
80 (*adjusted_ptr).data.take().unwrap()
82 super::__rust_foreign_exception();
88 pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
89 let sz = mem::size_of_val(&data);
90 let exception = __cxa_allocate_exception(sz) as *mut Exception;
91 if exception.is_null() {
92 return uw::_URC_FATAL_PHASE1_ERROR as u32;
94 ptr::write(exception, Exception { caught: AtomicBool::new(false), data: Some(data) });
95 __cxa_throw(exception as *mut _, &EXCEPTION_TYPE_INFO, exception_cleanup);
98 extern "C" fn exception_cleanup(ptr: *mut libc::c_void) -> *mut libc::c_void {
100 if let Some(b) = (ptr as *mut Exception).read().data {
102 super::__rust_drop_panic();
108 // This is required by the compiler to exist (e.g., it's a lang item), but it's
109 // never actually called by the compiler. Emscripten EH doesn't use a
110 // personality function at all, it instead uses __cxa_find_matching_catch.
111 // Wasm error handling would use __gxx_personality_wasm0.
112 #[lang = "eh_personality"]
113 unsafe extern "C" fn rust_eh_personality(
115 _actions: uw::_Unwind_Action,
116 _exception_class: uw::_Unwind_Exception_Class,
117 _exception_object: *mut uw::_Unwind_Exception,
118 _context: *mut uw::_Unwind_Context,
119 ) -> uw::_Unwind_Reason_Code {
120 core::intrinsics::abort()
124 fn __cxa_allocate_exception(thrown_size: libc::size_t) -> *mut libc::c_void;
125 fn __cxa_begin_catch(thrown_exception: *mut libc::c_void) -> *mut libc::c_void;
126 fn __cxa_end_catch();
128 thrown_exception: *mut libc::c_void,
129 tinfo: *const TypeInfo,
130 dest: extern "C" fn(*mut libc::c_void) -> *mut libc::c_void,