TLS: audit static TLS directory raw data sizing, alignment, and index allocation (#106) #1421

Open
slopgobblercobbler wants to merge 1 commit from upstream-pr/1431 into main
slopgobblercobbler commented 2026-11-24 15:53:27 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by slopgobblercobbler on 2026-11-24T15:53:27Z.


Summary

Fixes #106 Item 1:

  1. Static TLS sizing & integer overflow protection (crates/xcl/src/tls.rs):
    • Clamped carrier template size safely via carrier_template_size_aligned to MAX_CARRIER_TEMPLATE_SIZE = 0xFFFE_0000, using saturating_add and masking against requested alignment.
    • In install_with_characteristics, computed required_len = total_len.max(template_len as u64) to strictly prevent buffer overflow when total_len < template_len.
    • Used saturating_add in build_carrier_dll_with for data_vsize and size_of_image.
  2. Alignment preservation:
    • Added alignment_from_characteristics and characteristics_from_alignment to decode/encode PE IMAGE_SCN_ALIGN_* bits (bits 20..24, 1..8192 bytes).
    • Preserved Characteristics in the synthesized carrier DLL's IMAGE_TLS_DIRECTORY64, ensuring the OS loader allocates TLS blocks with requested alignment (e.g. 32/64 bytes for AVX/cachelines).
    • Adjusted carrier .data template offset when align > 256 so StartAddressOfRawData and EndAddressOfRawData satisfy the alignment requirement.
  3. Index allocation & bounds safety:
    • Added null/NO_INDEX checks in tls_block_of, take_slot_zero, exchange_for_new_thread, and fill_block.
    • Safely registered TlsImage into IMAGES only after load success, and added uninstall(img_name) for teardown cleanup.
  4. Loader error handling (crates/xcl/src/loader.rs):
    • Passed t.characteristics from pe.tls() to tls_install and wired crate::tls::uninstall(&module.name) and modules::unregister_mapped(base) on installation failure.

Changes

  • crates/xcl/src/tls.rs:
    • carrier_template_size_aligned: alignment and saturating length bounds.
    • alignment_from_characteristics / characteristics_from_alignment: PE alignment bit mapping.
    • uninstall: TLS image cleanup helper.
    • Unit tests: added 4 synthetic unit tests for alignment preservation, carrier PE headers, index allocation safety, and characteristics encoding.
  • crates/xcl/src/loader.rs:
    • Propagate TLS directory characteristics and clean up on failure.

Tested

  • Windows 11 x64, Rust 1.85+ stable (MSVC).
  • cargo test -p xcl -- tls: 10 passed, 0 failed (100% pass rate).
  • Full workspace test suite: 635 passed, 0 failed.
  • Clean diff: zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys.

Not Tested

  • Real title full boot traces (tested via synthetic PE and TLS carrier generation test harness).

AI Use

Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity

<!-- greyfinch-sync:ivall/xcl:issue:5475600363 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1431). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-11-24T15:53:27Z. --- ## Summary Fixes #106 Item 1: 1. **Static TLS sizing & integer overflow protection (`crates/xcl/src/tls.rs`)**: - Clamped carrier template size safely via `carrier_template_size_aligned` to `MAX_CARRIER_TEMPLATE_SIZE = 0xFFFE_0000`, using `saturating_add` and masking against requested alignment. - In `install_with_characteristics`, computed `required_len = total_len.max(template_len as u64)` to strictly prevent buffer overflow when `total_len < template_len`. - Used `saturating_add` in `build_carrier_dll_with` for `data_vsize` and `size_of_image`. 2. **Alignment preservation**: - Added `alignment_from_characteristics` and `characteristics_from_alignment` to decode/encode PE `IMAGE_SCN_ALIGN_*` bits (bits 20..24, 1..8192 bytes). - Preserved `Characteristics` in the synthesized carrier DLL's `IMAGE_TLS_DIRECTORY64`, ensuring the OS loader allocates TLS blocks with requested alignment (e.g. 32/64 bytes for AVX/cachelines). - Adjusted carrier `.data` template offset when `align > 256` so `StartAddressOfRawData` and `EndAddressOfRawData` satisfy the alignment requirement. 3. **Index allocation & bounds safety**: - Added null/`NO_INDEX` checks in `tls_block_of`, `take_slot_zero`, `exchange_for_new_thread`, and `fill_block`. - Safely registered `TlsImage` into `IMAGES` only after load success, and added `uninstall(img_name)` for teardown cleanup. 4. **Loader error handling (`crates/xcl/src/loader.rs`)**: - Passed `t.characteristics` from `pe.tls()` to `tls_install` and wired `crate::tls::uninstall(&module.name)` and `modules::unregister_mapped(base)` on installation failure. ## Changes - `crates/xcl/src/tls.rs`: - `carrier_template_size_aligned`: alignment and saturating length bounds. - `alignment_from_characteristics` / `characteristics_from_alignment`: PE alignment bit mapping. - `uninstall`: TLS image cleanup helper. - Unit tests: added 4 synthetic unit tests for alignment preservation, carrier PE headers, index allocation safety, and characteristics encoding. - `crates/xcl/src/loader.rs`: - Propagate TLS directory characteristics and clean up on failure. ## Tested - Windows 11 x64, Rust 1.85+ stable (MSVC). - `cargo test -p xcl -- tls`: 10 passed, 0 failed (100% pass rate). - Full workspace test suite: 635 passed, 0 failed. - Clean diff: zero trailing whitespace, GPL-2.0-only compliant, zero game data/keys. ## Not Tested - Real title full boot traces (tested via synthetic PE and TLS carrier generation test harness). ## AI Use Co-Authored-By: Claude Code (Claude Opus 5.5) / Gemini Antigravity
futurepro62 commented 2026-11-24 17:06:45 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T16:52:17Z.


