Walk import descriptors to the terminator in pe_map, as Windows does #84

Merged
Vonsek merged 3 commits from upstream-pr/85 into main 2026-11-21 17:05:06 +00:00
Vonsek commented 2026-11-21 15:41:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T15:41:15Z.


Change

pe_map_image used the import directory Size to decide whether an image has imports and to bound the descriptor walk. The Windows loader finds the table by its address and walks to the null descriptor, so copies of a source-built fixture with Size 0, 19 or 20 still load and bind every slot. pe_map rejected the first two ("import directory smaller than one descriptor") and left the slots of a descriptor past Size unbound.

pe_map now treats a directory with an address as present and walks it to the null descriptor, bounds-checking each descriptor against the image. IAT writes may not overlap the walked table (terminator included). An import directory that runs past the image is still rejected. Refs #46.

Verification

Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, at 4cac5e6:

ctest --preset dev --no-tests=error -R '^cpu_native_map$' -V
cpu_two_imports.dll: import directory Size 60, 0, 19 and 20 bind both slots under LoadLibraryExW and pe_map
1/1 Test #59: cpu_native_map ...................   Passed

The full ctest --preset dev --no-tests=error passed 78/78 (bvh_wave64_hardware and bvh_wave64_warp skipped, lane range 32..32), including cpu_native_seh, cpu_native_section and cpu_native_tls.

With the previous pe_map, the new check fails with Size 0: unexpected import requests []: no import is requested and both slots keep their hint/name RVAs, while Windows binds both.

Evidence and follow-up

tests/cpu/two_imports.asm is new source (MIT): two jump stubs that import mix_args from cpu_import_provider and probe from cpu_register_probe. It has no entry point, and the test only reads the two IAT slots after LoadLibraryExW or pe_map; nothing in the image runs. The test patches the import directory Size of a temporary copy to 60 (as built), 0, 19 and 20 (ending after the first descriptor), and requires each slot to hold the export of its provider and pe_map to request exactly the two imports.

The size-19 rejection control in check_rejections is dropped, because Windows binds that copy. The entry-point control from #78 and the outside-image control are unchanged. Thanks to @Leyukaka for the LoadLibraryExW measurements on build 26300, which this matches on 26200.

The TLS and delay-import directory checks still test Size; #103 covers them. Docs: docs/experiments.md#mapping-an-image-without-loadlibrary.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6530894314 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/85). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T15:41:15Z. --- ## Change `pe_map_image` used the import directory `Size` to decide whether an image has imports and to bound the descriptor walk. The Windows loader finds the table by its address and walks to the null descriptor, so copies of a source-built fixture with `Size` 0, 19 or 20 still load and bind every slot. `pe_map` rejected the first two ("import directory smaller than one descriptor") and left the slots of a descriptor past `Size` unbound. `pe_map` now treats a directory with an address as present and walks it to the null descriptor, bounds-checking each descriptor against the image. IAT writes may not overlap the walked table (terminator included). An import directory that runs past the image is still rejected. Refs #46. ## Verification Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, at 4cac5e6: ``` ctest --preset dev --no-tests=error -R '^cpu_native_map$' -V cpu_two_imports.dll: import directory Size 60, 0, 19 and 20 bind both slots under LoadLibraryExW and pe_map 1/1 Test #59: cpu_native_map ................... Passed ``` The full `ctest --preset dev --no-tests=error` passed 78/78 (`bvh_wave64_hardware` and `bvh_wave64_warp` skipped, lane range 32..32), including `cpu_native_seh`, `cpu_native_section` and `cpu_native_tls`. With the previous `pe_map`, the new check fails with `Size 0: unexpected import requests []`: no import is requested and both slots keep their hint/name RVAs, while Windows binds both. ## Evidence and follow-up `tests/cpu/two_imports.asm` is new source (MIT): two jump stubs that import `mix_args` from `cpu_import_provider` and `probe` from `cpu_register_probe`. It has no entry point, and the test only reads the two IAT slots after `LoadLibraryExW` or `pe_map`; nothing in the image runs. The test patches the import directory `Size` of a temporary copy to 60 (as built), 0, 19 and 20 (ending after the first descriptor), and requires each slot to hold the export of its provider and `pe_map` to request exactly the two imports. The size-19 rejection control in `check_rejections` is dropped, because Windows binds that copy. The entry-point control from #78 and the outside-image control are unchanged. Thanks to @Leyukaka for the `LoadLibraryExW` measurements on build 26300, which this matches on 26200. The TLS and delay-import directory checks still test `Size`; #103 covers them. Docs: `docs/experiments.md#mapping-an-image-without-loadlibrary`.
upstream-sync closed this pull request 2026-11-24 06:41:15 +00:00
R0Y00 commented 2026-11-24 06:47:35 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T15:46:39Z.


