pe_map: treat a relocation directory at address 0 as absent #170

Merged
Vonsek merged 2 commits from upstream-pr/171 into main 2026-11-22 03:52:44 +00:00
Vonsek commented 2026-11-21 17:55:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T17:55:21Z.


Change

Refs #77.

The thread on #77 settled on keeping Windows-compatible behavior for an empty relocation directory, and #115 pins that case. This PR covers a related case that neither #115 nor #122 handles: a base-relocation directory whose VirtualAddress is 0 but whose Size is not.

The Windows loader finds the directory with RtlImageDirectoryEntryToData, which returns nothing for an address of 0 whatever the Size, so the image counts as relocation-free. pe_map checked only that the range fit in the image. It then parsed the headers at RVA 0 as relocation blocks: the MZ signature became a block with SizeOfBlock 3, and the image was rejected as "bad relocation block".

  • tools/pe_map.cpp: a relocation directory at address 0 is treated as empty. The IMAGE_FILE_RELOCS_STRIPPED check and the block walk are unchanged, so this does not overlap #122's block-size and trailing-byte checks.
  • tests/cpu/native_map_check.py: check_reloc_address_zero patches the directory address of a reloc_route copy to 0 and keeps the linked Size. It reserves the preferred base, loads the copy with LoadLibraryExW and maps it with pe_map. Both must place it away from the preferred base and leave the single DIR64 pointer at its linked value. The copy has no entry point, so nothing in it runs. The new check is called next to check_delay_directory and needs no new STATUS entries.
  • docs/experiments.md: one paragraph in the pe_map section.

Verification

  • Windows: ctest --preset dev --no-tests=error -R '^cpu_native_map$' -V at 00a36b6 on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100: passed, printing cpu_reloc_original.dll: relocation directory at address 0 with Size 12 is ignored by LoadLibraryExW and pe_map; both map away from 0x180000000 and leave the DIR64 entry at 0x180001000. The full ctest --preset dev --no-tests=error passed 86/86, with bvh_wave64_hardware, bvh_wave64_warp and wave_shuffle_warp skipped.
  • Linux, before and after: tools/pe_map.cpp was compiled with clang++ against a minimal windows.h stand-in (mmap-backed VirtualAlloc, stdio file reads). The fixture was a source-built DLL (clang --target=x86_64-pc-windows-msvc plus lld-link /dll /noentry) with one exported function pointer in .data, so it has a single DIR64 relocation. The relocation directory is at 0x4000 with Size 12, and ImageBase is 0x180000000.
    • main 8ac95cf: the original maps and relocates the pointer. The copy with the directory address 0 fails with status 4, "bad relocation block".
    • this branch: the original is unchanged. The address-0 copy maps away from 0x180000000 with status 0, and the pointer keeps its linked 0x180001000. A control with the same copy and IMAGE_FILE_RELOCS_STRIPPED set is still rejected with "relocations stripped".

Evidence and follow-up

  • The Windows loader behavior was measured by the LoadLibraryExW comparison in cpu_native_map on Windows 11 build 26200: the copy with the relocation directory address 0 and Size 12 loads away from the reserved preferred base, and its DIR64 pointer keeps the linked value, so the loader treats the image as having no relocations.
  • If the preferred base cannot be reserved because part of it is already in use, the test continues. Either way, both resulting bases are checked against the preferred base.
  • Possible conflict with #122: it adds a call at the end of main() in the same test file, while this PR adds one near the top. In pe_map.cpp, the two changes touch neighboring but different lines.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532405722 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/171). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T17:55:21Z. --- ## Change Refs #77. The thread on #77 settled on keeping Windows-compatible behavior for an empty relocation directory, and #115 pins that case. This PR covers a related case that neither #115 nor #122 handles: a base-relocation directory whose `VirtualAddress` is 0 but whose `Size` is not. The Windows loader finds the directory with `RtlImageDirectoryEntryToData`, which returns nothing for an address of 0 whatever the `Size`, so the image counts as relocation-free. `pe_map` checked only that the range fit in the image. It then parsed the headers at RVA 0 as relocation blocks: the `MZ` signature became a block with `SizeOfBlock` 3, and the image was rejected as "bad relocation block". - `tools/pe_map.cpp`: a relocation directory at address 0 is treated as empty. The `IMAGE_FILE_RELOCS_STRIPPED` check and the block walk are unchanged, so this does not overlap #122's block-size and trailing-byte checks. - `tests/cpu/native_map_check.py`: `check_reloc_address_zero` patches the directory address of a `reloc_route` copy to 0 and keeps the linked `Size`. It reserves the preferred base, loads the copy with `LoadLibraryExW` and maps it with `pe_map`. Both must place it away from the preferred base and leave the single DIR64 pointer at its linked value. The copy has no entry point, so nothing in it runs. The new check is called next to `check_delay_directory` and needs no new `STATUS` entries. - `docs/experiments.md`: one paragraph in the `pe_map` section. ## Verification - Windows: `ctest --preset dev --no-tests=error -R '^cpu_native_map$' -V` at 00a36b6 on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100: passed, printing `cpu_reloc_original.dll: relocation directory at address 0 with Size 12 is ignored by LoadLibraryExW and pe_map; both map away from 0x180000000 and leave the DIR64 entry at 0x180001000`. The full `ctest --preset dev --no-tests=error` passed 86/86, with `bvh_wave64_hardware`, `bvh_wave64_warp` and `wave_shuffle_warp` skipped. - Linux, before and after: `tools/pe_map.cpp` was compiled with clang++ against a minimal `windows.h` stand-in (mmap-backed `VirtualAlloc`, stdio file reads). The fixture was a source-built DLL (`clang --target=x86_64-pc-windows-msvc` plus `lld-link /dll /noentry`) with one exported function pointer in `.data`, so it has a single DIR64 relocation. The relocation directory is at 0x4000 with Size 12, and ImageBase is 0x180000000. - main 8ac95cf: the original maps and relocates the pointer. The copy with the directory address 0 fails with status 4, "bad relocation block". - this branch: the original is unchanged. The address-0 copy maps away from 0x180000000 with status 0, and the pointer keeps its linked 0x180001000. A control with the same copy and `IMAGE_FILE_RELOCS_STRIPPED` set is still rejected with "relocations stripped". ## Evidence and follow-up - The Windows loader behavior was measured by the `LoadLibraryExW` comparison in `cpu_native_map` on Windows 11 build 26200: the copy with the relocation directory address 0 and Size 12 loads away from the reserved preferred base, and its DIR64 pointer keeps the linked value, so the loader treats the image as having no relocations. - If the preferred base cannot be reserved because part of it is already in use, the test continues. Either way, both resulting bases are checked against the preferred base. - Possible conflict with #122: it adds a call at the end of `main()` in the same test file, while this PR adds one near the top. In `pe_map.cpp`, the two changes touch neighboring but different lines.
