Debian Codegraph @

third_party/rust/minidump/tests/test_minidump.rs

raw
1 // Copyright 2015 Ted Mielczarek. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3
4 use debugid::{CodeId, DebugId};
5 use minidump::system_info::{Cpu, Os};
6 use minidump::*;
7 use minidump_common::format as md;
8 use num_traits::cast::FromPrimitive;
9 use std::fs::File;
10 use std::io::Read;
11 use std::path::PathBuf;
12 use std::time::SystemTime;
13
14 fn get_test_minidump_path(filename: &str) -> PathBuf {
15 let mut path = PathBuf::from(file!());
16 path.pop();
17 path.pop();
18 path.pop();
19 path.push("../");
20 path.push("testdata");
21 path.push(filename);
22 println!("{path:?}");
23 path
24 }
25
26 fn read_test_minidump() -> Result<MmapMinidump, Error> {
27 let path = get_test_minidump_path("test.dmp");
28 Minidump::read_path(path)
29 }
30
31 fn read_linux_minidump() -> Result<MmapMinidump, Error> {
32 let path = get_test_minidump_path("linux-mini.dmp");
33 Minidump::read_path(path)
34 }
35
36 #[ctor::ctor]
37 fn init_logger() {
38 env_logger::builder().is_test(true).init();
39 }
40
41 #[test]
42 fn test_minidump_read_path() {
43 read_test_minidump().unwrap();
44 }
45
46 #[test]
47 fn test_minidump_read() {
48 let path = get_test_minidump_path("test.dmp");
49 let mut f = File::open(path).unwrap();
50 let mut buf = vec![];
51 f.read_to_end(&mut buf).unwrap();
52 let _dump = Minidump::read(buf).unwrap();
53 }
54
55 #[test]
56 fn test_module_list() {
57 let dump = read_test_minidump().unwrap();
58 let module_list = dump.get_stream::<MinidumpModuleList>().unwrap();
59 assert_eq!(
60 module_list.module_at_address(0x400000).unwrap().code_file(),
61 "c:\\test_app.exe"
62 );
63 let modules = module_list.iter().collect::<Vec<_>>();
64 let module_files = modules.iter().map(|m| m.code_file()).collect::<Vec<_>>();
65 assert_eq!(modules.len(), 13);
66 assert_eq!(modules[0].base_address(), 0x400000);
67 assert_eq!(modules[0].size(), 0x2d000);
68 assert_eq!(modules[0].code_file(), "c:\\test_app.exe");
69 assert_eq!(
70 modules[0].code_identifier().unwrap(),
71 CodeId::new("45D35F6C2d000".to_string())
72 );
73 assert_eq!(modules[0].debug_file().unwrap(), "c:\\test_app.pdb");
74 assert_eq!(
75 modules[0].debug_identifier().unwrap(),
76 DebugId::from_breakpad("5A9832E5287241C1838ED98914E9B7FF1").unwrap()
77 );
78 assert!(modules[0].version().is_none());
79
80 assert_eq!(modules[12].base_address(), 0x76bf0000);
81 assert_eq!(modules[12].size(), 0xb000);
82 assert_eq!(modules[12].code_file(), "C:\\WINDOWS\\system32\\psapi.dll");
83 assert_eq!(
84 modules[12].code_identifier().unwrap(),
85 CodeId::new("411096CAb000".to_string())
86 );
87 assert_eq!(modules[12].debug_file().unwrap(), "psapi.pdb");
88 assert_eq!(
89 modules[12].debug_identifier().unwrap(),
90 DebugId::from_breakpad("A5C3A1F9689F43D8AD228A09293889702").unwrap()
91 );
92 assert_eq!(modules[12].version().unwrap(), "5.1.2600.2180");
93
94 assert_eq!(
95 module_files,
96 vec![
97 r"c:\test_app.exe",
98 r"C:\WINDOWS\system32\ntdll.dll",
99 r"C:\WINDOWS\system32\kernel32.dll",
100 r"C:\WINDOWS\system32\ole32.dll",
101 r"C:\WINDOWS\system32\advapi32.dll",
102 r"C:\WINDOWS\system32\rpcrt4.dll",
103 r"C:\WINDOWS\system32\gdi32.dll",
104 r"C:\WINDOWS\system32\user32.dll",
105 r"C:\WINDOWS\system32\msvcrt.dll",
106 r"C:\WINDOWS\system32\imm32.dll",
107 r"C:\WINDOWS\system32\dbghelp.dll",
108 r"C:\WINDOWS\system32\version.dll",
109 r"C:\WINDOWS\system32\psapi.dll",
110 ]
111 );
112
113 assert_eq!(
114 module_list
115 .by_addr()
116 .map(|m| m.code_file())
117 .collect::<Vec<_>>(),
118 vec![
119 r"c:\test_app.exe",
120 r"C:\WINDOWS\system32\dbghelp.dll",
121 r"C:\WINDOWS\system32\imm32.dll",
122 r"C:\WINDOWS\system32\psapi.dll",
123 r"C:\WINDOWS\system32\ole32.dll",
124 r"C:\WINDOWS\system32\version.dll",
125 r"C:\WINDOWS\system32\msvcrt.dll",
126 r"C:\WINDOWS\system32\user32.dll",
127 r"C:\WINDOWS\system32\advapi32.dll",
128 r"C:\WINDOWS\system32\rpcrt4.dll",
129 r"C:\WINDOWS\system32\gdi32.dll",
130 r"C:\WINDOWS\system32\kernel32.dll",
131 r"C:\WINDOWS\system32\ntdll.dll",
132 ]
133 );
134 }
135
136 #[test]
137 fn test_system_info() {
138 let dump = read_test_minidump().unwrap();
139 let system_info = dump.get_stream::<MinidumpSystemInfo>().unwrap();
140 assert_eq!(system_info.os, Os::Windows);
141 assert_eq!(system_info.cpu, Cpu::X86);
142 assert_eq!(
143 system_info.cpu_info().unwrap(),
144 "GenuineIntel family 6 model 13 stepping 8"
145 );
146 assert_eq!(&system_info.csd_version().unwrap(), "Service Pack 2");
147 }
148
149 #[test]
150 fn test_misc_info() {
151 let dump = read_test_minidump().unwrap();
152 let misc_info = dump.get_stream::<MinidumpMiscInfo>().unwrap();
153 assert_eq!(misc_info.raw.process_id(), Some(&3932));
154 assert_eq!(misc_info.raw.process_create_time(), Some(&0x45d35f73));
155 assert_eq!(
156 misc_info
157 .process_create_time()
158 .unwrap()
159 .duration_since(SystemTime::UNIX_EPOCH)
160 .unwrap()
161 .as_secs(),
162 1171480435, // = 2007-02-14T19:13:55
163 );
164 }
165
166 #[test]
167 fn test_breakpad_info() {
168 let dump = read_test_minidump().unwrap();
169 let breakpad_info = dump.get_stream::<MinidumpBreakpadInfo>().unwrap();
170 assert_eq!(breakpad_info.dump_thread_id.unwrap(), 0x11c0);
171 assert_eq!(breakpad_info.requesting_thread_id.unwrap(), 0xbf4);
172 }
173
174 #[test]
175 fn test_crashpad_info() {
176 let path = get_test_minidump_path("simple-crashpad.dmp");
177 let dump = Minidump::read_path(&path).unwrap();
178 let crashpad_info = dump.get_stream::<MinidumpCrashpadInfo>().unwrap();
179
180 let report_id = md::GUID {
181 data1: 0x42F9_DE72,
182 data2: 0x518A,
183 data3: 0x43DD,
184 data4: [0x97, 0xD7, 0x8D, 0xDC, 0x32, 0x8D, 0x36, 0x62],
185 };
186 assert_eq!(crashpad_info.raw.report_id, report_id);
187
188 let client_id = md::GUID {
189 data1: 0x6FD2_B3B9,
190 data2: 0x9833,
191 data3: 0x4B2F,
192 data4: [0xBB, 0xF7, 0xB, 0xCF, 0x50, 0x1B, 0xAD, 0x7E],
193 };
194 assert_eq!(crashpad_info.raw.client_id, client_id);
195
196 assert_eq!(crashpad_info.simple_annotations["hello"], "world");
197 assert_eq!(crashpad_info.module_list.len(), 2);
198
199 let module = &crashpad_info.module_list[0];
200 assert_eq!(module.module_index, 16);
201 assert_eq!(module.list_annotations, vec!["abort() called".to_owned()]);
202 assert!(module.simple_annotations.is_empty());
203 assert!(module.annotation_objects.is_empty());
204 }
205
206 #[test]
207 fn test_assertion() {
208 let path = get_test_minidump_path("invalid-parameter.dmp");
209 let dump = Minidump::read_path(&path).unwrap();
210 let assertion = dump.get_stream::<MinidumpAssertion>().unwrap();
211 assert_eq!(assertion.expression().unwrap(), "format != nullptr");
212 assert_eq!(assertion.function().unwrap(), "common_vfprintf");
213 assert_eq!(
214 assertion.file().unwrap(),
215 r"minkernel\crts\ucrt\src\appcrt\stdio\output.cpp"
216 );
217 assert_eq!(assertion.raw.line, 32);
218 assert_eq!(
219 md::AssertionType::from_u32(assertion.raw._type),
220 Some(md::AssertionType::InvalidParameter)
221 );
222 }
223
224 #[test]
225 fn test_exception() {
226 let dump = read_test_minidump().unwrap();
227 let exception = dump.get_stream::<MinidumpException>().unwrap();
228 let system_info = dump.get_stream::<MinidumpSystemInfo>().unwrap();
229 let misc_info = dump.get_stream::<MinidumpMiscInfo>().ok();
230 assert_eq!(exception.thread_id, 0xbf4);
231 assert_eq!(exception.raw.exception_record.exception_code, 0xc0000005);
232 if let Some(ctx) = exception
233 .context(&system_info, misc_info.as_ref())
234 .as_deref()
235 {
236 assert_eq!(ctx.get_instruction_pointer(), 0x40429e);
237 assert_eq!(ctx.get_stack_pointer(), 0x12fe84);
238 if let MinidumpContext {
239 raw: MinidumpRawContext::X86(ref raw),
240 ref valid,
241 } = *ctx
242 {
243 assert_eq!(raw.eip, 0x40429e);
244 assert_eq!(*valid, MinidumpContextValidity::All);
245 } else {
246 panic!("Wrong context type");
247 }
248 } else {
249 panic!("Missing context");
250 }
251 }
252
253 #[test]
254 fn test_thread_list() {
255 let dump = read_test_minidump().unwrap();
256 let thread_list = dump.get_stream::<MinidumpThreadList<'_>>().unwrap();
257 let system_info = dump.get_stream::<MinidumpSystemInfo>().unwrap();
258 let misc_info = dump.get_stream::<MinidumpMiscInfo>().ok();
259 let memory_list = dump.get_memory().unwrap_or_default();
260
261 let threads = &thread_list.threads;
262 assert_eq!(threads.len(), 2);
263 assert_eq!(threads[0].raw.thread_id, 0xbf4);
264 assert_eq!(threads[1].raw.thread_id, 0x11c0);
265 let id = threads[1].raw.thread_id;
266 assert_eq!(thread_list.get_thread(id).unwrap().raw.thread_id, id);
267 if let Some(ctx) = threads[0]
268 .context(&system_info, misc_info.as_ref())
269 .as_deref()
270 {
271 assert_eq!(ctx.get_instruction_pointer(), 0x7c90eb94);
272 assert_eq!(ctx.get_stack_pointer(), 0x12f320);
273 if let MinidumpContext {
274 raw: MinidumpRawContext::X86(ref raw),
275 ref valid,
276 } = *ctx
277 {
278 assert_eq!(raw.eip, 0x7c90eb94);
279 assert_eq!(*valid, MinidumpContextValidity::All);
280 } else {
281 panic!("Wrong context type");
282 }
283 } else {
284 panic!("Missing context");
285 }
286 if let Some(ref stack) = threads[0].stack_memory(&memory_list) {
287 // Try the beginning
288 assert_eq!(stack.get_memory_at_address::<u8>(0x12f31c).unwrap(), 0);
289 assert_eq!(stack.get_memory_at_address::<u16>(0x12f31c).unwrap(), 0);
290 assert_eq!(stack.get_memory_at_address::<u32>(0x12f31c).unwrap(), 0);
291 assert_eq!(
292 stack.get_memory_at_address::<u64>(0x12f31c).unwrap(),
293 0x7c90e9c000000000
294 );
295 // And the end
296 assert_eq!(stack.get_memory_at_address::<u8>(0x12ffff).unwrap(), 0);
297 assert_eq!(stack.get_memory_at_address::<u16>(0x12fffe).unwrap(), 0);
298 assert_eq!(stack.get_memory_at_address::<u32>(0x12fffc).unwrap(), 0);
299 assert_eq!(
300 stack.get_memory_at_address::<u64>(0x12fff8).unwrap(),
301 0x405443
302 );
303 } else {
304 panic!("Missing stack memory");
305 }
306 }
307
308 #[test]
309 fn test_empty_minidump() {
310 match Minidump::read(&b""[..]) {
311 Ok(_) => panic!("Should have failed to read minidump"),
312 Err(e) => assert_eq!(e, Error::MissingHeader),
313 }
314 }
315
316 #[test]
317 fn backwards_range() {
318 let data = include_bytes!("../../testdata/invalid-range.dmp");
319
320 match Minidump::read(&data[..]) {
321 Ok(f) => {
322 // TODO verify this is correct
323 // This seems to call `MinidumpStream::read()` with a `bytes` that is the entire
324 // minidump!
325 let _ = f
326 .get_stream::<MinidumpLinuxMaps>()
327 .expect_err("range should be invalid");
328 }
329 Err(e) => {
330 panic!("Expected to parse the header, got {:?}", e);
331 }
332 }
333 }
334
335 #[test]
336 fn test_record_count_mac_info() {
337 let data = include_bytes!("../../testdata/invalid-record-count.dmp");
338
339 match Minidump::read(&data[..]) {
340 Ok(f) => {
341 let _ = f.get_stream::<MinidumpMacCrashInfo>();
342 }
343 Err(e) => {
344 panic!("Expected to parse the header, got {:?}", e);
345 }
346 }
347 }
348
349 #[test]
350 fn test_linux_os_version() {
351 let dump = read_linux_minidump().unwrap();
352 let system_info = dump.get_stream::<MinidumpSystemInfo>().unwrap();
353
354 assert_eq!(system_info.os_parts().0, "4.9.60-linuxkit-aufs");
355 assert_eq!(
356 system_info.os_parts().1.unwrap(),
357 "#1 SMP Mon Nov 6 16:00:12 UTC 2017",
358 );
359 }
360
361 #[test]
362 fn test_full_dump_memory() {
363 let path = get_test_minidump_path("full-dump.dmp");
364 let dump = Minidump::read_path(&path).unwrap();
365 let memory_list = dump.get_stream::<MinidumpMemory64List<'_>>().unwrap();
366 assert_eq!(memory_list.iter().count(), 54);
367 let blocks: Vec<_> = memory_list.iter().take(3).collect();
368 assert_eq!(blocks[0].base_address, 0x007FFE0000);
369 assert_eq!(blocks[0].size, 0x1000);
370 assert_eq!(blocks[0].bytes[0..8], [0, 0, 0, 0, 0, 0, 0xA0, 0x0F]);
371 assert_eq!(blocks[1].base_address, 0x007FFE9000);
372 assert_eq!(blocks[1].size, 0x1000);
373 assert_eq!(blocks[1].bytes[0..8], [0x48, 0x61, 0x6C, 0x54, 0, 0, 0, 0]);
374 assert_eq!(blocks[2].base_address, 0x9897D0D000);
375 assert_eq!(blocks[2].size, 0x3000);
376 assert_eq!(blocks[2].bytes[0..8], [0, 0, 0, 0, 0, 0, 0, 0]);
377 }