Bind delay imports through the resolver in pe_map #127

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

@⁠dcastell1 on GitHub · 2026-11-21 16:40 UTC · original
dcastell1:delay-imports → main · +368 −33 · 9 files · 2 commits · merged 2026-11-21 17:55 UTC

Change

pe_map_image rejected every image with a delay-import directory. It now resolves each IMAGE_DELAYLOAD_DESCRIPTOR entry through the same host resolver as ordinary imports and writes the bound address into the descriptor's IAT.

The PE loader does not resolve delay imports; the image's own helper does, with LoadLibrary and GetProcAddress. pe_map has neither and must bind through the host resolver, so it binds at map time. This is a deliberate difference from Windows: an absent provider fails the map with MAP_UNRESOLVED_IMPORT, where Windows maps and fails on the first call. The test documents it. Descriptors must set dlattrRva (bit 0); old-style virtual-address descriptors are rejected as unsupported.

The delay directory is validated before use: address and size in the image, at least one descriptor, DLL name inside the image, name table and IAT present, each thunk and IAT slot inside the image. A normal import IAT write may not overlap the delay directory, because the delay walk happens after the normal bindings.

Test

New cpu_native_delay with delay_route, which imports delayed_value by name and delayed_ordinal by ordinal 2 from cpu_delay_provider.dll using /DELAYLOAD. It compares the mapped calls with the Windows delay helper, checks both IAT slots before the calls, binds an altered provider to change both results, and maps without the provider to check the rejection. Damaged copies clear dlattrRva, move the DLL name outside the image, zero the name table, move the IAT outside the image, or shrink the directory to zero or below one descriptor; all are rejected before the provider is requested.

On Windows 11 build 26300.9974, VS 2022 17.14 (MSVC 14.44), Python 3.13:

ctest --preset dev --no-tests=error -R '^(cpu_native_map|cpu_native_seh|cpu_native_tls|cpu_native_delay)$' -V

4/4 pass.

Refs #46.

Note for #103: this removes the delay imports rejection line that #103 also edits. A zero-size delay directory is rejected here as MAP_FORMAT with "delay import directory smaller than one descriptor", which is more specific than the unsupported status. The TLS zero-size rule in #103 is independent.

**@⁠dcastell1** on GitHub · 2026-11-21 16:40 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/127) `dcastell1:delay-imports` → `main` · +368 −33 · 9 files · 2 commits · merged 2026-11-21 17:55 UTC ## Change `pe_map_image` rejected every image with a delay-import directory. It now resolves each `IMAGE_DELAYLOAD_DESCRIPTOR` entry through the same host resolver as ordinary imports and writes the bound address into the descriptor's IAT. The PE loader does not resolve delay imports; the image's own helper does, with `LoadLibrary` and `GetProcAddress`. `pe_map` has neither and must bind through the host resolver, so it binds at map time. This is a deliberate difference from Windows: an absent provider fails the map with `MAP_UNRESOLVED_IMPORT`, where Windows maps and fails on the first call. The test documents it. Descriptors must set `dlattrRva` (bit 0); old-style virtual-address descriptors are rejected as unsupported. The delay directory is validated before use: address and size in the image, at least one descriptor, DLL name inside the image, name table and IAT present, each thunk and IAT slot inside the image. A normal import IAT write may not overlap the delay directory, because the delay walk happens after the normal bindings. ## Test New `cpu_native_delay` with `delay_route`, which imports `delayed_value` by name and `delayed_ordinal` by ordinal 2 from `cpu_delay_provider.dll` using `/DELAYLOAD`. It compares the mapped calls with the Windows delay helper, checks both IAT slots before the calls, binds an altered provider to change both results, and maps without the provider to check the rejection. Damaged copies clear `dlattrRva`, move the DLL name outside the image, zero the name table, move the IAT outside the image, or shrink the directory to zero or below one descriptor; all are rejected before the provider is requested. On Windows 11 build 26300.9974, VS 2022 17.14 (MSVC 14.44), Python 3.13: `ctest --preset dev --no-tests=error -R '^(cpu_native_map|cpu_native_seh|cpu_native_tls|cpu_native_delay)$' -V` 4/4 pass. Refs #46. Note for #103: this removes the `delay imports` rejection line that #103 also edits. A zero-size delay directory is rejected here as `MAP_FORMAT` with "delay import directory smaller than one descriptor", which is more specific than the unsupported status. The TLS zero-size rule in #103 is independent.
Resolve each IMAGE_DELAYLOAD_DESCRIPTOR entry at map time and write the
bound address into its IAT. pe_map has no LoadLibrary, so the image's
delay-load helper would bypass the host resolver. Descriptors without
dlattrRva are rejected, as are directories that are empty or smaller
than one descriptor. A normal IAT write may not overlap the delay
directory, which is read after the normal imports are bound.

