dstorage: retain queued file references and validate null out pointers #577

Closed
github-bot wants to merge 1 commit from gh-pr/577/head into gh-pr/577/base
Owner

@⁠DenRakEiw on GitHub · 2026-11-24 05:11 UTC · original
fix/dstorage-file-lifetime → main · +189 −21 · 1 files · 1 commits · closed without merging

Defensive COM lifetime hardening for crates/victory-hle/src/modules/dstorage.rs:

  • Retain queued file references (QueuedRead): When a request uses a supported file-backed option set (options & !SUPPORTED_OPTIONS == 0 && options & MEMORY_SOURCE == 0 && !source.is_null()), QueuedRead::new takes a COM reference (add_ref_instance) on the File object and releases it when the worker finishes reading (before signaling status completion) or if the queued read is discarded on Close. This defensively enforces standard DirectStorage lifetime guarantees so a title releasing its IDStorageFile handle while reads are in flight cannot cause a use-after-free, while guaranteeing unsupported option bits (such as bit 23 / physical-page sources) never treat request.source as a File object.
  • Null out validation: open_file, create_status_array, and create_queue reject null out pointers up front with E_INVALIDARG before allocating handles or scheduler lanes.
  • Tests: Added 4 unit tests covering release-after-enqueue completion, release-after-enqueue on queue close, null out rejection without leaks, and unsupported option bits (1 << 23) leaving request.source untouched and returning E_NOTIMPL.

Verified locally on Windows 11 x64 (Rust 1.98.1 MSVC):

  • cargo test -p victory-hle --lib dstorage:: — 18 passed, 0 failed
  • cargo clippy -p victory-hle --all-targets — 0 warnings
  • cargo xtask policy — ok

AI-assisted (Gemini 4 Neon).

**@⁠DenRakEiw** on GitHub · 2026-11-24 05:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/577) `fix/dstorage-file-lifetime` → `main` · +189 −21 · 1 files · 1 commits · closed without merging Defensive COM lifetime hardening for `crates/victory-hle/src/modules/dstorage.rs`: - **Retain queued file references (`QueuedRead`):** When a request uses a supported file-backed option set (`options & !SUPPORTED_OPTIONS == 0 && options & MEMORY_SOURCE == 0 && !source.is_null()`), `QueuedRead::new` takes a COM reference (`add_ref_instance`) on the `File` object and releases it when the worker finishes reading (before signaling status completion) or if the queued read is discarded on `Close`. This defensively enforces standard DirectStorage lifetime guarantees so a title releasing its `IDStorageFile` handle while reads are in flight cannot cause a use-after-free, while guaranteeing unsupported option bits (such as bit 23 / physical-page sources) never treat `request.source` as a `File` object. - **Null `out` validation:** `open_file`, `create_status_array`, and `create_queue` reject null `out` pointers up front with `E_INVALIDARG` before allocating handles or scheduler lanes. - **Tests:** Added 4 unit tests covering release-after-enqueue completion, release-after-enqueue on queue close, null `out` rejection without leaks, and unsupported option bits (`1 << 23`) leaving `request.source` untouched and returning `E_NOTIMPL`. Verified locally on Windows 11 x64 (Rust 1.98.1 MSVC): - `cargo test -p victory-hle --lib dstorage::` — 18 passed, 0 failed - `cargo clippy -p victory-hle --all-targets` — 0 warnings - `cargo xtask policy` — ok AI-assisted (Gemini 4 Neon).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:09:01 +00:00
github-bot closed this pull request 2026-11-26 04:09:01 +00:00
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 12:17 UTC · original

Independent verification & code review on Windows 11 x64 (merged onto main ffe310e):

Checks Run

  • cargo xtask policy: PASS (154 files ok)
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings (default + --features diagnostics): PASS (0 warnings)
  • cargo test -p victory-hle --locked: PASS (97/97 unit tests + 1/1 no_dll_planting, including all 16/16 dstorage tests)

Review Findings

The QueuedRead RAII ref-counting (including releasing the file outside the scheduler mutex before lanes.finish() signals the status array) and the upfront out.is_null() checks in open_file / create_status_array / create_queue look solid and cleanly fix the UAF and leak paths.

