Map source-built images without LoadLibrary and compare with the lifted calls #49

Merged
Leyukaka merged 2 commits from upstream-pr/49 into main 2026-11-21 14:50:43 +00:00
Leyukaka commented 2026-11-21 13:53:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T13:53:24Z.


Change

Items 1–3 of #46. Item 4 (SEH) is left open for a separate fixture, as requested.

  • tools/pe_map.cpp maps an x64 PE32+ image into its own allocation, away from the preferred base. It applies DIR64 relocations, binds ordinary imports by name or ordinal through a resolver supplied by the host, sets section protections, writes the actual base into the mapped headers and registers .pdata. Images with TLS, delay imports or an entry point are rejected. Header fields are bounds-checked before use.
  • tests/cpu/reloc_route.asm is a new fixture with the same call_mix contract as direct.asm. It calls through an absolute code pointer in .data, so the image has exactly one DIR64 relocation. The existing import fixtures have none.
  • cpu_native_map (tests/cpu/native_map_check.py) reuses the direct_check helpers. The new section in docs/experiments.md is linked from the README CPU row.

Verification

At b102794 rebased on 7c175f2: Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10.

ctest --preset dev --no-tests=error -R '^cpu_native_map$' -V
named: mapped at 0x... (preferred 0x180000000), 0 DIR64 relocation(s), 249 argument pairs match native LoadLibrary, lifted DLL
named with altered shim: ... 249 argument pairs match altered shim reference
cpu_import_original.dll: rejected unbound cpu_import_provider.dll!mix_args, an entry point and an import directory outside the image
ordinal: ... 249 argument pairs match native LoadLibrary, lifted DLL
ordinal with altered shim: ... 249 argument pairs match altered shim reference
cpu_ordinal_original.dll: rejected unbound cpu_ordinal_provider.dll!#77, an entry point and an import directory outside the image
relocated: ... 1 DIR64 relocation(s), 249 argument pairs match native LoadLibrary
relocated with altered shim: ... 249 argument pairs match altered shim reference

The full suite gives 71 tests and 0 failures; only bvh_wave64_hardware and bvh_wave64_warp are skipped (lane range 32..32 / 4..4).

For the named and ordinal cases, the same 249 inputs go through three paths: the Windows-loaded original, the mapped image and the recompiled DLL. Each comparison covers the return value, the three words written by the callee through the guarded trace buffer, and the RBX/RSI sentinels. The unresolved-import case is part of the CTest run.

As a one-off manual check (not in CI), I broke the loader four ways: skipped relocations, skipped the IAT write, skipped .pdata registration and left the preferred ImageBase in the headers. The test failed with a specific assertion in each case.

Evidence and follow-up

All fixtures are built from this repository; there is no game data. Open points:

  • SEH through a mapped image (item 4), which needs a fixture that raises and catches;
  • TLS callbacks and entry-point calls, which are rejected for now;
  • imports are bound by a Python resolver in the test. A native shim table for kernelx/xmem would be the next step if this route proves useful.

Refs #46

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6529677053 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/49). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T13:53:24Z. --- ## Change Items 1–3 of #46. Item 4 (SEH) is left open for a separate fixture, as requested. - `tools/pe_map.cpp` maps an x64 PE32+ image into its own allocation, away from the preferred base. It applies DIR64 relocations, binds ordinary imports by name or ordinal through a resolver supplied by the host, sets section protections, writes the actual base into the mapped headers and registers `.pdata`. Images with TLS, delay imports or an entry point are rejected. Header fields are bounds-checked before use. - `tests/cpu/reloc_route.asm` is a new fixture with the same `call_mix` contract as `direct.asm`. It calls through an absolute code pointer in `.data`, so the image has exactly one DIR64 relocation. The existing import fixtures have none. - `cpu_native_map` (`tests/cpu/native_map_check.py`) reuses the `direct_check` helpers. The new section in `docs/experiments.md` is linked from the README CPU row. ## Verification At `b102794` rebased on `7c175f2`: Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10. ``` ctest --preset dev --no-tests=error -R '^cpu_native_map$' -V named: mapped at 0x... (preferred 0x180000000), 0 DIR64 relocation(s), 249 argument pairs match native LoadLibrary, lifted DLL named with altered shim: ... 249 argument pairs match altered shim reference cpu_import_original.dll: rejected unbound cpu_import_provider.dll!mix_args, an entry point and an import directory outside the image ordinal: ... 249 argument pairs match native LoadLibrary, lifted DLL ordinal with altered shim: ... 249 argument pairs match altered shim reference cpu_ordinal_original.dll: rejected unbound cpu_ordinal_provider.dll!#77, an entry point and an import directory outside the image relocated: ... 1 DIR64 relocation(s), 249 argument pairs match native LoadLibrary relocated with altered shim: ... 249 argument pairs match altered shim reference ``` The full suite gives 71 tests and 0 failures; only `bvh_wave64_hardware` and `bvh_wave64_warp` are skipped (lane range 32..32 / 4..4). For the named and ordinal cases, the same 249 inputs go through three paths: the Windows-loaded original, the mapped image and the recompiled DLL. Each comparison covers the return value, the three words written by the callee through the guarded trace buffer, and the RBX/RSI sentinels. The unresolved-import case is part of the CTest run. As a one-off manual check (not in CI), I broke the loader four ways: skipped relocations, skipped the IAT write, skipped `.pdata` registration and left the preferred `ImageBase` in the headers. The test failed with a specific assertion in each case. ## Evidence and follow-up All fixtures are built from this repository; there is no game data. Open points: - SEH through a mapped image (item 4), which needs a fixture that raises and catches; - TLS callbacks and entry-point calls, which are rejected for now; - imports are bound by a Python resolver in the test. A native shim table for kernelx/xmem would be the next step if this route proves useful. Refs #46 _AI-assisted._
upstream-sync closed this pull request 2026-11-24 06:41:07 +00:00
R0Y00 commented 2026-11-24 06:47:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T14:01:49Z.


