Read section file data in whole 0x200-byte sectors, as Windows does #111

Manually merged
github merged 3 commits from gh-pr/111/head into gh-pr/111/base 2026-11-26 21:27:39 +00:00
Owner

@⁠danielradosa on GitHub · 2026-11-21 16:17 UTC · original
danielradosa:pe-map-sector-reads → main · +72 −28 · 3 files · 3 commits · merged 2026-11-21 16:36 UTC

Change

pe_map_image copied min(SizeOfRawData, VirtualSize) bytes starting exactly at PointerToRawData. LoadLibraryExW reads raw section data in whole 0x200-byte file sectors instead. It rounds PointerToRawData down and PointerToRawData + SizeOfRawData up to a multiple of 0x200, copies that range up to the section's virtual size, and leaves bytes past the end of the file zero. pe_map now reads the same range. A PointerToRawData of zero still maps nothing. Refs #46.

This builds on #107. The first two commits belong to #107; this PR's change is the last commit, which extends the cpu_native_section test from #107.

Verification

cpu_native_section compares every section of the register probe between LoadLibraryExW and pe_map, on the original and on six patched copies. Each rounding case is also checked to differ from an exact read of the header fields, so the test fails if pe_map goes back to reading exactly at PointerToRawData.

Patched copy pe_map from #107 this PR
.rsrc PointerToRawData 0 match match
.rdata start inside a sector (PointerToRawData + 0x10) differs at byte 0x0 match
.rdata start in the first sector (PointerToRawData 0x100) differs at byte 0x0 match
.rdata raw size below a sector (SizeOfRawData 0x10) differs at byte 0x10 match
.rdata raw data crossing a sector (start + 0x1f0, SizeOfRawData 0x20) differs at byte 0x0 match
.rsrc rounded end past the file (VirtualSize 0x200, SizeOfRawData 0x1f0, file 0x10 bytes shorter) match match

In every differing case, the section pe_map mapped equals the exact read. Windows 11 build 26300.9974, MSVC 19.51.36256, Python 3.14.4:

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

Exploratory runs, not part of the test, separated the sector size from FileAlignment. With FileAlignment patched to 0x400 or 0x1000, Windows still rounded the start down to 0x200. For a raw size below one sector, Windows read to the next 0x200 boundary with FileAlignment 0x400, but with 0x1000 it read further, at least to the 0x219-byte virtual size of .rdata. The end rule for FileAlignment 0x1000 and above is open for another contributor.

The rounding cases patch .rdata. In .rsrc, a rounded start can put other data where the resource directory should be, and LoadLibraryExW then fails with ERROR_BAD_EXE_FORMAT.

Also open: pe_map rejects a section whose declared raw data runs past the end of the file ("section outside image"), while LoadLibraryExW maps it and zero-fills the missing bytes. Examples: .pdata with PointerToRawData 0xe00 in a 0xe00-byte file, or .rdata at 0xc00 with a 0x219-byte virtual size and 0x200 bytes left in the file. This PR does not change that.

AI-assisted.

**@⁠danielradosa** on GitHub · 2026-11-21 16:17 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/111) `danielradosa:pe-map-sector-reads` → `main` · +72 −28 · 3 files · 3 commits · merged 2026-11-21 16:36 UTC ## Change `pe_map_image` copied `min(SizeOfRawData, VirtualSize)` bytes starting exactly at `PointerToRawData`. `LoadLibraryExW` reads raw section data in whole `0x200`-byte file sectors instead. It rounds `PointerToRawData` down and `PointerToRawData + SizeOfRawData` up to a multiple of `0x200`, copies that range up to the section's virtual size, and leaves bytes past the end of the file zero. `pe_map` now reads the same range. A `PointerToRawData` of zero still maps nothing. Refs #46. This builds on #107. The first two commits belong to #107; this PR's change is the last commit, which extends the `cpu_native_section` test from #107. ## Verification `cpu_native_section` compares every section of the register probe between `LoadLibraryExW` and `pe_map`, on the original and on six patched copies. Each rounding case is also checked to differ from an exact read of the header fields, so the test fails if `pe_map` goes back to reading exactly at `PointerToRawData`. | Patched copy | `pe_map` from #107 | this PR | |---|---|---| | `.rsrc` `PointerToRawData` 0 | match | match | | `.rdata` start inside a sector (`PointerToRawData` + 0x10) | differs at byte 0x0 | match | | `.rdata` start in the first sector (`PointerToRawData` 0x100) | differs at byte 0x0 | match | | `.rdata` raw size below a sector (`SizeOfRawData` 0x10) | differs at byte 0x10 | match | | `.rdata` raw data crossing a sector (start + 0x1f0, `SizeOfRawData` 0x20) | differs at byte 0x0 | match | | `.rsrc` rounded end past the file (`VirtualSize` 0x200, `SizeOfRawData` 0x1f0, file 0x10 bytes shorter) | match | match | In every differing case, the section `pe_map` mapped equals the exact read. Windows 11 build 26300.9974, MSVC 19.51.36256, Python 3.14.4: ```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 Exploratory runs, not part of the test, separated the sector size from `FileAlignment`. With `FileAlignment` patched to `0x400` or `0x1000`, Windows still rounded the start down to `0x200`. For a raw size below one sector, Windows read to the next `0x200` boundary with `FileAlignment` `0x400`, but with `0x1000` it read further, at least to the `0x219`-byte virtual size of `.rdata`. The end rule for `FileAlignment` `0x1000` and above is open for another contributor. The rounding cases patch `.rdata`. In `.rsrc`, a rounded start can put other data where the resource directory should be, and `LoadLibraryExW` then fails with `ERROR_BAD_EXE_FORMAT`. Also open: `pe_map` rejects a section whose declared raw data runs past the end of the file ("section outside image"), while `LoadLibraryExW` maps it and zero-fills the missing bytes. Examples: `.pdata` with `PointerToRawData` 0xe00 in a 0xe00-byte file, or `.rdata` at 0xc00 with a 0x219-byte virtual size and 0x200 bytes left in the file. This PR does not change that. _AI-assisted._
pe_map copied min(SizeOfRawData, VirtualSize) bytes starting exactly at
PointerToRawData. LoadLibraryExW rounds PointerToRawData down and the end
of the raw data up to a multiple of 0x200, copies that range up to the
virtual size, and leaves bytes past the end of the file zero. pe_map now
reads the same range; a PointerToRawData of zero still maps nothing.

cpu_native_section gains five patched copies: a start inside a sector, a
start in the first sector, a raw size below one sector, raw data crossing
a sector boundary, and a file shortened so the rounded end of .rsrc lies
past it. Before the change the first four failed, each holding an exact
read of the header fields; after it, all four sections match the Windows
loader in every case.

Refs #46
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:27:37 +00:00
github manually merged commit 3074f34a41 into gh-pr/111/base 2026-11-26 21:27:39 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 16:18 UTC · original · edited 2026-11-21 16:20 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:20:55.281090Z b67071a 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:18 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/111#issuecomment-7065542902) · edited 2026-11-21 16:20 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"b67071a5a5968a2a06957373b81ccf0cdb5e645d","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:20:55.281090Z">2026-11-21T16:20:55.281090Z</relative-time> | `b67071a` | 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.