Skip to content

Commit a86cdff

Browse files
committed
Auto merge of #159785 - bjorn3:refactor_unwind2, r=clarfonthey
Share _Unwind_Exception definition between native and wasm And a couple of minor cleanups. Follow up to #159010
2 parents da86f4d + 0ccc68e commit a86cdff

6 files changed

Lines changed: 79 additions & 157 deletions

File tree

‎library/std/src/sys/personality/emcc.rs‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎library/std/src/sys/personality/mod.rs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ mod dwarf;
1414

1515
#[cfg(not(any(test, doctest)))]
1616
cfg_select! {
17-
target_os = "emscripten" => {
18-
mod emcc;
19-
}
2017
any(target_env = "msvc", target_family = "wasm", target_os = "motor") => {
2118
// This is required by the compiler to exist (e.g., it's a lang item),
2219
// but it's never actually called by the compiler because

‎library/unwind/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ cfg_select! {
3737
) => {
3838
mod libunwind;
3939
pub use libunwind::*;
40+
mod types;
4041
}
4142
target_family = "wasm" => {
43+
mod types;
4244
mod wasm;
4345
pub use wasm::*;
4446
}

‎library/unwind/src/libunwind.rs‎

Lines changed: 17 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,12 @@ pub use unwinding::custom_eh_frame_finder::{
88
EhFrameFinder, FrameInfo, FrameInfoKind, set_custom_eh_frame_finder,
99
};
1010

11-
#[repr(C)]
12-
#[derive(Debug, Copy, Clone, PartialEq)]
13-
pub enum _Unwind_Reason_Code {
14-
_URC_NO_REASON = 0,
15-
_URC_FOREIGN_EXCEPTION_CAUGHT = 1,
16-
_URC_FATAL_PHASE2_ERROR = 2,
17-
_URC_FATAL_PHASE1_ERROR = 3,
18-
_URC_NORMAL_STOP = 4,
19-
_URC_END_OF_STACK = 5,
20-
_URC_HANDLER_FOUND = 6,
21-
_URC_INSTALL_CONTEXT = 7,
22-
_URC_CONTINUE_UNWIND = 8,
23-
_URC_FAILURE = 9, // used only by ARM EHABI
24-
}
25-
pub use _Unwind_Reason_Code::*;
11+
pub use crate::types::*;
2612

27-
pub type _Unwind_Exception_Class = u64;
28-
pub type _Unwind_Word = *const u8;
2913
pub type _Unwind_Ptr = *const u8;
30-
pub type _Unwind_Trace_Fn =
31-
extern "C" fn(ctx: *mut _Unwind_Context, arg: *mut c_void) -> _Unwind_Reason_Code;
32-
33-
pub const unwinder_private_data_size: usize = cfg_select! {
34-
target_arch = "x86" => 5,
35-
all(target_arch = "x86_64", not(any(target_os = "windows", target_os = "cygwin"))) => 2,
36-
all(target_arch = "x86_64", any(target_os = "windows", target_os = "cygwin")) => 6,
37-
all(target_arch = "arm", not(target_vendor = "apple")) => 20,
38-
all(target_arch = "arm", target_vendor = "apple") => 5,
39-
all(target_arch = "aarch64", target_pointer_width = "64", not(target_os = "windows")) => 2,
40-
all(target_arch = "aarch64", target_pointer_width = "64", target_os = "windows") => 6,
41-
all(target_arch = "aarch64", target_pointer_width = "32") => 5,
42-
target_arch = "m68k" => 2,
43-
any(target_arch = "mips", target_arch = "mips32r6") => 2,
44-
target_arch = "csky" => 2,
45-
any(target_arch = "mips64", target_arch = "mips64r6") => 2,
46-
any(target_arch = "powerpc", target_arch = "powerpc64") => 2,
47-
target_arch = "s390x" => 2,
48-
any(target_arch = "sparc", target_arch = "sparc64") => 2,
49-
any(target_arch = "riscv64", target_arch = "riscv32") => 2,
50-
all(target_family = "wasm", target_os = "emscripten") => 20,
51-
all(target_arch = "wasm32", any(target_os = "linux", target_os = "wasi")) => 2,
52-
target_arch = "hexagon" => 5,
53-
any(target_arch = "loongarch32", target_arch = "loongarch64") => 2,
54-
};
55-
56-
#[repr(C)]
57-
pub struct _Unwind_Exception {
58-
pub exception_class: _Unwind_Exception_Class,
59-
pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
60-
pub private: [_Unwind_Word; unwinder_private_data_size],
61-
}
62-
63-
// Check the size of _Unwind_Exception against the source of thruth when using the unwinding crate.
64-
#[cfg(target_os = "xous")]
65-
const _: () = {
66-
assert!(size_of::<unwinding::abi::UnwindException>() == size_of::<_Unwind_Exception>());
67-
};
6814

6915
pub enum _Unwind_Context {}
7016

71-
pub type _Unwind_Exception_Cleanup_Fn =
72-
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
73-
7417
// FIXME: The `#[link]` attributes on `extern "C"` block marks those symbols declared in
7518
// the block are reexported in dylib build of std. This is needed when build rustc with
7619
// feature `llvm-libunwind`, as no other cdylib will provided those _Unwind_* symbols.
@@ -110,10 +53,10 @@ cfg_select! {
11053
pub type _Unwind_Action = c_int;
11154

11255
pub const _UA_SEARCH_PHASE: c_int = 1;
113-
pub const _UA_CLEANUP_PHASE: c_int = 2;
114-
pub const _UA_HANDLER_FRAME: c_int = 4;
56+
//pub const _UA_CLEANUP_PHASE: c_int = 2;
57+
//pub const _UA_HANDLER_FRAME: c_int = 4;
11558
pub const _UA_FORCE_UNWIND: c_int = 8;
116-
pub const _UA_END_OF_STACK: c_int = 16;
59+
//pub const _UA_END_OF_STACK: c_int = 16;
11760

11861
#[cfg_attr(
11962
all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
@@ -122,11 +65,9 @@ cfg_select! {
12265
unsafe extern "C" {
12366
pub fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word;
12467
pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word);
125-
pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> _Unwind_Word;
12668
pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word);
12769
pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int)
12870
-> _Unwind_Word;
129-
pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
13071
}
13172

13273
}
@@ -213,12 +154,6 @@ cfg_select! {
213154
(&raw mut value) as *mut c_void); }
214155
}
215156

