| 1 |
use super::*; |
| 2 |
|
| 3 |
use mach2::exception_types as et; |
| 4 |
|
| 5 |
impl MinidumpWriter { |
| 6 |
/// Writes the [`minidump_common::format::MINIDUMP_EXCEPTION_STREAM`] stream. |
| 7 |
/// |
| 8 |
/// This stream is optional on MacOS as a user requested minidump could |
| 9 |
/// choose not to specify the exception information. |
| 10 |
pub(crate) fn write_exception( |
| 11 |
&mut self, |
| 12 |
buffer: &mut DumpBuf, |
| 13 |
dumper: &TaskDumper, |
| 14 |
) -> Result<MDRawDirectory, WriterError> { |
| 15 |
// This shouldn't fail since we won't be writing this stream if the crash context is |
| 16 |
// not present |
| 17 |
let crash_context = self |
| 18 |
.crash_context |
| 19 |
.as_ref() |
| 20 |
.ok_or(WriterError::NoCrashContext)?; |
| 21 |
|
| 22 |
let thread_state = dumper.read_thread_state(crash_context.thread).ok(); |
| 23 |
|
| 24 |
let thread_context = if let Some(ts) = &thread_state { |
| 25 |
let mut cpu = Default::default(); |
| 26 |
Self::fill_cpu_context(ts, &mut cpu); |
| 27 |
MemoryWriter::alloc_with_val(buffer, cpu) |
| 28 |
.map(|mw| mw.location()) |
| 29 |
.ok() |
| 30 |
} else { |
| 31 |
None |
| 32 |
}; |
| 33 |
|
| 34 |
let exception_record = crash_context |
| 35 |
.exception |
| 36 |
.as_ref() |
| 37 |
.map(|exc| { |
| 38 |
let code = exc.code as u64; |
| 39 |
|
| 40 |
// `EXC_CRASH` exceptions wrap other exceptions, so we want to |
| 41 |
// retrieve the _actual_ exception |
| 42 |
let wrapped_exc = if exc.kind as u32 == et::EXC_CRASH { |
| 43 |
recover_exc_crash_wrapped_exception(code) |
| 44 |
} else { |
| 45 |
None |
| 46 |
}; |
| 47 |
|
| 48 |
// For EXC_RESOURCE and EXC_GUARD crashes Crashpad records the |
| 49 |
// uppermost 32 bits of the exception code in the exception flags, |
| 50 |
// as they are the most interesting for those exceptions. Neither |
| 51 |
// of these exceptions can be wrapped by an `EXC_CRASH` |
| 52 |
// |
| 53 |
// EXC_GUARD |
| 54 |
// code: |
| 55 |
// +-------------------+----------------+--------------+ |
| 56 |
// |[63:61] guard type | [60:32] flavor | [31:0] target| |
| 57 |
// +-------------------+----------------+--------------+ |
| 58 |
// |
| 59 |
// EXC_RESOURCE |
| 60 |
// code: |
| 61 |
// +--------------------------------------------------------+ |
| 62 |
// |[63:61] resource type | [60:58] flavor | [57:32] unused | |
| 63 |
// +--------------------------------------------------------+ |
| 64 |
let exception_code = |
| 65 |
if exc.kind as u32 == et::EXC_RESOURCE || exc.kind as u32 == et::EXC_GUARD { |
| 66 |
(code >> 32) as u32 |
| 67 |
} else if let Some(wrapped) = wrapped_exc { |
| 68 |
wrapped.code |
| 69 |
} else { |
| 70 |
// For all other exceptions types, the value in the code |
| 71 |
// _should_ never exceed 32 bits, crashpad does an actual |
| 72 |
// range check here. |
| 73 |
if code > u32::MAX.into() { |
| 74 |
// TODO: do something more than logging? |
| 75 |
log::warn!("exception code {code:#018x} exceeds the expected 32 bits"); |
| 76 |
} |
| 77 |
code as u32 |
| 78 |
}; |
| 79 |
|
| 80 |
let exception_kind = if let Some(wrapped) = wrapped_exc { |
| 81 |
wrapped.kind |
| 82 |
} else { |
| 83 |
exc.kind |
| 84 |
}; |
| 85 |
|
| 86 |
let exception_address = |
| 87 |
if exception_kind == et::EXC_BAD_ACCESS && exc.subcode.is_some() { |
| 88 |
exc.subcode.unwrap_or_default() |
| 89 |
} else if let Some(ts) = thread_state { |
| 90 |
ts.pc() |
| 91 |
} else { |
| 92 |
0 |
| 93 |
}; |
| 94 |
|
| 95 |
// The naming is confusing here, but it is how it is |
| 96 |
let mut md_exc = MDException { |
| 97 |
exception_code: exception_kind, |
| 98 |
exception_flags: exception_code, |
| 99 |
exception_address, |
| 100 |
..Default::default() |
| 101 |
}; |
| 102 |
|
| 103 |
// Now append the (mostly) original information to the "ancillary" |
| 104 |
// exception_information at the end. This allows a minidump parser |
| 105 |
// to recover the full exception information for the crash, rather |
| 106 |
// than only using the (potentially) truncated information we |
| 107 |
// just set in `exception_code` and `exception_flags` |
| 108 |
md_exc.exception_information[0] = exception_kind as u64; |
| 109 |
md_exc.exception_information[1] = code; |
| 110 |
|
| 111 |
md_exc.number_parameters = if let Some(subcode) = exc.subcode { |
| 112 |
md_exc.exception_information[2] = subcode; |
| 113 |
3 |
| 114 |
} else { |
| 115 |
2 |
| 116 |
}; |
| 117 |
|
| 118 |
md_exc |
| 119 |
}) |
| 120 |
.unwrap_or_default(); |
| 121 |
|
| 122 |
let stream = MDRawExceptionStream { |
| 123 |
thread_id: crash_context.thread, |
| 124 |
exception_record, |
| 125 |
thread_context: thread_context.unwrap_or_default(), |
| 126 |
__align: 0, |
| 127 |
}; |
| 128 |
|
| 129 |
let exc_section = MemoryWriter::<MDRawExceptionStream>::alloc_with_val(buffer, stream)?; |
| 130 |
|
| 131 |
Ok(MDRawDirectory { |
| 132 |
stream_type: MDStreamType::ExceptionStream as u32, |
| 133 |
location: exc_section.location(), |
| 134 |
}) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/// [`et::EXC_CRASH`] is a wrapper exception around another exception, but not |
| 139 |
/// all exceptions can be wrapped by it, so this function validates that the |
| 140 |
/// `EXC_CRASH` is actually valid |
| 141 |
#[inline] |
| 142 |
fn is_valid_exc_crash(exc_code: u64) -> bool { |
| 143 |
let wrapped = ((exc_code >> 20) & 0xf) as u32; |
| 144 |
|
| 145 |
!( |
| 146 |
wrapped == et::EXC_CRASH // EXC_CRASH can't wrap another one |
| 147 |
|| wrapped == et::EXC_RESOURCE // EXC_RESOURCE would lose information |
| 148 |
|| wrapped == et::EXC_GUARD // EXC_GUARD would lose information |
| 149 |
|| wrapped == et::EXC_CORPSE_NOTIFY |
| 150 |
// cannot be wrapped |
| 151 |
) |
| 152 |
} |
| 153 |
|
| 154 |
/// The details for an exception wrapped by an `EXC_CRASH` |
| 155 |
#[derive(Copy, Clone)] |
| 156 |
struct WrappedException { |
| 157 |
/// The `EXC_*` that was wrapped |
| 158 |
kind: u32, |
| 159 |
/// The code of the wrapped exception, for all exceptions other than |
| 160 |
/// `EXC_RESOURCE` and `EXC_GUARD` this _should_ never exceed 32 bits, and |
| 161 |
/// is one of the reasons that `EXC_CRASH` cannot wrap those 2 exceptions |
| 162 |
code: u32, |
| 163 |
/// The Unix signal number that the original exception was converted into |
| 164 |
_signal: u8, |
| 165 |
} |
| 166 |
|
| 167 |
/// Unwraps an `EXC_CRASH` exception code to the inner exception it wraps. |
| 168 |
/// |
| 169 |
/// Will return `None` if the specified code is wrapping an exception that |
| 170 |
/// should not be possible to be wrapped in an `EXC_CRASH` |
| 171 |
#[inline] |
| 172 |
fn recover_exc_crash_wrapped_exception(code: u64) -> Option<WrappedException> { |
| 173 |
is_valid_exc_crash(code).then(|| WrappedException { |
| 174 |
kind: ((code >> 20) & 0xf) as u32, |
| 175 |
code: (code & 0xfffff) as u32, |
| 176 |
_signal: ((code >> 24) & 0xff) as u8, |
| 177 |
}) |
| 178 |
} |