pe: install a main image's TLS block in slot 0 of every thread #981

Merged
rasptum merged 8 commits from upstream-pr/988 into main 2026-11-23 23:47:19 +00:00
rasptum commented 2026-11-23 20:03:25 +00:00 (Migrated from github.com)

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


Refs #870, Refs #165

This is the focused main-image TLS slot and per-thread initialization PR that R0Y00 asked for on #870 (18:29Z). It was reworked after review in 2134f9d (explicit participants), 419e859 (retryable release refusal), 685dce4 (0/1 release convention, notification gating), 440de93 (explicit guest-thread enrollment instead of process termination) and dc7bd04 (a failed ResumeThread refuses the creation). R0Y00 added 4af2ab0 (reclaiming terminated suspended enrollments, with the cancelled control). The rule and fixture shape are MoonYueya's (#870, 18:23Z and 19:17Z), and they offered to review. The code and fixtures here are this repository's own. It builds on #883 and #920 and is separate from #961 (ordinal-only imports).

Problem

Code compiled with /GA assumes its EXE owns TLS index 0. It reads thread-locals as mov rcx, gs:[58h] / mov rcx, [rcx], with no _tls_index load. pe_map serves the template through a carrier DLL with its own index, so a mapped /GA image reads and writes whatever block the host keeps in slot 0.

Change (tools/pe_map.cpp)

pe_map_run_main is pe_map_run for the image the host treats as its main executable. pe_map_run is unchanged.

Explicit participation, with an own-thread handshake. Only participating threads get the image's block in slot 0, and each thread swaps its own slot. No thread writes another thread's slot, and there is no sweep. The participants are:

  • the thread that calls pe_map_run_main, from before the process-attach callbacks until pe_map_release on that thread;
  • guest threads enrolled through pe_map_main_create_thread (below), from their DLL_THREAD_ATTACH (before any image is notified) to their DLL_THREAD_DETACH (after every image was notified, and before ntdll frees the thread's blocks);
  • host threads between their own pe_map_main_thread_enter and pe_map_main_thread_leave.

Explicit guest-thread enrollment. No thread is classified by inspecting it, and no thread notification can end the process. The host binds the main image's CreateThread imports to pe_map_main_create_thread, which has CreateThread's signature. While an image is started as the main image, it does the following:

  1. reserves an entry in a fixed 64-entry enrollment table;
  2. creates the thread suspended at a pe_map start routine;
  3. records its thread ID and a SYNCHRONIZE handle before it can run;
  4. resumes it unless the caller passed CREATE_SUSPENDED.

Only a thread found in that table installs the image's block at its DLL_THREAD_ATTACH. Every other thread returns at once, untouched. The pe_map start routine calls the guest's start routine only if the installation succeeded. Failures are reported before guest code runs:

  • Enrollment refused (no free entry, or the thread cannot be watched): pe_map_main_create_thread returns null with ERROR_NOT_ENOUGH_MEMORY and no thread exists.
  • Resume failed (ResumeThread returned DWORD(-1)): pe_map_main_create_thread returns null with ResumeThread's last error, never a suspended handle. The thread, still suspended at the pe_map start routine, is ended. Its entry and watch handle stay reserved until the handle is signaled, so release keeps refusing and the slot is not reused while the thread could exist.
  • Installation failed: the start routine returns 0xE0000201 without calling the guest's routine. The main image's callbacks are not notified, because the thread does not participate.

All three are counted in pe_map_main_tls_stats. Entries whose thread ended before its start routine (for example a suspended worker the guest terminated) are reclaimed once their handle is signaled (4af2ab0). pe_map_main_tls_inject forces the enrollment (1), installation (2) or resume (3) failure, for tests.

I chose enrollment over scoping the mode to a dedicated child process. Any rule that classifies a thread inside DLL_THREAD_ATTACH can fail at a point where the only choices are to run guest code with the wrong block or to kill the process. With enrollment the creator decides before the thread exists.

Supported thread entries.

  • The thread that calls pe_map_run_main.
  • Threads the image creates through a CreateThread import bound to pe_map_main_create_thread.
  • Host threads between enter (returns 0 when it cannot install) and leave.

Not supported, and running with the host's slot 0:

  • threads the image creates by any other route: a CreateThread address it looked up itself, CreateRemoteThread(Ex), NtCreateThreadEx, thread-pool work, or a C runtime's _beginthreadex in another DLL;
  • host threads that call the image without entering.

No allocation on attach. A participant keeps its previous slot 0 in pe_map TLS slots reserved at the start. They are below TLS_MINIMUM_AVAILABLE, so setting them never allocates. Its thread handle goes into a fixed 64-entry table.

Release is a retryable refusal. pe_map_release keeps its published 0/1 convention. It returns 0 and changes nothing while any participant other than the calling thread may still run the image's code. That covers:

  • a participant whose handle is not signaled or cannot be waited on;
  • a participant without a handle;
  • an enrolled thread that has not yet passed its start routine.

pe_map_main_tls_stats then reports last release 2. While refused, the image stays registered and mapped, its unwind data stays registered, its carrier stays loaded, and every participant keeps its slot. The caller retries.

An accepted release is checked and committed under one lock, so no participant can join in between. It does the following:

  1. gives the calling thread its saved slot 0 back;
  2. counts participants whose handle is signaled as confirmed exited;
  3. ends the mode;
  4. unregisters and frees the image exactly once.

Notifications. The main image's TLS callbacks receive DLL_THREAD_ATTACH and DLL_THREAD_DETACH only on participating threads, because a /GA callback on another thread would read the host's block. Other images, and EXEs started with pe_map_run, are notified as before.

On a participating thread the host's own index-0 code reads the image's block, so a host that owns slot 0 must not touch its thread-locals there. That rules out a Python host for this test: in Python 3.13, python313.dll carries static TLS.

Tests

cpu_exe_tls_slot0 is a CRT-free /GA EXE. Its template is an int (7) plus a 0x1400-byte array of 0xFF. Its TLS callback and each thread (the initial thread and two workers) check whether slot 0 equals the entry at _tls_index. Only when it does, a thread checks its template copy, writes the whole array, reads it back and calls HeapValidate. There are two variants:

  • cpu_exe_tls_slot0_pause adds worker c, which pauses inside the image's code on an event.
  • cpu_exe_tls_slot0_enroll first starts probe worker p, whose start routine touches a global sentinel as its first statement, and records a failed CreateThread instead of exiting.

Windows evidence and mapped evidence are in separate tests.

cpu_native_exe_tls_slot0_windows: the Windows launch only, where the EXE owns index 0:

LOG:m1=yy m:yyyy a2=yy a:yyyy a3=y- b2=yy b:yyyy b3=y- X=0

cpu_native_exe_tls_slot0 and cpu_native_exe_tls_slot0_enroll: pe_map only. cpu_exe_tls_slot0_host is a CRT-free host with its own TLS directory and a thread-local sentinel, so its block owns slot 0. It binds only kernel32.dll and runs each scenario as its own child process. Under pe_map_run_main it binds the guest's CreateThread to pe_map_main_create_thread. Under pe_map_run it binds kernel32's.

Non-participating host threads. Each scenario has two:

  • a reader that samples its own slot 0 and sentinel from before the map until after release;
  • a late thread created after the start, which checks its own slot 0 and sentinel.

Neither may ever see a change. The guest exports its TLS callback counts by reason (exe_slot0_callback_counts). Under pe_map_run_main, the callback must not run during the late thread's life. Under pe_map_run, the late thread's attach and detach still reach the guest.

A refused release returns 0 with the refusal reported, and the accepted release is the only one that returns 1. Every accepted release must leave the image no longer committed, RtlLookupFunctionEntry finding nothing for its entry point, and the carrier unloaded. A further release must return 0 (exactly one cleanup).

Counters, in order: installed, restored, exited, refused releases, pending, active, last release, untracked, enrollments refused, enrolled threads failed.

Scenario First release Then Guest thread callbacks (attach, detach); for the late thread Counters
main: a host thread enters and leaves 1 one cleanup 2,2; 0 4,4,0,0,0,0,1,0,0,0
alive: a host participant is still entered 0, refused, image, unwind data and carrier intact it leaves; 1, one cleanup 2,2; 0 4,4,0,1,0,0,1,0,0,0
exited: a host participant is ended with TerminateThread 1, exit confirmed one cleanup 2,2; 0 4,3,1,0,0,0,1,0,0,0
untracked: 64 host participants, the last finds the table full 0, refused (cannot inspect), intact it leaves; 1, one cleanup 2,2; 0 67,67,0,1,0,0,1,0,0,0
paused: guest worker c paused inside the image's code 0, refused, intact c resumes, finds its block, returns; 1, one cleanup 3,3; 0 5,5,0,1,0,0,1,0,0,0
plain: pe_map_run (negative control), every slot-0 check n, X=8 1 one cleanup 3,3; 2 0,0,0,0,0,0,-1,0,0,0

cpu_native_exe_tls_slot0_enroll covers forced enrollment failures. Each scenario also has a host participant, and the reader, late-thread, release and cleanup checks apply:

Scenario Probe: sentinel, created, last error, exit code Guest callbacks Counters
probe (control): p enrolled, log adds p2=yy p:yyyy p3=y- 1,1,0,0 3,3 5,5,0,0,0,0,1,0,0,0
enroll_refused: p's enrollment fails 0,0,8,0: CreateThread returned null, no thread 2,2 4,4,0,0,0,0,1,0,1,0
install_failed: installation on p fails 0,1,0,e0000201: p ended before its start routine 2,2 4,4,0,0,0,0,1,0,0,1
resume_failed: the resume of p fails 0,0,5,0: null with ResumeThread's error, p ended while suspended 2,2 4,4,0,0,0,0,1,0,1,0
cancelled (R0Y00): 65 suspended workers ended in turn, then p 1,1,0,0, log adds p2=yy p:yyyy p3=y- 3,3 5,5,0,0,0,0,1,0,0,65

The probe worker is created without CREATE_SUSPENDED, so the bound CreateThread performs the resume. In every failure scenario the sentinel stayed 0, the guest got no callback for p, workers a and b were enrolled normally afterwards, and the host process survived: it printed its report and exited 0.

  • paused is the paused-participant test R0Y00 asked for. alive and untracked are the deterministic controls for a participant that is still running or cannot be inspected.
  • Mutations (not committed). Each of these fails the test:
    • Release that never refuses: alive fails, and paused crashes with 0xC0000005.
    • No notification gate: main fails with guest totals 4,4.
    • Guest start routine called after a failed installation: install_failed fails with sentinel 1 and p:n---.
    • Previous __fastfail restored: install_failed fails because the child host dies with 0xC0000409.
    • A failed resume ignored, handle returned: resume_failed fails: the guest waits on a thread that never runs, and the child exits with 0xE0000003.

Tested

Commit dc7bd04. Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1 x64, TMP/TEMP on a non-system drive.

cmake --preset dev
cmake --build --preset dev
ctest --preset dev --no-tests=error -R '^cpu_native_exe_tls_slot0(_windows|_enroll)?$' --repeat until-fail:5 -V
cmake --preset release
cmake --build --preset release --target pe_map cpu_exe_tls_slot0 cpu_exe_tls_slot0_pause cpu_exe_tls_slot0_enroll cpu_exe_tls_slot0_host  # plus the neighbour tests' targets
ctest --preset release --no-tests=error -R '^cpu_native_exe_tls_slot0(_windows|_enroll)?$' --repeat until-fail:5 -V
python -B tests/form_coverage_check.py
python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py
  • The three TLS suites (^cpu_native_exe_tls_slot0(_windows|_enroll)?$) with --repeat until-fail:5: 5/5 on dev and 5/5 on release.
  • The neighbour set ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|exe_tls_slot0|exe_tls_slot0_windows|exe_tls_slot0_enroll|load_cookie)$': 11/11 on dev and 11/11 on release. This set includes the ordinary DLL notification tests and the pe_map_run EXE tests, which are unchanged.
    • One release run failed cpu_native_startup: its native Windows launch, which does not load pe_map, hit the 30 s timeout while a repository-wide scan was running alongside.
    • Rerun alone, that test passed 5/5, and the full set passed 11/11.
  • The three Python checks pass. No capability skips.
  • The branch is behind main but merges cleanly. main has not changed pe_map.cpp since the base.

