HLE: retain DirectStorage file references across queued reads #2441

Merged
fuzeofficlal merged 1 commit from upstream-pr/2441 into main 2026-11-30 14:12:02 +00:00
fuzeofficlal commented 2026-11-29 13:04:26 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by fuzeofficlal on 2026-11-29T13:04:26Z.


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
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5550980404 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2441). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-29T13:04:26Z. --- ## 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`
Dukestar69 commented 2026-11-29 13:25:58 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-29T13:25:58Z.

Review: APPROVED


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

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6103068698 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2441#pullrequestreview-6103068698). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-29T13:25:58Z. **Review:** APPROVED --- 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).
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
trustno1-coder/VIctory-NVIDIA!2441
No description provided.