pe: notify a started EXE's TLS callbacks on thread attach and detach #913

Merged
rasptum merged 2 commits from upstream-pr/920 into main 2026-11-23 17:40:00 +00:00
rasptum commented 2026-11-23 17:10:23 +00:00 (Migrated from github.com)

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


Refs #165, Refs #870

What this changes

This is the source-built EXE comparison still open on #165: TLS notifications on worker threads and a startup log for an EXE's own image. Static intake on #870 reported two TLS callbacks in the main executable itself, not in a DLL.

tools/pe_map.cpp (minimal): pe_map_run called an EXE's TLS callbacks only for process attach. notify() returns early for an image without IMAGE_FILE_DLL, so threads a mapped EXE started never reached its callbacks. Under a Windows launch, an EXE's TLS callbacks receive thread attach and detach. Now pe_map_run marks the EXE's registration as started. From then until release, pe_map.dll's thread notifications call the EXE's TLS callbacks, never its entry point, with DLL_THREAD_ATTACH and DLL_THREAD_DETACH. A mapped EXE that was never started still receives nothing, as under LoadLibrary. The cookie path from #899 is untouched.

Fixture cpu_exe_tls (tests/cpu/exe_tls.cpp, CRT-free, MSVC): the EXE supplies its own TLS directory (_tls_used, _tls_index, .tls markers, .CRT$XLA/XLZ). Its template holds the int and 8 KiB array from #883, so it spans a page boundary. Callbacks are in .CRT$XLB and .CRT$XLM. The entry point writes 20 to the main thread's copy and starts two workers that are alive at the same time: A writes 50, and B starts after A's write and writes 60. It then joins A, then B, so the order is fixed, and prints one ordered log.

Test cpu_native_exe_tls (tests/cpu/native_exe_tls_check.py) reuses the #883 helpers (layout, swap_callbacks, zero_second_page). It launches the EXE with Windows, then maps it with pe_map_image and starts it with pe_map_run in a child process. The log and the exit code must be identical and must equal the expected log:

LOG:m11=7 m13=7 E mf=7 mw=20014 a21=7 a23=7 af=7 aw=20032 b21=7 b23=7 bf=7 bw=2003c al=20032 a31=20032 a33=20032 bl=2003c b31=2003c b33=2003c ml=20014 X=20014
exit 0x20014

Each callback event reads thread, reason, caller. m11 is the main thread on process attach, caller 1. a2x/a3x and b2x/b3x are the workers' thread attach and detach. E is the entry point. f/w/l are a thread's own first, written and last observations.

Negative controls: both are rejected under both loaders.

  • Swapped callback table: callback order: m1 ran callers ['3', '1'].
  • Template zeroed from its first page boundary: template: callbacks on m1 observed ['4d40007', '4d40007'], expected ['7', '7'] (dev).

Without the pe_map.cpp change, using the mapper from current main, the test fails with mapped: notifications for ['m1'], expected ['a2', 'a3', 'b2', 'b3', 'm1'].

Test

Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1 x64, Ninja.

cmake --preset dev
cmake --build --preset dev --target pe_map cpu_exe_tls
ctest --preset dev --no-tests=error -R '^cpu_native_exe_tls$' -V
cmake --preset release
cmake --build --preset release --target pe_map cpu_exe_tls
ctest --preset release --no-tests=error -R '^cpu_native_exe_tls$' -V
python -B tests/form_coverage_check.py
python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py
  • Commit ef15a16 (rebased on main with #899):
    • ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|load_cookie)$' passed 8/8 on dev and 8/8 on release.
  • Final commit 41b1de6, rebased again after main moved 12 commits. The merge was clean, and those commits did not touch pe_map.cpp or the harness modules.
    • cpu_native_exe_tls, cpu_native_startup, cpu_native_exe_start and cpu_native_tls_value passed 4/4 on dev and on release.
    • The three Python checks pass.
  • No capability skips.

Environment note: the C: system drive filled up during the first release run on ef15a16. cpu_native_tls_data and cpu_native_tls_value then failed with Windows error 112 while writing the TLS carrier to %TEMP%. Rerunning with TMP/TEMP on another drive passed 8/8. This was a host disk condition, not a code result.

Scope

