pe: check a page-spanning TLS template and two callbacks in table order #876

Merged
rasptum merged 2 commits from upstream-pr/883 into main 2026-11-23 16:08:10 +00:00
rasptum commented 2026-11-23 15:43:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-23T15:43:57Z.


Refs #870, Refs #165

What this changes

Extends the existing cpu_native_tls_value test (tests/cpu/tls_value.cpp, tests/cpu/native_tls_value_check.py) instead of adding a parallel fixture. This is the loader TLS follow-up listed on #870. The #870 checkpoint of 15:20Z reports two TLS callbacks in the static intake of the main executable, so this checks two-callback ordering on a source-built image. No game-derived data is used. No change to tools/pe_map.cpp or CMakeLists.txt.

Fixture (cpu_tls_value, MSVC, static CRT):

  • The initialized TLS template adds an 8 KiB array of distinct words (span_word(i)), so the template spans a 4 KiB page boundary with different values on each side.
  • A second TLS callback in .CRT$XLM (caller 3) follows the existing one in .CRT$XLB (caller 1). Each callback and the entry point (caller 2) report the int in the low 16 bits and the number of array words that differ from the template above them. A fresh block reports 7.
  • tls_value_worker keeps a worker alive while another worker writes.

Harness, under LoadLibraryW and pe_map, each in a fresh child process:

  • Structural check with pefile: the array crosses a page boundary inside the template, and the two callbacks appear in the callback table in that order.
  • The template in the loaded image matches the file after load and at the end.
  • The main thread observes, then writes 20. Worker A observes, writes 50 and waits. Worker B starts after A's write, observes, writes 60 and waits. Then both observe again.
  • Every notification is checked: process attach and detach on main, and thread attach and detach on A and B. The callers must run 1, 3, 2 and each must see its own thread's copy. Native and mapped per-thread sequences must be equal. The two workers are resumed together, so their exit order is not compared.

Negative controls patch copies of the built DLL. Both loaders must reject each one with a named diagnostic, or the test fails:

  • swapped callback table entries → callback order: ('main', 1) ran callers [3, 1, 2]
  • template zeroed from its first page boundary → template: main observed 0x6380007, expected 0x7 (value 7, 1592 array words differ from the template) (dev)

A mutation that removes the order check makes the test fail on the swapped control. This shows the test fails closed.

Scope

This checks the TLS stage ahead of the current #870 boundary. Per the maintainer's checkpoints, the real mapping probe still stops at import binding (#880). This PR does not move that boundary and makes no claim about the executable's TLS or entry run. It is separate from #872 (conversion driver).

Test

Commit 02cd2ac, rebased on origin/main. 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_tls_value cpu_tls_original cpu_tls_sink
ctest --preset dev --no-tests=error -R '^cpu_native_tls_value$' -V
cmake --preset release
cmake --build --preset release --target pe_map cpu_tls_value cpu_tls_original cpu_tls_sink
ctest --preset release --no-tests=error -R '^cpu_native_tls_value$' -V
python -B tests/form_coverage_check.py
python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py
Preset Result Template Page boundary
dev (/Od) 1/1 passed RVA 0x6e8c0, 8224 bytes RVA 0x6f000 (words 0x91994997 / 0x77d0c348)
release (/O2) 1/1 passed RVA 0x16290, 8224 bytes RVA 0x17000 (words 0x57698763 / 0x35a10114)

