HLE: retain DirectStorage file references across queued reads #2985

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/2987 into main
fuzeofficlal commented 2026-11-26 02:24:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T02:24:10Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6618143636 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2987). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T02:24:10Z. --- ## 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.
kotekompotek commented 2026-11-26 03:04:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T02:36:59Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6067813308 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2987#pullrequestreview-6067813308). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T02:36:59Z. **Review:** COMMENTED --- 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.
futurepro62 commented 2026-11-26 06:03:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-26T05:07:20Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7183199499 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2987#issuecomment-7183199499). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-26T05:07:20Z. --- 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.
Marvisak commented 2026-11-26 06:03:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T05:41:03Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7183710289 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2987#issuecomment-7183710289). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T05:41:03Z. --- <!-- 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)_
Dukestar69 commented 2026-11-26 08:02:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T07:50:47Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6070273384 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2987#pullrequestreview-6070273384). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T07:50:47Z. **Review:** APPROVED --- 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 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/2987:upstream-pr/2987'
git switch 'upstream-pr/2987'

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/2987'
git switch 'upstream-pr/2987'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2987'
git switch 'upstream-pr/2987'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2987'
git switch 'main'
git merge --squash 'upstream-pr/2987'
git switch 'main'
git merge --ff-only 'upstream-pr/2987'
git switch 'main'
git merge 'upstream-pr/2987'
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
Marvisak/VIctory!2985
No description provided.