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

Manually merged
github merged 1 commit from gh-pr/78/head into gh-pr/78/base 2026-11-26 21:25:39 +00:00
Owner

@⁠Leyukaka on GitHub · 2026-11-21 15:38 UTC · original
Leyukaka:tls-callbacks → main · +438 −11 · 9 files · 1 commits · merged 2026-11-21 16:22 UTC

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.

**@⁠Leyukaka** on GitHub · 2026-11-21 15:38 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/78) `Leyukaka:tls-callbacks` → `main` · +438 −11 · 9 files · 1 commits · merged 2026-11-21 16:22 UTC ## 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._
pe_map now notifies a mapped image like LoadLibrary/FreeLibrary: TLS
callbacks in table order, then the entry point, on attach and on detach,
with a full detach when the entry point returns FALSE. The order was
measured with tls_route loaded by Windows; cpu_native_tls compares both
loaders through a sink DLL that outlives the image. TLS data templates
stay rejected.

Refs #46
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:25:38 +00:00
github manually merged commit 73f7166d2f into gh-pr/78/base 2026-11-26 21:25:39 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 15:46 UTC · original

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.

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

@⁠Leyukaka on GitHub · 2026-11-21 16:02 UTC · original

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.

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