Not covered

  • CRT EXEs and the exit path. A CRT EXE ends with ExitProcess, so slot 0 must be returned from a hook on the image's own ExitProcess import, and its CRT's thread-detach callback frees what it finds in slot 0 (MoonYueya's two pitfalls). R0Y00 suggested reviewing the exit path separately.
  • The unsupported thread entries listed above.
  • More than one main image per process.

This checks a loader stage. It makes no claim that the main executable runs.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6567739839 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T20:03:25Z. --- Refs #870, Refs #165 This is the focused main-image TLS slot and per-thread initialization PR that R0Y00 asked for on #870 (18:29Z). It was reworked after review in `2134f9d` (explicit participants), `419e859` (retryable release refusal), `685dce4` (0/1 release convention, notification gating), `440de93` (explicit guest-thread enrollment instead of process termination) and `dc7bd04` (a failed `ResumeThread` refuses the creation). R0Y00 added `4af2ab0` (reclaiming terminated suspended enrollments, with the `cancelled` control). The rule and fixture shape are MoonYueya's (#870, 18:23Z and 19:17Z), and they offered to review. The code and fixtures here are this repository's own. It builds on #883 and #920 and is separate from #961 (ordinal-only imports). ## Problem Code compiled with `/GA` assumes its EXE owns TLS index 0. It reads thread-locals as `mov rcx, gs:[58h]` / `mov rcx, [rcx]`, with no `_tls_index` load. `pe_map` serves the template through a carrier DLL with its own index, so a mapped `/GA` image reads and writes whatever block the host keeps in slot 0. ## Change (`tools/pe_map.cpp`) `pe_map_run_main` is `pe_map_run` for the image the host treats as its main executable. `pe_map_run` is unchanged. **Explicit participation, with an own-thread handshake.** Only participating threads get the image's block in slot 0, and each thread swaps its own slot. No thread writes another thread's slot, and there is no sweep. The participants are: - the thread that calls `pe_map_run_main`, from before the process-attach callbacks until `pe_map_release` on that thread; - guest threads enrolled through `pe_map_main_create_thread` (below), from their `DLL_THREAD_ATTACH` (before any image is notified) to their `DLL_THREAD_DETACH` (after every image was notified, and before ntdll frees the thread's blocks); - host threads between their own `pe_map_main_thread_enter` and `pe_map_main_thread_leave`. **Explicit guest-thread enrollment.** No thread is classified by inspecting it, and no thread notification can end the process. The host binds the main image's `CreateThread` imports to `pe_map_main_create_thread`, which has `CreateThread`'s signature. While an image is started as the main image, it does the following: 1. reserves an entry in a fixed 64-entry enrollment table; 2. creates the thread suspended at a pe_map start routine; 3. records its thread ID and a `SYNCHRONIZE` handle before it can run; 4. resumes it unless the caller passed `CREATE_SUSPENDED`. Only a thread found in that table installs the image's block at its `DLL_THREAD_ATTACH`. Every other thread returns at once, untouched. The pe_map start routine calls the guest's start routine only if the installation succeeded. Failures are reported before guest code runs: - **Enrollment refused** (no free entry, or the thread cannot be watched): `pe_map_main_create_thread` returns null with `ERROR_NOT_ENOUGH_MEMORY` and no thread exists. - **Resume failed** (`ResumeThread` returned `DWORD(-1)`): `pe_map_main_create_thread` returns null with `ResumeThread`'s last error, never a suspended handle. The thread, still suspended at the pe_map start routine, is ended. Its entry and watch handle stay reserved until the handle is signaled, so release keeps refusing and the slot is not reused while the thread could exist. - **Installation failed:** the start routine returns `0xE0000201` without calling the guest's routine. The main image's callbacks are not notified, because the thread does not participate. All three are counted in `pe_map_main_tls_stats`. Entries whose thread ended before its start routine (for example a suspended worker the guest terminated) are reclaimed once their handle is signaled (`4af2ab0`). `pe_map_main_tls_inject` forces the enrollment (1), installation (2) or resume (3) failure, for tests. I chose enrollment over scoping the mode to a dedicated child process. Any rule that classifies a thread inside `DLL_THREAD_ATTACH` can fail at a point where the only choices are to run guest code with the wrong block or to kill the process. With enrollment the creator decides before the thread exists. **Supported thread entries.** - The thread that calls `pe_map_run_main`. - Threads the image creates through a `CreateThread` import bound to `pe_map_main_create_thread`. - Host threads between enter (returns 0 when it cannot install) and leave. **Not supported**, and running with the host's slot 0: - threads the image creates by any other route: a `CreateThread` address it looked up itself, `CreateRemoteThread(Ex)`, `NtCreateThreadEx`, thread-pool work, or a C runtime's `_beginthreadex` in another DLL; - host threads that call the image without entering. **No allocation on attach.** A participant keeps its previous slot 0 in pe_map TLS slots reserved at the start. They are below `TLS_MINIMUM_AVAILABLE`, so setting them never allocates. Its thread handle goes into a fixed 64-entry table. **Release is a retryable refusal.** `pe_map_release` keeps its published 0/1 convention. It returns 0 and changes nothing while any participant other than the calling thread may still run the image's code. That covers: - a participant whose handle is not signaled or cannot be waited on; - a participant without a handle; - an enrolled thread that has not yet passed its start routine. `pe_map_main_tls_stats` then reports last release 2. While refused, the image stays registered and mapped, its unwind data stays registered, its carrier stays loaded, and every participant keeps its slot. The caller retries. An accepted release is checked and committed under one lock, so no participant can join in between. It does the following: 1. gives the calling thread its saved slot 0 back; 2. counts participants whose handle is signaled as confirmed exited; 3. ends the mode; 4. unregisters and frees the image exactly once. **Notifications.** The main image's TLS callbacks receive `DLL_THREAD_ATTACH` and `DLL_THREAD_DETACH` only on participating threads, because a `/GA` callback on another thread would read the host's block. Other images, and EXEs started with `pe_map_run`, are notified as before. On a participating thread the host's own index-0 code reads the image's block, so a host that owns slot 0 must not touch its thread-locals there. That rules out a Python host for this test: in Python 3.13, `python313.dll` carries static TLS. ## Tests `cpu_exe_tls_slot0` is a CRT-free `/GA` EXE. Its template is an int (7) plus a `0x1400`-byte array of `0xFF`. Its TLS callback and each thread (the initial thread and two workers) check whether slot 0 equals the entry at `_tls_index`. Only when it does, a thread checks its template copy, writes the whole array, reads it back and calls `HeapValidate`. There are two variants: - `cpu_exe_tls_slot0_pause` adds worker c, which pauses inside the image's code on an event. - `cpu_exe_tls_slot0_enroll` first starts probe worker p, whose start routine touches a global sentinel as its first statement, and records a failed `CreateThread` instead of exiting. Windows evidence and mapped evidence are in separate tests. **`cpu_native_exe_tls_slot0_windows`**: the Windows launch only, where the EXE owns index 0: ```text LOG:m1=yy m:yyyy a2=yy a:yyyy a3=y- b2=yy b:yyyy b3=y- X=0 ``` **`cpu_native_exe_tls_slot0`** and **`cpu_native_exe_tls_slot0_enroll`**: `pe_map` only. `cpu_exe_tls_slot0_host` is a CRT-free host with its own TLS directory and a thread-local sentinel, so its block owns slot 0. It binds only `kernel32.dll` and runs each scenario as its own child process. Under `pe_map_run_main` it binds the guest's `CreateThread` to `pe_map_main_create_thread`. Under `pe_map_run` it binds kernel32's. **Non-participating host threads.** Each scenario has two: - a reader that samples its own slot 0 and sentinel from before the map until after release; - a late thread created after the start, which checks its own slot 0 and sentinel. Neither may ever see a change. The guest exports its TLS callback counts by reason (`exe_slot0_callback_counts`). Under `pe_map_run_main`, the callback must not run during the late thread's life. Under `pe_map_run`, the late thread's attach and detach still reach the guest. A refused release returns 0 with the refusal reported, and the accepted release is the only one that returns 1. Every accepted release must leave the image no longer committed, `RtlLookupFunctionEntry` finding nothing for its entry point, and the carrier unloaded. A further release must return 0 (exactly one cleanup). Counters, in order: installed, restored, exited, refused releases, pending, active, last release, untracked, enrollments refused, enrolled threads failed. | Scenario | First release | Then | Guest thread callbacks (attach, detach); for the late thread | Counters | |---|---|---|---|---| | `main`: a host thread enters and leaves | 1 | one cleanup | `2,2`; 0 | `4,4,0,0,0,0,1,0,0,0` | | `alive`: a host participant is still entered | 0, refused, image, unwind data and carrier intact | it leaves; 1, one cleanup | `2,2`; 0 | `4,4,0,1,0,0,1,0,0,0` | | `exited`: a host participant is ended with `TerminateThread` | 1, exit confirmed | one cleanup | `2,2`; 0 | `4,3,1,0,0,0,1,0,0,0` | | `untracked`: 64 host participants, the last finds the table full | 0, refused (cannot inspect), intact | it leaves; 1, one cleanup | `2,2`; 0 | `67,67,0,1,0,0,1,0,0,0` | | `paused`: guest worker c paused inside the image's code | 0, refused, intact | c resumes, finds its block, returns; 1, one cleanup | `3,3`; 0 | `5,5,0,1,0,0,1,0,0,0` | | `plain`: `pe_map_run` (negative control), every slot-0 check `n`, `X=8` | 1 | one cleanup | `3,3`; 2 | `0,0,0,0,0,0,-1,0,0,0` | `cpu_native_exe_tls_slot0_enroll` covers forced enrollment failures. Each scenario also has a host participant, and the reader, late-thread, release and cleanup checks apply: | Scenario | Probe: sentinel, created, last error, exit code | Guest callbacks | Counters | |---|---|---|---| | `probe` (control): p enrolled, log adds `p2=yy p:yyyy p3=y-` | `1,1,0,0` | `3,3` | `5,5,0,0,0,0,1,0,0,0` | | `enroll_refused`: p's enrollment fails | `0,0,8,0`: `CreateThread` returned null, no thread | `2,2` | `4,4,0,0,0,0,1,0,1,0` | | `install_failed`: installation on p fails | `0,1,0,e0000201`: p ended before its start routine | `2,2` | `4,4,0,0,0,0,1,0,0,1` | | `resume_failed`: the resume of p fails | `0,0,5,0`: null with `ResumeThread`'s error, p ended while suspended | `2,2` | `4,4,0,0,0,0,1,0,1,0` | | `cancelled` (R0Y00): 65 suspended workers ended in turn, then p | `1,1,0,0`, log adds `p2=yy p:yyyy p3=y-` | `3,3` | `5,5,0,0,0,0,1,0,0,65` | The probe worker is created without `CREATE_SUSPENDED`, so the bound `CreateThread` performs the resume. In every failure scenario the sentinel stayed 0, the guest got no callback for p, workers a and b were enrolled normally afterwards, and the host process survived: it printed its report and exited 0. - **`paused`** is the paused-participant test R0Y00 asked for. **`alive` and `untracked`** are the deterministic controls for a participant that is still running or cannot be inspected. - **Mutations (not committed).** Each of these fails the test: - **Release that never refuses:** `alive` fails, and `paused` crashes with `0xC0000005`. - **No notification gate:** `main` fails with guest totals `4,4`. - **Guest start routine called after a failed installation:** `install_failed` fails with sentinel 1 and `p:n---`. - **Previous `__fastfail` restored:** `install_failed` fails because the child host dies with `0xC0000409`. - **A failed resume ignored, handle returned:** `resume_failed` fails: the guest waits on a thread that never runs, and the child exits with `0xE0000003`. ## Tested Commit `dc7bd04`. Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1 x64, `TMP`/`TEMP` on a non-system drive. ```powershell cmake --preset dev cmake --build --preset dev ctest --preset dev --no-tests=error -R '^cpu_native_exe_tls_slot0(_windows|_enroll)?$' --repeat until-fail:5 -V cmake --preset release cmake --build --preset release --target pe_map cpu_exe_tls_slot0 cpu_exe_tls_slot0_pause cpu_exe_tls_slot0_enroll cpu_exe_tls_slot0_host # plus the neighbour tests' targets ctest --preset release --no-tests=error -R '^cpu_native_exe_tls_slot0(_windows|_enroll)?$' --repeat until-fail:5 -V python -B tests/form_coverage_check.py python -B tests/docs_links_check.py python -B tests/bidi_controls_check.py ``` - **The three TLS suites** (`^cpu_native_exe_tls_slot0(_windows|_enroll)?$`) with `--repeat until-fail:5`: 5/5 on `dev` and 5/5 on `release`. - **The neighbour set** `ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|exe_tls_slot0|exe_tls_slot0_windows|exe_tls_slot0_enroll|load_cookie)$'`: 11/11 on `dev` and 11/11 on `release`. This set includes the ordinary DLL notification tests and the `pe_map_run` EXE tests, which are unchanged. - One `release` run failed `cpu_native_startup`: its native Windows launch, which does not load pe_map, hit the 30 s timeout while a repository-wide scan was running alongside. - Rerun alone, that test passed 5/5, and the full set passed 11/11. - The three Python checks pass. No capability skips. - The branch is behind `main` but merges cleanly. `main` has not changed `pe_map.cpp` since the base. ## Not covered - CRT EXEs and the exit path. A CRT EXE ends with `ExitProcess`, so slot 0 must be returned from a hook on the image's own `ExitProcess` import, and its CRT's thread-detach callback frees what it finds in slot 0 (MoonYueya's two pitfalls). R0Y00 suggested reviewing the exit path separately. - The unsupported thread entries listed above. - More than one main image per process. This checks a loader stage. It makes no claim that the main executable runs. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:45:43 +00:00
R0Y00 commented 2026-11-24 06:49:37 +00:00 (Migrated from github.com)

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


The source-built /GA fixture and the independent host TLS checks are useful. This entry still cannot safely accept every live thread. During install, an unsuccessful enumeration or inspection is treated as an empty result. During release, a failed OpenThread or inspection is counted as a dead thread, even though its slot may remain installed. That path can report success and unload the carrier while an existing thread may still use the block. DLL_THREAD_ATTACH also continues after allocation failure with the host slot still in place.

Please start with a controlled host and explicit participation of threads allowed to execute the mapped image, with a safe install/restore handshake. If inspection or restore is uncertain, distinguish it from confirmed exit and preserve the backing state. Add a deterministic failure control and a host-TLS reader that proves no unowned thread is modified. Keep native and mapped startup evidence separate.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7112671905 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7112671905). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T20:10:22Z. --- The source-built /GA fixture and the independent host TLS checks are useful. This entry still cannot safely accept every live thread. During install, an unsuccessful enumeration or inspection is treated as an empty result. During release, a failed OpenThread or inspection is counted as a dead thread, even though its slot may remain installed. That path can report success and unload the carrier while an existing thread may still use the block. DLL_THREAD_ATTACH also continues after allocation failure with the host slot still in place. Please start with a controlled host and explicit participation of threads allowed to execute the mapped image, with a safe install/restore handshake. If inspection or restore is uncertain, distinguish it from confirmed exit and preserve the backing state. Add a deterministic failure control and a host-TLS reader that proves no unowned thread is modified. Keep native and mapped startup evidence separate.
legacy-code-archaeologist commented 2026-11-24 06:49:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T20:19:49Z.


