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

Manually merged
github merged 2 commits from gh-pr/49/head into gh-pr/49/base 2026-11-26 21:24:13 +00:00
Owner

@⁠Leyukaka on GitHub · 2026-11-21 13:53 UTC · original
Leyukaka:native-pe-map → main · +554 −1 · 7 files · 2 commits · merged 2026-11-21 14:50 UTC

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.

**@⁠Leyukaka** on GitHub · 2026-11-21 13:53 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/49) `Leyukaka:native-pe-map` → `main` · +554 −1 · 7 files · 2 commits · merged 2026-11-21 14:50 UTC ## 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._
Add tools/pe_map.cpp, a small loader that maps an x64 PE away from its
preferred base, applies DIR64 relocations, binds imports through a host
resolver and registers .pdata. The cpu_native_map test runs the named and
ordinal import fixtures and a new relocation fixture through the mapped
image, the Windows-loaded original and the lifted DLL.

Refs #46
An import directory smaller than one descriptor now fails with MAP_FORMAT
before the resolver runs, and pe_map_export returns null for an export
address slot holding RVA 0. cpu_native_map covers both with damaged copies
of the fixtures and keeps a positive lookup in the same image.

Refs #46
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:24:08 +00:00
github manually merged commit 10c4136c75 into gh-pr/49/base 2026-11-26 21:24:13 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 14:01 UTC · original

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!

**@⁠R0Y00** on GitHub · 2026-11-21 14:01 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/49#issuecomment-7063766093) 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!
Author
Owner

@⁠Leyukaka on GitHub · 2026-11-21 14:39 UTC · original

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).

**@⁠Leyukaka** on GitHub · 2026-11-21 14:39 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/49#issuecomment-7064258590) 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).
This discussion has been locked. Commenting is limited to contributors.
No description provided.