HLE: retain IDStorageFile references across queued DirectStorage read requests #2521

Closed
reeledMarek wants to merge 1 commit from upstream-pr/2523 into main
reeledMarek commented 2026-11-25 15:16:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T15:16:38Z.


Problem

dstorage::enqueue_request stored raw Request values without calling AddRef on file-backed request.source (Instance<File>), unlike enqueue_status which retains its StatusArray.

Under DirectStorage / COM rules, a caller may call Release() on an IDStorageFile immediately after enqueueing reads. Doing so drops Box<Instance<File>> and closes the underlying OS file handle while the request is still queued or running on a worker thread, causing a heap use-after-free.

Fix

  • Introduce ReadRequest wrapping Request + optional Instance<File> reference: ReadRequest::new calls add_ref_instance for non-null file-backed requests, and Drop releases the reference (covering completion, closed lanes, and queue teardown).
  • Release the file reference immediately after execute() finishes and before lanes.finish() publishes status completion so callers see the OS handle closed once status reports S_OK.

Testing

  • Added synthetic unit test queued_file_read_survives_caller_releasing_file in crates/victory-hle/src/modules/dstorage.rs.
  • Verified cargo test -p xtask --locked passes asset policy.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6607727646 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2523). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T15:16:38Z. --- ### Problem `dstorage::enqueue_request` stored raw `Request` values without calling `AddRef` on file-backed `request.source` (`Instance<File>`), unlike `enqueue_status` which retains its `StatusArray`. Under DirectStorage / COM rules, a caller may call `Release()` on an `IDStorageFile` immediately after enqueueing reads. Doing so drops `Box<Instance<File>>` and closes the underlying OS file handle while the request is still queued or running on a worker thread, causing a heap use-after-free. ### Fix - Introduce `ReadRequest` wrapping `Request` + optional `Instance<File>` reference: `ReadRequest::new` calls `add_ref_instance` for non-null file-backed requests, and `Drop` releases the reference (covering completion, closed lanes, and queue teardown). - Release the file reference immediately after `execute()` finishes and before `lanes.finish()` publishes status completion so callers see the OS handle closed once status reports `S_OK`. ### Testing - Added synthetic unit test `queued_file_read_survives_caller_releasing_file` in `crates/victory-hle/src/modules/dstorage.rs`. - Verified `cargo test -p xtask --locked` passes asset policy.
Marvisak commented 2026-11-25 16:09:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:53:25Z.


Automated triage for the maintainers. Needs work.

The change itself reads cleanly: ReadRequest in crates/victory-hle/src/modules/dstorage.rs takes a reference on the file in enqueue_request and drops it after execute or when the request is discarded. But it changes runtime behaviour on an assumption the code on main explicitly makes the other way (read_into: "a file object we created; the title keeps it open"), and the PR gives no evidence that the title, or the real runtime, releases a file while reads on it are still queued, or that queued requests keep the file alive. The new test asserts the new reference count rather than a documented behaviour. What would move this forward: a reference (public documentation of the queue's file lifetime rules, or a trace showing the title releasing a file with reads in flight). CI: the check job passes.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167403137 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2523#issuecomment-7167403137). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:53:25Z. --- <!-- victory-triage v1 head=352ac30bf12ee4dbad9361128921290b123661c9 verdict=needs-work at=2026-11-25T15:53:14Z --> Automated triage for the maintainers. Needs work. The change itself reads cleanly: `ReadRequest` in crates/victory-hle/src/modules/dstorage.rs takes a reference on the file in `enqueue_request` and drops it after `execute` or when the request is discarded. But it changes runtime behaviour on an assumption the code on main explicitly makes the other way (`read_into`: "a file object we created; the title keeps it open"), and the PR gives no evidence that the title, or the real runtime, releases a file while reads on it are still queued, or that queued requests keep the file alive. The new test asserts the new reference count rather than a documented behaviour. What would move this forward: a reference (public documentation of the queue's file lifetime rules, or a trace showing the title releasing a file with reads in flight). CI: the `check` job passes. --- _Generated by [Claude Code](https://claude.ai/code)_
reeledMarek commented 2026-11-25 20:06:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T20:05:31Z.


Closing per triage — checked Microsoft's DirectStorage documentation and confirmed the caller is required to keep IDStorageFile alive until queued requests complete, matching main's existing assumption. Thanks for the review!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7173178640 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2523#issuecomment-7173178640). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T20:05:31Z. --- Closing per triage — checked Microsoft's DirectStorage documentation and confirmed the caller is required to keep `IDStorageFile` alive until queued requests complete, matching `main`'s existing assumption. Thanks for the review!
upstream-sync closed this pull request 2026-11-25 21:02:31 +00:00

Pull request closed

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!2521
No description provided.