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