Find TLS and delay-import directories by address in pe_map #102

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

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


Change

pe_map_image tested only Size for the TLS and delay-import directories. Measured on Windows 11 build 26200, the loader finds the TLS directory by its address:

TLS directory (as built: Size 40) LoadLibrary pe_map before pe_map now
Size 0 callback, then entry point entry point only (callback skipped) callback, then entry point
Size 1 or 39 callback, then entry point format: "TLS directory outside image" callback, then entry point
address 0, Size 40 entry point only unsupported: "TLS data" (read the DOS header) entry point only
address past the image, or ending past it fails, error 998 format: "TLS directory outside image" same

The loader does not read the delay-import directory at all: copies of the named import fixture with an empty table, address 0 with Size 32, an address past the image, or a descriptor that names a DLL all load. pe_map now separates these cases instead of testing Size:

Delay-import directory LoadLibraryExW pe_map before pe_map now
empty table, Size 0 loads maps maps
empty table, Size 32 loads unsupported: "delay imports" maps
address 0, Size 32 loads unsupported: "delay imports" maps
address past the image loads unsupported (Size 32) or maps (Size 0) format: "delay import directory outside image"
descriptor naming a DLL, Size 0 loads maps unsupported: "delay imports"

So TLS is present when its address is set and must fit in the image; supported TLS callbacks keep running, and TLS data templates stay unsupported. Malformed directory metadata is a format error, distinct from supported TLS and from unsupported delay imports. Refs #46.

