DStorage: preserve GetLastError across heap allocation in open_file and permit shared write/delete #1930

Closed
ashrafsdrop wants to merge 1 commit from upstream-pr/1932 into main
ashrafsdrop commented 2026-11-25 01:10:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ashrafsdrop on 2026-11-25T01:10:00Z.


Resolves VIC-BT-037 from #1249:

Summary

  1. Preserve OS Error Code:
    In \crates/victory-hle/src/modules/dstorage.rs:open_file, previously \String::from_utf16_lossy\ was executed before \std::io::Error::last_os_error()\ when \CreateFileW\ failed. Because UTF-16 decoding performs heap allocations which can make Win32 calls resetting or mutating the thread-local error state, \GetLastError()\ could be clobbered before being mapped by \ rom_win32. Captures \last_os_error()\ immediately after the failure check.

  2. Permit Concurrent Write and Delete Access:
    Adds \FILE_SHARE_WRITE\ and \FILE_SHARE_DELETE\ to \FILE_SHARE_READ\ in \CreateFileW. Matches Windows DirectStorage behavior permitting concurrent streaming or file replacements without encountering \ERROR_SHARING_VIOLATION.

  3. Tests:
    Adds unit test \open_file_preserves_error_and_allows_shared_access\ asserting that opening a missing file accurately returns \

