GPU: hold SHADER_HEAPS while a shader-visible heap is used (port of upstream 628b95d) #668

Open
github-bot wants to merge 2 commits from github-bot/gh-668 into main AGit
Owner

@⁠coderanonymus on GitHub · 2026-11-25 04:16 UTC · original
coderanonymus:shader-heaps-lock → main · +100 −22 · 5 files · 2 commits · open

Port of upstream 628b95d (by Manuel Pérez). It is stacked on #642, which fixes the clippy failure on main.

I left this commit out of #653 because it doesn't build on the fork as it stands. It merges cleanly with #653.

Why

shader_slot() takes the SHADER_HEAPS lock, finds the heap and returns &Heap after the lock is released. A title that releases a descriptor heap on one thread while another writes a descriptor can therefore have Heap::drop free the heap while it is still being read.

The fork has a second caller with the same window. make_waiting() reads the slot (view(handle)) and makes the host view from a heap it no longer holds. The fork's own comment there notes the handle is "read only while its heap is there", but nothing kept the heap there.

What

  • Upstream's commit, cherry-picked as it is:
    • mirror() keeps SHADER_HEAPS locked for its whole run.
    • copy_simple() returns on an invalid heap kind.
    • create_heap refuses a placed heap whose address + size wraps.
    • resolve_queries checks offset + count * bytes for overflow, with a test (resolve_queries_rejects_overflowing_offset).
  • For the fork, in the same commit: make_waiting() finds the heap the same way and keeps SHADER_HEAPS held while it reads the slot and calls HostHeap::mirror(.., true). shader_slot() is gone.

Review

Ultracode-reviewed: three independent read-only reviewers traced the fork's code.

  • Re-entrancy. Everything reachable under the lock was traced, including HostHeap::mirror for both for_draw values, wait_for, host_copy, host texture creation and retire. Nothing takes SHADER_HEAPS again, records a descriptor, or drops a heap.
  • Lock order. Locks held when SHADER_HEAPS is taken: submitted, RELEASING, a queue's held lock, list.state. Locks taken under it: pending, made, VIEWS, WAITING, MADE_LATE, resource, shadow and card locks. No path takes the two sets in the opposite order, so the order is acyclic.
  • The rest of the pick. The fork doesn't rely on copy_simple's old behaviour, and the new test needs no host device.

The cost is wait time, not deadlock. Descriptor writes on other threads can now wait while a slot's host view is made, which can create a host texture. That's a short wait per slot. The first Heap::host() on a heap replays its queued writes while holding pending, so writes to any heap wait once per heap at startup.

Verification

  • cargo clippy --workspace --all-targets --locked -- -D warnings, and the same with --all-features, for x86_64-pc-windows-msvc (cross-checked from Linux): clean.
  • Not run: Windows tests, the game, or a run under load. The new test needs Windows.

No game data used.

🤖 Generated with Claude Code

