| 1 |
use super::*; |
| 2 |
|
| 3 |
struct ImageLoadInfo { |
| 4 |
/// The preferred load address of the TEXT segment |
| 5 |
vm_addr: u64, |
| 6 |
/// The size of the TEXT segment |
| 7 |
vm_size: u64, |
| 8 |
/// The difference between the images preferred and actual load address |
| 9 |
slide: isize, |
| 10 |
} |
| 11 |
|
| 12 |
struct ImageDetails { |
| 13 |
/// Unique identifier for the module |
| 14 |
uuid: [u8; 16], |
| 15 |
/// The load info for the image indicating the range of addresses it covers |
| 16 |
load_info: ImageLoadInfo, |
| 17 |
/// Path to the module on the local filesystem. Note that as of MacOS 11.0.1 |
| 18 |
/// for system libraries, this path won't actually exist on the filesystem. |
| 19 |
/// This data is more useful as human readable information in a minidump, |
| 20 |
/// but is not required, as the real identifier is the UUID |
| 21 |
file_path: Option<String>, |
| 22 |
/// Version information, not present for the main executable |
| 23 |
version: Option<u32>, |
| 24 |
} |
| 25 |
|
| 26 |
impl MinidumpWriter { |
| 27 |
/// Writes the [`MDStreamType::ModuleListStream`] to the minidump, which is |
| 28 |
/// the last of all loaded modules (images) in the process. |
| 29 |
/// |
| 30 |
/// Notably, this includes the UUID of the image which is needed to look up |
| 31 |
/// debug symbols for the module, as well as the address range covered by |
| 32 |
/// the module to know which debug symbols are used to resolve which instruction |
| 33 |
/// addresses |
| 34 |
pub(crate) fn write_module_list( |
| 35 |
&mut self, |
| 36 |
buffer: &mut DumpBuf, |
| 37 |
dumper: &TaskDumper, |
| 38 |
) -> Result<MDRawDirectory, WriterError> { |
| 39 |
// The list of modules is pretty critical information, but there could |
| 40 |
// still be useful information in the minidump without them if we can't |
| 41 |
// retrieve them for some reason |
| 42 |
let modules = self |
| 43 |
.write_loaded_modules(buffer, dumper) |
| 44 |
.unwrap_or_default(); |
| 45 |
|
| 46 |
let list_header = MemoryWriter::<u32>::alloc_with_val(buffer, modules.len() as u32)?; |
| 47 |
|
| 48 |
let mut dirent = MDRawDirectory { |
| 49 |
stream_type: MDStreamType::ModuleListStream as u32, |
| 50 |
location: list_header.location(), |
| 51 |
}; |
| 52 |
|
| 53 |
if !modules.is_empty() { |
| 54 |
let mapping_list = MemoryArrayWriter::<MDRawModule>::alloc_from_iter(buffer, modules)?; |
| 55 |
dirent.location.data_size += mapping_list.location().data_size; |
| 56 |
} |
| 57 |
|
| 58 |
Ok(dirent) |
| 59 |
} |
| 60 |
|
| 61 |
fn write_loaded_modules( |
| 62 |
&self, |
| 63 |
buf: &mut DumpBuf, |
| 64 |
dumper: &TaskDumper, |
| 65 |
) -> Result<Vec<MDRawModule>, WriterError> { |
| 66 |
let (all_images_info, mut images) = dumper.read_images()?; |
| 67 |
|
| 68 |
// Apparently MacOS will happily list the same image multiple times |
| 69 |
// for some reason, so sort the images by load address and remove all |
| 70 |
// of the duplicates |
| 71 |
images.sort(); |
| 72 |
images.dedup(); |
| 73 |
|
| 74 |
let mut modules = Vec::with_capacity(images.len()); |
| 75 |
|
| 76 |
for image in images { |
| 77 |
if let Ok(image_details) = self.read_image(image, dumper) { |
| 78 |
let is_main_executable = image_details.version.is_none(); |
| 79 |
|
| 80 |
if let Ok(module) = self.write_module(image_details, buf) { |
| 81 |
// We want to keep the modules sorted by their load address except |
| 82 |
// in the case of the main executable image which we want to put |
| 83 |
// first, as it is most likely the culprit, or at least generally |
| 84 |
// the most interesting module for human and machine inspectors |
| 85 |
if is_main_executable { |
| 86 |
modules.insert(0, module); |
| 87 |
} else { |
| 88 |
modules.push(module) |
| 89 |
}; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if !modules |
| 95 |
.get(0) |
| 96 |
.map(|rm| rm.version_info.signature != format::VS_FFI_SIGNATURE) |
| 97 |
.unwrap_or_default() |
| 98 |
{ |
| 99 |
Err(TaskDumpError::NoExecutableImage.into()) |
| 100 |
} else { |
| 101 |
// Crashpad also has code for loading the dyld info from the all images |
| 102 |
// array above, but AFAICT (and from crashpad's own comments) this will |
| 103 |
// never actually happen. It's more robust in the face of changes from |
| 104 |
// Apple, which considering their penchant for changings things often |
| 105 |
// and not actually documenting anything, is fair, but if that ever |
| 106 |
// happens we can just...change the code. |
| 107 |
if let Ok(dyld_image) = self.read_dyld(&all_images_info, dumper) { |
| 108 |
if let Ok(module) = self.write_module(dyld_image, buf) { |
| 109 |
modules.push(module); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
Ok(modules) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/// Obtains important image metadata by traversing the image's load commands |
| 118 |
/// |
| 119 |
/// # Errors |
| 120 |
/// |
| 121 |
/// The image's load commands cannot be traversed, or a required load command |
| 122 |
/// is missing |
| 123 |
fn read_image( |
| 124 |
&self, |
| 125 |
image: ImageInfo, |
| 126 |
dumper: &TaskDumper, |
| 127 |
) -> Result<ImageDetails, TaskDumpError> { |
| 128 |
let mut load_info = None; |
| 129 |
let mut version = None; |
| 130 |
let mut uuid = None; |
| 131 |
|
| 132 |
{ |
| 133 |
let load_commands = dumper.read_load_commands(&image)?; |
| 134 |
|
| 135 |
for lc in load_commands.iter() { |
| 136 |
match lc { |
| 137 |
mach::LoadCommand::Segment(seg) if load_info.is_none() => { |
| 138 |
if &seg.segment_name[..7] == b"__TEXT\0" { |
| 139 |
let slide = image.load_address as isize - seg.vm_addr as isize; |
| 140 |
|
| 141 |
load_info = Some(ImageLoadInfo { |
| 142 |
vm_addr: seg.vm_addr, |
| 143 |
vm_size: seg.vm_size, |
| 144 |
slide, |
| 145 |
}); |
| 146 |
} |
| 147 |
} |
| 148 |
mach::LoadCommand::Dylib(dylib) if version.is_none() => { |
| 149 |
version = Some(dylib.dylib.current_version); |
| 150 |
} |
| 151 |
mach::LoadCommand::Uuid(img_id) if uuid.is_none() => { |
| 152 |
uuid = Some(img_id.uuid); |
| 153 |
} |
| 154 |
_ => {} |
| 155 |
} |
| 156 |
|
| 157 |
if load_info.is_some() && version.is_some() && uuid.is_some() { |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
let load_info = load_info.ok_or(TaskDumpError::MissingLoadCommand { |
| 164 |
name: "LC_SEGMENT_64", |
| 165 |
id: mach::LoadCommandKind::Segment, |
| 166 |
})?; |
| 167 |
let uuid = uuid.ok_or(TaskDumpError::MissingLoadCommand { |
| 168 |
name: "LC_UUID", |
| 169 |
id: mach::LoadCommandKind::Uuid, |
| 170 |
})?; |
| 171 |
|
| 172 |
let file_path = if image.file_path != 0 { |
| 173 |
dumper |
| 174 |
.read_string(image.file_path, None) |
| 175 |
.unwrap_or_default() |
| 176 |
} else { |
| 177 |
None |
| 178 |
}; |
| 179 |
|
| 180 |
Ok(ImageDetails { |
| 181 |
uuid, |
| 182 |
load_info, |
| 183 |
file_path, |
| 184 |
version, |
| 185 |
}) |
| 186 |
} |
| 187 |
|
| 188 |
/// Reads the dynamic linker, which is similar but |
| 189 |
fn read_dyld( |
| 190 |
&self, |
| 191 |
all_images: &task_dumper::AllImagesInfo, |
| 192 |
dumper: &TaskDumper, |
| 193 |
) -> Result<ImageDetails, TaskDumpError> { |
| 194 |
let image = ImageInfo { |
| 195 |
load_address: all_images.dyld_image_load_address, |
| 196 |
file_path: 0, |
| 197 |
file_mod_date: 0, |
| 198 |
}; |
| 199 |
|
| 200 |
let mut load_info = None; |
| 201 |
let mut version = None; |
| 202 |
let mut uuid = None; |
| 203 |
let mut file_path = None; |
| 204 |
|
| 205 |
{ |
| 206 |
let load_commands = dumper.read_load_commands(&image)?; |
| 207 |
|
| 208 |
for lc in load_commands.iter() { |
| 209 |
match lc { |
| 210 |
mach::LoadCommand::Segment(seg) if load_info.is_none() => { |
| 211 |
if &seg.segment_name[..7] == b"__TEXT\0" { |
| 212 |
let slide = image.load_address as isize - seg.vm_addr as isize; |
| 213 |
|
| 214 |
load_info = Some(ImageLoadInfo { |
| 215 |
vm_addr: seg.vm_addr, |
| 216 |
vm_size: seg.vm_size, |
| 217 |
slide, |
| 218 |
}); |
| 219 |
} |
| 220 |
} |
| 221 |
mach::LoadCommand::Dylib(dylib) if version.is_none() => { |
| 222 |
version = Some(dylib.dylib.current_version); |
| 223 |
} |
| 224 |
mach::LoadCommand::Uuid(img_id) if uuid.is_none() => { |
| 225 |
uuid = Some(img_id.uuid); |
| 226 |
} |
| 227 |
mach::LoadCommand::DylinkerCommand(dy_cmd) if file_path.is_none() => { |
| 228 |
file_path = Some(dy_cmd.name.to_owned()); |
| 229 |
} |
| 230 |
_ => {} |
| 231 |
} |
| 232 |
|
| 233 |
if load_info.is_some() && version.is_some() && uuid.is_some() && file_path.is_some() |
| 234 |
{ |
| 235 |
break; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
let load_info = load_info.ok_or(TaskDumpError::MissingLoadCommand { |
| 241 |
name: "LC_SEGMENT_64", |
| 242 |
id: mach::LoadCommandKind::Segment, |
| 243 |
})?; |
| 244 |
let uuid = uuid.ok_or(TaskDumpError::MissingLoadCommand { |
| 245 |
name: "LC_UUID", |
| 246 |
id: mach::LoadCommandKind::Uuid, |
| 247 |
})?; |
| 248 |
|
| 249 |
Ok(ImageDetails { |
| 250 |
uuid, |
| 251 |
load_info, |
| 252 |
file_path, |
| 253 |
version, |
| 254 |
}) |
| 255 |
} |
| 256 |
|
| 257 |
fn write_module( |
| 258 |
&self, |
| 259 |
image: ImageDetails, |
| 260 |
buf: &mut DumpBuf, |
| 261 |
) -> Result<MDRawModule, WriterError> { |
| 262 |
let file_path = image.file_path.as_deref().unwrap_or_default(); |
| 263 |
let module_name = write_string_to_location(buf, file_path)?; |
| 264 |
|
| 265 |
let mut raw_module = MDRawModule { |
| 266 |
base_of_image: (image.load_info.vm_addr as isize + image.load_info.slide) as u64, |
| 267 |
size_of_image: image.load_info.vm_size as u32, |
| 268 |
module_name_rva: module_name.rva, |
| 269 |
..Default::default() |
| 270 |
}; |
| 271 |
|
| 272 |
// Version info is not available for the main executable image since |
| 273 |
// it doesn't issue a LC_ID_DYLIB load command |
| 274 |
if let Some(version) = image.version { |
| 275 |
raw_module.version_info.signature = format::VS_FFI_SIGNATURE; |
| 276 |
raw_module.version_info.struct_version = format::VS_FFI_STRUCVERSION; |
| 277 |
|
| 278 |
// Convert MAC dylib version format, which is a 32 bit number, to the |
| 279 |
// format used by minidump. |
| 280 |
raw_module.version_info.file_version_hi = version >> 16; |
| 281 |
raw_module.version_info.file_version_lo = ((version & 0xff00) << 8) | (version & 0xff); |
| 282 |
} |
| 283 |
|
| 284 |
let module_name = if let Some(sep_index) = file_path.rfind('/') { |
| 285 |
&file_path[sep_index + 1..] |
| 286 |
} else if file_path.is_empty() { |
| 287 |
"<Unknown>" |
| 288 |
} else { |
| 289 |
file_path |
| 290 |
}; |
| 291 |
|
| 292 |
#[derive(scroll::Pwrite, scroll::SizeWith)] |
| 293 |
struct CvInfoPdb { |
| 294 |
cv_signature: u32, |
| 295 |
signature: format::GUID, |
| 296 |
age: u32, |
| 297 |
} |
| 298 |
|
| 299 |
let cv = MemoryWriter::alloc_with_val( |
| 300 |
buf, |
| 301 |
CvInfoPdb { |
| 302 |
cv_signature: format::CvSignature::Pdb70 as u32, |
| 303 |
age: 0, |
| 304 |
signature: image.uuid.into(), |
| 305 |
}, |
| 306 |
)?; |
| 307 |
|
| 308 |
// Note that we don't use write_string_to_location here as the module |
| 309 |
// name is a simple 8-bit string, not 16-bit like most other strings |
| 310 |
// in the minidump, and is directly part of the record itself, not an rva |
| 311 |
buf.write_all(module_name.as_bytes()); |
| 312 |
buf.write_all(&[0]); // null terminator |
| 313 |
|
| 314 |
let mut cv_location = cv.location(); |
| 315 |
cv_location.data_size += module_name.len() as u32 + 1; |
| 316 |
raw_module.cv_record = cv_location; |
| 317 |
|
| 318 |
Ok(raw_module) |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
#[cfg(test)] |
| 323 |
// The libc functions used here are all marked as deprecated, saying you |
| 324 |
// should use the mach2 crate, however, the mach2 crate does not expose |
| 325 |
// any of these functions so... |
| 326 |
#[allow(deprecated)] |
| 327 |
mod test { |
| 328 |
use super::*; |
| 329 |
|
| 330 |
// This function isn't declared in libc nor mach2. And is also undocumented |
| 331 |
// by apple, I know, SHOCKING |
| 332 |
extern "C" { |
| 333 |
fn getsegmentdata( |
| 334 |
header: *const libc::mach_header, |
| 335 |
segname: *const u8, |
| 336 |
size: &mut u64, |
| 337 |
) -> *const u8; |
| 338 |
} |
| 339 |
|
| 340 |
/// Tests that the images we write as modules to the minidump are consistent |
| 341 |
/// with those reported by the kernel. The kernel function used as the source |
| 342 |
/// of truth can only be used to obtain info for the current process, which |
| 343 |
/// is why they aren't used in the actual implementation as we want to handle |
| 344 |
/// both the local and intra-process scenarios |
| 345 |
#[test] |
| 346 |
fn images_match() { |
| 347 |
if std::env::var_os("CI").is_some() && cfg!(target_arch = "aarch64") { |
| 348 |
// https://github.com/rust-minidump/minidump-writer/issues/101 |
| 349 |
println!( |
| 350 |
"this fails on github actions but works on a local aarch64-apple-darwin machine..." |
| 351 |
); |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
let mdw = MinidumpWriter::new(None, None); |
| 356 |
let td = TaskDumper::new(mdw.task); |
| 357 |
|
| 358 |
let (all_images, images) = td.read_images().unwrap(); |
| 359 |
|
| 360 |
let actual_image_count = unsafe { libc::_dyld_image_count() } as u32; |
| 361 |
|
| 362 |
assert_eq!(actual_image_count, images.len() as u32); |
| 363 |
|
| 364 |
for index in 0..actual_image_count { |
| 365 |
let expected_img_hdr = unsafe { libc::_dyld_get_image_header(index) }; |
| 366 |
|
| 367 |
let actual_img = &images[index as usize]; |
| 368 |
|
| 369 |
assert_eq!(actual_img.load_address, expected_img_hdr as u64); |
| 370 |
|
| 371 |
let mut expect_segment_size = 0; |
| 372 |
let expect_segment_data = unsafe { |
| 373 |
getsegmentdata( |
| 374 |
expected_img_hdr, |
| 375 |
b"__TEXT\0".as_ptr(), |
| 376 |
&mut expect_segment_size, |
| 377 |
) |
| 378 |
}; |
| 379 |
|
| 380 |
let actual_img_details = mdw |
| 381 |
.read_image(*actual_img, &td) |
| 382 |
.expect("failed to get image details"); |
| 383 |
|
| 384 |
let expected_image_name = |
| 385 |
unsafe { std::ffi::CStr::from_ptr(libc::_dyld_get_image_name(index)) }; |
| 386 |
|
| 387 |
let expected_slide = unsafe { libc::_dyld_get_image_vmaddr_slide(index) }; |
| 388 |
assert_eq!( |
| 389 |
expected_slide, actual_img_details.load_info.slide, |
| 390 |
"image {index}({expected_image_name:?}) slide is incorrect" |
| 391 |
); |
| 392 |
|
| 393 |
// The segment pointer has already been adjusted by the slide |
| 394 |
assert_eq!( |
| 395 |
expect_segment_data as u64, |
| 396 |
(actual_img_details.load_info.vm_addr as isize + actual_img_details.load_info.slide) |
| 397 |
as u64, |
| 398 |
"image {index}({expected_image_name:?}) TEXT address is incorrect" |
| 399 |
); |
| 400 |
assert_eq!( |
| 401 |
expect_segment_size, actual_img_details.load_info.vm_size, |
| 402 |
"image {index}({expected_image_name:?}) TEXT size is incorrect" |
| 403 |
); |
| 404 |
|
| 405 |
assert_eq!( |
| 406 |
expected_image_name.to_str().unwrap(), |
| 407 |
actual_img_details.file_path.unwrap() |
| 408 |
); |
| 409 |
} |
| 410 |
|
| 411 |
let dyld = mdw |
| 412 |
.read_dyld(&all_images, &td) |
| 413 |
.expect("failed to read dyld"); |
| 414 |
|
| 415 |
// If the user overrides the dynamic linker and runs this test it will |
| 416 |
// fail, but that's kind of on you, person reading this comment wondering |
| 417 |
// why the test fails. Or Apple changed the path in whatever MacOS version |
| 418 |
// in which case, please file a PR! |
| 419 |
assert_eq!("/usr/lib/dyld", dyld.file_path.as_deref().unwrap()); |
| 420 |
assert!(dyld.load_info.vm_size > 0); |
| 421 |
} |
| 422 |
} |