One real safety edge case to tighten before merge:

  1. QueuedRead::new dereferences request.source as Instance<File> even for unsupported source option bits (crates/victory-hle/src/modules/dstorage.rs:709-716):
    • Currently QueuedRead::new calls add_ref_instance(file) whenever request.options & MEMORY_SOURCE == 0 && !request.source.is_null().
    • However, execute() (line 843) rejects unsupported option bits with E_NOTIMPL — including bit 23 (physical-pages source, documented at line 691), where request.source points to a page-number array rather than an Instance<File> COM object.
    • On main, execute() returned E_NOTIMPL without ever touching request.source. Here, EnqueueRequest now calls add_ref_instance (atomic increment at offset +8) on request.source before execute() can reject it, which can corrupt caller memory or fault.
    • Fix: Guard QueuedRead::new with the same supported-options mask as execute() (e.g. request.options & !(ZLIB | BCPACK | PAGE_DESTINATION | MEMORY_SOURCE) == 0 && request.options & MEMORY_SOURCE == 0 && !request.source.is_null()), so requests that execute() will reject with E_NOTIMPL never touch request.source.
  2. (Minor) There is a double blank line after impl Drop for QueuedRead (lines 730–731).

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 12:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/577#issuecomment-7131228075) Independent verification & code review on Windows 11 x64 (merged onto `main` `ffe310e`): ### Checks Run - `cargo xtask policy`: **PASS** (`154 files ok`) - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings` (default + `--features diagnostics`): **PASS** (0 warnings) - `cargo test -p victory-hle --locked`: **PASS** (`97/97` unit tests + `1/1` `no_dll_planting`, including all `16/16` `dstorage` tests) ### Review Findings The `QueuedRead` RAII ref-counting (including releasing the file outside the scheduler mutex before `lanes.finish()` signals the status array) and the upfront `out.is_null()` checks in `open_file` / `create_status_array` / `create_queue` look solid and cleanly fix the UAF and leak paths. One real safety edge case to tighten before merge: 1. **`QueuedRead::new` dereferences `request.source` as `Instance<File>` even for unsupported source option bits (`crates/victory-hle/src/modules/dstorage.rs:709-716`)**: - Currently `QueuedRead::new` calls `add_ref_instance(file)` whenever `request.options & MEMORY_SOURCE == 0 && !request.source.is_null()`. - However, `execute()` (line 843) rejects unsupported option bits with `E_NOTIMPL` — including **bit 23** (physical-pages source, documented at line 691), where `request.source` points to a page-number array rather than an `Instance<File>` COM object. - On `main`, `execute()` returned `E_NOTIMPL` without ever touching `request.source`. Here, `EnqueueRequest` now calls `add_ref_instance` (atomic increment at offset `+8`) on `request.source` before `execute()` can reject it, which can corrupt caller memory or fault. - **Fix**: Guard `QueuedRead::new` with the same supported-options mask as `execute()` (e.g. `request.options & !(ZLIB | BCPACK | PAGE_DESTINATION | MEMORY_SOURCE) == 0 && request.options & MEMORY_SOURCE == 0 && !request.source.is_null()`), so requests that `execute()` will reject with `E_NOTIMPL` never touch `request.source`. 2. *(Minor)* There is a double blank line after `impl Drop for QueuedRead` (lines 730–731). *AI-assisted; synthetic tests only, no game files.*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:28 UTC · original · edited 2026-11-25 03:26 UTC

Automated triage for the maintainers.

Merge candidate. In crates/victory-hle/src/modules/dstorage.rs, QueuedRead holds a reference on a request's File from EnqueueRequest until the worker finishes the read, or until Close discards it, and the worker releases it before finish signals the status array. A shared SUPPORTED_OPTIONS mask now gates both QueuedRead::new and execute, so unsupported option bits such as bit 23 never touch source; unsupported_options_do_not_touch_source_as_file covers this. The null-out checks in open_file, create_status_array and create_queue, and the null check in read_into, are harmless. CI passes.

