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

Open
github-bot wants to merge 1 commit from github-bot/gh-1431 into main AGit
Owner

@⁠slopgobblercobbler on GitHub · 2026-11-24 15:53 UTC · original
slopgobblercobbler:fix/tls-directory-audit → main · +233 −30 · 2 files · 1 commits · open

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

**@⁠slopgobblercobbler** on GitHub · 2026-11-24 15:53 UTC · [original](https://github.com/xcl-dev/xcl/pull/1431) `slopgobblercobbler:fix/tls-directory-audit` → `main` · +233 −30 · 2 files · 1 commits · open ## 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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:53:40 +00:00
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-24 16:52 UTC · original · edited 2026-11-24 17:01 UTC

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.
**@⁠futurepro62** on GitHub · 2026-11-24 16:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/1431#issuecomment-7138335161) · edited 2026-11-24 17:01 UTC 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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 18:52 UTC · original

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!

**@⁠DavidM1911** on GitHub · 2026-11-24 18:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/1431#issuecomment-7141269804) 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!
Author
Owner

@⁠Toons29 on GitHub · 2026-11-25 03:30 UTC · original

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.

**@⁠Toons29** on GitHub · 2026-11-25 03:30 UTC · [original](https://github.com/xcl-dev/xcl/pull/1431#issuecomment-7151983181) 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.
@ -90,0 +121,4 @@
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)
Author
Owner

@⁠gut0to on GitHub · 2026-11-24 18:56 UTC · original

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.

**@⁠gut0to** on GitHub · 2026-11-24 18:56 UTC · [original](https://github.com/xcl-dev/xcl/pull/1431#discussion_r4684991170) 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1431/head:github-bot/gh-1431'
git switch 'github-bot/gh-1431'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1431'
git switch 'github-bot/gh-1431'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1431'
git switch 'github-bot/gh-1431'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1431'
git switch 'main'
git merge --squash 'github-bot/gh-1431'
git switch 'main'
git merge --ff-only 'github-bot/gh-1431'
git switch 'main'
git merge 'github-bot/gh-1431'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.