Repository navigation
d3d12-pe: use synthetic PE32+ fixtures in portable image tests (D3D12-PE-PORTABLE-IMAGE-TESTS) - #2878
matinsanei wants to merge 3 commits into
Conversation
…-PE-PORTABLE-IMAGE-TESTS)
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
|
Verified both the portable suite and the real Windows PE test at AI-assisted with OpenAI Codex (GPT-6). |
KelboLive
left a comment
There was a problem hiding this comment.
Tests now build the image at test time from constants with explicit to_le_bytes at fixed offsets — no committed binary, no usize/size_of/repr(C), so endianness and padding cannot change the bytes. That fixes the current_exe()/ELF problem properly rather than papering over it, and cargo check --target x86_64-unknown-linux-gnu --all-targets compiles clean, so no dangling own_image reference is left in the non-Windows configuration. 36/36 here, fmt and clippy clean, check_no_game_data clean.
Parity with the old assertions holds where it matters: the RVA test still fails if the entry is outside the section (mutation), and the IAT-overflow test still requires the import walker to run — deleting every descriptor parse fails it. The new is_ok() positive control is load-bearing, which I checked rather than assumed: removing it lets the test go green on a fixture whose import name points outside the image.
Two small nits:
- The positive control only asserts
is_ok(), so nothing proves the import was actually decoded —assert_eq!(info.imports[0].dll, "test.dll")is free here and would make the fixture's purpose visible. rejects_garbage_without_panickingnow truncates to 0x100 where the old 0x200 also passes (I checked: the old value still passes), so the case quietly shifted from "raw data missing, headers intact" to "section table missing". Keeping 0x200 would have preserved exactly the old coverage.
One thing to record, not fix here: #[cfg(windows)] on parses_own_test_binary makes explicit something that was already Windows-only (it failed, not skipped, on Linux), but non-Windows now exercises no real-PE import/TLS/exception coverage. Fine for a portability change; a synthetic import + TLS fixture would close it if Linux CI ever runs this crate.
VERIFY-2878 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game filesVerdict: PASS. The four image tests that could only run on Windows now run everywhere, and the Windows-only coverage is kept. I broke the code under test five ways; four of the five were caught, and the one that was not is a weak assertion that predates this pull request. Head 1. What it changesThe tests used the test binary itself as the PE fixture: fn own_image() -> Vec<u8> {
std::fs::read(std::env::current_exe().unwrap()).unwrap()
}which is an ELF on Linux and a PE only on Windows. The pull request keeps that for the one test that is genuinely about a real MSVC image ( fn synthetic_pe_image() -> Vec<u8> {
let mut data = synthetic_image(&[]);
let opt = 0x58;
data[opt + 16..opt + 20].copy_from_slice(&0x1000u32.to_le_bytes()); // AddressOfEntryPoint
data[opt + 112..opt + 120].fill(0); // clear the export directory
data
}
2. Red and green, on Linux
The four failures on So on 3. The fixture is a correct PE32+ imageI checked the layout arithmetic field by field against
The file is 0x400 bytes and the section's raw data is 0x200..0x400, so RVA 0x1000 maps to file offset 0x200 — and that is exactly what the test's own arithmetic assumes ( 4. Breaking the code under testThis pull request touches only tests, so the question is whether the tests would notice the code they cover being wrong. I mutated the production code and ran the suite each time:
M2 and M6 are the two boundary guards the tests exist for, and both are caught. M3 and M4 show that the fixture is read the way the test intends. 5. The new assertion is what pins the import nameThe pull request adds one assertion: let info = ImageInfo::parse(&data).unwrap();
assert_eq!(info.imports[0].dll, "test.dll");I removed that line and re-ran M3 (the import name decoded as "wrong.dll"): 35 passed, 0 failed — the mutation is no longer noticed. With the line in place, M3 is caught. So that one assertion is what makes the import-name decoding covered at all; the rest of the test only checks that parsing fails in one case. 6. One assertion that is weaker than it looks
let off = info.rva_to_file_offset(info.entry_rva).unwrap() as usize;
assert!(off < data.len());M1 returns an offset that is one byte wrong and still inside the file, and the whole suite stays green. The correct answer is known now that the fixture is synthetic — RVA 0x1000 is the section at file offset 0x200 — so 7. What I did not check
AI-assisted: MiniMax M3.1 Flash. |
|
Verified on Windows 11, Rust 1.98.1, no GPU and no game files.
What is measurable here, and it holds. The four tests no longer depend on the host binary at all: Two notes, neither blocking.
The change is the right shape: the crate is portable, and a test that reads the host's executable is a test that only runs where the host happens to be the format under test. |
Coordination item:
D3D12-PE-PORTABLE-IMAGE-TESTS(claimed in #2570 (comment)).Summary
leonida-peis a portable crate with no Windows dependencies, yetcargo test -p leonida-pefailed 4 of 36 tests on Linux/non-Windows hosts becauseown_image()readstd::env::current_exe()(which is an ELF/Mach-O binary, not PE32+).Changes
d3d12/crates/leonida-pe/src/image.rs:synthetic_pe_image()building a minimal valid PE32+ image withentry_rva = 0x1000.rva_to_file_offset_maps_entry_point,rva_to_file_offset_rejects_offsets_past_4_gib,rejects_garbage_without_panicking, andrejects_iat_outside_imageto use synthetic PE32+ fixtures so they run on all platforms.own_image()andparses_own_test_binary(which inspects the host Windows test binary's.pdataandkernel32.dllimports) behind#[cfg(windows)].Verification
cargo test --manifest-path d3d12/Cargo.toml -p leonida-pe-> 32 passed, 4 failed (Malformed("Unknown file magic")).cargo test --manifest-path d3d12/Cargo.toml -p leonida-pe-> 35 passed, 0 failed.cargo clippy --manifest-path d3d12/Cargo.toml -p leonida-pe --all-targets -- -D warnings-> clean.cargo fmt --manifest-path d3d12/Cargo.toml -p leonida-pe -- --check-> clean.python3 -I scripts/check_no_game_data.py d3d12/crates/leonida-pe/src/image.rs-> clean.