On your reply: the blocking issue is fixed as described, and the description now calls the change defensive, which settles the evidence question.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/577#issuecomment-7149582818) · edited 2026-11-25 03:26 UTC <!-- victory-triage v1 head=07b7e20c5fc7a193b60a1b48aab63a5fbb5ef502 verdict=merge-candidate at=2026-11-25T03:26:40Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-hle/src/modules/dstorage.rs`, `QueuedRead` holds a reference on a request's `File` from `EnqueueRequest` until the worker finishes the read, or until `Close` discards it, and the worker releases it before `finish` signals the status array. A shared `SUPPORTED_OPTIONS` mask now gates both `QueuedRead::new` and `execute`, so unsupported option bits such as bit 23 never touch `source`; `unsupported_options_do_not_touch_source_as_file` covers this. The null-`out` checks in `open_file`, `create_status_array` and `create_queue`, and the null check in `read_into`, are harmless. CI passes. On your reply: the blocking issue is fixed as described, and the description now calls the change defensive, which settles the evidence question. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠DenRakEiw on GitHub · 2026-11-25 02:57 UTC · original

Addressed all triage and review feedback (rebased onto origin/main 940d32f):

  1. Blocking fix (SUPPORTED_OPTIONS guard): Extracted const SUPPORTED_OPTIONS: u64 = ZLIB | BCPACK | PAGE_DESTINATION | MEMORY_SOURCE and shared it between QueuedRead::new and execute. QueuedRead::new now only calls add_ref_instance when request.options & !SUPPORTED_OPTIONS == 0 && request.options & MEMORY_SOURCE == 0 && !request.source.is_null(), so unsupported option bits (e.g. bit 23 physical-page sources) never touch request.source.
  2. Regression test: Added unsupported_options_do_not_touch_source_as_file verifying that a bit-23 request leaves a sentinel source buffer untouched and fails with E_NOTIMPL.
  3. Cleanup & framing: Removed the stray blank line after impl Drop for QueuedRead, updated count() for job.read.request.options after rebase, and clarified in the PR description that this is defensive COM lifetime handling per DirectStorage semantics.

Re-verified locally: cargo test -p victory-hle --lib dstorage:: (18/18 passed), cargo clippy -p victory-hle --all-targets (0 warnings), cargo xtask policy (ok).

AI-assisted (Gemini 4 Neon).

**@⁠DenRakEiw** on GitHub · 2026-11-25 02:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/577#issuecomment-7151416769) Addressed all triage and review feedback (rebased onto `origin/main` `940d32f`): 1. **Blocking fix (`SUPPORTED_OPTIONS` guard):** Extracted `const SUPPORTED_OPTIONS: u64 = ZLIB | BCPACK | PAGE_DESTINATION | MEMORY_SOURCE` and shared it between `QueuedRead::new` and `execute`. `QueuedRead::new` now only calls `add_ref_instance` when `request.options & !SUPPORTED_OPTIONS == 0 && request.options & MEMORY_SOURCE == 0 && !request.source.is_null()`, so unsupported option bits (e.g. bit 23 physical-page sources) never touch `request.source`. 2. **Regression test:** Added `unsupported_options_do_not_touch_source_as_file` verifying that a bit-23 request leaves a sentinel source buffer untouched and fails with `E_NOTIMPL`. 3. **Cleanup & framing:** Removed the stray blank line after `impl Drop for QueuedRead`, updated `count()` for `job.read.request.options` after rebase, and clarified in the PR description that this is defensive COM lifetime handling per DirectStorage semantics. Re-verified locally: `cargo test -p victory-hle --lib dstorage::` (18/18 passed), `cargo clippy -p victory-hle --all-targets` (0 warnings), `cargo xtask policy` (ok). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 21:58 UTC · original

Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because it no longer builds on main and fixes a case the title does not produce. Lanes::submit_as gained its physical-page hold logic (eddde734) after this branch was cut, and the merged build fails with three errors (r.options on a Command::Read that now holds a QueuedRead, and two newer tests that read job.request). The title never closes or releases its DirectStorage file objects and always passes out pointers to the factory, so the reference counting would rewire the scheduler's types for something it never does. It also collides with #1457 in Lanes::start.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 21:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/577#issuecomment-7175786303) Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing because it no longer builds on `main` and fixes a case the title does not produce. `Lanes::submit_as` gained its physical-page hold logic (eddde734) after this branch was cut, and the merged build fails with three errors (`r.options` on a `Command::Read` that now holds a `QueuedRead`, and two newer tests that read `job.request`). The title never closes or releases its DirectStorage file objects and always passes out pointers to the factory, so the reference counting would rewire the scheduler's types for something it never does. It also collides with #1457 in `Lanes::start`. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

Pull request closed

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/Marvisak--VIctory!577
No description provided.