This checks the EXE TLS stage ahead of the current #870 boundary (import binding, #880). It does not claim the executable reaches TLS or its entry point. Not covered: process detach at exit, C-runtime startup in a mapped EXE, GetModuleHandle(NULL) and the PEB.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6564641507 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/920). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T17:10:23Z. --- Refs #165, Refs #870 ## What this changes This is the source-built EXE comparison still open on #165: TLS notifications on worker threads and a startup log for an EXE's own image. Static intake on #870 reported two TLS callbacks in the main executable itself, not in a DLL. **`tools/pe_map.cpp` (minimal):** `pe_map_run` called an EXE's TLS callbacks only for process attach. `notify()` returns early for an image without `IMAGE_FILE_DLL`, so threads a mapped EXE started never reached its callbacks. Under a Windows launch, an EXE's TLS callbacks receive thread attach and detach. Now `pe_map_run` marks the EXE's registration as started. From then until release, `pe_map.dll`'s thread notifications call the EXE's TLS callbacks, never its entry point, with `DLL_THREAD_ATTACH` and `DLL_THREAD_DETACH`. A mapped EXE that was never started still receives nothing, as under `LoadLibrary`. The cookie path from #899 is untouched. **Fixture `cpu_exe_tls`** (`tests/cpu/exe_tls.cpp`, CRT-free, MSVC): the EXE supplies its own TLS directory (`_tls_used`, `_tls_index`, `.tls` markers, `.CRT$XLA`/`XLZ`). Its template holds the int and 8 KiB array from #883, so it spans a page boundary. Callbacks are in `.CRT$XLB` and `.CRT$XLM`. The entry point writes 20 to the main thread's copy and starts two workers that are alive at the same time: A writes 50, and B starts after A's write and writes 60. It then joins A, then B, so the order is fixed, and prints one ordered log. **Test `cpu_native_exe_tls`** (`tests/cpu/native_exe_tls_check.py`) reuses the #883 helpers (`layout`, `swap_callbacks`, `zero_second_page`). It launches the EXE with Windows, then maps it with `pe_map_image` and starts it with `pe_map_run` in a child process. The log and the exit code must be identical and must equal the expected log: ```text LOG:m11=7 m13=7 E mf=7 mw=20014 a21=7 a23=7 af=7 aw=20032 b21=7 b23=7 bf=7 bw=2003c al=20032 a31=20032 a33=20032 bl=2003c b31=2003c b33=2003c ml=20014 X=20014 exit 0x20014 ``` Each callback event reads thread, reason, caller. `m11` is the main thread on process attach, caller 1. `a2x`/`a3x` and `b2x`/`b3x` are the workers' thread attach and detach. `E` is the entry point. `f`/`w`/`l` are a thread's own first, written and last observations. **Negative controls:** both are rejected under both loaders. - Swapped callback table: `callback order: m1 ran callers ['3', '1']`. - Template zeroed from its first page boundary: `template: callbacks on m1 observed ['4d40007', '4d40007'], expected ['7', '7']` (dev). **Without the `pe_map.cpp` change**, using the mapper from current main, the test fails with `mapped: notifications for ['m1'], expected ['a2', 'a3', 'b2', 'b3', 'm1']`. ## Test Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1 x64, Ninja. ```powershell cmake --preset dev cmake --build --preset dev --target pe_map cpu_exe_tls ctest --preset dev --no-tests=error -R '^cpu_native_exe_tls$' -V cmake --preset release cmake --build --preset release --target pe_map cpu_exe_tls ctest --preset release --no-tests=error -R '^cpu_native_exe_tls$' -V python -B tests/form_coverage_check.py python -B tests/docs_links_check.py python -B tests/bidi_controls_check.py ``` - **Commit `ef15a16`** (rebased on main with #899): - `ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|load_cookie)$'` passed 8/8 on `dev` and 8/8 on `release`. - **Final commit `41b1de6`**, rebased again after main moved 12 commits. The merge was clean, and those commits did not touch `pe_map.cpp` or the harness modules. - `cpu_native_exe_tls`, `cpu_native_startup`, `cpu_native_exe_start` and `cpu_native_tls_value` passed 4/4 on `dev` and on `release`. - The three Python checks pass. - No capability skips. Environment note: the C: system drive filled up during the first `release` run on `ef15a16`. `cpu_native_tls_data` and `cpu_native_tls_value` then failed with Windows error 112 while writing the TLS carrier to `%TEMP%`. Rerunning with `TMP`/`TEMP` on another drive passed 8/8. This was a host disk condition, not a code result. ## Scope This checks the EXE TLS stage ahead of the current #870 boundary (import binding, #880). It does not claim the executable reaches TLS or its entry point. Not covered: process detach at exit, C-runtime startup in a mapped EXE, `GetModuleHandle(NULL)` and the PEB. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:45:25 +00:00
R0Y00 commented 2026-11-24 06:49:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:15:39Z.


The two joined workers and per-thread log are a good match for the remaining #165 fixture. One ordering boundary needs a control: started is currently published before process-attach TLS callbacks finish, so a host thread notification can enter the EXE's TLS callbacks before process initialization has completed. Please define and test when notifications become eligible. For the entry-created workers in this fixture, publish after ordered process-attach callbacks and before calling the entry, with host-side thread creation during startup outside that scoped guarantee. Verify a mapped-only or refused run gets no notifications and keep EXE process detach separate. The child only needs a fixed kernel32 provider; please bind that explicitly instead of loading whatever DLL names arrive from the fixture PE.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7108391233 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/920#issuecomment-7108391233). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T17:15:39Z. --- The two joined workers and per-thread log are a good match for the remaining #165 fixture. One ordering boundary needs a control: started is currently published before process-attach TLS callbacks finish, so a host thread notification can enter the EXE's TLS callbacks before process initialization has completed. Please define and test when notifications become eligible. For the entry-created workers in this fixture, publish after ordered process-attach callbacks and before calling the entry, with host-side thread creation during startup outside that scoped guarantee. Verify a mapped-only or refused run gets no notifications and keep EXE process detach separate. The child only needs a fixed kernel32 provider; please bind that explicitly instead of loading whatever DLL names arrive from the fixture PE.
rasptum commented 2026-11-24 06:49:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-23T17:24:03Z.