Independent run of a472aba on Windows 11 (the PR was run on Windows 10): Windows 11 Pro 26100, Intel Core i7-6820HQ, MSVC 19.52, dev preset, host Python 3.12. The head merges cleanly with main at 2f2893f (git merge-tree).

ctest --preset dev --no-tests=error -R '^cpu_native_exe_tls_slot0$' -V

Result: passes. The guest log of the main-image run equals the native launch, and the plain pe_map_run control reports no on every slot-0 check:

native: LOG:m1=yy m:yyyy a2=yy a:yyyy a3=y- b2=yy b:yyyy b3=y- X=0 exit 0x0
main image: LOG:m1=yy m:yyyy a2=yy a:yyyy a3=y- b2=yy b:yyyy b3=y- X=0
main image: HOST: mode=main started=1 exit=0 installed_main=y installed_existing=y reuse=n stale_dropped_on_reuse=n host_tls=y existing_host_tls=y heap_valid=y stats=5,4,1,0,0,0
plain pe_map_run (control): LOG:m1=n- m:n--- a2=n- a:n--- a3=n- b2=n- b:n--- b3=n- X=8

Not covered here: this host did not reuse the terminated thread's ID within the attempts (reuse=n). The stale record was dropped at release instead, so the reuse path is not covered by this run.