216-
pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context)
217-
-> _Unwind_Word {
218-
let val = unsafe { _Unwind_GetGR(ctx, UNWIND_IP_REG) };
219-
val.map_addr(|v| v & !1)
220-
}
221-
222157
pub unsafe fn _Unwind_SetIP(ctx: *mut _Unwind_Context,
223158
value: _Unwind_Word) {
224159
// Propagate thumb bit to instruction pointer
@@ -232,45 +167,24 @@ cfg_select! {
232167
-> _Unwind_Word {
233168
unsafe {
234169
*ip_before_insn = 0;
235-
_Unwind_GetIP(ctx)
170+
let val = unsafe { _Unwind_GetGR(ctx, UNWIND_IP_REG) };
171+
val.map_addr(|v| v & !1)
236172
}
237173
}
238-
239-
// This function also doesn't exist on Android or ARM/Linux, so make it a no-op
240-
pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
241-
pc
242-
}
243174
}
244175
}
245176

246-
cfg_select! {
247-
all(target_vendor = "apple", not(target_os = "watchos"), target_arch = "arm") => {
248-
// 32-bit ARM Apple (except for watchOS armv7k specifically) uses SjLj and
249-
// does not provide _Unwind_Backtrace()
250-
unsafe extern "C-unwind" {
251-
pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
252-
}
253-
254-
pub use _Unwind_SjLj_RaiseException as _Unwind_RaiseException;
255-
}
256-
_ => {
257-
#[cfg_attr(
258-
all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
259-
link(name = "unwind", kind = "static", modifiers = "-bundle")
260-
)]
261-
unsafe extern "C-unwind" {
262-
pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
263-
}
264-
#[cfg_attr(
265-
all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
266-
link(name = "unwind", kind = "static", modifiers = "-bundle")
267-
)]
268-
unsafe extern "C" {
269-
pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
270-
trace_argument: *mut c_void)
271-
-> _Unwind_Reason_Code;
272-
}
273-
}
177+
#[cfg_attr(
178+
all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
179+
link(name = "unwind", kind = "static", modifiers = "-bundle")
180+
)]
181+
unsafe extern "C-unwind" {
182+
// 32-bit ARM Apple (except for watchOS armv7k specifically) uses SjLj
183+
#[cfg_attr(
184+
all(target_vendor = "apple", not(target_os = "watchos"), target_arch = "arm"),
185+
link_name = "_Unwind_SjLj_RaiseException"
186+
)]
187+
pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
274188
}
275189

