Update exception struct to match current version of rustc

This commit is contained in:
Gary Guo 2023-10-28 00:24:56 +01:00
parent 00a7465328
commit ad9086f9bb

View File

@ -7,6 +7,8 @@ use crate::abi::*;
pub use crate::panic_handler::*; pub use crate::panic_handler::*;
use crate::panicking::Exception; use crate::panicking::Exception;
static CANARY: u8 = 0;
#[repr(transparent)] #[repr(transparent)]
struct RustPanic(Box<dyn Any + Send>, DropGuard); struct RustPanic(Box<dyn Any + Send>, DropGuard);
@ -25,6 +27,8 @@ impl Drop for DropGuard {
#[repr(C)] #[repr(C)]
struct ExceptionWithPayload { struct ExceptionWithPayload {
exception: MaybeUninit<UnwindException>, exception: MaybeUninit<UnwindException>,
// See rust/library/panic_unwind/src/gcc.rs for the canary values
canary: *const u8,
payload: RustPanic, payload: RustPanic,
} }
@ -34,12 +38,23 @@ unsafe impl Exception for RustPanic {
fn wrap(this: Self) -> *mut UnwindException { fn wrap(this: Self) -> *mut UnwindException {
Box::into_raw(Box::new(ExceptionWithPayload { Box::into_raw(Box::new(ExceptionWithPayload {
exception: MaybeUninit::uninit(), exception: MaybeUninit::uninit(),
canary: &CANARY,
payload: this, payload: this,
})) as *mut UnwindException })) as *mut UnwindException
} }
unsafe fn unwrap(ex: *mut UnwindException) -> Self { unsafe fn unwrap(ex: *mut UnwindException) -> Self {
let ex = unsafe { Box::from_raw(ex as *mut ExceptionWithPayload) }; let ex = ex as *mut ExceptionWithPayload;
let canary = unsafe { core::ptr::addr_of!((*ex).canary).read() };
if !core::ptr::eq(canary, &CANARY) {
// This is a Rust exception but not generated by us.
#[cfg(feature = "panic-handler")]
{
foreign_exception();
}
crate::util::abort();
}
let ex = unsafe { Box::from_raw(ex) };
ex.payload ex.payload
} }
} }