Thanks for the import-directory checks. #78 also touches the mapper and native-map tests. Please keep this draft until the branches are reconciled, then update the verification section with the Windows result.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065134907 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/85#issuecomment-7065134907). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T15:46:39Z. --- Thanks for the import-directory checks. #78 also touches the mapper and native-map tests. Please keep this draft until the branches are reconciled, then update the verification section with the Windows result.
Leyukaka commented 2026-11-24 06:47:36 +00:00 (Migrated from github.com)

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


Thanks. I wrote pe_map, so I checked the Windows side your verification section leaves open. I patched only the import-directory Size field of cpu_import_original.dll (import directory at RVA 0x22b4, Size 40, one descriptor plus terminator, IAT slot at RVA 0x2000). Each copy was loaded with LoadLibraryExW(path, NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS), with cpu_import_provider.dll next to it. I only read the IAT slot afterwards; nothing in the image was called. Windows 11 build 26300:

Import directory Size LoadLibraryExW IAT slot after load
40 (as built) loaded mix_args from the provider
0 loaded mix_args from the provider
19 loaded mix_args from the provider
20 (terminator outside) loaded mix_args from the provider

This confirms your point that the loader does not use Size for the import walk: the zero-size copy is bound normally. It also means the fix in this PR is stricter than Windows rather than matching it. The Size 0 and Size 19 copies, which Windows loads and binds, both fail here with "import directory smaller than one descriptor". The Size 19 rejection was added in #49 at the maintainer's request.

I didn't measure the two-DLL case, where a second live descriptor lies past Size.

There seem to be two consistent options, and I think it's the maintainer's call:

  1. Match Windows: ignore Size, walk from VirtualAddress to the null descriptor with each descriptor bounds-checked against SizeOfImage, and bind everything found. The Size 0/19/20 copies and your hidden-descriptor case would then map with every slot bound.
  2. Stay stricter than Windows, as this PR does. It would help to state that in docs/experiments.md, for example: "pe_map rejects an import directory whose Size is zero or stops before a live descriptor; Windows ignores Size and binds them."

