| 1 |
#![cfg(all(target_os = "windows", target_arch = "x86_64"))] |
| 2 |
|
| 3 |
use minidump::{ |
| 4 |
CrashReason, Minidump, MinidumpBreakpadInfo, MinidumpMemoryList, MinidumpSystemInfo, |
| 5 |
MinidumpThreadList, |
| 6 |
}; |
| 7 |
use minidump_writer::minidump_writer::MinidumpWriter; |
| 8 |
mod common; |
| 9 |
use common::start_child_and_return; |
| 10 |
|
| 11 |
const EXCEPTION_ILLEGAL_INSTRUCTION: i32 = -1073741795; |
| 12 |
const STATUS_INVALID_PARAMETER: i32 = -1073741811; |
| 13 |
#[link(name = "kernel32")] |
| 14 |
extern "system" { |
| 15 |
fn GetCurrentThreadId() -> u32; |
| 16 |
} |
| 17 |
|
| 18 |
fn get_crash_reason<'a, T: std::ops::Deref<Target = [u8]> + 'a>( |
| 19 |
md: &Minidump<'a, T>, |
| 20 |
) -> CrashReason { |
| 21 |
let exc: minidump::MinidumpException<'_> = |
| 22 |
md.get_stream().expect("unable to find exception stream"); |
| 23 |
|
| 24 |
exc.get_crash_reason( |
| 25 |
minidump::system_info::Os::Windows, |
| 26 |
minidump::system_info::Cpu::X86_64, |
| 27 |
) |
| 28 |
} |
| 29 |
|
| 30 |
/// Ensures that we can write minidumps for the current process, even if this is |
| 31 |
/// not necessarily the primary intended use case of out-of-process dumping |
| 32 |
#[test] |
| 33 |
fn dump_current_process() { |
| 34 |
let mut tmpfile = tempfile::Builder::new() |
| 35 |
.prefix("windows_current_process") |
| 36 |
.tempfile() |
| 37 |
.unwrap(); |
| 38 |
|
| 39 |
MinidumpWriter::dump_local_context( |
| 40 |
Some(STATUS_INVALID_PARAMETER), |
| 41 |
None, |
| 42 |
None, |
| 43 |
tmpfile.as_file_mut(), |
| 44 |
) |
| 45 |
.expect("failed to write minidump"); |
| 46 |
|
| 47 |
let md = Minidump::read_path(tmpfile.path()).expect("failed to read minidump"); |
| 48 |
|
| 49 |
let _: MinidumpThreadList = md.get_stream().expect("Couldn't find MinidumpThreadList"); |
| 50 |
let _: MinidumpMemoryList = md.get_stream().expect("Couldn't find MinidumpMemoryList"); |
| 51 |
let _: MinidumpSystemInfo = md.get_stream().expect("Couldn't find MinidumpSystemInfo"); |
| 52 |
|
| 53 |
let crash_reason = get_crash_reason(&md); |
| 54 |
|
| 55 |
assert_eq!( |
| 56 |
crash_reason, |
| 57 |
CrashReason::from_windows_error(STATUS_INVALID_PARAMETER as u32) |
| 58 |
); |
| 59 |
|
| 60 |
// SAFETY: syscall |
| 61 |
let thread_id = unsafe { GetCurrentThreadId() }; |
| 62 |
|
| 63 |
let bp_info: MinidumpBreakpadInfo = |
| 64 |
md.get_stream().expect("Couldn't find MinidumpBreakpadInfo"); |
| 65 |
|
| 66 |
assert_eq!(bp_info.dump_thread_id.unwrap(), thread_id); |
| 67 |
assert_eq!(bp_info.requesting_thread_id.unwrap(), thread_id); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn dump_specific_thread() { |
| 72 |
let mut tmpfile = tempfile::Builder::new() |
| 73 |
.prefix("windows_current_process") |
| 74 |
.tempfile() |
| 75 |
.unwrap(); |
| 76 |
|
| 77 |
let (tx, rx) = std::sync::mpsc::channel(); |
| 78 |
|
| 79 |
let jh = std::thread::spawn(move || { |
| 80 |
// SAFETY: syscall |
| 81 |
let thread_id = unsafe { GetCurrentThreadId() }; |
| 82 |
while tx.send(thread_id).is_ok() { |
| 83 |
std::thread::sleep(std::time::Duration::from_millis(10)); |
| 84 |
} |
| 85 |
}); |
| 86 |
|
| 87 |
let crashing_thread_id = rx.recv().unwrap(); |
| 88 |
|
| 89 |
MinidumpWriter::dump_local_context( |
| 90 |
Some(STATUS_INVALID_PARAMETER), |
| 91 |
Some(crashing_thread_id), |
| 92 |
None, |
| 93 |
tmpfile.as_file_mut(), |
| 94 |
) |
| 95 |
.expect("failed to write minidump"); |
| 96 |
|
| 97 |
drop(rx); |
| 98 |
jh.join().unwrap(); |
| 99 |
|
| 100 |
let md = Minidump::read_path(tmpfile.path()).expect("failed to read minidump"); |
| 101 |
|
| 102 |
let _: MinidumpThreadList = md.get_stream().expect("Couldn't find MinidumpThreadList"); |
| 103 |
let _: MinidumpMemoryList = md.get_stream().expect("Couldn't find MinidumpMemoryList"); |
| 104 |
let _: MinidumpSystemInfo = md.get_stream().expect("Couldn't find MinidumpSystemInfo"); |
| 105 |
|
| 106 |
let crash_reason = get_crash_reason(&md); |
| 107 |
|
| 108 |
assert_eq!( |
| 109 |
crash_reason, |
| 110 |
CrashReason::from_windows_error(STATUS_INVALID_PARAMETER as u32) |
| 111 |
); |
| 112 |
|
| 113 |
// SAFETY: syscall |
| 114 |
let requesting_thread_id = unsafe { GetCurrentThreadId() }; |
| 115 |
|
| 116 |
let bp_info: MinidumpBreakpadInfo = |
| 117 |
md.get_stream().expect("Couldn't find MinidumpBreakpadInfo"); |
| 118 |
|
| 119 |
assert_eq!(bp_info.dump_thread_id.unwrap(), crashing_thread_id); |
| 120 |
assert_eq!(bp_info.requesting_thread_id.unwrap(), requesting_thread_id); |
| 121 |
} |
| 122 |
|
| 123 |
/// Ensures that we can write minidumps for an external process. Unfortunately |
| 124 |
/// this requires us to know the actual pointer in the client process for the |
| 125 |
/// exception, as the `MiniDumpWriteDump` syscall directly reads points from |
| 126 |
/// the process memory, so we communicate that back from the client process |
| 127 |
/// via stdout |
| 128 |
#[test] |
| 129 |
fn dump_external_process() { |
| 130 |
use std::io::BufRead; |
| 131 |
|
| 132 |
let mut child = start_child_and_return(&[&format!("{:x}", EXCEPTION_ILLEGAL_INSTRUCTION)]); |
| 133 |
|
| 134 |
let (process_id, exception_pointers, thread_id, exception_code) = { |
| 135 |
let mut f = std::io::BufReader::new(child.stdout.as_mut().expect("Can't open stdout")); |
| 136 |
let mut buf = String::new(); |
| 137 |
f.read_line(&mut buf).expect("failed to read stdout"); |
| 138 |
assert!(!buf.is_empty()); |
| 139 |
|
| 140 |
let mut biter = buf.trim().split(' '); |
| 141 |
|
| 142 |
let process_id: u32 = biter.next().unwrap().parse().unwrap(); |
| 143 |
let exception_pointers: usize = biter.next().unwrap().parse().unwrap(); |
| 144 |
let thread_id: u32 = biter.next().unwrap().parse().unwrap(); |
| 145 |
let exception_code = u32::from_str_radix(biter.next().unwrap(), 16).unwrap(); |
| 146 |
|
| 147 |
(process_id, exception_pointers, thread_id, exception_code) |
| 148 |
}; |
| 149 |
|
| 150 |
let exception_code = exception_code as i32; |
| 151 |
assert_eq!(exception_code, EXCEPTION_ILLEGAL_INSTRUCTION); |
| 152 |
|
| 153 |
let crash_context = crash_context::CrashContext { |
| 154 |
exception_pointers: exception_pointers as _, |
| 155 |
process_id, |
| 156 |
thread_id, |
| 157 |
exception_code, |
| 158 |
}; |
| 159 |
|
| 160 |
let mut tmpfile = tempfile::Builder::new() |
| 161 |
.prefix("windows_external_process") |
| 162 |
.tempfile() |
| 163 |
.unwrap(); |
| 164 |
|
| 165 |
// SAFETY: We keep the process we are dumping alive until the minidump is written |
| 166 |
// and the test process keep the pointers it sent us alive until it is killed |
| 167 |
MinidumpWriter::dump_crash_context(crash_context, None, tmpfile.as_file_mut()) |
| 168 |
.expect("failed to write minidump"); |
| 169 |
|
| 170 |
child.kill().expect("failed to kill child"); |
| 171 |
|
| 172 |
let md = Minidump::read_path(tmpfile.path()).expect("failed to read minidump"); |
| 173 |
|
| 174 |
let _: MinidumpThreadList = md.get_stream().expect("Couldn't find MinidumpThreadList"); |
| 175 |
let _: MinidumpMemoryList = md.get_stream().expect("Couldn't find MinidumpMemoryList"); |
| 176 |
let _: MinidumpSystemInfo = md.get_stream().expect("Couldn't find MinidumpSystemInfo"); |
| 177 |
|
| 178 |
let crash_reason = get_crash_reason(&md); |
| 179 |
|
| 180 |
assert_eq!( |
| 181 |
crash_reason, |
| 182 |
CrashReason::from_windows_code(EXCEPTION_ILLEGAL_INSTRUCTION as u32) |
| 183 |
); |
| 184 |
} |