Pin relocation-free mapping away from a reserved preferred base #114

Merged
Leyukaka merged 1 commit from upstream-pr/115 into main 2026-11-21 17:56:46 +00:00
Leyukaka commented 2026-11-21 16:25:55 +00:00 (Migrated from github.com)

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


Change

This adds the CTest requested in #77, which settles the empty-directory case with measurements and leaves pe_map unchanged.

tests/cpu/native_reloc_free_check.py (cpu_native_reloc_free) uses cpu_import_original: no absolute addresses, base-relocation directory Size 0, IMAGE_FILE_RELOCS_STRIPPED clear. I used it rather than the cpu_register_probe from my issue comment because it exports call_mix, so the call can be checked after loading; the register probe is the harness.

Each case runs in its own child process, and:

  • reserves the preferred base (0x180000000) for SizeOfImage, then checks with VirtualQuery that the whole range is held by that reservation;
  • loads the image with LoadLibraryExW and maps it with pe_map;
  • checks that each loader placed it away from the preferred base and that call_mix matches reference() on all 249 input_pairs() (return value, trace words, RBX/RSI).

The test covers:

  • the image as built;
  • a copy with DYNAMIC_BASE cleared;
  • a control without the reservation: Windows loads the DYNAMIC_BASE-cleared copy at 0x180000000. That makes the reserved Windows case the discriminating one, while ASLR would move the as-built copy anyway. pe_map always allocates away from ImageBase; for it, the test shows that an empty directory is accepted at another base and that the image runs there;
  • a RELOCS_STRIPPED control with the base reserved: LoadLibraryExW fails with error 487 (ERROR_INVALID_ADDRESS), and pe_map rejects the copy as "relocations stripped".

The docs get a subsection at the end of the pe_map section.

Verification

At the head of this branch on a46f1ff (Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10):

ctest --preset dev --no-tests=error -R '^cpu_native_reloc_free$' -V
DYNAMIC_BASE cleared / windows, nothing reserved: loaded at its preferred base 0x180000000
as built / windows: preferred 0x180000000 reserved, loaded at 0x7fff..., 249 of 249 call_mix results match
as built / pe_map: preferred 0x180000000 reserved, loaded at 0x2ed..., 249 of 249 call_mix results match
DYNAMIC_BASE cleared / windows: preferred 0x180000000 reserved, loaded at 0x1ea..., 249 of 249 call_mix results match
DYNAMIC_BASE cleared / pe_map: preferred 0x180000000 reserved, loaded at 0x189..., 249 of 249 call_mix results match
RELOCS_STRIPPED set: Windows refused the load with error 487 (ERROR_INVALID_ADDRESS), pe_map rejected it as relocations stripped

The full suite gives 77 tests and 0 failures; only bvh_wave64_hardware and bvh_wave64_warp are skipped.

As a one-off manual check (not in CI), I added the rejection proposed in #77 to pe_map (if (delta && !relocs.Size) → fail). The test then fails with "as built / pe_map: load failed with [4, 'empty relocation directory']".

Evidence and follow-up

The fixture was already built from this repository, and the variants are byte patches written to a temporary directory. There is no game data. The exact bases vary between runs, so only base ≠ preferred is asserted. CI runs Python 3.12; I only ran 3.14 locally.

Refs #77

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531400813 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/115). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T16:25:55Z. --- ## Change This adds the CTest requested in #77, which settles the empty-directory case with measurements and leaves `pe_map` unchanged. `tests/cpu/native_reloc_free_check.py` (`cpu_native_reloc_free`) uses `cpu_import_original`: no absolute addresses, base-relocation directory Size 0, `IMAGE_FILE_RELOCS_STRIPPED` clear. I used it rather than the `cpu_register_probe` from my issue comment because it exports `call_mix`, so the call can be checked after loading; the register probe is the harness. Each case runs in its own child process, and: - reserves the preferred base (`0x180000000`) for `SizeOfImage`, then checks with `VirtualQuery` that the whole range is held by that reservation; - loads the image with `LoadLibraryExW` and maps it with `pe_map`; - checks that each loader placed it away from the preferred base and that `call_mix` matches `reference()` on all 249 `input_pairs()` (return value, trace words, RBX/RSI). The test covers: - the image as built; - a copy with `DYNAMIC_BASE` cleared; - **a control without the reservation:** Windows loads the `DYNAMIC_BASE`-cleared copy at `0x180000000`. That makes the reserved Windows case the discriminating one, while ASLR would move the as-built copy anyway. `pe_map` always allocates away from `ImageBase`; for it, the test shows that an empty directory is accepted at another base and that the image runs there; - **a `RELOCS_STRIPPED` control with the base reserved:** `LoadLibraryExW` fails with error 487 (`ERROR_INVALID_ADDRESS`), and `pe_map` rejects the copy as "relocations stripped". The docs get a subsection at the end of the `pe_map` section. ## Verification At the head of this branch on `a46f1ff` (Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10): ``` ctest --preset dev --no-tests=error -R '^cpu_native_reloc_free$' -V DYNAMIC_BASE cleared / windows, nothing reserved: loaded at its preferred base 0x180000000 as built / windows: preferred 0x180000000 reserved, loaded at 0x7fff..., 249 of 249 call_mix results match as built / pe_map: preferred 0x180000000 reserved, loaded at 0x2ed..., 249 of 249 call_mix results match DYNAMIC_BASE cleared / windows: preferred 0x180000000 reserved, loaded at 0x1ea..., 249 of 249 call_mix results match DYNAMIC_BASE cleared / pe_map: preferred 0x180000000 reserved, loaded at 0x189..., 249 of 249 call_mix results match RELOCS_STRIPPED set: Windows refused the load with error 487 (ERROR_INVALID_ADDRESS), pe_map rejected it as relocations stripped ``` The full suite gives 77 tests and 0 failures; only `bvh_wave64_hardware` and `bvh_wave64_warp` are skipped. As a one-off manual check (not in CI), I added the rejection proposed in #77 to `pe_map` (`if (delta && !relocs.Size)` → fail). The test then fails with "as built / pe_map: load failed with [4, 'empty relocation directory']". ## Evidence and follow-up The fixture was already built from this repository, and the variants are byte patches written to a temporary directory. There is no game data. The exact bases vary between runs, so only base ≠ preferred is asserted. CI runs Python 3.12; I only ran 3.14 locally. Refs #77 _AI-assisted._
upstream-sync closed this pull request 2026-11-24 06:41:23 +00:00
R0Y00 commented 2026-11-24 06:47:41 +00:00 (Migrated from github.com)

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


