Skip to content

TLS: preserve Characteristics alignment and fix on_thread_attach contention (#106) - #196

Open
JGonzalezRodriguez wants to merge 2 commits into
xcl-dev:mainfrom
JGonzalezRodriguez:fix/tls-alignment-and-attach
Open

JGonzalezRodriguez wants to merge 2 commits into
xcl-dev:mainfrom
JGonzalezRodriguez:fix/tls-alignment-and-attach

Conversation

@JGonzalezRodriguez

@JGonzalezRodriguez JGonzalezRodriguez commented Nov 23, 2026 •

Copy link
Copy Markdown

Part of #106. Preserves IMAGE_TLS_DIRECTORY64.Characteristics alignment in synthetic carrier DLLs and fixes on_thread_attach() skipping TLS initialization under IMAGES.write() contention.

What is in it

  • pe.rs, tls.rs, loader.rs: Decode and sanitize IMAGE_TLS_DIRECTORY64.Characteristics alignment bits (bits 20..23 IMAGE_SCN_ALIGN_*BYTES, nibble 1..=14 → 1..=8192 B, clearing reserved bits and 0xF). Align StartAddressOfRawData in build_carrier_dll_with() to the requested alignment, clamp u32/u64 carrier size arithmetic (MAX_CARRIER_TEMPLATE), and reject oversized requests in tls::install() before creating files or writing *index_ptr.
  • tls.rs: Fix on_thread_attach() silently skipping template initialization when IMAGES.try_read() hits WouldBlock during the short IMAGES.write() push. Move registration after LoadLibraryW (avoiding NO_INDEX placeholders on load failure), write *index_ptr = index (when non-null) only after IMAGES.write() succeeds, and use blocking IMAGES.read() under the lock order OS loader lock → MOVED → IMAGES.

Tested

  • Cross-compiled on Linux (CachyOS, Ryzen 9 7950X3D, RTX 4090 610.57.4, rustc 1.98.1 x86_64-pc-windows-msvc via clang-cl 23.1.1 + lld-link + xwin SDK 10.0.26100, Proton Experimental + local relaxed vkd3d-proton):
    • cargo build --release --workspace --target x86_64-pc-windows-msvc: 0 errors, 0 warnings.
    • pe::tests::: 12 passed, 0 failed.
    • tls::tests:: (parallel & --test-threads=1): 8 passed, 0 failed, 1 ignored (carrier_variants_probe).
    • loader::tests::: 2 passed, 0 failed.
    • xcl_testtitle.exe: 47/50 passed (all TLS/loader/DLL/D3D12.X checks pass; 3 known Wine/FAudio diffs).
  • Native Windows 11 (community verifications below):
    • @mmmharius on 49d28fc merged onto 60565c4 (Windows + RTX 3060): full scripts/test.ps1 (incl. e2e xcl_testtitle) — 617 passed, 0 failed, 20 ignored (2 GPU skips: no wave64).
    • @Kalmarv on prior commit dd71d57 (Windows 11 Pro 26200, x64 MSVC, rustc 1.99.0): all 12 PE/TLS tests passed (including live 32..=8192 B alignment on invoking + 8 new threads; 4 carried-back behavior tests failed on base 956a476).

Regression run

  • Not run here (no retail game files on this host); awaiting a scripts/smoke.ps1 run by a tester with a retail title.

Not tested

  • Retail Xbox titles / scripts/smoke.ps1 regression run.

AI use

  • Antigravity (Gemini frontier model) assisted with implementation, unit tests, independent adversarial code review, and PR summary; all builds and tests above were executed locally and verified.

@JGonzalezRodriguez
JGonzalezRodriguez marked this pull request as draft November 23, 2026 20:27
@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

I built PR head dd71d576f11e97faf4c332b30b5202316ea7d2dd directly on native Windows 11 Pro build 26200, x64 MSVC, rustc 1.99.0. All 12 enabled PE and TLS tests passed in isolated processes. The live alignment check passed on the invoking thread and eight new threads for each requested alignment from 32 through 8192 bytes. The existing ignored carrier probe was skipped.

On base 956a476bf738a098f39df4f4c8acef682e43030f, I carried four behavior tests back and adapted their calls to the old carrier and install signatures. All four failed as expected. The PE and carrier checks read zero in Characteristics, and workers in the contention check saw zero-filled blocks where the template contained 0xA5. Seven existing controls passed. The fifth new test calls alignment helpers introduced by this PR and ran on the head only.

The alignment mask, RVA rounding and relocated-address checks agree. Moving registration after the successful carrier load also avoids retaining a placeholder on load failure. One wording detail in the PR description: the old write guard already ended at the push statement before LoadLibraryW. The contention bug comes from skipping initialization when try_read misses that short write. These checks used synthetic carrier DLLs. Issue #106 still needs a title-side run.

@JGonzalezRodriguez

Copy link
Copy Markdown
Author

Thanks @Kalmarv for running the native Windows 11 verification (including the base-commit control checks) and for catching that PR description wording!

Pushed follow-up commit 466425d addressing independent review findings, and updated the PR description to clarify that the old IMAGES.write() guard already ended before LoadLibraryW (so the contention bug was try_read() missing that short write, while moving registration after LoadLibraryW prevents leaking a NO_INDEX placeholder on failure).

Summary of what 466425d tightens:

  • Overflow guards: clamped carrier_template_size_for() (MAX_CARRIER_TEMPLATE) and build_carrier_dll_with() against u32/u64 overflow, and made tls::install() reject oversized requests before creating files or writing *index_ptr (with new test install_rejects_oversized_requests, now 7/7 tls::tests).
  • Write ordering: moved *index_ptr = index after IMAGES.write() succeeds.
  • Test hardening: added a pre-existing thread check in carrier_respects_tls_block_alignment_on_all_threads (exercising fill_existing_threads()), synchronized Part 2 stress workers via Condvar until installer.join() so workers cannot exit and unmap their TEB mid-enumeration, loaded the mapped-PE inspection DLL with DONT_RESOLVE_DLL_REFERENCES + FreeLibrary + cleanup to avoid burning extra static TLS slots, and verified all nibbles 1..=14 + 0/0xF + exact DIR64 relocations.

AI-assisted.

@JGonzalezRodriguez
JGonzalezRodriguez marked this pull request as ready for review November 23, 2026 20:59
@mmmharius

Copy link
Copy Markdown
Contributor

Tested on Windows with an NVIDIA RTX 3060 (32-lane waves), merged onto current main.

  • PR head 49d28fc merged onto main 60565c4 (local merge, no conflicts).
  • Full scripts/test.ps1 (end-to-end test title included): all green.
  • The six new tests pass: parses_tls_characteristics_from_pe, tls_characteristics_alignment_decoding, carrier_preserves_tls_characteristics_and_template_alignment, carrier_respects_tls_block_alignment_on_all_threads, install_rejects_oversized_requests, on_thread_attach_never_skips_init_under_images_write_contention. carrier_variants_probe stays ignored.
  • GPU tests skipped on this adapter: 2 (no wave64), same as on main.

Synthetic tests only, no game data. tests were run by me.

@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 49d28fc52e11553e9b0d5ddb1eeee528a16cdce3 on AMD RDNA4. I merged it onto current main d5442d2 locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200 (native Windows), Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 710 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 669 passed, 0 failed, 21 ignored
  • 0 SKIPPED; the tls and loader tests pass.

Review: Note the overlap with #431 (open), which removes fill_existing_threads that this PR keeps and changes; #431's description says the two conflict in three places. I ran this one merged onto main alone, not together with #431.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@einfachama

Copy link
Copy Markdown
Contributor

Regression run on a retail title for #196, since Not tested lists it: head 49d28fc52e11553e9b0d5ddb1eeee528a16cdce3 merged onto main d208e14 (clean merge, local merge commit f3484b8). No regression at my retail title's first menu, and the title's TLS images now log their alignment.

Tested

  • NVIDIA GeForce RTX 4060 8 GB (Ada, 32-lane waves), driver 621.24; Ryzen 7 5800X; 32 GB RAM; Windows 11 Pro 26300; Rust 1.100.0 MSVC; DXC 1.9.2602.17; debug for the tests, release for the title runs.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 710 passed, 0 failed, 41 ignored (main d208e14 in its clean run: 704 / 0 / 41). SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • My retail title, scripts/smoke.ps1 -Seconds 60, unattended, the learned window in both runs. XCL's log has the same TLS lines as on main (3 images with TLS, the executable in slot 0, the same indices, block sizes and thread counts), now with an align value for each image (4 to 16 B here). No TLS error.
xcl-smoke v1
commit: d208e14b765f469f4b56301b78afa8dac5378339
gpu: NVIDIA GeForce RTX 4060
title: title
seconds: 60
exit: 0x0
frames shown: 1616 (PresentX 1616)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14635 MB
xcl-smoke v1
commit: f3484b8f9b563fe7fae3b2e1dc58ea8ba4a3f75c
gpu: NVIDIA GeForce RTX 4060
title: title
seconds: 60
exit: 0x0
frames shown: 1609 (PresentX 1609)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14452 MB

The second block is the local merge onto main, not the PR's head commit, so it can't stand in for the PR's own Regression run (pr_check.py wants the head commit there). It shows that the title's TLS setup is unchanged here apart from the alignment.

Not tested

  • The on_thread_attach contention case under a real title (nothing I could provoke at the menu), and anything past the first menu.
  • The PR at its own base.

AI use

  • Claude Code with Claude Opus 5.5 ran the builds, tests and title runs in an unattended session and wrote this report from the logs; I checked it before posting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants