Call TLS callbacks and the entry point from pe_map in the Windows order #77

Merged
Leyukaka merged 1 commit from upstream-pr/78 into main 2026-11-21 16:22:10 +00:00
Leyukaka commented 2026-11-21 15:38:12 +00:00 (Migrated from github.com)

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


Change

Follow-up to #46, now closed. Before this change pe_map rejected every image with a TLS directory or an entry point, which covers almost every real DLL. It now notifies a mapped image the way LoadLibrary and FreeLibrary do on Windows.

  • Attach: TLS callbacks in table order, then the entry point, each with DLL_PROCESS_ATTACH, the module base and a null reserved argument.
  • Detach (pe_map_release): TLS callbacks, then the entry point, with DLL_PROCESS_DETACH.
  • Entry point returns FALSE: the full detach in the same order, then MAP_INIT_FAILED. LoadLibraryW fails with error 1114 in the same case.
  • Non-DLL image: no notification at all. LoadLibraryW does the same, and for an EXE the entry point is the program itself.
  • Rejected: TLS data templates, an entry point or TLS callback outside an executable section, and a list longer than 256 callbacks. Implicit TLS data would need a slot in ntdll's per-thread ThreadLocalStoragePointer array, so AddressOfIndex is left as linked. In the test process, Windows wrote index 8 for this image.

I did not assume the order. I measured it on Windows 11 build 26300 with tls_route loaded through LoadLibraryW, and the test compares both loaders. I know of no Microsoft documentation for the detach order, so the docs describe it as measured.

New files:

  • tests/cpu/tls_route.asm: a handwritten _tls_used with one callback and an empty template, plus /ENTRY:dll_entry. The refuse variant returns FALSE from the entry point.
  • tests/cpu/tls_sink.cpp: records every call and outlives the image, so detach calls stay observable.
  • tests/cpu/native_tls_check.py (cpu_native_tls): one child process per loader and image.

Verification

At the head of this branch, on b7f7e3f (Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10):

ctest --preset dev --no-tests=error -R '^cpu_native_(map|seh|tls)$'
100% tests passed, 0 tests failed out of 3

The full suite gives 75 tests and 0 failures; only bvh_wave64_hardware and bvh_wave64_warp are skipped.

Controls in cpu_native_tls:

  • a nonzero SizeOfZeroFill, an entry point in .data and a TLS callback pointing into .data are rejected before any notification;
  • with IMAGE_FILE_DLL cleared, with and without an entry point, both loaders load and release the copy without any call;
  • a callback list overwritten after mapping still gets the entry point's detach on release.

cpu_native_map now expects "entry point outside executable section" for an out-of-image entry point.

As one-off manual checks (not in CI), I broke pe_map eight ways, and each made cpu_native_tls fail:

  • callbacks skipped;
  • entry point called before the callbacks;
  • no detach after a failed attach;
  • no detach on release;
  • DLL gate removed;
  • each executable-section check removed (two mutants);
  • the release fallback for a corrupted list removed.

Evidence and follow-up

The fixtures are original and built from this repository; there is no game data. Still open: implicit TLS data, thread attach and detach for threads created after mapping, and delay imports. CI runs Python 3.12; I only ran 3.14 locally.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6530860215 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/78). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T15:38:12Z. --- ## Change Follow-up to #46, now closed. Before this change `pe_map` rejected every image with a TLS directory or an entry point, which covers almost every real DLL. It now notifies a mapped image the way `LoadLibrary` and `FreeLibrary` do on Windows. - **Attach:** TLS callbacks in table order, then the entry point, each with `DLL_PROCESS_ATTACH`, the module base and a null reserved argument. - **Detach (`pe_map_release`):** TLS callbacks, then the entry point, with `DLL_PROCESS_DETACH`. - **Entry point returns FALSE:** the full detach in the same order, then `MAP_INIT_FAILED`. `LoadLibraryW` fails with error 1114 in the same case. - **Non-DLL image:** no notification at all. `LoadLibraryW` does the same, and for an EXE the entry point is the program itself. - **Rejected:** TLS data templates, an entry point or TLS callback outside an executable section, and a list longer than 256 callbacks. Implicit TLS data would need a slot in ntdll's per-thread `ThreadLocalStoragePointer` array, so `AddressOfIndex` is left as linked. In the test process, Windows wrote index 8 for this image. I did not assume the order. I measured it on Windows 11 build 26300 with `tls_route` loaded through `LoadLibraryW`, and the test compares both loaders. I know of no Microsoft documentation for the detach order, so the docs describe it as measured. New files: - `tests/cpu/tls_route.asm`: a handwritten `_tls_used` with one callback and an empty template, plus `/ENTRY:dll_entry`. The `refuse` variant returns FALSE from the entry point. - `tests/cpu/tls_sink.cpp`: records every call and outlives the image, so detach calls stay observable. - `tests/cpu/native_tls_check.py` (`cpu_native_tls`): one child process per loader and image. ## Verification At the head of this branch, on `b7f7e3f` (Windows 11 build 26300, VS Build Tools 17.14, Python 3.14.10): ``` ctest --preset dev --no-tests=error -R '^cpu_native_(map|seh|tls)$' 100% tests passed, 0 tests failed out of 3 ``` The full suite gives 75 tests and 0 failures; only `bvh_wave64_hardware` and `bvh_wave64_warp` are skipped. Controls in `cpu_native_tls`: - a nonzero `SizeOfZeroFill`, an entry point in `.data` and a TLS callback pointing into `.data` are rejected before any notification; - with `IMAGE_FILE_DLL` cleared, with and without an entry point, both loaders load and release the copy without any call; - a callback list overwritten after mapping still gets the entry point's detach on release. `cpu_native_map` now expects "entry point outside executable section" for an out-of-image entry point. As one-off manual checks (not in CI), I broke `pe_map` eight ways, and each made `cpu_native_tls` fail: - callbacks skipped; - entry point called before the callbacks; - no detach after a failed attach; - no detach on release; - DLL gate removed; - each executable-section check removed (two mutants); - the release fallback for a corrupted list removed. ## Evidence and follow-up The fixtures are original and built from this repository; there is no game data. Still open: implicit TLS data, thread attach and detach for threads created after mapping, and delay imports. CI runs Python 3.12; I only ran 3.14 locally. _AI-assisted._
upstream-sync closed this pull request 2026-11-24 06:41:13 +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:21Z.


