Debian Codegraph @

third_party/rust/minidump-writer/tests/mac_minidump_writer.rs

raw
1 #![cfg(target_os = "macos")]
2
3 mod common;
4 use common::start_child_and_return;
5
6 use minidump::{
7 CrashReason, Minidump, MinidumpBreakpadInfo, MinidumpMemoryList, MinidumpMiscInfo,
8 MinidumpModuleList, MinidumpSystemInfo, MinidumpThreadList,
9 };
10 use minidump_writer::minidump_writer::MinidumpWriter;
11
12 fn get_crash_reason<'a, T: std::ops::Deref<Target = [u8]> + 'a>(
13 md: &Minidump<'a, T>,
14 ) -> CrashReason {
15 let exc: minidump::MinidumpException<'_> =
16 md.get_stream().expect("unable to find exception stream");
17
18 exc.get_crash_reason(
19 minidump::system_info::Os::MacOs,
20 if cfg!(target_arch = "x86_64") {
21 minidump::system_info::Cpu::X86_64
22 } else if cfg!(target_arch = "aarch64") {
23 minidump::system_info::Cpu::Arm64
24 } else {
25 unimplemented!()
26 },
27 )
28 }
29
30 struct Captured<'md> {
31 #[allow(dead_code)]
32 task: u32,
33 thread: u32,
34 minidump: Minidump<'md, memmap2::Mmap>,
35 }
36
37 fn capture_minidump(name: &str, exception_kind: u32) -> Captured<'_> {
38 // Create a mach port server to retrieve the crash details from the child
39 let mut server = crash_context::ipc::Server::create(&std::ffi::CString::new(name).unwrap())
40 .expect("failed to create mach port service");
41
42 let mut child = start_child_and_return(&[name, &exception_kind.to_string()]);
43
44 // Wait for the child to spinup and report a crash context to us
45 let mut rcc = server
46 .try_recv_crash_context(Some(std::time::Duration::from_secs(5)))
47 .expect("failed to receive context")
48 .expect("receive timed out");
49
50 let mut tmpfile = tempfile::Builder::new().prefix(name).tempfile().unwrap();
51
52 let task = rcc.crash_context.task;
53 let thread = rcc.crash_context.thread;
54
55 let mut dumper = MinidumpWriter::with_crash_context(rcc.crash_context);
56
57 dumper
58 .dump(tmpfile.as_file_mut())
59 .expect("failed to write minidump");
60
61 // Signal the child that we've received and processed the crash context
62 rcc.acker
63 .send_ack(1, Some(std::time::Duration::from_secs(2)))
64 .expect("failed to send ack");
65
66 child.kill().expect("failed to kill child");
67
68 let minidump = Minidump::read_path(tmpfile.path()).expect("failed to read minidump");
69
70 Captured {
71 task,
72 thread,
73 minidump,
74 }
75 }
76
77 #[test]
78 fn dump_external_process() {
79 if std::env::var("CI").is_ok() {
80 println!("test disabled, consistently times out because of potato runners");
81 return;
82 }
83
84 let approximate_proc_start_time = std::time::SystemTime::now()
85 .duration_since(std::time::SystemTime::UNIX_EPOCH)
86 .unwrap()
87 .as_secs();
88
89 let md = capture_minidump(
90 "dump_external_process",
91 mach2::exception_types::EXC_BREAKPOINT,
92 )
93 .minidump;
94
95 let crash_reason = get_crash_reason(&md);
96
97 assert!(matches!(
98 crash_reason,
99 CrashReason::MacGeneral(minidump_common::errors::ExceptionCodeMac::EXC_BREAKPOINT, _)
100 ));
101
102 let _: MinidumpModuleList = md.get_stream().expect("Couldn't find MinidumpModuleList");
103 let _: MinidumpThreadList = md.get_stream().expect("Couldn't find MinidumpThreadList");
104 let _: MinidumpMemoryList = md.get_stream().expect("Couldn't find MinidumpMemoryList");
105 let _: MinidumpSystemInfo = md.get_stream().expect("Couldn't find MinidumpSystemInfo");
106 let _: MinidumpBreakpadInfo = md.get_stream().expect("Couldn't find MinidumpBreakpadInfo");
107
108 let misc_info: MinidumpMiscInfo = md.get_stream().expect("Couldn't find MinidumpMiscInfo");
109
110 if let minidump::RawMiscInfo::MiscInfo2(mi) = &misc_info.raw {
111 // Unfortunately the minidump format only has 32-bit precision for the
112 // process start time
113 let process_create_time = mi.process_create_time as u64;
114
115 assert!(
116 process_create_time >= approximate_proc_start_time
117 && process_create_time <= approximate_proc_start_time + 2
118 );
119
120 // I've tried busy looping to spend CPU time to get this up, but
121 // MACH_TASK_BASIC_INFO which should give terminated thread times only ever
122 // reports 0, and TASK_THREAD_TIMES_INFO which should show active thread
123 // times I've only been able to get upt to a few thousand microseconds
124 // even when busy looping for well over a second, and those get truncated
125 // to whole seconds. And it seems that crashpad doesn't have tests around
126 // this, though that's hard to say given how tedious it is finding stuff
127 // in that bloated codebase
128 // assert!(mi.process_user_time > 0);
129 // assert!(mi.process_kernel_time > 0);
130
131 // These aren't currently available on aarch64, or if they are, they
132 // are not via the same sysctlbyname mechanism. Would be nice if Apple
133 // documented...anything
134 if cfg!(target_arch = "x86_64") {
135 assert!(mi.processor_max_mhz > 0);
136 assert!(mi.processor_current_mhz > 0);
137 }
138 } else {
139 panic!("unexpected misc info type {:?}", misc_info);
140 }
141 }
142
143 // /// Validates we can actually walk the stack for each thread in the minidump,
144 // /// this is using minidump-processor, which (currently) depends on breakpad
145 // /// symbols, however https://github.com/mozilla/dump_syms is not available as
146 // /// a library https://github.com/mozilla/dump_syms/issues/253, so we just require
147 // /// that it already be installed, hence the ignore
148 // #[test]
149 // fn stackwalks() {
150 // if std::env::var("CI").is_ok() {
151 // println!("test disabled, consistently times out because of potato runners");
152 // return;
153 // }
154
155 // println!("generating minidump...");
156 // let md = capture_minidump("stackwalks", mach2::exception_types::EXC_BREAKPOINT);
157
158 // // Generate the breakpad symbols
159 // println!("generating symbols...");
160 // dump_syms::dumper::single_file(
161 // &dump_syms::dumper::Config {
162 // output: dump_syms::dumper::Output::Store(".test-symbols".into()),
163 // symbol_server: None,
164 // debug_id: None,
165 // code_id: None,
166 // arch: if cfg!(target_arch = "aarch64") {
167 // "arm64"
168 // } else if cfg!(target_arch = "x86_64") {
169 // "x86_64"
170 // } else {
171 // panic!("invalid MacOS target architecture")
172 // },
173 // num_jobs: 2, // default this
174 // check_cfi: false,
175 // emit_inlines: false,
176 // mapping_var: None,
177 // mapping_src: None,
178 // mapping_dest: None,
179 // mapping_file: None,
180 // },
181 // "target/debug/test",
182 // )
183 // .expect("failed to dump symbols");
184
185 // let provider = minidump_unwind::Symbolizer::new(minidump_unwind::simple_symbol_supplier(vec![
186 // ".test-symbols".into(),
187 // ]));
188
189 // let state = futures::executor::block_on(async {
190 // minidump_processor::process_minidump(&md.minidump, &provider).await
191 // })
192 // .unwrap();
193
194 // //state.print(&mut std::io::stdout()).map_err(|_| ()).unwrap();
195
196 // // We expect at least 2 threads, one of which is the fake crashing thread
197 // let fake_crash_thread = state
198 // .threads
199 // .iter()
200 // .find(|cs| cs.thread_id == md.thread)
201 // .expect("failed to find crash thread");
202
203 // assert_eq!(
204 // fake_crash_thread.thread_name.as_deref(),
205 // Some("test-thread")
206 // );
207
208 // assert!(
209 // fake_crash_thread.frames.iter().any(|sf| {
210 // sf.function_name
211 // .as_ref()
212 // .map_or(false, |fname| fname.ends_with("wait_until_killed"))
213 // }),
214 // "unable to locate expected function"
215 // );
216
217 // let mod_list: MinidumpModuleList = md
218 // .minidump
219 // .get_stream()
220 // .expect("Couldn't find MinidumpModuleList");
221
222 // // Ensure we found dyld
223 // assert!(mod_list
224 // .iter()
225 // .any(|module| &module.name == "/usr/lib/dyld"));
226 // }