pe: serve implicit TLS data templates through a carrier DLL #692

Merged
rasptum merged 3 commits from upstream-pr/698 into main 2026-11-23 04:12:51 +00:00
rasptum commented 2026-11-22 20:51:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-22T20:51:45Z.


Change

pe_map_image rejected every image whose TLS directory has a data template (MAP_UNSUPPORTED, "TLS data"). MSVC emits such a template for any thread_local or __declspec(thread) variable, and the static C runtime adds its own, so this stopped most C++ images before a single instruction ran. This PR makes pe_map accept them. Refs #165.

Mechanism. Compiled x64 code reaches a thread-local through the thread's ThreadLocalStoragePointer array (gs:[0x58]), indexed by the DWORD at AddressOfIndex. ntdll owns that array: it assigns the indexes, sizes the array, and allocates and frees one block per thread. pe_map does not write to it. Instead, for an image with a template or a zero-fill size:

  1. After relocation and section protection, pe_map copies the relocated template into a small generated DLL (the "carrier") in a private directory under the temporary directory (see "Carrier lifetime" below). The carrier has no code and no entry point: a TLS directory with the copied template and the image's own SizeOfZeroFill and Characteristics (alignment), an import table that is only the null descriptor, and three base relocations for the directory's addresses.
  2. It loads the carrier with LoadLibraryExW. Windows assigns it a real index and allocates, aligns, initializes and frees one block per thread, for threads that already exist and for threads created later.
  3. It stores the carrier's index at the image's AddressOfIndex before any TLS callback or entry point runs.
  4. On release it unloads the carrier after the image's process-detach notifications, which returns the index and the blocks, then deletes the carrier and its directory through the handles it holds.

Why this mechanism. The alternatives I considered:

  • Writing the TEB array directly with an index pe_map picks. The array length and the index bitmap are private to ntdll, so a later LoadLibrary of any DLL with TLS can hand out the same index or replace the array. Not sound.
  • Calling ntdll's internal TLS routine found by a byte pattern. Works only on the builds the pattern was made for.
  • Giving pe_map.dll a fixed-size TLS block of its own and lending it to the image. One image per block, a size fixed at build time, and it collides with the C runtime's own thread-locals in pe_map.dll.

The carrier needs none of that: every TLS decision is made by the Windows loader on a well-formed DLL.

Rejected inputs (all after imports are bound, without any notification):

Input pe_map Windows 10 build 19045
template ends past the image MAP_FORMAT, "TLS template outside image" LoadLibraryW fails, error 998
template ends before its start MAP_FORMAT, "TLS template ends before it starts" fails, error 193
template or zero-fill size above 16 MiB MAP_UNSUPPORTED, "TLS template larger than 16 MiB" not measured
template on pages unreadable after section protection MAP_UNSUPPORTED, "TLS template not readable" not measured, no fixture
Windows refuses the carrier or assigns it no index MAP_MEMORY with the step and the Windows error n/a
carrier directory or file replaced, changed or not the file Windows mapped MAP_MEMORY with the step n/a

Known differences from a Windows load.

  • The template is copied once, at map time. A later write to the template inside the image does not reach threads created afterwards. Not measured on Windows.
  • Code that assumes TLS index 0 without reading AddressOfIndex cannot be served. MSVC output reads the index; I did not measure other compilers.
  • An image with an empty template and no zero-fill size keeps AddressOfIndex as linked, as before.
  • pe_map now writes one carrier per mapped TLS image, holding a copy of the image's template, in a directory of its own under the temporary directory. Both are removed on pe_map_release or when the map fails. A process that ends with an image still mapped leaves its carrier directory; nothing removes it later.
  • The carrier load and unload call LoadLibraryExW and FreeLibrary, also when pe_map_image or the final release runs inside a loader notification. No existing test maps a TLS-data image from inside a callback.

Carrier lifetime (changed after review). Each map call creates pe_map_tls_<32 random hex digits> under the temporary directory with a protected DACL for the process's user only and holds it open without delete sharing. It writes pe_map_tls_<same digits>.dll there with CREATE_NEW, records its file ID and closes the write handle (the loader's open does not share writing). It reopens the file for reading and deletion, shared for reading only, checks the file ID and the bytes read back, loads it, and checks with GetMappedFileNameW that the loaded carrier is the file behind the held handle. Cleanup goes through those two handles only, with POSIX delete semantics; nothing is deleted by name or pattern, so a map call removes only what it created. Measured on Windows 10 build 19045: while the two handles are held, writing, renaming or deleting the file and renaming its directory fail with error 32, renaming the directory above fails with error 5, and LoadLibraryExW still loads the file. Not closed: a process of the same user can replace the file between the close of the write handle and the reopen; the ID and read-back checks then fail the map, but no fixture provokes that race.

