Skip to content

d3d12-pe: use synthetic PE32+ fixtures in portable image tests (D3D12-PE-PORTABLE-IMAGE-TESTS) - #2878

Open
matinsanei wants to merge 3 commits into
ProjectLeonida:mainfrom
matinsanei:fix/leonida-pe-portable-tests
Open

matinsanei wants to merge 3 commits into
ProjectLeonida:mainfrom
matinsanei:fix/leonida-pe-portable-tests

Conversation

@matinsanei

Copy link
Copy Markdown
Contributor

Coordination item: D3D12-PE-PORTABLE-IMAGE-TESTS (claimed in #2570 (comment)).

Summary

leonida-pe is a portable crate with no Windows dependencies, yet cargo test -p leonida-pe failed 4 of 36 tests on Linux/non-Windows hosts because own_image() read std::env::current_exe() (which is an ELF/Mach-O binary, not PE32+).

Changes

  • d3d12/crates/leonida-pe/src/image.rs:
    • Added synthetic_pe_image() building a minimal valid PE32+ image with entry_rva = 0x1000.
    • Updated rva_to_file_offset_maps_entry_point, rva_to_file_offset_rejects_offsets_past_4_gib, rejects_garbage_without_panicking, and rejects_iat_outside_image to use synthetic PE32+ fixtures so they run on all platforms.
    • Gated own_image() and parses_own_test_binary (which inspects the host Windows test binary's .pdata and kernel32.dll imports) behind #[cfg(windows)].

Verification

  • Before change on Linux: cargo test --manifest-path d3d12/Cargo.toml -p leonida-pe -> 32 passed, 4 failed (Malformed("Unknown file magic")).
  • After change on Linux:
    • 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.
  • AI-assisted.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 13:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@merzleckeier

Copy link
Copy Markdown
Contributor

Verified both the portable suite and the real Windows PE test at 52fcdeb81 + 9f27ba2e1, with Rust 1.98.1. On native Linux, cargo test --locked -p leonida-pe --lib on main gave 32 passes and four failures from parsing an ELF executable as PE. With the PR it gave 35 passes and zero failures. The Win64 MSVC-target binary under Wine 11.22 passed all 36 tests, explicitly including image::tests::parses_own_test_binary. The Linux arms used independent Cargo target directories.

AI-assisted with OpenAI Codex (GPT-6).

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. rejects_garbage_without_panicking now 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.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2878 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files

Verdict: 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 58456fb4, base the current main 88a9f133, merge base ed867d7b, 1 commit, 1 file, +29/−10: d3d12/crates/leonida-pe/src/image.rs. The whole change is inside mod tests; no production code is touched.

1. What it changes

The 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 (#[cfg(windows)] fn parses_own_test_binary) and gives the other four a hand-built PE32+ 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
}

leonida-pe is one of the crates that builds on Linux, so this is measurable here.

2. Red and green, on Linux

tree cargo test -p leonida-pe
main 88a9f133 32 passed, 4 FAILED
this pull request 58456fb4 35 passed, 0 failed

The four failures on main are exactly the fixture-dependent ones, and they fail for the reason the pull request describes — the fixture is not a PE:

test image::tests::parses_own_test_binary ... FAILED
  panicked at crates/leonida-pe/src/image.rs:451:51
test image::tests::rva_to_file_offset_maps_entry_point ... FAILED
test image::tests::rva_to_file_offset_rejects_offsets_past_4_gib ... FAILED
test image::tests::rejects_iat_outside_image ... FAILED

So on main these four tests are red on every non-Windows machine — they are not skipped, they fail. This pull request turns them green everywhere.

3. The fixture is a correct PE32+ image

I checked the layout arithmetic field by field against synthetic_image (image.rs:533), which builds a 0x400-byte file:

field value offset
e_lfanew 0x40 d[0x3C..0x40]
PE\0\0 d[0x40..0x44]
machine / sections AMD64 / 1 d[0x44..0x48]
optional header size / magic 240 / 0x20B (PE32+) d[0x54..0x58], d[0x58..0x5A]
AddressOfEntryPoint 0x1000 d[opt+16..opt+20], set by synthetic_pe_image
SectionAlignment / FileAlignment 0x2000 / 0x200 d[opt+56..opt+64]
NumberOfRvaAndSizes 16 d[opt+108..opt+112]
section .edata RVA 0x1000, raw 0x200, raw size 0x200 d[opt+240..opt+264]

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 (Name at RVA 0x1028 → file 0x228, FirstThunk at RVA 0x1034 → file 0x234). The import descriptor is at file 0x200, which is RVA 0x1000 ✓, and the PR writes "test.dll\0" at 0x228 ✓. The IMAGE_IMPORT_DESCRIPTOR field offsets it uses (+12 Name, +16 FirstThunk) are right. The export directory is cleared because the section has no real export table.

4. Breaking the code under test

This 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:

mutation caught by
M1 rva_to_file_offset returns the offset XOR 1 (wrong, but still inside the file) nothing — see §5
M2 the 4 GiB guard checked_add → wrapping_add rva_to_file_offset_rejects_offsets_past_4_gib
M3 the ordinary import's DLL name decoded as something else rejects_iat_outside_image
M4 the entry point read from the wrong optional-header offset rva_to_file_offset_maps_entry_point
M6 the section raw-data bounds check disabled rejects_garbage_without_panicking, rejects_section_extending_beyond_file

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 name

The 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

rva_to_file_offset_maps_entry_point asserts only that the offset is inside the file:

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 assert_eq!(off, 0x200) would pin the mapping. This weakness is in main as well (the same assertion over own_image()); the pull request does not introduce it, and I am not asking for a change, but the synthetic fixture makes the fix a one-liner.

7. What I did not check

  • Windows. Everything above is Linux (Fedora 45, x86_64) with the pinned toolchain (rustc 1.98.1). parses_own_test_binary is #[cfg(windows)] and did not run here, so the Windows-only path — parsing a real MSVC-linked PE32+ image and checking .pdata, the TLS directory and kernel32.dll — is unchanged by this pull request but also unverified by me.
  • Whether the synthetic image covers what the real one covered. The four portable tests now exercise a synthetic image with one section, no exports and two import descriptors; main's versions exercised the test binary, with its real section table, real exports and a real import table. The portable tests are narrower in what they see. The real-image coverage is kept by parses_own_test_binary, but it is the one test that cannot run off Windows.
  • The other crates in the workspace — leonida-gpu, leonida-hle and leonida-loader do not build on Linux for reasons that predate this pull request (upstream: "The crates are Windows-only today … Linux follows Linux: build leonida.exe and run it under Proton or Wine #2577"), so I ran leonida-pe on its own and not the workspace suite.
  • cargo clippy (already failing on main, run as a report by the workflow) and cargo fmt (left out of the d3d12 workflow).

AI-assisted: MiniMax M3.1 Flash.

@Neutron-0

Copy link
Copy Markdown

Verified on Windows 11, Rust 1.98.1, no GPU and no game files.

cargo test -p leonida-pe --lib on the head 63b8ea9d3: 36 passed, 0 failed. On this Windows machine the same suite on the base e04721c7f also passes (36/36), which is expected: own_image() reads the host's PE32+ test binary, and on Windows that is a PE32+ image. The four failures in the report are the non-Windows case, which this machine cannot run, so that half of the claim is not measurable here.

What is measurable here, and it holds. The four tests no longer depend on the host binary at all: rva_to_file_offset_maps_entry_point, rva_to_file_offset_rejects_offsets_past_4_gib, rejects_garbage_without_panicking and rejects_iat_outside_image now build a synthetic PE32+ image and assert exact values (rva_to_file_offset of the entry point is 0x200, the import descriptor's DLL name is test.dll) rather than "the offset is somewhere in the buffer". The overflow guards the tests were written for - rva + 0x20 past 4 GiB, a FirstThunk of 0xFFFF_FFF8 - are still exercised, now against a fixture whose bytes are known.

Two notes, neither blocking.

  1. synthetic_pe_image() builds on synthetic_image(&[]), the helper the layout tests use, so the fixture is the same shape the crate's own layout constants describe; that is the right base. The import directory is written into the section at RVA 0x1000 (file offset 0x200), whose size is 40 bytes for two descriptors, while the fixture's section holds only the one descriptor that is written - the second descriptor's bytes are whatever the section already holds (zeroes). ImageInfo::parse stops at the zeroed descriptor, so the count is right, but a reader that walked both descriptors would read the zeroes as a second entry with OriginalFirstThunk = 0. Writing the second descriptor's terminator explicitly would make the fixture's intent ("two descriptors") true rather than implied.
  2. #[cfg(windows)] on own_image() and parses_own_test_binary is the right call, and the test count on Windows stays at 36. On Linux the count drops by one (the gated test) while the four rewritten tests stay, which is what the report describes.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants