Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions d3d12/crates/leonida-pe/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,23 @@ mod tests {
use super::*;

/// The test binary itself is a real MSVC-linked PE32+ image.
#[cfg(windows)]
fn own_image() -> Vec<u8> {
std::fs::read(std::env::current_exe().unwrap()).unwrap()
}

/// Minimal valid PE32+ image with no data directories and `entry_rva = 0x1000`.
fn synthetic_pe_image() -> Vec<u8> {
let mut data = synthetic_image(&[]);
let opt = 0x58;
// AddressOfEntryPoint inside the single section at RVA 0x1000.
data[opt + 16..opt + 20].copy_from_slice(&0x1000u32.to_le_bytes());
// Clear the export directory so the empty section does not need valid exports.
data[opt + 112..opt + 120].fill(0);
data
}

#[cfg(windows)]
#[test]
fn parses_own_test_binary() {
let info = ImageInfo::parse(&own_image()).unwrap();
Expand All @@ -467,15 +480,15 @@ mod tests {

#[test]
fn rva_to_file_offset_maps_entry_point() {
let data = own_image();
let data = synthetic_pe_image();
let info = ImageInfo::parse(&data).unwrap();
let off = info.rva_to_file_offset(info.entry_rva).unwrap() as usize;
assert!(off < data.len());
assert_eq!(off, 0x200);
}

#[test]
fn rva_to_file_offset_rejects_offsets_past_4_gib() {
let mut info = ImageInfo::parse(&own_image()).unwrap();
let mut info = ImageInfo::parse(&synthetic_pe_image()).unwrap();
let rva = info.sections[0].rva + 0x20;
assert!(info.rva_to_file_offset(rva).is_some());
// Used to overflow (a panic in debug builds, a wrong offset in
Expand All @@ -488,20 +501,26 @@ mod tests {
fn rejects_garbage_without_panicking() {
assert!(ImageInfo::parse(b"").is_err());
assert!(ImageInfo::parse(b"MZ not really").is_err());
let mut truncated = own_image();
let mut truncated = synthetic_pe_image();
truncated.truncate(0x200);
assert!(ImageInfo::parse(&truncated).is_err());
}

#[test]
fn rejects_iat_outside_image() {
let mut data = own_image();
let dd = layout::data_directory_offset(&data, dir::IMPORT).unwrap();
let import_rva = layout::read_u32(&data, dd).unwrap();
let desc = ImageInfo::parse(&data)
.unwrap()
.rva_to_file_offset(import_rva)
.unwrap() as usize;
let mut data = synthetic_pe_image();
let opt = 0x58;
// Point Import directory (index 1) to RVA 0x1000, size 40 bytes (two descriptors).
data[opt + 120..opt + 124].copy_from_slice(&0x1000u32.to_le_bytes());
data[opt + 124..opt + 128].copy_from_slice(&40u32.to_le_bytes());
// Descriptor 0 at file offset 0x200: Name at RVA 0x1028, FirstThunk at RVA 0x1034.
let desc = 0x200;
data[desc + 12..desc + 16].copy_from_slice(&0x1028u32.to_le_bytes());
data[desc + 16..desc + 20].copy_from_slice(&0x1034u32.to_le_bytes());
data[0x228..0x231].copy_from_slice(b"test.dll\0");
let info = ImageInfo::parse(&data).unwrap();
assert_eq!(info.imports[0].dll, "test.dll");

// First descriptor's FirstThunk, at the 4 GiB limit: used to overflow.
data[desc + 16..desc + 20].copy_from_slice(&0xFFFF_FFF8u32.to_le_bytes());
assert!(matches!(
Expand Down
Loading