upstream-sync closed this pull request 2026-11-24 06:41:39 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:47:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T18:17:09Z.


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-21T18:19:30.337620Z 00a36b6 Draft marked ready
ℹ️ 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:7067141356 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/171#issuecomment-7067141356). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T18:17:09Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"00a36b6db850bc6efe82feffece0ab191f60b1d4","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-21T18:19:30.337620Z">2026-11-21T18:19:30.337620Z</relative-time> | `00a36b6` | Draft marked ready | <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:48 +00:00 (Migrated from github.com)

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


The loader comparison is clear. Unlike the position-independent #115 control, this fixture retains an absolute pointer at the linked address after the image is mapped elsewhere. The observable success/failure policy is with the lead developer in #199.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067177242 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/171#issuecomment-7067177242). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T18:20:03Z. --- The loader comparison is clear. Unlike the position-independent #115 control, this fixture retains an absolute pointer at the linked address after the image is mapped elsewhere. The observable success/failure policy is with the lead developer in #199.
Leyukaka commented 2026-11-24 06:47:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T19:25:09Z.


Two observations from reading the diff at 00a36b6, without building it:

  1. The docs and the PR description disagree about the Windows run. The new paragraph in docs/experiments.md says "The Windows half of this comparison has not been run yet" and describes a Linux build against a windows.h stand-in. The description reports the Windows cpu_native_map run on build 26200, which printed the success line. If the Windows run is the evidence, the paragraph is out of date.

  2. A Size larger than the image now maps without a test. On main, inside(relocs.VirtualAddress, relocs.Size, opt.SizeOfImage) rejects a directory at address 0 whose Size exceeds SizeOfImage, with "relocation directory outside image". With the substitution, relocs is the empty directory for any Size, so that image now maps. The test keeps the linked Size, so it does not exercise this case. Whatever the success/failure policy settles on, a copy with address 0 and Size = SizeOfImage + 1, run under both LoadLibraryExW and pe_map, would pin it down.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068006174 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/171#issuecomment-7068006174). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T19:25:09Z. --- Two observations from reading the diff at `00a36b6`, without building it: 1. **The docs and the PR description disagree about the Windows run.** The new paragraph in `docs/experiments.md` says "The Windows half of this comparison has not been run yet" and describes a Linux build against a `windows.h` stand-in. The description reports the Windows `cpu_native_map` run on build 26200, which printed the success line. If the Windows run is the evidence, the paragraph is out of date. 2. **A Size larger than the image now maps without a test.** On `main`, `inside(relocs.VirtualAddress, relocs.Size, opt.SizeOfImage)` rejects a directory at address 0 whose `Size` exceeds `SizeOfImage`, with "relocation directory outside image". With the substitution, `relocs` is the empty directory for any `Size`, so that image now maps. The test keeps the linked `Size`, so it does not exercise this case. Whatever the success/failure policy settles on, a copy with address 0 and `Size = SizeOfImage + 1`, run under both `LoadLibraryExW` and `pe_map`, would pin it down. _AI-assisted._
R0Y00 commented 2026-11-24 06:48:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T03:50:00Z.


I verified both review cases with the Windows loader and pe_map, using the same source-built image and a reserved preferred base. With relocation RVA 0, both the linked size (12) and SizeOfImage+1 (28673) mapped elsewhere and retained the linked DIR64 value. For this API we match the observed Windows mapping behavior. The test only reads the slot. I added the oversized case and replaced the old experiment note with these results.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073810850 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/171#issuecomment-7073810850). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T03:50:00Z. --- I verified both review cases with the Windows loader and pe_map, using the same source-built image and a reserved preferred base. With relocation RVA 0, both the linked size (12) and SizeOfImage+1 (28673) mapped elsewhere and retained the linked DIR64 value. For this API we match the observed Windows mapping behavior. The test only reads the slot. I added the oversized case and replaced the old experiment note with these results.
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!170
No description provided.