test(allocator): check pe_import_first's malformed images in memory #14134

Merged
roboterspinne merged 1 commit from upstream-pr/14134 into main 2026-11-05 08:45:35 +00:00
roboterspinne commented 2026-10-31 20:25:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-31T20:25:09Z.


What

pe_import_first (TIMEOUT 10, core/libs/CMakeLists.txt:389) timed out without output from time to time on Windows 11 with Microsoft Defender's real-time and cloud protection at their defaults. Each failure case wrote its image to %TEMP% and passed it to MoveImportFirstInFile, and that call stalled on the freshly written copy of optional-header-incomplete-import-entry (SizeOfOptionalHeader 127, added in #11356). A probe that wrote fresh copies of the test's images (8 changed bytes in the DOS header) and timed MoveImportFirstInFile measured 296.8 to 10062.0 ms for that image in 24 runs, 6 of them over 1 s and 2 at about 10.06 s; every other image the test wrote took at most 43.9 ms, and the same image with MZ replaced by XX took 8.7 to 18.7 ms. That points to Defender's check of an unknown PE-shaped file; the cause is inferred from these timings, not observed. A run that hits a long stall exceeds the test's 10 s limit. The test passes on the project's Windows runners in 0.03 s.

The failure cases do not need the file. Every throw in MoveImportFirst (PeImportFirst.cpp:40-85, including the bounds checks in read16 and read32) comes before its only write to the image (line 87), and MoveImportFirstInFile opens the file for writing only after MoveImportFirst returns (lines 97-99). The eight malformed-image cases (truncated-directory, pe32, directory-count-zero, directory-count-one, optional-header-without-imports, optional-header-incomplete-import-entry, unterminated-name, name-past-the-file) now run MoveImportFirst on a copy of the bytes and require the same error text and an unchanged copy, which also checks that MoveImportFirst leaves the bytes alone when it throws; the file round trip could not see that, because the file is only written after a successful move. missing-import stays on disk and keeps covering the unchanged-file path, and the two file moves are unchanged. The names, expected texts and order of the cases are unchanged. TIMEOUT 10 stays, since CONTRIBUTING.md asks to shrink a test that gets close instead of raising its timeout.

pr_overlap.py --branch reports no overlap with open pull requests.

Tested

Windows 11 Home (build 26200), Intel Core i9-12900H, WinLibs GCC 15.2.0, full build (Ninja, Release, BUILD_TESTING=ON), on 2f7381f9ea (based on 474030e584):

  • Before this change, pe_import_first timed out in a full ctest --test-dir build -j 6 on 73f4f7e06e and again in a serial rerun.
  • ctest --test-dir build -R '^pe_import_first$' passed three times in 0.03 to 0.11 s.
  • The probe above: of the files the test wrote before this change, optional-header-incomplete-import-entry took 296.8 to 10062.0 ms (24 runs with fresh bytes) and the other seven malformed images at most 42.8 ms (12 runs each with fresh bytes); the three files it writes now (two-directory, moved, missing-import) took 13.3 to 43.9 ms (12 runs each with fresh bytes).
  • Two scratch copies of the test, one with a wrong expected text for optional-header-incomplete-import-entry and one with a valid PE32+ image for pe32, fail with failed with another error: the image has no imports and the move did not fail.
  • python tools/check_conventions.py --base 474030e584 with this title and description passed on 2f7381f9ea.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5101239717 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14134). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-31T20:25:09Z. --- ### What `pe_import_first` (`TIMEOUT 10`, `core/libs/CMakeLists.txt:389`) timed out without output from time to time on Windows 11 with Microsoft Defender's real-time and cloud protection at their defaults. Each failure case wrote its image to `%TEMP%` and passed it to `MoveImportFirstInFile`, and that call stalled on the freshly written copy of `optional-header-incomplete-import-entry` (`SizeOfOptionalHeader` 127, added in #11356). A probe that wrote fresh copies of the test's images (8 changed bytes in the DOS header) and timed `MoveImportFirstInFile` measured 296.8 to 10062.0 ms for that image in 24 runs, 6 of them over 1 s and 2 at about 10.06 s; every other image the test wrote took at most 43.9 ms, and the same image with `MZ` replaced by `XX` took 8.7 to 18.7 ms. That points to Defender's check of an unknown PE-shaped file; the cause is inferred from these timings, not observed. A run that hits a long stall exceeds the test's 10 s limit. The test passes on the project's Windows runners in 0.03 s. The failure cases do not need the file. Every throw in `MoveImportFirst` (`PeImportFirst.cpp:40-85`, including the bounds checks in `read16` and `read32`) comes before its only write to the image (line 87), and `MoveImportFirstInFile` opens the file for writing only after `MoveImportFirst` returns (lines 97-99). The eight malformed-image cases (`truncated-directory`, `pe32`, `directory-count-zero`, `directory-count-one`, `optional-header-without-imports`, `optional-header-incomplete-import-entry`, `unterminated-name`, `name-past-the-file`) now run `MoveImportFirst` on a copy of the bytes and require the same error text and an unchanged copy, which also checks that `MoveImportFirst` leaves the bytes alone when it throws; the file round trip could not see that, because the file is only written after a successful move. `missing-import` stays on disk and keeps covering the unchanged-file path, and the two file moves are unchanged. The names, expected texts and order of the cases are unchanged. `TIMEOUT 10` stays, since CONTRIBUTING.md asks to shrink a test that gets close instead of raising its timeout. `pr_overlap.py --branch` reports no overlap with open pull requests. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11 Home (build 26200), Intel Core i9-12900H, WinLibs GCC 15.2.0, full build (Ninja, Release, `BUILD_TESTING=ON`), on 2f7381f9ea (based on 474030e584): - Before this change, `pe_import_first` timed out in a full `ctest --test-dir build -j 6` on 73f4f7e06e and again in a serial rerun. - `ctest --test-dir build -R '^pe_import_first$'` passed three times in 0.03 to 0.11 s. - The probe above: of the files the test wrote before this change, `optional-header-incomplete-import-entry` took 296.8 to 10062.0 ms (24 runs with fresh bytes) and the other seven malformed images at most 42.8 ms (12 runs each with fresh bytes); the three files it writes now (`two-directory`, `moved`, `missing-import`) took 13.3 to 43.9 ms (12 runs each with fresh bytes). - Two scratch copies of the test, one with a wrong expected text for `optional-header-incomplete-import-entry` and one with a valid PE32+ image for `pe32`, fail with `failed with another error: the image has no imports` and `the move did not fail`. - `python tools/check_conventions.py --base 474030e584` with this title and description passed on 2f7381f9ea. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes
MarcZX10 commented 2026-10-31 20:36:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T20:36:51Z.

Review: APPROVED


Reviewed — looks good

The in-memory failure cases check that malformed images remain unchanged, while successful and missing-import file-path cases remain covered.

What I ran

Reviewed head 2f7381f9ea4f258e12415cce50c54d7d08b7062b against base 0836dfdf4fecb1e0326100c490476e15d3a74da5; the stack helper's merge preview was clean. Environment: Debian Linux, GCC 12.2.0.

git diff --check upstream/main...refs/remotes/review/pr-14134
python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14134

Results

Check Result
Diff check Passed; exit 0.
Conventions and PR template Passed.
Dependency and mergeability check Passed; clean against current base.
Hosted Windows workflow Overall job passed, but its log says no guest core/libs test sources changed; it did not run this allocator test. Linux and specialized jobs were skipped.
pe_import_first build and test The author reports a full Windows build and three exact-head CTest passes (0.03–0.11 s); I did not reproduce them locally. The author also reports two scratch negative controls failed as expected.
Conventions check output
All conventions checks passed.
Merge preview output
- PR #14134: **clean** against current base; head `2f7381f9ea4f258e12415cce50c54d7d08b7062b`; changed paths: none reported

Findings

Blocking

None found in the test-only diff.

Non-blocking

The malformed-image cases no longer exercise the file wrapper, but the unchanged-file case for a missing import and both successful file operations remain. In the reviewed implementation, the file wrapper opens the output only after the in-memory operation returns successfully.

Sources

Not verified

I did not build or run the C++ test locally. The Windows workflow did not run this allocator test; the full build, repeated CTest runs, and scratch negative controls above are author-reported evidence.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5724815330 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14134#pullrequestreview-5724815330). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T20:36:51Z. **Review:** APPROVED --- # Reviewed — looks good The in-memory failure cases check that malformed images remain unchanged, while successful and missing-import file-path cases remain covered. ## What I ran Reviewed head `2f7381f9ea4f258e12415cce50c54d7d08b7062b` against base `0836dfdf4fecb1e0326100c490476e15d3a74da5`; the stack helper's merge preview was clean. Environment: Debian Linux, GCC 12.2.0. ```sh git diff --check upstream/main...refs/remotes/review/pr-14134 python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14134 ``` ## Results | Check | Result | |---|---| | Diff check | Passed; exit 0. | | Conventions and PR template | Passed. | | Dependency and mergeability check | Passed; clean against current base. | | Hosted Windows workflow | Overall job passed, but its log says no guest core/libs test sources changed; it did not run this allocator test. Linux and specialized jobs were skipped. | | `pe_import_first` build and test | The author reports a full Windows build and three exact-head CTest passes (0.03–0.11 s); I did not reproduce them locally. The author also reports two scratch negative controls failed as expected. | <details><summary>Conventions check output</summary> ```text All conventions checks passed. ``` </details> <details><summary>Merge preview output</summary> ```text - PR #14134: **clean** against current base; head `2f7381f9ea4f258e12415cce50c54d7d08b7062b`; changed paths: none reported ``` </details> ## Findings ### Blocking None found in the test-only diff. ### Non-blocking The malformed-image cases no longer exercise the file wrapper, but the unchanged-file case for a missing import and both successful file operations remain. In the reviewed implementation, the file wrapper opens the output only after the in-memory operation returns successfully. ## Sources - [Changed malformed-image assertions at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/2f7381f9ea4f258e12415cce50c54d7d08b7062b/core/libs/allocator/tests/PeImportFirstTests.cpp#L143-L209) - [File wrapper writes only after the in-memory operation succeeds](https://github.com/boykopovar/AnyPS5/blob/2f7381f9ea4f258e12415cce50c54d7d08b7062b/core/libs/allocator/PeImportFirst.cpp#L90-L100) ## Not verified I did not build or run the C++ test locally. The Windows workflow did not run this allocator test; the full build, repeated CTest runs, and scratch negative controls above are author-reported evidence.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!14134
No description provided.