Check section raw data against the end of the file as Windows does #142

Merged
danielradosa merged 4 commits from upstream-pr/143 into main 2026-11-22 16:09:23 +00:00
danielradosa commented 2026-11-21 17:04:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by danielradosa on 2026-11-21T17:04:14Z.


Change

pe_map_image refused any section whose copied raw data ran past the end of the file ("section outside image"), and otherwise trusted the raw range. LoadLibraryExW checks raw data against the file by two rules only:

  • no section's PointerToRawData + SizeOfRawData may reach 4 GiB;
  • the last section in the table, if its SizeOfRawData is nonzero, must have raw data that ends inside the file. This is compared without sector rounding.

A section with SizeOfRawData 0 is not checked. If the last section has no raw data, no section is checked against the file size. The raw data of any other section may run past the end of the file or start after it: Windows maps it, and the missing bytes read as zero.

pe_map now applies the same two rules, with distinct messages, and clamps its sector reads at the end of the file. This closes the open case listed in #111. Refs #46.

Verification

cpu_native_section gains five patched copies of the register probe. In the probe, .rsrc is the last section; .rdata and .pdata are not.

Copy Windows pe_map on main this branch
.rdata raw data running past the file loads, missing bytes zero refuses ("outside image") matches Windows
.pdata raw data after the file loads, zero-filled refuses matches Windows
.pdata no raw data, offset after the file loads refuses matches Windows
.pdata raw range reaching 4 GiB ERROR_BAD_EXE_FORMAT maps refuses ("raw data reaches 4 GiB")
file one byte short, inside the last section .rsrc ERROR_BAD_EXE_FORMAT maps refuses ("last section ... runs past the file")

The six earlier cases still match. The existing "rounded end past the file" case is the control for the truncated file: a shortened file loads when the last section's header ends at the new end of the file.

Windows 11 build 26300.9974, MSVC 19.51.36256, Python 3.14.4:

ctest --preset dev --no-tests=error -R '^cpu_native_section$' -V
  • On main at 47e990b: 5 of 11 cases differ.
  • On this branch: all 11 match.
  • Full suite on this branch: 76 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, fixed the edges of both rules:

  • Raw offsets 0x7ffffe00 and 0xfffffc00 load, while 0xfffffe00 + 0x200 (exactly 2^32) fails.
  • PointerToRawData 0 with SizeOfRawData 0xffffffff loads; 0x10 + 0xfffffff0 fails.
  • A last section with PointerToRawData 0 and SizeOfRawData 0x10000 fails. With SizeOfRawData 0 it loads at any offset.
  • When .rsrc has no raw data, .pdata raw data ending past the file still loads, so the check does not move to an earlier section.

The .rsrc cases kept the resource directory entry intact. Windows also validates resource data on load, so each .rsrc failure above was confirmed with the entry cleared as well.

This covers images with FileAlignment 0x200, as in the source-built fixtures. The FileAlignment 0x1000 case from #111 is still open.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531834256 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/143). Originally posted by [danielradosa](https://github.com/danielradosa) on 2026-11-21T17:04:14Z. --- ## Change `pe_map_image` refused any section whose copied raw data ran past the end of the file ("section outside image"), and otherwise trusted the raw range. `LoadLibraryExW` checks raw data against the file by two rules only: - no section's `PointerToRawData + SizeOfRawData` may reach 4 GiB; - the last section in the table, if its `SizeOfRawData` is nonzero, must have raw data that ends inside the file. This is compared without sector rounding. A section with `SizeOfRawData` 0 is not checked. If the last section has no raw data, no section is checked against the file size. The raw data of any other section may run past the end of the file or start after it: Windows maps it, and the missing bytes read as zero. `pe_map` now applies the same two rules, with distinct messages, and clamps its sector reads at the end of the file. This closes the open case listed in #111. Refs #46. ## Verification `cpu_native_section` gains five patched copies of the register probe. In the probe, `.rsrc` is the last section; `.rdata` and `.pdata` are not. | Copy | Windows | `pe_map` on `main` | this branch | |---|---|---|---| | `.rdata` raw data running past the file | loads, missing bytes zero | refuses ("outside image") | matches Windows | | `.pdata` raw data after the file | loads, zero-filled | refuses | matches Windows | | `.pdata` no raw data, offset after the file | loads | refuses | matches Windows | | `.pdata` raw range reaching 4 GiB | `ERROR_BAD_EXE_FORMAT` | maps | refuses ("raw data reaches 4 GiB") | | file one byte short, inside the last section `.rsrc` | `ERROR_BAD_EXE_FORMAT` | maps | refuses ("last section ... runs past the file") | The six earlier cases still match. The existing "rounded end past the file" case is the control for the truncated file: a shortened file loads when the last section's header ends at the new end of the file. 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 ``` - On `main` at 47e990b: 5 of 11 cases differ. - On this branch: all 11 match. - Full suite on this branch: 76 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, fixed the edges of both rules: - Raw offsets `0x7ffffe00` and `0xfffffc00` load, while `0xfffffe00 + 0x200` (exactly 2^32) fails. - `PointerToRawData` 0 with `SizeOfRawData` `0xffffffff` loads; `0x10 + 0xfffffff0` fails. - A last section with `PointerToRawData` 0 and `SizeOfRawData` 0x10000 fails. With `SizeOfRawData` 0 it loads at any offset. - When `.rsrc` has no raw data, `.pdata` raw data ending past the file still loads, so the check does not move to an earlier section. The `.rsrc` cases kept the resource directory entry intact. Windows also validates resource data on load, so each `.rsrc` failure above was confirmed with the entry cleared as well. This covers images with `FileAlignment` `0x200`, as in the source-built fixtures. The `FileAlignment` `0x1000` case from #111 is still open. _AI-assisted._
upstream-sync closed this pull request 2026-11-24 06:41:32 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:47:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T17:04:26Z.


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-21T17:05:46.225089Z e1aa634 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:7066188778 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/143#issuecomment-7066188778). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T17:04:26Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"e1aa6340cd3ec05d396ca0e42888c79556d5bc9c","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-21T17:05:46.225089Z">2026-11-21T17:05:46.225089Z</relative-time> | `e1aa634` | 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>
R0Y00 commented 2026-11-24 06:47:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T18:16:05Z.


