| 1 |
//! Contains types and helpers for dealing with `EXC_GUARD` exceptions. |
| 2 |
//! |
| 3 |
//! `EXC_GUARD` exceptions embed details about the guarded resource in the `code` |
| 4 |
//! and `subcode` fields of the exception |
| 5 |
//! |
| 6 |
//! See <https://github.com/apple-oss-distributions/xnu/blob/e7776783b89a353188416a9a346c6cdb4928faad/osfmk/kern/exc_guard.h> |
| 7 |
//! for the top level types that this module wraps. |
| 8 |
|
| 9 |
use mach2::exception_types::EXC_GUARD; |
| 10 |
|
| 11 |
/// The set of possible guard kinds |
| 12 |
#[derive(Copy, Clone, Debug)] |
| 13 |
#[repr(u8)] |
| 14 |
pub enum GuardKind { |
| 15 |
/// Null variant |
| 16 |
None = 0, |
| 17 |
/// A `mach_port_t` |
| 18 |
MachPort = 1, |
| 19 |
/// File descriptor |
| 20 |
Fd = 2, |
| 21 |
/// Userland assertion |
| 22 |
User = 3, |
| 23 |
/// Vnode |
| 24 |
Vnode = 4, |
| 25 |
/// Virtual memory operation |
| 26 |
VirtualMemory = 5, |
| 27 |
/// Rejected system call trap |
| 28 |
RejectedSyscall = 6, |
| 29 |
} |
| 30 |
|
| 31 |
#[inline] |
| 32 |
pub fn extract_guard_kind(code: u64) -> u8 { |
| 33 |
((code >> 61) & 0x7) as u8 |
| 34 |
} |
| 35 |
|
| 36 |
#[inline] |
| 37 |
pub fn extract_guard_flavor(code: u64) -> u32 { |
| 38 |
((code >> 32) & 0x1fffffff) as u32 |
| 39 |
} |
| 40 |
|
| 41 |
#[inline] |
| 42 |
pub fn extract_guard_target(code: u64) -> u32 { |
| 43 |
code as u32 |
| 44 |
} |
| 45 |
|
| 46 |
/// The extracted details of an `EXC_GUARD` exception |
| 47 |
pub struct GuardException { |
| 48 |
/// One of [`GuardKind`] |
| 49 |
pub kind: u8, |
| 50 |
/// The specific guard flavor that was violated, specific to each `kind` |
| 51 |
pub flavor: u32, |
| 52 |
/// The resource that was guarded |
| 53 |
pub target: u32, |
| 54 |
/// Target specific guard information |
| 55 |
pub identifier: u64, |
| 56 |
} |
| 57 |
|
| 58 |
/// Extracts the guard details from an exceptions code and subcode |
| 59 |
/// |
| 60 |
/// code: |
| 61 |
/// +-------------------+----------------+--------------+ |
| 62 |
/// |[63:61] guard type | [60:32] flavor | [31:0] target| |
| 63 |
/// +-------------------+----------------+--------------+ |
| 64 |
/// |
| 65 |
/// subcode: |
| 66 |
/// +---------------------------------------------------+ |
| 67 |
/// |[63:0] guard identifier | |
| 68 |
/// +---------------------------------------------------+ |
| 69 |
#[inline] |
| 70 |
pub fn extract_guard_exception(code: u64, subcode: u64) -> GuardException { |
| 71 |
GuardException { |
| 72 |
kind: extract_guard_kind(code), |
| 73 |
flavor: extract_guard_flavor(code), |
| 74 |
target: extract_guard_target(code), |
| 75 |
identifier: subcode, |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl super::ExceptionInfo { |
| 80 |
/// If this is an `EXC_GUARD` exception, retrieves the exception metadata |
| 81 |
/// from the code, otherwise returns `None` |
| 82 |
pub fn guard_exception(&self) -> Option<GuardException> { |
| 83 |
if self.kind != EXC_GUARD { |
| 84 |
return None; |
| 85 |
} |
| 86 |
|
| 87 |
Some(extract_guard_exception( |
| 88 |
self.code, |
| 89 |
self.subcode.unwrap_or_default(), |
| 90 |
)) |
| 91 |
} |
| 92 |
} |