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

Open
coderanonymus wants to merge 2 commits from upstream-pr/668 into main
coderanonymus commented 2026-11-25 04:16:41 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by coderanonymus on 2026-11-25T04:16:41Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:6597088897 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/668). Originally posted by [coderanonymus](https://github.com/coderanonymus) on 2026-11-25T04:16:41Z. --- 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)
chatgpt-codex-connector[bot commented 2026-11-25 05:08:58 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T04:16:49Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7152697586 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/668#issuecomment-7152697586). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T04:16:49Z. --- <!-- 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>
chatgpt-codex-connector[bot commented 2026-11-25 05:08:58 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by chatgpt-codex-connector[bot] on 2026-11-25T04:16:51Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:comment:7152698087 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/668#issuecomment-7152698087). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-25T04:16:51Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/668:upstream-pr/668'
git switch 'upstream-pr/668'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/668'
git switch 'upstream-pr/668'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/668'
git switch 'upstream-pr/668'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/668'
git switch 'main'
git merge --squash 'upstream-pr/668'
git switch 'main'
git merge --ff-only 'upstream-pr/668'
git switch 'main'
git merge 'upstream-pr/668'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!668
No description provided.