HLE: retain DirectStorage file references across queued reads #2441

Manually merged
github merged 1 commit from github-bot/gh-2441 into main 2026-11-30 14:17:11 +00:00 AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-29 13:04 UTC · original
fuzeofficlal:fix/hle-dstorage-retain-file-ref → main · +150 −34 · 1 files · 1 commits · merged 2026-11-30 14:12 UTC

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 handles while the request was still queued or running, causing the worker thread to dereference freed memory in read_into, source_path, or ErrorRecord::of.

Fix

  • Add a non-Copy HeldFile(Option<NonNull<Instance<File>>>) owned by Read and moved into Job:
    • 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 when the lane closes.
  • Pass Job by value into Lanes::finish, construct any ErrorRecord while file is still alive, and drop file before calling lane.failed / lane.flush() so the host file handles are guaranteed closed before completion or error events are published to the title.
  • Guard null request.source in read_into (returning Err(E_INVALIDARG)).

Verification

  • Added 3 regression tests in modules::dstorage::tests (queued_reads_keep_the_file_open_until_completion, closing_a_queue_releases_unsubmitted_file_reads, read_into_rejects_a_null_source).
  • Verified locally on VIctory-NVIDIA:
    • cargo test --locked -p victory-hle --lib modules::dstorage::tests -- --test-threads=1
    • cargo test --locked -p victory-hle -- --test-threads=1
    • cargo clippy --locked -p victory-hle --all-targets (no new warnings)
    • cargo run --quiet --locked -p xtask -- policy --staged
**@⁠fuzeofficlal** on GitHub · 2026-11-29 13:04 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2441) `fuzeofficlal:fix/hle-dstorage-retain-file-ref` → `main` · +150 −34 · 1 files · 1 commits · merged 2026-11-30 14:12 UTC ## 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 handles while the request was still queued or running, causing the worker thread to dereference freed memory in `read_into`, `source_path`, or `ErrorRecord::of`. ## Fix - Add a non-`Copy` `HeldFile(Option<NonNull<Instance<File>>>)` owned by `Read` and moved into `Job`: - 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 when the lane closes. - Pass `Job` by value into `Lanes::finish`, construct any `ErrorRecord` while `file` is still alive, and drop `file` **before** calling `lane.failed` / `lane.flush()` so the host file handles are guaranteed closed before completion or error events are published to the title. - Guard null `request.source` in `read_into` (returning `Err(E_INVALIDARG)`). ## Verification - Added 3 regression tests in `modules::dstorage::tests` (`queued_reads_keep_the_file_open_until_completion`, `closing_a_queue_releases_unsubmitted_file_reads`, `read_into_rejects_a_null_source`). - Verified locally on `VIctory-NVIDIA`: - `cargo test --locked -p victory-hle --lib modules::dstorage::tests -- --test-threads=1` - `cargo test --locked -p victory-hle -- --test-threads=1` - `cargo clippy --locked -p victory-hle --all-targets` (no new warnings) - `cargo run --quiet --locked -p xtask -- policy --staged`
Hold a COM reference to Instance<File> in HeldFile from enqueue until the read finishes (after constructing any ErrorRecord and before publishing status completion or signalling the error event) 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 13:06:14 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-29 13:25 UTC · original · approved these changes

Tested locally merged onto main (829188e9).

  • Merge: Clean merge in crates/victory-hle/src/modules/dstorage.rs.
  • Tests: cargo test -p victory-hle --lib dstorage passed (32 passed; 0 failed; 2 ignored, including the new queued_reads_keep_the_file_open_until_completion and closing_a_queue_releases_unsubmitted_file_reads tests) with 0 warnings in victory-hle.

Important lifetime fix preventing use-after-release of DirectStorage file handles across queued reads. LGTM.

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

**@⁠Dukestar69** on GitHub · 2026-11-29 13:25 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2441#pullrequestreview-6103068698) · **approved these changes** Tested locally merged onto `main` (`829188e9`). - **Merge**: Clean merge in `crates/victory-hle/src/modules/dstorage.rs`. - **Tests**: `cargo test -p victory-hle --lib dstorage` passed (`32 passed; 0 failed; 2 ignored`, including the new `queued_reads_keep_the_file_open_until_completion` and `closing_a_queue_releases_unsubmitted_file_reads` tests) with 0 warnings in `victory-hle`. Important lifetime fix preventing use-after-release of DirectStorage file handles across queued reads. LGTM. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
github manually merged commit 9d51109f40 into main 2026-11-30 14:17:11 +00:00
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/trustno1-coder--VIctory-NVIDIA!2441
No description provided.