Zero-fill a mapped section that has no file offset, as Windows does #106

Merged
danielradosa merged 4 commits from upstream-pr/107 into main 2026-11-21 16:34:25 +00:00
danielradosa commented 2026-11-21 16:01:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by danielradosa on 2026-11-21T16:01:40Z.


Change

pe_map_image copied min(SizeOfRawData, VirtualSize) bytes from PointerToRawData even when that pointer was zero. A section with no file offset therefore received the DOS and NT headers from the start of the file. The Windows loader leaves such a section zero-filled. The mapper now copies nothing for a section whose PointerToRawData is zero, so the section keeps the zeroed memory it was allocated with. This is the open case listed at the end of #85. Refs #46.

Verification

The new cpu_native_section test loads the source-built register probe (no entry point, no imports, so no code runs) with LoadLibraryExW and with pe_map, and compares every section byte for byte. It then repeats this on a copy whose .rsrc header has PointerToRawData set to zero and SizeOfRawData unchanged. It also checks that the patch changes only .rsrc and that Windows zero-fills it.

Windows 11 build 26300.9974, MSVC 19.51.36256, Python 3.14.4:

Build Result
main at 88fd8c5 fails: pe_map .rsrc holds file bytes from offset 0, Windows .rsrc differs
this branch passes: 4 of 4 sections match in both the unpatched and patched copies
ctest --preset dev --no-tests=error -R '^cpu_native_section$' -V

Full suite on this branch: 74 passed, 2 skipped (bvh_wave64_*, capability skips on an RTX 4060 and WARP), 0 failed.

Evidence and follow-up

LoadLibraryExW with LOAD_LIBRARY_AS_IMAGE_RESOURCE also zero-fills the patched section. The fixture is the existing cpu_register_probe; the patched copy is made at test time in a temporary directory, so there is no new fixture file.

Correction: an earlier version of this description, and the docs note in the first commit, said LoadLibraryExW rejects a PointerToRawData of 0x100 or 0x1ff because it is below the 0x200 file alignment. Further runs show that Windows rounds a nonzero offset down to a multiple of 0x200 instead. In .rsrc that reads the headers as the resource directory, and that is what fails; with the resource directory entry cleared, the same copy loads. The second commit corrects the docs note. Matching the sector rounding in pe_map is a separate behavior, proposed in #111.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531107778 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/107). Originally posted by [danielradosa](https://github.com/danielradosa) on 2026-11-21T16:01:40Z. --- ## Change `pe_map_image` copied `min(SizeOfRawData, VirtualSize)` bytes from `PointerToRawData` even when that pointer was zero. A section with no file offset therefore received the DOS and NT headers from the start of the file. The Windows loader leaves such a section zero-filled. The mapper now copies nothing for a section whose `PointerToRawData` is zero, so the section keeps the zeroed memory it was allocated with. This is the open case listed at the end of #85. Refs #46. ## Verification The new `cpu_native_section` test loads the source-built register probe (no entry point, no imports, so no code runs) with `LoadLibraryExW` and with `pe_map`, and compares every section byte for byte. It then repeats this on a copy whose `.rsrc` header has `PointerToRawData` set to zero and `SizeOfRawData` unchanged. It also checks that the patch changes only `.rsrc` and that Windows zero-fills it. Windows 11 build 26300.9974, MSVC 19.51.36256, Python 3.14.4: | Build | Result | |---|---| | `main` at 88fd8c5 | fails: `pe_map .rsrc holds file bytes from offset 0, Windows .rsrc differs` | | this branch | passes: 4 of 4 sections match in both the unpatched and patched copies | ```powershell ctest --preset dev --no-tests=error -R '^cpu_native_section$' -V ``` Full suite on this branch: 74 passed, 2 skipped (`bvh_wave64_*`, capability skips on an RTX 4060 and WARP), 0 failed. ## Evidence and follow-up `LoadLibraryExW` with `LOAD_LIBRARY_AS_IMAGE_RESOURCE` also zero-fills the patched section. The fixture is the existing `cpu_register_probe`; the patched copy is made at test time in a temporary directory, so there is no new fixture file. Correction: an earlier version of this description, and the docs note in the first commit, said `LoadLibraryExW` rejects a `PointerToRawData` of `0x100` or `0x1ff` because it is below the `0x200` file alignment. Further runs show that Windows rounds a nonzero offset down to a multiple of `0x200` instead. In `.rsrc` that reads the headers as the resource directory, and that is what fails; with the resource directory entry cleared, the same copy loads. The second commit corrects the docs note. Matching the sector rounding in `pe_map` is a separate behavior, proposed in #111. _AI-assisted._
upstream-sync closed this pull request 2026-11-24 06:41:20 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:47:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T16:01:49Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T16:03:19.648570Z 2ce2b96 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065324248 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/107#issuecomment-7065324248). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T16:01:49Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"2ce2b96f6cb962acfa1ce2188a2b6db6283351de","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-21T16:03:19.648570Z">2026-11-21T16:03:19.648570Z</relative-time> | `2ce2b96` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
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
R0Y00/Xbox2Windows!106
No description provided.