Both presets reject both negative controls under both loaders. The three Python checks pass. No capability skips. The other TLS tests (cpu_native_tls, cpu_native_tls_data) were not rerun: their sources and the sink are unchanged.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6562997004 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/883). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T15:43:57Z. --- Refs #870, Refs #165 ## What this changes Extends the existing `cpu_native_tls_value` test (`tests/cpu/tls_value.cpp`, `tests/cpu/native_tls_value_check.py`) instead of adding a parallel fixture. This is the loader TLS follow-up listed on #870. The #870 checkpoint of 15:20Z reports two TLS callbacks in the static intake of the main executable, so this checks two-callback ordering on a source-built image. No game-derived data is used. No change to `tools/pe_map.cpp` or `CMakeLists.txt`. Fixture (`cpu_tls_value`, MSVC, static CRT): - The initialized TLS template adds an 8 KiB array of distinct words (`span_word(i)`), so the template spans a 4 KiB page boundary with different values on each side. - A second TLS callback in `.CRT$XLM` (caller 3) follows the existing one in `.CRT$XLB` (caller 1). Each callback and the entry point (caller 2) report the int in the low 16 bits and the number of array words that differ from the template above them. A fresh block reports 7. - `tls_value_worker` keeps a worker alive while another worker writes. Harness, under `LoadLibraryW` and `pe_map`, each in a fresh child process: - Structural check with `pefile`: the array crosses a page boundary inside the template, and the two callbacks appear in the callback table in that order. - The template in the loaded image matches the file after load and at the end. - The main thread observes, then writes 20. Worker A observes, writes 50 and waits. Worker B starts after A's write, observes, writes 60 and waits. Then both observe again. - Every notification is checked: process attach and detach on main, and thread attach and detach on A and B. The callers must run 1, 3, 2 and each must see its own thread's copy. Native and mapped per-thread sequences must be equal. The two workers are resumed together, so their exit order is not compared. Negative controls patch copies of the built DLL. Both loaders must reject each one with a named diagnostic, or the test fails: - swapped callback table entries → `callback order: ('main', 1) ran callers [3, 1, 2]` - template zeroed from its first page boundary → `template: main observed 0x6380007, expected 0x7 (value 7, 1592 array words differ from the template)` (dev) A mutation that removes the order check makes the test fail on the swapped control. This shows the test fails closed. ## Scope This checks the TLS stage ahead of the current #870 boundary. Per the maintainer's checkpoints, the real mapping probe still stops at import binding (#880). This PR does not move that boundary and makes no claim about the executable's TLS or entry run. It is separate from #872 (conversion driver). ## Test Commit `02cd2ac`, rebased on `origin/main`. 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_tls_value cpu_tls_original cpu_tls_sink ctest --preset dev --no-tests=error -R '^cpu_native_tls_value$' -V cmake --preset release cmake --build --preset release --target pe_map cpu_tls_value cpu_tls_original cpu_tls_sink ctest --preset release --no-tests=error -R '^cpu_native_tls_value$' -V python -B tests/form_coverage_check.py python -B tests/docs_links_check.py python -B tests/bidi_controls_check.py ``` | Preset | Result | Template | Page boundary | |---|---|---|---| | dev (/Od) | 1/1 passed | RVA 0x6e8c0, 8224 bytes | RVA 0x6f000 (words 0x91994997 / 0x77d0c348) | | release (/O2) | 1/1 passed | RVA 0x16290, 8224 bytes | RVA 0x17000 (words 0x57698763 / 0x35a10114) | Both presets reject both negative controls under both loaders. The three Python checks pass. No capability skips. The other TLS tests (`cpu_native_tls`, `cpu_native_tls_data`) were not rerun: their sources and the sink are unchanged. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:45:13 +00:00
R0Y00 commented 2026-11-24 06:49:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T15:53:00Z.


Review follow-up at 54b6781: the layout now selects the first boundary strictly inside the array even when the linker places the start exactly on a page boundary. Both presets passed all three TLS CTests on Windows 11 with MSVC 19.51. Native and mapped runs rejected the swapped callback order and the damaged later-page controls separately, and the positive runs matched on both worker threads. CI is rerunning on the new head. This is the DLL/TLS slice of #165; the EXE acceptance remains open.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7106260304 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/883#issuecomment-7106260304). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T15:53:00Z. --- Review follow-up at `54b6781`: the layout now selects the first boundary strictly inside the array even when the linker places the start exactly on a page boundary. Both presets passed all three TLS CTests on Windows 11 with MSVC 19.51. Native and mapped runs rejected the swapped callback order and the damaged later-page controls separately, and the positive runs matched on both worker threads. CI is rerunning on the new head. This is the DLL/TLS slice of #165; the EXE acceptance remains open.
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!876
No description provided.