Skip to content

TLS: clamp carrier template size before rounding to prevent u64/u32 overflow - #1403

Open
b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:tls/carrier-size-overflow
Open

b3IKK wants to merge 1 commit into
xcl-dev:mainfrom
b3IKK:tls/carrier-size-overflow

Conversation

@b3IKK

@b3IKK b3IKK commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

carrier_template_size_for computed (n + 15) & !15 before clamping, overflowing u64 when total_len > u64::MAX - 15 (and its upper bound u32::MAX - 0x10000 was not 16-byte aligned). build_carrier_dll_with only clamped at the lower bound (template_size.max(16)), so large template_size values overflowed u32 in OFF_TEMPLATE + template_size, DATA_RVA + data_vsize + 0xFFF, and va(OFF_TEMPLATE + template_size).

This defines MAX_CARRIER_TEMPLATE = (u32::MAX - 0x10000) & !15 and clamps to [16, MAX_CARRIER_TEMPLATE] in both functions.

What is in it

  • crates/xcl/src/tls.rs: clamp to [16, MAX_CARRIER_TEMPLATE] in carrier_template_size_for and build_carrier_dll_with; add unit test carrier_size_and_build_clamp_without_overflow (verified panicking on u64 addition overflow before the fix).

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 752 passed, 0 failed, 41 ignored (SKIPPED GPU tests: 2 no wave64 — gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware)
  • cargo test -p xcl --lib tls::tests: 5 passed, 0 failed, 1 ignored (carrier_size_and_build_clamp_without_overflow panicked before the fix)

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1226 (PresentX 1226)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

PR:

xcl-smoke v1
commit: ce99e53ac3ed43c7e74d445f949ecf856ade445b
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1640 (PresentX 1640)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14598 MB

Not tested

  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Gemini 4 Neon: audited tls.rs, wrote the fix and unit test, ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.

…verflow

carrier_template_size_for computed `(n + 15) & !15` before clamping,
overflowing u64 when `total_len > u64::MAX - 15` (and its upper bound
`u32::MAX - 0x10000` was not 16-byte aligned). build_carrier_dll_with
only clamped at the lower bound `max(16)`, so large `template_size`
values overflowed u32 in `OFF_TEMPLATE + template_size`,
`DATA_RVA + data_vsize + 0xFFF`, and `va(OFF_TEMPLATE + template_size)`.

Define `MAX_CARRIER_TEMPLATE = (u32::MAX - 0x10000) & !15` and clamp
to `[16, MAX_CARRIER_TEMPLATE]` in both functions, with a unit test.

Assisted-by: OpenCode (Gemini 4 Neon)
@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head ce99e53 (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: 752 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: carrier_size_and_build_clamp_without_overflow.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 752 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.

Thanks!

@S1m0ne1997

Copy link
Copy Markdown

Test report for ce99e53ac3ed43c7e74d445f949ecf856ade445b (this PR's head), for #1607.

Tested

  • NVIDIA GeForce RTX 4090 24 GB (driver 621.40), AMD Ryzen 9 9950X3D, 47 GB RAM, Windows 11 Home 10.0.26300, rustc 1.100.0 (0ef1bc386 2026-11-09) x86_64-pc-windows-msvc, debug.
  • cargo build --workspace, then cargo test --workspace -- --test-threads=4 at the head: 752 passed, 0 failed, 41 ignored.
  • SKIPPED (2): gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu_wave32_program_in_wave64_hardware (no wave64).

Regression run (retail title, scripts/smoke.ps1 -Seconds 60, release; main built from the commit in its block, this PR from its head with no changes to crates/, both run with the scripts/smoke.ps1 of main, which hashes the name of the game folder)

main:

xcl-smoke v1
commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324
gpu: NVIDIA GeForce RTX 4090
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1599 (PresentX 1599)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16384 MB

this PR:

xcl-smoke v1
commit: ce99e53ac3ed43c7e74d445f949ecf856ade445b
gpu: NVIDIA GeForce RTX 4090
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1617 (PresentX 1617)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17886 MB

Not tested

  • AMD, other NVIDIA cards and drivers, Linux. I did not review the diff; this report is the build, the suite and the regression run only.

AI use: Claude Code with Claude Opus 5.5 ran the build, the suite and the regression run and wrote this report at my request.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…v#1403)

Live head ce99e53 still OPEN (merge-base --is-ancestor vs upstream/main exit 1).

tls.rs is cfg(windows)-only: const MAX_CARRIER_TEMPLATE + clamp in
carrier_template_size_for + build_carrier_dll_with + upstream test land
verbatim - tree-vs-head diff on the file is zero (pre-change tree matched
the PR parent byte-for-byte). tls_linux.rs has no carrier DLL (blocks come
straight from alloc_zeroed; install already rejects oversize via
usize::try_from + Layout::from_size_align) so the overflow class cannot
occur there - pin test only: huge total_len is a clean Err, no panic, no
allocation, no slot consumed.

Failing-first: old formula grafted to a standalone harness panics in debug
(attempt to add with overflow at (n+15)); new formula clamps to 0xFFFEFFF0
(16-aligned, OFF_TEMPLATE+MAX fits u32).

Gates: both cargo checks 0 errors; lib 746/0/1 threads=4 (+1 pin test);
title e2e 12 unique FAILs all D3D12.X GPU-only 0 non-GPU; retail fresh-root
exit 139 SIGSEGV game_scarlett_master.exe+0x2237320, stubs 20, xgr 3 apis,
unmapped [] - HOLDS, M1-RE TERMINAL. Lock audit: 0 new lock() lines.
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()).
@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.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Nov 29, 2026
@BBareth

BBareth commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

merge-check claim
head: ce99e53

@github-actions github-actions Bot added area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Nov 30, 2026
@BBareth

BBareth commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: 9dfbe3e
head: ce99e53
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2886 passed, 1 failed, 51 ignored; failing: gpu::dxil::msaa_tests::gpu_direct_image_get_resinfo_d16, which fails on the base alone too (base: 2885 passed, 1 failed, 51 ignored, 28 SKIPPED lines). 28 SKIPPED lines, as on the base. The new test tls::tests::carrier_size_and_build_clamp_without_overflow passed, as did the existing tls tests.
review: no blocking finding; one file, every hunk read. carrier_template_size_for clamped after rounding: n + 15 overflowed for a length near u64::MAX (a panic in debug builds), and the old cap u32::MAX - 0x10000 is not a multiple of 16, so the largest result was not 16-aligned. The length is now clamped to (u32::MAX - 0x10000) & !15 first and then rounded, and build_carrier_dll_with clamps its argument to the same limit, so OFF_TEMPLATE + template_size and the image size stay inside u32. The test builds the largest carrier and parses it back. Sizes below the cap give the same result as before.
performance: none.
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB, driver 621.24 (32.0.16.2124); i7-12700K; Windows 11 26200. Claude Code, Claude Fable 5.2 read the diff, ran the build and the suite and wrote this check.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) labels Nov 30, 2026
@tiagonleal

