Skip to content

Commit 125b26a

Browse files
committed
Use Itanium ABI for thrown exceptions
1 parent 658a0a2 commit 125b26a

File tree

7 files changed

+67
-43
lines changed

7 files changed

+67
-43
lines changed

library/panic_unwind/src/gcc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
6262
let exception = Box::new(Exception {
6363
_uwe: uw::_Unwind_Exception {
6464
exception_class: rust_exception_class(),
65-
exception_cleanup,
65+
exception_cleanup: Some(exception_cleanup),
6666
private: [core::ptr::null(); uw::unwinder_private_data_size],
6767
},
6868
canary: &CANARY,

library/panic_unwind/src/lib.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#![doc(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
1717
#![feature(core_intrinsics)]
1818
#![feature(lang_items)]
19-
#![cfg_attr(
20-
all(target_family = "wasm", not(target_os = "emscripten")),
21-
feature(link_llvm_intrinsics)
22-
)]
2319
#![feature(panic_unwind)]
2420
#![feature(staged_api)]
2521
#![feature(std_internals)]
@@ -57,12 +53,10 @@ cfg_if::cfg_if! {
5753
target_os = "solid_asp3",
5854
all(target_family = "unix", not(target_os = "espidf")),
5955
all(target_vendor = "fortanix", target_env = "sgx"),
56+
target_family = "wasm",
6057
))] {
6158
#[path = "gcc.rs"]
6259
mod real_imp;
63-
} else if #[cfg(target_family = "wasm")] {
64-
#[path = "wasm.rs"]
65-
mod real_imp;
6660
} else {
6761
// Targets that don't support unwinding.
6862
// - os=none ("bare metal" targets)

library/panic_unwind/src/wasm.rs

-32
This file was deleted.

library/unwind/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#![feature(cfg_target_abi)]
77
#![feature(strict_provenance)]
88
#![cfg_attr(not(target_env = "msvc"), feature(libc))]
9+
#![cfg_attr(
10+
all(target_family = "wasm", not(target_os = "emscripten")),
11+
feature(link_llvm_intrinsics)
12+
)]
913
#![allow(internal_features)]
1014

1115
cfg_if::cfg_if! {
@@ -29,9 +33,11 @@ cfg_if::cfg_if! {
2933
} else if #[cfg(target_os = "xous")] {
3034
mod unwinding;
3135
pub use unwinding::*;
36+
} else if #[cfg(target_family = "wasm")] {
37+
mod wasm;
38+
pub use wasm::*;
3239
} else {
3340
// no unwinder on the system!
34-
// - wasm32 (not emscripten, which is "unix" family)
3541
// - os=none ("bare metal" targets)
3642
// - os=hermit
3743
// - os=uefi

library/unwind/src/libunwind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub struct _Unwind_Exception {
9191
pub enum _Unwind_Context {}
9292

9393
pub type _Unwind_Exception_Cleanup_Fn =
94-
extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception);
94+
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
9595

9696
// FIXME: The `#[link]` attributes on `extern "C"` block marks those symbols declared in
9797
// the block are reexported in dylib build of std. This is needed when build rustc with

library/unwind/src/unwinding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub const unwinder_private_data_size: usize = core::mem::size_of::<UnwindExcepti
4646
- core::mem::size_of::<_Unwind_Exception_Cleanup_Fn>();
4747

4848
pub type _Unwind_Exception_Cleanup_Fn =
49-
extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception);
49+
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
5050

5151
#[repr(C)]
5252
pub struct _Unwind_Exception {

library/unwind/src/wasm.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! A shim for libunwind implemented in terms of the native wasm `throw` instruction.
2+
3+
#![allow(nonstandard_style)]
4+
5+
#[repr(C)]
6+
#[derive(Debug, Copy, Clone, PartialEq)]
7+
pub enum _Unwind_Reason_Code {
8+
_URC_NO_REASON = 0,
9+
_URC_FOREIGN_EXCEPTION_CAUGHT = 1,
10+
_URC_FATAL_PHASE2_ERROR = 2,
11+
_URC_FATAL_PHASE1_ERROR = 3,
12+
_URC_NORMAL_STOP = 4,
13+
_URC_END_OF_STACK = 5,
14+
_URC_HANDLER_FOUND = 6,
15+
_URC_INSTALL_CONTEXT = 7,
16+
_URC_CONTINUE_UNWIND = 8,
17+
_URC_FAILURE = 9, // used only by ARM EHABI
18+
}
19+
pub use _Unwind_Reason_Code::*;
20+
21+
pub type _Unwind_Exception_Class = u64;
22+
pub type _Unwind_Word = *const u8;
23+
24+
pub const unwinder_private_data_size: usize = 2;
25+
26+
#[repr(C)]
27+
pub struct _Unwind_Exception {
28+
pub exception_class: _Unwind_Exception_Class,
29+
pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
30+
pub private: [_Unwind_Word; unwinder_private_data_size],
31+
}
32+
33+
pub type _Unwind_Exception_Cleanup_Fn =
34+
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
35+
36+
pub unsafe fn _Unwind_DeleteException(exception: *mut _Unwind_Exception) {
37+
if let Some(exception_cleanup) = unsafe { (*exception).exception_cleanup } {
38+
exception_cleanup(_URC_FOREIGN_EXCEPTION_CAUGHT, exception);
39+
}
40+
}
41+
42+
pub unsafe fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
43+
extern "C" {
44+
/// LLVM lowers this intrinsic to the `throw` instruction.
45+
// FIXME(coolreader18): move to stdarch
46+
#[link_name = "llvm.wasm.throw"]
47+
fn wasm_throw(tag: i32, ptr: *mut u8) -> !;
48+
}
49+
50+
// The wasm `throw` instruction takes a "tag", which differentiates certain
51+
// types of exceptions from others. LLVM currently just identifies these
52+
// via integers, with 0 corresponding to C++ exceptions and 1 to C setjmp()/longjmp().
53+
// Ideally, we'd be able to choose something unique for Rust, but for now,
54+
// we pretend to be C++ and implement the Itanium exception-handling ABI.
55+
wasm_throw(0, exception.cast())
56+
}

0 commit comments

Comments
 (0)