Other/User defined WIndows Exceptions (#402)

* other exceptions

* add

* 46th

* fix

* fmt
This commit is contained in:
Dongjia Zhang 2021-12-22 03:18:58 +09:00 committed by GitHub
parent b0019ae4a9
commit d697554810
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

View File

@ -18,7 +18,7 @@ use core::{
sync::atomic::{compiler_fence, Ordering}, sync::atomic::{compiler_fence, Ordering},
}; };
use num_enum::{IntoPrimitive, TryFromPrimitive}; use num_enum::TryFromPrimitive;
//const EXCEPTION_CONTINUE_EXECUTION: c_long = -1; //const EXCEPTION_CONTINUE_EXECUTION: c_long = -1;
//const EXCEPTION_CONTINUE_SEARCH: c_long = 0; //const EXCEPTION_CONTINUE_SEARCH: c_long = 0;
@ -83,7 +83,7 @@ pub const STATUS_ASSERTION_FAILURE: u32 = 0xC0000420;
pub const STATUS_SXS_EARLY_DEACTIVATION: u32 = 0xC015000F; pub const STATUS_SXS_EARLY_DEACTIVATION: u32 = 0xC015000F;
pub const STATUS_SXS_INVALID_DEACTIVATION: u32 = 0xC0150010; pub const STATUS_SXS_INVALID_DEACTIVATION: u32 = 0xC0150010;
#[derive(IntoPrimitive, TryFromPrimitive, Clone, Copy)] #[derive(TryFromPrimitive, Clone, Copy)]
#[repr(u32)] #[repr(u32)]
pub enum ExceptionCode { pub enum ExceptionCode {
// From https://docs.microsoft.com/en-us/windows/win32/debug/getexceptioncode // From https://docs.microsoft.com/en-us/windows/win32/debug/getexceptioncode
@ -133,6 +133,8 @@ pub enum ExceptionCode {
AssertionFailure = STATUS_ASSERTION_FAILURE, AssertionFailure = STATUS_ASSERTION_FAILURE,
SXSEarlyDeactivation = STATUS_SXS_EARLY_DEACTIVATION, SXSEarlyDeactivation = STATUS_SXS_EARLY_DEACTIVATION,
SXSInvalidDeactivation = STATUS_SXS_INVALID_DEACTIVATION, SXSInvalidDeactivation = STATUS_SXS_INVALID_DEACTIVATION,
#[num_enum(default)]
Other,
} }
pub static CRASH_EXCEPTIONS: &[ExceptionCode] = &[ pub static CRASH_EXCEPTIONS: &[ExceptionCode] = &[
@ -150,6 +152,7 @@ pub static CRASH_EXCEPTIONS: &[ExceptionCode] = &[
ExceptionCode::HeapCorruption, ExceptionCode::HeapCorruption,
ExceptionCode::StackBufferOverrun, ExceptionCode::StackBufferOverrun,
ExceptionCode::AssertionFailure, ExceptionCode::AssertionFailure,
ExceptionCode::Other,
]; ];
impl PartialEq for ExceptionCode { impl PartialEq for ExceptionCode {
@ -212,13 +215,14 @@ impl Display for ExceptionCode {
ExceptionCode::AssertionFailure => write!(f, "STATUS_ASSERTION_FAILURE")?, ExceptionCode::AssertionFailure => write!(f, "STATUS_ASSERTION_FAILURE")?,
ExceptionCode::SXSEarlyDeactivation => write!(f, "STATUS_SXS_EARLY_DEACTIVATION")?, ExceptionCode::SXSEarlyDeactivation => write!(f, "STATUS_SXS_EARLY_DEACTIVATION")?,
ExceptionCode::SXSInvalidDeactivation => write!(f, "STATUS_SXS_INVALID_DEACTIVATION")?, ExceptionCode::SXSInvalidDeactivation => write!(f, "STATUS_SXS_INVALID_DEACTIVATION")?,
ExceptionCode::Other => write!(f, "Other/User defined exception")?,
}; };
Ok(()) Ok(())
} }
} }
pub static EXCEPTION_CODES_MAPPING: [ExceptionCode; 45] = [ pub static EXCEPTION_CODES_MAPPING: [ExceptionCode; 46] = [
ExceptionCode::AccessViolation, ExceptionCode::AccessViolation,
ExceptionCode::ArrayBoundsExceeded, ExceptionCode::ArrayBoundsExceeded,
ExceptionCode::Breakpoint, ExceptionCode::Breakpoint,
@ -264,6 +268,7 @@ pub static EXCEPTION_CODES_MAPPING: [ExceptionCode; 45] = [
ExceptionCode::AssertionFailure, ExceptionCode::AssertionFailure,
ExceptionCode::SXSEarlyDeactivation, ExceptionCode::SXSEarlyDeactivation,
ExceptionCode::SXSInvalidDeactivation, ExceptionCode::SXSInvalidDeactivation,
ExceptionCode::Other,
]; ];
pub trait Handler { pub trait Handler {

View File

@ -686,8 +686,7 @@ mod windows_exception_handler {
use core::sync::atomic::{compiler_fence, Ordering}; use core::sync::atomic::{compiler_fence, Ordering};
use windows::Win32::System::Threading::ExitProcess; use windows::Win32::System::Threading::ExitProcess;
pub type HandlerFuncPtr = pub type HandlerFuncPtr = unsafe fn(*mut EXCEPTION_POINTERS, &mut InProcessExecutorHandlerData);
unsafe fn(ExceptionCode, *mut EXCEPTION_POINTERS, &mut InProcessExecutorHandlerData);
/*pub unsafe fn nop_handler( /*pub unsafe fn nop_handler(
_code: ExceptionCode, _code: ExceptionCode,
@ -703,7 +702,7 @@ mod windows_exception_handler {
let data = &mut GLOBAL_STATE; let data = &mut GLOBAL_STATE;
if !data.crash_handler.is_null() { if !data.crash_handler.is_null() {
let func: HandlerFuncPtr = transmute(data.crash_handler); let func: HandlerFuncPtr = transmute(data.crash_handler);
(func)(code, exception_pointers, data); (func)(exception_pointers, data);
} }
} }
} }
@ -817,7 +816,6 @@ mod windows_exception_handler {
} }
pub unsafe fn inproc_crash_handler<E, EM, I, OC, OF, OT, S, Z>( pub unsafe fn inproc_crash_handler<E, EM, I, OC, OF, OT, S, Z>(
code: ExceptionCode,
exception_pointers: *mut EXCEPTION_POINTERS, exception_pointers: *mut EXCEPTION_POINTERS,
data: &mut InProcessExecutorHandlerData, data: &mut InProcessExecutorHandlerData,
) where ) where
@ -851,6 +849,18 @@ mod windows_exception_handler {
data.tp_timer = ptr::null_mut(); data.tp_timer = ptr::null_mut();
} }
let code = ExceptionCode::try_from(
exception_pointers
.as_mut()
.unwrap()
.ExceptionRecord
.as_mut()
.unwrap()
.ExceptionCode
.0,
)
.unwrap();
#[cfg(feature = "std")] #[cfg(feature = "std")]
eprintln!("Crashed with {}", code); eprintln!("Crashed with {}", code);
if data.current_input_ptr.is_null() { if data.current_input_ptr.is_null() {