summaryrefslogtreecommitdiffstats
path: root/third_party/rust/minidump-writer/tests/windows_minidump_writer.rs
blob: 1b2c1d4bb955582bad83c180369c7dbd5ab059b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![cfg(all(target_os = "windows", target_arch = "x86_64"))]

use minidump::{
    CrashReason, Minidump, MinidumpBreakpadInfo, MinidumpMemoryList, MinidumpSystemInfo,
    MinidumpThreadList,
};
use minidump_writer::minidump_writer::MinidumpWriter;
mod common;
use common::start_child_and_return;

const EXCEPTION_ILLEGAL_INSTRUCTION: i32 = -1073741795;
const STATUS_INVALID_PARAMETER: i32 = -1073741811;
#[link(name = "kernel32")]
extern "system" {
    fn GetCurrentThreadId() -> u32;
}

fn get_crash_reason<'a, T: std::ops::Deref<Target = [u8]> + 'a>(
    md: &Minidump<'a, T>,
) -> CrashReason {
    let exc: minidump::MinidumpException<'_> =
        md.get_stream().expect("unable to find exception stream");

    exc.get_crash_reason(
        minidump::system_info::Os::Windows,
        minidump::system_info::Cpu::X86_64,
    )
}

/// Ensures that we can write minidumps for the current process, even if this is
/// not necessarily the primary intended use case of out-of-process dumping
#[test]
fn dump_current_process() {
    let mut tmpfile = tempfile::Builder::new()
        .prefix("windows_current_process")
        .tempfile()
        .unwrap();

    MinidumpWriter::dump_local_context(
        Some(STATUS_INVALID_PARAMETER),
        None,
        None,
        tmpfile.as_file_mut(),
    )
    .expect("failed to write minidump");

    let md = Minidump::read_path(tmpfile.path()).expect("failed to read minidump");

    let _: MinidumpThreadList = md.get_stream().expect("Couldn't find MinidumpThreadList");
    let _: MinidumpMemoryList = md.get_stream().expect("Couldn't find MinidumpMemoryList");
    let _: MinidumpSystemInfo = md.get_stream().expect("Couldn't find MinidumpSystemInfo");

    let crash_reason = get_crash_reason(&md);

    assert_eq!(
        crash_reason,
        CrashReason::from_windows_error(STATUS_INVALID_PARAMETER as u32)
    );

    // SAFETY: syscall
    let thread_id = unsafe { GetCurrentThreadId() };

    let bp_info: MinidumpBreakpadInfo =
        md.get_stream().expect("Couldn't find MinidumpBreakpadInfo");

    assert_eq!(bp_info.dump_thread_id.unwrap(), thread_id);
    assert_eq!(bp_info.requesting_thread_id.unwrap(), thread_id);
}

#[test]
fn dump_specific_thread() {
    let mut tmpfile = tempfile::Builder::new()
        .prefix("windows_current_process")
        .tempfile()
        .unwrap();

    let (tx, rx) = std::sync::mpsc::channel();

    let jh = std::thread::spawn(move || {
        // SAFETY: syscall
        let thread_id = unsafe { GetCurrentThreadId() };
        while tx.send(thread_id).is_ok() {
            std::thread::sleep(std::time::Duration::from_millis(10));
        }
    });

    let crashing_thread_id = rx.recv().unwrap();

    MinidumpWriter::dump_local_context(
        Some(STATUS_INVALID_PARAMETER),
        Some(crashing_thread_id),
        None,
        tmpfile.as_file_mut(),
    )
    .expect("failed to write minidump");

    drop(rx);
    jh.join().unwrap();

    let md = Minidump::read_path(tmpfile.path()).expect("failed to read minidump");

    let _: MinidumpThreadList = md.get_stream().expect("Couldn't find MinidumpThreadList");
    let _: MinidumpMemoryList = md.get_stream().expect("Couldn't find MinidumpMemoryList");
    let _: MinidumpSystemInfo = md.get_stream().expect("Couldn't find MinidumpSystemInfo");

    let crash_reason = get_crash_reason(&md);

    assert_eq!(
        crash_reason,
        CrashReason::from_windows_error(STATUS_INVALID_PARAMETER as u32)
    );

    // SAFETY: syscall
    let requesting_thread_id = unsafe { GetCurrentThreadId() };

    let bp_info: MinidumpBreakpadInfo =
        md.get_stream().expect("Couldn't find MinidumpBreakpadInfo");

    assert_eq!(bp_info.dump_thread_id.unwrap(), crashing_thread_id);
    assert_eq!(bp_info.requesting_thread_id.unwrap(), requesting_thread_id);
}

/// Ensures that we can write minidumps for an external process. Unfortunately
/// this requires us to know the actual pointer in the client process for the
/// exception, as the `MiniDumpWriteDump` syscall directly reads points from
/// the process memory, so we communicate that back from the client process
/// via stdout
#[test]
fn dump_external_process() {
    use std::io::BufRead;

    let mut child = start_child_and_return(&[&format!("{:x}", EXCEPTION_ILLEGAL_INSTRUCTION)]);

    let (process_id, exception_pointers, thread_id, exception_code) = {
        let mut f = std::io::BufReader::new(child.stdout.as_mut().expect("Can't open stdout"));
        let mut buf = String::new();
        f.read_line(&mut buf).expect("failed to read stdout");
        assert!(!buf.is_empty());

        let mut biter = buf.trim().split(' ');

        let process_id: u32 = biter.next().unwrap().parse().unwrap();
        let exception_pointers: usize = biter.next().unwrap().parse().unwrap();
        let thread_id: u32 = biter.next().unwrap().parse().unwrap();
        let exception_code = u32::from_str_radix(biter.next().unwrap(), 16).unwrap();

        (process_id, exception_pointers, thread_id, exception_code)
    };

    let exception_code = exception_code as i32;
    assert_eq!(exception_code, EXCEPTION_ILLEGAL_INSTRUCTION);

    let crash_context = crash_context::CrashContext {
        exception_pointers: exception_pointers as _,
        process_id,
        thread_id,
        exception_code,
    };

    let mut tmpfile = tempfile::Builder::new()
        .prefix("windows_external_process")
        .tempfile()
        .unwrap();

    // SAFETY: We keep the process we are dumping alive until the minidump is written
    // and the test process keep the pointers it sent us alive until it is killed
    MinidumpWriter::dump_crash_context(crash_context, None, tmpfile.as_file_mut())
        .expect("failed to write minidump");

    child.kill().expect("failed to kill child");

    let md = Minidump::read_path(tmpfile.path()).expect("failed to read minidump");

    let _: MinidumpThreadList = md.get_stream().expect("Couldn't find MinidumpThreadList");
    let _: MinidumpMemoryList = md.get_stream().expect("Couldn't find MinidumpMemoryList");
    let _: MinidumpSystemInfo = md.get_stream().expect("Couldn't find MinidumpSystemInfo");

    let crash_reason = get_crash_reason(&md);

    assert_eq!(
        crash_reason,
        CrashReason::from_windows_code(EXCEPTION_ILLEGAL_INSTRUCTION as u32)
    );
}