HLE: retain DirectStorage file references across queued reads #2987

Open
github-bot wants to merge 1 commit from github-bot/gh-2987 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 02:24 UTC · original
fuzeofficlal:fix/hle-dstorage-retain-file-ref → main · +140 −39 · 1 files · 1 commits · open

Problem

enqueue_request in crates/victory-hle/src/modules/dstorage.rs copied the raw Request struct (including its source file pointer) without retaining a reference on Instance<File>. Unlike enqueue_status (which calls add_ref_instance and releases on Status::drop), if a title released its final reference to a file object after enqueueing/submitting a read, release::<File> dropped the Instance<File> and closed the underlying host file handle while the request was still queued or running, causing the worker thread to dereference freed memory in read_into / source_name.

Fix

  • Wrap queued reads in a non-Copy QueuedRequest holding Option<NonNull<Instance<File>>>:
    • Calls add_ref_instance on enqueue for file-backed non-null requests (options & MEMORY_SOURCE == 0).
    • Calls release in Drop when the request completes, is rejected by a closed queue, or is discarded by close_lane.
  • Pass Job by value into Lanes::finish and drop QueuedRequest before calling lane.flush() so the file handle is guaranteed closed before completion is published to the title's StatusArray.
  • Guard null request.source in read_into (returning Err(E_INVALIDARG)) and source_name (returning "null").

Verification

  • Added 3 unit tests in modules::dstorage::tests:
    • queued_reads_keep_the_file_open_until_completion: enqueues 2 reads, calls Release immediately (asserting refcount 2), submits, waits for S_OK, verifies std::fs::remove_file succeeds immediately upon completion (confirming zero handle leak on Windows FILE_SHARE_READ handle), and verifies both read buffers match.
    • closing_a_queue_releases_unsubmitted_file_reads: verifies unsubmitted file reads drop their file references when the queue closes (remove_file succeeds).
    • read_into_rejects_a_null_source: verifies both file and memory null sources return E_INVALIDARG.
  • cargo test --locked -p victory-hle --lib modules::dstorage::tests -- --test-threads=1: 19/19 passed.
  • cargo clippy --locked -p victory-hle --tests -- -D warnings (default & --all-features): 0 warnings.
  • cargo xtask policy: 191 files ok.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 02:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/2987) `fuzeofficlal:fix/hle-dstorage-retain-file-ref` → `main` · +140 −39 · 1 files · 1 commits · open ## Problem `enqueue_request` in `crates/victory-hle/src/modules/dstorage.rs` copied the raw `Request` struct (including its `source` file pointer) without retaining a reference on `Instance<File>`. Unlike `enqueue_status` (which calls `add_ref_instance` and releases on `Status::drop`), if a title released its final reference to a file object after enqueueing/submitting a read, `release::<File>` dropped the `Instance<File>` and closed the underlying host file handle while the request was still queued or running, causing the worker thread to dereference freed memory in `read_into` / `source_name`. ## Fix - Wrap queued reads in a non-`Copy` `QueuedRequest` holding `Option<NonNull<Instance<File>>>`: - Calls `add_ref_instance` on enqueue for file-backed non-null requests (`options & MEMORY_SOURCE == 0`). - Calls `release` in `Drop` when the request completes, is rejected by a closed queue, or is discarded by `close_lane`. - Pass `Job` by value into `Lanes::finish` and drop `QueuedRequest` **before** calling `lane.flush()` so the file handle is guaranteed closed before completion is published to the title's `StatusArray`. - Guard null `request.source` in `read_into` (returning `Err(E_INVALIDARG)`) and `source_name` (returning `"null"`). ## Verification - Added 3 unit tests in `modules::dstorage::tests`: - `queued_reads_keep_the_file_open_until_completion`: enqueues 2 reads, calls `Release` immediately (asserting refcount `2`), submits, waits for `S_OK`, verifies `std::fs::remove_file` succeeds immediately upon completion (confirming zero handle leak on Windows `FILE_SHARE_READ` handle), and verifies both read buffers match. - `closing_a_queue_releases_unsubmitted_file_reads`: verifies unsubmitted file reads drop their file references when the queue closes (`remove_file` succeeds). - `read_into_rejects_a_null_source`: verifies both file and memory null sources return `E_INVALIDARG`. - `cargo test --locked -p victory-hle --lib modules::dstorage::tests -- --test-threads=1`: **19/19 passed**. - `cargo clippy --locked -p victory-hle --tests -- -D warnings` (default & `--all-features`): 0 warnings. - `cargo xtask policy`: 191 files ok.