Thanks, @Leyukaka! Comparing the Windows-loaded, manually mapped, and recompiled functions on the same inputs gives us a practical way to narrow down future discrepancies. I especially appreciate the altered-provider checks.

Could you make two small fixes in tools/pe_map.cpp and cover them in the regression suite?

  1. In pe_map_image, reject a nonempty import directory whose declared size is smaller than one IMAGE_IMPORT_DESCRIPTOR. Currently the descriptor count rounds down to zero and the image can be accepted with its imports silently skipped. Please add a fixture that expects mapping to return null with MAP_FORMAT before invoking the resolver.
  2. In pe_map_export, return nullptr when the matched export address table slot contains RVA zero. The current bounds check returns the image base for that slot. Please cover a named export pointing to an empty slot and retain a positive lookup for a valid export.

Once those cases are covered and CI passes on the updated commit, we’re happy to merge. Thanks again for the contribution!

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7063766093 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/49#issuecomment-7063766093). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T14:01:49Z. --- Thanks, @Leyukaka! Comparing the Windows-loaded, manually mapped, and recompiled functions on the same inputs gives us a practical way to narrow down future discrepancies. I especially appreciate the altered-provider checks. Could you make two small fixes in `tools/pe_map.cpp` and cover them in the regression suite? 1. In `pe_map_image`, reject a nonempty import directory whose declared size is smaller than one `IMAGE_IMPORT_DESCRIPTOR`. Currently the descriptor count rounds down to zero and the image can be accepted with its imports silently skipped. Please add a fixture that expects mapping to return null with `MAP_FORMAT` before invoking the resolver. 2. In `pe_map_export`, return `nullptr` when the matched export address table slot contains RVA zero. The current bounds check returns the image base for that slot. Please cover a named export pointing to an empty slot and retain a positive lookup for a valid export. Once those cases are covered and CI passes on the updated commit, we’re happy to merge. Thanks again for the contribution!
Leyukaka commented 2026-11-24 06:47:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T14:39:57Z.


Thanks for the review. Both fixes are in, and the branch is rebased on bbdaba7.

  1. pe_map_image now fails with MAP_FORMAT ("import directory smaller than one descriptor") when the import directory is nonempty but smaller than one IMAGE_IMPORT_DESCRIPTOR. In CTest, a copy of each import fixture with the directory size set to 19 bytes must return null with that status, and the resolver must not be called.
  2. pe_map_export returns nullptr when the matched slot holds RVA 0. The test maps a copy of cpu_register_probe.dll with the clobber_control slot zeroed. That name now returns null, while probe in the same mapped image still resolves to its RVA.

Without either fix the test fails: the undersized directory maps with its imports skipped, and the empty slot returns the image base. Locally cpu_native_map passes and the full suite has 72 tests with 0 failures (wave64 skipped on the RTX 5080 and WARP).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7064258590 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/49#issuecomment-7064258590). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T14:39:57Z. --- Thanks for the review. Both fixes are in, and the branch is rebased on `bbdaba7`. 1. `pe_map_image` now fails with `MAP_FORMAT` ("import directory smaller than one descriptor") when the import directory is nonempty but smaller than one `IMAGE_IMPORT_DESCRIPTOR`. In CTest, a copy of each import fixture with the directory size set to 19 bytes must return null with that status, and the resolver must not be called. 2. `pe_map_export` returns `nullptr` when the matched slot holds RVA 0. The test maps a copy of `cpu_register_probe.dll` with the `clobber_control` slot zeroed. That name now returns null, while `probe` in the same mapped image still resolves to its RVA. Without either fix the test fails: the undersized directory maps with its imports skipped, and the empty slot returns the image base. Locally `cpu_native_map` passes and the full suite has 72 tests with 0 failures (wave64 skipped on the RTX 5080 and WARP).
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!49
No description provided.