HLE/dstorage: retain IDStorageFile COM reference across queued reads #621

Open
AscilCH wants to merge 1 commit from upstream-pr/621 into main
AscilCH commented 2026-11-25 03:25:56 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T03:25:56Z.


Problem

In crates/victory-hle/src/modules/dstorage.rs, enqueue_status properly retained a COM reference on IDStorageStatusArray via add_ref_instance / Status::drop, while enqueue_request pushed raw Copy Request structs into the scheduler without retaining request.source when backed by an IDStorageFile.

Under DirectStorage COM rules, titles may call IDStorageFile::Release() immediately after IDStorageQueue::EnqueueRequest() (even before Submit()), relying on the queue to hold an internal reference until the read finishes. Without retaining the file, Release() immediately freed Instance<File> and closed its OS file handle while the read sat in unsubmitted, reads, or a running worker Job, causing a worker use-after-free in read_into().

Fix

  • Introduce RAII wrapper QueuedRead holding request: Request and file: Option<FileRef>, calling add_ref_instance on construction for supported file-backed requests (options & !SUPPORTED == 0 && options & MEMORY_SOURCE == 0 && !source.is_null()) and release on release_file() / Drop.
  • Call job.request.release_file() in work() after execute() and before locking lanes / calling lanes.finish(), ensuring the COM refcount and OS file handle are released before status completion becomes visible to the title.
  • Add synthetic unit tests releasing_file_after_enqueue_keeps_it_alive_until_read_completes and closing_queue_with_unsubmitted_file_read_releases_file_ref.

Verification

  • cargo test -p victory-hle --locked dstorage -- --test-threads=1: 16 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-hle --no-deps --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6596489380 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/621). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T03:25:56Z. --- ## Problem In `crates/victory-hle/src/modules/dstorage.rs`, `enqueue_status` properly retained a COM reference on `IDStorageStatusArray` via `add_ref_instance` / `Status::drop`, while `enqueue_request` pushed raw `Copy` `Request` structs into the scheduler without retaining `request.source` when backed by an `IDStorageFile`. Under DirectStorage COM rules, titles may call `IDStorageFile::Release()` immediately after `IDStorageQueue::EnqueueRequest()` (even before `Submit()`), relying on the queue to hold an internal reference until the read finishes. Without retaining the file, `Release()` immediately freed `Instance<File>` and closed its OS file handle while the read sat in `unsubmitted`, `reads`, or a running worker `Job`, causing a worker use-after-free in `read_into()`. ## Fix - Introduce RAII wrapper `QueuedRead` holding `request: Request` and `file: Option<FileRef>`, calling `add_ref_instance` on construction for supported file-backed requests (`options & !SUPPORTED == 0 && options & MEMORY_SOURCE == 0 && !source.is_null()`) and `release` on `release_file()` / `Drop`. - Call `job.request.release_file()` in `work()` after `execute()` and before locking `lanes` / calling `lanes.finish()`, ensuring the COM refcount and OS file handle are released before status completion becomes visible to the title. - Add synthetic unit tests `releasing_file_after_enqueue_keeps_it_alive_until_read_completes` and `closing_queue_with_unsubmitted_file_read_releases_file_ref`. ## Verification - `cargo test -p victory-hle --locked dstorage -- --test-threads=1`: 16 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-hle --no-deps --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
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/621:upstream-pr/621'
git switch 'upstream-pr/621'

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