**@⁠coderanonymus** on GitHub · 2026-11-25 04:16 UTC · [original](https://github.com/fxceless/victory-fork/pull/668) `coderanonymus:shader-heaps-lock` → `main` · +100 −22 · 5 files · 2 commits · open Port of upstream 628b95d (by Manuel Pérez). It is stacked on #642, which fixes the clippy failure on `main`. I left this commit out of #653 because it doesn't build on the fork as it stands. It merges cleanly with #653. ## Why `shader_slot()` takes the `SHADER_HEAPS` lock, finds the heap and returns `&Heap` after the lock is released. A title that releases a descriptor heap on one thread while another writes a descriptor can therefore have `Heap::drop` free the heap while it is still being read. The fork has a second caller with the same window. `make_waiting()` reads the slot (`view(handle)`) and makes the host view from a heap it no longer holds. The fork's own comment there notes the handle is "read only while its heap is there", but nothing kept the heap there. ## What - Upstream's commit, cherry-picked as it is: - `mirror()` keeps `SHADER_HEAPS` locked for its whole run. - `copy_simple()` returns on an invalid heap kind. - `create_heap` refuses a placed heap whose address + size wraps. - `resolve_queries` checks `offset + count * bytes` for overflow, with a test (`resolve_queries_rejects_overflowing_offset`). - For the fork, in the same commit: `make_waiting()` finds the heap the same way and keeps `SHADER_HEAPS` held while it reads the slot and calls `HostHeap::mirror(.., true)`. `shader_slot()` is gone. ## Review Ultracode-reviewed: three independent read-only reviewers traced the fork's code. - **Re-entrancy.** Everything reachable under the lock was traced, including `HostHeap::mirror` for both `for_draw` values, `wait_for`, `host_copy`, host texture creation and `retire`. Nothing takes `SHADER_HEAPS` again, records a descriptor, or drops a heap. - **Lock order.** Locks held when `SHADER_HEAPS` is taken: `submitted`, `RELEASING`, a queue's held lock, `list.state`. Locks taken under it: `pending`, `made`, `VIEWS`, `WAITING`, `MADE_LATE`, resource, shadow and card locks. No path takes the two sets in the opposite order, so the order is acyclic. - **The rest of the pick.** The fork doesn't rely on `copy_simple`'s old behaviour, and the new test needs no host device. The cost is wait time, not deadlock. Descriptor writes on other threads can now wait while a slot's host view is made, which can create a host texture. That's a short wait per slot. The first `Heap::host()` on a heap replays its queued writes while holding `pending`, so writes to any heap wait once per heap at startup. ## Verification - `cargo clippy --workspace --all-targets --locked -- -D warnings`, and the same with `--all-features`, for `x86_64-pc-windows-msvc` (cross-checked from Linux): clean. - Not run: Windows tests, the game, or a run under load. The new test needs Windows. No game data used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- indirect.rs: the timing rig kept the written depth view it never reads
  after setup; drop the field.
- draw.rs: the scissor census sorts by a reversed key.
- resource.rs: placed_filler_twins is read only by the diagnostics
  build's copystat; allow it to be dead without the feature, as
  shadow and pipeline do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mirror() previously dropped the SHADER_HEAPS lock inside shader_slot() before using the returned &Heap, allowing a concurrent Heap::drop to free the heap while mirror() accessed it. Also guards copy_simple() against invalid descriptor kinds, rejects placed heaps whose address+size wraps u64, and checks offset+count*bytes in resolve_queries() against overflow (with a regression test).

(cherry picked from commit 628b95da5e)

Port notes: the fork's make_waiting() was a second caller of
shader_slot(). It now looks the heap up the same way and keeps
SHADER_HEAPS held while it reads the slot (view()), and while it makes
the host view (heap.host, HostHeap::mirror), so the heap cannot be freed
under it either.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:48:35 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 04:16 UTC · original

Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-25T04:16:46.412170Z
We couldn’t complete this review. Comment @codex review to try again.
90ecea5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review" or "@⁠codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 04:16 UTC · [original](https://github.com/fxceless/victory-fork/pull/668#issuecomment-7152697586) <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"90ecea5828ff04713d46e9084b9f2905cc48bde5","has_any_findings":true,"has_unresolved_findings":true,"status":"failed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ❌ Failed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ❌ **Failed** <relative-time datetime="2026-11-25T04:16:46.412170Z">2026-11-25T04:16:46.412170Z</relative-time><br/>We couldn’t complete this review. Comment `@codex review` to try again. | `90ecea5` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review" or "@⁠codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-25 04:16 UTC · original

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-25 04:16 UTC · [original](https://github.com/fxceless/victory-fork/pull/668#issuecomment-7152698087) You have reached your Codex usage limits for code reviews. You can see your limits in the [Codex usage dashboard](https://chatgpt.com/codex/cloud/settings/usage).
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/descriptor.rs
  • crates/victory-gpu/src/indirect.rs
  • crates/victory-gpu/src/resource.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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