<!-- greyfinch-sync:Marvisak/VIctory:issue:6594790655 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1932). Originally posted by [ashrafsdrop](https://github.com/ashrafsdrop) on 2026-11-25T01:10:00Z. --- Resolves VIC-BT-037 from #1249: ### Summary 1. **Preserve OS Error Code**: In \crates/victory-hle/src/modules/dstorage.rs:open_file\, previously \String::from_utf16_lossy\ was executed before \std::io::Error::last_os_error()\ when \CreateFileW\ failed. Because UTF-16 decoding performs heap allocations which can make Win32 calls resetting or mutating the thread-local error state, \GetLastError()\ could be clobbered before being mapped by \ rom_win32\. Captures \last_os_error()\ immediately after the failure check. 2. **Permit Concurrent Write and Delete Access**: Adds \FILE_SHARE_WRITE\ and \FILE_SHARE_DELETE\ to \FILE_SHARE_READ\ in \CreateFileW\. Matches Windows DirectStorage behavior permitting concurrent streaming or file replacements without encountering \ERROR_SHARING_VIOLATION\. 3. **Tests**: Adds unit test \open_file_preserves_error_and_allows_shared_access\ asserting that opening a missing file accurately returns \
catminou4 commented 2026-11-25 03:02:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T02:10:39Z.

Review: COMMENTED


Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff.

DStorage: preserve GetLastError across heap allocation: error-code preservation fix — classic Win32 bug class, good catch.

<!-- greyfinch-sync:Marvisak/VIctory:review:6050615016 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1932#pullrequestreview-6050615016). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T02:10:39Z. **Review:** COMMENTED --- Static review only: victory-hle is Windows-only — nothing compiles or runs on macOS; source review of the diff. DStorage: preserve GetLastError across heap allocation: error-code preservation fix — classic Win32 bug class, good catch.
AgneyaK commented 2026-11-25 03:03:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AgneyaK on 2026-11-25T02:27:54Z.


Tested ce91efa merged onto main 940d32f (clean), Windows 11 26300, Rust 1.98.1 MSVC.

  • The share-mode half is covered. With only this PR's test module on main's dstorage.rs, open_file_preserves_error_and_allows_shared_access fails at the first open with -2147024864 (0x80070020, ERROR_SHARING_VIOLATION): main opens with FILE_SHARE_READ only, and the test holds a write handle. With the PR it passes.
  • The GetLastError ordering half isn't really tested. The missing.bin assertion (0x80070002) is the result main gives too: from_utf16_lossy allocating didn't change the last error in this run. The reorder is still the safer order. It just isn't shown by a failing test.
  • Stable: cargo test -p victory-hle --lib --locked 5 runs, 144 passed each. cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean.

Not tested: the title. Its DirectStorage files are package archives opened read-only, so I'd expect no change there.

AI use: Claude Code (Claude Opus 5.5) ran these checks and wrote this; the results are from the runs described.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150756993 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1932#issuecomment-7150756993). Originally posted by [AgneyaK](https://github.com/AgneyaK) on 2026-11-25T02:27:54Z. --- Tested `ce91efa` merged onto `main` `940d32f` (clean), Windows 11 26300, Rust 1.98.1 MSVC. - **The share-mode half is covered.** With only this PR's test module on `main`'s `dstorage.rs`, `open_file_preserves_error_and_allows_shared_access` fails at the first open with `-2147024864` (`0x80070020`, `ERROR_SHARING_VIOLATION`): `main` opens with `FILE_SHARE_READ` only, and the test holds a write handle. With the PR it passes. - **The `GetLastError` ordering half isn't really tested.** The `missing.bin` assertion (`0x80070002`) is the result `main` gives too: `from_utf16_lossy` allocating didn't change the last error in this run. The reorder is still the safer order. It just isn't shown by a failing test. - **Stable:** `cargo test -p victory-hle --lib --locked` 5 runs, `144 passed` each. `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean. Not tested: the title. Its DirectStorage files are package archives opened read-only, so I'd expect no change there. AI use: Claude Code (Claude Opus 5.5) ran these checks and wrote this; the results are from the runs described.
Marvisak commented 2026-11-25 09:06:11 +00:00 (Migrated from github.com)

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


Automated triage for the maintainers.

Merge candidate. A small hardening change. Recommending a look, not approving.

  • crates/victory-hle/src/modules/dstorage.rs, open_file: the OS error is now read right after CreateFileW fails, before the path is decoded with String::from_utf16_lossy. The path is decoded once for both branches. The share mode is back to FILE_SHARE_READ alone, so the behaviour change from the earlier head is gone. By my reading the result is the same as main in every case seen so far, and only the order is safer.
  • The new test open_file_reports_missing_file_error checks that a missing file maps to 0x80070002. That also passes on main, so it guards the error mapping, not the reorder. Small nit: the test never releases the file object it opens, so the handle stays open until the process exits.

CI: check passing.

On your reply: both requested changes are in (error captured first with one decode, share-mode change dropped), so the verdict moves from needs-work.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7156858479 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1932#issuecomment-7156858479). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T08:18:11Z. --- <!-- victory-triage v1 head=9325572cca0dd6a4c5b0316d6d72a1de0b9d4113 verdict=merge-candidate at=2026-11-25T11:22:07Z --> Automated triage for the maintainers. **Merge candidate.** A small hardening change. Recommending a look, not approving. - `crates/victory-hle/src/modules/dstorage.rs`, `open_file`: the OS error is now read right after `CreateFileW` fails, before the path is decoded with `String::from_utf16_lossy`. The path is decoded once for both branches. The share mode is back to `FILE_SHARE_READ` alone, so the behaviour change from the earlier head is gone. By my reading the result is the same as `main` in every case seen so far, and only the order is safer. - The new test `open_file_reports_missing_file_error` checks that a missing file maps to `0x80070002`. That also passes on `main`, so it guards the error mapping, not the reorder. Small nit: the test never releases the file object it opens, so the handle stays open until the process exits. CI: `check` passing. On your reply: both requested changes are in (error captured first with one decode, share-mode change dropped), so the verdict moves from needs-work. --- _Generated by [Claude Code](https://claude.ai/code)_
ashrafsdrop commented 2026-11-25 11:05:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ashrafsdrop on 2026-11-25T11:03:13Z.


Updated in head 9325572:

  1. Immediate Error Capture & Single-Pass Decode:

    • let err = (handle as isize == -1).then(std::io::Error::last_os_error); captures the OS error immediately before path decoding, preventing heap allocations from clobbering thread-local error state.
    • Deduped the UTF-16 path decoding block to execute once for both error and success paths.
  2. Retain Read-Only Share Mode:

    • Dropped FILE_SHARE_WRITE | FILE_SHARE_DELETE, keeping FILE_SHARE_READ to prevent torn reads on title package archives.
  3. Rebased on Main:

    • Cleanly rebased onto latest origin/main (940d32f), picking up the GPU-less CI test runner fixes.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7160523948 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1932#issuecomment-7160523948). Originally posted by [ashrafsdrop](https://github.com/ashrafsdrop) on 2026-11-25T11:03:13Z. --- Updated in head `9325572`: 1. **Immediate Error Capture & Single-Pass Decode**: - `let err = (handle as isize == -1).then(std::io::Error::last_os_error);` captures the OS error immediately before path decoding, preventing heap allocations from clobbering thread-local error state. - Deduped the UTF-16 path decoding block to execute once for both error and success paths. 2. **Retain Read-Only Share Mode**: - Dropped `FILE_SHARE_WRITE | FILE_SHARE_DELETE`, keeping `FILE_SHARE_READ` to prevent torn reads on title package archives. 3. **Rebased on Main**: - Cleanly rebased onto latest `origin/main` (`940d32f`), picking up the GPU-less CI test runner fixes.
Marvisak commented 2026-11-25 22:04:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:04:33Z.


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 changes nothing observable: the allocator's HeapAlloc does not touch the last error on success, and the new test passes on main too, as noted in the thread. The share-mode change in the title is no longer part of the diff.

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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7175903110 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1932#issuecomment-7175903110). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:04:33Z. --- 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 changes nothing observable: the allocator's `HeapAlloc` does not touch the last error on success, and the new test passes on `main` too, as noted in the thread. The share-mode change in the title is no longer part of the diff. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.
upstream-sync closed this pull request 2026-11-25 23:03:26 +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!1930
No description provided.