Skip to content

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

Open
slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/tls-directory-audit
Open

slopgobblercobbler wants to merge 1 commit into
xcl-dev:mainfrom
slopgobblercobbler:fix/tls-directory-audit

Conversation

@slopgobblercobbler

Copy link
Copy Markdown
Contributor

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

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…README

Integrates 12 additional open upstream PRs on top of upstream/main
(b7aa9f3):
- xcl-dev#1432: GFX10 to DXGI format mappings, depth SRVs, and byte sizing
- xcl-dev#1447: Arena::take overflow/alignment and Uma::free pool validation
- xcl-dev#1441: drop SC_KEYMENU so the window is never frozen in menu mode
- xcl-dev#1431, xcl-dev#1436: TLS directory sizing/alignment and RegisterClassExA checks
- xcl-dev#1430, xcl-dev#1446: XGameSave quota/URL and XAudio2 XMA channel validation
- xcl-dev#1415, xcl-dev#1433: VFS merged-directory sync and CreateFile2 checks
- xcl-dev#1418: XGameRuntime device association synchronization
- xcl-dev#1410, xcl-dev#1413: wave64 form logging and XCL_SAFE_BARRIER cache bypass

Also removes duplicate a_huge_constant_count_has_no_layout test left
from pre-applying xcl-dev#1290 before upstream merged it, and updates README.md.

Verified under GE-Proton11-14 (RX 9060 XT / RADV):
- Release build (x86_64-pc-windows-msvc): ok (22.05s)
- Proton unit test sweep across all merged/touched modules: 209 passed,
  2 failed (known Wine/vkd3d-proton diffs: xma2_source_voice current_buffer_context
  before Start, and vkd3d-proton accepting UNORDERED_ACCESS without UAV flag),
  1 ignored, 0 skipped.
@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

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.

@DavidM1911

Copy link
Copy Markdown
Contributor

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 gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread crates/xcl/src/tls.rs
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 gut0to left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

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.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…cl-dev#1431)

Port upstream xcl-dev#1431 (live head 8052b9c, still OPEN): carrier
template sizing with alignment + 0xFFFE_0000 cap, Characteristics
align encode/decode 1..8192, .data offset fix for align>256, null and
NO_INDEX guards, register-after-load with uninstall on failure, and
plumbing t.characteristics into tls_install.

tls.rs + loader.rs land verbatim (Windows-only); the xcl-dev#1403 build-clamp
pin test is retained (still passes under the stricter cap). Linux twin:
ImageSpec/PendingTls carry characteristics, blocks allocate with the
decoded alignment (floor 16), index writes use write_unaligned,
alloc guards null templates, uninstall() drops the registry entry and
detaches blocks; init_dll/load_game_dll uninstall on failure. The
template-exceeds-block check stays fail-closed (strictly stronger than
upstream max()).
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…ention (port upstream xcl-dev#196)

Port upstream xcl-dev#196 (live head 49d28fc, still OPEN vs upstream/main).
pe.rs (shared, direct): SCN_ALIGN_MASK + TlsDir::alignment/
align_characteristics + tls_alignment_from_characteristics/
sanitize_tls_characteristics (nibble 1..=14 -> 1..=8192, reserved/0xF
cleared) + 2 tests (decoding runs everywhere; carrier round-trip
gated cfg(windows) — builder lives in the cfg(windows) module).
tls.rs (cfg(windows)): build_carrier_dll_with sanitizes Characteristics,
aligns template-start RVA for every valid align (not just >256), clamps
template_size against u32::MAX/DATA_RVA/off (kept xcl-dev#1431 0xFFFE_0000
effective-size cap — strictly stronger than the PR's 0xFFFEFFF0, so no
MAX_CARRIER_TEMPLATE const); install_with_characteristics rejects
oversize before any side effect (no file/LoadLibrary/publish, index_ptr
untouched); on_thread_attach blocks on IMAGES.read with poison recovery
instead of try_read-skip (lock order OS-loader -> MOVED -> IMAGES,
write() held only for push). install() 6-arg + loader characteristics
plumbing already on linux via xcl-dev#1431 — kept. Linux twin needs no change:
tls_linux.rs rejects oversize before lock_install (usize/Layout checks)
and never try_skips attach (zero try_* in file).
Tests: adapted to xcl-dev#1431 sizing (0x2000 template exact for all nibbles;
third oversize case is template-exceeds-carrier-max since total<vs>template
widening is xcl-dev#1431 semantics); new alignment/contention/oversize tests are
Windows-only (file never compiles on Linux — rustfmt-parse checked).
Gates: both checks 0 errors, pe:: 24/24, lib 752/0/1, title e2e 12 unique
FAILs all D3D12.X 0 non-GPU, retail HOLDS (+0x2237320, stubs 20).
@github-actions

Copy link
Copy Markdown

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

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

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.

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

Labels

stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

6 participants