Verification

Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, at 01aa32f (on main a2a62ee, with #78):

ctest --preset dev --no-tests=error -R '^cpu_native_' -V
cpu_native_map: Windows loads every delay-import variant; pe_map maps an address of 0 and an empty table, and rejects a directory past the image (format) and a descriptor naming a DLL (unsupported)
cpu_native_tls: TLS directory with Size 0, 1 or 39: callback and entry point run under Windows and pe_map; address 0: entry point only under both; a directory past the image fails both loads
4/4 passed (cpu_native_map, cpu_native_seh, cpu_native_section, cpu_native_tls)

The existing #78 TLS checks (notification order, FALSE entry point, rejections, non-DLL copies, release after the callback list changes) pass unchanged. Against main's pe_map, cpu_native_tls fails at "TLS Size 0" (callback missing under pe_map) and cpu_native_map fails at "delay empty table, Size 32". The full ctest --preset dev --no-tests=error passed 78/78 (bvh_wave64_hardware and bvh_wave64_warp skipped, lane range 32..32).

Evidence and follow-up

No new fixtures. The TLS cases patch temporary copies of cpu_tls_original and compare LoadLibrary with pe_map in a child process per load, using the existing notification sink. The delay cases patch temporary copies of cpu_import_original, which has no entry point, so LoadLibraryExW runs nothing in the image. The empty table is the zero-filled tail of the .rdata page; the descriptor that names a DLL is the import descriptor read at offset 8, whose ForwarderChain and Name line up with Attributes and DllNameRVA.

Assumption: an empty delay table is recognised by a zero DllNameRVA in the first descriptor. #85 changes the ordinary import walk in the same file; the two touch different blocks, and whichever lands second needs a small rebase in tests/cpu/native_map_check.py. Docs: the native mapping and TLS paragraphs in docs/experiments.md.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531084665 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/103). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T15:59:41Z. --- ## Change `pe_map_image` tested only `Size` for the TLS and delay-import directories. Measured on Windows 11 build 26200, the loader finds the TLS directory by its address: | TLS directory (as built: Size 40) | `LoadLibrary` | `pe_map` before | `pe_map` now | |---|---|---|---| | Size 0 | callback, then entry point | entry point only (callback skipped) | callback, then entry point | | Size 1 or 39 | callback, then entry point | format: "TLS directory outside image" | callback, then entry point | | address 0, Size 40 | entry point only | unsupported: "TLS data" (read the DOS header) | entry point only | | address past the image, or ending past it | fails, error 998 | format: "TLS directory outside image" | same | The loader does not read the delay-import directory at all: copies of the named import fixture with an empty table, address 0 with Size 32, an address past the image, or a descriptor that names a DLL all load. `pe_map` now separates these cases instead of testing `Size`: | Delay-import directory | `LoadLibraryExW` | `pe_map` before | `pe_map` now | |---|---|---|---| | empty table, Size 0 | loads | maps | maps | | empty table, Size 32 | loads | unsupported: "delay imports" | maps | | address 0, Size 32 | loads | unsupported: "delay imports" | maps | | address past the image | loads | unsupported (Size 32) or maps (Size 0) | format: "delay import directory outside image" | | descriptor naming a DLL, Size 0 | loads | maps | unsupported: "delay imports" | So TLS is present when its address is set and must fit in the image; supported TLS callbacks keep running, and TLS data templates stay unsupported. Malformed directory metadata is a format error, distinct from supported TLS and from unsupported delay imports. Refs #46. ## Verification Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, at 01aa32f (on main a2a62ee, with #78): ``` ctest --preset dev --no-tests=error -R '^cpu_native_' -V cpu_native_map: Windows loads every delay-import variant; pe_map maps an address of 0 and an empty table, and rejects a directory past the image (format) and a descriptor naming a DLL (unsupported) cpu_native_tls: TLS directory with Size 0, 1 or 39: callback and entry point run under Windows and pe_map; address 0: entry point only under both; a directory past the image fails both loads 4/4 passed (cpu_native_map, cpu_native_seh, cpu_native_section, cpu_native_tls) ``` The existing #78 TLS checks (notification order, FALSE entry point, rejections, non-DLL copies, release after the callback list changes) pass unchanged. Against main's `pe_map`, `cpu_native_tls` fails at "TLS Size 0" (callback missing under `pe_map`) and `cpu_native_map` fails at "delay empty table, Size 32". The full `ctest --preset dev --no-tests=error` passed 78/78 (`bvh_wave64_hardware` and `bvh_wave64_warp` skipped, lane range 32..32). ## Evidence and follow-up No new fixtures. The TLS cases patch temporary copies of `cpu_tls_original` and compare `LoadLibrary` with `pe_map` in a child process per load, using the existing notification sink. The delay cases patch temporary copies of `cpu_import_original`, which has no entry point, so `LoadLibraryExW` runs nothing in the image. The empty table is the zero-filled tail of the `.rdata` page; the descriptor that names a DLL is the import descriptor read at offset 8, whose `ForwarderChain` and `Name` line up with `Attributes` and `DllNameRVA`. Assumption: an empty delay table is recognised by a zero `DllNameRVA` in the first descriptor. #85 changes the ordinary import walk in the same file; the two touch different blocks, and whichever lands second needs a small rebase in `tests/cpu/native_map_check.py`. Docs: the native mapping and TLS paragraphs in `docs/experiments.md`.
upstream-sync closed this pull request 2026-11-24 06:41:19 +00:00
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:20Z.


Thanks for finding the zero-size case. #78 has now landed. Please update on main and keep supported TLS callbacks working; distinguish malformed directory metadata from supported TLS and from unsupported delay imports. Include Windows comparisons for the disputed address/size cases.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065609475 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/103#issuecomment-7065609475). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T16:23:20Z. --- Thanks for finding the zero-size case. #78 has now landed. Please update on main and keep supported TLS callbacks working; distinguish malformed directory metadata from supported TLS and from unsupported delay imports. Include Windows comparisons for the disputed address/size cases.
Vonsek commented 2026-11-24 06:47:39 +00:00 (Migrated from github.com)

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


Updated on main with #78 (01aa32f):

  • Supported TLS callbacks keep working. The TLS directory is now found by its address, as the loader does: copies with Size 0, 1 or 39 run the callback and then the entry point under both LoadLibrary and pe_map, and with the address set to 0 only the entry point runs under both. Before, pe_map skipped the callback for Size 0, rejected Size 1 and 39, and reported "TLS data" for address 0 because it read the DOS header.
  • Malformed metadata is now a format error, separate from supported TLS and from unsupported delay imports: a TLS directory that does not fit in the image ("TLS directory outside image"; Windows fails with error 998) and a delay-import directory outside the image ("delay import directory outside image").
  • Delay imports: the loader does not read that directory at all. All five patched copies load under LoadLibraryExW. pe_map maps an address of 0 and an empty table, and keeps a descriptor that names a DLL unsupported.

The Windows comparisons for each address/size case are in cpu_native_tls and cpu_native_map, with before/after tables in the description. cpu_native_* 4/4 and the full suite 78/78 pass on Windows 11 26200.9974.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066117202 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/103#issuecomment-7066117202). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T16:59:08Z. --- Updated on main with #78 (01aa32f): - Supported TLS callbacks keep working. The TLS directory is now found by its address, as the loader does: copies with `Size` 0, 1 or 39 run the callback and then the entry point under both `LoadLibrary` and `pe_map`, and with the address set to 0 only the entry point runs under both. Before, `pe_map` skipped the callback for Size 0, rejected Size 1 and 39, and reported "TLS data" for address 0 because it read the DOS header. - Malformed metadata is now a format error, separate from supported TLS and from unsupported delay imports: a TLS directory that does not fit in the image ("TLS directory outside image"; Windows fails with error 998) and a delay-import directory outside the image ("delay import directory outside image"). - Delay imports: the loader does not read that directory at all. All five patched copies load under `LoadLibraryExW`. `pe_map` maps an address of 0 and an empty table, and keeps a descriptor that names a DLL unsupported. The Windows comparisons for each address/size case are in `cpu_native_tls` and `cpu_native_map`, with before/after tables in the description. `cpu_native_*` 4/4 and the full suite 78/78 pass on Windows 11 26200.9974.
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!102
No description provided.