tiagonleal commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: 9dfbe3e
head: ce99e53
merge: clean
build: cargo build --workspace -> ok (also cargo build --release -p xcl)
tests: cargo test --workspace --no-fail-fast -j 8 -- --test-threads=4 -> 2886 passed, 1 failed, 51 ignored; SKIPPED: 24 lines, the same as on the base; failing: gpu::dxil::msaa_tests::gpu_direct_image_get_resinfo_d16, which fails the same way on the base alone (2885 passed, 1 failed, 51 ignored; #3513 fixes it). The new test tls::tests::carrier_size_and_build_clamp_without_overflow ran and passed. With carrier_template_size_for put back to the two lines on main, it fails with "attempt to add with overflow" at tls.rs:80; file restored after.
review: one file, whole diff read, arithmetic checked at the edges. MAX_CARRIER_TEMPLATE is (0xFFFFFFFF - 0x10000) & !15 = 0xFFFEFFF0 (tls.rs:75). carrier_template_size_for (tls.rs:78-80): 0 gives 16, 16 gives 16, 17 gives 32, exact multiples are unchanged (0x1000 gives 0x1000). The cap is itself a multiple of 16, so after the clamp n + 15 is at most 0xFFFEFFFF and the result at most the cap, with no u64 or u32 overflow for any input up to u64::MAX. Below the cap the results are the same as on main; above it main returned 0xFFFEFFFF (not 16-aligned) and now 0xFFFEFFF0. build_carrier_dll_with (tls.rs:89): at the cap, OFF_TEMPLATE + template_size is 0xFFFF00F0 and DATA_RVA + data_vsize + 0xFFF is 0xFFFF40EF, both inside u32, and va() (tls.rs:164) adds in u32 before widening, also inside. It does not round its argument up to 16 (as on main), so a direct caller passing an unaligned size still gets that size. On main the only production caller (tls.rs:626) already passed at most 0xFFFEFFFF, which does not overflow the build arithmetic, so the u32 case was reachable only through a direct call, and the u64 case needed total_len > u64::MAX - 15. Outside the diff and not fixed here: total_len (pe.rs:140) adds size_of_zero_fill to template_len with a plain +, which can overflow first on a malformed TLS directory. Sizes over the cap are still cut silently rather than refused, as before. Overlap: #1431 rewrites both carrier_template_size_for and build_carrier_dll_with, so the second to land needs a rebase.
performance: scripts/smoke.ps1, 60 s of a retail title, release, after one warm-up run of the base: 1676 frames and 17943 MB peak commit against 1659 frames and 17322 MB on the base (warm-up: 1689, 17197 MB). No device removal, no crash. The title's TLS size is far below the cap, so its carrier is the same as on main; I read the commit difference as run-to-run spread (last of five runs in a row) but did not rerun to confirm it.
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB (617.42, wave32), Ryzen 7 9800X3D, 32 GB, Windows 11 26300, rustc 1.100.0 / Claude Code (Claude Opus 5.5 ran the build, suite, tests and smoke, reviewed the diff against the merged source and wrote this)

  • Tiago

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) labels Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head ce99e53

#196 contains the same clamp in tls.rs::carrier_template_size_for / build_carrier_dll_with (total_len.clamp(16, MAX_CARRIER_TEMPLATE)) as part of a 500-line TLS alignment and locking rework, and #1431 caps the size through a new carrier_template_size_aligned helper. This PR is the overflow fix on its own, with a test.
No problems found in the diff: the new cap is 16-aligned (the old min(u32::MAX - 0x10000) was not) and DATA_RVA + OFF_TEMPLATE + cap + 0xFFF stays below u32::MAX.
Recommendation: keep open.

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

area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH ready-to-merge Has 2 independent safe merge-checks on current head (#3041) stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants