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

Manually merged
github merged 4 commits from gh-pr/107/head into gh-pr/107/base 2026-11-26 21:27:23 +00:00
Owner

@⁠danielradosa on GitHub · 2026-11-21 16:01 UTC · original
danielradosa:pe-map-zero-raw-pointer → main · +117 −1 · 4 files · 4 commits · merged 2026-11-21 16:34 UTC

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.

**@⁠danielradosa** on GitHub · 2026-11-21 16:01 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/107) `danielradosa:pe-map-zero-raw-pointer` → `main` · +117 −1 · 4 files · 4 commits · merged 2026-11-21 16:34 UTC ## 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._
pe_map copied min(SizeOfRawData, VirtualSize) bytes from PointerToRawData
even when that pointer was zero, so a section with no file offset received
the DOS and NT headers. LoadLibraryExW leaves such a section zero-filled.

cpu_native_section loads the register probe with LoadLibraryExW and with
pe_map and compares every section byte for byte, then repeats this on a
copy whose .rsrc PointerToRawData is zero. Before the change the mapped
.rsrc held the bytes from file offset 0; after it, all four sections match
the Windows loader.

Refs #46
The note said LoadLibraryExW rejects a PointerToRawData of 0x100 or 0x1ff
because it is below the 0x200 file alignment. Further runs show Windows
rounds the offset down to 0x200 instead: .rsrc then reads the headers
from offset 0 as its resource directory, and that is what fails. With the
resource directory entry cleared, the same copy loads.

Refs #46
# Conflicts:
#	CMakeLists.txt
#	docs/experiments.md
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:27:21 +00:00
github manually merged commit b8bd6e806c into gh-pr/107/base 2026-11-26 21:27:23 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 16:01 UTC · original · edited 2026-11-21 16:03 UTC

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.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 16:01 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/107#issuecomment-7065324248) · edited 2026-11-21 16:03 UTC <!-- 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>
This discussion has been locked. Commenting is limited to contributors.
No description provided.