Wider selection: tls|native_exe|pe_map|static_exe passes 12/12, with no skips. I did not run the release preset here.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7112914948 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7112914948). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T20:19:49Z. --- Independent run of `a472aba` on Windows 11 (the PR was run on Windows 10): Windows 11 Pro 26100, Intel Core i7-6820HQ, MSVC 19.52, `dev` preset, host Python 3.12. The head merges cleanly with `main` at `2f2893f` (`git merge-tree`). ```powershell ctest --preset dev --no-tests=error -R '^cpu_native_exe_tls_slot0$' -V ``` **Result:** passes. The guest log of the main-image run equals the native launch, and the plain `pe_map_run` control reports no on every slot-0 check: ```text native: LOG:m1=yy m:yyyy a2=yy a:yyyy a3=y- b2=yy b:yyyy b3=y- X=0 exit 0x0 main image: LOG:m1=yy m:yyyy a2=yy a:yyyy a3=y- b2=yy b:yyyy b3=y- X=0 main image: HOST: mode=main started=1 exit=0 installed_main=y installed_existing=y reuse=n stale_dropped_on_reuse=n host_tls=y existing_host_tls=y heap_valid=y stats=5,4,1,0,0,0 plain pe_map_run (control): LOG:m1=n- m:n--- a2=n- a:n--- a3=n- b2=n- b:n--- b3=n- X=8 ``` **Not covered here:** this host did not reuse the terminated thread's ID within the attempts (`reuse=n`). The stale record was dropped at release instead, so the reuse path is not covered by this run. **Wider selection:** `tls|native_exe|pe_map|static_exe` passes 12/12, with no skips. I did not run the `release` preset here. AI-assisted (Claude Code).
rasptum commented 2026-11-24 06:49:38 +00:00 (Migrated from github.com)

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


Thanks. Reworked in 2134f9d, a new commit on top with no force-push. The PR description above still describes the first version (sweep of all threads); the updated model is in docs/experiments.md, "Main-image TLS slot 0".

  1. Explicit participation and a safe handshake.
    • There is no sweep, and no thread writes another thread's slot.
    • The participants are: the thread that calls pe_map_run_main (until release on that thread); threads the image starts (Windows start address inside the image), from their attach before any image is notified to their detach after every image was notified; and host threads between their own pe_map_main_thread_enter and pe_map_main_thread_leave.
    • Each participant saves and restores its own slot 0.
  2. Uncertain outcomes are distinct from confirmed exit, and the backing is kept.
    • Each participant's thread handle goes into a fixed table, so release is no longer based on OpenThread.
    • At release, each participant is restored, confirmed exited (its handle is signaled), or uncertain. Uncertain covers: still running, untracked (table full or handle not duplicated), an attaching thread whose start address could not be read, or a slot 0 that no longer held the image's block.
    • Any uncertain outcome reports an incomplete release (pe_map_main_tls_stats last release 0) and keeps the carrier loaded. The participant's own later leave or detach still restores it, and a new main image is refused while it is pending.
  3. No allocation on DLL_THREAD_ATTACH.
    • The saved block lives in pe_map TLS slots reserved at start, below TLS_MINIMUM_AVAILABLE, so setting them never allocates. Handles go in the fixed table.
    • A failed handle duplication or a full table makes the participant untracked, which fails the release closed instead of leaving the host's slot in place.
    • A thread whose start address cannot be read is counted as a fault, which also fails the release closed.
  4. Deterministic failure controls, one process per scenario. Counters are installed, restored, exited, uncertain, pending, active, last release and untracked.
    • alive: a host participant is inside the image at release → uncertain, carrier kept. Counters 4,4,0,1,0,0,0,0.
    • untracked: 64 host participants, so the last finds the table full and cannot be inspected → uncertain, carrier kept. Counters 67,67,0,1,0,0,0,0.
    • exited: a participant is ended with TerminateThread → exit confirmed from its handle, release complete. Counters 4,3,1,0,0,0,1,0.
    • main: complete release, carrier unloaded. Counters 4,4,0,0,0,0,1,0.
    • plain (control): pe_map_run, every slot-0 check n.
    • Mutations, not committed: counting a still-running participant as exited, or ignoring untracked participants, each fails the test because the carrier is unloaded and the release is reported complete.
  5. Host-TLS reader. In every scenario, a host thread that never participates samples its own slot 0 and its thread-local sentinel from before the map until after release. Neither changed in any run, over tens of thousands of samples per scenario in release and millions in dev.
  6. Separate evidence. cpu_native_exe_tls_slot0_windows covers only the Windows launch. cpu_native_exe_tls_slot0 covers only the pe_map scenarios.

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|exe_tls_slot0|exe_tls_slot0_windows|load_cookie)$' passed 10/10 on dev and 10/10 on release.
  • form_coverage_check, docs_links_check and bidi_controls_check pass.
  • One dev run failed untracked from a stale pe_map.dll: restoring the source after the mutation runs kept the file's old timestamp, so Ninja skipped the rebuild. After a rebuild it passed 10/10 untracked runs and the full set again.
  • The branch is behind main but merges cleanly; main has not changed pe_map.cpp since the base.