Hold a COM reference to Instance<File> in QueuedRequest from enqueue until the read finishes (before publishing status completion) or is discarded on queue close, matching StatusArray refcounting and preventing worker use-after-free when the title releases its file handle early. Also guard null request sources in read_into and source_name.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:19:25 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-26 02:36 UTC · original · reviewed

Verified: real use-after-free — a queued read held only the raw Request's source pointer, so a title closing the file while reads sat unsubmitted or in-flight left a dangling Instance. QueuedRequest add_refs on enqueue (non-memory sources) and releases on drop, which covers job completion, closed-queue command drops, and lane teardown. The drop-in-finish ordering before flush/publish is right, and the null-source guards in read_into/source_name are a sensible belt. Tests cover both the completion path and the close-drops-reads path via refcounts.

**@⁠kotekompotek** on GitHub · 2026-11-26 02:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/2987#pullrequestreview-6067813308) · **reviewed** Verified: real use-after-free — a queued read held only the raw Request's source pointer, so a title closing the file while reads sat unsubmitted or in-flight left a dangling Instance<File>. QueuedRequest add_refs on enqueue (non-memory sources) and releases on drop, which covers job completion, closed-queue command drops, and lane teardown. The drop-in-finish ordering before flush/publish is right, and the null-source guards in read_into/source_name are a sensible belt. Tests cover both the completion path and the close-drops-reads path via refcounts.
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-26 05:07 UTC · original

Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head 3223996 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 869 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: queued_reads_keep_the_file_open_until_completion, closing_a_queue_releases_unsubmitted_file_reads, read_into_rejects_a_null_source.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-26 05:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/2987#issuecomment-7183199499) Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head `3223996` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **869 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `queued_reads_keep_the_file_open_until_completion`, `closing_a_queue_releases_unsubmitted_file_reads`, `read_into_rejects_a_null_source`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 05:41 UTC · original

Automated triage for the maintainers.

Merge candidate. In crates/victory-hle/src/modules/dstorage.rs, enqueue_request copied the Request with a raw file pointer and took no reference, so read_into/source_name relied on the title keeping the file open (main's SAFETY comment says as much). Queued reads are now a QueuedRequest that holds a reference on the Instance<File> from enqueue until the job is finished or dropped by a closed queue. This is the same pattern enqueue_status already uses for status arrays. Lanes::finish takes the job by value and releases the file before flush publishes completion. I checked the paths that drop a request: a finished job, queue on a closed lane, and close_lane's unsubmitted list. Each releases exactly once, and memory-source requests take no reference. The two new tests cover early release and close, and the null-source guard in read_into turns a crash into E_INVALIDARG.

Note for the maintainer: this is defensive. No title run shows the title releasing a file with reads in flight, and a title-visible Release count is now higher while reads are queued. The change is cheap and removes a use-after-free if that ever happens.

CI: passing (check).

Recommending a look, not approving.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 05:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/2987#issuecomment-7183710289) <!-- victory-triage v1 head=3223996de1292f1ffb4929d2f31a92163b4dc643 verdict=merge-candidate at=2026-11-26T05:40:55Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-hle/src/modules/dstorage.rs`, `enqueue_request` copied the `Request` with a raw file pointer and took no reference, so `read_into`/`source_name` relied on the title keeping the file open (main's `SAFETY` comment says as much). Queued reads are now a `QueuedRequest` that holds a reference on the `Instance<File>` from enqueue until the job is finished or dropped by a closed queue. This is the same pattern `enqueue_status` already uses for status arrays. `Lanes::finish` takes the job by value and releases the file before `flush` publishes completion. I checked the paths that drop a request: a finished job, `queue` on a closed lane, and `close_lane`'s unsubmitted list. Each releases exactly once, and memory-source requests take no reference. The two new tests cover early release and close, and the null-source guard in `read_into` turns a crash into `E_INVALIDARG`. Note for the maintainer: this is defensive. No title run shows the title releasing a file with reads in flight, and a title-visible `Release` count is now higher while reads are queued. The change is cheap and removes a use-after-free if that ever happens. CI: passing (`check`). Recommending a look, not approving. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 07:50 UTC · original · approved these changes

Tested locally merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-hle dstorage: all tests passed.
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 07:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2987#pullrequestreview-6070273384) · **approved these changes** Tested locally merged onto `origin/main` (`63bcca4`) on Windows 11 x64: - `cargo test -p victory-hle dstorage`: all tests passed. - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2987'
git switch 'github-bot/gh-2987'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2987'
git switch 'github-bot/gh-2987'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2987'
git switch 'main'
git merge --squash 'github-bot/gh-2987'
git switch 'main'
git merge --ff-only 'github-bot/gh-2987'
git switch 'main'
git merge 'github-bot/gh-2987'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2987
No description provided.