Files. tools/pe_map.cpp; new fixture tests/cpu/tls_data.cpp (built twice: cpu_tls_data and cpu_tls_data_refuse, whose entry point refuses) and test tests/cpu/native_tls_data_check.py (cpu_native_tls_data); tests/cpu/native_tls_value_check.py and tests/cpu/native_tls_check.py updated because they pinned the old rejection; CMakeLists.txt; docs/experiments.md ("Implicit TLS data").

Verification

Windows 10 Pro 22H2 build 19045, MSVC 19.44.35207.1 (x64), dev preset, commits 11987cd, 0dc025c and f618e68 on main at 3b9bb1a.

cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^cpu_native_|pe_|static_exe_intake|conditional_import|bundled_export' -j 4
ctest --preset dev --no-tests=error -R '^cpu_native_tls' --repeat until-fail:10
100% tests passed, 0 tests failed out of 30
100% tests passed, 0 tests failed out of 3      (each passed 10 runs in a row)
ctest --preset dev --no-tests=error -R '^cpu_native_tls_data$' -V
native and mapped agree on 10 observations: the loading thread, a thread that existed before the load and a new thread each start from the template in a block of their own, aligned to 64 bytes; indexes native 6/7, mapped 7/8
while mapped, renaming the carrier or its directory and opening it for writing fail with error 32; nothing is left after release; a carrier left by an ended process is not touched by a later map
failed maps remove only their own carrier, beside an ended process's old-layout file and another carrier directory: the carrier was written and Windows refused it: Windows cannot load the TLS carrier (error 5); the carrier was loaded and the entry point refused: entry point returned FALSE; Windows fails the refusing fixture with error 1114 after the same attach
native and mapped agree on copies with SizeOfZeroFill 0x10, 4096-byte alignment, no alignment
pe_map rejects a template past the image, a template ending before its start, a template above 16 MiB; Windows fails the first two with errors 998 and 193

