Skip to content

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

Open
RUIZ-Enrique-25002136 wants to merge 2 commits into
Androtim:mainfrom
RUIZ-Enrique-25002136:fix/dstorage-file-uaf-and-leaks
Open

RUIZ-Enrique-25002136 wants to merge 2 commits into
Androtim:mainfrom
RUIZ-Enrique-25002136:fix/dstorage-file-uaf-and-leaks

Conversation

@RUIZ-Enrique-25002136

Copy link
Copy Markdown
Contributor

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)

@Androtim

Androtim commented Dec 2, 2026

Copy link
Copy Markdown
Owner

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 Androtim added the triage: needs work Triage found bugs, missing tests, or requested changes label Dec 2, 2026
@Androtim

Androtim commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Automated triage for the maintainers (not an approval).

Verdict: needs-work · CI (check): failing

Review Summary

The overall approach for #319 (retaining Instance<File> across queued/in-flight reads, cleaning up WALLS_FILES on final release, and rejecting null out pointers in the factory methods before allocating resources) is sound, but CI is currently failing due to a compile error in crates/victory-hle/src/modules/dstorage.rs, and there is one unsafe edge case in FileRef::retain.

Issues Found

  1. Compile error (E0428): duplicate FileRef definition (crates/victory-hle/src/modules/dstorage.rs)

    • origin/main already defines type FileRef = *const Instance<File>; at line 478 (used by file_close at line 483 and file_get_file_information at line 487).
    • This PR adds struct FileRef(*mut Instance<File>); in the same module scope, which causes error[E0428]: the name 'FileRef' is defined multiple times on x86_64-pc-windows-msvc CI.
    • Rename the new RAII guard (e.g., OwnedFileRef or RetainedFile) so it does not collide with the existing FileRef type alias.
  2. Unsafe cast in FileRef::retain when physical-page source (bit 23) is set (crates/victory-hle/src/modules/dstorage.rs)

    • FileRef::retain currently checks request.options & MEMORY_SOURCE != 0 || request.source.is_null(), then casts request.source to *mut Instance<File> and calls add_ref_instance(file).
    • Per the Request options comment in dstorage.rs (lines 1260–1261), bit 23 selects a source in physical pages rather than an Instance<File> pointer (and is rejected later during execution). If a request arrives with bit 23 set, retain would perform an atomic increment on a non-Instance<File> pointer and later call release_file on it.
    • Guard against bit 23 (e.g., const PAGE_SOURCE: u64 = 1 << 23; and return None when request.options & (MEMORY_SOURCE | PAGE_SOURCE) != 0).

To change the verdict:

  • Rename the new struct FileRef (e.g., to OwnedFileRef) to resolve the E0428 collision with type FileRef at crates/victory-hle/src/modules/dstorage.rs:478.
  • Guard retain against physical-page source requests (1 << 23) in addition to MEMORY_SOURCE and null source.
  • Ensure Windows CI (clippy and victory-hle unit tests) passes cleanly.

Generated with Antigravity CLI (agy).

@Androtim Androtim added the area: runtime HLE, threading, lifecycle, or OS runtime label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: runtime HLE, threading, lifecycle, or OS runtime triage: needs work Triage found bugs, missing tests, or requested changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(hle/dstorage): use-after-free of Instance<File> in queued requests and WALLS_FILES/factory resource leaks

2 participants