From d697554810b13e9634ca5dd9bd1b36f443c85409 Mon Sep 17 00:00:00 2001 From: Dongjia Zhang Date: Wed, 22 Dec 2021 03:18:58 +0900 Subject: [PATCH] Other/User defined WIndows Exceptions (#402) * other exceptions * add * 46th * fix * fmt --- libafl/src/bolts/os/windows_exceptions.rs | 11 ++++++++--- libafl/src/executors/inprocess.rs | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/libafl/src/bolts/os/windows_exceptions.rs b/libafl/src/bolts/os/windows_exceptions.rs index 2f6012a1a8..2d2ac7abb5 100644 --- a/libafl/src/bolts/os/windows_exceptions.rs +++ b/libafl/src/bolts/os/windows_exceptions.rs @@ -18,7 +18,7 @@ use core::{ sync::atomic::{compiler_fence, Ordering}, }; -use num_enum::{IntoPrimitive, TryFromPrimitive}; +use num_enum::TryFromPrimitive; //const EXCEPTION_CONTINUE_EXECUTION: c_long = -1; //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_INVALID_DEACTIVATION: u32 = 0xC0150010; -#[derive(IntoPrimitive, TryFromPrimitive, Clone, Copy)] +#[derive(TryFromPrimitive, Clone, Copy)] #[repr(u32)] pub enum ExceptionCode { // From https://docs.microsoft.com/en-us/windows/win32/debug/getexceptioncode @@ -133,6 +133,8 @@ pub enum ExceptionCode { AssertionFailure = STATUS_ASSERTION_FAILURE, SXSEarlyDeactivation = STATUS_SXS_EARLY_DEACTIVATION, SXSInvalidDeactivation = STATUS_SXS_INVALID_DEACTIVATION, + #[num_enum(default)] + Other, } pub static CRASH_EXCEPTIONS: &[ExceptionCode] = &[ @@ -150,6 +152,7 @@ pub static CRASH_EXCEPTIONS: &[ExceptionCode] = &[ ExceptionCode::HeapCorruption, ExceptionCode::StackBufferOverrun, ExceptionCode::AssertionFailure, + ExceptionCode::Other, ]; impl PartialEq for ExceptionCode { @@ -212,13 +215,14 @@ impl Display for ExceptionCode { ExceptionCode::AssertionFailure => write!(f, "STATUS_ASSERTION_FAILURE")?, ExceptionCode::SXSEarlyDeactivation => write!(f, "STATUS_SXS_EARLY_DEACTIVATION")?, ExceptionCode::SXSInvalidDeactivation => write!(f, "STATUS_SXS_INVALID_DEACTIVATION")?, + ExceptionCode::Other => write!(f, "Other/User defined exception")?, }; Ok(()) } } -pub static EXCEPTION_CODES_MAPPING: [ExceptionCode; 45] = [ +pub static EXCEPTION_CODES_MAPPING: [ExceptionCode; 46] = [ ExceptionCode::AccessViolation, ExceptionCode::ArrayBoundsExceeded, ExceptionCode::Breakpoint, @@ -264,6 +268,7 @@ pub static EXCEPTION_CODES_MAPPING: [ExceptionCode; 45] = [ ExceptionCode::AssertionFailure, ExceptionCode::SXSEarlyDeactivation, ExceptionCode::SXSInvalidDeactivation, + ExceptionCode::Other, ]; pub trait Handler { diff --git a/libafl/src/executors/inprocess.rs b/libafl/src/executors/inprocess.rs index 21d1dff421..136f85d56f 100644 --- a/libafl/src/executors/inprocess.rs +++ b/libafl/src/executors/inprocess.rs @@ -686,8 +686,7 @@ mod windows_exception_handler { use core::sync::atomic::{compiler_fence, Ordering}; use windows::Win32::System::Threading::ExitProcess; - pub type HandlerFuncPtr = - unsafe fn(ExceptionCode, *mut EXCEPTION_POINTERS, &mut InProcessExecutorHandlerData); + pub type HandlerFuncPtr = unsafe fn(*mut EXCEPTION_POINTERS, &mut InProcessExecutorHandlerData); /*pub unsafe fn nop_handler( _code: ExceptionCode, @@ -703,7 +702,7 @@ mod windows_exception_handler { let data = &mut GLOBAL_STATE; if !data.crash_handler.is_null() { 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( - code: ExceptionCode, exception_pointers: *mut EXCEPTION_POINTERS, data: &mut InProcessExecutorHandlerData, ) where @@ -851,6 +849,18 @@ mod windows_exception_handler { 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")] eprintln!("Crashed with {}", code); if data.current_input_ptr.is_null() {