Thanks. Addressed in c631998 (a new commit on top, no force-push):

  1. Eligibility. pe_map_run now publishes started only after every process-attach TLS callback has returned, and before it calls the entry point. A thread that attaches during the process-attach callbacks is not notified, whether a callback or the host started it. That case is documented as out of scope in pe_map_run's comment and in docs/experiments.md, because Windows holds such a thread until process initialization ends. The control is cpu_exe_tls_early: its first process-attach callback starts and joins a probe thread. Under pe_map the probe runs (LOG:m11=7 pf=7 m13=7 E ...) and receives no EXE notification. With the previous ordering the test fails: notifications for [..., 'p2', 'p3'], and counts [0, 2, 6, 6] instead of [0, 2, 4, 4]. Because of that hold, the variant runs mapped only.
  2. Mapped-only and refused runs. The child gives the callbacks per-reason counters (exe_tls_mirror) and reads them after release. Two host threads attach and detach in each case. An EXE that is mapped but never started, and one whose pe_map_run is refused (null exit code, returns 0), both count [0, 0, 0, 0].
  3. Process detach kept separate. The EXE branch of notify_mapped forwards only DLL_THREAD_ATTACH/DLL_THREAD_DETACH. A started run counts 0 process detaches, 2 process attaches, 4 thread attaches and 4 thread detaches, including through release.
  4. Fixed kernel32 provider. The child binds only kernel32.dll to the host's kernel32. Any other name stays unresolved, so the map fails. The test checks that kernel32 was the only DLL requested.

Results: Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1. ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|load_cookie)$' passes 8/8 on dev and 8/8 on release. The native and mapped startup log and exit code are unchanged, and both negative controls are still rejected under both loaders. form_coverage_check, docs_links_check and bidi_controls_check pass. TMP/TEMP were on a non-system drive for these runs.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7108601746 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/920#issuecomment-7108601746). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T17:24:03Z. --- Thanks. Addressed in `c631998` (a new commit on top, no force-push): 1. **Eligibility.** `pe_map_run` now publishes `started` only after every process-attach TLS callback has returned, and before it calls the entry point. A thread that attaches during the process-attach callbacks is not notified, whether a callback or the host started it. That case is documented as out of scope in `pe_map_run`'s comment and in `docs/experiments.md`, because Windows holds such a thread until process initialization ends. The control is `cpu_exe_tls_early`: its first process-attach callback starts and joins a probe thread. Under `pe_map` the probe runs (`LOG:m11=7 pf=7 m13=7 E ...`) and receives no EXE notification. With the previous ordering the test fails: `notifications for [..., 'p2', 'p3']`, and counts `[0, 2, 6, 6]` instead of `[0, 2, 4, 4]`. Because of that hold, the variant runs mapped only. 2. **Mapped-only and refused runs.** The child gives the callbacks per-reason counters (`exe_tls_mirror`) and reads them after release. Two host threads attach and detach in each case. An EXE that is mapped but never started, and one whose `pe_map_run` is refused (null exit code, returns 0), both count `[0, 0, 0, 0]`. 3. **Process detach kept separate.** The EXE branch of `notify_mapped` forwards only `DLL_THREAD_ATTACH`/`DLL_THREAD_DETACH`. A started run counts 0 process detaches, 2 process attaches, 4 thread attaches and 4 thread detaches, including through release. 4. **Fixed kernel32 provider.** The child binds only `kernel32.dll` to the host's kernel32. Any other name stays unresolved, so the map fails. The test checks that kernel32 was the only DLL requested. **Results:** Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1. `ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|load_cookie)$'` passes 8/8 on `dev` and 8/8 on `release`. The native and mapped startup log and exit code are unchanged, and both negative controls are still rejected under both loaders. `form_coverage_check`, `docs_links_check` and `bidi_controls_check` pass. `TMP`/`TEMP` were on a non-system drive for these runs. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:31:35Z.


I reran the source-built comparison on c631998 with Windows x64 Release and MSVC 19.51. The original EXE reported exit 0x20014 and the same ordered log under Windows and pe_map. The early worker ran without a callback, and mapped-only and refused runs both recorded zero notifications. Both callback-order and cross-page template mutations failed under both loaders. This verifies the entry-created workers and the documented startup boundary. I will keep #165 open for its remaining coverage.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7108787321 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/920#issuecomment-7108787321). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T17:31:35Z. --- I reran the source-built comparison on c631998 with Windows x64 Release and MSVC 19.51. The original EXE reported exit 0x20014 and the same ordered log under Windows and pe_map. The early worker ran without a callback, and mapped-only and refused runs both recorded zero notifications. Both callback-order and cross-page template mutations failed under both loaders. This verifies the entry-created workers and the documented startup boundary. I will keep #165 open for its remaining coverage.
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!913
No description provided.