Still not covered: CRT EXEs and the ExitProcess exit path, threads that start in host code and call into the image without entering, and more than one main image.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7113148830 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7113148830). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T20:29:51Z. --- Thanks. Reworked in `2134f9d`, a new commit on top with no force-push. The PR description above still describes the first version (sweep of all threads); the updated model is in `docs/experiments.md`, "Main-image TLS slot 0". 1. **Explicit participation and a safe handshake.** - There is no sweep, and no thread writes another thread's slot. - The participants are: the thread that calls `pe_map_run_main` (until release on that thread); threads the image starts (Windows start address inside the image), from their attach before any image is notified to their detach after every image was notified; and host threads between their own `pe_map_main_thread_enter` and `pe_map_main_thread_leave`. - Each participant saves and restores its own slot 0. 2. **Uncertain outcomes are distinct from confirmed exit, and the backing is kept.** - Each participant's thread handle goes into a fixed table, so release is no longer based on `OpenThread`. - At release, each participant is restored, confirmed exited (its handle is signaled), or uncertain. Uncertain covers: still running, untracked (table full or handle not duplicated), an attaching thread whose start address could not be read, or a slot 0 that no longer held the image's block. - Any uncertain outcome reports an incomplete release (`pe_map_main_tls_stats` last release 0) and keeps the carrier loaded. The participant's own later leave or detach still restores it, and a new main image is refused while it is pending. 3. **No allocation on `DLL_THREAD_ATTACH`.** - The saved block lives in pe_map TLS slots reserved at start, below `TLS_MINIMUM_AVAILABLE`, so setting them never allocates. Handles go in the fixed table. - A failed handle duplication or a full table makes the participant untracked, which fails the release closed instead of leaving the host's slot in place. - A thread whose start address cannot be read is counted as a fault, which also fails the release closed. 4. **Deterministic failure controls, one process per scenario.** Counters are installed, restored, exited, uncertain, pending, active, last release and untracked. - `alive`: a host participant is inside the image at release → uncertain, carrier kept. Counters `4,4,0,1,0,0,0,0`. - `untracked`: 64 host participants, so the last finds the table full and cannot be inspected → uncertain, carrier kept. Counters `67,67,0,1,0,0,0,0`. - `exited`: a participant is ended with `TerminateThread` → exit confirmed from its handle, release complete. Counters `4,3,1,0,0,0,1,0`. - `main`: complete release, carrier unloaded. Counters `4,4,0,0,0,0,1,0`. - `plain` (control): `pe_map_run`, every slot-0 check `n`. - Mutations, not committed: counting a still-running participant as exited, or ignoring untracked participants, each fails the test because the carrier is unloaded and the release is reported complete. 5. **Host-TLS reader.** In every scenario, a host thread that never participates samples its own slot 0 and its thread-local sentinel from before the map until after release. Neither changed in any run, over tens of thousands of samples per scenario in `release` and millions in `dev`. 6. **Separate evidence.** `cpu_native_exe_tls_slot0_windows` covers only the Windows launch. `cpu_native_exe_tls_slot0` covers only the `pe_map` scenarios. **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|exe_tls_slot0|exe_tls_slot0_windows|load_cookie)$'` passed 10/10 on `dev` and 10/10 on `release`. - `form_coverage_check`, `docs_links_check` and `bidi_controls_check` pass. - One `dev` run failed `untracked` from a stale `pe_map.dll`: restoring the source after the mutation runs kept the file's old timestamp, so Ninja skipped the rebuild. After a rebuild it passed 10/10 `untracked` runs and the full set again. - The branch is behind `main` but merges cleanly; `main` has not changed `pe_map.cpp` since the base. Still not covered: CRT EXEs and the `ExitProcess` exit path, threads that start in host code and call into the image without entering, and more than one main image. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:39 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T20:46:04Z.


The own-thread handshake and nonparticipant check are useful improvements. There is still a lifetime gap in 2134f9d: if another participant is live at release, release_main only sets keep_carrier. finalize still unregisters unwind data and frees the EXE, and pe_map_release returns 1. The alive fixture leaves the worker in host test code, so its later TLS restoration cannot exercise that gap. Please either require all participants to leave or be confirmed exited before the API unregisters the mapping (with a retryable refusal), or hold a lifetime reference covering image code, unwind data and carrier until the last participant is done. A source-built test should pause a participant at a known point during image work, ask release from the host, then allow it to return and verify one final cleanup. The existing no-sweep and unrelated host TLS controls should stay.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7113528988 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7113528988). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T20:46:04Z. --- The own-thread handshake and nonparticipant check are useful improvements. There is still a lifetime gap in `2134f9d`: if another participant is live at release, `release_main` only sets `keep_carrier`. `finalize` still unregisters unwind data and frees the EXE, and `pe_map_release` returns 1. The `alive` fixture leaves the worker in host test code, so its later TLS restoration cannot exercise that gap. Please either require all participants to leave or be confirmed exited before the API unregisters the mapping (with a retryable refusal), or hold a lifetime reference covering image code, unwind data and carrier until the last participant is done. A source-built test should pause a participant at a known point during image work, ask release from the host, then allow it to return and verify one final cleanup. The existing no-sweep and unrelated host TLS controls should stay.
rasptum commented 2026-11-24 06:49:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-23T21:05:14Z.


Thanks. Addressed in 419e859, a new commit on top with no force-push. I picked the first model: a retryable refusal.

  1. Lifetime gap fixed.
    • pe_map_release now returns 2 and changes nothing while any participant other than the calling thread may still run the image's code. That covers a handle that is not signaled or cannot be waited on, a participant without a handle, and an attaching thread whose start address could not be read.
    • While refused, the image stays registered and mapped, its unwind data stays registered, its carrier stays loaded, and every participant keeps its slot. The caller retries.
    • An accepted release is checked and committed under one slot_lock hold, so no participant can join in between. It restores the calling thread, counts signaled participants as exited, ends the mode, and then unregisters and frees the image once through the existing path.
    • keep_carrier is removed. There is no longer a path that frees the EXE while a participant may still run.
  2. Paused-participant test. cpu_exe_tls_slot0_pause adds guest worker c, which runs its slot-0 check and then pauses inside the image's code on an event. The host does the following:
    • asks for the release and gets 2;
    • checks that the image is still committed, that RtlLookupFunctionEntry still finds its entry point, that the carrier is loaded, and that the starting thread's slot 0 still holds the image's block;
    • resumes c. c finds its own block and the values it wrote, then returns and detaches;
    • releases again and gets 1. After that the image is no longer committed, the unwind lookup finds nothing, and the carrier is unloaded;
    • releases a third time and gets 0, so there was exactly one cleanup.
  3. Existing controls kept. There is still no sweep: each participant swaps only its own slot. The non-participating reader still samples its own slot 0 and sentinel in every scenario and never sees a change. alive and untracked now expect the refusal, then an accepted release after the participant leaves. exited is confirmed from its handle. Windows evidence stays in its own test.
  4. Mutation (not committed). With a pe_map_release that never refuses, alive fails: there is no refusal, and the cleanup happens while the participant is live. paused crashes with 0xC0000005, because c resumes in the freed image.

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|exe_tls_slot0|exe_tls_slot0_windows|load_cookie)$' passed 10/10 on dev and 10/10 on release.
  • Counters (installed, restored, exited, refused, pending, active, last release, untracked):
Scenario Counters
main 4,4,0,0,0,0,1,0
alive 4,4,0,1,0,0,1,0
exited 4,3,1,0,0,0,1,0
untracked 67,67,0,1,0,0,1,0
paused 5,5,0,1,0,0,1,0
plain 0,0,0,0,0,0,-1,0
  • The three Python checks pass.
  • The branch is behind main but merges cleanly, and main has not changed pe_map.cpp.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7113978762 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7113978762). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T21:05:14Z. --- Thanks. Addressed in `419e859`, a new commit on top with no force-push. I picked the first model: **a retryable refusal**. 1. **Lifetime gap fixed.** - `pe_map_release` now returns **2 and changes nothing** while any participant other than the calling thread may still run the image's code. That covers a handle that is not signaled or cannot be waited on, a participant without a handle, and an attaching thread whose start address could not be read. - While refused, the image stays registered and mapped, its unwind data stays registered, its carrier stays loaded, and every participant keeps its slot. The caller retries. - An accepted release is checked and committed under one `slot_lock` hold, so no participant can join in between. It restores the calling thread, counts signaled participants as exited, ends the mode, and then unregisters and frees the image once through the existing path. - `keep_carrier` is removed. There is no longer a path that frees the EXE while a participant may still run. 2. **Paused-participant test.** `cpu_exe_tls_slot0_pause` adds guest worker c, which runs its slot-0 check and then pauses **inside the image's code** on an event. The host does the following: - asks for the release and gets `2`; - checks that the image is still committed, that `RtlLookupFunctionEntry` still finds its entry point, that the carrier is loaded, and that the starting thread's slot 0 still holds the image's block; - resumes c. c finds its own block and the values it wrote, then returns and detaches; - releases again and gets `1`. After that the image is no longer committed, the unwind lookup finds nothing, and the carrier is unloaded; - releases a third time and gets `0`, so there was exactly one cleanup. 3. **Existing controls kept.** There is still no sweep: each participant swaps only its own slot. The non-participating reader still samples its own slot 0 and sentinel in every scenario and never sees a change. `alive` and `untracked` now expect the refusal, then an accepted release after the participant leaves. `exited` is confirmed from its handle. Windows evidence stays in its own test. 4. **Mutation (not committed).** With a `pe_map_release` that never refuses, `alive` fails: there is no refusal, and the cleanup happens while the participant is live. `paused` crashes with `0xC0000005`, because c resumes in the freed image. **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|exe_tls_slot0|exe_tls_slot0_windows|load_cookie)$'` passed 10/10 on `dev` and 10/10 on `release`. - Counters (installed, restored, exited, refused, pending, active, last release, untracked): | Scenario | Counters | |---|---| | `main` | `4,4,0,0,0,0,1,0` | | `alive` | `4,4,0,1,0,0,1,0` | | `exited` | `4,3,1,0,0,0,1,0` | | `untracked` | `67,67,0,1,0,0,1,0` | | `paused` | `5,5,0,1,0,0,1,0` | | `plain` | `0,0,0,0,0,0,-1,0` | - The three Python checks pass. - The branch is behind `main` but merges cleanly, and `main` has not changed `pe_map.cpp`. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T21:17:33Z.


The paused guest worker now tests the lifetime gap from the previous review: the refused release keeps code, unwind data and carrier intact until the worker finishes. Two compatibility checks remain for 419e859.

  1. Keep the published pe_map_release 0/1 convention. A return of 2 is truthy, and existing callers treat nonzero as successful unregister. Return 0 for the opt-in mode's retryable refusal, before altering registration. A distinct reason can stay in the main-image status API; keep checking that the later release returns 1 exactly once.
  2. install_current_thread skips a thread that does not participate, but notify_thread still calls the main image's thread callbacks on it. A /GA callback then sees the host's slot 0. The current unrelated reader starts before the image is mapped, so it cannot exercise attach/detach after startup. Gate the opt-in image's notifications on the same slot ownership, preserve ordinary DLL and pe_map_run notifications, and add a host thread created after startup with a TLS sentinel and explicit callback-count checks. Also state how a failure to classify a new guest worker prevents it from entering guest code with the wrong block.

