Use Itanium ABI for thrown exceptions

This commit is contained in:
Noa 2024-02-22 17:32:06 -06:00
parent 658a0a20ea
commit 125b26acf6
No known key found for this signature in database
GPG Key ID: 7F9F7DB1768C59CF
7 changed files with 67 additions and 43 deletions

View File

@ -62,7 +62,7 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
let exception = Box::new(Exception {
_uwe: uw::_Unwind_Exception {
exception_class: rust_exception_class(),
exception_cleanup,
exception_cleanup: Some(exception_cleanup),
private: [core::ptr::null(); uw::unwinder_private_data_size],
},
canary: &CANARY,

View File

@ -16,10 +16,6 @@
#![doc(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
#![feature(core_intrinsics)]
#![feature(lang_items)]
#![cfg_attr(
all(target_family = "wasm", not(target_os = "emscripten")),
feature(link_llvm_intrinsics)
)]
#![feature(panic_unwind)]
#![feature(staged_api)]
#![feature(std_internals)]
@ -57,12 +53,10 @@
target_os = "solid_asp3",
all(target_family = "unix", not(target_os = "espidf")),
all(target_vendor = "fortanix", target_env = "sgx"),
target_family = "wasm",
))] {
#[path = "gcc.rs"]
mod real_imp;
} else if #[cfg(target_family = "wasm")] {
#[path = "wasm.rs"]
mod real_imp;
} else {
// Targets that don't support unwinding.
// - os=none ("bare metal" targets)

View File

@ -1,32 +0,0 @@
//! Unwinding panics for wasm32.
use alloc::boxed::Box;
use core::any::Any;
// The type of the exception payload that the wasm engine propagates
// through unwinding for us. LLVM requires that it be a thin pointer.
type Payload = Box<Box<dyn Any + Send>>;
extern "C" {
/// LLVM lowers this intrinsic to the `throw` instruction.
#[link_name = "llvm.wasm.throw"]
fn wasm_throw(tag: i32, ptr: *mut u8) -> !;
}
pub unsafe fn panic(payload: Box<dyn Any + Send>) -> u32 {
// The payload we pass to `wasm_throw` will be exactly the argument we get
// in `cleanup` below. So we just box it up once, to get something pointer-sized.
let payload_box: Payload = Box::new(payload);
// The wasm `throw` instruction takes a "tag", which differentiates certain
// types of exceptions from others. LLVM currently just identifies these
// via integers, with 0 corresponding to C++ exceptions and 1 to C setjmp()/longjmp().
// Ideally, we'd be able to choose something unique for Rust, such that we
// don't try to treat a C++ exception payload as a `Box<Box<dyn Any>>`, but
// otherwise, pretending to be C++ works for now.
wasm_throw(0, Box::into_raw(payload_box) as *mut u8)
}
pub unsafe fn cleanup(payload_box: *mut u8) -> Box<dyn Any + Send> {
// Recover the underlying `Box`.
let payload_box: Payload = Box::from_raw(payload_box as *mut _);
*payload_box
}

View File

@ -6,6 +6,10 @@
#![feature(cfg_target_abi)]
#![feature(strict_provenance)]
#![cfg_attr(not(target_env = "msvc"), feature(libc))]
#![cfg_attr(
all(target_family = "wasm", not(target_os = "emscripten")),
feature(link_llvm_intrinsics)
)]
#![allow(internal_features)]
cfg_if::cfg_if! {
@ -29,9 +33,11 @@
} else if #[cfg(target_os = "xous")] {
mod unwinding;
pub use unwinding::*;
} else if #[cfg(target_family = "wasm")] {
mod wasm;
pub use wasm::*;
} else {
// no unwinder on the system!
// - wasm32 (not emscripten, which is "unix" family)
// - os=none ("bare metal" targets)
// - os=hermit
// - os=uefi

View File

@ -91,7 +91,7 @@ pub struct _Unwind_Exception {
pub enum _Unwind_Context {}
pub type _Unwind_Exception_Cleanup_Fn =
extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception);
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
// FIXME: The `#[link]` attributes on `extern "C"` block marks those symbols declared in
// the block are reexported in dylib build of std. This is needed when build rustc with

View File

@ -46,7 +46,7 @@ pub enum _Unwind_Context {}
- core::mem::size_of::<_Unwind_Exception_Cleanup_Fn>();
pub type _Unwind_Exception_Cleanup_Fn =
extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception);
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
#[repr(C)]
pub struct _Unwind_Exception {

View File

@ -0,0 +1,56 @@
//! A shim for libunwind implemented in terms of the native wasm `throw` instruction.
#![allow(nonstandard_style)]
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum _Unwind_Reason_Code {
_URC_NO_REASON = 0,
_URC_FOREIGN_EXCEPTION_CAUGHT = 1,
_URC_FATAL_PHASE2_ERROR = 2,
_URC_FATAL_PHASE1_ERROR = 3,
_URC_NORMAL_STOP = 4,
_URC_END_OF_STACK = 5,
_URC_HANDLER_FOUND = 6,
_URC_INSTALL_CONTEXT = 7,
_URC_CONTINUE_UNWIND = 8,
_URC_FAILURE = 9, // used only by ARM EHABI
}
pub use _Unwind_Reason_Code::*;
pub type _Unwind_Exception_Class = u64;
pub type _Unwind_Word = *const u8;
pub const unwinder_private_data_size: usize = 2;
#[repr(C)]
pub struct _Unwind_Exception {
pub exception_class: _Unwind_Exception_Class,
pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
pub private: [_Unwind_Word; unwinder_private_data_size],
}
pub type _Unwind_Exception_Cleanup_Fn =
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
pub unsafe fn _Unwind_DeleteException(exception: *mut _Unwind_Exception) {
if let Some(exception_cleanup) = unsafe { (*exception).exception_cleanup } {
exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, exception);
}
}
pub unsafe fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
extern "C" {
/// LLVM lowers this intrinsic to the `throw` instruction.
// FIXME(coolreader18): move to stdarch
#[link_name = "llvm.wasm.throw"]
fn wasm_throw(tag: i32, ptr: *mut u8) -> !;
}
// The wasm `throw` instruction takes a "tag", which differentiates certain
// types of exceptions from others. LLVM currently just identifies these
// via integers, with 0 corresponding to C++ exceptions and 1 to C setjmp()/longjmp().
// Ideally, we'd be able to choose something unique for Rust, but for now,
// we pretend to be C++ and implement the Itanium exception-handling ABI.
wasm_throw(0, exception.cast())
}