The cancelled Windows job last logged Start 43: cpu_native_section at 17:45:21 UTC and ended at 18:04:34 UTC with no result for that test. The two Linux jobs passed. I retried the Windows job once. Please check the section comparison on the Windows runner and report which named case starts but never returns, with a bounded timeout so the job can identify it. Cancelled Windows job.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067127641 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/143#issuecomment-7067127641). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:16:05Z. --- The cancelled Windows job last logged `Start 43: cpu_native_section` at 17:45:21 UTC and ended at 18:04:34 UTC with no result for that test. The two Linux jobs passed. I retried the Windows job once. Please check the section comparison on the Windows runner and report which named case starts but never returns, with a bounded timeout so the job can identify it. [Cancelled Windows job](https://github.com/R0Y00/Xbox2Windows/actions/runs/44929389054/job/136929498965).
danielradosa commented 2026-11-24 06:48:31 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by danielradosa on 2026-11-22T12:02:47Z.


Thanks for the log. The branch has two updates:

  1. A bounded timeout. Each case prints start <section> <case> before it loads anything, stdout is line-buffered, and cpu_native_section has TIMEOUT 120. If a load never returns, CTest ends the test, and the last start line in its output names the case. As a check, I temporarily set the timeout to 0.3 s: the run ended with ***Timeout, and its output stopped at start .rdata raw data crossing a sector.
  2. Current main merged. The conflict was in main() of tests/cpu/native_section_check.py, and both sides are kept: this PR's refusal count, and the check_protections and check_alignments calls from #418.

A likely cause, not confirmed: the cancelled run on afa72b5 did not yet have the SetErrorMode(SEM_FAILCRITICALERRORS) call that #418 added to this test. This PR's two refusal cases (a raw range reaching 4 GiB, and a file one byte short of the last section) make LoadLibraryExW fail with 193. On the runner, that failure could have opened a Bad Image dialog that nobody dismisses. The next Windows run will show whether that was the cause; if a case still hangs, the log now names it.

Locally, on Windows 11 build 26300.9974, every LoadLibraryExW call in the test returns within 11 to 15 ms. cpu_native_section passes in 0.3 s, covering all 11 cases plus the protection and alignment checks.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7078837505 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/143#issuecomment-7078837505). Originally posted by [danielradosa](https://github.com/danielradosa) on 2026-11-22T12:02:47Z. --- Thanks for the log. The branch has two updates: 1. **A bounded timeout.** Each case prints `start <section> <case>` before it loads anything, stdout is line-buffered, and `cpu_native_section` has `TIMEOUT 120`. If a load never returns, CTest ends the test, and the last `start` line in its output names the case. As a check, I temporarily set the timeout to 0.3 s: the run ended with `***Timeout`, and its output stopped at `start .rdata raw data crossing a sector`. 2. **Current `main` merged.** The conflict was in `main()` of `tests/cpu/native_section_check.py`, and both sides are kept: this PR's refusal count, and the `check_protections` and `check_alignments` calls from #418. A likely cause, not confirmed: the cancelled run on afa72b5 did not yet have the `SetErrorMode(SEM_FAILCRITICALERRORS)` call that #418 added to this test. This PR's two refusal cases (a raw range reaching 4 GiB, and a file one byte short of the last section) make `LoadLibraryExW` fail with 193. On the runner, that failure could have opened a Bad Image dialog that nobody dismisses. The next Windows run will show whether that was the cause; if a case still hangs, the log now names it. Locally, on Windows 11 build 26300.9974, every `LoadLibraryExW` call in the test returns within 11 to 15 ms. `cpu_native_section` passes in 0.3 s, covering all 11 cases plus the protection and alignment checks.
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!142
No description provided.