Please keep the paused-worker/unwind checks and the existing host-sentinel checks.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7114263349 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7114263349). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T21:17:33Z. --- The paused guest worker now tests the lifetime gap from the previous review: the refused release keeps code, unwind data and carrier intact until the worker finishes. Two compatibility checks remain for `419e859`. 1. Keep the published `pe_map_release` 0/1 convention. A return of 2 is truthy, and existing callers treat nonzero as successful unregister. Return 0 for the opt-in mode's retryable refusal, before altering registration. A distinct reason can stay in the main-image status API; keep checking that the later release returns 1 exactly once. 2. `install_current_thread` skips a thread that does not participate, but `notify_thread` still calls the main image's thread callbacks on it. A /GA callback then sees the host's slot 0. The current unrelated reader starts before the image is mapped, so it cannot exercise attach/detach after startup. Gate the opt-in image's notifications on the same slot ownership, preserve ordinary DLL and pe_map_run notifications, and add a host thread created after startup with a TLS sentinel and explicit callback-count checks. Also state how a failure to classify a new guest worker prevents it from entering guest code with the wrong block. Please keep the paused-worker/unwind checks and the existing host-sentinel checks.
rasptum commented 2026-11-24 06:49:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-23T21:32:05Z.


Thanks. Addressed in 685dce4, a new commit on top with no force-push. The paused-worker, unwind-data and host-sentinel checks are unchanged.

  1. pe_map_release keeps its 0/1 convention.
    • The main image's retryable refusal now returns 0, decided before registration is touched.
    • The reason is in pe_map_main_tls_stats (last release = 2).
    • The host checks the refusal as release == 0 plus status 2. It checks that the later accepted release returns 1, that it is the only release that does, and that a further release returns 0.
  2. Main-image notifications are gated on slot ownership.
    • The main image's TLS callbacks receive DLL_THREAD_ATTACH and DLL_THREAD_DETACH only on threads that participate. The gate uses the same participation slot that install_current_thread and restore_current_thread use.
    • Ordinary DLLs and EXEs started with pe_map_run are notified exactly as before. cpu_native_tls, cpu_native_tls_value, cpu_native_tls_data, cpu_native_lifetime, cpu_native_exe_tls and cpu_native_startup pass unchanged.
    • New check: every scenario starts a host thread after startup that checks its own slot 0 and TLS sentinel throughout its life. The guest exports its callback counts by reason (exe_slot0_callback_counts), read before and after that thread's life:
      • Under pe_map_run_main, the guest gets 0 callbacks for the late thread, and its thread attach/detach totals count only its own workers: 2,2, or 3,3 with the paused worker.
      • Under plain pe_map_run, the late thread's attach and detach still reach the guest (2, totals 3,3). This is the preserved behaviour.
      • The late thread's slot 0 and sentinel never changed in any scenario.
    • Mutation (not committed): without the gate, main fails because the guest receives 2 callbacks for the late thread and its totals become 4,4.
  3. Classification failure. A new thread whose start address cannot be read (NtQueryInformationThread with ThreadQuerySetWin32StartAddress), or an image thread whose block cannot be installed, cannot be run safely either way. As a guest thread it would reach guest code with the host's block, and treated as one, host code would read the image's block. pe_map therefore ends the process with __fastfail(FAST_FAIL_FATAL_APP_EXIT) inside DLL_THREAD_ATTACH, before the thread reaches its start routine. A misclassified worker can never enter guest code with the wrong block. The earlier "fault" counter that made release refuse is removed. This path is not exercised by a test: it would need fault injection in the query.

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|exe_tls_slot0|exe_tls_slot0_windows|load_cookie)$' passed 10/10 on dev and 10/10 on release.
  • The three Python checks pass.
  • Scenario counters are unchanged from 419e859.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7114587995 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7114587995). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T21:32:05Z. --- Thanks. Addressed in `685dce4`, a new commit on top with no force-push. The paused-worker, unwind-data and host-sentinel checks are unchanged. 1. **`pe_map_release` keeps its 0/1 convention.** - The main image's retryable refusal now returns **0**, decided before registration is touched. - The reason is in `pe_map_main_tls_stats` (last release = 2). - The host checks the refusal as `release == 0` plus status 2. It checks that the later accepted release returns **1**, that it is the only release that does, and that a further release returns 0. 2. **Main-image notifications are gated on slot ownership.** - The main image's TLS callbacks receive `DLL_THREAD_ATTACH` and `DLL_THREAD_DETACH` only on threads that participate. The gate uses the same participation slot that `install_current_thread` and `restore_current_thread` use. - Ordinary DLLs and EXEs started with `pe_map_run` are notified exactly as before. `cpu_native_tls`, `cpu_native_tls_value`, `cpu_native_tls_data`, `cpu_native_lifetime`, `cpu_native_exe_tls` and `cpu_native_startup` pass unchanged. - **New check:** every scenario starts a host thread *after* startup that checks its own slot 0 and TLS sentinel throughout its life. The guest exports its callback counts by reason (`exe_slot0_callback_counts`), read before and after that thread's life: - Under `pe_map_run_main`, the guest gets **0** callbacks for the late thread, and its thread attach/detach totals count only its own workers: `2,2`, or `3,3` with the paused worker. - Under plain `pe_map_run`, the late thread's attach and detach still reach the guest (**2**, totals `3,3`). This is the preserved behaviour. - The late thread's slot 0 and sentinel never changed in any scenario. - **Mutation (not committed):** without the gate, `main` fails because the guest receives 2 callbacks for the late thread and its totals become `4,4`. 3. **Classification failure.** A new thread whose start address cannot be read (`NtQueryInformationThread` with `ThreadQuerySetWin32StartAddress`), or an image thread whose block cannot be installed, cannot be run safely either way. As a guest thread it would reach guest code with the host's block, and treated as one, host code would read the image's block. pe_map therefore ends the process with `__fastfail(FAST_FAIL_FATAL_APP_EXIT)` inside `DLL_THREAD_ATTACH`, before the thread reaches its start routine. A misclassified worker can never enter guest code with the wrong block. The earlier "fault" counter that made release refuse is removed. This path is not exercised by a test: it would need fault injection in the query. **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|exe_tls_slot0|exe_tls_slot0_windows|load_cookie)$'` passed 10/10 on `dev` and 10/10 on `release`. - The three Python checks pass. - Scenario counters are unchanged from `419e859`. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:43 +00:00 (Migrated from github.com)

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


I reproduced 2/2 slot-0 tests on Windows Release at 685dce48. The late host thread gets zero guest callbacks in the main-image mode and still gets two in plain mode. The paused worker also preserves the image, unwind data and carrier while release returns 0. The remaining issue is the newly added process termination in DLL_THREAD_ATTACH if classification or TLS installation fails. That can kill the whole host on an unrelated thread's notification. Scope this experiment to a dedicated child process and state which thread-entry patterns it supports, or give the host an explicit guest-thread enrollment path that can report failure before guest code runs. A child fixture that forces the query or installation to fail must show the outcome before the start routine touches a sentinel. Keep the present 0/1 and late-host checks.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7114756035 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7114756035). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T21:39:48Z. --- I reproduced 2/2 slot-0 tests on Windows Release at `685dce48`. The late host thread gets zero guest callbacks in the main-image mode and still gets two in plain mode. The paused worker also preserves the image, unwind data and carrier while release returns 0. The remaining issue is the newly added process termination in `DLL_THREAD_ATTACH` if classification or TLS installation fails. That can kill the whole host on an unrelated thread's notification. Scope this experiment to a dedicated child process and state which thread-entry patterns it supports, or give the host an explicit guest-thread enrollment path that can report failure before guest code runs. A child fixture that forces the query or installation to fail must show the outcome before the start routine touches a sentinel. Keep the present 0/1 and late-host checks.
rasptum commented 2026-11-24 06:49:50 +00:00 (Migrated from github.com)

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


Thanks. Addressed in 440de93, a new commit on top with no force-push. I chose the explicit guest-thread enrollment path, not a dedicated child-process scope. The termination is gone: no thread notification can end the process any more.

Why enrollment. A child-process scope would still need a rule for classifying threads, and any classification that can fail in DLL_THREAD_ATTACH has the same problem: at that point the thread can only run guest code with the wrong block, or be killed. With enrollment the creator decides, before the thread exists, that it is a guest thread. pe_map never has to guess about a thread it did not create, and every failure surfaces before the guest's start routine runs.

  1. No process termination. The NtQueryInformationThread start-address classification and both __fastfail calls are removed. DLL_THREAD_ATTACH installs slot 0 only on a thread found in pe_map's enrollment table. Every other thread, whatever its start address, returns at once and is never touched, so an unrelated thread's notification can no longer affect the host.

  2. Enrollment path. The host binds the main image's CreateThread imports to the new export pe_map_main_create_thread, which has CreateThread's signature. While an image is started as the main image, it does the following:

    • reserves an entry in a fixed 64-entry enrollment table;
    • creates the thread suspended at a pe_map start routine;
    • records its thread ID and a SYNCHRONIZE handle before the thread can run;
    • resumes it unless the caller passed CREATE_SUSPENDED.

    The thread installs the image's block at its own DLL_THREAD_ATTACH, before any image is notified, as before. The pe_map start routine calls the guest's start routine only if that installation succeeded. Failures are reported before guest code runs:

    • Enrollment refused (no free entry, or the thread cannot be watched): pe_map_main_create_thread returns null with ERROR_NOT_ENOUGH_MEMORY and no thread exists. The guest's caller sees an ordinary CreateThread failure.
    • Installation failed: the pe_map start routine returns 0xE0000201 without calling the guest's start routine. The main image's callbacks are not notified, because the thread does not participate.

    Both outcomes are counted in pe_map_main_tls_stats, which now has 10 entries: enrollments refused and enrolled threads failed. An enrolled thread that has not yet passed its start routine counts as one that may run the image's code, so release refuses while it lives.

  3. Supported thread entries.

    • The thread that calls pe_map_run_main.
    • Threads the image creates through a CreateThread import that the host bound to pe_map_main_create_thread.
    • Host threads between pe_map_main_thread_enter (returns 0 if it cannot install) and pe_map_main_thread_leave.

    Not supported, and running with the host's slot 0:

    • threads the image creates by any other route: a CreateThread address it looked up itself, CreateRemoteThread(Ex), NtCreateThreadEx, thread-pool work, or a C runtime's _beginthreadex in another DLL;
    • host threads that call into the image without entering.

    This is in docs/experiments.md and the PR description.

  4. Child fixture for forced failures. cpu_native_exe_tls_slot0_enroll runs one child host process per scenario with cpu_exe_tls_slot0_enroll. Its probe worker p touches a global sentinel as the first statement of its start routine, and its entry records a failed CreateThread instead of exiting. pe_map_main_tls_inject (test-only, one shot) makes the next enrollment (1) or installation (2) fail. There is no query left to fail, so the enrollment refusal takes its place.

Scenario Probe: sentinel, created, last error, exit code Guest callbacks Counters
probe (control) 1,1,0,0, log adds p2=yy p:yyyy p3=y- 3,3 5,5,0,0,0,0,1,0,0,0
enroll_refused 0,0,8,0: null, no thread 2,2 4,4,0,0,0,0,1,0,1,0
install_failed 0,1,0,e0000201: ended before its start routine 2,2 4,4,0,0,0,0,1,0,0,1

In both failure scenarios the sentinel stays 0, the guest gets no callback for p, workers a and b are enrolled normally afterwards, and the host survives: it prints its report and exits 0. The reader, late-thread, release and cleanup checks all apply.

Mutations (not committed):

  • If the start routine runs the guest's routine after a failed installation, install_failed fails: sentinel 1, and p's check sees the host's block (p:n---, X=1).
  • With the previous __fastfail restored, install_failed fails because the child host dies with 0xC0000409 before printing.
  1. Existing checks kept, unchanged in outcome. All six scenarios show the same results as at 685dce4; their counters only gain the two new zero columns. That covers the 0/1 release convention with exactly one cleanup, alive and untracked refused and then accepted, exited confirmed from its handle, and paused refusing with image, unwind data and carrier intact. The late host thread gets 0 guest callbacks under pe_map_run_main and 2 under pe_map_run. The non-participating reader and the late thread never see their slot 0 or sentinel change. The guest's workers are now enrolled through the bound CreateThread, and the native and mapped evidence stay in separate tests.

Results: Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1, TMP/TEMP on a non-system drive.

  • ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|exe_tls_slot0|exe_tls_slot0_windows|exe_tls_slot0_enroll|load_cookie)$' passed 11/11 on dev (full build) and 11/11 on release, with no capability skips. This includes the ordinary DLL notification tests and the pe_map_run EXE tests, which are unchanged.
  • form_coverage_check, docs_links_check and bidi_controls_check pass.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7116409346 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7116409346). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T22:58:42Z. --- Thanks. Addressed in `440de93`, a new commit on top with no force-push. I chose the **explicit guest-thread enrollment path**, not a dedicated child-process scope. The termination is gone: no thread notification can end the process any more. **Why enrollment.** A child-process scope would still need a rule for classifying threads, and any classification that can fail in `DLL_THREAD_ATTACH` has the same problem: at that point the thread can only run guest code with the wrong block, or be killed. With enrollment the creator decides, before the thread exists, that it is a guest thread. pe_map never has to guess about a thread it did not create, and every failure surfaces before the guest's start routine runs. 1. **No process termination.** The `NtQueryInformationThread` start-address classification and both `__fastfail` calls are removed. `DLL_THREAD_ATTACH` installs slot 0 only on a thread found in pe_map's enrollment table. Every other thread, whatever its start address, returns at once and is never touched, so an unrelated thread's notification can no longer affect the host. 2. **Enrollment path.** The host binds the main image's `CreateThread` imports to the new export `pe_map_main_create_thread`, which has `CreateThread`'s signature. While an image is started as the main image, it does the following: - reserves an entry in a fixed 64-entry enrollment table; - creates the thread suspended at a pe_map start routine; - records its thread ID and a `SYNCHRONIZE` handle before the thread can run; - resumes it unless the caller passed `CREATE_SUSPENDED`. The thread installs the image's block at its own `DLL_THREAD_ATTACH`, before any image is notified, as before. The pe_map start routine calls the guest's start routine only if that installation succeeded. Failures are reported before guest code runs: - **Enrollment refused** (no free entry, or the thread cannot be watched): `pe_map_main_create_thread` returns null with `ERROR_NOT_ENOUGH_MEMORY` and no thread exists. The guest's caller sees an ordinary `CreateThread` failure. - **Installation failed:** the pe_map start routine returns `0xE0000201` without calling the guest's start routine. The main image's callbacks are not notified, because the thread does not participate. Both outcomes are counted in `pe_map_main_tls_stats`, which now has 10 entries: enrollments refused and enrolled threads failed. An enrolled thread that has not yet passed its start routine counts as one that may run the image's code, so release refuses while it lives. 3. **Supported thread entries.** - The thread that calls `pe_map_run_main`. - Threads the image creates through a `CreateThread` import that the host bound to `pe_map_main_create_thread`. - Host threads between `pe_map_main_thread_enter` (returns 0 if it cannot install) and `pe_map_main_thread_leave`. **Not supported**, and running with the host's slot 0: - threads the image creates by any other route: a `CreateThread` address it looked up itself, `CreateRemoteThread(Ex)`, `NtCreateThreadEx`, thread-pool work, or a C runtime's `_beginthreadex` in another DLL; - host threads that call into the image without entering. This is in `docs/experiments.md` and the PR description. 4. **Child fixture for forced failures.** `cpu_native_exe_tls_slot0_enroll` runs one child host process per scenario with `cpu_exe_tls_slot0_enroll`. Its probe worker p touches a global sentinel as the first statement of its start routine, and its entry records a failed `CreateThread` instead of exiting. `pe_map_main_tls_inject` (test-only, one shot) makes the next enrollment (1) or installation (2) fail. There is no query left to fail, so the enrollment refusal takes its place. | Scenario | Probe: sentinel, created, last error, exit code | Guest callbacks | Counters | |---|---|---|---| | `probe` (control) | `1,1,0,0`, log adds `p2=yy p:yyyy p3=y-` | `3,3` | `5,5,0,0,0,0,1,0,0,0` | | `enroll_refused` | `0,0,8,0`: null, no thread | `2,2` | `4,4,0,0,0,0,1,0,1,0` | | `install_failed` | `0,1,0,e0000201`: ended before its start routine | `2,2` | `4,4,0,0,0,0,1,0,0,1` | In both failure scenarios the sentinel stays 0, the guest gets no callback for p, workers a and b are enrolled normally afterwards, and the host survives: it prints its report and exits 0. The reader, late-thread, release and cleanup checks all apply. **Mutations (not committed):** - If the start routine runs the guest's routine after a failed installation, `install_failed` fails: sentinel 1, and p's check sees the host's block (`p:n---`, `X=1`). - With the previous `__fastfail` restored, `install_failed` fails because the child host dies with `0xC0000409` before printing. 5. **Existing checks kept, unchanged in outcome.** All six scenarios show the same results as at `685dce4`; their counters only gain the two new zero columns. That covers the 0/1 release convention with exactly one cleanup, `alive` and `untracked` refused and then accepted, `exited` confirmed from its handle, and `paused` refusing with image, unwind data and carrier intact. The late host thread gets 0 guest callbacks under `pe_map_run_main` and 2 under `pe_map_run`. The non-participating reader and the late thread never see their slot 0 or sentinel change. The guest's workers are now enrolled through the bound `CreateThread`, and the native and mapped evidence stay in separate tests. **Results:** Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1, `TMP`/`TEMP` on a non-system drive. - `ctest -R '^cpu_native_(exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|exe_tls_slot0|exe_tls_slot0_windows|exe_tls_slot0_enroll|load_cookie)$'` passed 11/11 on `dev` (full build) and 11/11 on `release`, with no capability skips. This includes the ordinary DLL notification tests and the `pe_map_run` EXE tests, which are unchanged. - `form_coverage_check`, `docs_links_check` and `bidi_controls_check` pass. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:50 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T23:07:44Z.


The explicit enrollment revision removed the process-kill path, and the original native/mapped/enrollment suites passed locally. I found one additional lifecycle case: after 64 suspended workers were cancelled before entry, creation 65 failed even though all 64 handles were signaled. I pushed a focused fix and a control that completes 65 cancellations without entry or callback and accounts for every exit. All three TLS suites passed five repeats in Windows Release.

One startup API failure path remains: the wrapper ignores ResumeThread returning DWORD(-1). Please preserve that error and refuse creation to the guest instead of returning a live suspended handle as success. Keep enrollment/watch state until the private failed thread is safely accounted for, and add a one-shot failure control verifying the guest start sentinel stays untouched and a later enrollment can still run. ResumeThread error contract

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7116583859 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7116583859). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T23:07:44Z. --- The explicit enrollment revision removed the process-kill path, and the original native/mapped/enrollment suites passed locally. I found one additional lifecycle case: after 64 suspended workers were cancelled before entry, creation 65 failed even though all 64 handles were signaled. I pushed a focused fix and a control that completes 65 cancellations without entry or callback and accounts for every exit. All three TLS suites passed five repeats in Windows Release. One startup API failure path remains: the wrapper ignores ResumeThread returning DWORD(-1). Please preserve that error and refuse creation to the guest instead of returning a live suspended handle as success. Keep enrollment/watch state until the private failed thread is safely accounted for, and add a one-shot failure control verifying the guest start sentinel stays untouched and a later enrollment can still run. [ResumeThread error contract](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-resumethread)
rasptum commented 2026-11-24 06:49:51 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-23T23:28:07Z.


Thanks for the review and for 4af2ab0. I built on top of it without rewriting it. The ResumeThread path is addressed in dc7bd046, a new commit with no force-push.

  1. ResumeThread failure refuses creation. If ResumeThread returns DWORD(-1), pe_map_main_create_thread does the following:

    • keeps ResumeThread's last error;
    • marks the entry CANCELLED under the lock;
    • ends the private thread, which is still suspended at the pe_map start routine, so nothing ran, guest code included;
    • waits up to 10 s for its handle, closes the thread handle, and returns null with the preserved error.

    The guest never gets a live suspended handle, and the thread-ID output is reset to 0.

  2. Enrollment and watch state are kept until the thread is accounted for. The CANCELLED entry keeps its slot and its SYNCHRONIZE handle until that handle is signaled. While it is unsignaled, release refuses and the slot is not reused. Once it is signaled, your reap_ended_enrollments frees it, immediately in the wrapper or later at the next creation or the accepted release. A cancelled thread is counted once, as a refused enrollment (the guest saw null). Reaping does not count it again as a failed enrolled thread.

  3. One-shot failure control. pe_map_main_tls_inject(3) makes the next resume in the wrapper fail with ERROR_ACCESS_DENIED. The probe worker is now created without CREATE_SUSPENDED, so the wrapper itself performs the resume. The thread ID is written before the resume, so it is known before the thread can run. New scenario resume_failed, in its own child host:

    • probe 0,0,5,0: sentinel untouched, CreateThread returned null, last error 5, no exit code;
    • guest callbacks 2,2 (none for p);
    • counters 4,4,0,0,0,0,1,0,1,0, so the refusal is counted and nothing is pending;
    • workers a and b enroll and run afterwards (the later enrollment), the guest log equals the Windows log, and the host prints its report and exits 0.

    Mutation (not committed): with the old behaviour, ignoring the failure and returning the handle, resume_failed fails. The guest waits on a thread that never runs, and the child exits with 0xE0000003.

  4. Existing suites kept, including your cancelled control: 65 cancellations, 5,5,0,0,0,0,1,0,0,65, no start routine and no callback. probe, enroll_refused and install_failed are unchanged, and the six mapped scenarios and the Windows test are unchanged.

Results: Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1, at dc7bd046.

  • The three TLS suites (^cpu_native_exe_tls_slot0(_windows|_enroll)?$) with --repeat until-fail:5: 5/5 on dev and 5/5 on release.
  • The neighbour set (exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|load_cookie plus the three slot-0 suites): 11/11 on dev and 11/11 on release.
    • The first release run had one failure: the native Windows launch of cpu_startup_exe.exe in cpu_native_startup hit its 30 s timeout while a repository-wide bidi_controls_check scan was running alongside. That leg does not load pe_map.
    • Rerun alone, cpu_native_startup passed 5/5 at about 6 s each, and the full set passed 11/11.
  • form_coverage_check, docs_links_check and bidi_controls_check pass. No capability skips.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7116991786 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7116991786). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T23:28:07Z. --- Thanks for the review and for `4af2ab0`. I built on top of it without rewriting it. The ResumeThread path is addressed in `dc7bd046`, a new commit with no force-push. 1. **ResumeThread failure refuses creation.** If `ResumeThread` returns `DWORD(-1)`, `pe_map_main_create_thread` does the following: - keeps `ResumeThread`'s last error; - marks the entry `CANCELLED` under the lock; - ends the private thread, which is still suspended at the pe_map start routine, so nothing ran, guest code included; - waits up to 10 s for its handle, closes the thread handle, and returns **null with the preserved error**. The guest never gets a live suspended handle, and the thread-ID output is reset to 0. 2. **Enrollment and watch state are kept until the thread is accounted for.** The `CANCELLED` entry keeps its slot and its `SYNCHRONIZE` handle until that handle is signaled. While it is unsignaled, release refuses and the slot is not reused. Once it is signaled, your `reap_ended_enrollments` frees it, immediately in the wrapper or later at the next creation or the accepted release. A cancelled thread is counted once, as a refused enrollment (the guest saw null). Reaping does not count it again as a failed enrolled thread. 3. **One-shot failure control.** `pe_map_main_tls_inject(3)` makes the next resume in the wrapper fail with `ERROR_ACCESS_DENIED`. The probe worker is now created without `CREATE_SUSPENDED`, so the wrapper itself performs the resume. The thread ID is written before the resume, so it is known before the thread can run. New scenario `resume_failed`, in its own child host: - probe `0,0,5,0`: sentinel untouched, `CreateThread` returned null, last error 5, no exit code; - guest callbacks `2,2` (none for p); - counters `4,4,0,0,0,0,1,0,1,0`, so the refusal is counted and nothing is pending; - workers a and b enroll and run afterwards (the later enrollment), the guest log equals the Windows log, and the host prints its report and exits 0. **Mutation (not committed):** with the old behaviour, ignoring the failure and returning the handle, `resume_failed` fails. The guest waits on a thread that never runs, and the child exits with `0xE0000003`. 4. **Existing suites kept**, including your `cancelled` control: 65 cancellations, `5,5,0,0,0,0,1,0,0,65`, no start routine and no callback. `probe`, `enroll_refused` and `install_failed` are unchanged, and the six mapped scenarios and the Windows test are unchanged. **Results:** Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1, at `dc7bd046`. - **The three TLS suites** (`^cpu_native_exe_tls_slot0(_windows|_enroll)?$`) with `--repeat until-fail:5`: 5/5 on `dev` and 5/5 on `release`. - **The neighbour set** (`exe_start|startup|tls|tls_value|tls_data|lifetime|exe_tls|load_cookie` plus the three slot-0 suites): 11/11 on `dev` and 11/11 on `release`. - The first `release` run had one failure: the native Windows launch of `cpu_startup_exe.exe` in `cpu_native_startup` hit its 30 s timeout while a repository-wide `bidi_controls_check` scan was running alongside. That leg does not load pe_map. - Rerun alone, `cpu_native_startup` passed 5/5 at about 6 s each, and the full set passed 11/11. - `form_coverage_check`, `docs_links_check` and `bidi_controls_check` pass. No capability skips. AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:52 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T23:29:30Z.


Your dc7bd046 now returns the ResumeThread error and exercises the refused start and a later successful enrollment. I kept that revision and pushed 969b7f72 to wait on the creator-owned handle: a concurrent enrollment can reap and close the watch as soon as the worker exits. The three native/mapped TLS CTests passed eight repeats each on Windows Release (24/24), including the resume and cancelled-worker controls. The updated head is waiting on CI.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7117024110 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/988#issuecomment-7117024110). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T23:29:30Z. --- Your dc7bd046 now returns the ResumeThread error and exercises the refused start and a later successful enrollment. I kept that revision and pushed 969b7f72 to wait on the creator-owned handle: a concurrent enrollment can reap and close the watch as soon as the worker exits. The three native/mapped TLS CTests passed eight repeats each on Windows Release (24/24), including the resume and cancelled-worker controls. The updated head is waiting on CI.
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!981
No description provided.