| 1 |
//! All of these tests are specific to the MacOS task dumper |
| 2 |
#![cfg(target_os = "macos")] |
| 3 |
|
| 4 |
use minidump_writer::{mach::LoadCommand, task_dumper::TaskDumper}; |
| 5 |
use std::fmt::Write; |
| 6 |
|
| 7 |
fn call_otool(args: &[&str]) -> String { |
| 8 |
let mut cmd = std::process::Command::new("otool"); |
| 9 |
cmd.args(args); |
| 10 |
|
| 11 |
let exe_path = std::env::current_exe().expect("unable to retrieve test executable path"); |
| 12 |
cmd.arg(exe_path); |
| 13 |
|
| 14 |
let output = cmd.output().expect("failed to spawn otool"); |
| 15 |
|
| 16 |
assert!(output.status.success()); |
| 17 |
|
| 18 |
String::from_utf8(output.stdout).expect("stdout was invalid utf-8") |
| 19 |
} |
| 20 |
|
| 21 |
/// Validates we can iterate the load commands for all of the images in the task |
| 22 |
#[test] |
| 23 |
fn iterates_load_commands() { |
| 24 |
let lc_str = call_otool(&["-l"]); |
| 25 |
|
| 26 |
let mut expected = String::new(); |
| 27 |
let mut lc_index = 0; |
| 28 |
|
| 29 |
expected.push('\n'); |
| 30 |
|
| 31 |
while let Some(nlc) = lc_str[lc_index..].find("Load command ") { |
| 32 |
lc_index += nlc; |
| 33 |
|
| 34 |
let block = match lc_str[lc_index + 13..].find("Load command ") { |
| 35 |
Some(ind) => &lc_str[lc_index + 13..lc_index + 13 + ind], |
| 36 |
None => &lc_str[lc_index..], |
| 37 |
}; |
| 38 |
|
| 39 |
// otool prints the load command index for each command, but we only |
| 40 |
// handle the small subset of the available load commands we care about |
| 41 |
// so just ignore that |
| 42 |
let block = &block[block.find('\n').unwrap() + 1..]; |
| 43 |
|
| 44 |
// otool also prints all the sections for LC_SEGMENT_* commands, but |
| 45 |
// we don't care about those, so ignore them |
| 46 |
let block = match block.find("Section") { |
| 47 |
Some(ind) => &block[..ind], |
| 48 |
None => block, |
| 49 |
}; |
| 50 |
|
| 51 |
lc_index += 13; |
| 52 |
|
| 53 |
let cmd = block |
| 54 |
.find("cmd ") |
| 55 |
.expect("load commnd didn't specify cmd kind"); |
| 56 |
let cmd_end = block[cmd..] |
| 57 |
.find('\n') |
| 58 |
.expect("load cmd didn't end with newline"); |
| 59 |
if matches!( |
| 60 |
&block[cmd + 4..cmd + cmd_end], |
| 61 |
"LC_SEGMENT_64" | "LC_UUID" | "LC_ID_DYLIB" | "LC_LOAD_DYLINKER" |
| 62 |
) { |
| 63 |
expected.push_str(block); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
let task_dumper = TaskDumper::new( |
| 68 |
// SAFETY: syscall |
| 69 |
unsafe { mach2::traps::mach_task_self() }, |
| 70 |
); |
| 71 |
|
| 72 |
let mut actual = String::new(); |
| 73 |
|
| 74 |
// Unfortunately, Apple decided to move dynamic libs into a shared cache, |
| 75 |
// removing them from the file system completely, and unless I'm missing it |
| 76 |
// there is no way to get the load commands for the dylibs since otool |
| 77 |
// only understands file paths? So we just get the load commands for the main |
| 78 |
// executable instead, this means that we miss the `LC_ID_DYLIB` commands |
| 79 |
// since they only apply to dylibs, but this test is more that we can |
| 80 |
// correctly iterate through the load commands themselves, so this _should_ |
| 81 |
// be fine... |
| 82 |
let exe_img = task_dumper |
| 83 |
.read_executable_image() |
| 84 |
.expect("failed to read executable image"); |
| 85 |
|
| 86 |
{ |
| 87 |
let lcmds = task_dumper |
| 88 |
.read_load_commands(&exe_img) |
| 89 |
.expect("failed to read load commands"); |
| 90 |
|
| 91 |
for lc in lcmds.iter() { |
| 92 |
match lc { |
| 93 |
LoadCommand::Segment(seg) => { |
| 94 |
let segname = std::str::from_utf8(&seg.segment_name).unwrap(); |
| 95 |
let segname = &segname[..segname.find('\0').unwrap()]; |
| 96 |
write!( |
| 97 |
&mut actual, |
| 98 |
" |
| 99 |
cmd LC_SEGMENT_64 |
| 100 |
cmdsize {} |
| 101 |
segname {} |
| 102 |
vmaddr 0x{:016x} |
| 103 |
vmsize 0x{:016x} |
| 104 |
fileoff {} |
| 105 |
filesize {} |
| 106 |
maxprot 0x{:08x} |
| 107 |
initprot 0x{:08x} |
| 108 |
nsects {} |
| 109 |
flags 0x{:x}", |
| 110 |
seg.cmd_size, |
| 111 |
segname, |
| 112 |
seg.vm_addr, |
| 113 |
seg.vm_size, |
| 114 |
seg.file_off, |
| 115 |
seg.file_size, |
| 116 |
seg.max_prot, |
| 117 |
seg.init_prot, |
| 118 |
seg.num_sections, |
| 119 |
seg.flags, |
| 120 |
) |
| 121 |
.unwrap(); |
| 122 |
} |
| 123 |
LoadCommand::Dylib(_dylib) => { |
| 124 |
unreachable!(); |
| 125 |
} |
| 126 |
LoadCommand::Uuid(uuid) => { |
| 127 |
let id = uuid::Uuid::from_bytes(uuid.uuid); |
| 128 |
let mut uuid_buf = [0u8; uuid::fmt::Hyphenated::LENGTH]; |
| 129 |
let uuid_str = id.hyphenated().encode_upper(&mut uuid_buf); |
| 130 |
|
| 131 |
write!( |
| 132 |
&mut actual, |
| 133 |
" |
| 134 |
cmd LC_UUID |
| 135 |
cmdsize {} |
| 136 |
uuid {uuid_str} |
| 137 |
", |
| 138 |
uuid.cmd_size, |
| 139 |
) |
| 140 |
.unwrap(); |
| 141 |
} |
| 142 |
LoadCommand::DylinkerCommand(dy_cmd) => { |
| 143 |
write!( |
| 144 |
&mut actual, |
| 145 |
" |
| 146 |
cmd LC_LOAD_DYLINKER |
| 147 |
cmdsize {} |
| 148 |
name {} (offset {})", |
| 149 |
dy_cmd.cmd_size, dy_cmd.name, dy_cmd.name_offset, |
| 150 |
) |
| 151 |
.unwrap(); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
similar_asserts::assert_eq!(expected, actual); |
| 158 |
} |