fix(hle/dstorage): retain Instance<File> for queued reads and fix WALLS_FILES/factory leaks (#319) #338

Manually merged
github merged 3 commits from github-bot/gh-338 into gh-pr/338/base 2026-12-02 15:06:52 +00:00 AGit
Owner

@⁠RUIZ-Enrique-25002136 on GitHub · 2026-12-02 13:28 UTC · original
RUIZ-Enrique-25002136:fix/dstorage-file-uaf-and-leaks → main · +173 −14 · 1 files · 3 commits · merged 2026-12-02 15:03 UTC

Summary

Fixes both the queued Instance<File> use-after-free and the WALLS_FILES / factory resource leaks in crates/victory-hle/src/modules/dstorage.rs tracked in #319.

Fixes #319

Changes

  1. Prevent Instance<File> use-after-free in queued/in-flight reads:
    • Added release_file(this: *mut Instance<File>) -> u32 (wired into FILE_VTABLE slot 2) and RAII struct FileRef(*mut Instance<File>) whose retain(request) increments the COM refcount (add_ref_instance) when request.options & MEMORY_SOURCE == 0 && !request.source.is_null() and whose Drop calls release_file.
    • Added _file: Option<FileRef> to Read (removing Clone, Copy) and Job, retained in enqueue_request, moved from Read to Job in Lanes::start, and automatically released on job completion or queue close_lane.
  2. Eliminate WALLS_FILES unbounded growth & address aliasing:
    • Changed WALLS_FILES from Mutex<Vec<(usize, String)>> to Mutex<Option<HashMap<usize, String>>>.
    • Only inserts into WALLS_FILES when walls_trace() > 0, and removes the entry in release_file when refs hits 0 before freeing Box::from_raw(this).
  3. Prevent factory resource leaks on null out:
    • Added if out.is_null() { return E_POINTER; } at the entry of open_file, create_status_array, and create_queue before opening file handles, allocating Instances, creating Win32 CreateEventW handles, or inserting Lanes.
  4. Unit tests:
    • Added factory_rejects_null_out_with_e_pointer and queued_read_retains_file_refcount_until_dropped.

Rig Specs Tested On

  • CPU: Linux workstation (x86_64)
  • GPU & VRAM: N/A (Windows-only victory-hle crate; full clippy & tests verified on GitHub Actions windows-latest CI)
  • System RAM: N/A
  • OS & GPU Driver: Linux (x86_64) — Windows x86_64-pc-windows-msvc CI verifies compilation, clippy, and unit tests

Run Settings & Profile

  • Build & Commit SHA: rebased on origin/main (b0842d9)
  • Console Profile & Internal Resolution / Scale: N/A
  • Ray Tracing: N/A
  • Upscaling: N/A
  • Frame Generation: N/A
  • Launcher Command / Preset & Extra VICTORY_* Overrides: N/A
  • Scene / Area & Session Length: N/A

Measurements & Verification

  • Base FPS (without Frame Gen): N/A
  • Frame-Gen FPS (if enabled): N/A
  • VRAM / RAM Usage: N/A
  • Crashes / DRED Resets: N/A
  • Automated Checks:
    • cargo clippy --workspace --all-targets --locked -- -D warnings (verified on GitHub Actions windows-latest CI)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (verified on GitHub Actions windows-latest CI)
    • cargo test --locked -p xtask -> 8 passed, 0 failed
    • cargo xtask policy -> asset policy: 324 files ok
    • cargo test --workspace --exclude victory-gpu --exclude victory-shader --locked (verified on GitHub Actions windows-latest CI)
**@⁠RUIZ-Enrique-25002136** on GitHub · 2026-12-02 13:28 UTC · [original](https://github.com/Androtim/MiniVI/pull/338) `RUIZ-Enrique-25002136:fix/dstorage-file-uaf-and-leaks` → `main` · +173 −14 · 1 files · 3 commits · merged 2026-12-02 15:03 UTC ## Summary Fixes both the queued `Instance<File>` use-after-free and the `WALLS_FILES` / factory resource leaks in `crates/victory-hle/src/modules/dstorage.rs` tracked in #319. Fixes #319 ### Changes 1. **Prevent `Instance<File>` use-after-free in queued/in-flight reads:** - Added `release_file(this: *mut Instance<File>) -> u32` (wired into `FILE_VTABLE` slot 2) and RAII `struct FileRef(*mut Instance<File>)` whose `retain(request)` increments the COM refcount (`add_ref_instance`) when `request.options & MEMORY_SOURCE == 0 && !request.source.is_null()` and whose `Drop` calls `release_file`. - Added `_file: Option<FileRef>` to `Read` (removing `Clone, Copy`) and `Job`, retained in `enqueue_request`, moved from `Read` to `Job` in `Lanes::start`, and automatically released on job completion or queue `close_lane`. 2. **Eliminate `WALLS_FILES` unbounded growth & address aliasing:** - Changed `WALLS_FILES` from `Mutex<Vec<(usize, String)>>` to `Mutex<Option<HashMap<usize, String>>>`. - Only inserts into `WALLS_FILES` when `walls_trace() > 0`, and removes the entry in `release_file` when `refs` hits `0` before freeing `Box::from_raw(this)`. 3. **Prevent factory resource leaks on null `out`:** - Added `if out.is_null() { return E_POINTER; }` at the entry of `open_file`, `create_status_array`, and `create_queue` before opening file handles, allocating `Instance`s, creating Win32 `CreateEventW` handles, or inserting `Lane`s. 4. **Unit tests:** - Added `factory_rejects_null_out_with_e_pointer` and `queued_read_retains_file_refcount_until_dropped`. ## Rig Specs Tested On - **CPU:** Linux workstation (`x86_64`) - **GPU & VRAM:** N/A (Windows-only `victory-hle` crate; full clippy & tests verified on GitHub Actions `windows-latest` CI) - **System RAM:** N/A - **OS & GPU Driver:** Linux (`x86_64`) — Windows `x86_64-pc-windows-msvc` CI verifies compilation, clippy, and unit tests ## Run Settings & Profile - **Build & Commit SHA:** rebased on `origin/main` (`b0842d9`) - **Console Profile & Internal Resolution / Scale:** N/A - **Ray Tracing:** N/A - **Upscaling:** N/A - **Frame Generation:** N/A - **Launcher Command / Preset & Extra `VICTORY_*` Overrides:** N/A - **Scene / Area & Session Length:** N/A ## Measurements & Verification - **Base FPS (without Frame Gen):** N/A - **Frame-Gen FPS (if enabled):** N/A - **VRAM / RAM Usage:** N/A - **Crashes / DRED Resets:** N/A - **Automated Checks:** - [ ] `cargo clippy --workspace --all-targets --locked -- -D warnings` (verified on GitHub Actions `windows-latest` CI) - [ ] `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (verified on GitHub Actions `windows-latest` CI) - [x] `cargo test --locked -p xtask` -> 8 passed, 0 failed - [x] `cargo xtask policy` -> `asset policy: 324 files ok` - [ ] `cargo test --workspace --exclude victory-gpu --exclude victory-shader --locked` (verified on GitHub Actions `windows-latest` CI)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 13:30:03 +00:00
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 13:34 UTC · original

CI run 46891843287 failed to compile victory-hle due to a type-name collision in crates/victory-hle/src/modules/dstorage.rs:

  • dstorage.rs:126: newly added struct FileRef(*mut Instance<File>); collides with existing type FileRef = *const Instance<File>; at dstorage.rs:529 (E0428, causing E0599 / E0614 at lines 542 and 546).

Renaming the RAII guard to e.g. struct RetainedFile(*mut Instance<File>); should fix all three errors cleanly. The rest of the diff (release_file removing from WALLS_FILES, walls_trace() > 0 gate, out.is_null() -> E_POINTER guards, and queued-read refcount retention + tests) looks solid — once renamed and CI is green, this is ready to merge!

**@⁠Androtim** on GitHub · 2026-12-02 13:34 UTC · [original](https://github.com/Androtim/MiniVI/pull/338#issuecomment-7347096055) CI run `46891843287` failed to compile `victory-hle` due to a type-name collision in `crates/victory-hle/src/modules/dstorage.rs`: - `dstorage.rs:126`: newly added `struct FileRef(*mut Instance<File>);` collides with existing `type FileRef = *const Instance<File>;` at `dstorage.rs:529` (`E0428`, causing `E0599` / `E0614` at lines 542 and 546). Renaming the RAII guard to e.g. `struct RetainedFile(*mut Instance<File>);` should fix all three errors cleanly. The rest of the diff (`release_file` removing from `WALLS_FILES`, `walls_trace() > 0` gate, `out.is_null() -> E_POINTER` guards, and queued-read refcount retention + tests) looks solid — once renamed and CI is green, this is ready to merge!
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 14:05 UTC · original · edited 2026-12-02 15:15 UTC

Automated triage for the maintainers (not an approval).

Verdict: merge-candidate · CI (check): passing

Review Summary

Clean, focused fix in crates/victory-hle/src/modules/dstorage.rs addressing queued Instance<File> lifetime safety, WALLS_FILES growth/aliasing, and factory null-out leaks (#319). CI is passing.

Verified Changes

  • Queued/in-flight Instance<File> retention (RetainedFile, release_file, FILE_VTABLE):
    • RetainedFile::retain increments the COM refcount via add_ref_instance in enqueue_request for file-backed requests (skipping null, MEMORY_SOURCE, and PAGE_SOURCE sources), moves ownership from Read into Job in Lanes::start, and releases via release_file on Drop.
    • Covers all drop paths cleanly: normal completion (after Lanes::finish / ErrorRecord::of finishes reading &job.request), queue teardown in close_lane (unsubmitted.clear() / reads.clear()), and early return when queue sees a closed lane or unavailable scheduler.
    • ErrorRecord::of and walls_request are also updated to handle PAGE_SOURCE (1 << 23) without treating request.source as an Instance<File>.
  • WALLS_FILES lifecycle (open_file, release_file, walls_request):
    • Switched from Mutex<Vec<(usize, String)>> to Mutex<Option<HashMap<usize, String>>>, gated by walls_trace() > 0 (OnceLock, so insert/remove gating is consistent).
    • release_file removes the address from WALLS_FILES before Box::from_raw(this), preventing address-reuse aliasing and avoiding lock-order inversion with scheduler.lock().
  • Factory null-out guards (open_file, create_status_array, create_queue):
    • Early if out.is_null() { return E_POINTER; } checks run before opening file handles, creating Instances, creating Win32 events (CreateEventW), or inserting Lanes.
  • Tests:
    • factory_rejects_null_out_with_e_pointer, queued_read_retains_file_refcount_until_dropped, and retain_ignores_memory_page_and_null_sources cover the new invariants.

Verdict

merge-candidate — CPU-side HLE/COM lifetime and leak fix with passing CI and unit tests; no real-GPU smoke test required. (Independent technical review, not formal maintainer approval.)


Generated with Antigravity CLI (agy).

**@⁠Androtim** on GitHub · 2026-12-02 14:05 UTC · [original](https://github.com/Androtim/MiniVI/pull/338#issuecomment-7347970830) · edited 2026-12-02 15:15 UTC <!-- minivi-triage v1 head=76f31089d9e09b1378fe45cb5f31d6b3ab575a5c verdict=merge-candidate ci=passing at=2026-12-02T15:16:20Z --> Automated triage for the maintainers (not an approval). **Verdict:** `merge-candidate` · **CI (`check`):** `passing` ### Review Summary Clean, focused fix in `crates/victory-hle/src/modules/dstorage.rs` addressing queued `Instance<File>` lifetime safety, `WALLS_FILES` growth/aliasing, and factory null-`out` leaks (#319). CI is passing. ### Verified Changes - **Queued/in-flight `Instance<File>` retention (`RetainedFile`, `release_file`, `FILE_VTABLE`):** - `RetainedFile::retain` increments the COM refcount via `add_ref_instance` in `enqueue_request` for file-backed requests (skipping null, `MEMORY_SOURCE`, and `PAGE_SOURCE` sources), moves ownership from `Read` into `Job` in `Lanes::start`, and releases via `release_file` on `Drop`. - Covers all drop paths cleanly: normal completion (after `Lanes::finish` / `ErrorRecord::of` finishes reading `&job.request`), queue teardown in `close_lane` (`unsubmitted.clear()` / `reads.clear()`), and early return when `queue` sees a closed lane or unavailable scheduler. - `ErrorRecord::of` and `walls_request` are also updated to handle `PAGE_SOURCE` (`1 << 23`) without treating `request.source` as an `Instance<File>`. - **`WALLS_FILES` lifecycle (`open_file`, `release_file`, `walls_request`):** - Switched from `Mutex<Vec<(usize, String)>>` to `Mutex<Option<HashMap<usize, String>>>`, gated by `walls_trace() > 0` (`OnceLock`, so insert/remove gating is consistent). - `release_file` removes the address from `WALLS_FILES` *before* `Box::from_raw(this)`, preventing address-reuse aliasing and avoiding lock-order inversion with `scheduler.lock()`. - **Factory null-`out` guards (`open_file`, `create_status_array`, `create_queue`):** - Early `if out.is_null() { return E_POINTER; }` checks run before opening file handles, creating `Instance`s, creating Win32 events (`CreateEventW`), or inserting `Lane`s. - **Tests:** - `factory_rejects_null_out_with_e_pointer`, `queued_read_retains_file_refcount_until_dropped`, and `retain_ignores_memory_page_and_null_sources` cover the new invariants. ### Verdict `merge-candidate` — CPU-side HLE/COM lifetime and leak fix with passing CI and unit tests; no real-GPU smoke test required. (Independent technical review, not formal maintainer approval.) --- _Generated with Antigravity CLI (`agy`)._
Author
Owner

@⁠Androtim on GitHub · 2026-12-02 14:55 UTC · original

Fixed the review/CI issues on this branch:

  • Renamed the new RAII guard from FileRef to RetainedFile to eliminate the E0428 collision with type FileRef = *const Instance<File>;.
  • Added PAGE_SOURCE = 1 << 23 guard in RetainedFile::retain, ErrorRecord::of, and walls_request so physical-page source requests never dereference request.source as Instance<File>.
  • Added unit test retain_ignores_memory_page_and_null_sources.

Verified locally:

  • cargo +1.98.1-x86_64-pc-windows-gnu xtask policy -> 324 files ok
  • cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-hle dstorage --locked -> 34 passed; 0 failed; 1 ignored
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings -> pass
  • cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics --locked -- -D warnings -> pass
**@⁠Androtim** on GitHub · 2026-12-02 14:55 UTC · [original](https://github.com/Androtim/MiniVI/pull/338#issuecomment-7349369685) Fixed the review/CI issues on this branch: - Renamed the new RAII guard from `FileRef` to `RetainedFile` to eliminate the `E0428` collision with `type FileRef = *const Instance<File>;`. - Added `PAGE_SOURCE = 1 << 23` guard in `RetainedFile::retain`, `ErrorRecord::of`, and `walls_request` so physical-page source requests never dereference `request.source` as `Instance<File>`. - Added unit test `retain_ignores_memory_page_and_null_sources`. Verified locally: - `cargo +1.98.1-x86_64-pc-windows-gnu xtask policy` -> `324 files ok` - `cargo +1.98.1-x86_64-pc-windows-gnu test -p victory-hle dstorage --locked` -> `34 passed; 0 failed; 1 ignored` - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --locked -- -D warnings` -> pass - `cargo +1.98.1-x86_64-pc-windows-gnu clippy --workspace --all-targets --features diagnostics --locked -- -D warnings` -> pass
github-bot changed target branch from main to gh-pr/338/base 2026-12-02 15:06:49 +00:00
github manually merged commit 76f31089d9 into gh-pr/338/base 2026-12-02 15:06:52 +00:00
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/Androtim--MiniVI!338
No description provided.