The relocation-free comparison addresses #77. GitHub now reports conflicts with main. Please sync the branch, retain the newly registered CPU fixtures, and rerun cpu_native_reloc_free before re-review.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066371697 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/115#issuecomment-7066371697). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T17:18:06Z. --- The relocation-free comparison addresses #77. GitHub now reports conflicts with main. Please sync the branch, retain the newly registered CPU fixtures, and rerun cpu_native_reloc_free before re-review.
Leyukaka commented 2026-11-24 06:47:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T17:26:10Z.


Synced on 86c927a. The only conflicts were in CMakeLists.txt and docs/experiments.md, and both were resolved without dropping anything:

  • cpu_native_map keeps its new --two argument (cpu_two_imports), and cpu_native_reloc_free is registered after it.
  • The new subsection stays at the end of the pe_map section, before "Switch dispatch through jump tables".

Rerun on Windows 11 build 26300:

ctest --preset dev --no-tests=error -R '^cpu_native_reloc_free$' -V
DYNAMIC_BASE cleared / windows, nothing reserved: loaded at its preferred base 0x180000000
as built / windows: preferred 0x180000000 reserved, loaded at 0x7fff..., 249 of 249 call_mix results match
as built / pe_map: preferred 0x180000000 reserved, loaded at 0x1a1..., 249 of 249 call_mix results match
DYNAMIC_BASE cleared / windows: preferred 0x180000000 reserved, loaded at 0x21e..., 249 of 249 call_mix results match
DYNAMIC_BASE cleared / pe_map: preferred 0x180000000 reserved, loaded at 0x218..., 249 of 249 call_mix results match
RELOCS_STRIPPED set: Windows refused the load with error 487 (ERROR_INVALID_ADDRESS), pe_map rejected it as relocations stripped

All five cpu_native_* tests pass, including cpu_native_section, and the full suite gives 83 tests and 0 failures; only the two wave64 tests are skipped.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066479831 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/115#issuecomment-7066479831). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T17:26:10Z. --- Synced on `86c927a`. The only conflicts were in `CMakeLists.txt` and `docs/experiments.md`, and both were resolved without dropping anything: - `cpu_native_map` keeps its new `--two` argument (`cpu_two_imports`), and `cpu_native_reloc_free` is registered after it. - The new subsection stays at the end of the `pe_map` section, before "Switch dispatch through jump tables". Rerun on Windows 11 build 26300: ``` ctest --preset dev --no-tests=error -R '^cpu_native_reloc_free$' -V DYNAMIC_BASE cleared / windows, nothing reserved: loaded at its preferred base 0x180000000 as built / windows: preferred 0x180000000 reserved, loaded at 0x7fff..., 249 of 249 call_mix results match as built / pe_map: preferred 0x180000000 reserved, loaded at 0x1a1..., 249 of 249 call_mix results match DYNAMIC_BASE cleared / windows: preferred 0x180000000 reserved, loaded at 0x21e..., 249 of 249 call_mix results match DYNAMIC_BASE cleared / pe_map: preferred 0x180000000 reserved, loaded at 0x218..., 249 of 249 call_mix results match RELOCS_STRIPPED set: Windows refused the load with error 487 (ERROR_INVALID_ADDRESS), pe_map rejected it as relocations stripped ``` All five `cpu_native_*` tests pass, including `cpu_native_section`, and the full suite gives 83 tests and 0 failures; only the two wave64 tests are skipped. _AI-assisted._
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!114
No description provided.