Thanks for the native Windows comparison. Please address one review finding before merge: tls_callbacks reads the directory and each slot after section protections are applied, including during release. Image bounds alone do not guarantee these bytes are readable. Please validate readability over each full read and cover the rejection with a source-owned fixture. The branch also currently conflicts with main. Please sync it and rerun the native TLS comparisons.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065131073 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/78#issuecomment-7065131073). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T15:46:21Z. --- Thanks for the native Windows comparison. Please address one review finding before merge: tls_callbacks reads the directory and each slot after section protections are applied, including during release. Image bounds alone do not guarantee these bytes are readable. Please validate readability over each full read and cover the rejection with a source-owned fixture. The branch also currently conflicts with main. Please sync it and rerun the native TLS comparisons.
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-21T16:02:48Z.


Thanks for the review. Both points are addressed, and the branch is rebased on 88fd8c5.

Readability. tls_callbacks now calls readable(address, size) before reading the TLS directory and before reading each 8-byte callback slot. That helper queries the live protection of every page in the range with VirtualQuery: the pages must be committed and not PAGE_NOACCESS, execute-only or PAGE_GUARD. It checks the live protection rather than the section headers, so it also covers pages the image reprotects after mapping. It is used on attach and in pe_map_release. Each value is copied once after its check, so the validated value is the one used. The query is not atomic with the read; a comment says that concurrent reprotection by another thread is not handled.

Source-owned fixtures. tls_route.asm now places the callback list in its own read-only .tlscb section, so the directory and the list can be made unreadable separately. New controls in cpu_native_tls:

  • .data with READ and WRITE cleared, which makes the directory inaccessible, is rejected as "TLS directory not readable" before any notification;
  • .tlscb with READ cleared is rejected as "TLS callback list not readable" before any notification;
  • the mapped list made PAGE_NOACCESS with VirtualProtect before pe_map_release: the list is skipped and only the entry point is detached.

Without either readable() check, the test fails with an access violation (two manual mutants, not in CI). The native TLS comparisons were rerun after the sync. Locally cpu_native_map, cpu_native_seh and cpu_native_tls pass, and the full suite has 76 tests with 0 failures (wave64 skipped on the RTX 5080 and WARP).

pe_map_export reads the export tables without this check; the docs note that as a remaining limit.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7065337658 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/78#issuecomment-7065337658). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T16:02:48Z. --- Thanks for the review. Both points are addressed, and the branch is rebased on `88fd8c5`. **Readability.** `tls_callbacks` now calls `readable(address, size)` before reading the TLS directory and before reading each 8-byte callback slot. That helper queries the live protection of every page in the range with `VirtualQuery`: the pages must be committed and not `PAGE_NOACCESS`, execute-only or `PAGE_GUARD`. It checks the live protection rather than the section headers, so it also covers pages the image reprotects after mapping. It is used on attach and in `pe_map_release`. Each value is copied once after its check, so the validated value is the one used. The query is not atomic with the read; a comment says that concurrent reprotection by another thread is not handled. **Source-owned fixtures.** `tls_route.asm` now places the callback list in its own read-only `.tlscb` section, so the directory and the list can be made unreadable separately. New controls in `cpu_native_tls`: - `.data` with READ and WRITE cleared, which makes the directory inaccessible, is rejected as "TLS directory not readable" before any notification; - `.tlscb` with READ cleared is rejected as "TLS callback list not readable" before any notification; - the mapped list made `PAGE_NOACCESS` with `VirtualProtect` before `pe_map_release`: the list is skipped and only the entry point is detached. Without either `readable()` check, the test fails with an access violation (two manual mutants, not in CI). The native TLS comparisons were rerun after the sync. Locally `cpu_native_map`, `cpu_native_seh` and `cpu_native_tls` pass, and the full suite has 76 tests with 0 failures (wave64 skipped on the RTX 5080 and WARP). `pe_map_export` reads the export tables without this check; the docs note that as a remaining limit. _AI-assisted._
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!77
No description provided.