cpu_native_delay maps a source-built route with one named and one ordinal
delay import, compares both calls with the Windows delay helper, checks
the IAT slots before the calls, and exercises an altered provider and an
unbound map. Copies with a cleared RVA attribute, an out-of-image name,
a zero name table, an out-of-image IAT, or a short directory are rejected
before the provider is requested.

Verified on Windows 11 build 26300.9974 with VS 2022 17.14 (MSVC 14.44)
and Python 3.13: cpu_native_map, cpu_native_seh, cpu_native_tls and
cpu_native_delay pass.
pe_map masked a named thunk with 0x7fffffff before the RVA bounds check,
so a value with bits 31..62 set could alias a valid low RVA. Reject those
bits before narrowing and add a source-built control that sets bit 40 in
the fixture's named thunk. cpu_native_map, cpu_native_seh, cpu_native_tls
and cpu_native_delay pass on Windows 11 build 26300.9974 with MSVC 14.44.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:28:47 +00:00
github manually merged commit 31cbe52f28 into gh-pr/127/base 2026-11-26 21:28:49 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 17:16 UTC · original

Please sync with main and reconcile the delay-directory handling from #103. Preserve its empty-directory and malformed-directory controls and add the new binding cases alongside them.

**@⁠R0Y00** on GitHub · 2026-11-21 17:16 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/127#issuecomment-7066352288) Please sync with main and reconcile the delay-directory handling from #103. Preserve its empty-directory and malformed-directory controls and add the new binding cases alongside them.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 17:23 UTC · original

Thanks for incorporating the #103 path in 56184a1. GitHub still reports a merge conflict against current main. Please reconcile with the merged ordinary import walk from #85 and preserve its walked-descriptor IAT overlap protection, then run the normal and delay-import tests on the same head.

**@⁠R0Y00** on GitHub · 2026-11-21 17:23 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/127#issuecomment-7066444999) Thanks for incorporating the #103 path in 56184a1. GitHub still reports a merge conflict against current main. Please reconcile with the merged ordinary import walk from #85 and preserve its walked-descriptor IAT overlap protection, then run the normal and delay-import tests on the same head.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 17:26 UTC · original

The latest 2dc4694 does preserve both walked descriptor ranges. One remaining validation issue in the new delay path: a named thunk is masked with 0x7fffffff before the RVA bounds check, so an out-of-range value can alias a valid low RVA. Please reject invalid high bits before narrowing and cover the rejection with a source-built control.

**@⁠R0Y00** on GitHub · 2026-11-21 17:26 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/127#issuecomment-7066489087) The latest 2dc4694 does preserve both walked descriptor ranges. One remaining validation issue in the new delay path: a named thunk is masked with `0x7fffffff` before the RVA bounds check, so an out-of-range value can alias a valid low RVA. Please reject invalid high bits before narrowing and cover the rejection with a source-built control.
Author
Owner

@⁠dcastell1 on GitHub · 2026-11-21 17:32 UTC · original

Handled in ee269b8: name_rva keeps all 64 bits until name_rva >> 31 rejects bits 31..62, then image.at<WORD> bounds-checks the low RVA. The new control sets bit 40 in the named thunk of the source-built delay fixture and expects MAP_FORMAT "delay import name for cpu_delay_provider.dll outside image". cpu_native_map, cpu_native_seh, cpu_native_tls and cpu_native_delay pass on the same head (Windows 11 build 26300.9974, MSVC 14.44).

**@⁠dcastell1** on GitHub · 2026-11-21 17:32 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/127#issuecomment-7066555772) Handled in ee269b8: `name_rva` keeps all 64 bits until `name_rva >> 31` rejects bits 31..62, then `image.at<WORD>` bounds-checks the low RVA. The new control sets bit 40 in the named thunk of the source-built delay fixture and expects `MAP_FORMAT` "delay import name for cpu_delay_provider.dll outside image". `cpu_native_map`, `cpu_native_seh`, `cpu_native_tls` and `cpu_native_delay` pass on the same head (Windows 11 build 26300.9974, MSVC 14.44).
This discussion has been locked. Commenting is limited to contributors.
No description provided.