276190
cfg_select! {

‎library/unwind/src/types.rs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#![allow(nonstandard_style)]
2+
3+
#[repr(C)]
4+
#[derive(Debug, Copy, Clone, PartialEq)]
5+
pub enum _Unwind_Reason_Code {
6+
_URC_NO_REASON = 0,
7+
_URC_FOREIGN_EXCEPTION_CAUGHT = 1,
8+
_URC_FATAL_PHASE2_ERROR = 2,
9+
_URC_FATAL_PHASE1_ERROR = 3,
10+
_URC_NORMAL_STOP = 4,
11+
_URC_END_OF_STACK = 5,
12+
_URC_HANDLER_FOUND = 6,
13+
_URC_INSTALL_CONTEXT = 7,
14+
_URC_CONTINUE_UNWIND = 8,
15+
_URC_FAILURE = 9, // used only by ARM EHABI
16+
}
17+
pub use _Unwind_Reason_Code::*;
18+
19+
pub type _Unwind_Exception_Class = u64;
20+
pub type _Unwind_Word = *const u8;
21+
22+
pub const unwinder_private_data_size: usize = cfg_select! {
23+
target_arch = "x86" => 5,
24+
all(target_arch = "x86_64", not(any(target_os = "windows", target_os = "cygwin"))) => 2,
25+
all(target_arch = "x86_64", any(target_os = "windows", target_os = "cygwin")) => 6,
26+
all(target_arch = "arm", not(target_vendor = "apple")) => 20,
27+
all(target_arch = "arm", target_vendor = "apple") => 5,
28+
all(target_arch = "aarch64", target_pointer_width = "64", not(target_os = "windows")) => 2,
29+
all(target_arch = "aarch64", target_pointer_width = "64", target_os = "windows") => 6,
30+
all(target_arch = "aarch64", target_pointer_width = "32") => 5,
31+
target_arch = "m68k" => 2,
32+
any(target_arch = "mips", target_arch = "mips32r6") => 2,
33+
target_arch = "csky" => 2,
34+
any(target_arch = "mips64", target_arch = "mips64r6") => 2,
35+
any(target_arch = "powerpc", target_arch = "powerpc64") => 2,
36+
target_arch = "s390x" => 2,
37+
any(target_arch = "sparc", target_arch = "sparc64") => 2,
38+
any(target_arch = "riscv64", target_arch = "riscv32") => 2,
39+
all(target_family = "wasm", target_os = "emscripten") => 20,
40+
target_family = "wasm" => 2,
41+
target_arch = "hexagon" => 5,
42+
any(target_arch = "loongarch32", target_arch = "loongarch64") => 2,
43+
};
44+
45+
#[repr(C)]
46+
pub struct _Unwind_Exception {
47+
pub exception_class: _Unwind_Exception_Class,
48+
pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
49+
pub private: [_Unwind_Word; unwinder_private_data_size],
50+
}
51+
52+
// Check the size of _Unwind_Exception against the source of truth when using the unwinding crate.
53+
#[cfg(target_os = "xous")]
54+
const _: () = {
55+
assert!(size_of::<unwinding::abi::UnwindException>() == size_of::<_Unwind_Exception>());
56+
};
57+
58+
pub type _Unwind_Exception_Cleanup_Fn =
59+
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;

‎library/unwind/src/wasm.rs‎

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,7 @@ core::arch::global_asm!(
2626
"__cpp_exception:",
2727
);
2828

29-
#[repr(C)]
30-
#[derive(Debug, Copy, Clone, PartialEq)]
31-
pub enum _Unwind_Reason_Code {
32-
_URC_NO_REASON = 0,
33-
_URC_FOREIGN_EXCEPTION_CAUGHT = 1,
34-
_URC_FATAL_PHASE2_ERROR = 2,
35-
_URC_FATAL_PHASE1_ERROR = 3,
36-
_URC_NORMAL_STOP = 4,
37-
_URC_END_OF_STACK = 5,
38-
_URC_HANDLER_FOUND = 6,
39-
_URC_INSTALL_CONTEXT = 7,
40-
_URC_CONTINUE_UNWIND = 8,
41-
_URC_FAILURE = 9, // used only by ARM EHABI
42-
}
43-
pub use _Unwind_Reason_Code::*;
44-
45-
pub type _Unwind_Exception_Class = u64;
46-
pub type _Unwind_Word = *const u8;
47-
48-
pub const unwinder_private_data_size: usize = 2;
49-
50-
#[repr(C)]
51-
pub struct _Unwind_Exception {
52-
pub exception_class: _Unwind_Exception_Class,
53-
pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
54-
pub private: [_Unwind_Word; unwinder_private_data_size],
55-
}
56-
57-
pub type _Unwind_Exception_Cleanup_Fn =
58-
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
29+
pub use crate::types::*;
5930

6031
pub unsafe fn _Unwind_DeleteException(exception: *mut _Unwind_Exception) {
6132
if let Some(exception_cleanup) = unsafe { (*exception).exception_cleanup } {

0 commit comments

Comments
 (0)