d3d12-pe: use synthetic PE32+ fixtures in portable image tests (D3D12-PE-PORTABLE-IMAGE-TESTS) #2878
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2878"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by matinsanei on 2026-12-01T13:30:40Z.
Coordination item:
D3D12-PE-PORTABLE-IMAGE-TESTS(claimed in https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7312616767).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.Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-12-01T13:30:51Z.
Review: COMMENTED
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.
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T13:43:42Z.
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 --libon 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 includingimage::tests::parses_own_test_binary. The Linux arms used independent Cargo target directories.AI-assisted with OpenAI Codex (GPT-6).
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T14:03:10Z.
Review: COMMENTED
Tests now build the image at test time from constants with explicit
to_le_bytesat fixed offsets — no committed binary, nousize/size_of/repr(C), so endianness and padding cannot change the bytes. That fixes thecurrent_exe()/ELF problem properly rather than papering over it, andcargo check --target x86_64-unknown-linux-gnu --all-targetscompiles clean, so no danglingown_imagereference is left in the non-Windows configuration. 36/36 here, fmt and clippy clean,check_no_game_dataclean.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:
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)]onparses_own_test_binarymakes 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.Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-01T15:34:15Z.
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 currentmain88a9f133, merge baseed867d7b, 1 commit, 1 file, +29/−10:d3d12/crates/leonida-pe/src/image.rs. The whole change is insidemod tests; no production code is touched.1. What it changes
The tests used the test binary itself as the PE fixture:
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:leonida-peis one of the crates that builds on Linux, so this is measurable here.2. Red and green, on Linux
cargo test -p leonida-pemain88a9f13358456fb4The four failures on
mainare exactly the fixture-dependent ones, and they fail for the reason the pull request describes — the fixture is not a PE:So on
mainthese 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:e_lfanewd[0x3C..0x40]PE\0\0d[0x40..0x44]d[0x44..0x48]d[0x54..0x58],d[0x58..0x5A]AddressOfEntryPointd[opt+16..opt+20], set bysynthetic_pe_imageSectionAlignment/FileAlignmentd[opt+56..opt+64]NumberOfRvaAndSizesd[opt+108..opt+112].edatad[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 (
Nameat RVA 0x1028 → file 0x228,FirstThunkat RVA 0x1034 → file 0x234). The import descriptor is at file 0x200, which is RVA 0x1000 ✓, and the PR writes "test.dll\0" at 0x228 ✓. TheIMAGE_IMPORT_DESCRIPTORfield offsets it uses (+12Name,+16FirstThunk) 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:
rva_to_file_offsetreturns the offset XOR 1 (wrong, but still inside the file)checked_add→wrapping_addrva_to_file_offset_rejects_offsets_past_4_gibrejects_iat_outside_imagerva_to_file_offset_maps_entry_pointrejects_garbage_without_panicking,rejects_section_extending_beyond_fileM2 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:
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_pointasserts only that the offset is inside the file: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 inmainas well (the same assertion overown_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
rustc 1.98.1).parses_own_test_binaryis#[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 andkernel32.dll— is unchanged by this pull request but also unverified by me.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 byparses_own_test_binary, but it is the one test that cannot run off Windows.leonida-gpu,leonida-hleandleonida-loaderdo not build on Linux for reasons that predate this pull request (upstream: "The crates are Windows-only today … Linux follows #2577"), so I ranleonida-peon its own and not the workspace suite.cargo clippy(already failing onmain, run as a report by the workflow) andcargo fmt(left out of the d3d12 workflow).AI-assisted: MiniMax M3.1 Flash.
Imported from ubiquitarianism/leonida. Originally posted by Neutron-0 on 2026-12-01T16:20:43Z.
Verified on Windows 11, Rust 1.98.1, no GPU and no game files.
cargo test -p leonida-pe --libon the head63b8ea9d3: 36 passed, 0 failed. On this Windows machine the same suite on the basee04721c7falso 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_panickingandrejects_iat_outside_imagenow build a synthetic PE32+ image and assert exact values (rva_to_file_offsetof the entry point is0x200, the import descriptor's DLL name istest.dll) rather than "the offset is somewhere in the buffer". The overflow guards the tests were written for -rva + 0x20past 4 GiB, aFirstThunkof0xFFFF_FFF8- are still exercised, now against a fixture whose bytes are known.Two notes, neither blocking.
synthetic_pe_image()builds onsynthetic_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 RVA0x1000(file offset0x200), 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::parsestops at the zeroed descriptor, so the count is right, but a reader that walked both descriptors would read the zeroes as a second entry withOriginalFirstThunk = 0. Writing the second descriptor's terminator explicitly would make the fixture's intent ("two descriptors") true rather than implied.#[cfg(windows)]onown_image()andparses_own_test_binaryis 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.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T12:29:53Z.
This pull request touches the render path, so it needs an in-game run before it can be merged: unit tests and CI do not catch rendering regressions (a device loss after an earlier merge is why this gate exists).
Could you, or anyone with a working setup, post a run report on the current head merged onto current
main:mainSHA it was merged onto;mainalone for comparison, if you can.Please do not attach logs or screenshots that show game content. If a run on this exact head is already posted, a link to it in a reply is enough.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T18:14:36Z.
Verified on Linux: this is exactly the case the PR fixes. On current
main, fourleonida-petests fail off Windows; with the head merged, all pass.cargo test -p leonida-pe --locked(fromd3d12/)main45dc41f95(red)parses_own_test_binary,rva_to_file_offset_maps_entry_point,rejects_iat_outside_image,rva_to_file_offset_rejects_offsets_past_4_gib(each reads the test binary itself, which is ELF here)main+ this head (green)parses_own_test_binaryis now#[cfg(windows)], so 35 rather than 36)Read in the diff: test-only, in
d3d12/crates/leonida-pe/src/image.rs.synthetic_pe_image()builds on the existingsynthetic_image(&[]), sets the entry RVA to the one section, and clears the export directory.rva_to_file_offset_maps_entry_pointnow asserts the exact offset (0x200) instead ofoff < data.len(), which is stronger.rejects_iat_outside_imagebuilds its own import descriptor and first checks that the image parses withtest.dllbefore it corruptsFirstThunk, so the error it then expects comes from the corruption, not from a broken fixture. The real-binary check stays on Windows. No production code changes.CI: the two red checks on this head (
check,linux-python-and-viewer, run 2026-12-01 16:1x) fail in Python tests this PR does not touch (thetest_f.T.test_afixture in the TLS-callback tests and the fuzzReferenceerrors). That ismainat the old merge base. A run after merging currentmainshould clear them.The render-path gate requested on this PR does not apply to a test-only diff, as far as I can see: nothing outside
#[cfg(test)]changes.Verdict: ready once CI is rerun on a head that includes current
main.AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.
Generated by Claude Code
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T19:28:07Z.
Re-verified on the current head (Linux): the four
leonida-petests still fail onmainand pass with the head. The PR's code is unchanged; the new commit only mergesmain.cargo test -p leonida-pe --locked(fromd3d12/)mainb41763227(red)parses_own_test_binary,rva_to_file_offset_maps_entry_point,rejects_iat_outside_image,rva_to_file_offset_rejects_offsets_past_4_gibmain+ head (green)python -I scripts/check_no_game_data.py d3d12/crates/leonida-pe/src/image.rs: exit 0.python -I scripts/run_standalone_tests.py -j 8): 660 files, 1 failed (tests/test_run_game_wine.py). Plainmaingives the same result, and the failure comes from the local environment, not this PR.Earlier report: no defects. The CI failures it noted came from the old merge base and are gone: on
ab38b0136,check,linux-python-and-viewer,no-game-dataandsemantic-testspass.CI, one red job:
d3d12 (windows-latest)failed inleonida-lua, not in this PR's crate.tests::a_scan_without_a_size_reaches_the_end_of_the_imagepanicked atcrates\leonida-lua\src\tests.rs:587(["tail 1 0"]vs["tail 1 1"]). That flaky test is already inmain; the cause, an unaligned fake image base, is explained on #3179. The job stopped there, soparses_own_test_binaryinleonida-pe, which runs only on Windows, did not run on this head. A rerun of that job should confirm it.The diff is test-only (
#[cfg(test)]), so as far as I can see the render-path gate does not apply.Verdict: ready once
d3d12 (windows-latest)is rerun and passesleonida-pe.AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.
Generated by Claude Code