d3d12-gpu: in_shader_heap takes no empty range and does not overflow #3045
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3045"
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 ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T05:02:51Z.
Claimed in the coordination issue as: D3D12-GPU-IN-SHADER-HEAP-EMPTY-RANGE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7278305873)
What and why
in_shader_heap(at, len)ind3d12/crates/leonida-gpu/src/descriptor.rstestsfrom < end && start < to. Withlen == 0andatinside a placed shader-visible heap,from == toand both sides still hold, so a copy that writes no bytes counted as touching the heap, and the copy and placed-copy steps askedport_journalfor a round for nothing.at + lenwas also added unchecked.The change: a zero-length range touches no heap, and the end uses
saturating_add.This ports ahmetbagci's fix from the sibling VIctory tree (TaynanGT/Vice-Tay
0f102de, GPL-3.0), credited as co-author.Tested
descriptor::tests::in_shader_heap_takes_no_empty_range_and_does_not_overflow: places a shader-visible view heap over a buffer, then checks a 32-byte range inside is in, a zero-length range inside or at its start is not, a range to the top of memory is in, and(usize::MAX, 1)is not. Withmain's logic (8422def75) it fails at the "no bytes copied" assert; with the change it passes.cargo test -p leonida-gpu --lib: 669 passed, 0 failed, 59 ignored.cargo clippy -p leonida-gpu --lib --tests: no warning in the changed lines.GPUs tested: n/a (CPU unit tests)
AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no translator change)🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T07:18:01Z.
Independent verification on Windows 10 Pro 22H2 (build 19045), NVIDIA GeForce RTX 5070 12 GB (driver
621.40), MSVC toolchain (1.98.1). Headcb30a595amerged cleanly ontomain(f37c4a87d).python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/descriptor.rs: PASS.cargo test -p leonida-gpu --locked --lib in_shader_heap_takes_no_empty_range_and_does_not_overflow: 1 passed, 0 failed.cargo test -p leonida-gpu --locked --lib -- --skip depth_bounds_pipeline_loads_and_filters_draws: 668 passed, 0 failed, 59 ignored.cargo clippy -p leonida-gpu --locked --lib --tests: clean (0 new warnings).in_shader_heap(descriptor.rs:345-351) cleanly guardslen == 0->falseand computestowithsaturating_add.Verdict: PASS (unit tests & source review; title not run).
AI-assisted: yes (Antigravity).
Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-02T07:56:25Z.
VERIFY-3045 — Linux, Fedora 45, Rust 1.98.1, cross-compiled for x86_64-pc-windows-gnu, Wine 11.21, no GPU device, no game files
Verdict: PASS. The change fixes a real wrong answer and a real overflow in
in_shader_heap, the fix is at the root cause (the empty range and the addition), it is minimal and in the style of the file, and the new test fails on the merge base with exactly the message the fix is about and passes with the pull request. Nothing regressed: 728 tests on each side, 0 regressions, the only three failures are the same threewatchtests that fail on the base as well.head
cb30a595ababbbb14d4fd2e2f8a65ed8127e5960("d3d12-gpu: in_shader_heap takes no empty range and does not overflow"), baseorigin/main=0ae5a2664b9cd2fd30abfb1f6096f1c07b030f39, merge base8422def759cda12c89f918438eeb8d53dd5972a6(4 commits behindmain). One file, +25/−1:d3d12/crates/leonida-gpu/src/descriptor.rs.1. What it changes
descriptor::in_shader_heap(at: usize, len: usize) -> boolanswers "does[at, at + len)touch the title memory of a placed shader-visible heap of descriptors". It is called from two places, both of which use the answer to decide whether to ask the journal for a round:host.rs:9781— a deferredStep::Copyinto a heap's records ("Renderer port #7: into a heap's records, the rounds asked earlier did not see it") →crate::port_journal::prep_wrote().host.rs:9814— the same for a deferredStep::Placed.The old body was
and the new body is
Two defects, both fixed at the source:
len == 0,from == to, sofrom < end && start < tois true for every heap that containsat— the answer was "yes" for a copy of no bytes, and the caller asked the journal for a round it did not need. The fix makeslen == 0answer "no" before the heap list is consulted at all, so the empty case no longer depends on the heap list at all.at as u64 + len as u64could overflow. At the top of memory the addition overflows: in a debug build it panics ("attempt to add with overflow"), and in a release build it wraps, sotobecomes a small number and the range test answers something unrelated to the range asked about.saturating_addmakes the end of the range the top of memory instead, which is the truthful answer for a range that runs past it.2. The new test checks the property that matters
descriptor::tests::in_shader_heap_takes_no_empty_range_and_does_not_overflow(the PR's only new test) is the property the caller depends on, not an implementation detail:It places a real shader-visible descriptor heap over a CPU buffer (
create(desc, Placement { address, size }, &mut h)— the crate's own placement bookkeeping,create_heapwith a null device, no GPU and no game files needed), and then asks five questions:in_shader_heap(address + 32, 32)→ true: a real copy into the heap is still recognised (the fix did not break the positive case).in_shader_heap(address + 32, 0)→ false: "no bytes copied" — the empty copy does not touch the heap.in_shader_heap(address, 0)→ false: "no bytes copied, at its start" — the empty copy is refused even when it starts exactly at the heap's first byte (the case where the oldfrom == totest was true).in_shader_heap(address + 32, usize::MAX)→ true: a length that runs to the top of memory still hits the heap (the saturating add).in_shader_heap(usize::MAX, 1)→ false: a range that starts at the top of memory touches no heap (the old code overflowed here).The test body really runs here; it is not a no-op that reports "ok" because there is no device. The heap is placed in ordinary host memory over a
Vec, so no device is needed, and I confirmed the failure on the red side is an assertion failure inside the test, not a skip: the red side prints(line 3807 on the red side = line 3811 on the green side, where the four added lines of the fix shift it), i.e. the second assertion,
!in_shader_heap(address + 32, 0), is reached and fails. The old code answered "true" for the empty range, exactly as analysed above.The test discriminates the two defects separately. Assertions 2 and 3 are the empty-range defect; assertions 4 and 5 are the overflow defect (the old code panics with "attempt to add with overflow" at
at == usize::MAX, so on the base the test cannot even get past the fifth assertion).3. What the tests do here
I cross-compiled the crate for
x86_64-pc-windows-gnuand ran the test binaries under Wine withpython3 tools/wine_red_green.py 3045 --crate leonida-gpu(run from/home/shangli/oc-workspace/gta6), which builds the red side the wayscripts/red_green.pydoes (merge base + only the PR's test hunks) and the green side from the PR head, then runs every test binary of the crate under Wine and compares per-test outcomes.916f84bd0a3a(merge base8422def7+ the PR's test hunks)descriptor.rs:3807cb30a595(PR head)The tool's verdict: "red then green" — 1 red→green (the new test), 0 regressions, 3 failing on both sides, 665 passing on both sides, no test present on one side only.
To be sure the red side really had the fix missing (and not, say, a stale binary), I also built the red side alone and ran the new test by name under Wine (
lane/red_3045.log): the test binary reported1 failed; 727 filtered outwith the panic above.4. Any failure I saw and why it is not this PR
watchtests fail on both sides, on the merge base as well as on the head:watch::many_askers_tests::holes_are_known_until_the_runtime_tells,watch::many_askers_tests::no_mapping_is_lost_between_askers,watch::tests::what_is_left_alone_is_no_known_hole. They are inwatch.rs, which this pull request does not touch at all, and they fail identically with the pull request reverted (measured 6/6 failures per side on the base of PR #3046, which has the same merge base). They are environment failures of the guard-page/exception machinery ofwatchunder Wine without a real GPU, not a property of this change.descriptor::SHADER_HEAPSand only removes it withrelease(h)after the last assertion. If an assertion fails, the heap entry is left behind for the rest of the test process. On the green side the test passes, so this never happens there; on a red build the stale entry would point at freed memory. Nothing else in the crate's tests asksin_shader_heap, so no other test is affected, and it does not change the verdict.5. What I did not check
in_shader_heapis a runtime decision made while the title's copies are deferred. I could not check that a real copy into a real shader-visible heap still asks the journal for a round, nor that an empty copy no longer does; the test only exercises the bookkeeping function over host memory.cargo test --target x86_64-pc-windows-gnuunder Wine 11.21 on Fedora 45. The#[cfg(windows)]code path was taken (the crate does not compile for Linux), but the ABI/exception behaviour of a real Windows build is not exercised.cargo testproduces); I did not check the release behaviour of the old code, so the "wraps to a wrong answer" half of my analysis of defect 2 is reasoning from the code, not something I measured.at + lenaddition at the call sites:host.rs:9781/host.rs:9814compute their owndst/len; I read the two call sites but did not trace every producer of aStep::Copy/Step::Placedlength, so I did not check whether a zero-length or overflowing step can actually reachin_shader_heapin the running title.leonida-hle,leonida-loader) and the repository's own Python test suite were not run; onlyleonida-gpuwas, because the change is confined to it.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:16:07Z.
RUN (in game, grouped): this head
cb30a595awas merged withgit merge --no-ff, together with nine other rendering pull requests, ontomainf6dd2242e(no conflicts), and the d3d12 layer was rebuilt from that tree (diag profile, with DLSS). RTX 4060 8 GB, driver 610.88, Windows 11;scripts/play-d3d12.ps1 -SeriesS -Resolution 1080 -FrameGen 2, low-memory profile, keyboard-driven entry into the world and a walking route.Heads in this run: #2854
81a6e94a9, #293668215ad0b, #2939561bbc19c, #2997ef429f923, #3043915d4d5aa, #3045cb30a595a, #30460f19a0819, #3047c33e5b48e, #30482ab0b4b9d, #3051856615ce7.AI-assisted (Claude Code, Claude Opus 5.5)
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T13:21:06Z.
Merging. Head
cb30a595amerges cleanly withmainbf7886746and does not force a rebase of #2566. Checked on headcb30a595a: all 6 CI checks green; third-party red/green by imShangLi on this head; an in-game run of at least 381 s with this head merged onto mainf6dd2242e(grouped with nine other rendering pull requests, comment above) showed no device loss, no unhandled exception and no hang; item claimed in the coordination issue; author has earlier merged pull requests; conflict-free merge against main. Merged by MoonEyes.AI-assisted (Claude Code).