Full-suite run, since this PR has no test report yet. Tested exact head 8052b9c (on its base 930e8b4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 755 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: alignment_preservation_and_characteristics, carrier_alignments_in_pe_headers, index_allocation_safety, install_with_characteristics_aligned.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 755 passed, 0 failed, 41 ignored, no skips.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:ivall/xcl:comment:7138335161 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1431#issuecomment-7138335161). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-24T16:52:17Z. --- Full-suite run, since this PR has no test report yet. Tested exact head `8052b9c` (on its base `930e8b4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4`: **755 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `alignment_preservation_and_characteristics`, `carrier_alignments_in_pe_headers`, `index_allocation_safety`, `install_with_characteristics_aligned`. - Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **755 passed, 0 failed, 41 ignored**, no skips. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
gut0to commented 2026-11-24 18:56:01 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-24T18:56:01Z.

Review: COMMENTED


AI-assisted review; I checked the RVA arithmetic against the supported 8 KiB alignment.

<!-- greyfinch-sync:ivall/xcl:review:6045425559 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1431#pullrequestreview-6045425559). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-24T18:56:01Z. **Review:** COMMENTED --- AI-assisted review; I checked the RVA arithmetic against the supported 8 KiB alignment.
gut0to commented 2026-11-24 18:59:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-24T18:59:03Z.

Review: COMMENTED


CONTRIBUTING.md also requires paired real-title scripts/smoke.ps1 runs for this runtime change, with main and this head tested on the same title and duration. The current description has no regression blocks and says real-title execution was not tested. Please add the required pair before merge, documenting if the title does not exercise TLS installation. AI-assisted review; I checked the current description against the guide.

<!-- greyfinch-sync:ivall/xcl:review:6045468321 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1431#pullrequestreview-6045468321). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-24T18:59:03Z. **Review:** COMMENTED --- CONTRIBUTING.md also requires paired real-title `scripts/smoke.ps1` runs for this runtime change, with `main` and this head tested on the same title and duration. The current description has no regression blocks and says real-title execution was not tested. Please add the required pair before merge, documenting if the title does not exercise TLS installation. AI-assisted review; I checked the current description against the guide.
DavidM1911 commented 2026-11-24 19:07:39 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T18:52:21Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7141269804 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1431#issuecomment-7141269804). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T18:52:21Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~21 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
gut0to commented 2026-11-24 19:08:01 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-24T18:56:01Z.

Code review location: crates/xcl/src/tls.rs; line 124

@@ -84,9 +118,15 @@ pub fn build_carrier_dll(template_size: u32) -> Vec<u8> {
 
 /// Build a minimal x64 DLL whose TLS block is exactly `template_size` bytes.
 pub fn build_carrier_dll_with(template_size: u32, opts: CarrierOpts) -> Vec<u8> {
-    let template_size = template_size.max(16);
-    let data_vsize = OFF_TEMPLATE + template_size;
-    let size_of_image = (DATA_RVA + data_vsize + 0xFFF) & !0xFFF;
+    let align = alignment_from_characteristics(opts.characteristics);
+    let template_size = carrier_template_size_aligned(template_size as u64, align);
+    let off_template = if align > 256 {
+        (OFF_TEMPLATE + align - 1) & !(align - 1)

This aligns the section-relative offset, but the final template RVA is DATA_RVA + off_template with DATA_RVA = 0x3000. For the supported 8 KiB alignment, this computes off_template = 0x2000, so the start RVA is 0x5000 and 0x5000 % 0x2000 == 0x1000; it is not 8 KiB aligned. The current 512-byte test passes because 0x3000 is divisible by 512. Could the offset be chosen from the absolute RVA and add an 8 KiB header test? AI-assisted review; I verified the arithmetic.

<!-- greyfinch-sync:ivall/xcl:review-comment:4684991170 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1431#discussion_r4684991170). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-24T18:56:01Z. **Code review location:** `crates/xcl/src/tls.rs`; line 124 ~~~~diff @@ -84,9 +118,15 @@ pub fn build_carrier_dll(template_size: u32) -> Vec<u8> { /// Build a minimal x64 DLL whose TLS block is exactly `template_size` bytes. pub fn build_carrier_dll_with(template_size: u32, opts: CarrierOpts) -> Vec<u8> { - let template_size = template_size.max(16); - let data_vsize = OFF_TEMPLATE + template_size; - let size_of_image = (DATA_RVA + data_vsize + 0xFFF) & !0xFFF; + let align = alignment_from_characteristics(opts.characteristics); + let template_size = carrier_template_size_aligned(template_size as u64, align); + let off_template = if align > 256 { + (OFF_TEMPLATE + align - 1) & !(align - 1) ~~~~ --- This aligns the section-relative offset, but the final template RVA is `DATA_RVA + off_template` with `DATA_RVA = 0x3000`. For the supported 8 KiB alignment, this computes `off_template = 0x2000`, so the start RVA is `0x5000` and `0x5000 % 0x2000 == 0x1000`; it is not 8 KiB aligned. The current 512-byte test passes because `0x3000` is divisible by 512. Could the offset be chosen from the absolute RVA and add an 8 KiB header test? AI-assisted review; I verified the arithmetic.
Toons29 commented 2026-11-25 04:10:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Toons29 on 2026-11-25T03:30:45Z.


Regression run of 8052b9c2 on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 8052b9c2faa5136dee387b1890c4ab8dfd92ae21
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1602 (PresentX 1602)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14813 MB

Against the limits in CONTRIBUTING.md: 99 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

<!-- greyfinch-sync:ivall/xcl:comment:7151983181 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1431#issuecomment-7151983181). Originally posted by [Toons29](https://github.com/Toons29) on 2026-11-25T03:30:45Z. --- Regression run of `8052b9c2` on the retail title, for this PR's **Regression run** section. Both blocks are from `scripts/smoke.ps1` as printed, not edited: `main` on `4178e888`, this PR on its head commit with no uncommitted changes, same title and time. ``` xcl-smoke v1 commit: 4178e888c0b5be50a2887eda45cdb832763b68a4 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 14831 MB ``` ``` xcl-smoke v1 commit: 8052b9c2faa5136dee387b1890c4ab8dfd92ae21 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1602 (PresentX 1602) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 14813 MB ``` Against the limits in CONTRIBUTING.md: 99 % of `main`'s frames, 100 % of its peak commit, no crash, no device removal. AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, `cargo build --release -p xcl`, 60 s, `XCL_NETWORK=0`. This is a regression check only: it does not show whether the run reaches the code this PR changes. AI use: Claude Code (Claude Opus 5.5) built both trees and ran `scripts/smoke.ps1`; I checked the blocks before posting.
github-actions[bot] commented 2026-11-28 08:01:56 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-28T08:01:56Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7236316733 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1431#issuecomment-7236316733). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-28T08:01:56Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 05:43:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:43:43Z.


Triage (#1607) – head 8052b9c

This makes the same change as #196: loader.rs passes t.characteristics to the TLS install, tls.rs writes them into the carrier's IMAGE_TLS_DIRECTORY64, aligns the template start to the decoded alignment, and moves the IMAGES push after LoadLibraryW with the slot index already set. #196 was opened first, also sanitises the reserved/0xF alignment bits (this PR copies the raw Characteristics into the carrier) and merges cleanly; this PR conflicts with main.
The extra guards here (k == 0 / NO_INDEX in take_slot_zero and exchange_for_new_thread, write_unaligned on the index, uninstall on error) could go to #196 or a small follow-up; take_slot_zero was already hardened on main by #1083.
Recommendation: close (duplicate of #196).

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7302074472 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1431#issuecomment-7302074472). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:43:43Z. --- **Triage (#1607)** – head 8052b9c This makes the same change as #196: loader.rs passes t.characteristics to the TLS install, tls.rs writes them into the carrier's IMAGE_TLS_DIRECTORY64, aligns the template start to the decoded alignment, and moves the IMAGES push after LoadLibraryW with the slot index already set. #196 was opened first, also sanitises the reserved/0xF alignment bits (this PR copies the raw Characteristics into the carrier) and merges cleanly; this PR conflicts with main. The extra guards here (k == 0 / NO_INDEX in take_slot_zero and exchange_for_new_thread, write_unaligned on the index, uninstall on error) could go to #196 or a small follow-up; take_slot_zero was already hardened on main by #1083. Recommendation: close (duplicate of #196). AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1431:upstream-pr/1431'
git switch 'upstream-pr/1431'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1431'
git switch 'upstream-pr/1431'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1431'
git switch 'upstream-pr/1431'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1431'
git switch 'main'
git merge --squash 'upstream-pr/1431'
git switch 'main'
git merge --ff-only 'upstream-pr/1431'
git switch 'main'
git merge 'upstream-pr/1431'
git push origin 'main'
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
xcl-dev/xcl!1421
No description provided.