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

Merged
danielradosa merged 3 commits from upstream-pr/111 into main 2026-11-21 16:36:28 +00:00
danielradosa commented 2026-11-21 16:17:59 +00:00 (Migrated from github.com)

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


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531313566 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/111). Originally posted by [danielradosa](https://github.com/danielradosa) on 2026-11-21T16:17:59Z. --- ## 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._
upstream-sync closed this pull request 2026-11-24 06:41:21 +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:18:15Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065542902 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/111#issuecomment-7065542902). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T16:18:15Z. --- <!-- 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>
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!110
No description provided.