From 52fcdeb813efee5f35985a88ebedbd8e7322e5bb Mon Sep 17 00:00:00 2001 From: Matin Sanei Date: Tue, 1 Dec 2026 14:28:15 +0100 Subject: [PATCH 1/3] d3d12-pe: use synthetic PE32+ fixtures in portable image tests (D3D12-PE-PORTABLE-IMAGE-TESTS) --- d3d12/crates/leonida-pe/src/image.rs | 40 ++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/d3d12/crates/leonida-pe/src/image.rs b/d3d12/crates/leonida-pe/src/image.rs index b22808bfba..dd486ca3fe 100644 --- a/d3d12/crates/leonida-pe/src/image.rs +++ b/d3d12/crates/leonida-pe/src/image.rs @@ -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 { 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 { + 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(); @@ -467,7 +480,7 @@ 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()); @@ -475,7 +488,7 @@ mod tests { #[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 @@ -488,20 +501,25 @@ 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(); - truncated.truncate(0x200); + let mut truncated = synthetic_pe_image(); + truncated.truncate(0x100); 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"); + assert!(ImageInfo::parse(&data).is_ok()); + // 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!( From 58456fb4c0f5b621dd007fb61f262f7931b06677 Mon Sep 17 00:00:00 2001 From: Matin Sanei Date: Tue, 1 Dec 2026 15:24:24 +0100 Subject: [PATCH 2/3] d3d12-pe: assert decoded import dll name and keep 0x200 truncation length --- d3d12/crates/leonida-pe/src/image.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/d3d12/crates/leonida-pe/src/image.rs b/d3d12/crates/leonida-pe/src/image.rs index dd486ca3fe..fce05376b9 100644 --- a/d3d12/crates/leonida-pe/src/image.rs +++ b/d3d12/crates/leonida-pe/src/image.rs @@ -502,7 +502,7 @@ mod tests { assert!(ImageInfo::parse(b"").is_err()); assert!(ImageInfo::parse(b"MZ not really").is_err()); let mut truncated = synthetic_pe_image(); - truncated.truncate(0x100); + truncated.truncate(0x200); assert!(ImageInfo::parse(&truncated).is_err()); } @@ -518,7 +518,8 @@ mod tests { 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"); - assert!(ImageInfo::parse(&data).is_ok()); + 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()); From 63b8ea9d382e08e2d620f83da3af5d6cccd85e50 Mon Sep 17 00:00:00 2001 From: Matin Sanei Date: Tue, 1 Dec 2026 17:10:10 +0100 Subject: [PATCH 3/3] d3d12-pe: assert exact file offset 0x200 in rva_to_file_offset_maps_entry_point --- d3d12/crates/leonida-pe/src/image.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d3d12/crates/leonida-pe/src/image.rs b/d3d12/crates/leonida-pe/src/image.rs index fce05376b9..9ddcc078b4 100644 --- a/d3d12/crates/leonida-pe/src/image.rs +++ b/d3d12/crates/leonida-pe/src/image.rs @@ -483,7 +483,7 @@ mod tests { 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]