TLS: clamp carrier template size before rounding to prevent u64/u32 overflow #1393
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1403"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T15:38:53Z.
carrier_template_size_forcomputed(n + 15) & !15before clamping, overflowingu64whentotal_len > u64::MAX - 15(and its upper boundu32::MAX - 0x10000was not 16-byte aligned).build_carrier_dll_withonly clamped at the lower bound (template_size.max(16)), so largetemplate_sizevalues overflowedu32inOFF_TEMPLATE + template_size,DATA_RVA + data_vsize + 0xFFF, andva(OFF_TEMPLATE + template_size).This defines
MAX_CARRIER_TEMPLATE = (u32::MAX - 0x10000) & !15and clamps to[16, MAX_CARRIER_TEMPLATE]in both functions.What is in it
crates/xcl/src/tls.rs: clamp to[16, MAX_CARRIER_TEMPLATE]incarrier_template_size_forandbuild_carrier_dll_with; add unit testcarrier_size_and_build_clamp_without_overflow(verified panicking onu64addition overflow before the fix).Tested
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: 2no 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_overflowpanicked before the fix)Regression run
main:
PR:
Not tested
AI use
tls.rs, wrote the fix and unit test, ran the test suite andscripts/smoke.ps1regression runs, and drafted this PR description.Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T15:54:55Z.
Full-suite run, since this PR has no test report yet. Tested exact head
ce99e53(on its base930e8b4).Tested
cargo build --workspace, thenXCL_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.carrier_size_and_build_clamp_without_overflow.XCL_ADAPTER=radeon),cargo test --workspace -- --test-threads=4: 752 passed, 0 failed, 41 ignored, no skips.Not tested
AI use
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T17:01:48Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.Thanks!
Imported from ivall/xcl. Originally posted by S1m0ne1997 on 2026-11-25T14:55:21Z.
Test report for
ce99e53ac3ed43c7e74d445f949ecf856ade445b(this PR's head), for #1607.Tested
rustc 1.100.0 (0ef1bc386 2026-11-09)x86_64-pc-windows-msvc, debug.cargo build --workspace, thencargo test --workspace -- --test-threads=4at the head: 752 passed, 0 failed, 41 ignored.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;mainbuilt from the commit in its block, this PR from its head with no changes tocrates/, both run with thescripts/smoke.ps1ofmain, which hashes the name of the game folder)main:
this PR:
Not tested
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.
Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-29T20:42:25Z.
Nothing has happened on this PR for 3 days, so it is marked
staleand will be closed in 4 days. A push or a comment takes the mark off: rebase it ontomainif it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T13:24:33Z.
merge-check claim
head:
ce99e53ac3Imported from ivall/xcl. Originally posted by BBareth on 2026-11-30T13:36:28Z.
merge-check
base:
9dfbe3ea66head:
ce99e53ac3merge: 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_overflowpassed, as did the existing tls tests.review: no blocking finding; one file, every hunk read.
carrier_template_size_forclamped after rounding:n + 15overflowed for a length nearu64::MAX(a panic in debug builds), and the old capu32::MAX - 0x10000is not a multiple of 16, so the largest result was not 16-aligned. The length is now clamped to(u32::MAX - 0x10000) & !15first and then rounded, andbuild_carrier_dll_withclamps its argument to the same limit, soOFF_TEMPLATE + template_sizeand 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.
Imported from ivall/xcl. Originally posted by tiagonleal on 2026-11-30T13:46:44Z.
merge-check
base:
9dfbe3ea66head:
ce99e53ac3merge: 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 testtls::tests::carrier_size_and_build_clamp_without_overflowran and passed. Withcarrier_template_size_forput back to the two lines on main, it fails with "attempt to add with overflow" attls.rs:80; file restored after.review: one file, whole diff read, arithmetic checked at the edges.
MAX_CARRIER_TEMPLATEis(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 clampn + 15is at most 0xFFFEFFFF and the result at most the cap, with no u64 or u32 overflow for any input up tou64::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_sizeis 0xFFFF00F0 andDATA_RVA + data_vsize + 0xFFFis 0xFFFF40EF, both inside u32, andva()(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 neededtotal_len > u64::MAX - 15. Outside the diff and not fixed here:total_len(pe.rs:140) addssize_of_zero_filltotemplate_lenwith 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 bothcarrier_template_size_forandbuild_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)
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:38:38Z.
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.