PeImportFirst rewrites malformed import metadata #11286
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 boykopovar/AnyPS5. Originally posted by malekaifa on 2026-10-26T00:06:54Z.
What happens
core/libs/allocator/PeImportFirst.cppcan rewrite malformed PE32+ import metadata instead of rejecting it before the in-place update.Two deterministic cases reproduce on current
mainacb900c194325b9ee8c23c10768a9a491454f0b2:NumberOfRvaAndSizesis0or1, stale nonzero bytes in the optional-header slot where Import Directory entry #1 would normally live are still read as an import RVA. A synthetic image with valid descriptor bytes at that RVA exits successfully and has its import descriptors reordered even though the PE declares no Import Directory entry.MoveImportFirstInFileis an in-place build-time PE rewriter, and its existing tests already require malformed inputs to fail without changing the file. These cases violate that boundary and can hide a damaged artifact by modifying it further.Reproduction
Repository-only synthetic PE fixtures are sufficient; no title, GPU or external binary is involved.
For the directory-count case, start from the existing
PeImportFirstTests.cppimage forkernel32.dll+mimalloc-redirect.dll, set the PE32+NumberOfRvaAndSizesfield at optional-header offset+108to0(or1), leave the stale import-directory RVA/descriptors in place, and callMoveImportFirstInFile(..., mimalloc-redirect.dll).Current result: success and descriptor reordering. Expected: failure such as
the image has no imports, with the file byte-for-byte unchanged.For the unterminated-name case, start from the same image and truncate the file immediately after the bytes of the final
mimalloc-redirect.dllname, removing its terminating NUL while leaving the descriptor and its Name RVA intact.Current result: success and descriptor reordering. Expected: failure such as
unterminated import name, with the file byte-for-byte unchanged.Both current-main failures were reproduced independently before preparing the fix.
Proposed fix
NumberOfRvaAndSizes >= 2before reading it.failsWithoutTouchingTheFile(...)helper for regression coverage of directory counts0/1, a too-short optional header, and an unterminated DLL name.A tested candidate is published at
malekaifa/AnyPS5:fix/pe-import-directory-count(21d93e844c7ba135459961f85124af9d883fc318). The focusedPeImportFirstTests.cppexecutable passes under WSL/g++ andtools/check_conventions.py --base origin/mainplusgit diff --checkpass. No independent GPT-6.1 review has been run yet; that is deferred until this becomes the PR being submitted.AI-assisted: yes (OpenAI Codex).