A merge note: this PR and #78 both edit the entry-point control and the summary print in check_rejections (tests/cpu/native_map_check.py). #78 changes the control to "entry point outside executable section", because entry points are now called. They also edit the same paragraph of the pe_map section. Whichever lands second will need a small rebase, and I'm happy to do it on #78's side. If it helps, I can also run the Windows ctest line that your verification section leaves open.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065241885 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/85#issuecomment-7065241885). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T15:55:19Z. --- Thanks. I wrote `pe_map`, so I checked the Windows side your verification section leaves open. I patched only the import-directory `Size` field of `cpu_import_original.dll` (import directory at RVA `0x22b4`, Size 40, one descriptor plus terminator, IAT slot at RVA `0x2000`). Each copy was loaded with `LoadLibraryExW(path, NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)`, with `cpu_import_provider.dll` next to it. I only read the IAT slot afterwards; nothing in the image was called. Windows 11 build 26300: | Import directory Size | `LoadLibraryExW` | IAT slot after load | |---|---|---| | 40 (as built) | loaded | `mix_args` from the provider | | 0 | loaded | `mix_args` from the provider | | 19 | loaded | `mix_args` from the provider | | 20 (terminator outside) | loaded | `mix_args` from the provider | This confirms your point that the loader does not use `Size` for the import walk: the zero-size copy is bound normally. It also means the fix in this PR is stricter than Windows rather than matching it. The Size 0 and Size 19 copies, which Windows loads and binds, both fail here with "import directory smaller than one descriptor". The Size 19 rejection was added in #49 at the maintainer's request. I didn't measure the two-DLL case, where a second live descriptor lies past `Size`. There seem to be two consistent options, and I think it's the maintainer's call: 1. **Match Windows:** ignore `Size`, walk from `VirtualAddress` to the null descriptor with each descriptor bounds-checked against `SizeOfImage`, and bind everything found. The Size 0/19/20 copies and your hidden-descriptor case would then map with every slot bound. 2. **Stay stricter than Windows,** as this PR does. It would help to state that in `docs/experiments.md`, for example: "pe_map rejects an import directory whose Size is zero or stops before a live descriptor; Windows ignores Size and binds them." A merge note: this PR and #78 both edit the entry-point control and the summary `print` in `check_rejections` (`tests/cpu/native_map_check.py`). #78 changes the control to "entry point outside executable section", because entry points are now called. They also edit the same paragraph of the `pe_map` section. Whichever lands second will need a small rebase, and I'm happy to do it on #78's side. If it helps, I can also run the Windows `ctest` line that your verification section leaves open. _AI-assisted._
R0Y00 commented 2026-11-24 06:47:37 +00:00 (Migrated from github.com)

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


Thanks for measuring the Windows behavior. Please use the Windows-compatible interpretation for ordinary imports, with independent bounds checks. Please include a source-built two-provider comparison that confirms both slots are bound when the listed size ends early, and reconcile the existing size-19 expectation and #78 tests before requesting another review.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065605540 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/85#issuecomment-7065605540). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T16:23:01Z. --- Thanks for measuring the Windows behavior. Please use the Windows-compatible interpretation for ordinary imports, with independent bounds checks. Please include a source-built two-provider comparison that confirms both slots are bound when the listed size ends early, and reconcile the existing size-19 expectation and #78 tests before requesting another review.
Vonsek commented 2026-11-24 06:47:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T16:54:28Z.


Updated (4cac5e6, rebased on current main with #78):

  • pe_map now uses the Windows-compatible interpretation for ordinary imports: the import table is found by address and walked to the null descriptor, with each descriptor bounds-checked against the image independently. Size is no longer used, and IAT writes may not overlap the walked table.
  • New source-built two-provider fixture cpu_two_imports (mix_args from cpu_import_provider, probe from cpu_register_probe). cpu_native_map patches its import directory Size to 60, 0, 19 and 20 and requires both slots to be bound under LoadLibraryExW and under pe_map. On Windows 11 26200.9974 both bind both slots in all four cases; the previous pe_map fails at Size 0 with no import requested.
  • The size-19 rejection control is removed, since Windows binds that copy. The #78 entry-point control and the outside-image control are kept as they are on main.

Full ctest --preset dev --no-tests=error on this head: 78/78 passed (two wave64 skips). The PR title and description are updated to match.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066039538 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/85#issuecomment-7066039538). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T16:54:28Z. --- Updated (4cac5e6, rebased on current main with #78): - `pe_map` now uses the Windows-compatible interpretation for ordinary imports: the import table is found by address and walked to the null descriptor, with each descriptor bounds-checked against the image independently. `Size` is no longer used, and IAT writes may not overlap the walked table. - New source-built two-provider fixture `cpu_two_imports` (`mix_args` from `cpu_import_provider`, `probe` from `cpu_register_probe`). `cpu_native_map` patches its import directory `Size` to 60, 0, 19 and 20 and requires both slots to be bound under `LoadLibraryExW` and under `pe_map`. On Windows 11 26200.9974 both bind both slots in all four cases; the previous `pe_map` fails at Size 0 with no import requested. - The size-19 rejection control is removed, since Windows binds that copy. The #78 entry-point control and the outside-image control are kept as they are on main. Full `ctest --preset dev --no-tests=error` on this head: 78/78 passed (two wave64 skips). The PR title and description are updated to match.
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!84
No description provided.