2019-02-08 07:53:55 -06:00
|
|
|
//! Unwinding for *emscripten* target.
|
2016-09-27 16:25:52 -05:00
|
|
|
//!
|
|
|
|
//! Whereas Rust's usual unwinding implementation for Unix platforms
|
2019-02-08 07:53:55 -06:00
|
|
|
//! calls into the libunwind APIs directly, on Emscripten we instead
|
2016-09-27 16:25:52 -05:00
|
|
|
//! call into the C++ unwinding APIs. This is just an expedience since
|
2019-02-08 07:53:55 -06:00
|
|
|
//! Emscripten's runtime always implements those APIs and does not
|
2016-09-27 16:25:52 -05:00
|
|
|
//! implement libunwind.
|
|
|
|
|
2016-09-22 14:55:42 -05:00
|
|
|
use alloc::boxed::Box;
|
|
|
|
use core::any::Any;
|
2020-03-21 02:50:38 -05:00
|
|
|
use core::intrinsics;
|
2019-02-02 12:00:02 -06:00
|
|
|
use core::mem;
|
2016-09-22 14:55:42 -05:00
|
|
|
use core::ptr;
|
2020-03-21 02:50:38 -05:00
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
2016-09-22 14:55:42 -05:00
|
|
|
use unwind as uw;
|
|
|
|
|
2019-10-27 17:33:16 -05:00
|
|
|
// This matches the layout of std::type_info in C++
|
|
|
|
#[repr(C)]
|
|
|
|
struct TypeInfo {
|
|
|
|
vtable: *const usize,
|
|
|
|
name: *const u8,
|
|
|
|
}
|
|
|
|
unsafe impl Sync for TypeInfo {}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
// The leading `\x01` byte here is actually a magical signal to LLVM to
|
|
|
|
// *not* apply any other mangling like prefixing with a `_` character.
|
|
|
|
//
|
|
|
|
// This symbol is the vtable used by C++'s `std::type_info`. Objects of type
|
|
|
|
// `std::type_info`, type descriptors, have a pointer to this table. Type
|
|
|
|
// descriptors are referenced by the C++ EH structures defined above and
|
|
|
|
// that we construct below.
|
|
|
|
//
|
|
|
|
// Note that the real size is larger than 3 usize, but we only need our
|
|
|
|
// vtable to point to the third element.
|
|
|
|
#[link_name = "\x01_ZTVN10__cxxabiv117__class_type_infoE"]
|
|
|
|
static CLASS_TYPE_INFO_VTABLE: [usize; 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
// std::type_info for a rust_panic class
|
|
|
|
#[lang = "eh_catch_typeinfo"]
|
|
|
|
static EXCEPTION_TYPE_INFO: TypeInfo = TypeInfo {
|
|
|
|
// Normally we would use .as_ptr().add(2) but this doesn't work in a const context.
|
|
|
|
vtable: unsafe { &CLASS_TYPE_INFO_VTABLE[2] },
|
|
|
|
// This intentionally doesn't use the normal name mangling scheme because
|
|
|
|
// we don't want C++ to be able to produce or catch Rust panics.
|
|
|
|
name: b"rust_panic\0".as_ptr(),
|
|
|
|
};
|
|
|
|
|
2019-12-26 09:46:32 -06:00
|
|
|
struct Exception {
|
2021-12-14 08:23:34 -06:00
|
|
|
// This is necessary because C++ code can capture our exception with
|
2020-03-21 02:50:38 -05:00
|
|
|
// std::exception_ptr and rethrow it multiple times, possibly even in
|
|
|
|
// another thread.
|
|
|
|
caught: AtomicBool,
|
|
|
|
|
2020-01-07 04:36:57 -06:00
|
|
|
// This needs to be an Option because the object's lifetime follows C++
|
|
|
|
// semantics: when catch_unwind moves the Box out of the exception it must
|
|
|
|
// still leave the exception object in a valid state because its destructor
|
2020-03-02 07:59:20 -06:00
|
|
|
// is still going to be called by __cxa_end_catch.
|
2019-12-26 09:46:32 -06:00
|
|
|
data: Option<Box<dyn Any + Send>>,
|
|
|
|
}
|
|
|
|
|
2018-07-11 10:11:08 -05:00
|
|
|
pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
|
2020-03-21 02:50:38 -05:00
|
|
|
// intrinsics::try actually gives us a pointer to this structure.
|
|
|
|
#[repr(C)]
|
|
|
|
struct CatchData {
|
|
|
|
ptr: *mut u8,
|
|
|
|
is_rust_panic: bool,
|
|
|
|
}
|
|
|
|
let catch_data = &*(ptr as *mut CatchData);
|
|
|
|
|
|
|
|
let adjusted_ptr = __cxa_begin_catch(catch_data.ptr as *mut libc::c_void) as *mut Exception;
|
|
|
|
let out = if catch_data.is_rust_panic {
|
|
|
|
let was_caught = (*adjusted_ptr).caught.swap(true, Ordering::SeqCst);
|
|
|
|
if was_caught {
|
|
|
|
// Since cleanup() isn't allowed to panic, we just abort instead.
|
|
|
|
intrinsics::abort();
|
|
|
|
}
|
|
|
|
(*adjusted_ptr).data.take().unwrap()
|
|
|
|
} else {
|
|
|
|
super::__rust_foreign_exception();
|
|
|
|
};
|
2019-10-27 17:33:16 -05:00
|
|
|
__cxa_end_catch();
|
2020-03-21 02:50:38 -05:00
|
|
|
out
|
2016-09-22 14:55:42 -05:00
|
|
|
}
|
|
|
|
|
2018-07-11 10:11:08 -05:00
|
|
|
pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
|
2016-09-22 14:55:42 -05:00
|
|
|
let sz = mem::size_of_val(&data);
|
2019-12-26 09:46:32 -06:00
|
|
|
let exception = __cxa_allocate_exception(sz) as *mut Exception;
|
2020-01-10 06:52:00 -06:00
|
|
|
if exception.is_null() {
|
2016-09-22 14:55:42 -05:00
|
|
|
return uw::_URC_FATAL_PHASE1_ERROR as u32;
|
|
|
|
}
|
2020-03-21 02:50:38 -05:00
|
|
|
ptr::write(exception, Exception { caught: AtomicBool::new(false), data: Some(data) });
|
2019-12-26 09:46:32 -06:00
|
|
|
__cxa_throw(exception as *mut _, &EXCEPTION_TYPE_INFO, exception_cleanup);
|
2020-01-12 18:39:41 -06:00
|
|
|
}
|
2019-12-26 09:46:32 -06:00
|
|
|
|
2020-03-21 02:50:38 -05:00
|
|
|
extern "C" fn exception_cleanup(ptr: *mut libc::c_void) -> *mut libc::c_void {
|
2020-01-12 18:39:41 -06:00
|
|
|
unsafe {
|
2020-01-13 02:04:48 -06:00
|
|
|
if let Some(b) = (ptr as *mut Exception).read().data {
|
|
|
|
drop(b);
|
|
|
|
super::__rust_drop_panic();
|
|
|
|
}
|
|
|
|
ptr
|
2019-12-26 09:46:32 -06:00
|
|
|
}
|
2016-09-22 14:55:42 -05:00
|
|
|
}
|
|
|
|
|
2016-10-18 12:39:47 -05:00
|
|
|
extern "C" {
|
2016-09-22 14:55:42 -05:00
|
|
|
fn __cxa_allocate_exception(thrown_size: libc::size_t) -> *mut libc::c_void;
|
2019-10-27 17:33:16 -05:00
|
|
|
fn __cxa_begin_catch(thrown_exception: *mut libc::c_void) -> *mut libc::c_void;
|
|
|
|
fn __cxa_end_catch();
|
2016-09-22 14:55:42 -05:00
|
|
|
fn __cxa_throw(
|
|
|
|
thrown_exception: *mut libc::c_void,
|
2019-10-27 17:33:16 -05:00
|
|
|
tinfo: *const TypeInfo,
|
2020-03-21 02:50:38 -05:00
|
|
|
dest: extern "C" fn(*mut libc::c_void) -> *mut libc::c_void,
|
2019-10-27 17:33:16 -05:00
|
|
|
) -> !;
|
2016-09-22 14:55:42 -05:00
|
|
|
}
|