Full suite, ctest --preset dev -j 4, run before the last two small checks (a GetMappedFileNameW length check and passing the BCryptGenRandom status through) and comment edits were added to 0dc025c: 304 tests, 265 passed, 33 capability skips, 6 failed. None of the six touches this change:

  • gpu_alias_lookup_writes_warp and pixel_aot_blend_scalar_hardware also failed in the full run on 11987cd, before this change.
  • spirv_ballot_extract_warp and spirv_ballot_extract_hardware (added by #656) fail with D3D12 881: CreateComputeShader: ... Shader version provided: cs_6_6. WARP 10.0.19041.5794 accepts up to cs_6_2 and the RTX 3060 driver 32.0.15.9636 on this Windows 10 build up to cs_6_5.
  • pixel_aot_hardware and pixel_aot_predicate_rounding_hardware timed out at 60 s while another build was running and passed when rerun alone (42.3 s and 50.6 s).

Evidence and follow-up

cpu_tls_data is a source-built DLL with the static C runtime. Its template holds an initialized value, a zero-filled value, a 64-byte aligned array, a pointer to a global (a base relocation inside the template), a zero-filled range of three pages and a C++ thread_local with a dynamic initializer. Each case runs in a fresh child process.

Measured, native LoadLibraryW against pe_map:

  • Loading thread, a thread created before the load, a thread created after it: each starts from the template, in its own block, array on a 64-byte boundary, pointer aimed at the loaded image's global, dynamic initializer run. Same under both loaders.
  • A write on one thread is not visible on another. A second copy loaded at the same time gets a different index and its own values. After release and reload the loading thread starts from the template again. Same under both loaders.
  • cpu_native_tls_value (from #523): the TLS callback and entry point now read 7 on attach, thread attach, thread detach and detach under both loaders. Before this change pe_map rejected the image.
  • Patched copies with SizeOfZeroFill 0x10, alignment 4096 and alignment cleared load under Windows; pe_map hands both fields to Windows unchanged and agrees on each. The existing cpu_native_tls control "zero-fill size 16 on an empty template" moved from a rejection to a both-loaders-accept control for the same reason.
  • A DLL without an import directory loads under Windows but receives no TLS index, with or without an entry point. An import directory holding only the null descriptor is enough. Because this decides whether the carrier works, its index slot starts at 0xffffffff and pe_map fails the map if Windows leaves it unchanged, instead of using a wrong index.
  • Carriers: one while an image is mapped, two with two images, nothing left in the child's temporary directory after release. While mapped, the carrier and its directory cannot be renamed and the carrier cannot be opened for writing (error 32). A child that exits with an image mapped leaves one carrier; a later process mapping in the same directory leaves it untouched.
  • Failure controls, each beside a file in the old carrier layout named after an ended process and a carrier directory of the current layout: a map whose carrier Windows refuses after it was written (the child's temporary directory has an inherited low integrity label and the child sets NoLowMandatoryLabelImages, error 5; a labeled copy of the fixture loads without that policy and fails with error 5 under it), and a map whose entry point refuses after the carrier was loaded (its DllMain first reads the template values, so the carrier was in place; Windows fails the same DLL with error 1114). Both remove their own carrier and leave the other two exactly as they were.

Negative controls: the three rejected copies above, each checked for no leftover carrier. Mutations of pe_map.cpp that the test catches: the old name-and-PID sweep restored, a sweep of every carrier-named directory, the held handles shared for deletion, the held handle closed before the load, the carrier not deleted on release, and a failed map that forgets its carrier instead of cleaning up. All six were run on the final code. Without the pe_map.cpp change the new test fails at the first mapped child with [6, 'TLS data'].

Windows 11: legacy-code-archaeologist verified 11987cd on build 26100 (30 of 30 tests); 0dc025c is not yet measured there. Not measured: an EXE with TLS data started through pe_map_run (the path is the same as for a DLL, but there is no fixture yet); a template modified after load; images built by compilers other than MSVC.

Overlap: no open pull request touches tools/pe_map.cpp or the TLS tests (checked with gh pr list on 22 November 2026). #543 (index slot must be writable) is merged and this change relies on its check.

AI-assisted: written and measured with Claude Code; the outputs above are copied from local runs.

The fixture tests/cpu/tls_data.cpp is original source written for this PR, under the repository's MIT License. If someone has Windows 11 at hand, a run of ctest --preset dev --no-tests=error -R '^cpu_native_tls' -V there would close the main gap in the measurements.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6547630428 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/698). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-22T20:51:45Z. --- ## Change `pe_map_image` rejected every image whose TLS directory has a data template (`MAP_UNSUPPORTED`, "TLS data"). MSVC emits such a template for any `thread_local` or `__declspec(thread)` variable, and the static C runtime adds its own, so this stopped most C++ images before a single instruction ran. This PR makes `pe_map` accept them. Refs #165. **Mechanism.** Compiled x64 code reaches a thread-local through the thread's `ThreadLocalStoragePointer` array (`gs:[0x58]`), indexed by the DWORD at `AddressOfIndex`. ntdll owns that array: it assigns the indexes, sizes the array, and allocates and frees one block per thread. `pe_map` does not write to it. Instead, for an image with a template or a zero-fill size: 1. After relocation and section protection, `pe_map` copies the relocated template into a small generated DLL (the "carrier") in a private directory under the temporary directory (see "Carrier lifetime" below). The carrier has no code and no entry point: a TLS directory with the copied template and the image's own `SizeOfZeroFill` and `Characteristics` (alignment), an import table that is only the null descriptor, and three base relocations for the directory's addresses. 2. It loads the carrier with `LoadLibraryExW`. Windows assigns it a real index and allocates, aligns, initializes and frees one block per thread, for threads that already exist and for threads created later. 3. It stores the carrier's index at the image's `AddressOfIndex` before any TLS callback or entry point runs. 4. On release it unloads the carrier after the image's process-detach notifications, which returns the index and the blocks, then deletes the carrier and its directory through the handles it holds. **Why this mechanism.** The alternatives I considered: - Writing the TEB array directly with an index `pe_map` picks. The array length and the index bitmap are private to ntdll, so a later `LoadLibrary` of any DLL with TLS can hand out the same index or replace the array. Not sound. - Calling ntdll's internal TLS routine found by a byte pattern. Works only on the builds the pattern was made for. - Giving `pe_map.dll` a fixed-size TLS block of its own and lending it to the image. One image per block, a size fixed at build time, and it collides with the C runtime's own thread-locals in `pe_map.dll`. The carrier needs none of that: every TLS decision is made by the Windows loader on a well-formed DLL. **Rejected inputs** (all after imports are bound, without any notification): | Input | `pe_map` | Windows 10 build 19045 | |---|---|---| | template ends past the image | `MAP_FORMAT`, "TLS template outside image" | `LoadLibraryW` fails, error 998 | | template ends before its start | `MAP_FORMAT`, "TLS template ends before it starts" | fails, error 193 | | template or zero-fill size above 16 MiB | `MAP_UNSUPPORTED`, "TLS template larger than 16 MiB" | not measured | | template on pages unreadable after section protection | `MAP_UNSUPPORTED`, "TLS template not readable" | not measured, no fixture | | Windows refuses the carrier or assigns it no index | `MAP_MEMORY` with the step and the Windows error | n/a | | carrier directory or file replaced, changed or not the file Windows mapped | `MAP_MEMORY` with the step | n/a | **Known differences from a Windows load.** - The template is copied once, at map time. A later write to the template inside the image does not reach threads created afterwards. Not measured on Windows. - Code that assumes TLS index 0 without reading `AddressOfIndex` cannot be served. MSVC output reads the index; I did not measure other compilers. - An image with an empty template and no zero-fill size keeps `AddressOfIndex` as linked, as before. - `pe_map` now writes one carrier per mapped TLS image, holding a copy of the image's template, in a directory of its own under the temporary directory. Both are removed on `pe_map_release` or when the map fails. A process that ends with an image still mapped leaves its carrier directory; nothing removes it later. - The carrier load and unload call `LoadLibraryExW` and `FreeLibrary`, also when `pe_map_image` or the final release runs inside a loader notification. No existing test maps a TLS-data image from inside a callback. **Carrier lifetime** (changed after review). Each map call creates `pe_map_tls_<32 random hex digits>` under the temporary directory with a protected DACL for the process's user only and holds it open without delete sharing. It writes `pe_map_tls_<same digits>.dll` there with `CREATE_NEW`, records its file ID and closes the write handle (the loader's open does not share writing). It reopens the file for reading and deletion, shared for reading only, checks the file ID and the bytes read back, loads it, and checks with `GetMappedFileNameW` that the loaded carrier is the file behind the held handle. Cleanup goes through those two handles only, with POSIX delete semantics; nothing is deleted by name or pattern, so a map call removes only what it created. Measured on Windows 10 build 19045: while the two handles are held, writing, renaming or deleting the file and renaming its directory fail with error 32, renaming the directory above fails with error 5, and `LoadLibraryExW` still loads the file. Not closed: a process of the same user can replace the file between the close of the write handle and the reopen; the ID and read-back checks then fail the map, but no fixture provokes that race. **Files.** `tools/pe_map.cpp`; new fixture `tests/cpu/tls_data.cpp` (built twice: `cpu_tls_data` and `cpu_tls_data_refuse`, whose entry point refuses) and test `tests/cpu/native_tls_data_check.py` (`cpu_native_tls_data`); `tests/cpu/native_tls_value_check.py` and `tests/cpu/native_tls_check.py` updated because they pinned the old rejection; `CMakeLists.txt`; `docs/experiments.md` ("Implicit TLS data"). ## Verification Windows 10 Pro 22H2 build 19045, MSVC 19.44.35207.1 (x64), `dev` preset, commits `11987cd`, `0dc025c` and `f618e68` on `main` at `3b9bb1a`. ```powershell cmake --build --preset dev ctest --preset dev --no-tests=error -R '^cpu_native_|pe_|static_exe_intake|conditional_import|bundled_export' -j 4 ctest --preset dev --no-tests=error -R '^cpu_native_tls' --repeat until-fail:10 ``` ```text 100% tests passed, 0 tests failed out of 30 100% tests passed, 0 tests failed out of 3 (each passed 10 runs in a row) ``` ```powershell ctest --preset dev --no-tests=error -R '^cpu_native_tls_data$' -V ``` ```text native and mapped agree on 10 observations: the loading thread, a thread that existed before the load and a new thread each start from the template in a block of their own, aligned to 64 bytes; indexes native 6/7, mapped 7/8 while mapped, renaming the carrier or its directory and opening it for writing fail with error 32; nothing is left after release; a carrier left by an ended process is not touched by a later map failed maps remove only their own carrier, beside an ended process's old-layout file and another carrier directory: the carrier was written and Windows refused it: Windows cannot load the TLS carrier (error 5); the carrier was loaded and the entry point refused: entry point returned FALSE; Windows fails the refusing fixture with error 1114 after the same attach native and mapped agree on copies with SizeOfZeroFill 0x10, 4096-byte alignment, no alignment pe_map rejects a template past the image, a template ending before its start, a template above 16 MiB; Windows fails the first two with errors 998 and 193 ``` Full suite, `ctest --preset dev -j 4`, run before the last two small checks (a `GetMappedFileNameW` length check and passing the `BCryptGenRandom` status through) and comment edits were added to `0dc025c`: 304 tests, 265 passed, 33 capability skips, 6 failed. None of the six touches this change: - `gpu_alias_lookup_writes_warp` and `pixel_aot_blend_scalar_hardware` also failed in the full run on `11987cd`, before this change. - `spirv_ballot_extract_warp` and `spirv_ballot_extract_hardware` (added by #656) fail with `D3D12 881: CreateComputeShader: ... Shader version provided: cs_6_6`. WARP 10.0.19041.5794 accepts up to cs_6_2 and the RTX 3060 driver 32.0.15.9636 on this Windows 10 build up to cs_6_5. - `pixel_aot_hardware` and `pixel_aot_predicate_rounding_hardware` timed out at 60 s while another build was running and passed when rerun alone (42.3 s and 50.6 s). ## Evidence and follow-up `cpu_tls_data` is a source-built DLL with the static C runtime. Its template holds an initialized value, a zero-filled value, a 64-byte aligned array, a pointer to a global (a base relocation inside the template), a zero-filled range of three pages and a C++ `thread_local` with a dynamic initializer. Each case runs in a fresh child process. Measured, native `LoadLibraryW` against `pe_map`: - Loading thread, a thread created before the load, a thread created after it: each starts from the template, in its own block, array on a 64-byte boundary, pointer aimed at the loaded image's global, dynamic initializer run. Same under both loaders. - A write on one thread is not visible on another. A second copy loaded at the same time gets a different index and its own values. After release and reload the loading thread starts from the template again. Same under both loaders. - `cpu_native_tls_value` (from #523): the TLS callback and entry point now read 7 on attach, thread attach, thread detach and detach under both loaders. Before this change `pe_map` rejected the image. - Patched copies with `SizeOfZeroFill` 0x10, alignment 4096 and alignment cleared load under Windows; `pe_map` hands both fields to Windows unchanged and agrees on each. The existing `cpu_native_tls` control "zero-fill size 16 on an empty template" moved from a rejection to a both-loaders-accept control for the same reason. - A DLL without an import directory loads under Windows but receives no TLS index, with or without an entry point. An import directory holding only the null descriptor is enough. Because this decides whether the carrier works, its index slot starts at `0xffffffff` and `pe_map` fails the map if Windows leaves it unchanged, instead of using a wrong index. - Carriers: one while an image is mapped, two with two images, nothing left in the child's temporary directory after release. While mapped, the carrier and its directory cannot be renamed and the carrier cannot be opened for writing (error 32). A child that exits with an image mapped leaves one carrier; a later process mapping in the same directory leaves it untouched. - Failure controls, each beside a file in the old carrier layout named after an ended process and a carrier directory of the current layout: a map whose carrier Windows refuses after it was written (the child's temporary directory has an inherited low integrity label and the child sets `NoLowMandatoryLabelImages`, error 5; a labeled copy of the fixture loads without that policy and fails with error 5 under it), and a map whose entry point refuses after the carrier was loaded (its `DllMain` first reads the template values, so the carrier was in place; Windows fails the same DLL with error 1114). Both remove their own carrier and leave the other two exactly as they were. Negative controls: the three rejected copies above, each checked for no leftover carrier. Mutations of `pe_map.cpp` that the test catches: the old name-and-PID sweep restored, a sweep of every carrier-named directory, the held handles shared for deletion, the held handle closed before the load, the carrier not deleted on release, and a failed map that forgets its carrier instead of cleaning up. All six were run on the final code. Without the `pe_map.cpp` change the new test fails at the first mapped child with `[6, 'TLS data']`. Windows 11: legacy-code-archaeologist verified `11987cd` on build 26100 (30 of 30 tests); `0dc025c` is not yet measured there. Not measured: an EXE with TLS data started through `pe_map_run` (the path is the same as for a DLL, but there is no fixture yet); a template modified after load; images built by compilers other than MSVC. Overlap: no open pull request touches `tools/pe_map.cpp` or the TLS tests (checked with `gh pr list` on 22 November 2026). #543 (index slot must be writable) is merged and this change relies on its check. AI-assisted: written and measured with Claude Code; the outputs above are copied from local runs. The fixture `tests/cpu/tls_data.cpp` is original source written for this PR, under the repository's MIT License. If someone has Windows 11 at hand, a run of `ctest --preset dev --no-tests=error -R '^cpu_native_tls' -V` there would close the main gap in the measurements. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:44:23 +00:00
legacy-code-archaeologist commented 2026-11-24 06:48:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T22:18:25Z.


Verification of 11987cd on Windows 11 build 26100.9168 (x64, Intel Skylake), MSVC 19.52.37029, dev preset. It merges cleanly with main.

ctest --preset dev --no-tests=error -R "^cpu_native_|pe_|static_exe_intake|conditional_import|bundled_export" -j 4
100% tests passed, 0 tests failed out of 30

The Windows-side observations of the new TLS-data test hold on this build. The TLS index numbers differ from a 26300 run, as expected for loader state:

cpu_native_tls_data:
native and mapped agree on 10 observations: the loading thread, a thread that existed before the load and a new thread each start from the template in a block of their own, aligned to 64 bytes; indexes native 7/8, mapped 8/9
native and mapped agree on copies with SizeOfZeroFill 0x10, 4096-byte alignment, no alignment
pe_map rejects a template past the image, a template ending before its start, a template above 16 MiB; Windows fails the first two with errors 998 and 193

cpu_native_tls (including the #543 index controls) and cpu_native_tls_value also pass on this build.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7086714507 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/698#issuecomment-7086714507). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T22:18:25Z. --- Verification of `11987cd` on Windows 11 build 26100.9168 (x64, Intel Skylake), MSVC 19.52.37029, `dev` preset. It merges cleanly with `main`. ```text ctest --preset dev --no-tests=error -R "^cpu_native_|pe_|static_exe_intake|conditional_import|bundled_export" -j 4 100% tests passed, 0 tests failed out of 30 ``` The Windows-side observations of the new TLS-data test hold on this build. The TLS index numbers differ from a 26300 run, as expected for loader state: ```text cpu_native_tls_data: native and mapped agree on 10 observations: the loading thread, a thread that existed before the load and a new thread each start from the template in a block of their own, aligned to 64 bytes; indexes native 7/8, mapped 8/9 native and mapped agree on copies with SizeOfZeroFill 0x10, 4096-byte alignment, no alignment pe_map rejects a template past the image, a template ending before its start, a template above 16 MiB; Windows fails the first two with errors 998 and 193 ``` `cpu_native_tls` (including the #543 index controls) and `cpu_native_tls_value` also pass on this build. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:48:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T02:23:49Z.


The native/mapped thread-local results and the two distinct TLS indexes are good evidence for the carrier approach. Before merging, please narrow the filesystem lifetime. load_carrier closes the write handle and reopens the carrier by name before loading it. Use a private directory or another way to ensure the path continues to identify the file this invocation wrote until the loader opens it. Please also remove wildcard cleanup of other processes’ carrier paths from the mapping operation, or establish ownership without relying on the file name and PID alone. Add a failure/cleanup control that verifies a failed map only disposes of its own carrier. Keep the separate existing-thread and new-thread comparisons.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7089904151 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/698#issuecomment-7089904151). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T02:23:49Z. --- The native/mapped thread-local results and the two distinct TLS indexes are good evidence for the carrier approach. Before merging, please narrow the filesystem lifetime. `load_carrier` closes the write handle and reopens the carrier by name before loading it. Use a private directory or another way to ensure the path continues to identify the file this invocation wrote until the loader opens it. Please also remove wildcard cleanup of other processes’ carrier paths from the mapping operation, or establish ownership without relying on the file name and PID alone. Add a failure/cleanup control that verifies a failed map only disposes of its own carrier. Keep the separate existing-thread and new-thread comparisons.
rasptum commented 2026-11-24 06:48:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-23T03:34:42Z.


Thanks for the review. All four points are addressed in 0dc025c, a new commit on top of 11987cd (nothing rewritten; the branch still merges cleanly with main). The PR description is updated with the details.

1. Filesystem lifetime. Each map call now creates its own directory under the temporary directory, pe_map_tls_<32 random hex digits>, with a protected DACL for the process's user, and holds it open without delete sharing. The carrier is written there with CREATE_NEW. The write handle still has to close before the load, because the loader's own open does not share writing. The carrier is then reopened for reading and deletion, shared for reading only, and held until release. The reopened file must have the file ID of the one written and read back the same bytes, and after LoadLibraryExW the file GetMappedFileNameW reports for the carrier must be the file behind the held handle. Each check fails the map with its own diagnostic.

Measured on Windows 10 build 19045 with a scratch program: while the two handles are held and before the load, opening the file for writing, renaming it, deleting it and renaming its directory fail with error 32, renaming the directory above fails with error 5, and LoadLibraryExW still loads the file. Not closed: between closing the write handle and the reopen, a process of the same user could replace the file. The ID and read-back checks would then fail the map rather than load the other file; no fixture provokes that race.

2. Wildcard cleanup. sweep_carriers is gone. A map call deletes only the file and directory it created, through the two handles it holds; nothing is deleted by name or pattern. As a consequence, a carrier left by a process that ended with an image still mapped is no longer removed by anyone. The test asserts that a later process leaves it alone, and the docs list it as a limit.

3. Failure/cleanup control. cpu_native_tls_data gives each child its own TMP/TEMP. Two failure cases run beside a file in the old carrier layout named after a process that has ended and a carrier directory in the new layout:

  • After write, before load: the child accepts only Microsoft-signed images from then on, so Windows refuses the carrier: Windows cannot load the TLS carrier (error 577).
  • After load: the new fixture cpu_tls_data_refuse reads its thread-locals in DllMain (template values, so the carrier is in place) and returns FALSE: entry point returned FALSE. Windows fails the same DLL with error 1114 after the same observation.

Both remove their own carrier and leave the two other files byte for byte. Mutations of pe_map.cpp, each rebuilt and run on the final code:

Mutation First failing check
old sweep_carriers restored verbatim signed-only: the old-layout file is deleted
every pe_map_tls_* directory removed an ended process's carrier is gone after a later map
failed map forgets its carrier instead of cleaning up signed-only: a third carrier directory is left
held handles shared for deletion renaming the directory while mapped fails with error 5 instead of 32
held handle closed before the load carriers left after release
carrier not deleted on release carriers left after release

4. Thread comparisons. The loading-thread, existing-thread and new-thread comparisons are unchanged and pass.

Windows 10 Pro 22H2 build 19045, MSVC 19.44.35207.1 (x64), dev preset, on main at 3b9bb1a:

ctest --preset dev --no-tests=error -R '^cpu_native_tls_data$' -V
ctest --preset dev --no-tests=error -R '^cpu_native_tls' --repeat until-fail:10
ctest --preset dev --no-tests=error -R '^cpu_native_|pe_|static_exe_intake|conditional_import|bundled_export' -j 4
while mapped, renaming the carrier or its directory and opening it for writing fail with error 32; nothing is left after release; a carrier left by an ended process is not touched by a later map
failed maps remove only their own carrier, beside an ended process's old-layout file and another carrier directory: the carrier was written and Windows refused it: Windows cannot load the TLS carrier (error 577); the carrier was loaded and the entry point refused: entry point returned FALSE; Windows fails the refusing fixture with error 1114 after the same attach
100% tests passed, 0 tests failed out of 3      (10 runs in a row)
100% tests passed, 0 tests failed out of 30

The full suite results are in the PR description; none of its failures is in this area. This commit has not been run on Windows 11; the build 26100 run by legacy-code-archaeologist covers 11987cd.

AI-assisted (Claude Code); the outputs above are copied from local runs.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7090854936 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/698#issuecomment-7090854936). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T03:34:42Z. --- Thanks for the review. All four points are addressed in `0dc025c`, a new commit on top of `11987cd` (nothing rewritten; the branch still merges cleanly with `main`). The PR description is updated with the details. **1. Filesystem lifetime.** Each map call now creates its own directory under the temporary directory, `pe_map_tls_<32 random hex digits>`, with a protected DACL for the process's user, and holds it open without delete sharing. The carrier is written there with `CREATE_NEW`. The write handle still has to close before the load, because the loader's own open does not share writing. The carrier is then reopened for reading and deletion, shared for reading only, and held until release. The reopened file must have the file ID of the one written and read back the same bytes, and after `LoadLibraryExW` the file `GetMappedFileNameW` reports for the carrier must be the file behind the held handle. Each check fails the map with its own diagnostic. Measured on Windows 10 build 19045 with a scratch program: while the two handles are held and before the load, opening the file for writing, renaming it, deleting it and renaming its directory fail with error 32, renaming the directory above fails with error 5, and `LoadLibraryExW` still loads the file. Not closed: between closing the write handle and the reopen, a process of the same user could replace the file. The ID and read-back checks would then fail the map rather than load the other file; no fixture provokes that race. **2. Wildcard cleanup.** `sweep_carriers` is gone. A map call deletes only the file and directory it created, through the two handles it holds; nothing is deleted by name or pattern. As a consequence, a carrier left by a process that ended with an image still mapped is no longer removed by anyone. The test asserts that a later process leaves it alone, and the docs list it as a limit. **3. Failure/cleanup control.** `cpu_native_tls_data` gives each child its own `TMP`/`TEMP`. Two failure cases run beside a file in the old carrier layout named after a process that has ended and a carrier directory in the new layout: - After write, before load: the child accepts only Microsoft-signed images from then on, so Windows refuses the carrier: `Windows cannot load the TLS carrier (error 577)`. - After load: the new fixture `cpu_tls_data_refuse` reads its thread-locals in `DllMain` (template values, so the carrier is in place) and returns FALSE: `entry point returned FALSE`. Windows fails the same DLL with error 1114 after the same observation. Both remove their own carrier and leave the two other files byte for byte. Mutations of `pe_map.cpp`, each rebuilt and run on the final code: | Mutation | First failing check | |---|---| | old `sweep_carriers` restored verbatim | signed-only: the old-layout file is deleted | | every `pe_map_tls_*` directory removed | an ended process's carrier is gone after a later map | | failed map forgets its carrier instead of cleaning up | signed-only: a third carrier directory is left | | held handles shared for deletion | renaming the directory while mapped fails with error 5 instead of 32 | | held handle closed before the load | carriers left after release | | carrier not deleted on release | carriers left after release | **4. Thread comparisons.** The loading-thread, existing-thread and new-thread comparisons are unchanged and pass. Windows 10 Pro 22H2 build 19045, MSVC 19.44.35207.1 (x64), `dev` preset, on `main` at `3b9bb1a`: ```powershell ctest --preset dev --no-tests=error -R '^cpu_native_tls_data$' -V ctest --preset dev --no-tests=error -R '^cpu_native_tls' --repeat until-fail:10 ctest --preset dev --no-tests=error -R '^cpu_native_|pe_|static_exe_intake|conditional_import|bundled_export' -j 4 ``` ```text while mapped, renaming the carrier or its directory and opening it for writing fail with error 32; nothing is left after release; a carrier left by an ended process is not touched by a later map failed maps remove only their own carrier, beside an ended process's old-layout file and another carrier directory: the carrier was written and Windows refused it: Windows cannot load the TLS carrier (error 577); the carrier was loaded and the entry point refused: entry point returned FALSE; Windows fails the refusing fixture with error 1114 after the same attach 100% tests passed, 0 tests failed out of 3 (10 runs in a row) 100% tests passed, 0 tests failed out of 30 ``` The full suite results are in the PR description; none of its failures is in this area. This commit has not been run on Windows 11; the build 26100 run by legacy-code-archaeologist covers `11987cd`. AI-assisted (Claude Code); the outputs above are copied from local runs.
rasptum commented 2026-11-24 06:49:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-23T04:50:10Z.


Follow-up for the record (CI passed on f618e68, both Windows jobs): in both Windows jobs, the "carrier refused after it was written" control hung until its 60 s timeout on the windows-2025 runner. It finished in about a second on Windows 10 build 19045. That child set the Microsoft-signed-only policy, which refuses every non-Microsoft image the child loads from then on, not only the carrier. I did not find out what it was waiting for on the runner.

f618e68 narrows the control. The child's temporary directory now gets an inherited low integrity label (icacls /setintegritylevel (OI)(CI)low), and the child sets NoLowMandatoryLabelImages, which refuses only images with that label. Only the carrier written there is affected: Windows cannot load the TLS carrier (error 5). A copy of the fixture placed in the same directory is the control: it loads without the policy and fails with error 5 under it. The restored old sweep and a failed map that skips its cleanup still fail the control. Nothing else changed; the description above already says this. The refusing-entry-point control ran after this one, so the runner had not reached it before.

ctest --preset dev --no-tests=error -R '^cpu_native_tls' --repeat until-fail:5
100% tests passed, 0 tests failed out of 3

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7091905742 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/698#issuecomment-7091905742). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T04:50:10Z. --- Follow-up for the record (CI passed on `f618e68`, both Windows jobs): in both Windows jobs, the "carrier refused after it was written" control hung until its 60 s timeout on the windows-2025 runner. It finished in about a second on Windows 10 build 19045. That child set the Microsoft-signed-only policy, which refuses every non-Microsoft image the child loads from then on, not only the carrier. I did not find out what it was waiting for on the runner. `f618e68` narrows the control. The child's temporary directory now gets an inherited low integrity label (`icacls /setintegritylevel (OI)(CI)low`), and the child sets `NoLowMandatoryLabelImages`, which refuses only images with that label. Only the carrier written there is affected: `Windows cannot load the TLS carrier (error 5)`. A copy of the fixture placed in the same directory is the control: it loads without the policy and fails with error 5 under it. The restored old sweep and a failed map that skips its cleanup still fail the control. Nothing else changed; the description above already says this. The refusing-entry-point control ran after this one, so the runner had not reached it before. ```powershell ctest --preset dev --no-tests=error -R '^cpu_native_tls' --repeat until-fail:5 ``` ```text 100% tests passed, 0 tests failed out of 3 ``` AI-assisted (Claude Code).
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!692
No description provided.