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 use alloc::boxed::Box;
10 use core::any::Any;
11 use core::intrinsics;
12 use core::mem;
13 use core::ptr;
14 use core::sync::atomic::{AtomicBool, Ordering};
15 use unwind as uw;
16
17 // This matches the layout of std::type_info in C++
18 #[repr(C)]
19 struct TypeInfo {
20 vtable: *const usize,
21 name: *const u8,
22 }
23 unsafe impl Sync for TypeInfo {}
24
25 extern "C" {
26 // The leading `\x01` byte here is actually a magical signal to LLVM to
27 // *not* apply any other mangling like prefixing with a `_` character.
28 //
29 // This symbol is the vtable used by C++'s `std::type_info`. Objects of type
30 // `std::type_info`, type descriptors, have a pointer to this table. Type
31 // descriptors are referenced by the C++ EH structures defined above and
32 // that we construct below.
33 //
34 // Note that the real size is larger than 3 usize, but we only need our
35 // vtable to point to the third element.
36 #[link_name = "\x01_ZTVN10__cxxabiv117__class_type_infoE"]
37 static CLASS_TYPE_INFO_VTABLE: [usize; 3];
38 }
39
40 // std::type_info for a rust_panic class
41 #[lang = "eh_catch_typeinfo"]
42 static EXCEPTION_TYPE_INFO: TypeInfo = TypeInfo {
43 // Normally we would use .as_ptr().add(2) but this doesn't work in a const context.
44 vtable: unsafe { &CLASS_TYPE_INFO_VTABLE[2] },
45 // This intentionally doesn't use the normal name mangling scheme because
46 // we don't want C++ to be able to produce or catch Rust panics.
47 name: b"rust_panic\0".as_ptr(),
48 };
49
50 // NOTE(nbdd0121): The `canary` field is part of stable ABI.
51 #[repr(C)]
52 struct Exception {
53 // See `gcc.rs` on why this is present. We already have a static here so just use it.
54 canary: *const TypeInfo,
55
56 // This is necessary because C++ code can capture our exception with
57 // std::exception_ptr and rethrow it multiple times, possibly even in
58 // another thread.
59 caught: AtomicBool,
60
61 // This needs to be an Option because the object's lifetime follows C++
62 // semantics: when catch_unwind moves the Box out of the exception it must
63 // still leave the exception object in a valid state because its destructor
64 // is still going to be called by __cxa_end_catch.
65 data: Option<Box<dyn Any + Send>>,
66 }
67
cleanup(ptr: *mut u8) -> Box<dyn Any + Send>68 pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
69 // intrinsics::try actually gives us a pointer to this structure.
70 #[repr(C)]
71 struct CatchData {
72 ptr: *mut u8,
73 is_rust_panic: bool,
74 }
75 let catch_data = &*(ptr as *mut CatchData);
76
77 let adjusted_ptr = __cxa_begin_catch(catch_data.ptr as *mut libc::c_void) as *mut Exception;
78 if !catch_data.is_rust_panic {
79 super::__rust_foreign_exception();
80 }
81
82 let canary = ptr::addr_of!((*adjusted_ptr).canary).read();
83 if !ptr::eq(canary, &EXCEPTION_TYPE_INFO) {
84 super::__rust_foreign_exception();
85 }
86
87 let was_caught = (*adjusted_ptr).caught.swap(true, Ordering::SeqCst);
88 if was_caught {
89 // Since cleanup() isn't allowed to panic, we just abort instead.
90 intrinsics::abort();
91 }
92 let out = (*adjusted_ptr).data.take().unwrap();
93 __cxa_end_catch();
94 out
95 }
96
panic(data: Box<dyn Any + Send>) -> u3297 pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
98 let exception = __cxa_allocate_exception(mem::size_of::<Exception>()) as *mut Exception;
99 if exception.is_null() {
100 return uw::_URC_FATAL_PHASE1_ERROR as u32;
101 }
102 ptr::write(
103 exception,
104 Exception {
105 canary: &EXCEPTION_TYPE_INFO,
106 caught: AtomicBool::new(false),
107 data: Some(data),
108 },
109 );
110 __cxa_throw(exception as *mut _, &EXCEPTION_TYPE_INFO, exception_cleanup);
111 }
112
exception_cleanup(ptr: *mut libc::c_void) -> *mut libc::c_void113 extern "C" fn exception_cleanup(ptr: *mut libc::c_void) -> *mut libc::c_void {
114 unsafe {
115 if let Some(b) = (ptr as *mut Exception).read().data {
116 drop(b);
117 super::__rust_drop_panic();
118 }
119 ptr
120 }
121 }
122
123 extern "C" {
__cxa_allocate_exception(thrown_size: libc::size_t) -> *mut libc::c_void124 fn __cxa_allocate_exception(thrown_size: libc::size_t) -> *mut libc::c_void;
__cxa_begin_catch(thrown_exception: *mut libc::c_void) -> *mut libc::c_void125 fn __cxa_begin_catch(thrown_exception: *mut libc::c_void) -> *mut libc::c_void;
__cxa_end_catch()126 fn __cxa_end_catch();
__cxa_throw( thrown_exception: *mut libc::c_void, tinfo: *const TypeInfo, dest: extern "C" fn(*mut libc::c_void) -> *mut libc::c_void, ) -> !127 fn __cxa_throw(
128 thrown_exception: *mut libc::c_void,
129 tinfo: *const TypeInfo,
130 dest: extern "C" fn(*mut libc::c_void) -> *mut libc::c_void,
131 ) -> !;
132 }
133