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

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

@⁠AscilCH on GitHub · 2026-11-25 03:25 UTC · original
AscilCH:fix/hle-dstorage-retain-file-ref-on-enqueue → main · +120 −19 · 1 files · 1 commits · open

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

**@⁠AscilCH** on GitHub · 2026-11-25 03:25 UTC · [original](https://github.com/fxceless/victory-fork/pull/621) `AscilCH:fix/hle-dstorage-retain-file-ref-on-enqueue` → `main` · +120 −19 · 1 files · 1 commits · open ## 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.)*
`enqueue_status` retained a COM reference on `IDStorageStatusArray` via `Status`,
but `enqueue_request` stored raw `Request` structs without retaining `source`
when backed by an `IDStorageFile`. If a title called `IDStorageFile::Release()`
after `EnqueueRequest` (standard DirectStorage lifetime semantics), the worker
dereferenced a freed `Instance<File>` and read from a closed OS handle.

- Add RAII wrapper `QueuedRead(Request)` calling `add_ref_instance` / `release`
  on `Instance<File>` for supported file-backed requests.
- Drop `Job` (releasing the file ref and closing the OS handle if last ref)
  before signalling status completion to the title in `work()`.
- Add synthetic unit tests covering `Release()` before `Submit()` and queue
  `Close()` with unsubmitted file reads.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:47:34 +00:00
This pull request has changes conflicting with the target branch.
  • crates/victory-hle/src/modules/dstorage.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-621'
git switch 'github-bot/gh-621'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-621'
git switch 'github-bot/gh-621'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-621'
git switch 'main'
git merge --squash 'github-bot/gh-621'
git switch 'main'
git merge --ff-only 'github-bot/gh-621'
git switch 'main'
git merge 'github-bot/gh-621'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.